Revision: miles@gnu.org--gnu-2005/gnus--devo--0--patch-182
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'cl)
32   (defvar tool-bar-map))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 (require 'mm-decode)
42 (require 'nnoo)
43
44 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
45 (autoload 'gnus-cache-write-active "gnus-cache")
46 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
47 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
48 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
49 (autoload 'mm-uu-dissect "mm-uu")
50 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
51   "Deuglify broken Outlook (Express) articles and redisplay."
52   t)
53 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
54 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
55 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
56
57 (defcustom gnus-kill-summary-on-exit t
58   "*If non-nil, kill the summary buffer when you exit from it.
59 If nil, the summary will become a \"*Dead Summary*\" buffer, and
60 it will be killed sometime later."
61   :group 'gnus-summary-exit
62   :type 'boolean)
63
64 (defcustom gnus-fetch-old-headers nil
65   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
66 If an unread article in the group refers to an older, already read (or
67 just marked as read) article, the old article will not normally be
68 displayed in the Summary buffer.  If this variable is t, Gnus
69 will attempt to grab the headers to the old articles, and thereby
70 build complete threads.  If it has the value `some', only enough
71 headers to connect otherwise loose threads will be displayed.  This
72 variable can also be a number.  In that case, no more than that number
73 of old headers will be fetched.  If it has the value `invisible', all
74 old headers will be fetched, but none will be displayed.
75
76 The server has to support NOV for any of this to work."
77   :group 'gnus-thread
78   :type '(choice (const :tag "off" nil)
79                  (const :tag "on" t)
80                  (const some)
81                  (const invisible)
82                  number
83                  (sexp :menu-tag "other" t)))
84
85 (defcustom gnus-refer-thread-limit 200
86   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
87 If t, fetch all the available old headers."
88   :group 'gnus-thread
89   :type '(choice number
90                  (sexp :menu-tag "other" t)))
91
92 (defcustom gnus-summary-make-false-root 'adopt
93   "*nil means that Gnus won't gather loose threads.
94 If the root of a thread has expired or been read in a previous
95 session, the information necessary to build a complete thread has been
96 lost.  Instead of having many small sub-threads from this original thread
97 scattered all over the summary buffer, Gnus can gather them.
98
99 If non-nil, Gnus will try to gather all loose sub-threads from an
100 original thread into one large thread.
101
102 If this variable is non-nil, it should be one of `none', `adopt',
103 `dummy' or `empty'.
104
105 If this variable is `none', Gnus will not make a false root, but just
106 present the sub-threads after another.
107 If this variable is `dummy', Gnus will create a dummy root that will
108 have all the sub-threads as children.
109 If this variable is `adopt', Gnus will make one of the \"children\"
110 the parent and mark all the step-children as such.
111 If this variable is `empty', the \"children\" are printed with empty
112 subject fields.  (Or rather, they will be printed with a string
113 given by the `gnus-summary-same-subject' variable.)"
114   :group 'gnus-thread
115   :type '(choice (const :tag "off" nil)
116                  (const none)
117                  (const dummy)
118                  (const adopt)
119                  (const empty)))
120
121 (defcustom gnus-summary-make-false-root-always nil
122   "Always make a false dummy root."
123   :version "22.1"
124   :group 'gnus-thread
125   :type 'boolean)
126
127 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
128   "*A regexp to match subjects to be excluded from loose thread gathering.
129 As loose thread gathering is done on subjects only, that means that
130 there can be many false gatherings performed.  By rooting out certain
131 common subjects, gathering might become saner."
132   :group 'gnus-thread
133   :type 'regexp)
134
135 (defcustom gnus-summary-gather-subject-limit nil
136   "*Maximum length of subject comparisons when gathering loose threads.
137 Use nil to compare full subjects.  Setting this variable to a low
138 number will help gather threads that have been corrupted by
139 newsreaders chopping off subject lines, but it might also mean that
140 unrelated articles that have subject that happen to begin with the
141 same few characters will be incorrectly gathered.
142
143 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
144 comparing subjects."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const fuzzy)
148                  (sexp :menu-tag "on" t)))
149
150 (defcustom gnus-simplify-subject-functions nil
151   "List of functions taking a string argument that simplify subjects.
152 The functions are applied recursively.
153
154 Useful functions to put in this list include:
155 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
156 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
157   :group 'gnus-thread
158   :type '(repeat function))
159
160 (defcustom gnus-simplify-ignored-prefixes nil
161   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
162   :group 'gnus-thread
163   :type '(choice (const :tag "off" nil)
164                  regexp))
165
166 (defcustom gnus-build-sparse-threads nil
167   "*If non-nil, fill in the gaps in threads.
168 If `some', only fill in the gaps that are needed to tie loose threads
169 together.  If `more', fill in all leaf nodes that Gnus can find.  If
170 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
171   :group 'gnus-thread
172   :type '(choice (const :tag "off" nil)
173                  (const some)
174                  (const more)
175                  (sexp :menu-tag "all" t)))
176
177 (defcustom gnus-summary-thread-gathering-function
178   'gnus-gather-threads-by-subject
179   "*Function used for gathering loose threads.
180 There are two pre-defined functions: `gnus-gather-threads-by-subject',
181 which only takes Subjects into consideration; and
182 `gnus-gather-threads-by-references', which compared the References
183 headers of the articles to find matches."
184   :group 'gnus-thread
185   :type '(radio (function-item gnus-gather-threads-by-subject)
186                 (function-item gnus-gather-threads-by-references)
187                 (function :tag "other")))
188
189 (defcustom gnus-summary-same-subject ""
190   "*String indicating that the current article has the same subject as the previous.
191 This variable will only be used if the value of
192 `gnus-summary-make-false-root' is `empty'."
193   :group 'gnus-summary-format
194   :type 'string)
195
196 (defcustom gnus-summary-goto-unread t
197   "*If t, many commands will go to the next unread article.
198 This applies to marking commands as well as other commands that
199 \"naturally\" select the next article, like, for instance, `SPC' at
200 the end of an article.
201
202 If nil, the marking commands do NOT go to the next unread article
203 \(they go to the next article instead).  If `never', commands that
204 usually go to the next unread article, will go to the next article,
205 whether it is read or not."
206   :group 'gnus-summary-marks
207   :link '(custom-manual "(gnus)Setting Marks")
208   :type '(choice (const :tag "off" nil)
209                  (const never)
210                  (sexp :menu-tag "on" t)))
211
212 (defcustom gnus-summary-default-score 0
213   "*Default article score level.
214 All scores generated by the score files will be added to this score.
215 If this variable is nil, scoring will be disabled."
216   :group 'gnus-score-default
217   :type '(choice (const :tag "disable")
218                  integer))
219
220 (defcustom gnus-summary-default-high-score 0
221   "*Default threshold for a high scored article.
222 An article will be highlighted as high scored if its score is greater
223 than this score."
224   :version "22.1"
225   :group 'gnus-score-default
226   :type 'integer)
227
228 (defcustom gnus-summary-default-low-score 0
229   "*Default threshold for a low scored article.
230 An article will be highlighted as low scored if its score is smaller
231 than this score."
232   :version "22.1"
233   :group 'gnus-score-default
234   :type 'integer)
235
236 (defcustom gnus-summary-zcore-fuzz 0
237   "*Fuzziness factor for the zcore in the summary buffer.
238 Articles with scores closer than this to `gnus-summary-default-score'
239 will not be marked."
240   :group 'gnus-summary-format
241   :type 'integer)
242
243 (defcustom gnus-simplify-subject-fuzzy-regexp nil
244   "*Strings to be removed when doing fuzzy matches.
245 This can either be a regular expression or list of regular expressions
246 that will be removed from subject strings if fuzzy subject
247 simplification is selected."
248   :group 'gnus-thread
249   :type '(repeat regexp))
250
251 (defcustom gnus-show-threads t
252   "*If non-nil, display threads in summary mode."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-hide-subtree nil
257   "*If non-nil, hide all threads initially.
258 This can be a predicate specifier which says which threads to hide.
259 If threads are hidden, you have to run the command
260 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
261 to expose hidden threads."
262   :group 'gnus-thread
263   :type '(radio (sexp :format "Non-nil\n"
264                       :match (lambda (widget value)
265                                (not (or (consp value) (functionp value))))
266                       :value t)
267                 (const nil)
268                 (sexp :tag "Predicate specifier")))
269
270 (defcustom gnus-thread-hide-killed t
271   "*If non-nil, hide killed threads automatically."
272   :group 'gnus-thread
273   :type 'boolean)
274
275 (defcustom gnus-thread-ignore-subject t
276   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
277 If nil, articles that have different subjects from their parents will
278 start separate threads."
279   :group 'gnus-thread
280   :type 'boolean)
281
282 (defcustom gnus-thread-operation-ignore-subject t
283   "*If non-nil, subjects will be ignored when doing thread commands.
284 This affects commands like `gnus-summary-kill-thread' and
285 `gnus-summary-lower-thread'.
286
287 If this variable is nil, articles in the same thread with different
288 subjects will not be included in the operation in question.  If this
289 variable is `fuzzy', only articles that have subjects that are fuzzily
290 equal will be included."
291   :group 'gnus-thread
292   :type '(choice (const :tag "off" nil)
293                  (const fuzzy)
294                  (sexp :tag "on" t)))
295
296 (defcustom gnus-thread-indent-level 4
297   "*Number that says how much each sub-thread should be indented."
298   :group 'gnus-thread
299   :type 'integer)
300
301 (defcustom gnus-auto-extend-newsgroup t
302   "*If non-nil, extend newsgroup forward and backward when requested."
303   :group 'gnus-summary-choose
304   :type 'boolean)
305
306 (defcustom gnus-auto-select-first t
307   "*If non-nil, select the article under point.
308 Which article this is is controlled by the `gnus-auto-select-subject'
309 variable.
310
311 If you want to prevent automatic selection of articles in some
312 newsgroups, set the variable to nil in `gnus-select-group-hook'."
313   :group 'gnus-group-select
314   :type '(choice (const :tag "none" nil)
315                  (sexp :menu-tag "first" t)))
316
317 (defcustom gnus-auto-select-subject 'unread
318   "*Says what subject to place under point when entering a group.
319
320 This variable can either be the symbols `first' (place point on the
321 first subject), `unread' (place point on the subject line of the first
322 unread article), `best' (place point on the subject line of the
323 higest-scored article), `unseen' (place point on the subject line of
324 the first unseen article), `unseen-or-unread' (place point on the subject
325 line of the first unseen article or, if all article have been seen, on the
326 subject line of the first unread article), or a function to be called to
327 place point on some subject line."
328   :version "22.1"
329   :group 'gnus-group-select
330   :type '(choice (const best)
331                  (const unread)
332                  (const first)
333                  (const unseen)
334                  (const unseen-or-unread)))
335
336 (defcustom gnus-auto-select-next t
337   "*If non-nil, offer to go to the next group from the end of the previous.
338 If the value is t and the next newsgroup is empty, Gnus will exit
339 summary mode and go back to group mode.  If the value is neither nil
340 nor t, Gnus will select the following unread newsgroup.  In
341 particular, if the value is the symbol `quietly', the next unread
342 newsgroup will be selected without any confirmation, and if it is
343 `almost-quietly', the next group will be selected without any
344 confirmation if you are located on the last article in the group.
345 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
346 will go to the next group without confirmation."
347   :group 'gnus-summary-maneuvering
348   :type '(choice (const :tag "off" nil)
349                  (const quietly)
350                  (const almost-quietly)
351                  (const slightly-quietly)
352                  (sexp :menu-tag "on" t)))
353
354 (defcustom gnus-auto-select-same nil
355   "*If non-nil, select the next article with the same subject.
356 If there are no more articles with the same subject, go to
357 the first unread article."
358   :group 'gnus-summary-maneuvering
359   :type 'boolean)
360
361 (defcustom gnus-auto-goto-ignores 'unfetched
362   "*Says how to handle unfetched articles when maneuvering.
363
364 This variable can either be the symbols nil (maneuver to any
365 article), `undownloaded' (maneuvering while unplugged ignores articles
366 that have not been fetched), `always-undownloaded' (maneuvering always
367 ignores articles that have not been fetched), `unfetched' (maneuvering
368 ignores articles whose headers have not been fetched).
369
370 NOTE: The list of unfetched articles will always be nil when plugged
371 and, when unplugged, a subset of the undownloaded article list."
372   :version "22.1"
373   :group 'gnus-summary-maneuvering
374   :type '(choice (const :tag "None" nil)
375                  (const :tag "Undownloaded when unplugged" undownloaded)
376                  (const :tag "Undownloaded" always-undownloaded)
377                  (const :tag "Unfetched" unfetched)))
378
379 (defcustom gnus-summary-check-current nil
380   "*If non-nil, consider the current article when moving.
381 The \"unread\" movement commands will stay on the same line if the
382 current article is unread."
383   :group 'gnus-summary-maneuvering
384   :type 'boolean)
385
386 (defcustom gnus-auto-center-summary 2
387   "*If non-nil, always center the current summary buffer.
388 In particular, if `vertical' do only vertical recentering.  If non-nil
389 and non-`vertical', do both horizontal and vertical recentering."
390   :group 'gnus-summary-maneuvering
391   :type '(choice (const :tag "none" nil)
392                  (const vertical)
393                  (integer :tag "height")
394                  (sexp :menu-tag "both" t)))
395
396 (defvar gnus-auto-center-group t
397   "*If non-nil, always center the group buffer.")
398
399 (defcustom gnus-show-all-headers nil
400   "*If non-nil, don't hide any headers."
401   :group 'gnus-article-hiding
402   :group 'gnus-article-headers
403   :type 'boolean)
404
405 (defcustom gnus-summary-ignore-duplicates nil
406   "*If non-nil, ignore articles with identical Message-ID headers."
407   :group 'gnus-summary
408   :type 'boolean)
409
410 (defcustom gnus-single-article-buffer t
411   "*If non-nil, display all articles in the same buffer.
412 If nil, each group will get its own article buffer."
413   :group 'gnus-article-various
414   :type 'boolean)
415
416 (defcustom gnus-break-pages t
417   "*If non-nil, do page breaking on articles.
418 The page delimiter is specified by the `gnus-page-delimiter'
419 variable."
420   :group 'gnus-article-various
421   :type 'boolean)
422
423 (defcustom gnus-move-split-methods nil
424   "*Variable used to suggest where articles are to be moved to.
425 It uses the same syntax as the `gnus-split-methods' variable.
426 However, whereas `gnus-split-methods' specifies file names as targets,
427 this variable specifies group names."
428   :group 'gnus-summary-mail
429   :type '(repeat (choice (list :value (fun) function)
430                          (cons :value ("" "") regexp (repeat string))
431                          (sexp :value nil))))
432
433 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
434   "Function used to compute default prefix for article move/copy/etc prompts.
435 The function should take one argument, a group name, and return a
436 string with the suggested prefix."
437   :group 'gnus-summary-mail
438   :type 'function)
439
440 ;; FIXME: Although the custom type is `character' for the following variables,
441 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
442
443 (defcustom gnus-unread-mark ?           ;Whitespace
444   "*Mark used for unread articles."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-ticked-mark ?!
449   "*Mark used for ticked articles."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-dormant-mark ??
454   "*Mark used for dormant articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-del-mark ?r
459   "*Mark used for del'd articles."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-read-mark ?R
464   "*Mark used for read articles."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-expirable-mark ?E
469   "*Mark used for expirable articles."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-killed-mark ?K
474   "*Mark used for killed articles."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-spam-mark ?$
479   "*Mark used for spam articles."
480   :version "22.1"
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-souped-mark ?F
485   "*Mark used for souped articles."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-kill-file-mark ?X
490   "*Mark used for articles killed by kill files."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-low-score-mark ?Y
495   "*Mark used for articles with a low score."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-catchup-mark ?C
500   "*Mark used for articles that are caught up."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-replied-mark ?A
505   "*Mark used for articles that have been replied to."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-forwarded-mark ?F
510   "*Mark used for articles that have been forwarded."
511   :version "22.1"
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-recent-mark ?N
516   "*Mark used for articles that are recent."
517   :version "22.1"
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-cached-mark ?*
522   "*Mark used for articles that are in the cache."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-saved-mark ?S
527   "*Mark used for articles that have been saved."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-unseen-mark ?.
532   "*Mark used for articles that haven't been seen."
533   :version "22.1"
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-no-mark ?               ;Whitespace
538   "*Mark used for articles that have no other secondary mark."
539   :version "22.1"
540   :group 'gnus-summary-marks
541   :type 'character)
542
543 (defcustom gnus-ancient-mark ?O
544   "*Mark used for ancient articles."
545   :group 'gnus-summary-marks
546   :type 'character)
547
548 (defcustom gnus-sparse-mark ?Q
549   "*Mark used for sparsely reffed articles."
550   :group 'gnus-summary-marks
551   :type 'character)
552
553 (defcustom gnus-canceled-mark ?G
554   "*Mark used for canceled articles."
555   :group 'gnus-summary-marks
556   :type 'character)
557
558 (defcustom gnus-duplicate-mark ?M
559   "*Mark used for duplicate articles."
560   :group 'gnus-summary-marks
561   :type 'character)
562
563 (defcustom gnus-undownloaded-mark ?-
564   "*Mark used for articles that weren't downloaded."
565   :version "22.1"
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-downloaded-mark ?+
570   "*Mark used for articles that were downloaded."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-downloadable-mark ?%
575   "*Mark used for articles that are to be downloaded."
576   :group 'gnus-summary-marks
577   :type 'character)
578
579 (defcustom gnus-unsendable-mark ?=
580   "*Mark used for articles that won't be sent."
581   :group 'gnus-summary-marks
582   :type 'character)
583
584 (defcustom gnus-score-over-mark ?+
585   "*Score mark used for articles with high scores."
586   :group 'gnus-summary-marks
587   :type 'character)
588
589 (defcustom gnus-score-below-mark ?-
590   "*Score mark used for articles with low scores."
591   :group 'gnus-summary-marks
592   :type 'character)
593
594 (defcustom gnus-empty-thread-mark ?     ;Whitespace
595   "*There is no thread under the article."
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-not-empty-thread-mark ?=
600   "*There is a thread under the article."
601   :group 'gnus-summary-marks
602   :type 'character)
603
604 (defcustom gnus-view-pseudo-asynchronously nil
605   "*If non-nil, Gnus will view pseudo-articles asynchronously."
606   :group 'gnus-extract-view
607   :type 'boolean)
608
609 (defcustom gnus-auto-expirable-marks
610   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
611         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
612         gnus-souped-mark gnus-duplicate-mark)
613   "*The list of marks converted into expiration if a group is auto-expirable."
614   :version "21.1"
615   :group 'gnus-summary
616   :type '(repeat character))
617
618 (defcustom gnus-inhibit-user-auto-expire t
619   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
620   :version "21.1"
621   :group 'gnus-summary
622   :type 'boolean)
623
624 (defcustom gnus-view-pseudos nil
625   "*If `automatic', pseudo-articles will be viewed automatically.
626 If `not-confirm', pseudos will be viewed automatically, and the user
627 will not be asked to confirm the command."
628   :group 'gnus-extract-view
629   :type '(choice (const :tag "off" nil)
630                  (const automatic)
631                  (const not-confirm)))
632
633 (defcustom gnus-view-pseudos-separately t
634   "*If non-nil, one pseudo-article will be created for each file to be viewed.
635 If nil, all files that use the same viewing command will be given as a
636 list of parameters to that command."
637   :group 'gnus-extract-view
638   :type 'boolean)
639
640 (defcustom gnus-insert-pseudo-articles t
641   "*If non-nil, insert pseudo-articles when decoding articles."
642   :group 'gnus-extract-view
643   :type 'boolean)
644
645 (defcustom gnus-summary-dummy-line-format
646   "   %(:                             :%) %S\n"
647   "*The format specification for the dummy roots in the summary buffer.
648 It works along the same lines as a normal formatting string,
649 with some simple extensions.
650
651 %S  The subject
652
653 General format specifiers can also be used.
654 See `(gnus)Formatting Variables'."
655   :link '(custom-manual "(gnus)Formatting Variables")
656   :group 'gnus-threading
657   :type 'string)
658
659 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
660   "*The format specification for the summary mode line.
661 It works along the same lines as a normal formatting string,
662 with some simple extensions:
663
664 %G  Group name
665 %p  Unprefixed group name
666 %A  Current article number
667 %z  Current article score
668 %V  Gnus version
669 %U  Number of unread articles in the group
670 %e  Number of unselected articles in the group
671 %Z  A string with unread/unselected article counts
672 %g  Shortish group name
673 %S  Subject of the current article
674 %u  User-defined spec
675 %s  Current score file name
676 %d  Number of dormant articles
677 %r  Number of articles that have been marked as read in this session
678 %E  Number of articles expunged by the score files"
679   :group 'gnus-summary-format
680   :type 'string)
681
682 (defcustom gnus-list-identifiers nil
683   "Regexp that matches list identifiers to be removed from subject.
684 This can also be a list of regexps."
685   :version "21.1"
686   :group 'gnus-summary-format
687   :group 'gnus-article-hiding
688   :type '(choice (const :tag "none" nil)
689                  (regexp :value ".*")
690                  (repeat :value (".*") regexp)))
691
692 (defcustom gnus-summary-mark-below 0
693   "*Mark all articles with a score below this variable as read.
694 This variable is local to each summary buffer and usually set by the
695 score file."
696   :group 'gnus-score-default
697   :type 'integer)
698
699 (defun gnus-widget-reversible-match (widget value)
700   "Ignoring WIDGET, convert VALUE to internal form.
701 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
702   ;; (debug value)
703   (or (symbolp value)
704       (and (listp value)
705            (eq (length value) 2)
706            (eq (nth 0 value) 'not)
707            (symbolp (nth 1 value)))))
708
709 (defun gnus-widget-reversible-to-internal (widget value)
710   "Ignoring WIDGET, convert VALUE to internal form.
711 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
712 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
713   ;; (debug value)
714   (if (atom value)
715       (list value nil)
716     (list (nth 1 value) t)))
717
718 (defun gnus-widget-reversible-to-external (widget value)
719   "Ignoring WIDGET, convert VALUE to external form.
720 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
721 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
722   ;; (debug value)
723   (if (nth 1 value)
724       (list 'not (nth 0 value))
725     (nth 0 value)))
726
727 (define-widget 'gnus-widget-reversible 'group
728   "A `group' that convert values."
729   :match 'gnus-widget-reversible-match
730   :value-to-internal 'gnus-widget-reversible-to-internal
731   :value-to-external 'gnus-widget-reversible-to-external)
732
733 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
734   "*List of functions used for sorting articles in the summary buffer.
735
736 Each function takes two articles and returns non-nil if the first
737 article should be sorted before the other.  If you use more than one
738 function, the primary sort function should be the last.  You should
739 probably always include `gnus-article-sort-by-number' in the list of
740 sorting functions -- preferably first.  Also note that sorting by date
741 is often much slower than sorting by number, and the sorting order is
742 very similar.  (Sorting by date means sorting by the time the message
743 was sent, sorting by number means sorting by arrival time.)
744
745 Each item can also be a list `(not F)' where F is a function;
746 this reverses the sort order.
747
748 Ready-made functions include `gnus-article-sort-by-number',
749 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
750 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
751 and `gnus-article-sort-by-score'.
752
753 When threading is turned on, the variable `gnus-thread-sort-functions'
754 controls how articles are sorted."
755   :group 'gnus-summary-sort
756   :type '(repeat (gnus-widget-reversible
757                   (choice (function-item gnus-article-sort-by-number)
758                           (function-item gnus-article-sort-by-author)
759                           (function-item gnus-article-sort-by-subject)
760                           (function-item gnus-article-sort-by-date)
761                           (function-item gnus-article-sort-by-score)
762                           (function-item gnus-article-sort-by-random)
763                           (function :tag "other"))
764                   (boolean :tag "Reverse order"))))
765
766
767 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
768   "*List of functions used for sorting threads in the summary buffer.
769 By default, threads are sorted by article number.
770
771 Each function takes two threads and returns non-nil if the first
772 thread should be sorted before the other.  If you use more than one
773 function, the primary sort function should be the last.  You should
774 probably always include `gnus-thread-sort-by-number' in the list of
775 sorting functions -- preferably first.  Also note that sorting by date
776 is often much slower than sorting by number, and the sorting order is
777 very similar.  (Sorting by date means sorting by the time the message
778 was sent, sorting by number means sorting by arrival time.)
779
780 Each list item can also be a list `(not F)' where F is a
781 function; this specifies reversed sort order.
782
783 Ready-made functions include `gnus-thread-sort-by-number',
784 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
785 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
786 `gnus-thread-sort-by-most-recent-number',
787 `gnus-thread-sort-by-most-recent-date',
788 `gnus-thread-sort-by-random', and
789 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
790
791 When threading is turned off, the variable
792 `gnus-article-sort-functions' controls how articles are sorted."
793   :group 'gnus-summary-sort
794   :type '(repeat 
795           (gnus-widget-reversible
796            (choice (function-item gnus-thread-sort-by-number)
797                    (function-item gnus-thread-sort-by-author)
798                    (function-item gnus-thread-sort-by-subject)
799                    (function-item gnus-thread-sort-by-date)
800                    (function-item gnus-thread-sort-by-score)
801                    (function-item gnus-thread-sort-by-most-recent-number)
802                    (function-item gnus-thread-sort-by-most-recent-date)
803                    (function-item gnus-thread-sort-by-random)
804                    (function-item gnus-thread-sort-by-total-score)
805                    (function :tag "other"))
806            (boolean :tag "Reverse order"))))
807
808 (defcustom gnus-thread-score-function '+
809   "*Function used for calculating the total score of a thread.
810
811 The function is called with the scores of the article and each
812 subthread and should then return the score of the thread.
813
814 Some functions you can use are `+', `max', or `min'."
815   :group 'gnus-summary-sort
816   :type 'function)
817
818 (defcustom gnus-summary-expunge-below nil
819   "All articles that have a score less than this variable will be expunged.
820 This variable is local to the summary buffers."
821   :group 'gnus-score-default
822   :type '(choice (const :tag "off" nil)
823                  integer))
824
825 (defcustom gnus-thread-expunge-below nil
826   "All threads that have a total score less than this variable will be expunged.
827 See `gnus-thread-score-function' for en explanation of what a
828 \"thread score\" is.
829
830 This variable is local to the summary buffers."
831   :group 'gnus-threading
832   :group 'gnus-score-default
833   :type '(choice (const :tag "off" nil)
834                  integer))
835
836 (defcustom gnus-summary-mode-hook nil
837   "*A hook for Gnus summary mode.
838 This hook is run before any variables are set in the summary buffer."
839   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
840   :group 'gnus-summary-various
841   :type 'hook)
842
843 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
844 (when (featurep 'xemacs)
845   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
846   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
847   (add-hook 'gnus-summary-mode-hook
848             'gnus-xmas-switch-horizontal-scrollbar-off))
849
850 (defcustom gnus-summary-menu-hook nil
851   "*Hook run after the creation of the summary mode menu."
852   :group 'gnus-summary-visual
853   :type 'hook)
854
855 (defcustom gnus-summary-exit-hook nil
856   "*A hook called on exit from the summary buffer.
857 It will be called with point in the group buffer."
858   :group 'gnus-summary-exit
859   :type 'hook)
860
861 (defcustom gnus-summary-prepare-hook nil
862   "*A hook called after the summary buffer has been generated.
863 If you want to modify the summary buffer, you can use this hook."
864   :group 'gnus-summary-various
865   :type 'hook)
866
867 (defcustom gnus-summary-prepared-hook nil
868   "*A hook called as the last thing after the summary buffer has been generated."
869   :group 'gnus-summary-various
870   :type 'hook)
871
872 (defcustom gnus-summary-generate-hook nil
873   "*A hook run just before generating the summary buffer.
874 This hook is commonly used to customize threading variables and the
875 like."
876   :group 'gnus-summary-various
877   :type 'hook)
878
879 (defcustom gnus-select-group-hook nil
880   "*A hook called when a newsgroup is selected.
881
882 If you'd like to simplify subjects like the
883 `gnus-summary-next-same-subject' command does, you can use the
884 following hook:
885
886  (add-hook gnus-select-group-hook
887            (lambda ()
888              (mapcar (lambda (header)
889                        (mail-header-set-subject
890                         header
891                         (gnus-simplify-subject
892                          (mail-header-subject header) 're-only)))
893                      gnus-newsgroup-headers)))"
894   :group 'gnus-group-select
895   :type 'hook)
896
897 (defcustom gnus-select-article-hook nil
898   "*A hook called when an article is selected."
899   :group 'gnus-summary-choose
900   :options '(gnus-agent-fetch-selected-article)
901   :type 'hook)
902
903 (defcustom gnus-visual-mark-article-hook
904   (list 'gnus-highlight-selected-summary)
905   "*Hook run after selecting an article in the summary buffer.
906 It is meant to be used for highlighting the article in some way.  It
907 is not run if `gnus-visual' is nil."
908   :group 'gnus-summary-visual
909   :type 'hook)
910
911 (defcustom gnus-parse-headers-hook nil
912   "*A hook called before parsing the headers."
913   :group 'gnus-various
914   :type 'hook)
915
916 (defcustom gnus-exit-group-hook nil
917   "*A hook called when exiting summary mode.
918 This hook is not called from the non-updating exit commands like `Q'."
919   :group 'gnus-various
920   :type 'hook)
921
922 (defcustom gnus-summary-update-hook
923   (list 'gnus-summary-highlight-line)
924   "*A hook called when a summary line is changed.
925 The hook will not be called if `gnus-visual' is nil.
926
927 The default function `gnus-summary-highlight-line' will
928 highlight the line according to the `gnus-summary-highlight'
929 variable."
930   :group 'gnus-summary-visual
931   :type 'hook)
932
933 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
934   "*A hook called when an article is selected for the first time.
935 The hook is intended to mark an article as read (or unread)
936 automatically when it is selected."
937   :group 'gnus-summary-choose
938   :type 'hook)
939
940 (defcustom gnus-group-no-more-groups-hook nil
941   "*A hook run when returning to group mode having no more (unread) groups."
942   :group 'gnus-group-select
943   :type 'hook)
944
945 (defcustom gnus-ps-print-hook nil
946   "*A hook run before ps-printing something from Gnus."
947   :group 'gnus-summary
948   :type 'hook)
949
950 (defcustom gnus-summary-article-move-hook nil
951   "*A hook called after an article is moved, copied, respooled, or crossposted."
952   :version "22.1"
953   :group 'gnus-summary
954   :type 'hook)
955
956 (defcustom gnus-summary-article-delete-hook nil
957   "*A hook called after an article is deleted."
958   :version "22.1"
959   :group 'gnus-summary
960   :type 'hook)
961
962 (defcustom gnus-summary-article-expire-hook nil
963   "*A hook called after an article is expired."
964   :version "22.1"
965   :group 'gnus-summary
966   :type 'hook)
967
968 (defcustom gnus-summary-display-arrow
969   (and (fboundp 'display-graphic-p)
970        (display-graphic-p))
971   "*If non-nil, display an arrow highlighting the current article."
972   :version "22.1"
973   :group 'gnus-summary
974   :type 'boolean)
975
976 (defcustom gnus-summary-selected-face 'gnus-summary-selected
977   "Face used for highlighting the current article in the summary buffer."
978   :group 'gnus-summary-visual
979   :type 'face)
980
981 (defvar gnus-tmp-downloaded nil)
982
983 (defcustom gnus-summary-highlight
984   '(((eq mark gnus-canceled-mark)
985      . gnus-summary-cancelled)
986     ((and uncached (> score default-high))
987      . gnus-summary-high-undownloaded)
988     ((and uncached (< score default-low))
989      . gnus-summary-low-undownloaded)
990     (uncached
991      . gnus-summary-normal-undownloaded)
992     ((and (> score default-high)
993           (or (eq mark gnus-dormant-mark)
994               (eq mark gnus-ticked-mark)))
995      . gnus-summary-high-ticked)
996     ((and (< score default-low)
997           (or (eq mark gnus-dormant-mark)
998               (eq mark gnus-ticked-mark)))
999      . gnus-summary-low-ticked)
1000     ((or (eq mark gnus-dormant-mark)
1001          (eq mark gnus-ticked-mark))
1002      . gnus-summary-normal-ticked)
1003     ((and (> score default-high) (eq mark gnus-ancient-mark))
1004      . gnus-summary-high-ancient)
1005     ((and (< score default-low) (eq mark gnus-ancient-mark))
1006      . gnus-summary-low-ancient)
1007     ((eq mark gnus-ancient-mark)
1008      . gnus-summary-normal-ancient)
1009     ((and (> score default-high) (eq mark gnus-unread-mark))
1010      . gnus-summary-high-unread)
1011     ((and (< score default-low) (eq mark gnus-unread-mark))
1012      . gnus-summary-low-unread)
1013     ((eq mark gnus-unread-mark)
1014      . gnus-summary-normal-unread)
1015     ((> score default-high)
1016      . gnus-summary-high-read)
1017     ((< score default-low)
1018      . gnus-summary-low-read)
1019     (t
1020      . gnus-summary-normal-read))
1021   "*Controls the highlighting of summary buffer lines.
1022
1023 A list of (FORM . FACE) pairs.  When deciding how a a particular
1024 summary line should be displayed, each form is evaluated.  The content
1025 of the face field after the first true form is used.  You can change
1026 how those summary lines are displayed, by editing the face field.
1027
1028 You can use the following variables in the FORM field.
1029
1030 score:        The article's score
1031 default:      The default article score.
1032 default-high: The default score for high scored articles.
1033 default-low:  The default score for low scored articles.
1034 below:        The score below which articles are automatically marked as read.
1035 mark:         The article's mark.
1036 uncached:     Non-nil if the article is uncached."
1037   :group 'gnus-summary-visual
1038   :type '(repeat (cons (sexp :tag "Form" nil)
1039                        face)))
1040
1041 (defcustom gnus-alter-header-function nil
1042   "Function called to allow alteration of article header structures.
1043 The function is called with one parameter, the article header vector,
1044 which it may alter in any way."
1045   :type '(choice (const :tag "None" nil)
1046                  function)
1047   :group 'gnus-summary)
1048
1049 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1050   "Variable that says which function should be used to decode a string with encoded words.")
1051
1052 (defcustom gnus-extra-headers '(To Newsgroups)
1053   "*Extra headers to parse."
1054   :version "21.1"
1055   :group 'gnus-summary
1056   :type '(repeat symbol))
1057
1058 (defcustom gnus-ignored-from-addresses
1059   (and user-mail-address (regexp-quote user-mail-address))
1060   "*Regexp of From headers that may be suppressed in favor of To headers."
1061   :version "21.1"
1062   :group 'gnus-summary
1063   :type 'regexp)
1064
1065 (defcustom gnus-summary-to-prefix "-> "
1066   "*String prefixed to the To field in the summary line when
1067 using `gnus-ignored-from-addresses'."
1068   :version "22.1"
1069   :group 'gnus-summary
1070   :type 'string)
1071
1072 (defcustom gnus-summary-newsgroup-prefix "=> "
1073   "*String prefixed to the Newsgroup field in the summary
1074 line when using `gnus-ignored-from-addresses'."
1075   :version "22.1"
1076   :group 'gnus-summary
1077   :type 'string)
1078
1079 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1080   "List of charsets that should be ignored.
1081 When these charsets are used in the \"charset\" parameter, the
1082 default charset will be used instead."
1083   :version "21.1"
1084   :type '(repeat symbol)
1085   :group 'gnus-charset)
1086
1087 (gnus-define-group-parameter
1088  ignored-charsets
1089  :type list
1090  :function-document
1091  "Return the ignored charsets of GROUP."
1092  :variable gnus-group-ignored-charsets-alist
1093  :variable-default
1094  '(("alt\\.chinese\\.text" iso-8859-1))
1095  :variable-document
1096  "Alist of regexps (to match group names) and charsets that should be ignored.
1097 When these charsets are used in the \"charset\" parameter, the
1098 default charset will be used instead."
1099  :variable-group gnus-charset
1100  :variable-type '(repeat (cons (regexp :tag "Group")
1101                                (repeat symbol)))
1102  :parameter-type '(choice :tag "Ignored charsets"
1103                           :value nil
1104                           (repeat (symbol)))
1105  :parameter-document       "\
1106 List of charsets that should be ignored.
1107
1108 When these charsets are used in the \"charset\" parameter, the
1109 default charset will be used instead.")
1110
1111 (defcustom gnus-group-highlight-words-alist nil
1112   "Alist of group regexps and highlight regexps.
1113 This variable uses the same syntax as `gnus-emphasis-alist'."
1114   :version "21.1"
1115   :type '(repeat (cons (regexp :tag "Group")
1116                        (repeat (list (regexp :tag "Highlight regexp")
1117                                      (number :tag "Group for entire word" 0)
1118                                      (number :tag "Group for displayed part" 0)
1119                                      (symbol :tag "Face"
1120                                              gnus-emphasis-highlight-words)))))
1121   :group 'gnus-summary-visual)
1122
1123 (defcustom gnus-summary-show-article-charset-alist
1124   nil
1125   "Alist of number and charset.
1126 The article will be shown with the charset corresponding to the
1127 numbered argument.
1128 For example: ((1 . cn-gb-2312) (2 . big5))."
1129   :version "21.1"
1130   :type '(repeat (cons (number :tag "Argument" 1)
1131                        (symbol :tag "Charset")))
1132   :group 'gnus-charset)
1133
1134 (defcustom gnus-preserve-marks t
1135   "Whether marks are preserved when moving, copying and respooling messages."
1136   :version "21.1"
1137   :type 'boolean
1138   :group 'gnus-summary-marks)
1139
1140 (defcustom gnus-alter-articles-to-read-function nil
1141   "Function to be called to alter the list of articles to be selected."
1142   :type '(choice (const nil) function)
1143   :group 'gnus-summary)
1144
1145 (defcustom gnus-orphan-score nil
1146   "*All orphans get this score added.  Set in the score file."
1147   :group 'gnus-score-default
1148   :type '(choice (const nil)
1149                  integer))
1150
1151 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1152   "*A regexp to match MIME parts when saving multiple parts of a
1153 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1154 This regexp will be used by default when prompting the user for which
1155 type of files to save."
1156   :group 'gnus-summary
1157   :type 'regexp)
1158
1159 (defcustom gnus-read-all-available-headers nil
1160   "Whether Gnus should parse all headers made available to it.
1161 This is mostly relevant for slow back ends where the user may
1162 wish to widen the summary buffer to include all headers
1163 that were fetched.  Say, for nnultimate groups."
1164   :version "22.1"
1165   :group 'gnus-summary
1166   :type '(choice boolean regexp))
1167
1168 (defcustom gnus-summary-muttprint-program "muttprint"
1169   "Command (and optional arguments) used to run Muttprint."
1170   :version "22.1"
1171   :group 'gnus-summary
1172   :type 'string)
1173
1174 (defcustom gnus-article-loose-mime t
1175   "If non-nil, don't require MIME-Version header.
1176 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1177 supply the MIME-Version header or deliberately strip it from the mail.
1178 If non-nil (the default), Gnus will treat some articles as MIME
1179 even if the MIME-Version header is missing."
1180   :version "22.1"
1181   :type 'boolean
1182   :group 'gnus-article-mime)
1183
1184 (defcustom gnus-article-emulate-mime t
1185   "If non-nil, use MIME emulation for uuencode and the like.
1186 This means that Gnus will search message bodies for text that look
1187 like uuencoded bits, yEncoded bits, and so on, and present that using
1188 the normal Gnus MIME machinery."
1189   :version "22.1"
1190   :type 'boolean
1191   :group 'gnus-article-mime)
1192
1193 ;;; Internal variables
1194
1195 (defvar gnus-summary-display-cache nil)
1196 (defvar gnus-article-mime-handles nil)
1197 (defvar gnus-article-decoded-p nil)
1198 (defvar gnus-article-charset nil)
1199 (defvar gnus-article-ignored-charsets nil)
1200 (defvar gnus-scores-exclude-files nil)
1201 (defvar gnus-page-broken nil)
1202
1203 (defvar gnus-original-article nil)
1204 (defvar gnus-article-internal-prepare-hook nil)
1205 (defvar gnus-newsgroup-process-stack nil)
1206
1207 (defvar gnus-thread-indent-array nil)
1208 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1209 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1210   "Function called to sort the articles within a thread after it has been gathered together.")
1211
1212 (defvar gnus-summary-save-parts-type-history nil)
1213 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1214
1215 ;; Avoid highlighting in kill files.
1216 (defvar gnus-summary-inhibit-highlight nil)
1217 (defvar gnus-newsgroup-selected-overlay nil)
1218 (defvar gnus-inhibit-limiting nil)
1219 (defvar gnus-newsgroup-adaptive-score-file nil)
1220 (defvar gnus-current-score-file nil)
1221 (defvar gnus-current-move-group nil)
1222 (defvar gnus-current-copy-group nil)
1223 (defvar gnus-current-crosspost-group nil)
1224 (defvar gnus-newsgroup-display nil)
1225
1226 (defvar gnus-newsgroup-dependencies nil)
1227 (defvar gnus-newsgroup-adaptive nil)
1228 (defvar gnus-summary-display-article-function nil)
1229 (defvar gnus-summary-highlight-line-function nil
1230   "Function called after highlighting a summary line.")
1231
1232 (defvar gnus-summary-line-format-alist
1233   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1234     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1235     (?s gnus-tmp-subject-or-nil ?s)
1236     (?n gnus-tmp-name ?s)
1237     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1238         ?s)
1239     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1240             gnus-tmp-from) ?s)
1241     (?F gnus-tmp-from ?s)
1242     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1243     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1244     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1245     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1246     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1247     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1248     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1249     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1250     (?L gnus-tmp-lines ?s)
1251     (?O gnus-tmp-downloaded ?c)
1252     (?I gnus-tmp-indentation ?s)
1253     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1254     (?R gnus-tmp-replied ?c)
1255     (?\[ gnus-tmp-opening-bracket ?c)
1256     (?\] gnus-tmp-closing-bracket ?c)
1257     (?\> (make-string gnus-tmp-level ? ) ?s)
1258     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1259     (?i gnus-tmp-score ?d)
1260     (?z gnus-tmp-score-char ?c)
1261     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1262     (?U gnus-tmp-unread ?c)
1263     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1264         ?s)
1265     (?t (gnus-summary-number-of-articles-in-thread
1266          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1267         ?d)
1268     (?e (gnus-summary-number-of-articles-in-thread
1269          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1270         ?c)
1271     (?u gnus-tmp-user-defined ?s)
1272     (?P (gnus-pick-line-number) ?d)
1273     (?B gnus-tmp-thread-tree-header-string ?s)
1274     (user-date (gnus-user-date
1275                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1276   "An alist of format specifications that can appear in summary lines.
1277 These are paired with what variables they correspond with, along with
1278 the type of the variable (string, integer, character, etc).")
1279
1280 (defvar gnus-summary-dummy-line-format-alist
1281   `((?S gnus-tmp-subject ?s)
1282     (?N gnus-tmp-number ?d)
1283     (?u gnus-tmp-user-defined ?s)))
1284
1285 (defvar gnus-summary-mode-line-format-alist
1286   `((?G gnus-tmp-group-name ?s)
1287     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1288     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1289     (?A gnus-tmp-article-number ?d)
1290     (?Z gnus-tmp-unread-and-unselected ?s)
1291     (?V gnus-version ?s)
1292     (?U gnus-tmp-unread-and-unticked ?d)
1293     (?S gnus-tmp-subject ?s)
1294     (?e gnus-tmp-unselected ?d)
1295     (?u gnus-tmp-user-defined ?s)
1296     (?d (length gnus-newsgroup-dormant) ?d)
1297     (?t (length gnus-newsgroup-marked) ?d)
1298     (?h (length gnus-newsgroup-spam-marked) ?d)
1299     (?r (length gnus-newsgroup-reads) ?d)
1300     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1301     (?E gnus-newsgroup-expunged-tally ?d)
1302     (?s (gnus-current-score-file-nondirectory) ?s)))
1303
1304 (defvar gnus-last-search-regexp nil
1305   "Default regexp for article search command.")
1306
1307 (defvar gnus-last-shell-command nil
1308   "Default shell command on article.")
1309
1310 (defvar gnus-newsgroup-agentized nil
1311   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1312 (defvar gnus-newsgroup-begin nil)
1313 (defvar gnus-newsgroup-end nil)
1314 (defvar gnus-newsgroup-last-rmail nil)
1315 (defvar gnus-newsgroup-last-mail nil)
1316 (defvar gnus-newsgroup-last-folder nil)
1317 (defvar gnus-newsgroup-last-file nil)
1318 (defvar gnus-newsgroup-auto-expire nil)
1319 (defvar gnus-newsgroup-active nil)
1320
1321 (defvar gnus-newsgroup-data nil)
1322 (defvar gnus-newsgroup-data-reverse nil)
1323 (defvar gnus-newsgroup-limit nil)
1324 (defvar gnus-newsgroup-limits nil)
1325 (defvar gnus-summary-use-undownloaded-faces nil)
1326
1327 (defvar gnus-newsgroup-unreads nil
1328   "Sorted list of unread articles in the current newsgroup.")
1329
1330 (defvar gnus-newsgroup-unselected nil
1331   "Sorted list of unselected unread articles in the current newsgroup.")
1332
1333 (defvar gnus-newsgroup-reads nil
1334   "Alist of read articles and article marks in the current newsgroup.")
1335
1336 (defvar gnus-newsgroup-expunged-tally nil)
1337
1338 (defvar gnus-newsgroup-marked nil
1339   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1340
1341 (defvar gnus-newsgroup-spam-marked nil
1342   "List of ranges of articles that have been marked as spam.")
1343
1344 (defvar gnus-newsgroup-killed nil
1345   "List of ranges of articles that have been through the scoring process.")
1346
1347 (defvar gnus-newsgroup-cached nil
1348   "Sorted list of articles that come from the article cache.")
1349
1350 (defvar gnus-newsgroup-saved nil
1351   "List of articles that have been saved.")
1352
1353 (defvar gnus-newsgroup-kill-headers nil)
1354
1355 (defvar gnus-newsgroup-replied nil
1356   "List of articles that have been replied to in the current newsgroup.")
1357
1358 (defvar gnus-newsgroup-forwarded nil
1359   "List of articles that have been forwarded in the current newsgroup.")
1360
1361 (defvar gnus-newsgroup-recent nil
1362   "List of articles that have are recent in the current newsgroup.")
1363
1364 (defvar gnus-newsgroup-expirable nil
1365   "Sorted list of articles in the current newsgroup that can be expired.")
1366
1367 (defvar gnus-newsgroup-processable nil
1368   "List of articles in the current newsgroup that can be processed.")
1369
1370 (defvar gnus-newsgroup-downloadable nil
1371   "Sorted list of articles in the current newsgroup that can be processed.")
1372
1373 (defvar gnus-newsgroup-unfetched nil
1374   "Sorted list of articles in the current newsgroup whose headers have
1375 not been fetched into the agent.
1376
1377 This list will always be a subset of gnus-newsgroup-undownloaded.")
1378
1379 (defvar gnus-newsgroup-undownloaded nil
1380   "List of articles in the current newsgroup that haven't been downloaded.")
1381
1382 (defvar gnus-newsgroup-unsendable nil
1383   "List of articles in the current newsgroup that won't be sent.")
1384
1385 (defvar gnus-newsgroup-bookmarks nil
1386   "List of articles in the current newsgroup that have bookmarks.")
1387
1388 (defvar gnus-newsgroup-dormant nil
1389   "Sorted list of dormant articles in the current newsgroup.")
1390
1391 (defvar gnus-newsgroup-unseen nil
1392   "List of unseen articles in the current newsgroup.")
1393
1394 (defvar gnus-newsgroup-seen nil
1395   "Range of seen articles in the current newsgroup.")
1396
1397 (defvar gnus-newsgroup-articles nil
1398   "List of articles in the current newsgroup.")
1399
1400 (defvar gnus-newsgroup-scored nil
1401   "List of scored articles in the current newsgroup.")
1402
1403 (defvar gnus-newsgroup-headers nil
1404   "List of article headers in the current newsgroup.")
1405
1406 (defvar gnus-newsgroup-threads nil)
1407
1408 (defvar gnus-newsgroup-prepared nil
1409   "Whether the current group has been prepared properly.")
1410
1411 (defvar gnus-newsgroup-ancient nil
1412   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1413
1414 (defvar gnus-newsgroup-sparse nil)
1415
1416 (defvar gnus-current-article nil)
1417 (defvar gnus-article-current nil)
1418 (defvar gnus-current-headers nil)
1419 (defvar gnus-have-all-headers nil)
1420 (defvar gnus-last-article nil)
1421 (defvar gnus-newsgroup-history nil)
1422 (defvar gnus-newsgroup-charset nil)
1423 (defvar gnus-newsgroup-ephemeral-charset nil)
1424 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1425
1426 (defvar gnus-article-before-search nil)
1427
1428 (defvar gnus-summary-local-variables
1429   '(gnus-newsgroup-name
1430     gnus-newsgroup-begin gnus-newsgroup-end
1431     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1432     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1433     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1434     gnus-newsgroup-unselected gnus-newsgroup-marked
1435     gnus-newsgroup-spam-marked
1436     gnus-newsgroup-reads gnus-newsgroup-saved
1437     gnus-newsgroup-replied gnus-newsgroup-forwarded
1438     gnus-newsgroup-recent
1439     gnus-newsgroup-expirable
1440     gnus-newsgroup-processable gnus-newsgroup-killed
1441     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1442     gnus-newsgroup-unfetched
1443     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1444     gnus-newsgroup-seen gnus-newsgroup-articles
1445     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1446     gnus-newsgroup-headers gnus-newsgroup-threads
1447     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1448     gnus-current-article gnus-current-headers gnus-have-all-headers
1449     gnus-last-article gnus-article-internal-prepare-hook
1450     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1451     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1452     gnus-thread-expunge-below
1453     gnus-score-alist gnus-current-score-file
1454     (gnus-summary-expunge-below . global)
1455     (gnus-summary-mark-below . global)
1456     (gnus-orphan-score . global)
1457     gnus-newsgroup-active gnus-scores-exclude-files
1458     gnus-newsgroup-history gnus-newsgroup-ancient
1459     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1460     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1461     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1462     (gnus-newsgroup-expunged-tally . 0)
1463     gnus-cache-removable-articles gnus-newsgroup-cached
1464     gnus-newsgroup-data gnus-newsgroup-data-reverse
1465     gnus-newsgroup-limit gnus-newsgroup-limits
1466     gnus-newsgroup-charset gnus-newsgroup-display
1467     gnus-summary-use-undownloaded-faces)
1468   "Variables that are buffer-local to the summary buffers.")
1469
1470 (defvar gnus-newsgroup-variables nil
1471   "A list of variables that have separate values in different newsgroups.
1472 A list of newsgroup (summary buffer) local variables, or cons of
1473 variables and their default expressions to be evalled (when the default
1474 values are not nil), that should be made global while the summary buffer
1475 is active.
1476
1477 Note: The default expressions will be evaluated (using function `eval')
1478 before assignment to the local variable rather than just assigned to it.
1479 If the default expression is the symbol `global', that symbol will not
1480 be evaluated but the global value of the local variable will be used
1481 instead.
1482
1483 These variables can be used to set variables in the group parameters
1484 while still allowing them to affect operations done in other buffers.
1485 For example:
1486
1487 \(setq gnus-newsgroup-variables
1488      '(message-use-followup-to
1489        (gnus-visible-headers .
1490          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1491 ")
1492
1493 ;; Byte-compiler warning.
1494 (eval-when-compile
1495   ;; Bind features so that require will believe that gnus-sum has
1496   ;; already been loaded (avoids infinite recursion)
1497   (let ((features (cons 'gnus-sum features)))
1498     ;; Several of the declarations in gnus-sum are needed to load the
1499     ;; following files. Right now, these definitions have been
1500     ;; compiled but not defined (evaluated).  We could either do a
1501     ;; eval-and-compile about all of the declarations or evaluate the
1502     ;; source file.
1503     (if (boundp 'gnus-newsgroup-variables)
1504         nil
1505       (load "gnus-sum.el" t t t))
1506     (require 'gnus)
1507     (require 'gnus-art)))
1508
1509 ;; MIME stuff.
1510
1511 (defvar gnus-decode-encoded-word-methods
1512   '(mail-decode-encoded-word-string)
1513   "List of methods used to decode encoded words.
1514
1515 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1516 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1517 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1518 whose names match REGEXP.
1519
1520 For example:
1521 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1522  mail-decode-encoded-word-string
1523  (\"chinese\" . rfc1843-decode-string))")
1524
1525 (defvar gnus-decode-encoded-word-methods-cache nil)
1526
1527 (defun gnus-multi-decode-encoded-word-string (string)
1528   "Apply the functions from `gnus-encoded-word-methods' that match."
1529   (unless (and gnus-decode-encoded-word-methods-cache
1530                (eq gnus-newsgroup-name
1531                    (car gnus-decode-encoded-word-methods-cache)))
1532     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1533     (mapcar (lambda (x)
1534               (if (symbolp x)
1535                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1536                 (if (and gnus-newsgroup-name
1537                          (string-match (car x) gnus-newsgroup-name))
1538                     (nconc gnus-decode-encoded-word-methods-cache
1539                            (list (cdr x))))))
1540             gnus-decode-encoded-word-methods))
1541   (let ((xlist gnus-decode-encoded-word-methods-cache))
1542     (pop xlist)
1543     (while xlist
1544       (setq string (funcall (pop xlist) string))))
1545   string)
1546
1547 ;; Subject simplification.
1548
1549 (defun gnus-simplify-whitespace (str)
1550   "Remove excessive whitespace from STR."
1551   ;; Multiple spaces.
1552   (while (string-match "[ \t][ \t]+" str)
1553     (setq str (concat (substring str 0 (match-beginning 0))
1554                         " "
1555                         (substring str (match-end 0)))))
1556   ;; Leading spaces.
1557   (when (string-match "^[ \t]+" str)
1558     (setq str (substring str (match-end 0))))
1559   ;; Trailing spaces.
1560   (when (string-match "[ \t]+$" str)
1561     (setq str (substring str 0 (match-beginning 0))))
1562   str)
1563
1564 (defun gnus-simplify-all-whitespace (str)
1565   "Remove all whitespace from STR."
1566   (while (string-match "[ \t\n]+" str)
1567     (setq str (replace-match "" nil nil str)))
1568   str)
1569
1570 (defsubst gnus-simplify-subject-re (subject)
1571   "Remove \"Re:\" from subject lines."
1572   (if (string-match message-subject-re-regexp subject)
1573       (substring subject (match-end 0))
1574     subject))
1575
1576 (defun gnus-simplify-subject (subject &optional re-only)
1577   "Remove `Re:' and words in parentheses.
1578 If RE-ONLY is non-nil, strip leading `Re:'s only."
1579   (let ((case-fold-search t))           ;Ignore case.
1580     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1581     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1582       (setq subject (substring subject (match-end 0))))
1583     ;; Remove uninteresting prefixes.
1584     (when (and (not re-only)
1585                gnus-simplify-ignored-prefixes
1586                (string-match gnus-simplify-ignored-prefixes subject))
1587       (setq subject (substring subject (match-end 0))))
1588     ;; Remove words in parentheses from end.
1589     (unless re-only
1590       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1591         (setq subject (substring subject 0 (match-beginning 0)))))
1592     ;; Return subject string.
1593     subject))
1594
1595 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1596 ;; all whitespace.
1597 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1598   (goto-char (point-min))
1599   (while (re-search-forward regexp nil t)
1600     (replace-match (or newtext ""))))
1601
1602 (defun gnus-simplify-buffer-fuzzy ()
1603   "Simplify string in the buffer fuzzily.
1604 The string in the accessible portion of the current buffer is simplified.
1605 It is assumed to be a single-line subject.
1606 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1607 matter is removed.  Additional things can be deleted by setting
1608 `gnus-simplify-subject-fuzzy-regexp'."
1609   (let ((case-fold-search t)
1610         (modified-tick))
1611     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1612
1613     (while (not (eq modified-tick (buffer-modified-tick)))
1614       (setq modified-tick (buffer-modified-tick))
1615       (cond
1616        ((listp gnus-simplify-subject-fuzzy-regexp)
1617         (mapcar 'gnus-simplify-buffer-fuzzy-step
1618                 gnus-simplify-subject-fuzzy-regexp))
1619        (gnus-simplify-subject-fuzzy-regexp
1620         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1621       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1622       (gnus-simplify-buffer-fuzzy-step
1623        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1624       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1625
1626     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1627     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1628     (gnus-simplify-buffer-fuzzy-step " $")
1629     (gnus-simplify-buffer-fuzzy-step "^ +")))
1630
1631 (defun gnus-simplify-subject-fuzzy (subject)
1632   "Simplify a subject string fuzzily.
1633 See `gnus-simplify-buffer-fuzzy' for details."
1634   (save-excursion
1635     (gnus-set-work-buffer)
1636     (let ((case-fold-search t))
1637       ;; Remove uninteresting prefixes.
1638       (when (and gnus-simplify-ignored-prefixes
1639                  (string-match gnus-simplify-ignored-prefixes subject))
1640         (setq subject (substring subject (match-end 0))))
1641       (insert subject)
1642       (inline (gnus-simplify-buffer-fuzzy))
1643       (buffer-string))))
1644
1645 (defsubst gnus-simplify-subject-fully (subject)
1646   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1647   (cond
1648    (gnus-simplify-subject-functions
1649     (gnus-map-function gnus-simplify-subject-functions subject))
1650    ((null gnus-summary-gather-subject-limit)
1651     (gnus-simplify-subject-re subject))
1652    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1653     (gnus-simplify-subject-fuzzy subject))
1654    ((numberp gnus-summary-gather-subject-limit)
1655     (gnus-limit-string (gnus-simplify-subject-re subject)
1656                        gnus-summary-gather-subject-limit))
1657    (t
1658     subject)))
1659
1660 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1661   "Check whether two subjects are equal.
1662 If optional argument SIMPLE-FIRST is t, first argument is already
1663 simplified."
1664   (cond
1665    ((null simple-first)
1666     (equal (gnus-simplify-subject-fully s1)
1667            (gnus-simplify-subject-fully s2)))
1668    (t
1669     (equal s1
1670            (gnus-simplify-subject-fully s2)))))
1671
1672 (defun gnus-summary-bubble-group ()
1673   "Increase the score of the current group.
1674 This is a handy function to add to `gnus-summary-exit-hook' to
1675 increase the score of each group you read."
1676   (gnus-group-add-score gnus-newsgroup-name))
1677
1678 \f
1679 ;;;
1680 ;;; Gnus summary mode
1681 ;;;
1682
1683 (put 'gnus-summary-mode 'mode-class 'special)
1684
1685 (defvar gnus-article-commands-menu)
1686
1687 ;; Non-orthogonal keys
1688
1689 (gnus-define-keys gnus-summary-mode-map
1690   " " gnus-summary-next-page
1691   "\177" gnus-summary-prev-page
1692   [delete] gnus-summary-prev-page
1693   [backspace] gnus-summary-prev-page
1694   "\r" gnus-summary-scroll-up
1695   "\M-\r" gnus-summary-scroll-down
1696   "n" gnus-summary-next-unread-article
1697   "p" gnus-summary-prev-unread-article
1698   "N" gnus-summary-next-article
1699   "P" gnus-summary-prev-article
1700   "\M-\C-n" gnus-summary-next-same-subject
1701   "\M-\C-p" gnus-summary-prev-same-subject
1702   "\M-n" gnus-summary-next-unread-subject
1703   "\M-p" gnus-summary-prev-unread-subject
1704   "." gnus-summary-first-unread-article
1705   "," gnus-summary-best-unread-article
1706   "\M-s" gnus-summary-search-article-forward
1707   "\M-r" gnus-summary-search-article-backward
1708   "<" gnus-summary-beginning-of-article
1709   ">" gnus-summary-end-of-article
1710   "j" gnus-summary-goto-article
1711   "^" gnus-summary-refer-parent-article
1712   "\M-^" gnus-summary-refer-article
1713   "u" gnus-summary-tick-article-forward
1714   "!" gnus-summary-tick-article-forward
1715   "U" gnus-summary-tick-article-backward
1716   "d" gnus-summary-mark-as-read-forward
1717   "D" gnus-summary-mark-as-read-backward
1718   "E" gnus-summary-mark-as-expirable
1719   "\M-u" gnus-summary-clear-mark-forward
1720   "\M-U" gnus-summary-clear-mark-backward
1721   "k" gnus-summary-kill-same-subject-and-select
1722   "\C-k" gnus-summary-kill-same-subject
1723   "\M-\C-k" gnus-summary-kill-thread
1724   "\M-\C-l" gnus-summary-lower-thread
1725   "e" gnus-summary-edit-article
1726   "#" gnus-summary-mark-as-processable
1727   "\M-#" gnus-summary-unmark-as-processable
1728   "\M-\C-t" gnus-summary-toggle-threads
1729   "\M-\C-s" gnus-summary-show-thread
1730   "\M-\C-h" gnus-summary-hide-thread
1731   "\M-\C-f" gnus-summary-next-thread
1732   "\M-\C-b" gnus-summary-prev-thread
1733   [(meta down)] gnus-summary-next-thread
1734   [(meta up)] gnus-summary-prev-thread
1735   "\M-\C-u" gnus-summary-up-thread
1736   "\M-\C-d" gnus-summary-down-thread
1737   "&" gnus-summary-execute-command
1738   "c" gnus-summary-catchup-and-exit
1739   "\C-w" gnus-summary-mark-region-as-read
1740   "\C-t" gnus-summary-toggle-truncation
1741   "?" gnus-summary-mark-as-dormant
1742   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1743   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1744   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1745   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1746   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1747   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1748   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1749   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1750   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1751   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1752   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1753   "=" gnus-summary-expand-window
1754   "\C-x\C-s" gnus-summary-reselect-current-group
1755   "\M-g" gnus-summary-rescan-group
1756   "w" gnus-summary-stop-page-breaking
1757   "\C-c\C-r" gnus-summary-caesar-message
1758   "f" gnus-summary-followup
1759   "F" gnus-summary-followup-with-original
1760   "C" gnus-summary-cancel-article
1761   "r" gnus-summary-reply
1762   "R" gnus-summary-reply-with-original
1763   "\C-c\C-f" gnus-summary-mail-forward
1764   "o" gnus-summary-save-article
1765   "\C-o" gnus-summary-save-article-mail
1766   "|" gnus-summary-pipe-output
1767   "\M-k" gnus-summary-edit-local-kill
1768   "\M-K" gnus-summary-edit-global-kill
1769   ;; "V" gnus-version
1770   "\C-c\C-d" gnus-summary-describe-group
1771   "q" gnus-summary-exit
1772   "Q" gnus-summary-exit-no-update
1773   "\C-c\C-i" gnus-info-find-node
1774   gnus-mouse-2 gnus-mouse-pick-article
1775   [follow-link] mouse-face
1776   "m" gnus-summary-mail-other-window
1777   "a" gnus-summary-post-news
1778   "i" gnus-summary-news-other-window
1779   "x" gnus-summary-limit-to-unread
1780   "s" gnus-summary-isearch-article
1781   "t" gnus-summary-toggle-header
1782   "g" gnus-summary-show-article
1783   "l" gnus-summary-goto-last-article
1784   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1785   "\C-d" gnus-summary-enter-digest-group
1786   "\M-\C-d" gnus-summary-read-document
1787   "\M-\C-e" gnus-summary-edit-parameters
1788   "\M-\C-a" gnus-summary-customize-parameters
1789   "\C-c\C-b" gnus-bug
1790   "*" gnus-cache-enter-article
1791   "\M-*" gnus-cache-remove-article
1792   "\M-&" gnus-summary-universal-argument
1793   "\C-l" gnus-recenter
1794   "I" gnus-summary-increase-score
1795   "L" gnus-summary-lower-score
1796   "\M-i" gnus-symbolic-argument
1797   "h" gnus-summary-select-article-buffer
1798
1799   "b" gnus-article-view-part
1800   "\M-t" gnus-summary-toggle-display-buttonized
1801
1802   "V" gnus-summary-score-map
1803   "X" gnus-uu-extract-map
1804   "S" gnus-summary-send-map)
1805
1806 ;; Sort of orthogonal keymap
1807 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1808   "t" gnus-summary-tick-article-forward
1809   "!" gnus-summary-tick-article-forward
1810   "d" gnus-summary-mark-as-read-forward
1811   "r" gnus-summary-mark-as-read-forward
1812   "c" gnus-summary-clear-mark-forward
1813   " " gnus-summary-clear-mark-forward
1814   "e" gnus-summary-mark-as-expirable
1815   "x" gnus-summary-mark-as-expirable
1816   "?" gnus-summary-mark-as-dormant
1817   "b" gnus-summary-set-bookmark
1818   "B" gnus-summary-remove-bookmark
1819   "#" gnus-summary-mark-as-processable
1820   "\M-#" gnus-summary-unmark-as-processable
1821   "S" gnus-summary-limit-include-expunged
1822   "C" gnus-summary-catchup
1823   "H" gnus-summary-catchup-to-here
1824   "h" gnus-summary-catchup-from-here
1825   "\C-c" gnus-summary-catchup-all
1826   "k" gnus-summary-kill-same-subject-and-select
1827   "K" gnus-summary-kill-same-subject
1828   "P" gnus-uu-mark-map)
1829
1830 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1831   "c" gnus-summary-clear-above
1832   "u" gnus-summary-tick-above
1833   "m" gnus-summary-mark-above
1834   "k" gnus-summary-kill-below)
1835
1836 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1837   "/" gnus-summary-limit-to-subject
1838   "n" gnus-summary-limit-to-articles
1839   "w" gnus-summary-pop-limit
1840   "s" gnus-summary-limit-to-subject
1841   "a" gnus-summary-limit-to-author
1842   "u" gnus-summary-limit-to-unread
1843   "m" gnus-summary-limit-to-marks
1844   "M" gnus-summary-limit-exclude-marks
1845   "v" gnus-summary-limit-to-score
1846   "*" gnus-summary-limit-include-cached
1847   "D" gnus-summary-limit-include-dormant
1848   "T" gnus-summary-limit-include-thread
1849   "d" gnus-summary-limit-exclude-dormant
1850   "t" gnus-summary-limit-to-age
1851   "." gnus-summary-limit-to-unseen
1852   "x" gnus-summary-limit-to-extra
1853   "p" gnus-summary-limit-to-display-predicate
1854   "E" gnus-summary-limit-include-expunged
1855   "c" gnus-summary-limit-exclude-childless-dormant
1856   "C" gnus-summary-limit-mark-excluded-as-read
1857   "o" gnus-summary-insert-old-articles
1858   "N" gnus-summary-insert-new-articles
1859   "r" gnus-summary-limit-to-replied
1860   "R" gnus-summary-limit-to-recipient)
1861
1862 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1863   "n" gnus-summary-next-unread-article
1864   "p" gnus-summary-prev-unread-article
1865   "N" gnus-summary-next-article
1866   "P" gnus-summary-prev-article
1867   "\C-n" gnus-summary-next-same-subject
1868   "\C-p" gnus-summary-prev-same-subject
1869   "\M-n" gnus-summary-next-unread-subject
1870   "\M-p" gnus-summary-prev-unread-subject
1871   "f" gnus-summary-first-unread-article
1872   "b" gnus-summary-best-unread-article
1873   "j" gnus-summary-goto-article
1874   "g" gnus-summary-goto-subject
1875   "l" gnus-summary-goto-last-article
1876   "o" gnus-summary-pop-article)
1877
1878 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1879   "k" gnus-summary-kill-thread
1880   "l" gnus-summary-lower-thread
1881   "i" gnus-summary-raise-thread
1882   "T" gnus-summary-toggle-threads
1883   "t" gnus-summary-rethread-current
1884   "^" gnus-summary-reparent-thread
1885   "s" gnus-summary-show-thread
1886   "S" gnus-summary-show-all-threads
1887   "h" gnus-summary-hide-thread
1888   "H" gnus-summary-hide-all-threads
1889   "n" gnus-summary-next-thread
1890   "p" gnus-summary-prev-thread
1891   "u" gnus-summary-up-thread
1892   "o" gnus-summary-top-thread
1893   "d" gnus-summary-down-thread
1894   "#" gnus-uu-mark-thread
1895   "\M-#" gnus-uu-unmark-thread)
1896
1897 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1898   "g" gnus-summary-prepare
1899   "c" gnus-summary-insert-cached-articles
1900   "d" gnus-summary-insert-dormant-articles)
1901
1902 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1903   "c" gnus-summary-catchup-and-exit
1904   "C" gnus-summary-catchup-all-and-exit
1905   "E" gnus-summary-exit-no-update
1906   "Q" gnus-summary-exit
1907   "Z" gnus-summary-exit
1908   "n" gnus-summary-catchup-and-goto-next-group
1909   "R" gnus-summary-reselect-current-group
1910   "G" gnus-summary-rescan-group
1911   "N" gnus-summary-next-group
1912   "s" gnus-summary-save-newsrc
1913   "P" gnus-summary-prev-group)
1914
1915 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1916   " " gnus-summary-next-page
1917   "n" gnus-summary-next-page
1918   "\177" gnus-summary-prev-page
1919   [delete] gnus-summary-prev-page
1920   "p" gnus-summary-prev-page
1921   "\r" gnus-summary-scroll-up
1922   "\M-\r" gnus-summary-scroll-down
1923   "<" gnus-summary-beginning-of-article
1924   ">" gnus-summary-end-of-article
1925   "b" gnus-summary-beginning-of-article
1926   "e" gnus-summary-end-of-article
1927   "^" gnus-summary-refer-parent-article
1928   "r" gnus-summary-refer-parent-article
1929   "D" gnus-summary-enter-digest-group
1930   "R" gnus-summary-refer-references
1931   "T" gnus-summary-refer-thread
1932   "g" gnus-summary-show-article
1933   "s" gnus-summary-isearch-article
1934   "P" gnus-summary-print-article
1935   "M" gnus-mailing-list-insinuate
1936   "t" gnus-article-babel)
1937
1938 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1939   "b" gnus-article-add-buttons
1940   "B" gnus-article-add-buttons-to-head
1941   "o" gnus-article-treat-overstrike
1942   "e" gnus-article-emphasize
1943   "w" gnus-article-fill-cited-article
1944   "Q" gnus-article-fill-long-lines
1945   "C" gnus-article-capitalize-sentences
1946   "c" gnus-article-remove-cr
1947   "q" gnus-article-de-quoted-unreadable
1948   "6" gnus-article-de-base64-unreadable
1949   "Z" gnus-article-decode-HZ
1950   "A" gnus-article-treat-ansi-sequences
1951   "h" gnus-article-wash-html
1952   "u" gnus-article-unsplit-urls
1953   "s" gnus-summary-force-verify-and-decrypt
1954   "f" gnus-article-display-x-face
1955   "l" gnus-summary-stop-page-breaking
1956   "r" gnus-summary-caesar-message
1957   "m" gnus-summary-morse-message
1958   "t" gnus-summary-toggle-header
1959   "g" gnus-treat-smiley
1960   "v" gnus-summary-verbose-headers
1961   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1962   "p" gnus-article-verify-x-pgp-sig
1963   "d" gnus-article-treat-dumbquotes
1964   "i" gnus-summary-idna-message)
1965
1966 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1967   ;; mnemonic: deuglif*Y*
1968   "u" gnus-article-outlook-unwrap-lines
1969   "a" gnus-article-outlook-repair-attribution
1970   "c" gnus-article-outlook-rearrange-citation
1971   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1972
1973 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1974   "a" gnus-article-hide
1975   "h" gnus-article-hide-headers
1976   "b" gnus-article-hide-boring-headers
1977   "s" gnus-article-hide-signature
1978   "c" gnus-article-hide-citation
1979   "C" gnus-article-hide-citation-in-followups
1980   "l" gnus-article-hide-list-identifiers
1981   "B" gnus-article-strip-banner
1982   "P" gnus-article-hide-pem
1983   "\C-c" gnus-article-hide-citation-maybe)
1984
1985 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1986   "a" gnus-article-highlight
1987   "h" gnus-article-highlight-headers
1988   "c" gnus-article-highlight-citation
1989   "s" gnus-article-highlight-signature)
1990
1991 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1992   "f" gnus-article-treat-fold-headers
1993   "u" gnus-article-treat-unfold-headers
1994   "n" gnus-article-treat-fold-newsgroups)
1995
1996 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1997   "x" gnus-article-display-x-face
1998   "d" gnus-article-display-face
1999   "s" gnus-treat-smiley
2000   "D" gnus-article-remove-images
2001   "f" gnus-treat-from-picon
2002   "m" gnus-treat-mail-picon
2003   "n" gnus-treat-newsgroups-picon)
2004
2005 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2006   "w" gnus-article-decode-mime-words
2007   "c" gnus-article-decode-charset
2008   "v" gnus-mime-view-all-parts
2009   "b" gnus-article-view-part)
2010
2011 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2012   "z" gnus-article-date-ut
2013   "u" gnus-article-date-ut
2014   "l" gnus-article-date-local
2015   "p" gnus-article-date-english
2016   "e" gnus-article-date-lapsed
2017   "o" gnus-article-date-original
2018   "i" gnus-article-date-iso8601
2019   "s" gnus-article-date-user)
2020
2021 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2022   "t" gnus-article-remove-trailing-blank-lines
2023   "l" gnus-article-strip-leading-blank-lines
2024   "m" gnus-article-strip-multiple-blank-lines
2025   "a" gnus-article-strip-blank-lines
2026   "A" gnus-article-strip-all-blank-lines
2027   "s" gnus-article-strip-leading-space
2028   "e" gnus-article-strip-trailing-space
2029   "w" gnus-article-remove-leading-whitespace)
2030
2031 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2032   "v" gnus-version
2033   "f" gnus-summary-fetch-faq
2034   "d" gnus-summary-describe-group
2035   "h" gnus-summary-describe-briefly
2036   "i" gnus-info-find-node
2037   "c" gnus-group-fetch-charter
2038   "C" gnus-group-fetch-control)
2039
2040 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2041   "e" gnus-summary-expire-articles
2042   "\M-\C-e" gnus-summary-expire-articles-now
2043   "\177" gnus-summary-delete-article
2044   [delete] gnus-summary-delete-article
2045   [backspace] gnus-summary-delete-article
2046   "m" gnus-summary-move-article
2047   "r" gnus-summary-respool-article
2048   "w" gnus-summary-edit-article
2049   "c" gnus-summary-copy-article
2050   "B" gnus-summary-crosspost-article
2051   "q" gnus-summary-respool-query
2052   "t" gnus-summary-respool-trace
2053   "i" gnus-summary-import-article
2054   "I" gnus-summary-create-article
2055   "p" gnus-summary-article-posted-p)
2056
2057 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2058   "o" gnus-summary-save-article
2059   "m" gnus-summary-save-article-mail
2060   "F" gnus-summary-write-article-file
2061   "r" gnus-summary-save-article-rmail
2062   "f" gnus-summary-save-article-file
2063   "b" gnus-summary-save-article-body-file
2064   "h" gnus-summary-save-article-folder
2065   "v" gnus-summary-save-article-vm
2066   "p" gnus-summary-pipe-output
2067   "P" gnus-summary-muttprint
2068   "s" gnus-soup-add-article)
2069
2070 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2071   "b" gnus-summary-display-buttonized
2072   "m" gnus-summary-repair-multipart
2073   "v" gnus-article-view-part
2074   "o" gnus-article-save-part
2075   "c" gnus-article-copy-part
2076   "C" gnus-article-view-part-as-charset
2077   "e" gnus-article-view-part-externally
2078   "E" gnus-article-encrypt-body
2079   "i" gnus-article-inline-part
2080   "|" gnus-article-pipe-part)
2081
2082 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2083   "p" gnus-summary-mark-as-processable
2084   "u" gnus-summary-unmark-as-processable
2085   "U" gnus-summary-unmark-all-processable
2086   "v" gnus-uu-mark-over
2087   "s" gnus-uu-mark-series
2088   "r" gnus-uu-mark-region
2089   "g" gnus-uu-unmark-region
2090   "R" gnus-uu-mark-by-regexp
2091   "G" gnus-uu-unmark-by-regexp
2092   "t" gnus-uu-mark-thread
2093   "T" gnus-uu-unmark-thread
2094   "a" gnus-uu-mark-all
2095   "b" gnus-uu-mark-buffer
2096   "S" gnus-uu-mark-sparse
2097   "k" gnus-summary-kill-process-mark
2098   "y" gnus-summary-yank-process-mark
2099   "w" gnus-summary-save-process-mark
2100   "i" gnus-uu-invert-processable)
2101
2102 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2103   ;;"x" gnus-uu-extract-any
2104   "m" gnus-summary-save-parts
2105   "u" gnus-uu-decode-uu
2106   "U" gnus-uu-decode-uu-and-save
2107   "s" gnus-uu-decode-unshar
2108   "S" gnus-uu-decode-unshar-and-save
2109   "o" gnus-uu-decode-save
2110   "O" gnus-uu-decode-save
2111   "b" gnus-uu-decode-binhex
2112   "B" gnus-uu-decode-binhex
2113   "p" gnus-uu-decode-postscript
2114   "P" gnus-uu-decode-postscript-and-save)
2115
2116 (gnus-define-keys
2117     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2118   "u" gnus-uu-decode-uu-view
2119   "U" gnus-uu-decode-uu-and-save-view
2120   "s" gnus-uu-decode-unshar-view
2121   "S" gnus-uu-decode-unshar-and-save-view
2122   "o" gnus-uu-decode-save-view
2123   "O" gnus-uu-decode-save-view
2124   "b" gnus-uu-decode-binhex-view
2125   "B" gnus-uu-decode-binhex-view
2126   "p" gnus-uu-decode-postscript-view
2127   "P" gnus-uu-decode-postscript-and-save-view)
2128
2129 (defvar gnus-article-post-menu nil)
2130
2131 (defconst gnus-summary-menu-maxlen 20)
2132
2133 (defun gnus-summary-menu-split (menu)
2134   ;; If we have lots of elements, divide them into groups of 20
2135   ;; and make a pane (or submenu) for each one.
2136   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2137       (let ((menu menu) sublists next
2138             (i 1))
2139         (while menu
2140           ;; Pull off the next gnus-summary-menu-maxlen elements
2141           ;; and make them the next element of sublist.
2142           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2143           (if next
2144               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2145                       nil))
2146           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2147                                              (aref (car (last menu)) 0)) menu)
2148                                sublists))
2149           (setq i (1+ i))
2150           (setq menu next))
2151         (nreverse sublists))
2152     ;; Few elements--put them all in one pane.
2153     menu))
2154
2155 (defun gnus-summary-make-menu-bar ()
2156   (gnus-turn-off-edit-menu 'summary)
2157
2158   (unless (boundp 'gnus-summary-misc-menu)
2159
2160     (easy-menu-define
2161       gnus-summary-kill-menu gnus-summary-mode-map ""
2162       (cons
2163        "Score"
2164        (nconc
2165         (list
2166          ["Customize" gnus-score-customize t])
2167         (gnus-make-score-map 'increase)
2168         (gnus-make-score-map 'lower)
2169         '(("Mark"
2170            ["Kill below" gnus-summary-kill-below t]
2171            ["Mark above" gnus-summary-mark-above t]
2172            ["Tick above" gnus-summary-tick-above t]
2173            ["Clear above" gnus-summary-clear-above t])
2174           ["Current score" gnus-summary-current-score t]
2175           ["Set score" gnus-summary-set-score t]
2176           ["Switch current score file..." gnus-score-change-score-file t]
2177           ["Set mark below..." gnus-score-set-mark-below t]
2178           ["Set expunge below..." gnus-score-set-expunge-below t]
2179           ["Edit current score file" gnus-score-edit-current-scores t]
2180           ["Edit score file" gnus-score-edit-file t]
2181           ["Trace score" gnus-score-find-trace t]
2182           ["Find words" gnus-score-find-favourite-words t]
2183           ["Rescore buffer" gnus-summary-rescore t]
2184           ["Increase score..." gnus-summary-increase-score t]
2185           ["Lower score..." gnus-summary-lower-score t]))))
2186
2187     ;; Define both the Article menu in the summary buffer and the
2188     ;; equivalent Commands menu in the article buffer here for
2189     ;; consistency.
2190     (let ((innards
2191            `(("Hide"
2192               ["All" gnus-article-hide t]
2193               ["Headers" gnus-article-hide-headers t]
2194               ["Signature" gnus-article-hide-signature t]
2195               ["Citation" gnus-article-hide-citation t]
2196               ["List identifiers" gnus-article-hide-list-identifiers t]
2197               ["Banner" gnus-article-strip-banner t]
2198               ["Boring headers" gnus-article-hide-boring-headers t])
2199              ("Highlight"
2200               ["All" gnus-article-highlight t]
2201               ["Headers" gnus-article-highlight-headers t]
2202               ["Signature" gnus-article-highlight-signature t]
2203               ["Citation" gnus-article-highlight-citation t])
2204              ("MIME"
2205               ["Words" gnus-article-decode-mime-words t]
2206               ["Charset" gnus-article-decode-charset t]
2207               ["QP" gnus-article-de-quoted-unreadable t]
2208               ["Base64" gnus-article-de-base64-unreadable t]
2209               ["View MIME buttons" gnus-summary-display-buttonized t]
2210               ["View all" gnus-mime-view-all-parts t]
2211               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2212               ["Encrypt body" gnus-article-encrypt-body
2213                :active (not (gnus-group-read-only-p))
2214                ,@(if (featurep 'xemacs) nil
2215                    '(:help "Encrypt the message body on disk"))]
2216               ["Extract all parts..." gnus-summary-save-parts t]
2217               ("Multipart"
2218                ["Repair multipart" gnus-summary-repair-multipart t]
2219                ["Pipe part..." gnus-article-pipe-part t]
2220                ["Inline part" gnus-article-inline-part t]
2221                ["Encrypt body" gnus-article-encrypt-body
2222                 :active (not (gnus-group-read-only-p))
2223                ,@(if (featurep 'xemacs) nil
2224                    '(:help "Encrypt the message body on disk"))]
2225                ["View part externally" gnus-article-view-part-externally t]
2226                ["View part with charset..." gnus-article-view-part-as-charset t]
2227                ["Copy part" gnus-article-copy-part t]
2228                ["Save part..." gnus-article-save-part t]
2229                ["View part" gnus-article-view-part t]))
2230              ("Date"
2231               ["Local" gnus-article-date-local t]
2232               ["ISO8601" gnus-article-date-iso8601 t]
2233               ["UT" gnus-article-date-ut t]
2234               ["Original" gnus-article-date-original t]
2235               ["Lapsed" gnus-article-date-lapsed t]
2236               ["User-defined" gnus-article-date-user t])
2237              ("Display"
2238               ["Remove images" gnus-article-remove-images t]
2239               ["Toggle smiley" gnus-treat-smiley t]
2240               ["Show X-Face" gnus-article-display-x-face t]
2241               ["Show picons in From" gnus-treat-from-picon t]
2242               ["Show picons in mail headers" gnus-treat-mail-picon t]
2243               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2244               ("View as different encoding"
2245                ,@(gnus-summary-menu-split
2246                   (mapcar
2247                    (lambda (cs)
2248                      ;; Since easymenu under Emacs doesn't allow
2249                      ;; lambda forms for menu commands, we should
2250                      ;; provide intern'ed function symbols.
2251                      (let ((command (intern (format "\
2252 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2253                        (fset command
2254                              `(lambda ()
2255                                 (interactive)
2256                                 (let ((gnus-summary-show-article-charset-alist
2257                                        '((1 . ,cs))))
2258                                   (gnus-summary-show-article 1))))
2259                        `[,(symbol-name cs) ,command t]))
2260                    (sort (if (fboundp 'coding-system-list)
2261                              (coding-system-list)
2262                            (mapcar 'car mm-mime-mule-charset-alist))
2263                          'string<)))))
2264              ("Washing"
2265               ("Remove Blanks"
2266                ["Leading" gnus-article-strip-leading-blank-lines t]
2267                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2268                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2269                ["All of the above" gnus-article-strip-blank-lines t]
2270                ["All" gnus-article-strip-all-blank-lines t]
2271                ["Leading space" gnus-article-strip-leading-space t]
2272                ["Trailing space" gnus-article-strip-trailing-space t]
2273                ["Leading space in headers"
2274                 gnus-article-remove-leading-whitespace t])
2275               ["Overstrike" gnus-article-treat-overstrike t]
2276               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2277               ["Emphasis" gnus-article-emphasize t]
2278               ["Word wrap" gnus-article-fill-cited-article t]
2279               ["Fill long lines" gnus-article-fill-long-lines t]
2280               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2281               ["Remove CR" gnus-article-remove-cr t]
2282               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2283               ["Base64" gnus-article-de-base64-unreadable t]
2284               ["Rot 13" gnus-summary-caesar-message
2285                ,@(if (featurep 'xemacs) '(t)
2286                    '(:help "\"Caesar rotate\" article by 13"))]
2287               ["De-IDNA" gnus-summary-idna-message t]
2288               ["Morse decode" gnus-summary-morse-message t]
2289               ["Unix pipe..." gnus-summary-pipe-message t]
2290               ["Add buttons" gnus-article-add-buttons t]
2291               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2292               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2293               ["Verbose header" gnus-summary-verbose-headers t]
2294               ["Toggle header" gnus-summary-toggle-header t]
2295               ["Unfold headers" gnus-article-treat-unfold-headers t]
2296               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2297               ["Html" gnus-article-wash-html t]
2298               ["Unsplit URLs" gnus-article-unsplit-urls t]
2299               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2300               ["Decode HZ" gnus-article-decode-HZ t]
2301               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2302               ("(Outlook) Deuglify"
2303                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2304                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2305                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2306                ["Full (Outlook) deuglify"
2307                 gnus-article-outlook-deuglify-article t])
2308               )
2309              ("Output"
2310               ["Save in default format..." gnus-summary-save-article
2311                ,@(if (featurep 'xemacs) '(t)
2312                    '(:help "Save article using default method"))]
2313               ["Save in file..." gnus-summary-save-article-file
2314                ,@(if (featurep 'xemacs) '(t)
2315                    '(:help "Save article in file"))]
2316               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2317               ["Save in MH folder..." gnus-summary-save-article-folder t]
2318               ["Save in VM folder..." gnus-summary-save-article-vm t]
2319               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2320               ["Save body in file..." gnus-summary-save-article-body-file t]
2321               ["Pipe through a filter..." gnus-summary-pipe-output t]
2322               ["Add to SOUP packet" gnus-soup-add-article t]
2323               ["Print with Muttprint..." gnus-summary-muttprint t]
2324               ["Print" gnus-summary-print-article
2325                ,@(if (featurep 'xemacs) '(t)
2326                    '(:help "Generate and print a PostScript image"))])
2327              ("Copy, move,... (Backend)"
2328               ,@(if (featurep 'xemacs) nil
2329                   '(:help "Copying, moving, expiring articles..."))
2330               ["Respool article..." gnus-summary-respool-article t]
2331               ["Move article..." gnus-summary-move-article
2332                (gnus-check-backend-function
2333                 'request-move-article gnus-newsgroup-name)]
2334               ["Copy article..." gnus-summary-copy-article t]
2335               ["Crosspost article..." gnus-summary-crosspost-article
2336                (gnus-check-backend-function
2337                 'request-replace-article gnus-newsgroup-name)]
2338               ["Import file..." gnus-summary-import-article
2339                (gnus-check-backend-function
2340                 'request-accept-article gnus-newsgroup-name)]
2341               ["Create article..." gnus-summary-create-article
2342                (gnus-check-backend-function
2343                 'request-accept-article gnus-newsgroup-name)]
2344               ["Check if posted" gnus-summary-article-posted-p t]
2345               ["Edit article" gnus-summary-edit-article
2346                (not (gnus-group-read-only-p))]
2347               ["Delete article" gnus-summary-delete-article
2348                (gnus-check-backend-function
2349                 'request-expire-articles gnus-newsgroup-name)]
2350               ["Query respool" gnus-summary-respool-query t]
2351               ["Trace respool" gnus-summary-respool-trace t]
2352               ["Delete expirable articles" gnus-summary-expire-articles-now
2353                (gnus-check-backend-function
2354                 'request-expire-articles gnus-newsgroup-name)])
2355              ("Extract"
2356               ["Uudecode" gnus-uu-decode-uu
2357                ,@(if (featurep 'xemacs) '(t)
2358                    '(:help "Decode uuencoded article(s)"))]
2359               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2360               ["Unshar" gnus-uu-decode-unshar t]
2361               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2362               ["Save" gnus-uu-decode-save t]
2363               ["Binhex" gnus-uu-decode-binhex t]
2364               ["Postscript" gnus-uu-decode-postscript t]
2365               ["All MIME parts" gnus-summary-save-parts t])
2366              ("Cache"
2367               ["Enter article" gnus-cache-enter-article t]
2368               ["Remove article" gnus-cache-remove-article t])
2369              ["Translate" gnus-article-babel t]
2370              ["Select article buffer" gnus-summary-select-article-buffer t]
2371              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2372              ["Isearch article..." gnus-summary-isearch-article t]
2373              ["Beginning of the article" gnus-summary-beginning-of-article t]
2374              ["End of the article" gnus-summary-end-of-article t]
2375              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2376              ["Fetch referenced articles" gnus-summary-refer-references t]
2377              ["Fetch current thread" gnus-summary-refer-thread t]
2378              ["Fetch article with id..." gnus-summary-refer-article t]
2379              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2380              ["Redisplay" gnus-summary-show-article t]
2381              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2382       (easy-menu-define
2383         gnus-summary-article-menu gnus-summary-mode-map ""
2384         (cons "Article" innards))
2385
2386       (if (not (keymapp gnus-summary-article-menu))
2387           (easy-menu-define
2388             gnus-article-commands-menu gnus-article-mode-map ""
2389             (cons "Commands" innards))
2390         ;; in Emacs, don't share menu.
2391         (setq gnus-article-commands-menu
2392               (copy-keymap gnus-summary-article-menu))
2393         (define-key gnus-article-mode-map [menu-bar commands]
2394           (cons "Commands" gnus-article-commands-menu))))
2395
2396     (easy-menu-define
2397       gnus-summary-thread-menu gnus-summary-mode-map ""
2398       '("Threads"
2399         ["Find all messages in thread" gnus-summary-refer-thread t]
2400         ["Toggle threading" gnus-summary-toggle-threads t]
2401         ["Hide threads" gnus-summary-hide-all-threads t]
2402         ["Show threads" gnus-summary-show-all-threads t]
2403         ["Hide thread" gnus-summary-hide-thread t]
2404         ["Show thread" gnus-summary-show-thread t]
2405         ["Go to next thread" gnus-summary-next-thread t]
2406         ["Go to previous thread" gnus-summary-prev-thread t]
2407         ["Go down thread" gnus-summary-down-thread t]
2408         ["Go up thread" gnus-summary-up-thread t]
2409         ["Top of thread" gnus-summary-top-thread t]
2410         ["Mark thread as read" gnus-summary-kill-thread t]
2411         ["Lower thread score" gnus-summary-lower-thread t]
2412         ["Raise thread score" gnus-summary-raise-thread t]
2413         ["Rethread current" gnus-summary-rethread-current t]))
2414
2415     (easy-menu-define
2416       gnus-summary-post-menu gnus-summary-mode-map ""
2417       `("Post"
2418         ["Send a message (mail or news)" gnus-summary-post-news
2419          ,@(if (featurep 'xemacs) '(t)
2420              '(:help "Compose a new message (mail or news)"))]
2421         ["Followup" gnus-summary-followup
2422          ,@(if (featurep 'xemacs) '(t)
2423              '(:help "Post followup to this article"))]
2424         ["Followup and yank" gnus-summary-followup-with-original
2425          ,@(if (featurep 'xemacs) '(t)
2426              '(:help "Post followup to this article, quoting its contents"))]
2427         ["Supersede article" gnus-summary-supersede-article t]
2428         ["Cancel article" gnus-summary-cancel-article
2429          ,@(if (featurep 'xemacs) '(t)
2430              '(:help "Cancel an article you posted"))]
2431         ["Reply" gnus-summary-reply t]
2432         ["Reply and yank" gnus-summary-reply-with-original t]
2433         ["Wide reply" gnus-summary-wide-reply t]
2434         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2435          ,@(if (featurep 'xemacs) '(t)
2436              '(:help "Mail a reply, quoting this article"))]
2437         ["Very wide reply" gnus-summary-very-wide-reply t]
2438         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2439          ,@(if (featurep 'xemacs) '(t)
2440              '(:help "Mail a very wide reply, quoting this article"))]
2441         ["Mail forward" gnus-summary-mail-forward t]
2442         ["Post forward" gnus-summary-post-forward t]
2443         ["Digest and mail" gnus-uu-digest-mail-forward t]
2444         ["Digest and post" gnus-uu-digest-post-forward t]
2445         ["Resend message" gnus-summary-resend-message t]
2446         ["Resend message edit" gnus-summary-resend-message-edit t]
2447         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2448         ["Send a mail" gnus-summary-mail-other-window t]
2449         ["Create a local message" gnus-summary-news-other-window t]
2450         ["Uuencode and post" gnus-uu-post-news
2451          ,@(if (featurep 'xemacs) '(t)
2452              '(:help "Post a uuencoded article"))]
2453         ["Followup via news" gnus-summary-followup-to-mail t]
2454         ["Followup via news and yank"
2455          gnus-summary-followup-to-mail-with-original t]
2456         ;;("Draft"
2457         ;;["Send" gnus-summary-send-draft t]
2458         ;;["Send bounced" gnus-resend-bounced-mail t])
2459         ))
2460
2461     (cond
2462      ((not (keymapp gnus-summary-post-menu))
2463       (setq gnus-article-post-menu gnus-summary-post-menu))
2464      ((not gnus-article-post-menu)
2465       ;; Don't share post menu.
2466       (setq gnus-article-post-menu
2467             (copy-keymap gnus-summary-post-menu))))
2468     (define-key gnus-article-mode-map [menu-bar post]
2469       (cons "Post" gnus-article-post-menu))
2470
2471     (easy-menu-define
2472       gnus-summary-misc-menu gnus-summary-mode-map ""
2473       `("Gnus"
2474         ("Mark Read"
2475          ["Mark as read" gnus-summary-mark-as-read-forward t]
2476          ["Mark same subject and select"
2477           gnus-summary-kill-same-subject-and-select t]
2478          ["Mark same subject" gnus-summary-kill-same-subject t]
2479          ["Catchup" gnus-summary-catchup
2480           ,@(if (featurep 'xemacs) '(t)
2481               '(:help "Mark unread articles in this group as read"))]
2482          ["Catchup all" gnus-summary-catchup-all t]
2483          ["Catchup to here" gnus-summary-catchup-to-here t]
2484          ["Catchup from here" gnus-summary-catchup-from-here t]
2485          ["Catchup region" gnus-summary-mark-region-as-read
2486           (gnus-mark-active-p)]
2487          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2488         ("Mark Various"
2489          ["Tick" gnus-summary-tick-article-forward t]
2490          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2491          ["Remove marks" gnus-summary-clear-mark-forward t]
2492          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2493          ["Set bookmark" gnus-summary-set-bookmark t]
2494          ["Remove bookmark" gnus-summary-remove-bookmark t])
2495         ("Limit to"
2496          ["Marks..." gnus-summary-limit-to-marks t]
2497          ["Subject..." gnus-summary-limit-to-subject t]
2498          ["Author..." gnus-summary-limit-to-author t]
2499          ["Recipient..." gnus-summary-limit-to-recipient t]
2500          ["Age..." gnus-summary-limit-to-age t]
2501          ["Extra..." gnus-summary-limit-to-extra t]
2502          ["Score..." gnus-summary-limit-to-score t]
2503          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2504          ["Unread" gnus-summary-limit-to-unread t]
2505          ["Unseen" gnus-summary-limit-to-unseen t]
2506          ["Replied" gnus-summary-limit-to-replied t]
2507          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2508          ["Next articles" gnus-summary-limit-to-articles t]
2509          ["Pop limit" gnus-summary-pop-limit t]
2510          ["Show dormant" gnus-summary-limit-include-dormant t]
2511          ["Hide childless dormant"
2512           gnus-summary-limit-exclude-childless-dormant t]
2513          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2514          ["Hide marked" gnus-summary-limit-exclude-marks t]
2515          ["Show expunged" gnus-summary-limit-include-expunged t])
2516         ("Process Mark"
2517          ["Set mark" gnus-summary-mark-as-processable t]
2518          ["Remove mark" gnus-summary-unmark-as-processable t]
2519          ["Remove all marks" gnus-summary-unmark-all-processable t]
2520          ["Invert marks" gnus-uu-invert-processable t]
2521          ["Mark above" gnus-uu-mark-over t]
2522          ["Mark series" gnus-uu-mark-series t]
2523          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2524          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2525          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2526          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2527          ["Mark all" gnus-uu-mark-all t]
2528          ["Mark buffer" gnus-uu-mark-buffer t]
2529          ["Mark sparse" gnus-uu-mark-sparse t]
2530          ["Mark thread" gnus-uu-mark-thread t]
2531          ["Unmark thread" gnus-uu-unmark-thread t]
2532          ("Process Mark Sets"
2533           ["Kill" gnus-summary-kill-process-mark t]
2534           ["Yank" gnus-summary-yank-process-mark
2535            gnus-newsgroup-process-stack]
2536           ["Save" gnus-summary-save-process-mark t]
2537           ["Run command on marked..." gnus-summary-universal-argument t]))
2538         ("Scroll article"
2539          ["Page forward" gnus-summary-next-page
2540           ,@(if (featurep 'xemacs) '(t)
2541               '(:help "Show next page of article"))]
2542          ["Page backward" gnus-summary-prev-page
2543           ,@(if (featurep 'xemacs) '(t)
2544               '(:help "Show previous page of article"))]
2545          ["Line forward" gnus-summary-scroll-up t])
2546         ("Move"
2547          ["Next unread article" gnus-summary-next-unread-article t]
2548          ["Previous unread article" gnus-summary-prev-unread-article t]
2549          ["Next article" gnus-summary-next-article t]
2550          ["Previous article" gnus-summary-prev-article t]
2551          ["Next unread subject" gnus-summary-next-unread-subject t]
2552          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2553          ["Next article same subject" gnus-summary-next-same-subject t]
2554          ["Previous article same subject" gnus-summary-prev-same-subject t]
2555          ["First unread article" gnus-summary-first-unread-article t]
2556          ["Best unread article" gnus-summary-best-unread-article t]
2557          ["Go to subject number..." gnus-summary-goto-subject t]
2558          ["Go to article number..." gnus-summary-goto-article t]
2559          ["Go to the last article" gnus-summary-goto-last-article t]
2560          ["Pop article off history" gnus-summary-pop-article t])
2561         ("Sort"
2562          ["Sort by number" gnus-summary-sort-by-number t]
2563          ["Sort by author" gnus-summary-sort-by-author t]
2564          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2565          ["Sort by subject" gnus-summary-sort-by-subject t]
2566          ["Sort by date" gnus-summary-sort-by-date t]
2567          ["Sort by score" gnus-summary-sort-by-score t]
2568          ["Sort by lines" gnus-summary-sort-by-lines t]
2569          ["Sort by characters" gnus-summary-sort-by-chars t]
2570          ["Randomize" gnus-summary-sort-by-random t]
2571          ["Original sort" gnus-summary-sort-by-original t])
2572         ("Help"
2573          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2574          ["Describe group" gnus-summary-describe-group t]
2575          ["Fetch charter" gnus-group-fetch-charter
2576           ,@(if (featurep 'xemacs) nil
2577               '(:help "Display the charter of the current group"))]
2578          ["Fetch control message" gnus-group-fetch-control
2579           ,@(if (featurep 'xemacs) nil
2580               '(:help "Display the archived control message for the current group"))]
2581          ["Read manual" gnus-info-find-node t])
2582         ("Modes"
2583          ["Pick and read" gnus-pick-mode t]
2584          ["Binary" gnus-binary-mode t])
2585         ("Regeneration"
2586          ["Regenerate" gnus-summary-prepare t]
2587          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2588          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2589          ["Toggle threading" gnus-summary-toggle-threads t])
2590         ["See old articles" gnus-summary-insert-old-articles t]
2591         ["See new articles" gnus-summary-insert-new-articles t]
2592         ["Filter articles..." gnus-summary-execute-command t]
2593         ["Run command on articles..." gnus-summary-universal-argument t]
2594         ["Search articles forward..." gnus-summary-search-article-forward t]
2595         ["Search articles backward..." gnus-summary-search-article-backward t]
2596         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2597         ["Expand window" gnus-summary-expand-window t]
2598         ["Expire expirable articles" gnus-summary-expire-articles
2599          (gnus-check-backend-function
2600           'request-expire-articles gnus-newsgroup-name)]
2601         ["Edit local kill file" gnus-summary-edit-local-kill t]
2602         ["Edit main kill file" gnus-summary-edit-global-kill t]
2603         ["Edit group parameters" gnus-summary-edit-parameters t]
2604         ["Customize group parameters" gnus-summary-customize-parameters t]
2605         ["Send a bug report" gnus-bug t]
2606         ("Exit"
2607          ["Catchup and exit" gnus-summary-catchup-and-exit
2608           ,@(if (featurep 'xemacs) '(t)
2609               '(:help "Mark unread articles in this group as read, then exit"))]
2610          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2611          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2612          ["Exit group" gnus-summary-exit
2613           ,@(if (featurep 'xemacs) '(t)
2614               '(:help "Exit current group, return to group selection mode"))]
2615          ["Exit group without updating" gnus-summary-exit-no-update t]
2616          ["Exit and goto next group" gnus-summary-next-group t]
2617          ["Exit and goto prev group" gnus-summary-prev-group t]
2618          ["Reselect group" gnus-summary-reselect-current-group t]
2619          ["Rescan group" gnus-summary-rescan-group t]
2620          ["Update dribble" gnus-summary-save-newsrc t])))
2621
2622     (gnus-run-hooks 'gnus-summary-menu-hook)))
2623
2624 (defvar gnus-summary-tool-bar-map nil)
2625
2626 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2627 (defun gnus-summary-make-tool-bar ()
2628   (if (and (fboundp 'tool-bar-add-item-from-menu)
2629            (default-value 'tool-bar-mode)
2630            (not gnus-summary-tool-bar-map))
2631       (setq gnus-summary-tool-bar-map
2632             (let ((tool-bar-map (make-sparse-keymap))
2633                   (load-path (mm-image-load-path)))
2634               (tool-bar-add-item-from-menu
2635                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2636               (tool-bar-add-item-from-menu
2637                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2638               (tool-bar-add-item-from-menu
2639                'gnus-summary-post-news "post" gnus-summary-mode-map)
2640               (tool-bar-add-item-from-menu
2641                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2642               (tool-bar-add-item-from-menu
2643                'gnus-summary-followup "followup" gnus-summary-mode-map)
2644               (tool-bar-add-item-from-menu
2645                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2646               (tool-bar-add-item-from-menu
2647                'gnus-summary-reply "reply" gnus-summary-mode-map)
2648               (tool-bar-add-item-from-menu
2649                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2650               (tool-bar-add-item-from-menu
2651                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2652               (tool-bar-add-item-from-menu
2653                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2654               (tool-bar-add-item-from-menu
2655                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2656               (tool-bar-add-item-from-menu
2657                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2658               (tool-bar-add-item-from-menu
2659                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2660               (tool-bar-add-item-from-menu
2661                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2662               (tool-bar-add-item-from-menu
2663                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2664               (tool-bar-add-item-from-menu
2665                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2666               tool-bar-map)))
2667   (if gnus-summary-tool-bar-map
2668       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2669
2670 (defun gnus-score-set-default (var value)
2671   "A version of set that updates the GNU Emacs menu-bar."
2672   (set var value)
2673   ;; It is the message that forces the active status to be updated.
2674   (message ""))
2675
2676 (defun gnus-make-score-map (type)
2677   "Make a summary score map of type TYPE."
2678   (if t
2679       nil
2680     (let ((headers '(("author" "from" string)
2681                      ("subject" "subject" string)
2682                      ("article body" "body" string)
2683                      ("article head" "head" string)
2684                      ("xref" "xref" string)
2685                      ("extra header" "extra" string)
2686                      ("lines" "lines" number)
2687                      ("followups to author" "followup" string)))
2688           (types '((number ("less than" <)
2689                            ("greater than" >)
2690                            ("equal" =))
2691                    (string ("substring" s)
2692                            ("exact string" e)
2693                            ("fuzzy string" f)
2694                            ("regexp" r))))
2695           (perms '(("temporary" (current-time-string))
2696                    ("permanent" nil)
2697                    ("immediate" now)))
2698           header)
2699       (list
2700        (apply
2701         'nconc
2702         (list
2703          (if (eq type 'lower)
2704              "Lower score"
2705            "Increase score"))
2706         (let (outh)
2707           (while headers
2708             (setq header (car headers))
2709             (setq outh
2710                   (cons
2711                    (apply
2712                     'nconc
2713                     (list (car header))
2714                     (let ((ts (cdr (assoc (nth 2 header) types)))
2715                           outt)
2716                       (while ts
2717                         (setq outt
2718                               (cons
2719                                (apply
2720                                 'nconc
2721                                 (list (caar ts))
2722                                 (let ((ps perms)
2723                                       outp)
2724                                   (while ps
2725                                     (setq outp
2726                                           (cons
2727                                            (vector
2728                                             (caar ps)
2729                                             (list
2730                                              'gnus-summary-score-entry
2731                                              (nth 1 header)
2732                                              (if (or (string= (nth 1 header)
2733                                                               "head")
2734                                                      (string= (nth 1 header)
2735                                                               "body"))
2736                                                  ""
2737                                                (list 'gnus-summary-header
2738                                                      (nth 1 header)))
2739                                              (list 'quote (nth 1 (car ts)))
2740                                              (list 'gnus-score-delta-default
2741                                                    nil)
2742                                              (nth 1 (car ps))
2743                                              t)
2744                                             t)
2745                                            outp))
2746                                     (setq ps (cdr ps)))
2747                                   (list (nreverse outp))))
2748                                outt))
2749                         (setq ts (cdr ts)))
2750                       (list (nreverse outt))))
2751                    outh))
2752             (setq headers (cdr headers)))
2753           (list (nreverse outh))))))))
2754
2755 \f
2756
2757 (defun gnus-summary-mode (&optional group)
2758   "Major mode for reading articles.
2759
2760 All normal editing commands are switched off.
2761 \\<gnus-summary-mode-map>
2762 Each line in this buffer represents one article.  To read an
2763 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2764 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2765 respectively.
2766
2767 You can also post articles and send mail from this buffer.  To
2768 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2769 of an article, type `\\[gnus-summary-reply]'.
2770
2771 There are approx. one gazillion commands you can execute in this
2772 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2773
2774 The following commands are available:
2775
2776 \\{gnus-summary-mode-map}"
2777   (interactive)
2778   (kill-all-local-variables)
2779   (when (gnus-visual-p 'summary-menu 'menu)
2780     (gnus-summary-make-menu-bar)
2781     (gnus-summary-make-tool-bar))
2782   (gnus-summary-make-local-variables)
2783   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2784     (gnus-summary-make-local-variables))
2785   (gnus-make-thread-indent-array)
2786   (gnus-simplify-mode-line)
2787   (setq major-mode 'gnus-summary-mode)
2788   (setq mode-name "Summary")
2789   (make-local-variable 'minor-mode-alist)
2790   (use-local-map gnus-summary-mode-map)
2791   (buffer-disable-undo)
2792   (setq buffer-read-only t              ;Disable modification
2793         show-trailing-whitespace nil)
2794   (setq truncate-lines t)
2795   (setq selective-display t)
2796   (setq selective-display-ellipses t)   ;Display `...'
2797   (gnus-summary-set-display-table)
2798   (gnus-set-default-directory)
2799   (setq gnus-newsgroup-name group)
2800   (make-local-variable 'gnus-summary-line-format)
2801   (make-local-variable 'gnus-summary-line-format-spec)
2802   (make-local-variable 'gnus-summary-dummy-line-format)
2803   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2804   (make-local-variable 'gnus-summary-mark-positions)
2805   (gnus-make-local-hook 'pre-command-hook)
2806   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2807   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2808   (turn-on-gnus-mailing-list-mode)
2809   (mm-enable-multibyte)
2810   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2811   (gnus-update-summary-mark-positions))
2812
2813 (defun gnus-summary-make-local-variables ()
2814   "Make all the local summary buffer variables."
2815   (let (global)
2816     (dolist (local gnus-summary-local-variables)
2817       (if (consp local)
2818           (progn
2819             (if (eq (cdr local) 'global)
2820                 ;; Copy the global value of the variable.
2821                 (setq global (symbol-value (car local)))
2822               ;; Use the value from the list.
2823               (setq global (eval (cdr local))))
2824             (set (make-local-variable (car local)) global))
2825         ;; Simple nil-valued local variable.
2826         (set (make-local-variable local) nil)))))
2827
2828 (defun gnus-summary-clear-local-variables ()
2829   (let ((locals gnus-summary-local-variables))
2830     (while locals
2831       (if (consp (car locals))
2832           (and (symbolp (caar locals))
2833                (set (caar locals) nil))
2834         (and (symbolp (car locals))
2835              (set (car locals) nil)))
2836       (setq locals (cdr locals)))))
2837
2838 ;; Summary data functions.
2839
2840 (defmacro gnus-data-number (data)
2841   `(car ,data))
2842
2843 (defmacro gnus-data-set-number (data number)
2844   `(setcar ,data ,number))
2845
2846 (defmacro gnus-data-mark (data)
2847   `(nth 1 ,data))
2848
2849 (defmacro gnus-data-set-mark (data mark)
2850   `(setcar (nthcdr 1 ,data) ,mark))
2851
2852 (defmacro gnus-data-pos (data)
2853   `(nth 2 ,data))
2854
2855 (defmacro gnus-data-set-pos (data pos)
2856   `(setcar (nthcdr 2 ,data) ,pos))
2857
2858 (defmacro gnus-data-header (data)
2859   `(nth 3 ,data))
2860
2861 (defmacro gnus-data-set-header (data header)
2862   `(setf (nth 3 ,data) ,header))
2863
2864 (defmacro gnus-data-level (data)
2865   `(nth 4 ,data))
2866
2867 (defmacro gnus-data-unread-p (data)
2868   `(= (nth 1 ,data) gnus-unread-mark))
2869
2870 (defmacro gnus-data-read-p (data)
2871   `(/= (nth 1 ,data) gnus-unread-mark))
2872
2873 (defmacro gnus-data-pseudo-p (data)
2874   `(consp (nth 3 ,data)))
2875
2876 (defmacro gnus-data-find (number)
2877   `(assq ,number gnus-newsgroup-data))
2878
2879 (defmacro gnus-data-find-list (number &optional data)
2880   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2881      (memq (assq ,number bdata)
2882            bdata)))
2883
2884 (defmacro gnus-data-make (number mark pos header level)
2885   `(list ,number ,mark ,pos ,header ,level))
2886
2887 (defun gnus-data-enter (after-article number mark pos header level offset)
2888   (let ((data (gnus-data-find-list after-article)))
2889     (unless data
2890       (error "No such article: %d" after-article))
2891     (setcdr data (cons (gnus-data-make number mark pos header level)
2892                        (cdr data)))
2893     (setq gnus-newsgroup-data-reverse nil)
2894     (gnus-data-update-list (cddr data) offset)))
2895
2896 (defun gnus-data-enter-list (after-article list &optional offset)
2897   (when list
2898     (let ((data (and after-article (gnus-data-find-list after-article)))
2899           (ilist list))
2900       (if (not (or data
2901                    after-article))
2902           (let ((odata gnus-newsgroup-data))
2903             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2904             (when offset
2905               (gnus-data-update-list odata offset)))
2906         ;; Find the last element in the list to be spliced into the main
2907         ;; list.
2908         (setq list (last list))
2909         (if (not data)
2910             (progn
2911               (setcdr list gnus-newsgroup-data)
2912               (setq gnus-newsgroup-data ilist)
2913               (when offset
2914                 (gnus-data-update-list (cdr list) offset)))
2915           (setcdr list (cdr data))
2916           (setcdr data ilist)
2917           (when offset
2918             (gnus-data-update-list (cdr list) offset))))
2919       (setq gnus-newsgroup-data-reverse nil))))
2920
2921 (defun gnus-data-remove (article &optional offset)
2922   (let ((data gnus-newsgroup-data))
2923     (if (= (gnus-data-number (car data)) article)
2924         (progn
2925           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2926                 gnus-newsgroup-data-reverse nil)
2927           (when offset
2928             (gnus-data-update-list gnus-newsgroup-data offset)))
2929       (while (cdr data)
2930         (when (= (gnus-data-number (cadr data)) article)
2931           (setcdr data (cddr data))
2932           (when offset
2933             (gnus-data-update-list (cdr data) offset))
2934           (setq data nil
2935                 gnus-newsgroup-data-reverse nil))
2936         (setq data (cdr data))))))
2937
2938 (defmacro gnus-data-list (backward)
2939   `(if ,backward
2940        (or gnus-newsgroup-data-reverse
2941            (setq gnus-newsgroup-data-reverse
2942                  (reverse gnus-newsgroup-data)))
2943      gnus-newsgroup-data))
2944
2945 (defun gnus-data-update-list (data offset)
2946   "Add OFFSET to the POS of all data entries in DATA."
2947   (setq gnus-newsgroup-data-reverse nil)
2948   (while data
2949     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2950     (setq data (cdr data))))
2951
2952 (defun gnus-summary-article-pseudo-p (article)
2953   "Say whether this article is a pseudo article or not."
2954   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2955
2956 (defmacro gnus-summary-article-sparse-p (article)
2957   "Say whether this article is a sparse article or not."
2958   `(memq ,article gnus-newsgroup-sparse))
2959
2960 (defmacro gnus-summary-article-ancient-p (article)
2961   "Say whether this article is a sparse article or not."
2962   `(memq ,article gnus-newsgroup-ancient))
2963
2964 (defun gnus-article-parent-p (number)
2965   "Say whether this article is a parent or not."
2966   (let ((data (gnus-data-find-list number)))
2967     (and (cdr data)              ; There has to be an article after...
2968          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2969             (gnus-data-level (nth 1 data))))))
2970
2971 (defun gnus-article-children (number)
2972   "Return a list of all children to NUMBER."
2973   (let* ((data (gnus-data-find-list number))
2974          (level (gnus-data-level (car data)))
2975          children)
2976     (setq data (cdr data))
2977     (while (and data
2978                 (= (gnus-data-level (car data)) (1+ level)))
2979       (push (gnus-data-number (car data)) children)
2980       (setq data (cdr data)))
2981     children))
2982
2983 (defmacro gnus-summary-skip-intangible ()
2984   "If the current article is intangible, then jump to a different article."
2985   '(let ((to (get-text-property (point) 'gnus-intangible)))
2986      (and to (gnus-summary-goto-subject to))))
2987
2988 (defmacro gnus-summary-article-intangible-p ()
2989   "Say whether this article is intangible or not."
2990   '(get-text-property (point) 'gnus-intangible))
2991
2992 (defun gnus-article-read-p (article)
2993   "Say whether ARTICLE is read or not."
2994   (not (or (memq article gnus-newsgroup-marked)
2995            (memq article gnus-newsgroup-spam-marked)
2996            (memq article gnus-newsgroup-unreads)
2997            (memq article gnus-newsgroup-unselected)
2998            (memq article gnus-newsgroup-dormant))))
2999
3000 ;; Some summary mode macros.
3001
3002 (defmacro gnus-summary-article-number ()
3003   "The article number of the article on the current line.
3004 If there isn't an article number here, then we return the current
3005 article number."
3006   '(progn
3007      (gnus-summary-skip-intangible)
3008      (or (get-text-property (point) 'gnus-number)
3009          (gnus-summary-last-subject))))
3010
3011 (defmacro gnus-summary-article-header (&optional number)
3012   "Return the header of article NUMBER."
3013   `(gnus-data-header (gnus-data-find
3014                       ,(or number '(gnus-summary-article-number)))))
3015
3016 (defmacro gnus-summary-thread-level (&optional number)
3017   "Return the level of thread that starts with article NUMBER."
3018   `(if (and (eq gnus-summary-make-false-root 'dummy)
3019             (get-text-property (point) 'gnus-intangible))
3020        0
3021      (gnus-data-level (gnus-data-find
3022                        ,(or number '(gnus-summary-article-number))))))
3023
3024 (defmacro gnus-summary-article-mark (&optional number)
3025   "Return the mark of article NUMBER."
3026   `(gnus-data-mark (gnus-data-find
3027                     ,(or number '(gnus-summary-article-number)))))
3028
3029 (defmacro gnus-summary-article-pos (&optional number)
3030   "Return the position of the line of article NUMBER."
3031   `(gnus-data-pos (gnus-data-find
3032                    ,(or number '(gnus-summary-article-number)))))
3033
3034 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3035 (defmacro gnus-summary-article-subject (&optional number)
3036   "Return current subject string or nil if nothing."
3037   `(let ((headers
3038           ,(if number
3039                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3040              '(gnus-data-header (assq (gnus-summary-article-number)
3041                                       gnus-newsgroup-data)))))
3042      (and headers
3043           (vectorp headers)
3044           (mail-header-subject headers))))
3045
3046 (defmacro gnus-summary-article-score (&optional number)
3047   "Return current article score."
3048   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3049                   gnus-newsgroup-scored))
3050        gnus-summary-default-score 0))
3051
3052 (defun gnus-summary-article-children (&optional number)
3053   "Return a list of article numbers that are children of article NUMBER."
3054   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3055          (level (gnus-data-level (car data)))
3056          l children)
3057     (while (and (setq data (cdr data))
3058                 (> (setq l (gnus-data-level (car data))) level))
3059       (and (= (1+ level) l)
3060            (push (gnus-data-number (car data))
3061                  children)))
3062     (nreverse children)))
3063
3064 (defun gnus-summary-article-parent (&optional number)
3065   "Return the article number of the parent of article NUMBER."
3066   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3067                                     (gnus-data-list t)))
3068          (level (gnus-data-level (car data))))
3069     (if (zerop level)
3070         ()                              ; This is a root.
3071       ;; We search until we find an article with a level less than
3072       ;; this one.  That function has to be the parent.
3073       (while (and (setq data (cdr data))
3074                   (not (< (gnus-data-level (car data)) level))))
3075       (and data (gnus-data-number (car data))))))
3076
3077 (defun gnus-unread-mark-p (mark)
3078   "Say whether MARK is the unread mark."
3079   (= mark gnus-unread-mark))
3080
3081 (defun gnus-read-mark-p (mark)
3082   "Say whether MARK is one of the marks that mark as read.
3083 This is all marks except unread, ticked, dormant, and expirable."
3084   (not (or (= mark gnus-unread-mark)
3085            (= mark gnus-ticked-mark)
3086            (= mark gnus-spam-mark)
3087            (= mark gnus-dormant-mark)
3088            (= mark gnus-expirable-mark))))
3089
3090 (defmacro gnus-article-mark (number)
3091   "Return the MARK of article NUMBER.
3092 This macro should only be used when computing the mark the \"first\"
3093 time; i.e., when generating the summary lines.  After that,
3094 `gnus-summary-article-mark' should be used to examine the
3095 marks of articles."
3096   `(cond
3097     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3098     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3099     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3100     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3101     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3102     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3103     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3104     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3105            gnus-ancient-mark))))
3106
3107 ;; Saving hidden threads.
3108
3109 (defmacro gnus-save-hidden-threads (&rest forms)
3110   "Save hidden threads, eval FORMS, and restore the hidden threads."
3111   (let ((config (make-symbol "config")))
3112     `(let ((,config (gnus-hidden-threads-configuration)))
3113        (unwind-protect
3114            (save-excursion
3115              ,@forms)
3116          (gnus-restore-hidden-threads-configuration ,config)))))
3117 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3118 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3119
3120 (defun gnus-data-compute-positions ()
3121   "Compute the positions of all articles."
3122   (setq gnus-newsgroup-data-reverse nil)
3123   (let ((data gnus-newsgroup-data))
3124     (save-excursion
3125       (gnus-save-hidden-threads
3126         (gnus-summary-show-all-threads)
3127         (goto-char (point-min))
3128         (while data
3129           (while (get-text-property (point) 'gnus-intangible)
3130             (forward-line 1))
3131           (gnus-data-set-pos (car data) (+ (point) 3))
3132           (setq data (cdr data))
3133           (forward-line 1))))))
3134
3135 (defun gnus-hidden-threads-configuration ()
3136   "Return the current hidden threads configuration."
3137   (save-excursion
3138     (let (config)
3139       (goto-char (point-min))
3140       (while (search-forward "\r" nil t)
3141         (push (1- (point)) config))
3142       config)))
3143
3144 (defun gnus-restore-hidden-threads-configuration (config)
3145   "Restore hidden threads configuration from CONFIG."
3146   (save-excursion
3147     (let (point buffer-read-only)
3148       (while (setq point (pop config))
3149         (when (and (< point (point-max))
3150                    (goto-char point)
3151                    (eq (char-after) ?\n))
3152           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3153
3154 ;; Various summary mode internalish functions.
3155
3156 (defun gnus-mouse-pick-article (e)
3157   (interactive "e")
3158   (mouse-set-point e)
3159   (gnus-summary-next-page nil t))
3160
3161 (defun gnus-summary-set-display-table ()
3162   "Change the display table.
3163 Odd characters have a tendency to mess
3164 up nicely formatted displays - we make all possible glyphs
3165 display only a single character."
3166
3167   ;; We start from the standard display table, if any.
3168   (let ((table (or (copy-sequence standard-display-table)
3169                    (make-display-table)))
3170         (i 32))
3171     ;; Nix out all the control chars...
3172     (while (>= (setq i (1- i)) 0)
3173       (aset table i [??]))
3174    ;; ... but not newline and cr, of course.  (cr is necessary for the
3175     ;; selective display).
3176     (aset table ?\n nil)
3177     (aset table ?\r nil)
3178     ;; We keep TAB as well.
3179     (aset table ?\t nil)
3180     ;; We nix out any glyphs over 126 that are not set already.
3181     (let ((i 256))
3182       (while (>= (setq i (1- i)) 127)
3183         ;; Only modify if the entry is nil.
3184         (unless (aref table i)
3185           (aset table i [??]))))
3186     (setq buffer-display-table table)))
3187
3188 (defun gnus-summary-set-article-display-arrow (pos)
3189   "Update the overlay arrow to point to line at position POS."
3190   (when (and gnus-summary-display-arrow
3191              (boundp 'overlay-arrow-position)
3192              (boundp 'overlay-arrow-string))
3193     (save-excursion
3194       (goto-char pos)
3195       (beginning-of-line)
3196       (unless overlay-arrow-position
3197         (setq overlay-arrow-position (make-marker)))
3198       (setq overlay-arrow-string "=>"
3199             overlay-arrow-position (set-marker overlay-arrow-position
3200                                                (point)
3201                                                (current-buffer))))))
3202
3203 (defun gnus-summary-setup-buffer (group)
3204   "Initialize summary buffer."
3205   (let ((buffer (gnus-summary-buffer-name group))
3206         (dead-name (concat "*Dead Summary "
3207                            (gnus-group-decoded-name group) "*")))
3208     ;; If a dead summary buffer exists, we kill it.
3209     (when (gnus-buffer-live-p dead-name)
3210       (gnus-kill-buffer dead-name))
3211     (if (get-buffer buffer)
3212         (progn
3213           (set-buffer buffer)
3214           (setq gnus-summary-buffer (current-buffer))
3215           (not gnus-newsgroup-prepared))
3216       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3217       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3218       (gnus-summary-mode group)
3219       (when gnus-carpal
3220         (gnus-carpal-setup-buffer 'summary))
3221       (unless gnus-single-article-buffer
3222         (make-local-variable 'gnus-article-buffer)
3223         (make-local-variable 'gnus-article-current)
3224         (make-local-variable 'gnus-original-article-buffer))
3225       (setq gnus-newsgroup-name group)
3226       ;; Set any local variables in the group parameters.
3227       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3228       t)))
3229
3230 (defun gnus-set-global-variables ()
3231   "Set the global equivalents of the buffer-local variables.
3232 They are set to the latest values they had.  These reflect the summary
3233 buffer that was in action when the last article was fetched."
3234   (when (eq major-mode 'gnus-summary-mode)
3235     (setq gnus-summary-buffer (current-buffer))
3236     (let ((name gnus-newsgroup-name)
3237           (marked gnus-newsgroup-marked)
3238           (spam gnus-newsgroup-spam-marked)
3239           (unread gnus-newsgroup-unreads)
3240           (headers gnus-current-headers)
3241           (data gnus-newsgroup-data)
3242           (summary gnus-summary-buffer)
3243           (article-buffer gnus-article-buffer)
3244           (original gnus-original-article-buffer)
3245           (gac gnus-article-current)
3246           (reffed gnus-reffed-article-number)
3247           (score-file gnus-current-score-file)
3248           (default-charset gnus-newsgroup-charset)
3249           vlist)
3250       (let ((locals gnus-newsgroup-variables))
3251         (while locals
3252           (if (consp (car locals))
3253               (push (eval (caar locals)) vlist)
3254             (push (eval (car locals)) vlist))
3255           (setq locals (cdr locals)))
3256         (setq vlist (nreverse vlist)))
3257       (with-current-buffer gnus-group-buffer
3258         (setq gnus-newsgroup-name name
3259               gnus-newsgroup-marked marked
3260               gnus-newsgroup-spam-marked spam
3261               gnus-newsgroup-unreads unread
3262               gnus-current-headers headers
3263               gnus-newsgroup-data data
3264               gnus-article-current gac
3265               gnus-summary-buffer summary
3266               gnus-article-buffer article-buffer
3267               gnus-original-article-buffer original
3268               gnus-reffed-article-number reffed
3269               gnus-current-score-file score-file
3270               gnus-newsgroup-charset default-charset)
3271         (let ((locals gnus-newsgroup-variables))
3272           (while locals
3273             (if (consp (car locals))
3274                 (set (caar locals) (pop vlist))
3275               (set (car locals) (pop vlist)))
3276             (setq locals (cdr locals))))
3277         ;; The article buffer also has local variables.
3278         (when (gnus-buffer-live-p gnus-article-buffer)
3279           (set-buffer gnus-article-buffer)
3280           (setq gnus-summary-buffer summary))))))
3281
3282 (defun gnus-summary-article-unread-p (article)
3283   "Say whether ARTICLE is unread or not."
3284   (memq article gnus-newsgroup-unreads))
3285
3286 (defun gnus-summary-first-article-p (&optional article)
3287   "Return whether ARTICLE is the first article in the buffer."
3288   (if (not (setq article (or article (gnus-summary-article-number))))
3289       nil
3290     (eq article (caar gnus-newsgroup-data))))
3291
3292 (defun gnus-summary-last-article-p (&optional article)
3293   "Return whether ARTICLE is the last article in the buffer."
3294   (if (not (setq article (or article (gnus-summary-article-number))))
3295       ;; All non-existent numbers are the last article.  :-)
3296       t
3297     (not (cdr (gnus-data-find-list article)))))
3298
3299 (defun gnus-make-thread-indent-array ()
3300   (let ((n 200))
3301     (unless (and gnus-thread-indent-array
3302                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3303       (setq gnus-thread-indent-array (make-vector 201 "")
3304             gnus-thread-indent-array-level gnus-thread-indent-level)
3305       (while (>= n 0)
3306         (aset gnus-thread-indent-array n
3307               (make-string (* n gnus-thread-indent-level) ? ))
3308         (setq n (1- n))))))
3309
3310 (defun gnus-update-summary-mark-positions ()
3311   "Compute where the summary marks are to go."
3312   (save-excursion
3313     (when (gnus-buffer-exists-p gnus-summary-buffer)
3314       (set-buffer gnus-summary-buffer))
3315     (let ((spec gnus-summary-line-format-spec)
3316           pos)
3317       (save-excursion
3318         (gnus-set-work-buffer)
3319         (let ((gnus-tmp-unread ?Z)
3320               (gnus-replied-mark ?Z)
3321               (gnus-score-below-mark ?Z)
3322               (gnus-score-over-mark ?Z)
3323               (gnus-undownloaded-mark ?Z)
3324               (gnus-summary-line-format-spec spec)
3325               (gnus-newsgroup-downloadable '(0))
3326               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3327               case-fold-search ignores)
3328           ;; Here, all marks are bound to Z.
3329           (gnus-summary-insert-line header
3330                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3331           (goto-char (point-min))
3332           ;; Memorize the positions of the same characters as dummy marks.
3333           (while (re-search-forward "[A-D]" nil t)
3334             (push (point) ignores))
3335           (erase-buffer)
3336           ;; We use A-D as dummy marks in order to know column positions
3337           ;; where marks should be inserted.
3338           (setq gnus-tmp-unread ?A
3339                 gnus-replied-mark ?B
3340                 gnus-score-below-mark ?C
3341                 gnus-score-over-mark ?C
3342                 gnus-undownloaded-mark ?D)
3343           (gnus-summary-insert-line header
3344                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3345           ;; Ignore characters which aren't dummy marks.
3346           (dolist (p ignores)
3347             (delete-region (goto-char (1- p)) p)
3348             (insert ?Z))
3349           (goto-char (point-min))
3350           (setq pos (list (cons 'unread
3351                                 (and (search-forward "A" nil t)
3352                                      (- (point) (point-min) 1)))))
3353           (goto-char (point-min))
3354           (push (cons 'replied (and (search-forward "B" nil t)
3355                                     (- (point) (point-min) 1)))
3356                 pos)
3357           (goto-char (point-min))
3358           (push (cons 'score (and (search-forward "C" nil t)
3359                                   (- (point) (point-min) 1)))
3360                 pos)
3361           (goto-char (point-min))
3362           (push (cons 'download (and (search-forward "D" nil t)
3363                                      (- (point) (point-min) 1)))
3364                 pos)))
3365       (setq gnus-summary-mark-positions pos))))
3366
3367 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3368   "Insert a dummy root in the summary buffer."
3369   (beginning-of-line)
3370   (gnus-add-text-properties
3371    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3372    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3373
3374 (defun gnus-summary-extract-address-component (from)
3375   (or (car (funcall gnus-extract-address-components from))
3376       from))
3377
3378 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3379   (let ((mail-parse-charset gnus-newsgroup-charset)
3380         ; Is it really necessary to do this next part for each summary line?
3381         ; Luckily, doesn't seem to slow things down much.
3382         (mail-parse-ignored-charsets
3383          (with-current-buffer gnus-summary-buffer
3384            gnus-newsgroup-ignored-charsets)))
3385     (or
3386      (and gnus-ignored-from-addresses
3387           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3388           (let ((extra-headers (mail-header-extra header))
3389                 to
3390                 newsgroups)
3391             (cond
3392              ((setq to (cdr (assq 'To extra-headers)))
3393               (concat gnus-summary-to-prefix
3394                       (inline
3395                         (gnus-summary-extract-address-component
3396                          (funcall gnus-decode-encoded-word-function to)))))
3397              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3398               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3399      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3400
3401 (defun gnus-summary-insert-line (gnus-tmp-header
3402                                  gnus-tmp-level gnus-tmp-current
3403                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3404                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3405                                  &optional gnus-tmp-dummy gnus-tmp-score
3406                                  gnus-tmp-process)
3407   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3408          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3409          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3410          (gnus-tmp-score-char
3411           (if (or (null gnus-summary-default-score)
3412                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3413                       gnus-summary-zcore-fuzz))
3414               ?                         ;Whitespace
3415             (if (< gnus-tmp-score gnus-summary-default-score)
3416                 gnus-score-below-mark gnus-score-over-mark)))
3417          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3418          (gnus-tmp-replied
3419           (cond (gnus-tmp-process gnus-process-mark)
3420                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3421                  gnus-cached-mark)
3422                 (gnus-tmp-replied gnus-replied-mark)
3423                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3424                  gnus-forwarded-mark)
3425                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3426                  gnus-saved-mark)
3427                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3428                  gnus-recent-mark)
3429                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3430                  gnus-unseen-mark)
3431                 (t gnus-no-mark)))
3432          (gnus-tmp-downloaded
3433           (cond (undownloaded
3434                  gnus-undownloaded-mark)
3435                 (gnus-newsgroup-agentized
3436                  gnus-downloaded-mark)
3437                 (t
3438                  gnus-no-mark)))
3439          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3440          (gnus-tmp-name
3441           (cond
3442            ((string-match "<[^>]+> *$" gnus-tmp-from)
3443             (let ((beg (match-beginning 0)))
3444               (or (and (string-match "^\".+\"" gnus-tmp-from)
3445                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3446                   (substring gnus-tmp-from 0 beg))))
3447            ((string-match "(.+)" gnus-tmp-from)
3448             (substring gnus-tmp-from
3449                        (1+ (match-beginning 0)) (1- (match-end 0))))
3450            (t gnus-tmp-from)))
3451          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3452          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3453          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3454          (buffer-read-only nil))
3455     (when (string= gnus-tmp-name "")
3456       (setq gnus-tmp-name gnus-tmp-from))
3457     (unless (numberp gnus-tmp-lines)
3458       (setq gnus-tmp-lines -1))
3459     (if (= gnus-tmp-lines -1)
3460         (setq gnus-tmp-lines "?")
3461       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3462       (gnus-put-text-property
3463      (point)
3464      (progn (eval gnus-summary-line-format-spec) (point))
3465        'gnus-number gnus-tmp-number)
3466     (when (gnus-visual-p 'summary-highlight 'highlight)
3467       (forward-line -1)
3468       (gnus-run-hooks 'gnus-summary-update-hook)
3469       (forward-line 1))))
3470
3471 (defun gnus-summary-update-line (&optional dont-update)
3472   "Update summary line after change."
3473   (when (and gnus-summary-default-score
3474              (not gnus-summary-inhibit-highlight))
3475     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3476            (article (gnus-summary-article-number))
3477            (score (gnus-summary-article-score article)))
3478       (unless dont-update
3479         (if (and gnus-summary-mark-below
3480                  (< (gnus-summary-article-score)
3481                     gnus-summary-mark-below))
3482             ;; This article has a low score, so we mark it as read.
3483             (when (memq article gnus-newsgroup-unreads)
3484               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3485           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3486             ;; This article was previously marked as read on account
3487             ;; of a low score, but now it has risen, so we mark it as
3488             ;; unread.
3489             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3490         (gnus-summary-update-mark
3491          (if (or (null gnus-summary-default-score)
3492                  (<= (abs (- score gnus-summary-default-score))
3493                      gnus-summary-zcore-fuzz))
3494              ?                          ;Whitespace
3495            (if (< score gnus-summary-default-score)
3496                gnus-score-below-mark gnus-score-over-mark))
3497          'score))
3498       ;; Do visual highlighting.
3499       (when (gnus-visual-p 'summary-highlight 'highlight)
3500         (gnus-run-hooks 'gnus-summary-update-hook)))))
3501
3502 (defvar gnus-tmp-new-adopts nil)
3503
3504 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3505   "Return the number of articles in THREAD.
3506 This may be 0 in some cases -- if none of the articles in
3507 the thread are to be displayed."
3508   (let* ((number
3509          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3510           (cond
3511            ((not (listp thread))
3512             1)
3513            ((and (consp thread) (cdr thread))
3514             (apply
3515              '+ 1 (mapcar
3516                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3517            ((null thread)
3518             1)
3519            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3520             1)
3521            (t 0))))
3522     (when (and level (zerop level) gnus-tmp-new-adopts)
3523       (incf number
3524             (apply '+ (mapcar
3525                        'gnus-summary-number-of-articles-in-thread
3526                        gnus-tmp-new-adopts))))
3527     (if char
3528         (if (> number 1) gnus-not-empty-thread-mark
3529           gnus-empty-thread-mark)
3530       number)))
3531
3532 (defsubst gnus-summary-line-message-size (head)
3533   "Return pretty-printed version of message size.
3534 This function is intended to be used in
3535 `gnus-summary-line-format-alist'."
3536   (let ((c (or (mail-header-chars head) -1)))
3537     (cond ((< c 0) "n/a")               ; chars not available
3538           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3539           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3540           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3541           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3542
3543
3544 (defun gnus-summary-set-local-parameters (group)
3545   "Go through the local params of GROUP and set all variable specs in that list."
3546   (let ((vars '(quit-config)))          ; Ignore quit-config.
3547     (dolist (elem (gnus-group-find-parameter group))
3548       (and (consp elem)                 ; Has to be a cons.
3549            (consp (cdr elem))           ; The cdr has to be a list.
3550            (symbolp (car elem))         ; Has to be a symbol in there.
3551            (not (memq (car elem) vars))
3552            (ignore-errors               ; So we set it.
3553              (push (car elem) vars)
3554              (make-local-variable (car elem))
3555              (set (car elem) (eval (nth 1 elem))))))))
3556
3557 (defun gnus-summary-read-group (group &optional show-all no-article
3558                                       kill-buffer no-display backward
3559                                       select-articles)
3560   "Start reading news in newsgroup GROUP.
3561 If SHOW-ALL is non-nil, already read articles are also listed.
3562 If NO-ARTICLE is non-nil, no article is selected initially.
3563 If NO-DISPLAY, don't generate a summary buffer."
3564   (let (result)
3565     (while (and group
3566                 (null (setq result
3567                             (let ((gnus-auto-select-next nil))
3568                               (or (gnus-summary-read-group-1
3569                                    group show-all no-article
3570                                    kill-buffer no-display
3571                                    select-articles)
3572                                   (setq show-all nil
3573                                         select-articles nil)))))
3574                 (eq gnus-auto-select-next 'quietly))
3575       (set-buffer gnus-group-buffer)
3576       ;; The entry function called above goes to the next
3577       ;; group automatically, so we go two groups back
3578       ;; if we are searching for the previous group.
3579       (when backward
3580         (gnus-group-prev-unread-group 2))
3581       (if (not (equal group (gnus-group-group-name)))
3582           (setq group (gnus-group-group-name))
3583         (setq group nil)))
3584     result))
3585
3586 (defun gnus-summary-read-group-1 (group show-all no-article
3587                                         kill-buffer no-display
3588                                         &optional select-articles)
3589   ;; Killed foreign groups can't be entered.
3590   ;;  (when (and (not (gnus-group-native-p group))
3591   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3592   ;;    (error "Dead non-native groups can't be entered"))
3593   (gnus-message 5 "Retrieving newsgroup: %s..."
3594                 (gnus-group-decoded-name group))
3595   (let* ((new-group (gnus-summary-setup-buffer group))
3596          (quit-config (gnus-group-quit-config group))
3597          (did-select (and new-group (gnus-select-newsgroup
3598                                      group show-all select-articles))))
3599     (cond
3600      ;; This summary buffer exists already, so we just select it.
3601      ((not new-group)
3602       (gnus-set-global-variables)
3603       (when kill-buffer
3604         (gnus-kill-or-deaden-summary kill-buffer))
3605       (gnus-configure-windows 'summary 'force)
3606       (gnus-set-mode-line 'summary)
3607       (gnus-summary-position-point)
3608       (message "")
3609       t)
3610      ;; We couldn't select this group.
3611      ((null did-select)
3612       (when (and (eq major-mode 'gnus-summary-mode)
3613                  (not (equal (current-buffer) kill-buffer)))
3614         (kill-buffer (current-buffer))
3615         (if (not quit-config)
3616             (progn
3617               ;; Update the info -- marks might need to be removed,
3618               ;; for instance.
3619               (gnus-summary-update-info)
3620               (set-buffer gnus-group-buffer)
3621               (gnus-group-jump-to-group group)
3622               (gnus-group-next-unread-group 1))
3623           (gnus-handle-ephemeral-exit quit-config)))
3624       (let ((grpinfo (gnus-get-info group)))
3625         (if (null (gnus-info-read grpinfo))
3626             (gnus-message 3 "Group %s contains no messages"
3627                           (gnus-group-decoded-name group))
3628           (gnus-message 3 "Can't select group")))
3629       nil)
3630      ;; The user did a `C-g' while prompting for number of articles,
3631      ;; so we exit this group.
3632      ((eq did-select 'quit)
3633       (and (eq major-mode 'gnus-summary-mode)
3634            (not (equal (current-buffer) kill-buffer))
3635            (kill-buffer (current-buffer)))
3636       (when kill-buffer
3637         (gnus-kill-or-deaden-summary kill-buffer))
3638       (if (not quit-config)
3639           (progn
3640             (set-buffer gnus-group-buffer)
3641             (gnus-group-jump-to-group group)
3642             (gnus-group-next-unread-group 1)
3643             (gnus-configure-windows 'group 'force))
3644         (gnus-handle-ephemeral-exit quit-config))
3645       ;; Finally signal the quit.
3646       (signal 'quit nil))
3647      ;; The group was successfully selected.
3648      (t
3649       (gnus-set-global-variables)
3650       ;; Save the active value in effect when the group was entered.
3651       (setq gnus-newsgroup-active
3652             (gnus-copy-sequence
3653              (gnus-active gnus-newsgroup-name)))
3654       ;; You can change the summary buffer in some way with this hook.
3655       (gnus-run-hooks 'gnus-select-group-hook)
3656       (when (memq 'summary (gnus-update-format-specifications
3657                             nil 'summary 'summary-mode 'summary-dummy))
3658         ;; The format specification for the summary line was updated,
3659         ;; so we need to update the mark positions as well.
3660         (gnus-update-summary-mark-positions))
3661       ;; Do score processing.
3662       (when gnus-use-scoring
3663         (gnus-possibly-score-headers))
3664       ;; Check whether to fill in the gaps in the threads.
3665       (when gnus-build-sparse-threads
3666         (gnus-build-sparse-threads))
3667       ;; Find the initial limit.
3668       (if show-all
3669           (let ((gnus-newsgroup-dormant nil))
3670             (gnus-summary-initial-limit show-all))
3671         (gnus-summary-initial-limit show-all))
3672       ;; Generate the summary buffer.
3673       (unless no-display
3674         (gnus-summary-prepare))
3675       (when gnus-use-trees
3676         (gnus-tree-open group)
3677         (setq gnus-summary-highlight-line-function
3678               'gnus-tree-highlight-article))
3679       ;; If the summary buffer is empty, but there are some low-scored
3680       ;; articles or some excluded dormants, we include these in the
3681       ;; buffer.
3682       (when (and (zerop (buffer-size))
3683                  (not no-display))
3684         (cond (gnus-newsgroup-dormant
3685                (gnus-summary-limit-include-dormant))
3686               ((and gnus-newsgroup-scored show-all)
3687                (gnus-summary-limit-include-expunged t))))
3688       ;; Function `gnus-apply-kill-file' must be called in this hook.
3689       (gnus-run-hooks 'gnus-apply-kill-hook)
3690       (if (and (zerop (buffer-size))
3691                (not no-display))
3692           (progn
3693             ;; This newsgroup is empty.
3694             (gnus-summary-catchup-and-exit nil t)
3695             (gnus-message 6 "No unread news")
3696             (when kill-buffer
3697               (gnus-kill-or-deaden-summary kill-buffer))
3698             ;; Return nil from this function.
3699             nil)
3700         ;; Hide conversation thread subtrees.  We cannot do this in
3701         ;; gnus-summary-prepare-hook since kill processing may not
3702         ;; work with hidden articles.
3703         (gnus-summary-maybe-hide-threads)
3704         (when kill-buffer
3705           (gnus-kill-or-deaden-summary kill-buffer))
3706         (gnus-summary-auto-select-subject)
3707         ;; Show first unread article if requested.
3708         (if (and (not no-article)
3709                  (not no-display)
3710                  gnus-newsgroup-unreads
3711                  gnus-auto-select-first)
3712             (progn
3713               (gnus-configure-windows 'summary)
3714               (let ((art (gnus-summary-article-number)))
3715                 (unless (and (not gnus-plugged)
3716                              (or (memq art gnus-newsgroup-undownloaded)
3717                                  (memq art gnus-newsgroup-downloadable)))
3718                   (gnus-summary-goto-article art))))
3719           ;; Don't select any articles.
3720           (gnus-summary-position-point)
3721           (gnus-configure-windows 'summary 'force)
3722           (gnus-set-mode-line 'summary))
3723         (when (and gnus-auto-center-group
3724                    (get-buffer-window gnus-group-buffer t))
3725           ;; Gotta use windows, because recenter does weird stuff if
3726           ;; the current buffer ain't the displayed window.
3727           (let ((owin (selected-window)))
3728             (select-window (get-buffer-window gnus-group-buffer t))
3729             (when (gnus-group-goto-group group)
3730               (recenter))
3731             (select-window owin)))
3732         ;; Mark this buffer as "prepared".
3733         (setq gnus-newsgroup-prepared t)
3734         (gnus-run-hooks 'gnus-summary-prepared-hook)
3735         (unless (gnus-ephemeral-group-p group)
3736           (gnus-group-update-group group))
3737         t)))))
3738
3739 (defun gnus-summary-auto-select-subject ()
3740   "Select the subject line on initial group entry."
3741   (goto-char (point-min))
3742   (cond
3743    ((eq gnus-auto-select-subject 'best)
3744     (gnus-summary-best-unread-subject))
3745    ((eq gnus-auto-select-subject 'unread)
3746     (gnus-summary-first-unread-subject))
3747    ((eq gnus-auto-select-subject 'unseen)
3748     (gnus-summary-first-unseen-subject))
3749    ((eq gnus-auto-select-subject 'unseen-or-unread)
3750     (gnus-summary-first-unseen-or-unread-subject))
3751    ((eq gnus-auto-select-subject 'first)
3752     ;; Do nothing.
3753     )
3754    ((functionp gnus-auto-select-subject)
3755     (funcall gnus-auto-select-subject))))
3756
3757 (defun gnus-summary-prepare ()
3758   "Generate the summary buffer."
3759   (interactive)
3760   (let ((buffer-read-only nil))
3761     (erase-buffer)
3762     (setq gnus-newsgroup-data nil
3763           gnus-newsgroup-data-reverse nil)
3764     (gnus-run-hooks 'gnus-summary-generate-hook)
3765     ;; Generate the buffer, either with threads or without.
3766     (when gnus-newsgroup-headers
3767       (gnus-summary-prepare-threads
3768        (if gnus-show-threads
3769            (gnus-sort-gathered-threads
3770             (funcall gnus-summary-thread-gathering-function
3771                      (gnus-sort-threads
3772                       (gnus-cut-threads (gnus-make-threads)))))
3773          ;; Unthreaded display.
3774          (gnus-sort-articles gnus-newsgroup-headers))))
3775     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3776     ;; Call hooks for modifying summary buffer.
3777     (goto-char (point-min))
3778     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3779
3780 (defsubst gnus-general-simplify-subject (subject)
3781   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3782   (setq subject
3783         (cond
3784          ;; Truncate the subject.
3785          (gnus-simplify-subject-functions
3786           (gnus-map-function gnus-simplify-subject-functions subject))
3787          ((numberp gnus-summary-gather-subject-limit)
3788           (setq subject (gnus-simplify-subject-re subject))
3789           (if (> (length subject) gnus-summary-gather-subject-limit)
3790               (substring subject 0 gnus-summary-gather-subject-limit)
3791             subject))
3792          ;; Fuzzily simplify it.
3793          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3794           (gnus-simplify-subject-fuzzy subject))
3795          ;; Just remove the leading "Re:".
3796          (t
3797           (gnus-simplify-subject-re subject))))
3798
3799   (if (and gnus-summary-gather-exclude-subject
3800            (string-match gnus-summary-gather-exclude-subject subject))
3801       nil                         ; This article shouldn't be gathered
3802     subject))
3803
3804 (defun gnus-summary-simplify-subject-query ()
3805   "Query where the respool algorithm would put this article."
3806   (interactive)
3807   (gnus-summary-select-article)
3808   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3809
3810 (defun gnus-gather-threads-by-subject (threads)
3811   "Gather threads by looking at Subject headers."
3812   (if (not gnus-summary-make-false-root)
3813       threads
3814     (let ((hashtb (gnus-make-hashtable 1024))
3815           (prev threads)
3816           (result threads)
3817           subject hthread whole-subject)
3818       (while threads
3819         (setq subject (gnus-general-simplify-subject
3820                        (setq whole-subject (mail-header-subject
3821                                             (caar threads)))))
3822         (when subject
3823           (if (setq hthread (gnus-gethash subject hashtb))
3824               (progn
3825                 ;; We enter a dummy root into the thread, if we
3826                 ;; haven't done that already.
3827                 (unless (stringp (caar hthread))
3828                   (setcar hthread (list whole-subject (car hthread))))
3829                 ;; We add this new gathered thread to this gathered
3830                 ;; thread.
3831                 (setcdr (car hthread)
3832                         (nconc (cdar hthread) (list (car threads))))
3833                 ;; Remove it from the list of threads.
3834                 (setcdr prev (cdr threads))
3835                 (setq threads prev))
3836             ;; Enter this thread into the hash table.
3837             (gnus-sethash subject
3838                           (if gnus-summary-make-false-root-always
3839                               (progn
3840                                 ;; If you want a dummy root above all
3841                                 ;; threads...
3842                                 (setcar threads (list whole-subject
3843                                                       (car threads)))
3844                                 threads)
3845                             threads)
3846                           hashtb)))
3847         (setq prev threads)
3848         (setq threads (cdr threads)))
3849       result)))
3850
3851 (defun gnus-gather-threads-by-references (threads)
3852   "Gather threads by looking at References headers."
3853   (let ((idhashtb (gnus-make-hashtable 1024))
3854         (thhashtb (gnus-make-hashtable 1024))
3855         (prev threads)
3856         (result threads)
3857         ids references id gthread gid entered ref)
3858     (while threads
3859       (when (setq references (mail-header-references (caar threads)))
3860         (setq id (mail-header-id (caar threads))
3861               ids (inline (gnus-split-references references))
3862               entered nil)
3863         (while (setq ref (pop ids))
3864           (setq ids (delete ref ids))
3865           (if (not (setq gid (gnus-gethash ref idhashtb)))
3866               (progn
3867                 (gnus-sethash ref id idhashtb)
3868                 (gnus-sethash id threads thhashtb))
3869             (setq gthread (gnus-gethash gid thhashtb))
3870             (unless entered
3871               ;; We enter a dummy root into the thread, if we
3872               ;; haven't done that already.
3873               (unless (stringp (caar gthread))
3874                 (setcar gthread (list (mail-header-subject (caar gthread))
3875                                       (car gthread))))
3876               ;; We add this new gathered thread to this gathered
3877               ;; thread.
3878               (setcdr (car gthread)
3879                       (nconc (cdar gthread) (list (car threads)))))
3880             ;; Add it into the thread hash table.
3881             (gnus-sethash id gthread thhashtb)
3882             (setq entered t)
3883             ;; Remove it from the list of threads.
3884             (setcdr prev (cdr threads))
3885             (setq threads prev))))
3886       (setq prev threads)
3887       (setq threads (cdr threads)))
3888     result))
3889
3890 (defun gnus-sort-gathered-threads (threads)
3891   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3892   (let ((result threads))
3893     (while threads
3894       (when (stringp (caar threads))
3895         (setcdr (car threads)
3896                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3897       (setq threads (cdr threads)))
3898     result))
3899
3900 (defun gnus-thread-loop-p (root thread)
3901   "Say whether ROOT is in THREAD."
3902   (let ((stack (list thread))
3903         (infloop 0)
3904         th)
3905     (while (setq thread (pop stack))
3906       (setq th (cdr thread))
3907       (while (and th
3908                   (not (eq (caar th) root)))
3909         (pop th))
3910       (if th
3911           ;; We have found a loop.
3912           (let (ref-dep)
3913             (setcdr thread (delq (car th) (cdr thread)))
3914             (if (boundp (setq ref-dep (intern "none"
3915                                               gnus-newsgroup-dependencies)))
3916                 (setcdr (symbol-value ref-dep)
3917                         (nconc (cdr (symbol-value ref-dep))
3918                                (list (car th))))
3919               (set ref-dep (list nil (car th))))
3920             (setq infloop 1
3921                   stack nil))
3922         ;; Push all the subthreads onto the stack.
3923         (push (cdr thread) stack)))
3924     infloop))
3925
3926 (defun gnus-make-threads ()
3927   "Go through the dependency hashtb and find the roots.  Return all threads."
3928   (let (threads)
3929     (while (catch 'infloop
3930              (mapatoms
3931               (lambda (refs)
3932                 ;; Deal with self-referencing References loops.
3933                 (when (and (car (symbol-value refs))
3934                            (not (zerop
3935                                  (apply
3936                                   '+
3937                                   (mapcar
3938                                    (lambda (thread)
3939                                      (gnus-thread-loop-p
3940                                       (car (symbol-value refs)) thread))
3941                                    (cdr (symbol-value refs)))))))
3942                   (setq threads nil)
3943                   (throw 'infloop t))
3944                 (unless (car (symbol-value refs))
3945                   ;; These threads do not refer back to any other
3946                   ;; articles, so they're roots.
3947                   (setq threads (append (cdr (symbol-value refs)) threads))))
3948               gnus-newsgroup-dependencies)))
3949     threads))
3950
3951 ;; Build the thread tree.
3952 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3953   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3954
3955 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3956 if it was already present.
3957
3958 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3959 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3960 Message-IDs will be renamed to a unique Message-ID before being
3961 entered.
3962
3963 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3964   (let* ((id (mail-header-id header))
3965          (id-dep (and id (intern id dependencies)))
3966          parent-id ref ref-dep ref-header replaced)
3967     ;; Enter this `header' in the `dependencies' table.
3968     (cond
3969      ((not id-dep)
3970       (setq header nil))
3971      ;; The first two cases do the normal part: enter a new `header'
3972      ;; in the `dependencies' table.
3973      ((not (boundp id-dep))
3974       (set id-dep (list header)))
3975      ((null (car (symbol-value id-dep)))
3976       (setcar (symbol-value id-dep) header))
3977
3978      ;; From here the `header' was already present in the
3979      ;; `dependencies' table.
3980      (force-new
3981       ;; Overrides an existing entry;
3982       ;; just set the header part of the entry.
3983       (setcar (symbol-value id-dep) header)
3984       (setq replaced t))
3985
3986      ;; Renames the existing `header' to a unique Message-ID.
3987      ((not gnus-summary-ignore-duplicates)
3988       ;; An article with this Message-ID has already been seen.
3989       ;; We rename the Message-ID.
3990       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3991            (list header))
3992       (mail-header-set-id header id))
3993
3994      ;; The last case ignores an existing entry, except it adds any
3995      ;; additional Xrefs (in case the two articles came from different
3996      ;; servers.
3997      ;; Also sets `header' to `nil' meaning that the `dependencies'
3998      ;; table was *not* modified.
3999      (t
4000       (mail-header-set-xref
4001        (car (symbol-value id-dep))
4002        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4003                    "")
4004                (or (mail-header-xref header) "")))
4005       (setq header nil)))
4006
4007     (when (and header (not replaced))
4008       ;; First check that we are not creating a References loop.
4009       (setq parent-id (gnus-parent-id (mail-header-references header)))
4010       (setq ref parent-id)
4011       (while (and ref
4012                   (setq ref-dep (intern-soft ref dependencies))
4013                   (boundp ref-dep)
4014                   (setq ref-header (car (symbol-value ref-dep))))
4015         (if (string= id ref)
4016             ;; Yuk!  This is a reference loop.  Make the article be a
4017             ;; root article.
4018             (progn
4019               (mail-header-set-references (car (symbol-value id-dep)) "none")
4020               (setq ref nil)
4021               (setq parent-id nil))
4022           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4023       (setq ref-dep (intern (or parent-id "none") dependencies))
4024       (if (boundp ref-dep)
4025           (setcdr (symbol-value ref-dep)
4026                   (nconc (cdr (symbol-value ref-dep))
4027                          (list (symbol-value id-dep))))
4028         (set ref-dep (list nil (symbol-value id-dep)))))
4029     header))
4030
4031 (defun gnus-extract-message-id-from-in-reply-to (string)
4032   (if (string-match "<[^>]+>" string)
4033       (substring string (match-beginning 0) (match-end 0))
4034     nil))
4035
4036 (defun gnus-build-sparse-threads ()
4037   (let ((headers gnus-newsgroup-headers)
4038         (mail-parse-charset gnus-newsgroup-charset)
4039         (gnus-summary-ignore-duplicates t)
4040         header references generation relations
4041         subject child end new-child date)
4042     ;; First we create an alist of generations/relations, where
4043     ;; generations is how much we trust the relation, and the relation
4044     ;; is parent/child.
4045     (gnus-message 7 "Making sparse threads...")
4046     (save-excursion
4047       (nnheader-set-temp-buffer " *gnus sparse threads*")
4048       (while (setq header (pop headers))
4049         (when (and (setq references (mail-header-references header))
4050                    (not (string= references "")))
4051           (insert references)
4052           (setq child (mail-header-id header)
4053                 subject (mail-header-subject header)
4054                 date (mail-header-date header)
4055                 generation 0)
4056           (while (search-backward ">" nil t)
4057             (setq end (1+ (point)))
4058             (when (search-backward "<" nil t)
4059               (setq new-child (buffer-substring (point) end))
4060               (push (list (incf generation)
4061                           child (setq child new-child)
4062                           subject date)
4063                     relations)))
4064           (when child
4065             (push (list (1+ generation) child nil subject) relations))
4066           (erase-buffer)))
4067       (kill-buffer (current-buffer)))
4068     ;; Sort over trustworthiness.
4069     (mapcar
4070      (lambda (relation)
4071        (when (gnus-dependencies-add-header
4072               (make-full-mail-header
4073                gnus-reffed-article-number
4074                (nth 3 relation) "" (or (nth 4 relation) "")
4075                (nth 1 relation)
4076                (or (nth 2 relation) "") 0 0 "")
4077               gnus-newsgroup-dependencies nil)
4078          (push gnus-reffed-article-number gnus-newsgroup-limit)
4079          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4080          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4081                gnus-newsgroup-reads)
4082          (decf gnus-reffed-article-number)))
4083      (sort relations 'car-less-than-car))
4084     (gnus-message 7 "Making sparse threads...done")))
4085
4086 (defun gnus-build-old-threads ()
4087   ;; Look at all the articles that refer back to old articles, and
4088   ;; fetch the headers for the articles that aren't there.  This will
4089   ;; build complete threads - if the roots haven't been expired by the
4090   ;; server, that is.
4091   (let ((mail-parse-charset gnus-newsgroup-charset)
4092         id heads)
4093     (mapatoms
4094      (lambda (refs)
4095        (when (not (car (symbol-value refs)))
4096          (setq heads (cdr (symbol-value refs)))
4097          (while heads
4098            (if (memq (mail-header-number (caar heads))
4099                      gnus-newsgroup-dormant)
4100                (setq heads (cdr heads))
4101              (setq id (symbol-name refs))
4102              (while (and (setq id (gnus-build-get-header id))
4103                          (not (car (gnus-id-to-thread id)))))
4104              (setq heads nil)))))
4105      gnus-newsgroup-dependencies)))
4106
4107 (defsubst gnus-remove-odd-characters (string)
4108   "Translate STRING into something that doesn't contain weird characters."
4109   (mm-subst-char-in-string
4110    ?\r ?\-
4111    (mm-subst-char-in-string ?\n ?\- string t) t))
4112
4113 ;; This function has to be called with point after the article number
4114 ;; on the beginning of the line.
4115 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4116   (let ((eol (point-at-eol))
4117         (buffer (current-buffer))
4118         header references in-reply-to)
4119
4120     ;; overview: [num subject from date id refs chars lines misc]
4121     (unwind-protect
4122         (let (x)
4123           (narrow-to-region (point) eol)
4124           (unless (eobp)
4125             (forward-char))
4126
4127           (setq header
4128                 (make-full-mail-header
4129                  number                 ; number
4130                  (condition-case ()     ; subject
4131                      (gnus-remove-odd-characters
4132                       (funcall gnus-decode-encoded-word-function
4133                                (setq x (nnheader-nov-field))))
4134                    (error x))
4135                  (condition-case ()     ; from
4136                      (gnus-remove-odd-characters
4137                       (funcall gnus-decode-encoded-word-function
4138                                (setq x (nnheader-nov-field))))
4139                    (error x))
4140                  (nnheader-nov-field)   ; date
4141                  (nnheader-nov-read-message-id number)  ; id
4142                  (setq references (nnheader-nov-field)) ; refs
4143                  (nnheader-nov-read-integer) ; chars
4144                  (nnheader-nov-read-integer) ; lines
4145                  (unless (eobp)
4146                    (if (looking-at "Xref: ")
4147                        (goto-char (match-end 0)))
4148                    (nnheader-nov-field)) ; Xref
4149                  (nnheader-nov-parse-extra)))) ; extra
4150
4151       (widen))
4152
4153     (when (and (string= references "")
4154                (setq in-reply-to (mail-header-extra header))
4155                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4156       (mail-header-set-references
4157        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4158
4159     (when gnus-alter-header-function
4160       (funcall gnus-alter-header-function header))
4161     (gnus-dependencies-add-header header dependencies force-new)))
4162
4163 (defun gnus-build-get-header (id)
4164   "Look through the buffer of NOV lines and find the header to ID.
4165 Enter this line into the dependencies hash table, and return
4166 the id of the parent article (if any)."
4167   (let ((deps gnus-newsgroup-dependencies)
4168         found header)
4169     (prog1
4170         (save-excursion
4171           (set-buffer nntp-server-buffer)
4172           (let ((case-fold-search nil))
4173             (goto-char (point-min))
4174             (while (and (not found)
4175                         (search-forward id nil t))
4176               (beginning-of-line)
4177               (setq found (looking-at
4178                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4179                                    (regexp-quote id))))
4180               (or found (beginning-of-line 2)))
4181             (when found
4182               (beginning-of-line)
4183               (and
4184                (setq header (gnus-nov-parse-line
4185                              (read (current-buffer)) deps))
4186                (gnus-parent-id (mail-header-references header))))))
4187       (when header
4188         (let ((number (mail-header-number header)))
4189           (push number gnus-newsgroup-limit)
4190           (push header gnus-newsgroup-headers)
4191           (if (memq number gnus-newsgroup-unselected)
4192               (progn
4193                 (setq gnus-newsgroup-unreads
4194                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4195                                                number))
4196                 (setq gnus-newsgroup-unselected
4197                       (delq number gnus-newsgroup-unselected)))
4198             (push number gnus-newsgroup-ancient)))))))
4199
4200 (defun gnus-build-all-threads ()
4201   "Read all the headers."
4202   (let ((gnus-summary-ignore-duplicates t)
4203         (mail-parse-charset gnus-newsgroup-charset)
4204         (dependencies gnus-newsgroup-dependencies)
4205         header article)
4206     (save-excursion
4207       (set-buffer nntp-server-buffer)
4208       (let ((case-fold-search nil))
4209         (goto-char (point-min))
4210         (while (not (eobp))
4211           (ignore-errors
4212             (setq article (read (current-buffer))
4213                   header (gnus-nov-parse-line article dependencies)))
4214           (when header
4215             (with-current-buffer gnus-summary-buffer
4216               (push header gnus-newsgroup-headers)
4217               (if (memq (setq article (mail-header-number header))
4218                         gnus-newsgroup-unselected)
4219                   (progn
4220                     (setq gnus-newsgroup-unreads
4221                           (gnus-add-to-sorted-list
4222                            gnus-newsgroup-unreads article))
4223                     (setq gnus-newsgroup-unselected
4224                           (delq article gnus-newsgroup-unselected)))
4225                 (push article gnus-newsgroup-ancient)))
4226             (forward-line 1)))))))
4227
4228 (defun gnus-summary-update-article-line (article header)
4229   "Update the line for ARTICLE using HEADER."
4230   (let* ((id (mail-header-id header))
4231          (thread (gnus-id-to-thread id)))
4232     (unless thread
4233       (error "Article in no thread"))
4234     ;; Update the thread.
4235     (setcar thread header)
4236     (gnus-summary-goto-subject article)
4237     (let* ((datal (gnus-data-find-list article))
4238            (data (car datal))
4239            (buffer-read-only nil)
4240            (level (gnus-summary-thread-level)))
4241       (gnus-delete-line)
4242       (let ((inserted (- (point)
4243                          (progn
4244                            (gnus-summary-insert-line
4245                             header level nil
4246                             (memq article gnus-newsgroup-undownloaded)
4247                             (gnus-article-mark article)
4248                             (memq article gnus-newsgroup-replied)
4249                             (memq article gnus-newsgroup-expirable)
4250                             ;; Only insert the Subject string when it's different
4251                             ;; from the previous Subject string.
4252                             (if (and
4253                                  gnus-show-threads
4254                                  (gnus-subject-equal
4255                                   (condition-case ()
4256                                       (mail-header-subject
4257                                        (gnus-data-header
4258                                         (cadr
4259                                          (gnus-data-find-list
4260                                           article
4261                                           (gnus-data-list t)))))
4262                                     ;; Error on the side of excessive subjects.
4263                                     (error ""))
4264                                   (mail-header-subject header)))
4265                                 ""
4266                               (mail-header-subject header))
4267                             nil (cdr (assq article gnus-newsgroup-scored))
4268                             (memq article gnus-newsgroup-processable))
4269                            (point)))))
4270         (when (cdr datal)
4271           (gnus-data-update-list
4272            (cdr datal)
4273            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4274
4275 (defun gnus-summary-update-article (article &optional iheader)
4276   "Update ARTICLE in the summary buffer."
4277   (set-buffer gnus-summary-buffer)
4278   (let* ((header (gnus-summary-article-header article))
4279          (id (mail-header-id header))
4280          (data (gnus-data-find article))
4281          (thread (gnus-id-to-thread id))
4282          (references (mail-header-references header))
4283          (parent
4284           (gnus-id-to-thread
4285            (or (gnus-parent-id
4286                 (when (and references
4287                            (not (equal "" references)))
4288                   references))
4289                "none")))
4290          (buffer-read-only nil)
4291          (old (car thread)))
4292     (when thread
4293       (unless iheader
4294         (setcar thread nil)
4295         (when parent
4296           (delq thread parent)))
4297       (if (gnus-summary-insert-subject id header)
4298           ;; Set the (possibly) new article number in the data structure.
4299           (gnus-data-set-number data (gnus-id-to-article id))
4300         (setcar thread old)
4301         nil))))
4302
4303 (defun gnus-rebuild-thread (id &optional line)
4304   "Rebuild the thread containing ID.
4305 If LINE, insert the rebuilt thread starting on line LINE."
4306   (let ((buffer-read-only nil)
4307         old-pos current thread data)
4308     (if (not gnus-show-threads)
4309         (setq thread (list (car (gnus-id-to-thread id))))
4310       ;; Get the thread this article is part of.
4311       (setq thread (gnus-remove-thread id)))
4312     (setq old-pos (point-at-bol))
4313     (setq current (save-excursion
4314                     (and (re-search-backward "[\r\n]" nil t)
4315                          (gnus-summary-article-number))))
4316     ;; If this is a gathered thread, we have to go some re-gathering.
4317     (when (stringp (car thread))
4318       (let ((subject (car thread))
4319             roots thr)
4320         (setq thread (cdr thread))
4321         (while thread
4322           (unless (memq (setq thr (gnus-id-to-thread
4323                                    (gnus-root-id
4324                                     (mail-header-id (caar thread)))))
4325                         roots)
4326             (push thr roots))
4327           (setq thread (cdr thread)))
4328         ;; We now have all (unique) roots.
4329         (if (= (length roots) 1)
4330             ;; All the loose roots are now one solid root.
4331             (setq thread (car roots))
4332           (setq thread (cons subject (gnus-sort-threads roots))))))
4333     (let (threads)
4334       ;; We then insert this thread into the summary buffer.
4335       (when line
4336         (goto-char (point-min))
4337         (forward-line (1- line)))
4338       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4339         (if gnus-show-threads
4340             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4341           (gnus-summary-prepare-unthreaded thread))
4342         (setq data (nreverse gnus-newsgroup-data))
4343         (setq threads gnus-newsgroup-threads))
4344       ;; We splice the new data into the data structure.
4345       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4346       ;;!!! then we want to insert at the beginning of the buffer.
4347       ;;!!! That happens to be true with Gnus now, but that may
4348       ;;!!! change in the future.  Perhaps.
4349       (gnus-data-enter-list
4350        (if line nil current) data (- (point) old-pos))
4351       (setq gnus-newsgroup-threads
4352             (nconc threads gnus-newsgroup-threads))
4353       (gnus-data-compute-positions))))
4354
4355 (defun gnus-number-to-header (number)
4356   "Return the header for article NUMBER."
4357   (let ((headers gnus-newsgroup-headers))
4358     (while (and headers
4359                 (not (= number (mail-header-number (car headers)))))
4360       (pop headers))
4361     (when headers
4362       (car headers))))
4363
4364 (defun gnus-parent-headers (in-headers &optional generation)
4365   "Return the headers of the GENERATIONeth parent of HEADERS."
4366   (unless generation
4367     (setq generation 1))
4368   (let ((parent t)
4369         (headers in-headers)
4370         references)
4371     (while (and parent
4372                 (not (zerop generation))
4373                 (setq references (mail-header-references headers)))
4374       (setq headers (if (and references
4375                              (setq parent (gnus-parent-id references)))
4376                         (car (gnus-id-to-thread parent))
4377                       nil))
4378       (decf generation))
4379     (and (not (eq headers in-headers))
4380          headers)))
4381
4382 (defun gnus-id-to-thread (id)
4383   "Return the (sub-)thread where ID appears."
4384   (gnus-gethash id gnus-newsgroup-dependencies))
4385
4386 (defun gnus-id-to-article (id)
4387   "Return the article number of ID."
4388   (let ((thread (gnus-id-to-thread id)))
4389     (when (and thread
4390                (car thread))
4391       (mail-header-number (car thread)))))
4392
4393 (defun gnus-id-to-header (id)
4394   "Return the article headers of ID."
4395   (car (gnus-id-to-thread id)))
4396
4397 (defun gnus-article-displayed-root-p (article)
4398   "Say whether ARTICLE is a root(ish) article."
4399   (let ((level (gnus-summary-thread-level article))
4400         (refs (mail-header-references  (gnus-summary-article-header article)))
4401         particle)
4402     (cond
4403      ((null level) nil)
4404      ((zerop level) t)
4405      ((null refs) t)
4406      ((null (gnus-parent-id refs)) t)
4407      ((and (= 1 level)
4408            (null (setq particle (gnus-id-to-article
4409                                  (gnus-parent-id refs))))
4410            (null (gnus-summary-thread-level particle)))))))
4411
4412 (defun gnus-root-id (id)
4413   "Return the id of the root of the thread where ID appears."
4414   (let (last-id prev)
4415     (while (and id (setq prev (car (gnus-id-to-thread id))))
4416       (setq last-id id
4417             id (gnus-parent-id (mail-header-references prev))))
4418     last-id))
4419
4420 (defun gnus-articles-in-thread (thread)
4421   "Return the list of articles in THREAD."
4422   (cons (mail-header-number (car thread))
4423         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4424
4425 (defun gnus-remove-thread (id &optional dont-remove)
4426   "Remove the thread that has ID in it."
4427   (let (headers thread last-id)
4428     ;; First go up in this thread until we find the root.
4429     (setq last-id (gnus-root-id id)
4430           headers (message-flatten-list (gnus-id-to-thread last-id)))
4431     ;; We have now found the real root of this thread.  It might have
4432     ;; been gathered into some loose thread, so we have to search
4433     ;; through the threads to find the thread we wanted.
4434     (let ((threads gnus-newsgroup-threads)
4435           sub)
4436       (while threads
4437         (setq sub (car threads))
4438         (if (stringp (car sub))
4439             ;; This is a gathered thread, so we look at the roots
4440             ;; below it to find whether this article is in this
4441             ;; gathered root.
4442             (progn
4443               (setq sub (cdr sub))
4444               (while sub
4445                 (when (member (caar sub) headers)
4446                   (setq thread (car threads)
4447                         threads nil
4448                         sub nil))
4449                 (setq sub (cdr sub))))
4450           ;; It's an ordinary thread, so we check it.
4451           (when (eq (car sub) (car headers))
4452             (setq thread sub
4453                   threads nil)))
4454         (setq threads (cdr threads)))
4455       ;; If this article is in no thread, then it's a root.
4456       (if thread
4457           (unless dont-remove
4458             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4459         (setq thread (gnus-id-to-thread last-id)))
4460       (when thread
4461         (prog1
4462             thread                      ; We return this thread.
4463           (unless dont-remove
4464             (if (stringp (car thread))
4465                 (progn
4466                   ;; If we use dummy roots, then we have to remove the
4467                   ;; dummy root as well.
4468                   (when (eq gnus-summary-make-false-root 'dummy)
4469                     ;; We go to the dummy root by going to
4470                     ;; the first sub-"thread", and then one line up.
4471                     (gnus-summary-goto-article
4472                      (mail-header-number (caadr thread)))
4473                     (forward-line -1)
4474                     (gnus-delete-line)
4475                     (gnus-data-compute-positions))
4476                   (setq thread (cdr thread))
4477                   (while thread
4478                     (gnus-remove-thread-1 (car thread))
4479                     (setq thread (cdr thread))))
4480               (gnus-remove-thread-1 thread))))))))
4481
4482 (defun gnus-remove-thread-1 (thread)
4483   "Remove the thread THREAD recursively."
4484   (let ((number (mail-header-number (pop thread)))
4485         d)
4486     (setq thread (reverse thread))
4487     (while thread
4488       (gnus-remove-thread-1 (pop thread)))
4489     (when (setq d (gnus-data-find number))
4490       (goto-char (gnus-data-pos d))
4491       (gnus-summary-show-thread)
4492       (gnus-data-remove
4493        number
4494        (- (point-at-bol)
4495           (prog1
4496               (1+ (point-at-eol))
4497             (gnus-delete-line)))))))
4498
4499 (defun gnus-sort-threads-1 (threads func)
4500   (sort (mapcar (lambda (thread)
4501                   (cons (car thread)
4502                         (and (cdr thread)
4503                              (gnus-sort-threads-1 (cdr thread) func))))
4504                 threads) func))
4505
4506 (defun gnus-sort-threads (threads)
4507   "Sort THREADS."
4508   (if (not gnus-thread-sort-functions)
4509       threads
4510     (gnus-message 8 "Sorting threads...")
4511     (let ((max-lisp-eval-depth 5000))
4512       (prog1 (gnus-sort-threads-1
4513          threads
4514          (gnus-make-sort-function gnus-thread-sort-functions))
4515         (gnus-message 8 "Sorting threads...done")))))
4516
4517 (defun gnus-sort-articles (articles)
4518   "Sort ARTICLES."
4519   (when gnus-article-sort-functions
4520     (gnus-message 7 "Sorting articles...")
4521     (prog1
4522         (setq gnus-newsgroup-headers
4523               (sort articles (gnus-make-sort-function
4524                               gnus-article-sort-functions)))
4525       (gnus-message 7 "Sorting articles...done"))))
4526
4527 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4528 (defmacro gnus-thread-header (thread)
4529   "Return header of first article in THREAD.
4530 Note that THREAD must never, ever be anything else than a variable -
4531 using some other form will lead to serious barfage."
4532   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4533   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4534   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4535         (vector thread) 2))
4536
4537 (defsubst gnus-article-sort-by-number (h1 h2)
4538   "Sort articles by article number."
4539   (< (mail-header-number h1)
4540      (mail-header-number h2)))
4541
4542 (defun gnus-thread-sort-by-number (h1 h2)
4543   "Sort threads by root article number."
4544   (gnus-article-sort-by-number
4545    (gnus-thread-header h1) (gnus-thread-header h2)))
4546
4547 (defsubst gnus-article-sort-by-random (h1 h2)
4548   "Sort articles by article number."
4549   (zerop (random 2)))
4550
4551 (defun gnus-thread-sort-by-random (h1 h2)
4552   "Sort threads by root article number."
4553   (gnus-article-sort-by-random
4554    (gnus-thread-header h1) (gnus-thread-header h2)))
4555
4556 (defsubst gnus-article-sort-by-lines (h1 h2)
4557   "Sort articles by article Lines header."
4558   (< (mail-header-lines h1)
4559      (mail-header-lines h2)))
4560
4561 (defun gnus-thread-sort-by-lines (h1 h2)
4562   "Sort threads by root article Lines header."
4563   (gnus-article-sort-by-lines
4564    (gnus-thread-header h1) (gnus-thread-header h2)))
4565
4566 (defsubst gnus-article-sort-by-chars (h1 h2)
4567   "Sort articles by octet length."
4568   (< (mail-header-chars h1)
4569      (mail-header-chars h2)))
4570
4571 (defun gnus-thread-sort-by-chars (h1 h2)
4572   "Sort threads by root article octet length."
4573   (gnus-article-sort-by-chars
4574    (gnus-thread-header h1) (gnus-thread-header h2)))
4575
4576 (defsubst gnus-article-sort-by-author (h1 h2)
4577   "Sort articles by root author."
4578   (string-lessp
4579    (let ((extract (funcall
4580                    gnus-extract-address-components
4581                    (mail-header-from h1))))
4582      (or (car extract) (cadr extract) ""))
4583    (let ((extract (funcall
4584                    gnus-extract-address-components
4585                    (mail-header-from h2))))
4586      (or (car extract) (cadr extract) ""))))
4587
4588 (defun gnus-thread-sort-by-author (h1 h2)
4589   "Sort threads by root author."
4590   (gnus-article-sort-by-author
4591    (gnus-thread-header h1)  (gnus-thread-header h2)))
4592
4593 (defsubst gnus-article-sort-by-recipient (h1 h2)
4594   "Sort articles by recipient."
4595   (string-lessp
4596    (let ((extract (funcall
4597                    gnus-extract-address-components
4598                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4599      (or (car extract) (cadr extract)))
4600    (let ((extract (funcall
4601                    gnus-extract-address-components
4602                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4603      (or (car extract) (cadr extract)))))
4604
4605 (defsubst gnus-article-sort-by-subject (h1 h2)
4606   "Sort articles by root subject."
4607   (string-lessp
4608    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4609    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4610
4611 (defun gnus-thread-sort-by-subject (h1 h2)
4612   "Sort threads by root subject."
4613   (gnus-article-sort-by-subject
4614    (gnus-thread-header h1) (gnus-thread-header h2)))
4615
4616 (defsubst gnus-article-sort-by-date (h1 h2)
4617   "Sort articles by root article date."
4618   (time-less-p
4619    (gnus-date-get-time (mail-header-date h1))
4620    (gnus-date-get-time (mail-header-date h2))))
4621
4622 (defun gnus-thread-sort-by-date (h1 h2)
4623   "Sort threads by root article date."
4624   (gnus-article-sort-by-date
4625    (gnus-thread-header h1) (gnus-thread-header h2)))
4626
4627 (defsubst gnus-article-sort-by-score (h1 h2)
4628   "Sort articles by root article score.
4629 Unscored articles will be counted as having a score of zero."
4630   (> (or (cdr (assq (mail-header-number h1)
4631                     gnus-newsgroup-scored))
4632          gnus-summary-default-score 0)
4633      (or (cdr (assq (mail-header-number h2)
4634                     gnus-newsgroup-scored))
4635          gnus-summary-default-score 0)))
4636
4637 (defun gnus-thread-sort-by-score (h1 h2)
4638   "Sort threads by root article score."
4639   (gnus-article-sort-by-score
4640    (gnus-thread-header h1) (gnus-thread-header h2)))
4641
4642 (defun gnus-thread-sort-by-total-score (h1 h2)
4643   "Sort threads by the sum of all scores in the thread.
4644 Unscored articles will be counted as having a score of zero."
4645   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4646
4647 (defun gnus-thread-total-score (thread)
4648   ;; This function find the total score of THREAD.
4649   (cond
4650    ((null thread)
4651     0)
4652    ((consp thread)
4653     (if (stringp (car thread))
4654         (apply gnus-thread-score-function 0
4655                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4656       (gnus-thread-total-score-1 thread)))
4657    (t
4658     (gnus-thread-total-score-1 (list thread)))))
4659
4660 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4661   "Sort threads such that the thread with the most recently arrived article comes first."
4662   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4663
4664 (defun gnus-thread-highest-number (thread)
4665   "Return the highest article number in THREAD."
4666   (apply 'max (mapcar (lambda (header)
4667                         (mail-header-number header))
4668                       (message-flatten-list thread))))
4669
4670 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4671   "Sort threads such that the thread with the most recently dated article comes first."
4672   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4673
4674 (defun gnus-thread-latest-date (thread)
4675   "Return the highest article date in THREAD."
4676   (let ((previous-time 0))
4677     (apply 'max
4678            (mapcar
4679             (lambda (header)
4680               (setq previous-time
4681                     (condition-case ()
4682                         (time-to-seconds (mail-header-parse-date
4683                                           (mail-header-date header)))
4684                       (error previous-time))))
4685             (sort
4686              (message-flatten-list thread)
4687              (lambda (h1 h2)
4688                (< (mail-header-number h1)
4689                   (mail-header-number h2))))))))
4690
4691 (defun gnus-thread-total-score-1 (root)
4692   ;; This function find the total score of the thread below ROOT.
4693   (setq root (car root))
4694   (apply gnus-thread-score-function
4695          (or (append
4696               (mapcar 'gnus-thread-total-score
4697                       (cdr (gnus-id-to-thread (mail-header-id root))))
4698               (when (> (mail-header-number root) 0)
4699                 (list (or (cdr (assq (mail-header-number root)
4700                                      gnus-newsgroup-scored))
4701                           gnus-summary-default-score 0))))
4702              (list gnus-summary-default-score)
4703              '(0))))
4704
4705 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4706 (defvar gnus-tmp-prev-subject nil)
4707 (defvar gnus-tmp-false-parent nil)
4708 (defvar gnus-tmp-root-expunged nil)
4709 (defvar gnus-tmp-dummy-line nil)
4710
4711 (eval-when-compile (defvar gnus-tmp-header))
4712 (defun gnus-extra-header (type &optional header)
4713   "Return the extra header of TYPE."
4714   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4715       ""))
4716
4717 (defvar gnus-tmp-thread-tree-header-string "")
4718
4719 (defcustom gnus-sum-thread-tree-root "> "
4720   "With %B spec, used for the root of a thread.
4721 If nil, use subject instead."
4722   :version "22.1"
4723   :type '(radio (const :format "%v  " nil) string)
4724   :group 'gnus-thread)
4725 (defcustom gnus-sum-thread-tree-false-root "> "
4726   "With %B spec, used for a false root of a thread.
4727 If nil, use subject instead."
4728   :version "22.1"
4729   :type '(radio (const :format "%v  " nil) string)
4730   :group 'gnus-thread)
4731 (defcustom gnus-sum-thread-tree-single-indent ""
4732   "With %B spec, used for a thread with just one message.
4733 If nil, use subject instead."
4734   :version "22.1"
4735   :type '(radio (const :format "%v  " nil) string)
4736   :group 'gnus-thread)
4737 (defcustom gnus-sum-thread-tree-vertical "| "
4738   "With %B spec, used for drawing a vertical line."
4739   :version "22.1"
4740   :type 'string
4741   :group 'gnus-thread)
4742 (defcustom gnus-sum-thread-tree-indent "  "
4743   "With %B spec, used for indenting."
4744   :version "22.1"
4745   :type 'string
4746   :group 'gnus-thread)
4747 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4748   "With %B spec, used for a leaf with brothers."
4749   :version "22.1"
4750   :type 'string
4751   :group 'gnus-thread)
4752 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4753   "With %B spec, used for a leaf without brothers."
4754   :version "22.1"
4755   :type 'string
4756   :group 'gnus-thread)
4757
4758 (defun gnus-summary-prepare-threads (threads)
4759   "Prepare summary buffer from THREADS and indentation LEVEL.
4760 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4761 or a straight list of headers."
4762   (gnus-message 7 "Generating summary...")
4763
4764   (setq gnus-newsgroup-threads threads)
4765   (beginning-of-line)
4766
4767   (let ((gnus-tmp-level 0)
4768         (default-score (or gnus-summary-default-score 0))
4769         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4770         (building-line-count gnus-summary-display-while-building)
4771         (building-count (integerp gnus-summary-display-while-building))
4772         thread number subject stack state gnus-tmp-gathered beg-match
4773         new-roots gnus-tmp-new-adopts thread-end simp-subject
4774         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4775         gnus-tmp-replied gnus-tmp-subject-or-nil
4776         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4777         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4778         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4779         tree-stack)
4780
4781     (setq gnus-tmp-prev-subject nil
4782           gnus-tmp-thread-tree-header-string "")
4783
4784     (if (vectorp (car threads))
4785         ;; If this is a straight (sic) list of headers, then a
4786         ;; threaded summary display isn't required, so we just create
4787         ;; an unthreaded one.
4788         (gnus-summary-prepare-unthreaded threads)
4789
4790       ;; Do the threaded display.
4791
4792       (if gnus-summary-display-while-building
4793           (switch-to-buffer (buffer-name)))
4794       (while (or threads stack gnus-tmp-new-adopts new-roots)
4795
4796         (if (and (= gnus-tmp-level 0)
4797                  (or (not stack)
4798                      (= (caar stack) 0))
4799                  (not gnus-tmp-false-parent)
4800                  (or gnus-tmp-new-adopts new-roots))
4801             (if gnus-tmp-new-adopts
4802                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4803                       thread (list (car gnus-tmp-new-adopts))
4804                       gnus-tmp-header (caar thread)
4805                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4806               (when new-roots
4807                 (setq thread (list (car new-roots))
4808                       gnus-tmp-header (caar thread)
4809                       new-roots (cdr new-roots))))
4810
4811           (if threads
4812               ;; If there are some threads, we do them before the
4813               ;; threads on the stack.
4814               (setq thread threads
4815                     gnus-tmp-header (caar thread))
4816             ;; There were no current threads, so we pop something off
4817             ;; the stack.
4818             (setq state (car stack)
4819                   gnus-tmp-level (car state)
4820                   tree-stack (cadr state)
4821                   thread (caddr state)
4822                   stack (cdr stack)
4823                   gnus-tmp-header (caar thread))))
4824
4825         (setq gnus-tmp-false-parent nil)
4826         (setq gnus-tmp-root-expunged nil)
4827         (setq thread-end nil)
4828
4829         (if (stringp gnus-tmp-header)
4830             ;; The header is a dummy root.
4831             (cond
4832              ((eq gnus-summary-make-false-root 'adopt)
4833               ;; We let the first article adopt the rest.
4834               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4835                                                (cddar thread)))
4836               (setq gnus-tmp-gathered
4837                     (nconc (mapcar
4838                             (lambda (h) (mail-header-number (car h)))
4839                             (cddar thread))
4840                            gnus-tmp-gathered))
4841               (setq thread (cons (list (caar thread)
4842                                        (cadar thread))
4843                                  (cdr thread)))
4844               (setq gnus-tmp-level -1
4845                     gnus-tmp-false-parent t))
4846              ((eq gnus-summary-make-false-root 'empty)
4847               ;; We print adopted articles with empty subject fields.
4848               (setq gnus-tmp-gathered
4849                     (nconc (mapcar
4850                             (lambda (h) (mail-header-number (car h)))
4851                             (cddar thread))
4852                            gnus-tmp-gathered))
4853               (setq gnus-tmp-level -1))
4854              ((eq gnus-summary-make-false-root 'dummy)
4855               ;; We remember that we probably want to output a dummy
4856               ;; root.
4857               (setq gnus-tmp-dummy-line gnus-tmp-header)
4858               (setq gnus-tmp-prev-subject gnus-tmp-header))
4859              (t
4860               ;; We do not make a root for the gathered
4861               ;; sub-threads at all.
4862               (setq gnus-tmp-level -1)))
4863
4864           (setq number (mail-header-number gnus-tmp-header)
4865                 subject (mail-header-subject gnus-tmp-header)
4866                 simp-subject (gnus-simplify-subject-fully subject))
4867
4868           (cond
4869            ;; If the thread has changed subject, we might want to make
4870            ;; this subthread into a root.
4871            ((and (null gnus-thread-ignore-subject)
4872                  (not (zerop gnus-tmp-level))
4873                  gnus-tmp-prev-subject
4874                  (not (string= gnus-tmp-prev-subject simp-subject)))
4875             (setq new-roots (nconc new-roots (list (car thread)))
4876                   thread-end t
4877                   gnus-tmp-header nil))
4878            ;; If the article lies outside the current limit,
4879            ;; then we do not display it.
4880            ((not (memq number gnus-newsgroup-limit))
4881             (setq gnus-tmp-gathered
4882                   (nconc (mapcar
4883                           (lambda (h) (mail-header-number (car h)))
4884                           (cdar thread))
4885                          gnus-tmp-gathered))
4886             (setq gnus-tmp-new-adopts (if (cdar thread)
4887                                           (append gnus-tmp-new-adopts
4888                                                   (cdar thread))
4889                                         gnus-tmp-new-adopts)
4890                   thread-end t
4891                   gnus-tmp-header nil)
4892             (when (zerop gnus-tmp-level)
4893               (setq gnus-tmp-root-expunged t)))
4894            ;; Perhaps this article is to be marked as read?
4895            ((and gnus-summary-mark-below
4896                  (< (or (cdr (assq number gnus-newsgroup-scored))
4897                         default-score)
4898                     gnus-summary-mark-below)
4899                  ;; Don't touch sparse articles.
4900                  (not (gnus-summary-article-sparse-p number))
4901                  (not (gnus-summary-article-ancient-p number)))
4902             (setq gnus-newsgroup-unreads
4903                   (delq number gnus-newsgroup-unreads))
4904             (if gnus-newsgroup-auto-expire
4905                 (setq gnus-newsgroup-expirable
4906                       (gnus-add-to-sorted-list
4907                        gnus-newsgroup-expirable number))
4908               (push (cons number gnus-low-score-mark)
4909                     gnus-newsgroup-reads))))
4910
4911           (when gnus-tmp-header
4912             ;; We may have an old dummy line to output before this
4913             ;; article.
4914             (when (and gnus-tmp-dummy-line
4915                        (gnus-subject-equal
4916                         gnus-tmp-dummy-line
4917                         (mail-header-subject gnus-tmp-header)))
4918               (gnus-summary-insert-dummy-line
4919                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4920               (setq gnus-tmp-dummy-line nil))
4921
4922             ;; Compute the mark.
4923             (setq gnus-tmp-unread (gnus-article-mark number))
4924
4925             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4926                                   gnus-tmp-header gnus-tmp-level)
4927                   gnus-newsgroup-data)
4928
4929             ;; Actually insert the line.
4930             (setq
4931              gnus-tmp-subject-or-nil
4932              (cond
4933               ((and gnus-thread-ignore-subject
4934                     gnus-tmp-prev-subject
4935                     (not (string= gnus-tmp-prev-subject simp-subject)))
4936                subject)
4937               ((zerop gnus-tmp-level)
4938                (if (and (eq gnus-summary-make-false-root 'empty)
4939                         (memq number gnus-tmp-gathered)
4940                         gnus-tmp-prev-subject
4941                         (string= gnus-tmp-prev-subject simp-subject))
4942                    gnus-summary-same-subject
4943                  subject))
4944               (t gnus-summary-same-subject)))
4945             (if (and (eq gnus-summary-make-false-root 'adopt)
4946                      (= gnus-tmp-level 1)
4947                      (memq number gnus-tmp-gathered))
4948                 (setq gnus-tmp-opening-bracket ?\<
4949                       gnus-tmp-closing-bracket ?\>)
4950               (setq gnus-tmp-opening-bracket ?\[
4951                     gnus-tmp-closing-bracket ?\]))
4952             (setq
4953              gnus-tmp-indentation
4954              (aref gnus-thread-indent-array gnus-tmp-level)
4955              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4956              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4957                                 gnus-summary-default-score 0)
4958              gnus-tmp-score-char
4959              (if (or (null gnus-summary-default-score)
4960                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4961                          gnus-summary-zcore-fuzz))
4962                  ?                      ;Whitespace
4963                (if (< gnus-tmp-score gnus-summary-default-score)
4964                    gnus-score-below-mark gnus-score-over-mark))
4965              gnus-tmp-replied
4966              (cond ((memq number gnus-newsgroup-processable)
4967                     gnus-process-mark)
4968                    ((memq number gnus-newsgroup-cached)
4969                     gnus-cached-mark)
4970                    ((memq number gnus-newsgroup-replied)
4971                     gnus-replied-mark)
4972                    ((memq number gnus-newsgroup-forwarded)
4973                     gnus-forwarded-mark)
4974                    ((memq number gnus-newsgroup-saved)
4975                     gnus-saved-mark)
4976                    ((memq number gnus-newsgroup-recent)
4977                     gnus-recent-mark)
4978                    ((memq number gnus-newsgroup-unseen)
4979                     gnus-unseen-mark)
4980                    (t gnus-no-mark))
4981              gnus-tmp-downloaded
4982              (cond ((memq number gnus-newsgroup-undownloaded)
4983                     gnus-undownloaded-mark)
4984                    (gnus-newsgroup-agentized
4985                     gnus-downloaded-mark)
4986                    (t
4987                     gnus-no-mark))
4988              gnus-tmp-from (mail-header-from gnus-tmp-header)
4989              gnus-tmp-name
4990              (cond
4991               ((string-match "<[^>]+> *$" gnus-tmp-from)
4992                (setq beg-match (match-beginning 0))
4993                (or (and (string-match "^\".+\"" gnus-tmp-from)
4994                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4995                    (substring gnus-tmp-from 0 beg-match)))
4996               ((string-match "(.+)" gnus-tmp-from)
4997                (substring gnus-tmp-from
4998                           (1+ (match-beginning 0)) (1- (match-end 0))))
4999               (t gnus-tmp-from))
5000
5001              ;; Do the %B string
5002              gnus-tmp-thread-tree-header-string
5003              (cond
5004               ((not gnus-show-threads) "")
5005               ((zerop gnus-tmp-level)
5006                (cond ((cdar thread)
5007                       (or gnus-sum-thread-tree-root subject))
5008                      (gnus-tmp-new-adopts
5009                       (or gnus-sum-thread-tree-false-root subject))
5010                      (t
5011                       (or gnus-sum-thread-tree-single-indent subject))))
5012               (t
5013                (concat (apply 'concat
5014                               (mapcar (lambda (item)
5015                                         (if (= item 1)
5016                                             gnus-sum-thread-tree-vertical
5017                                           gnus-sum-thread-tree-indent))
5018                                       (cdr (reverse tree-stack))))
5019                        (if (nth 1 thread)
5020                            gnus-sum-thread-tree-leaf-with-other
5021                          gnus-sum-thread-tree-single-leaf)))))
5022             (when (string= gnus-tmp-name "")
5023               (setq gnus-tmp-name gnus-tmp-from))
5024             (unless (numberp gnus-tmp-lines)
5025               (setq gnus-tmp-lines -1))
5026             (if (= gnus-tmp-lines -1)
5027                 (setq gnus-tmp-lines "?")
5028               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5029               (gnus-put-text-property
5030              (point)
5031              (progn (eval gnus-summary-line-format-spec) (point))
5032                'gnus-number number)
5033             (when gnus-visual-p
5034               (forward-line -1)
5035               (gnus-run-hooks 'gnus-summary-update-hook)
5036               (forward-line 1))
5037
5038             (setq gnus-tmp-prev-subject simp-subject)))
5039
5040         (when (nth 1 thread)
5041           (push (list (max 0 gnus-tmp-level)
5042                       (copy-sequence tree-stack)
5043                       (nthcdr 1 thread))
5044                 stack))
5045         (push (if (nth 1 thread) 1 0) tree-stack)
5046         (incf gnus-tmp-level)
5047         (setq threads (if thread-end nil (cdar thread)))
5048         (if gnus-summary-display-while-building
5049             (if building-count
5050                 (progn
5051                   ;; use a set frequency
5052                   (setq building-line-count (1- building-line-count))
5053                   (when (= building-line-count 0)
5054                     (sit-for 0)
5055                     (setq building-line-count
5056                           gnus-summary-display-while-building)))
5057               ;; always
5058               (sit-for 0)))
5059         (unless threads
5060           (setq gnus-tmp-level 0)))))
5061   (gnus-message 7 "Generating summary...done"))
5062
5063 (defun gnus-summary-prepare-unthreaded (headers)
5064   "Generate an unthreaded summary buffer based on HEADERS."
5065   (let (header number mark)
5066
5067     (beginning-of-line)
5068
5069     (while headers
5070       ;; We may have to root out some bad articles...
5071       (when (memq (setq number (mail-header-number
5072                                 (setq header (pop headers))))
5073                   gnus-newsgroup-limit)
5074         ;; Mark article as read when it has a low score.
5075         (when (and gnus-summary-mark-below
5076                    (< (or (cdr (assq number gnus-newsgroup-scored))
5077                           gnus-summary-default-score 0)
5078                       gnus-summary-mark-below)
5079                    (not (gnus-summary-article-ancient-p number)))
5080           (setq gnus-newsgroup-unreads
5081                 (delq number gnus-newsgroup-unreads))
5082           (if gnus-newsgroup-auto-expire
5083               (push number gnus-newsgroup-expirable)
5084             (push (cons number gnus-low-score-mark)
5085                   gnus-newsgroup-reads)))
5086
5087         (setq mark (gnus-article-mark number))
5088         (push (gnus-data-make number mark (1+ (point)) header 0)
5089               gnus-newsgroup-data)
5090         (gnus-summary-insert-line
5091          header 0 number
5092          (memq number gnus-newsgroup-undownloaded)
5093          mark (memq number gnus-newsgroup-replied)
5094          (memq number gnus-newsgroup-expirable)
5095          (mail-header-subject header) nil
5096          (cdr (assq number gnus-newsgroup-scored))
5097          (memq number gnus-newsgroup-processable))))))
5098
5099 (defun gnus-summary-remove-list-identifiers ()
5100   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5101   (let ((regexp (if (consp gnus-list-identifiers)
5102                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5103                   gnus-list-identifiers))
5104         changed subject)
5105     (when regexp
5106       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5107       (dolist (header gnus-newsgroup-headers)
5108         (setq subject (mail-header-subject header)
5109               changed nil)
5110         (while (string-match regexp subject)
5111           (setq subject
5112                 (concat (substring subject 0 (match-beginning 1))
5113                         (substring subject (match-end 0)))
5114                 changed t))
5115         (when changed
5116           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5117             (setq subject
5118                   (concat (substring subject 0 (match-beginning 1))
5119                           (substring subject (match-end 1)))))
5120           (mail-header-set-subject header subject))))))
5121
5122 (defun gnus-fetch-headers (articles)
5123   "Fetch headers of ARTICLES."
5124   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5125     (gnus-message 5 "Fetching headers for %s..." name)
5126     (prog1
5127         (if (eq 'nov
5128                 (setq gnus-headers-retrieved-by
5129                       (gnus-retrieve-headers
5130                        articles gnus-newsgroup-name
5131                        ;; We might want to fetch old headers, but
5132                        ;; not if there is only 1 article.
5133                        (and (or (and
5134                                  (not (eq gnus-fetch-old-headers 'some))
5135                                  (not (numberp gnus-fetch-old-headers)))
5136                                 (> (length articles) 1))
5137                             gnus-fetch-old-headers))))
5138             (gnus-get-newsgroup-headers-xover
5139              articles nil nil gnus-newsgroup-name t)
5140           (gnus-get-newsgroup-headers))
5141       (gnus-message 5 "Fetching headers for %s...done" name))))
5142
5143 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5144   "Select newsgroup GROUP.
5145 If READ-ALL is non-nil, all articles in the group are selected.
5146 If SELECT-ARTICLES, only select those articles from GROUP."
5147   (let* ((entry (gnus-group-entry group))
5148          ;;!!! Dirty hack; should be removed.
5149          (gnus-summary-ignore-duplicates
5150           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5151               t
5152             gnus-summary-ignore-duplicates))
5153          (info (nth 2 entry))
5154          articles fetched-articles cached)
5155
5156     (unless (gnus-check-server
5157              (set (make-local-variable 'gnus-current-select-method)
5158                   (gnus-find-method-for-group group)))
5159       (error "Couldn't open server"))
5160
5161     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5162         (gnus-activate-group group)     ; Or we can activate it...
5163         (progn                          ; Or we bug out.
5164           (when (equal major-mode 'gnus-summary-mode)
5165             (gnus-kill-buffer (current-buffer)))
5166           (error "Couldn't activate group %s: %s"
5167                  group (gnus-status-message group))))
5168
5169     (unless (gnus-request-group group t)
5170       (when (equal major-mode 'gnus-summary-mode)
5171         (gnus-kill-buffer (current-buffer)))
5172       (error "Couldn't request group %s: %s"
5173              group (gnus-status-message group)))
5174
5175     (when gnus-agent
5176       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5177
5178       (setq gnus-summary-use-undownloaded-faces
5179             (gnus-agent-find-parameter
5180              group
5181              'agent-enable-undownloaded-faces)))
5182
5183     (setq gnus-newsgroup-name group
5184           gnus-newsgroup-unselected nil
5185           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5186
5187     (let ((display (gnus-group-find-parameter group 'display)))
5188       (setq gnus-newsgroup-display
5189             (cond
5190              ((not (zerop (or (car-safe read-all) 0)))
5191               ;; The user entered the group with C-u SPC/RET, let's show
5192               ;; all articles.
5193               'gnus-not-ignore)
5194              ((eq display 'all)
5195               'gnus-not-ignore)
5196              ((arrayp display)
5197               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5198              ((numberp display)
5199               ;; The following is probably the "correct" solution, but
5200               ;; it makes Gnus fetch all headers and then limit the
5201               ;; articles (which is slow), so instead we hack the
5202               ;; select-articles parameter instead. -- Simon Josefsson
5203               ;; <jas@kth.se>
5204               ;;
5205               ;; (gnus-byte-compile
5206               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5207               ;;                         display)))))
5208               (setq select-articles
5209                     (gnus-uncompress-range
5210                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5211                              (if (> tmp 0)
5212                                  tmp
5213                                1))
5214                            (cdr (gnus-active group)))))
5215               nil)
5216              (t
5217               nil))))
5218
5219     (gnus-summary-setup-default-charset)
5220
5221     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5222     (when (gnus-virtual-group-p group)
5223       (setq cached gnus-newsgroup-cached))
5224
5225     (setq gnus-newsgroup-unreads
5226           (gnus-sorted-ndifference
5227            (gnus-sorted-ndifference gnus-newsgroup-unreads
5228                                     gnus-newsgroup-marked)
5229            gnus-newsgroup-dormant))
5230
5231     (setq gnus-newsgroup-processable nil)
5232
5233     (gnus-update-read-articles group gnus-newsgroup-unreads)
5234
5235     ;; Adjust and set lists of article marks.
5236     (when info
5237       (gnus-adjust-marked-articles info))
5238     (if (setq articles select-articles)
5239         (setq gnus-newsgroup-unselected
5240               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5241       (setq articles (gnus-articles-to-read group read-all)))
5242
5243     (cond
5244      ((null articles)
5245       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5246       'quit)
5247      ((eq articles 0) nil)
5248      (t
5249       ;; Init the dependencies hash table.
5250       (setq gnus-newsgroup-dependencies
5251             (gnus-make-hashtable (length articles)))
5252       (gnus-set-global-variables)
5253       ;; Retrieve the headers and read them in.
5254
5255       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5256
5257       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5258       (when cached
5259         (setq gnus-newsgroup-cached cached))
5260
5261       ;; Suppress duplicates?
5262       (when gnus-suppress-duplicates
5263         (gnus-dup-suppress-articles))
5264
5265       ;; Set the initial limit.
5266       (setq gnus-newsgroup-limit (copy-sequence articles))
5267       ;; Remove canceled articles from the list of unread articles.
5268       (setq fetched-articles
5269             (mapcar (lambda (headers) (mail-header-number headers))
5270                     gnus-newsgroup-headers))
5271       (setq gnus-newsgroup-articles fetched-articles)
5272       (setq gnus-newsgroup-unreads
5273             (gnus-sorted-nintersection
5274              gnus-newsgroup-unreads fetched-articles))
5275       (gnus-compute-unseen-list)
5276
5277       ;; Removed marked articles that do not exist.
5278       (gnus-update-missing-marks
5279        (gnus-sorted-difference articles fetched-articles))
5280       ;; We might want to build some more threads first.
5281       (when (and gnus-fetch-old-headers
5282                  (eq gnus-headers-retrieved-by 'nov))
5283         (if (eq gnus-fetch-old-headers 'invisible)
5284             (gnus-build-all-threads)
5285           (gnus-build-old-threads)))
5286       ;; Let the Gnus agent mark articles as read.
5287       (when gnus-agent
5288         (gnus-agent-get-undownloaded-list))
5289       ;; Remove list identifiers from subject
5290       (when gnus-list-identifiers
5291         (gnus-summary-remove-list-identifiers))
5292       ;; Check whether auto-expire is to be done in this group.
5293       (setq gnus-newsgroup-auto-expire
5294             (gnus-group-auto-expirable-p group))
5295       ;; Set up the article buffer now, if necessary.
5296       (unless gnus-single-article-buffer
5297         (gnus-article-setup-buffer))
5298       ;; First and last article in this newsgroup.
5299       (when gnus-newsgroup-headers
5300         (setq gnus-newsgroup-begin
5301               (mail-header-number (car gnus-newsgroup-headers))
5302               gnus-newsgroup-end
5303               (mail-header-number
5304                (gnus-last-element gnus-newsgroup-headers))))
5305       ;; GROUP is successfully selected.
5306       (or gnus-newsgroup-headers t)))))
5307
5308 (defun gnus-compute-unseen-list ()
5309   ;; The `seen' marks are treated specially.
5310   (if (not gnus-newsgroup-seen)
5311       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5312     (setq gnus-newsgroup-unseen
5313           (gnus-inverse-list-range-intersection
5314            gnus-newsgroup-articles gnus-newsgroup-seen))))
5315
5316 (defun gnus-summary-display-make-predicate (display)
5317   (require 'gnus-agent)
5318   (when (= (length display) 1)
5319     (setq display (car display)))
5320   (unless gnus-summary-display-cache
5321     (dolist (elem (append '((unread . unread)
5322                             (read . read)
5323                             (unseen . unseen))
5324                           gnus-article-mark-lists))
5325       (push (cons (cdr elem)
5326                   (gnus-byte-compile
5327                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5328             gnus-summary-display-cache)))
5329   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5330         (gnus-category-predicate-cache gnus-summary-display-cache))
5331     (gnus-get-predicate display)))
5332
5333 ;; Uses the dynamically bound `number' variable.
5334 (eval-when-compile
5335   (defvar number))
5336 (defun gnus-article-marked-p (type &optional article)
5337   (let ((article (or article number)))
5338     (cond
5339      ((eq type 'tick)
5340       (memq article gnus-newsgroup-marked))
5341      ((eq type 'spam)
5342       (memq article gnus-newsgroup-spam-marked))
5343      ((eq type 'unsend)
5344       (memq article gnus-newsgroup-unsendable))
5345      ((eq type 'undownload)
5346       (memq article gnus-newsgroup-undownloaded))
5347      ((eq type 'download)
5348       (memq article gnus-newsgroup-downloadable))
5349      ((eq type 'unread)
5350       (memq article gnus-newsgroup-unreads))
5351      ((eq type 'read)
5352       (memq article gnus-newsgroup-reads))
5353      ((eq type 'dormant)
5354       (memq article gnus-newsgroup-dormant) )
5355      ((eq type 'expire)
5356       (memq article gnus-newsgroup-expirable))
5357      ((eq type 'reply)
5358       (memq article gnus-newsgroup-replied))
5359      ((eq type 'killed)
5360       (memq article gnus-newsgroup-killed))
5361      ((eq type 'bookmark)
5362       (assq article gnus-newsgroup-bookmarks))
5363      ((eq type 'score)
5364       (assq article gnus-newsgroup-scored))
5365      ((eq type 'save)
5366       (memq article gnus-newsgroup-saved))
5367      ((eq type 'cache)
5368       (memq article gnus-newsgroup-cached))
5369      ((eq type 'forward)
5370       (memq article gnus-newsgroup-forwarded))
5371      ((eq type 'seen)
5372       (not (memq article gnus-newsgroup-unseen)))
5373      ((eq type 'recent)
5374       (memq article gnus-newsgroup-recent))
5375      (t t))))
5376
5377 (defun gnus-articles-to-read (group &optional read-all)
5378   "Find out what articles the user wants to read."
5379   (let* ((articles
5380           ;; Select all articles if `read-all' is non-nil, or if there
5381           ;; are no unread articles.
5382           (if (or read-all
5383                   (and (zerop (length gnus-newsgroup-marked))
5384                        (zerop (length gnus-newsgroup-unreads)))
5385                   ;; Fetch all if the predicate is non-nil.
5386                   gnus-newsgroup-display)
5387               ;; We want to select the headers for all the articles in
5388               ;; the group, so we select either all the active
5389               ;; articles in the group, or (if that's nil), the
5390               ;; articles in the cache.
5391               (or
5392                (gnus-uncompress-range (gnus-active group))
5393                (gnus-cache-articles-in-group group))
5394             ;; Select only the "normal" subset of articles.
5395             (gnus-sorted-nunion
5396              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5397              gnus-newsgroup-unreads)))
5398          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5399          (scored (length scored-list))
5400          (number (length articles))
5401          (marked (+ (length gnus-newsgroup-marked)
5402                     (length gnus-newsgroup-dormant)))
5403          (select
5404           (cond
5405            ((numberp read-all)
5406             read-all)
5407            ((numberp gnus-newsgroup-display)
5408             gnus-newsgroup-display)
5409            (t
5410             (condition-case ()
5411                 (cond
5412                  ((and (or (<= scored marked) (= scored number))
5413                        (numberp gnus-large-newsgroup)
5414                        (> number gnus-large-newsgroup))
5415                   (let* ((cursor-in-echo-area nil)
5416                          (initial (gnus-parameter-large-newsgroup-initial
5417                                    gnus-newsgroup-name))
5418                          (input
5419                           (read-string
5420                            (format
5421                             "How many articles from %s (%s %d): "
5422                             (gnus-limit-string
5423                              (gnus-group-decoded-name gnus-newsgroup-name)
5424                              35)
5425                             (if initial "max" "default")
5426                             number)
5427                            (if initial
5428                                (cons (number-to-string initial)
5429                                      0)))))
5430                     (if (string-match "^[ \t]*$" input) number input)))
5431                  ((and (> scored marked) (< scored number)
5432                        (> (- scored number) 20))
5433                   (let ((input
5434                          (read-string
5435                           (format "%s %s (%d scored, %d total): "
5436                                   "How many articles from"
5437                                   (gnus-group-decoded-name group)
5438                                   scored number))))
5439                     (if (string-match "^[ \t]*$" input)
5440                         number input)))
5441                  (t number))
5442               (quit
5443                (message "Quit getting the articles to read")
5444                nil))))))
5445     (setq select (if (stringp select) (string-to-number select) select))
5446     (if (or (null select) (zerop select))
5447         select
5448       (if (and (not (zerop scored)) (<= (abs select) scored))
5449           (progn
5450             (setq articles (sort scored-list '<))
5451             (setq number (length articles)))
5452         (setq articles (copy-sequence articles)))
5453
5454       (when (< (abs select) number)
5455         (if (< select 0)
5456             ;; Select the N oldest articles.
5457             (setcdr (nthcdr (1- (abs select)) articles) nil)
5458           ;; Select the N most recent articles.
5459           (setq articles (nthcdr (- number select) articles))))
5460       (setq gnus-newsgroup-unselected
5461             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5462       (when gnus-alter-articles-to-read-function
5463         (setq articles
5464               (sort
5465                (funcall gnus-alter-articles-to-read-function
5466                         gnus-newsgroup-name articles)
5467                '<)))
5468       articles)))
5469
5470 (defun gnus-killed-articles (killed articles)
5471   (let (out)
5472     (while articles
5473       (when (inline (gnus-member-of-range (car articles) killed))
5474         (push (car articles) out))
5475       (setq articles (cdr articles)))
5476     out))
5477
5478 (defun gnus-uncompress-marks (marks)
5479   "Uncompress the mark ranges in MARKS."
5480   (let ((uncompressed '(score bookmark))
5481         out)
5482     (while marks
5483       (if (memq (caar marks) uncompressed)
5484           (push (car marks) out)
5485         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5486       (setq marks (cdr marks)))
5487     out))
5488
5489 (defun gnus-article-mark-to-type (mark)
5490   "Return the type of MARK."
5491   (or (cadr (assq mark gnus-article-special-mark-lists))
5492       'list))
5493
5494 (defun gnus-article-unpropagatable-p (mark)
5495   "Return whether MARK should be propagated to back end."
5496   (memq mark gnus-article-unpropagated-mark-lists))
5497
5498 (defun gnus-adjust-marked-articles (info)
5499   "Set all article lists and remove all marks that are no longer valid."
5500   (let* ((marked-lists (gnus-info-marks info))
5501          (active (gnus-active (gnus-info-group info)))
5502          (min (car active))
5503          (max (cdr active))
5504          (types gnus-article-mark-lists)
5505          marks var articles article mark mark-type
5506          bgn end)
5507
5508     (dolist (marks marked-lists)
5509       (setq mark (car marks)
5510             mark-type (gnus-article-mark-to-type mark)
5511             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5512
5513       ;; We set the variable according to the type of the marks list,
5514       ;; and then adjust the marks to a subset of the active articles.
5515       (cond
5516        ;; Adjust "simple" lists - compressed yet unsorted
5517        ((eq mark-type 'list)
5518         ;; Simultaneously uncompress and clip to active range
5519         ;; See gnus-uncompress-range for a description of possible marks
5520         (let (l lh)
5521           (if (not (cadr marks))
5522               (set var nil)
5523             (setq articles (if (numberp (cddr marks))
5524                                (list (cdr marks))
5525                              (cdr marks))
5526                   lh (cons nil nil)
5527                   l lh)
5528
5529             (while (setq article (pop articles))
5530               (cond ((consp article)
5531                      (setq bgn (max (car article) min)
5532                            end (min (cdr article) max))
5533                      (while (<= bgn end)
5534                        (setq l (setcdr l (cons bgn nil))
5535                              bgn (1+ bgn))))
5536                     ((and (<= min article)
5537                           (>= max article))
5538                      (setq l (setcdr l (cons article nil))))))
5539             (set var (cdr lh)))))
5540        ;; Adjust assocs.
5541        ((eq mark-type 'tuple)
5542         (set var (setq articles (cdr marks)))
5543         (when (not (listp (cdr (symbol-value var))))
5544           (set var (list (symbol-value var))))
5545         (when (not (listp (cdr articles)))
5546           (setq articles (list articles)))
5547         (while articles
5548           (when (or (not (consp (setq article (pop articles))))
5549                     (< (car article) min)
5550                     (> (car article) max))
5551             (set var (delq article (symbol-value var))))))
5552        ;; Adjust ranges (sloppily).
5553        ((eq mark-type 'range)
5554         (cond
5555          ((eq mark 'seen)
5556           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5557           ;; It should be (seen (NUM1 . NUM2)).
5558           (when (numberp (cddr marks))
5559             (setcdr marks (list (cdr marks))))
5560           (setq articles (cdr marks))
5561           (while (and articles
5562                       (or (and (consp (car articles))
5563                                (> min (cdar articles)))
5564                           (and (numberp (car articles))
5565                                (> min (car articles)))))
5566             (pop articles))
5567           (set var articles))))))))
5568
5569 (defun gnus-update-missing-marks (missing)
5570   "Go through the list of MISSING articles and remove them from the mark lists."
5571   (when missing
5572     (let (var m)
5573       ;; Go through all types.
5574       (dolist (elem gnus-article-mark-lists)
5575         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5576           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5577           (when (symbol-value var)
5578             ;; This list has articles.  So we delete all missing
5579             ;; articles from it.
5580             (setq m missing)
5581             (while m
5582               (set var (delq (pop m) (symbol-value var))))))))))
5583
5584 (defun gnus-update-marks ()
5585   "Enter the various lists of marked articles into the newsgroup info list."
5586   (let ((types gnus-article-mark-lists)
5587         (info (gnus-get-info gnus-newsgroup-name))
5588         type list newmarked symbol delta-marks)
5589     (when info
5590       ;; Add all marks lists to the list of marks lists.
5591       (while (setq type (pop types))
5592         (setq list (symbol-value
5593                     (setq symbol
5594                           (intern (format "gnus-newsgroup-%s" (car type))))))
5595
5596         (when list
5597           ;; Get rid of the entries of the articles that have the
5598           ;; default score.
5599           (when (and (eq (cdr type) 'score)
5600                      gnus-save-score
5601                      list)
5602             (let* ((arts list)
5603                    (prev (cons nil list))
5604                    (all prev))
5605               (while arts
5606                 (if (or (not (consp (car arts)))
5607                         (= (cdar arts) gnus-summary-default-score))
5608                     (setcdr prev (cdr arts))
5609                   (setq prev arts))
5610                 (setq arts (cdr arts)))
5611               (setq list (cdr all)))))
5612
5613         (when (eq (cdr type) 'seen)
5614           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5615
5616         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5617           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5618
5619         (when (and (gnus-check-backend-function
5620                     'request-set-mark gnus-newsgroup-name)
5621                    (not (gnus-article-unpropagatable-p (cdr type))))
5622           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5623                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5624                  (add (gnus-remove-from-range
5625                        (gnus-copy-sequence list) old)))
5626             (when add
5627               (push (list add 'add (list (cdr type))) delta-marks))
5628             (when del
5629               (push (list del 'del (list (cdr type))) delta-marks))))
5630
5631         (when list
5632           (push (cons (cdr type) list) newmarked)))
5633
5634       (when delta-marks
5635         (unless (gnus-check-group gnus-newsgroup-name)
5636           (error "Can't open server for %s" gnus-newsgroup-name))
5637         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5638
5639       ;; Enter these new marks into the info of the group.
5640       (if (nthcdr 3 info)
5641           (setcar (nthcdr 3 info) newmarked)
5642         ;; Add the marks lists to the end of the info.
5643         (when newmarked
5644           (setcdr (nthcdr 2 info) (list newmarked))))
5645
5646       ;; Cut off the end of the info if there's nothing else there.
5647       (let ((i 5))
5648         (while (and (> i 2)
5649                     (not (nth i info)))
5650           (when (nthcdr (decf i) info)
5651             (setcdr (nthcdr i info) nil)))))))
5652
5653 (defun gnus-set-mode-line (where)
5654   "Set the mode line of the article or summary buffers.
5655 If WHERE is `summary', the summary mode line format will be used."
5656   ;; Is this mode line one we keep updated?
5657   (when (and (memq where gnus-updated-mode-lines)
5658              (symbol-value
5659               (intern (format "gnus-%s-mode-line-format-spec" where))))
5660     (let (mode-string)
5661       (save-excursion
5662         ;; We evaluate this in the summary buffer since these
5663         ;; variables are buffer-local to that buffer.
5664         (set-buffer gnus-summary-buffer)
5665        ;; We bind all these variables that are used in the `eval' form
5666         ;; below.
5667         (let* ((mformat (symbol-value
5668                          (intern
5669                           (format "gnus-%s-mode-line-format-spec" where))))
5670                (gnus-tmp-group-name (gnus-group-decoded-name
5671                                      gnus-newsgroup-name))
5672                (gnus-tmp-article-number (or gnus-current-article 0))
5673                (gnus-tmp-unread gnus-newsgroup-unreads)
5674                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5675                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5676                (gnus-tmp-unread-and-unselected
5677                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5678                             (zerop gnus-tmp-unselected))
5679                        "")
5680                       ((zerop gnus-tmp-unselected)
5681                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5682                       (t (format "{%d(+%d) more}"
5683                                  gnus-tmp-unread-and-unticked
5684                                  gnus-tmp-unselected))))
5685                (gnus-tmp-subject
5686                 (if (and gnus-current-headers
5687                          (vectorp gnus-current-headers))
5688                     (gnus-mode-string-quote
5689                      (mail-header-subject gnus-current-headers))
5690                   ""))
5691                bufname-length max-len
5692                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5693           (setq mode-string (eval mformat))
5694           (setq bufname-length (if (string-match "%b" mode-string)
5695                                    (- (length
5696                                        (buffer-name
5697                                         (if (eq where 'summary)
5698                                             nil
5699                                           (get-buffer gnus-article-buffer))))
5700                                       2)
5701                                  0))
5702           (setq max-len (max 4 (if gnus-mode-non-string-length
5703                                    (- (window-width)
5704                                       gnus-mode-non-string-length
5705                                       bufname-length)
5706                                  (length mode-string))))
5707           ;; We might have to chop a bit of the string off...
5708           (when (> (length mode-string) max-len)
5709             (setq mode-string
5710                   (concat (truncate-string-to-width mode-string (- max-len 3))
5711                           "...")))
5712           ;; Pad the mode string a bit.
5713           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5714       ;; Update the mode line.
5715       (setq mode-line-buffer-identification
5716             (gnus-mode-line-buffer-identification (list mode-string)))
5717       (set-buffer-modified-p t))))
5718
5719 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5720   "Go through the HEADERS list and add all Xrefs to a hash table.
5721 The resulting hash table is returned, or nil if no Xrefs were found."
5722   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5723          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5724          (xref-hashtb (gnus-make-hashtable))
5725          start group entry number xrefs header)
5726     (while headers
5727       (setq header (pop headers))
5728       (when (and (setq xrefs (mail-header-xref header))
5729                  (not (memq (setq number (mail-header-number header))
5730                             unreads)))
5731         (setq start 0)
5732         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5733           (setq start (match-end 0))
5734           (setq group (if prefix
5735                           (concat prefix (substring xrefs (match-beginning 1)
5736                                                     (match-end 1)))
5737                         (substring xrefs (match-beginning 1) (match-end 1))))
5738           (setq number
5739                 (string-to-number (substring xrefs (match-beginning 2)
5740                                           (match-end 2))))
5741           (if (setq entry (gnus-gethash group xref-hashtb))
5742               (setcdr entry (cons number (cdr entry)))
5743             (gnus-sethash group (cons number nil) xref-hashtb)))))
5744     (and start xref-hashtb)))
5745
5746 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5747   "Look through all the headers and mark the Xrefs as read."
5748   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5749         name info xref-hashtb idlist method nth4)
5750     (save-excursion
5751       (set-buffer gnus-group-buffer)
5752       (when (setq xref-hashtb
5753                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5754         (mapatoms
5755          (lambda (group)
5756            (unless (string= from-newsgroup (setq name (symbol-name group)))
5757              (setq idlist (symbol-value group))
5758              ;; Dead groups are not updated.
5759              (and (prog1
5760                       (setq info (gnus-get-info name))
5761                     (when (stringp (setq nth4 (gnus-info-method info)))
5762                       (setq nth4 (gnus-server-to-method nth4))))
5763                   ;; Only do the xrefs if the group has the same
5764                   ;; select method as the group we have just read.
5765                   (or (gnus-methods-equal-p
5766                        nth4 (gnus-find-method-for-group from-newsgroup))
5767                       virtual
5768                       (equal nth4 (setq method (gnus-find-method-for-group
5769                                                 from-newsgroup)))
5770                       (and (equal (car nth4) (car method))
5771                            (equal (nth 1 nth4) (nth 1 method))))
5772                   gnus-use-cross-reference
5773                   (or (not (eq gnus-use-cross-reference t))
5774                       virtual
5775                       ;; Only do cross-references on subscribed
5776                       ;; groups, if that is what is wanted.
5777                       (<= (gnus-info-level info) gnus-level-subscribed))
5778                   (gnus-group-make-articles-read name idlist))))
5779          xref-hashtb)))))
5780
5781 (defun gnus-compute-read-articles (group articles)
5782   (let* ((entry (gnus-group-entry group))
5783          (info (nth 2 entry))
5784          (active (gnus-active group))
5785          ninfo)
5786     (when entry
5787       ;; First peel off all invalid article numbers.
5788       (when active
5789         (let ((ids articles)
5790               id first)
5791           (while (setq id (pop ids))
5792             (when (and first (> id (cdr active)))
5793               ;; We'll end up in this situation in one particular
5794               ;; obscure situation.  If you re-scan a group and get
5795               ;; a new article that is cross-posted to a different
5796               ;; group that has not been re-scanned, you might get
5797               ;; crossposted article that has a higher number than
5798               ;; Gnus believes possible.  So we re-activate this
5799               ;; group as well.  This might mean doing the
5800               ;; crossposting thingy will *increase* the number
5801               ;; of articles in some groups.  Tsk, tsk.
5802               (setq active (or (gnus-activate-group group) active)))
5803             (when (or (> id (cdr active))
5804                       (< id (car active)))
5805               (setq articles (delq id articles))))))
5806       ;; If the read list is nil, we init it.
5807       (if (and active
5808                (null (gnus-info-read info))
5809                (> (car active) 1))
5810           (setq ninfo (cons 1 (1- (car active))))
5811         (setq ninfo (gnus-info-read info)))
5812       ;; Then we add the read articles to the range.
5813       (gnus-add-to-range
5814        ninfo (setq articles (sort articles '<))))))
5815
5816 (defun gnus-group-make-articles-read (group articles)
5817   "Update the info of GROUP to say that ARTICLES are read."
5818   (let* ((num 0)
5819          (entry (gnus-group-entry group))
5820          (info (nth 2 entry))
5821          (active (gnus-active group))
5822          range)
5823     (when entry
5824       (setq range (gnus-compute-read-articles group articles))
5825       (with-current-buffer gnus-group-buffer
5826         (gnus-undo-register
5827           `(progn
5828              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5829              (gnus-info-set-read ',info ',(gnus-info-read info))
5830              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5831              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5832              (gnus-group-update-group ,group t))))
5833       ;; Add the read articles to the range.
5834       (gnus-info-set-read info range)
5835       (gnus-request-set-mark group (list (list range 'add '(read))))
5836       ;; Then we have to re-compute how many unread
5837       ;; articles there are in this group.
5838       (when active
5839         (cond
5840          ((not range)
5841           (setq num (- (1+ (cdr active)) (car active))))
5842          ((not (listp (cdr range)))
5843           (setq num (- (cdr active) (- (1+ (cdr range))
5844                                        (car range)))))
5845          (t
5846           (while range
5847             (if (numberp (car range))
5848                 (setq num (1+ num))
5849               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5850             (setq range (cdr range)))
5851           (setq num (- (cdr active) num))))
5852         ;; Update the number of unread articles.
5853         (setcar entry num)
5854         ;; Update the group buffer.
5855         (unless (gnus-ephemeral-group-p group)
5856           (gnus-group-update-group group t))))))
5857
5858 (defvar gnus-newsgroup-none-id 0)
5859
5860 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5861   (let ((cur nntp-server-buffer)
5862         (dependencies
5863          (or dependencies
5864              (with-current-buffer gnus-summary-buffer
5865                gnus-newsgroup-dependencies)))
5866         headers id end ref number
5867         (mail-parse-charset gnus-newsgroup-charset)
5868         (mail-parse-ignored-charsets
5869          (save-excursion (condition-case nil
5870                              (set-buffer gnus-summary-buffer)
5871                            (error))
5872                          gnus-newsgroup-ignored-charsets)))
5873     (save-excursion
5874       (set-buffer nntp-server-buffer)
5875       ;; Translate all TAB characters into SPACE characters.
5876       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5877       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5878       (ietf-drums-unfold-fws)
5879       (gnus-run-hooks 'gnus-parse-headers-hook)
5880       (let ((case-fold-search t)
5881             in-reply-to header p lines chars)
5882         (goto-char (point-min))
5883         ;; Search to the beginning of the next header.  Error messages
5884         ;; do not begin with 2 or 3.
5885         (while (re-search-forward "^[23][0-9]+ " nil t)
5886           (setq id nil
5887                 ref nil)
5888           ;; This implementation of this function, with nine
5889           ;; search-forwards instead of the one re-search-forward and
5890           ;; a case (which basically was the old function) is actually
5891           ;; about twice as fast, even though it looks messier.  You
5892           ;; can't have everything, I guess.  Speed and elegance
5893           ;; doesn't always go hand in hand.
5894           (setq
5895            header
5896            (vector
5897             ;; Number.
5898             (prog1
5899                 (setq number (read cur))
5900               (end-of-line)
5901               (setq p (point))
5902               (narrow-to-region (point)
5903                                 (or (and (search-forward "\n.\n" nil t)
5904                                          (- (point) 2))
5905                                     (point))))
5906             ;; Subject.
5907             (progn
5908               (goto-char p)
5909               (if (search-forward "\nsubject:" nil t)
5910                   (funcall gnus-decode-encoded-word-function
5911                            (nnheader-header-value))
5912                 "(none)"))
5913             ;; From.
5914             (progn
5915               (goto-char p)
5916               (if (search-forward "\nfrom:" nil t)
5917                   (funcall gnus-decode-encoded-word-function
5918                            (nnheader-header-value))
5919                 "(nobody)"))
5920             ;; Date.
5921             (progn
5922               (goto-char p)
5923               (if (search-forward "\ndate:" nil t)
5924                   (nnheader-header-value) ""))
5925             ;; Message-ID.
5926             (progn
5927               (goto-char p)
5928               (setq id (if (re-search-forward
5929                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5930                            ;; We do it this way to make sure the Message-ID
5931                            ;; is (somewhat) syntactically valid.
5932                            (buffer-substring (match-beginning 1)
5933                                              (match-end 1))
5934                          ;; If there was no message-id, we just fake one
5935                          ;; to make subsequent routines simpler.
5936                          (nnheader-generate-fake-message-id number))))
5937             ;; References.
5938             (progn
5939               (goto-char p)
5940               (if (search-forward "\nreferences:" nil t)
5941                   (progn
5942                     (setq end (point))
5943                     (prog1
5944                         (nnheader-header-value)
5945                       (setq ref
5946                             (buffer-substring
5947                              (progn
5948                                (end-of-line)
5949                                (search-backward ">" end t)
5950                                (1+ (point)))
5951                              (progn
5952                                (search-backward "<" end t)
5953                                (point))))))
5954                 ;; Get the references from the in-reply-to header if there
5955                 ;; were no references and the in-reply-to header looks
5956                 ;; promising.
5957                 (if (and (search-forward "\nin-reply-to:" nil t)
5958                          (setq in-reply-to (nnheader-header-value))
5959                          (string-match "<[^>]+>" in-reply-to))
5960                     (let (ref2)
5961                       (setq ref (substring in-reply-to (match-beginning 0)
5962                                            (match-end 0)))
5963                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5964                         (setq ref2 (substring in-reply-to (match-beginning 0)
5965                                               (match-end 0)))
5966                         (when (> (length ref2) (length ref))
5967                           (setq ref ref2)))
5968                       ref)
5969                   (setq ref nil))))
5970             ;; Chars.
5971             (progn
5972               (goto-char p)
5973               (if (search-forward "\nchars: " nil t)
5974                   (if (numberp (setq chars (ignore-errors (read cur))))
5975                       chars -1)
5976                 -1))
5977             ;; Lines.
5978             (progn
5979               (goto-char p)
5980               (if (search-forward "\nlines: " nil t)
5981                   (if (numberp (setq lines (ignore-errors (read cur))))
5982                       lines -1)
5983                 -1))
5984             ;; Xref.
5985             (progn
5986               (goto-char p)
5987               (and (search-forward "\nxref:" nil t)
5988                    (nnheader-header-value)))
5989             ;; Extra.
5990             (when gnus-extra-headers
5991               (let ((extra gnus-extra-headers)
5992                     out)
5993                 (while extra
5994                   (goto-char p)
5995                   (when (search-forward
5996                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5997                     (push (cons (car extra) (nnheader-header-value))
5998                           out))
5999                   (pop extra))
6000                 out))))
6001           (when (equal id ref)
6002             (setq ref nil))
6003
6004           (when gnus-alter-header-function
6005             (funcall gnus-alter-header-function header)
6006             (setq id (mail-header-id header)
6007                   ref (gnus-parent-id (mail-header-references header))))
6008
6009           (when (setq header
6010                       (gnus-dependencies-add-header
6011                        header dependencies force-new))
6012             (push header headers))
6013           (goto-char (point-max))
6014           (widen))
6015         (nreverse headers)))))
6016
6017 ;; Goes through the xover lines and returns a list of vectors
6018 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6019                                                   force-new dependencies
6020                                                   group also-fetch-heads)
6021   "Parse the news overview data in the server buffer.
6022 Return a list of headers that match SEQUENCE (see
6023 `nntp-retrieve-headers')."
6024   ;; Get the Xref when the users reads the articles since most/some
6025   ;; NNTP servers do not include Xrefs when using XOVER.
6026   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6027   (let ((mail-parse-charset gnus-newsgroup-charset)
6028         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6029         (cur nntp-server-buffer)
6030         (dependencies (or dependencies gnus-newsgroup-dependencies))
6031         (allp (cond
6032                ((eq gnus-read-all-available-headers t)
6033                 t)
6034                ((stringp gnus-read-all-available-headers)
6035                 (string-match gnus-read-all-available-headers group))
6036                (t
6037                 nil)))
6038         number headers header)
6039     (save-excursion
6040       (set-buffer nntp-server-buffer)
6041       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6042       ;; Allow the user to mangle the headers before parsing them.
6043       (gnus-run-hooks 'gnus-parse-headers-hook)
6044       (goto-char (point-min))
6045       (gnus-parse-without-error
6046         (while (and (or sequence allp)
6047                     (not (eobp)))
6048           (setq number (read cur))
6049           (when (not allp)
6050             (while (and sequence
6051                         (< (car sequence) number))
6052               (setq sequence (cdr sequence))))
6053           (when (and (or allp
6054                          (and sequence
6055                               (eq number (car sequence))))
6056                      (progn
6057                        (setq sequence (cdr sequence))
6058                        (setq header (inline
6059                                       (gnus-nov-parse-line
6060                                        number dependencies force-new)))))
6061             (push header headers))
6062           (forward-line 1)))
6063       ;; A common bug in inn is that if you have posted an article and
6064       ;; then retrieves the active file, it will answer correctly --
6065       ;; the new article is included.  However, a NOV entry for the
6066       ;; article may not have been generated yet, so this may fail.
6067       ;; We work around this problem by retrieving the last few
6068       ;; headers using HEAD.
6069       (if (or (not also-fetch-heads)
6070               (not sequence))
6071           ;; We (probably) got all the headers.
6072           (nreverse headers)
6073         (let ((gnus-nov-is-evil t))
6074           (nconc
6075            (nreverse headers)
6076            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6077              (gnus-get-newsgroup-headers))))))))
6078
6079 (defun gnus-article-get-xrefs ()
6080   "Fill in the Xref value in `gnus-current-headers', if necessary.
6081 This is meant to be called in `gnus-article-internal-prepare-hook'."
6082   (let ((headers (with-current-buffer gnus-summary-buffer
6083                    gnus-current-headers)))
6084     (or (not gnus-use-cross-reference)
6085         (not headers)
6086         (and (mail-header-xref headers)
6087              (not (string= (mail-header-xref headers) "")))
6088         (let ((case-fold-search t)
6089               xref)
6090           (save-restriction
6091             (nnheader-narrow-to-headers)
6092             (goto-char (point-min))
6093             (when (or (and (not (eobp))
6094                            (eq (downcase (char-after)) ?x)
6095                            (looking-at "Xref:"))
6096                       (search-forward "\nXref:" nil t))
6097               (goto-char (1+ (match-end 0)))
6098               (setq xref (buffer-substring (point) (point-at-eol)))
6099               (mail-header-set-xref headers xref)))))))
6100
6101 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6102   "Find article ID and insert the summary line for that article.
6103 OLD-HEADER can either be a header or a line number to insert
6104 the subject line on."
6105   (let* ((line (and (numberp old-header) old-header))
6106          (old-header (and (vectorp old-header) old-header))
6107          (header (cond ((and old-header use-old-header)
6108                         old-header)
6109                        ((and (numberp id)
6110                              (gnus-number-to-header id))
6111                         (gnus-number-to-header id))
6112                        (t
6113                         (gnus-read-header id))))
6114          (number (and (numberp id) id))
6115          d)
6116     (when header
6117       ;; Rebuild the thread that this article is part of and go to the
6118       ;; article we have fetched.
6119       (when (and (not gnus-show-threads)
6120                  old-header)
6121         (when (and number
6122                    (setq d (gnus-data-find (mail-header-number old-header))))
6123           (goto-char (gnus-data-pos d))
6124           (gnus-data-remove
6125            number
6126            (- (point-at-bol)
6127               (prog1
6128                   (1+ (point-at-eol))
6129                 (gnus-delete-line))))))
6130       ;; Remove list identifiers from subject.
6131       (when gnus-list-identifiers
6132         (let ((gnus-newsgroup-headers (list header)))
6133           (gnus-summary-remove-list-identifiers)))
6134       (when old-header
6135         (mail-header-set-number header (mail-header-number old-header)))
6136       (setq gnus-newsgroup-sparse
6137             (delq (setq number (mail-header-number header))
6138                   gnus-newsgroup-sparse))
6139       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6140       (push number gnus-newsgroup-limit)
6141       (gnus-rebuild-thread (mail-header-id header) line)
6142       (gnus-summary-goto-subject number nil t))
6143     (when (and (numberp number)
6144                (> number 0))
6145       ;; We have to update the boundaries even if we can't fetch the
6146       ;; article if ID is a number -- so that the next `P' or `N'
6147       ;; command will fetch the previous (or next) article even
6148       ;; if the one we tried to fetch this time has been canceled.
6149       (when (> number gnus-newsgroup-end)
6150         (setq gnus-newsgroup-end number))
6151       (when (< number gnus-newsgroup-begin)
6152         (setq gnus-newsgroup-begin number))
6153       (setq gnus-newsgroup-unselected
6154             (delq number gnus-newsgroup-unselected)))
6155     ;; Report back a success?
6156     (and header (mail-header-number header))))
6157
6158 ;;; Process/prefix in the summary buffer
6159
6160 (defun gnus-summary-work-articles (n)
6161   "Return a list of articles to be worked upon.
6162 The prefix argument, the list of process marked articles, and the
6163 current article will be taken into consideration."
6164   (save-excursion
6165     (set-buffer gnus-summary-buffer)
6166     (cond
6167      (n
6168       ;; A numerical prefix has been given.
6169       (setq n (prefix-numeric-value n))
6170       (let ((backward (< n 0))
6171             (n (abs (prefix-numeric-value n)))
6172             articles article)
6173         (save-excursion
6174           (while
6175               (and (> n 0)
6176                    (push (setq article (gnus-summary-article-number))
6177                          articles)
6178                    (if backward
6179                        (gnus-summary-find-prev nil article)
6180                      (gnus-summary-find-next nil article)))
6181             (decf n)))
6182         (nreverse articles)))
6183      ((and (gnus-region-active-p) (mark))
6184       (message "region active")
6185       ;; Work on the region between point and mark.
6186       (let ((max (max (point) (mark)))
6187             articles article)
6188         (save-excursion
6189           (goto-char (min (min (point) (mark))))
6190           (while
6191               (and
6192                (push (setq article (gnus-summary-article-number)) articles)
6193                (gnus-summary-find-next nil article)
6194                (< (point) max)))
6195           (nreverse articles))))
6196      (gnus-newsgroup-processable
6197       ;; There are process-marked articles present.
6198       ;; Save current state.
6199       (gnus-summary-save-process-mark)
6200       ;; Return the list.
6201       (reverse gnus-newsgroup-processable))
6202      (t
6203       ;; Just return the current article.
6204       (list (gnus-summary-article-number))))))
6205
6206 (defmacro gnus-summary-iterate (arg &rest forms)
6207   "Iterate over the process/prefixed articles and do FORMS.
6208 ARG is the interactive prefix given to the command.  FORMS will be
6209 executed with point over the summary line of the articles."
6210   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6211     `(let ((,articles (gnus-summary-work-articles ,arg)))
6212        (while ,articles
6213          (gnus-summary-goto-subject (car ,articles))
6214          ,@forms
6215          (pop ,articles)))))
6216
6217 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6218 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6219
6220 (defun gnus-summary-save-process-mark ()
6221   "Push the current set of process marked articles on the stack."
6222   (interactive)
6223   (push (copy-sequence gnus-newsgroup-processable)
6224         gnus-newsgroup-process-stack))
6225
6226 (defun gnus-summary-kill-process-mark ()
6227   "Push the current set of process marked articles on the stack and unmark."
6228   (interactive)
6229   (gnus-summary-save-process-mark)
6230   (gnus-summary-unmark-all-processable))
6231
6232 (defun gnus-summary-yank-process-mark ()
6233   "Pop the last process mark state off the stack and restore it."
6234   (interactive)
6235   (unless gnus-newsgroup-process-stack
6236     (error "Empty mark stack"))
6237   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6238
6239 (defun gnus-summary-process-mark-set (set)
6240   "Make SET into the current process marked articles."
6241   (gnus-summary-unmark-all-processable)
6242   (mapc 'gnus-summary-set-process-mark set))
6243
6244 ;;; Searching and stuff
6245
6246 (defun gnus-summary-search-group (&optional backward use-level)
6247   "Search for next unread newsgroup.
6248 If optional argument BACKWARD is non-nil, search backward instead."
6249   (save-excursion
6250     (set-buffer gnus-group-buffer)
6251     (when (gnus-group-search-forward
6252            backward nil (if use-level (gnus-group-group-level) nil))
6253       (gnus-group-group-name))))
6254
6255 (defun gnus-summary-best-group (&optional exclude-group)
6256   "Find the name of the best unread group.
6257 If EXCLUDE-GROUP, do not go to this group."
6258   (with-current-buffer gnus-group-buffer
6259     (save-excursion
6260       (gnus-group-best-unread-group exclude-group))))
6261
6262 (defun gnus-summary-find-next (&optional unread article backward)
6263   (if backward
6264       (gnus-summary-find-prev unread article)
6265     (let* ((dummy (gnus-summary-article-intangible-p))
6266            (article (or article (gnus-summary-article-number)))
6267            (data (gnus-data-find-list article))
6268            result)
6269       (when (and (not dummy)
6270                  (or (not gnus-summary-check-current)
6271                      (not unread)
6272                      (not (gnus-data-unread-p (car data)))))
6273         (setq data (cdr data)))
6274       (when (setq result
6275                   (if unread
6276                       (progn
6277                         (while data
6278                           (unless (memq (gnus-data-number (car data))
6279                                         (cond
6280                                          ((eq gnus-auto-goto-ignores
6281                                               'always-undownloaded)
6282                                           gnus-newsgroup-undownloaded)
6283                                          (gnus-plugged
6284                                           nil)
6285                                          ((eq gnus-auto-goto-ignores
6286                                               'unfetched)
6287                                           gnus-newsgroup-unfetched)
6288                                          ((eq gnus-auto-goto-ignores
6289                                               'undownloaded)
6290                                           gnus-newsgroup-undownloaded)))
6291                             (when (gnus-data-unread-p (car data))
6292                               (setq result (car data)
6293                                     data nil)))
6294                           (setq data (cdr data)))
6295                         result)
6296                     (car data)))
6297         (goto-char (gnus-data-pos result))
6298         (gnus-data-number result)))))
6299
6300 (defun gnus-summary-find-prev (&optional unread article)
6301   (let* ((eobp (eobp))
6302          (article (or article (gnus-summary-article-number)))
6303          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6304          result)
6305     (when (and (not eobp)
6306                (or (not gnus-summary-check-current)
6307                    (not unread)
6308                    (not (gnus-data-unread-p (car data)))))
6309       (setq data (cdr data)))
6310     (when (setq result
6311                 (if unread
6312                     (progn
6313                       (while data
6314                         (unless (memq (gnus-data-number (car data))
6315                                       (cond
6316                                        ((eq gnus-auto-goto-ignores
6317                                             'always-undownloaded)
6318                                         gnus-newsgroup-undownloaded)
6319                                        (gnus-plugged
6320                                         nil)
6321                                        ((eq gnus-auto-goto-ignores
6322                                             'unfetched)
6323                                         gnus-newsgroup-unfetched)
6324                                        ((eq gnus-auto-goto-ignores
6325                                             'undownloaded)
6326                                         gnus-newsgroup-undownloaded)))
6327                           (when (gnus-data-unread-p (car data))
6328                             (setq result (car data)
6329                                   data nil)))
6330                         (setq data (cdr data)))
6331                       result)
6332                   (car data)))
6333       (goto-char (gnus-data-pos result))
6334       (gnus-data-number result))))
6335
6336 (defun gnus-summary-find-subject (subject &optional unread backward article)
6337   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6338          (article (or article (gnus-summary-article-number)))
6339          (articles (gnus-data-list backward))
6340          (arts (gnus-data-find-list article articles))
6341          result)
6342     (when (or (not gnus-summary-check-current)
6343               (not unread)
6344               (not (gnus-data-unread-p (car arts))))
6345       (setq arts (cdr arts)))
6346     (while arts
6347       (and (or (not unread)
6348                (gnus-data-unread-p (car arts)))
6349            (vectorp (gnus-data-header (car arts)))
6350            (gnus-subject-equal
6351             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6352            (setq result (car arts)
6353                  arts nil))
6354       (setq arts (cdr arts)))
6355     (and result
6356          (goto-char (gnus-data-pos result))
6357          (gnus-data-number result))))
6358
6359 (defun gnus-summary-search-forward (&optional unread subject backward)
6360   "Search forward for an article.
6361 If UNREAD, look for unread articles.  If SUBJECT, look for
6362 articles with that subject.  If BACKWARD, search backward instead."
6363   (cond (subject (gnus-summary-find-subject subject unread backward))
6364         (backward (gnus-summary-find-prev unread))
6365         (t (gnus-summary-find-next unread))))
6366
6367 (defun gnus-recenter (&optional n)
6368   "Center point in window and redisplay frame.
6369 Also do horizontal recentering."
6370   (interactive "P")
6371   (when (and gnus-auto-center-summary
6372              (not (eq gnus-auto-center-summary 'vertical)))
6373     (gnus-horizontal-recenter))
6374   (recenter n))
6375
6376 (defun gnus-summary-recenter ()
6377   "Center point in the summary window.
6378 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6379 displayed, no centering will be performed."
6380   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6381   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6382   (interactive)
6383   ;; The user has to want it.
6384   (when gnus-auto-center-summary
6385     (let* ((top (cond ((< (window-height) 4) 0)
6386                       ((< (window-height) 7) 1)
6387                       (t (if (numberp gnus-auto-center-summary)
6388                              gnus-auto-center-summary
6389                            (/ (1- (window-height)) 2)))))
6390            (height (1- (window-height)))
6391            (bottom (save-excursion (goto-char (point-max))
6392                                    (forward-line (- height))
6393                                    (point)))
6394            (window (get-buffer-window (current-buffer))))
6395       (when (get-buffer-window gnus-article-buffer)
6396         ;; Only do recentering when the article buffer is displayed,
6397         ;; Set the window start to either `bottom', which is the biggest
6398         ;; possible valid number, or the second line from the top,
6399         ;; whichever is the least.
6400         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6401           (if (> bottom top-pos)
6402               ;; Keep the second line from the top visible
6403               (set-window-start window top-pos t)
6404             ;; Try to keep the bottom line visible; if it's partially
6405             ;; obscured, either scroll one more line to make it fully
6406             ;; visible, or revert to using TOP-POS.
6407             (save-excursion
6408               (goto-char (point-max))
6409               (forward-line -1)
6410               (let ((last-line-start (point)))
6411                 (goto-char bottom)
6412                 (set-window-start window (point) t)
6413                 (when (not (pos-visible-in-window-p last-line-start window))
6414                   (forward-line 1)
6415                   (set-window-start window (min (point) top-pos) t)))))))
6416       ;; Do horizontal recentering while we're at it.
6417       (when (and (get-buffer-window (current-buffer) t)
6418                  (not (eq gnus-auto-center-summary 'vertical)))
6419         (let ((selected (selected-window)))
6420           (select-window (get-buffer-window (current-buffer) t))
6421           (gnus-summary-position-point)
6422           (gnus-horizontal-recenter)
6423           (select-window selected))))))
6424
6425 (defun gnus-summary-jump-to-group (newsgroup)
6426   "Move point to NEWSGROUP in group mode buffer."
6427   ;; Keep update point of group mode buffer if visible.
6428   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6429       (save-window-excursion
6430         ;; Take care of tree window mode.
6431         (when (get-buffer-window gnus-group-buffer)
6432           (pop-to-buffer gnus-group-buffer))
6433         (gnus-group-jump-to-group newsgroup))
6434     (save-excursion
6435       ;; Take care of tree window mode.
6436       (if (get-buffer-window gnus-group-buffer)
6437           (pop-to-buffer gnus-group-buffer)
6438         (set-buffer gnus-group-buffer))
6439       (gnus-group-jump-to-group newsgroup))))
6440
6441 ;; This function returns a list of article numbers based on the
6442 ;; difference between the ranges of read articles in this group and
6443 ;; the range of active articles.
6444 (defun gnus-list-of-unread-articles (group)
6445   (let* ((read (gnus-info-read (gnus-get-info group)))
6446          (active (or (gnus-active group) (gnus-activate-group group)))
6447          (last (or (cdr active)
6448                    (error "Group %s couldn't be activated " group)))
6449          first nlast unread)
6450     ;; If none are read, then all are unread.
6451     (if (not read)
6452         (setq first (car active))
6453       ;; If the range of read articles is a single range, then the
6454       ;; first unread article is the article after the last read
6455       ;; article.  Sounds logical, doesn't it?
6456       (if (and (not (listp (cdr read)))
6457                (or (< (car read) (car active))
6458                    (progn (setq read (list read))
6459                           nil)))
6460           (setq first (max (car active) (1+ (cdr read))))
6461         ;; `read' is a list of ranges.
6462         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6463                                   (caar read)))
6464                   1)
6465           (setq first (car active)))
6466         (while read
6467           (when first
6468             (while (< first nlast)
6469               (setq unread (cons first unread)
6470                     first (1+ first))))
6471           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6472           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6473           (setq read (cdr read)))))
6474     ;; And add the last unread articles.
6475     (while (<= first last)
6476       (setq unread (cons first unread)
6477             first (1+ first)))
6478     ;; Return the list of unread articles.
6479     (delq 0 (nreverse unread))))
6480
6481 (defun gnus-list-of-read-articles (group)
6482   "Return a list of unread, unticked and non-dormant articles."
6483   (let* ((info (gnus-get-info group))
6484          (marked (gnus-info-marks info))
6485          (active (gnus-active group)))
6486     (and info active
6487          (gnus-list-range-difference
6488           (gnus-list-range-difference
6489            (gnus-sorted-complement
6490             (gnus-uncompress-range active)
6491             (gnus-list-of-unread-articles group))
6492            (cdr (assq 'dormant marked)))
6493           (cdr (assq 'tick marked))))))
6494
6495 ;; This function returns a sequence of article numbers based on the
6496 ;; difference between the ranges of read articles in this group and
6497 ;; the range of active articles.
6498 (defun gnus-sequence-of-unread-articles (group)
6499   (let* ((read (gnus-info-read (gnus-get-info group)))
6500          (active (or (gnus-active group) (gnus-activate-group group)))
6501          (last (cdr active))
6502          first nlast unread)
6503     ;; If none are read, then all are unread.
6504     (if (not read)
6505         (setq first (car active))
6506       ;; If the range of read articles is a single range, then the
6507       ;; first unread article is the article after the last read
6508       ;; article.  Sounds logical, doesn't it?
6509       (if (and (not (listp (cdr read)))
6510                (or (< (car read) (car active))
6511                    (progn (setq read (list read))
6512                           nil)))
6513           (setq first (max (car active) (1+ (cdr read))))
6514         ;; `read' is a list of ranges.
6515         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6516                                   (caar read)))
6517                   1)
6518           (setq first (car active)))
6519         (while read
6520           (when first
6521             (push (cons first nlast) unread))
6522           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6523           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6524           (setq read (cdr read)))))
6525     ;; And add the last unread articles.
6526     (cond ((< first last)
6527            (push (cons first last) unread))
6528           ((= first last)
6529            (push first unread)))
6530     ;; Return the sequence of unread articles.
6531     (delq 0 (nreverse unread))))
6532
6533 ;; Various summary commands
6534
6535 (defun gnus-summary-select-article-buffer ()
6536   "Reconfigure windows to show article buffer."
6537   (interactive)
6538   (if (not (gnus-buffer-live-p gnus-article-buffer))
6539       (error "There is no article buffer for this summary buffer")
6540     (gnus-configure-windows 'article)
6541     (select-window (get-buffer-window gnus-article-buffer))))
6542
6543 (defun gnus-summary-universal-argument (arg)
6544   "Perform any operation on all articles that are process/prefixed."
6545   (interactive "P")
6546   (let ((articles (gnus-summary-work-articles arg))
6547         func article)
6548     (if (eq
6549          (setq
6550           func
6551           (key-binding
6552            (read-key-sequence
6553             (substitute-command-keys
6554              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6555          'undefined)
6556         (gnus-error 1 "Undefined key")
6557       (save-excursion
6558         (while articles
6559           (gnus-summary-goto-subject (setq article (pop articles)))
6560           (let (gnus-newsgroup-processable)
6561             (command-execute func))
6562           (gnus-summary-remove-process-mark article)))))
6563   (gnus-summary-position-point))
6564
6565 (defun gnus-summary-toggle-truncation (&optional arg)
6566   "Toggle truncation of summary lines.
6567 With ARG, turn line truncation on if ARG is positive."
6568   (interactive "P")
6569   (setq truncate-lines
6570         (if (null arg) (not truncate-lines)
6571           (> (prefix-numeric-value arg) 0)))
6572   (redraw-display))
6573
6574 (defun gnus-summary-find-for-reselect ()
6575   "Return the number of an article to stay on across a reselect.
6576 The current article is considered, then following articles, then previous
6577 articles.  An article is sought which is not cancelled and isn't a temporary
6578 insertion from another group.  If there's no such then return a dummy 0."
6579   (let (found)
6580     (dolist (rev '(nil t))
6581       (unless found      ; don't demand the reverse list if we don't need it
6582         (let ((data (gnus-data-find-list
6583                      (gnus-summary-article-number) (gnus-data-list rev))))
6584           (while (and data (not found))
6585             (if (and (< 0 (gnus-data-number (car data)))
6586                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6587                 (setq found (gnus-data-number (car data))))
6588             (setq data (cdr data))))))
6589     (or found 0)))
6590
6591 (defun gnus-summary-reselect-current-group (&optional all rescan)
6592   "Exit and then reselect the current newsgroup.
6593 The prefix argument ALL means to select all articles."
6594   (interactive "P")
6595   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6596     (error "Ephemeral groups can't be reselected"))
6597   (let ((current-subject (gnus-summary-find-for-reselect))
6598         (group gnus-newsgroup-name))
6599     (setq gnus-newsgroup-begin nil)
6600     (gnus-summary-exit nil 'leave-hidden)
6601     ;; We have to adjust the point of group mode buffer because
6602     ;; point was moved to the next unread newsgroup by exiting.
6603     (gnus-summary-jump-to-group group)
6604     (when rescan
6605       (save-excursion
6606         (gnus-group-get-new-news-this-group 1)))
6607     (gnus-group-read-group all t)
6608     (gnus-summary-goto-subject current-subject nil t)))
6609
6610 (defun gnus-summary-rescan-group (&optional all)
6611   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6612   (interactive "P")
6613   (gnus-summary-reselect-current-group all t))
6614
6615 (defun gnus-summary-update-info (&optional non-destructive)
6616   (save-excursion
6617     (let ((group gnus-newsgroup-name))
6618       (when group
6619         (when gnus-newsgroup-kill-headers
6620           (setq gnus-newsgroup-killed
6621                 (gnus-compress-sequence
6622                  (gnus-sorted-union
6623                   (gnus-list-range-intersection
6624                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6625                   gnus-newsgroup-unreads)
6626                  t)))
6627         (unless (listp (cdr gnus-newsgroup-killed))
6628           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6629         (let ((headers gnus-newsgroup-headers))
6630           ;; Set the new ranges of read articles.
6631           (with-current-buffer gnus-group-buffer
6632             (gnus-undo-force-boundary))
6633           (gnus-update-read-articles
6634            group (gnus-sorted-union
6635                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6636           ;; Set the current article marks.
6637           (let ((gnus-newsgroup-scored
6638                  (if (and (not gnus-save-score)
6639                           (not non-destructive))
6640                      nil
6641                    gnus-newsgroup-scored)))
6642             (save-excursion
6643               (gnus-update-marks)))
6644           ;; Do the cross-ref thing.
6645           (when gnus-use-cross-reference
6646             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6647           ;; Do not switch windows but change the buffer to work.
6648           (set-buffer gnus-group-buffer)
6649           (unless (gnus-ephemeral-group-p group)
6650             (gnus-group-update-group group)))))))
6651
6652 (defun gnus-summary-save-newsrc (&optional force)
6653   "Save the current number of read/marked articles in the dribble buffer.
6654 The dribble buffer will then be saved.
6655 If FORCE (the prefix), also save the .newsrc file(s)."
6656   (interactive "P")
6657   (gnus-summary-update-info t)
6658   (if force
6659       (gnus-save-newsrc-file)
6660     (gnus-dribble-save)))
6661
6662 (defun gnus-summary-exit (&optional temporary leave-hidden)
6663   "Exit reading current newsgroup, and then return to group selection mode.
6664 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6665   (interactive)
6666   (gnus-set-global-variables)
6667   (when (gnus-buffer-live-p gnus-article-buffer)
6668     (save-excursion
6669       (set-buffer gnus-article-buffer)
6670       (mm-destroy-parts gnus-article-mime-handles)
6671       ;; Set it to nil for safety reason.
6672       (setq gnus-article-mime-handle-alist nil)
6673       (setq gnus-article-mime-handles nil)))
6674   (gnus-kill-save-kill-buffer)
6675   (gnus-async-halt-prefetch)
6676   (let* ((group gnus-newsgroup-name)
6677          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6678          (gnus-group-is-exiting-p t)
6679          (mode major-mode)
6680          (group-point nil)
6681          (buf (current-buffer)))
6682     (unless quit-config
6683       ;; Do adaptive scoring, and possibly save score files.
6684       (when gnus-newsgroup-adaptive
6685         (gnus-score-adaptive))
6686       (when gnus-use-scoring
6687         (gnus-score-save)))
6688     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6689     ;; If we have several article buffers, we kill them at exit.
6690     (unless gnus-single-article-buffer
6691       (gnus-kill-buffer gnus-original-article-buffer)
6692       (setq gnus-article-current nil))
6693     (when gnus-use-cache
6694       (gnus-cache-possibly-remove-articles)
6695       (gnus-cache-save-buffers))
6696     (gnus-async-prefetch-remove-group group)
6697     (when gnus-suppress-duplicates
6698       (gnus-dup-enter-articles))
6699     (when gnus-use-trees
6700       (gnus-tree-close group))
6701     (when gnus-use-cache
6702       (gnus-cache-write-active))
6703     ;; Remove entries for this group.
6704     (nnmail-purge-split-history (gnus-group-real-name group))
6705     ;; Make all changes in this group permanent.
6706     (unless quit-config
6707       (gnus-run-hooks 'gnus-exit-group-hook)
6708       (gnus-summary-update-info))
6709     (gnus-close-group group)
6710     ;; Make sure where we were, and go to next newsgroup.
6711     (set-buffer gnus-group-buffer)
6712     (unless quit-config
6713       (gnus-group-jump-to-group group))
6714     (gnus-run-hooks 'gnus-summary-exit-hook)
6715     (unless (or quit-config
6716                 ;; If this group has disappeared from the summary
6717                 ;; buffer, don't skip forwards.
6718                 (not (string= group (gnus-group-group-name))))
6719       (gnus-group-next-unread-group 1))
6720     (setq group-point (point))
6721     (if temporary
6722         nil                             ;Nothing to do.
6723       ;; If we have several article buffers, we kill them at exit.
6724       (unless gnus-single-article-buffer
6725         (gnus-kill-buffer gnus-article-buffer)
6726         (gnus-kill-buffer gnus-original-article-buffer)
6727         (setq gnus-article-current nil))
6728       (set-buffer buf)
6729       (if (not gnus-kill-summary-on-exit)
6730           (progn
6731             (gnus-deaden-summary)
6732             (setq mode nil))
6733         ;; We set all buffer-local variables to nil.  It is unclear why
6734         ;; this is needed, but if we don't, buffer-local variables are
6735         ;; not garbage-collected, it seems.  This would the lead to en
6736         ;; ever-growing Emacs.
6737         (gnus-summary-clear-local-variables)
6738         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6739           (gnus-summary-clear-local-variables))
6740         (when (get-buffer gnus-article-buffer)
6741           (bury-buffer gnus-article-buffer))
6742         ;; Return to group mode buffer.
6743         (when (eq mode 'gnus-summary-mode)
6744           (gnus-kill-buffer buf)))
6745       (setq gnus-current-select-method gnus-select-method)
6746       (set-buffer gnus-group-buffer)
6747       (if quit-config
6748           (gnus-handle-ephemeral-exit quit-config)
6749         (goto-char group-point)
6750         ;; If gnus-group-buffer is already displayed, make sure we also move
6751         ;; the cursor in the window that displays it.
6752         (let ((win (get-buffer-window (current-buffer) 0)))
6753           (if win (set-window-point win (point))))
6754         (unless leave-hidden
6755           (gnus-configure-windows 'group 'force)))
6756       ;; Clear the current group name.
6757       (unless quit-config
6758         (setq gnus-newsgroup-name nil)))))
6759
6760 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6761 (defun gnus-summary-exit-no-update (&optional no-questions)
6762   "Quit reading current newsgroup without updating read article info."
6763   (interactive)
6764   (let* ((group gnus-newsgroup-name)
6765          (gnus-group-is-exiting-p t)
6766          (gnus-group-is-exiting-without-update-p t)
6767          (quit-config (gnus-group-quit-config group)))
6768     (when (or no-questions
6769               gnus-expert-user
6770               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6771       (gnus-async-halt-prefetch)
6772       (run-hooks 'gnus-summary-prepare-exit-hook)
6773       (when (gnus-buffer-live-p gnus-article-buffer)
6774         (save-excursion
6775           (set-buffer gnus-article-buffer)
6776           (mm-destroy-parts gnus-article-mime-handles)
6777           ;; Set it to nil for safety reason.
6778           (setq gnus-article-mime-handle-alist nil)
6779           (setq gnus-article-mime-handles nil)))
6780       ;; If we have several article buffers, we kill them at exit.
6781       (unless gnus-single-article-buffer
6782         (gnus-kill-buffer gnus-article-buffer)
6783         (gnus-kill-buffer gnus-original-article-buffer)
6784         (setq gnus-article-current nil))
6785       (if (not gnus-kill-summary-on-exit)
6786           (gnus-deaden-summary)
6787         (gnus-close-group group)
6788         (gnus-summary-clear-local-variables)
6789         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6790           (gnus-summary-clear-local-variables))
6791         (gnus-kill-buffer gnus-summary-buffer))
6792       (unless gnus-single-article-buffer
6793         (setq gnus-article-current nil))
6794       (when gnus-use-trees
6795         (gnus-tree-close group))
6796       (gnus-async-prefetch-remove-group group)
6797       (when (get-buffer gnus-article-buffer)
6798         (bury-buffer gnus-article-buffer))
6799       ;; Return to the group buffer.
6800       (gnus-configure-windows 'group 'force)
6801       ;; Clear the current group name.
6802       (setq gnus-newsgroup-name nil)
6803       (unless (gnus-ephemeral-group-p group)
6804         (gnus-group-update-group group))
6805       (when (equal (gnus-group-group-name) group)
6806         (gnus-group-next-unread-group 1))
6807       (when quit-config
6808         (gnus-handle-ephemeral-exit quit-config)))))
6809
6810 (defun gnus-handle-ephemeral-exit (quit-config)
6811   "Handle movement when leaving an ephemeral group.
6812 The state which existed when entering the ephemeral is reset."
6813   (if (not (buffer-name (car quit-config)))
6814       (gnus-configure-windows 'group 'force)
6815     (set-buffer (car quit-config))
6816     (cond ((eq major-mode 'gnus-summary-mode)
6817            (gnus-set-global-variables))
6818           ((eq major-mode 'gnus-article-mode)
6819            (save-excursion
6820              ;; The `gnus-summary-buffer' variable may point
6821              ;; to the old summary buffer when using a single
6822              ;; article buffer.
6823              (unless (gnus-buffer-live-p gnus-summary-buffer)
6824                (set-buffer gnus-group-buffer))
6825              (set-buffer gnus-summary-buffer)
6826              (gnus-set-global-variables))))
6827     (if (or (eq (cdr quit-config) 'article)
6828             (eq (cdr quit-config) 'pick))
6829         (progn
6830           ;; The current article may be from the ephemeral group
6831           ;; thus it is best that we reload this article
6832           ;;
6833           ;; If we're exiting from a large digest, this can be
6834           ;; extremely slow.  So, it's better not to reload it. -- jh.
6835           ;;(gnus-summary-show-article)
6836           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6837               (gnus-configure-windows 'pick 'force)
6838             (gnus-configure-windows (cdr quit-config) 'force)))
6839       (gnus-configure-windows (cdr quit-config) 'force))
6840     (when (eq major-mode 'gnus-summary-mode)
6841       (gnus-summary-next-subject 1 nil t)
6842       (gnus-summary-recenter)
6843       (gnus-summary-position-point))))
6844
6845 ;;; Dead summaries.
6846
6847 (defvar gnus-dead-summary-mode-map nil)
6848
6849 (unless gnus-dead-summary-mode-map
6850   (setq gnus-dead-summary-mode-map (make-keymap))
6851   (suppress-keymap gnus-dead-summary-mode-map)
6852   (substitute-key-definition
6853    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6854   (dolist (key '("\C-d" "\r" "\177" [delete]))
6855     (define-key gnus-dead-summary-mode-map
6856       key 'gnus-summary-wake-up-the-dead))
6857   (dolist (key '("q" "Q"))
6858     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6859
6860 (defvar gnus-dead-summary-mode nil
6861   "Minor mode for Gnus summary buffers.")
6862
6863 (defun gnus-dead-summary-mode (&optional arg)
6864   "Minor mode for Gnus summary buffers."
6865   (interactive "P")
6866   (when (eq major-mode 'gnus-summary-mode)
6867     (make-local-variable 'gnus-dead-summary-mode)
6868     (setq gnus-dead-summary-mode
6869           (if (null arg) (not gnus-dead-summary-mode)
6870             (> (prefix-numeric-value arg) 0)))
6871     (when gnus-dead-summary-mode
6872       (add-minor-mode
6873        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6874
6875 (defun gnus-deaden-summary ()
6876   "Make the current summary buffer into a dead summary buffer."
6877   ;; Kill any previous dead summary buffer.
6878   (when (and gnus-dead-summary
6879              (buffer-name gnus-dead-summary))
6880     (with-current-buffer gnus-dead-summary
6881       (when gnus-dead-summary-mode
6882         (kill-buffer (current-buffer)))))
6883   ;; Make this the current dead summary.
6884   (setq gnus-dead-summary (current-buffer))
6885   (gnus-dead-summary-mode 1)
6886   (let ((name (buffer-name)))
6887     (when (string-match "Summary" name)
6888       (rename-buffer
6889        (concat (substring name 0 (match-beginning 0)) "Dead "
6890                (substring name (match-beginning 0)))
6891        t)
6892       (bury-buffer))))
6893
6894 (defun gnus-kill-or-deaden-summary (buffer)
6895   "Kill or deaden the summary BUFFER."
6896   (save-excursion
6897     (when (and (buffer-name buffer)
6898                (not gnus-single-article-buffer))
6899       (with-current-buffer buffer
6900         (gnus-kill-buffer gnus-article-buffer)
6901         (gnus-kill-buffer gnus-original-article-buffer)))
6902     (cond
6903      ;; Kill the buffer.
6904      (gnus-kill-summary-on-exit
6905       (when (and gnus-use-trees
6906                  (gnus-buffer-exists-p buffer))
6907         (save-excursion
6908           (set-buffer buffer)
6909           (gnus-tree-close gnus-newsgroup-name)))
6910       (gnus-kill-buffer buffer))
6911      ;; Deaden the buffer.
6912      ((gnus-buffer-exists-p buffer)
6913       (save-excursion
6914         (set-buffer buffer)
6915         (gnus-deaden-summary))))))
6916
6917 (defun gnus-summary-wake-up-the-dead (&rest args)
6918   "Wake up the dead summary buffer."
6919   (interactive)
6920   (gnus-dead-summary-mode -1)
6921   (let ((name (buffer-name)))
6922     (when (string-match "Dead " name)
6923       (rename-buffer
6924        (concat (substring name 0 (match-beginning 0))
6925                (substring name (match-end 0)))
6926        t)))
6927   (gnus-message 3 "This dead summary is now alive again"))
6928
6929 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6930 (defun gnus-summary-fetch-faq (&optional faq-dir)
6931   "Fetch the FAQ for the current group.
6932 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6933 in."
6934   (interactive
6935    (list
6936     (when current-prefix-arg
6937       (completing-read
6938        "FAQ dir: " (and (listp gnus-group-faq-directory)
6939                         (mapcar 'list
6940                                 gnus-group-faq-directory))))))
6941   (let (gnus-faq-buffer)
6942     (when (setq gnus-faq-buffer
6943                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6944       (gnus-configure-windows 'summary-faq))))
6945
6946 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6947 (defun gnus-summary-describe-group (&optional force)
6948   "Describe the current newsgroup."
6949   (interactive "P")
6950   (gnus-group-describe-group force gnus-newsgroup-name))
6951
6952 (defun gnus-summary-describe-briefly ()
6953   "Describe summary mode commands briefly."
6954   (interactive)
6955   (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")))
6956
6957 ;; Walking around group mode buffer from summary mode.
6958
6959 (defun gnus-summary-next-group (&optional no-article target-group backward)
6960   "Exit current newsgroup and then select next unread newsgroup.
6961 If prefix argument NO-ARTICLE is non-nil, no article is selected
6962 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6963 previous group instead."
6964   (interactive "P")
6965   ;; Stop pre-fetching.
6966   (gnus-async-halt-prefetch)
6967   (let ((current-group gnus-newsgroup-name)
6968         (current-buffer (current-buffer))
6969         entered)
6970     ;; First we semi-exit this group to update Xrefs and all variables.
6971     ;; We can't do a real exit, because the window conf must remain
6972     ;; the same in case the user is prompted for info, and we don't
6973     ;; want the window conf to change before that...
6974     (gnus-summary-exit t)
6975     (while (not entered)
6976       ;; Then we find what group we are supposed to enter.
6977       (set-buffer gnus-group-buffer)
6978       (gnus-group-jump-to-group current-group)
6979       (setq target-group
6980             (or target-group
6981                 (if (eq gnus-keep-same-level 'best)
6982                     (gnus-summary-best-group gnus-newsgroup-name)
6983                   (gnus-summary-search-group backward gnus-keep-same-level))))
6984       (if (not target-group)
6985           ;; There are no further groups, so we return to the group
6986           ;; buffer.
6987           (progn
6988             (gnus-message 5 "Returning to the group buffer")
6989             (setq entered t)
6990             (when (gnus-buffer-live-p current-buffer)
6991               (set-buffer current-buffer)
6992               (gnus-summary-exit))
6993             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6994         ;; We try to enter the target group.
6995         (gnus-group-jump-to-group target-group)
6996         (let ((unreads (gnus-group-group-unread)))
6997           (if (and (or (eq t unreads)
6998                        (and unreads (not (zerop unreads))))
6999                    (gnus-summary-read-group
7000                     target-group nil no-article
7001                     (and (buffer-name current-buffer) current-buffer)
7002                     nil backward))
7003               (setq entered t)
7004             (setq current-group target-group
7005                   target-group nil)))))))
7006
7007 (defun gnus-summary-prev-group (&optional no-article)
7008   "Exit current newsgroup and then select previous unread newsgroup.
7009 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7010   (interactive "P")
7011   (gnus-summary-next-group no-article nil t))
7012
7013 ;; Walking around summary lines.
7014
7015 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7016   "Go to the first subject satisfying any non-nil constraint.
7017 If UNREAD is non-nil, the article should be unread.
7018 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7019 If UNSEEN is non-nil, the article should be unseen.
7020 Returns the article selected or nil if there are no matching articles."
7021   (interactive "P")
7022   (cond
7023    ;; Empty summary.
7024    ((null gnus-newsgroup-data)
7025     (gnus-message 3 "No articles in the group")
7026     nil)
7027    ;; Pick the first article.
7028    ((not (or unread undownloaded unseen))
7029     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7030     (gnus-data-number (car gnus-newsgroup-data)))
7031    ;; Find the first unread article.
7032    (t
7033     (let ((data gnus-newsgroup-data))
7034       (while (and data
7035                   (let ((num (gnus-data-number (car data))))
7036                     (or (memq num gnus-newsgroup-unfetched)
7037                         (not (or (and unread
7038                                       (memq num gnus-newsgroup-unreads))
7039                                  (and undownloaded
7040                                       (memq num gnus-newsgroup-undownloaded))
7041                                  (and unseen
7042                                       (memq num gnus-newsgroup-unseen)))))))
7043         (setq data (cdr data)))
7044       (prog1
7045           (if data
7046               (progn
7047                 (goto-char (gnus-data-pos (car data)))
7048                 (gnus-data-number (car data)))
7049             (gnus-message 3 "No more%s articles"
7050                           (let* ((r (when unread " unread"))
7051                                  (d (when undownloaded " undownloaded"))
7052                                  (s (when unseen " unseen"))
7053                                  (l (delq nil (list r d s))))
7054                             (cond ((= 3 (length l))
7055                                    (concat r "," d ", or" s))
7056                                   ((= 2 (length l))
7057                                    (concat (car l) ", or" (cadr l)))
7058                                   ((= 1 (length l))
7059                                    (car l))
7060                                   (t
7061                                    ""))))
7062             nil
7063             )
7064         (gnus-summary-position-point))))))
7065
7066 (defun gnus-summary-next-subject (n &optional unread dont-display)
7067   "Go to next N'th summary line.
7068 If N is negative, go to the previous N'th subject line.
7069 If UNREAD is non-nil, only unread articles are selected.
7070 The difference between N and the actual number of steps taken is
7071 returned."
7072   (interactive "p")
7073   (let ((backward (< n 0))
7074         (n (abs n)))
7075     (while (and (> n 0)
7076                 (if backward
7077                     (gnus-summary-find-prev unread)
7078                   (gnus-summary-find-next unread)))
7079       (unless (zerop (setq n (1- n)))
7080         (gnus-summary-show-thread)))
7081     (when (/= 0 n)
7082       (gnus-message 7 "No more%s articles"
7083                     (if unread " unread" "")))
7084     (unless dont-display
7085       (gnus-summary-recenter)
7086       (gnus-summary-position-point))
7087     n))
7088
7089 (defun gnus-summary-next-unread-subject (n)
7090   "Go to next N'th unread summary line."
7091   (interactive "p")
7092   (gnus-summary-next-subject n t))
7093
7094 (defun gnus-summary-prev-subject (n &optional unread)
7095   "Go to previous N'th summary line.
7096 If optional argument UNREAD is non-nil, only unread article is selected."
7097   (interactive "p")
7098   (gnus-summary-next-subject (- n) unread))
7099
7100 (defun gnus-summary-prev-unread-subject (n)
7101   "Go to previous N'th unread summary line."
7102   (interactive "p")
7103   (gnus-summary-next-subject (- n) t))
7104
7105 (defun gnus-summary-goto-subjects (articles)
7106   "Insert the subject header for ARTICLES in the current buffer."
7107   (save-excursion
7108     (dolist (article articles)
7109       (gnus-summary-goto-subject article t)))
7110   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7111   (gnus-summary-position-point))
7112
7113 (defun gnus-summary-goto-subject (article &optional force silent)
7114   "Go the subject line of ARTICLE.
7115 If FORCE, also allow jumping to articles not currently shown."
7116   (interactive "nArticle number: ")
7117   (unless (numberp article)
7118     (error "Article %s is not a number" article))
7119   (let ((b (point))
7120         (data (gnus-data-find article)))
7121     ;; We read in the article if we have to.
7122     (and (not data)
7123          force
7124          (gnus-summary-insert-subject
7125           article
7126           (if (or (numberp force) (vectorp force)) force)
7127           t)
7128          (setq data (gnus-data-find article)))
7129     (goto-char b)
7130     (if (not data)
7131         (progn
7132           (unless silent
7133             (gnus-message 3 "Can't find article %d" article))
7134           nil)
7135       (let ((pt (gnus-data-pos data)))
7136         (goto-char pt)
7137         (gnus-summary-set-article-display-arrow pt))
7138       (gnus-summary-position-point)
7139       article)))
7140
7141 ;; Walking around summary lines with displaying articles.
7142
7143 (defun gnus-summary-expand-window (&optional arg)
7144   "Make the summary buffer take up the entire Emacs frame.
7145 Given a prefix, will force an `article' buffer configuration."
7146   (interactive "P")
7147   (if arg
7148       (gnus-configure-windows 'article 'force)
7149     (gnus-configure-windows 'summary 'force)))
7150
7151 (defun gnus-summary-display-article (article &optional all-header)
7152   "Display ARTICLE in article buffer."
7153   (when (gnus-buffer-live-p gnus-article-buffer)
7154     (with-current-buffer gnus-article-buffer
7155       (mm-enable-multibyte)))
7156   (gnus-set-global-variables)
7157   (when (gnus-buffer-live-p gnus-article-buffer)
7158     (with-current-buffer gnus-article-buffer
7159       (setq gnus-article-charset gnus-newsgroup-charset)
7160       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7161       (mm-enable-multibyte)))
7162   (if (null article)
7163       nil
7164     (prog1
7165         (if gnus-summary-display-article-function
7166             (funcall gnus-summary-display-article-function article all-header)
7167           (gnus-article-prepare article all-header))
7168       (gnus-run-hooks 'gnus-select-article-hook)
7169       (when (and gnus-current-article
7170                  (not (zerop gnus-current-article)))
7171         (gnus-summary-goto-subject gnus-current-article))
7172       (gnus-summary-recenter)
7173       (when (and gnus-use-trees gnus-show-threads)
7174         (gnus-possibly-generate-tree article)
7175         (gnus-highlight-selected-tree article))
7176       ;; Successfully display article.
7177       (gnus-article-set-window-start
7178        (cdr (assq article gnus-newsgroup-bookmarks))))))
7179
7180 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7181   "Select the current article.
7182 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7183 non-nil, the article will be re-fetched even if it already present in
7184 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7185 be displayed."
7186   ;; Make sure we are in the summary buffer to work around bbdb bug.
7187   (unless (eq major-mode 'gnus-summary-mode)
7188     (set-buffer gnus-summary-buffer))
7189   (let ((article (or article (gnus-summary-article-number)))
7190         (all-headers (not (not all-headers))) ;Must be t or nil.
7191         gnus-summary-display-article-function)
7192     (and (not pseudo)
7193          (gnus-summary-article-pseudo-p article)
7194          (error "This is a pseudo-article"))
7195     (save-excursion
7196       (set-buffer gnus-summary-buffer)
7197       (if (or (and gnus-single-article-buffer
7198                    (or (null gnus-current-article)
7199                        (null gnus-article-current)
7200                        (null (get-buffer gnus-article-buffer))
7201                        (not (eq article (cdr gnus-article-current)))
7202                        (not (equal (car gnus-article-current)
7203                                    gnus-newsgroup-name))))
7204               (and (not gnus-single-article-buffer)
7205                    (or (null gnus-current-article)
7206                        (not (eq gnus-current-article article))))
7207               force)
7208           ;; The requested article is different from the current article.
7209           (progn
7210             (gnus-summary-display-article article all-headers)
7211             (when (gnus-buffer-live-p gnus-article-buffer)
7212               (with-current-buffer gnus-article-buffer
7213                 (if (not gnus-article-decoded-p) ;; a local variable
7214                     (mm-disable-multibyte))))
7215             (gnus-article-set-window-start
7216              (cdr (assq article gnus-newsgroup-bookmarks)))
7217             article)
7218         'old))))
7219
7220 (defun gnus-summary-force-verify-and-decrypt ()
7221   "Display buttons for signed/encrypted parts and verify/decrypt them."
7222   (interactive)
7223   (let ((mm-verify-option 'known)
7224         (mm-decrypt-option 'known)
7225         (gnus-article-emulate-mime t)
7226         (gnus-buttonized-mime-types (append (list "multipart/signed"
7227                                                   "multipart/encrypted")
7228                                             gnus-buttonized-mime-types)))
7229     (gnus-summary-select-article nil 'force)))
7230
7231 (defun gnus-summary-set-current-mark (&optional current-mark)
7232   "Obsolete function."
7233   nil)
7234
7235 (defun gnus-summary-next-article (&optional unread subject backward push)
7236   "Select the next article.
7237 If UNREAD, only unread articles are selected.
7238 If SUBJECT, only articles with SUBJECT are selected.
7239 If BACKWARD, the previous article is selected instead of the next."
7240   (interactive "P")
7241   (cond
7242    ;; Is there such an article?
7243    ((and (gnus-summary-search-forward unread subject backward)
7244          (or (gnus-summary-display-article (gnus-summary-article-number))
7245              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7246     (gnus-summary-position-point))
7247    ;; If not, we try the first unread, if that is wanted.
7248    ((and subject
7249          gnus-auto-select-same
7250          (gnus-summary-first-unread-article))
7251     (gnus-summary-position-point)
7252     (gnus-message 6 "Wrapped"))
7253    ;; Try to get next/previous article not displayed in this group.
7254    ((and gnus-auto-extend-newsgroup
7255          (not unread) (not subject))
7256     (gnus-summary-goto-article
7257      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7258      nil (count-lines (point-min) (point))))
7259    ;; Go to next/previous group.
7260    (t
7261     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7262       (gnus-summary-jump-to-group gnus-newsgroup-name))
7263     (let ((cmd last-command-char)
7264           (point
7265            (with-current-buffer gnus-group-buffer
7266              (point)))
7267           (group
7268            (if (eq gnus-keep-same-level 'best)
7269                (gnus-summary-best-group gnus-newsgroup-name)
7270              (gnus-summary-search-group backward gnus-keep-same-level))))
7271       ;; For some reason, the group window gets selected.  We change
7272       ;; it back.
7273       (select-window (get-buffer-window (current-buffer)))
7274       ;; Select next unread newsgroup automagically.
7275       (cond
7276        ((or (not gnus-auto-select-next)
7277             (not cmd))
7278         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7279        ((or (eq gnus-auto-select-next 'quietly)
7280             (and (eq gnus-auto-select-next 'slightly-quietly)
7281                  push)
7282             (and (eq gnus-auto-select-next 'almost-quietly)
7283                  (gnus-summary-last-article-p)))
7284         ;; Select quietly.
7285         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7286             (gnus-summary-exit)
7287           (gnus-message 7 "No more%s articles (%s)..."
7288                         (if unread " unread" "")
7289                         (if group (concat "selecting " group)
7290                           "exiting"))
7291           (gnus-summary-next-group nil group backward)))
7292        (t
7293         (when (gnus-key-press-event-p last-input-event)
7294           (gnus-summary-walk-group-buffer
7295            gnus-newsgroup-name cmd unread backward point))))))))
7296
7297 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7298   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7299                       (?\C-p (gnus-group-prev-unread-group 1))))
7300         (cursor-in-echo-area t)
7301         keve key group ended prompt)
7302     (save-excursion
7303       (set-buffer gnus-group-buffer)
7304       (goto-char start)
7305       (setq group
7306             (if (eq gnus-keep-same-level 'best)
7307                 (gnus-summary-best-group gnus-newsgroup-name)
7308               (gnus-summary-search-group backward gnus-keep-same-level))))
7309     (while (not ended)
7310       (setq prompt
7311             (format
7312              "No more%s articles%s " (if unread " unread" "")
7313              (if (and group
7314                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7315                  (format " (Type %s for %s [%s])"
7316                          (single-key-description cmd)
7317                          (gnus-group-decoded-name group)
7318                          (gnus-group-unread group))
7319                (format " (Type %s to exit %s)"
7320                        (single-key-description cmd)
7321                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7322       ;; Confirm auto selection.
7323       (setq key (car (setq keve (gnus-read-event-char prompt)))
7324             ended t)
7325       (cond
7326        ((assq key keystrokes)
7327         (let ((obuf (current-buffer)))
7328           (switch-to-buffer gnus-group-buffer)
7329           (when group
7330             (gnus-group-jump-to-group group))
7331           (eval (cadr (assq key keystrokes)))
7332           (setq group (gnus-group-group-name))
7333           (switch-to-buffer obuf))
7334         (setq ended nil))
7335        ((equal key cmd)
7336         (if (or (not group)
7337                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7338             (gnus-summary-exit)
7339           (gnus-summary-next-group nil group backward)))
7340        (t
7341         (push (cdr keve) unread-command-events))))))
7342
7343 (defun gnus-summary-next-unread-article ()
7344   "Select unread article after current one."
7345   (interactive)
7346   (gnus-summary-next-article
7347    (or (not (eq gnus-summary-goto-unread 'never))
7348        (gnus-summary-last-article-p (gnus-summary-article-number)))
7349    (and gnus-auto-select-same
7350         (gnus-summary-article-subject))))
7351
7352 (defun gnus-summary-prev-article (&optional unread subject)
7353   "Select the article after the current one.
7354 If UNREAD is non-nil, only unread articles are selected."
7355   (interactive "P")
7356   (gnus-summary-next-article unread subject t))
7357
7358 (defun gnus-summary-prev-unread-article ()
7359   "Select unread article before current one."
7360   (interactive)
7361   (gnus-summary-prev-article
7362    (or (not (eq gnus-summary-goto-unread 'never))
7363        (gnus-summary-first-article-p (gnus-summary-article-number)))
7364    (and gnus-auto-select-same
7365         (gnus-summary-article-subject))))
7366
7367 (defun gnus-summary-next-page (&optional lines circular stop)
7368   "Show next page of the selected article.
7369 If at the end of the current article, select the next article.
7370 LINES says how many lines should be scrolled up.
7371
7372 If CIRCULAR is non-nil, go to the start of the article instead of
7373 selecting the next article when reaching the end of the current
7374 article.
7375
7376 If STOP is non-nil, just stop when reaching the end of the message.
7377
7378 Also see the variable `gnus-article-skip-boring'."
7379   (interactive "P")
7380   (setq gnus-summary-buffer (current-buffer))
7381   (gnus-set-global-variables)
7382   (let ((article (gnus-summary-article-number))
7383         (article-window (get-buffer-window gnus-article-buffer t))
7384         endp)
7385     ;; If the buffer is empty, we have no article.
7386     (unless article
7387       (error "No article to select"))
7388     (gnus-configure-windows 'article)
7389     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7390         (if (and (eq gnus-summary-goto-unread 'never)
7391                  (not (gnus-summary-last-article-p article)))
7392             (gnus-summary-next-article)
7393           (gnus-summary-next-unread-article))
7394       (if (or (null gnus-current-article)
7395               (null gnus-article-current)
7396               (/= article (cdr gnus-article-current))
7397               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7398           ;; Selected subject is different from current article's.
7399           (gnus-summary-display-article article)
7400         (when article-window
7401           (gnus-eval-in-buffer-window gnus-article-buffer
7402             (setq endp (or (gnus-article-next-page lines)
7403                            (gnus-article-only-boring-p))))
7404           (when endp
7405             (cond (stop
7406                    (gnus-message 3 "End of message"))
7407                   (circular
7408                    (gnus-summary-beginning-of-article))
7409                   (lines
7410                    (gnus-message 3 "End of message"))
7411                   ((null lines)
7412                    (if (and (eq gnus-summary-goto-unread 'never)
7413                             (not (gnus-summary-last-article-p article)))
7414                        (gnus-summary-next-article)
7415                      (gnus-summary-next-unread-article))))))))
7416     (gnus-summary-recenter)
7417     (gnus-summary-position-point)))
7418
7419 (defun gnus-summary-prev-page (&optional lines move)
7420   "Show previous page of selected article.
7421 Argument LINES specifies lines to be scrolled down.
7422 If MOVE, move to the previous unread article if point is at
7423 the beginning of the buffer."
7424   (interactive "P")
7425   (let ((article (gnus-summary-article-number))
7426         (article-window (get-buffer-window gnus-article-buffer t))
7427         endp)
7428     (gnus-configure-windows 'article)
7429     (if (or (null gnus-current-article)
7430             (null gnus-article-current)
7431             (/= article (cdr gnus-article-current))
7432             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7433         ;; Selected subject is different from current article's.
7434         (gnus-summary-display-article article)
7435       (gnus-summary-recenter)
7436       (when article-window
7437         (gnus-eval-in-buffer-window gnus-article-buffer
7438           (setq endp (gnus-article-prev-page lines)))
7439         (when (and move endp)
7440           (cond (lines
7441                  (gnus-message 3 "Beginning of message"))
7442                 ((null lines)
7443                  (if (and (eq gnus-summary-goto-unread 'never)
7444                           (not (gnus-summary-first-article-p article)))
7445                      (gnus-summary-prev-article)
7446                    (gnus-summary-prev-unread-article))))))))
7447   (gnus-summary-position-point))
7448
7449 (defun gnus-summary-prev-page-or-article (&optional lines)
7450   "Show previous page of selected article.
7451 Argument LINES specifies lines to be scrolled down.
7452 If at the beginning of the article, go to the next article."
7453   (interactive "P")
7454   (gnus-summary-prev-page lines t))
7455
7456 (defun gnus-summary-scroll-up (lines)
7457   "Scroll up (or down) one line current article.
7458 Argument LINES specifies lines to be scrolled up (or down if negative)."
7459   (interactive "p")
7460   (gnus-configure-windows 'article)
7461   (gnus-summary-show-thread)
7462   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7463     (gnus-eval-in-buffer-window gnus-article-buffer
7464       (cond ((> lines 0)
7465              (when (gnus-article-next-page lines)
7466                (gnus-message 3 "End of message")))
7467             ((< lines 0)
7468              (gnus-article-prev-page (- lines))))))
7469   (gnus-summary-recenter)
7470   (gnus-summary-position-point))
7471
7472 (defun gnus-summary-scroll-down (lines)
7473   "Scroll down (or up) one line current article.
7474 Argument LINES specifies lines to be scrolled down (or up if negative)."
7475   (interactive "p")
7476   (gnus-summary-scroll-up (- lines)))
7477
7478 (defun gnus-summary-next-same-subject ()
7479   "Select next article which has the same subject as current one."
7480   (interactive)
7481   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7482
7483 (defun gnus-summary-prev-same-subject ()
7484   "Select previous article which has the same subject as current one."
7485   (interactive)
7486   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7487
7488 (defun gnus-summary-next-unread-same-subject ()
7489   "Select next unread article which has the same subject as current one."
7490   (interactive)
7491   (gnus-summary-next-article t (gnus-summary-article-subject)))
7492
7493 (defun gnus-summary-prev-unread-same-subject ()
7494   "Select previous unread article which has the same subject as current one."
7495   (interactive)
7496   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7497
7498 (defun gnus-summary-first-unread-article ()
7499   "Select the first unread article.
7500 Return nil if there are no unread articles."
7501   (interactive)
7502   (prog1
7503       (when (gnus-summary-first-subject t)
7504         (gnus-summary-show-thread)
7505         (gnus-summary-first-subject t)
7506         (gnus-summary-display-article (gnus-summary-article-number)))
7507     (gnus-summary-position-point)))
7508
7509 (defun gnus-summary-first-unread-subject ()
7510   "Place the point on the subject line of the first unread article.
7511 Return nil if there are no unread articles."
7512   (interactive)
7513   (prog1
7514       (when (gnus-summary-first-subject t)
7515         (gnus-summary-show-thread)
7516         (gnus-summary-first-subject t))
7517     (gnus-summary-position-point)))
7518
7519 (defun gnus-summary-first-unseen-subject ()
7520   "Place the point on the subject line of the first unseen article.
7521 Return nil if there are no unseen articles."
7522   (interactive)
7523   (prog1
7524       (when (gnus-summary-first-subject nil nil t)
7525         (gnus-summary-show-thread)
7526         (gnus-summary-first-subject nil nil t))
7527     (gnus-summary-position-point)))
7528
7529 (defun gnus-summary-first-unseen-or-unread-subject ()
7530   "Place the point on the subject line of the first unseen article or,
7531 if all article have been seen, on the subject line of the first unread
7532 article."
7533   (interactive)
7534   (prog1
7535       (unless (when (gnus-summary-first-subject nil nil t)
7536                 (gnus-summary-show-thread)
7537                 (gnus-summary-first-subject nil nil t))
7538         (when (gnus-summary-first-subject t)
7539           (gnus-summary-show-thread)
7540           (gnus-summary-first-subject t)))
7541     (gnus-summary-position-point)))
7542
7543 (defun gnus-summary-first-article ()
7544   "Select the first article.
7545 Return nil if there are no articles."
7546   (interactive)
7547   (prog1
7548       (when (gnus-summary-first-subject)
7549         (gnus-summary-show-thread)
7550         (gnus-summary-first-subject)
7551         (gnus-summary-display-article (gnus-summary-article-number)))
7552     (gnus-summary-position-point)))
7553
7554 (defun gnus-summary-best-unread-article (&optional arg)
7555   "Select the unread article with the highest score.
7556 If given a prefix argument, select the next unread article that has a
7557 score higher than the default score."
7558   (interactive "P")
7559   (let ((article (if arg
7560                      (gnus-summary-better-unread-subject)
7561                    (gnus-summary-best-unread-subject))))
7562     (if article
7563         (gnus-summary-goto-article article)
7564       (error "No unread articles"))))
7565
7566 (defun gnus-summary-best-unread-subject ()
7567   "Select the unread subject with the highest score."
7568   (interactive)
7569   (let ((best -1000000)
7570         (data gnus-newsgroup-data)
7571         article score)
7572     (while data
7573       (and (gnus-data-unread-p (car data))
7574            (> (setq score
7575                     (gnus-summary-article-score (gnus-data-number (car data))))
7576               best)
7577            (setq best score
7578                  article (gnus-data-number (car data))))
7579       (setq data (cdr data)))
7580     (when article
7581       (gnus-summary-goto-subject article))
7582     (gnus-summary-position-point)
7583     article))
7584
7585 (defun gnus-summary-better-unread-subject ()
7586   "Select the first unread subject that has a score over the default score."
7587   (interactive)
7588   (let ((data gnus-newsgroup-data)
7589         article score)
7590     (while (and (setq article (gnus-data-number (car data)))
7591                 (or (gnus-data-read-p (car data))
7592                     (not (> (gnus-summary-article-score article)
7593                             gnus-summary-default-score))))
7594       (setq data (cdr data)))
7595     (when article
7596       (gnus-summary-goto-subject article))
7597     (gnus-summary-position-point)
7598     article))
7599
7600 (defun gnus-summary-last-subject ()
7601   "Go to the last displayed subject line in the group."
7602   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7603     (when article
7604       (gnus-summary-goto-subject article))))
7605
7606 (defun gnus-summary-goto-article (article &optional all-headers force)
7607   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7608 If ALL-HEADERS is non-nil, no header lines are hidden.
7609 If FORCE, go to the article even if it isn't displayed.  If FORCE
7610 is a number, it is the line the article is to be displayed on."
7611   (interactive
7612    (list
7613     (completing-read
7614      "Article number or Message-ID: "
7615      (mapcar (lambda (number) (list (int-to-string number)))
7616              gnus-newsgroup-limit))
7617     current-prefix-arg
7618     t))
7619   (prog1
7620       (if (and (stringp article)
7621                (string-match "@\\|%40" article))
7622           (gnus-summary-refer-article article)
7623         (when (stringp article)
7624           (setq article (string-to-number article)))
7625         (if (gnus-summary-goto-subject article force)
7626             (gnus-summary-display-article article all-headers)
7627           (gnus-message 4 "Couldn't go to article %s" article) nil))
7628     (gnus-summary-position-point)))
7629
7630 (defun gnus-summary-goto-last-article ()
7631   "Go to the previously read article."
7632   (interactive)
7633   (prog1
7634       (when gnus-last-article
7635         (gnus-summary-goto-article gnus-last-article nil t))
7636     (gnus-summary-position-point)))
7637
7638 (defun gnus-summary-pop-article (number)
7639   "Pop one article off the history and go to the previous.
7640 NUMBER articles will be popped off."
7641   (interactive "p")
7642   (let (to)
7643     (setq gnus-newsgroup-history
7644           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7645     (if to
7646         (gnus-summary-goto-article (car to) nil t)
7647       (error "Article history empty")))
7648   (gnus-summary-position-point))
7649
7650 ;; Summary commands and functions for limiting the summary buffer.
7651
7652 (defun gnus-summary-limit-to-articles (n)
7653   "Limit the summary buffer to the next N articles.
7654 If not given a prefix, use the process marked articles instead."
7655   (interactive "P")
7656   (prog1
7657       (let ((articles (gnus-summary-work-articles n)))
7658         (setq gnus-newsgroup-processable nil)
7659         (gnus-summary-limit articles))
7660     (gnus-summary-position-point)))
7661
7662 (defun gnus-summary-pop-limit (&optional total)
7663   "Restore the previous limit.
7664 If given a prefix, remove all limits."
7665   (interactive "P")
7666   (when total
7667     (setq gnus-newsgroup-limits
7668           (list (mapcar (lambda (h) (mail-header-number h))
7669                         gnus-newsgroup-headers))))
7670   (unless gnus-newsgroup-limits
7671     (error "No limit to pop"))
7672   (prog1
7673       (gnus-summary-limit nil 'pop)
7674     (gnus-summary-position-point)))
7675
7676 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7677   "Limit the summary buffer to articles that have subjects that match a regexp.
7678 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7679   (interactive
7680    (list (read-string (if current-prefix-arg
7681                           "Exclude subject (regexp): "
7682                         "Limit to subject (regexp): "))
7683          nil current-prefix-arg))
7684   (unless header
7685     (setq header "subject"))
7686   (when (not (equal "" subject))
7687     (prog1
7688         (let ((articles (gnus-summary-find-matching
7689                          (or header "subject") subject 'all nil nil
7690                          not-matching)))
7691           (unless articles
7692             (error "Found no matches for \"%s\"" subject))
7693           (gnus-summary-limit articles))
7694       (gnus-summary-position-point))))
7695
7696 (defun gnus-summary-limit-to-author (from &optional not-matching)
7697   "Limit the summary buffer to articles that have authors that match a regexp.
7698 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7699   (interactive
7700    (list (read-string (if current-prefix-arg
7701                           "Exclude author (regexp): "
7702                         "Limit to author (regexp): "))
7703          current-prefix-arg))
7704   (gnus-summary-limit-to-subject from "from" not-matching))
7705
7706 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7707   "Limit the summary buffer to articles with the given RECIPIENT.
7708
7709 If NOT-MATCHING, exclude RECIPIENT.
7710
7711 To and Cc headers are checked.  You need to include them in
7712 `nnmail-extra-headers'."
7713   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7714   (interactive
7715    (list (read-string (format "%s recipient (regexp): "
7716                               (if current-prefix-arg "Exclude" "Limit to")))
7717          current-prefix-arg))
7718   (when (not (equal "" recipient))
7719     (prog1 (let* ((to
7720                    (if (memq 'To nnmail-extra-headers)
7721                        (gnus-summary-find-matching
7722                         (cons 'extra 'To) recipient 'all nil nil
7723                         not-matching)
7724                      (gnus-message
7725                       1 "`To' isn't present in `nnmail-extra-headers'")
7726                      (sit-for 1)
7727                      nil))
7728                   (cc
7729                    (if (memq 'Cc nnmail-extra-headers)
7730                        (gnus-summary-find-matching
7731                         (cons 'extra 'Cc) recipient 'all nil nil
7732                         not-matching)
7733                      (gnus-message
7734                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7735                      (sit-for 1)
7736                      nil))
7737                   (articles
7738                    (if not-matching
7739                        ;; We need the numbers that are in both lists:
7740                        (mapcar (lambda (a)
7741                                  (and (memq a to) a))
7742                                cc)
7743                      (nconc to cc))))
7744              (unless articles
7745                (error "Found no matches for \"%s\"" recipient))
7746              (gnus-summary-limit articles))
7747       (gnus-summary-position-point))))
7748
7749 (defun gnus-summary-limit-to-age (age &optional younger-p)
7750   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7751 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7752 articles that are younger than AGE days."
7753   (interactive
7754    (let ((younger current-prefix-arg)
7755          (days-got nil)
7756          days)
7757      (while (not days-got)
7758        (setq days (if younger
7759                       (read-string "Limit to articles younger than (in days, older when negative): ")
7760                     (read-string
7761                      "Limit to articles older than (in days, younger when negative): ")))
7762        (when (> (length days) 0)
7763          (setq days (read days)))
7764        (if (numberp days)
7765            (progn
7766              (setq days-got t)
7767              (when (< days 0)
7768                (setq younger (not younger))
7769                (setq days (* days -1))))
7770          (message "Please enter a number.")
7771          (sleep-for 1)))
7772      (list days younger)))
7773   (prog1
7774       (let ((data gnus-newsgroup-data)
7775             (cutoff (days-to-time age))
7776             articles d date is-younger)
7777         (while (setq d (pop data))
7778           (when (and (vectorp (gnus-data-header d))
7779                      (setq date (mail-header-date (gnus-data-header d))))
7780             (setq is-younger (time-less-p
7781                               (time-since (condition-case ()
7782                                               (date-to-time date)
7783                                             (error '(0 0))))
7784                               cutoff))
7785             (when (if younger-p
7786                       is-younger
7787                     (not is-younger))
7788               (push (gnus-data-number d) articles))))
7789         (gnus-summary-limit (nreverse articles)))
7790     (gnus-summary-position-point)))
7791
7792 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7793   "Limit the summary buffer to articles that match an 'extra' header."
7794   (interactive
7795    (let ((header
7796           (intern
7797            (gnus-completing-read-with-default
7798             (symbol-name (car gnus-extra-headers))
7799             (if current-prefix-arg
7800                 "Exclude extra header:"
7801               "Limit extra header:")
7802             (mapcar (lambda (x)
7803                       (cons (symbol-name x) x))
7804                     gnus-extra-headers)
7805             nil
7806             t))))
7807      (list header
7808            (read-string (format "%s header %s (regexp): "
7809                                 (if current-prefix-arg "Exclude" "Limit to")
7810                                 header))
7811            current-prefix-arg)))
7812   (when (not (equal "" regexp))
7813     (prog1
7814         (let ((articles (gnus-summary-find-matching
7815                          (cons 'extra header) regexp 'all nil nil
7816                          not-matching)))
7817           (unless articles
7818             (error "Found no matches for \"%s\"" regexp))
7819           (gnus-summary-limit articles))
7820       (gnus-summary-position-point))))
7821
7822 (defun gnus-summary-limit-to-display-predicate ()
7823   "Limit the summary buffer to the predicated in the `display' group parameter."
7824   (interactive)
7825   (unless gnus-newsgroup-display
7826     (error "There is no `display' group parameter"))
7827   (let (articles)
7828     (dolist (number gnus-newsgroup-articles)
7829       (when (funcall gnus-newsgroup-display)
7830         (push number articles)))
7831     (gnus-summary-limit articles))
7832   (gnus-summary-position-point))
7833
7834 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7835 (make-obsolete
7836  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7837
7838 (defun gnus-summary-limit-to-unread (&optional all)
7839   "Limit the summary buffer to articles that are not marked as read.
7840 If ALL is non-nil, limit strictly to unread articles."
7841   (interactive "P")
7842   (if all
7843       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7844     (gnus-summary-limit-to-marks
7845      ;; Concat all the marks that say that an article is read and have
7846      ;; those removed.
7847      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7848            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7849            gnus-low-score-mark gnus-expirable-mark
7850            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7851            gnus-duplicate-mark gnus-souped-mark)
7852      'reverse)))
7853
7854 (defun gnus-summary-limit-to-replied (&optional unreplied)
7855   "Limit the summary buffer to replied articles.
7856 If UNREPLIED (the prefix), limit to unreplied articles."
7857   (interactive "P")
7858   (if unreplied
7859       (gnus-summary-limit
7860        (gnus-set-difference gnus-newsgroup-articles
7861         gnus-newsgroup-replied))
7862     (gnus-summary-limit gnus-newsgroup-replied))
7863   (gnus-summary-position-point))
7864
7865 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7866 (make-obsolete 'gnus-summary-delete-marked-with
7867                'gnus-summary-limit-exclude-marks)
7868
7869 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7870   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7871 If REVERSE, limit the summary buffer to articles that are marked
7872 with MARKS.  MARKS can either be a string of marks or a list of marks.
7873 Returns how many articles were removed."
7874   (interactive "sMarks: ")
7875   (gnus-summary-limit-to-marks marks t))
7876
7877 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7878   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7879 If REVERSE (the prefix), limit the summary buffer to articles that are
7880 not marked with MARKS.  MARKS can either be a string of marks or a
7881 list of marks.
7882 Returns how many articles were removed."
7883   (interactive "sMarks: \nP")
7884   (prog1
7885       (let ((data gnus-newsgroup-data)
7886             (marks (if (listp marks) marks
7887                      (append marks nil))) ; Transform to list.
7888             articles)
7889         (while data
7890           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7891                   (memq (gnus-data-mark (car data)) marks))
7892             (push (gnus-data-number (car data)) articles))
7893           (setq data (cdr data)))
7894         (gnus-summary-limit articles))
7895     (gnus-summary-position-point)))
7896
7897 (defun gnus-summary-limit-to-score (score)
7898   "Limit to articles with score at or above SCORE."
7899   (interactive "NLimit to articles with score of at least: ")
7900   (let ((data gnus-newsgroup-data)
7901         articles)
7902     (while data
7903       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7904                 score)
7905         (push (gnus-data-number (car data)) articles))
7906       (setq data (cdr data)))
7907     (prog1
7908         (gnus-summary-limit articles)
7909       (gnus-summary-position-point))))
7910
7911 (defun gnus-summary-limit-to-unseen ()
7912   "Limit to unseen articles."
7913   (interactive)
7914   (prog1
7915       (gnus-summary-limit gnus-newsgroup-unseen)
7916     (gnus-summary-position-point)))
7917
7918 (defun gnus-summary-limit-include-thread (id)
7919   "Display all the hidden articles that is in the thread with ID in it.
7920 When called interactively, ID is the Message-ID of the current
7921 article."
7922   (interactive (list (mail-header-id (gnus-summary-article-header))))
7923   (let ((articles (gnus-articles-in-thread
7924                    (gnus-id-to-thread (gnus-root-id id)))))
7925     (prog1
7926         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7927       (gnus-summary-limit-include-matching-articles
7928        "subject"
7929        (regexp-quote (gnus-simplify-subject-re
7930                       (mail-header-subject (gnus-id-to-header id)))))
7931       (gnus-summary-position-point))))
7932
7933 (defun gnus-summary-limit-include-matching-articles (header regexp)
7934   "Display all the hidden articles that have HEADERs that match REGEXP."
7935   (interactive (list (read-string "Match on header: ")
7936                      (read-string "Regexp: ")))
7937   (let ((articles (gnus-find-matching-articles header regexp)))
7938     (prog1
7939         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7940       (gnus-summary-position-point))))
7941
7942 (defun gnus-summary-insert-dormant-articles ()
7943   "Insert all the dormant articles for this group into the current buffer."
7944   (interactive)
7945   (let ((gnus-verbose (max 6 gnus-verbose)))
7946     (if (not gnus-newsgroup-dormant)
7947         (gnus-message 3 "No cached articles for this group")
7948       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7949
7950 (defun gnus-summary-limit-include-dormant ()
7951   "Display all the hidden articles that are marked as dormant.
7952 Note that this command only works on a subset of the articles currently
7953 fetched for this group."
7954   (interactive)
7955   (unless gnus-newsgroup-dormant
7956     (error "There are no dormant articles in this group"))
7957   (prog1
7958       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7959     (gnus-summary-position-point)))
7960
7961 (defun gnus-summary-limit-exclude-dormant ()
7962   "Hide all dormant articles."
7963   (interactive)
7964   (prog1
7965       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7966     (gnus-summary-position-point)))
7967
7968 (defun gnus-summary-limit-exclude-childless-dormant ()
7969   "Hide all dormant articles that have no children."
7970   (interactive)
7971   (let ((data (gnus-data-list t))
7972         articles d children)
7973     ;; Find all articles that are either not dormant or have
7974     ;; children.
7975     (while (setq d (pop data))
7976       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7977                 (and (setq children
7978                            (gnus-article-children (gnus-data-number d)))
7979                      (let (found)
7980                        (while children
7981                          (when (memq (car children) articles)
7982                            (setq children nil
7983                                  found t))
7984                          (pop children))
7985                        found)))
7986         (push (gnus-data-number d) articles)))
7987     ;; Do the limiting.
7988     (prog1
7989         (gnus-summary-limit articles)
7990       (gnus-summary-position-point))))
7991
7992 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7993   "Mark all unread excluded articles as read.
7994 If ALL, mark even excluded ticked and dormants as read."
7995   (interactive "P")
7996   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7997   (let ((articles (gnus-sorted-ndifference
7998                    (sort
7999                     (mapcar (lambda (h) (mail-header-number h))
8000                             gnus-newsgroup-headers)
8001                     '<)
8002                    gnus-newsgroup-limit))
8003         article)
8004     (setq gnus-newsgroup-unreads
8005           (gnus-sorted-intersection gnus-newsgroup-unreads
8006                                     gnus-newsgroup-limit))
8007     (if all
8008         (setq gnus-newsgroup-dormant nil
8009               gnus-newsgroup-marked nil
8010               gnus-newsgroup-reads
8011               (nconc
8012                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8013                gnus-newsgroup-reads))
8014       (while (setq article (pop articles))
8015         (unless (or (memq article gnus-newsgroup-dormant)
8016                     (memq article gnus-newsgroup-marked))
8017           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8018
8019 (defun gnus-summary-limit (articles &optional pop)
8020   (if pop
8021       ;; We pop the previous limit off the stack and use that.
8022       (setq articles (car gnus-newsgroup-limits)
8023             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8024     ;; We use the new limit, so we push the old limit on the stack.
8025     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8026   ;; Set the limit.
8027   (setq gnus-newsgroup-limit articles)
8028   (let ((total (length gnus-newsgroup-data))
8029         (data (gnus-data-find-list (gnus-summary-article-number)))
8030         (gnus-summary-mark-below nil)   ; Inhibit this.
8031         found)
8032     ;; This will do all the work of generating the new summary buffer
8033     ;; according to the new limit.
8034     (gnus-summary-prepare)
8035     ;; Hide any threads, possibly.
8036     (gnus-summary-maybe-hide-threads)
8037     ;; Try to return to the article you were at, or one in the
8038     ;; neighborhood.
8039     (when data
8040       ;; We try to find some article after the current one.
8041       (while data
8042         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8043           (setq data nil
8044                 found t))
8045         (setq data (cdr data))))
8046     (unless found
8047       ;; If there is no data, that means that we were after the last
8048       ;; article.  The same goes when we can't find any articles
8049       ;; after the current one.
8050       (goto-char (point-max))
8051       (gnus-summary-find-prev))
8052     (gnus-set-mode-line 'summary)
8053     ;; We return how many articles were removed from the summary
8054     ;; buffer as a result of the new limit.
8055     (- total (length gnus-newsgroup-data))))
8056
8057 (defsubst gnus-invisible-cut-children (threads)
8058   (let ((num 0))
8059     (while threads
8060       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8061         (incf num))
8062       (pop threads))
8063     (< num 2)))
8064
8065 (defsubst gnus-cut-thread (thread)
8066   "Go forwards in the thread until we find an article that we want to display."
8067   (when (or (eq gnus-fetch-old-headers 'some)
8068             (eq gnus-fetch-old-headers 'invisible)
8069             (numberp gnus-fetch-old-headers)
8070             (eq gnus-build-sparse-threads 'some)
8071             (eq gnus-build-sparse-threads 'more))
8072     ;; Deal with old-fetched headers and sparse threads.
8073     (while (and
8074             thread
8075             (or
8076              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8077              (gnus-summary-article-ancient-p
8078               (mail-header-number (car thread))))
8079             (if (or (<= (length (cdr thread)) 1)
8080                     (eq gnus-fetch-old-headers 'invisible))
8081                 (setq gnus-newsgroup-limit
8082                       (delq (mail-header-number (car thread))
8083                             gnus-newsgroup-limit)
8084                       thread (cadr thread))
8085               (when (gnus-invisible-cut-children (cdr thread))
8086                 (let ((th (cdr thread)))
8087                   (while th
8088                     (if (memq (mail-header-number (caar th))
8089                               gnus-newsgroup-limit)
8090                         (setq thread (car th)
8091                               th nil)
8092                       (setq th (cdr th))))))))))
8093   thread)
8094
8095 (defun gnus-cut-threads (threads)
8096   "Cut off all uninteresting articles from the beginning of THREADS."
8097   (when (or (eq gnus-fetch-old-headers 'some)
8098             (eq gnus-fetch-old-headers 'invisible)
8099             (numberp gnus-fetch-old-headers)
8100             (eq gnus-build-sparse-threads 'some)
8101             (eq gnus-build-sparse-threads 'more))
8102     (let ((th threads))
8103       (while th
8104         (setcar th (gnus-cut-thread (car th)))
8105         (setq th (cdr th)))))
8106   ;; Remove nixed out threads.
8107   (delq nil threads))
8108
8109 (defun gnus-summary-initial-limit (&optional show-if-empty)
8110   "Figure out what the initial limit is supposed to be on group entry.
8111 This entails weeding out unwanted dormants, low-scored articles,
8112 fetch-old-headers verbiage, and so on."
8113   ;; Most groups have nothing to remove.
8114   (if (or gnus-inhibit-limiting
8115           (and (null gnus-newsgroup-dormant)
8116                (eq gnus-newsgroup-display 'gnus-not-ignore)
8117                (not (eq gnus-fetch-old-headers 'some))
8118                (not (numberp gnus-fetch-old-headers))
8119                (not (eq gnus-fetch-old-headers 'invisible))
8120                (null gnus-summary-expunge-below)
8121                (not (eq gnus-build-sparse-threads 'some))
8122                (not (eq gnus-build-sparse-threads 'more))
8123                (null gnus-thread-expunge-below)
8124                (not gnus-use-nocem)))
8125       ()                                ; Do nothing.
8126     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8127     (setq gnus-newsgroup-limit nil)
8128     (mapatoms
8129      (lambda (node)
8130        (unless (car (symbol-value node))
8131          ;; These threads have no parents -- they are roots.
8132          (let ((nodes (cdr (symbol-value node)))
8133                thread)
8134            (while nodes
8135              (if (and gnus-thread-expunge-below
8136                       (< (gnus-thread-total-score (car nodes))
8137                          gnus-thread-expunge-below))
8138                  (gnus-expunge-thread (pop nodes))
8139                (setq thread (pop nodes))
8140                (gnus-summary-limit-children thread))))))
8141      gnus-newsgroup-dependencies)
8142     ;; If this limitation resulted in an empty group, we might
8143     ;; pop the previous limit and use it instead.
8144     (when (and (not gnus-newsgroup-limit)
8145                show-if-empty)
8146       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8147     gnus-newsgroup-limit))
8148
8149 (defun gnus-summary-limit-children (thread)
8150   "Return 1 if this subthread is visible and 0 if it is not."
8151   ;; First we get the number of visible children to this thread.  This
8152   ;; is done by recursing down the thread using this function, so this
8153   ;; will really go down to a leaf article first, before slowly
8154   ;; working its way up towards the root.
8155   (when thread
8156     (let* ((max-lisp-eval-depth 5000)
8157            (children
8158            (if (cdr thread)
8159                (apply '+ (mapcar 'gnus-summary-limit-children
8160                                  (cdr thread)))
8161              0))
8162           (number (mail-header-number (car thread)))
8163           score)
8164       (if (and
8165            (not (memq number gnus-newsgroup-marked))
8166            (or
8167             ;; If this article is dormant and has absolutely no visible
8168             ;; children, then this article isn't visible.
8169             (and (memq number gnus-newsgroup-dormant)
8170                  (zerop children))
8171             ;; If this is "fetch-old-headered" and there is no
8172             ;; visible children, then we don't want this article.
8173             (and (or (eq gnus-fetch-old-headers 'some)
8174                      (numberp gnus-fetch-old-headers))
8175                  (gnus-summary-article-ancient-p number)
8176                  (zerop children))
8177             ;; If this is "fetch-old-headered" and `invisible', then
8178             ;; we don't want this article.
8179             (and (eq gnus-fetch-old-headers 'invisible)
8180                  (gnus-summary-article-ancient-p number))
8181             ;; If this is a sparsely inserted article with no children,
8182             ;; we don't want it.
8183             (and (eq gnus-build-sparse-threads 'some)
8184                  (gnus-summary-article-sparse-p number)
8185                  (zerop children))
8186             ;; If we use expunging, and this article is really
8187             ;; low-scored, then we don't want this article.
8188             (when (and gnus-summary-expunge-below
8189                        (< (setq score
8190                                 (or (cdr (assq number gnus-newsgroup-scored))
8191                                     gnus-summary-default-score))
8192                           gnus-summary-expunge-below))
8193               ;; We increase the expunge-tally here, but that has
8194               ;; nothing to do with the limits, really.
8195               (incf gnus-newsgroup-expunged-tally)
8196               ;; We also mark as read here, if that's wanted.
8197               (when (and gnus-summary-mark-below
8198                          (< score gnus-summary-mark-below))
8199                 (setq gnus-newsgroup-unreads
8200                       (delq number gnus-newsgroup-unreads))
8201                 (if gnus-newsgroup-auto-expire
8202                     (push number gnus-newsgroup-expirable)
8203                   (push (cons number gnus-low-score-mark)
8204                         gnus-newsgroup-reads)))
8205               t)
8206             ;; Do the `display' group parameter.
8207             (and gnus-newsgroup-display
8208                  (not (funcall gnus-newsgroup-display)))
8209             ;; Check NoCeM things.
8210             (when (and gnus-use-nocem
8211                        (gnus-nocem-unwanted-article-p
8212                         (mail-header-id (car thread))))
8213               (setq gnus-newsgroup-unreads
8214                     (delq number gnus-newsgroup-unreads))
8215               t)))
8216           ;; Nope, invisible article.
8217           0
8218         ;; Ok, this article is to be visible, so we add it to the limit
8219         ;; and return 1.
8220         (push number gnus-newsgroup-limit)
8221         1))))
8222
8223 (defun gnus-expunge-thread (thread)
8224   "Mark all articles in THREAD as read."
8225   (let* ((number (mail-header-number (car thread))))
8226     (incf gnus-newsgroup-expunged-tally)
8227     ;; We also mark as read here, if that's wanted.
8228     (setq gnus-newsgroup-unreads
8229           (delq number gnus-newsgroup-unreads))
8230     (if gnus-newsgroup-auto-expire
8231         (push number gnus-newsgroup-expirable)
8232       (push (cons number gnus-low-score-mark)
8233             gnus-newsgroup-reads)))
8234   ;; Go recursively through all subthreads.
8235   (mapcar 'gnus-expunge-thread (cdr thread)))
8236
8237 ;; Summary article oriented commands
8238
8239 (defun gnus-summary-refer-parent-article (n)
8240   "Refer parent article N times.
8241 If N is negative, go to ancestor -N instead.
8242 The difference between N and the number of articles fetched is returned."
8243   (interactive "p")
8244   (let ((skip 1)
8245         error header ref)
8246     (when (not (natnump n))
8247       (setq skip (abs n)
8248             n 1))
8249     (while (and (> n 0)
8250                 (not error))
8251       (setq header (gnus-summary-article-header))
8252       (if (and (eq (mail-header-number header)
8253                    (cdr gnus-article-current))
8254                (equal gnus-newsgroup-name
8255                       (car gnus-article-current)))
8256           ;; If we try to find the parent of the currently
8257           ;; displayed article, then we take a look at the actual
8258           ;; References header, since this is slightly more
8259           ;; reliable than the References field we got from the
8260           ;; server.
8261           (save-excursion
8262             (set-buffer gnus-original-article-buffer)
8263             (nnheader-narrow-to-headers)
8264             (unless (setq ref (message-fetch-field "references"))
8265               (when (setq ref (message-fetch-field "in-reply-to"))
8266                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8267             (widen))
8268         (setq ref
8269               ;; It's not the current article, so we take a bet on
8270               ;; the value we got from the server.
8271               (mail-header-references header)))
8272       (if (and ref
8273                (not (equal ref "")))
8274           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8275             (gnus-message 1 "Couldn't find parent"))
8276         (gnus-message 1 "No references in article %d"
8277                       (gnus-summary-article-number))
8278         (setq error t))
8279       (decf n))
8280     (gnus-summary-position-point)
8281     n))
8282
8283 (defun gnus-summary-refer-references ()
8284   "Fetch all articles mentioned in the References header.
8285 Return the number of articles fetched."
8286   (interactive)
8287   (let ((ref (mail-header-references (gnus-summary-article-header)))
8288         (current (gnus-summary-article-number))
8289         (n 0))
8290     (if (or (not ref)
8291             (equal ref ""))
8292         (error "No References in the current article")
8293       ;; For each Message-ID in the References header...
8294       (while (string-match "<[^>]*>" ref)
8295         (incf n)
8296         ;; ... fetch that article.
8297         (gnus-summary-refer-article
8298          (prog1 (match-string 0 ref)
8299            (setq ref (substring ref (match-end 0))))))
8300       (gnus-summary-goto-subject current)
8301       (gnus-summary-position-point)
8302       n)))
8303
8304 (defun gnus-summary-refer-thread (&optional limit)
8305   "Fetch all articles in the current thread.
8306 If LIMIT (the numerical prefix), fetch that many old headers instead
8307 of what's specified by the `gnus-refer-thread-limit' variable."
8308   (interactive "P")
8309   (let ((id (mail-header-id (gnus-summary-article-header)))
8310         (limit (if limit (prefix-numeric-value limit)
8311                  gnus-refer-thread-limit)))
8312     (unless (eq gnus-fetch-old-headers 'invisible)
8313       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8314       ;; Retrieve the headers and read them in.
8315       (if (eq (if (numberp limit)
8316                   (gnus-retrieve-headers
8317                    (list (min
8318                           (+ (mail-header-number
8319                               (gnus-summary-article-header))
8320                              limit)
8321                           gnus-newsgroup-end))
8322                    gnus-newsgroup-name (* limit 2))
8323                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8324                 ;; headers.
8325                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8326                                        gnus-newsgroup-name limit))
8327               'nov)
8328           (gnus-build-all-threads)
8329         (error "Can't fetch thread from back ends that don't support NOV"))
8330       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8331     (gnus-summary-limit-include-thread id)))
8332
8333 (defun gnus-summary-refer-article (message-id)
8334   "Fetch an article specified by MESSAGE-ID."
8335   (interactive "sMessage-ID: ")
8336   (when (and (stringp message-id)
8337              (not (zerop (length message-id))))
8338     (setq message-id (gnus-replace-in-string message-id " " ""))
8339     ;; Construct the correct Message-ID if necessary.
8340     ;; Suggested by tale@pawl.rpi.edu.
8341     (unless (string-match "^<" message-id)
8342       (setq message-id (concat "<" message-id)))
8343     (unless (string-match ">$" message-id)
8344       (setq message-id (concat message-id ">")))
8345     ;; People often post MIDs from URLs, so unhex it:
8346     (unless (string-match "@" message-id)
8347       (setq message-id (gnus-url-unhex-string message-id)))
8348     (let* ((header (gnus-id-to-header message-id))
8349            (sparse (and header
8350                         (gnus-summary-article-sparse-p
8351                          (mail-header-number header))
8352                         (memq (mail-header-number header)
8353                               gnus-newsgroup-limit)))
8354            number)
8355       (cond
8356        ;; If the article is present in the buffer we just go to it.
8357        ((and header
8358              (or (not (gnus-summary-article-sparse-p
8359                        (mail-header-number header)))
8360                  sparse))
8361         (prog1
8362             (gnus-summary-goto-article
8363              (mail-header-number header) nil t)
8364           (when sparse
8365             (gnus-summary-update-article (mail-header-number header)))))
8366        (t
8367         ;; We fetch the article.
8368         (catch 'found
8369           (dolist (gnus-override-method (gnus-refer-article-methods))
8370             (when (and (gnus-check-server gnus-override-method)
8371                        ;; Fetch the header,
8372                        (setq number (gnus-summary-insert-subject message-id)))
8373               ;; and display the article.
8374               (gnus-summary-select-article nil nil nil number)
8375               (throw 'found t)))
8376           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8377
8378 (defun gnus-refer-article-methods ()
8379   "Return a list of referable methods."
8380   (cond
8381    ;; No method, so we default to current and native.
8382    ((null gnus-refer-article-method)
8383     (list gnus-current-select-method gnus-select-method))
8384    ;; Current.
8385    ((eq 'current gnus-refer-article-method)
8386     (list gnus-current-select-method))
8387    ;; List of select methods.
8388    ((not (and (symbolp (car gnus-refer-article-method))
8389               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8390     (let (out)
8391       (dolist (method gnus-refer-article-method)
8392         (push (if (eq 'current method)
8393                   gnus-current-select-method
8394                 method)
8395               out))
8396       (nreverse out)))
8397    ;; One single select method.
8398    (t
8399     (list gnus-refer-article-method))))
8400
8401 (defun gnus-summary-edit-parameters ()
8402   "Edit the group parameters of the current group."
8403   (interactive)
8404   (gnus-group-edit-group gnus-newsgroup-name 'params))
8405
8406 (defun gnus-summary-customize-parameters ()
8407   "Customize the group parameters of the current group."
8408   (interactive)
8409   (gnus-group-customize gnus-newsgroup-name))
8410
8411 (defun gnus-summary-enter-digest-group (&optional force)
8412   "Enter an nndoc group based on the current article.
8413 If FORCE, force a digest interpretation.  If not, try
8414 to guess what the document format is."
8415   (interactive "P")
8416   (let ((conf gnus-current-window-configuration))
8417     (save-window-excursion
8418       (save-excursion
8419         (let (gnus-article-prepare-hook
8420               gnus-display-mime-function
8421               gnus-break-pages)
8422           (gnus-summary-select-article))))
8423     (setq gnus-current-window-configuration conf)
8424     (let* ((name (format "%s-%d"
8425                          (gnus-group-prefixed-name
8426                           gnus-newsgroup-name (list 'nndoc ""))
8427                          (with-current-buffer gnus-summary-buffer
8428                            gnus-current-article)))
8429            (ogroup gnus-newsgroup-name)
8430            (params (append (gnus-info-params (gnus-get-info ogroup))
8431                            (list (cons 'to-group ogroup))
8432                            (list (cons 'parent-group ogroup))
8433                            (list (cons 'save-article-group ogroup))))
8434            (case-fold-search t)
8435            (buf (current-buffer))
8436            dig to-address)
8437       (save-excursion
8438         (set-buffer gnus-original-article-buffer)
8439         ;; Have the digest group inherit the main mail address of
8440         ;; the parent article.
8441         (when (setq to-address (or (gnus-fetch-field "reply-to")
8442                                    (gnus-fetch-field "from")))
8443           (setq params (append
8444                         (list (cons 'to-address
8445                                     (funcall gnus-decode-encoded-word-function
8446                                              to-address))))))
8447         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8448         (insert-buffer-substring gnus-original-article-buffer)
8449         ;; Remove lines that may lead nndoc to misinterpret the
8450         ;; document type.
8451         (narrow-to-region
8452          (goto-char (point-min))
8453          (or (search-forward "\n\n" nil t) (point)))
8454         (goto-char (point-min))
8455         (delete-matching-lines "^Path:\\|^From ")
8456         (widen))
8457       (unwind-protect
8458           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8459                     (gnus-newsgroup-ephemeral-ignored-charsets
8460                      gnus-newsgroup-ignored-charsets))
8461                 (gnus-group-read-ephemeral-group
8462                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8463                               (nndoc-article-type
8464                                ,(if force 'mbox 'guess)))
8465                  t nil nil nil
8466                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8467                                                         "ADAPT")))))
8468               ;; Make all postings to this group go to the parent group.
8469               (nconc (gnus-info-params (gnus-get-info name))
8470                      params)
8471             ;; Couldn't select this doc group.
8472             (switch-to-buffer buf)
8473             (gnus-set-global-variables)
8474             (gnus-configure-windows 'summary)
8475             (gnus-message 3 "Article couldn't be entered?"))
8476         (kill-buffer dig)))))
8477
8478 (defun gnus-summary-read-document (n)
8479   "Open a new group based on the current article(s).
8480 This will allow you to read digests and other similar
8481 documents as newsgroups.
8482 Obeys the standard process/prefix convention."
8483   (interactive "P")
8484   (let* ((ogroup gnus-newsgroup-name)
8485          (params (append (gnus-info-params (gnus-get-info ogroup))
8486                          (list (cons 'to-group ogroup))))
8487          group egroup groups vgroup)
8488     (dolist (article (gnus-summary-work-articles n))
8489       (setq group (format "%s-%d" gnus-newsgroup-name article))
8490       (gnus-summary-remove-process-mark article)
8491       (when (gnus-summary-display-article article)
8492         (save-excursion
8493           (with-temp-buffer
8494             (insert-buffer-substring gnus-original-article-buffer)
8495             ;; Remove some headers that may lead nndoc to make
8496             ;; the wrong guess.
8497             (message-narrow-to-head)
8498             (goto-char (point-min))
8499             (delete-matching-lines "^Path:\\|^From ")
8500             (widen)
8501             (if (setq egroup
8502                       (gnus-group-read-ephemeral-group
8503                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8504                                      (nndoc-article-type guess))
8505                        t nil t))
8506                 (progn
8507             ;; Make all postings to this group go to the parent group.
8508                   (nconc (gnus-info-params (gnus-get-info egroup))
8509                          params)
8510                   (push egroup groups))
8511               ;; Couldn't select this doc group.
8512               (gnus-error 3 "Article couldn't be entered"))))))
8513     ;; Now we have selected all the documents.
8514     (cond
8515      ((not groups)
8516       (error "None of the articles could be interpreted as documents"))
8517      ((gnus-group-read-ephemeral-group
8518        (setq vgroup (format
8519                      "nnvirtual:%s-%s" gnus-newsgroup-name
8520                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8521        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8522        t
8523        (cons (current-buffer) 'summary)))
8524      (t
8525       (error "Couldn't select virtual nndoc group")))))
8526
8527 (defun gnus-summary-isearch-article (&optional regexp-p)
8528   "Do incremental search forward on the current article.
8529 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8530   (interactive "P")
8531   (gnus-summary-select-article)
8532   (gnus-configure-windows 'article)
8533   (gnus-eval-in-buffer-window gnus-article-buffer
8534     (save-restriction
8535       (widen)
8536       (isearch-forward regexp-p))))
8537
8538 (defun gnus-summary-search-article-forward (regexp &optional backward)
8539   "Search for an article containing REGEXP forward.
8540 If BACKWARD, search backward instead."
8541   (interactive
8542    (list (read-string
8543           (format "Search article %s (regexp%s): "
8544                   (if current-prefix-arg "backward" "forward")
8545                   (if gnus-last-search-regexp
8546                       (concat ", default " gnus-last-search-regexp)
8547                     "")))
8548          current-prefix-arg))
8549   (if (string-equal regexp "")
8550       (setq regexp (or gnus-last-search-regexp ""))
8551     (setq gnus-last-search-regexp regexp)
8552     (setq gnus-article-before-search gnus-current-article))
8553   ;; Intentionally set gnus-last-article.
8554   (setq gnus-last-article gnus-article-before-search)
8555   (let ((gnus-last-article gnus-last-article))
8556     (if (gnus-summary-search-article regexp backward)
8557         (gnus-summary-show-thread)
8558       (signal 'search-failed (list regexp)))))
8559
8560 (defun gnus-summary-search-article-backward (regexp)
8561   "Search for an article containing REGEXP backward."
8562   (interactive
8563    (list (read-string
8564           (format "Search article backward (regexp%s): "
8565                   (if gnus-last-search-regexp
8566                       (concat ", default " gnus-last-search-regexp)
8567                     "")))))
8568   (gnus-summary-search-article-forward regexp 'backward))
8569
8570 (defun gnus-summary-search-article (regexp &optional backward)
8571   "Search for an article containing REGEXP.
8572 Optional argument BACKWARD means do search for backward.
8573 `gnus-select-article-hook' is not called during the search."
8574   ;; We have to require this here to make sure that the following
8575   ;; dynamic binding isn't shadowed by autoloading.
8576   (require 'gnus-async)
8577   (require 'gnus-art)
8578   (let ((gnus-select-article-hook nil)  ;Disable hook.
8579         (gnus-article-prepare-hook nil)
8580         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8581         (gnus-use-article-prefetch nil)
8582         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8583         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8584         (gnus-visual nil)
8585         (gnus-keep-backlog nil)
8586         (gnus-break-pages nil)
8587         (gnus-summary-display-arrow nil)
8588         (gnus-updated-mode-lines nil)
8589         (gnus-auto-center-summary nil)
8590         (sum (current-buffer))
8591         (gnus-display-mime-function nil)
8592         (found nil)
8593         point)
8594     (gnus-save-hidden-threads
8595       (gnus-summary-select-article)
8596       (set-buffer gnus-article-buffer)
8597       (goto-char (window-point (get-buffer-window (current-buffer))))
8598       (when backward
8599         (forward-line -1))
8600       (while (not found)
8601         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8602         (if (if backward
8603                 (re-search-backward regexp nil t)
8604               (re-search-forward regexp nil t))
8605             ;; We found the regexp.
8606             (progn
8607               (setq found 'found)
8608               (beginning-of-line)
8609               (set-window-start
8610                (get-buffer-window (current-buffer))
8611                (point))
8612               (forward-line 1)
8613               (set-window-point
8614                (get-buffer-window (current-buffer))
8615                (point))
8616               (set-buffer sum)
8617               (setq point (point)))
8618           ;; We didn't find it, so we go to the next article.
8619           (set-buffer sum)
8620           (setq found 'not)
8621           (while (eq found 'not)
8622             (if (not (if backward (gnus-summary-find-prev)
8623                        (gnus-summary-find-next)))
8624                 ;; No more articles.
8625                 (setq found t)
8626               ;; Select the next article and adjust point.
8627               (unless (gnus-summary-article-sparse-p
8628                        (gnus-summary-article-number))
8629                 (setq found nil)
8630                 (gnus-summary-select-article)
8631                 (set-buffer gnus-article-buffer)
8632                 (widen)
8633                 (goto-char (if backward (point-max) (point-min))))))))
8634       (gnus-message 7 ""))
8635     ;; Return whether we found the regexp.
8636     (when (eq found 'found)
8637       (goto-char point)
8638       (gnus-summary-show-thread)
8639       (gnus-summary-goto-subject gnus-current-article)
8640       (gnus-summary-position-point)
8641       t)))
8642
8643 (defun gnus-find-matching-articles (header regexp)
8644   "Return a list of all articles that match REGEXP on HEADER.
8645 This search includes all articles in the current group that Gnus has
8646 fetched headers for, whether they are displayed or not."
8647   (let ((articles nil)
8648         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8649         (case-fold-search t))
8650     (dolist (header gnus-newsgroup-headers)
8651       (when (string-match regexp (funcall func header))
8652         (push (mail-header-number header) articles)))
8653     (nreverse articles)))
8654
8655 (defun gnus-summary-find-matching (header regexp &optional backward unread
8656                                           not-case-fold not-matching)
8657   "Return a list of all articles that match REGEXP on HEADER.
8658 The search stars on the current article and goes forwards unless
8659 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8660 If UNREAD is non-nil, only unread articles will
8661 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8662 in the comparisons. If NOT-MATCHING, return a list of all articles that
8663 not match REGEXP on HEADER."
8664   (let ((case-fold-search (not not-case-fold))
8665         articles d func)
8666     (if (consp header)
8667         (if (eq (car header) 'extra)
8668             (setq func
8669                   `(lambda (h)
8670                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8671                          "")))
8672           (error "%s is an invalid header" header))
8673       (unless (fboundp (intern (concat "mail-header-" header)))
8674         (error "%s is not a valid header" header))
8675       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8676     (dolist (d (if (eq backward 'all)
8677                    gnus-newsgroup-data
8678                  (gnus-data-find-list
8679                   (gnus-summary-article-number)
8680                   (gnus-data-list backward))))
8681       (when (and (or (not unread)       ; We want all articles...
8682                      (gnus-data-unread-p d)) ; Or just unreads.
8683                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8684                  (if not-matching
8685                      (not (string-match
8686                            regexp
8687                            (funcall func (gnus-data-header d))))
8688                    (string-match regexp
8689                                  (funcall func (gnus-data-header d)))))
8690         (push (gnus-data-number d) articles))) ; Success!
8691     (nreverse articles)))
8692
8693 (defun gnus-summary-execute-command (header regexp command &optional backward)
8694   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8695 If HEADER is an empty string (or nil), the match is done on the entire
8696 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8697   (interactive
8698    (list (let ((completion-ignore-case t))
8699            (completing-read
8700             "Header name: "
8701             (mapcar (lambda (header) (list (format "%s" header)))
8702                     (append
8703                      '("Number" "Subject" "From" "Lines" "Date"
8704                        "Message-ID" "Xref" "References" "Body")
8705                      gnus-extra-headers))
8706             nil 'require-match))
8707          (read-string "Regexp: ")
8708          (read-key-sequence "Command: ")
8709          current-prefix-arg))
8710   (when (equal header "Body")
8711     (setq header ""))
8712   ;; Hidden thread subtrees must be searched as well.
8713   (gnus-summary-show-all-threads)
8714   ;; We don't want to change current point nor window configuration.
8715   (save-excursion
8716     (save-window-excursion
8717       (let (gnus-visual
8718             gnus-treat-strip-trailing-blank-lines
8719             gnus-treat-strip-leading-blank-lines
8720             gnus-treat-strip-multiple-blank-lines
8721             gnus-treat-hide-boring-headers
8722             gnus-treat-fold-newsgroups
8723             gnus-article-prepare-hook)
8724         (gnus-message 6 "Executing %s..." (key-description command))
8725         ;; We'd like to execute COMMAND interactively so as to give arguments.
8726         (gnus-execute header regexp
8727                       `(call-interactively ',(key-binding command))
8728                       backward)
8729         (gnus-message 6 "Executing %s...done" (key-description command))))))
8730
8731 (defun gnus-summary-beginning-of-article ()
8732   "Scroll the article back to the beginning."
8733   (interactive)
8734   (gnus-summary-select-article)
8735   (gnus-configure-windows 'article)
8736   (gnus-eval-in-buffer-window gnus-article-buffer
8737     (widen)
8738     (goto-char (point-min))
8739     (when gnus-break-pages
8740       (gnus-narrow-to-page))))
8741
8742 (defun gnus-summary-end-of-article ()
8743   "Scroll to the end of the article."
8744   (interactive)
8745   (gnus-summary-select-article)
8746   (gnus-configure-windows 'article)
8747   (gnus-eval-in-buffer-window gnus-article-buffer
8748     (widen)
8749     (goto-char (point-max))
8750     (recenter -3)
8751     (when gnus-break-pages
8752       (when (re-search-backward page-delimiter nil t)
8753         (narrow-to-region (match-end 0) (point-max)))
8754       (gnus-narrow-to-page))))
8755
8756 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8757   "Truncate to LEN and quote all \"(\"'s in STRING."
8758   (gnus-replace-in-string (if (and len (> (length string) len))
8759                               (substring string 0 len)
8760                             string)
8761                           "[()]" "\\\\\\&"))
8762
8763 (defun gnus-summary-print-article (&optional filename n)
8764   "Generate and print a PostScript image of the process-marked (mail) articles.
8765
8766 If used interactively, print the current article if none are
8767 process-marked.  With prefix arg, prompt the user for the name of the
8768 file to save in.
8769
8770 When used from Lisp, accept two optional args FILENAME and N.  N means
8771 to print the next N articles.  If N is negative, print the N previous
8772 articles.  If N is nil and articles have been marked with the process
8773 mark, print these instead.
8774
8775 If the optional first argument FILENAME is nil, send the image to the
8776 printer.  If FILENAME is a string, save the PostScript image in a file with
8777 that name.  If FILENAME is a number, prompt the user for the name of the file
8778 to save in."
8779   (interactive (list (ps-print-preprint current-prefix-arg)))
8780   (dolist (article (gnus-summary-work-articles n))
8781     (gnus-summary-select-article nil nil 'pseudo article)
8782     (gnus-eval-in-buffer-window gnus-article-buffer
8783       (gnus-print-buffer))
8784     (gnus-summary-remove-process-mark article))
8785   (ps-despool filename))
8786
8787 (defun gnus-print-buffer ()
8788   (let ((buffer (generate-new-buffer " *print*")))
8789     (unwind-protect
8790         (progn
8791           (copy-to-buffer buffer (point-min) (point-max))
8792           (set-buffer buffer)
8793           (gnus-remove-text-with-property 'gnus-decoration)
8794           (when (gnus-visual-p 'article-highlight 'highlight)
8795             ;; Copy-to-buffer doesn't copy overlay.  So redo
8796             ;; highlight.
8797             (let ((gnus-article-buffer buffer))
8798               (gnus-article-highlight-citation t)
8799               (gnus-article-highlight-signature)
8800               (gnus-article-emphasize)
8801               (gnus-article-delete-invisible-text)))
8802           (let ((ps-left-header
8803                  (list
8804                   (concat "("
8805                           (gnus-summary-print-truncate-and-quote
8806                            (mail-header-subject gnus-current-headers)
8807                            66) ")")
8808                   (concat "("
8809                           (gnus-summary-print-truncate-and-quote
8810                            (mail-header-from gnus-current-headers)
8811                            45) ")")))
8812                 (ps-right-header
8813                  (list
8814                   "/pagenumberstring load"
8815                   (concat "("
8816                           (mail-header-date gnus-current-headers) ")"))))
8817             (gnus-run-hooks 'gnus-ps-print-hook)
8818             (save-excursion
8819               (if window-system
8820                   (ps-spool-buffer-with-faces)
8821                 (ps-spool-buffer)))))
8822       (kill-buffer buffer))))
8823
8824 (defun gnus-summary-show-article (&optional arg)
8825   "Force redisplaying of the current article.
8826 If ARG (the prefix) is a number, show the article with the charset
8827 defined in `gnus-summary-show-article-charset-alist', or the charset
8828 input.
8829 If ARG (the prefix) is non-nil and not a number, show the raw article
8830 without any article massaging functions being run.  Normally, the key
8831 strokes are `C-u g'."
8832   (interactive "P")
8833   (cond
8834    ((numberp arg)
8835     (gnus-summary-show-article t)
8836     (let ((gnus-newsgroup-charset
8837            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8838                (mm-read-coding-system
8839                 "View as charset: " ;; actually it is coding system.
8840                 (with-current-buffer gnus-article-buffer
8841                   (mm-detect-coding-region (point) (point-max))))))
8842           (gnus-newsgroup-ignored-charsets 'gnus-all))
8843       (gnus-summary-select-article nil 'force)
8844       (let ((deps gnus-newsgroup-dependencies)
8845             head header lines)
8846         (save-excursion
8847           (set-buffer gnus-original-article-buffer)
8848           (save-restriction
8849             (message-narrow-to-head)
8850             (setq head (buffer-string))
8851             (goto-char (point-min))
8852             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8853               (goto-char (point-max))
8854               (widen)
8855               (setq lines (1- (count-lines (point) (point-max))))))
8856           (with-temp-buffer
8857             (insert (format "211 %d Article retrieved.\n"
8858                             (cdr gnus-article-current)))
8859             (insert head)
8860             (if lines (insert (format "Lines: %d\n" lines)))
8861             (insert ".\n")
8862             (let ((nntp-server-buffer (current-buffer)))
8863               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8864         (gnus-data-set-header
8865          (gnus-data-find (cdr gnus-article-current))
8866          header)
8867         (gnus-summary-update-article-line
8868          (cdr gnus-article-current) header)
8869         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8870           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8871    ((not arg)
8872     ;; Select the article the normal way.
8873     (gnus-summary-select-article nil 'force))
8874    (t
8875     ;; We have to require this here to make sure that the following
8876     ;; dynamic binding isn't shadowed by autoloading.
8877     (require 'gnus-async)
8878     (require 'gnus-art)
8879     ;; Bind the article treatment functions to nil.
8880     (let ((gnus-have-all-headers t)
8881           gnus-article-prepare-hook
8882           gnus-article-decode-hook
8883           gnus-display-mime-function
8884           gnus-break-pages)
8885       ;; Destroy any MIME parts.
8886       (when (gnus-buffer-live-p gnus-article-buffer)
8887         (save-excursion
8888           (set-buffer gnus-article-buffer)
8889           (mm-destroy-parts gnus-article-mime-handles)
8890           ;; Set it to nil for safety reason.
8891           (setq gnus-article-mime-handle-alist nil)
8892           (setq gnus-article-mime-handles nil)))
8893       (gnus-summary-select-article nil 'force))))
8894   (gnus-summary-goto-subject gnus-current-article)
8895   (gnus-summary-position-point))
8896
8897 (defun gnus-summary-show-raw-article ()
8898   "Show the raw article without any article massaging functions being run."
8899   (interactive)
8900   (gnus-summary-show-article t))
8901
8902 (defun gnus-summary-verbose-headers (&optional arg)
8903   "Toggle permanent full header display.
8904 If ARG is a positive number, turn header display on.
8905 If ARG is a negative number, turn header display off."
8906   (interactive "P")
8907   (setq gnus-show-all-headers
8908         (cond ((or (not (numberp arg))
8909                    (zerop arg))
8910                (not gnus-show-all-headers))
8911               ((natnump arg)
8912                t)))
8913   (gnus-summary-show-article))
8914
8915 (defun gnus-summary-toggle-header (&optional arg)
8916   "Show the headers if they are hidden, or hide them if they are shown.
8917 If ARG is a positive number, show the entire header.
8918 If ARG is a negative number, hide the unwanted header lines."
8919   (interactive "P")
8920   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8921                      (get-buffer-window gnus-article-buffer t))))
8922     (with-current-buffer gnus-article-buffer
8923       (widen)
8924       (article-narrow-to-head)
8925       (let* ((buffer-read-only nil)
8926              (inhibit-point-motion-hooks t)
8927              (hidden (if (numberp arg)
8928                          (>= arg 0)
8929                        (or (not (looking-at "[^ \t\n]+:"))
8930                            (gnus-article-hidden-text-p 'headers))))
8931              s e)
8932         (delete-region (point-min) (point-max))
8933         (with-current-buffer gnus-original-article-buffer
8934           (goto-char (setq s (point-min)))
8935           (setq e (if (search-forward "\n\n" nil t)
8936                       (1- (point))
8937                     (point-max))))
8938         (insert-buffer-substring gnus-original-article-buffer s e)
8939         (run-hooks 'gnus-article-decode-hook)
8940         (if hidden
8941             (let ((gnus-treat-hide-headers nil)
8942                   (gnus-treat-hide-boring-headers nil))
8943               (gnus-delete-wash-type 'headers)
8944               (gnus-treat-article 'head))
8945           (gnus-treat-article 'head))
8946         (widen)
8947         (if window
8948             (set-window-start window (goto-char (point-min))))
8949         (if gnus-break-pages
8950             (gnus-narrow-to-page)
8951           (when (gnus-visual-p 'page-marker)
8952             (let ((buffer-read-only nil))
8953               (gnus-remove-text-with-property 'gnus-prev)
8954               (gnus-remove-text-with-property 'gnus-next))))
8955         (gnus-set-mode-line 'article)))))
8956
8957 (defun gnus-summary-show-all-headers ()
8958   "Make all header lines visible."
8959   (interactive)
8960   (gnus-summary-toggle-header 1))
8961
8962 (defun gnus-summary-caesar-message (&optional arg)
8963   "Caesar rotate the current article by 13.
8964 The numerical prefix specifies how many places to rotate each letter
8965 forward."
8966   (interactive "P")
8967   (gnus-summary-select-article)
8968   (let ((mail-header-separator ""))
8969     (gnus-eval-in-buffer-window gnus-article-buffer
8970       (save-restriction
8971         (widen)
8972         (let ((start (window-start))
8973               buffer-read-only)
8974           (message-caesar-buffer-body arg)
8975           (set-window-start (get-buffer-window (current-buffer)) start)))))
8976   ;; Create buttons and stuff...
8977   (gnus-treat-article nil))
8978
8979 (defun gnus-summary-idna-message (&optional arg)
8980   "Decode IDNA encoded domain names in the current articles.
8981 IDNA encoded domain names looks like `xn--bar'.  If a string
8982 remain unencoded after running this function, it is likely an
8983 invalid IDNA string (`xn--bar' is invalid).
8984
8985 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
8986 installed for this command to work."
8987   (interactive "P")
8988   (if (not (and (condition-case nil (require 'idna)
8989                   (file-error))
8990                 (mm-coding-system-p 'utf-8)
8991                 (executable-find (symbol-value 'idna-program))))
8992       (gnus-message
8993        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
8994     (gnus-summary-select-article)
8995     (let ((mail-header-separator ""))
8996       (gnus-eval-in-buffer-window gnus-article-buffer
8997         (save-restriction
8998           (widen)
8999           (let ((start (window-start))
9000                 buffer-read-only)
9001             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9002               (replace-match (idna-to-unicode (match-string 1))))
9003             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9004
9005 (autoload 'unmorse-region "morse"
9006   "Convert morse coded text in region to ordinary ASCII text."
9007   t)
9008
9009 (defun gnus-summary-morse-message (&optional arg)
9010   "Morse decode the current article."
9011   (interactive "P")
9012   (gnus-summary-select-article)
9013   (let ((mail-header-separator ""))
9014     (gnus-eval-in-buffer-window gnus-article-buffer
9015       (save-excursion
9016         (save-restriction
9017           (widen)
9018           (let ((pos (window-start))
9019                 buffer-read-only)
9020             (goto-char (point-min))
9021             (when (message-goto-body)
9022               (gnus-narrow-to-body))
9023             (goto-char (point-min))
9024             (while (search-forward "·" (point-max) t)
9025               (replace-match "."))
9026             (unmorse-region (point-min) (point-max))
9027             (widen)
9028             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9029
9030 (defun gnus-summary-stop-page-breaking ()
9031   "Stop page breaking in the current article."
9032   (interactive)
9033   (gnus-summary-select-article)
9034   (gnus-eval-in-buffer-window gnus-article-buffer
9035     (widen)
9036     (when (gnus-visual-p 'page-marker)
9037       (let ((buffer-read-only nil))
9038         (gnus-remove-text-with-property 'gnus-prev)
9039         (gnus-remove-text-with-property 'gnus-next))
9040       (setq gnus-page-broken nil))))
9041
9042 (defun gnus-summary-move-article (&optional n to-newsgroup
9043                                             select-method action)
9044   "Move the current article to a different newsgroup.
9045 If N is a positive number, move the N next articles.
9046 If N is a negative number, move the N previous articles.
9047 If N is nil and any articles have been marked with the process mark,
9048 move those articles instead.
9049 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9050 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9051 re-spool using this method.
9052
9053 When called interactively with TO-NEWSGROUP being nil, the value of
9054 the variable `gnus-move-split-methods' is used for finding a default
9055 for the target newsgroup.
9056
9057 For this function to work, both the current newsgroup and the
9058 newsgroup that you want to move to have to support the `request-move'
9059 and `request-accept' functions.
9060
9061 ACTION can be either `move' (the default), `crosspost' or `copy'."
9062   (interactive "P")
9063   (unless action
9064     (setq action 'move))
9065   ;; Check whether the source group supports the required functions.
9066   (cond ((and (eq action 'move)
9067               (not (gnus-check-backend-function
9068                     'request-move-article gnus-newsgroup-name)))
9069          (error "The current group does not support article moving"))
9070         ((and (eq action 'crosspost)
9071               (not (gnus-check-backend-function
9072                     'request-replace-article gnus-newsgroup-name)))
9073          (error "The current group does not support article editing")))
9074   (let ((articles (gnus-summary-work-articles n))
9075         (prefix (if (gnus-check-backend-function
9076                      'request-move-article gnus-newsgroup-name)
9077                     (funcall gnus-move-group-prefix-function
9078                              gnus-newsgroup-name)
9079                   ""))
9080         (names '((move "Move" "Moving")
9081                  (copy "Copy" "Copying")
9082                  (crosspost "Crosspost" "Crossposting")))
9083         (copy-buf (save-excursion
9084                     (nnheader-set-temp-buffer " *copy article*")))
9085         art-group to-method new-xref article to-groups articles-to-update-marks)
9086     (unless (assq action names)
9087       (error "Unknown action %s" action))
9088     ;; Read the newsgroup name.
9089     (when (and (not to-newsgroup)
9090                (not select-method))
9091       (if (and gnus-move-split-methods
9092                (not
9093                 (and (memq gnus-current-article articles)
9094                      (gnus-buffer-live-p gnus-original-article-buffer))))
9095           ;; When `gnus-move-split-methods' is non-nil, we have to
9096           ;; select an article to give `gnus-read-move-group-name' an
9097           ;; opportunity to suggest an appropriate default.  However,
9098           ;; we needn't render or mark the article.
9099           (let ((gnus-display-mime-function nil)
9100                 (gnus-article-prepare-hook nil)
9101                 (gnus-mark-article-hook nil))
9102             (gnus-summary-select-article nil nil nil (car articles))))
9103       (setq to-newsgroup
9104             (gnus-read-move-group-name
9105              (cadr (assq action names))
9106              (symbol-value (intern (format "gnus-current-%s-group" action)))
9107              articles prefix))
9108       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9109     (setq to-method (or select-method
9110                         (gnus-server-to-method
9111                          (gnus-group-method to-newsgroup))))
9112     ;; Check the method we are to move this article to...
9113     (unless (gnus-check-backend-function
9114              'request-accept-article (car to-method))
9115       (error "%s does not support article copying" (car to-method)))
9116     (unless (gnus-check-server to-method)
9117       (error "Can't open server %s" (car to-method)))
9118     (gnus-message 6 "%s to %s: %s..."
9119                   (caddr (assq action names))
9120                   (or (car select-method) to-newsgroup) articles)
9121     (while articles
9122       (setq article (pop articles))
9123       (setq
9124        art-group
9125        (cond
9126         ;; Move the article.
9127         ((eq action 'move)
9128          ;; Remove this article from future suppression.
9129          (gnus-dup-unsuppress-article article)
9130          (let* ((from-method (gnus-find-method-for-group
9131                               gnus-newsgroup-name))
9132                 (to-method (gnus-find-method-for-group
9133                             to-newsgroup))
9134                 (move-is-internal (gnus-method-equal from-method to-method)))
9135          (gnus-request-move-article
9136           article                       ; Article to move
9137           gnus-newsgroup-name           ; From newsgroup
9138           (nth 1 (gnus-find-method-for-group
9139                   gnus-newsgroup-name)) ; Server
9140           (list 'gnus-request-accept-article
9141                 to-newsgroup (list 'quote select-method)
9142                 (not articles) t)       ; Accept form
9143           (not articles)                ; Only save nov last time
9144           move-is-internal)))           ; is this move internal?
9145         ;; Copy the article.
9146         ((eq action 'copy)
9147          (save-excursion
9148            (set-buffer copy-buf)
9149            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9150              (save-restriction
9151                (nnheader-narrow-to-headers)
9152                (dolist (hdr gnus-copy-article-ignored-headers)
9153                  (message-remove-header hdr t)))
9154              (gnus-request-accept-article
9155               to-newsgroup select-method (not articles) t))))
9156         ;; Crosspost the article.
9157         ((eq action 'crosspost)
9158          (let ((xref (message-tokenize-header
9159                       (mail-header-xref (gnus-summary-article-header article))
9160                       " ")))
9161            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9162                                   ":" (number-to-string article)))
9163            (unless xref
9164              (setq xref (list (system-name))))
9165            (setq new-xref
9166                  (concat
9167                   (mapconcat 'identity
9168                              (delete "Xref:" (delete new-xref xref))
9169                              " ")
9170                   " " new-xref))
9171            (save-excursion
9172              (set-buffer copy-buf)
9173              ;; First put the article in the destination group.
9174              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9175              (when (consp (setq art-group
9176                                 (gnus-request-accept-article
9177                                  to-newsgroup select-method (not articles))))
9178                (setq new-xref (concat new-xref " " (car art-group)
9179                                       ":"
9180                                       (number-to-string (cdr art-group))))
9181                ;; Now we have the new Xrefs header, so we insert
9182                ;; it and replace the new article.
9183                (nnheader-replace-header "Xref" new-xref)
9184                (gnus-request-replace-article
9185                 (cdr art-group) to-newsgroup (current-buffer))
9186                art-group))))))
9187       (cond
9188        ((not art-group)
9189         (gnus-message 1 "Couldn't %s article %s: %s"
9190                       (cadr (assq action names)) article
9191                       (nnheader-get-report (car to-method))))
9192        ((eq art-group 'junk)
9193         (when (eq action 'move)
9194           (gnus-summary-mark-article article gnus-canceled-mark)
9195           (gnus-message 4 "Deleted article %s" article)
9196           ;; run the delete hook
9197           (run-hook-with-args 'gnus-summary-article-delete-hook
9198                               action
9199                               (gnus-data-header
9200                                (assoc article (gnus-data-list nil)))
9201                               gnus-newsgroup-name nil
9202                               select-method)))
9203        (t
9204         (let* ((pto-group (gnus-group-prefixed-name
9205                            (car art-group) to-method))
9206                (info (gnus-get-info pto-group))
9207                (to-group (gnus-info-group info))
9208                to-marks)
9209           ;; Update the group that has been moved to.
9210           (when (and info
9211                      (memq action '(move copy)))
9212             (unless (member to-group to-groups)
9213               (push to-group to-groups))
9214
9215             (unless (memq article gnus-newsgroup-unreads)
9216               (push 'read to-marks)
9217               (gnus-info-set-read
9218                info (gnus-add-to-range (gnus-info-read info)
9219                                        (list (cdr art-group)))))
9220
9221             ;; See whether the article is to be put in the cache.
9222             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9223                              gnus-article-mark-lists
9224                            (delete '(expirable . expire)
9225                                    (copy-sequence gnus-article-mark-lists))))
9226                   (to-article (cdr art-group)))
9227
9228               ;; Enter the article into the cache in the new group,
9229               ;; if that is required.
9230               (when gnus-use-cache
9231                 (gnus-cache-possibly-enter-article
9232                  to-group to-article
9233                  (memq article gnus-newsgroup-marked)
9234                  (memq article gnus-newsgroup-dormant)
9235                  (memq article gnus-newsgroup-unreads)))
9236
9237               (when gnus-preserve-marks
9238                 ;; Copy any marks over to the new group.
9239                 (when (and (equal to-group gnus-newsgroup-name)
9240                            (not (memq article gnus-newsgroup-unreads)))
9241                   ;; Mark this article as read in this group.
9242                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9243                   (setcdr (gnus-active to-group) to-article)
9244                   (setcdr gnus-newsgroup-active to-article))
9245
9246                 (while marks
9247                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9248                     (when (memq article (symbol-value
9249                                          (intern (format "gnus-newsgroup-%s"
9250                                                          (caar marks)))))
9251                       (push (cdar marks) to-marks)
9252                       ;; If the other group is the same as this group,
9253                       ;; then we have to add the mark to the list.
9254                       (when (equal to-group gnus-newsgroup-name)
9255                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9256                              (cons to-article
9257                                    (symbol-value
9258                                     (intern (format "gnus-newsgroup-%s"
9259                                                     (caar marks)))))))
9260                       ;; Copy the marks to other group.
9261                       (gnus-add-marked-articles
9262                        to-group (cdar marks) (list to-article) info)))
9263                   (setq marks (cdr marks)))
9264
9265                 (gnus-request-set-mark
9266                  to-group (list (list (list to-article) 'add to-marks))))
9267
9268               (gnus-dribble-enter
9269                (concat "(gnus-group-set-info '"
9270                        (gnus-prin1-to-string (gnus-get-info to-group))
9271                        ")"))))
9272
9273           ;; Update the Xref header in this article to point to
9274           ;; the new crossposted article we have just created.
9275           (when (eq action 'crosspost)
9276             (save-excursion
9277               (set-buffer copy-buf)
9278               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9279               (nnheader-replace-header "Xref" new-xref)
9280               (gnus-request-replace-article
9281                article gnus-newsgroup-name (current-buffer))))
9282
9283           ;; run the move/copy/crosspost/respool hook
9284           (run-hook-with-args 'gnus-summary-article-move-hook
9285                               action
9286                               (gnus-data-header
9287                                (assoc article (gnus-data-list nil)))
9288                               gnus-newsgroup-name
9289                               to-newsgroup
9290                               select-method))
9291
9292         ;;;!!!Why is this necessary?
9293         (set-buffer gnus-summary-buffer)
9294         
9295         (gnus-summary-goto-subject article)
9296         (when (eq action 'move)
9297           (gnus-summary-mark-article article gnus-canceled-mark))))
9298       (push article articles-to-update-marks))
9299
9300     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9301     ;; Re-activate all groups that have been moved to.
9302     (save-excursion
9303       (set-buffer gnus-group-buffer)
9304       (let ((gnus-group-marked to-groups))
9305         (gnus-group-get-new-news-this-group nil t)))
9306     
9307     (gnus-kill-buffer copy-buf)
9308     (gnus-summary-position-point)
9309     (gnus-set-mode-line 'summary)))
9310
9311 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9312   "Copy the current article to some other group.
9313 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9314 When called interactively, if TO-NEWSGROUP is nil, use the value of
9315 the variable `gnus-move-split-methods' for finding a default target
9316 newsgroup.
9317 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9318 re-spool using this method."
9319   (interactive "P")
9320   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9321
9322 (defun gnus-summary-crosspost-article (&optional n)
9323   "Crosspost the current article to some other group."
9324   (interactive "P")
9325   (gnus-summary-move-article n nil nil 'crosspost))
9326
9327 (defcustom gnus-summary-respool-default-method nil
9328   "Default method type for respooling an article.
9329 If nil, use to the current newsgroup method."
9330   :type 'symbol
9331   :group 'gnus-summary-mail)
9332
9333 (defcustom gnus-summary-display-while-building nil
9334   "If non-nil, show and update the summary buffer as it's being built.
9335 If the value is t, update the buffer after every line is inserted.  If
9336 the value is an integer (N), update the display every N lines."
9337   :version "22.1"
9338   :group 'gnus-thread
9339   :type '(choice (const :tag "off" nil)
9340                  number
9341                  (const :tag "frequently" t)))
9342
9343 (defun gnus-summary-respool-article (&optional n method)
9344   "Respool the current article.
9345 The article will be squeezed through the mail spooling process again,
9346 which means that it will be put in some mail newsgroup or other
9347 depending on `nnmail-split-methods'.
9348 If N is a positive number, respool the N next articles.
9349 If N is a negative number, respool the N previous articles.
9350 If N is nil and any articles have been marked with the process mark,
9351 respool those articles instead.
9352
9353 Respooling can be done both from mail groups and \"real\" newsgroups.
9354 In the former case, the articles in question will be moved from the
9355 current group into whatever groups they are destined to.  In the
9356 latter case, they will be copied into the relevant groups."
9357   (interactive
9358    (list current-prefix-arg
9359          (let* ((methods (gnus-methods-using 'respool))
9360                 (methname
9361                  (symbol-name (or gnus-summary-respool-default-method
9362                                   (car (gnus-find-method-for-group
9363                                         gnus-newsgroup-name)))))
9364                 (method
9365                  (gnus-completing-read-with-default
9366                   methname "What backend do you want to use when respooling?"
9367                   methods nil t nil 'gnus-mail-method-history))
9368                 ms)
9369            (cond
9370             ((zerop (length (setq ms (gnus-servers-using-backend
9371                                       (intern method)))))
9372              (list (intern method) ""))
9373             ((= 1 (length ms))
9374              (car ms))
9375             (t
9376              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9377                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9378                            ms-alist))))))))
9379   (unless method
9380     (error "No method given for respooling"))
9381   (if (assoc (symbol-name
9382               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9383              (gnus-methods-using 'respool))
9384       (gnus-summary-move-article n nil method)
9385     (gnus-summary-copy-article n nil method)))
9386
9387 (defun gnus-summary-import-article (file &optional edit)
9388   "Import an arbitrary file into a mail newsgroup."
9389   (interactive "fImport file: \nP")
9390   (let ((group gnus-newsgroup-name)
9391         (now (current-time))
9392         atts lines group-art)
9393     (unless (gnus-check-backend-function 'request-accept-article group)
9394       (error "%s does not support article importing" group))
9395     (or (file-readable-p file)
9396         (not (file-regular-p file))
9397         (error "Can't read %s" file))
9398     (save-excursion
9399       (set-buffer (gnus-get-buffer-create " *import file*"))
9400       (erase-buffer)
9401       (nnheader-insert-file-contents file)
9402       (goto-char (point-min))
9403       (if (nnheader-article-p)
9404           (save-restriction
9405             (goto-char (point-min))
9406             (search-forward "\n\n" nil t)
9407             (narrow-to-region (point-min) (1- (point)))
9408             (goto-char (point-min))
9409             (unless (re-search-forward "^date:" nil t)
9410               (goto-char (point-max))
9411               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9412        ;; This doesn't look like an article, so we fudge some headers.
9413         (setq atts (file-attributes file)
9414               lines (count-lines (point-min) (point-max)))
9415         (insert "From: " (read-string "From: ") "\n"
9416                 "Subject: " (read-string "Subject: ") "\n"
9417                 "Date: " (message-make-date (nth 5 atts)) "\n"
9418                 "Message-ID: " (message-make-message-id) "\n"
9419                 "Lines: " (int-to-string lines) "\n"
9420                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9421       (setq group-art (gnus-request-accept-article group nil t))
9422       (kill-buffer (current-buffer)))
9423     (setq gnus-newsgroup-active (gnus-activate-group group))
9424     (forward-line 1)
9425     (gnus-summary-goto-article (cdr group-art) nil t)
9426     (when edit
9427       (gnus-summary-edit-article))))
9428
9429 (defun gnus-summary-create-article ()
9430   "Create an article in a mail newsgroup."
9431   (interactive)
9432   (let ((group gnus-newsgroup-name)
9433         (now (current-time))
9434         group-art)
9435     (unless (gnus-check-backend-function 'request-accept-article group)
9436       (error "%s does not support article importing" group))
9437     (save-excursion
9438       (set-buffer (gnus-get-buffer-create " *import file*"))
9439       (erase-buffer)
9440       (goto-char (point-min))
9441       ;; This doesn't look like an article, so we fudge some headers.
9442       (insert "From: " (read-string "From: ") "\n"
9443               "Subject: " (read-string "Subject: ") "\n"
9444               "Date: " (message-make-date now) "\n"
9445               "Message-ID: " (message-make-message-id) "\n")
9446       (setq group-art (gnus-request-accept-article group nil t))
9447       (kill-buffer (current-buffer)))
9448     (setq gnus-newsgroup-active (gnus-activate-group group))
9449     (forward-line 1)
9450     (gnus-summary-goto-article (cdr group-art) nil t)
9451     (gnus-summary-edit-article)))
9452
9453 (defun gnus-summary-article-posted-p ()
9454   "Say whether the current (mail) article is available from news as well.
9455 This will be the case if the article has both been mailed and posted."
9456   (interactive)
9457   (let ((id (mail-header-references (gnus-summary-article-header)))
9458         (gnus-override-method (car (gnus-refer-article-methods))))
9459     (if (gnus-request-head id "")
9460         (gnus-message 2 "The current message was found on %s"
9461                       gnus-override-method)
9462       (gnus-message 2 "The current message couldn't be found on %s"
9463                     gnus-override-method)
9464       nil)))
9465
9466 (defun gnus-summary-expire-articles (&optional now)
9467   "Expire all articles that are marked as expirable in the current group."
9468   (interactive)
9469   (when (and (not gnus-group-is-exiting-without-update-p)
9470              (gnus-check-backend-function
9471               'request-expire-articles gnus-newsgroup-name))
9472     ;; This backend supports expiry.
9473     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9474            (expirable (if total
9475                           (progn
9476                             ;; We need to update the info for
9477                             ;; this group for `gnus-list-of-read-articles'
9478                             ;; to give us the right answer.
9479                             (gnus-run-hooks 'gnus-exit-group-hook)
9480                             (gnus-summary-update-info)
9481                             (gnus-list-of-read-articles gnus-newsgroup-name))
9482                         (setq gnus-newsgroup-expirable
9483                               (sort gnus-newsgroup-expirable '<))))
9484            (expiry-wait (if now 'immediate
9485                           (gnus-group-find-parameter
9486                            gnus-newsgroup-name 'expiry-wait)))
9487            (nnmail-expiry-target
9488             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9489                 nnmail-expiry-target))
9490            es)
9491       (when expirable
9492         ;; There are expirable articles in this group, so we run them
9493         ;; through the expiry process.
9494         (gnus-message 6 "Expiring articles...")
9495         (unless (gnus-check-group gnus-newsgroup-name)
9496           (error "Can't open server for %s" gnus-newsgroup-name))
9497         ;; The list of articles that weren't expired is returned.
9498         (save-excursion
9499           (if expiry-wait
9500               (let ((nnmail-expiry-wait-function nil)
9501                     (nnmail-expiry-wait expiry-wait))
9502                 (setq es (gnus-request-expire-articles
9503                           expirable gnus-newsgroup-name)))
9504             (setq es (gnus-request-expire-articles
9505                       expirable gnus-newsgroup-name)))
9506           (unless total
9507             (setq gnus-newsgroup-expirable es))
9508           ;; We go through the old list of expirable, and mark all
9509           ;; really expired articles as nonexistent.
9510           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9511             (let ((gnus-use-cache nil))
9512               (dolist (article expirable)
9513                 (when (and (not (memq article es))
9514                            (gnus-data-find article))
9515                   (gnus-summary-mark-article article gnus-canceled-mark)
9516                   (run-hook-with-args 'gnus-summary-article-expire-hook
9517                                       'delete
9518                                       (gnus-data-header
9519                                        (assoc article (gnus-data-list nil)))
9520                                       gnus-newsgroup-name
9521                                       nil
9522                                       nil))))))
9523         (gnus-message 6 "Expiring articles...done")))))
9524
9525 (defun gnus-summary-expire-articles-now ()
9526   "Expunge all expirable articles in the current group.
9527 This means that *all* articles that are marked as expirable will be
9528 deleted forever, right now."
9529   (interactive)
9530   (or gnus-expert-user
9531       (gnus-yes-or-no-p
9532        "Are you really, really, really sure you want to delete all these messages? ")
9533       (error "Phew!"))
9534   (gnus-summary-expire-articles t))
9535
9536 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9537 (defun gnus-summary-delete-article (&optional n)
9538   "Delete the N next (mail) articles.
9539 This command actually deletes articles.  This is not a marking
9540 command.  The article will disappear forever from your life, never to
9541 return.
9542
9543 If N is negative, delete backwards.
9544 If N is nil and articles have been marked with the process mark,
9545 delete these instead.
9546
9547 If `gnus-novice-user' is non-nil you will be asked for
9548 confirmation before the articles are deleted."
9549   (interactive "P")
9550   (unless (gnus-check-backend-function 'request-expire-articles
9551                                        gnus-newsgroup-name)
9552     (error "The current newsgroup does not support article deletion"))
9553   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9554     (error "Couldn't open server"))
9555   ;; Compute the list of articles to delete.
9556   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9557         (nnmail-expiry-target 'delete)
9558         not-deleted)
9559     (if (and gnus-novice-user
9560              (not (gnus-yes-or-no-p
9561                    (format "Do you really want to delete %s forever? "
9562                            (if (> (length articles) 1)
9563                                (format "these %s articles" (length articles))
9564                              "this article")))))
9565         ()
9566       ;; Delete the articles.
9567       (setq not-deleted (gnus-request-expire-articles
9568                          articles gnus-newsgroup-name 'force))
9569       (while articles
9570         (gnus-summary-remove-process-mark (car articles))
9571         ;; The backend might not have been able to delete the article
9572         ;; after all.
9573         (unless (memq (car articles) not-deleted)
9574           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9575         (let* ((article (car articles))
9576                (ghead  (gnus-data-header
9577                                     (assoc article (gnus-data-list nil)))))
9578           (run-hook-with-args 'gnus-summary-article-delete-hook
9579                               'delete ghead gnus-newsgroup-name nil
9580                               nil))
9581         (setq articles (cdr articles)))
9582       (when not-deleted
9583         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9584     (gnus-summary-position-point)
9585     (gnus-set-mode-line 'summary)
9586     not-deleted))
9587
9588 (defun gnus-summary-edit-article (&optional arg)
9589   "Edit the current article.
9590 This will have permanent effect only in mail groups.
9591 If ARG is nil, edit the decoded articles.
9592 If ARG is 1, edit the raw articles.
9593 If ARG is 2, edit the raw articles even in read-only groups.
9594 If ARG is 3, edit the articles with the current handles.
9595 Otherwise, allow editing of articles even in read-only
9596 groups."
9597   (interactive "P")
9598   (let (force raw current-handles)
9599     (cond
9600      ((null arg))
9601      ((eq arg 1)
9602       (setq raw t))
9603      ((eq arg 2)
9604       (setq raw t
9605             force t))
9606      ((eq arg 3)
9607       (setq current-handles
9608             (and (gnus-buffer-live-p gnus-article-buffer)
9609                  (with-current-buffer gnus-article-buffer
9610                    (prog1
9611                        gnus-article-mime-handles
9612                      (setq gnus-article-mime-handles nil))))))
9613      (t
9614       (setq force t)))
9615     (when (and raw (not force)
9616                (member gnus-newsgroup-name '("nndraft:delayed"
9617                                              "nndraft:drafts"
9618                                              "nndraft:queue")))
9619       (error "Can't edit the raw article in group %s"
9620              gnus-newsgroup-name))
9621     (save-excursion
9622       (set-buffer gnus-summary-buffer)
9623       (let ((mail-parse-charset gnus-newsgroup-charset)
9624             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9625         (gnus-set-global-variables)
9626         (when (and (not force)
9627                    (gnus-group-read-only-p))
9628           (error "The current newsgroup does not support article editing"))
9629         (gnus-summary-show-article t)
9630         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9631           (with-current-buffer gnus-article-buffer
9632             (mm-enable-multibyte)))
9633         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9634             (setq raw t))
9635         (gnus-article-edit-article
9636          (if raw 'ignore
9637            `(lambda ()
9638               (let ((mbl mml-buffer-list))
9639                 (setq mml-buffer-list nil)
9640                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9641                   (mime-to-mml ,'current-handles))
9642                 (let ((mbl1 mml-buffer-list))
9643                   (setq mml-buffer-list mbl)
9644                   (set (make-local-variable 'mml-buffer-list) mbl1))
9645                 (gnus-make-local-hook 'kill-buffer-hook)
9646                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9647          `(lambda (no-highlight)
9648             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9649                   (message-options message-options)
9650                   (message-options-set-recipient)
9651                   (mail-parse-ignored-charsets
9652                    ',gnus-newsgroup-ignored-charsets))
9653               ,(if (not raw) '(progn
9654                                 (mml-to-mime)
9655                                 (mml-destroy-buffers)
9656                                 (remove-hook 'kill-buffer-hook
9657                                              'mml-destroy-buffers t)
9658                                 (kill-local-variable 'mml-buffer-list)))
9659               (gnus-summary-edit-article-done
9660                ,(or (mail-header-references gnus-current-headers) "")
9661                ,(gnus-group-read-only-p)
9662                ,gnus-summary-buffer no-highlight))))))))
9663
9664 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9665
9666 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9667                                                  no-highlight)
9668   "Make edits to the current article permanent."
9669   (interactive)
9670   (save-excursion
9671    ;; The buffer restriction contains the entire article if it exists.
9672     (when (article-goto-body)
9673       (let ((lines (count-lines (point) (point-max)))
9674             (length (- (point-max) (point)))
9675             (case-fold-search t)
9676             (body (copy-marker (point))))
9677         (goto-char (point-min))
9678         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9679           (delete-region (match-beginning 1) (match-end 1))
9680           (insert (number-to-string length)))
9681         (goto-char (point-min))
9682         (when (re-search-forward
9683                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9684           (delete-region (match-beginning 1) (match-end 1))
9685           (insert (number-to-string length)))
9686         (goto-char (point-min))
9687         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9688           (delete-region (match-beginning 1) (match-end 1))
9689           (insert (number-to-string lines))))))
9690   ;; Replace the article.
9691   (let ((buf (current-buffer)))
9692     (with-temp-buffer
9693       (insert-buffer-substring buf)
9694
9695       (if (and (not read-only)
9696                (not (gnus-request-replace-article
9697                      (cdr gnus-article-current) (car gnus-article-current)
9698                      (current-buffer) t)))
9699           (error "Couldn't replace article")
9700         ;; Update the summary buffer.
9701         (if (and references
9702                  (equal (message-tokenize-header references " ")
9703                         (message-tokenize-header
9704                          (or (message-fetch-field "references") "") " ")))
9705             ;; We only have to update this line.
9706             (save-excursion
9707               (save-restriction
9708                 (message-narrow-to-head)
9709                 (let ((head (buffer-string))
9710                       header)
9711                   (with-temp-buffer
9712                     (insert (format "211 %d Article retrieved.\n"
9713                                     (cdr gnus-article-current)))
9714                     (insert head)
9715                     (insert ".\n")
9716                     (let ((nntp-server-buffer (current-buffer)))
9717                       (setq header (car (gnus-get-newsgroup-headers
9718                                          nil t))))
9719                     (save-excursion
9720                       (set-buffer gnus-summary-buffer)
9721                       (gnus-data-set-header
9722                        (gnus-data-find (cdr gnus-article-current))
9723                        header)
9724                       (gnus-summary-update-article-line
9725                        (cdr gnus-article-current) header)
9726                       (if (gnus-summary-goto-subject
9727                            (cdr gnus-article-current) nil t)
9728                           (gnus-summary-update-secondary-mark
9729                            (cdr gnus-article-current))))))))
9730           ;; Update threads.
9731           (set-buffer (or buffer gnus-summary-buffer))
9732           (gnus-summary-update-article (cdr gnus-article-current))
9733           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9734               (gnus-summary-update-secondary-mark
9735                (cdr gnus-article-current))))
9736         ;; Prettify the article buffer again.
9737         (unless no-highlight
9738           (save-excursion
9739             (set-buffer gnus-article-buffer)
9740             ;;;!!! Fix this -- article should be rehighlighted.
9741             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9742             (set-buffer gnus-original-article-buffer)
9743             (gnus-request-article
9744              (cdr gnus-article-current)
9745              (car gnus-article-current) (current-buffer))))
9746         ;; Prettify the summary buffer line.
9747         (when (gnus-visual-p 'summary-highlight 'highlight)
9748           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9749
9750 (defun gnus-summary-edit-wash (key)
9751   "Perform editing command KEY in the article buffer."
9752   (interactive
9753    (list
9754     (progn
9755       (message "%s" (concat (this-command-keys) "- "))
9756       (read-char))))
9757   (message "")
9758   (gnus-summary-edit-article)
9759   (execute-kbd-macro (concat (this-command-keys) key))
9760   (gnus-article-edit-done))
9761
9762 ;;; Respooling
9763
9764 (defun gnus-summary-respool-query (&optional silent trace)
9765   "Query where the respool algorithm would put this article."
9766   (interactive)
9767   (let (gnus-mark-article-hook)
9768     (gnus-summary-select-article)
9769     (save-excursion
9770       (set-buffer gnus-original-article-buffer)
9771       (let ((groups (nnmail-article-group 'identity trace)))
9772         (unless silent
9773           (if groups
9774               (message "This message would go to %s"
9775                        (mapconcat 'car groups ", "))
9776             (message "This message would go to no groups"))
9777           groups)))))
9778
9779 (defun gnus-summary-respool-trace ()
9780   "Trace where the respool algorithm would put this article.
9781 Display a buffer showing all fancy splitting patterns which matched."
9782   (interactive)
9783   (gnus-summary-respool-query nil t))
9784
9785 ;; Summary marking commands.
9786
9787 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9788   "Mark articles which has the same subject as read, and then select the next.
9789 If UNMARK is positive, remove any kind of mark.
9790 If UNMARK is negative, tick articles."
9791   (interactive "P")
9792   (when unmark
9793     (setq unmark (prefix-numeric-value unmark)))
9794   (let ((count
9795          (gnus-summary-mark-same-subject
9796           (gnus-summary-article-subject) unmark)))
9797     ;; Select next unread article.  If auto-select-same mode, should
9798     ;; select the first unread article.
9799     (gnus-summary-next-article t (and gnus-auto-select-same
9800                                       (gnus-summary-article-subject)))
9801     (gnus-message 7 "%d article%s marked as %s"
9802                   count (if (= count 1) " is" "s are")
9803                   (if unmark "unread" "read"))))
9804
9805 (defun gnus-summary-kill-same-subject (&optional unmark)
9806   "Mark articles which has the same subject as read.
9807 If UNMARK is positive, remove any kind of mark.
9808 If UNMARK is negative, tick articles."
9809   (interactive "P")
9810   (when unmark
9811     (setq unmark (prefix-numeric-value unmark)))
9812   (let ((count
9813          (gnus-summary-mark-same-subject
9814           (gnus-summary-article-subject) unmark)))
9815     ;; If marked as read, go to next unread subject.
9816     (when (null unmark)
9817       ;; Go to next unread subject.
9818       (gnus-summary-next-subject 1 t))
9819     (gnus-message 7 "%d articles are marked as %s"
9820                   count (if unmark "unread" "read"))))
9821
9822 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9823   "Mark articles with same SUBJECT as read, and return marked number.
9824 If optional argument UNMARK is positive, remove any kinds of marks.
9825 If optional argument UNMARK is negative, mark articles as unread instead."
9826   (let ((count 1))
9827     (save-excursion
9828       (cond
9829        ((null unmark)                   ; Mark as read.
9830         (while (and
9831                 (progn
9832                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9833                   (gnus-summary-show-thread) t)
9834                 (gnus-summary-find-subject subject))
9835           (setq count (1+ count))))
9836        ((> unmark 0)                    ; Tick.
9837         (while (and
9838                 (progn
9839                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9840                   (gnus-summary-show-thread) t)
9841                 (gnus-summary-find-subject subject))
9842           (setq count (1+ count))))
9843        (t                               ; Mark as unread.
9844         (while (and
9845                 (progn
9846                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9847                   (gnus-summary-show-thread) t)
9848                 (gnus-summary-find-subject subject))
9849           (setq count (1+ count)))))
9850       (gnus-set-mode-line 'summary)
9851       ;; Return the number of marked articles.
9852       count)))
9853
9854 (defun gnus-summary-mark-as-processable (n &optional unmark)
9855   "Set the process mark on the next N articles.
9856 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9857 the process mark instead.  The difference between N and the actual
9858 number of articles marked is returned."
9859   (interactive "P")
9860   (if (and (null n) (gnus-region-active-p))
9861       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9862     (setq n (prefix-numeric-value n))
9863     (let ((backward (< n 0))
9864           (n (abs n)))
9865       (while (and
9866               (> n 0)
9867               (if unmark
9868                   (gnus-summary-remove-process-mark
9869                    (gnus-summary-article-number))
9870                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9871               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9872         (setq n (1- n)))
9873       (when (/= 0 n)
9874         (gnus-message 7 "No more articles"))
9875       (gnus-summary-recenter)
9876       (gnus-summary-position-point)
9877       n)))
9878
9879 (defun gnus-summary-unmark-as-processable (n)
9880   "Remove the process mark from the next N articles.
9881 If N is negative, unmark backward instead.  The difference between N and
9882 the actual number of articles unmarked is returned."
9883   (interactive "P")
9884   (gnus-summary-mark-as-processable n t))
9885
9886 (defun gnus-summary-unmark-all-processable ()
9887   "Remove the process mark from all articles."
9888   (interactive)
9889   (save-excursion
9890     (while gnus-newsgroup-processable
9891       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9892   (gnus-summary-position-point))
9893
9894 (defun gnus-summary-add-mark (article type)
9895   "Mark ARTICLE with a mark of TYPE."
9896   (let ((vtype (car (assq type gnus-article-mark-lists)))
9897         var)
9898     (if (not vtype)
9899         (error "No such mark type: %s" type)
9900       (setq var (intern (format "gnus-newsgroup-%s" type)))
9901       (set var (cons article (symbol-value var)))
9902       (if (memq type '(processable cached replied forwarded recent saved))
9903           (gnus-summary-update-secondary-mark article)
9904         ;;; !!! This is bogus.  We should find out what primary
9905         ;;; !!! mark we want to set.
9906         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9907
9908 (defun gnus-summary-mark-as-expirable (n)
9909   "Mark N articles forward as expirable.
9910 If N is negative, mark backward instead.  The difference between N and
9911 the actual number of articles marked is returned."
9912   (interactive "p")
9913   (gnus-summary-mark-forward n gnus-expirable-mark))
9914
9915 (defun gnus-summary-mark-as-spam (n)
9916   "Mark N articles forward as spam.
9917 If N is negative, mark backward instead.  The difference between N and
9918 the actual number of articles marked is returned."
9919   (interactive "p")
9920   (gnus-summary-mark-forward n gnus-spam-mark))
9921
9922 (defun gnus-summary-mark-article-as-replied (article)
9923   "Mark ARTICLE as replied to and update the summary line.
9924 ARTICLE can also be a list of articles."
9925   (interactive (list (gnus-summary-article-number)))
9926   (let ((articles (if (listp article) article (list article))))
9927     (dolist (article articles)
9928       (unless (numberp article)
9929         (error "%s is not a number" article))
9930       (push article gnus-newsgroup-replied)
9931       (let ((buffer-read-only nil))
9932         (when (gnus-summary-goto-subject article nil t)
9933           (gnus-summary-update-secondary-mark article))))))
9934
9935 (defun gnus-summary-mark-article-as-forwarded (article)
9936   "Mark ARTICLE as forwarded and update the summary line.
9937 ARTICLE can also be a list of articles."
9938   (let ((articles (if (listp article) article (list article))))
9939     (dolist (article articles)
9940       (push article gnus-newsgroup-forwarded)
9941       (let ((buffer-read-only nil))
9942         (when (gnus-summary-goto-subject article nil t)
9943           (gnus-summary-update-secondary-mark article))))))
9944
9945 (defun gnus-summary-set-bookmark (article)
9946   "Set a bookmark in current article."
9947   (interactive (list (gnus-summary-article-number)))
9948   (when (or (not (get-buffer gnus-article-buffer))
9949             (not gnus-current-article)
9950             (not gnus-article-current)
9951             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9952     (error "No current article selected"))
9953   ;; Remove old bookmark, if one exists.
9954   (gnus-pull article gnus-newsgroup-bookmarks)
9955   ;; Set the new bookmark, which is on the form
9956   ;; (article-number . line-number-in-body).
9957   (push
9958    (cons article
9959          (with-current-buffer gnus-article-buffer
9960            (count-lines
9961             (min (point)
9962                  (save-excursion
9963                    (article-goto-body)
9964                    (point)))
9965             (point))))
9966    gnus-newsgroup-bookmarks)
9967   (gnus-message 6 "A bookmark has been added to the current article."))
9968
9969 (defun gnus-summary-remove-bookmark (article)
9970   "Remove the bookmark from the current article."
9971   (interactive (list (gnus-summary-article-number)))
9972   ;; Remove old bookmark, if one exists.
9973   (if (not (assq article gnus-newsgroup-bookmarks))
9974       (gnus-message 6 "No bookmark in current article.")
9975     (gnus-pull article gnus-newsgroup-bookmarks)
9976     (gnus-message 6 "Removed bookmark.")))
9977
9978 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9979 (defun gnus-summary-mark-as-dormant (n)
9980   "Mark N articles forward as dormant.
9981 If N is negative, mark backward instead.  The difference between N and
9982 the actual number of articles marked is returned."
9983   (interactive "p")
9984   (gnus-summary-mark-forward n gnus-dormant-mark))
9985
9986 (defun gnus-summary-set-process-mark (article)
9987   "Set the process mark on ARTICLE and update the summary line."
9988   (setq gnus-newsgroup-processable
9989         (cons article
9990               (delq article gnus-newsgroup-processable)))
9991   (when (gnus-summary-goto-subject article)
9992     (gnus-summary-show-thread)
9993     (gnus-summary-goto-subject article)
9994     (gnus-summary-update-secondary-mark article)))
9995
9996 (defun gnus-summary-remove-process-mark (&rest articles)
9997   "Remove the process mark from ARTICLES and update the summary line."
9998   (dolist (article articles)
9999     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10000     (when (gnus-summary-goto-subject article)
10001       (gnus-summary-show-thread)
10002       (gnus-summary-goto-subject article)
10003       (gnus-summary-update-secondary-mark article))))
10004
10005 (defun gnus-summary-set-saved-mark (article)
10006   "Set the process mark on ARTICLE and update the summary line."
10007   (push article gnus-newsgroup-saved)
10008   (when (gnus-summary-goto-subject article)
10009     (gnus-summary-update-secondary-mark article)))
10010
10011 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10012   "Mark N articles as read forwards.
10013 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10014 The difference between N and the actual number of articles marked is
10015 returned.
10016 If NO-EXPIRE, auto-expiry will be inhibited."
10017   (interactive "p")
10018   (gnus-summary-show-thread)
10019   (let ((backward (< n 0))
10020         (gnus-summary-goto-unread
10021          (and gnus-summary-goto-unread
10022               (not (eq gnus-summary-goto-unread 'never))
10023               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10024                                     gnus-ticked-mark gnus-dormant-mark)))))
10025         (n (abs n))
10026         (mark (or mark gnus-del-mark)))
10027     (while (and (> n 0)
10028                 (gnus-summary-mark-article nil mark no-expire)
10029                 (zerop (gnus-summary-next-subject
10030                         (if backward -1 1)
10031                         (and gnus-summary-goto-unread
10032                              (not (eq gnus-summary-goto-unread 'never)))
10033                         t)))
10034       (setq n (1- n)))
10035     (when (/= 0 n)
10036       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10037     (gnus-summary-recenter)
10038     (gnus-summary-position-point)
10039     (gnus-set-mode-line 'summary)
10040     n))
10041
10042 (defun gnus-summary-mark-article-as-read (mark)
10043   "Mark the current article quickly as read with MARK."
10044   (let ((article (gnus-summary-article-number)))
10045     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10046     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10047     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10048     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10049     (push (cons article mark) gnus-newsgroup-reads)
10050     ;; Possibly remove from cache, if that is used.
10051     (when gnus-use-cache
10052       (gnus-cache-enter-remove-article article))
10053     ;; Allow the backend to change the mark.
10054     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10055     ;; Check for auto-expiry.
10056     (when (and gnus-newsgroup-auto-expire
10057                (memq mark gnus-auto-expirable-marks))
10058       (setq mark gnus-expirable-mark)
10059       ;; Let the backend know about the mark change.
10060       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10061       (push article gnus-newsgroup-expirable))
10062     ;; Set the mark in the buffer.
10063     (gnus-summary-update-mark mark 'unread)
10064     t))
10065
10066 (defun gnus-summary-mark-article-as-unread (mark)
10067   "Mark the current article quickly as unread with MARK."
10068   (let* ((article (gnus-summary-article-number))
10069          (old-mark (gnus-summary-article-mark article)))
10070     ;; Allow the backend to change the mark.
10071     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10072     (if (eq mark old-mark)
10073         t
10074       (if (<= article 0)
10075           (progn
10076             (gnus-error 1 "Can't mark negative article numbers")
10077             nil)
10078         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10079         (setq gnus-newsgroup-spam-marked
10080               (delq article gnus-newsgroup-spam-marked))
10081         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10082         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10083         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10084         (cond ((= mark gnus-ticked-mark)
10085                (setq gnus-newsgroup-marked
10086                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10087                                               article)))
10088               ((= mark gnus-spam-mark)
10089                (setq gnus-newsgroup-spam-marked
10090                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10091                                               article)))
10092               ((= mark gnus-dormant-mark)
10093                (setq gnus-newsgroup-dormant
10094                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10095                                               article)))
10096               (t
10097                (setq gnus-newsgroup-unreads
10098                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10099                                               article))))
10100         (gnus-pull article gnus-newsgroup-reads)
10101
10102         ;; See whether the article is to be put in the cache.
10103         (and gnus-use-cache
10104              (vectorp (gnus-summary-article-header article))
10105              (save-excursion
10106                (gnus-cache-possibly-enter-article
10107                 gnus-newsgroup-name article
10108                 (= mark gnus-ticked-mark)
10109                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10110
10111         ;; Fix the mark.
10112         (gnus-summary-update-mark mark 'unread)
10113         t))))
10114
10115 (defun gnus-summary-mark-article (&optional article mark no-expire)
10116   "Mark ARTICLE with MARK.  MARK can be any character.
10117 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10118 `??' (dormant) and `?E' (expirable).
10119 If MARK is nil, then the default character `?r' is used.
10120 If ARTICLE is nil, then the article on the current line will be
10121 marked.
10122 If NO-EXPIRE, auto-expiry will be inhibited."
10123   ;; The mark might be a string.
10124   (when (stringp mark)
10125     (setq mark (aref mark 0)))
10126   ;; If no mark is given, then we check auto-expiring.
10127   (when (null mark)
10128     (setq mark gnus-del-mark))
10129   (when (and (not no-expire)
10130              gnus-newsgroup-auto-expire
10131              (memq mark gnus-auto-expirable-marks))
10132     (setq mark gnus-expirable-mark))
10133   (let ((article (or article (gnus-summary-article-number)))
10134         (old-mark (gnus-summary-article-mark article)))
10135     ;; Allow the backend to change the mark.
10136     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10137     (if (eq mark old-mark)
10138         t
10139       (unless article
10140         (error "No article on current line"))
10141       (if (not (if (or (= mark gnus-unread-mark)
10142                        (= mark gnus-ticked-mark)
10143                        (= mark gnus-spam-mark)
10144                        (= mark gnus-dormant-mark))
10145                    (gnus-mark-article-as-unread article mark)
10146                  (gnus-mark-article-as-read article mark)))
10147           t
10148         ;; See whether the article is to be put in the cache.
10149         (and gnus-use-cache
10150              (not (= mark gnus-canceled-mark))
10151              (vectorp (gnus-summary-article-header article))
10152              (save-excursion
10153                (gnus-cache-possibly-enter-article
10154                 gnus-newsgroup-name article
10155                 (= mark gnus-ticked-mark)
10156                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10157
10158         (when (gnus-summary-goto-subject article nil t)
10159           (let ((buffer-read-only nil))
10160             (gnus-summary-show-thread)
10161             ;; Fix the mark.
10162             (gnus-summary-update-mark mark 'unread)
10163             t))))))
10164
10165 (defun gnus-summary-update-secondary-mark (article)
10166   "Update the secondary (read, process, cache) mark."
10167   (gnus-summary-update-mark
10168    (cond ((memq article gnus-newsgroup-processable)
10169           gnus-process-mark)
10170          ((memq article gnus-newsgroup-cached)
10171           gnus-cached-mark)
10172          ((memq article gnus-newsgroup-replied)
10173           gnus-replied-mark)
10174          ((memq article gnus-newsgroup-forwarded)
10175           gnus-forwarded-mark)
10176          ((memq article gnus-newsgroup-saved)
10177           gnus-saved-mark)
10178          ((memq article gnus-newsgroup-recent)
10179           gnus-recent-mark)
10180          ((memq article gnus-newsgroup-unseen)
10181           gnus-unseen-mark)
10182          (t gnus-no-mark))
10183    'replied)
10184   (when (gnus-visual-p 'summary-highlight 'highlight)
10185     (gnus-run-hooks 'gnus-summary-update-hook))
10186   t)
10187
10188 (defun gnus-summary-update-download-mark (article)
10189   "Update the download mark."
10190   (gnus-summary-update-mark
10191    (cond ((memq article gnus-newsgroup-undownloaded)
10192           gnus-undownloaded-mark)
10193          (gnus-newsgroup-agentized
10194           gnus-downloaded-mark)
10195          (t
10196           gnus-no-mark))
10197    'download)
10198   (gnus-summary-update-line t)
10199   t)
10200
10201 (defun gnus-summary-update-mark (mark type)
10202   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10203         (buffer-read-only nil))
10204     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10205     (when forward
10206       (when (looking-at "\r")
10207         (incf forward))
10208       (when (<= (+ forward (point)) (point-max))
10209         ;; Go to the right position on the line.
10210         (goto-char (+ forward (point)))
10211         ;; Replace the old mark with the new mark.
10212         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10213         ;; Optionally update the marks by some user rule.
10214         (when (eq type 'unread)
10215           (gnus-data-set-mark
10216            (gnus-data-find (gnus-summary-article-number)) mark)
10217           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10218
10219 (defun gnus-mark-article-as-read (article &optional mark)
10220   "Enter ARTICLE in the pertinent lists and remove it from others."
10221   ;; Make the article expirable.
10222   (let ((mark (or mark gnus-del-mark)))
10223     (setq gnus-newsgroup-expirable
10224           (if (= mark gnus-expirable-mark)
10225               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10226             (delq article gnus-newsgroup-expirable)))
10227     ;; Remove from unread and marked lists.
10228     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10229     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10230     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10231     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10232     (push (cons article mark) gnus-newsgroup-reads)
10233     ;; Possibly remove from cache, if that is used.
10234     (when gnus-use-cache
10235       (gnus-cache-enter-remove-article article))
10236     t))
10237
10238 (defun gnus-mark-article-as-unread (article &optional mark)
10239   "Enter ARTICLE in the pertinent lists and remove it from others."
10240   (let ((mark (or mark gnus-ticked-mark)))
10241     (if (<= article 0)
10242         (progn
10243           (gnus-error 1 "Can't mark negative article numbers")
10244           nil)
10245       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10246             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10247             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10248             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10249             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10250
10251       ;; Unsuppress duplicates?
10252       (when gnus-suppress-duplicates
10253         (gnus-dup-unsuppress-article article))
10254
10255       (cond ((= mark gnus-ticked-mark)
10256              (setq gnus-newsgroup-marked
10257                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10258             ((= mark gnus-spam-mark)
10259              (setq gnus-newsgroup-spam-marked
10260                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10261                                             article)))
10262             ((= mark gnus-dormant-mark)
10263              (setq gnus-newsgroup-dormant
10264                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10265             (t
10266              (setq gnus-newsgroup-unreads
10267                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10268       (gnus-pull article gnus-newsgroup-reads)
10269       t)))
10270
10271 (defalias 'gnus-summary-mark-as-unread-forward
10272   'gnus-summary-tick-article-forward)
10273 (make-obsolete 'gnus-summary-mark-as-unread-forward
10274                'gnus-summary-tick-article-forward)
10275 (defun gnus-summary-tick-article-forward (n)
10276   "Tick N articles forwards.
10277 If N is negative, tick backwards instead.
10278 The difference between N and the number of articles ticked is returned."
10279   (interactive "p")
10280   (gnus-summary-mark-forward n gnus-ticked-mark))
10281
10282 (defalias 'gnus-summary-mark-as-unread-backward
10283   'gnus-summary-tick-article-backward)
10284 (make-obsolete 'gnus-summary-mark-as-unread-backward
10285                'gnus-summary-tick-article-backward)
10286 (defun gnus-summary-tick-article-backward (n)
10287   "Tick N articles backwards.
10288 The difference between N and the number of articles ticked is returned."
10289   (interactive "p")
10290   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10291
10292 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10293 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10294 (defun gnus-summary-tick-article (&optional article clear-mark)
10295   "Mark current article as unread.
10296 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10297 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10298   (interactive)
10299   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10300                                        gnus-ticked-mark)))
10301
10302 (defun gnus-summary-mark-as-read-forward (n)
10303   "Mark N articles as read forwards.
10304 If N is negative, mark backwards instead.
10305 The difference between N and the actual number of articles marked is
10306 returned."
10307   (interactive "p")
10308   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10309
10310 (defun gnus-summary-mark-as-read-backward (n)
10311   "Mark the N articles as read backwards.
10312 The difference between N and the actual number of articles marked is
10313 returned."
10314   (interactive "p")
10315   (gnus-summary-mark-forward
10316    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10317
10318 (defun gnus-summary-mark-as-read (&optional article mark)
10319   "Mark current article as read.
10320 ARTICLE specifies the article to be marked as read.
10321 MARK specifies a string to be inserted at the beginning of the line."
10322   (gnus-summary-mark-article article mark))
10323
10324 (defun gnus-summary-clear-mark-forward (n)
10325   "Clear marks from N articles forward.
10326 If N is negative, clear backward instead.
10327 The difference between N and the number of marks cleared is returned."
10328   (interactive "p")
10329   (gnus-summary-mark-forward n gnus-unread-mark))
10330
10331 (defun gnus-summary-clear-mark-backward (n)
10332   "Clear marks from N articles backward.
10333 The difference between N and the number of marks cleared is returned."
10334   (interactive "p")
10335   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10336
10337 (defun gnus-summary-mark-unread-as-read ()
10338   "Intended to be used by `gnus-summary-mark-article-hook'."
10339   (when (memq gnus-current-article gnus-newsgroup-unreads)
10340     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10341
10342 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10343   "Intended to be used by `gnus-summary-mark-article-hook'."
10344   (let ((mark (gnus-summary-article-mark)))
10345     (when (or (gnus-unread-mark-p mark)
10346               (gnus-read-mark-p mark))
10347       (gnus-summary-mark-article gnus-current-article
10348                                  (or new-mark gnus-read-mark)))))
10349
10350 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10351   "Intended to be used by `gnus-summary-mark-article-hook'."
10352   (let ((mark (gnus-summary-article-mark)))
10353     (when (or (gnus-unread-mark-p mark)
10354               (gnus-read-mark-p mark))
10355       (gnus-summary-mark-article (gnus-summary-article-number)
10356                                  (or new-mark gnus-read-mark)))))
10357
10358 (defun gnus-summary-mark-unread-as-ticked ()
10359   "Intended to be used by `gnus-summary-mark-article-hook'."
10360   (when (memq gnus-current-article gnus-newsgroup-unreads)
10361     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10362
10363 (defun gnus-summary-mark-region-as-read (point mark all)
10364   "Mark all unread articles between point and mark as read.
10365 If given a prefix, mark all articles between point and mark as read,
10366 even ticked and dormant ones."
10367   (interactive "r\nP")
10368   (save-excursion
10369     (let (article)
10370       (goto-char point)
10371       (beginning-of-line)
10372       (while (and
10373               (< (point) mark)
10374               (progn
10375                 (when (or all
10376                           (memq (setq article (gnus-summary-article-number))
10377                                 gnus-newsgroup-unreads))
10378                   (gnus-summary-mark-article article gnus-del-mark))
10379                 t)
10380               (gnus-summary-find-next))))))
10381
10382 (defun gnus-summary-mark-below (score mark)
10383   "Mark articles with score less than SCORE with MARK."
10384   (interactive "P\ncMark: ")
10385   (setq score (if score
10386                   (prefix-numeric-value score)
10387                 (or gnus-summary-default-score 0)))
10388   (save-excursion
10389     (set-buffer gnus-summary-buffer)
10390     (goto-char (point-min))
10391     (while
10392         (progn
10393           (and (< (gnus-summary-article-score) score)
10394                (gnus-summary-mark-article nil mark))
10395           (gnus-summary-find-next)))))
10396
10397 (defun gnus-summary-kill-below (&optional score)
10398   "Mark articles with score below SCORE as read."
10399   (interactive "P")
10400   (gnus-summary-mark-below score gnus-killed-mark))
10401
10402 (defun gnus-summary-clear-above (&optional score)
10403   "Clear all marks from articles with score above SCORE."
10404   (interactive "P")
10405   (gnus-summary-mark-above score gnus-unread-mark))
10406
10407 (defun gnus-summary-tick-above (&optional score)
10408   "Tick all articles with score above SCORE."
10409   (interactive "P")
10410   (gnus-summary-mark-above score gnus-ticked-mark))
10411
10412 (defun gnus-summary-mark-above (score mark)
10413   "Mark articles with score over SCORE with MARK."
10414   (interactive "P\ncMark: ")
10415   (setq score (if score
10416                   (prefix-numeric-value score)
10417                 (or gnus-summary-default-score 0)))
10418   (save-excursion
10419     (set-buffer gnus-summary-buffer)
10420     (goto-char (point-min))
10421     (while (and (progn
10422                   (when (> (gnus-summary-article-score) score)
10423                     (gnus-summary-mark-article nil mark))
10424                   t)
10425                 (gnus-summary-find-next)))))
10426
10427 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10428 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10429 (defun gnus-summary-limit-include-expunged (&optional no-error)
10430   "Display all the hidden articles that were expunged for low scores."
10431   (interactive)
10432   (let ((buffer-read-only nil))
10433     (let ((scored gnus-newsgroup-scored)
10434           headers h)
10435       (while scored
10436         (unless (gnus-summary-article-header (caar scored))
10437           (and (setq h (gnus-number-to-header (caar scored)))
10438                (< (cdar scored) gnus-summary-expunge-below)
10439                (push h headers)))
10440         (setq scored (cdr scored)))
10441       (if (not headers)
10442           (when (not no-error)
10443             (error "No expunged articles hidden"))
10444         (goto-char (point-min))
10445         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10446         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10447         (mapcar (lambda (x) (push (mail-header-number x)
10448                                   gnus-newsgroup-limit))
10449                 headers)
10450         (gnus-summary-prepare-unthreaded (nreverse headers))
10451         (goto-char (point-min))
10452         (gnus-summary-position-point)
10453         t))))
10454
10455 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10456   "Mark all unread articles in this newsgroup as read.
10457 If prefix argument ALL is non-nil, ticked and dormant articles will
10458 also be marked as read.
10459 If QUIETLY is non-nil, no questions will be asked.
10460
10461 If TO-HERE is non-nil, it should be a point in the buffer.  All
10462 articles before (after, if REVERSE is set) this point will be marked
10463 as read.
10464
10465 Note that this function will only catch up the unread article
10466 in the current summary buffer limitation.
10467
10468 The number of articles marked as read is returned."
10469   (interactive "P")
10470   (prog1
10471       (save-excursion
10472         (when (or quietly
10473                   (not gnus-interactive-catchup) ;Without confirmation?
10474                   gnus-expert-user
10475                   (gnus-y-or-n-p
10476                    (if all
10477                        "Mark absolutely all articles as read? "
10478                      "Mark all unread articles as read? ")))
10479           (if (and not-mark
10480                    (not gnus-newsgroup-adaptive)
10481                    (not gnus-newsgroup-auto-expire)
10482                    (not gnus-suppress-duplicates)
10483                    (or (not gnus-use-cache)
10484                        (eq gnus-use-cache 'passive)))
10485               (progn
10486                 (when all
10487                   (setq gnus-newsgroup-marked nil
10488                         gnus-newsgroup-spam-marked nil
10489                         gnus-newsgroup-dormant nil))
10490                 (setq gnus-newsgroup-unreads
10491                       (gnus-sorted-nunion
10492                        (gnus-intersection gnus-newsgroup-unreads
10493                                           gnus-newsgroup-downloadable)
10494                        gnus-newsgroup-unfetched)))
10495             ;; We actually mark all articles as canceled, which we
10496             ;; have to do when using auto-expiry or adaptive scoring.
10497             (gnus-summary-show-all-threads)
10498             (if (and to-here reverse)
10499                 (progn
10500                   (goto-char to-here)
10501                   (gnus-summary-mark-current-read-and-unread-as-read
10502                    gnus-catchup-mark)
10503                   (while (gnus-summary-find-next (not all))
10504                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10505               (when (gnus-summary-first-subject (not all))
10506                 (while (and
10507                         (if to-here (< (point) to-here) t)
10508                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10509                         (gnus-summary-find-next (not all))))))
10510             (gnus-set-mode-line 'summary))
10511           t))
10512     (gnus-summary-position-point)))
10513
10514 (defun gnus-summary-catchup-to-here (&optional all)
10515   "Mark all unticked articles before the current one as read.
10516 If ALL is non-nil, also mark ticked and dormant articles as read."
10517   (interactive "P")
10518   (save-excursion
10519     (gnus-save-hidden-threads
10520       (let ((beg (point)))
10521         ;; We check that there are unread articles.
10522         (when (or all (gnus-summary-find-prev))
10523           (gnus-summary-catchup all t beg)))))
10524   (gnus-summary-position-point))
10525
10526 (defun gnus-summary-catchup-from-here (&optional all)
10527   "Mark all unticked articles after (and including) the current one as read.
10528 If ALL is non-nil, also mark ticked and dormant articles as read."
10529   (interactive "P")
10530   (save-excursion
10531     (gnus-save-hidden-threads
10532       (let ((beg (point)))
10533         ;; We check that there are unread articles.
10534         (when (or all (gnus-summary-find-next))
10535           (gnus-summary-catchup all t beg nil t)))))
10536   (gnus-summary-position-point))
10537
10538 (defun gnus-summary-catchup-all (&optional quietly)
10539   "Mark all articles in this newsgroup as read.
10540 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10541 instead, which marks only unread articles as read."
10542   (interactive "P")
10543   (gnus-summary-catchup t quietly))
10544
10545 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10546   "Mark all unread articles in this group as read, then exit.
10547 If prefix argument ALL is non-nil, all articles are marked as read.
10548 If QUIETLY is non-nil, no questions will be asked."
10549   (interactive "P")
10550   (when (gnus-summary-catchup all quietly nil 'fast)
10551     ;; Select next newsgroup or exit.
10552     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10553              (eq gnus-auto-select-next 'quietly))
10554         (gnus-summary-next-group nil)
10555       (gnus-summary-exit))))
10556
10557 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10558   "Mark all articles in this newsgroup as read, and then exit.
10559 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10560 instead, which marks only unread articles as read."
10561   (interactive "P")
10562   (gnus-summary-catchup-and-exit t quietly))
10563
10564 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10565   "Mark all articles in this group as read and select the next group.
10566 If given a prefix, mark all articles, unread as well as ticked, as
10567 read."
10568   (interactive "P")
10569   (save-excursion
10570     (gnus-summary-catchup all))
10571   (gnus-summary-next-group))
10572
10573 ;;;
10574 ;;; with article
10575 ;;;
10576
10577 (defmacro gnus-with-article (article &rest forms)
10578   "Select ARTICLE and perform FORMS in the original article buffer.
10579 Then replace the article with the result."
10580   `(progn
10581      ;; We don't want the article to be marked as read.
10582      (let (gnus-mark-article-hook)
10583        (gnus-summary-select-article t t nil ,article))
10584      (set-buffer gnus-original-article-buffer)
10585      ,@forms
10586      (if (not (gnus-check-backend-function
10587                'request-replace-article (car gnus-article-current)))
10588          (gnus-message 5 "Read-only group; not replacing")
10589        (unless (gnus-request-replace-article
10590                 ,article (car gnus-article-current)
10591                 (current-buffer) t)
10592          (error "Couldn't replace article")))
10593      ;; The cache and backlog have to be flushed somewhat.
10594      (when gnus-keep-backlog
10595        (gnus-backlog-remove-article
10596         (car gnus-article-current) (cdr gnus-article-current)))
10597      (when gnus-use-cache
10598        (gnus-cache-update-article
10599         (car gnus-article-current) (cdr gnus-article-current)))))
10600
10601 (put 'gnus-with-article 'lisp-indent-function 1)
10602 (put 'gnus-with-article 'edebug-form-spec '(form body))
10603
10604 ;; Thread-based commands.
10605
10606 (defun gnus-summary-articles-in-thread (&optional article)
10607   "Return a list of all articles in the current thread.
10608 If ARTICLE is non-nil, return all articles in the thread that starts
10609 with that article."
10610   (let* ((article (or article (gnus-summary-article-number)))
10611          (data (gnus-data-find-list article))
10612          (top-level (gnus-data-level (car data)))
10613          (top-subject
10614           (cond ((null gnus-thread-operation-ignore-subject)
10615                  (gnus-simplify-subject-re
10616                   (mail-header-subject (gnus-data-header (car data)))))
10617                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10618                  (gnus-simplify-subject-fuzzy
10619                   (mail-header-subject (gnus-data-header (car data)))))
10620                 (t nil)))
10621          (end-point (save-excursion
10622                       (if (gnus-summary-go-to-next-thread)
10623                           (point) (point-max))))
10624          articles)
10625     (while (and data
10626                 (< (gnus-data-pos (car data)) end-point))
10627       (when (or (not top-subject)
10628                 (string= top-subject
10629                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10630                              (gnus-simplify-subject-fuzzy
10631                               (mail-header-subject
10632                                (gnus-data-header (car data))))
10633                            (gnus-simplify-subject-re
10634                             (mail-header-subject
10635                              (gnus-data-header (car data)))))))
10636         (push (gnus-data-number (car data)) articles))
10637       (unless (and (setq data (cdr data))
10638                    (> (gnus-data-level (car data)) top-level))
10639         (setq data nil)))
10640     ;; Return the list of articles.
10641     (nreverse articles)))
10642
10643 (defun gnus-summary-rethread-current ()
10644   "Rethread the thread the current article is part of."
10645   (interactive)
10646   (let* ((gnus-show-threads t)
10647          (article (gnus-summary-article-number))
10648          (id (mail-header-id (gnus-summary-article-header)))
10649          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10650     (unless id
10651       (error "No article on the current line"))
10652     (gnus-rebuild-thread id)
10653     (gnus-summary-goto-subject article)))
10654
10655 (defun gnus-summary-reparent-thread ()
10656   "Make the current article child of the marked (or previous) article.
10657
10658 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10659 is non-nil or the Subject: of both articles are the same."
10660   (interactive)
10661   (unless (not (gnus-group-read-only-p))
10662     (error "The current newsgroup does not support article editing"))
10663   (unless (<= (length gnus-newsgroup-processable) 1)
10664     (error "No more than one article may be marked"))
10665   (save-window-excursion
10666     (let ((gnus-article-buffer " *reparent*")
10667           (current-article (gnus-summary-article-number))
10668           ;; First grab the marked article, otherwise one line up.
10669           (parent-article (if (not (null gnus-newsgroup-processable))
10670                               (car gnus-newsgroup-processable)
10671                             (save-excursion
10672                               (if (eq (forward-line -1) 0)
10673                                   (gnus-summary-article-number)
10674                                 (error "Beginning of summary buffer"))))))
10675       (unless (not (eq current-article parent-article))
10676         (error "An article may not be self-referential"))
10677       (let ((message-id (mail-header-id
10678                          (gnus-summary-article-header parent-article))))
10679         (unless (and message-id (not (equal message-id "")))
10680           (error "No message-id in desired parent"))
10681         (gnus-with-article current-article
10682           (save-restriction
10683             (goto-char (point-min))
10684             (message-narrow-to-head)
10685             (if (re-search-forward "^References: " nil t)
10686                 (progn
10687                   (re-search-forward "^[^ \t]" nil t)
10688                   (forward-line -1)
10689                   (end-of-line)
10690                   (insert " " message-id))
10691               (insert "References: " message-id "\n"))))
10692         (set-buffer gnus-summary-buffer)
10693         (gnus-summary-unmark-all-processable)
10694         (gnus-summary-update-article current-article)
10695         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10696             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10697         (gnus-summary-rethread-current)
10698         (gnus-message 3 "Article %d is now the child of article %d"
10699                       current-article parent-article)))))
10700
10701 (defun gnus-summary-toggle-threads (&optional arg)
10702   "Toggle showing conversation threads.
10703 If ARG is positive number, turn showing conversation threads on."
10704   (interactive "P")
10705   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10706     (setq gnus-show-threads
10707           (if (null arg) (not gnus-show-threads)
10708             (> (prefix-numeric-value arg) 0)))
10709     (gnus-summary-prepare)
10710     (gnus-summary-goto-subject current)
10711     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10712     (gnus-summary-position-point)))
10713
10714 (defun gnus-summary-show-all-threads ()
10715   "Show all threads."
10716   (interactive)
10717   (save-excursion
10718     (let ((buffer-read-only nil))
10719       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10720   (gnus-summary-position-point))
10721
10722 (defun gnus-summary-show-thread ()
10723   "Show thread subtrees.
10724 Returns nil if no thread was there to be shown."
10725   (interactive)
10726   (let ((buffer-read-only nil)
10727         (orig (point))
10728         (end (point-at-eol))
10729         ;; Leave point at bol
10730         (beg (progn (beginning-of-line) (point))))
10731     (prog1
10732         ;; Any hidden lines here?
10733         (search-forward "\r" end t)
10734       (subst-char-in-region beg end ?\^M ?\n t)
10735       (goto-char orig)
10736       (gnus-summary-position-point))))
10737
10738 (defun gnus-summary-maybe-hide-threads ()
10739   "If requested, hide the threads that should be hidden."
10740   (when (and gnus-show-threads
10741              gnus-thread-hide-subtree)
10742     (gnus-summary-hide-all-threads
10743      (if (or (consp gnus-thread-hide-subtree)
10744              (functionp gnus-thread-hide-subtree))
10745          (gnus-make-predicate gnus-thread-hide-subtree)
10746        nil))))
10747
10748 ;;; Hiding predicates.
10749
10750 (defun gnus-article-unread-p (header)
10751   (memq (mail-header-number header) gnus-newsgroup-unreads))
10752
10753 (defun gnus-article-unseen-p (header)
10754   (memq (mail-header-number header) gnus-newsgroup-unseen))
10755
10756 (defun gnus-map-articles (predicate articles)
10757   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10758   (apply 'gnus-or (mapcar predicate
10759                           (mapcar 'gnus-summary-article-header articles))))
10760
10761 (defun gnus-summary-hide-all-threads (&optional predicate)
10762   "Hide all thread subtrees.
10763 If PREDICATE is supplied, threads that satisfy this predicate
10764 will not be hidden."
10765   (interactive)
10766   (save-excursion
10767     (goto-char (point-min))
10768     (let ((end nil))
10769       (while (not end)
10770         (when (or (not predicate)
10771                   (gnus-map-articles
10772                    predicate (gnus-summary-article-children)))
10773             (gnus-summary-hide-thread))
10774         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10775   (gnus-summary-position-point))
10776
10777 (defun gnus-summary-hide-thread ()
10778   "Hide thread subtrees.
10779 If PREDICATE is supplied, threads that satisfy this predicate
10780 will not be hidden.
10781 Returns nil if no threads were there to be hidden."
10782   (interactive)
10783   (let ((buffer-read-only nil)
10784         (start (point))
10785         (article (gnus-summary-article-number)))
10786     (goto-char start)
10787     ;; Go forward until either the buffer ends or the subthread
10788     ;; ends.
10789     (when (and (not (eobp))
10790                (or (zerop (gnus-summary-next-thread 1 t))
10791                    (goto-char (point-max))))
10792       (prog1
10793           (if (and (> (point) start)
10794                    (search-backward "\n" start t))
10795               (progn
10796                 (subst-char-in-region start (point) ?\n ?\^M)
10797                 (gnus-summary-goto-subject article))
10798             (goto-char start)
10799             nil)))))
10800
10801 (defun gnus-summary-go-to-next-thread (&optional previous)
10802   "Go to the same level (or less) next thread.
10803 If PREVIOUS is non-nil, go to previous thread instead.
10804 Return the article number moved to, or nil if moving was impossible."
10805   (let ((level (gnus-summary-thread-level))
10806         (way (if previous -1 1))
10807         (beg (point)))
10808     (forward-line way)
10809     (while (and (not (eobp))
10810                 (< level (gnus-summary-thread-level)))
10811       (forward-line way))
10812     (if (eobp)
10813         (progn
10814           (goto-char beg)
10815           nil)
10816       (setq beg (point))
10817       (prog1
10818           (gnus-summary-article-number)
10819         (goto-char beg)))))
10820
10821 (defun gnus-summary-next-thread (n &optional silent)
10822   "Go to the same level next N'th thread.
10823 If N is negative, search backward instead.
10824 Returns the difference between N and the number of skips actually
10825 done.
10826
10827 If SILENT, don't output messages."
10828   (interactive "p")
10829   (let ((backward (< n 0))
10830         (n (abs n)))
10831     (while (and (> n 0)
10832                 (gnus-summary-go-to-next-thread backward))
10833       (decf n))
10834     (unless silent
10835       (gnus-summary-position-point))
10836     (when (and (not silent) (/= 0 n))
10837       (gnus-message 7 "No more threads"))
10838     n))
10839
10840 (defun gnus-summary-prev-thread (n)
10841   "Go to the same level previous N'th thread.
10842 Returns the difference between N and the number of skips actually
10843 done."
10844   (interactive "p")
10845   (gnus-summary-next-thread (- n)))
10846
10847 (defun gnus-summary-go-down-thread ()
10848   "Go down one level in the current thread."
10849   (let ((children (gnus-summary-article-children)))
10850     (when children
10851       (gnus-summary-goto-subject (car children)))))
10852
10853 (defun gnus-summary-go-up-thread ()
10854   "Go up one level in the current thread."
10855   (let ((parent (gnus-summary-article-parent)))
10856     (when parent
10857       (gnus-summary-goto-subject parent))))
10858
10859 (defun gnus-summary-down-thread (n)
10860   "Go down thread N steps.
10861 If N is negative, go up instead.
10862 Returns the difference between N and how many steps down that were
10863 taken."
10864   (interactive "p")
10865   (let ((up (< n 0))
10866         (n (abs n)))
10867     (while (and (> n 0)
10868                 (if up (gnus-summary-go-up-thread)
10869                   (gnus-summary-go-down-thread)))
10870       (setq n (1- n)))
10871     (gnus-summary-position-point)
10872     (when (/= 0 n)
10873       (gnus-message 7 "Can't go further"))
10874     n))
10875
10876 (defun gnus-summary-up-thread (n)
10877   "Go up thread N steps.
10878 If N is negative, go down instead.
10879 Returns the difference between N and how many steps down that were
10880 taken."
10881   (interactive "p")
10882   (gnus-summary-down-thread (- n)))
10883
10884 (defun gnus-summary-top-thread ()
10885   "Go to the top of the thread."
10886   (interactive)
10887   (while (gnus-summary-go-up-thread))
10888   (gnus-summary-article-number))
10889
10890 (defun gnus-summary-kill-thread (&optional unmark)
10891   "Mark articles under current thread as read.
10892 If the prefix argument is positive, remove any kinds of marks.
10893 If the prefix argument is negative, tick articles instead."
10894   (interactive "P")
10895   (when unmark
10896     (setq unmark (prefix-numeric-value unmark)))
10897   (let ((articles (gnus-summary-articles-in-thread)))
10898     (save-excursion
10899       ;; Expand the thread.
10900       (gnus-summary-show-thread)
10901       ;; Mark all the articles.
10902       (while articles
10903         (gnus-summary-goto-subject (car articles))
10904         (cond ((null unmark)
10905                (gnus-summary-mark-article-as-read gnus-killed-mark))
10906               ((> unmark 0)
10907                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10908               (t
10909                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10910         (setq articles (cdr articles))))
10911     ;; Hide killed subtrees.
10912     (and (null unmark)
10913          gnus-thread-hide-killed
10914          (gnus-summary-hide-thread))
10915     ;; If marked as read, go to next unread subject.
10916     (when (null unmark)
10917       ;; Go to next unread subject.
10918       (gnus-summary-next-subject 1 t)))
10919   (gnus-set-mode-line 'summary))
10920
10921 ;; Summary sorting commands
10922
10923 (defun gnus-summary-sort-by-number (&optional reverse)
10924   "Sort the summary buffer by article number.
10925 Argument REVERSE means reverse order."
10926   (interactive "P")
10927   (gnus-summary-sort 'number reverse))
10928
10929 (defun gnus-summary-sort-by-random (&optional reverse)
10930   "Randomize the order in the summary buffer.
10931 Argument REVERSE means to randomize in reverse order."
10932   (interactive "P")
10933   (gnus-summary-sort 'random reverse))
10934
10935 (defun gnus-summary-sort-by-author (&optional reverse)
10936   "Sort the summary buffer by author name alphabetically.
10937 If `case-fold-search' is non-nil, case of letters is ignored.
10938 Argument REVERSE means reverse order."
10939   (interactive "P")
10940   (gnus-summary-sort 'author reverse))
10941
10942 (defun gnus-summary-sort-by-recipient (&optional reverse)
10943   "Sort the summary buffer by recipient name alphabetically.
10944 If `case-fold-search' is non-nil, case of letters is ignored.
10945 Argument REVERSE means reverse order."
10946   (interactive "P")
10947   (gnus-summary-sort 'recipient reverse))
10948
10949 (defun gnus-summary-sort-by-subject (&optional reverse)
10950   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10951 If `case-fold-search' is non-nil, case of letters is ignored.
10952 Argument REVERSE means reverse order."
10953   (interactive "P")
10954   (gnus-summary-sort 'subject reverse))
10955
10956 (defun gnus-summary-sort-by-date (&optional reverse)
10957   "Sort the summary buffer by date.
10958 Argument REVERSE means reverse order."
10959   (interactive "P")
10960   (gnus-summary-sort 'date reverse))
10961
10962 (defun gnus-summary-sort-by-score (&optional reverse)
10963   "Sort the summary buffer by score.
10964 Argument REVERSE means reverse order."
10965   (interactive "P")
10966   (gnus-summary-sort 'score reverse))
10967
10968 (defun gnus-summary-sort-by-lines (&optional reverse)
10969   "Sort the summary buffer by the number of lines.
10970 Argument REVERSE means reverse order."
10971   (interactive "P")
10972   (gnus-summary-sort 'lines reverse))
10973
10974 (defun gnus-summary-sort-by-chars (&optional reverse)
10975   "Sort the summary buffer by article length.
10976 Argument REVERSE means reverse order."
10977   (interactive "P")
10978   (gnus-summary-sort 'chars reverse))
10979
10980 (defun gnus-summary-sort-by-original (&optional reverse)
10981   "Sort the summary buffer using the default sorting method.
10982 Argument REVERSE means reverse order."
10983   (interactive "P")
10984   (let* ((buffer-read-only)
10985          (gnus-summary-prepare-hook nil))
10986     ;; We do the sorting by regenerating the threads.
10987     (gnus-summary-prepare)
10988     ;; Hide subthreads if needed.
10989     (gnus-summary-maybe-hide-threads)))
10990
10991 (defun gnus-summary-sort (predicate reverse)
10992   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10993   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10994          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10995          (gnus-thread-sort-functions
10996           (if (not reverse)
10997               thread
10998             `(lambda (t1 t2)
10999                (,thread t2 t1))))
11000          (gnus-sort-gathered-threads-function
11001           gnus-thread-sort-functions)
11002          (gnus-article-sort-functions
11003           (if (not reverse)
11004               article
11005             `(lambda (t1 t2)
11006                (,article t2 t1))))
11007          (buffer-read-only)
11008          (gnus-summary-prepare-hook nil))
11009     ;; We do the sorting by regenerating the threads.
11010     (gnus-summary-prepare)
11011     ;; Hide subthreads if needed.
11012     (gnus-summary-maybe-hide-threads)))
11013
11014 ;; Summary saving commands.
11015
11016 (defun gnus-summary-save-article (&optional n not-saved)
11017   "Save the current article using the default saver function.
11018 If N is a positive number, save the N next articles.
11019 If N is a negative number, save the N previous articles.
11020 If N is nil and any articles have been marked with the process mark,
11021 save those articles instead.
11022 The variable `gnus-default-article-saver' specifies the saver function."
11023   (interactive "P")
11024   (let* ((articles (gnus-summary-work-articles n))
11025          (save-buffer (save-excursion
11026                         (nnheader-set-temp-buffer " *Gnus Save*")))
11027          (num (length articles))
11028          header file)
11029     (dolist (article articles)
11030       (setq header (gnus-summary-article-header article))
11031       (if (not (vectorp header))
11032           ;; This is a pseudo-article.
11033           (if (assq 'name header)
11034               (gnus-copy-file (cdr (assq 'name header)))
11035             (gnus-message 1 "Article %d is unsaveable" article))
11036         ;; This is a real article.
11037         (save-window-excursion
11038           (let ((gnus-display-mime-function nil)
11039                 (gnus-article-prepare-hook nil))
11040             (gnus-summary-select-article t nil nil article)))
11041         (save-excursion
11042           (set-buffer save-buffer)
11043           (erase-buffer)
11044           (insert-buffer-substring gnus-original-article-buffer))
11045         (setq file (gnus-article-save save-buffer file num))
11046         (gnus-summary-remove-process-mark article)
11047         (unless not-saved
11048           (gnus-summary-set-saved-mark article))))
11049     (gnus-kill-buffer save-buffer)
11050     (gnus-summary-position-point)
11051     (gnus-set-mode-line 'summary)
11052     n))
11053
11054 (defun gnus-summary-pipe-output (&optional arg headers)
11055   "Pipe the current article to a subprocess.
11056 If N is a positive number, pipe the N next articles.
11057 If N is a negative number, pipe the N previous articles.
11058 If N is nil and any articles have been marked with the process mark,
11059 pipe those articles instead.
11060 If HEADERS (the symbolic prefix), include the headers, too."
11061   (interactive (gnus-interactive "P\ny"))
11062   (require 'gnus-art)
11063   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11064         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11065     (gnus-summary-save-article arg t))
11066   (let ((buffer (get-buffer "*Shell Command Output*")))
11067     (when (and buffer
11068                (not (zerop (buffer-size buffer))))
11069       (gnus-configure-windows 'pipe))))
11070
11071 (defun gnus-summary-save-article-mail (&optional arg)
11072   "Append the current article to an mail file.
11073 If N is a positive number, save the N next articles.
11074 If N is a negative number, save the N previous articles.
11075 If N is nil and any articles have been marked with the process mark,
11076 save those articles instead."
11077   (interactive "P")
11078   (require 'gnus-art)
11079   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11080     (gnus-summary-save-article arg)))
11081
11082 (defun gnus-summary-save-article-rmail (&optional arg)
11083   "Append the current article to an rmail file.
11084 If N is a positive number, save the N next articles.
11085 If N is a negative number, save the N previous articles.
11086 If N is nil and any articles have been marked with the process mark,
11087 save those articles instead."
11088   (interactive "P")
11089   (require 'gnus-art)
11090   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11091     (gnus-summary-save-article arg)))
11092
11093 (defun gnus-summary-save-article-file (&optional arg)
11094   "Append the current article to a file.
11095 If N is a positive number, save the N next articles.
11096 If N is a negative number, save the N previous articles.
11097 If N is nil and any articles have been marked with the process mark,
11098 save those articles instead."
11099   (interactive "P")
11100   (require 'gnus-art)
11101   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11102     (gnus-summary-save-article arg)))
11103
11104 (defun gnus-summary-write-article-file (&optional arg)
11105   "Write the current article to a file, deleting the previous file.
11106 If N is a positive number, save the N next articles.
11107 If N is a negative number, save the N previous articles.
11108 If N is nil and any articles have been marked with the process mark,
11109 save those articles instead."
11110   (interactive "P")
11111   (require 'gnus-art)
11112   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11113     (gnus-summary-save-article arg)))
11114
11115 (defun gnus-summary-save-article-body-file (&optional arg)
11116   "Append the current article body to a file.
11117 If N is a positive number, save the N next articles.
11118 If N is a negative number, save the N previous articles.
11119 If N is nil and any articles have been marked with the process mark,
11120 save those articles instead."
11121   (interactive "P")
11122   (require 'gnus-art)
11123   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11124     (gnus-summary-save-article arg)))
11125
11126 (defun gnus-summary-muttprint (&optional arg)
11127   "Print the current article using Muttprint.
11128 If N is a positive number, save the N next articles.
11129 If N is a negative number, save the N previous articles.
11130 If N is nil and any articles have been marked with the process mark,
11131 save those articles instead."
11132   (interactive "P")
11133   (require 'gnus-art)
11134   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11135     (gnus-summary-save-article arg t)))
11136
11137 (defun gnus-summary-pipe-message (program)
11138   "Pipe the current article through PROGRAM."
11139   (interactive "sProgram: ")
11140   (gnus-summary-select-article)
11141   (let ((mail-header-separator ""))
11142     (gnus-eval-in-buffer-window gnus-article-buffer
11143       (save-restriction
11144         (widen)
11145         (let ((start (window-start))
11146               buffer-read-only)
11147           (message-pipe-buffer-body program)
11148           (set-window-start (get-buffer-window (current-buffer)) start))))))
11149
11150 (defun gnus-get-split-value (methods)
11151   "Return a value based on the split METHODS."
11152   (let (split-name method result match)
11153     (when methods
11154       (save-excursion
11155         (set-buffer gnus-original-article-buffer)
11156         (save-restriction
11157           (nnheader-narrow-to-headers)
11158           (while (and methods (not split-name))
11159             (goto-char (point-min))
11160             (setq method (pop methods))
11161             (setq match (car method))
11162             (when (cond
11163                    ((stringp match)
11164                     ;; Regular expression.
11165                     (ignore-errors
11166                       (re-search-forward match nil t)))
11167                    ((functionp match)
11168                     ;; Function.
11169                     (save-restriction
11170                       (widen)
11171                       (setq result (funcall match gnus-newsgroup-name))))
11172                    ((consp match)
11173                     ;; Form.
11174                     (save-restriction
11175                       (widen)
11176                       (setq result (eval match)))))
11177               (setq split-name (cdr method))
11178               (cond ((stringp result)
11179                      (push (expand-file-name
11180                             result gnus-article-save-directory)
11181                            split-name))
11182                     ((consp result)
11183                      (setq split-name (append result split-name)))))))))
11184     (nreverse split-name)))
11185
11186 (defun gnus-valid-move-group-p (group)
11187   (and (boundp group)
11188        (symbol-name group)
11189        (symbol-value group)
11190        (gnus-get-function (gnus-find-method-for-group
11191                            (symbol-name group)) 'request-accept-article t)))
11192
11193 (defun gnus-read-move-group-name (prompt default articles prefix)
11194   "Read a group name."
11195   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11196          (minibuffer-confirm-incomplete nil) ; XEmacs
11197          (prom
11198           (format "%s %s to:"
11199                   prompt
11200                   (if (> (length articles) 1)
11201                       (format "these %d articles" (length articles))
11202                     "this article")))
11203          (to-newsgroup
11204           (cond
11205            ((null split-name)
11206             (gnus-completing-read-with-default
11207              default prom
11208              gnus-active-hashtb
11209              'gnus-valid-move-group-p
11210              nil prefix
11211              'gnus-group-history))
11212            ((= 1 (length split-name))
11213             (gnus-completing-read-with-default
11214              (car split-name) prom
11215              gnus-active-hashtb
11216              'gnus-valid-move-group-p
11217              nil nil
11218              'gnus-group-history))
11219            (t
11220             (gnus-completing-read-with-default
11221              nil prom
11222              (mapcar 'list (nreverse split-name))
11223              nil nil nil
11224              'gnus-group-history))))
11225          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11226     (when to-newsgroup
11227       (if (or (string= to-newsgroup "")
11228               (string= to-newsgroup prefix))
11229           (setq to-newsgroup default))
11230       (unless to-newsgroup
11231         (error "No group name entered"))
11232       (or (gnus-active to-newsgroup)
11233           (gnus-activate-group to-newsgroup nil nil to-method)
11234           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11235                                      to-newsgroup))
11236               (or (and (gnus-request-create-group to-newsgroup to-method)
11237                        (gnus-activate-group
11238                         to-newsgroup nil nil to-method)
11239                        (gnus-subscribe-group to-newsgroup))
11240                   (error "Couldn't create group %s" to-newsgroup)))
11241           (error "No such group: %s" to-newsgroup)))
11242     to-newsgroup))
11243
11244 (defun gnus-summary-save-parts (type dir n &optional reverse)
11245   "Save parts matching TYPE to DIR.
11246 If REVERSE, save parts that do not match TYPE."
11247   (interactive
11248    (list (read-string "Save parts of type: "
11249                       (or (car gnus-summary-save-parts-type-history)
11250                           gnus-summary-save-parts-default-mime)
11251                       'gnus-summary-save-parts-type-history)
11252          (setq gnus-summary-save-parts-last-directory
11253                (read-file-name "Save to directory: "
11254                                gnus-summary-save-parts-last-directory
11255                                nil t))
11256          current-prefix-arg))
11257   (gnus-summary-iterate n
11258     (let ((gnus-display-mime-function nil)
11259           (gnus-inhibit-treatment t))
11260       (gnus-summary-select-article))
11261     (save-excursion
11262       (set-buffer gnus-article-buffer)
11263       (let ((handles (or gnus-article-mime-handles
11264                          (mm-dissect-buffer nil gnus-article-loose-mime)
11265                          (and gnus-article-emulate-mime
11266                               (mm-uu-dissect)))))
11267         (when handles
11268           (gnus-summary-save-parts-1 type dir handles reverse)
11269           (unless gnus-article-mime-handles ;; Don't destroy this case.
11270             (mm-destroy-parts handles)))))))
11271
11272 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11273   (if (stringp (car handle))
11274       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11275               (cdr handle))
11276     (when (if reverse
11277               (not (string-match type (mm-handle-media-type handle)))
11278             (string-match type (mm-handle-media-type handle)))
11279       (let ((file (expand-file-name
11280                    (gnus-map-function
11281                     mm-file-name-rewrite-functions
11282                     (file-name-nondirectory
11283                      (or
11284                       (mail-content-type-get
11285                        (mm-handle-disposition handle) 'filename)
11286                       (mail-content-type-get
11287                        (mm-handle-type handle) 'name)
11288                       (concat gnus-newsgroup-name
11289                               "." (number-to-string
11290                                    (cdr gnus-article-current))))))
11291                    dir)))
11292         (unless (file-exists-p file)
11293           (mm-save-part-to-file handle file))))))
11294
11295 ;; Summary extract commands
11296
11297 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11298   (let ((buffer-read-only nil)
11299         (article (gnus-summary-article-number))
11300         after-article b e)
11301     (unless (gnus-summary-goto-subject article)
11302       (error "No such article: %d" article))
11303     (gnus-summary-position-point)
11304     ;; If all commands are to be bunched up on one line, we collect
11305     ;; them here.
11306     (unless gnus-view-pseudos-separately
11307       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11308             files action)
11309         (while ps
11310           (setq action (cdr (assq 'action (car ps))))
11311           (setq files (list (cdr (assq 'name (car ps)))))
11312           (while (and ps (cdr ps)
11313                       (string= (or action "1")
11314                                (or (cdr (assq 'action (cadr ps))) "2")))
11315             (push (cdr (assq 'name (cadr ps))) files)
11316             (setcdr ps (cddr ps)))
11317           (when files
11318             (when (not (string-match "%s" action))
11319               (push " " files))
11320             (push " " files)
11321             (when (assq 'execute (car ps))
11322               (setcdr (assq 'execute (car ps))
11323                       (funcall (if (string-match "%s" action)
11324                                    'format 'concat)
11325                                action
11326                                (mapconcat
11327                                 (lambda (f)
11328                                   (if (equal f " ")
11329                                       f
11330                                     (shell-quote-argument f)))
11331                                 files " ")))))
11332           (setq ps (cdr ps)))))
11333     (if (and gnus-view-pseudos (not not-view))
11334         (while pslist
11335           (when (assq 'execute (car pslist))
11336             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11337                                   (eq gnus-view-pseudos 'not-confirm)))
11338           (setq pslist (cdr pslist)))
11339       (save-excursion
11340         (while pslist
11341           (setq after-article (or (cdr (assq 'article (car pslist)))
11342                                   (gnus-summary-article-number)))
11343           (gnus-summary-goto-subject after-article)
11344           (forward-line 1)
11345           (setq b (point))
11346           (insert "    " (file-name-nondirectory
11347                           (cdr (assq 'name (car pslist))))
11348                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11349           (setq e (point))
11350           (forward-line -1)             ; back to `b'
11351           (gnus-add-text-properties
11352            b (1- e) (list 'gnus-number gnus-reffed-article-number
11353                           gnus-mouse-face-prop gnus-mouse-face))
11354           (gnus-data-enter
11355            after-article gnus-reffed-article-number
11356            gnus-unread-mark b (car pslist) 0 (- e b))
11357           (setq gnus-newsgroup-unreads
11358                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11359                                          gnus-reffed-article-number))
11360           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11361           (setq pslist (cdr pslist)))))))
11362
11363 (defun gnus-pseudos< (p1 p2)
11364   (let ((c1 (cdr (assq 'action p1)))
11365         (c2 (cdr (assq 'action p2))))
11366     (and c1 c2 (string< c1 c2))))
11367
11368 (defun gnus-request-pseudo-article (props)
11369   (cond ((assq 'execute props)
11370          (gnus-execute-command (cdr (assq 'execute props)))))
11371   (let ((gnus-current-article (gnus-summary-article-number)))
11372     (gnus-run-hooks 'gnus-mark-article-hook)))
11373
11374 (defun gnus-execute-command (command &optional automatic)
11375   (save-excursion
11376     (gnus-article-setup-buffer)
11377     (set-buffer gnus-article-buffer)
11378     (setq buffer-read-only nil)
11379     (let ((command (if automatic command
11380                      (read-string "Command: " (cons command 0)))))
11381       (erase-buffer)
11382       (insert "$ " command "\n\n")
11383       (if gnus-view-pseudo-asynchronously
11384           (start-process "gnus-execute" (current-buffer) shell-file-name
11385                          shell-command-switch command)
11386         (call-process shell-file-name nil t nil
11387                       shell-command-switch command)))))
11388
11389 ;; Summary kill commands.
11390
11391 (defun gnus-summary-edit-global-kill (article)
11392   "Edit the \"global\" kill file."
11393   (interactive (list (gnus-summary-article-number)))
11394   (gnus-group-edit-global-kill article))
11395
11396 (defun gnus-summary-edit-local-kill ()
11397   "Edit a local kill file applied to the current newsgroup."
11398   (interactive)
11399   (setq gnus-current-headers (gnus-summary-article-header))
11400   (gnus-group-edit-local-kill
11401    (gnus-summary-article-number) gnus-newsgroup-name))
11402
11403 ;;; Header reading.
11404
11405 (defun gnus-read-header (id &optional header)
11406   "Read the headers of article ID and enter them into the Gnus system."
11407   (let ((group gnus-newsgroup-name)
11408         (gnus-override-method
11409          (or
11410           gnus-override-method
11411           (and (gnus-news-group-p gnus-newsgroup-name)
11412                (car (gnus-refer-article-methods)))))
11413         where)
11414     ;; First we check to see whether the header in question is already
11415     ;; fetched.
11416     (if (stringp id)
11417         ;; This is a Message-ID.
11418         (setq header (or header (gnus-id-to-header id)))
11419       ;; This is an article number.
11420       (setq header (or header (gnus-summary-article-header id))))
11421     (if (and header
11422              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11423         ;; We have found the header.
11424         header
11425       ;; We have to really fetch the header to this article.
11426       (save-excursion
11427         (set-buffer nntp-server-buffer)
11428         (when (setq where (gnus-request-head id group))
11429           (nnheader-fold-continuation-lines)
11430           (goto-char (point-max))
11431           (insert ".\n")
11432           (goto-char (point-min))
11433           (insert "211 ")
11434           (princ (cond
11435                   ((numberp id) id)
11436                   ((cdr where) (cdr where))
11437                   (header (mail-header-number header))
11438                   (t gnus-reffed-article-number))
11439                  (current-buffer))
11440           (insert " Article retrieved.\n"))
11441         (if (or (not where)
11442                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11443             ()                          ; Malformed head.
11444           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11445             (when (and (stringp id)
11446                        (not (string= (gnus-group-real-name group)
11447                                      (car where))))
11448               ;; If we fetched by Message-ID and the article came
11449               ;; from a different group, we fudge some bogus article
11450               ;; numbers for this article.
11451               (mail-header-set-number header gnus-reffed-article-number))
11452             (save-excursion
11453               (set-buffer gnus-summary-buffer)
11454               (decf gnus-reffed-article-number)
11455               (gnus-remove-header (mail-header-number header))
11456               (push header gnus-newsgroup-headers)
11457               (setq gnus-current-headers header)
11458               (push (mail-header-number header) gnus-newsgroup-limit)))
11459           header)))))
11460
11461 (defun gnus-remove-header (number)
11462   "Remove header NUMBER from `gnus-newsgroup-headers'."
11463   (if (and gnus-newsgroup-headers
11464            (= number (mail-header-number (car gnus-newsgroup-headers))))
11465       (pop gnus-newsgroup-headers)
11466     (let ((headers gnus-newsgroup-headers))
11467       (while (and (cdr headers)
11468                   (not (= number (mail-header-number (cadr headers)))))
11469         (pop headers))
11470       (when (cdr headers)
11471         (setcdr headers (cddr headers))))))
11472
11473 ;;;
11474 ;;; summary highlights
11475 ;;;
11476
11477 (defun gnus-highlight-selected-summary ()
11478   "Highlight selected article in summary buffer."
11479   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11480   (when gnus-summary-selected-face
11481     (save-excursion
11482       (let* ((beg (point-at-bol))
11483              (end (point-at-eol))
11484              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11485              (from (if (get-text-property beg gnus-mouse-face-prop)
11486                        beg
11487                      (or (next-single-property-change
11488                           beg gnus-mouse-face-prop nil end)
11489                          beg)))
11490              (to
11491               (if (= from end)
11492                   (- from 2)
11493                 (or (next-single-property-change
11494                      from gnus-mouse-face-prop nil end)
11495                     end))))
11496         ;; If no mouse-face prop on line we will have to = from = end,
11497         ;; so we highlight the entire line instead.
11498         (when (= (+ to 2) from)
11499           (setq from beg)
11500           (setq to end))
11501         (if gnus-newsgroup-selected-overlay
11502             ;; Move old overlay.
11503             (gnus-move-overlay
11504              gnus-newsgroup-selected-overlay from to (current-buffer))
11505           ;; Create new overlay.
11506           (gnus-overlay-put
11507            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11508            'face gnus-summary-selected-face))))))
11509
11510 (defvar gnus-summary-highlight-line-cached nil)
11511 (defvar gnus-summary-highlight-line-trigger nil)
11512
11513 (defun gnus-summary-highlight-line-0 ()
11514   (if (and (eq gnus-summary-highlight-line-trigger
11515                gnus-summary-highlight)
11516            gnus-summary-highlight-line-cached)
11517       gnus-summary-highlight-line-cached
11518     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11519           gnus-summary-highlight-line-cached
11520           (let* ((cond (list 'cond))
11521                  (c cond)
11522                  (list gnus-summary-highlight))
11523             (while list
11524               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11525                               nil))
11526               (setq c (cdr c)
11527                     list (cdr list)))
11528             (gnus-byte-compile (list 'lambda nil cond))))))
11529
11530 (defun gnus-summary-highlight-line ()
11531   "Highlight current line according to `gnus-summary-highlight'."
11532   (let* ((beg (point-at-bol))
11533          (article (or (gnus-summary-article-number) gnus-current-article))
11534          (score (or (cdr (assq article
11535                                gnus-newsgroup-scored))
11536                     gnus-summary-default-score 0))
11537          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11538          (inhibit-read-only t)
11539          (default gnus-summary-default-score)
11540          (default-high gnus-summary-default-high-score)
11541          (default-low gnus-summary-default-low-score)
11542          (uncached (and gnus-summary-use-undownloaded-faces
11543                         (memq article gnus-newsgroup-undownloaded)
11544                         (not (memq article gnus-newsgroup-cached)))))
11545     (let ((face (funcall (gnus-summary-highlight-line-0))))
11546       (unless (eq face (get-text-property beg 'face))
11547         (gnus-put-text-property-excluding-characters-with-faces
11548          beg (point-at-eol) 'face
11549          (setq face (if (boundp face) (symbol-value face) face)))
11550         (when gnus-summary-highlight-line-function
11551           (funcall gnus-summary-highlight-line-function article face))))))
11552
11553 (defun gnus-update-read-articles (group unread &optional compute)
11554   "Update the list of read articles in GROUP.
11555 UNREAD is a sorted list."
11556   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11557         (info (gnus-get-info group))
11558         (prev 1)
11559         read)
11560     (if (or (not info) (not active))
11561         ;; There is no info on this group if it was, in fact,
11562         ;; killed.  Gnus stores no information on killed groups, so
11563         ;; there's nothing to be done.
11564         ;; One could store the information somewhere temporarily,
11565         ;; perhaps...  Hmmm...
11566         ()
11567       ;; Remove any negative articles numbers.
11568       (while (and unread (< (car unread) 0))
11569         (setq unread (cdr unread)))
11570       ;; Remove any expired article numbers
11571       (while (and unread (< (car unread) (car active)))
11572         (setq unread (cdr unread)))
11573       ;; Compute the ranges of read articles by looking at the list of
11574       ;; unread articles.
11575       (while unread
11576         (when (/= (car unread) prev)
11577           (push (if (= prev (1- (car unread))) prev
11578                   (cons prev (1- (car unread))))
11579                 read))
11580         (setq prev (1+ (car unread)))
11581         (setq unread (cdr unread)))
11582       (when (<= prev (cdr active))
11583         (push (cons prev (cdr active)) read))
11584       (setq read (if (> (length read) 1) (nreverse read) read))
11585       (if compute
11586           read
11587         (save-excursion
11588           (let (setmarkundo)
11589             ;; Propagate the read marks to the backend.
11590             (when (gnus-check-backend-function 'request-set-mark group)
11591               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11592                     (add (gnus-remove-from-range read (gnus-info-read info))))
11593                 (when (or add del)
11594                   (unless (gnus-check-group group)
11595                     (error "Can't open server for %s" group))
11596                   (gnus-request-set-mark
11597                    group (delq nil (list (if add (list add 'add '(read)))
11598                                          (if del (list del 'del '(read))))))
11599                   (setq setmarkundo
11600                         `(gnus-request-set-mark
11601                           ,group
11602                           ',(delq nil (list
11603                                        (if del (list del 'add '(read)))
11604                                        (if add (list add 'del '(read))))))))))
11605             (set-buffer gnus-group-buffer)
11606             (gnus-undo-register
11607               `(progn
11608                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11609                  (gnus-info-set-read ',info ',(gnus-info-read info))
11610                  (gnus-get-unread-articles-in-group ',info
11611                                                     (gnus-active ,group))
11612                  (gnus-group-update-group ,group t)
11613                  ,setmarkundo))))
11614         ;; Enter this list into the group info.
11615         (gnus-info-set-read info read)
11616         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11617         (gnus-get-unread-articles-in-group info (gnus-active group))
11618         t))))
11619
11620 (defun gnus-offer-save-summaries ()
11621   "Offer to save all active summary buffers."
11622   (let (buffers)
11623     ;; Go through all buffers and find all summaries.
11624     (dolist (buffer (buffer-list))
11625       (when (and (setq buffer (buffer-name buffer))
11626                  (string-match "Summary" buffer)
11627                  (with-current-buffer buffer
11628                    ;; We check that this is, indeed, a summary buffer.
11629                    (and (eq major-mode 'gnus-summary-mode)
11630                         ;; Also make sure this isn't bogus.
11631                         gnus-newsgroup-prepared
11632                         ;; Also make sure that this isn't a
11633                         ;; dead summary buffer.
11634                         (not gnus-dead-summary-mode))))
11635         (push buffer buffers)))
11636     ;; Go through all these summary buffers and offer to save them.
11637     (when buffers
11638       (save-excursion
11639         (map-y-or-n-p
11640          "Update summary buffer %s? "
11641          (lambda (buf)
11642            (switch-to-buffer buf)
11643            (gnus-summary-exit))
11644          buffers)))))
11645
11646 (defun gnus-summary-setup-default-charset ()
11647   "Setup newsgroup default charset."
11648   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11649       (setq gnus-newsgroup-charset nil)
11650     (let* ((ignored-charsets
11651             (or gnus-newsgroup-ephemeral-ignored-charsets
11652                 (append
11653                  (and gnus-newsgroup-name
11654                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11655                  gnus-newsgroup-ignored-charsets))))
11656       (setq gnus-newsgroup-charset
11657             (or gnus-newsgroup-ephemeral-charset
11658                 (and gnus-newsgroup-name
11659                      (gnus-parameter-charset gnus-newsgroup-name))
11660                 gnus-default-charset))
11661       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11662            ignored-charsets))))
11663
11664 ;;;
11665 ;;; Mime Commands
11666 ;;;
11667
11668 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11669   "Display the current article buffer fully MIME-buttonized.
11670 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11671 treated as multipart/mixed."
11672   (interactive "P")
11673   (require 'gnus-art)
11674   (let ((gnus-unbuttonized-mime-types nil)
11675         (gnus-mime-display-multipart-as-mixed show-all-parts))
11676     (gnus-summary-show-article)))
11677
11678 (defun gnus-summary-repair-multipart (article)
11679   "Add a Content-Type header to a multipart article without one."
11680   (interactive (list (gnus-summary-article-number)))
11681   (gnus-with-article article
11682     (message-narrow-to-head)
11683     (message-remove-header "Mime-Version")
11684     (goto-char (point-max))
11685     (insert "Mime-Version: 1.0\n")
11686     (widen)
11687     (when (search-forward "\n--" nil t)
11688       (let ((separator (buffer-substring (point) (point-at-eol))))
11689         (message-narrow-to-head)
11690         (message-remove-header "Content-Type")
11691         (goto-char (point-max))
11692         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11693                         separator))
11694         (widen))))
11695   (let (gnus-mark-article-hook)
11696     (gnus-summary-select-article t t nil article)))
11697
11698 (defun gnus-summary-toggle-display-buttonized ()
11699   "Toggle the buttonizing of the article buffer."
11700   (interactive)
11701   (require 'gnus-art)
11702   (if (setq gnus-inhibit-mime-unbuttonizing
11703             (not gnus-inhibit-mime-unbuttonizing))
11704       (let ((gnus-unbuttonized-mime-types nil))
11705         (gnus-summary-show-article))
11706     (gnus-summary-show-article)))
11707
11708 ;;;
11709 ;;; Generic summary marking commands
11710 ;;;
11711
11712 (defvar gnus-summary-marking-alist
11713   '((read gnus-del-mark "d")
11714     (unread gnus-unread-mark "u")
11715     (ticked gnus-ticked-mark "!")
11716     (dormant gnus-dormant-mark "?")
11717     (expirable gnus-expirable-mark "e"))
11718   "An alist of names/marks/keystrokes.")
11719
11720 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11721 (defvar gnus-summary-mark-map)
11722
11723 (defun gnus-summary-make-all-marking-commands ()
11724   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11725   (dolist (elem gnus-summary-marking-alist)
11726     (apply 'gnus-summary-make-marking-command elem)))
11727
11728 (defun gnus-summary-make-marking-command (name mark keystroke)
11729   (let ((map (make-sparse-keymap)))
11730     (define-key gnus-summary-generic-mark-map keystroke map)
11731     (dolist (lway `((next "next" next nil "n")
11732                     (next-unread "next unread" next t "N")
11733                     (prev "previous" prev nil "p")
11734                     (prev-unread "previous unread" prev t "P")
11735                     (nomove "" nil nil ,keystroke)))
11736       (let ((func (gnus-summary-make-marking-command-1
11737                    mark (car lway) lway name)))
11738         (setq func (eval func))
11739         (define-key map (nth 4 lway) func)))))
11740
11741 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11742   `(defun ,(intern
11743             (format "gnus-summary-put-mark-as-%s%s"
11744                     name (if (eq way 'nomove)
11745                              ""
11746                            (concat "-" (symbol-name way)))))
11747      (n)
11748      ,(format
11749        "Mark the current article as %s%s.
11750 If N, the prefix, then repeat N times.
11751 If N is negative, move in reverse order.
11752 The difference between N and the actual number of articles marked is
11753 returned."
11754        name (cadr lway))
11755      (interactive "p")
11756      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11757
11758 (defun gnus-summary-generic-mark (n mark move unread)
11759   "Mark N articles with MARK."
11760   (unless (eq major-mode 'gnus-summary-mode)
11761     (error "This command can only be used in the summary buffer"))
11762   (gnus-summary-show-thread)
11763   (let ((nummove
11764          (cond
11765           ((eq move 'next) 1)
11766           ((eq move 'prev) -1)
11767           (t 0))))
11768     (if (zerop nummove)
11769         (setq n 1)
11770       (when (< n 0)
11771         (setq n (abs n)
11772               nummove (* -1 nummove))))
11773     (while (and (> n 0)
11774                 (gnus-summary-mark-article nil mark)
11775                 (zerop (gnus-summary-next-subject nummove unread t)))
11776       (setq n (1- n)))
11777     (when (/= 0 n)
11778       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11779     (gnus-summary-recenter)
11780     (gnus-summary-position-point)
11781     (gnus-set-mode-line 'summary)
11782     n))
11783
11784 (defun gnus-summary-insert-articles (articles)
11785   (when (setq articles
11786               (gnus-sorted-difference articles
11787                                       (mapcar (lambda (h)
11788                                                 (mail-header-number h))
11789                                               gnus-newsgroup-headers)))
11790     (setq gnus-newsgroup-headers
11791           (gnus-merge 'list
11792                       gnus-newsgroup-headers
11793                       (gnus-fetch-headers articles)
11794                       'gnus-article-sort-by-number))
11795     ;; Suppress duplicates?
11796     (when gnus-suppress-duplicates
11797       (gnus-dup-suppress-articles))
11798
11799     ;; We might want to build some more threads first.
11800     (when (and gnus-fetch-old-headers
11801                (eq gnus-headers-retrieved-by 'nov))
11802       (if (eq gnus-fetch-old-headers 'invisible)
11803           (gnus-build-all-threads)
11804         (gnus-build-old-threads)))
11805     ;; Let the Gnus agent mark articles as read.
11806     (when gnus-agent
11807       (gnus-agent-get-undownloaded-list))
11808     ;; Remove list identifiers from subject
11809     (when gnus-list-identifiers
11810       (gnus-summary-remove-list-identifiers))
11811     ;; First and last article in this newsgroup.
11812     (when gnus-newsgroup-headers
11813       (setq gnus-newsgroup-begin
11814             (mail-header-number (car gnus-newsgroup-headers))
11815             gnus-newsgroup-end
11816             (mail-header-number
11817              (gnus-last-element gnus-newsgroup-headers))))
11818     (when gnus-use-scoring
11819       (gnus-possibly-score-headers))))
11820
11821 (defun gnus-summary-insert-old-articles (&optional all)
11822   "Insert all old articles in this group.
11823 If ALL is non-nil, already read articles become readable.
11824 If ALL is a number, fetch this number of articles."
11825   (interactive "P")
11826   (prog1
11827       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11828             older len)
11829         (setq older
11830               ;; Some nntp servers lie about their active range.  When
11831               ;; this happens, the active range can be in the millions.
11832               ;; Use a compressed range to avoid creating a huge list.
11833               (gnus-range-difference (list gnus-newsgroup-active) old))
11834         (setq len (gnus-range-length older))
11835         (cond
11836          ((null older) nil)
11837          ((numberp all)
11838           (if (< all len)
11839               (let ((older-range (nreverse older)))
11840                 (setq older nil)
11841
11842                 (while (> all 0)
11843                   (let* ((r (pop older-range))
11844                          (min (if (numberp r) r (car r)))
11845                          (max (if (numberp r) r (cdr r))))
11846                     (while (and (<= min max)
11847                                 (> all 0))
11848                       (push max older)
11849                       (setq all (1- all)
11850                             max (1- max))))))
11851             (setq older (gnus-uncompress-range older))))
11852          (all
11853           (setq older (gnus-uncompress-range older)))
11854          (t
11855           (when (and (numberp gnus-large-newsgroup)
11856                    (> len gnus-large-newsgroup))
11857               (let* ((cursor-in-echo-area nil)
11858                      (initial (gnus-parameter-large-newsgroup-initial
11859                                gnus-newsgroup-name))
11860                      (input
11861                       (read-string
11862                        (format
11863                         "How many articles from %s (%s %d): "
11864                         (gnus-limit-string
11865                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11866                         (if initial "max" "default")
11867                         len)
11868                        (if initial
11869                            (cons (number-to-string initial)
11870                                  0)))))
11871                 (unless (string-match "^[ \t]*$" input)
11872                   (setq all (string-to-number input))
11873                   (if (< all len)
11874                       (let ((older-range (nreverse older)))
11875                         (setq older nil)
11876
11877                         (while (> all 0)
11878                           (let* ((r (pop older-range))
11879                                  (min (if (numberp r) r (car r)))
11880                                  (max (if (numberp r) r (cdr r))))
11881                             (while (and (<= min max)
11882                                         (> all 0))
11883                               (push max older)
11884                               (setq all (1- all)
11885                                     max (1- max))))))))))
11886           (setq older (gnus-uncompress-range older))))
11887         (if (not older)
11888             (message "No old news.")
11889           (gnus-summary-insert-articles older)
11890           (gnus-summary-limit (gnus-sorted-nunion old older))))
11891     (gnus-summary-position-point)))
11892
11893 (defun gnus-summary-insert-new-articles ()
11894   "Insert all new articles in this group."
11895   (interactive)
11896   (prog1
11897       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11898             (old-active gnus-newsgroup-active)
11899             (nnmail-fetched-sources (list t))
11900             i new)
11901         (setq gnus-newsgroup-active
11902               (gnus-activate-group gnus-newsgroup-name 'scan))
11903         (setq i (cdr gnus-newsgroup-active))
11904         (while (> i (cdr old-active))
11905           (push i new)
11906           (decf i))
11907         (if (not new)
11908             (message "No gnus is bad news")
11909           (gnus-summary-insert-articles new)
11910           (setq gnus-newsgroup-unreads
11911                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11912           (gnus-summary-limit (gnus-sorted-nunion old new))))
11913     (gnus-summary-position-point)))
11914
11915 (gnus-summary-make-all-marking-commands)
11916
11917 (gnus-ems-redefine)
11918
11919 (provide 'gnus-sum)
11920
11921 (run-hooks 'gnus-sum-load-hook)
11922
11923 ;; Local Variables:
11924 ;; coding: iso-8859-1
11925 ;; End:
11926
11927 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11928 ;;; gnus-sum.el ends here