Revision: miles@gnu.org--gnu-2005/gnus--devo--0--patch-250
[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 select an article."
261   :group 'gnus-thread
262   :type '(radio (sexp :format "Non-nil\n"
263                       :match (lambda (widget value)
264                                (not (or (consp value) (functionp value))))
265                       :value t)
266                 (const nil)
267                 (sexp :tag "Predicate specifier")))
268
269 (defcustom gnus-thread-hide-killed t
270   "*If non-nil, hide killed threads automatically."
271   :group 'gnus-thread
272   :type 'boolean)
273
274 (defcustom gnus-thread-ignore-subject t
275   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
276 If nil, articles that have different subjects from their parents will
277 start separate threads."
278   :group 'gnus-thread
279   :type 'boolean)
280
281 (defcustom gnus-thread-operation-ignore-subject t
282   "*If non-nil, subjects will be ignored when doing thread commands.
283 This affects commands like `gnus-summary-kill-thread' and
284 `gnus-summary-lower-thread'.
285
286 If this variable is nil, articles in the same thread with different
287 subjects will not be included in the operation in question.  If this
288 variable is `fuzzy', only articles that have subjects that are fuzzily
289 equal will be included."
290   :group 'gnus-thread
291   :type '(choice (const :tag "off" nil)
292                  (const fuzzy)
293                  (sexp :tag "on" t)))
294
295 (defcustom gnus-thread-indent-level 4
296   "*Number that says how much each sub-thread should be indented."
297   :group 'gnus-thread
298   :type 'integer)
299
300 (defcustom gnus-auto-extend-newsgroup t
301   "*If non-nil, extend newsgroup forward and backward when requested."
302   :group 'gnus-summary-choose
303   :type 'boolean)
304
305 (defcustom gnus-auto-select-first t
306   "*If non-nil, select the article under point.
307 Which article this is is controlled by the `gnus-auto-select-subject'
308 variable.
309
310 If you want to prevent automatic selection of articles in some
311 newsgroups, set the variable to nil in `gnus-select-group-hook'."
312   :group 'gnus-group-select
313   :type '(choice (const :tag "none" nil)
314                  (sexp :menu-tag "first" t)))
315
316 (defcustom gnus-auto-select-subject 'unread
317   "*Says what subject to place under point when entering a group.
318
319 This variable can either be the symbols `first' (place point on the
320 first subject), `unread' (place point on the subject line of the first
321 unread article), `best' (place point on the subject line of the
322 higest-scored article), `unseen' (place point on the subject line of
323 the first unseen article), `unseen-or-unread' (place point on the subject
324 line of the first unseen article or, if all article have been seen, on the
325 subject line of the first unread article), or a function to be called to
326 place point on some subject line."
327   :version "22.1"
328   :group 'gnus-group-select
329   :type '(choice (const best)
330                  (const unread)
331                  (const first)
332                  (const unseen)
333                  (const unseen-or-unread)))
334
335 (defcustom gnus-auto-select-next t
336   "*If non-nil, offer to go to the next group from the end of the previous.
337 If the value is t and the next newsgroup is empty, Gnus will exit
338 summary mode and go back to group mode.  If the value is neither nil
339 nor t, Gnus will select the following unread newsgroup.  In
340 particular, if the value is the symbol `quietly', the next unread
341 newsgroup will be selected without any confirmation, and if it is
342 `almost-quietly', the next group will be selected without any
343 confirmation if you are located on the last article in the group.
344 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
345 will go to the next group without confirmation."
346   :group 'gnus-summary-maneuvering
347   :type '(choice (const :tag "off" nil)
348                  (const quietly)
349                  (const almost-quietly)
350                  (const slightly-quietly)
351                  (sexp :menu-tag "on" t)))
352
353 (defcustom gnus-auto-select-same nil
354   "*If non-nil, select the next article with the same subject.
355 If there are no more articles with the same subject, go to
356 the first unread article."
357   :group 'gnus-summary-maneuvering
358   :type 'boolean)
359
360 (defcustom gnus-auto-goto-ignores 'unfetched
361   "*Says how to handle unfetched articles when maneuvering.
362
363 This variable can either be the symbols nil (maneuver to any
364 article), `undownloaded' (maneuvering while unplugged ignores articles
365 that have not been fetched), `always-undownloaded' (maneuvering always
366 ignores articles that have not been fetched), `unfetched' (maneuvering
367 ignores articles whose headers have not been fetched).
368
369 NOTE: The list of unfetched articles will always be nil when plugged
370 and, when unplugged, a subset of the undownloaded article list."
371   :version "22.1"
372   :group 'gnus-summary-maneuvering
373   :type '(choice (const :tag "None" nil)
374                  (const :tag "Undownloaded when unplugged" undownloaded)
375                  (const :tag "Undownloaded" always-undownloaded)
376                  (const :tag "Unfetched" unfetched)))
377
378 (defcustom gnus-summary-check-current nil
379   "*If non-nil, consider the current article when moving.
380 The \"unread\" movement commands will stay on the same line if the
381 current article is unread."
382   :group 'gnus-summary-maneuvering
383   :type 'boolean)
384
385 (defcustom gnus-auto-center-summary 2
386   "*If non-nil, always center the current summary buffer.
387 In particular, if `vertical' do only vertical recentering.  If non-nil
388 and non-`vertical', do both horizontal and vertical recentering."
389   :group 'gnus-summary-maneuvering
390   :type '(choice (const :tag "none" nil)
391                  (const vertical)
392                  (integer :tag "height")
393                  (sexp :menu-tag "both" t)))
394
395 (defvar gnus-auto-center-group t
396   "*If non-nil, always center the group buffer.")
397
398 (defcustom gnus-show-all-headers nil
399   "*If non-nil, don't hide any headers."
400   :group 'gnus-article-hiding
401   :group 'gnus-article-headers
402   :type 'boolean)
403
404 (defcustom gnus-summary-ignore-duplicates nil
405   "*If non-nil, ignore articles with identical Message-ID headers."
406   :group 'gnus-summary
407   :type 'boolean)
408
409 (defcustom gnus-single-article-buffer t
410   "*If non-nil, display all articles in the same buffer.
411 If nil, each group will get its own article buffer."
412   :group 'gnus-article-various
413   :type 'boolean)
414
415 (defcustom gnus-break-pages t
416   "*If non-nil, do page breaking on articles.
417 The page delimiter is specified by the `gnus-page-delimiter'
418 variable."
419   :group 'gnus-article-various
420   :type 'boolean)
421
422 (defcustom gnus-move-split-methods nil
423   "*Variable used to suggest where articles are to be moved to.
424 It uses the same syntax as the `gnus-split-methods' variable.
425 However, whereas `gnus-split-methods' specifies file names as targets,
426 this variable specifies group names."
427   :group 'gnus-summary-mail
428   :type '(repeat (choice (list :value (fun) function)
429                          (cons :value ("" "") regexp (repeat string))
430                          (sexp :value nil))))
431
432 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
433   "Function used to compute default prefix for article move/copy/etc prompts.
434 The function should take one argument, a group name, and return a
435 string with the suggested prefix."
436   :group 'gnus-summary-mail
437   :type 'function)
438
439 ;; FIXME: Although the custom type is `character' for the following variables,
440 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
441
442 (defcustom gnus-unread-mark ?           ;Whitespace
443   "*Mark used for unread articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-ticked-mark ?!
448   "*Mark used for ticked articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-dormant-mark ??
453   "*Mark used for dormant articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-del-mark ?r
458   "*Mark used for del'd articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-read-mark ?R
463   "*Mark used for read articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-expirable-mark ?E
468   "*Mark used for expirable articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-killed-mark ?K
473   "*Mark used for killed articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-spam-mark ?$
478   "*Mark used for spam articles."
479   :version "22.1"
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-souped-mark ?F
484   "*Mark used for souped articles."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-kill-file-mark ?X
489   "*Mark used for articles killed by kill files."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-low-score-mark ?Y
494   "*Mark used for articles with a low score."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-catchup-mark ?C
499   "*Mark used for articles that are caught up."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-replied-mark ?A
504   "*Mark used for articles that have been replied to."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-forwarded-mark ?F
509   "*Mark used for articles that have been forwarded."
510   :version "22.1"
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-recent-mark ?N
515   "*Mark used for articles that are recent."
516   :version "22.1"
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-cached-mark ?*
521   "*Mark used for articles that are in the cache."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-saved-mark ?S
526   "*Mark used for articles that have been saved."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-unseen-mark ?.
531   "*Mark used for articles that haven't been seen."
532   :version "22.1"
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-no-mark ?               ;Whitespace
537   "*Mark used for articles that have no other secondary mark."
538   :version "22.1"
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-ancient-mark ?O
543   "*Mark used for ancient articles."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-sparse-mark ?Q
548   "*Mark used for sparsely reffed articles."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-canceled-mark ?G
553   "*Mark used for canceled articles."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-duplicate-mark ?M
558   "*Mark used for duplicate articles."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-undownloaded-mark ?-
563   "*Mark used for articles that weren't downloaded."
564   :version "22.1"
565   :group 'gnus-summary-marks
566   :type 'character)
567
568 (defcustom gnus-downloaded-mark ?+
569   "*Mark used for articles that were downloaded."
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-downloadable-mark ?%
574   "*Mark used for articles that are to be downloaded."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-unsendable-mark ?=
579   "*Mark used for articles that won't be sent."
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-score-over-mark ?+
584   "*Score mark used for articles with high scores."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-score-below-mark ?-
589   "*Score mark used for articles with low scores."
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-empty-thread-mark ?     ;Whitespace
594   "*There is no thread under the article."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-not-empty-thread-mark ?=
599   "*There is a thread under the article."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-view-pseudo-asynchronously nil
604   "*If non-nil, Gnus will view pseudo-articles asynchronously."
605   :group 'gnus-extract-view
606   :type 'boolean)
607
608 (defcustom gnus-auto-expirable-marks
609   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
610         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
611         gnus-souped-mark gnus-duplicate-mark)
612   "*The list of marks converted into expiration if a group is auto-expirable."
613   :version "21.1"
614   :group 'gnus-summary
615   :type '(repeat character))
616
617 (defcustom gnus-inhibit-user-auto-expire t
618   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
619   :version "21.1"
620   :group 'gnus-summary
621   :type 'boolean)
622
623 (defcustom gnus-view-pseudos nil
624   "*If `automatic', pseudo-articles will be viewed automatically.
625 If `not-confirm', pseudos will be viewed automatically, and the user
626 will not be asked to confirm the command."
627   :group 'gnus-extract-view
628   :type '(choice (const :tag "off" nil)
629                  (const automatic)
630                  (const not-confirm)))
631
632 (defcustom gnus-view-pseudos-separately t
633   "*If non-nil, one pseudo-article will be created for each file to be viewed.
634 If nil, all files that use the same viewing command will be given as a
635 list of parameters to that command."
636   :group 'gnus-extract-view
637   :type 'boolean)
638
639 (defcustom gnus-insert-pseudo-articles t
640   "*If non-nil, insert pseudo-articles when decoding articles."
641   :group 'gnus-extract-view
642   :type 'boolean)
643
644 (defcustom gnus-summary-dummy-line-format
645   "   %(:                             :%) %S\n"
646   "*The format specification for the dummy roots in the summary buffer.
647 It works along the same lines as a normal formatting string,
648 with some simple extensions.
649
650 %S  The subject
651
652 General format specifiers can also be used.
653 See `(gnus)Formatting Variables'."
654   :link '(custom-manual "(gnus)Formatting Variables")
655   :group 'gnus-threading
656   :type 'string)
657
658 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
659   "*The format specification for the summary mode line.
660 It works along the same lines as a normal formatting string,
661 with some simple extensions:
662
663 %G  Group name
664 %p  Unprefixed group name
665 %A  Current article number
666 %z  Current article score
667 %V  Gnus version
668 %U  Number of unread articles in the group
669 %e  Number of unselected articles in the group
670 %Z  A string with unread/unselected article counts
671 %g  Shortish group name
672 %S  Subject of the current article
673 %u  User-defined spec
674 %s  Current score file name
675 %d  Number of dormant articles
676 %r  Number of articles that have been marked as read in this session
677 %E  Number of articles expunged by the score files"
678   :group 'gnus-summary-format
679   :type 'string)
680
681 (defcustom gnus-list-identifiers nil
682   "Regexp that matches list identifiers to be removed from subject.
683 This can also be a list of regexps."
684   :version "21.1"
685   :group 'gnus-summary-format
686   :group 'gnus-article-hiding
687   :type '(choice (const :tag "none" nil)
688                  (regexp :value ".*")
689                  (repeat :value (".*") regexp)))
690
691 (defcustom gnus-summary-mark-below 0
692   "*Mark all articles with a score below this variable as read.
693 This variable is local to each summary buffer and usually set by the
694 score file."
695   :group 'gnus-score-default
696   :type 'integer)
697
698 (defun gnus-widget-reversible-match (widget value)
699   "Ignoring WIDGET, convert VALUE to internal form.
700 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
701   ;; (debug value)
702   (or (symbolp value)
703       (and (listp value)
704            (eq (length value) 2)
705            (eq (nth 0 value) 'not)
706            (symbolp (nth 1 value)))))
707
708 (defun gnus-widget-reversible-to-internal (widget value)
709   "Ignoring WIDGET, convert VALUE to internal form.
710 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
711 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
712   ;; (debug value)
713   (if (atom value)
714       (list value nil)
715     (list (nth 1 value) t)))
716
717 (defun gnus-widget-reversible-to-external (widget value)
718   "Ignoring WIDGET, convert VALUE to external form.
719 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
720 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
721   ;; (debug value)
722   (if (nth 1 value)
723       (list 'not (nth 0 value))
724     (nth 0 value)))
725
726 (define-widget 'gnus-widget-reversible 'group
727   "A `group' that convert values."
728   :match 'gnus-widget-reversible-match
729   :value-to-internal 'gnus-widget-reversible-to-internal
730   :value-to-external 'gnus-widget-reversible-to-external)
731
732 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
733   "*List of functions used for sorting articles in the summary buffer.
734
735 Each function takes two articles and returns non-nil if the first
736 article should be sorted before the other.  If you use more than one
737 function, the primary sort function should be the last.  You should
738 probably always include `gnus-article-sort-by-number' in the list of
739 sorting functions -- preferably first.  Also note that sorting by date
740 is often much slower than sorting by number, and the sorting order is
741 very similar.  (Sorting by date means sorting by the time the message
742 was sent, sorting by number means sorting by arrival time.)
743
744 Each item can also be a list `(not F)' where F is a function;
745 this reverses the sort order.
746
747 Ready-made functions include `gnus-article-sort-by-number',
748 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
749 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
750 and `gnus-article-sort-by-score'.
751
752 When threading is turned on, the variable `gnus-thread-sort-functions'
753 controls how articles are sorted."
754   :group 'gnus-summary-sort
755   :type '(repeat (gnus-widget-reversible
756                   (choice (function-item gnus-article-sort-by-number)
757                           (function-item gnus-article-sort-by-author)
758                           (function-item gnus-article-sort-by-subject)
759                           (function-item gnus-article-sort-by-date)
760                           (function-item gnus-article-sort-by-score)
761                           (function-item gnus-article-sort-by-random)
762                           (function :tag "other"))
763                   (boolean :tag "Reverse order"))))
764
765
766 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
767   "*List of functions used for sorting threads in the summary buffer.
768 By default, threads are sorted by article number.
769
770 Each function takes two threads and returns non-nil if the first
771 thread should be sorted before the other.  If you use more than one
772 function, the primary sort function should be the last.  You should
773 probably always include `gnus-thread-sort-by-number' in the list of
774 sorting functions -- preferably first.  Also note that sorting by date
775 is often much slower than sorting by number, and the sorting order is
776 very similar.  (Sorting by date means sorting by the time the message
777 was sent, sorting by number means sorting by arrival time.)
778
779 Each list item can also be a list `(not F)' where F is a
780 function; this specifies reversed sort order.
781
782 Ready-made functions include `gnus-thread-sort-by-number',
783 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
784 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
785 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
786 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
787 and `gnus-thread-sort-by-total-score' (see
788 `gnus-thread-score-function').
789
790 When threading is turned off, the variable
791 `gnus-article-sort-functions' controls how articles are sorted."
792   :group 'gnus-summary-sort
793   :type '(repeat
794           (gnus-widget-reversible
795            (choice (function-item gnus-thread-sort-by-number)
796                    (function-item gnus-thread-sort-by-author)
797                    (function-item gnus-thread-sort-by-recipient)
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  
1060        (not (string= user-mail-address ""))
1061        (regexp-quote user-mail-address))
1062   "*Regexp of From headers that may be suppressed in favor of To headers."
1063   :version "21.1"
1064   :group 'gnus-summary
1065   :type 'regexp)
1066
1067 (defcustom gnus-summary-to-prefix "-> "
1068   "*String prefixed to the To field in the summary line when
1069 using `gnus-ignored-from-addresses'."
1070   :version "22.1"
1071   :group 'gnus-summary
1072   :type 'string)
1073
1074 (defcustom gnus-summary-newsgroup-prefix "=> "
1075   "*String prefixed to the Newsgroup field in the summary
1076 line when using `gnus-ignored-from-addresses'."
1077   :version "22.1"
1078   :group 'gnus-summary
1079   :type 'string)
1080
1081 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1082   "List of charsets that should be ignored.
1083 When these charsets are used in the \"charset\" parameter, the
1084 default charset will be used instead."
1085   :version "21.1"
1086   :type '(repeat symbol)
1087   :group 'gnus-charset)
1088
1089 (gnus-define-group-parameter
1090  ignored-charsets
1091  :type list
1092  :function-document
1093  "Return the ignored charsets of GROUP."
1094  :variable gnus-group-ignored-charsets-alist
1095  :variable-default
1096  '(("alt\\.chinese\\.text" iso-8859-1))
1097  :variable-document
1098  "Alist of regexps (to match group names) and charsets that should be ignored.
1099 When these charsets are used in the \"charset\" parameter, the
1100 default charset will be used instead."
1101  :variable-group gnus-charset
1102  :variable-type '(repeat (cons (regexp :tag "Group")
1103                                (repeat symbol)))
1104  :parameter-type '(choice :tag "Ignored charsets"
1105                           :value nil
1106                           (repeat (symbol)))
1107  :parameter-document       "\
1108 List of charsets that should be ignored.
1109
1110 When these charsets are used in the \"charset\" parameter, the
1111 default charset will be used instead.")
1112
1113 (defcustom gnus-group-highlight-words-alist nil
1114   "Alist of group regexps and highlight regexps.
1115 This variable uses the same syntax as `gnus-emphasis-alist'."
1116   :version "21.1"
1117   :type '(repeat (cons (regexp :tag "Group")
1118                        (repeat (list (regexp :tag "Highlight regexp")
1119                                      (number :tag "Group for entire word" 0)
1120                                      (number :tag "Group for displayed part" 0)
1121                                      (symbol :tag "Face"
1122                                              gnus-emphasis-highlight-words)))))
1123   :group 'gnus-summary-visual)
1124
1125 (defcustom gnus-summary-show-article-charset-alist
1126   nil
1127   "Alist of number and charset.
1128 The article will be shown with the charset corresponding to the
1129 numbered argument.
1130 For example: ((1 . cn-gb-2312) (2 . big5))."
1131   :version "21.1"
1132   :type '(repeat (cons (number :tag "Argument" 1)
1133                        (symbol :tag "Charset")))
1134   :group 'gnus-charset)
1135
1136 (defcustom gnus-preserve-marks t
1137   "Whether marks are preserved when moving, copying and respooling messages."
1138   :version "21.1"
1139   :type 'boolean
1140   :group 'gnus-summary-marks)
1141
1142 (defcustom gnus-alter-articles-to-read-function nil
1143   "Function to be called to alter the list of articles to be selected."
1144   :type '(choice (const nil) function)
1145   :group 'gnus-summary)
1146
1147 (defcustom gnus-orphan-score nil
1148   "*All orphans get this score added.  Set in the score file."
1149   :group 'gnus-score-default
1150   :type '(choice (const nil)
1151                  integer))
1152
1153 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1154   "*A regexp to match MIME parts when saving multiple parts of a
1155 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1156 This regexp will be used by default when prompting the user for which
1157 type of files to save."
1158   :group 'gnus-summary
1159   :type 'regexp)
1160
1161 (defcustom gnus-read-all-available-headers nil
1162   "Whether Gnus should parse all headers made available to it.
1163 This is mostly relevant for slow back ends where the user may
1164 wish to widen the summary buffer to include all headers
1165 that were fetched.  Say, for nnultimate groups."
1166   :version "22.1"
1167   :group 'gnus-summary
1168   :type '(choice boolean regexp))
1169
1170 (defcustom gnus-summary-muttprint-program "muttprint"
1171   "Command (and optional arguments) used to run Muttprint."
1172   :version "22.1"
1173   :group 'gnus-summary
1174   :type 'string)
1175
1176 (defcustom gnus-article-loose-mime t
1177   "If non-nil, don't require MIME-Version header.
1178 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1179 supply the MIME-Version header or deliberately strip it from the mail.
1180 If non-nil (the default), Gnus will treat some articles as MIME
1181 even if the MIME-Version header is missing."
1182   :version "22.1"
1183   :type 'boolean
1184   :group 'gnus-article-mime)
1185
1186 (defcustom gnus-article-emulate-mime t
1187   "If non-nil, use MIME emulation for uuencode and the like.
1188 This means that Gnus will search message bodies for text that look
1189 like uuencoded bits, yEncoded bits, and so on, and present that using
1190 the normal Gnus MIME machinery."
1191   :version "22.1"
1192   :type 'boolean
1193   :group 'gnus-article-mime)
1194
1195 ;;; Internal variables
1196
1197 (defvar gnus-summary-display-cache nil)
1198 (defvar gnus-article-mime-handles nil)
1199 (defvar gnus-article-decoded-p nil)
1200 (defvar gnus-article-charset nil)
1201 (defvar gnus-article-ignored-charsets nil)
1202 (defvar gnus-scores-exclude-files nil)
1203 (defvar gnus-page-broken nil)
1204
1205 (defvar gnus-original-article nil)
1206 (defvar gnus-article-internal-prepare-hook nil)
1207 (defvar gnus-newsgroup-process-stack nil)
1208
1209 (defvar gnus-thread-indent-array nil)
1210 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1211 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1212   "Function called to sort the articles within a thread after it has been gathered together.")
1213
1214 (defvar gnus-summary-save-parts-type-history nil)
1215 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1216
1217 ;; Avoid highlighting in kill files.
1218 (defvar gnus-summary-inhibit-highlight nil)
1219 (defvar gnus-newsgroup-selected-overlay nil)
1220 (defvar gnus-inhibit-limiting nil)
1221 (defvar gnus-newsgroup-adaptive-score-file nil)
1222 (defvar gnus-current-score-file nil)
1223 (defvar gnus-current-move-group nil)
1224 (defvar gnus-current-copy-group nil)
1225 (defvar gnus-current-crosspost-group nil)
1226 (defvar gnus-newsgroup-display nil)
1227
1228 (defvar gnus-newsgroup-dependencies nil)
1229 (defvar gnus-newsgroup-adaptive nil)
1230 (defvar gnus-summary-display-article-function nil)
1231 (defvar gnus-summary-highlight-line-function nil
1232   "Function called after highlighting a summary line.")
1233
1234 (defvar gnus-summary-line-format-alist
1235   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1236     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1237     (?s gnus-tmp-subject-or-nil ?s)
1238     (?n gnus-tmp-name ?s)
1239     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1240         ?s)
1241     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1242             gnus-tmp-from) ?s)
1243     (?F gnus-tmp-from ?s)
1244     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1245     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1246     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1247     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1248     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1249     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1250     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1251     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1252     (?L gnus-tmp-lines ?s)
1253     (?O gnus-tmp-downloaded ?c)
1254     (?I gnus-tmp-indentation ?s)
1255     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1256     (?R gnus-tmp-replied ?c)
1257     (?\[ gnus-tmp-opening-bracket ?c)
1258     (?\] gnus-tmp-closing-bracket ?c)
1259     (?\> (make-string gnus-tmp-level ? ) ?s)
1260     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1261     (?i gnus-tmp-score ?d)
1262     (?z gnus-tmp-score-char ?c)
1263     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1264     (?U gnus-tmp-unread ?c)
1265     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1266         ?s)
1267     (?t (gnus-summary-number-of-articles-in-thread
1268          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1269         ?d)
1270     (?e (gnus-summary-number-of-articles-in-thread
1271          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1272         ?c)
1273     (?u gnus-tmp-user-defined ?s)
1274     (?P (gnus-pick-line-number) ?d)
1275     (?B gnus-tmp-thread-tree-header-string ?s)
1276     (user-date (gnus-user-date
1277                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1278   "An alist of format specifications that can appear in summary lines.
1279 These are paired with what variables they correspond with, along with
1280 the type of the variable (string, integer, character, etc).")
1281
1282 (defvar gnus-summary-dummy-line-format-alist
1283   `((?S gnus-tmp-subject ?s)
1284     (?N gnus-tmp-number ?d)
1285     (?u gnus-tmp-user-defined ?s)))
1286
1287 (defvar gnus-summary-mode-line-format-alist
1288   `((?G gnus-tmp-group-name ?s)
1289     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1290     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1291     (?A gnus-tmp-article-number ?d)
1292     (?Z gnus-tmp-unread-and-unselected ?s)
1293     (?V gnus-version ?s)
1294     (?U gnus-tmp-unread-and-unticked ?d)
1295     (?S gnus-tmp-subject ?s)
1296     (?e gnus-tmp-unselected ?d)
1297     (?u gnus-tmp-user-defined ?s)
1298     (?d (length gnus-newsgroup-dormant) ?d)
1299     (?t (length gnus-newsgroup-marked) ?d)
1300     (?h (length gnus-newsgroup-spam-marked) ?d)
1301     (?r (length gnus-newsgroup-reads) ?d)
1302     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1303     (?E gnus-newsgroup-expunged-tally ?d)
1304     (?s (gnus-current-score-file-nondirectory) ?s)))
1305
1306 (defvar gnus-last-search-regexp nil
1307   "Default regexp for article search command.")
1308
1309 (defvar gnus-last-shell-command nil
1310   "Default shell command on article.")
1311
1312 (defvar gnus-newsgroup-agentized nil
1313   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1314 (defvar gnus-newsgroup-begin nil)
1315 (defvar gnus-newsgroup-end nil)
1316 (defvar gnus-newsgroup-last-rmail nil)
1317 (defvar gnus-newsgroup-last-mail nil)
1318 (defvar gnus-newsgroup-last-folder nil)
1319 (defvar gnus-newsgroup-last-file nil)
1320 (defvar gnus-newsgroup-auto-expire nil)
1321 (defvar gnus-newsgroup-active nil)
1322
1323 (defvar gnus-newsgroup-data nil)
1324 (defvar gnus-newsgroup-data-reverse nil)
1325 (defvar gnus-newsgroup-limit nil)
1326 (defvar gnus-newsgroup-limits nil)
1327 (defvar gnus-summary-use-undownloaded-faces nil)
1328
1329 (defvar gnus-newsgroup-unreads nil
1330   "Sorted list of unread articles in the current newsgroup.")
1331
1332 (defvar gnus-newsgroup-unselected nil
1333   "Sorted list of unselected unread articles in the current newsgroup.")
1334
1335 (defvar gnus-newsgroup-reads nil
1336   "Alist of read articles and article marks in the current newsgroup.")
1337
1338 (defvar gnus-newsgroup-expunged-tally nil)
1339
1340 (defvar gnus-newsgroup-marked nil
1341   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1342
1343 (defvar gnus-newsgroup-spam-marked nil
1344   "List of ranges of articles that have been marked as spam.")
1345
1346 (defvar gnus-newsgroup-killed nil
1347   "List of ranges of articles that have been through the scoring process.")
1348
1349 (defvar gnus-newsgroup-cached nil
1350   "Sorted list of articles that come from the article cache.")
1351
1352 (defvar gnus-newsgroup-saved nil
1353   "List of articles that have been saved.")
1354
1355 (defvar gnus-newsgroup-kill-headers nil)
1356
1357 (defvar gnus-newsgroup-replied nil
1358   "List of articles that have been replied to in the current newsgroup.")
1359
1360 (defvar gnus-newsgroup-forwarded nil
1361   "List of articles that have been forwarded in the current newsgroup.")
1362
1363 (defvar gnus-newsgroup-recent nil
1364   "List of articles that have are recent in the current newsgroup.")
1365
1366 (defvar gnus-newsgroup-expirable nil
1367   "Sorted list of articles in the current newsgroup that can be expired.")
1368
1369 (defvar gnus-newsgroup-processable nil
1370   "List of articles in the current newsgroup that can be processed.")
1371
1372 (defvar gnus-newsgroup-downloadable nil
1373   "Sorted list of articles in the current newsgroup that can be processed.")
1374
1375 (defvar gnus-newsgroup-unfetched nil
1376   "Sorted list of articles in the current newsgroup whose headers have
1377 not been fetched into the agent.
1378
1379 This list will always be a subset of gnus-newsgroup-undownloaded.")
1380
1381 (defvar gnus-newsgroup-undownloaded nil
1382   "List of articles in the current newsgroup that haven't been downloaded.")
1383
1384 (defvar gnus-newsgroup-unsendable nil
1385   "List of articles in the current newsgroup that won't be sent.")
1386
1387 (defvar gnus-newsgroup-bookmarks nil
1388   "List of articles in the current newsgroup that have bookmarks.")
1389
1390 (defvar gnus-newsgroup-dormant nil
1391   "Sorted list of dormant articles in the current newsgroup.")
1392
1393 (defvar gnus-newsgroup-unseen nil
1394   "List of unseen articles in the current newsgroup.")
1395
1396 (defvar gnus-newsgroup-seen nil
1397   "Range of seen articles in the current newsgroup.")
1398
1399 (defvar gnus-newsgroup-articles nil
1400   "List of articles in the current newsgroup.")
1401
1402 (defvar gnus-newsgroup-scored nil
1403   "List of scored articles in the current newsgroup.")
1404
1405 (defvar gnus-newsgroup-headers nil
1406   "List of article headers in the current newsgroup.")
1407
1408 (defvar gnus-newsgroup-threads nil)
1409
1410 (defvar gnus-newsgroup-prepared nil
1411   "Whether the current group has been prepared properly.")
1412
1413 (defvar gnus-newsgroup-ancient nil
1414   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1415
1416 (defvar gnus-newsgroup-sparse nil)
1417
1418 (defvar gnus-current-article nil)
1419 (defvar gnus-article-current nil)
1420 (defvar gnus-current-headers nil)
1421 (defvar gnus-have-all-headers nil)
1422 (defvar gnus-last-article nil)
1423 (defvar gnus-newsgroup-history nil)
1424 (defvar gnus-newsgroup-charset nil)
1425 (defvar gnus-newsgroup-ephemeral-charset nil)
1426 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1427
1428 (defvar gnus-article-before-search nil)
1429
1430 (defvar gnus-summary-local-variables
1431   '(gnus-newsgroup-name
1432     gnus-newsgroup-begin gnus-newsgroup-end
1433     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1434     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1435     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1436     gnus-newsgroup-unselected gnus-newsgroup-marked
1437     gnus-newsgroup-spam-marked
1438     gnus-newsgroup-reads gnus-newsgroup-saved
1439     gnus-newsgroup-replied gnus-newsgroup-forwarded
1440     gnus-newsgroup-recent
1441     gnus-newsgroup-expirable
1442     gnus-newsgroup-processable gnus-newsgroup-killed
1443     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1444     gnus-newsgroup-unfetched
1445     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1446     gnus-newsgroup-seen gnus-newsgroup-articles
1447     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1448     gnus-newsgroup-headers gnus-newsgroup-threads
1449     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1450     gnus-current-article gnus-current-headers gnus-have-all-headers
1451     gnus-last-article gnus-article-internal-prepare-hook
1452     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1453     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1454     gnus-thread-expunge-below
1455     gnus-score-alist gnus-current-score-file
1456     (gnus-summary-expunge-below . global)
1457     (gnus-summary-mark-below . global)
1458     (gnus-orphan-score . global)
1459     gnus-newsgroup-active gnus-scores-exclude-files
1460     gnus-newsgroup-history gnus-newsgroup-ancient
1461     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1462     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1463     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1464     (gnus-newsgroup-expunged-tally . 0)
1465     gnus-cache-removable-articles gnus-newsgroup-cached
1466     gnus-newsgroup-data gnus-newsgroup-data-reverse
1467     gnus-newsgroup-limit gnus-newsgroup-limits
1468     gnus-newsgroup-charset gnus-newsgroup-display
1469     gnus-summary-use-undownloaded-faces)
1470   "Variables that are buffer-local to the summary buffers.")
1471
1472 (defvar gnus-newsgroup-variables nil
1473   "A list of variables that have separate values in different newsgroups.
1474 A list of newsgroup (summary buffer) local variables, or cons of
1475 variables and their default expressions to be evalled (when the default
1476 values are not nil), that should be made global while the summary buffer
1477 is active.
1478
1479 Note: The default expressions will be evaluated (using function `eval')
1480 before assignment to the local variable rather than just assigned to it.
1481 If the default expression is the symbol `global', that symbol will not
1482 be evaluated but the global value of the local variable will be used
1483 instead.
1484
1485 These variables can be used to set variables in the group parameters
1486 while still allowing them to affect operations done in other buffers.
1487 For example:
1488
1489 \(setq gnus-newsgroup-variables
1490      '(message-use-followup-to
1491        (gnus-visible-headers .
1492          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1493 ")
1494
1495 ;; Byte-compiler warning.
1496 (eval-when-compile
1497   ;; Bind features so that require will believe that gnus-sum has
1498   ;; already been loaded (avoids infinite recursion)
1499   (let ((features (cons 'gnus-sum features)))
1500     ;; Several of the declarations in gnus-sum are needed to load the
1501     ;; following files. Right now, these definitions have been
1502     ;; compiled but not defined (evaluated).  We could either do a
1503     ;; eval-and-compile about all of the declarations or evaluate the
1504     ;; source file.
1505     (if (boundp 'gnus-newsgroup-variables)
1506         nil
1507       (load "gnus-sum.el" t t t))
1508     (require 'gnus)
1509     (require 'gnus-art)))
1510
1511 ;; MIME stuff.
1512
1513 (defvar gnus-decode-encoded-word-methods
1514   '(mail-decode-encoded-word-string)
1515   "List of methods used to decode encoded words.
1516
1517 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1518 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1519 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1520 whose names match REGEXP.
1521
1522 For example:
1523 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1524  mail-decode-encoded-word-string
1525  (\"chinese\" . rfc1843-decode-string))")
1526
1527 (defvar gnus-decode-encoded-word-methods-cache nil)
1528
1529 (defun gnus-multi-decode-encoded-word-string (string)
1530   "Apply the functions from `gnus-encoded-word-methods' that match."
1531   (unless (and gnus-decode-encoded-word-methods-cache
1532                (eq gnus-newsgroup-name
1533                    (car gnus-decode-encoded-word-methods-cache)))
1534     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1535     (mapcar (lambda (x)
1536               (if (symbolp x)
1537                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1538                 (if (and gnus-newsgroup-name
1539                          (string-match (car x) gnus-newsgroup-name))
1540                     (nconc gnus-decode-encoded-word-methods-cache
1541                            (list (cdr x))))))
1542             gnus-decode-encoded-word-methods))
1543   (let ((xlist gnus-decode-encoded-word-methods-cache))
1544     (pop xlist)
1545     (while xlist
1546       (setq string (funcall (pop xlist) string))))
1547   string)
1548
1549 ;; Subject simplification.
1550
1551 (defun gnus-simplify-whitespace (str)
1552   "Remove excessive whitespace from STR."
1553   ;; Multiple spaces.
1554   (while (string-match "[ \t][ \t]+" str)
1555     (setq str (concat (substring str 0 (match-beginning 0))
1556                         " "
1557                         (substring str (match-end 0)))))
1558   ;; Leading spaces.
1559   (when (string-match "^[ \t]+" str)
1560     (setq str (substring str (match-end 0))))
1561   ;; Trailing spaces.
1562   (when (string-match "[ \t]+$" str)
1563     (setq str (substring str 0 (match-beginning 0))))
1564   str)
1565
1566 (defun gnus-simplify-all-whitespace (str)
1567   "Remove all whitespace from STR."
1568   (while (string-match "[ \t\n]+" str)
1569     (setq str (replace-match "" nil nil str)))
1570   str)
1571
1572 (defsubst gnus-simplify-subject-re (subject)
1573   "Remove \"Re:\" from subject lines."
1574   (if (string-match message-subject-re-regexp subject)
1575       (substring subject (match-end 0))
1576     subject))
1577
1578 (defun gnus-simplify-subject (subject &optional re-only)
1579   "Remove `Re:' and words in parentheses.
1580 If RE-ONLY is non-nil, strip leading `Re:'s only."
1581   (let ((case-fold-search t))           ;Ignore case.
1582     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1583     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1584       (setq subject (substring subject (match-end 0))))
1585     ;; Remove uninteresting prefixes.
1586     (when (and (not re-only)
1587                gnus-simplify-ignored-prefixes
1588                (string-match gnus-simplify-ignored-prefixes subject))
1589       (setq subject (substring subject (match-end 0))))
1590     ;; Remove words in parentheses from end.
1591     (unless re-only
1592       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1593         (setq subject (substring subject 0 (match-beginning 0)))))
1594     ;; Return subject string.
1595     subject))
1596
1597 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1598 ;; all whitespace.
1599 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1600   (goto-char (point-min))
1601   (while (re-search-forward regexp nil t)
1602     (replace-match (or newtext ""))))
1603
1604 (defun gnus-simplify-buffer-fuzzy ()
1605   "Simplify string in the buffer fuzzily.
1606 The string in the accessible portion of the current buffer is simplified.
1607 It is assumed to be a single-line subject.
1608 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1609 matter is removed.  Additional things can be deleted by setting
1610 `gnus-simplify-subject-fuzzy-regexp'."
1611   (let ((case-fold-search t)
1612         (modified-tick))
1613     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1614
1615     (while (not (eq modified-tick (buffer-modified-tick)))
1616       (setq modified-tick (buffer-modified-tick))
1617       (cond
1618        ((listp gnus-simplify-subject-fuzzy-regexp)
1619         (mapcar 'gnus-simplify-buffer-fuzzy-step
1620                 gnus-simplify-subject-fuzzy-regexp))
1621        (gnus-simplify-subject-fuzzy-regexp
1622         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1623       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1624       (gnus-simplify-buffer-fuzzy-step
1625        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1626       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1627
1628     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1629     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1630     (gnus-simplify-buffer-fuzzy-step " $")
1631     (gnus-simplify-buffer-fuzzy-step "^ +")))
1632
1633 (defun gnus-simplify-subject-fuzzy (subject)
1634   "Simplify a subject string fuzzily.
1635 See `gnus-simplify-buffer-fuzzy' for details."
1636   (save-excursion
1637     (gnus-set-work-buffer)
1638     (let ((case-fold-search t))
1639       ;; Remove uninteresting prefixes.
1640       (when (and gnus-simplify-ignored-prefixes
1641                  (string-match gnus-simplify-ignored-prefixes subject))
1642         (setq subject (substring subject (match-end 0))))
1643       (insert subject)
1644       (inline (gnus-simplify-buffer-fuzzy))
1645       (buffer-string))))
1646
1647 (defsubst gnus-simplify-subject-fully (subject)
1648   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1649   (cond
1650    (gnus-simplify-subject-functions
1651     (gnus-map-function gnus-simplify-subject-functions subject))
1652    ((null gnus-summary-gather-subject-limit)
1653     (gnus-simplify-subject-re subject))
1654    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1655     (gnus-simplify-subject-fuzzy subject))
1656    ((numberp gnus-summary-gather-subject-limit)
1657     (gnus-limit-string (gnus-simplify-subject-re subject)
1658                        gnus-summary-gather-subject-limit))
1659    (t
1660     subject)))
1661
1662 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1663   "Check whether two subjects are equal.
1664 If optional argument SIMPLE-FIRST is t, first argument is already
1665 simplified."
1666   (cond
1667    ((null simple-first)
1668     (equal (gnus-simplify-subject-fully s1)
1669            (gnus-simplify-subject-fully s2)))
1670    (t
1671     (equal s1
1672            (gnus-simplify-subject-fully s2)))))
1673
1674 (defun gnus-summary-bubble-group ()
1675   "Increase the score of the current group.
1676 This is a handy function to add to `gnus-summary-exit-hook' to
1677 increase the score of each group you read."
1678   (gnus-group-add-score gnus-newsgroup-name))
1679
1680 \f
1681 ;;;
1682 ;;; Gnus summary mode
1683 ;;;
1684
1685 (put 'gnus-summary-mode 'mode-class 'special)
1686
1687 (defvar gnus-article-commands-menu)
1688
1689 ;; Non-orthogonal keys
1690
1691 (gnus-define-keys gnus-summary-mode-map
1692   " " gnus-summary-next-page
1693   "\177" gnus-summary-prev-page
1694   [delete] gnus-summary-prev-page
1695   [backspace] gnus-summary-prev-page
1696   "\r" gnus-summary-scroll-up
1697   "\M-\r" gnus-summary-scroll-down
1698   "n" gnus-summary-next-unread-article
1699   "p" gnus-summary-prev-unread-article
1700   "N" gnus-summary-next-article
1701   "P" gnus-summary-prev-article
1702   "\M-\C-n" gnus-summary-next-same-subject
1703   "\M-\C-p" gnus-summary-prev-same-subject
1704   "\M-n" gnus-summary-next-unread-subject
1705   "\M-p" gnus-summary-prev-unread-subject
1706   "." gnus-summary-first-unread-article
1707   "," gnus-summary-best-unread-article
1708   "\M-s" gnus-summary-search-article-forward
1709   "\M-r" gnus-summary-search-article-backward
1710   "<" gnus-summary-beginning-of-article
1711   ">" gnus-summary-end-of-article
1712   "j" gnus-summary-goto-article
1713   "^" gnus-summary-refer-parent-article
1714   "\M-^" gnus-summary-refer-article
1715   "u" gnus-summary-tick-article-forward
1716   "!" gnus-summary-tick-article-forward
1717   "U" gnus-summary-tick-article-backward
1718   "d" gnus-summary-mark-as-read-forward
1719   "D" gnus-summary-mark-as-read-backward
1720   "E" gnus-summary-mark-as-expirable
1721   "\M-u" gnus-summary-clear-mark-forward
1722   "\M-U" gnus-summary-clear-mark-backward
1723   "k" gnus-summary-kill-same-subject-and-select
1724   "\C-k" gnus-summary-kill-same-subject
1725   "\M-\C-k" gnus-summary-kill-thread
1726   "\M-\C-l" gnus-summary-lower-thread
1727   "e" gnus-summary-edit-article
1728   "#" gnus-summary-mark-as-processable
1729   "\M-#" gnus-summary-unmark-as-processable
1730   "\M-\C-t" gnus-summary-toggle-threads
1731   "\M-\C-s" gnus-summary-show-thread
1732   "\M-\C-h" gnus-summary-hide-thread
1733   "\M-\C-f" gnus-summary-next-thread
1734   "\M-\C-b" gnus-summary-prev-thread
1735   [(meta down)] gnus-summary-next-thread
1736   [(meta up)] gnus-summary-prev-thread
1737   "\M-\C-u" gnus-summary-up-thread
1738   "\M-\C-d" gnus-summary-down-thread
1739   "&" gnus-summary-execute-command
1740   "c" gnus-summary-catchup-and-exit
1741   "\C-w" gnus-summary-mark-region-as-read
1742   "\C-t" gnus-summary-toggle-truncation
1743   "?" gnus-summary-mark-as-dormant
1744   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1745   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1746   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1747   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1748   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1749   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1750   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1751   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1752   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1753   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1754   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1755   "=" gnus-summary-expand-window
1756   "\C-x\C-s" gnus-summary-reselect-current-group
1757   "\M-g" gnus-summary-rescan-group
1758   "w" gnus-summary-stop-page-breaking
1759   "\C-c\C-r" gnus-summary-caesar-message
1760   "f" gnus-summary-followup
1761   "F" gnus-summary-followup-with-original
1762   "C" gnus-summary-cancel-article
1763   "r" gnus-summary-reply
1764   "R" gnus-summary-reply-with-original
1765   "\C-c\C-f" gnus-summary-mail-forward
1766   "o" gnus-summary-save-article
1767   "\C-o" gnus-summary-save-article-mail
1768   "|" gnus-summary-pipe-output
1769   "\M-k" gnus-summary-edit-local-kill
1770   "\M-K" gnus-summary-edit-global-kill
1771   ;; "V" gnus-version
1772   "\C-c\C-d" gnus-summary-describe-group
1773   "q" gnus-summary-exit
1774   "Q" gnus-summary-exit-no-update
1775   "\C-c\C-i" gnus-info-find-node
1776   gnus-mouse-2 gnus-mouse-pick-article
1777   [follow-link] mouse-face
1778   "m" gnus-summary-mail-other-window
1779   "a" gnus-summary-post-news
1780   "i" gnus-summary-news-other-window
1781   "x" gnus-summary-limit-to-unread
1782   "s" gnus-summary-isearch-article
1783   "t" gnus-summary-toggle-header
1784   "g" gnus-summary-show-article
1785   "l" gnus-summary-goto-last-article
1786   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1787   "\C-d" gnus-summary-enter-digest-group
1788   "\M-\C-d" gnus-summary-read-document
1789   "\M-\C-e" gnus-summary-edit-parameters
1790   "\M-\C-a" gnus-summary-customize-parameters
1791   "\C-c\C-b" gnus-bug
1792   "*" gnus-cache-enter-article
1793   "\M-*" gnus-cache-remove-article
1794   "\M-&" gnus-summary-universal-argument
1795   "\C-l" gnus-recenter
1796   "I" gnus-summary-increase-score
1797   "L" gnus-summary-lower-score
1798   "\M-i" gnus-symbolic-argument
1799   "h" gnus-summary-select-article-buffer
1800
1801   "b" gnus-article-view-part
1802   "\M-t" gnus-summary-toggle-display-buttonized
1803
1804   "V" gnus-summary-score-map
1805   "X" gnus-uu-extract-map
1806   "S" gnus-summary-send-map)
1807
1808 ;; Sort of orthogonal keymap
1809 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1810   "t" gnus-summary-tick-article-forward
1811   "!" gnus-summary-tick-article-forward
1812   "d" gnus-summary-mark-as-read-forward
1813   "r" gnus-summary-mark-as-read-forward
1814   "c" gnus-summary-clear-mark-forward
1815   " " gnus-summary-clear-mark-forward
1816   "e" gnus-summary-mark-as-expirable
1817   "x" gnus-summary-mark-as-expirable
1818   "?" gnus-summary-mark-as-dormant
1819   "b" gnus-summary-set-bookmark
1820   "B" gnus-summary-remove-bookmark
1821   "#" gnus-summary-mark-as-processable
1822   "\M-#" gnus-summary-unmark-as-processable
1823   "S" gnus-summary-limit-include-expunged
1824   "C" gnus-summary-catchup
1825   "H" gnus-summary-catchup-to-here
1826   "h" gnus-summary-catchup-from-here
1827   "\C-c" gnus-summary-catchup-all
1828   "k" gnus-summary-kill-same-subject-and-select
1829   "K" gnus-summary-kill-same-subject
1830   "P" gnus-uu-mark-map)
1831
1832 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1833   "c" gnus-summary-clear-above
1834   "u" gnus-summary-tick-above
1835   "m" gnus-summary-mark-above
1836   "k" gnus-summary-kill-below)
1837
1838 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1839   "/" gnus-summary-limit-to-subject
1840   "n" gnus-summary-limit-to-articles
1841   "w" gnus-summary-pop-limit
1842   "s" gnus-summary-limit-to-subject
1843   "a" gnus-summary-limit-to-author
1844   "u" gnus-summary-limit-to-unread
1845   "m" gnus-summary-limit-to-marks
1846   "M" gnus-summary-limit-exclude-marks
1847   "v" gnus-summary-limit-to-score
1848   "*" gnus-summary-limit-include-cached
1849   "D" gnus-summary-limit-include-dormant
1850   "T" gnus-summary-limit-include-thread
1851   "d" gnus-summary-limit-exclude-dormant
1852   "t" gnus-summary-limit-to-age
1853   "." gnus-summary-limit-to-unseen
1854   "x" gnus-summary-limit-to-extra
1855   "p" gnus-summary-limit-to-display-predicate
1856   "E" gnus-summary-limit-include-expunged
1857   "c" gnus-summary-limit-exclude-childless-dormant
1858   "C" gnus-summary-limit-mark-excluded-as-read
1859   "o" gnus-summary-insert-old-articles
1860   "N" gnus-summary-insert-new-articles
1861   "r" gnus-summary-limit-to-replied
1862   "R" gnus-summary-limit-to-recipient)
1863
1864 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1865   "n" gnus-summary-next-unread-article
1866   "p" gnus-summary-prev-unread-article
1867   "N" gnus-summary-next-article
1868   "P" gnus-summary-prev-article
1869   "\C-n" gnus-summary-next-same-subject
1870   "\C-p" gnus-summary-prev-same-subject
1871   "\M-n" gnus-summary-next-unread-subject
1872   "\M-p" gnus-summary-prev-unread-subject
1873   "f" gnus-summary-first-unread-article
1874   "b" gnus-summary-best-unread-article
1875   "j" gnus-summary-goto-article
1876   "g" gnus-summary-goto-subject
1877   "l" gnus-summary-goto-last-article
1878   "o" gnus-summary-pop-article)
1879
1880 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1881   "k" gnus-summary-kill-thread
1882   "l" gnus-summary-lower-thread
1883   "i" gnus-summary-raise-thread
1884   "T" gnus-summary-toggle-threads
1885   "t" gnus-summary-rethread-current
1886   "^" gnus-summary-reparent-thread
1887   "s" gnus-summary-show-thread
1888   "S" gnus-summary-show-all-threads
1889   "h" gnus-summary-hide-thread
1890   "H" gnus-summary-hide-all-threads
1891   "n" gnus-summary-next-thread
1892   "p" gnus-summary-prev-thread
1893   "u" gnus-summary-up-thread
1894   "o" gnus-summary-top-thread
1895   "d" gnus-summary-down-thread
1896   "#" gnus-uu-mark-thread
1897   "\M-#" gnus-uu-unmark-thread)
1898
1899 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1900   "g" gnus-summary-prepare
1901   "c" gnus-summary-insert-cached-articles
1902   "d" gnus-summary-insert-dormant-articles)
1903
1904 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1905   "c" gnus-summary-catchup-and-exit
1906   "C" gnus-summary-catchup-all-and-exit
1907   "E" gnus-summary-exit-no-update
1908   "Q" gnus-summary-exit
1909   "Z" gnus-summary-exit
1910   "n" gnus-summary-catchup-and-goto-next-group
1911   "R" gnus-summary-reselect-current-group
1912   "G" gnus-summary-rescan-group
1913   "N" gnus-summary-next-group
1914   "s" gnus-summary-save-newsrc
1915   "P" gnus-summary-prev-group)
1916
1917 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1918   " " gnus-summary-next-page
1919   "n" gnus-summary-next-page
1920   "\177" gnus-summary-prev-page
1921   [delete] gnus-summary-prev-page
1922   "p" gnus-summary-prev-page
1923   "\r" gnus-summary-scroll-up
1924   "\M-\r" gnus-summary-scroll-down
1925   "<" gnus-summary-beginning-of-article
1926   ">" gnus-summary-end-of-article
1927   "b" gnus-summary-beginning-of-article
1928   "e" gnus-summary-end-of-article
1929   "^" gnus-summary-refer-parent-article
1930   "r" gnus-summary-refer-parent-article
1931   "D" gnus-summary-enter-digest-group
1932   "R" gnus-summary-refer-references
1933   "T" gnus-summary-refer-thread
1934   "g" gnus-summary-show-article
1935   "s" gnus-summary-isearch-article
1936   "P" gnus-summary-print-article
1937   "M" gnus-mailing-list-insinuate
1938   "t" gnus-article-babel)
1939
1940 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1941   "b" gnus-article-add-buttons
1942   "B" gnus-article-add-buttons-to-head
1943   "o" gnus-article-treat-overstrike
1944   "e" gnus-article-emphasize
1945   "w" gnus-article-fill-cited-article
1946   "Q" gnus-article-fill-long-lines
1947   "C" gnus-article-capitalize-sentences
1948   "c" gnus-article-remove-cr
1949   "q" gnus-article-de-quoted-unreadable
1950   "6" gnus-article-de-base64-unreadable
1951   "Z" gnus-article-decode-HZ
1952   "A" gnus-article-treat-ansi-sequences
1953   "h" gnus-article-wash-html
1954   "u" gnus-article-unsplit-urls
1955   "s" gnus-summary-force-verify-and-decrypt
1956   "f" gnus-article-display-x-face
1957   "l" gnus-summary-stop-page-breaking
1958   "r" gnus-summary-caesar-message
1959   "m" gnus-summary-morse-message
1960   "t" gnus-summary-toggle-header
1961   "g" gnus-treat-smiley
1962   "v" gnus-summary-verbose-headers
1963   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1964   "p" gnus-article-verify-x-pgp-sig
1965   "d" gnus-article-treat-dumbquotes
1966   "i" gnus-summary-idna-message)
1967
1968 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1969   ;; mnemonic: deuglif*Y*
1970   "u" gnus-article-outlook-unwrap-lines
1971   "a" gnus-article-outlook-repair-attribution
1972   "c" gnus-article-outlook-rearrange-citation
1973   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1974
1975 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1976   "a" gnus-article-hide
1977   "h" gnus-article-hide-headers
1978   "b" gnus-article-hide-boring-headers
1979   "s" gnus-article-hide-signature
1980   "c" gnus-article-hide-citation
1981   "C" gnus-article-hide-citation-in-followups
1982   "l" gnus-article-hide-list-identifiers
1983   "B" gnus-article-strip-banner
1984   "P" gnus-article-hide-pem
1985   "\C-c" gnus-article-hide-citation-maybe)
1986
1987 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1988   "a" gnus-article-highlight
1989   "h" gnus-article-highlight-headers
1990   "c" gnus-article-highlight-citation
1991   "s" gnus-article-highlight-signature)
1992
1993 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1994   "f" gnus-article-treat-fold-headers
1995   "u" gnus-article-treat-unfold-headers
1996   "n" gnus-article-treat-fold-newsgroups)
1997
1998 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1999   "x" gnus-article-display-x-face
2000   "d" gnus-article-display-face
2001   "s" gnus-treat-smiley
2002   "D" gnus-article-remove-images
2003   "f" gnus-treat-from-picon
2004   "m" gnus-treat-mail-picon
2005   "n" gnus-treat-newsgroups-picon)
2006
2007 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2008   "w" gnus-article-decode-mime-words
2009   "c" gnus-article-decode-charset
2010   "v" gnus-mime-view-all-parts
2011   "b" gnus-article-view-part)
2012
2013 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2014   "z" gnus-article-date-ut
2015   "u" gnus-article-date-ut
2016   "l" gnus-article-date-local
2017   "p" gnus-article-date-english
2018   "e" gnus-article-date-lapsed
2019   "o" gnus-article-date-original
2020   "i" gnus-article-date-iso8601
2021   "s" gnus-article-date-user)
2022
2023 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2024   "t" gnus-article-remove-trailing-blank-lines
2025   "l" gnus-article-strip-leading-blank-lines
2026   "m" gnus-article-strip-multiple-blank-lines
2027   "a" gnus-article-strip-blank-lines
2028   "A" gnus-article-strip-all-blank-lines
2029   "s" gnus-article-strip-leading-space
2030   "e" gnus-article-strip-trailing-space
2031   "w" gnus-article-remove-leading-whitespace)
2032
2033 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2034   "v" gnus-version
2035   "f" gnus-summary-fetch-faq
2036   "d" gnus-summary-describe-group
2037   "h" gnus-summary-describe-briefly
2038   "i" gnus-info-find-node
2039   "c" gnus-group-fetch-charter
2040   "C" gnus-group-fetch-control)
2041
2042 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2043   "e" gnus-summary-expire-articles
2044   "\M-\C-e" gnus-summary-expire-articles-now
2045   "\177" gnus-summary-delete-article
2046   [delete] gnus-summary-delete-article
2047   [backspace] gnus-summary-delete-article
2048   "m" gnus-summary-move-article
2049   "r" gnus-summary-respool-article
2050   "w" gnus-summary-edit-article
2051   "c" gnus-summary-copy-article
2052   "B" gnus-summary-crosspost-article
2053   "q" gnus-summary-respool-query
2054   "t" gnus-summary-respool-trace
2055   "i" gnus-summary-import-article
2056   "I" gnus-summary-create-article
2057   "p" gnus-summary-article-posted-p)
2058
2059 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2060   "o" gnus-summary-save-article
2061   "m" gnus-summary-save-article-mail
2062   "F" gnus-summary-write-article-file
2063   "r" gnus-summary-save-article-rmail
2064   "f" gnus-summary-save-article-file
2065   "b" gnus-summary-save-article-body-file
2066   "h" gnus-summary-save-article-folder
2067   "v" gnus-summary-save-article-vm
2068   "p" gnus-summary-pipe-output
2069   "P" gnus-summary-muttprint
2070   "s" gnus-soup-add-article)
2071
2072 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2073   "b" gnus-summary-display-buttonized
2074   "m" gnus-summary-repair-multipart
2075   "v" gnus-article-view-part
2076   "o" gnus-article-save-part
2077   "O" gnus-article-save-part-and-strip
2078   "r" gnus-article-replace-part
2079   "d" gnus-article-delete-part
2080   "j" gnus-article-jump-to-part
2081   "c" gnus-article-copy-part
2082   "C" gnus-article-view-part-as-charset
2083   "e" gnus-article-view-part-externally
2084   "E" gnus-article-encrypt-body
2085   "i" gnus-article-inline-part
2086   "|" gnus-article-pipe-part)
2087
2088 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2089   "p" gnus-summary-mark-as-processable
2090   "u" gnus-summary-unmark-as-processable
2091   "U" gnus-summary-unmark-all-processable
2092   "v" gnus-uu-mark-over
2093   "s" gnus-uu-mark-series
2094   "r" gnus-uu-mark-region
2095   "g" gnus-uu-unmark-region
2096   "R" gnus-uu-mark-by-regexp
2097   "G" gnus-uu-unmark-by-regexp
2098   "t" gnus-uu-mark-thread
2099   "T" gnus-uu-unmark-thread
2100   "a" gnus-uu-mark-all
2101   "b" gnus-uu-mark-buffer
2102   "S" gnus-uu-mark-sparse
2103   "k" gnus-summary-kill-process-mark
2104   "y" gnus-summary-yank-process-mark
2105   "w" gnus-summary-save-process-mark
2106   "i" gnus-uu-invert-processable)
2107
2108 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2109   ;;"x" gnus-uu-extract-any
2110   "m" gnus-summary-save-parts
2111   "u" gnus-uu-decode-uu
2112   "U" gnus-uu-decode-uu-and-save
2113   "s" gnus-uu-decode-unshar
2114   "S" gnus-uu-decode-unshar-and-save
2115   "o" gnus-uu-decode-save
2116   "O" gnus-uu-decode-save
2117   "b" gnus-uu-decode-binhex
2118   "B" gnus-uu-decode-binhex
2119   "p" gnus-uu-decode-postscript
2120   "P" gnus-uu-decode-postscript-and-save)
2121
2122 (gnus-define-keys
2123     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2124   "u" gnus-uu-decode-uu-view
2125   "U" gnus-uu-decode-uu-and-save-view
2126   "s" gnus-uu-decode-unshar-view
2127   "S" gnus-uu-decode-unshar-and-save-view
2128   "o" gnus-uu-decode-save-view
2129   "O" gnus-uu-decode-save-view
2130   "b" gnus-uu-decode-binhex-view
2131   "B" gnus-uu-decode-binhex-view
2132   "p" gnus-uu-decode-postscript-view
2133   "P" gnus-uu-decode-postscript-and-save-view)
2134
2135 (defvar gnus-article-post-menu nil)
2136
2137 (defconst gnus-summary-menu-maxlen 20)
2138
2139 (defun gnus-summary-menu-split (menu)
2140   ;; If we have lots of elements, divide them into groups of 20
2141   ;; and make a pane (or submenu) for each one.
2142   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2143       (let ((menu menu) sublists next
2144             (i 1))
2145         (while menu
2146           ;; Pull off the next gnus-summary-menu-maxlen elements
2147           ;; and make them the next element of sublist.
2148           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2149           (if next
2150               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2151                       nil))
2152           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2153                                              (aref (car (last menu)) 0)) menu)
2154                                sublists))
2155           (setq i (1+ i))
2156           (setq menu next))
2157         (nreverse sublists))
2158     ;; Few elements--put them all in one pane.
2159     menu))
2160
2161 (defun gnus-summary-make-menu-bar ()
2162   (gnus-turn-off-edit-menu 'summary)
2163
2164   (unless (boundp 'gnus-summary-misc-menu)
2165
2166     (easy-menu-define
2167       gnus-summary-kill-menu gnus-summary-mode-map ""
2168       (cons
2169        "Score"
2170        (nconc
2171         (list
2172          ["Customize" gnus-score-customize t])
2173         (gnus-make-score-map 'increase)
2174         (gnus-make-score-map 'lower)
2175         '(("Mark"
2176            ["Kill below" gnus-summary-kill-below t]
2177            ["Mark above" gnus-summary-mark-above t]
2178            ["Tick above" gnus-summary-tick-above t]
2179            ["Clear above" gnus-summary-clear-above t])
2180           ["Current score" gnus-summary-current-score t]
2181           ["Set score" gnus-summary-set-score t]
2182           ["Switch current score file..." gnus-score-change-score-file t]
2183           ["Set mark below..." gnus-score-set-mark-below t]
2184           ["Set expunge below..." gnus-score-set-expunge-below t]
2185           ["Edit current score file" gnus-score-edit-current-scores t]
2186           ["Edit score file" gnus-score-edit-file t]
2187           ["Trace score" gnus-score-find-trace t]
2188           ["Find words" gnus-score-find-favourite-words t]
2189           ["Rescore buffer" gnus-summary-rescore t]
2190           ["Increase score..." gnus-summary-increase-score t]
2191           ["Lower score..." gnus-summary-lower-score t]))))
2192
2193     ;; Define both the Article menu in the summary buffer and the
2194     ;; equivalent Commands menu in the article buffer here for
2195     ;; consistency.
2196     (let ((innards
2197            `(("Hide"
2198               ["All" gnus-article-hide t]
2199               ["Headers" gnus-article-hide-headers t]
2200               ["Signature" gnus-article-hide-signature t]
2201               ["Citation" gnus-article-hide-citation t]
2202               ["List identifiers" gnus-article-hide-list-identifiers t]
2203               ["Banner" gnus-article-strip-banner t]
2204               ["Boring headers" gnus-article-hide-boring-headers t])
2205              ("Highlight"
2206               ["All" gnus-article-highlight t]
2207               ["Headers" gnus-article-highlight-headers t]
2208               ["Signature" gnus-article-highlight-signature t]
2209               ["Citation" gnus-article-highlight-citation t])
2210              ("MIME"
2211               ["Words" gnus-article-decode-mime-words t]
2212               ["Charset" gnus-article-decode-charset t]
2213               ["QP" gnus-article-de-quoted-unreadable t]
2214               ["Base64" gnus-article-de-base64-unreadable t]
2215               ["View MIME buttons" gnus-summary-display-buttonized t]
2216               ["View all" gnus-mime-view-all-parts t]
2217               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2218               ["Encrypt body" gnus-article-encrypt-body
2219                :active (not (gnus-group-read-only-p))
2220                ,@(if (featurep 'xemacs) nil
2221                    '(:help "Encrypt the message body on disk"))]
2222               ["Extract all parts..." gnus-summary-save-parts t]
2223               ("Multipart"
2224                ["Repair multipart" gnus-summary-repair-multipart t]
2225                ["Pipe part..." gnus-article-pipe-part t]
2226                ["Inline part" gnus-article-inline-part t]
2227                ["Encrypt body" gnus-article-encrypt-body
2228                 :active (not (gnus-group-read-only-p))
2229                ,@(if (featurep 'xemacs) nil
2230                    '(:help "Encrypt the message body on disk"))]
2231                ["View part externally" gnus-article-view-part-externally t]
2232                ["View part with charset..." gnus-article-view-part-as-charset t]
2233                ["Copy part" gnus-article-copy-part t]
2234                ["Save part..." gnus-article-save-part t]
2235                ["View part" gnus-article-view-part t]))
2236              ("Date"
2237               ["Local" gnus-article-date-local t]
2238               ["ISO8601" gnus-article-date-iso8601 t]
2239               ["UT" gnus-article-date-ut t]
2240               ["Original" gnus-article-date-original t]
2241               ["Lapsed" gnus-article-date-lapsed t]
2242               ["User-defined" gnus-article-date-user t])
2243              ("Display"
2244               ["Remove images" gnus-article-remove-images t]
2245               ["Toggle smiley" gnus-treat-smiley t]
2246               ["Show X-Face" gnus-article-display-x-face t]
2247               ["Show picons in From" gnus-treat-from-picon t]
2248               ["Show picons in mail headers" gnus-treat-mail-picon t]
2249               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2250               ("View as different encoding"
2251                ,@(gnus-summary-menu-split
2252                   (mapcar
2253                    (lambda (cs)
2254                      ;; Since easymenu under Emacs doesn't allow
2255                      ;; lambda forms for menu commands, we should
2256                      ;; provide intern'ed function symbols.
2257                      (let ((command (intern (format "\
2258 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2259                        (fset command
2260                              `(lambda ()
2261                                 (interactive)
2262                                 (let ((gnus-summary-show-article-charset-alist
2263                                        '((1 . ,cs))))
2264                                   (gnus-summary-show-article 1))))
2265                        `[,(symbol-name cs) ,command t]))
2266                    (sort (if (fboundp 'coding-system-list)
2267                              (coding-system-list)
2268                            (mapcar 'car mm-mime-mule-charset-alist))
2269                          'string<)))))
2270              ("Washing"
2271               ("Remove Blanks"
2272                ["Leading" gnus-article-strip-leading-blank-lines t]
2273                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2274                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2275                ["All of the above" gnus-article-strip-blank-lines t]
2276                ["All" gnus-article-strip-all-blank-lines t]
2277                ["Leading space" gnus-article-strip-leading-space t]
2278                ["Trailing space" gnus-article-strip-trailing-space t]
2279                ["Leading space in headers"
2280                 gnus-article-remove-leading-whitespace t])
2281               ["Overstrike" gnus-article-treat-overstrike t]
2282               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2283               ["Emphasis" gnus-article-emphasize t]
2284               ["Word wrap" gnus-article-fill-cited-article t]
2285               ["Fill long lines" gnus-article-fill-long-lines t]
2286               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2287               ["Remove CR" gnus-article-remove-cr t]
2288               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2289               ["Base64" gnus-article-de-base64-unreadable t]
2290               ["Rot 13" gnus-summary-caesar-message
2291                ,@(if (featurep 'xemacs) '(t)
2292                    '(:help "\"Caesar rotate\" article by 13"))]
2293               ["De-IDNA" gnus-summary-idna-message t]
2294               ["Morse decode" gnus-summary-morse-message t]
2295               ["Unix pipe..." gnus-summary-pipe-message t]
2296               ["Add buttons" gnus-article-add-buttons t]
2297               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2298               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2299               ["Verbose header" gnus-summary-verbose-headers t]
2300               ["Toggle header" gnus-summary-toggle-header t]
2301               ["Unfold headers" gnus-article-treat-unfold-headers t]
2302               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2303               ["Html" gnus-article-wash-html t]
2304               ["Unsplit URLs" gnus-article-unsplit-urls t]
2305               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2306               ["Decode HZ" gnus-article-decode-HZ t]
2307               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2308               ("(Outlook) Deuglify"
2309                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2310                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2311                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2312                ["Full (Outlook) deuglify"
2313                 gnus-article-outlook-deuglify-article t])
2314               )
2315              ("Output"
2316               ["Save in default format..." gnus-summary-save-article
2317                ,@(if (featurep 'xemacs) '(t)
2318                    '(:help "Save article using default method"))]
2319               ["Save in file..." gnus-summary-save-article-file
2320                ,@(if (featurep 'xemacs) '(t)
2321                    '(:help "Save article in file"))]
2322               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2323               ["Save in MH folder..." gnus-summary-save-article-folder t]
2324               ["Save in VM folder..." gnus-summary-save-article-vm t]
2325               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2326               ["Save body in file..." gnus-summary-save-article-body-file t]
2327               ["Pipe through a filter..." gnus-summary-pipe-output t]
2328               ["Add to SOUP packet" gnus-soup-add-article t]
2329               ["Print with Muttprint..." gnus-summary-muttprint t]
2330               ["Print" gnus-summary-print-article
2331                ,@(if (featurep 'xemacs) '(t)
2332                    '(:help "Generate and print a PostScript image"))])
2333              ("Copy, move,... (Backend)"
2334               ,@(if (featurep 'xemacs) nil
2335                   '(:help "Copying, moving, expiring articles..."))
2336               ["Respool article..." gnus-summary-respool-article t]
2337               ["Move article..." gnus-summary-move-article
2338                (gnus-check-backend-function
2339                 'request-move-article gnus-newsgroup-name)]
2340               ["Copy article..." gnus-summary-copy-article t]
2341               ["Crosspost article..." gnus-summary-crosspost-article
2342                (gnus-check-backend-function
2343                 'request-replace-article gnus-newsgroup-name)]
2344               ["Import file..." gnus-summary-import-article
2345                (gnus-check-backend-function
2346                 'request-accept-article gnus-newsgroup-name)]
2347               ["Create article..." gnus-summary-create-article
2348                (gnus-check-backend-function
2349                 'request-accept-article gnus-newsgroup-name)]
2350               ["Check if posted" gnus-summary-article-posted-p t]
2351               ["Edit article" gnus-summary-edit-article
2352                (not (gnus-group-read-only-p))]
2353               ["Delete article" gnus-summary-delete-article
2354                (gnus-check-backend-function
2355                 'request-expire-articles gnus-newsgroup-name)]
2356               ["Query respool" gnus-summary-respool-query t]
2357               ["Trace respool" gnus-summary-respool-trace t]
2358               ["Delete expirable articles" gnus-summary-expire-articles-now
2359                (gnus-check-backend-function
2360                 'request-expire-articles gnus-newsgroup-name)])
2361              ("Extract"
2362               ["Uudecode" gnus-uu-decode-uu
2363                ,@(if (featurep 'xemacs) '(t)
2364                    '(:help "Decode uuencoded article(s)"))]
2365               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2366               ["Unshar" gnus-uu-decode-unshar t]
2367               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2368               ["Save" gnus-uu-decode-save t]
2369               ["Binhex" gnus-uu-decode-binhex t]
2370               ["Postscript" gnus-uu-decode-postscript t]
2371               ["All MIME parts" gnus-summary-save-parts t])
2372              ("Cache"
2373               ["Enter article" gnus-cache-enter-article t]
2374               ["Remove article" gnus-cache-remove-article t])
2375              ["Translate" gnus-article-babel t]
2376              ["Select article buffer" gnus-summary-select-article-buffer t]
2377              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2378              ["Isearch article..." gnus-summary-isearch-article t]
2379              ["Beginning of the article" gnus-summary-beginning-of-article t]
2380              ["End of the article" gnus-summary-end-of-article t]
2381              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2382              ["Fetch referenced articles" gnus-summary-refer-references t]
2383              ["Fetch current thread" gnus-summary-refer-thread t]
2384              ["Fetch article with id..." gnus-summary-refer-article t]
2385              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2386              ["Redisplay" gnus-summary-show-article t]
2387              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2388       (easy-menu-define
2389         gnus-summary-article-menu gnus-summary-mode-map ""
2390         (cons "Article" innards))
2391
2392       (if (not (keymapp gnus-summary-article-menu))
2393           (easy-menu-define
2394             gnus-article-commands-menu gnus-article-mode-map ""
2395             (cons "Commands" innards))
2396         ;; in Emacs, don't share menu.
2397         (setq gnus-article-commands-menu
2398               (copy-keymap gnus-summary-article-menu))
2399         (define-key gnus-article-mode-map [menu-bar commands]
2400           (cons "Commands" gnus-article-commands-menu))))
2401
2402     (easy-menu-define
2403       gnus-summary-thread-menu gnus-summary-mode-map ""
2404       '("Threads"
2405         ["Find all messages in thread" gnus-summary-refer-thread t]
2406         ["Toggle threading" gnus-summary-toggle-threads t]
2407         ["Hide threads" gnus-summary-hide-all-threads t]
2408         ["Show threads" gnus-summary-show-all-threads t]
2409         ["Hide thread" gnus-summary-hide-thread t]
2410         ["Show thread" gnus-summary-show-thread t]
2411         ["Go to next thread" gnus-summary-next-thread t]
2412         ["Go to previous thread" gnus-summary-prev-thread t]
2413         ["Go down thread" gnus-summary-down-thread t]
2414         ["Go up thread" gnus-summary-up-thread t]
2415         ["Top of thread" gnus-summary-top-thread t]
2416         ["Mark thread as read" gnus-summary-kill-thread t]
2417         ["Lower thread score" gnus-summary-lower-thread t]
2418         ["Raise thread score" gnus-summary-raise-thread t]
2419         ["Rethread current" gnus-summary-rethread-current t]))
2420
2421     (easy-menu-define
2422       gnus-summary-post-menu gnus-summary-mode-map ""
2423       `("Post"
2424         ["Send a message (mail or news)" gnus-summary-post-news
2425          ,@(if (featurep 'xemacs) '(t)
2426              '(:help "Compose a new message (mail or news)"))]
2427         ["Followup" gnus-summary-followup
2428          ,@(if (featurep 'xemacs) '(t)
2429              '(:help "Post followup to this article"))]
2430         ["Followup and yank" gnus-summary-followup-with-original
2431          ,@(if (featurep 'xemacs) '(t)
2432              '(:help "Post followup to this article, quoting its contents"))]
2433         ["Supersede article" gnus-summary-supersede-article t]
2434         ["Cancel article" gnus-summary-cancel-article
2435          ,@(if (featurep 'xemacs) '(t)
2436              '(:help "Cancel an article you posted"))]
2437         ["Reply" gnus-summary-reply t]
2438         ["Reply and yank" gnus-summary-reply-with-original t]
2439         ["Wide reply" gnus-summary-wide-reply t]
2440         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2441          ,@(if (featurep 'xemacs) '(t)
2442              '(:help "Mail a reply, quoting this article"))]
2443         ["Very wide reply" gnus-summary-very-wide-reply t]
2444         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2445          ,@(if (featurep 'xemacs) '(t)
2446              '(:help "Mail a very wide reply, quoting this article"))]
2447         ["Mail forward" gnus-summary-mail-forward t]
2448         ["Post forward" gnus-summary-post-forward t]
2449         ["Digest and mail" gnus-uu-digest-mail-forward t]
2450         ["Digest and post" gnus-uu-digest-post-forward t]
2451         ["Resend message" gnus-summary-resend-message t]
2452         ["Resend message edit" gnus-summary-resend-message-edit t]
2453         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2454         ["Send a mail" gnus-summary-mail-other-window t]
2455         ["Create a local message" gnus-summary-news-other-window t]
2456         ["Uuencode and post" gnus-uu-post-news
2457          ,@(if (featurep 'xemacs) '(t)
2458              '(:help "Post a uuencoded article"))]
2459         ["Followup via news" gnus-summary-followup-to-mail t]
2460         ["Followup via news and yank"
2461          gnus-summary-followup-to-mail-with-original t]
2462         ;;("Draft"
2463         ;;["Send" gnus-summary-send-draft t]
2464         ;;["Send bounced" gnus-resend-bounced-mail t])
2465         ))
2466
2467     (cond
2468      ((not (keymapp gnus-summary-post-menu))
2469       (setq gnus-article-post-menu gnus-summary-post-menu))
2470      ((not gnus-article-post-menu)
2471       ;; Don't share post menu.
2472       (setq gnus-article-post-menu
2473             (copy-keymap gnus-summary-post-menu))))
2474     (define-key gnus-article-mode-map [menu-bar post]
2475       (cons "Post" gnus-article-post-menu))
2476
2477     (easy-menu-define
2478       gnus-summary-misc-menu gnus-summary-mode-map ""
2479       `("Gnus"
2480         ("Mark Read"
2481          ["Mark as read" gnus-summary-mark-as-read-forward t]
2482          ["Mark same subject and select"
2483           gnus-summary-kill-same-subject-and-select t]
2484          ["Mark same subject" gnus-summary-kill-same-subject t]
2485          ["Catchup" gnus-summary-catchup
2486           ,@(if (featurep 'xemacs) '(t)
2487               '(:help "Mark unread articles in this group as read"))]
2488          ["Catchup all" gnus-summary-catchup-all t]
2489          ["Catchup to here" gnus-summary-catchup-to-here t]
2490          ["Catchup from here" gnus-summary-catchup-from-here t]
2491          ["Catchup region" gnus-summary-mark-region-as-read
2492           (gnus-mark-active-p)]
2493          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2494         ("Mark Various"
2495          ["Tick" gnus-summary-tick-article-forward t]
2496          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2497          ["Remove marks" gnus-summary-clear-mark-forward t]
2498          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2499          ["Set bookmark" gnus-summary-set-bookmark t]
2500          ["Remove bookmark" gnus-summary-remove-bookmark t])
2501         ("Limit to"
2502          ["Marks..." gnus-summary-limit-to-marks t]
2503          ["Subject..." gnus-summary-limit-to-subject t]
2504          ["Author..." gnus-summary-limit-to-author t]
2505          ["Recipient..." gnus-summary-limit-to-recipient t]
2506          ["Age..." gnus-summary-limit-to-age t]
2507          ["Extra..." gnus-summary-limit-to-extra t]
2508          ["Score..." gnus-summary-limit-to-score t]
2509          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2510          ["Unread" gnus-summary-limit-to-unread t]
2511          ["Unseen" gnus-summary-limit-to-unseen t]
2512          ["Replied" gnus-summary-limit-to-replied t]
2513          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2514          ["Next articles" gnus-summary-limit-to-articles t]
2515          ["Pop limit" gnus-summary-pop-limit t]
2516          ["Show dormant" gnus-summary-limit-include-dormant t]
2517          ["Hide childless dormant"
2518           gnus-summary-limit-exclude-childless-dormant t]
2519          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2520          ["Hide marked" gnus-summary-limit-exclude-marks t]
2521          ["Show expunged" gnus-summary-limit-include-expunged t])
2522         ("Process Mark"
2523          ["Set mark" gnus-summary-mark-as-processable t]
2524          ["Remove mark" gnus-summary-unmark-as-processable t]
2525          ["Remove all marks" gnus-summary-unmark-all-processable t]
2526          ["Invert marks" gnus-uu-invert-processable t]
2527          ["Mark above" gnus-uu-mark-over t]
2528          ["Mark series" gnus-uu-mark-series t]
2529          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2530          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2531          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2532          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2533          ["Mark all" gnus-uu-mark-all t]
2534          ["Mark buffer" gnus-uu-mark-buffer t]
2535          ["Mark sparse" gnus-uu-mark-sparse t]
2536          ["Mark thread" gnus-uu-mark-thread t]
2537          ["Unmark thread" gnus-uu-unmark-thread t]
2538          ("Process Mark Sets"
2539           ["Kill" gnus-summary-kill-process-mark t]
2540           ["Yank" gnus-summary-yank-process-mark
2541            gnus-newsgroup-process-stack]
2542           ["Save" gnus-summary-save-process-mark t]
2543           ["Run command on marked..." gnus-summary-universal-argument t]))
2544         ("Scroll article"
2545          ["Page forward" gnus-summary-next-page
2546           ,@(if (featurep 'xemacs) '(t)
2547               '(:help "Show next page of article"))]
2548          ["Page backward" gnus-summary-prev-page
2549           ,@(if (featurep 'xemacs) '(t)
2550               '(:help "Show previous page of article"))]
2551          ["Line forward" gnus-summary-scroll-up t])
2552         ("Move"
2553          ["Next unread article" gnus-summary-next-unread-article t]
2554          ["Previous unread article" gnus-summary-prev-unread-article t]
2555          ["Next article" gnus-summary-next-article t]
2556          ["Previous article" gnus-summary-prev-article t]
2557          ["Next unread subject" gnus-summary-next-unread-subject t]
2558          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2559          ["Next article same subject" gnus-summary-next-same-subject t]
2560          ["Previous article same subject" gnus-summary-prev-same-subject t]
2561          ["First unread article" gnus-summary-first-unread-article t]
2562          ["Best unread article" gnus-summary-best-unread-article t]
2563          ["Go to subject number..." gnus-summary-goto-subject t]
2564          ["Go to article number..." gnus-summary-goto-article t]
2565          ["Go to the last article" gnus-summary-goto-last-article t]
2566          ["Pop article off history" gnus-summary-pop-article t])
2567         ("Sort"
2568          ["Sort by number" gnus-summary-sort-by-number t]
2569          ["Sort by author" gnus-summary-sort-by-author t]
2570          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2571          ["Sort by subject" gnus-summary-sort-by-subject t]
2572          ["Sort by date" gnus-summary-sort-by-date t]
2573          ["Sort by score" gnus-summary-sort-by-score t]
2574          ["Sort by lines" gnus-summary-sort-by-lines t]
2575          ["Sort by characters" gnus-summary-sort-by-chars t]
2576          ["Randomize" gnus-summary-sort-by-random t]
2577          ["Original sort" gnus-summary-sort-by-original t])
2578         ("Help"
2579          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2580          ["Describe group" gnus-summary-describe-group t]
2581          ["Fetch charter" gnus-group-fetch-charter
2582           ,@(if (featurep 'xemacs) nil
2583               '(:help "Display the charter of the current group"))]
2584          ["Fetch control message" gnus-group-fetch-control
2585           ,@(if (featurep 'xemacs) nil
2586               '(:help "Display the archived control message for the current group"))]
2587          ["Read manual" gnus-info-find-node t])
2588         ("Modes"
2589          ["Pick and read" gnus-pick-mode t]
2590          ["Binary" gnus-binary-mode t])
2591         ("Regeneration"
2592          ["Regenerate" gnus-summary-prepare t]
2593          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2594          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2595          ["Toggle threading" gnus-summary-toggle-threads t])
2596         ["See old articles" gnus-summary-insert-old-articles t]
2597         ["See new articles" gnus-summary-insert-new-articles t]
2598         ["Filter articles..." gnus-summary-execute-command t]
2599         ["Run command on articles..." gnus-summary-universal-argument t]
2600         ["Search articles forward..." gnus-summary-search-article-forward t]
2601         ["Search articles backward..." gnus-summary-search-article-backward t]
2602         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2603         ["Expand window" gnus-summary-expand-window t]
2604         ["Expire expirable articles" gnus-summary-expire-articles
2605          (gnus-check-backend-function
2606           'request-expire-articles gnus-newsgroup-name)]
2607         ["Edit local kill file" gnus-summary-edit-local-kill t]
2608         ["Edit main kill file" gnus-summary-edit-global-kill t]
2609         ["Edit group parameters" gnus-summary-edit-parameters t]
2610         ["Customize group parameters" gnus-summary-customize-parameters t]
2611         ["Send a bug report" gnus-bug t]
2612         ("Exit"
2613          ["Catchup and exit" gnus-summary-catchup-and-exit
2614           ,@(if (featurep 'xemacs) '(t)
2615               '(:help "Mark unread articles in this group as read, then exit"))]
2616          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2617          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2618          ["Exit group" gnus-summary-exit
2619           ,@(if (featurep 'xemacs) '(t)
2620               '(:help "Exit current group, return to group selection mode"))]
2621          ["Exit group without updating" gnus-summary-exit-no-update t]
2622          ["Exit and goto next group" gnus-summary-next-group t]
2623          ["Exit and goto prev group" gnus-summary-prev-group t]
2624          ["Reselect group" gnus-summary-reselect-current-group t]
2625          ["Rescan group" gnus-summary-rescan-group t]
2626          ["Update dribble" gnus-summary-save-newsrc t])))
2627
2628     (gnus-run-hooks 'gnus-summary-menu-hook)))
2629
2630 (defvar gnus-summary-tool-bar-map nil)
2631
2632 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2633 (defun gnus-summary-make-tool-bar ()
2634   (if (and (fboundp 'tool-bar-add-item-from-menu)
2635            (default-value 'tool-bar-mode)
2636            (not gnus-summary-tool-bar-map))
2637       (setq gnus-summary-tool-bar-map
2638             (let ((tool-bar-map (make-sparse-keymap))
2639                   (load-path (mm-image-load-path)))
2640               (tool-bar-add-item-from-menu
2641                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2642               (tool-bar-add-item-from-menu
2643                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2644               (tool-bar-add-item-from-menu
2645                'gnus-summary-post-news "post" gnus-summary-mode-map)
2646               (tool-bar-add-item-from-menu
2647                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2648               (tool-bar-add-item-from-menu
2649                'gnus-summary-followup "followup" gnus-summary-mode-map)
2650               (tool-bar-add-item-from-menu
2651                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2652               (tool-bar-add-item-from-menu
2653                'gnus-summary-reply "reply" gnus-summary-mode-map)
2654               (tool-bar-add-item-from-menu
2655                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2656               (tool-bar-add-item-from-menu
2657                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2658               (tool-bar-add-item-from-menu
2659                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2660               (tool-bar-add-item-from-menu
2661                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2662               (tool-bar-add-item-from-menu
2663                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2664               (tool-bar-add-item-from-menu
2665                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2666               (tool-bar-add-item-from-menu
2667                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2668               (tool-bar-add-item-from-menu
2669                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2670               (tool-bar-add-item-from-menu
2671                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2672               tool-bar-map)))
2673   (if gnus-summary-tool-bar-map
2674       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2675
2676 (defun gnus-score-set-default (var value)
2677   "A version of set that updates the GNU Emacs menu-bar."
2678   (set var value)
2679   ;; It is the message that forces the active status to be updated.
2680   (message ""))
2681
2682 (defun gnus-make-score-map (type)
2683   "Make a summary score map of type TYPE."
2684   (if t
2685       nil
2686     (let ((headers '(("author" "from" string)
2687                      ("subject" "subject" string)
2688                      ("article body" "body" string)
2689                      ("article head" "head" string)
2690                      ("xref" "xref" string)
2691                      ("extra header" "extra" string)
2692                      ("lines" "lines" number)
2693                      ("followups to author" "followup" string)))
2694           (types '((number ("less than" <)
2695                            ("greater than" >)
2696                            ("equal" =))
2697                    (string ("substring" s)
2698                            ("exact string" e)
2699                            ("fuzzy string" f)
2700                            ("regexp" r))))
2701           (perms '(("temporary" (current-time-string))
2702                    ("permanent" nil)
2703                    ("immediate" now)))
2704           header)
2705       (list
2706        (apply
2707         'nconc
2708         (list
2709          (if (eq type 'lower)
2710              "Lower score"
2711            "Increase score"))
2712         (let (outh)
2713           (while headers
2714             (setq header (car headers))
2715             (setq outh
2716                   (cons
2717                    (apply
2718                     'nconc
2719                     (list (car header))
2720                     (let ((ts (cdr (assoc (nth 2 header) types)))
2721                           outt)
2722                       (while ts
2723                         (setq outt
2724                               (cons
2725                                (apply
2726                                 'nconc
2727                                 (list (caar ts))
2728                                 (let ((ps perms)
2729                                       outp)
2730                                   (while ps
2731                                     (setq outp
2732                                           (cons
2733                                            (vector
2734                                             (caar ps)
2735                                             (list
2736                                              'gnus-summary-score-entry
2737                                              (nth 1 header)
2738                                              (if (or (string= (nth 1 header)
2739                                                               "head")
2740                                                      (string= (nth 1 header)
2741                                                               "body"))
2742                                                  ""
2743                                                (list 'gnus-summary-header
2744                                                      (nth 1 header)))
2745                                              (list 'quote (nth 1 (car ts)))
2746                                              (list 'gnus-score-delta-default
2747                                                    nil)
2748                                              (nth 1 (car ps))
2749                                              t)
2750                                             t)
2751                                            outp))
2752                                     (setq ps (cdr ps)))
2753                                   (list (nreverse outp))))
2754                                outt))
2755                         (setq ts (cdr ts)))
2756                       (list (nreverse outt))))
2757                    outh))
2758             (setq headers (cdr headers)))
2759           (list (nreverse outh))))))))
2760
2761 \f
2762
2763 (defun gnus-summary-mode (&optional group)
2764   "Major mode for reading articles.
2765
2766 All normal editing commands are switched off.
2767 \\<gnus-summary-mode-map>
2768 Each line in this buffer represents one article.  To read an
2769 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2770 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2771 respectively.
2772
2773 You can also post articles and send mail from this buffer.  To
2774 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2775 of an article, type `\\[gnus-summary-reply]'.
2776
2777 There are approx. one gazillion commands you can execute in this
2778 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2779
2780 The following commands are available:
2781
2782 \\{gnus-summary-mode-map}"
2783   (interactive)
2784   (kill-all-local-variables)
2785   (when (gnus-visual-p 'summary-menu 'menu)
2786     (gnus-summary-make-menu-bar)
2787     (gnus-summary-make-tool-bar))
2788   (gnus-summary-make-local-variables)
2789   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2790     (gnus-summary-make-local-variables))
2791   (gnus-make-thread-indent-array)
2792   (gnus-simplify-mode-line)
2793   (setq major-mode 'gnus-summary-mode)
2794   (setq mode-name "Summary")
2795   (make-local-variable 'minor-mode-alist)
2796   (use-local-map gnus-summary-mode-map)
2797   (buffer-disable-undo)
2798   (setq buffer-read-only t              ;Disable modification
2799         show-trailing-whitespace nil)
2800   (setq truncate-lines t)
2801   (setq selective-display t)
2802   (setq selective-display-ellipses t)   ;Display `...'
2803   (gnus-summary-set-display-table)
2804   (gnus-set-default-directory)
2805   (setq gnus-newsgroup-name group)
2806   (make-local-variable 'gnus-summary-line-format)
2807   (make-local-variable 'gnus-summary-line-format-spec)
2808   (make-local-variable 'gnus-summary-dummy-line-format)
2809   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2810   (make-local-variable 'gnus-summary-mark-positions)
2811   (gnus-make-local-hook 'pre-command-hook)
2812   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2813   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2814   (turn-on-gnus-mailing-list-mode)
2815   (mm-enable-multibyte)
2816   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2817   (gnus-update-summary-mark-positions))
2818
2819 (defun gnus-summary-make-local-variables ()
2820   "Make all the local summary buffer variables."
2821   (let (global)
2822     (dolist (local gnus-summary-local-variables)
2823       (if (consp local)
2824           (progn
2825             (if (eq (cdr local) 'global)
2826                 ;; Copy the global value of the variable.
2827                 (setq global (symbol-value (car local)))
2828               ;; Use the value from the list.
2829               (setq global (eval (cdr local))))
2830             (set (make-local-variable (car local)) global))
2831         ;; Simple nil-valued local variable.
2832         (set (make-local-variable local) nil)))))
2833
2834 (defun gnus-summary-clear-local-variables ()
2835   (let ((locals gnus-summary-local-variables))
2836     (while locals
2837       (if (consp (car locals))
2838           (and (symbolp (caar locals))
2839                (set (caar locals) nil))
2840         (and (symbolp (car locals))
2841              (set (car locals) nil)))
2842       (setq locals (cdr locals)))))
2843
2844 ;; Summary data functions.
2845
2846 (defmacro gnus-data-number (data)
2847   `(car ,data))
2848
2849 (defmacro gnus-data-set-number (data number)
2850   `(setcar ,data ,number))
2851
2852 (defmacro gnus-data-mark (data)
2853   `(nth 1 ,data))
2854
2855 (defmacro gnus-data-set-mark (data mark)
2856   `(setcar (nthcdr 1 ,data) ,mark))
2857
2858 (defmacro gnus-data-pos (data)
2859   `(nth 2 ,data))
2860
2861 (defmacro gnus-data-set-pos (data pos)
2862   `(setcar (nthcdr 2 ,data) ,pos))
2863
2864 (defmacro gnus-data-header (data)
2865   `(nth 3 ,data))
2866
2867 (defmacro gnus-data-set-header (data header)
2868   `(setf (nth 3 ,data) ,header))
2869
2870 (defmacro gnus-data-level (data)
2871   `(nth 4 ,data))
2872
2873 (defmacro gnus-data-unread-p (data)
2874   `(= (nth 1 ,data) gnus-unread-mark))
2875
2876 (defmacro gnus-data-read-p (data)
2877   `(/= (nth 1 ,data) gnus-unread-mark))
2878
2879 (defmacro gnus-data-pseudo-p (data)
2880   `(consp (nth 3 ,data)))
2881
2882 (defmacro gnus-data-find (number)
2883   `(assq ,number gnus-newsgroup-data))
2884
2885 (defmacro gnus-data-find-list (number &optional data)
2886   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2887      (memq (assq ,number bdata)
2888            bdata)))
2889
2890 (defmacro gnus-data-make (number mark pos header level)
2891   `(list ,number ,mark ,pos ,header ,level))
2892
2893 (defun gnus-data-enter (after-article number mark pos header level offset)
2894   (let ((data (gnus-data-find-list after-article)))
2895     (unless data
2896       (error "No such article: %d" after-article))
2897     (setcdr data (cons (gnus-data-make number mark pos header level)
2898                        (cdr data)))
2899     (setq gnus-newsgroup-data-reverse nil)
2900     (gnus-data-update-list (cddr data) offset)))
2901
2902 (defun gnus-data-enter-list (after-article list &optional offset)
2903   (when list
2904     (let ((data (and after-article (gnus-data-find-list after-article)))
2905           (ilist list))
2906       (if (not (or data
2907                    after-article))
2908           (let ((odata gnus-newsgroup-data))
2909             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2910             (when offset
2911               (gnus-data-update-list odata offset)))
2912         ;; Find the last element in the list to be spliced into the main
2913         ;; list.
2914         (setq list (last list))
2915         (if (not data)
2916             (progn
2917               (setcdr list gnus-newsgroup-data)
2918               (setq gnus-newsgroup-data ilist)
2919               (when offset
2920                 (gnus-data-update-list (cdr list) offset)))
2921           (setcdr list (cdr data))
2922           (setcdr data ilist)
2923           (when offset
2924             (gnus-data-update-list (cdr list) offset))))
2925       (setq gnus-newsgroup-data-reverse nil))))
2926
2927 (defun gnus-data-remove (article &optional offset)
2928   (let ((data gnus-newsgroup-data))
2929     (if (= (gnus-data-number (car data)) article)
2930         (progn
2931           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2932                 gnus-newsgroup-data-reverse nil)
2933           (when offset
2934             (gnus-data-update-list gnus-newsgroup-data offset)))
2935       (while (cdr data)
2936         (when (= (gnus-data-number (cadr data)) article)
2937           (setcdr data (cddr data))
2938           (when offset
2939             (gnus-data-update-list (cdr data) offset))
2940           (setq data nil
2941                 gnus-newsgroup-data-reverse nil))
2942         (setq data (cdr data))))))
2943
2944 (defmacro gnus-data-list (backward)
2945   `(if ,backward
2946        (or gnus-newsgroup-data-reverse
2947            (setq gnus-newsgroup-data-reverse
2948                  (reverse gnus-newsgroup-data)))
2949      gnus-newsgroup-data))
2950
2951 (defun gnus-data-update-list (data offset)
2952   "Add OFFSET to the POS of all data entries in DATA."
2953   (setq gnus-newsgroup-data-reverse nil)
2954   (while data
2955     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2956     (setq data (cdr data))))
2957
2958 (defun gnus-summary-article-pseudo-p (article)
2959   "Say whether this article is a pseudo article or not."
2960   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2961
2962 (defmacro gnus-summary-article-sparse-p (article)
2963   "Say whether this article is a sparse article or not."
2964   `(memq ,article gnus-newsgroup-sparse))
2965
2966 (defmacro gnus-summary-article-ancient-p (article)
2967   "Say whether this article is a sparse article or not."
2968   `(memq ,article gnus-newsgroup-ancient))
2969
2970 (defun gnus-article-parent-p (number)
2971   "Say whether this article is a parent or not."
2972   (let ((data (gnus-data-find-list number)))
2973     (and (cdr data)              ; There has to be an article after...
2974          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2975             (gnus-data-level (nth 1 data))))))
2976
2977 (defun gnus-article-children (number)
2978   "Return a list of all children to NUMBER."
2979   (let* ((data (gnus-data-find-list number))
2980          (level (gnus-data-level (car data)))
2981          children)
2982     (setq data (cdr data))
2983     (while (and data
2984                 (= (gnus-data-level (car data)) (1+ level)))
2985       (push (gnus-data-number (car data)) children)
2986       (setq data (cdr data)))
2987     children))
2988
2989 (defmacro gnus-summary-skip-intangible ()
2990   "If the current article is intangible, then jump to a different article."
2991   '(let ((to (get-text-property (point) 'gnus-intangible)))
2992      (and to (gnus-summary-goto-subject to))))
2993
2994 (defmacro gnus-summary-article-intangible-p ()
2995   "Say whether this article is intangible or not."
2996   '(get-text-property (point) 'gnus-intangible))
2997
2998 (defun gnus-article-read-p (article)
2999   "Say whether ARTICLE is read or not."
3000   (not (or (memq article gnus-newsgroup-marked)
3001            (memq article gnus-newsgroup-spam-marked)
3002            (memq article gnus-newsgroup-unreads)
3003            (memq article gnus-newsgroup-unselected)
3004            (memq article gnus-newsgroup-dormant))))
3005
3006 ;; Some summary mode macros.
3007
3008 (defmacro gnus-summary-article-number ()
3009   "The article number of the article on the current line.
3010 If there isn't an article number here, then we return the current
3011 article number."
3012   '(progn
3013      (gnus-summary-skip-intangible)
3014      (or (get-text-property (point) 'gnus-number)
3015          (gnus-summary-last-subject))))
3016
3017 (defmacro gnus-summary-article-header (&optional number)
3018   "Return the header of article NUMBER."
3019   `(gnus-data-header (gnus-data-find
3020                       ,(or number '(gnus-summary-article-number)))))
3021
3022 (defmacro gnus-summary-thread-level (&optional number)
3023   "Return the level of thread that starts with article NUMBER."
3024   `(if (and (eq gnus-summary-make-false-root 'dummy)
3025             (get-text-property (point) 'gnus-intangible))
3026        0
3027      (gnus-data-level (gnus-data-find
3028                        ,(or number '(gnus-summary-article-number))))))
3029
3030 (defmacro gnus-summary-article-mark (&optional number)
3031   "Return the mark of article NUMBER."
3032   `(gnus-data-mark (gnus-data-find
3033                     ,(or number '(gnus-summary-article-number)))))
3034
3035 (defmacro gnus-summary-article-pos (&optional number)
3036   "Return the position of the line of article NUMBER."
3037   `(gnus-data-pos (gnus-data-find
3038                    ,(or number '(gnus-summary-article-number)))))
3039
3040 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3041 (defmacro gnus-summary-article-subject (&optional number)
3042   "Return current subject string or nil if nothing."
3043   `(let ((headers
3044           ,(if number
3045                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3046              '(gnus-data-header (assq (gnus-summary-article-number)
3047                                       gnus-newsgroup-data)))))
3048      (and headers
3049           (vectorp headers)
3050           (mail-header-subject headers))))
3051
3052 (defmacro gnus-summary-article-score (&optional number)
3053   "Return current article score."
3054   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3055                   gnus-newsgroup-scored))
3056        gnus-summary-default-score 0))
3057
3058 (defun gnus-summary-article-children (&optional number)
3059   "Return a list of article numbers that are children of article NUMBER."
3060   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3061          (level (gnus-data-level (car data)))
3062          l children)
3063     (while (and (setq data (cdr data))
3064                 (> (setq l (gnus-data-level (car data))) level))
3065       (and (= (1+ level) l)
3066            (push (gnus-data-number (car data))
3067                  children)))
3068     (nreverse children)))
3069
3070 (defun gnus-summary-article-parent (&optional number)
3071   "Return the article number of the parent of article NUMBER."
3072   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3073                                     (gnus-data-list t)))
3074          (level (gnus-data-level (car data))))
3075     (if (zerop level)
3076         ()                              ; This is a root.
3077       ;; We search until we find an article with a level less than
3078       ;; this one.  That function has to be the parent.
3079       (while (and (setq data (cdr data))
3080                   (not (< (gnus-data-level (car data)) level))))
3081       (and data (gnus-data-number (car data))))))
3082
3083 (defun gnus-unread-mark-p (mark)
3084   "Say whether MARK is the unread mark."
3085   (= mark gnus-unread-mark))
3086
3087 (defun gnus-read-mark-p (mark)
3088   "Say whether MARK is one of the marks that mark as read.
3089 This is all marks except unread, ticked, dormant, and expirable."
3090   (not (or (= mark gnus-unread-mark)
3091            (= mark gnus-ticked-mark)
3092            (= mark gnus-spam-mark)
3093            (= mark gnus-dormant-mark)
3094            (= mark gnus-expirable-mark))))
3095
3096 (defmacro gnus-article-mark (number)
3097   "Return the MARK of article NUMBER.
3098 This macro should only be used when computing the mark the \"first\"
3099 time; i.e., when generating the summary lines.  After that,
3100 `gnus-summary-article-mark' should be used to examine the
3101 marks of articles."
3102   `(cond
3103     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3104     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3105     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3106     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3107     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3108     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3109     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3110     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3111            gnus-ancient-mark))))
3112
3113 ;; Saving hidden threads.
3114
3115 (defmacro gnus-save-hidden-threads (&rest forms)
3116   "Save hidden threads, eval FORMS, and restore the hidden threads."
3117   (let ((config (make-symbol "config")))
3118     `(let ((,config (gnus-hidden-threads-configuration)))
3119        (unwind-protect
3120            (save-excursion
3121              ,@forms)
3122          (gnus-restore-hidden-threads-configuration ,config)))))
3123 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3124 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3125
3126 (defun gnus-data-compute-positions ()
3127   "Compute the positions of all articles."
3128   (setq gnus-newsgroup-data-reverse nil)
3129   (let ((data gnus-newsgroup-data))
3130     (save-excursion
3131       (gnus-save-hidden-threads
3132         (gnus-summary-show-all-threads)
3133         (goto-char (point-min))
3134         (while data
3135           (while (get-text-property (point) 'gnus-intangible)
3136             (forward-line 1))
3137           (gnus-data-set-pos (car data) (+ (point) 3))
3138           (setq data (cdr data))
3139           (forward-line 1))))))
3140
3141 (defun gnus-hidden-threads-configuration ()
3142   "Return the current hidden threads configuration."
3143   (save-excursion
3144     (let (config)
3145       (goto-char (point-min))
3146       (while (search-forward "\r" nil t)
3147         (push (1- (point)) config))
3148       config)))
3149
3150 (defun gnus-restore-hidden-threads-configuration (config)
3151   "Restore hidden threads configuration from CONFIG."
3152   (save-excursion
3153     (let (point buffer-read-only)
3154       (while (setq point (pop config))
3155         (when (and (< point (point-max))
3156                    (goto-char point)
3157                    (eq (char-after) ?\n))
3158           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3159
3160 ;; Various summary mode internalish functions.
3161
3162 (defun gnus-mouse-pick-article (e)
3163   (interactive "e")
3164   (mouse-set-point e)
3165   (gnus-summary-next-page nil t))
3166
3167 (defun gnus-summary-set-display-table ()
3168   "Change the display table.
3169 Odd characters have a tendency to mess
3170 up nicely formatted displays - we make all possible glyphs
3171 display only a single character."
3172
3173   ;; We start from the standard display table, if any.
3174   (let ((table (or (copy-sequence standard-display-table)
3175                    (make-display-table)))
3176         (i 32))
3177     ;; Nix out all the control chars...
3178     (while (>= (setq i (1- i)) 0)
3179       (aset table i [??]))
3180    ;; ... but not newline and cr, of course.  (cr is necessary for the
3181     ;; selective display).
3182     (aset table ?\n nil)
3183     (aset table ?\r nil)
3184     ;; We keep TAB as well.
3185     (aset table ?\t nil)
3186     ;; We nix out any glyphs over 126 that are not set already.
3187     (let ((i 256))
3188       (while (>= (setq i (1- i)) 127)
3189         ;; Only modify if the entry is nil.
3190         (unless (aref table i)
3191           (aset table i [??]))))
3192     (setq buffer-display-table table)))
3193
3194 (defun gnus-summary-set-article-display-arrow (pos)
3195   "Update the overlay arrow to point to line at position POS."
3196   (when (and gnus-summary-display-arrow
3197              (boundp 'overlay-arrow-position)
3198              (boundp 'overlay-arrow-string))
3199     (save-excursion
3200       (goto-char pos)
3201       (beginning-of-line)
3202       (unless overlay-arrow-position
3203         (setq overlay-arrow-position (make-marker)))
3204       (setq overlay-arrow-string "=>"
3205             overlay-arrow-position (set-marker overlay-arrow-position
3206                                                (point)
3207                                                (current-buffer))))))
3208
3209 (defun gnus-summary-setup-buffer (group)
3210   "Initialize summary buffer."
3211   (let ((buffer (gnus-summary-buffer-name group))
3212         (dead-name (concat "*Dead Summary "
3213                            (gnus-group-decoded-name group) "*")))
3214     ;; If a dead summary buffer exists, we kill it.
3215     (when (gnus-buffer-live-p dead-name)
3216       (gnus-kill-buffer dead-name))
3217     (if (get-buffer buffer)
3218         (progn
3219           (set-buffer buffer)
3220           (setq gnus-summary-buffer (current-buffer))
3221           (not gnus-newsgroup-prepared))
3222       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3223       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3224       (gnus-summary-mode group)
3225       (when gnus-carpal
3226         (gnus-carpal-setup-buffer 'summary))
3227       (unless gnus-single-article-buffer
3228         (make-local-variable 'gnus-article-buffer)
3229         (make-local-variable 'gnus-article-current)
3230         (make-local-variable 'gnus-original-article-buffer))
3231       (setq gnus-newsgroup-name group)
3232       ;; Set any local variables in the group parameters.
3233       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3234       t)))
3235
3236 (defun gnus-set-global-variables ()
3237   "Set the global equivalents of the buffer-local variables.
3238 They are set to the latest values they had.  These reflect the summary
3239 buffer that was in action when the last article was fetched."
3240   (when (eq major-mode 'gnus-summary-mode)
3241     (setq gnus-summary-buffer (current-buffer))
3242     (let ((name gnus-newsgroup-name)
3243           (marked gnus-newsgroup-marked)
3244           (spam gnus-newsgroup-spam-marked)
3245           (unread gnus-newsgroup-unreads)
3246           (headers gnus-current-headers)
3247           (data gnus-newsgroup-data)
3248           (summary gnus-summary-buffer)
3249           (article-buffer gnus-article-buffer)
3250           (original gnus-original-article-buffer)
3251           (gac gnus-article-current)
3252           (reffed gnus-reffed-article-number)
3253           (score-file gnus-current-score-file)
3254           (default-charset gnus-newsgroup-charset)
3255           vlist)
3256       (let ((locals gnus-newsgroup-variables))
3257         (while locals
3258           (if (consp (car locals))
3259               (push (eval (caar locals)) vlist)
3260             (push (eval (car locals)) vlist))
3261           (setq locals (cdr locals)))
3262         (setq vlist (nreverse vlist)))
3263       (with-current-buffer gnus-group-buffer
3264         (setq gnus-newsgroup-name name
3265               gnus-newsgroup-marked marked
3266               gnus-newsgroup-spam-marked spam
3267               gnus-newsgroup-unreads unread
3268               gnus-current-headers headers
3269               gnus-newsgroup-data data
3270               gnus-article-current gac
3271               gnus-summary-buffer summary
3272               gnus-article-buffer article-buffer
3273               gnus-original-article-buffer original
3274               gnus-reffed-article-number reffed
3275               gnus-current-score-file score-file
3276               gnus-newsgroup-charset default-charset)
3277         (let ((locals gnus-newsgroup-variables))
3278           (while locals
3279             (if (consp (car locals))
3280                 (set (caar locals) (pop vlist))
3281               (set (car locals) (pop vlist)))
3282             (setq locals (cdr locals))))
3283         ;; The article buffer also has local variables.
3284         (when (gnus-buffer-live-p gnus-article-buffer)
3285           (set-buffer gnus-article-buffer)
3286           (setq gnus-summary-buffer summary))))))
3287
3288 (defun gnus-summary-article-unread-p (article)
3289   "Say whether ARTICLE is unread or not."
3290   (memq article gnus-newsgroup-unreads))
3291
3292 (defun gnus-summary-first-article-p (&optional article)
3293   "Return whether ARTICLE is the first article in the buffer."
3294   (if (not (setq article (or article (gnus-summary-article-number))))
3295       nil
3296     (eq article (caar gnus-newsgroup-data))))
3297
3298 (defun gnus-summary-last-article-p (&optional article)
3299   "Return whether ARTICLE is the last article in the buffer."
3300   (if (not (setq article (or article (gnus-summary-article-number))))
3301       ;; All non-existent numbers are the last article.  :-)
3302       t
3303     (not (cdr (gnus-data-find-list article)))))
3304
3305 (defun gnus-make-thread-indent-array ()
3306   (let ((n 200))
3307     (unless (and gnus-thread-indent-array
3308                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3309       (setq gnus-thread-indent-array (make-vector 201 "")
3310             gnus-thread-indent-array-level gnus-thread-indent-level)
3311       (while (>= n 0)
3312         (aset gnus-thread-indent-array n
3313               (make-string (* n gnus-thread-indent-level) ? ))
3314         (setq n (1- n))))))
3315
3316 (defun gnus-update-summary-mark-positions ()
3317   "Compute where the summary marks are to go."
3318   (save-excursion
3319     (when (gnus-buffer-exists-p gnus-summary-buffer)
3320       (set-buffer gnus-summary-buffer))
3321     (let ((spec gnus-summary-line-format-spec)
3322           pos)
3323       (save-excursion
3324         (gnus-set-work-buffer)
3325         (let ((gnus-tmp-unread ?Z)
3326               (gnus-replied-mark ?Z)
3327               (gnus-score-below-mark ?Z)
3328               (gnus-score-over-mark ?Z)
3329               (gnus-undownloaded-mark ?Z)
3330               (gnus-summary-line-format-spec spec)
3331               (gnus-newsgroup-downloadable '(0))
3332               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3333               case-fold-search ignores)
3334           ;; Here, all marks are bound to Z.
3335           (gnus-summary-insert-line header
3336                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3337           (goto-char (point-min))
3338           ;; Memorize the positions of the same characters as dummy marks.
3339           (while (re-search-forward "[A-D]" nil t)
3340             (push (point) ignores))
3341           (erase-buffer)
3342           ;; We use A-D as dummy marks in order to know column positions
3343           ;; where marks should be inserted.
3344           (setq gnus-tmp-unread ?A
3345                 gnus-replied-mark ?B
3346                 gnus-score-below-mark ?C
3347                 gnus-score-over-mark ?C
3348                 gnus-undownloaded-mark ?D)
3349           (gnus-summary-insert-line header
3350                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3351           ;; Ignore characters which aren't dummy marks.
3352           (dolist (p ignores)
3353             (delete-region (goto-char (1- p)) p)
3354             (insert ?Z))
3355           (goto-char (point-min))
3356           (setq pos (list (cons 'unread
3357                                 (and (search-forward "A" nil t)
3358                                      (- (point) (point-min) 1)))))
3359           (goto-char (point-min))
3360           (push (cons 'replied (and (search-forward "B" nil t)
3361                                     (- (point) (point-min) 1)))
3362                 pos)
3363           (goto-char (point-min))
3364           (push (cons 'score (and (search-forward "C" nil t)
3365                                   (- (point) (point-min) 1)))
3366                 pos)
3367           (goto-char (point-min))
3368           (push (cons 'download (and (search-forward "D" nil t)
3369                                      (- (point) (point-min) 1)))
3370                 pos)))
3371       (setq gnus-summary-mark-positions pos))))
3372
3373 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3374   "Insert a dummy root in the summary buffer."
3375   (beginning-of-line)
3376   (gnus-add-text-properties
3377    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3378    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3379
3380 (defun gnus-summary-extract-address-component (from)
3381   (or (car (funcall gnus-extract-address-components from))
3382       from))
3383
3384 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3385   (let ((mail-parse-charset gnus-newsgroup-charset)
3386         ; Is it really necessary to do this next part for each summary line?
3387         ; Luckily, doesn't seem to slow things down much.
3388         (mail-parse-ignored-charsets
3389          (with-current-buffer gnus-summary-buffer
3390            gnus-newsgroup-ignored-charsets)))
3391     (or
3392      (and gnus-ignored-from-addresses
3393           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3394           (let ((extra-headers (mail-header-extra header))
3395                 to
3396                 newsgroups)
3397             (cond
3398              ((setq to (cdr (assq 'To extra-headers)))
3399               (concat gnus-summary-to-prefix
3400                       (inline
3401                         (gnus-summary-extract-address-component
3402                          (funcall gnus-decode-encoded-word-function to)))))
3403              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3404               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3405      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3406
3407 (defun gnus-summary-insert-line (gnus-tmp-header
3408                                  gnus-tmp-level gnus-tmp-current
3409                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3410                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3411                                  &optional gnus-tmp-dummy gnus-tmp-score
3412                                  gnus-tmp-process)
3413   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3414          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3415          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3416          (gnus-tmp-score-char
3417           (if (or (null gnus-summary-default-score)
3418                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3419                       gnus-summary-zcore-fuzz))
3420               ?                         ;Whitespace
3421             (if (< gnus-tmp-score gnus-summary-default-score)
3422                 gnus-score-below-mark gnus-score-over-mark)))
3423          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3424          (gnus-tmp-replied
3425           (cond (gnus-tmp-process gnus-process-mark)
3426                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3427                  gnus-cached-mark)
3428                 (gnus-tmp-replied gnus-replied-mark)
3429                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3430                  gnus-forwarded-mark)
3431                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3432                  gnus-saved-mark)
3433                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3434                  gnus-recent-mark)
3435                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3436                  gnus-unseen-mark)
3437                 (t gnus-no-mark)))
3438          (gnus-tmp-downloaded
3439           (cond (undownloaded
3440                  gnus-undownloaded-mark)
3441                 (gnus-newsgroup-agentized
3442                  gnus-downloaded-mark)
3443                 (t
3444                  gnus-no-mark)))
3445          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3446          (gnus-tmp-name
3447           (cond
3448            ((string-match "<[^>]+> *$" gnus-tmp-from)
3449             (let ((beg (match-beginning 0)))
3450               (or (and (string-match "^\".+\"" gnus-tmp-from)
3451                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3452                   (substring gnus-tmp-from 0 beg))))
3453            ((string-match "(.+)" gnus-tmp-from)
3454             (substring gnus-tmp-from
3455                        (1+ (match-beginning 0)) (1- (match-end 0))))
3456            (t gnus-tmp-from)))
3457          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3458          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3459          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3460          (buffer-read-only nil))
3461     (when (string= gnus-tmp-name "")
3462       (setq gnus-tmp-name gnus-tmp-from))
3463     (unless (numberp gnus-tmp-lines)
3464       (setq gnus-tmp-lines -1))
3465     (if (= gnus-tmp-lines -1)
3466         (setq gnus-tmp-lines "?")
3467       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3468       (gnus-put-text-property
3469      (point)
3470      (progn (eval gnus-summary-line-format-spec) (point))
3471        'gnus-number gnus-tmp-number)
3472     (when (gnus-visual-p 'summary-highlight 'highlight)
3473       (forward-line -1)
3474       (gnus-run-hooks 'gnus-summary-update-hook)
3475       (forward-line 1))))
3476
3477 (defun gnus-summary-update-line (&optional dont-update)
3478   "Update summary line after change."
3479   (when (and gnus-summary-default-score
3480              (not gnus-summary-inhibit-highlight))
3481     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3482            (article (gnus-summary-article-number))
3483            (score (gnus-summary-article-score article)))
3484       (unless dont-update
3485         (if (and gnus-summary-mark-below
3486                  (< (gnus-summary-article-score)
3487                     gnus-summary-mark-below))
3488             ;; This article has a low score, so we mark it as read.
3489             (when (memq article gnus-newsgroup-unreads)
3490               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3491           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3492             ;; This article was previously marked as read on account
3493             ;; of a low score, but now it has risen, so we mark it as
3494             ;; unread.
3495             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3496         (gnus-summary-update-mark
3497          (if (or (null gnus-summary-default-score)
3498                  (<= (abs (- score gnus-summary-default-score))
3499                      gnus-summary-zcore-fuzz))
3500              ?                          ;Whitespace
3501            (if (< score gnus-summary-default-score)
3502                gnus-score-below-mark gnus-score-over-mark))
3503          'score))
3504       ;; Do visual highlighting.
3505       (when (gnus-visual-p 'summary-highlight 'highlight)
3506         (gnus-run-hooks 'gnus-summary-update-hook)))))
3507
3508 (defvar gnus-tmp-new-adopts nil)
3509
3510 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3511   "Return the number of articles in THREAD.
3512 This may be 0 in some cases -- if none of the articles in
3513 the thread are to be displayed."
3514   (let* ((number
3515          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3516           (cond
3517            ((not (listp thread))
3518             1)
3519            ((and (consp thread) (cdr thread))
3520             (apply
3521              '+ 1 (mapcar
3522                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3523            ((null thread)
3524             1)
3525            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3526             1)
3527            (t 0))))
3528     (when (and level (zerop level) gnus-tmp-new-adopts)
3529       (incf number
3530             (apply '+ (mapcar
3531                        'gnus-summary-number-of-articles-in-thread
3532                        gnus-tmp-new-adopts))))
3533     (if char
3534         (if (> number 1) gnus-not-empty-thread-mark
3535           gnus-empty-thread-mark)
3536       number)))
3537
3538 (defsubst gnus-summary-line-message-size (head)
3539   "Return pretty-printed version of message size.
3540 This function is intended to be used in
3541 `gnus-summary-line-format-alist'."
3542   (let ((c (or (mail-header-chars head) -1)))
3543     (cond ((< c 0) "n/a")               ; chars not available
3544           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3545           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3546           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3547           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3548
3549
3550 (defun gnus-summary-set-local-parameters (group)
3551   "Go through the local params of GROUP and set all variable specs in that list."
3552   (let ((vars '(quit-config)))          ; Ignore quit-config.
3553     (dolist (elem (gnus-group-find-parameter group))
3554       (and (consp elem)                 ; Has to be a cons.
3555            (consp (cdr elem))           ; The cdr has to be a list.
3556            (symbolp (car elem))         ; Has to be a symbol in there.
3557            (not (memq (car elem) vars))
3558            (ignore-errors               ; So we set it.
3559              (push (car elem) vars)
3560              (make-local-variable (car elem))
3561              (set (car elem) (eval (nth 1 elem))))))))
3562
3563 (defun gnus-summary-read-group (group &optional show-all no-article
3564                                       kill-buffer no-display backward
3565                                       select-articles)
3566   "Start reading news in newsgroup GROUP.
3567 If SHOW-ALL is non-nil, already read articles are also listed.
3568 If NO-ARTICLE is non-nil, no article is selected initially.
3569 If NO-DISPLAY, don't generate a summary buffer."
3570   (let (result)
3571     (while (and group
3572                 (null (setq result
3573                             (let ((gnus-auto-select-next nil))
3574                               (or (gnus-summary-read-group-1
3575                                    group show-all no-article
3576                                    kill-buffer no-display
3577                                    select-articles)
3578                                   (setq show-all nil
3579                                         select-articles nil)))))
3580                 (eq gnus-auto-select-next 'quietly))
3581       (set-buffer gnus-group-buffer)
3582       ;; The entry function called above goes to the next
3583       ;; group automatically, so we go two groups back
3584       ;; if we are searching for the previous group.
3585       (when backward
3586         (gnus-group-prev-unread-group 2))
3587       (if (not (equal group (gnus-group-group-name)))
3588           (setq group (gnus-group-group-name))
3589         (setq group nil)))
3590     result))
3591
3592 (defun gnus-summary-read-group-1 (group show-all no-article
3593                                         kill-buffer no-display
3594                                         &optional select-articles)
3595   ;; Killed foreign groups can't be entered.
3596   ;;  (when (and (not (gnus-group-native-p group))
3597   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3598   ;;    (error "Dead non-native groups can't be entered"))
3599   (gnus-message 5 "Retrieving newsgroup: %s..."
3600                 (gnus-group-decoded-name group))
3601   (let* ((new-group (gnus-summary-setup-buffer group))
3602          (quit-config (gnus-group-quit-config group))
3603          (did-select (and new-group (gnus-select-newsgroup
3604                                      group show-all select-articles))))
3605     (cond
3606      ;; This summary buffer exists already, so we just select it.
3607      ((not new-group)
3608       (gnus-set-global-variables)
3609       (when kill-buffer
3610         (gnus-kill-or-deaden-summary kill-buffer))
3611       (gnus-configure-windows 'summary 'force)
3612       (gnus-set-mode-line 'summary)
3613       (gnus-summary-position-point)
3614       (message "")
3615       t)
3616      ;; We couldn't select this group.
3617      ((null did-select)
3618       (when (and (eq major-mode 'gnus-summary-mode)
3619                  (not (equal (current-buffer) kill-buffer)))
3620         (kill-buffer (current-buffer))
3621         (if (not quit-config)
3622             (progn
3623               ;; Update the info -- marks might need to be removed,
3624               ;; for instance.
3625               (gnus-summary-update-info)
3626               (set-buffer gnus-group-buffer)
3627               (gnus-group-jump-to-group group)
3628               (gnus-group-next-unread-group 1))
3629           (gnus-handle-ephemeral-exit quit-config)))
3630       (let ((grpinfo (gnus-get-info group)))
3631         (if (null (gnus-info-read grpinfo))
3632             (gnus-message 3 "Group %s contains no messages"
3633                           (gnus-group-decoded-name group))
3634           (gnus-message 3 "Can't select group")))
3635       nil)
3636      ;; The user did a `C-g' while prompting for number of articles,
3637      ;; so we exit this group.
3638      ((eq did-select 'quit)
3639       (and (eq major-mode 'gnus-summary-mode)
3640            (not (equal (current-buffer) kill-buffer))
3641            (kill-buffer (current-buffer)))
3642       (when kill-buffer
3643         (gnus-kill-or-deaden-summary kill-buffer))
3644       (if (not quit-config)
3645           (progn
3646             (set-buffer gnus-group-buffer)
3647             (gnus-group-jump-to-group group)
3648             (gnus-group-next-unread-group 1)
3649             (gnus-configure-windows 'group 'force))
3650         (gnus-handle-ephemeral-exit quit-config))
3651       ;; Finally signal the quit.
3652       (signal 'quit nil))
3653      ;; The group was successfully selected.
3654      (t
3655       (gnus-set-global-variables)
3656       ;; Save the active value in effect when the group was entered.
3657       (setq gnus-newsgroup-active
3658             (gnus-copy-sequence
3659              (gnus-active gnus-newsgroup-name)))
3660       ;; You can change the summary buffer in some way with this hook.
3661       (gnus-run-hooks 'gnus-select-group-hook)
3662       (when (memq 'summary (gnus-update-format-specifications
3663                             nil 'summary 'summary-mode 'summary-dummy))
3664         ;; The format specification for the summary line was updated,
3665         ;; so we need to update the mark positions as well.
3666         (gnus-update-summary-mark-positions))
3667       ;; Do score processing.
3668       (when gnus-use-scoring
3669         (gnus-possibly-score-headers))
3670       ;; Check whether to fill in the gaps in the threads.
3671       (when gnus-build-sparse-threads
3672         (gnus-build-sparse-threads))
3673       ;; Find the initial limit.
3674       (if show-all
3675           (let ((gnus-newsgroup-dormant nil))
3676             (gnus-summary-initial-limit show-all))
3677         (gnus-summary-initial-limit show-all))
3678       ;; Generate the summary buffer.
3679       (unless no-display
3680         (gnus-summary-prepare))
3681       (when gnus-use-trees
3682         (gnus-tree-open group)
3683         (setq gnus-summary-highlight-line-function
3684               'gnus-tree-highlight-article))
3685       ;; If the summary buffer is empty, but there are some low-scored
3686       ;; articles or some excluded dormants, we include these in the
3687       ;; buffer.
3688       (when (and (zerop (buffer-size))
3689                  (not no-display))
3690         (cond (gnus-newsgroup-dormant
3691                (gnus-summary-limit-include-dormant))
3692               ((and gnus-newsgroup-scored show-all)
3693                (gnus-summary-limit-include-expunged t))))
3694       ;; Function `gnus-apply-kill-file' must be called in this hook.
3695       (gnus-run-hooks 'gnus-apply-kill-hook)
3696       (if (and (zerop (buffer-size))
3697                (not no-display))
3698           (progn
3699             ;; This newsgroup is empty.
3700             (gnus-summary-catchup-and-exit nil t)
3701             (gnus-message 6 "No unread news")
3702             (when kill-buffer
3703               (gnus-kill-or-deaden-summary kill-buffer))
3704             ;; Return nil from this function.
3705             nil)
3706         ;; Hide conversation thread subtrees.  We cannot do this in
3707         ;; gnus-summary-prepare-hook since kill processing may not
3708         ;; work with hidden articles.
3709         (gnus-summary-maybe-hide-threads)
3710         (when kill-buffer
3711           (gnus-kill-or-deaden-summary kill-buffer))
3712         (gnus-summary-auto-select-subject)
3713         ;; Show first unread article if requested.
3714         (if (and (not no-article)
3715                  (not no-display)
3716                  gnus-newsgroup-unreads
3717                  gnus-auto-select-first)
3718             (progn
3719               (gnus-configure-windows 'summary)
3720               (let ((art (gnus-summary-article-number)))
3721                 (unless (and (not gnus-plugged)
3722                              (or (memq art gnus-newsgroup-undownloaded)
3723                                  (memq art gnus-newsgroup-downloadable)))
3724                   (gnus-summary-goto-article art))))
3725           ;; Don't select any articles.
3726           (gnus-summary-position-point)
3727           (gnus-configure-windows 'summary 'force)
3728           (gnus-set-mode-line 'summary))
3729         (when (and gnus-auto-center-group
3730                    (get-buffer-window gnus-group-buffer t))
3731           ;; Gotta use windows, because recenter does weird stuff if
3732           ;; the current buffer ain't the displayed window.
3733           (let ((owin (selected-window)))
3734             (select-window (get-buffer-window gnus-group-buffer t))
3735             (when (gnus-group-goto-group group)
3736               (recenter))
3737             (select-window owin)))
3738         ;; Mark this buffer as "prepared".
3739         (setq gnus-newsgroup-prepared t)
3740         (gnus-run-hooks 'gnus-summary-prepared-hook)
3741         (unless (gnus-ephemeral-group-p group)
3742           (gnus-group-update-group group))
3743         t)))))
3744
3745 (defun gnus-summary-auto-select-subject ()
3746   "Select the subject line on initial group entry."
3747   (goto-char (point-min))
3748   (cond
3749    ((eq gnus-auto-select-subject 'best)
3750     (gnus-summary-best-unread-subject))
3751    ((eq gnus-auto-select-subject 'unread)
3752     (gnus-summary-first-unread-subject))
3753    ((eq gnus-auto-select-subject 'unseen)
3754     (gnus-summary-first-unseen-subject))
3755    ((eq gnus-auto-select-subject 'unseen-or-unread)
3756     (gnus-summary-first-unseen-or-unread-subject))
3757    ((eq gnus-auto-select-subject 'first)
3758     ;; Do nothing.
3759     )
3760    ((functionp gnus-auto-select-subject)
3761     (funcall gnus-auto-select-subject))))
3762
3763 (defun gnus-summary-prepare ()
3764   "Generate the summary buffer."
3765   (interactive)
3766   (let ((buffer-read-only nil))
3767     (erase-buffer)
3768     (setq gnus-newsgroup-data nil
3769           gnus-newsgroup-data-reverse nil)
3770     (gnus-run-hooks 'gnus-summary-generate-hook)
3771     ;; Generate the buffer, either with threads or without.
3772     (when gnus-newsgroup-headers
3773       (gnus-summary-prepare-threads
3774        (if gnus-show-threads
3775            (gnus-sort-gathered-threads
3776             (funcall gnus-summary-thread-gathering-function
3777                      (gnus-sort-threads
3778                       (gnus-cut-threads (gnus-make-threads)))))
3779          ;; Unthreaded display.
3780          (gnus-sort-articles gnus-newsgroup-headers))))
3781     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3782     ;; Call hooks for modifying summary buffer.
3783     (goto-char (point-min))
3784     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3785
3786 (defsubst gnus-general-simplify-subject (subject)
3787   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3788   (setq subject
3789         (cond
3790          ;; Truncate the subject.
3791          (gnus-simplify-subject-functions
3792           (gnus-map-function gnus-simplify-subject-functions subject))
3793          ((numberp gnus-summary-gather-subject-limit)
3794           (setq subject (gnus-simplify-subject-re subject))
3795           (if (> (length subject) gnus-summary-gather-subject-limit)
3796               (substring subject 0 gnus-summary-gather-subject-limit)
3797             subject))
3798          ;; Fuzzily simplify it.
3799          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3800           (gnus-simplify-subject-fuzzy subject))
3801          ;; Just remove the leading "Re:".
3802          (t
3803           (gnus-simplify-subject-re subject))))
3804
3805   (if (and gnus-summary-gather-exclude-subject
3806            (string-match gnus-summary-gather-exclude-subject subject))
3807       nil                         ; This article shouldn't be gathered
3808     subject))
3809
3810 (defun gnus-summary-simplify-subject-query ()
3811   "Query where the respool algorithm would put this article."
3812   (interactive)
3813   (gnus-summary-select-article)
3814   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3815
3816 (defun gnus-gather-threads-by-subject (threads)
3817   "Gather threads by looking at Subject headers."
3818   (if (not gnus-summary-make-false-root)
3819       threads
3820     (let ((hashtb (gnus-make-hashtable 1024))
3821           (prev threads)
3822           (result threads)
3823           subject hthread whole-subject)
3824       (while threads
3825         (setq subject (gnus-general-simplify-subject
3826                        (setq whole-subject (mail-header-subject
3827                                             (caar threads)))))
3828         (when subject
3829           (if (setq hthread (gnus-gethash subject hashtb))
3830               (progn
3831                 ;; We enter a dummy root into the thread, if we
3832                 ;; haven't done that already.
3833                 (unless (stringp (caar hthread))
3834                   (setcar hthread (list whole-subject (car hthread))))
3835                 ;; We add this new gathered thread to this gathered
3836                 ;; thread.
3837                 (setcdr (car hthread)
3838                         (nconc (cdar hthread) (list (car threads))))
3839                 ;; Remove it from the list of threads.
3840                 (setcdr prev (cdr threads))
3841                 (setq threads prev))
3842             ;; Enter this thread into the hash table.
3843             (gnus-sethash subject
3844                           (if gnus-summary-make-false-root-always
3845                               (progn
3846                                 ;; If you want a dummy root above all
3847                                 ;; threads...
3848                                 (setcar threads (list whole-subject
3849                                                       (car threads)))
3850                                 threads)
3851                             threads)
3852                           hashtb)))
3853         (setq prev threads)
3854         (setq threads (cdr threads)))
3855       result)))
3856
3857 (defun gnus-gather-threads-by-references (threads)
3858   "Gather threads by looking at References headers."
3859   (let ((idhashtb (gnus-make-hashtable 1024))
3860         (thhashtb (gnus-make-hashtable 1024))
3861         (prev threads)
3862         (result threads)
3863         ids references id gthread gid entered ref)
3864     (while threads
3865       (when (setq references (mail-header-references (caar threads)))
3866         (setq id (mail-header-id (caar threads))
3867               ids (inline (gnus-split-references references))
3868               entered nil)
3869         (while (setq ref (pop ids))
3870           (setq ids (delete ref ids))
3871           (if (not (setq gid (gnus-gethash ref idhashtb)))
3872               (progn
3873                 (gnus-sethash ref id idhashtb)
3874                 (gnus-sethash id threads thhashtb))
3875             (setq gthread (gnus-gethash gid thhashtb))
3876             (unless entered
3877               ;; We enter a dummy root into the thread, if we
3878               ;; haven't done that already.
3879               (unless (stringp (caar gthread))
3880                 (setcar gthread (list (mail-header-subject (caar gthread))
3881                                       (car gthread))))
3882               ;; We add this new gathered thread to this gathered
3883               ;; thread.
3884               (setcdr (car gthread)
3885                       (nconc (cdar gthread) (list (car threads)))))
3886             ;; Add it into the thread hash table.
3887             (gnus-sethash id gthread thhashtb)
3888             (setq entered t)
3889             ;; Remove it from the list of threads.
3890             (setcdr prev (cdr threads))
3891             (setq threads prev))))
3892       (setq prev threads)
3893       (setq threads (cdr threads)))
3894     result))
3895
3896 (defun gnus-sort-gathered-threads (threads)
3897   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3898   (let ((result threads))
3899     (while threads
3900       (when (stringp (caar threads))
3901         (setcdr (car threads)
3902                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3903       (setq threads (cdr threads)))
3904     result))
3905
3906 (defun gnus-thread-loop-p (root thread)
3907   "Say whether ROOT is in THREAD."
3908   (let ((stack (list thread))
3909         (infloop 0)
3910         th)
3911     (while (setq thread (pop stack))
3912       (setq th (cdr thread))
3913       (while (and th
3914                   (not (eq (caar th) root)))
3915         (pop th))
3916       (if th
3917           ;; We have found a loop.
3918           (let (ref-dep)
3919             (setcdr thread (delq (car th) (cdr thread)))
3920             (if (boundp (setq ref-dep (intern "none"
3921                                               gnus-newsgroup-dependencies)))
3922                 (setcdr (symbol-value ref-dep)
3923                         (nconc (cdr (symbol-value ref-dep))
3924                                (list (car th))))
3925               (set ref-dep (list nil (car th))))
3926             (setq infloop 1
3927                   stack nil))
3928         ;; Push all the subthreads onto the stack.
3929         (push (cdr thread) stack)))
3930     infloop))
3931
3932 (defun gnus-make-threads ()
3933   "Go through the dependency hashtb and find the roots.  Return all threads."
3934   (let (threads)
3935     (while (catch 'infloop
3936              (mapatoms
3937               (lambda (refs)
3938                 ;; Deal with self-referencing References loops.
3939                 (when (and (car (symbol-value refs))
3940                            (not (zerop
3941                                  (apply
3942                                   '+
3943                                   (mapcar
3944                                    (lambda (thread)
3945                                      (gnus-thread-loop-p
3946                                       (car (symbol-value refs)) thread))
3947                                    (cdr (symbol-value refs)))))))
3948                   (setq threads nil)
3949                   (throw 'infloop t))
3950                 (unless (car (symbol-value refs))
3951                   ;; These threads do not refer back to any other
3952                   ;; articles, so they're roots.
3953                   (setq threads (append (cdr (symbol-value refs)) threads))))
3954               gnus-newsgroup-dependencies)))
3955     threads))
3956
3957 ;; Build the thread tree.
3958 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3959   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3960
3961 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3962 if it was already present.
3963
3964 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3965 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3966 Message-IDs will be renamed to a unique Message-ID before being
3967 entered.
3968
3969 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3970   (let* ((id (mail-header-id header))
3971          (id-dep (and id (intern id dependencies)))
3972          parent-id ref ref-dep ref-header replaced)
3973     ;; Enter this `header' in the `dependencies' table.
3974     (cond
3975      ((not id-dep)
3976       (setq header nil))
3977      ;; The first two cases do the normal part: enter a new `header'
3978      ;; in the `dependencies' table.
3979      ((not (boundp id-dep))
3980       (set id-dep (list header)))
3981      ((null (car (symbol-value id-dep)))
3982       (setcar (symbol-value id-dep) header))
3983
3984      ;; From here the `header' was already present in the
3985      ;; `dependencies' table.
3986      (force-new
3987       ;; Overrides an existing entry;
3988       ;; just set the header part of the entry.
3989       (setcar (symbol-value id-dep) header)
3990       (setq replaced t))
3991
3992      ;; Renames the existing `header' to a unique Message-ID.
3993      ((not gnus-summary-ignore-duplicates)
3994       ;; An article with this Message-ID has already been seen.
3995       ;; We rename the Message-ID.
3996       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3997            (list header))
3998       (mail-header-set-id header id))
3999
4000      ;; The last case ignores an existing entry, except it adds any
4001      ;; additional Xrefs (in case the two articles came from different
4002      ;; servers.
4003      ;; Also sets `header' to `nil' meaning that the `dependencies'
4004      ;; table was *not* modified.
4005      (t
4006       (mail-header-set-xref
4007        (car (symbol-value id-dep))
4008        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4009                    "")
4010                (or (mail-header-xref header) "")))
4011       (setq header nil)))
4012
4013     (when (and header (not replaced))
4014       ;; First check that we are not creating a References loop.
4015       (setq parent-id (gnus-parent-id (mail-header-references header)))
4016       (setq ref parent-id)
4017       (while (and ref
4018                   (setq ref-dep (intern-soft ref dependencies))
4019                   (boundp ref-dep)
4020                   (setq ref-header (car (symbol-value ref-dep))))
4021         (if (string= id ref)
4022             ;; Yuk!  This is a reference loop.  Make the article be a
4023             ;; root article.
4024             (progn
4025               (mail-header-set-references (car (symbol-value id-dep)) "none")
4026               (setq ref nil)
4027               (setq parent-id nil))
4028           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4029       (setq ref-dep (intern (or parent-id "none") dependencies))
4030       (if (boundp ref-dep)
4031           (setcdr (symbol-value ref-dep)
4032                   (nconc (cdr (symbol-value ref-dep))
4033                          (list (symbol-value id-dep))))
4034         (set ref-dep (list nil (symbol-value id-dep)))))
4035     header))
4036
4037 (defun gnus-extract-message-id-from-in-reply-to (string)
4038   (if (string-match "<[^>]+>" string)
4039       (substring string (match-beginning 0) (match-end 0))
4040     nil))
4041
4042 (defun gnus-build-sparse-threads ()
4043   (let ((headers gnus-newsgroup-headers)
4044         (mail-parse-charset gnus-newsgroup-charset)
4045         (gnus-summary-ignore-duplicates t)
4046         header references generation relations
4047         subject child end new-child date)
4048     ;; First we create an alist of generations/relations, where
4049     ;; generations is how much we trust the relation, and the relation
4050     ;; is parent/child.
4051     (gnus-message 7 "Making sparse threads...")
4052     (save-excursion
4053       (nnheader-set-temp-buffer " *gnus sparse threads*")
4054       (while (setq header (pop headers))
4055         (when (and (setq references (mail-header-references header))
4056                    (not (string= references "")))
4057           (insert references)
4058           (setq child (mail-header-id header)
4059                 subject (mail-header-subject header)
4060                 date (mail-header-date header)
4061                 generation 0)
4062           (while (search-backward ">" nil t)
4063             (setq end (1+ (point)))
4064             (when (search-backward "<" nil t)
4065               (setq new-child (buffer-substring (point) end))
4066               (push (list (incf generation)
4067                           child (setq child new-child)
4068                           subject date)
4069                     relations)))
4070           (when child
4071             (push (list (1+ generation) child nil subject) relations))
4072           (erase-buffer)))
4073       (kill-buffer (current-buffer)))
4074     ;; Sort over trustworthiness.
4075     (mapcar
4076      (lambda (relation)
4077        (when (gnus-dependencies-add-header
4078               (make-full-mail-header
4079                gnus-reffed-article-number
4080                (nth 3 relation) "" (or (nth 4 relation) "")
4081                (nth 1 relation)
4082                (or (nth 2 relation) "") 0 0 "")
4083               gnus-newsgroup-dependencies nil)
4084          (push gnus-reffed-article-number gnus-newsgroup-limit)
4085          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4086          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4087                gnus-newsgroup-reads)
4088          (decf gnus-reffed-article-number)))
4089      (sort relations 'car-less-than-car))
4090     (gnus-message 7 "Making sparse threads...done")))
4091
4092 (defun gnus-build-old-threads ()
4093   ;; Look at all the articles that refer back to old articles, and
4094   ;; fetch the headers for the articles that aren't there.  This will
4095   ;; build complete threads - if the roots haven't been expired by the
4096   ;; server, that is.
4097   (let ((mail-parse-charset gnus-newsgroup-charset)
4098         id heads)
4099     (mapatoms
4100      (lambda (refs)
4101        (when (not (car (symbol-value refs)))
4102          (setq heads (cdr (symbol-value refs)))
4103          (while heads
4104            (if (memq (mail-header-number (caar heads))
4105                      gnus-newsgroup-dormant)
4106                (setq heads (cdr heads))
4107              (setq id (symbol-name refs))
4108              (while (and (setq id (gnus-build-get-header id))
4109                          (not (car (gnus-id-to-thread id)))))
4110              (setq heads nil)))))
4111      gnus-newsgroup-dependencies)))
4112
4113 (defsubst gnus-remove-odd-characters (string)
4114   "Translate STRING into something that doesn't contain weird characters."
4115   (mm-subst-char-in-string
4116    ?\r ?\-
4117    (mm-subst-char-in-string ?\n ?\- string t) t))
4118
4119 ;; This function has to be called with point after the article number
4120 ;; on the beginning of the line.
4121 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4122   (let ((eol (point-at-eol))
4123         (buffer (current-buffer))
4124         header references in-reply-to)
4125
4126     ;; overview: [num subject from date id refs chars lines misc]
4127     (unwind-protect
4128         (let (x)
4129           (narrow-to-region (point) eol)
4130           (unless (eobp)
4131             (forward-char))
4132
4133           (setq header
4134                 (make-full-mail-header
4135                  number                 ; number
4136                  (condition-case ()     ; subject
4137                      (gnus-remove-odd-characters
4138                       (funcall gnus-decode-encoded-word-function
4139                                (setq x (nnheader-nov-field))))
4140                    (error x))
4141                  (condition-case ()     ; from
4142                      (gnus-remove-odd-characters
4143                       (funcall gnus-decode-encoded-word-function
4144                                (setq x (nnheader-nov-field))))
4145                    (error x))
4146                  (nnheader-nov-field)   ; date
4147                  (nnheader-nov-read-message-id number)  ; id
4148                  (setq references (nnheader-nov-field)) ; refs
4149                  (nnheader-nov-read-integer) ; chars
4150                  (nnheader-nov-read-integer) ; lines
4151                  (unless (eobp)
4152                    (if (looking-at "Xref: ")
4153                        (goto-char (match-end 0)))
4154                    (nnheader-nov-field)) ; Xref
4155                  (nnheader-nov-parse-extra)))) ; extra
4156
4157       (widen))
4158
4159     (when (and (string= references "")
4160                (setq in-reply-to (mail-header-extra header))
4161                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4162       (mail-header-set-references
4163        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4164
4165     (when gnus-alter-header-function
4166       (funcall gnus-alter-header-function header))
4167     (gnus-dependencies-add-header header dependencies force-new)))
4168
4169 (defun gnus-build-get-header (id)
4170   "Look through the buffer of NOV lines and find the header to ID.
4171 Enter this line into the dependencies hash table, and return
4172 the id of the parent article (if any)."
4173   (let ((deps gnus-newsgroup-dependencies)
4174         found header)
4175     (prog1
4176         (save-excursion
4177           (set-buffer nntp-server-buffer)
4178           (let ((case-fold-search nil))
4179             (goto-char (point-min))
4180             (while (and (not found)
4181                         (search-forward id nil t))
4182               (beginning-of-line)
4183               (setq found (looking-at
4184                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4185                                    (regexp-quote id))))
4186               (or found (beginning-of-line 2)))
4187             (when found
4188               (beginning-of-line)
4189               (and
4190                (setq header (gnus-nov-parse-line
4191                              (read (current-buffer)) deps))
4192                (gnus-parent-id (mail-header-references header))))))
4193       (when header
4194         (let ((number (mail-header-number header)))
4195           (push number gnus-newsgroup-limit)
4196           (push header gnus-newsgroup-headers)
4197           (if (memq number gnus-newsgroup-unselected)
4198               (progn
4199                 (setq gnus-newsgroup-unreads
4200                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4201                                                number))
4202                 (setq gnus-newsgroup-unselected
4203                       (delq number gnus-newsgroup-unselected)))
4204             (push number gnus-newsgroup-ancient)))))))
4205
4206 (defun gnus-build-all-threads ()
4207   "Read all the headers."
4208   (let ((gnus-summary-ignore-duplicates t)
4209         (mail-parse-charset gnus-newsgroup-charset)
4210         (dependencies gnus-newsgroup-dependencies)
4211         header article)
4212     (save-excursion
4213       (set-buffer nntp-server-buffer)
4214       (let ((case-fold-search nil))
4215         (goto-char (point-min))
4216         (while (not (eobp))
4217           (ignore-errors
4218             (setq article (read (current-buffer))
4219                   header (gnus-nov-parse-line article dependencies)))
4220           (when header
4221             (with-current-buffer gnus-summary-buffer
4222               (push header gnus-newsgroup-headers)
4223               (if (memq (setq article (mail-header-number header))
4224                         gnus-newsgroup-unselected)
4225                   (progn
4226                     (setq gnus-newsgroup-unreads
4227                           (gnus-add-to-sorted-list
4228                            gnus-newsgroup-unreads article))
4229                     (setq gnus-newsgroup-unselected
4230                           (delq article gnus-newsgroup-unselected)))
4231                 (push article gnus-newsgroup-ancient)))
4232             (forward-line 1)))))))
4233
4234 (defun gnus-summary-update-article-line (article header)
4235   "Update the line for ARTICLE using HEADER."
4236   (let* ((id (mail-header-id header))
4237          (thread (gnus-id-to-thread id)))
4238     (unless thread
4239       (error "Article in no thread"))
4240     ;; Update the thread.
4241     (setcar thread header)
4242     (gnus-summary-goto-subject article)
4243     (let* ((datal (gnus-data-find-list article))
4244            (data (car datal))
4245            (buffer-read-only nil)
4246            (level (gnus-summary-thread-level)))
4247       (gnus-delete-line)
4248       (let ((inserted (- (point)
4249                          (progn
4250                            (gnus-summary-insert-line
4251                             header level nil
4252                             (memq article gnus-newsgroup-undownloaded)
4253                             (gnus-article-mark article)
4254                             (memq article gnus-newsgroup-replied)
4255                             (memq article gnus-newsgroup-expirable)
4256                             ;; Only insert the Subject string when it's different
4257                             ;; from the previous Subject string.
4258                             (if (and
4259                                  gnus-show-threads
4260                                  (gnus-subject-equal
4261                                   (condition-case ()
4262                                       (mail-header-subject
4263                                        (gnus-data-header
4264                                         (cadr
4265                                          (gnus-data-find-list
4266                                           article
4267                                           (gnus-data-list t)))))
4268                                     ;; Error on the side of excessive subjects.
4269                                     (error ""))
4270                                   (mail-header-subject header)))
4271                                 ""
4272                               (mail-header-subject header))
4273                             nil (cdr (assq article gnus-newsgroup-scored))
4274                             (memq article gnus-newsgroup-processable))
4275                            (point)))))
4276         (when (cdr datal)
4277           (gnus-data-update-list
4278            (cdr datal)
4279            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4280
4281 (defun gnus-summary-update-article (article &optional iheader)
4282   "Update ARTICLE in the summary buffer."
4283   (set-buffer gnus-summary-buffer)
4284   (let* ((header (gnus-summary-article-header article))
4285          (id (mail-header-id header))
4286          (data (gnus-data-find article))
4287          (thread (gnus-id-to-thread id))
4288          (references (mail-header-references header))
4289          (parent
4290           (gnus-id-to-thread
4291            (or (gnus-parent-id
4292                 (when (and references
4293                            (not (equal "" references)))
4294                   references))
4295                "none")))
4296          (buffer-read-only nil)
4297          (old (car thread)))
4298     (when thread
4299       (unless iheader
4300         (setcar thread nil)
4301         (when parent
4302           (delq thread parent)))
4303       (if (gnus-summary-insert-subject id header)
4304           ;; Set the (possibly) new article number in the data structure.
4305           (gnus-data-set-number data (gnus-id-to-article id))
4306         (setcar thread old)
4307         nil))))
4308
4309 (defun gnus-rebuild-thread (id &optional line)
4310   "Rebuild the thread containing ID.
4311 If LINE, insert the rebuilt thread starting on line LINE."
4312   (let ((buffer-read-only nil)
4313         old-pos current thread data)
4314     (if (not gnus-show-threads)
4315         (setq thread (list (car (gnus-id-to-thread id))))
4316       ;; Get the thread this article is part of.
4317       (setq thread (gnus-remove-thread id)))
4318     (setq old-pos (point-at-bol))
4319     (setq current (save-excursion
4320                     (and (re-search-backward "[\r\n]" nil t)
4321                          (gnus-summary-article-number))))
4322     ;; If this is a gathered thread, we have to go some re-gathering.
4323     (when (stringp (car thread))
4324       (let ((subject (car thread))
4325             roots thr)
4326         (setq thread (cdr thread))
4327         (while thread
4328           (unless (memq (setq thr (gnus-id-to-thread
4329                                    (gnus-root-id
4330                                     (mail-header-id (caar thread)))))
4331                         roots)
4332             (push thr roots))
4333           (setq thread (cdr thread)))
4334         ;; We now have all (unique) roots.
4335         (if (= (length roots) 1)
4336             ;; All the loose roots are now one solid root.
4337             (setq thread (car roots))
4338           (setq thread (cons subject (gnus-sort-threads roots))))))
4339     (let (threads)
4340       ;; We then insert this thread into the summary buffer.
4341       (when line
4342         (goto-char (point-min))
4343         (forward-line (1- line)))
4344       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4345         (if gnus-show-threads
4346             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4347           (gnus-summary-prepare-unthreaded thread))
4348         (setq data (nreverse gnus-newsgroup-data))
4349         (setq threads gnus-newsgroup-threads))
4350       ;; We splice the new data into the data structure.
4351       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4352       ;;!!! then we want to insert at the beginning of the buffer.
4353       ;;!!! That happens to be true with Gnus now, but that may
4354       ;;!!! change in the future.  Perhaps.
4355       (gnus-data-enter-list
4356        (if line nil current) data (- (point) old-pos))
4357       (setq gnus-newsgroup-threads
4358             (nconc threads gnus-newsgroup-threads))
4359       (gnus-data-compute-positions))))
4360
4361 (defun gnus-number-to-header (number)
4362   "Return the header for article NUMBER."
4363   (let ((headers gnus-newsgroup-headers))
4364     (while (and headers
4365                 (not (= number (mail-header-number (car headers)))))
4366       (pop headers))
4367     (when headers
4368       (car headers))))
4369
4370 (defun gnus-parent-headers (in-headers &optional generation)
4371   "Return the headers of the GENERATIONeth parent of HEADERS."
4372   (unless generation
4373     (setq generation 1))
4374   (let ((parent t)
4375         (headers in-headers)
4376         references)
4377     (while (and parent
4378                 (not (zerop generation))
4379                 (setq references (mail-header-references headers)))
4380       (setq headers (if (and references
4381                              (setq parent (gnus-parent-id references)))
4382                         (car (gnus-id-to-thread parent))
4383                       nil))
4384       (decf generation))
4385     (and (not (eq headers in-headers))
4386          headers)))
4387
4388 (defun gnus-id-to-thread (id)
4389   "Return the (sub-)thread where ID appears."
4390   (gnus-gethash id gnus-newsgroup-dependencies))
4391
4392 (defun gnus-id-to-article (id)
4393   "Return the article number of ID."
4394   (let ((thread (gnus-id-to-thread id)))
4395     (when (and thread
4396                (car thread))
4397       (mail-header-number (car thread)))))
4398
4399 (defun gnus-id-to-header (id)
4400   "Return the article headers of ID."
4401   (car (gnus-id-to-thread id)))
4402
4403 (defun gnus-article-displayed-root-p (article)
4404   "Say whether ARTICLE is a root(ish) article."
4405   (let ((level (gnus-summary-thread-level article))
4406         (refs (mail-header-references  (gnus-summary-article-header article)))
4407         particle)
4408     (cond
4409      ((null level) nil)
4410      ((zerop level) t)
4411      ((null refs) t)
4412      ((null (gnus-parent-id refs)) t)
4413      ((and (= 1 level)
4414            (null (setq particle (gnus-id-to-article
4415                                  (gnus-parent-id refs))))
4416            (null (gnus-summary-thread-level particle)))))))
4417
4418 (defun gnus-root-id (id)
4419   "Return the id of the root of the thread where ID appears."
4420   (let (last-id prev)
4421     (while (and id (setq prev (car (gnus-id-to-thread id))))
4422       (setq last-id id
4423             id (gnus-parent-id (mail-header-references prev))))
4424     last-id))
4425
4426 (defun gnus-articles-in-thread (thread)
4427   "Return the list of articles in THREAD."
4428   (cons (mail-header-number (car thread))
4429         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4430
4431 (defun gnus-remove-thread (id &optional dont-remove)
4432   "Remove the thread that has ID in it."
4433   (let (headers thread last-id)
4434     ;; First go up in this thread until we find the root.
4435     (setq last-id (gnus-root-id id)
4436           headers (message-flatten-list (gnus-id-to-thread last-id)))
4437     ;; We have now found the real root of this thread.  It might have
4438     ;; been gathered into some loose thread, so we have to search
4439     ;; through the threads to find the thread we wanted.
4440     (let ((threads gnus-newsgroup-threads)
4441           sub)
4442       (while threads
4443         (setq sub (car threads))
4444         (if (stringp (car sub))
4445             ;; This is a gathered thread, so we look at the roots
4446             ;; below it to find whether this article is in this
4447             ;; gathered root.
4448             (progn
4449               (setq sub (cdr sub))
4450               (while sub
4451                 (when (member (caar sub) headers)
4452                   (setq thread (car threads)
4453                         threads nil
4454                         sub nil))
4455                 (setq sub (cdr sub))))
4456           ;; It's an ordinary thread, so we check it.
4457           (when (eq (car sub) (car headers))
4458             (setq thread sub
4459                   threads nil)))
4460         (setq threads (cdr threads)))
4461       ;; If this article is in no thread, then it's a root.
4462       (if thread
4463           (unless dont-remove
4464             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4465         (setq thread (gnus-id-to-thread last-id)))
4466       (when thread
4467         (prog1
4468             thread                      ; We return this thread.
4469           (unless dont-remove
4470             (if (stringp (car thread))
4471                 (progn
4472                   ;; If we use dummy roots, then we have to remove the
4473                   ;; dummy root as well.
4474                   (when (eq gnus-summary-make-false-root 'dummy)
4475                     ;; We go to the dummy root by going to
4476                     ;; the first sub-"thread", and then one line up.
4477                     (gnus-summary-goto-article
4478                      (mail-header-number (caadr thread)))
4479                     (forward-line -1)
4480                     (gnus-delete-line)
4481                     (gnus-data-compute-positions))
4482                   (setq thread (cdr thread))
4483                   (while thread
4484                     (gnus-remove-thread-1 (car thread))
4485                     (setq thread (cdr thread))))
4486               (gnus-remove-thread-1 thread))))))))
4487
4488 (defun gnus-remove-thread-1 (thread)
4489   "Remove the thread THREAD recursively."
4490   (let ((number (mail-header-number (pop thread)))
4491         d)
4492     (setq thread (reverse thread))
4493     (while thread
4494       (gnus-remove-thread-1 (pop thread)))
4495     (when (setq d (gnus-data-find number))
4496       (goto-char (gnus-data-pos d))
4497       (gnus-summary-show-thread)
4498       (gnus-data-remove
4499        number
4500        (- (point-at-bol)
4501           (prog1
4502               (1+ (point-at-eol))
4503             (gnus-delete-line)))))))
4504
4505 (defun gnus-sort-threads-1 (threads func)
4506   (sort (mapcar (lambda (thread)
4507                   (cons (car thread)
4508                         (and (cdr thread)
4509                              (gnus-sort-threads-1 (cdr thread) func))))
4510                 threads) func))
4511
4512 (defun gnus-sort-threads (threads)
4513   "Sort THREADS."
4514   (if (not gnus-thread-sort-functions)
4515       threads
4516     (gnus-message 8 "Sorting threads...")
4517     (let ((max-lisp-eval-depth 5000))
4518       (prog1 (gnus-sort-threads-1
4519          threads
4520          (gnus-make-sort-function gnus-thread-sort-functions))
4521         (gnus-message 8 "Sorting threads...done")))))
4522
4523 (defun gnus-sort-articles (articles)
4524   "Sort ARTICLES."
4525   (when gnus-article-sort-functions
4526     (gnus-message 7 "Sorting articles...")
4527     (prog1
4528         (setq gnus-newsgroup-headers
4529               (sort articles (gnus-make-sort-function
4530                               gnus-article-sort-functions)))
4531       (gnus-message 7 "Sorting articles...done"))))
4532
4533 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4534 (defmacro gnus-thread-header (thread)
4535   "Return header of first article in THREAD.
4536 Note that THREAD must never, ever be anything else than a variable -
4537 using some other form will lead to serious barfage."
4538   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4539   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4540   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4541         (vector thread) 2))
4542
4543 (defsubst gnus-article-sort-by-number (h1 h2)
4544   "Sort articles by article number."
4545   (< (mail-header-number h1)
4546      (mail-header-number h2)))
4547
4548 (defun gnus-thread-sort-by-number (h1 h2)
4549   "Sort threads by root article number."
4550   (gnus-article-sort-by-number
4551    (gnus-thread-header h1) (gnus-thread-header h2)))
4552
4553 (defsubst gnus-article-sort-by-random (h1 h2)
4554   "Sort articles by article number."
4555   (zerop (random 2)))
4556
4557 (defun gnus-thread-sort-by-random (h1 h2)
4558   "Sort threads by root article number."
4559   (gnus-article-sort-by-random
4560    (gnus-thread-header h1) (gnus-thread-header h2)))
4561
4562 (defsubst gnus-article-sort-by-lines (h1 h2)
4563   "Sort articles by article Lines header."
4564   (< (mail-header-lines h1)
4565      (mail-header-lines h2)))
4566
4567 (defun gnus-thread-sort-by-lines (h1 h2)
4568   "Sort threads by root article Lines header."
4569   (gnus-article-sort-by-lines
4570    (gnus-thread-header h1) (gnus-thread-header h2)))
4571
4572 (defsubst gnus-article-sort-by-chars (h1 h2)
4573   "Sort articles by octet length."
4574   (< (mail-header-chars h1)
4575      (mail-header-chars h2)))
4576
4577 (defun gnus-thread-sort-by-chars (h1 h2)
4578   "Sort threads by root article octet length."
4579   (gnus-article-sort-by-chars
4580    (gnus-thread-header h1) (gnus-thread-header h2)))
4581
4582 (defsubst gnus-article-sort-by-author (h1 h2)
4583   "Sort articles by root author."
4584   (string-lessp
4585    (let ((extract (funcall
4586                    gnus-extract-address-components
4587                    (mail-header-from h1))))
4588      (or (car extract) (cadr extract) ""))
4589    (let ((extract (funcall
4590                    gnus-extract-address-components
4591                    (mail-header-from h2))))
4592      (or (car extract) (cadr extract) ""))))
4593
4594 (defun gnus-thread-sort-by-author (h1 h2)
4595   "Sort threads by root author."
4596   (gnus-article-sort-by-author
4597    (gnus-thread-header h1)  (gnus-thread-header h2)))
4598
4599 (defsubst gnus-article-sort-by-recipient (h1 h2)
4600   "Sort articles by recipient."
4601   (string-lessp
4602    (let ((extract (funcall
4603                    gnus-extract-address-components
4604                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4605      (or (car extract) (cadr extract)))
4606    (let ((extract (funcall
4607                    gnus-extract-address-components
4608                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4609      (or (car extract) (cadr extract)))))
4610
4611 (defun gnus-thread-sort-by-recipient (h1 h2)
4612   "Sort threads by root recipient."
4613   (gnus-article-sort-by-recipient
4614    (gnus-thread-header h1) (gnus-thread-header h2)))
4615
4616 (defsubst gnus-article-sort-by-subject (h1 h2)
4617   "Sort articles by root subject."
4618   (string-lessp
4619    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4620    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4621
4622 (defun gnus-thread-sort-by-subject (h1 h2)
4623   "Sort threads by root subject."
4624   (gnus-article-sort-by-subject
4625    (gnus-thread-header h1) (gnus-thread-header h2)))
4626
4627 (defsubst gnus-article-sort-by-date (h1 h2)
4628   "Sort articles by root article date."
4629   (time-less-p
4630    (gnus-date-get-time (mail-header-date h1))
4631    (gnus-date-get-time (mail-header-date h2))))
4632
4633 (defun gnus-thread-sort-by-date (h1 h2)
4634   "Sort threads by root article date."
4635   (gnus-article-sort-by-date
4636    (gnus-thread-header h1) (gnus-thread-header h2)))
4637
4638 (defsubst gnus-article-sort-by-score (h1 h2)
4639   "Sort articles by root article score.
4640 Unscored articles will be counted as having a score of zero."
4641   (> (or (cdr (assq (mail-header-number h1)
4642                     gnus-newsgroup-scored))
4643          gnus-summary-default-score 0)
4644      (or (cdr (assq (mail-header-number h2)
4645                     gnus-newsgroup-scored))
4646          gnus-summary-default-score 0)))
4647
4648 (defun gnus-thread-sort-by-score (h1 h2)
4649   "Sort threads by root article score."
4650   (gnus-article-sort-by-score
4651    (gnus-thread-header h1) (gnus-thread-header h2)))
4652
4653 (defun gnus-thread-sort-by-total-score (h1 h2)
4654   "Sort threads by the sum of all scores in the thread.
4655 Unscored articles will be counted as having a score of zero."
4656   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4657
4658 (defun gnus-thread-total-score (thread)
4659   ;; This function find the total score of THREAD.
4660   (cond
4661    ((null thread)
4662     0)
4663    ((consp thread)
4664     (if (stringp (car thread))
4665         (apply gnus-thread-score-function 0
4666                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4667       (gnus-thread-total-score-1 thread)))
4668    (t
4669     (gnus-thread-total-score-1 (list thread)))))
4670
4671 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4672   "Sort threads such that the thread with the most recently arrived article comes first."
4673   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4674
4675 (defun gnus-thread-highest-number (thread)
4676   "Return the highest article number in THREAD."
4677   (apply 'max (mapcar (lambda (header)
4678                         (mail-header-number header))
4679                       (message-flatten-list thread))))
4680
4681 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4682   "Sort threads such that the thread with the most recently dated article comes first."
4683   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4684
4685 (defun gnus-thread-latest-date (thread)
4686   "Return the highest article date in THREAD."
4687   (let ((previous-time 0))
4688     (apply 'max
4689            (mapcar
4690             (lambda (header)
4691               (setq previous-time
4692                     (condition-case ()
4693                         (time-to-seconds (mail-header-parse-date
4694                                           (mail-header-date header)))
4695                       (error previous-time))))
4696             (sort
4697              (message-flatten-list thread)
4698              (lambda (h1 h2)
4699                (< (mail-header-number h1)
4700                   (mail-header-number h2))))))))
4701
4702 (defun gnus-thread-total-score-1 (root)
4703   ;; This function find the total score of the thread below ROOT.
4704   (setq root (car root))
4705   (apply gnus-thread-score-function
4706          (or (append
4707               (mapcar 'gnus-thread-total-score
4708                       (cdr (gnus-id-to-thread (mail-header-id root))))
4709               (when (> (mail-header-number root) 0)
4710                 (list (or (cdr (assq (mail-header-number root)
4711                                      gnus-newsgroup-scored))
4712                           gnus-summary-default-score 0))))
4713              (list gnus-summary-default-score)
4714              '(0))))
4715
4716 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4717 (defvar gnus-tmp-prev-subject nil)
4718 (defvar gnus-tmp-false-parent nil)
4719 (defvar gnus-tmp-root-expunged nil)
4720 (defvar gnus-tmp-dummy-line nil)
4721
4722 (eval-when-compile (defvar gnus-tmp-header))
4723 (defun gnus-extra-header (type &optional header)
4724   "Return the extra header of TYPE."
4725   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4726       ""))
4727
4728 (defvar gnus-tmp-thread-tree-header-string "")
4729
4730 (defcustom gnus-sum-thread-tree-root "> "
4731   "With %B spec, used for the root of a thread.
4732 If nil, use subject instead."
4733   :version "22.1"
4734   :type '(radio (const :format "%v  " nil) string)
4735   :group 'gnus-thread)
4736
4737 (defcustom gnus-sum-thread-tree-false-root "> "
4738   "With %B spec, used for a false root of a thread.
4739 If nil, use subject instead."
4740   :version "22.1"
4741   :type '(radio (const :format "%v  " nil) string)
4742   :group 'gnus-thread)
4743
4744 (defcustom gnus-sum-thread-tree-single-indent ""
4745   "With %B spec, used for a thread with just one message.
4746 If nil, use subject instead."
4747   :version "22.1"
4748   :type '(radio (const :format "%v  " nil) string)
4749   :group 'gnus-thread)
4750
4751 (defcustom gnus-sum-thread-tree-vertical "| "
4752   "With %B spec, used for drawing a vertical line."
4753   :version "22.1"
4754   :type 'string
4755   :group 'gnus-thread)
4756
4757 (defcustom gnus-sum-thread-tree-indent "  "
4758   "With %B spec, used for indenting."
4759   :version "22.1"
4760   :type 'string
4761   :group 'gnus-thread)
4762
4763 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4764   "With %B spec, used for a leaf with brothers."
4765   :version "22.1"
4766   :type 'string
4767   :group 'gnus-thread)
4768
4769 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4770   "With %B spec, used for a leaf without brothers."
4771   :version "22.1"
4772   :type 'string
4773   :group 'gnus-thread)
4774
4775 (defun gnus-summary-prepare-threads (threads)
4776   "Prepare summary buffer from THREADS and indentation LEVEL.
4777 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4778 or a straight list of headers."
4779   (gnus-message 7 "Generating summary...")
4780
4781   (setq gnus-newsgroup-threads threads)
4782   (beginning-of-line)
4783
4784   (let ((gnus-tmp-level 0)
4785         (default-score (or gnus-summary-default-score 0))
4786         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4787         (building-line-count gnus-summary-display-while-building)
4788         (building-count (integerp gnus-summary-display-while-building))
4789         thread number subject stack state gnus-tmp-gathered beg-match
4790         new-roots gnus-tmp-new-adopts thread-end simp-subject
4791         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4792         gnus-tmp-replied gnus-tmp-subject-or-nil
4793         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4794         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4795         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4796         tree-stack)
4797
4798     (setq gnus-tmp-prev-subject nil
4799           gnus-tmp-thread-tree-header-string "")
4800
4801     (if (vectorp (car threads))
4802         ;; If this is a straight (sic) list of headers, then a
4803         ;; threaded summary display isn't required, so we just create
4804         ;; an unthreaded one.
4805         (gnus-summary-prepare-unthreaded threads)
4806
4807       ;; Do the threaded display.
4808
4809       (if gnus-summary-display-while-building
4810           (switch-to-buffer (buffer-name)))
4811       (while (or threads stack gnus-tmp-new-adopts new-roots)
4812
4813         (if (and (= gnus-tmp-level 0)
4814                  (or (not stack)
4815                      (= (caar stack) 0))
4816                  (not gnus-tmp-false-parent)
4817                  (or gnus-tmp-new-adopts new-roots))
4818             (if gnus-tmp-new-adopts
4819                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4820                       thread (list (car gnus-tmp-new-adopts))
4821                       gnus-tmp-header (caar thread)
4822                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4823               (when new-roots
4824                 (setq thread (list (car new-roots))
4825                       gnus-tmp-header (caar thread)
4826                       new-roots (cdr new-roots))))
4827
4828           (if threads
4829               ;; If there are some threads, we do them before the
4830               ;; threads on the stack.
4831               (setq thread threads
4832                     gnus-tmp-header (caar thread))
4833             ;; There were no current threads, so we pop something off
4834             ;; the stack.
4835             (setq state (car stack)
4836                   gnus-tmp-level (car state)
4837                   tree-stack (cadr state)
4838                   thread (caddr state)
4839                   stack (cdr stack)
4840                   gnus-tmp-header (caar thread))))
4841
4842         (setq gnus-tmp-false-parent nil)
4843         (setq gnus-tmp-root-expunged nil)
4844         (setq thread-end nil)
4845
4846         (if (stringp gnus-tmp-header)
4847             ;; The header is a dummy root.
4848             (cond
4849              ((eq gnus-summary-make-false-root 'adopt)
4850               ;; We let the first article adopt the rest.
4851               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4852                                                (cddar thread)))
4853               (setq gnus-tmp-gathered
4854                     (nconc (mapcar
4855                             (lambda (h) (mail-header-number (car h)))
4856                             (cddar thread))
4857                            gnus-tmp-gathered))
4858               (setq thread (cons (list (caar thread)
4859                                        (cadar thread))
4860                                  (cdr thread)))
4861               (setq gnus-tmp-level -1
4862                     gnus-tmp-false-parent t))
4863              ((eq gnus-summary-make-false-root 'empty)
4864               ;; We print adopted articles with empty subject fields.
4865               (setq gnus-tmp-gathered
4866                     (nconc (mapcar
4867                             (lambda (h) (mail-header-number (car h)))
4868                             (cddar thread))
4869                            gnus-tmp-gathered))
4870               (setq gnus-tmp-level -1))
4871              ((eq gnus-summary-make-false-root 'dummy)
4872               ;; We remember that we probably want to output a dummy
4873               ;; root.
4874               (setq gnus-tmp-dummy-line gnus-tmp-header)
4875               (setq gnus-tmp-prev-subject gnus-tmp-header))
4876              (t
4877               ;; We do not make a root for the gathered
4878               ;; sub-threads at all.
4879               (setq gnus-tmp-level -1)))
4880
4881           (setq number (mail-header-number gnus-tmp-header)
4882                 subject (mail-header-subject gnus-tmp-header)
4883                 simp-subject (gnus-simplify-subject-fully subject))
4884
4885           (cond
4886            ;; If the thread has changed subject, we might want to make
4887            ;; this subthread into a root.
4888            ((and (null gnus-thread-ignore-subject)
4889                  (not (zerop gnus-tmp-level))
4890                  gnus-tmp-prev-subject
4891                  (not (string= gnus-tmp-prev-subject simp-subject)))
4892             (setq new-roots (nconc new-roots (list (car thread)))
4893                   thread-end t
4894                   gnus-tmp-header nil))
4895            ;; If the article lies outside the current limit,
4896            ;; then we do not display it.
4897            ((not (memq number gnus-newsgroup-limit))
4898             (setq gnus-tmp-gathered
4899                   (nconc (mapcar
4900                           (lambda (h) (mail-header-number (car h)))
4901                           (cdar thread))
4902                          gnus-tmp-gathered))
4903             (setq gnus-tmp-new-adopts (if (cdar thread)
4904                                           (append gnus-tmp-new-adopts
4905                                                   (cdar thread))
4906                                         gnus-tmp-new-adopts)
4907                   thread-end t
4908                   gnus-tmp-header nil)
4909             (when (zerop gnus-tmp-level)
4910               (setq gnus-tmp-root-expunged t)))
4911            ;; Perhaps this article is to be marked as read?
4912            ((and gnus-summary-mark-below
4913                  (< (or (cdr (assq number gnus-newsgroup-scored))
4914                         default-score)
4915                     gnus-summary-mark-below)
4916                  ;; Don't touch sparse articles.
4917                  (not (gnus-summary-article-sparse-p number))
4918                  (not (gnus-summary-article-ancient-p number)))
4919             (setq gnus-newsgroup-unreads
4920                   (delq number gnus-newsgroup-unreads))
4921             (if gnus-newsgroup-auto-expire
4922                 (setq gnus-newsgroup-expirable
4923                       (gnus-add-to-sorted-list
4924                        gnus-newsgroup-expirable number))
4925               (push (cons number gnus-low-score-mark)
4926                     gnus-newsgroup-reads))))
4927
4928           (when gnus-tmp-header
4929             ;; We may have an old dummy line to output before this
4930             ;; article.
4931             (when (and gnus-tmp-dummy-line
4932                        (gnus-subject-equal
4933                         gnus-tmp-dummy-line
4934                         (mail-header-subject gnus-tmp-header)))
4935               (gnus-summary-insert-dummy-line
4936                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4937               (setq gnus-tmp-dummy-line nil))
4938
4939             ;; Compute the mark.
4940             (setq gnus-tmp-unread (gnus-article-mark number))
4941
4942             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4943                                   gnus-tmp-header gnus-tmp-level)
4944                   gnus-newsgroup-data)
4945
4946             ;; Actually insert the line.
4947             (setq
4948              gnus-tmp-subject-or-nil
4949              (cond
4950               ((and gnus-thread-ignore-subject
4951                     gnus-tmp-prev-subject
4952                     (not (string= gnus-tmp-prev-subject simp-subject)))
4953                subject)
4954               ((zerop gnus-tmp-level)
4955                (if (and (eq gnus-summary-make-false-root 'empty)
4956                         (memq number gnus-tmp-gathered)
4957                         gnus-tmp-prev-subject
4958                         (string= gnus-tmp-prev-subject simp-subject))
4959                    gnus-summary-same-subject
4960                  subject))
4961               (t gnus-summary-same-subject)))
4962             (if (and (eq gnus-summary-make-false-root 'adopt)
4963                      (= gnus-tmp-level 1)
4964                      (memq number gnus-tmp-gathered))
4965                 (setq gnus-tmp-opening-bracket ?\<
4966                       gnus-tmp-closing-bracket ?\>)
4967               (setq gnus-tmp-opening-bracket ?\[
4968                     gnus-tmp-closing-bracket ?\]))
4969             (setq
4970              gnus-tmp-indentation
4971              (aref gnus-thread-indent-array gnus-tmp-level)
4972              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4973              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4974                                 gnus-summary-default-score 0)
4975              gnus-tmp-score-char
4976              (if (or (null gnus-summary-default-score)
4977                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4978                          gnus-summary-zcore-fuzz))
4979                  ?                      ;Whitespace
4980                (if (< gnus-tmp-score gnus-summary-default-score)
4981                    gnus-score-below-mark gnus-score-over-mark))
4982              gnus-tmp-replied
4983              (cond ((memq number gnus-newsgroup-processable)
4984                     gnus-process-mark)
4985                    ((memq number gnus-newsgroup-cached)
4986                     gnus-cached-mark)
4987                    ((memq number gnus-newsgroup-replied)
4988                     gnus-replied-mark)
4989                    ((memq number gnus-newsgroup-forwarded)
4990                     gnus-forwarded-mark)
4991                    ((memq number gnus-newsgroup-saved)
4992                     gnus-saved-mark)
4993                    ((memq number gnus-newsgroup-recent)
4994                     gnus-recent-mark)
4995                    ((memq number gnus-newsgroup-unseen)
4996                     gnus-unseen-mark)
4997                    (t gnus-no-mark))
4998              gnus-tmp-downloaded
4999              (cond ((memq number gnus-newsgroup-undownloaded)
5000                     gnus-undownloaded-mark)
5001                    (gnus-newsgroup-agentized
5002                     gnus-downloaded-mark)
5003                    (t
5004                     gnus-no-mark))
5005              gnus-tmp-from (mail-header-from gnus-tmp-header)
5006              gnus-tmp-name
5007              (cond
5008               ((string-match "<[^>]+> *$" gnus-tmp-from)
5009                (setq beg-match (match-beginning 0))
5010                (or (and (string-match "^\".+\"" gnus-tmp-from)
5011                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5012                    (substring gnus-tmp-from 0 beg-match)))
5013               ((string-match "(.+)" gnus-tmp-from)
5014                (substring gnus-tmp-from
5015                           (1+ (match-beginning 0)) (1- (match-end 0))))
5016               (t gnus-tmp-from))
5017
5018              ;; Do the %B string
5019              gnus-tmp-thread-tree-header-string
5020              (cond
5021               ((not gnus-show-threads) "")
5022               ((zerop gnus-tmp-level)
5023                (cond ((cdar thread)
5024                       (or gnus-sum-thread-tree-root subject))
5025                      (gnus-tmp-new-adopts
5026                       (or gnus-sum-thread-tree-false-root subject))
5027                      (t
5028                       (or gnus-sum-thread-tree-single-indent subject))))
5029               (t
5030                (concat (apply 'concat
5031                               (mapcar (lambda (item)
5032                                         (if (= item 1)
5033                                             gnus-sum-thread-tree-vertical
5034                                           gnus-sum-thread-tree-indent))
5035                                       (cdr (reverse tree-stack))))
5036                        (if (nth 1 thread)
5037                            gnus-sum-thread-tree-leaf-with-other
5038                          gnus-sum-thread-tree-single-leaf)))))
5039             (when (string= gnus-tmp-name "")
5040               (setq gnus-tmp-name gnus-tmp-from))
5041             (unless (numberp gnus-tmp-lines)
5042               (setq gnus-tmp-lines -1))
5043             (if (= gnus-tmp-lines -1)
5044                 (setq gnus-tmp-lines "?")
5045               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5046               (gnus-put-text-property
5047              (point)
5048              (progn (eval gnus-summary-line-format-spec) (point))
5049                'gnus-number number)
5050             (when gnus-visual-p
5051               (forward-line -1)
5052               (gnus-run-hooks 'gnus-summary-update-hook)
5053               (forward-line 1))
5054
5055             (setq gnus-tmp-prev-subject simp-subject)))
5056
5057         (when (nth 1 thread)
5058           (push (list (max 0 gnus-tmp-level)
5059                       (copy-sequence tree-stack)
5060                       (nthcdr 1 thread))
5061                 stack))
5062         (push (if (nth 1 thread) 1 0) tree-stack)
5063         (incf gnus-tmp-level)
5064         (setq threads (if thread-end nil (cdar thread)))
5065         (if gnus-summary-display-while-building
5066             (if building-count
5067                 (progn
5068                   ;; use a set frequency
5069                   (setq building-line-count (1- building-line-count))
5070                   (when (= building-line-count 0)
5071                     (sit-for 0)
5072                     (setq building-line-count
5073                           gnus-summary-display-while-building)))
5074               ;; always
5075               (sit-for 0)))
5076         (unless threads
5077           (setq gnus-tmp-level 0)))))
5078   (gnus-message 7 "Generating summary...done"))
5079
5080 (defun gnus-summary-prepare-unthreaded (headers)
5081   "Generate an unthreaded summary buffer based on HEADERS."
5082   (let (header number mark)
5083
5084     (beginning-of-line)
5085
5086     (while headers
5087       ;; We may have to root out some bad articles...
5088       (when (memq (setq number (mail-header-number
5089                                 (setq header (pop headers))))
5090                   gnus-newsgroup-limit)
5091         ;; Mark article as read when it has a low score.
5092         (when (and gnus-summary-mark-below
5093                    (< (or (cdr (assq number gnus-newsgroup-scored))
5094                           gnus-summary-default-score 0)
5095                       gnus-summary-mark-below)
5096                    (not (gnus-summary-article-ancient-p number)))
5097           (setq gnus-newsgroup-unreads
5098                 (delq number gnus-newsgroup-unreads))
5099           (if gnus-newsgroup-auto-expire
5100               (push number gnus-newsgroup-expirable)
5101             (push (cons number gnus-low-score-mark)
5102                   gnus-newsgroup-reads)))
5103
5104         (setq mark (gnus-article-mark number))
5105         (push (gnus-data-make number mark (1+ (point)) header 0)
5106               gnus-newsgroup-data)
5107         (gnus-summary-insert-line
5108          header 0 number
5109          (memq number gnus-newsgroup-undownloaded)
5110          mark (memq number gnus-newsgroup-replied)
5111          (memq number gnus-newsgroup-expirable)
5112          (mail-header-subject header) nil
5113          (cdr (assq number gnus-newsgroup-scored))
5114          (memq number gnus-newsgroup-processable))))))
5115
5116 (defun gnus-summary-remove-list-identifiers ()
5117   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5118   (let ((regexp (if (consp gnus-list-identifiers)
5119                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5120                   gnus-list-identifiers))
5121         changed subject)
5122     (when regexp
5123       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5124       (dolist (header gnus-newsgroup-headers)
5125         (setq subject (mail-header-subject header)
5126               changed nil)
5127         (while (string-match regexp subject)
5128           (setq subject
5129                 (concat (substring subject 0 (match-beginning 1))
5130                         (substring subject (match-end 0)))
5131                 changed t))
5132         (when changed
5133           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5134             (setq subject
5135                   (concat (substring subject 0 (match-beginning 1))
5136                           (substring subject (match-end 1)))))
5137           (mail-header-set-subject header subject))))))
5138
5139 (defun gnus-fetch-headers (articles)
5140   "Fetch headers of ARTICLES."
5141   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5142     (gnus-message 5 "Fetching headers for %s..." name)
5143     (prog1
5144         (if (eq 'nov
5145                 (setq gnus-headers-retrieved-by
5146                       (gnus-retrieve-headers
5147                        articles gnus-newsgroup-name
5148                        ;; We might want to fetch old headers, but
5149                        ;; not if there is only 1 article.
5150                        (and (or (and
5151                                  (not (eq gnus-fetch-old-headers 'some))
5152                                  (not (numberp gnus-fetch-old-headers)))
5153                                 (> (length articles) 1))
5154                             gnus-fetch-old-headers))))
5155             (gnus-get-newsgroup-headers-xover
5156              articles nil nil gnus-newsgroup-name t)
5157           (gnus-get-newsgroup-headers))
5158       (gnus-message 5 "Fetching headers for %s...done" name))))
5159
5160 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5161   "Select newsgroup GROUP.
5162 If READ-ALL is non-nil, all articles in the group are selected.
5163 If SELECT-ARTICLES, only select those articles from GROUP."
5164   (let* ((entry (gnus-group-entry group))
5165          ;;!!! Dirty hack; should be removed.
5166          (gnus-summary-ignore-duplicates
5167           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5168               t
5169             gnus-summary-ignore-duplicates))
5170          (info (nth 2 entry))
5171          articles fetched-articles cached)
5172
5173     (unless (gnus-check-server
5174              (set (make-local-variable 'gnus-current-select-method)
5175                   (gnus-find-method-for-group group)))
5176       (error "Couldn't open server"))
5177
5178     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5179         (gnus-activate-group group)     ; Or we can activate it...
5180         (progn                          ; Or we bug out.
5181           (when (equal major-mode 'gnus-summary-mode)
5182             (gnus-kill-buffer (current-buffer)))
5183           (error "Couldn't activate group %s: %s"
5184                  group (gnus-status-message group))))
5185
5186     (unless (gnus-request-group group t)
5187       (when (equal major-mode 'gnus-summary-mode)
5188         (gnus-kill-buffer (current-buffer)))
5189       (error "Couldn't request group %s: %s"
5190              group (gnus-status-message group)))
5191
5192     (when gnus-agent
5193       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5194
5195       (setq gnus-summary-use-undownloaded-faces
5196             (gnus-agent-find-parameter
5197              group
5198              'agent-enable-undownloaded-faces)))
5199
5200     (setq gnus-newsgroup-name group
5201           gnus-newsgroup-unselected nil
5202           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5203
5204     (let ((display (gnus-group-find-parameter group 'display)))
5205       (setq gnus-newsgroup-display
5206             (cond
5207              ((not (zerop (or (car-safe read-all) 0)))
5208               ;; The user entered the group with C-u SPC/RET, let's show
5209               ;; all articles.
5210               'gnus-not-ignore)
5211              ((eq display 'all)
5212               'gnus-not-ignore)
5213              ((arrayp display)
5214               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5215              ((numberp display)
5216               ;; The following is probably the "correct" solution, but
5217               ;; it makes Gnus fetch all headers and then limit the
5218               ;; articles (which is slow), so instead we hack the
5219               ;; select-articles parameter instead. -- Simon Josefsson
5220               ;; <jas@kth.se>
5221               ;;
5222               ;; (gnus-byte-compile
5223               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5224               ;;                         display)))))
5225               (setq select-articles
5226                     (gnus-uncompress-range
5227                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5228                              (if (> tmp 0)
5229                                  tmp
5230                                1))
5231                            (cdr (gnus-active group)))))
5232               nil)
5233              (t
5234               nil))))
5235
5236     (gnus-summary-setup-default-charset)
5237
5238     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5239     (when (gnus-virtual-group-p group)
5240       (setq cached gnus-newsgroup-cached))
5241
5242     (setq gnus-newsgroup-unreads
5243           (gnus-sorted-ndifference
5244            (gnus-sorted-ndifference gnus-newsgroup-unreads
5245                                     gnus-newsgroup-marked)
5246            gnus-newsgroup-dormant))
5247
5248     (setq gnus-newsgroup-processable nil)
5249
5250     (gnus-update-read-articles group gnus-newsgroup-unreads)
5251
5252     ;; Adjust and set lists of article marks.
5253     (when info
5254       (gnus-adjust-marked-articles info))
5255     (if (setq articles select-articles)
5256         (setq gnus-newsgroup-unselected
5257               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5258       (setq articles (gnus-articles-to-read group read-all)))
5259
5260     (cond
5261      ((null articles)
5262       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5263       'quit)
5264      ((eq articles 0) nil)
5265      (t
5266       ;; Init the dependencies hash table.
5267       (setq gnus-newsgroup-dependencies
5268             (gnus-make-hashtable (length articles)))
5269       (gnus-set-global-variables)
5270       ;; Retrieve the headers and read them in.
5271
5272       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5273
5274       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5275       (when cached
5276         (setq gnus-newsgroup-cached cached))
5277
5278       ;; Suppress duplicates?
5279       (when gnus-suppress-duplicates
5280         (gnus-dup-suppress-articles))
5281
5282       ;; Set the initial limit.
5283       (setq gnus-newsgroup-limit (copy-sequence articles))
5284       ;; Remove canceled articles from the list of unread articles.
5285       (setq fetched-articles
5286             (mapcar (lambda (headers) (mail-header-number headers))
5287                     gnus-newsgroup-headers))
5288       (setq gnus-newsgroup-articles fetched-articles)
5289       (setq gnus-newsgroup-unreads
5290             (gnus-sorted-nintersection
5291              gnus-newsgroup-unreads fetched-articles))
5292       (gnus-compute-unseen-list)
5293
5294       ;; Removed marked articles that do not exist.
5295       (gnus-update-missing-marks
5296        (gnus-sorted-difference articles fetched-articles))
5297       ;; We might want to build some more threads first.
5298       (when (and gnus-fetch-old-headers
5299                  (eq gnus-headers-retrieved-by 'nov))
5300         (if (eq gnus-fetch-old-headers 'invisible)
5301             (gnus-build-all-threads)
5302           (gnus-build-old-threads)))
5303       ;; Let the Gnus agent mark articles as read.
5304       (when gnus-agent
5305         (gnus-agent-get-undownloaded-list))
5306       ;; Remove list identifiers from subject
5307       (when gnus-list-identifiers
5308         (gnus-summary-remove-list-identifiers))
5309       ;; Check whether auto-expire is to be done in this group.
5310       (setq gnus-newsgroup-auto-expire
5311             (gnus-group-auto-expirable-p group))
5312       ;; Set up the article buffer now, if necessary.
5313       (unless gnus-single-article-buffer
5314         (gnus-article-setup-buffer))
5315       ;; First and last article in this newsgroup.
5316       (when gnus-newsgroup-headers
5317         (setq gnus-newsgroup-begin
5318               (mail-header-number (car gnus-newsgroup-headers))
5319               gnus-newsgroup-end
5320               (mail-header-number
5321                (gnus-last-element gnus-newsgroup-headers))))
5322       ;; GROUP is successfully selected.
5323       (or gnus-newsgroup-headers t)))))
5324
5325 (defun gnus-compute-unseen-list ()
5326   ;; The `seen' marks are treated specially.
5327   (if (not gnus-newsgroup-seen)
5328       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5329     (setq gnus-newsgroup-unseen
5330           (gnus-inverse-list-range-intersection
5331            gnus-newsgroup-articles gnus-newsgroup-seen))))
5332
5333 (defun gnus-summary-display-make-predicate (display)
5334   (require 'gnus-agent)
5335   (when (= (length display) 1)
5336     (setq display (car display)))
5337   (unless gnus-summary-display-cache
5338     (dolist (elem (append '((unread . unread)
5339                             (read . read)
5340                             (unseen . unseen))
5341                           gnus-article-mark-lists))
5342       (push (cons (cdr elem)
5343                   (gnus-byte-compile
5344                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5345             gnus-summary-display-cache)))
5346   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5347         (gnus-category-predicate-cache gnus-summary-display-cache))
5348     (gnus-get-predicate display)))
5349
5350 ;; Uses the dynamically bound `number' variable.
5351 (eval-when-compile
5352   (defvar number))
5353 (defun gnus-article-marked-p (type &optional article)
5354   (let ((article (or article number)))
5355     (cond
5356      ((eq type 'tick)
5357       (memq article gnus-newsgroup-marked))
5358      ((eq type 'spam)
5359       (memq article gnus-newsgroup-spam-marked))
5360      ((eq type 'unsend)
5361       (memq article gnus-newsgroup-unsendable))
5362      ((eq type 'undownload)
5363       (memq article gnus-newsgroup-undownloaded))
5364      ((eq type 'download)
5365       (memq article gnus-newsgroup-downloadable))
5366      ((eq type 'unread)
5367       (memq article gnus-newsgroup-unreads))
5368      ((eq type 'read)
5369       (memq article gnus-newsgroup-reads))
5370      ((eq type 'dormant)
5371       (memq article gnus-newsgroup-dormant) )
5372      ((eq type 'expire)
5373       (memq article gnus-newsgroup-expirable))
5374      ((eq type 'reply)
5375       (memq article gnus-newsgroup-replied))
5376      ((eq type 'killed)
5377       (memq article gnus-newsgroup-killed))
5378      ((eq type 'bookmark)
5379       (assq article gnus-newsgroup-bookmarks))
5380      ((eq type 'score)
5381       (assq article gnus-newsgroup-scored))
5382      ((eq type 'save)
5383       (memq article gnus-newsgroup-saved))
5384      ((eq type 'cache)
5385       (memq article gnus-newsgroup-cached))
5386      ((eq type 'forward)
5387       (memq article gnus-newsgroup-forwarded))
5388      ((eq type 'seen)
5389       (not (memq article gnus-newsgroup-unseen)))
5390      ((eq type 'recent)
5391       (memq article gnus-newsgroup-recent))
5392      (t t))))
5393
5394 (defun gnus-articles-to-read (group &optional read-all)
5395   "Find out what articles the user wants to read."
5396   (let* ((articles
5397           ;; Select all articles if `read-all' is non-nil, or if there
5398           ;; are no unread articles.
5399           (if (or read-all
5400                   (and (zerop (length gnus-newsgroup-marked))
5401                        (zerop (length gnus-newsgroup-unreads)))
5402                   ;; Fetch all if the predicate is non-nil.
5403                   gnus-newsgroup-display)
5404               ;; We want to select the headers for all the articles in
5405               ;; the group, so we select either all the active
5406               ;; articles in the group, or (if that's nil), the
5407               ;; articles in the cache.
5408               (or
5409                (gnus-uncompress-range (gnus-active group))
5410                (gnus-cache-articles-in-group group))
5411             ;; Select only the "normal" subset of articles.
5412             (gnus-sorted-nunion
5413              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5414              gnus-newsgroup-unreads)))
5415          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5416          (scored (length scored-list))
5417          (number (length articles))
5418          (marked (+ (length gnus-newsgroup-marked)
5419                     (length gnus-newsgroup-dormant)))
5420          (select
5421           (cond
5422            ((numberp read-all)
5423             read-all)
5424            ((numberp gnus-newsgroup-display)
5425             gnus-newsgroup-display)
5426            (t
5427             (condition-case ()
5428                 (cond
5429                  ((and (or (<= scored marked) (= scored number))
5430                        (numberp gnus-large-newsgroup)
5431                        (> number gnus-large-newsgroup))
5432                   (let* ((cursor-in-echo-area nil)
5433                          (initial (gnus-parameter-large-newsgroup-initial
5434                                    gnus-newsgroup-name))
5435                          (input
5436                           (read-string
5437                            (format
5438                             "How many articles from %s (%s %d): "
5439                             (gnus-limit-string
5440                              (gnus-group-decoded-name gnus-newsgroup-name)
5441                              35)
5442                             (if initial "max" "default")
5443                             number)
5444                            (if initial
5445                                (cons (number-to-string initial)
5446                                      0)))))
5447                     (if (string-match "^[ \t]*$" input) number input)))
5448                  ((and (> scored marked) (< scored number)
5449                        (> (- scored number) 20))
5450                   (let ((input
5451                          (read-string
5452                           (format "%s %s (%d scored, %d total): "
5453                                   "How many articles from"
5454                                   (gnus-group-decoded-name group)
5455                                   scored number))))
5456                     (if (string-match "^[ \t]*$" input)
5457                         number input)))
5458                  (t number))
5459               (quit
5460                (message "Quit getting the articles to read")
5461                nil))))))
5462     (setq select (if (stringp select) (string-to-number select) select))
5463     (if (or (null select) (zerop select))
5464         select
5465       (if (and (not (zerop scored)) (<= (abs select) scored))
5466           (progn
5467             (setq articles (sort scored-list '<))
5468             (setq number (length articles)))
5469         (setq articles (copy-sequence articles)))
5470
5471       (when (< (abs select) number)
5472         (if (< select 0)
5473             ;; Select the N oldest articles.
5474             (setcdr (nthcdr (1- (abs select)) articles) nil)
5475           ;; Select the N most recent articles.
5476           (setq articles (nthcdr (- number select) articles))))
5477       (setq gnus-newsgroup-unselected
5478             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5479       (when gnus-alter-articles-to-read-function
5480         (setq articles
5481               (sort
5482                (funcall gnus-alter-articles-to-read-function
5483                         gnus-newsgroup-name articles)
5484                '<)))
5485       articles)))
5486
5487 (defun gnus-killed-articles (killed articles)
5488   (let (out)
5489     (while articles
5490       (when (inline (gnus-member-of-range (car articles) killed))
5491         (push (car articles) out))
5492       (setq articles (cdr articles)))
5493     out))
5494
5495 (defun gnus-uncompress-marks (marks)
5496   "Uncompress the mark ranges in MARKS."
5497   (let ((uncompressed '(score bookmark))
5498         out)
5499     (while marks
5500       (if (memq (caar marks) uncompressed)
5501           (push (car marks) out)
5502         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5503       (setq marks (cdr marks)))
5504     out))
5505
5506 (defun gnus-article-mark-to-type (mark)
5507   "Return the type of MARK."
5508   (or (cadr (assq mark gnus-article-special-mark-lists))
5509       'list))
5510
5511 (defun gnus-article-unpropagatable-p (mark)
5512   "Return whether MARK should be propagated to back end."
5513   (memq mark gnus-article-unpropagated-mark-lists))
5514
5515 (defun gnus-adjust-marked-articles (info)
5516   "Set all article lists and remove all marks that are no longer valid."
5517   (let* ((marked-lists (gnus-info-marks info))
5518          (active (gnus-active (gnus-info-group info)))
5519          (min (car active))
5520          (max (cdr active))
5521          (types gnus-article-mark-lists)
5522          marks var articles article mark mark-type
5523          bgn end)
5524
5525     (dolist (marks marked-lists)
5526       (setq mark (car marks)
5527             mark-type (gnus-article-mark-to-type mark)
5528             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5529
5530       ;; We set the variable according to the type of the marks list,
5531       ;; and then adjust the marks to a subset of the active articles.
5532       (cond
5533        ;; Adjust "simple" lists - compressed yet unsorted
5534        ((eq mark-type 'list)
5535         ;; Simultaneously uncompress and clip to active range
5536         ;; See gnus-uncompress-range for a description of possible marks
5537         (let (l lh)
5538           (if (not (cadr marks))
5539               (set var nil)
5540             (setq articles (if (numberp (cddr marks))
5541                                (list (cdr marks))
5542                              (cdr marks))
5543                   lh (cons nil nil)
5544                   l lh)
5545
5546             (while (setq article (pop articles))
5547               (cond ((consp article)
5548                      (setq bgn (max (car article) min)
5549                            end (min (cdr article) max))
5550                      (while (<= bgn end)
5551                        (setq l (setcdr l (cons bgn nil))
5552                              bgn (1+ bgn))))
5553                     ((and (<= min article)
5554                           (>= max article))
5555                      (setq l (setcdr l (cons article nil))))))
5556             (set var (cdr lh)))))
5557        ;; Adjust assocs.
5558        ((eq mark-type 'tuple)
5559         (set var (setq articles (cdr marks)))
5560         (when (not (listp (cdr (symbol-value var))))
5561           (set var (list (symbol-value var))))
5562         (when (not (listp (cdr articles)))
5563           (setq articles (list articles)))
5564         (while articles
5565           (when (or (not (consp (setq article (pop articles))))
5566                     (< (car article) min)
5567                     (> (car article) max))
5568             (set var (delq article (symbol-value var))))))
5569        ;; Adjust ranges (sloppily).
5570        ((eq mark-type 'range)
5571         (cond
5572          ((eq mark 'seen)
5573           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5574           ;; It should be (seen (NUM1 . NUM2)).
5575           (when (numberp (cddr marks))
5576             (setcdr marks (list (cdr marks))))
5577           (setq articles (cdr marks))
5578           (while (and articles
5579                       (or (and (consp (car articles))
5580                                (> min (cdar articles)))
5581                           (and (numberp (car articles))
5582                                (> min (car articles)))))
5583             (pop articles))
5584           (set var articles))))))))
5585
5586 (defun gnus-update-missing-marks (missing)
5587   "Go through the list of MISSING articles and remove them from the mark lists."
5588   (when missing
5589     (let (var m)
5590       ;; Go through all types.
5591       (dolist (elem gnus-article-mark-lists)
5592         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5593           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5594           (when (symbol-value var)
5595             ;; This list has articles.  So we delete all missing
5596             ;; articles from it.
5597             (setq m missing)
5598             (while m
5599               (set var (delq (pop m) (symbol-value var))))))))))
5600
5601 (defun gnus-update-marks ()
5602   "Enter the various lists of marked articles into the newsgroup info list."
5603   (let ((types gnus-article-mark-lists)
5604         (info (gnus-get-info gnus-newsgroup-name))
5605         type list newmarked symbol delta-marks)
5606     (when info
5607       ;; Add all marks lists to the list of marks lists.
5608       (while (setq type (pop types))
5609         (setq list (symbol-value
5610                     (setq symbol
5611                           (intern (format "gnus-newsgroup-%s" (car type))))))
5612
5613         (when list
5614           ;; Get rid of the entries of the articles that have the
5615           ;; default score.
5616           (when (and (eq (cdr type) 'score)
5617                      gnus-save-score
5618                      list)
5619             (let* ((arts list)
5620                    (prev (cons nil list))
5621                    (all prev))
5622               (while arts
5623                 (if (or (not (consp (car arts)))
5624                         (= (cdar arts) gnus-summary-default-score))
5625                     (setcdr prev (cdr arts))
5626                   (setq prev arts))
5627                 (setq arts (cdr arts)))
5628               (setq list (cdr all)))))
5629
5630         (when (eq (cdr type) 'seen)
5631           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5632
5633         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5634           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5635
5636         (when (and (gnus-check-backend-function
5637                     'request-set-mark gnus-newsgroup-name)
5638                    (not (gnus-article-unpropagatable-p (cdr type))))
5639           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5640                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5641                  (add (gnus-remove-from-range
5642                        (gnus-copy-sequence list) old)))
5643             (when add
5644               (push (list add 'add (list (cdr type))) delta-marks))
5645             (when del
5646               (push (list del 'del (list (cdr type))) delta-marks))))
5647
5648         (when list
5649           (push (cons (cdr type) list) newmarked)))
5650
5651       (when delta-marks
5652         (unless (gnus-check-group gnus-newsgroup-name)
5653           (error "Can't open server for %s" gnus-newsgroup-name))
5654         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5655
5656       ;; Enter these new marks into the info of the group.
5657       (if (nthcdr 3 info)
5658           (setcar (nthcdr 3 info) newmarked)
5659         ;; Add the marks lists to the end of the info.
5660         (when newmarked
5661           (setcdr (nthcdr 2 info) (list newmarked))))
5662
5663       ;; Cut off the end of the info if there's nothing else there.
5664       (let ((i 5))
5665         (while (and (> i 2)
5666                     (not (nth i info)))
5667           (when (nthcdr (decf i) info)
5668             (setcdr (nthcdr i info) nil)))))))
5669
5670 (defun gnus-set-mode-line (where)
5671   "Set the mode line of the article or summary buffers.
5672 If WHERE is `summary', the summary mode line format will be used."
5673   ;; Is this mode line one we keep updated?
5674   (when (and (memq where gnus-updated-mode-lines)
5675              (symbol-value
5676               (intern (format "gnus-%s-mode-line-format-spec" where))))
5677     (let (mode-string)
5678       (save-excursion
5679         ;; We evaluate this in the summary buffer since these
5680         ;; variables are buffer-local to that buffer.
5681         (set-buffer gnus-summary-buffer)
5682        ;; We bind all these variables that are used in the `eval' form
5683         ;; below.
5684         (let* ((mformat (symbol-value
5685                          (intern
5686                           (format "gnus-%s-mode-line-format-spec" where))))
5687                (gnus-tmp-group-name (gnus-group-decoded-name
5688                                      gnus-newsgroup-name))
5689                (gnus-tmp-article-number (or gnus-current-article 0))
5690                (gnus-tmp-unread gnus-newsgroup-unreads)
5691                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5692                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5693                (gnus-tmp-unread-and-unselected
5694                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5695                             (zerop gnus-tmp-unselected))
5696                        "")
5697                       ((zerop gnus-tmp-unselected)
5698                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5699                       (t (format "{%d(+%d) more}"
5700                                  gnus-tmp-unread-and-unticked
5701                                  gnus-tmp-unselected))))
5702                (gnus-tmp-subject
5703                 (if (and gnus-current-headers
5704                          (vectorp gnus-current-headers))
5705                     (gnus-mode-string-quote
5706                      (mail-header-subject gnus-current-headers))
5707                   ""))
5708                bufname-length max-len
5709                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5710           (setq mode-string (eval mformat))
5711           (setq bufname-length (if (string-match "%b" mode-string)
5712                                    (- (length
5713                                        (buffer-name
5714                                         (if (eq where 'summary)
5715                                             nil
5716                                           (get-buffer gnus-article-buffer))))
5717                                       2)
5718                                  0))
5719           (setq max-len (max 4 (if gnus-mode-non-string-length
5720                                    (- (window-width)
5721                                       gnus-mode-non-string-length
5722                                       bufname-length)
5723                                  (length mode-string))))
5724           ;; We might have to chop a bit of the string off...
5725           (when (> (length mode-string) max-len)
5726             (setq mode-string
5727                   (concat (truncate-string-to-width mode-string (- max-len 3))
5728                           "...")))
5729           ;; Pad the mode string a bit.
5730           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5731       ;; Update the mode line.
5732       (setq mode-line-buffer-identification
5733             (gnus-mode-line-buffer-identification (list mode-string)))
5734       (set-buffer-modified-p t))))
5735
5736 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5737   "Go through the HEADERS list and add all Xrefs to a hash table.
5738 The resulting hash table is returned, or nil if no Xrefs were found."
5739   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5740          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5741          (xref-hashtb (gnus-make-hashtable))
5742          start group entry number xrefs header)
5743     (while headers
5744       (setq header (pop headers))
5745       (when (and (setq xrefs (mail-header-xref header))
5746                  (not (memq (setq number (mail-header-number header))
5747                             unreads)))
5748         (setq start 0)
5749         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5750           (setq start (match-end 0))
5751           (setq group (if prefix
5752                           (concat prefix (substring xrefs (match-beginning 1)
5753                                                     (match-end 1)))
5754                         (substring xrefs (match-beginning 1) (match-end 1))))
5755           (setq number
5756                 (string-to-number (substring xrefs (match-beginning 2)
5757                                           (match-end 2))))
5758           (if (setq entry (gnus-gethash group xref-hashtb))
5759               (setcdr entry (cons number (cdr entry)))
5760             (gnus-sethash group (cons number nil) xref-hashtb)))))
5761     (and start xref-hashtb)))
5762
5763 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5764   "Look through all the headers and mark the Xrefs as read."
5765   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5766         name info xref-hashtb idlist method nth4)
5767     (save-excursion
5768       (set-buffer gnus-group-buffer)
5769       (when (setq xref-hashtb
5770                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5771         (mapatoms
5772          (lambda (group)
5773            (unless (string= from-newsgroup (setq name (symbol-name group)))
5774              (setq idlist (symbol-value group))
5775              ;; Dead groups are not updated.
5776              (and (prog1
5777                       (setq info (gnus-get-info name))
5778                     (when (stringp (setq nth4 (gnus-info-method info)))
5779                       (setq nth4 (gnus-server-to-method nth4))))
5780                   ;; Only do the xrefs if the group has the same
5781                   ;; select method as the group we have just read.
5782                   (or (gnus-methods-equal-p
5783                        nth4 (gnus-find-method-for-group from-newsgroup))
5784                       virtual
5785                       (equal nth4 (setq method (gnus-find-method-for-group
5786                                                 from-newsgroup)))
5787                       (and (equal (car nth4) (car method))
5788                            (equal (nth 1 nth4) (nth 1 method))))
5789                   gnus-use-cross-reference
5790                   (or (not (eq gnus-use-cross-reference t))
5791                       virtual
5792                       ;; Only do cross-references on subscribed
5793                       ;; groups, if that is what is wanted.
5794                       (<= (gnus-info-level info) gnus-level-subscribed))
5795                   (gnus-group-make-articles-read name idlist))))
5796          xref-hashtb)))))
5797
5798 (defun gnus-compute-read-articles (group articles)
5799   (let* ((entry (gnus-group-entry group))
5800          (info (nth 2 entry))
5801          (active (gnus-active group))
5802          ninfo)
5803     (when entry
5804       ;; First peel off all invalid article numbers.
5805       (when active
5806         (let ((ids articles)
5807               id first)
5808           (while (setq id (pop ids))
5809             (when (and first (> id (cdr active)))
5810               ;; We'll end up in this situation in one particular
5811               ;; obscure situation.  If you re-scan a group and get
5812               ;; a new article that is cross-posted to a different
5813               ;; group that has not been re-scanned, you might get
5814               ;; crossposted article that has a higher number than
5815               ;; Gnus believes possible.  So we re-activate this
5816               ;; group as well.  This might mean doing the
5817               ;; crossposting thingy will *increase* the number
5818               ;; of articles in some groups.  Tsk, tsk.
5819               (setq active (or (gnus-activate-group group) active)))
5820             (when (or (> id (cdr active))
5821                       (< id (car active)))
5822               (setq articles (delq id articles))))))
5823       ;; If the read list is nil, we init it.
5824       (if (and active
5825                (null (gnus-info-read info))
5826                (> (car active) 1))
5827           (setq ninfo (cons 1 (1- (car active))))
5828         (setq ninfo (gnus-info-read info)))
5829       ;; Then we add the read articles to the range.
5830       (gnus-add-to-range
5831        ninfo (setq articles (sort articles '<))))))
5832
5833 (defun gnus-group-make-articles-read (group articles)
5834   "Update the info of GROUP to say that ARTICLES are read."
5835   (let* ((num 0)
5836          (entry (gnus-group-entry group))
5837          (info (nth 2 entry))
5838          (active (gnus-active group))
5839          range)
5840     (when entry
5841       (setq range (gnus-compute-read-articles group articles))
5842       (with-current-buffer gnus-group-buffer
5843         (gnus-undo-register
5844           `(progn
5845              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5846              (gnus-info-set-read ',info ',(gnus-info-read info))
5847              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5848              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5849              (gnus-group-update-group ,group t))))
5850       ;; Add the read articles to the range.
5851       (gnus-info-set-read info range)
5852       (gnus-request-set-mark group (list (list range 'add '(read))))
5853       ;; Then we have to re-compute how many unread
5854       ;; articles there are in this group.
5855       (when active
5856         (cond
5857          ((not range)
5858           (setq num (- (1+ (cdr active)) (car active))))
5859          ((not (listp (cdr range)))
5860           (setq num (- (cdr active) (- (1+ (cdr range))
5861                                        (car range)))))
5862          (t
5863           (while range
5864             (if (numberp (car range))
5865                 (setq num (1+ num))
5866               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5867             (setq range (cdr range)))
5868           (setq num (- (cdr active) num))))
5869         ;; Update the number of unread articles.
5870         (setcar entry num)
5871         ;; Update the group buffer.
5872         (unless (gnus-ephemeral-group-p group)
5873           (gnus-group-update-group group t))))))
5874
5875 (defvar gnus-newsgroup-none-id 0)
5876
5877 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5878   (let ((cur nntp-server-buffer)
5879         (dependencies
5880          (or dependencies
5881              (with-current-buffer gnus-summary-buffer
5882                gnus-newsgroup-dependencies)))
5883         headers id end ref number
5884         (mail-parse-charset gnus-newsgroup-charset)
5885         (mail-parse-ignored-charsets
5886          (save-excursion (condition-case nil
5887                              (set-buffer gnus-summary-buffer)
5888                            (error))
5889                          gnus-newsgroup-ignored-charsets)))
5890     (save-excursion
5891       (set-buffer nntp-server-buffer)
5892       ;; Translate all TAB characters into SPACE characters.
5893       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5894       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5895       (ietf-drums-unfold-fws)
5896       (gnus-run-hooks 'gnus-parse-headers-hook)
5897       (let ((case-fold-search t)
5898             in-reply-to header p lines chars)
5899         (goto-char (point-min))
5900         ;; Search to the beginning of the next header.  Error messages
5901         ;; do not begin with 2 or 3.
5902         (while (re-search-forward "^[23][0-9]+ " nil t)
5903           (setq id nil
5904                 ref nil)
5905           ;; This implementation of this function, with nine
5906           ;; search-forwards instead of the one re-search-forward and
5907           ;; a case (which basically was the old function) is actually
5908           ;; about twice as fast, even though it looks messier.  You
5909           ;; can't have everything, I guess.  Speed and elegance
5910           ;; doesn't always go hand in hand.
5911           (setq
5912            header
5913            (vector
5914             ;; Number.
5915             (prog1
5916                 (setq number (read cur))
5917               (end-of-line)
5918               (setq p (point))
5919               (narrow-to-region (point)
5920                                 (or (and (search-forward "\n.\n" nil t)
5921                                          (- (point) 2))
5922                                     (point))))
5923             ;; Subject.
5924             (progn
5925               (goto-char p)
5926               (if (search-forward "\nsubject:" nil t)
5927                   (funcall gnus-decode-encoded-word-function
5928                            (nnheader-header-value))
5929                 "(none)"))
5930             ;; From.
5931             (progn
5932               (goto-char p)
5933               (if (search-forward "\nfrom:" nil t)
5934                   (funcall gnus-decode-encoded-word-function
5935                            (nnheader-header-value))
5936                 "(nobody)"))
5937             ;; Date.
5938             (progn
5939               (goto-char p)
5940               (if (search-forward "\ndate:" nil t)
5941                   (nnheader-header-value) ""))
5942             ;; Message-ID.
5943             (progn
5944               (goto-char p)
5945               (setq id (if (re-search-forward
5946                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5947                            ;; We do it this way to make sure the Message-ID
5948                            ;; is (somewhat) syntactically valid.
5949                            (buffer-substring (match-beginning 1)
5950                                              (match-end 1))
5951                          ;; If there was no message-id, we just fake one
5952                          ;; to make subsequent routines simpler.
5953                          (nnheader-generate-fake-message-id number))))
5954             ;; References.
5955             (progn
5956               (goto-char p)
5957               (if (search-forward "\nreferences:" nil t)
5958                   (progn
5959                     (setq end (point))
5960                     (prog1
5961                         (nnheader-header-value)
5962                       (setq ref
5963                             (buffer-substring
5964                              (progn
5965                                (end-of-line)
5966                                (search-backward ">" end t)
5967                                (1+ (point)))
5968                              (progn
5969                                (search-backward "<" end t)
5970                                (point))))))
5971                 ;; Get the references from the in-reply-to header if there
5972                 ;; were no references and the in-reply-to header looks
5973                 ;; promising.
5974                 (if (and (search-forward "\nin-reply-to:" nil t)
5975                          (setq in-reply-to (nnheader-header-value))
5976                          (string-match "<[^>]+>" in-reply-to))
5977                     (let (ref2)
5978                       (setq ref (substring in-reply-to (match-beginning 0)
5979                                            (match-end 0)))
5980                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5981                         (setq ref2 (substring in-reply-to (match-beginning 0)
5982                                               (match-end 0)))
5983                         (when (> (length ref2) (length ref))
5984                           (setq ref ref2)))
5985                       ref)
5986                   (setq ref nil))))
5987             ;; Chars.
5988             (progn
5989               (goto-char p)
5990               (if (search-forward "\nchars: " nil t)
5991                   (if (numberp (setq chars (ignore-errors (read cur))))
5992                       chars -1)
5993                 -1))
5994             ;; Lines.
5995             (progn
5996               (goto-char p)
5997               (if (search-forward "\nlines: " nil t)
5998                   (if (numberp (setq lines (ignore-errors (read cur))))
5999                       lines -1)
6000                 -1))
6001             ;; Xref.
6002             (progn
6003               (goto-char p)
6004               (and (search-forward "\nxref:" nil t)
6005                    (nnheader-header-value)))
6006             ;; Extra.
6007             (when gnus-extra-headers
6008               (let ((extra gnus-extra-headers)
6009                     out)
6010                 (while extra
6011                   (goto-char p)
6012                   (when (search-forward
6013                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6014                     (push (cons (car extra) (nnheader-header-value))
6015                           out))
6016                   (pop extra))
6017                 out))))
6018           (when (equal id ref)
6019             (setq ref nil))
6020
6021           (when gnus-alter-header-function
6022             (funcall gnus-alter-header-function header)
6023             (setq id (mail-header-id header)
6024                   ref (gnus-parent-id (mail-header-references header))))
6025
6026           (when (setq header
6027                       (gnus-dependencies-add-header
6028                        header dependencies force-new))
6029             (push header headers))
6030           (goto-char (point-max))
6031           (widen))
6032         (nreverse headers)))))
6033
6034 ;; Goes through the xover lines and returns a list of vectors
6035 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6036                                                   force-new dependencies
6037                                                   group also-fetch-heads)
6038   "Parse the news overview data in the server buffer.
6039 Return a list of headers that match SEQUENCE (see
6040 `nntp-retrieve-headers')."
6041   ;; Get the Xref when the users reads the articles since most/some
6042   ;; NNTP servers do not include Xrefs when using XOVER.
6043   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6044   (let ((mail-parse-charset gnus-newsgroup-charset)
6045         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6046         (cur nntp-server-buffer)
6047         (dependencies (or dependencies gnus-newsgroup-dependencies))
6048         (allp (cond
6049                ((eq gnus-read-all-available-headers t)
6050                 t)
6051                ((stringp gnus-read-all-available-headers)
6052                 (string-match gnus-read-all-available-headers group))
6053                (t
6054                 nil)))
6055         number headers header)
6056     (save-excursion
6057       (set-buffer nntp-server-buffer)
6058       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6059       ;; Allow the user to mangle the headers before parsing them.
6060       (gnus-run-hooks 'gnus-parse-headers-hook)
6061       (goto-char (point-min))
6062       (gnus-parse-without-error
6063         (while (and (or sequence allp)
6064                     (not (eobp)))
6065           (setq number (read cur))
6066           (when (not allp)
6067             (while (and sequence
6068                         (< (car sequence) number))
6069               (setq sequence (cdr sequence))))
6070           (when (and (or allp
6071                          (and sequence
6072                               (eq number (car sequence))))
6073                      (progn
6074                        (setq sequence (cdr sequence))
6075                        (setq header (inline
6076                                       (gnus-nov-parse-line
6077                                        number dependencies force-new)))))
6078             (push header headers))
6079           (forward-line 1)))
6080       ;; A common bug in inn is that if you have posted an article and
6081       ;; then retrieves the active file, it will answer correctly --
6082       ;; the new article is included.  However, a NOV entry for the
6083       ;; article may not have been generated yet, so this may fail.
6084       ;; We work around this problem by retrieving the last few
6085       ;; headers using HEAD.
6086       (if (or (not also-fetch-heads)
6087               (not sequence))
6088           ;; We (probably) got all the headers.
6089           (nreverse headers)
6090         (let ((gnus-nov-is-evil t))
6091           (nconc
6092            (nreverse headers)
6093            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6094              (gnus-get-newsgroup-headers))))))))
6095
6096 (defun gnus-article-get-xrefs ()
6097   "Fill in the Xref value in `gnus-current-headers', if necessary.
6098 This is meant to be called in `gnus-article-internal-prepare-hook'."
6099   (let ((headers (with-current-buffer gnus-summary-buffer
6100                    gnus-current-headers)))
6101     (or (not gnus-use-cross-reference)
6102         (not headers)
6103         (and (mail-header-xref headers)
6104              (not (string= (mail-header-xref headers) "")))
6105         (let ((case-fold-search t)
6106               xref)
6107           (save-restriction
6108             (nnheader-narrow-to-headers)
6109             (goto-char (point-min))
6110             (when (or (and (not (eobp))
6111                            (eq (downcase (char-after)) ?x)
6112                            (looking-at "Xref:"))
6113                       (search-forward "\nXref:" nil t))
6114               (goto-char (1+ (match-end 0)))
6115               (setq xref (buffer-substring (point) (point-at-eol)))
6116               (mail-header-set-xref headers xref)))))))
6117
6118 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6119   "Find article ID and insert the summary line for that article.
6120 OLD-HEADER can either be a header or a line number to insert
6121 the subject line on."
6122   (let* ((line (and (numberp old-header) old-header))
6123          (old-header (and (vectorp old-header) old-header))
6124          (header (cond ((and old-header use-old-header)
6125                         old-header)
6126                        ((and (numberp id)
6127                              (gnus-number-to-header id))
6128                         (gnus-number-to-header id))
6129                        (t
6130                         (gnus-read-header id))))
6131          (number (and (numberp id) id))
6132          d)
6133     (when header
6134       ;; Rebuild the thread that this article is part of and go to the
6135       ;; article we have fetched.
6136       (when (and (not gnus-show-threads)
6137                  old-header)
6138         (when (and number
6139                    (setq d (gnus-data-find (mail-header-number old-header))))
6140           (goto-char (gnus-data-pos d))
6141           (gnus-data-remove
6142            number
6143            (- (point-at-bol)
6144               (prog1
6145                   (1+ (point-at-eol))
6146                 (gnus-delete-line))))))
6147       ;; Remove list identifiers from subject.
6148       (when gnus-list-identifiers
6149         (let ((gnus-newsgroup-headers (list header)))
6150           (gnus-summary-remove-list-identifiers)))
6151       (when old-header
6152         (mail-header-set-number header (mail-header-number old-header)))
6153       (setq gnus-newsgroup-sparse
6154             (delq (setq number (mail-header-number header))
6155                   gnus-newsgroup-sparse))
6156       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6157       (push number gnus-newsgroup-limit)
6158       (gnus-rebuild-thread (mail-header-id header) line)
6159       (gnus-summary-goto-subject number nil t))
6160     (when (and (numberp number)
6161                (> number 0))
6162       ;; We have to update the boundaries even if we can't fetch the
6163       ;; article if ID is a number -- so that the next `P' or `N'
6164       ;; command will fetch the previous (or next) article even
6165       ;; if the one we tried to fetch this time has been canceled.
6166       (when (> number gnus-newsgroup-end)
6167         (setq gnus-newsgroup-end number))
6168       (when (< number gnus-newsgroup-begin)
6169         (setq gnus-newsgroup-begin number))
6170       (setq gnus-newsgroup-unselected
6171             (delq number gnus-newsgroup-unselected)))
6172     ;; Report back a success?
6173     (and header (mail-header-number header))))
6174
6175 ;;; Process/prefix in the summary buffer
6176
6177 (defun gnus-summary-work-articles (n)
6178   "Return a list of articles to be worked upon.
6179 The prefix argument, the list of process marked articles, and the
6180 current article will be taken into consideration."
6181   (save-excursion
6182     (set-buffer gnus-summary-buffer)
6183     (cond
6184      (n
6185       ;; A numerical prefix has been given.
6186       (setq n (prefix-numeric-value n))
6187       (let ((backward (< n 0))
6188             (n (abs (prefix-numeric-value n)))
6189             articles article)
6190         (save-excursion
6191           (while
6192               (and (> n 0)
6193                    (push (setq article (gnus-summary-article-number))
6194                          articles)
6195                    (if backward
6196                        (gnus-summary-find-prev nil article)
6197                      (gnus-summary-find-next nil article)))
6198             (decf n)))
6199         (nreverse articles)))
6200      ((and (gnus-region-active-p) (mark))
6201       (message "region active")
6202       ;; Work on the region between point and mark.
6203       (let ((max (max (point) (mark)))
6204             articles article)
6205         (save-excursion
6206           (goto-char (min (min (point) (mark))))
6207           (while
6208               (and
6209                (push (setq article (gnus-summary-article-number)) articles)
6210                (gnus-summary-find-next nil article)
6211                (< (point) max)))
6212           (nreverse articles))))
6213      (gnus-newsgroup-processable
6214       ;; There are process-marked articles present.
6215       ;; Save current state.
6216       (gnus-summary-save-process-mark)
6217       ;; Return the list.
6218       (reverse gnus-newsgroup-processable))
6219      (t
6220       ;; Just return the current article.
6221       (list (gnus-summary-article-number))))))
6222
6223 (defmacro gnus-summary-iterate (arg &rest forms)
6224   "Iterate over the process/prefixed articles and do FORMS.
6225 ARG is the interactive prefix given to the command.  FORMS will be
6226 executed with point over the summary line of the articles."
6227   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6228     `(let ((,articles (gnus-summary-work-articles ,arg)))
6229        (while ,articles
6230          (gnus-summary-goto-subject (car ,articles))
6231          ,@forms
6232          (pop ,articles)))))
6233
6234 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6235 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6236
6237 (defun gnus-summary-save-process-mark ()
6238   "Push the current set of process marked articles on the stack."
6239   (interactive)
6240   (push (copy-sequence gnus-newsgroup-processable)
6241         gnus-newsgroup-process-stack))
6242
6243 (defun gnus-summary-kill-process-mark ()
6244   "Push the current set of process marked articles on the stack and unmark."
6245   (interactive)
6246   (gnus-summary-save-process-mark)
6247   (gnus-summary-unmark-all-processable))
6248
6249 (defun gnus-summary-yank-process-mark ()
6250   "Pop the last process mark state off the stack and restore it."
6251   (interactive)
6252   (unless gnus-newsgroup-process-stack
6253     (error "Empty mark stack"))
6254   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6255
6256 (defun gnus-summary-process-mark-set (set)
6257   "Make SET into the current process marked articles."
6258   (gnus-summary-unmark-all-processable)
6259   (mapc 'gnus-summary-set-process-mark set))
6260
6261 ;;; Searching and stuff
6262
6263 (defun gnus-summary-search-group (&optional backward use-level)
6264   "Search for next unread newsgroup.
6265 If optional argument BACKWARD is non-nil, search backward instead."
6266   (save-excursion
6267     (set-buffer gnus-group-buffer)
6268     (when (gnus-group-search-forward
6269            backward nil (if use-level (gnus-group-group-level) nil))
6270       (gnus-group-group-name))))
6271
6272 (defun gnus-summary-best-group (&optional exclude-group)
6273   "Find the name of the best unread group.
6274 If EXCLUDE-GROUP, do not go to this group."
6275   (with-current-buffer gnus-group-buffer
6276     (save-excursion
6277       (gnus-group-best-unread-group exclude-group))))
6278
6279 (defun gnus-summary-find-next (&optional unread article backward)
6280   (if backward
6281       (gnus-summary-find-prev unread article)
6282     (let* ((dummy (gnus-summary-article-intangible-p))
6283            (article (or article (gnus-summary-article-number)))
6284            (data (gnus-data-find-list article))
6285            result)
6286       (when (and (not dummy)
6287                  (or (not gnus-summary-check-current)
6288                      (not unread)
6289                      (not (gnus-data-unread-p (car data)))))
6290         (setq data (cdr data)))
6291       (when (setq result
6292                   (if unread
6293                       (progn
6294                         (while data
6295                           (unless (memq (gnus-data-number (car data))
6296                                         (cond
6297                                          ((eq gnus-auto-goto-ignores
6298                                               'always-undownloaded)
6299                                           gnus-newsgroup-undownloaded)
6300                                          (gnus-plugged
6301                                           nil)
6302                                          ((eq gnus-auto-goto-ignores
6303                                               'unfetched)
6304                                           gnus-newsgroup-unfetched)
6305                                          ((eq gnus-auto-goto-ignores
6306                                               'undownloaded)
6307                                           gnus-newsgroup-undownloaded)))
6308                             (when (gnus-data-unread-p (car data))
6309                               (setq result (car data)
6310                                     data nil)))
6311                           (setq data (cdr data)))
6312                         result)
6313                     (car data)))
6314         (goto-char (gnus-data-pos result))
6315         (gnus-data-number result)))))
6316
6317 (defun gnus-summary-find-prev (&optional unread article)
6318   (let* ((eobp (eobp))
6319          (article (or article (gnus-summary-article-number)))
6320          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6321          result)
6322     (when (and (not eobp)
6323                (or (not gnus-summary-check-current)
6324                    (not unread)
6325                    (not (gnus-data-unread-p (car data)))))
6326       (setq data (cdr data)))
6327     (when (setq result
6328                 (if unread
6329                     (progn
6330                       (while data
6331                         (unless (memq (gnus-data-number (car data))
6332                                       (cond
6333                                        ((eq gnus-auto-goto-ignores
6334                                             'always-undownloaded)
6335                                         gnus-newsgroup-undownloaded)
6336                                        (gnus-plugged
6337                                         nil)
6338                                        ((eq gnus-auto-goto-ignores
6339                                             'unfetched)
6340                                         gnus-newsgroup-unfetched)
6341                                        ((eq gnus-auto-goto-ignores
6342                                             'undownloaded)
6343                                         gnus-newsgroup-undownloaded)))
6344                           (when (gnus-data-unread-p (car data))
6345                             (setq result (car data)
6346                                   data nil)))
6347                         (setq data (cdr data)))
6348                       result)
6349                   (car data)))
6350       (goto-char (gnus-data-pos result))
6351       (gnus-data-number result))))
6352
6353 (defun gnus-summary-find-subject (subject &optional unread backward article)
6354   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6355          (article (or article (gnus-summary-article-number)))
6356          (articles (gnus-data-list backward))
6357          (arts (gnus-data-find-list article articles))
6358          result)
6359     (when (or (not gnus-summary-check-current)
6360               (not unread)
6361               (not (gnus-data-unread-p (car arts))))
6362       (setq arts (cdr arts)))
6363     (while arts
6364       (and (or (not unread)
6365                (gnus-data-unread-p (car arts)))
6366            (vectorp (gnus-data-header (car arts)))
6367            (gnus-subject-equal
6368             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6369            (setq result (car arts)
6370                  arts nil))
6371       (setq arts (cdr arts)))
6372     (and result
6373          (goto-char (gnus-data-pos result))
6374          (gnus-data-number result))))
6375
6376 (defun gnus-summary-search-forward (&optional unread subject backward)
6377   "Search forward for an article.
6378 If UNREAD, look for unread articles.  If SUBJECT, look for
6379 articles with that subject.  If BACKWARD, search backward instead."
6380   (cond (subject (gnus-summary-find-subject subject unread backward))
6381         (backward (gnus-summary-find-prev unread))
6382         (t (gnus-summary-find-next unread))))
6383
6384 (defun gnus-recenter (&optional n)
6385   "Center point in window and redisplay frame.
6386 Also do horizontal recentering."
6387   (interactive "P")
6388   (when (and gnus-auto-center-summary
6389              (not (eq gnus-auto-center-summary 'vertical)))
6390     (gnus-horizontal-recenter))
6391   (recenter n))
6392
6393 (defun gnus-summary-recenter ()
6394   "Center point in the summary window.
6395 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6396 displayed, no centering will be performed."
6397   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6398   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6399   (interactive)
6400   ;; The user has to want it.
6401   (when gnus-auto-center-summary
6402     (let* ((top (cond ((< (window-height) 4) 0)
6403                       ((< (window-height) 7) 1)
6404                       (t (if (numberp gnus-auto-center-summary)
6405                              gnus-auto-center-summary
6406                            (/ (1- (window-height)) 2)))))
6407            (height (1- (window-height)))
6408            (bottom (save-excursion (goto-char (point-max))
6409                                    (forward-line (- height))
6410                                    (point)))
6411            (window (get-buffer-window (current-buffer))))
6412       (when (get-buffer-window gnus-article-buffer)
6413         ;; Only do recentering when the article buffer is displayed,
6414         ;; Set the window start to either `bottom', which is the biggest
6415         ;; possible valid number, or the second line from the top,
6416         ;; whichever is the least.
6417         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6418           (if (> bottom top-pos)
6419               ;; Keep the second line from the top visible
6420               (set-window-start window top-pos t)
6421             ;; Try to keep the bottom line visible; if it's partially
6422             ;; obscured, either scroll one more line to make it fully
6423             ;; visible, or revert to using TOP-POS.
6424             (save-excursion
6425               (goto-char (point-max))
6426               (forward-line -1)
6427               (let ((last-line-start (point)))
6428                 (goto-char bottom)
6429                 (set-window-start window (point) t)
6430                 (when (not (pos-visible-in-window-p last-line-start window))
6431                   (forward-line 1)
6432                   (set-window-start window (min (point) top-pos) t)))))))
6433       ;; Do horizontal recentering while we're at it.
6434       (when (and (get-buffer-window (current-buffer) t)
6435                  (not (eq gnus-auto-center-summary 'vertical)))
6436         (let ((selected (selected-window)))
6437           (select-window (get-buffer-window (current-buffer) t))
6438           (gnus-summary-position-point)
6439           (gnus-horizontal-recenter)
6440           (select-window selected))))))
6441
6442 (defun gnus-summary-jump-to-group (newsgroup)
6443   "Move point to NEWSGROUP in group mode buffer."
6444   ;; Keep update point of group mode buffer if visible.
6445   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6446       (save-window-excursion
6447         ;; Take care of tree window mode.
6448         (when (get-buffer-window gnus-group-buffer)
6449           (pop-to-buffer gnus-group-buffer))
6450         (gnus-group-jump-to-group newsgroup))
6451     (save-excursion
6452       ;; Take care of tree window mode.
6453       (if (get-buffer-window gnus-group-buffer)
6454           (pop-to-buffer gnus-group-buffer)
6455         (set-buffer gnus-group-buffer))
6456       (gnus-group-jump-to-group newsgroup))))
6457
6458 ;; This function returns a list of article numbers based on the
6459 ;; difference between the ranges of read articles in this group and
6460 ;; the range of active articles.
6461 (defun gnus-list-of-unread-articles (group)
6462   (let* ((read (gnus-info-read (gnus-get-info group)))
6463          (active (or (gnus-active group) (gnus-activate-group group)))
6464          (last (or (cdr active)
6465                    (error "Group %s couldn't be activated " group)))
6466          first nlast unread)
6467     ;; If none are read, then all are unread.
6468     (if (not read)
6469         (setq first (car active))
6470       ;; If the range of read articles is a single range, then the
6471       ;; first unread article is the article after the last read
6472       ;; article.  Sounds logical, doesn't it?
6473       (if (and (not (listp (cdr read)))
6474                (or (< (car read) (car active))
6475                    (progn (setq read (list read))
6476                           nil)))
6477           (setq first (max (car active) (1+ (cdr read))))
6478         ;; `read' is a list of ranges.
6479         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6480                                   (caar read)))
6481                   1)
6482           (setq first (car active)))
6483         (while read
6484           (when first
6485             (while (< first nlast)
6486               (setq unread (cons first unread)
6487                     first (1+ first))))
6488           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6489           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6490           (setq read (cdr read)))))
6491     ;; And add the last unread articles.
6492     (while (<= first last)
6493       (setq unread (cons first unread)
6494             first (1+ first)))
6495     ;; Return the list of unread articles.
6496     (delq 0 (nreverse unread))))
6497
6498 (defun gnus-list-of-read-articles (group)
6499   "Return a list of unread, unticked and non-dormant articles."
6500   (let* ((info (gnus-get-info group))
6501          (marked (gnus-info-marks info))
6502          (active (gnus-active group)))
6503     (and info active
6504          (gnus-list-range-difference
6505           (gnus-list-range-difference
6506            (gnus-sorted-complement
6507             (gnus-uncompress-range active)
6508             (gnus-list-of-unread-articles group))
6509            (cdr (assq 'dormant marked)))
6510           (cdr (assq 'tick marked))))))
6511
6512 ;; This function returns a sequence of article numbers based on the
6513 ;; difference between the ranges of read articles in this group and
6514 ;; the range of active articles.
6515 (defun gnus-sequence-of-unread-articles (group)
6516   (let* ((read (gnus-info-read (gnus-get-info group)))
6517          (active (or (gnus-active group) (gnus-activate-group group)))
6518          (last (cdr active))
6519          first nlast unread)
6520     ;; If none are read, then all are unread.
6521     (if (not read)
6522         (setq first (car active))
6523       ;; If the range of read articles is a single range, then the
6524       ;; first unread article is the article after the last read
6525       ;; article.  Sounds logical, doesn't it?
6526       (if (and (not (listp (cdr read)))
6527                (or (< (car read) (car active))
6528                    (progn (setq read (list read))
6529                           nil)))
6530           (setq first (max (car active) (1+ (cdr read))))
6531         ;; `read' is a list of ranges.
6532         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6533                                   (caar read)))
6534                   1)
6535           (setq first (car active)))
6536         (while read
6537           (when first
6538             (push (cons first nlast) unread))
6539           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6540           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6541           (setq read (cdr read)))))
6542     ;; And add the last unread articles.
6543     (cond ((< first last)
6544            (push (cons first last) unread))
6545           ((= first last)
6546            (push first unread)))
6547     ;; Return the sequence of unread articles.
6548     (delq 0 (nreverse unread))))
6549
6550 ;; Various summary commands
6551
6552 (defun gnus-summary-select-article-buffer ()
6553   "Reconfigure windows to show article buffer."
6554   (interactive)
6555   (if (not (gnus-buffer-live-p gnus-article-buffer))
6556       (error "There is no article buffer for this summary buffer")
6557     (gnus-configure-windows 'article)
6558     (select-window (get-buffer-window gnus-article-buffer))))
6559
6560 (defun gnus-summary-universal-argument (arg)
6561   "Perform any operation on all articles that are process/prefixed."
6562   (interactive "P")
6563   (let ((articles (gnus-summary-work-articles arg))
6564         func article)
6565     (if (eq
6566          (setq
6567           func
6568           (key-binding
6569            (read-key-sequence
6570             (substitute-command-keys
6571              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6572          'undefined)
6573         (gnus-error 1 "Undefined key")
6574       (save-excursion
6575         (while articles
6576           (gnus-summary-goto-subject (setq article (pop articles)))
6577           (let (gnus-newsgroup-processable)
6578             (command-execute func))
6579           (gnus-summary-remove-process-mark article)))))
6580   (gnus-summary-position-point))
6581
6582 (defun gnus-summary-toggle-truncation (&optional arg)
6583   "Toggle truncation of summary lines.
6584 With ARG, turn line truncation on if ARG is positive."
6585   (interactive "P")
6586   (setq truncate-lines
6587         (if (null arg) (not truncate-lines)
6588           (> (prefix-numeric-value arg) 0)))
6589   (redraw-display))
6590
6591 (defun gnus-summary-find-for-reselect ()
6592   "Return the number of an article to stay on across a reselect.
6593 The current article is considered, then following articles, then previous
6594 articles.  An article is sought which is not cancelled and isn't a temporary
6595 insertion from another group.  If there's no such then return a dummy 0."
6596   (let (found)
6597     (dolist (rev '(nil t))
6598       (unless found      ; don't demand the reverse list if we don't need it
6599         (let ((data (gnus-data-find-list
6600                      (gnus-summary-article-number) (gnus-data-list rev))))
6601           (while (and data (not found))
6602             (if (and (< 0 (gnus-data-number (car data)))
6603                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6604                 (setq found (gnus-data-number (car data))))
6605             (setq data (cdr data))))))
6606     (or found 0)))
6607
6608 (defun gnus-summary-reselect-current-group (&optional all rescan)
6609   "Exit and then reselect the current newsgroup.
6610 The prefix argument ALL means to select all articles."
6611   (interactive "P")
6612   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6613     (error "Ephemeral groups can't be reselected"))
6614   (let ((current-subject (gnus-summary-find-for-reselect))
6615         (group gnus-newsgroup-name))
6616     (setq gnus-newsgroup-begin nil)
6617     (gnus-summary-exit nil 'leave-hidden)
6618     ;; We have to adjust the point of group mode buffer because
6619     ;; point was moved to the next unread newsgroup by exiting.
6620     (gnus-summary-jump-to-group group)
6621     (when rescan
6622       (save-excursion
6623         (gnus-group-get-new-news-this-group 1)))
6624     (gnus-group-read-group all t)
6625     (gnus-summary-goto-subject current-subject nil t)))
6626
6627 (defun gnus-summary-rescan-group (&optional all)
6628   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6629   (interactive "P")
6630   (gnus-summary-reselect-current-group all t))
6631
6632 (defun gnus-summary-update-info (&optional non-destructive)
6633   (save-excursion
6634     (let ((group gnus-newsgroup-name))
6635       (when group
6636         (when gnus-newsgroup-kill-headers
6637           (setq gnus-newsgroup-killed
6638                 (gnus-compress-sequence
6639                  (gnus-sorted-union
6640                   (gnus-list-range-intersection
6641                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6642                   gnus-newsgroup-unreads)
6643                  t)))
6644         (unless (listp (cdr gnus-newsgroup-killed))
6645           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6646         (let ((headers gnus-newsgroup-headers))
6647           ;; Set the new ranges of read articles.
6648           (with-current-buffer gnus-group-buffer
6649             (gnus-undo-force-boundary))
6650           (gnus-update-read-articles
6651            group (gnus-sorted-union
6652                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6653           ;; Set the current article marks.
6654           (let ((gnus-newsgroup-scored
6655                  (if (and (not gnus-save-score)
6656                           (not non-destructive))
6657                      nil
6658                    gnus-newsgroup-scored)))
6659             (save-excursion
6660               (gnus-update-marks)))
6661           ;; Do the cross-ref thing.
6662           (when gnus-use-cross-reference
6663             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6664           ;; Do not switch windows but change the buffer to work.
6665           (set-buffer gnus-group-buffer)
6666           (unless (gnus-ephemeral-group-p group)
6667             (gnus-group-update-group group)))))))
6668
6669 (defun gnus-summary-save-newsrc (&optional force)
6670   "Save the current number of read/marked articles in the dribble buffer.
6671 The dribble buffer will then be saved.
6672 If FORCE (the prefix), also save the .newsrc file(s)."
6673   (interactive "P")
6674   (gnus-summary-update-info t)
6675   (if force
6676       (gnus-save-newsrc-file)
6677     (gnus-dribble-save)))
6678
6679 (defun gnus-summary-exit (&optional temporary leave-hidden)
6680   "Exit reading current newsgroup, and then return to group selection mode.
6681 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6682   (interactive)
6683   (gnus-set-global-variables)
6684   (when (gnus-buffer-live-p gnus-article-buffer)
6685     (save-excursion
6686       (set-buffer gnus-article-buffer)
6687       (mm-destroy-parts gnus-article-mime-handles)
6688       ;; Set it to nil for safety reason.
6689       (setq gnus-article-mime-handle-alist nil)
6690       (setq gnus-article-mime-handles nil)))
6691   (gnus-kill-save-kill-buffer)
6692   (gnus-async-halt-prefetch)
6693   (let* ((group gnus-newsgroup-name)
6694          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6695          (gnus-group-is-exiting-p t)
6696          (mode major-mode)
6697          (group-point nil)
6698          (buf (current-buffer)))
6699     (unless quit-config
6700       ;; Do adaptive scoring, and possibly save score files.
6701       (when gnus-newsgroup-adaptive
6702         (gnus-score-adaptive))
6703       (when gnus-use-scoring
6704         (gnus-score-save)))
6705     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6706     ;; If we have several article buffers, we kill them at exit.
6707     (unless gnus-single-article-buffer
6708       (gnus-kill-buffer gnus-original-article-buffer)
6709       (setq gnus-article-current nil))
6710     (when gnus-use-cache
6711       (gnus-cache-possibly-remove-articles)
6712       (gnus-cache-save-buffers))
6713     (gnus-async-prefetch-remove-group group)
6714     (when gnus-suppress-duplicates
6715       (gnus-dup-enter-articles))
6716     (when gnus-use-trees
6717       (gnus-tree-close group))
6718     (when gnus-use-cache
6719       (gnus-cache-write-active))
6720     ;; Remove entries for this group.
6721     (nnmail-purge-split-history (gnus-group-real-name group))
6722     ;; Make all changes in this group permanent.
6723     (unless quit-config
6724       (gnus-run-hooks 'gnus-exit-group-hook)
6725       (gnus-summary-update-info))
6726     (gnus-close-group group)
6727     ;; Make sure where we were, and go to next newsgroup.
6728     (set-buffer gnus-group-buffer)
6729     (unless quit-config
6730       (gnus-group-jump-to-group group))
6731     (gnus-run-hooks 'gnus-summary-exit-hook)
6732     (unless (or quit-config
6733                 ;; If this group has disappeared from the summary
6734                 ;; buffer, don't skip forwards.
6735                 (not (string= group (gnus-group-group-name))))
6736       (gnus-group-next-unread-group 1))
6737     (setq group-point (point))
6738     (if temporary
6739         nil                             ;Nothing to do.
6740       ;; If we have several article buffers, we kill them at exit.
6741       (unless gnus-single-article-buffer
6742         (gnus-kill-buffer gnus-article-buffer)
6743         (gnus-kill-buffer gnus-original-article-buffer)
6744         (setq gnus-article-current nil))
6745       (set-buffer buf)
6746       (if (not gnus-kill-summary-on-exit)
6747           (progn
6748             (gnus-deaden-summary)
6749             (setq mode nil))
6750         ;; We set all buffer-local variables to nil.  It is unclear why
6751         ;; this is needed, but if we don't, buffer-local variables are
6752         ;; not garbage-collected, it seems.  This would the lead to en
6753         ;; ever-growing Emacs.
6754         (gnus-summary-clear-local-variables)
6755         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6756           (gnus-summary-clear-local-variables))
6757         (when (get-buffer gnus-article-buffer)
6758           (bury-buffer gnus-article-buffer))
6759         ;; Return to group mode buffer.
6760         (when (eq mode 'gnus-summary-mode)
6761           (gnus-kill-buffer buf)))
6762       (setq gnus-current-select-method gnus-select-method)
6763       (set-buffer gnus-group-buffer)
6764       (if quit-config
6765           (gnus-handle-ephemeral-exit quit-config)
6766         (goto-char group-point)
6767         ;; If gnus-group-buffer is already displayed, make sure we also move
6768         ;; the cursor in the window that displays it.
6769         (let ((win (get-buffer-window (current-buffer) 0)))
6770           (if win (set-window-point win (point))))
6771         (unless leave-hidden
6772           (gnus-configure-windows 'group 'force)))
6773       ;; Clear the current group name.
6774       (unless quit-config
6775         (setq gnus-newsgroup-name nil)))))
6776
6777 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6778 (defun gnus-summary-exit-no-update (&optional no-questions)
6779   "Quit reading current newsgroup without updating read article info."
6780   (interactive)
6781   (let* ((group gnus-newsgroup-name)
6782          (gnus-group-is-exiting-p t)
6783          (gnus-group-is-exiting-without-update-p t)
6784          (quit-config (gnus-group-quit-config group)))
6785     (when (or no-questions
6786               gnus-expert-user
6787               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6788       (gnus-async-halt-prefetch)
6789       (run-hooks 'gnus-summary-prepare-exit-hook)
6790       (when (gnus-buffer-live-p gnus-article-buffer)
6791         (save-excursion
6792           (set-buffer gnus-article-buffer)
6793           (mm-destroy-parts gnus-article-mime-handles)
6794           ;; Set it to nil for safety reason.
6795           (setq gnus-article-mime-handle-alist nil)
6796           (setq gnus-article-mime-handles nil)))
6797       ;; If we have several article buffers, we kill them at exit.
6798       (unless gnus-single-article-buffer
6799         (gnus-kill-buffer gnus-article-buffer)
6800         (gnus-kill-buffer gnus-original-article-buffer)
6801         (setq gnus-article-current nil))
6802       (if (not gnus-kill-summary-on-exit)
6803           (gnus-deaden-summary)
6804         (gnus-close-group group)
6805         (gnus-summary-clear-local-variables)
6806         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6807           (gnus-summary-clear-local-variables))
6808         (gnus-kill-buffer gnus-summary-buffer))
6809       (unless gnus-single-article-buffer
6810         (setq gnus-article-current nil))
6811       (when gnus-use-trees
6812         (gnus-tree-close group))
6813       (gnus-async-prefetch-remove-group group)
6814       (when (get-buffer gnus-article-buffer)
6815         (bury-buffer gnus-article-buffer))
6816       ;; Return to the group buffer.
6817       (gnus-configure-windows 'group 'force)
6818       ;; Clear the current group name.
6819       (setq gnus-newsgroup-name nil)
6820       (unless (gnus-ephemeral-group-p group)
6821         (gnus-group-update-group group))
6822       (when (equal (gnus-group-group-name) group)
6823         (gnus-group-next-unread-group 1))
6824       (when quit-config
6825         (gnus-handle-ephemeral-exit quit-config)))))
6826
6827 (defun gnus-handle-ephemeral-exit (quit-config)
6828   "Handle movement when leaving an ephemeral group.
6829 The state which existed when entering the ephemeral is reset."
6830   (if (not (buffer-name (car quit-config)))
6831       (gnus-configure-windows 'group 'force)
6832     (set-buffer (car quit-config))
6833     (cond ((eq major-mode 'gnus-summary-mode)
6834            (gnus-set-global-variables))
6835           ((eq major-mode 'gnus-article-mode)
6836            (save-excursion
6837              ;; The `gnus-summary-buffer' variable may point
6838              ;; to the old summary buffer when using a single
6839              ;; article buffer.
6840              (unless (gnus-buffer-live-p gnus-summary-buffer)
6841                (set-buffer gnus-group-buffer))
6842              (set-buffer gnus-summary-buffer)
6843              (gnus-set-global-variables))))
6844     (if (or (eq (cdr quit-config) 'article)
6845             (eq (cdr quit-config) 'pick))
6846         (progn
6847           ;; The current article may be from the ephemeral group
6848           ;; thus it is best that we reload this article
6849           ;;
6850           ;; If we're exiting from a large digest, this can be
6851           ;; extremely slow.  So, it's better not to reload it. -- jh.
6852           ;;(gnus-summary-show-article)
6853           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6854               (gnus-configure-windows 'pick 'force)
6855             (gnus-configure-windows (cdr quit-config) 'force)))
6856       (gnus-configure-windows (cdr quit-config) 'force))
6857     (when (eq major-mode 'gnus-summary-mode)
6858       (gnus-summary-next-subject 1 nil t)
6859       (gnus-summary-recenter)
6860       (gnus-summary-position-point))))
6861
6862 ;;; Dead summaries.
6863
6864 (defvar gnus-dead-summary-mode-map nil)
6865
6866 (unless gnus-dead-summary-mode-map
6867   (setq gnus-dead-summary-mode-map (make-keymap))
6868   (suppress-keymap gnus-dead-summary-mode-map)
6869   (substitute-key-definition
6870    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6871   (dolist (key '("\C-d" "\r" "\177" [delete]))
6872     (define-key gnus-dead-summary-mode-map
6873       key 'gnus-summary-wake-up-the-dead))
6874   (dolist (key '("q" "Q"))
6875     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6876
6877 (defvar gnus-dead-summary-mode nil
6878   "Minor mode for Gnus summary buffers.")
6879
6880 (defun gnus-dead-summary-mode (&optional arg)
6881   "Minor mode for Gnus summary buffers."
6882   (interactive "P")
6883   (when (eq major-mode 'gnus-summary-mode)
6884     (make-local-variable 'gnus-dead-summary-mode)
6885     (setq gnus-dead-summary-mode
6886           (if (null arg) (not gnus-dead-summary-mode)
6887             (> (prefix-numeric-value arg) 0)))
6888     (when gnus-dead-summary-mode
6889       (add-minor-mode
6890        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6891
6892 (defun gnus-deaden-summary ()
6893   "Make the current summary buffer into a dead summary buffer."
6894   ;; Kill any previous dead summary buffer.
6895   (when (and gnus-dead-summary
6896              (buffer-name gnus-dead-summary))
6897     (with-current-buffer gnus-dead-summary
6898       (when gnus-dead-summary-mode
6899         (kill-buffer (current-buffer)))))
6900   ;; Make this the current dead summary.
6901   (setq gnus-dead-summary (current-buffer))
6902   (gnus-dead-summary-mode 1)
6903   (let ((name (buffer-name)))
6904     (when (string-match "Summary" name)
6905       (rename-buffer
6906        (concat (substring name 0 (match-beginning 0)) "Dead "
6907                (substring name (match-beginning 0)))
6908        t)
6909       (bury-buffer))))
6910
6911 (defun gnus-kill-or-deaden-summary (buffer)
6912   "Kill or deaden the summary BUFFER."
6913   (save-excursion
6914     (when (and (buffer-name buffer)
6915                (not gnus-single-article-buffer))
6916       (with-current-buffer buffer
6917         (gnus-kill-buffer gnus-article-buffer)
6918         (gnus-kill-buffer gnus-original-article-buffer)))
6919     (cond
6920      ;; Kill the buffer.
6921      (gnus-kill-summary-on-exit
6922       (when (and gnus-use-trees
6923                  (gnus-buffer-exists-p buffer))
6924         (save-excursion
6925           (set-buffer buffer)
6926           (gnus-tree-close gnus-newsgroup-name)))
6927       (gnus-kill-buffer buffer))
6928      ;; Deaden the buffer.
6929      ((gnus-buffer-exists-p buffer)
6930       (save-excursion
6931         (set-buffer buffer)
6932         (gnus-deaden-summary))))))
6933
6934 (defun gnus-summary-wake-up-the-dead (&rest args)
6935   "Wake up the dead summary buffer."
6936   (interactive)
6937   (gnus-dead-summary-mode -1)
6938   (let ((name (buffer-name)))
6939     (when (string-match "Dead " name)
6940       (rename-buffer
6941        (concat (substring name 0 (match-beginning 0))
6942                (substring name (match-end 0)))
6943        t)))
6944   (gnus-message 3 "This dead summary is now alive again"))
6945
6946 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6947 (defun gnus-summary-fetch-faq (&optional faq-dir)
6948   "Fetch the FAQ for the current group.
6949 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6950 in."
6951   (interactive
6952    (list
6953     (when current-prefix-arg
6954       (completing-read
6955        "FAQ dir: " (and (listp gnus-group-faq-directory)
6956                         (mapcar 'list
6957                                 gnus-group-faq-directory))))))
6958   (let (gnus-faq-buffer)
6959     (when (setq gnus-faq-buffer
6960                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6961       (gnus-configure-windows 'summary-faq))))
6962
6963 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6964 (defun gnus-summary-describe-group (&optional force)
6965   "Describe the current newsgroup."
6966   (interactive "P")
6967   (gnus-group-describe-group force gnus-newsgroup-name))
6968
6969 (defun gnus-summary-describe-briefly ()
6970   "Describe summary mode commands briefly."
6971   (interactive)
6972   (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")))
6973
6974 ;; Walking around group mode buffer from summary mode.
6975
6976 (defun gnus-summary-next-group (&optional no-article target-group backward)
6977   "Exit current newsgroup and then select next unread newsgroup.
6978 If prefix argument NO-ARTICLE is non-nil, no article is selected
6979 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6980 previous group instead."
6981   (interactive "P")
6982   ;; Stop pre-fetching.
6983   (gnus-async-halt-prefetch)
6984   (let ((current-group gnus-newsgroup-name)
6985         (current-buffer (current-buffer))
6986         entered)
6987     ;; First we semi-exit this group to update Xrefs and all variables.
6988     ;; We can't do a real exit, because the window conf must remain
6989     ;; the same in case the user is prompted for info, and we don't
6990     ;; want the window conf to change before that...
6991     (gnus-summary-exit t)
6992     (while (not entered)
6993       ;; Then we find what group we are supposed to enter.
6994       (set-buffer gnus-group-buffer)
6995       (gnus-group-jump-to-group current-group)
6996       (setq target-group
6997             (or target-group
6998                 (if (eq gnus-keep-same-level 'best)
6999                     (gnus-summary-best-group gnus-newsgroup-name)
7000                   (gnus-summary-search-group backward gnus-keep-same-level))))
7001       (if (not target-group)
7002           ;; There are no further groups, so we return to the group
7003           ;; buffer.
7004           (progn
7005             (gnus-message 5 "Returning to the group buffer")
7006             (setq entered t)
7007             (when (gnus-buffer-live-p current-buffer)
7008               (set-buffer current-buffer)
7009               (gnus-summary-exit))
7010             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7011         ;; We try to enter the target group.
7012         (gnus-group-jump-to-group target-group)
7013         (let ((unreads (gnus-group-group-unread)))
7014           (if (and (or (eq t unreads)
7015                        (and unreads (not (zerop unreads))))
7016                    (gnus-summary-read-group
7017                     target-group nil no-article
7018                     (and (buffer-name current-buffer) current-buffer)
7019                     nil backward))
7020               (setq entered t)
7021             (setq current-group target-group
7022                   target-group nil)))))))
7023
7024 (defun gnus-summary-prev-group (&optional no-article)
7025   "Exit current newsgroup and then select previous unread newsgroup.
7026 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7027   (interactive "P")
7028   (gnus-summary-next-group no-article nil t))
7029
7030 ;; Walking around summary lines.
7031
7032 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7033   "Go to the first subject satisfying any non-nil constraint.
7034 If UNREAD is non-nil, the article should be unread.
7035 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7036 If UNSEEN is non-nil, the article should be unseen.
7037 Returns the article selected or nil if there are no matching articles."
7038   (interactive "P")
7039   (cond
7040    ;; Empty summary.
7041    ((null gnus-newsgroup-data)
7042     (gnus-message 3 "No articles in the group")
7043     nil)
7044    ;; Pick the first article.
7045    ((not (or unread undownloaded unseen))
7046     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7047     (gnus-data-number (car gnus-newsgroup-data)))
7048    ;; Find the first unread article.
7049    (t
7050     (let ((data gnus-newsgroup-data))
7051       (while (and data
7052                   (let ((num (gnus-data-number (car data))))
7053                     (or (memq num gnus-newsgroup-unfetched)
7054                         (not (or (and unread
7055                                       (memq num gnus-newsgroup-unreads))
7056                                  (and undownloaded
7057                                       (memq num gnus-newsgroup-undownloaded))
7058                                  (and unseen
7059                                       (memq num gnus-newsgroup-unseen)))))))
7060         (setq data (cdr data)))
7061       (prog1
7062           (if data
7063               (progn
7064                 (goto-char (gnus-data-pos (car data)))
7065                 (gnus-data-number (car data)))
7066             (gnus-message 3 "No more%s articles"
7067                           (let* ((r (when unread " unread"))
7068                                  (d (when undownloaded " undownloaded"))
7069                                  (s (when unseen " unseen"))
7070                                  (l (delq nil (list r d s))))
7071                             (cond ((= 3 (length l))
7072                                    (concat r "," d ", or" s))
7073                                   ((= 2 (length l))
7074                                    (concat (car l) ", or" (cadr l)))
7075                                   ((= 1 (length l))
7076                                    (car l))
7077                                   (t
7078                                    ""))))
7079             nil
7080             )
7081         (gnus-summary-position-point))))))
7082
7083 (defun gnus-summary-next-subject (n &optional unread dont-display)
7084   "Go to next N'th summary line.
7085 If N is negative, go to the previous N'th subject line.
7086 If UNREAD is non-nil, only unread articles are selected.
7087 The difference between N and the actual number of steps taken is
7088 returned."
7089   (interactive "p")
7090   (let ((backward (< n 0))
7091         (n (abs n)))
7092     (while (and (> n 0)
7093                 (if backward
7094                     (gnus-summary-find-prev unread)
7095                   (gnus-summary-find-next unread)))
7096       (unless (zerop (setq n (1- n)))
7097         (gnus-summary-show-thread)))
7098     (when (/= 0 n)
7099       (gnus-message 7 "No more%s articles"
7100                     (if unread " unread" "")))
7101     (unless dont-display
7102       (gnus-summary-recenter)
7103       (gnus-summary-position-point))
7104     n))
7105
7106 (defun gnus-summary-next-unread-subject (n)
7107   "Go to next N'th unread summary line."
7108   (interactive "p")
7109   (gnus-summary-next-subject n t))
7110
7111 (defun gnus-summary-prev-subject (n &optional unread)
7112   "Go to previous N'th summary line.
7113 If optional argument UNREAD is non-nil, only unread article is selected."
7114   (interactive "p")
7115   (gnus-summary-next-subject (- n) unread))
7116
7117 (defun gnus-summary-prev-unread-subject (n)
7118   "Go to previous N'th unread summary line."
7119   (interactive "p")
7120   (gnus-summary-next-subject (- n) t))
7121
7122 (defun gnus-summary-goto-subjects (articles)
7123   "Insert the subject header for ARTICLES in the current buffer."
7124   (save-excursion
7125     (dolist (article articles)
7126       (gnus-summary-goto-subject article t)))
7127   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7128   (gnus-summary-position-point))
7129
7130 (defun gnus-summary-goto-subject (article &optional force silent)
7131   "Go the subject line of ARTICLE.
7132 If FORCE, also allow jumping to articles not currently shown."
7133   (interactive "nArticle number: ")
7134   (unless (numberp article)
7135     (error "Article %s is not a number" article))
7136   (let ((b (point))
7137         (data (gnus-data-find article)))
7138     ;; We read in the article if we have to.
7139     (and (not data)
7140          force
7141          (gnus-summary-insert-subject
7142           article
7143           (if (or (numberp force) (vectorp force)) force)
7144           t)
7145          (setq data (gnus-data-find article)))
7146     (goto-char b)
7147     (if (not data)
7148         (progn
7149           (unless silent
7150             (gnus-message 3 "Can't find article %d" article))
7151           nil)
7152       (let ((pt (gnus-data-pos data)))
7153         (goto-char pt)
7154         (gnus-summary-set-article-display-arrow pt))
7155       (gnus-summary-position-point)
7156       article)))
7157
7158 ;; Walking around summary lines with displaying articles.
7159
7160 (defun gnus-summary-expand-window (&optional arg)
7161   "Make the summary buffer take up the entire Emacs frame.
7162 Given a prefix, will force an `article' buffer configuration."
7163   (interactive "P")
7164   (if arg
7165       (gnus-configure-windows 'article 'force)
7166     (gnus-configure-windows 'summary 'force)))
7167
7168 (defun gnus-summary-display-article (article &optional all-header)
7169   "Display ARTICLE in article buffer."
7170   (when (gnus-buffer-live-p gnus-article-buffer)
7171     (with-current-buffer gnus-article-buffer
7172       (mm-enable-multibyte)))
7173   (gnus-set-global-variables)
7174   (when (gnus-buffer-live-p gnus-article-buffer)
7175     (with-current-buffer gnus-article-buffer
7176       (setq gnus-article-charset gnus-newsgroup-charset)
7177       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7178       (mm-enable-multibyte)))
7179   (if (null article)
7180       nil
7181     (prog1
7182         (if gnus-summary-display-article-function
7183             (funcall gnus-summary-display-article-function article all-header)
7184           (gnus-article-prepare article all-header))
7185       (gnus-run-hooks 'gnus-select-article-hook)
7186       (when (and gnus-current-article
7187                  (not (zerop gnus-current-article)))
7188         (gnus-summary-goto-subject gnus-current-article))
7189       (gnus-summary-recenter)
7190       (when (and gnus-use-trees gnus-show-threads)
7191         (gnus-possibly-generate-tree article)
7192         (gnus-highlight-selected-tree article))
7193       ;; Successfully display article.
7194       (gnus-article-set-window-start
7195        (cdr (assq article gnus-newsgroup-bookmarks))))))
7196
7197 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7198   "Select the current article.
7199 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7200 non-nil, the article will be re-fetched even if it already present in
7201 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7202 be displayed."
7203   ;; Make sure we are in the summary buffer to work around bbdb bug.
7204   (unless (eq major-mode 'gnus-summary-mode)
7205     (set-buffer gnus-summary-buffer))
7206   (let ((article (or article (gnus-summary-article-number)))
7207         (all-headers (not (not all-headers))) ;Must be t or nil.
7208         gnus-summary-display-article-function)
7209     (and (not pseudo)
7210          (gnus-summary-article-pseudo-p article)
7211          (error "This is a pseudo-article"))
7212     (save-excursion
7213       (set-buffer gnus-summary-buffer)
7214       (if (or (and gnus-single-article-buffer
7215                    (or (null gnus-current-article)
7216                        (null gnus-article-current)
7217                        (null (get-buffer gnus-article-buffer))
7218                        (not (eq article (cdr gnus-article-current)))
7219                        (not (equal (car gnus-article-current)
7220                                    gnus-newsgroup-name))))
7221               (and (not gnus-single-article-buffer)
7222                    (or (null gnus-current-article)
7223                        (not (eq gnus-current-article article))))
7224               force)
7225           ;; The requested article is different from the current article.
7226           (progn
7227             (gnus-summary-display-article article all-headers)
7228             (when (gnus-buffer-live-p gnus-article-buffer)
7229               (with-current-buffer gnus-article-buffer
7230                 (if (not gnus-article-decoded-p) ;; a local variable
7231                     (mm-disable-multibyte))))
7232             (gnus-article-set-window-start
7233              (cdr (assq article gnus-newsgroup-bookmarks)))
7234             article)
7235         'old))))
7236
7237 (defun gnus-summary-force-verify-and-decrypt ()
7238   "Display buttons for signed/encrypted parts and verify/decrypt them."
7239   (interactive)
7240   (let ((mm-verify-option 'known)
7241         (mm-decrypt-option 'known)
7242         (gnus-article-emulate-mime t)
7243         (gnus-buttonized-mime-types (append (list "multipart/signed"
7244                                                   "multipart/encrypted")
7245                                             gnus-buttonized-mime-types)))
7246     (gnus-summary-select-article nil 'force)))
7247
7248 (defun gnus-summary-set-current-mark (&optional current-mark)
7249   "Obsolete function."
7250   nil)
7251
7252 (defun gnus-summary-next-article (&optional unread subject backward push)
7253   "Select the next article.
7254 If UNREAD, only unread articles are selected.
7255 If SUBJECT, only articles with SUBJECT are selected.
7256 If BACKWARD, the previous article is selected instead of the next."
7257   (interactive "P")
7258   (cond
7259    ;; Is there such an article?
7260    ((and (gnus-summary-search-forward unread subject backward)
7261          (or (gnus-summary-display-article (gnus-summary-article-number))
7262              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7263     (gnus-summary-position-point))
7264    ;; If not, we try the first unread, if that is wanted.
7265    ((and subject
7266          gnus-auto-select-same
7267          (gnus-summary-first-unread-article))
7268     (gnus-summary-position-point)
7269     (gnus-message 6 "Wrapped"))
7270    ;; Try to get next/previous article not displayed in this group.
7271    ((and gnus-auto-extend-newsgroup
7272          (not unread) (not subject))
7273     (gnus-summary-goto-article
7274      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7275      nil (count-lines (point-min) (point))))
7276    ;; Go to next/previous group.
7277    (t
7278     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7279       (gnus-summary-jump-to-group gnus-newsgroup-name))
7280     (let ((cmd last-command-char)
7281           (point
7282            (with-current-buffer gnus-group-buffer
7283              (point)))
7284           (group
7285            (if (eq gnus-keep-same-level 'best)
7286                (gnus-summary-best-group gnus-newsgroup-name)
7287              (gnus-summary-search-group backward gnus-keep-same-level))))
7288       ;; For some reason, the group window gets selected.  We change
7289       ;; it back.
7290       (select-window (get-buffer-window (current-buffer)))
7291       ;; Select next unread newsgroup automagically.
7292       (cond
7293        ((or (not gnus-auto-select-next)
7294             (not cmd))
7295         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7296        ((or (eq gnus-auto-select-next 'quietly)
7297             (and (eq gnus-auto-select-next 'slightly-quietly)
7298                  push)
7299             (and (eq gnus-auto-select-next 'almost-quietly)
7300                  (gnus-summary-last-article-p)))
7301         ;; Select quietly.
7302         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7303             (gnus-summary-exit)
7304           (gnus-message 7 "No more%s articles (%s)..."
7305                         (if unread " unread" "")
7306                         (if group (concat "selecting " group)
7307                           "exiting"))
7308           (gnus-summary-next-group nil group backward)))
7309        (t
7310         (when (gnus-key-press-event-p last-input-event)
7311           (gnus-summary-walk-group-buffer
7312            gnus-newsgroup-name cmd unread backward point))))))))
7313
7314 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7315   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7316                       (?\C-p (gnus-group-prev-unread-group 1))))
7317         (cursor-in-echo-area t)
7318         keve key group ended prompt)
7319     (save-excursion
7320       (set-buffer gnus-group-buffer)
7321       (goto-char start)
7322       (setq group
7323             (if (eq gnus-keep-same-level 'best)
7324                 (gnus-summary-best-group gnus-newsgroup-name)
7325               (gnus-summary-search-group backward gnus-keep-same-level))))
7326     (while (not ended)
7327       (setq prompt
7328             (format
7329              "No more%s articles%s " (if unread " unread" "")
7330              (if (and group
7331                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7332                  (format " (Type %s for %s [%s])"
7333                          (single-key-description cmd)
7334                          (gnus-group-decoded-name group)
7335                          (gnus-group-unread group))
7336                (format " (Type %s to exit %s)"
7337                        (single-key-description cmd)
7338                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7339       ;; Confirm auto selection.
7340       (setq key (car (setq keve (gnus-read-event-char prompt)))
7341             ended t)
7342       (cond
7343        ((assq key keystrokes)
7344         (let ((obuf (current-buffer)))
7345           (switch-to-buffer gnus-group-buffer)
7346           (when group
7347             (gnus-group-jump-to-group group))
7348           (eval (cadr (assq key keystrokes)))
7349           (setq group (gnus-group-group-name))
7350           (switch-to-buffer obuf))
7351         (setq ended nil))
7352        ((equal key cmd)
7353         (if (or (not group)
7354                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7355             (gnus-summary-exit)
7356           (gnus-summary-next-group nil group backward)))
7357        (t
7358         (push (cdr keve) unread-command-events))))))
7359
7360 (defun gnus-summary-next-unread-article ()
7361   "Select unread article after current one."
7362   (interactive)
7363   (gnus-summary-next-article
7364    (or (not (eq gnus-summary-goto-unread 'never))
7365        (gnus-summary-last-article-p (gnus-summary-article-number)))
7366    (and gnus-auto-select-same
7367         (gnus-summary-article-subject))))
7368
7369 (defun gnus-summary-prev-article (&optional unread subject)
7370   "Select the article after the current one.
7371 If UNREAD is non-nil, only unread articles are selected."
7372   (interactive "P")
7373   (gnus-summary-next-article unread subject t))
7374
7375 (defun gnus-summary-prev-unread-article ()
7376   "Select unread article before current one."
7377   (interactive)
7378   (gnus-summary-prev-article
7379    (or (not (eq gnus-summary-goto-unread 'never))
7380        (gnus-summary-first-article-p (gnus-summary-article-number)))
7381    (and gnus-auto-select-same
7382         (gnus-summary-article-subject))))
7383
7384 (defun gnus-summary-next-page (&optional lines circular stop)
7385   "Show next page of the selected article.
7386 If at the end of the current article, select the next article.
7387 LINES says how many lines should be scrolled up.
7388
7389 If CIRCULAR is non-nil, go to the start of the article instead of
7390 selecting the next article when reaching the end of the current
7391 article.
7392
7393 If STOP is non-nil, just stop when reaching the end of the message.
7394
7395 Also see the variable `gnus-article-skip-boring'."
7396   (interactive "P")
7397   (setq gnus-summary-buffer (current-buffer))
7398   (gnus-set-global-variables)
7399   (let ((article (gnus-summary-article-number))
7400         (article-window (get-buffer-window gnus-article-buffer t))
7401         endp)
7402     ;; If the buffer is empty, we have no article.
7403     (unless article
7404       (error "No article to select"))
7405     (gnus-configure-windows 'article)
7406     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7407         (if (and (eq gnus-summary-goto-unread 'never)
7408                  (not (gnus-summary-last-article-p article)))
7409             (gnus-summary-next-article)
7410           (gnus-summary-next-unread-article))
7411       (if (or (null gnus-current-article)
7412               (null gnus-article-current)
7413               (/= article (cdr gnus-article-current))
7414               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7415           ;; Selected subject is different from current article's.
7416           (gnus-summary-display-article article)
7417         (when article-window
7418           (gnus-eval-in-buffer-window gnus-article-buffer
7419             (setq endp (or (gnus-article-next-page lines)
7420                            (gnus-article-only-boring-p))))
7421           (when endp
7422             (cond (stop
7423                    (gnus-message 3 "End of message"))
7424                   (circular
7425                    (gnus-summary-beginning-of-article))
7426                   (lines
7427                    (gnus-message 3 "End of message"))
7428                   ((null lines)
7429                    (if (and (eq gnus-summary-goto-unread 'never)
7430                             (not (gnus-summary-last-article-p article)))
7431                        (gnus-summary-next-article)
7432                      (gnus-summary-next-unread-article))))))))
7433     (gnus-summary-recenter)
7434     (gnus-summary-position-point)))
7435
7436 (defun gnus-summary-prev-page (&optional lines move)
7437   "Show previous page of selected article.
7438 Argument LINES specifies lines to be scrolled down.
7439 If MOVE, move to the previous unread article if point is at
7440 the beginning of the buffer."
7441   (interactive "P")
7442   (let ((article (gnus-summary-article-number))
7443         (article-window (get-buffer-window gnus-article-buffer t))
7444         endp)
7445     (gnus-configure-windows 'article)
7446     (if (or (null gnus-current-article)
7447             (null gnus-article-current)
7448             (/= article (cdr gnus-article-current))
7449             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7450         ;; Selected subject is different from current article's.
7451         (gnus-summary-display-article article)
7452       (gnus-summary-recenter)
7453       (when article-window
7454         (gnus-eval-in-buffer-window gnus-article-buffer
7455           (setq endp (gnus-article-prev-page lines)))
7456         (when (and move endp)
7457           (cond (lines
7458                  (gnus-message 3 "Beginning of message"))
7459                 ((null lines)
7460                  (if (and (eq gnus-summary-goto-unread 'never)
7461                           (not (gnus-summary-first-article-p article)))
7462                      (gnus-summary-prev-article)
7463                    (gnus-summary-prev-unread-article))))))))
7464   (gnus-summary-position-point))
7465
7466 (defun gnus-summary-prev-page-or-article (&optional lines)
7467   "Show previous page of selected article.
7468 Argument LINES specifies lines to be scrolled down.
7469 If at the beginning of the article, go to the next article."
7470   (interactive "P")
7471   (gnus-summary-prev-page lines t))
7472
7473 (defun gnus-summary-scroll-up (lines)
7474   "Scroll up (or down) one line current article.
7475 Argument LINES specifies lines to be scrolled up (or down if negative)."
7476   (interactive "p")
7477   (gnus-configure-windows 'article)
7478   (gnus-summary-show-thread)
7479   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7480     (gnus-eval-in-buffer-window gnus-article-buffer
7481       (cond ((> lines 0)
7482              (when (gnus-article-next-page lines)
7483                (gnus-message 3 "End of message")))
7484             ((< lines 0)
7485              (gnus-article-prev-page (- lines))))))
7486   (gnus-summary-recenter)
7487   (gnus-summary-position-point))
7488
7489 (defun gnus-summary-scroll-down (lines)
7490   "Scroll down (or up) one line current article.
7491 Argument LINES specifies lines to be scrolled down (or up if negative)."
7492   (interactive "p")
7493   (gnus-summary-scroll-up (- lines)))
7494
7495 (defun gnus-summary-next-same-subject ()
7496   "Select next article which has the same subject as current one."
7497   (interactive)
7498   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7499
7500 (defun gnus-summary-prev-same-subject ()
7501   "Select previous article which has the same subject as current one."
7502   (interactive)
7503   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7504
7505 (defun gnus-summary-next-unread-same-subject ()
7506   "Select next unread article which has the same subject as current one."
7507   (interactive)
7508   (gnus-summary-next-article t (gnus-summary-article-subject)))
7509
7510 (defun gnus-summary-prev-unread-same-subject ()
7511   "Select previous unread article which has the same subject as current one."
7512   (interactive)
7513   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7514
7515 (defun gnus-summary-first-unread-article ()
7516   "Select the first unread article.
7517 Return nil if there are no unread articles."
7518   (interactive)
7519   (prog1
7520       (when (gnus-summary-first-subject t)
7521         (gnus-summary-show-thread)
7522         (gnus-summary-first-subject t)
7523         (gnus-summary-display-article (gnus-summary-article-number)))
7524     (gnus-summary-position-point)))
7525
7526 (defun gnus-summary-first-unread-subject ()
7527   "Place the point on the subject line of the first unread article.
7528 Return nil if there are no unread articles."
7529   (interactive)
7530   (prog1
7531       (when (gnus-summary-first-subject t)
7532         (gnus-summary-show-thread)
7533         (gnus-summary-first-subject t))
7534     (gnus-summary-position-point)))
7535
7536 (defun gnus-summary-first-unseen-subject ()
7537   "Place the point on the subject line of the first unseen article.
7538 Return nil if there are no unseen articles."
7539   (interactive)
7540   (prog1
7541       (when (gnus-summary-first-subject nil nil t)
7542         (gnus-summary-show-thread)
7543         (gnus-summary-first-subject nil nil t))
7544     (gnus-summary-position-point)))
7545
7546 (defun gnus-summary-first-unseen-or-unread-subject ()
7547   "Place the point on the subject line of the first unseen article or,
7548 if all article have been seen, on the subject line of the first unread
7549 article."
7550   (interactive)
7551   (prog1
7552       (unless (when (gnus-summary-first-subject nil nil t)
7553                 (gnus-summary-show-thread)
7554                 (gnus-summary-first-subject nil nil t))
7555         (when (gnus-summary-first-subject t)
7556           (gnus-summary-show-thread)
7557           (gnus-summary-first-subject t)))
7558     (gnus-summary-position-point)))
7559
7560 (defun gnus-summary-first-article ()
7561   "Select the first article.
7562 Return nil if there are no articles."
7563   (interactive)
7564   (prog1
7565       (when (gnus-summary-first-subject)
7566         (gnus-summary-show-thread)
7567         (gnus-summary-first-subject)
7568         (gnus-summary-display-article (gnus-summary-article-number)))
7569     (gnus-summary-position-point)))
7570
7571 (defun gnus-summary-best-unread-article (&optional arg)
7572   "Select the unread article with the highest score.
7573 If given a prefix argument, select the next unread article that has a
7574 score higher than the default score."
7575   (interactive "P")
7576   (let ((article (if arg
7577                      (gnus-summary-better-unread-subject)
7578                    (gnus-summary-best-unread-subject))))
7579     (if article
7580         (gnus-summary-goto-article article)
7581       (error "No unread articles"))))
7582
7583 (defun gnus-summary-best-unread-subject ()
7584   "Select the unread subject with the highest score."
7585   (interactive)
7586   (let ((best -1000000)
7587         (data gnus-newsgroup-data)
7588         article score)
7589     (while data
7590       (and (gnus-data-unread-p (car data))
7591            (> (setq score
7592                     (gnus-summary-article-score (gnus-data-number (car data))))
7593               best)
7594            (setq best score
7595                  article (gnus-data-number (car data))))
7596       (setq data (cdr data)))
7597     (when article
7598       (gnus-summary-goto-subject article))
7599     (gnus-summary-position-point)
7600     article))
7601
7602 (defun gnus-summary-better-unread-subject ()
7603   "Select the first unread subject that has a score over the default score."
7604   (interactive)
7605   (let ((data gnus-newsgroup-data)
7606         article score)
7607     (while (and (setq article (gnus-data-number (car data)))
7608                 (or (gnus-data-read-p (car data))
7609                     (not (> (gnus-summary-article-score article)
7610                             gnus-summary-default-score))))
7611       (setq data (cdr data)))
7612     (when article
7613       (gnus-summary-goto-subject article))
7614     (gnus-summary-position-point)
7615     article))
7616
7617 (defun gnus-summary-last-subject ()
7618   "Go to the last displayed subject line in the group."
7619   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7620     (when article
7621       (gnus-summary-goto-subject article))))
7622
7623 (defun gnus-summary-goto-article (article &optional all-headers force)
7624   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7625 If ALL-HEADERS is non-nil, no header lines are hidden.
7626 If FORCE, go to the article even if it isn't displayed.  If FORCE
7627 is a number, it is the line the article is to be displayed on."
7628   (interactive
7629    (list
7630     (completing-read
7631      "Article number or Message-ID: "
7632      (mapcar (lambda (number) (list (int-to-string number)))
7633              gnus-newsgroup-limit))
7634     current-prefix-arg
7635     t))
7636   (prog1
7637       (if (and (stringp article)
7638                (string-match "@\\|%40" article))
7639           (gnus-summary-refer-article article)
7640         (when (stringp article)
7641           (setq article (string-to-number article)))
7642         (if (gnus-summary-goto-subject article force)
7643             (gnus-summary-display-article article all-headers)
7644           (gnus-message 4 "Couldn't go to article %s" article) nil))
7645     (gnus-summary-position-point)))
7646
7647 (defun gnus-summary-goto-last-article ()
7648   "Go to the previously read article."
7649   (interactive)
7650   (prog1
7651       (when gnus-last-article
7652         (gnus-summary-goto-article gnus-last-article nil t))
7653     (gnus-summary-position-point)))
7654
7655 (defun gnus-summary-pop-article (number)
7656   "Pop one article off the history and go to the previous.
7657 NUMBER articles will be popped off."
7658   (interactive "p")
7659   (let (to)
7660     (setq gnus-newsgroup-history
7661           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7662     (if to
7663         (gnus-summary-goto-article (car to) nil t)
7664       (error "Article history empty")))
7665   (gnus-summary-position-point))
7666
7667 ;; Summary commands and functions for limiting the summary buffer.
7668
7669 (defun gnus-summary-limit-to-articles (n)
7670   "Limit the summary buffer to the next N articles.
7671 If not given a prefix, use the process marked articles instead."
7672   (interactive "P")
7673   (prog1
7674       (let ((articles (gnus-summary-work-articles n)))
7675         (setq gnus-newsgroup-processable nil)
7676         (gnus-summary-limit articles))
7677     (gnus-summary-position-point)))
7678
7679 (defun gnus-summary-pop-limit (&optional total)
7680   "Restore the previous limit.
7681 If given a prefix, remove all limits."
7682   (interactive "P")
7683   (when total
7684     (setq gnus-newsgroup-limits
7685           (list (mapcar (lambda (h) (mail-header-number h))
7686                         gnus-newsgroup-headers))))
7687   (unless gnus-newsgroup-limits
7688     (error "No limit to pop"))
7689   (prog1
7690       (gnus-summary-limit nil 'pop)
7691     (gnus-summary-position-point)))
7692
7693 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7694   "Limit the summary buffer to articles that have subjects that match a regexp.
7695 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7696   (interactive
7697    (list (read-string (if current-prefix-arg
7698                           "Exclude subject (regexp): "
7699                         "Limit to subject (regexp): "))
7700          nil current-prefix-arg))
7701   (unless header
7702     (setq header "subject"))
7703   (when (not (equal "" subject))
7704     (prog1
7705         (let ((articles (gnus-summary-find-matching
7706                          (or header "subject") subject 'all nil nil
7707                          not-matching)))
7708           (unless articles
7709             (error "Found no matches for \"%s\"" subject))
7710           (gnus-summary-limit articles))
7711       (gnus-summary-position-point))))
7712
7713 (defun gnus-summary-limit-to-author (from &optional not-matching)
7714   "Limit the summary buffer to articles that have authors that match a regexp.
7715 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7716   (interactive
7717    (list (read-string (if current-prefix-arg
7718                           "Exclude author (regexp): "
7719                         "Limit to author (regexp): "))
7720          current-prefix-arg))
7721   (gnus-summary-limit-to-subject from "from" not-matching))
7722
7723 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7724   "Limit the summary buffer to articles with the given RECIPIENT.
7725
7726 If NOT-MATCHING, exclude RECIPIENT.
7727
7728 To and Cc headers are checked.  You need to include them in
7729 `nnmail-extra-headers'."
7730   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7731   (interactive
7732    (list (read-string (format "%s recipient (regexp): "
7733                               (if current-prefix-arg "Exclude" "Limit to")))
7734          current-prefix-arg))
7735   (when (not (equal "" recipient))
7736     (prog1 (let* ((to
7737                    (if (memq 'To nnmail-extra-headers)
7738                        (gnus-summary-find-matching
7739                         (cons 'extra 'To) recipient 'all nil nil
7740                         not-matching)
7741                      (gnus-message
7742                       1 "`To' isn't present in `nnmail-extra-headers'")
7743                      (sit-for 1)
7744                      nil))
7745                   (cc
7746                    (if (memq 'Cc nnmail-extra-headers)
7747                        (gnus-summary-find-matching
7748                         (cons 'extra 'Cc) recipient 'all nil nil
7749                         not-matching)
7750                      (gnus-message
7751                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7752                      (sit-for 1)
7753                      nil))
7754                   (articles
7755                    (if not-matching
7756                        ;; We need the numbers that are in both lists:
7757                        (mapcar (lambda (a)
7758                                  (and (memq a to) a))
7759                                cc)
7760                      (nconc to cc))))
7761              (unless articles
7762                (error "Found no matches for \"%s\"" recipient))
7763              (gnus-summary-limit articles))
7764       (gnus-summary-position-point))))
7765
7766 (defun gnus-summary-limit-to-age (age &optional younger-p)
7767   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7768 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7769 articles that are younger than AGE days."
7770   (interactive
7771    (let ((younger current-prefix-arg)
7772          (days-got nil)
7773          days)
7774      (while (not days-got)
7775        (setq days (if younger
7776                       (read-string "Limit to articles younger than (in days, older when negative): ")
7777                     (read-string
7778                      "Limit to articles older than (in days, younger when negative): ")))
7779        (when (> (length days) 0)
7780          (setq days (read days)))
7781        (if (numberp days)
7782            (progn
7783              (setq days-got t)
7784              (when (< days 0)
7785                (setq younger (not younger))
7786                (setq days (* days -1))))
7787          (message "Please enter a number.")
7788          (sleep-for 1)))
7789      (list days younger)))
7790   (prog1
7791       (let ((data gnus-newsgroup-data)
7792             (cutoff (days-to-time age))
7793             articles d date is-younger)
7794         (while (setq d (pop data))
7795           (when (and (vectorp (gnus-data-header d))
7796                      (setq date (mail-header-date (gnus-data-header d))))
7797             (setq is-younger (time-less-p
7798                               (time-since (condition-case ()
7799                                               (date-to-time date)
7800                                             (error '(0 0))))
7801                               cutoff))
7802             (when (if younger-p
7803                       is-younger
7804                     (not is-younger))
7805               (push (gnus-data-number d) articles))))
7806         (gnus-summary-limit (nreverse articles)))
7807     (gnus-summary-position-point)))
7808
7809 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7810   "Limit the summary buffer to articles that match an 'extra' header."
7811   (interactive
7812    (let ((header
7813           (intern
7814            (gnus-completing-read-with-default
7815             (symbol-name (car gnus-extra-headers))
7816             (if current-prefix-arg
7817                 "Exclude extra header"
7818               "Limit extra header")
7819             (mapcar (lambda (x)
7820                       (cons (symbol-name x) x))
7821                     gnus-extra-headers)
7822             nil
7823             t))))
7824      (list header
7825            (read-string (format "%s header %s (regexp): "
7826                                 (if current-prefix-arg "Exclude" "Limit to")
7827                                 header))
7828            current-prefix-arg)))
7829   (when (not (equal "" regexp))
7830     (prog1
7831         (let ((articles (gnus-summary-find-matching
7832                          (cons 'extra header) regexp 'all nil nil
7833                          not-matching)))
7834           (unless articles
7835             (error "Found no matches for \"%s\"" regexp))
7836           (gnus-summary-limit articles))
7837       (gnus-summary-position-point))))
7838
7839 (defun gnus-summary-limit-to-display-predicate ()
7840   "Limit the summary buffer to the predicated in the `display' group parameter."
7841   (interactive)
7842   (unless gnus-newsgroup-display
7843     (error "There is no `display' group parameter"))
7844   (let (articles)
7845     (dolist (number gnus-newsgroup-articles)
7846       (when (funcall gnus-newsgroup-display)
7847         (push number articles)))
7848     (gnus-summary-limit articles))
7849   (gnus-summary-position-point))
7850
7851 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7852 (make-obsolete
7853  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7854
7855 (defun gnus-summary-limit-to-unread (&optional all)
7856   "Limit the summary buffer to articles that are not marked as read.
7857 If ALL is non-nil, limit strictly to unread articles."
7858   (interactive "P")
7859   (if all
7860       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7861     (gnus-summary-limit-to-marks
7862      ;; Concat all the marks that say that an article is read and have
7863      ;; those removed.
7864      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7865            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7866            gnus-low-score-mark gnus-expirable-mark
7867            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7868            gnus-duplicate-mark gnus-souped-mark)
7869      'reverse)))
7870
7871 (defun gnus-summary-limit-to-replied (&optional unreplied)
7872   "Limit the summary buffer to replied articles.
7873 If UNREPLIED (the prefix), limit to unreplied articles."
7874   (interactive "P")
7875   (if unreplied
7876       (gnus-summary-limit
7877        (gnus-set-difference gnus-newsgroup-articles
7878         gnus-newsgroup-replied))
7879     (gnus-summary-limit gnus-newsgroup-replied))
7880   (gnus-summary-position-point))
7881
7882 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7883 (make-obsolete 'gnus-summary-delete-marked-with
7884                'gnus-summary-limit-exclude-marks)
7885
7886 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7887   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7888 If REVERSE, limit the summary buffer to articles that are marked
7889 with MARKS.  MARKS can either be a string of marks or a list of marks.
7890 Returns how many articles were removed."
7891   (interactive "sMarks: ")
7892   (gnus-summary-limit-to-marks marks t))
7893
7894 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7895   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7896 If REVERSE (the prefix), limit the summary buffer to articles that are
7897 not marked with MARKS.  MARKS can either be a string of marks or a
7898 list of marks.
7899 Returns how many articles were removed."
7900   (interactive "sMarks: \nP")
7901   (prog1
7902       (let ((data gnus-newsgroup-data)
7903             (marks (if (listp marks) marks
7904                      (append marks nil))) ; Transform to list.
7905             articles)
7906         (while data
7907           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7908                   (memq (gnus-data-mark (car data)) marks))
7909             (push (gnus-data-number (car data)) articles))
7910           (setq data (cdr data)))
7911         (gnus-summary-limit articles))
7912     (gnus-summary-position-point)))
7913
7914 (defun gnus-summary-limit-to-score (score)
7915   "Limit to articles with score at or above SCORE."
7916   (interactive "NLimit to articles with score of at least: ")
7917   (let ((data gnus-newsgroup-data)
7918         articles)
7919     (while data
7920       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7921                 score)
7922         (push (gnus-data-number (car data)) articles))
7923       (setq data (cdr data)))
7924     (prog1
7925         (gnus-summary-limit articles)
7926       (gnus-summary-position-point))))
7927
7928 (defun gnus-summary-limit-to-unseen ()
7929   "Limit to unseen articles."
7930   (interactive)
7931   (prog1
7932       (gnus-summary-limit gnus-newsgroup-unseen)
7933     (gnus-summary-position-point)))
7934
7935 (defun gnus-summary-limit-include-thread (id)
7936   "Display all the hidden articles that is in the thread with ID in it.
7937 When called interactively, ID is the Message-ID of the current
7938 article."
7939   (interactive (list (mail-header-id (gnus-summary-article-header))))
7940   (let ((articles (gnus-articles-in-thread
7941                    (gnus-id-to-thread (gnus-root-id id)))))
7942     (prog1
7943         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7944       (gnus-summary-limit-include-matching-articles
7945        "subject"
7946        (regexp-quote (gnus-simplify-subject-re
7947                       (mail-header-subject (gnus-id-to-header id)))))
7948       (gnus-summary-position-point))))
7949
7950 (defun gnus-summary-limit-include-matching-articles (header regexp)
7951   "Display all the hidden articles that have HEADERs that match REGEXP."
7952   (interactive (list (read-string "Match on header: ")
7953                      (read-string "Regexp: ")))
7954   (let ((articles (gnus-find-matching-articles header regexp)))
7955     (prog1
7956         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7957       (gnus-summary-position-point))))
7958
7959 (defun gnus-summary-insert-dormant-articles ()
7960   "Insert all the dormant articles for this group into the current buffer."
7961   (interactive)
7962   (let ((gnus-verbose (max 6 gnus-verbose)))
7963     (if (not gnus-newsgroup-dormant)
7964         (gnus-message 3 "No cached articles for this group")
7965       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7966
7967 (defun gnus-summary-limit-include-dormant ()
7968   "Display all the hidden articles that are marked as dormant.
7969 Note that this command only works on a subset of the articles currently
7970 fetched for this group."
7971   (interactive)
7972   (unless gnus-newsgroup-dormant
7973     (error "There are no dormant articles in this group"))
7974   (prog1
7975       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7976     (gnus-summary-position-point)))
7977
7978 (defun gnus-summary-limit-exclude-dormant ()
7979   "Hide all dormant articles."
7980   (interactive)
7981   (prog1
7982       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7983     (gnus-summary-position-point)))
7984
7985 (defun gnus-summary-limit-exclude-childless-dormant ()
7986   "Hide all dormant articles that have no children."
7987   (interactive)
7988   (let ((data (gnus-data-list t))
7989         articles d children)
7990     ;; Find all articles that are either not dormant or have
7991     ;; children.
7992     (while (setq d (pop data))
7993       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7994                 (and (setq children
7995                            (gnus-article-children (gnus-data-number d)))
7996                      (let (found)
7997                        (while children
7998                          (when (memq (car children) articles)
7999                            (setq children nil
8000                                  found t))
8001                          (pop children))
8002                        found)))
8003         (push (gnus-data-number d) articles)))
8004     ;; Do the limiting.
8005     (prog1
8006         (gnus-summary-limit articles)
8007       (gnus-summary-position-point))))
8008
8009 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8010   "Mark all unread excluded articles as read.
8011 If ALL, mark even excluded ticked and dormants as read."
8012   (interactive "P")
8013   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8014   (let ((articles (gnus-sorted-ndifference
8015                    (sort
8016                     (mapcar (lambda (h) (mail-header-number h))
8017                             gnus-newsgroup-headers)
8018                     '<)
8019                    gnus-newsgroup-limit))
8020         article)
8021     (setq gnus-newsgroup-unreads
8022           (gnus-sorted-intersection gnus-newsgroup-unreads
8023                                     gnus-newsgroup-limit))
8024     (if all
8025         (setq gnus-newsgroup-dormant nil
8026               gnus-newsgroup-marked nil
8027               gnus-newsgroup-reads
8028               (nconc
8029                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8030                gnus-newsgroup-reads))
8031       (while (setq article (pop articles))
8032         (unless (or (memq article gnus-newsgroup-dormant)
8033                     (memq article gnus-newsgroup-marked))
8034           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8035
8036 (defun gnus-summary-limit (articles &optional pop)
8037   (if pop
8038       ;; We pop the previous limit off the stack and use that.
8039       (setq articles (car gnus-newsgroup-limits)
8040             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8041     ;; We use the new limit, so we push the old limit on the stack.
8042     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8043   ;; Set the limit.
8044   (setq gnus-newsgroup-limit articles)
8045   (let ((total (length gnus-newsgroup-data))
8046         (data (gnus-data-find-list (gnus-summary-article-number)))
8047         (gnus-summary-mark-below nil)   ; Inhibit this.
8048         found)
8049     ;; This will do all the work of generating the new summary buffer
8050     ;; according to the new limit.
8051     (gnus-summary-prepare)
8052     ;; Hide any threads, possibly.
8053     (gnus-summary-maybe-hide-threads)
8054     ;; Try to return to the article you were at, or one in the
8055     ;; neighborhood.
8056     (when data
8057       ;; We try to find some article after the current one.
8058       (while data
8059         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8060           (setq data nil
8061                 found t))
8062         (setq data (cdr data))))
8063     (unless found
8064       ;; If there is no data, that means that we were after the last
8065       ;; article.  The same goes when we can't find any articles
8066       ;; after the current one.
8067       (goto-char (point-max))
8068       (gnus-summary-find-prev))
8069     (gnus-set-mode-line 'summary)
8070     ;; We return how many articles were removed from the summary
8071     ;; buffer as a result of the new limit.
8072     (- total (length gnus-newsgroup-data))))
8073
8074 (defsubst gnus-invisible-cut-children (threads)
8075   (let ((num 0))
8076     (while threads
8077       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8078         (incf num))
8079       (pop threads))
8080     (< num 2)))
8081
8082 (defsubst gnus-cut-thread (thread)
8083   "Go forwards in the thread until we find an article that we want to display."
8084   (when (or (eq gnus-fetch-old-headers 'some)
8085             (eq gnus-fetch-old-headers 'invisible)
8086             (numberp gnus-fetch-old-headers)
8087             (eq gnus-build-sparse-threads 'some)
8088             (eq gnus-build-sparse-threads 'more))
8089     ;; Deal with old-fetched headers and sparse threads.
8090     (while (and
8091             thread
8092             (or
8093              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8094              (gnus-summary-article-ancient-p
8095               (mail-header-number (car thread))))
8096             (if (or (<= (length (cdr thread)) 1)
8097                     (eq gnus-fetch-old-headers 'invisible))
8098                 (setq gnus-newsgroup-limit
8099                       (delq (mail-header-number (car thread))
8100                             gnus-newsgroup-limit)
8101                       thread (cadr thread))
8102               (when (gnus-invisible-cut-children (cdr thread))
8103                 (let ((th (cdr thread)))
8104                   (while th
8105                     (if (memq (mail-header-number (caar th))
8106                               gnus-newsgroup-limit)
8107                         (setq thread (car th)
8108                               th nil)
8109                       (setq th (cdr th))))))))))
8110   thread)
8111
8112 (defun gnus-cut-threads (threads)
8113   "Cut off all uninteresting articles from the beginning of THREADS."
8114   (when (or (eq gnus-fetch-old-headers 'some)
8115             (eq gnus-fetch-old-headers 'invisible)
8116             (numberp gnus-fetch-old-headers)
8117             (eq gnus-build-sparse-threads 'some)
8118             (eq gnus-build-sparse-threads 'more))
8119     (let ((th threads))
8120       (while th
8121         (setcar th (gnus-cut-thread (car th)))
8122         (setq th (cdr th)))))
8123   ;; Remove nixed out threads.
8124   (delq nil threads))
8125
8126 (defun gnus-summary-initial-limit (&optional show-if-empty)
8127   "Figure out what the initial limit is supposed to be on group entry.
8128 This entails weeding out unwanted dormants, low-scored articles,
8129 fetch-old-headers verbiage, and so on."
8130   ;; Most groups have nothing to remove.
8131   (if (or gnus-inhibit-limiting
8132           (and (null gnus-newsgroup-dormant)
8133                (eq gnus-newsgroup-display 'gnus-not-ignore)
8134                (not (eq gnus-fetch-old-headers 'some))
8135                (not (numberp gnus-fetch-old-headers))
8136                (not (eq gnus-fetch-old-headers 'invisible))
8137                (null gnus-summary-expunge-below)
8138                (not (eq gnus-build-sparse-threads 'some))
8139                (not (eq gnus-build-sparse-threads 'more))
8140                (null gnus-thread-expunge-below)
8141                (not gnus-use-nocem)))
8142       ()                                ; Do nothing.
8143     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8144     (setq gnus-newsgroup-limit nil)
8145     (mapatoms
8146      (lambda (node)
8147        (unless (car (symbol-value node))
8148          ;; These threads have no parents -- they are roots.
8149          (let ((nodes (cdr (symbol-value node)))
8150                thread)
8151            (while nodes
8152              (if (and gnus-thread-expunge-below
8153                       (< (gnus-thread-total-score (car nodes))
8154                          gnus-thread-expunge-below))
8155                  (gnus-expunge-thread (pop nodes))
8156                (setq thread (pop nodes))
8157                (gnus-summary-limit-children thread))))))
8158      gnus-newsgroup-dependencies)
8159     ;; If this limitation resulted in an empty group, we might
8160     ;; pop the previous limit and use it instead.
8161     (when (and (not gnus-newsgroup-limit)
8162                show-if-empty)
8163       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8164     gnus-newsgroup-limit))
8165
8166 (defun gnus-summary-limit-children (thread)
8167   "Return 1 if this subthread is visible and 0 if it is not."
8168   ;; First we get the number of visible children to this thread.  This
8169   ;; is done by recursing down the thread using this function, so this
8170   ;; will really go down to a leaf article first, before slowly
8171   ;; working its way up towards the root.
8172   (when thread
8173     (let* ((max-lisp-eval-depth 5000)
8174            (children
8175            (if (cdr thread)
8176                (apply '+ (mapcar 'gnus-summary-limit-children
8177                                  (cdr thread)))
8178              0))
8179           (number (mail-header-number (car thread)))
8180           score)
8181       (if (and
8182            (not (memq number gnus-newsgroup-marked))
8183            (or
8184             ;; If this article is dormant and has absolutely no visible
8185             ;; children, then this article isn't visible.
8186             (and (memq number gnus-newsgroup-dormant)
8187                  (zerop children))
8188             ;; If this is "fetch-old-headered" and there is no
8189             ;; visible children, then we don't want this article.
8190             (and (or (eq gnus-fetch-old-headers 'some)
8191                      (numberp gnus-fetch-old-headers))
8192                  (gnus-summary-article-ancient-p number)
8193                  (zerop children))
8194             ;; If this is "fetch-old-headered" and `invisible', then
8195             ;; we don't want this article.
8196             (and (eq gnus-fetch-old-headers 'invisible)
8197                  (gnus-summary-article-ancient-p number))
8198             ;; If this is a sparsely inserted article with no children,
8199             ;; we don't want it.
8200             (and (eq gnus-build-sparse-threads 'some)
8201                  (gnus-summary-article-sparse-p number)
8202                  (zerop children))
8203             ;; If we use expunging, and this article is really
8204             ;; low-scored, then we don't want this article.
8205             (when (and gnus-summary-expunge-below
8206                        (< (setq score
8207                                 (or (cdr (assq number gnus-newsgroup-scored))
8208                                     gnus-summary-default-score))
8209                           gnus-summary-expunge-below))
8210               ;; We increase the expunge-tally here, but that has
8211               ;; nothing to do with the limits, really.
8212               (incf gnus-newsgroup-expunged-tally)
8213               ;; We also mark as read here, if that's wanted.
8214               (when (and gnus-summary-mark-below
8215                          (< score gnus-summary-mark-below))
8216                 (setq gnus-newsgroup-unreads
8217                       (delq number gnus-newsgroup-unreads))
8218                 (if gnus-newsgroup-auto-expire
8219                     (push number gnus-newsgroup-expirable)
8220                   (push (cons number gnus-low-score-mark)
8221                         gnus-newsgroup-reads)))
8222               t)
8223             ;; Do the `display' group parameter.
8224             (and gnus-newsgroup-display
8225                  (not (funcall gnus-newsgroup-display)))
8226             ;; Check NoCeM things.
8227             (when (and gnus-use-nocem
8228                        (gnus-nocem-unwanted-article-p
8229                         (mail-header-id (car thread))))
8230               (setq gnus-newsgroup-unreads
8231                     (delq number gnus-newsgroup-unreads))
8232               t)))
8233           ;; Nope, invisible article.
8234           0
8235         ;; Ok, this article is to be visible, so we add it to the limit
8236         ;; and return 1.
8237         (push number gnus-newsgroup-limit)
8238         1))))
8239
8240 (defun gnus-expunge-thread (thread)
8241   "Mark all articles in THREAD as read."
8242   (let* ((number (mail-header-number (car thread))))
8243     (incf gnus-newsgroup-expunged-tally)
8244     ;; We also mark as read here, if that's wanted.
8245     (setq gnus-newsgroup-unreads
8246           (delq number gnus-newsgroup-unreads))
8247     (if gnus-newsgroup-auto-expire
8248         (push number gnus-newsgroup-expirable)
8249       (push (cons number gnus-low-score-mark)
8250             gnus-newsgroup-reads)))
8251   ;; Go recursively through all subthreads.
8252   (mapcar 'gnus-expunge-thread (cdr thread)))
8253
8254 ;; Summary article oriented commands
8255
8256 (defun gnus-summary-refer-parent-article (n)
8257   "Refer parent article N times.
8258 If N is negative, go to ancestor -N instead.
8259 The difference between N and the number of articles fetched is returned."
8260   (interactive "p")
8261   (let ((skip 1)
8262         error header ref)
8263     (when (not (natnump n))
8264       (setq skip (abs n)
8265             n 1))
8266     (while (and (> n 0)
8267                 (not error))
8268       (setq header (gnus-summary-article-header))
8269       (if (and (eq (mail-header-number header)
8270                    (cdr gnus-article-current))
8271                (equal gnus-newsgroup-name
8272                       (car gnus-article-current)))
8273           ;; If we try to find the parent of the currently
8274           ;; displayed article, then we take a look at the actual
8275           ;; References header, since this is slightly more
8276           ;; reliable than the References field we got from the
8277           ;; server.
8278           (save-excursion
8279             (set-buffer gnus-original-article-buffer)
8280             (nnheader-narrow-to-headers)
8281             (unless (setq ref (message-fetch-field "references"))
8282               (when (setq ref (message-fetch-field "in-reply-to"))
8283                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8284             (widen))
8285         (setq ref
8286               ;; It's not the current article, so we take a bet on
8287               ;; the value we got from the server.
8288               (mail-header-references header)))
8289       (if (and ref
8290                (not (equal ref "")))
8291           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8292             (gnus-message 1 "Couldn't find parent"))
8293         (gnus-message 1 "No references in article %d"
8294                       (gnus-summary-article-number))
8295         (setq error t))
8296       (decf n))
8297     (gnus-summary-position-point)
8298     n))
8299
8300 (defun gnus-summary-refer-references ()
8301   "Fetch all articles mentioned in the References header.
8302 Return the number of articles fetched."
8303   (interactive)
8304   (let ((ref (mail-header-references (gnus-summary-article-header)))
8305         (current (gnus-summary-article-number))
8306         (n 0))
8307     (if (or (not ref)
8308             (equal ref ""))
8309         (error "No References in the current article")
8310       ;; For each Message-ID in the References header...
8311       (while (string-match "<[^>]*>" ref)
8312         (incf n)
8313         ;; ... fetch that article.
8314         (gnus-summary-refer-article
8315          (prog1 (match-string 0 ref)
8316            (setq ref (substring ref (match-end 0))))))
8317       (gnus-summary-goto-subject current)
8318       (gnus-summary-position-point)
8319       n)))
8320
8321 (defun gnus-summary-refer-thread (&optional limit)
8322   "Fetch all articles in the current thread.
8323 If LIMIT (the numerical prefix), fetch that many old headers instead
8324 of what's specified by the `gnus-refer-thread-limit' variable."
8325   (interactive "P")
8326   (let ((id (mail-header-id (gnus-summary-article-header)))
8327         (limit (if limit (prefix-numeric-value limit)
8328                  gnus-refer-thread-limit)))
8329     (unless (eq gnus-fetch-old-headers 'invisible)
8330       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8331       ;; Retrieve the headers and read them in.
8332       (if (eq (if (numberp limit)
8333                   (gnus-retrieve-headers
8334                    (list (min
8335                           (+ (mail-header-number
8336                               (gnus-summary-article-header))
8337                              limit)
8338                           gnus-newsgroup-end))
8339                    gnus-newsgroup-name (* limit 2))
8340                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8341                 ;; headers.
8342                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8343                                        gnus-newsgroup-name limit))
8344               'nov)
8345           (gnus-build-all-threads)
8346         (error "Can't fetch thread from back ends that don't support NOV"))
8347       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8348     (gnus-summary-limit-include-thread id)))
8349
8350 (defun gnus-summary-refer-article (message-id)
8351   "Fetch an article specified by MESSAGE-ID."
8352   (interactive "sMessage-ID: ")
8353   (when (and (stringp message-id)
8354              (not (zerop (length message-id))))
8355     (setq message-id (gnus-replace-in-string message-id " " ""))
8356     ;; Construct the correct Message-ID if necessary.
8357     ;; Suggested by tale@pawl.rpi.edu.
8358     (unless (string-match "^<" message-id)
8359       (setq message-id (concat "<" message-id)))
8360     (unless (string-match ">$" message-id)
8361       (setq message-id (concat message-id ">")))
8362     ;; People often post MIDs from URLs, so unhex it:
8363     (unless (string-match "@" message-id)
8364       (setq message-id (gnus-url-unhex-string message-id)))
8365     (let* ((header (gnus-id-to-header message-id))
8366            (sparse (and header
8367                         (gnus-summary-article-sparse-p
8368                          (mail-header-number header))
8369                         (memq (mail-header-number header)
8370                               gnus-newsgroup-limit)))
8371            number)
8372       (cond
8373        ;; If the article is present in the buffer we just go to it.
8374        ((and header
8375              (or (not (gnus-summary-article-sparse-p
8376                        (mail-header-number header)))
8377                  sparse))
8378         (prog1
8379             (gnus-summary-goto-article
8380              (mail-header-number header) nil t)
8381           (when sparse
8382             (gnus-summary-update-article (mail-header-number header)))))
8383        (t
8384         ;; We fetch the article.
8385         (catch 'found
8386           (dolist (gnus-override-method (gnus-refer-article-methods))
8387             (when (and (gnus-check-server gnus-override-method)
8388                        ;; Fetch the header,
8389                        (setq number (gnus-summary-insert-subject message-id)))
8390               ;; and display the article.
8391               (gnus-summary-select-article nil nil nil number)
8392               (throw 'found t)))
8393           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8394
8395 (defun gnus-refer-article-methods ()
8396   "Return a list of referable methods."
8397   (cond
8398    ;; No method, so we default to current and native.
8399    ((null gnus-refer-article-method)
8400     (list gnus-current-select-method gnus-select-method))
8401    ;; Current.
8402    ((eq 'current gnus-refer-article-method)
8403     (list gnus-current-select-method))
8404    ;; List of select methods.
8405    ((not (and (symbolp (car gnus-refer-article-method))
8406               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8407     (let (out)
8408       (dolist (method gnus-refer-article-method)
8409         (push (if (eq 'current method)
8410                   gnus-current-select-method
8411                 method)
8412               out))
8413       (nreverse out)))
8414    ;; One single select method.
8415    (t
8416     (list gnus-refer-article-method))))
8417
8418 (defun gnus-summary-edit-parameters ()
8419   "Edit the group parameters of the current group."
8420   (interactive)
8421   (gnus-group-edit-group gnus-newsgroup-name 'params))
8422
8423 (defun gnus-summary-customize-parameters ()
8424   "Customize the group parameters of the current group."
8425   (interactive)
8426   (gnus-group-customize gnus-newsgroup-name))
8427
8428 (defun gnus-summary-enter-digest-group (&optional force)
8429   "Enter an nndoc group based on the current article.
8430 If FORCE, force a digest interpretation.  If not, try
8431 to guess what the document format is."
8432   (interactive "P")
8433   (let ((conf gnus-current-window-configuration))
8434     (save-window-excursion
8435       (save-excursion
8436         (let (gnus-article-prepare-hook
8437               gnus-display-mime-function
8438               gnus-break-pages)
8439           (gnus-summary-select-article))))
8440     (setq gnus-current-window-configuration conf)
8441     (let* ((name (format "%s-%d"
8442                          (gnus-group-prefixed-name
8443                           gnus-newsgroup-name (list 'nndoc ""))
8444                          (with-current-buffer gnus-summary-buffer
8445                            gnus-current-article)))
8446            (ogroup gnus-newsgroup-name)
8447            (params (append (gnus-info-params (gnus-get-info ogroup))
8448                            (list (cons 'to-group ogroup))
8449                            (list (cons 'parent-group ogroup))
8450                            (list (cons 'save-article-group ogroup))))
8451            (case-fold-search t)
8452            (buf (current-buffer))
8453            dig to-address)
8454       (save-excursion
8455         (set-buffer gnus-original-article-buffer)
8456         ;; Have the digest group inherit the main mail address of
8457         ;; the parent article.
8458         (when (setq to-address (or (gnus-fetch-field "reply-to")
8459                                    (gnus-fetch-field "from")))
8460           (setq params (append
8461                         (list (cons 'to-address
8462                                     (funcall gnus-decode-encoded-word-function
8463                                              to-address))))))
8464         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8465         (insert-buffer-substring gnus-original-article-buffer)
8466         ;; Remove lines that may lead nndoc to misinterpret the
8467         ;; document type.
8468         (narrow-to-region
8469          (goto-char (point-min))
8470          (or (search-forward "\n\n" nil t) (point)))
8471         (goto-char (point-min))
8472         (delete-matching-lines "^Path:\\|^From ")
8473         (widen))
8474       (unwind-protect
8475           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8476                     (gnus-newsgroup-ephemeral-ignored-charsets
8477                      gnus-newsgroup-ignored-charsets))
8478                 (gnus-group-read-ephemeral-group
8479                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8480                               (nndoc-article-type
8481                                ,(if force 'mbox 'guess)))
8482                  t nil nil nil
8483                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8484                                                         "ADAPT")))))
8485               ;; Make all postings to this group go to the parent group.
8486               (nconc (gnus-info-params (gnus-get-info name))
8487                      params)
8488             ;; Couldn't select this doc group.
8489             (switch-to-buffer buf)
8490             (gnus-set-global-variables)
8491             (gnus-configure-windows 'summary)
8492             (gnus-message 3 "Article couldn't be entered?"))
8493         (kill-buffer dig)))))
8494
8495 (defun gnus-summary-read-document (n)
8496   "Open a new group based on the current article(s).
8497 This will allow you to read digests and other similar
8498 documents as newsgroups.
8499 Obeys the standard process/prefix convention."
8500   (interactive "P")
8501   (let* ((ogroup gnus-newsgroup-name)
8502          (params (append (gnus-info-params (gnus-get-info ogroup))
8503                          (list (cons 'to-group ogroup))))
8504          group egroup groups vgroup)
8505     (dolist (article (gnus-summary-work-articles n))
8506       (setq group (format "%s-%d" gnus-newsgroup-name article))
8507       (gnus-summary-remove-process-mark article)
8508       (when (gnus-summary-display-article article)
8509         (save-excursion
8510           (with-temp-buffer
8511             (insert-buffer-substring gnus-original-article-buffer)
8512             ;; Remove some headers that may lead nndoc to make
8513             ;; the wrong guess.
8514             (message-narrow-to-head)
8515             (goto-char (point-min))
8516             (delete-matching-lines "^Path:\\|^From ")
8517             (widen)
8518             (if (setq egroup
8519                       (gnus-group-read-ephemeral-group
8520                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8521                                      (nndoc-article-type guess))
8522                        t nil t))
8523                 (progn
8524             ;; Make all postings to this group go to the parent group.
8525                   (nconc (gnus-info-params (gnus-get-info egroup))
8526                          params)
8527                   (push egroup groups))
8528               ;; Couldn't select this doc group.
8529               (gnus-error 3 "Article couldn't be entered"))))))
8530     ;; Now we have selected all the documents.
8531     (cond
8532      ((not groups)
8533       (error "None of the articles could be interpreted as documents"))
8534      ((gnus-group-read-ephemeral-group
8535        (setq vgroup (format
8536                      "nnvirtual:%s-%s" gnus-newsgroup-name
8537                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8538        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8539        t
8540        (cons (current-buffer) 'summary)))
8541      (t
8542       (error "Couldn't select virtual nndoc group")))))
8543
8544 (defun gnus-summary-isearch-article (&optional regexp-p)
8545   "Do incremental search forward on the current article.
8546 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8547   (interactive "P")
8548   (gnus-summary-select-article)
8549   (gnus-configure-windows 'article)
8550   (gnus-eval-in-buffer-window gnus-article-buffer
8551     (save-restriction
8552       (widen)
8553       (isearch-forward regexp-p))))
8554
8555 (defun gnus-summary-search-article-forward (regexp &optional backward)
8556   "Search for an article containing REGEXP forward.
8557 If BACKWARD, search backward instead."
8558   (interactive
8559    (list (read-string
8560           (format "Search article %s (regexp%s): "
8561                   (if current-prefix-arg "backward" "forward")
8562                   (if gnus-last-search-regexp
8563                       (concat ", default " gnus-last-search-regexp)
8564                     "")))
8565          current-prefix-arg))
8566   (if (string-equal regexp "")
8567       (setq regexp (or gnus-last-search-regexp ""))
8568     (setq gnus-last-search-regexp regexp)
8569     (setq gnus-article-before-search gnus-current-article))
8570   ;; Intentionally set gnus-last-article.
8571   (setq gnus-last-article gnus-article-before-search)
8572   (let ((gnus-last-article gnus-last-article))
8573     (if (gnus-summary-search-article regexp backward)
8574         (gnus-summary-show-thread)
8575       (signal 'search-failed (list regexp)))))
8576
8577 (defun gnus-summary-search-article-backward (regexp)
8578   "Search for an article containing REGEXP backward."
8579   (interactive
8580    (list (read-string
8581           (format "Search article backward (regexp%s): "
8582                   (if gnus-last-search-regexp
8583                       (concat ", default " gnus-last-search-regexp)
8584                     "")))))
8585   (gnus-summary-search-article-forward regexp 'backward))
8586
8587 (defun gnus-summary-search-article (regexp &optional backward)
8588   "Search for an article containing REGEXP.
8589 Optional argument BACKWARD means do search for backward.
8590 `gnus-select-article-hook' is not called during the search."
8591   ;; We have to require this here to make sure that the following
8592   ;; dynamic binding isn't shadowed by autoloading.
8593   (require 'gnus-async)
8594   (require 'gnus-art)
8595   (let ((gnus-select-article-hook nil)  ;Disable hook.
8596         (gnus-article-prepare-hook nil)
8597         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8598         (gnus-use-article-prefetch nil)
8599         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8600         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8601         (gnus-visual nil)
8602         (gnus-keep-backlog nil)
8603         (gnus-break-pages nil)
8604         (gnus-summary-display-arrow nil)
8605         (gnus-updated-mode-lines nil)
8606         (gnus-auto-center-summary nil)
8607         (sum (current-buffer))
8608         (gnus-display-mime-function nil)
8609         (found nil)
8610         point)
8611     (gnus-save-hidden-threads
8612       (gnus-summary-select-article)
8613       (set-buffer gnus-article-buffer)
8614       (goto-char (window-point (get-buffer-window (current-buffer))))
8615       (when backward
8616         (forward-line -1))
8617       (while (not found)
8618         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8619         (if (if backward
8620                 (re-search-backward regexp nil t)
8621               (re-search-forward regexp nil t))
8622             ;; We found the regexp.
8623             (progn
8624               (setq found 'found)
8625               (beginning-of-line)
8626               (set-window-start
8627                (get-buffer-window (current-buffer))
8628                (point))
8629               (forward-line 1)
8630               (set-window-point
8631                (get-buffer-window (current-buffer))
8632                (point))
8633               (set-buffer sum)
8634               (setq point (point)))
8635           ;; We didn't find it, so we go to the next article.
8636           (set-buffer sum)
8637           (setq found 'not)
8638           (while (eq found 'not)
8639             (if (not (if backward (gnus-summary-find-prev)
8640                        (gnus-summary-find-next)))
8641                 ;; No more articles.
8642                 (setq found t)
8643               ;; Select the next article and adjust point.
8644               (unless (gnus-summary-article-sparse-p
8645                        (gnus-summary-article-number))
8646                 (setq found nil)
8647                 (gnus-summary-select-article)
8648                 (set-buffer gnus-article-buffer)
8649                 (widen)
8650                 (goto-char (if backward (point-max) (point-min))))))))
8651       (gnus-message 7 ""))
8652     ;; Return whether we found the regexp.
8653     (when (eq found 'found)
8654       (goto-char point)
8655       (gnus-summary-show-thread)
8656       (gnus-summary-goto-subject gnus-current-article)
8657       (gnus-summary-position-point)
8658       t)))
8659
8660 (defun gnus-find-matching-articles (header regexp)
8661   "Return a list of all articles that match REGEXP on HEADER.
8662 This search includes all articles in the current group that Gnus has
8663 fetched headers for, whether they are displayed or not."
8664   (let ((articles nil)
8665         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8666         (case-fold-search t))
8667     (dolist (header gnus-newsgroup-headers)
8668       (when (string-match regexp (funcall func header))
8669         (push (mail-header-number header) articles)))
8670     (nreverse articles)))
8671
8672 (defun gnus-summary-find-matching (header regexp &optional backward unread
8673                                           not-case-fold not-matching)
8674   "Return a list of all articles that match REGEXP on HEADER.
8675 The search stars on the current article and goes forwards unless
8676 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8677 If UNREAD is non-nil, only unread articles will
8678 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8679 in the comparisons. If NOT-MATCHING, return a list of all articles that
8680 not match REGEXP on HEADER."
8681   (let ((case-fold-search (not not-case-fold))
8682         articles d func)
8683     (if (consp header)
8684         (if (eq (car header) 'extra)
8685             (setq func
8686                   `(lambda (h)
8687                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8688                          "")))
8689           (error "%s is an invalid header" header))
8690       (unless (fboundp (intern (concat "mail-header-" header)))
8691         (error "%s is not a valid header" header))
8692       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8693     (dolist (d (if (eq backward 'all)
8694                    gnus-newsgroup-data
8695                  (gnus-data-find-list
8696                   (gnus-summary-article-number)
8697                   (gnus-data-list backward))))
8698       (when (and (or (not unread)       ; We want all articles...
8699                      (gnus-data-unread-p d)) ; Or just unreads.
8700                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8701                  (if not-matching
8702                      (not (string-match
8703                            regexp
8704                            (funcall func (gnus-data-header d))))
8705                    (string-match regexp
8706                                  (funcall func (gnus-data-header d)))))
8707         (push (gnus-data-number d) articles))) ; Success!
8708     (nreverse articles)))
8709
8710 (defun gnus-summary-execute-command (header regexp command &optional backward)
8711   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8712 If HEADER is an empty string (or nil), the match is done on the entire
8713 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8714   (interactive
8715    (list (let ((completion-ignore-case t))
8716            (completing-read
8717             "Header name: "
8718             (mapcar (lambda (header) (list (format "%s" header)))
8719                     (append
8720                      '("Number" "Subject" "From" "Lines" "Date"
8721                        "Message-ID" "Xref" "References" "Body")
8722                      gnus-extra-headers))
8723             nil 'require-match))
8724          (read-string "Regexp: ")
8725          (read-key-sequence "Command: ")
8726          current-prefix-arg))
8727   (when (equal header "Body")
8728     (setq header ""))
8729   ;; Hidden thread subtrees must be searched as well.
8730   (gnus-summary-show-all-threads)
8731   ;; We don't want to change current point nor window configuration.
8732   (save-excursion
8733     (save-window-excursion
8734       (let (gnus-visual
8735             gnus-treat-strip-trailing-blank-lines
8736             gnus-treat-strip-leading-blank-lines
8737             gnus-treat-strip-multiple-blank-lines
8738             gnus-treat-hide-boring-headers
8739             gnus-treat-fold-newsgroups
8740             gnus-article-prepare-hook)
8741         (gnus-message 6 "Executing %s..." (key-description command))
8742         ;; We'd like to execute COMMAND interactively so as to give arguments.
8743         (gnus-execute header regexp
8744                       `(call-interactively ',(key-binding command))
8745                       backward)
8746         (gnus-message 6 "Executing %s...done" (key-description command))))))
8747
8748 (defun gnus-summary-beginning-of-article ()
8749   "Scroll the article back to the beginning."
8750   (interactive)
8751   (gnus-summary-select-article)
8752   (gnus-configure-windows 'article)
8753   (gnus-eval-in-buffer-window gnus-article-buffer
8754     (widen)
8755     (goto-char (point-min))
8756     (when gnus-break-pages
8757       (gnus-narrow-to-page))))
8758
8759 (defun gnus-summary-end-of-article ()
8760   "Scroll to the end of the article."
8761   (interactive)
8762   (gnus-summary-select-article)
8763   (gnus-configure-windows 'article)
8764   (gnus-eval-in-buffer-window gnus-article-buffer
8765     (widen)
8766     (goto-char (point-max))
8767     (recenter -3)
8768     (when gnus-break-pages
8769       (when (re-search-backward page-delimiter nil t)
8770         (narrow-to-region (match-end 0) (point-max)))
8771       (gnus-narrow-to-page))))
8772
8773 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8774   "Truncate to LEN and quote all \"(\"'s in STRING."
8775   (gnus-replace-in-string (if (and len (> (length string) len))
8776                               (substring string 0 len)
8777                             string)
8778                           "[()]" "\\\\\\&"))
8779
8780 (defun gnus-summary-print-article (&optional filename n)
8781   "Generate and print a PostScript image of the process-marked (mail) articles.
8782
8783 If used interactively, print the current article if none are
8784 process-marked.  With prefix arg, prompt the user for the name of the
8785 file to save in.
8786
8787 When used from Lisp, accept two optional args FILENAME and N.  N means
8788 to print the next N articles.  If N is negative, print the N previous
8789 articles.  If N is nil and articles have been marked with the process
8790 mark, print these instead.
8791
8792 If the optional first argument FILENAME is nil, send the image to the
8793 printer.  If FILENAME is a string, save the PostScript image in a file with
8794 that name.  If FILENAME is a number, prompt the user for the name of the file
8795 to save in."
8796   (interactive (list (ps-print-preprint current-prefix-arg)))
8797   (dolist (article (gnus-summary-work-articles n))
8798     (gnus-summary-select-article nil nil 'pseudo article)
8799     (gnus-eval-in-buffer-window gnus-article-buffer
8800       (gnus-print-buffer))
8801     (gnus-summary-remove-process-mark article))
8802   (ps-despool filename))
8803
8804 (defun gnus-print-buffer ()
8805   (let ((buffer (generate-new-buffer " *print*")))
8806     (unwind-protect
8807         (progn
8808           (copy-to-buffer buffer (point-min) (point-max))
8809           (set-buffer buffer)
8810           (gnus-remove-text-with-property 'gnus-decoration)
8811           (when (gnus-visual-p 'article-highlight 'highlight)
8812             ;; Copy-to-buffer doesn't copy overlay.  So redo
8813             ;; highlight.
8814             (let ((gnus-article-buffer buffer))
8815               (gnus-article-highlight-citation t)
8816               (gnus-article-highlight-signature)
8817               (gnus-article-emphasize)
8818               (gnus-article-delete-invisible-text)))
8819           (let ((ps-left-header
8820                  (list
8821                   (concat "("
8822                           (gnus-summary-print-truncate-and-quote
8823                            (mail-header-subject gnus-current-headers)
8824                            66) ")")
8825                   (concat "("
8826                           (gnus-summary-print-truncate-and-quote
8827                            (mail-header-from gnus-current-headers)
8828                            45) ")")))
8829                 (ps-right-header
8830                  (list
8831                   "/pagenumberstring load"
8832                   (concat "("
8833                           (mail-header-date gnus-current-headers) ")"))))
8834             (gnus-run-hooks 'gnus-ps-print-hook)
8835             (save-excursion
8836               (if window-system
8837                   (ps-spool-buffer-with-faces)
8838                 (ps-spool-buffer)))))
8839       (kill-buffer buffer))))
8840
8841 (defun gnus-summary-show-article (&optional arg)
8842   "Force redisplaying of the current article.
8843 If ARG (the prefix) is a number, show the article with the charset
8844 defined in `gnus-summary-show-article-charset-alist', or the charset
8845 input.
8846 If ARG (the prefix) is non-nil and not a number, show the raw article
8847 without any article massaging functions being run.  Normally, the key
8848 strokes are `C-u g'."
8849   (interactive "P")
8850   (cond
8851    ((numberp arg)
8852     (gnus-summary-show-article t)
8853     (let ((gnus-newsgroup-charset
8854            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8855                (mm-read-coding-system
8856                 "View as charset: " ;; actually it is coding system.
8857                 (with-current-buffer gnus-article-buffer
8858                   (mm-detect-coding-region (point) (point-max))))))
8859           (gnus-newsgroup-ignored-charsets 'gnus-all))
8860       (gnus-summary-select-article nil 'force)
8861       (let ((deps gnus-newsgroup-dependencies)
8862             head header lines)
8863         (save-excursion
8864           (set-buffer gnus-original-article-buffer)
8865           (save-restriction
8866             (message-narrow-to-head)
8867             (setq head (buffer-string))
8868             (goto-char (point-min))
8869             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8870               (goto-char (point-max))
8871               (widen)
8872               (setq lines (1- (count-lines (point) (point-max))))))
8873           (with-temp-buffer
8874             (insert (format "211 %d Article retrieved.\n"
8875                             (cdr gnus-article-current)))
8876             (insert head)
8877             (if lines (insert (format "Lines: %d\n" lines)))
8878             (insert ".\n")
8879             (let ((nntp-server-buffer (current-buffer)))
8880               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8881         (gnus-data-set-header
8882          (gnus-data-find (cdr gnus-article-current))
8883          header)
8884         (gnus-summary-update-article-line
8885          (cdr gnus-article-current) header)
8886         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8887           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8888    ((not arg)
8889     ;; Select the article the normal way.
8890     (gnus-summary-select-article nil 'force))
8891    (t
8892     ;; We have to require this here to make sure that the following
8893     ;; dynamic binding isn't shadowed by autoloading.
8894     (require 'gnus-async)
8895     (require 'gnus-art)
8896     ;; Bind the article treatment functions to nil.
8897     (let ((gnus-have-all-headers t)
8898           gnus-article-prepare-hook
8899           gnus-article-decode-hook
8900           gnus-display-mime-function
8901           gnus-break-pages)
8902       ;; Destroy any MIME parts.
8903       (when (gnus-buffer-live-p gnus-article-buffer)
8904         (save-excursion
8905           (set-buffer gnus-article-buffer)
8906           (mm-destroy-parts gnus-article-mime-handles)
8907           ;; Set it to nil for safety reason.
8908           (setq gnus-article-mime-handle-alist nil)
8909           (setq gnus-article-mime-handles nil)))
8910       (gnus-summary-select-article nil 'force))))
8911   (gnus-summary-goto-subject gnus-current-article)
8912   (gnus-summary-position-point))
8913
8914 (defun gnus-summary-show-raw-article ()
8915   "Show the raw article without any article massaging functions being run."
8916   (interactive)
8917   (gnus-summary-show-article t))
8918
8919 (defun gnus-summary-verbose-headers (&optional arg)
8920   "Toggle permanent full header display.
8921 If ARG is a positive number, turn header display on.
8922 If ARG is a negative number, turn header display off."
8923   (interactive "P")
8924   (setq gnus-show-all-headers
8925         (cond ((or (not (numberp arg))
8926                    (zerop arg))
8927                (not gnus-show-all-headers))
8928               ((natnump arg)
8929                t)))
8930   (gnus-summary-show-article))
8931
8932 (defun gnus-summary-toggle-header (&optional arg)
8933   "Show the headers if they are hidden, or hide them if they are shown.
8934 If ARG is a positive number, show the entire header.
8935 If ARG is a negative number, hide the unwanted header lines."
8936   (interactive "P")
8937   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8938                      (get-buffer-window gnus-article-buffer t))))
8939     (with-current-buffer gnus-article-buffer
8940       (widen)
8941       (article-narrow-to-head)
8942       (let* ((buffer-read-only nil)
8943              (inhibit-point-motion-hooks t)
8944              (hidden (if (numberp arg)
8945                          (>= arg 0)
8946                        (or (not (looking-at "[^ \t\n]+:"))
8947                            (gnus-article-hidden-text-p 'headers))))
8948              s e)
8949         (delete-region (point-min) (point-max))
8950         (with-current-buffer gnus-original-article-buffer
8951           (goto-char (setq s (point-min)))
8952           (setq e (if (search-forward "\n\n" nil t)
8953                       (1- (point))
8954                     (point-max))))
8955         (insert-buffer-substring gnus-original-article-buffer s e)
8956         (run-hooks 'gnus-article-decode-hook)
8957         (if hidden
8958             (let ((gnus-treat-hide-headers nil)
8959                   (gnus-treat-hide-boring-headers nil))
8960               (gnus-delete-wash-type 'headers)
8961               (gnus-treat-article 'head))
8962           (gnus-treat-article 'head))
8963         (widen)
8964         (if window
8965             (set-window-start window (goto-char (point-min))))
8966         (if gnus-break-pages
8967             (gnus-narrow-to-page)
8968           (when (gnus-visual-p 'page-marker)
8969             (let ((buffer-read-only nil))
8970               (gnus-remove-text-with-property 'gnus-prev)
8971               (gnus-remove-text-with-property 'gnus-next))))
8972         (gnus-set-mode-line 'article)))))
8973
8974 (defun gnus-summary-show-all-headers ()
8975   "Make all header lines visible."
8976   (interactive)
8977   (gnus-summary-toggle-header 1))
8978
8979 (defun gnus-summary-caesar-message (&optional arg)
8980   "Caesar rotate the current article by 13.
8981 The numerical prefix specifies how many places to rotate each letter
8982 forward."
8983   (interactive "P")
8984   (gnus-summary-select-article)
8985   (let ((mail-header-separator ""))
8986     (gnus-eval-in-buffer-window gnus-article-buffer
8987       (save-restriction
8988         (widen)
8989         (let ((start (window-start))
8990               buffer-read-only)
8991           (message-caesar-buffer-body arg)
8992           (set-window-start (get-buffer-window (current-buffer)) start)))))
8993   ;; Create buttons and stuff...
8994   (gnus-treat-article nil))
8995
8996 (defun gnus-summary-idna-message (&optional arg)
8997   "Decode IDNA encoded domain names in the current articles.
8998 IDNA encoded domain names looks like `xn--bar'.  If a string
8999 remain unencoded after running this function, it is likely an
9000 invalid IDNA string (`xn--bar' is invalid).
9001
9002 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9003 installed for this command to work."
9004   (interactive "P")
9005   (if (not (and (condition-case nil (require 'idna)
9006                   (file-error))
9007                 (mm-coding-system-p 'utf-8)
9008                 (executable-find (symbol-value 'idna-program))))
9009       (gnus-message
9010        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9011     (gnus-summary-select-article)
9012     (let ((mail-header-separator ""))
9013       (gnus-eval-in-buffer-window gnus-article-buffer
9014         (save-restriction
9015           (widen)
9016           (let ((start (window-start))
9017                 buffer-read-only)
9018             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9019               (replace-match (idna-to-unicode (match-string 1))))
9020             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9021
9022 (autoload 'unmorse-region "morse"
9023   "Convert morse coded text in region to ordinary ASCII text."
9024   t)
9025
9026 (defun gnus-summary-morse-message (&optional arg)
9027   "Morse decode the current article."
9028   (interactive "P")
9029   (gnus-summary-select-article)
9030   (let ((mail-header-separator ""))
9031     (gnus-eval-in-buffer-window gnus-article-buffer
9032       (save-excursion
9033         (save-restriction
9034           (widen)
9035           (let ((pos (window-start))
9036                 buffer-read-only)
9037             (goto-char (point-min))
9038             (when (message-goto-body)
9039               (gnus-narrow-to-body))
9040             (goto-char (point-min))
9041             (while (search-forward "·" (point-max) t)
9042               (replace-match "."))
9043             (unmorse-region (point-min) (point-max))
9044             (widen)
9045             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9046
9047 (defun gnus-summary-stop-page-breaking ()
9048   "Stop page breaking in the current article."
9049   (interactive)
9050   (gnus-summary-select-article)
9051   (gnus-eval-in-buffer-window gnus-article-buffer
9052     (widen)
9053     (when (gnus-visual-p 'page-marker)
9054       (let ((buffer-read-only nil))
9055         (gnus-remove-text-with-property 'gnus-prev)
9056         (gnus-remove-text-with-property 'gnus-next))
9057       (setq gnus-page-broken nil))))
9058
9059 (defun gnus-summary-move-article (&optional n to-newsgroup
9060                                             select-method action)
9061   "Move the current article to a different newsgroup.
9062 If N is a positive number, move the N next articles.
9063 If N is a negative number, move the N previous articles.
9064 If N is nil and any articles have been marked with the process mark,
9065 move those articles instead.
9066 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9067 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9068 re-spool using this method.
9069
9070 When called interactively with TO-NEWSGROUP being nil, the value of
9071 the variable `gnus-move-split-methods' is used for finding a default
9072 for the target newsgroup.
9073
9074 For this function to work, both the current newsgroup and the
9075 newsgroup that you want to move to have to support the `request-move'
9076 and `request-accept' functions.
9077
9078 ACTION can be either `move' (the default), `crosspost' or `copy'."
9079   (interactive "P")
9080   (unless action
9081     (setq action 'move))
9082   ;; Check whether the source group supports the required functions.
9083   (cond ((and (eq action 'move)
9084               (not (gnus-check-backend-function
9085                     'request-move-article gnus-newsgroup-name)))
9086          (error "The current group does not support article moving"))
9087         ((and (eq action 'crosspost)
9088               (not (gnus-check-backend-function
9089                     'request-replace-article gnus-newsgroup-name)))
9090          (error "The current group does not support article editing")))
9091   (let ((articles (gnus-summary-work-articles n))
9092         (prefix (if (gnus-check-backend-function
9093                      'request-move-article gnus-newsgroup-name)
9094                     (funcall gnus-move-group-prefix-function
9095                              gnus-newsgroup-name)
9096                   ""))
9097         (names '((move "Move" "Moving")
9098                  (copy "Copy" "Copying")
9099                  (crosspost "Crosspost" "Crossposting")))
9100         (copy-buf (save-excursion
9101                     (nnheader-set-temp-buffer " *copy article*")))
9102         art-group to-method new-xref article to-groups articles-to-update-marks)
9103     (unless (assq action names)
9104       (error "Unknown action %s" action))
9105     ;; Read the newsgroup name.
9106     (when (and (not to-newsgroup)
9107                (not select-method))
9108       (if (and gnus-move-split-methods
9109                (not
9110                 (and (memq gnus-current-article articles)
9111                      (gnus-buffer-live-p gnus-original-article-buffer))))
9112           ;; When `gnus-move-split-methods' is non-nil, we have to
9113           ;; select an article to give `gnus-read-move-group-name' an
9114           ;; opportunity to suggest an appropriate default.  However,
9115           ;; we needn't render or mark the article.
9116           (let ((gnus-display-mime-function nil)
9117                 (gnus-article-prepare-hook nil)
9118                 (gnus-mark-article-hook nil))
9119             (gnus-summary-select-article nil nil nil (car articles))))
9120       (setq to-newsgroup
9121             (gnus-read-move-group-name
9122              (cadr (assq action names))
9123              (symbol-value (intern (format "gnus-current-%s-group" action)))
9124              articles prefix))
9125       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9126     (setq to-method (or select-method
9127                         (gnus-server-to-method
9128                          (gnus-group-method to-newsgroup))))
9129     ;; Check the method we are to move this article to...
9130     (unless (gnus-check-backend-function
9131              'request-accept-article (car to-method))
9132       (error "%s does not support article copying" (car to-method)))
9133     (unless (gnus-check-server to-method)
9134       (error "Can't open server %s" (car to-method)))
9135     (gnus-message 6 "%s to %s: %s..."
9136                   (caddr (assq action names))
9137                   (or (car select-method) to-newsgroup) articles)
9138     (while articles
9139       (setq article (pop articles))
9140       (setq
9141        art-group
9142        (cond
9143         ;; Move the article.
9144         ((eq action 'move)
9145          ;; Remove this article from future suppression.
9146          (gnus-dup-unsuppress-article article)
9147          (let* ((from-method (gnus-find-method-for-group
9148                               gnus-newsgroup-name))
9149                 (to-method (gnus-find-method-for-group
9150                             to-newsgroup))
9151                 (move-is-internal (gnus-method-equal from-method to-method)))
9152          (gnus-request-move-article
9153           article                       ; Article to move
9154           gnus-newsgroup-name           ; From newsgroup
9155           (nth 1 (gnus-find-method-for-group
9156                   gnus-newsgroup-name)) ; Server
9157           (list 'gnus-request-accept-article
9158                 to-newsgroup (list 'quote select-method)
9159                 (not articles) t)       ; Accept form
9160           (not articles)                ; Only save nov last time
9161           move-is-internal)))           ; is this move internal?
9162         ;; Copy the article.
9163         ((eq action 'copy)
9164          (save-excursion
9165            (set-buffer copy-buf)
9166            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9167              (save-restriction
9168                (nnheader-narrow-to-headers)
9169                (dolist (hdr gnus-copy-article-ignored-headers)
9170                  (message-remove-header hdr t)))
9171              (gnus-request-accept-article
9172               to-newsgroup select-method (not articles) t))))
9173         ;; Crosspost the article.
9174         ((eq action 'crosspost)
9175          (let ((xref (message-tokenize-header
9176                       (mail-header-xref (gnus-summary-article-header article))
9177                       " ")))
9178            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9179                                   ":" (number-to-string article)))
9180            (unless xref
9181              (setq xref (list (system-name))))
9182            (setq new-xref
9183                  (concat
9184                   (mapconcat 'identity
9185                              (delete "Xref:" (delete new-xref xref))
9186                              " ")
9187                   " " new-xref))
9188            (save-excursion
9189              (set-buffer copy-buf)
9190              ;; First put the article in the destination group.
9191              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9192              (when (consp (setq art-group
9193                                 (gnus-request-accept-article
9194                                  to-newsgroup select-method (not articles))))
9195                (setq new-xref (concat new-xref " " (car art-group)
9196                                       ":"
9197                                       (number-to-string (cdr art-group))))
9198                ;; Now we have the new Xrefs header, so we insert
9199                ;; it and replace the new article.
9200                (nnheader-replace-header "Xref" new-xref)
9201                (gnus-request-replace-article
9202                 (cdr art-group) to-newsgroup (current-buffer))
9203                art-group))))))
9204       (cond
9205        ((not art-group)
9206         (gnus-message 1 "Couldn't %s article %s: %s"
9207                       (cadr (assq action names)) article
9208                       (nnheader-get-report (car to-method))))
9209        ((eq art-group 'junk)
9210         (when (eq action 'move)
9211           (gnus-summary-mark-article article gnus-canceled-mark)
9212           (gnus-message 4 "Deleted article %s" article)
9213           ;; run the delete hook
9214           (run-hook-with-args 'gnus-summary-article-delete-hook
9215                               action
9216                               (gnus-data-header
9217                                (assoc article (gnus-data-list nil)))
9218                               gnus-newsgroup-name nil
9219                               select-method)))
9220        (t
9221         (let* ((pto-group (gnus-group-prefixed-name
9222                            (car art-group) to-method))
9223                (info (gnus-get-info pto-group))
9224                (to-group (gnus-info-group info))
9225                to-marks)
9226           ;; Update the group that has been moved to.
9227           (when (and info
9228                      (memq action '(move copy)))
9229             (unless (member to-group to-groups)
9230               (push to-group to-groups))
9231
9232             (unless (memq article gnus-newsgroup-unreads)
9233               (push 'read to-marks)
9234               (gnus-info-set-read
9235                info (gnus-add-to-range (gnus-info-read info)
9236                                        (list (cdr art-group)))))
9237
9238             ;; See whether the article is to be put in the cache.
9239             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9240                              gnus-article-mark-lists
9241                            (delete '(expirable . expire)
9242                                    (copy-sequence gnus-article-mark-lists))))
9243                   (to-article (cdr art-group)))
9244
9245               ;; Enter the article into the cache in the new group,
9246               ;; if that is required.
9247               (when gnus-use-cache
9248                 (gnus-cache-possibly-enter-article
9249                  to-group to-article
9250                  (memq article gnus-newsgroup-marked)
9251                  (memq article gnus-newsgroup-dormant)
9252                  (memq article gnus-newsgroup-unreads)))
9253
9254               (when gnus-preserve-marks
9255                 ;; Copy any marks over to the new group.
9256                 (when (and (equal to-group gnus-newsgroup-name)
9257                            (not (memq article gnus-newsgroup-unreads)))
9258                   ;; Mark this article as read in this group.
9259                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9260                   (setcdr (gnus-active to-group) to-article)
9261                   (setcdr gnus-newsgroup-active to-article))
9262
9263                 (while marks
9264                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9265                     (when (memq article (symbol-value
9266                                          (intern (format "gnus-newsgroup-%s"
9267                                                          (caar marks)))))
9268                       (push (cdar marks) to-marks)
9269                       ;; If the other group is the same as this group,
9270                       ;; then we have to add the mark to the list.
9271                       (when (equal to-group gnus-newsgroup-name)
9272                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9273                              (cons to-article
9274                                    (symbol-value
9275                                     (intern (format "gnus-newsgroup-%s"
9276                                                     (caar marks)))))))
9277                       ;; Copy the marks to other group.
9278                       (gnus-add-marked-articles
9279                        to-group (cdar marks) (list to-article) info)))
9280                   (setq marks (cdr marks)))
9281
9282                 (gnus-request-set-mark
9283                  to-group (list (list (list to-article) 'add to-marks))))
9284
9285               (gnus-dribble-enter
9286                (concat "(gnus-group-set-info '"
9287                        (gnus-prin1-to-string (gnus-get-info to-group))
9288                        ")"))))
9289
9290           ;; Update the Xref header in this article to point to
9291           ;; the new crossposted article we have just created.
9292           (when (eq action 'crosspost)
9293             (save-excursion
9294               (set-buffer copy-buf)
9295               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9296               (nnheader-replace-header "Xref" new-xref)
9297               (gnus-request-replace-article
9298                article gnus-newsgroup-name (current-buffer))))
9299
9300           ;; run the move/copy/crosspost/respool hook
9301           (run-hook-with-args 'gnus-summary-article-move-hook
9302                               action
9303                               (gnus-data-header
9304                                (assoc article (gnus-data-list nil)))
9305                               gnus-newsgroup-name
9306                               to-newsgroup
9307                               select-method))
9308
9309         ;;;!!!Why is this necessary?
9310         (set-buffer gnus-summary-buffer)
9311         
9312         (gnus-summary-goto-subject article)
9313         (when (eq action 'move)
9314           (gnus-summary-mark-article article gnus-canceled-mark))))
9315       (push article articles-to-update-marks))
9316
9317     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9318     ;; Re-activate all groups that have been moved to.
9319     (save-excursion
9320       (set-buffer gnus-group-buffer)
9321       (let ((gnus-group-marked to-groups))
9322         (gnus-group-get-new-news-this-group nil t)))
9323     
9324     (gnus-kill-buffer copy-buf)
9325     (gnus-summary-position-point)
9326     (gnus-set-mode-line 'summary)))
9327
9328 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9329   "Copy the current article to some other group.
9330 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9331 When called interactively, if TO-NEWSGROUP is nil, use the value of
9332 the variable `gnus-move-split-methods' for finding a default target
9333 newsgroup.
9334 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9335 re-spool using this method."
9336   (interactive "P")
9337   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9338
9339 (defun gnus-summary-crosspost-article (&optional n)
9340   "Crosspost the current article to some other group."
9341   (interactive "P")
9342   (gnus-summary-move-article n nil nil 'crosspost))
9343
9344 (defcustom gnus-summary-respool-default-method nil
9345   "Default method type for respooling an article.
9346 If nil, use to the current newsgroup method."
9347   :type 'symbol
9348   :group 'gnus-summary-mail)
9349
9350 (defcustom gnus-summary-display-while-building nil
9351   "If non-nil, show and update the summary buffer as it's being built.
9352 If the value is t, update the buffer after every line is inserted.  If
9353 the value is an integer (N), update the display every N lines."
9354   :version "22.1"
9355   :group 'gnus-thread
9356   :type '(choice (const :tag "off" nil)
9357                  number
9358                  (const :tag "frequently" t)))
9359
9360 (defun gnus-summary-respool-article (&optional n method)
9361   "Respool the current article.
9362 The article will be squeezed through the mail spooling process again,
9363 which means that it will be put in some mail newsgroup or other
9364 depending on `nnmail-split-methods'.
9365 If N is a positive number, respool the N next articles.
9366 If N is a negative number, respool the N previous articles.
9367 If N is nil and any articles have been marked with the process mark,
9368 respool those articles instead.
9369
9370 Respooling can be done both from mail groups and \"real\" newsgroups.
9371 In the former case, the articles in question will be moved from the
9372 current group into whatever groups they are destined to.  In the
9373 latter case, they will be copied into the relevant groups."
9374   (interactive
9375    (list current-prefix-arg
9376          (let* ((methods (gnus-methods-using 'respool))
9377                 (methname
9378                  (symbol-name (or gnus-summary-respool-default-method
9379                                   (car (gnus-find-method-for-group
9380                                         gnus-newsgroup-name)))))
9381                 (method
9382                  (gnus-completing-read-with-default
9383                   methname "Backend to use when respooling"
9384                   methods nil t nil 'gnus-mail-method-history))
9385                 ms)
9386            (cond
9387             ((zerop (length (setq ms (gnus-servers-using-backend
9388                                       (intern method)))))
9389              (list (intern method) ""))
9390             ((= 1 (length ms))
9391              (car ms))
9392             (t
9393              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9394                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9395                            ms-alist))))))))
9396   (unless method
9397     (error "No method given for respooling"))
9398   (if (assoc (symbol-name
9399               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9400              (gnus-methods-using 'respool))
9401       (gnus-summary-move-article n nil method)
9402     (gnus-summary-copy-article n nil method)))
9403
9404 (defun gnus-summary-import-article (file &optional edit)
9405   "Import an arbitrary file into a mail newsgroup."
9406   (interactive "fImport file: \nP")
9407   (let ((group gnus-newsgroup-name)
9408         (now (current-time))
9409         atts lines group-art)
9410     (unless (gnus-check-backend-function 'request-accept-article group)
9411       (error "%s does not support article importing" group))
9412     (or (file-readable-p file)
9413         (not (file-regular-p file))
9414         (error "Can't read %s" file))
9415     (save-excursion
9416       (set-buffer (gnus-get-buffer-create " *import file*"))
9417       (erase-buffer)
9418       (nnheader-insert-file-contents file)
9419       (goto-char (point-min))
9420       (if (nnheader-article-p)
9421           (save-restriction
9422             (goto-char (point-min))
9423             (search-forward "\n\n" nil t)
9424             (narrow-to-region (point-min) (1- (point)))
9425             (goto-char (point-min))
9426             (unless (re-search-forward "^date:" nil t)
9427               (goto-char (point-max))
9428               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9429        ;; This doesn't look like an article, so we fudge some headers.
9430         (setq atts (file-attributes file)
9431               lines (count-lines (point-min) (point-max)))
9432         (insert "From: " (read-string "From: ") "\n"
9433                 "Subject: " (read-string "Subject: ") "\n"
9434                 "Date: " (message-make-date (nth 5 atts)) "\n"
9435                 "Message-ID: " (message-make-message-id) "\n"
9436                 "Lines: " (int-to-string lines) "\n"
9437                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9438       (setq group-art (gnus-request-accept-article group nil t))
9439       (kill-buffer (current-buffer)))
9440     (setq gnus-newsgroup-active (gnus-activate-group group))
9441     (forward-line 1)
9442     (gnus-summary-goto-article (cdr group-art) nil t)
9443     (when edit
9444       (gnus-summary-edit-article))))
9445
9446 (defun gnus-summary-create-article ()
9447   "Create an article in a mail newsgroup."
9448   (interactive)
9449   (let ((group gnus-newsgroup-name)
9450         (now (current-time))
9451         group-art)
9452     (unless (gnus-check-backend-function 'request-accept-article group)
9453       (error "%s does not support article importing" group))
9454     (save-excursion
9455       (set-buffer (gnus-get-buffer-create " *import file*"))
9456       (erase-buffer)
9457       (goto-char (point-min))
9458       ;; This doesn't look like an article, so we fudge some headers.
9459       (insert "From: " (read-string "From: ") "\n"
9460               "Subject: " (read-string "Subject: ") "\n"
9461               "Date: " (message-make-date now) "\n"
9462               "Message-ID: " (message-make-message-id) "\n")
9463       (setq group-art (gnus-request-accept-article group nil t))
9464       (kill-buffer (current-buffer)))
9465     (setq gnus-newsgroup-active (gnus-activate-group group))
9466     (forward-line 1)
9467     (gnus-summary-goto-article (cdr group-art) nil t)
9468     (gnus-summary-edit-article)))
9469
9470 (defun gnus-summary-article-posted-p ()
9471   "Say whether the current (mail) article is available from news as well.
9472 This will be the case if the article has both been mailed and posted."
9473   (interactive)
9474   (let ((id (mail-header-references (gnus-summary-article-header)))
9475         (gnus-override-method (car (gnus-refer-article-methods))))
9476     (if (gnus-request-head id "")
9477         (gnus-message 2 "The current message was found on %s"
9478                       gnus-override-method)
9479       (gnus-message 2 "The current message couldn't be found on %s"
9480                     gnus-override-method)
9481       nil)))
9482
9483 (defun gnus-summary-expire-articles (&optional now)
9484   "Expire all articles that are marked as expirable in the current group."
9485   (interactive)
9486   (when (and (not gnus-group-is-exiting-without-update-p)
9487              (gnus-check-backend-function
9488               'request-expire-articles gnus-newsgroup-name))
9489     ;; This backend supports expiry.
9490     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9491            (expirable (if total
9492                           (progn
9493                             ;; We need to update the info for
9494                             ;; this group for `gnus-list-of-read-articles'
9495                             ;; to give us the right answer.
9496                             (gnus-run-hooks 'gnus-exit-group-hook)
9497                             (gnus-summary-update-info)
9498                             (gnus-list-of-read-articles gnus-newsgroup-name))
9499                         (setq gnus-newsgroup-expirable
9500                               (sort gnus-newsgroup-expirable '<))))
9501            (expiry-wait (if now 'immediate
9502                           (gnus-group-find-parameter
9503                            gnus-newsgroup-name 'expiry-wait)))
9504            (nnmail-expiry-target
9505             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9506                 nnmail-expiry-target))
9507            es)
9508       (when expirable
9509         ;; There are expirable articles in this group, so we run them
9510         ;; through the expiry process.
9511         (gnus-message 6 "Expiring articles...")
9512         (unless (gnus-check-group gnus-newsgroup-name)
9513           (error "Can't open server for %s" gnus-newsgroup-name))
9514         ;; The list of articles that weren't expired is returned.
9515         (save-excursion
9516           (if expiry-wait
9517               (let ((nnmail-expiry-wait-function nil)
9518                     (nnmail-expiry-wait expiry-wait))
9519                 (setq es (gnus-request-expire-articles
9520                           expirable gnus-newsgroup-name)))
9521             (setq es (gnus-request-expire-articles
9522                       expirable gnus-newsgroup-name)))
9523           (unless total
9524             (setq gnus-newsgroup-expirable es))
9525           ;; We go through the old list of expirable, and mark all
9526           ;; really expired articles as nonexistent.
9527           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9528             (let ((gnus-use-cache nil))
9529               (dolist (article expirable)
9530                 (when (and (not (memq article es))
9531                            (gnus-data-find article))
9532                   (gnus-summary-mark-article article gnus-canceled-mark)
9533                   (run-hook-with-args 'gnus-summary-article-expire-hook
9534                                       'delete
9535                                       (gnus-data-header
9536                                        (assoc article (gnus-data-list nil)))
9537                                       gnus-newsgroup-name
9538                                       nil
9539                                       nil))))))
9540         (gnus-message 6 "Expiring articles...done")))))
9541
9542 (defun gnus-summary-expire-articles-now ()
9543   "Expunge all expirable articles in the current group.
9544 This means that *all* articles that are marked as expirable will be
9545 deleted forever, right now."
9546   (interactive)
9547   (or gnus-expert-user
9548       (gnus-yes-or-no-p
9549        "Are you really, really, really sure you want to delete all these messages? ")
9550       (error "Phew!"))
9551   (gnus-summary-expire-articles t))
9552
9553 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9554 (defun gnus-summary-delete-article (&optional n)
9555   "Delete the N next (mail) articles.
9556 This command actually deletes articles.  This is not a marking
9557 command.  The article will disappear forever from your life, never to
9558 return.
9559
9560 If N is negative, delete backwards.
9561 If N is nil and articles have been marked with the process mark,
9562 delete these instead.
9563
9564 If `gnus-novice-user' is non-nil you will be asked for
9565 confirmation before the articles are deleted."
9566   (interactive "P")
9567   (unless (gnus-check-backend-function 'request-expire-articles
9568                                        gnus-newsgroup-name)
9569     (error "The current newsgroup does not support article deletion"))
9570   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9571     (error "Couldn't open server"))
9572   ;; Compute the list of articles to delete.
9573   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9574         (nnmail-expiry-target 'delete)
9575         not-deleted)
9576     (if (and gnus-novice-user
9577              (not (gnus-yes-or-no-p
9578                    (format "Do you really want to delete %s forever? "
9579                            (if (> (length articles) 1)
9580                                (format "these %s articles" (length articles))
9581                              "this article")))))
9582         ()
9583       ;; Delete the articles.
9584       (setq not-deleted (gnus-request-expire-articles
9585                          articles gnus-newsgroup-name 'force))
9586       (while articles
9587         (gnus-summary-remove-process-mark (car articles))
9588         ;; The backend might not have been able to delete the article
9589         ;; after all.
9590         (unless (memq (car articles) not-deleted)
9591           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9592         (let* ((article (car articles))
9593                (ghead  (gnus-data-header
9594                                     (assoc article (gnus-data-list nil)))))
9595           (run-hook-with-args 'gnus-summary-article-delete-hook
9596                               'delete ghead gnus-newsgroup-name nil
9597                               nil))
9598         (setq articles (cdr articles)))
9599       (when not-deleted
9600         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9601     (gnus-summary-position-point)
9602     (gnus-set-mode-line 'summary)
9603     not-deleted))
9604
9605 (defun gnus-summary-edit-article (&optional arg)
9606   "Edit the current article.
9607 This will have permanent effect only in mail groups.
9608 If ARG is nil, edit the decoded articles.
9609 If ARG is 1, edit the raw articles.
9610 If ARG is 2, edit the raw articles even in read-only groups.
9611 If ARG is 3, edit the articles with the current handles.
9612 Otherwise, allow editing of articles even in read-only
9613 groups."
9614   (interactive "P")
9615   (let (force raw current-handles)
9616     (cond
9617      ((null arg))
9618      ((eq arg 1)
9619       (setq raw t))
9620      ((eq arg 2)
9621       (setq raw t
9622             force t))
9623      ((eq arg 3)
9624       (setq current-handles
9625             (and (gnus-buffer-live-p gnus-article-buffer)
9626                  (with-current-buffer gnus-article-buffer
9627                    (prog1
9628                        gnus-article-mime-handles
9629                      (setq gnus-article-mime-handles nil))))))
9630      (t
9631       (setq force t)))
9632     (when (and raw (not force)
9633                (member gnus-newsgroup-name '("nndraft:delayed"
9634                                              "nndraft:drafts"
9635                                              "nndraft:queue")))
9636       (error "Can't edit the raw article in group %s"
9637              gnus-newsgroup-name))
9638     (save-excursion
9639       (set-buffer gnus-summary-buffer)
9640       (let ((mail-parse-charset gnus-newsgroup-charset)
9641             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9642         (gnus-set-global-variables)
9643         (when (and (not force)
9644                    (gnus-group-read-only-p))
9645           (error "The current newsgroup does not support article editing"))
9646         (gnus-summary-show-article t)
9647         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9648           (with-current-buffer gnus-article-buffer
9649             (mm-enable-multibyte)))
9650         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9651             (setq raw t))
9652         (gnus-article-edit-article
9653          (if raw 'ignore
9654            `(lambda ()
9655               (let ((mbl mml-buffer-list))
9656                 (setq mml-buffer-list nil)
9657                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9658                   (mime-to-mml ,'current-handles))
9659                 (let ((mbl1 mml-buffer-list))
9660                   (setq mml-buffer-list mbl)
9661                   (set (make-local-variable 'mml-buffer-list) mbl1))
9662                 (gnus-make-local-hook 'kill-buffer-hook)
9663                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9664          `(lambda (no-highlight)
9665             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9666                   (message-options message-options)
9667                   (message-options-set-recipient)
9668                   (mail-parse-ignored-charsets
9669                    ',gnus-newsgroup-ignored-charsets))
9670               ,(if (not raw) '(progn
9671                                 (mml-to-mime)
9672                                 (mml-destroy-buffers)
9673                                 (remove-hook 'kill-buffer-hook
9674                                              'mml-destroy-buffers t)
9675                                 (kill-local-variable 'mml-buffer-list)))
9676               (gnus-summary-edit-article-done
9677                ,(or (mail-header-references gnus-current-headers) "")
9678                ,(gnus-group-read-only-p)
9679                ,gnus-summary-buffer no-highlight))))))))
9680
9681 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9682
9683 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9684                                                  no-highlight)
9685   "Make edits to the current article permanent."
9686   (interactive)
9687   (save-excursion
9688    ;; The buffer restriction contains the entire article if it exists.
9689     (when (article-goto-body)
9690       (let ((lines (count-lines (point) (point-max)))
9691             (length (- (point-max) (point)))
9692             (case-fold-search t)
9693             (body (copy-marker (point))))
9694         (goto-char (point-min))
9695         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9696           (delete-region (match-beginning 1) (match-end 1))
9697           (insert (number-to-string length)))
9698         (goto-char (point-min))
9699         (when (re-search-forward
9700                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9701           (delete-region (match-beginning 1) (match-end 1))
9702           (insert (number-to-string length)))
9703         (goto-char (point-min))
9704         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9705           (delete-region (match-beginning 1) (match-end 1))
9706           (insert (number-to-string lines))))))
9707   ;; Replace the article.
9708   (let ((buf (current-buffer)))
9709     (with-temp-buffer
9710       (insert-buffer-substring buf)
9711
9712       (if (and (not read-only)
9713                (not (gnus-request-replace-article
9714                      (cdr gnus-article-current) (car gnus-article-current)
9715                      (current-buffer) t)))
9716           (error "Couldn't replace article")
9717         ;; Update the summary buffer.
9718         (if (and references
9719                  (equal (message-tokenize-header references " ")
9720                         (message-tokenize-header
9721                          (or (message-fetch-field "references") "") " ")))
9722             ;; We only have to update this line.
9723             (save-excursion
9724               (save-restriction
9725                 (message-narrow-to-head)
9726                 (let ((head (buffer-substring-no-properties
9727                              (point-min) (point-max)))
9728                       header)
9729                   (with-temp-buffer
9730                     (insert (format "211 %d Article retrieved.\n"
9731                                     (cdr gnus-article-current)))
9732                     (insert head)
9733                     (insert ".\n")
9734                     (let ((nntp-server-buffer (current-buffer)))
9735                       (setq header (car (gnus-get-newsgroup-headers
9736                                          nil t))))
9737                     (save-excursion
9738                       (set-buffer gnus-summary-buffer)
9739                       (gnus-data-set-header
9740                        (gnus-data-find (cdr gnus-article-current))
9741                        header)
9742                       (gnus-summary-update-article-line
9743                        (cdr gnus-article-current) header)
9744                       (if (gnus-summary-goto-subject
9745                            (cdr gnus-article-current) nil t)
9746                           (gnus-summary-update-secondary-mark
9747                            (cdr gnus-article-current))))))))
9748           ;; Update threads.
9749           (set-buffer (or buffer gnus-summary-buffer))
9750           (gnus-summary-update-article (cdr gnus-article-current))
9751           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9752               (gnus-summary-update-secondary-mark
9753                (cdr gnus-article-current))))
9754         ;; Prettify the article buffer again.
9755         (unless no-highlight
9756           (save-excursion
9757             (set-buffer gnus-article-buffer)
9758             ;;;!!! Fix this -- article should be rehighlighted.
9759             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9760             (set-buffer gnus-original-article-buffer)
9761             (gnus-request-article
9762              (cdr gnus-article-current)
9763              (car gnus-article-current) (current-buffer))))
9764         ;; Prettify the summary buffer line.
9765         (when (gnus-visual-p 'summary-highlight 'highlight)
9766           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9767
9768 (defun gnus-summary-edit-wash (key)
9769   "Perform editing command KEY in the article buffer."
9770   (interactive
9771    (list
9772     (progn
9773       (message "%s" (concat (this-command-keys) "- "))
9774       (read-char))))
9775   (message "")
9776   (gnus-summary-edit-article)
9777   (execute-kbd-macro (concat (this-command-keys) key))
9778   (gnus-article-edit-done))
9779
9780 ;;; Respooling
9781
9782 (defun gnus-summary-respool-query (&optional silent trace)
9783   "Query where the respool algorithm would put this article."
9784   (interactive)
9785   (let (gnus-mark-article-hook)
9786     (gnus-summary-select-article)
9787     (save-excursion
9788       (set-buffer gnus-original-article-buffer)
9789       (let ((groups (nnmail-article-group 'identity trace)))
9790         (unless silent
9791           (if groups
9792               (message "This message would go to %s"
9793                        (mapconcat 'car groups ", "))
9794             (message "This message would go to no groups"))
9795           groups)))))
9796
9797 (defun gnus-summary-respool-trace ()
9798   "Trace where the respool algorithm would put this article.
9799 Display a buffer showing all fancy splitting patterns which matched."
9800   (interactive)
9801   (gnus-summary-respool-query nil t))
9802
9803 ;; Summary marking commands.
9804
9805 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9806   "Mark articles which has the same subject as read, and then select the next.
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     ;; Select next unread article.  If auto-select-same mode, should
9816     ;; select the first unread article.
9817     (gnus-summary-next-article t (and gnus-auto-select-same
9818                                       (gnus-summary-article-subject)))
9819     (gnus-message 7 "%d article%s marked as %s"
9820                   count (if (= count 1) " is" "s are")
9821                   (if unmark "unread" "read"))))
9822
9823 (defun gnus-summary-kill-same-subject (&optional unmark)
9824   "Mark articles which has the same subject as read.
9825 If UNMARK is positive, remove any kind of mark.
9826 If UNMARK is negative, tick articles."
9827   (interactive "P")
9828   (when unmark
9829     (setq unmark (prefix-numeric-value unmark)))
9830   (let ((count
9831          (gnus-summary-mark-same-subject
9832           (gnus-summary-article-subject) unmark)))
9833     ;; If marked as read, go to next unread subject.
9834     (when (null unmark)
9835       ;; Go to next unread subject.
9836       (gnus-summary-next-subject 1 t))
9837     (gnus-message 7 "%d articles are marked as %s"
9838                   count (if unmark "unread" "read"))))
9839
9840 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9841   "Mark articles with same SUBJECT as read, and return marked number.
9842 If optional argument UNMARK is positive, remove any kinds of marks.
9843 If optional argument UNMARK is negative, mark articles as unread instead."
9844   (let ((count 1))
9845     (save-excursion
9846       (cond
9847        ((null unmark)                   ; Mark as read.
9848         (while (and
9849                 (progn
9850                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9851                   (gnus-summary-show-thread) t)
9852                 (gnus-summary-find-subject subject))
9853           (setq count (1+ count))))
9854        ((> unmark 0)                    ; Tick.
9855         (while (and
9856                 (progn
9857                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9858                   (gnus-summary-show-thread) t)
9859                 (gnus-summary-find-subject subject))
9860           (setq count (1+ count))))
9861        (t                               ; Mark as unread.
9862         (while (and
9863                 (progn
9864                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9865                   (gnus-summary-show-thread) t)
9866                 (gnus-summary-find-subject subject))
9867           (setq count (1+ count)))))
9868       (gnus-set-mode-line 'summary)
9869       ;; Return the number of marked articles.
9870       count)))
9871
9872 (defun gnus-summary-mark-as-processable (n &optional unmark)
9873   "Set the process mark on the next N articles.
9874 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9875 the process mark instead.  The difference between N and the actual
9876 number of articles marked is returned."
9877   (interactive "P")
9878   (if (and (null n) (gnus-region-active-p))
9879       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9880     (setq n (prefix-numeric-value n))
9881     (let ((backward (< n 0))
9882           (n (abs n)))
9883       (while (and
9884               (> n 0)
9885               (if unmark
9886                   (gnus-summary-remove-process-mark
9887                    (gnus-summary-article-number))
9888                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9889               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9890         (setq n (1- n)))
9891       (when (/= 0 n)
9892         (gnus-message 7 "No more articles"))
9893       (gnus-summary-recenter)
9894       (gnus-summary-position-point)
9895       n)))
9896
9897 (defun gnus-summary-unmark-as-processable (n)
9898   "Remove the process mark from the next N articles.
9899 If N is negative, unmark backward instead.  The difference between N and
9900 the actual number of articles unmarked is returned."
9901   (interactive "P")
9902   (gnus-summary-mark-as-processable n t))
9903
9904 (defun gnus-summary-unmark-all-processable ()
9905   "Remove the process mark from all articles."
9906   (interactive)
9907   (save-excursion
9908     (while gnus-newsgroup-processable
9909       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9910   (gnus-summary-position-point))
9911
9912 (defun gnus-summary-add-mark (article type)
9913   "Mark ARTICLE with a mark of TYPE."
9914   (let ((vtype (car (assq type gnus-article-mark-lists)))
9915         var)
9916     (if (not vtype)
9917         (error "No such mark type: %s" type)
9918       (setq var (intern (format "gnus-newsgroup-%s" type)))
9919       (set var (cons article (symbol-value var)))
9920       (if (memq type '(processable cached replied forwarded recent saved))
9921           (gnus-summary-update-secondary-mark article)
9922         ;;; !!! This is bogus.  We should find out what primary
9923         ;;; !!! mark we want to set.
9924         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9925
9926 (defun gnus-summary-mark-as-expirable (n)
9927   "Mark N articles forward as expirable.
9928 If N is negative, mark backward instead.  The difference between N and
9929 the actual number of articles marked is returned."
9930   (interactive "p")
9931   (gnus-summary-mark-forward n gnus-expirable-mark))
9932
9933 (defun gnus-summary-mark-as-spam (n)
9934   "Mark N articles forward as spam.
9935 If N is negative, mark backward instead.  The difference between N and
9936 the actual number of articles marked is returned."
9937   (interactive "p")
9938   (gnus-summary-mark-forward n gnus-spam-mark))
9939
9940 (defun gnus-summary-mark-article-as-replied (article)
9941   "Mark ARTICLE as replied to and update the summary line.
9942 ARTICLE can also be a list of articles."
9943   (interactive (list (gnus-summary-article-number)))
9944   (let ((articles (if (listp article) article (list article))))
9945     (dolist (article articles)
9946       (unless (numberp article)
9947         (error "%s is not a number" article))
9948       (push article gnus-newsgroup-replied)
9949       (let ((buffer-read-only nil))
9950         (when (gnus-summary-goto-subject article nil t)
9951           (gnus-summary-update-secondary-mark article))))))
9952
9953 (defun gnus-summary-mark-article-as-forwarded (article)
9954   "Mark ARTICLE as forwarded and update the summary line.
9955 ARTICLE can also be a list of articles."
9956   (let ((articles (if (listp article) article (list article))))
9957     (dolist (article articles)
9958       (push article gnus-newsgroup-forwarded)
9959       (let ((buffer-read-only nil))
9960         (when (gnus-summary-goto-subject article nil t)
9961           (gnus-summary-update-secondary-mark article))))))
9962
9963 (defun gnus-summary-set-bookmark (article)
9964   "Set a bookmark in current article."
9965   (interactive (list (gnus-summary-article-number)))
9966   (when (or (not (get-buffer gnus-article-buffer))
9967             (not gnus-current-article)
9968             (not gnus-article-current)
9969             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9970     (error "No current article selected"))
9971   ;; Remove old bookmark, if one exists.
9972   (gnus-pull article gnus-newsgroup-bookmarks)
9973   ;; Set the new bookmark, which is on the form
9974   ;; (article-number . line-number-in-body).
9975   (push
9976    (cons article
9977          (with-current-buffer gnus-article-buffer
9978            (count-lines
9979             (min (point)
9980                  (save-excursion
9981                    (article-goto-body)
9982                    (point)))
9983             (point))))
9984    gnus-newsgroup-bookmarks)
9985   (gnus-message 6 "A bookmark has been added to the current article."))
9986
9987 (defun gnus-summary-remove-bookmark (article)
9988   "Remove the bookmark from the current article."
9989   (interactive (list (gnus-summary-article-number)))
9990   ;; Remove old bookmark, if one exists.
9991   (if (not (assq article gnus-newsgroup-bookmarks))
9992       (gnus-message 6 "No bookmark in current article.")
9993     (gnus-pull article gnus-newsgroup-bookmarks)
9994     (gnus-message 6 "Removed bookmark.")))
9995
9996 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9997 (defun gnus-summary-mark-as-dormant (n)
9998   "Mark N articles forward as dormant.
9999 If N is negative, mark backward instead.  The difference between N and
10000 the actual number of articles marked is returned."
10001   (interactive "p")
10002   (gnus-summary-mark-forward n gnus-dormant-mark))
10003
10004 (defun gnus-summary-set-process-mark (article)
10005   "Set the process mark on ARTICLE and update the summary line."
10006   (setq gnus-newsgroup-processable
10007         (cons article
10008               (delq article gnus-newsgroup-processable)))
10009   (when (gnus-summary-goto-subject article)
10010     (gnus-summary-show-thread)
10011     (gnus-summary-goto-subject article)
10012     (gnus-summary-update-secondary-mark article)))
10013
10014 (defun gnus-summary-remove-process-mark (&rest articles)
10015   "Remove the process mark from ARTICLES and update the summary line."
10016   (dolist (article articles)
10017     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10018     (when (gnus-summary-goto-subject article)
10019       (gnus-summary-show-thread)
10020       (gnus-summary-goto-subject article)
10021       (gnus-summary-update-secondary-mark article))))
10022
10023 (defun gnus-summary-set-saved-mark (article)
10024   "Set the process mark on ARTICLE and update the summary line."
10025   (push article gnus-newsgroup-saved)
10026   (when (gnus-summary-goto-subject article)
10027     (gnus-summary-update-secondary-mark article)))
10028
10029 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10030   "Mark N articles as read forwards.
10031 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10032 The difference between N and the actual number of articles marked is
10033 returned.
10034 If NO-EXPIRE, auto-expiry will be inhibited."
10035   (interactive "p")
10036   (gnus-summary-show-thread)
10037   (let ((backward (< n 0))
10038         (gnus-summary-goto-unread
10039          (and gnus-summary-goto-unread
10040               (not (eq gnus-summary-goto-unread 'never))
10041               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10042                                     gnus-ticked-mark gnus-dormant-mark)))))
10043         (n (abs n))
10044         (mark (or mark gnus-del-mark)))
10045     (while (and (> n 0)
10046                 (gnus-summary-mark-article nil mark no-expire)
10047                 (zerop (gnus-summary-next-subject
10048                         (if backward -1 1)
10049                         (and gnus-summary-goto-unread
10050                              (not (eq gnus-summary-goto-unread 'never)))
10051                         t)))
10052       (setq n (1- n)))
10053     (when (/= 0 n)
10054       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10055     (gnus-summary-recenter)
10056     (gnus-summary-position-point)
10057     (gnus-set-mode-line 'summary)
10058     n))
10059
10060 (defun gnus-summary-mark-article-as-read (mark)
10061   "Mark the current article quickly as read with MARK."
10062   (let ((article (gnus-summary-article-number)))
10063     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10064     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10065     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10066     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10067     (push (cons article mark) gnus-newsgroup-reads)
10068     ;; Possibly remove from cache, if that is used.
10069     (when gnus-use-cache
10070       (gnus-cache-enter-remove-article article))
10071     ;; Allow the backend to change the mark.
10072     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10073     ;; Check for auto-expiry.
10074     (when (and gnus-newsgroup-auto-expire
10075                (memq mark gnus-auto-expirable-marks))
10076       (setq mark gnus-expirable-mark)
10077       ;; Let the backend know about the mark change.
10078       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10079       (push article gnus-newsgroup-expirable))
10080     ;; Set the mark in the buffer.
10081     (gnus-summary-update-mark mark 'unread)
10082     t))
10083
10084 (defun gnus-summary-mark-article-as-unread (mark)
10085   "Mark the current article quickly as unread with MARK."
10086   (let* ((article (gnus-summary-article-number))
10087          (old-mark (gnus-summary-article-mark article)))
10088     ;; Allow the backend to change the mark.
10089     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10090     (if (eq mark old-mark)
10091         t
10092       (if (<= article 0)
10093           (progn
10094             (gnus-error 1 "Can't mark negative article numbers")
10095             nil)
10096         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10097         (setq gnus-newsgroup-spam-marked
10098               (delq article gnus-newsgroup-spam-marked))
10099         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10100         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10101         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10102         (cond ((= mark gnus-ticked-mark)
10103                (setq gnus-newsgroup-marked
10104                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10105                                               article)))
10106               ((= mark gnus-spam-mark)
10107                (setq gnus-newsgroup-spam-marked
10108                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10109                                               article)))
10110               ((= mark gnus-dormant-mark)
10111                (setq gnus-newsgroup-dormant
10112                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10113                                               article)))
10114               (t
10115                (setq gnus-newsgroup-unreads
10116                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10117                                               article))))
10118         (gnus-pull article gnus-newsgroup-reads)
10119
10120         ;; See whether the article is to be put in the cache.
10121         (and gnus-use-cache
10122              (vectorp (gnus-summary-article-header article))
10123              (save-excursion
10124                (gnus-cache-possibly-enter-article
10125                 gnus-newsgroup-name article
10126                 (= mark gnus-ticked-mark)
10127                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10128
10129         ;; Fix the mark.
10130         (gnus-summary-update-mark mark 'unread)
10131         t))))
10132
10133 (defun gnus-summary-mark-article (&optional article mark no-expire)
10134   "Mark ARTICLE with MARK.  MARK can be any character.
10135 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10136 `??' (dormant) and `?E' (expirable).
10137 If MARK is nil, then the default character `?r' is used.
10138 If ARTICLE is nil, then the article on the current line will be
10139 marked.
10140 If NO-EXPIRE, auto-expiry will be inhibited."
10141   ;; The mark might be a string.
10142   (when (stringp mark)
10143     (setq mark (aref mark 0)))
10144   ;; If no mark is given, then we check auto-expiring.
10145   (when (null mark)
10146     (setq mark gnus-del-mark))
10147   (when (and (not no-expire)
10148              gnus-newsgroup-auto-expire
10149              (memq mark gnus-auto-expirable-marks))
10150     (setq mark gnus-expirable-mark))
10151   (let ((article (or article (gnus-summary-article-number)))
10152         (old-mark (gnus-summary-article-mark article)))
10153     ;; Allow the backend to change the mark.
10154     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10155     (if (eq mark old-mark)
10156         t
10157       (unless article
10158         (error "No article on current line"))
10159       (if (not (if (or (= mark gnus-unread-mark)
10160                        (= mark gnus-ticked-mark)
10161                        (= mark gnus-spam-mark)
10162                        (= mark gnus-dormant-mark))
10163                    (gnus-mark-article-as-unread article mark)
10164                  (gnus-mark-article-as-read article mark)))
10165           t
10166         ;; See whether the article is to be put in the cache.
10167         (and gnus-use-cache
10168              (not (= mark gnus-canceled-mark))
10169              (vectorp (gnus-summary-article-header article))
10170              (save-excursion
10171                (gnus-cache-possibly-enter-article
10172                 gnus-newsgroup-name article
10173                 (= mark gnus-ticked-mark)
10174                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10175
10176         (when (gnus-summary-goto-subject article nil t)
10177           (let ((buffer-read-only nil))
10178             (gnus-summary-show-thread)
10179             ;; Fix the mark.
10180             (gnus-summary-update-mark mark 'unread)
10181             t))))))
10182
10183 (defun gnus-summary-update-secondary-mark (article)
10184   "Update the secondary (read, process, cache) mark."
10185   (gnus-summary-update-mark
10186    (cond ((memq article gnus-newsgroup-processable)
10187           gnus-process-mark)
10188          ((memq article gnus-newsgroup-cached)
10189           gnus-cached-mark)
10190          ((memq article gnus-newsgroup-replied)
10191           gnus-replied-mark)
10192          ((memq article gnus-newsgroup-forwarded)
10193           gnus-forwarded-mark)
10194          ((memq article gnus-newsgroup-saved)
10195           gnus-saved-mark)
10196          ((memq article gnus-newsgroup-recent)
10197           gnus-recent-mark)
10198          ((memq article gnus-newsgroup-unseen)
10199           gnus-unseen-mark)
10200          (t gnus-no-mark))
10201    'replied)
10202   (when (gnus-visual-p 'summary-highlight 'highlight)
10203     (gnus-run-hooks 'gnus-summary-update-hook))
10204   t)
10205
10206 (defun gnus-summary-update-download-mark (article)
10207   "Update the download mark."
10208   (gnus-summary-update-mark
10209    (cond ((memq article gnus-newsgroup-undownloaded)
10210           gnus-undownloaded-mark)
10211          (gnus-newsgroup-agentized
10212           gnus-downloaded-mark)
10213          (t
10214           gnus-no-mark))
10215    'download)
10216   (gnus-summary-update-line t)
10217   t)
10218
10219 (defun gnus-summary-update-mark (mark type)
10220   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10221         (buffer-read-only nil))
10222     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10223     (when forward
10224       (when (looking-at "\r")
10225         (incf forward))
10226       (when (<= (+ forward (point)) (point-max))
10227         ;; Go to the right position on the line.
10228         (goto-char (+ forward (point)))
10229         ;; Replace the old mark with the new mark.
10230         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10231         ;; Optionally update the marks by some user rule.
10232         (when (eq type 'unread)
10233           (gnus-data-set-mark
10234            (gnus-data-find (gnus-summary-article-number)) mark)
10235           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10236
10237 (defun gnus-mark-article-as-read (article &optional mark)
10238   "Enter ARTICLE in the pertinent lists and remove it from others."
10239   ;; Make the article expirable.
10240   (let ((mark (or mark gnus-del-mark)))
10241     (setq gnus-newsgroup-expirable
10242           (if (= mark gnus-expirable-mark)
10243               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10244             (delq article gnus-newsgroup-expirable)))
10245     ;; Remove from unread and marked lists.
10246     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10247     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10248     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10249     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10250     (push (cons article mark) gnus-newsgroup-reads)
10251     ;; Possibly remove from cache, if that is used.
10252     (when gnus-use-cache
10253       (gnus-cache-enter-remove-article article))
10254     t))
10255
10256 (defun gnus-mark-article-as-unread (article &optional mark)
10257   "Enter ARTICLE in the pertinent lists and remove it from others."
10258   (let ((mark (or mark gnus-ticked-mark)))
10259     (if (<= article 0)
10260         (progn
10261           (gnus-error 1 "Can't mark negative article numbers")
10262           nil)
10263       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10264             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10265             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10266             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10267             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10268
10269       ;; Unsuppress duplicates?
10270       (when gnus-suppress-duplicates
10271         (gnus-dup-unsuppress-article article))
10272
10273       (cond ((= mark gnus-ticked-mark)
10274              (setq gnus-newsgroup-marked
10275                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10276             ((= mark gnus-spam-mark)
10277              (setq gnus-newsgroup-spam-marked
10278                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10279                                             article)))
10280             ((= mark gnus-dormant-mark)
10281              (setq gnus-newsgroup-dormant
10282                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10283             (t
10284              (setq gnus-newsgroup-unreads
10285                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10286       (gnus-pull article gnus-newsgroup-reads)
10287       t)))
10288
10289 (defalias 'gnus-summary-mark-as-unread-forward
10290   'gnus-summary-tick-article-forward)
10291 (make-obsolete 'gnus-summary-mark-as-unread-forward
10292                'gnus-summary-tick-article-forward)
10293 (defun gnus-summary-tick-article-forward (n)
10294   "Tick N articles forwards.
10295 If N is negative, tick backwards instead.
10296 The difference between N and the number of articles ticked is returned."
10297   (interactive "p")
10298   (gnus-summary-mark-forward n gnus-ticked-mark))
10299
10300 (defalias 'gnus-summary-mark-as-unread-backward
10301   'gnus-summary-tick-article-backward)
10302 (make-obsolete 'gnus-summary-mark-as-unread-backward
10303                'gnus-summary-tick-article-backward)
10304 (defun gnus-summary-tick-article-backward (n)
10305   "Tick N articles backwards.
10306 The difference between N and the number of articles ticked is returned."
10307   (interactive "p")
10308   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10309
10310 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10311 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10312 (defun gnus-summary-tick-article (&optional article clear-mark)
10313   "Mark current article as unread.
10314 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10315 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10316   (interactive)
10317   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10318                                        gnus-ticked-mark)))
10319
10320 (defun gnus-summary-mark-as-read-forward (n)
10321   "Mark N articles as read forwards.
10322 If N is negative, mark backwards instead.
10323 The difference between N and the actual number of articles marked is
10324 returned."
10325   (interactive "p")
10326   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10327
10328 (defun gnus-summary-mark-as-read-backward (n)
10329   "Mark the N articles as read backwards.
10330 The difference between N and the actual number of articles marked is
10331 returned."
10332   (interactive "p")
10333   (gnus-summary-mark-forward
10334    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10335
10336 (defun gnus-summary-mark-as-read (&optional article mark)
10337   "Mark current article as read.
10338 ARTICLE specifies the article to be marked as read.
10339 MARK specifies a string to be inserted at the beginning of the line."
10340   (gnus-summary-mark-article article mark))
10341
10342 (defun gnus-summary-clear-mark-forward (n)
10343   "Clear marks from N articles forward.
10344 If N is negative, clear backward instead.
10345 The difference between N and the number of marks cleared is returned."
10346   (interactive "p")
10347   (gnus-summary-mark-forward n gnus-unread-mark))
10348
10349 (defun gnus-summary-clear-mark-backward (n)
10350   "Clear marks from N articles backward.
10351 The difference between N and the number of marks cleared is returned."
10352   (interactive "p")
10353   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10354
10355 (defun gnus-summary-mark-unread-as-read ()
10356   "Intended to be used by `gnus-summary-mark-article-hook'."
10357   (when (memq gnus-current-article gnus-newsgroup-unreads)
10358     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10359
10360 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10361   "Intended to be used by `gnus-summary-mark-article-hook'."
10362   (let ((mark (gnus-summary-article-mark)))
10363     (when (or (gnus-unread-mark-p mark)
10364               (gnus-read-mark-p mark))
10365       (gnus-summary-mark-article gnus-current-article
10366                                  (or new-mark gnus-read-mark)))))
10367
10368 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10369   "Intended to be used by `gnus-summary-mark-article-hook'."
10370   (let ((mark (gnus-summary-article-mark)))
10371     (when (or (gnus-unread-mark-p mark)
10372               (gnus-read-mark-p mark))
10373       (gnus-summary-mark-article (gnus-summary-article-number)
10374                                  (or new-mark gnus-read-mark)))))
10375
10376 (defun gnus-summary-mark-unread-as-ticked ()
10377   "Intended to be used by `gnus-summary-mark-article-hook'."
10378   (when (memq gnus-current-article gnus-newsgroup-unreads)
10379     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10380
10381 (defun gnus-summary-mark-region-as-read (point mark all)
10382   "Mark all unread articles between point and mark as read.
10383 If given a prefix, mark all articles between point and mark as read,
10384 even ticked and dormant ones."
10385   (interactive "r\nP")
10386   (save-excursion
10387     (let (article)
10388       (goto-char point)
10389       (beginning-of-line)
10390       (while (and
10391               (< (point) mark)
10392               (progn
10393                 (when (or all
10394                           (memq (setq article (gnus-summary-article-number))
10395                                 gnus-newsgroup-unreads))
10396                   (gnus-summary-mark-article article gnus-del-mark))
10397                 t)
10398               (gnus-summary-find-next))))))
10399
10400 (defun gnus-summary-mark-below (score mark)
10401   "Mark articles with score less than SCORE with MARK."
10402   (interactive "P\ncMark: ")
10403   (setq score (if score
10404                   (prefix-numeric-value score)
10405                 (or gnus-summary-default-score 0)))
10406   (save-excursion
10407     (set-buffer gnus-summary-buffer)
10408     (goto-char (point-min))
10409     (while
10410         (progn
10411           (and (< (gnus-summary-article-score) score)
10412                (gnus-summary-mark-article nil mark))
10413           (gnus-summary-find-next)))))
10414
10415 (defun gnus-summary-kill-below (&optional score)
10416   "Mark articles with score below SCORE as read."
10417   (interactive "P")
10418   (gnus-summary-mark-below score gnus-killed-mark))
10419
10420 (defun gnus-summary-clear-above (&optional score)
10421   "Clear all marks from articles with score above SCORE."
10422   (interactive "P")
10423   (gnus-summary-mark-above score gnus-unread-mark))
10424
10425 (defun gnus-summary-tick-above (&optional score)
10426   "Tick all articles with score above SCORE."
10427   (interactive "P")
10428   (gnus-summary-mark-above score gnus-ticked-mark))
10429
10430 (defun gnus-summary-mark-above (score mark)
10431   "Mark articles with score over SCORE with MARK."
10432   (interactive "P\ncMark: ")
10433   (setq score (if score
10434                   (prefix-numeric-value score)
10435                 (or gnus-summary-default-score 0)))
10436   (save-excursion
10437     (set-buffer gnus-summary-buffer)
10438     (goto-char (point-min))
10439     (while (and (progn
10440                   (when (> (gnus-summary-article-score) score)
10441                     (gnus-summary-mark-article nil mark))
10442                   t)
10443                 (gnus-summary-find-next)))))
10444
10445 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10446 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10447 (defun gnus-summary-limit-include-expunged (&optional no-error)
10448   "Display all the hidden articles that were expunged for low scores."
10449   (interactive)
10450   (let ((buffer-read-only nil))
10451     (let ((scored gnus-newsgroup-scored)
10452           headers h)
10453       (while scored
10454         (unless (gnus-summary-article-header (caar scored))
10455           (and (setq h (gnus-number-to-header (caar scored)))
10456                (< (cdar scored) gnus-summary-expunge-below)
10457                (push h headers)))
10458         (setq scored (cdr scored)))
10459       (if (not headers)
10460           (when (not no-error)
10461             (error "No expunged articles hidden"))
10462         (goto-char (point-min))
10463         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10464         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10465         (mapcar (lambda (x) (push (mail-header-number x)
10466                                   gnus-newsgroup-limit))
10467                 headers)
10468         (gnus-summary-prepare-unthreaded (nreverse headers))
10469         (goto-char (point-min))
10470         (gnus-summary-position-point)
10471         t))))
10472
10473 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10474   "Mark all unread articles in this newsgroup as read.
10475 If prefix argument ALL is non-nil, ticked and dormant articles will
10476 also be marked as read.
10477 If QUIETLY is non-nil, no questions will be asked.
10478
10479 If TO-HERE is non-nil, it should be a point in the buffer.  All
10480 articles before (after, if REVERSE is set) this point will be marked
10481 as read.
10482
10483 Note that this function will only catch up the unread article
10484 in the current summary buffer limitation.
10485
10486 The number of articles marked as read is returned."
10487   (interactive "P")
10488   (prog1
10489       (save-excursion
10490         (when (or quietly
10491                   (not gnus-interactive-catchup) ;Without confirmation?
10492                   gnus-expert-user
10493                   (gnus-y-or-n-p
10494                    (if all
10495                        "Mark absolutely all articles as read? "
10496                      "Mark all unread articles as read? ")))
10497           (if (and not-mark
10498                    (not gnus-newsgroup-adaptive)
10499                    (not gnus-newsgroup-auto-expire)
10500                    (not gnus-suppress-duplicates)
10501                    (or (not gnus-use-cache)
10502                        (eq gnus-use-cache 'passive)))
10503               (progn
10504                 (when all
10505                   (setq gnus-newsgroup-marked nil
10506                         gnus-newsgroup-spam-marked nil
10507                         gnus-newsgroup-dormant nil))
10508                 (setq gnus-newsgroup-unreads
10509                       (gnus-sorted-nunion
10510                        (gnus-intersection gnus-newsgroup-unreads
10511                                           gnus-newsgroup-downloadable)
10512                        gnus-newsgroup-unfetched)))
10513             ;; We actually mark all articles as canceled, which we
10514             ;; have to do when using auto-expiry or adaptive scoring.
10515             (gnus-summary-show-all-threads)
10516             (if (and to-here reverse)
10517                 (progn
10518                   (goto-char to-here)
10519                   (gnus-summary-mark-current-read-and-unread-as-read
10520                    gnus-catchup-mark)
10521                   (while (gnus-summary-find-next (not all))
10522                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10523               (when (gnus-summary-first-subject (not all))
10524                 (while (and
10525                         (if to-here (< (point) to-here) t)
10526                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10527                         (gnus-summary-find-next (not all))))))
10528             (gnus-set-mode-line 'summary))
10529           t))
10530     (gnus-summary-position-point)))
10531
10532 (defun gnus-summary-catchup-to-here (&optional all)
10533   "Mark all unticked articles before the current one as read.
10534 If ALL is non-nil, also mark ticked and dormant articles as read."
10535   (interactive "P")
10536   (save-excursion
10537     (gnus-save-hidden-threads
10538       (let ((beg (point)))
10539         ;; We check that there are unread articles.
10540         (when (or all (gnus-summary-find-prev))
10541           (gnus-summary-catchup all t beg)))))
10542   (gnus-summary-position-point))
10543
10544 (defun gnus-summary-catchup-from-here (&optional all)
10545   "Mark all unticked articles after (and including) the current one as read.
10546 If ALL is non-nil, also mark ticked and dormant articles as read."
10547   (interactive "P")
10548   (save-excursion
10549     (gnus-save-hidden-threads
10550       (let ((beg (point)))
10551         ;; We check that there are unread articles.
10552         (when (or all (gnus-summary-find-next))
10553           (gnus-summary-catchup all t beg nil t)))))
10554   (gnus-summary-position-point))
10555
10556 (defun gnus-summary-catchup-all (&optional quietly)
10557   "Mark all articles in this newsgroup as read.
10558 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10559 instead, which marks only unread articles as read."
10560   (interactive "P")
10561   (gnus-summary-catchup t quietly))
10562
10563 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10564   "Mark all unread articles in this group as read, then exit.
10565 If prefix argument ALL is non-nil, all articles are marked as read.
10566 If QUIETLY is non-nil, no questions will be asked."
10567   (interactive "P")
10568   (when (gnus-summary-catchup all quietly nil 'fast)
10569     ;; Select next newsgroup or exit.
10570     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10571              (eq gnus-auto-select-next 'quietly))
10572         (gnus-summary-next-group nil)
10573       (gnus-summary-exit))))
10574
10575 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10576   "Mark all articles in this newsgroup as read, and then exit.
10577 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10578 instead, which marks only unread articles as read."
10579   (interactive "P")
10580   (gnus-summary-catchup-and-exit t quietly))
10581
10582 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10583   "Mark all articles in this group as read and select the next group.
10584 If given a prefix, mark all articles, unread as well as ticked, as
10585 read."
10586   (interactive "P")
10587   (save-excursion
10588     (gnus-summary-catchup all))
10589   (gnus-summary-next-group))
10590
10591 ;;;
10592 ;;; with article
10593 ;;;
10594
10595 (defmacro gnus-with-article (article &rest forms)
10596   "Select ARTICLE and perform FORMS in the original article buffer.
10597 Then replace the article with the result."
10598   `(progn
10599      ;; We don't want the article to be marked as read.
10600      (let (gnus-mark-article-hook)
10601        (gnus-summary-select-article t t nil ,article))
10602      (set-buffer gnus-original-article-buffer)
10603      ,@forms
10604      (if (not (gnus-check-backend-function
10605                'request-replace-article (car gnus-article-current)))
10606          (gnus-message 5 "Read-only group; not replacing")
10607        (unless (gnus-request-replace-article
10608                 ,article (car gnus-article-current)
10609                 (current-buffer) t)
10610          (error "Couldn't replace article")))
10611      ;; The cache and backlog have to be flushed somewhat.
10612      (when gnus-keep-backlog
10613        (gnus-backlog-remove-article
10614         (car gnus-article-current) (cdr gnus-article-current)))
10615      (when gnus-use-cache
10616        (gnus-cache-update-article
10617         (car gnus-article-current) (cdr gnus-article-current)))))
10618
10619 (put 'gnus-with-article 'lisp-indent-function 1)
10620 (put 'gnus-with-article 'edebug-form-spec '(form body))
10621
10622 ;; Thread-based commands.
10623
10624 (defun gnus-summary-articles-in-thread (&optional article)
10625   "Return a list of all articles in the current thread.
10626 If ARTICLE is non-nil, return all articles in the thread that starts
10627 with that article."
10628   (let* ((article (or article (gnus-summary-article-number)))
10629          (data (gnus-data-find-list article))
10630          (top-level (gnus-data-level (car data)))
10631          (top-subject
10632           (cond ((null gnus-thread-operation-ignore-subject)
10633                  (gnus-simplify-subject-re
10634                   (mail-header-subject (gnus-data-header (car data)))))
10635                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10636                  (gnus-simplify-subject-fuzzy
10637                   (mail-header-subject (gnus-data-header (car data)))))
10638                 (t nil)))
10639          (end-point (save-excursion
10640                       (if (gnus-summary-go-to-next-thread)
10641                           (point) (point-max))))
10642          articles)
10643     (while (and data
10644                 (< (gnus-data-pos (car data)) end-point))
10645       (when (or (not top-subject)
10646                 (string= top-subject
10647                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10648                              (gnus-simplify-subject-fuzzy
10649                               (mail-header-subject
10650                                (gnus-data-header (car data))))
10651                            (gnus-simplify-subject-re
10652                             (mail-header-subject
10653                              (gnus-data-header (car data)))))))
10654         (push (gnus-data-number (car data)) articles))
10655       (unless (and (setq data (cdr data))
10656                    (> (gnus-data-level (car data)) top-level))
10657         (setq data nil)))
10658     ;; Return the list of articles.
10659     (nreverse articles)))
10660
10661 (defun gnus-summary-rethread-current ()
10662   "Rethread the thread the current article is part of."
10663   (interactive)
10664   (let* ((gnus-show-threads t)
10665          (article (gnus-summary-article-number))
10666          (id (mail-header-id (gnus-summary-article-header)))
10667          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10668     (unless id
10669       (error "No article on the current line"))
10670     (gnus-rebuild-thread id)
10671     (gnus-summary-goto-subject article)))
10672
10673 (defun gnus-summary-reparent-thread ()
10674   "Make the current article child of the marked (or previous) article.
10675
10676 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10677 is non-nil or the Subject: of both articles are the same."
10678   (interactive)
10679   (unless (not (gnus-group-read-only-p))
10680     (error "The current newsgroup does not support article editing"))
10681   (unless (<= (length gnus-newsgroup-processable) 1)
10682     (error "No more than one article may be marked"))
10683   (save-window-excursion
10684     (let ((gnus-article-buffer " *reparent*")
10685           (current-article (gnus-summary-article-number))
10686           ;; First grab the marked article, otherwise one line up.
10687           (parent-article (if (not (null gnus-newsgroup-processable))
10688                               (car gnus-newsgroup-processable)
10689                             (save-excursion
10690                               (if (eq (forward-line -1) 0)
10691                                   (gnus-summary-article-number)
10692                                 (error "Beginning of summary buffer"))))))
10693       (unless (not (eq current-article parent-article))
10694         (error "An article may not be self-referential"))
10695       (let ((message-id (mail-header-id
10696                          (gnus-summary-article-header parent-article))))
10697         (unless (and message-id (not (equal message-id "")))
10698           (error "No message-id in desired parent"))
10699         (gnus-with-article current-article
10700           (save-restriction
10701             (goto-char (point-min))
10702             (message-narrow-to-head)
10703             (if (re-search-forward "^References: " nil t)
10704                 (progn
10705                   (re-search-forward "^[^ \t]" nil t)
10706                   (forward-line -1)
10707                   (end-of-line)
10708                   (insert " " message-id))
10709               (insert "References: " message-id "\n"))))
10710         (set-buffer gnus-summary-buffer)
10711         (gnus-summary-unmark-all-processable)
10712         (gnus-summary-update-article current-article)
10713         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10714             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10715         (gnus-summary-rethread-current)
10716         (gnus-message 3 "Article %d is now the child of article %d"
10717                       current-article parent-article)))))
10718
10719 (defun gnus-summary-toggle-threads (&optional arg)
10720   "Toggle showing conversation threads.
10721 If ARG is positive number, turn showing conversation threads on."
10722   (interactive "P")
10723   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10724     (setq gnus-show-threads
10725           (if (null arg) (not gnus-show-threads)
10726             (> (prefix-numeric-value arg) 0)))
10727     (gnus-summary-prepare)
10728     (gnus-summary-goto-subject current)
10729     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10730     (gnus-summary-position-point)))
10731
10732 (defun gnus-summary-show-all-threads ()
10733   "Show all threads."
10734   (interactive)
10735   (save-excursion
10736     (let ((buffer-read-only nil))
10737       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10738   (gnus-summary-position-point))
10739
10740 (defun gnus-summary-show-thread ()
10741   "Show thread subtrees.
10742 Returns nil if no thread was there to be shown."
10743   (interactive)
10744   (let ((buffer-read-only nil)
10745         (orig (point))
10746         (end (point-at-eol))
10747         ;; Leave point at bol
10748         (beg (progn (beginning-of-line) (point))))
10749     (prog1
10750         ;; Any hidden lines here?
10751         (search-forward "\r" end t)
10752       (subst-char-in-region beg end ?\^M ?\n t)
10753       (goto-char orig)
10754       (gnus-summary-position-point))))
10755
10756 (defun gnus-summary-maybe-hide-threads ()
10757   "If requested, hide the threads that should be hidden."
10758   (when (and gnus-show-threads
10759              gnus-thread-hide-subtree)
10760     (gnus-summary-hide-all-threads
10761      (if (or (consp gnus-thread-hide-subtree)
10762              (functionp gnus-thread-hide-subtree))
10763          (gnus-make-predicate gnus-thread-hide-subtree)
10764        nil))))
10765
10766 ;;; Hiding predicates.
10767
10768 (defun gnus-article-unread-p (header)
10769   (memq (mail-header-number header) gnus-newsgroup-unreads))
10770
10771 (defun gnus-article-unseen-p (header)
10772   (memq (mail-header-number header) gnus-newsgroup-unseen))
10773
10774 (defun gnus-map-articles (predicate articles)
10775   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10776   (apply 'gnus-or (mapcar predicate
10777                           (mapcar 'gnus-summary-article-header articles))))
10778
10779 (defun gnus-summary-hide-all-threads (&optional predicate)
10780   "Hide all thread subtrees.
10781 If PREDICATE is supplied, threads that satisfy this predicate
10782 will not be hidden."
10783   (interactive)
10784   (save-excursion
10785     (goto-char (point-min))
10786     (let ((end nil))
10787       (while (not end)
10788         (when (or (not predicate)
10789                   (gnus-map-articles
10790                    predicate (gnus-summary-article-children)))
10791             (gnus-summary-hide-thread))
10792         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10793   (gnus-summary-position-point))
10794
10795 (defun gnus-summary-hide-thread ()
10796   "Hide thread subtrees.
10797 If PREDICATE is supplied, threads that satisfy this predicate
10798 will not be hidden.
10799 Returns nil if no threads were there to be hidden."
10800   (interactive)
10801   (let ((buffer-read-only nil)
10802         (start (point))
10803         (article (gnus-summary-article-number)))
10804     (goto-char start)
10805     ;; Go forward until either the buffer ends or the subthread
10806     ;; ends.
10807     (when (and (not (eobp))
10808                (or (zerop (gnus-summary-next-thread 1 t))
10809                    (goto-char (point-max))))
10810       (prog1
10811           (if (and (> (point) start)
10812                    (search-backward "\n" start t))
10813               (progn
10814                 (subst-char-in-region start (point) ?\n ?\^M)
10815                 (gnus-summary-goto-subject article))
10816             (goto-char start)
10817             nil)))))
10818
10819 (defun gnus-summary-go-to-next-thread (&optional previous)
10820   "Go to the same level (or less) next thread.
10821 If PREVIOUS is non-nil, go to previous thread instead.
10822 Return the article number moved to, or nil if moving was impossible."
10823   (let ((level (gnus-summary-thread-level))
10824         (way (if previous -1 1))
10825         (beg (point)))
10826     (forward-line way)
10827     (while (and (not (eobp))
10828                 (< level (gnus-summary-thread-level)))
10829       (forward-line way))
10830     (if (eobp)
10831         (progn
10832           (goto-char beg)
10833           nil)
10834       (setq beg (point))
10835       (prog1
10836           (gnus-summary-article-number)
10837         (goto-char beg)))))
10838
10839 (defun gnus-summary-next-thread (n &optional silent)
10840   "Go to the same level next N'th thread.
10841 If N is negative, search backward instead.
10842 Returns the difference between N and the number of skips actually
10843 done.
10844
10845 If SILENT, don't output messages."
10846   (interactive "p")
10847   (let ((backward (< n 0))
10848         (n (abs n)))
10849     (while (and (> n 0)
10850                 (gnus-summary-go-to-next-thread backward))
10851       (decf n))
10852     (unless silent
10853       (gnus-summary-position-point))
10854     (when (and (not silent) (/= 0 n))
10855       (gnus-message 7 "No more threads"))
10856     n))
10857
10858 (defun gnus-summary-prev-thread (n)
10859   "Go to the same level previous N'th thread.
10860 Returns the difference between N and the number of skips actually
10861 done."
10862   (interactive "p")
10863   (gnus-summary-next-thread (- n)))
10864
10865 (defun gnus-summary-go-down-thread ()
10866   "Go down one level in the current thread."
10867   (let ((children (gnus-summary-article-children)))
10868     (when children
10869       (gnus-summary-goto-subject (car children)))))
10870
10871 (defun gnus-summary-go-up-thread ()
10872   "Go up one level in the current thread."
10873   (let ((parent (gnus-summary-article-parent)))
10874     (when parent
10875       (gnus-summary-goto-subject parent))))
10876
10877 (defun gnus-summary-down-thread (n)
10878   "Go down thread N steps.
10879 If N is negative, go up instead.
10880 Returns the difference between N and how many steps down that were
10881 taken."
10882   (interactive "p")
10883   (let ((up (< n 0))
10884         (n (abs n)))
10885     (while (and (> n 0)
10886                 (if up (gnus-summary-go-up-thread)
10887                   (gnus-summary-go-down-thread)))
10888       (setq n (1- n)))
10889     (gnus-summary-position-point)
10890     (when (/= 0 n)
10891       (gnus-message 7 "Can't go further"))
10892     n))
10893
10894 (defun gnus-summary-up-thread (n)
10895   "Go up thread N steps.
10896 If N is negative, go down instead.
10897 Returns the difference between N and how many steps down that were
10898 taken."
10899   (interactive "p")
10900   (gnus-summary-down-thread (- n)))
10901
10902 (defun gnus-summary-top-thread ()
10903   "Go to the top of the thread."
10904   (interactive)
10905   (while (gnus-summary-go-up-thread))
10906   (gnus-summary-article-number))
10907
10908 (defun gnus-summary-kill-thread (&optional unmark)
10909   "Mark articles under current thread as read.
10910 If the prefix argument is positive, remove any kinds of marks.
10911 If the prefix argument is negative, tick articles instead."
10912   (interactive "P")
10913   (when unmark
10914     (setq unmark (prefix-numeric-value unmark)))
10915   (let ((articles (gnus-summary-articles-in-thread)))
10916     (save-excursion
10917       ;; Expand the thread.
10918       (gnus-summary-show-thread)
10919       ;; Mark all the articles.
10920       (while articles
10921         (gnus-summary-goto-subject (car articles))
10922         (cond ((null unmark)
10923                (gnus-summary-mark-article-as-read gnus-killed-mark))
10924               ((> unmark 0)
10925                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10926               (t
10927                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10928         (setq articles (cdr articles))))
10929     ;; Hide killed subtrees.
10930     (and (null unmark)
10931          gnus-thread-hide-killed
10932          (gnus-summary-hide-thread))
10933     ;; If marked as read, go to next unread subject.
10934     (when (null unmark)
10935       ;; Go to next unread subject.
10936       (gnus-summary-next-subject 1 t)))
10937   (gnus-set-mode-line 'summary))
10938
10939 ;; Summary sorting commands
10940
10941 (defun gnus-summary-sort-by-number (&optional reverse)
10942   "Sort the summary buffer by article number.
10943 Argument REVERSE means reverse order."
10944   (interactive "P")
10945   (gnus-summary-sort 'number reverse))
10946
10947 (defun gnus-summary-sort-by-random (&optional reverse)
10948   "Randomize the order in the summary buffer.
10949 Argument REVERSE means to randomize in reverse order."
10950   (interactive "P")
10951   (gnus-summary-sort 'random reverse))
10952
10953 (defun gnus-summary-sort-by-author (&optional reverse)
10954   "Sort the summary buffer by author name alphabetically.
10955 If `case-fold-search' is non-nil, case of letters is ignored.
10956 Argument REVERSE means reverse order."
10957   (interactive "P")
10958   (gnus-summary-sort 'author reverse))
10959
10960 (defun gnus-summary-sort-by-recipient (&optional reverse)
10961   "Sort the summary buffer by recipient name alphabetically.
10962 If `case-fold-search' is non-nil, case of letters is ignored.
10963 Argument REVERSE means reverse order."
10964   (interactive "P")
10965   (gnus-summary-sort 'recipient reverse))
10966
10967 (defun gnus-summary-sort-by-subject (&optional reverse)
10968   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10969 If `case-fold-search' is non-nil, case of letters is ignored.
10970 Argument REVERSE means reverse order."
10971   (interactive "P")
10972   (gnus-summary-sort 'subject reverse))
10973
10974 (defun gnus-summary-sort-by-date (&optional reverse)
10975   "Sort the summary buffer by date.
10976 Argument REVERSE means reverse order."
10977   (interactive "P")
10978   (gnus-summary-sort 'date reverse))
10979
10980 (defun gnus-summary-sort-by-score (&optional reverse)
10981   "Sort the summary buffer by score.
10982 Argument REVERSE means reverse order."
10983   (interactive "P")
10984   (gnus-summary-sort 'score reverse))
10985
10986 (defun gnus-summary-sort-by-lines (&optional reverse)
10987   "Sort the summary buffer by the number of lines.
10988 Argument REVERSE means reverse order."
10989   (interactive "P")
10990   (gnus-summary-sort 'lines reverse))
10991
10992 (defun gnus-summary-sort-by-chars (&optional reverse)
10993   "Sort the summary buffer by article length.
10994 Argument REVERSE means reverse order."
10995   (interactive "P")
10996   (gnus-summary-sort 'chars reverse))
10997
10998 (defun gnus-summary-sort-by-original (&optional reverse)
10999   "Sort the summary buffer using the default sorting method.
11000 Argument REVERSE means reverse order."
11001   (interactive "P")
11002   (let* ((buffer-read-only)
11003          (gnus-summary-prepare-hook nil))
11004     ;; We do the sorting by regenerating the threads.
11005     (gnus-summary-prepare)
11006     ;; Hide subthreads if needed.
11007     (gnus-summary-maybe-hide-threads)))
11008
11009 (defun gnus-summary-sort (predicate reverse)
11010   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11011   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11012          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11013          (gnus-thread-sort-functions
11014           (if (not reverse)
11015               thread
11016             `(lambda (t1 t2)
11017                (,thread t2 t1))))
11018          (gnus-sort-gathered-threads-function
11019           gnus-thread-sort-functions)
11020          (gnus-article-sort-functions
11021           (if (not reverse)
11022               article
11023             `(lambda (t1 t2)
11024                (,article t2 t1))))
11025          (buffer-read-only)
11026          (gnus-summary-prepare-hook nil))
11027     ;; We do the sorting by regenerating the threads.
11028     (gnus-summary-prepare)
11029     ;; Hide subthreads if needed.
11030     (gnus-summary-maybe-hide-threads)))
11031
11032 ;; Summary saving commands.
11033
11034 (defun gnus-summary-save-article (&optional n not-saved)
11035   "Save the current article using the default saver function.
11036 If N is a positive number, save the N next articles.
11037 If N is a negative number, save the N previous articles.
11038 If N is nil and any articles have been marked with the process mark,
11039 save those articles instead.
11040 The variable `gnus-default-article-saver' specifies the saver function."
11041   (interactive "P")
11042   (let* ((articles (gnus-summary-work-articles n))
11043          (save-buffer (save-excursion
11044                         (nnheader-set-temp-buffer " *Gnus Save*")))
11045          (num (length articles))
11046          header file)
11047     (dolist (article articles)
11048       (setq header (gnus-summary-article-header article))
11049       (if (not (vectorp header))
11050           ;; This is a pseudo-article.
11051           (if (assq 'name header)
11052               (gnus-copy-file (cdr (assq 'name header)))
11053             (gnus-message 1 "Article %d is unsaveable" article))
11054         ;; This is a real article.
11055         (save-window-excursion
11056           (let ((gnus-display-mime-function nil)
11057                 (gnus-article-prepare-hook nil))
11058             (gnus-summary-select-article t nil nil article)))
11059         (save-excursion
11060           (set-buffer save-buffer)
11061           (erase-buffer)
11062           (insert-buffer-substring gnus-original-article-buffer))
11063         (setq file (gnus-article-save save-buffer file num))
11064         (gnus-summary-remove-process-mark article)
11065         (unless not-saved
11066           (gnus-summary-set-saved-mark article))))
11067     (gnus-kill-buffer save-buffer)
11068     (gnus-summary-position-point)
11069     (gnus-set-mode-line 'summary)
11070     n))
11071
11072 (defun gnus-summary-pipe-output (&optional arg headers)
11073   "Pipe the current article to a subprocess.
11074 If N is a positive number, pipe the N next articles.
11075 If N is a negative number, pipe the N previous articles.
11076 If N is nil and any articles have been marked with the process mark,
11077 pipe those articles instead.
11078 If HEADERS (the symbolic prefix), include the headers, too."
11079   (interactive (gnus-interactive "P\ny"))
11080   (require 'gnus-art)
11081   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11082         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11083     (gnus-summary-save-article arg t))
11084   (let ((buffer (get-buffer "*Shell Command Output*")))
11085     (when (and buffer
11086                (not (zerop (buffer-size buffer))))
11087       (gnus-configure-windows 'pipe))))
11088
11089 (defun gnus-summary-save-article-mail (&optional arg)
11090   "Append the current article to an mail file.
11091 If N is a positive number, save the N next articles.
11092 If N is a negative number, save the N previous articles.
11093 If N is nil and any articles have been marked with the process mark,
11094 save those articles instead."
11095   (interactive "P")
11096   (require 'gnus-art)
11097   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11098     (gnus-summary-save-article arg)))
11099
11100 (defun gnus-summary-save-article-rmail (&optional arg)
11101   "Append the current article to an rmail file.
11102 If N is a positive number, save the N next articles.
11103 If N is a negative number, save the N previous articles.
11104 If N is nil and any articles have been marked with the process mark,
11105 save those articles instead."
11106   (interactive "P")
11107   (require 'gnus-art)
11108   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11109     (gnus-summary-save-article arg)))
11110
11111 (defun gnus-summary-save-article-file (&optional arg)
11112   "Append the current article to a file.
11113 If N is a positive number, save the N next articles.
11114 If N is a negative number, save the N previous articles.
11115 If N is nil and any articles have been marked with the process mark,
11116 save those articles instead."
11117   (interactive "P")
11118   (require 'gnus-art)
11119   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11120     (gnus-summary-save-article arg)))
11121
11122 (defun gnus-summary-write-article-file (&optional arg)
11123   "Write the current article to a file, deleting the previous file.
11124 If N is a positive number, save the N next articles.
11125 If N is a negative number, save the N previous articles.
11126 If N is nil and any articles have been marked with the process mark,
11127 save those articles instead."
11128   (interactive "P")
11129   (require 'gnus-art)
11130   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11131     (gnus-summary-save-article arg)))
11132
11133 (defun gnus-summary-save-article-body-file (&optional arg)
11134   "Append the current article body to a file.
11135 If N is a positive number, save the N next articles.
11136 If N is a negative number, save the N previous articles.
11137 If N is nil and any articles have been marked with the process mark,
11138 save those articles instead."
11139   (interactive "P")
11140   (require 'gnus-art)
11141   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11142     (gnus-summary-save-article arg)))
11143
11144 (defun gnus-summary-muttprint (&optional arg)
11145   "Print the current article using Muttprint.
11146 If N is a positive number, save the N next articles.
11147 If N is a negative number, save the N previous articles.
11148 If N is nil and any articles have been marked with the process mark,
11149 save those articles instead."
11150   (interactive "P")
11151   (require 'gnus-art)
11152   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11153     (gnus-summary-save-article arg t)))
11154
11155 (defun gnus-summary-pipe-message (program)
11156   "Pipe the current article through PROGRAM."
11157   (interactive "sProgram: ")
11158   (gnus-summary-select-article)
11159   (let ((mail-header-separator ""))
11160     (gnus-eval-in-buffer-window gnus-article-buffer
11161       (save-restriction
11162         (widen)
11163         (let ((start (window-start))
11164               buffer-read-only)
11165           (message-pipe-buffer-body program)
11166           (set-window-start (get-buffer-window (current-buffer)) start))))))
11167
11168 (defun gnus-get-split-value (methods)
11169   "Return a value based on the split METHODS."
11170   (let (split-name method result match)
11171     (when methods
11172       (save-excursion
11173         (set-buffer gnus-original-article-buffer)
11174         (save-restriction
11175           (nnheader-narrow-to-headers)
11176           (while (and methods (not split-name))
11177             (goto-char (point-min))
11178             (setq method (pop methods))
11179             (setq match (car method))
11180             (when (cond
11181                    ((stringp match)
11182                     ;; Regular expression.
11183                     (ignore-errors
11184                       (re-search-forward match nil t)))
11185                    ((functionp match)
11186                     ;; Function.
11187                     (save-restriction
11188                       (widen)
11189                       (setq result (funcall match gnus-newsgroup-name))))
11190                    ((consp match)
11191                     ;; Form.
11192                     (save-restriction
11193                       (widen)
11194                       (setq result (eval match)))))
11195               (setq split-name (cdr method))
11196               (cond ((stringp result)
11197                      (push (expand-file-name
11198                             result gnus-article-save-directory)
11199                            split-name))
11200                     ((consp result)
11201                      (setq split-name (append result split-name)))))))))
11202     (nreverse split-name)))
11203
11204 (defun gnus-valid-move-group-p (group)
11205   (and (boundp group)
11206        (symbol-name group)
11207        (symbol-value group)
11208        (gnus-get-function (gnus-find-method-for-group
11209                            (symbol-name group)) 'request-accept-article t)))
11210
11211 (defun gnus-read-move-group-name (prompt default articles prefix)
11212   "Read a group name."
11213   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11214          (minibuffer-confirm-incomplete nil) ; XEmacs
11215          (prom
11216           (format "%s %s to"
11217                   prompt
11218                   (if (> (length articles) 1)
11219                       (format "these %d articles" (length articles))
11220                     "this article")))
11221          (to-newsgroup
11222           (cond
11223            ((null split-name)
11224             (gnus-completing-read-with-default
11225              default prom
11226              gnus-active-hashtb
11227              'gnus-valid-move-group-p
11228              nil prefix
11229              'gnus-group-history))
11230            ((= 1 (length split-name))
11231             (gnus-completing-read-with-default
11232              (car split-name) prom
11233              gnus-active-hashtb
11234              'gnus-valid-move-group-p
11235              nil nil
11236              'gnus-group-history))
11237            (t
11238             (gnus-completing-read-with-default
11239              nil prom
11240              (mapcar 'list (nreverse split-name))
11241              nil nil nil
11242              'gnus-group-history))))
11243          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11244     (when to-newsgroup
11245       (if (or (string= to-newsgroup "")
11246               (string= to-newsgroup prefix))
11247           (setq to-newsgroup default))
11248       (unless to-newsgroup
11249         (error "No group name entered"))
11250       (or (gnus-active to-newsgroup)
11251           (gnus-activate-group to-newsgroup nil nil to-method)
11252           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11253                                      to-newsgroup))
11254               (or (and (gnus-request-create-group to-newsgroup to-method)
11255                        (gnus-activate-group
11256                         to-newsgroup nil nil to-method)
11257                        (gnus-subscribe-group to-newsgroup))
11258                   (error "Couldn't create group %s" to-newsgroup)))
11259           (error "No such group: %s" to-newsgroup)))
11260     to-newsgroup))
11261
11262 (defun gnus-summary-save-parts (type dir n &optional reverse)
11263   "Save parts matching TYPE to DIR.
11264 If REVERSE, save parts that do not match TYPE."
11265   (interactive
11266    (list (read-string "Save parts of type: "
11267                       (or (car gnus-summary-save-parts-type-history)
11268                           gnus-summary-save-parts-default-mime)
11269                       'gnus-summary-save-parts-type-history)
11270          (setq gnus-summary-save-parts-last-directory
11271                (read-file-name "Save to directory: "
11272                                gnus-summary-save-parts-last-directory
11273                                nil t))
11274          current-prefix-arg))
11275   (gnus-summary-iterate n
11276     (let ((gnus-display-mime-function nil)
11277           (gnus-inhibit-treatment t))
11278       (gnus-summary-select-article))
11279     (save-excursion
11280       (set-buffer gnus-article-buffer)
11281       (let ((handles (or gnus-article-mime-handles
11282                          (mm-dissect-buffer nil gnus-article-loose-mime)
11283                          (and gnus-article-emulate-mime
11284                               (mm-uu-dissect)))))
11285         (when handles
11286           (gnus-summary-save-parts-1 type dir handles reverse)
11287           (unless gnus-article-mime-handles ;; Don't destroy this case.
11288             (mm-destroy-parts handles)))))))
11289
11290 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11291   (if (stringp (car handle))
11292       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11293               (cdr handle))
11294     (when (if reverse
11295               (not (string-match type (mm-handle-media-type handle)))
11296             (string-match type (mm-handle-media-type handle)))
11297       (let ((file (expand-file-name
11298                    (gnus-map-function
11299                     mm-file-name-rewrite-functions
11300                     (file-name-nondirectory
11301                      (or
11302                       (mail-content-type-get
11303                        (mm-handle-disposition handle) 'filename)
11304                       (mail-content-type-get
11305                        (mm-handle-type handle) 'name)
11306                       (concat gnus-newsgroup-name
11307                               "." (number-to-string
11308                                    (cdr gnus-article-current))))))
11309                    dir)))
11310         (unless (file-exists-p file)
11311           (mm-save-part-to-file handle file))))))
11312
11313 ;; Summary extract commands
11314
11315 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11316   (let ((buffer-read-only nil)
11317         (article (gnus-summary-article-number))
11318         after-article b e)
11319     (unless (gnus-summary-goto-subject article)
11320       (error "No such article: %d" article))
11321     (gnus-summary-position-point)
11322     ;; If all commands are to be bunched up on one line, we collect
11323     ;; them here.
11324     (unless gnus-view-pseudos-separately
11325       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11326             files action)
11327         (while ps
11328           (setq action (cdr (assq 'action (car ps))))
11329           (setq files (list (cdr (assq 'name (car ps)))))
11330           (while (and ps (cdr ps)
11331                       (string= (or action "1")
11332                                (or (cdr (assq 'action (cadr ps))) "2")))
11333             (push (cdr (assq 'name (cadr ps))) files)
11334             (setcdr ps (cddr ps)))
11335           (when files
11336             (when (not (string-match "%s" action))
11337               (push " " files))
11338             (push " " files)
11339             (when (assq 'execute (car ps))
11340               (setcdr (assq 'execute (car ps))
11341                       (funcall (if (string-match "%s" action)
11342                                    'format 'concat)
11343                                action
11344                                (mapconcat
11345                                 (lambda (f)
11346                                   (if (equal f " ")
11347                                       f
11348                                     (shell-quote-argument f)))
11349                                 files " ")))))
11350           (setq ps (cdr ps)))))
11351     (if (and gnus-view-pseudos (not not-view))
11352         (while pslist
11353           (when (assq 'execute (car pslist))
11354             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11355                                   (eq gnus-view-pseudos 'not-confirm)))
11356           (setq pslist (cdr pslist)))
11357       (save-excursion
11358         (while pslist
11359           (setq after-article (or (cdr (assq 'article (car pslist)))
11360                                   (gnus-summary-article-number)))
11361           (gnus-summary-goto-subject after-article)
11362           (forward-line 1)
11363           (setq b (point))
11364           (insert "    " (file-name-nondirectory
11365                           (cdr (assq 'name (car pslist))))
11366                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11367           (setq e (point))
11368           (forward-line -1)             ; back to `b'
11369           (gnus-add-text-properties
11370            b (1- e) (list 'gnus-number gnus-reffed-article-number
11371                           gnus-mouse-face-prop gnus-mouse-face))
11372           (gnus-data-enter
11373            after-article gnus-reffed-article-number
11374            gnus-unread-mark b (car pslist) 0 (- e b))
11375           (setq gnus-newsgroup-unreads
11376                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11377                                          gnus-reffed-article-number))
11378           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11379           (setq pslist (cdr pslist)))))))
11380
11381 (defun gnus-pseudos< (p1 p2)
11382   (let ((c1 (cdr (assq 'action p1)))
11383         (c2 (cdr (assq 'action p2))))
11384     (and c1 c2 (string< c1 c2))))
11385
11386 (defun gnus-request-pseudo-article (props)
11387   (cond ((assq 'execute props)
11388          (gnus-execute-command (cdr (assq 'execute props)))))
11389   (let ((gnus-current-article (gnus-summary-article-number)))
11390     (gnus-run-hooks 'gnus-mark-article-hook)))
11391
11392 (defun gnus-execute-command (command &optional automatic)
11393   (save-excursion
11394     (gnus-article-setup-buffer)
11395     (set-buffer gnus-article-buffer)
11396     (setq buffer-read-only nil)
11397     (let ((command (if automatic command
11398                      (read-string "Command: " (cons command 0)))))
11399       (erase-buffer)
11400       (insert "$ " command "\n\n")
11401       (if gnus-view-pseudo-asynchronously
11402           (start-process "gnus-execute" (current-buffer) shell-file-name
11403                          shell-command-switch command)
11404         (call-process shell-file-name nil t nil
11405                       shell-command-switch command)))))
11406
11407 ;; Summary kill commands.
11408
11409 (defun gnus-summary-edit-global-kill (article)
11410   "Edit the \"global\" kill file."
11411   (interactive (list (gnus-summary-article-number)))
11412   (gnus-group-edit-global-kill article))
11413
11414 (defun gnus-summary-edit-local-kill ()
11415   "Edit a local kill file applied to the current newsgroup."
11416   (interactive)
11417   (setq gnus-current-headers (gnus-summary-article-header))
11418   (gnus-group-edit-local-kill
11419    (gnus-summary-article-number) gnus-newsgroup-name))
11420
11421 ;;; Header reading.
11422
11423 (defun gnus-read-header (id &optional header)
11424   "Read the headers of article ID and enter them into the Gnus system."
11425   (let ((group gnus-newsgroup-name)
11426         (gnus-override-method
11427          (or
11428           gnus-override-method
11429           (and (gnus-news-group-p gnus-newsgroup-name)
11430                (car (gnus-refer-article-methods)))))
11431         where)
11432     ;; First we check to see whether the header in question is already
11433     ;; fetched.
11434     (if (stringp id)
11435         ;; This is a Message-ID.
11436         (setq header (or header (gnus-id-to-header id)))
11437       ;; This is an article number.
11438       (setq header (or header (gnus-summary-article-header id))))
11439     (if (and header
11440              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11441         ;; We have found the header.
11442         header
11443       ;; We have to really fetch the header to this article.
11444       (save-excursion
11445         (set-buffer nntp-server-buffer)
11446         (when (setq where (gnus-request-head id group))
11447           (nnheader-fold-continuation-lines)
11448           (goto-char (point-max))
11449           (insert ".\n")
11450           (goto-char (point-min))
11451           (insert "211 ")
11452           (princ (cond
11453                   ((numberp id) id)
11454                   ((cdr where) (cdr where))
11455                   (header (mail-header-number header))
11456                   (t gnus-reffed-article-number))
11457                  (current-buffer))
11458           (insert " Article retrieved.\n"))
11459         (if (or (not where)
11460                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11461             ()                          ; Malformed head.
11462           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11463             (when (and (stringp id)
11464                        (not (string= (gnus-group-real-name group)
11465                                      (car where))))
11466               ;; If we fetched by Message-ID and the article came
11467               ;; from a different group, we fudge some bogus article
11468               ;; numbers for this article.
11469               (mail-header-set-number header gnus-reffed-article-number))
11470             (save-excursion
11471               (set-buffer gnus-summary-buffer)
11472               (decf gnus-reffed-article-number)
11473               (gnus-remove-header (mail-header-number header))
11474               (push header gnus-newsgroup-headers)
11475               (setq gnus-current-headers header)
11476               (push (mail-header-number header) gnus-newsgroup-limit)))
11477           header)))))
11478
11479 (defun gnus-remove-header (number)
11480   "Remove header NUMBER from `gnus-newsgroup-headers'."
11481   (if (and gnus-newsgroup-headers
11482            (= number (mail-header-number (car gnus-newsgroup-headers))))
11483       (pop gnus-newsgroup-headers)
11484     (let ((headers gnus-newsgroup-headers))
11485       (while (and (cdr headers)
11486                   (not (= number (mail-header-number (cadr headers)))))
11487         (pop headers))
11488       (when (cdr headers)
11489         (setcdr headers (cddr headers))))))
11490
11491 ;;;
11492 ;;; summary highlights
11493 ;;;
11494
11495 (defun gnus-highlight-selected-summary ()
11496   "Highlight selected article in summary buffer."
11497   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11498   (when gnus-summary-selected-face
11499     (save-excursion
11500       (let* ((beg (point-at-bol))
11501              (end (point-at-eol))
11502              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11503              (from (if (get-text-property beg gnus-mouse-face-prop)
11504                        beg
11505                      (or (next-single-property-change
11506                           beg gnus-mouse-face-prop nil end)
11507                          beg)))
11508              (to
11509               (if (= from end)
11510                   (- from 2)
11511                 (or (next-single-property-change
11512                      from gnus-mouse-face-prop nil end)
11513                     end))))
11514         ;; If no mouse-face prop on line we will have to = from = end,
11515         ;; so we highlight the entire line instead.
11516         (when (= (+ to 2) from)
11517           (setq from beg)
11518           (setq to end))
11519         (if gnus-newsgroup-selected-overlay
11520             ;; Move old overlay.
11521             (gnus-move-overlay
11522              gnus-newsgroup-selected-overlay from to (current-buffer))
11523           ;; Create new overlay.
11524           (gnus-overlay-put
11525            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11526            'face gnus-summary-selected-face))))))
11527
11528 (defvar gnus-summary-highlight-line-cached nil)
11529 (defvar gnus-summary-highlight-line-trigger nil)
11530
11531 (defun gnus-summary-highlight-line-0 ()
11532   (if (and (eq gnus-summary-highlight-line-trigger
11533                gnus-summary-highlight)
11534            gnus-summary-highlight-line-cached)
11535       gnus-summary-highlight-line-cached
11536     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11537           gnus-summary-highlight-line-cached
11538           (let* ((cond (list 'cond))
11539                  (c cond)
11540                  (list gnus-summary-highlight))
11541             (while list
11542               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11543                               nil))
11544               (setq c (cdr c)
11545                     list (cdr list)))
11546             (gnus-byte-compile (list 'lambda nil cond))))))
11547
11548 (defun gnus-summary-highlight-line ()
11549   "Highlight current line according to `gnus-summary-highlight'."
11550   (let* ((beg (point-at-bol))
11551          (article (or (gnus-summary-article-number) gnus-current-article))
11552          (score (or (cdr (assq article
11553                                gnus-newsgroup-scored))
11554                     gnus-summary-default-score 0))
11555          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11556          (inhibit-read-only t)
11557          (default gnus-summary-default-score)
11558          (default-high gnus-summary-default-high-score)
11559          (default-low gnus-summary-default-low-score)
11560          (uncached (and gnus-summary-use-undownloaded-faces
11561                         (memq article gnus-newsgroup-undownloaded)
11562                         (not (memq article gnus-newsgroup-cached)))))
11563     (let ((face (funcall (gnus-summary-highlight-line-0))))
11564       (unless (eq face (get-text-property beg 'face))
11565         (gnus-put-text-property-excluding-characters-with-faces
11566          beg (point-at-eol) 'face
11567          (setq face (if (boundp face) (symbol-value face) face)))
11568         (when gnus-summary-highlight-line-function
11569           (funcall gnus-summary-highlight-line-function article face))))))
11570
11571 (defun gnus-update-read-articles (group unread &optional compute)
11572   "Update the list of read articles in GROUP.
11573 UNREAD is a sorted list."
11574   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11575         (info (gnus-get-info group))
11576         (prev 1)
11577         read)
11578     (if (or (not info) (not active))
11579         ;; There is no info on this group if it was, in fact,
11580         ;; killed.  Gnus stores no information on killed groups, so
11581         ;; there's nothing to be done.
11582         ;; One could store the information somewhere temporarily,
11583         ;; perhaps...  Hmmm...
11584         ()
11585       ;; Remove any negative articles numbers.
11586       (while (and unread (< (car unread) 0))
11587         (setq unread (cdr unread)))
11588       ;; Remove any expired article numbers
11589       (while (and unread (< (car unread) (car active)))
11590         (setq unread (cdr unread)))
11591       ;; Compute the ranges of read articles by looking at the list of
11592       ;; unread articles.
11593       (while unread
11594         (when (/= (car unread) prev)
11595           (push (if (= prev (1- (car unread))) prev
11596                   (cons prev (1- (car unread))))
11597                 read))
11598         (setq prev (1+ (car unread)))
11599         (setq unread (cdr unread)))
11600       (when (<= prev (cdr active))
11601         (push (cons prev (cdr active)) read))
11602       (setq read (if (> (length read) 1) (nreverse read) read))
11603       (if compute
11604           read
11605         (save-excursion
11606           (let (setmarkundo)
11607             ;; Propagate the read marks to the backend.
11608             (when (gnus-check-backend-function 'request-set-mark group)
11609               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11610                     (add (gnus-remove-from-range read (gnus-info-read info))))
11611                 (when (or add del)
11612                   (unless (gnus-check-group group)
11613                     (error "Can't open server for %s" group))
11614                   (gnus-request-set-mark
11615                    group (delq nil (list (if add (list add 'add '(read)))
11616                                          (if del (list del 'del '(read))))))
11617                   (setq setmarkundo
11618                         `(gnus-request-set-mark
11619                           ,group
11620                           ',(delq nil (list
11621                                        (if del (list del 'add '(read)))
11622                                        (if add (list add 'del '(read))))))))))
11623             (set-buffer gnus-group-buffer)
11624             (gnus-undo-register
11625               `(progn
11626                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11627                  (gnus-info-set-read ',info ',(gnus-info-read info))
11628                  (gnus-get-unread-articles-in-group ',info
11629                                                     (gnus-active ,group))
11630                  (gnus-group-update-group ,group t)
11631                  ,setmarkundo))))
11632         ;; Enter this list into the group info.
11633         (gnus-info-set-read info read)
11634         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11635         (gnus-get-unread-articles-in-group info (gnus-active group))
11636         t))))
11637
11638 (defun gnus-offer-save-summaries ()
11639   "Offer to save all active summary buffers."
11640   (let (buffers)
11641     ;; Go through all buffers and find all summaries.
11642     (dolist (buffer (buffer-list))
11643       (when (and (setq buffer (buffer-name buffer))
11644                  (string-match "Summary" buffer)
11645                  (with-current-buffer buffer
11646                    ;; We check that this is, indeed, a summary buffer.
11647                    (and (eq major-mode 'gnus-summary-mode)
11648                         ;; Also make sure this isn't bogus.
11649                         gnus-newsgroup-prepared
11650                         ;; Also make sure that this isn't a
11651                         ;; dead summary buffer.
11652                         (not gnus-dead-summary-mode))))
11653         (push buffer buffers)))
11654     ;; Go through all these summary buffers and offer to save them.
11655     (when buffers
11656       (save-excursion
11657         (map-y-or-n-p
11658          "Update summary buffer %s? "
11659          (lambda (buf)
11660            (switch-to-buffer buf)
11661            (gnus-summary-exit))
11662          buffers)))))
11663
11664 (defun gnus-summary-setup-default-charset ()
11665   "Setup newsgroup default charset."
11666   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11667       (setq gnus-newsgroup-charset nil)
11668     (let* ((ignored-charsets
11669             (or gnus-newsgroup-ephemeral-ignored-charsets
11670                 (append
11671                  (and gnus-newsgroup-name
11672                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11673                  gnus-newsgroup-ignored-charsets))))
11674       (setq gnus-newsgroup-charset
11675             (or gnus-newsgroup-ephemeral-charset
11676                 (and gnus-newsgroup-name
11677                      (gnus-parameter-charset gnus-newsgroup-name))
11678                 gnus-default-charset))
11679       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11680            ignored-charsets))))
11681
11682 ;;;
11683 ;;; Mime Commands
11684 ;;;
11685
11686 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11687   "Display the current article buffer fully MIME-buttonized.
11688 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11689 treated as multipart/mixed."
11690   (interactive "P")
11691   (require 'gnus-art)
11692   (let ((gnus-unbuttonized-mime-types nil)
11693         (gnus-mime-display-multipart-as-mixed show-all-parts))
11694     (gnus-summary-show-article)))
11695
11696 (defun gnus-summary-repair-multipart (article)
11697   "Add a Content-Type header to a multipart article without one."
11698   (interactive (list (gnus-summary-article-number)))
11699   (gnus-with-article article
11700     (message-narrow-to-head)
11701     (message-remove-header "Mime-Version")
11702     (goto-char (point-max))
11703     (insert "Mime-Version: 1.0\n")
11704     (widen)
11705     (when (search-forward "\n--" nil t)
11706       (let ((separator (buffer-substring (point) (point-at-eol))))
11707         (message-narrow-to-head)
11708         (message-remove-header "Content-Type")
11709         (goto-char (point-max))
11710         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11711                         separator))
11712         (widen))))
11713   (let (gnus-mark-article-hook)
11714     (gnus-summary-select-article t t nil article)))
11715
11716 (defun gnus-summary-toggle-display-buttonized ()
11717   "Toggle the buttonizing of the article buffer."
11718   (interactive)
11719   (require 'gnus-art)
11720   (if (setq gnus-inhibit-mime-unbuttonizing
11721             (not gnus-inhibit-mime-unbuttonizing))
11722       (let ((gnus-unbuttonized-mime-types nil))
11723         (gnus-summary-show-article))
11724     (gnus-summary-show-article)))
11725
11726 ;;;
11727 ;;; Generic summary marking commands
11728 ;;;
11729
11730 (defvar gnus-summary-marking-alist
11731   '((read gnus-del-mark "d")
11732     (unread gnus-unread-mark "u")
11733     (ticked gnus-ticked-mark "!")
11734     (dormant gnus-dormant-mark "?")
11735     (expirable gnus-expirable-mark "e"))
11736   "An alist of names/marks/keystrokes.")
11737
11738 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11739 (defvar gnus-summary-mark-map)
11740
11741 (defun gnus-summary-make-all-marking-commands ()
11742   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11743   (dolist (elem gnus-summary-marking-alist)
11744     (apply 'gnus-summary-make-marking-command elem)))
11745
11746 (defun gnus-summary-make-marking-command (name mark keystroke)
11747   (let ((map (make-sparse-keymap)))
11748     (define-key gnus-summary-generic-mark-map keystroke map)
11749     (dolist (lway `((next "next" next nil "n")
11750                     (next-unread "next unread" next t "N")
11751                     (prev "previous" prev nil "p")
11752                     (prev-unread "previous unread" prev t "P")
11753                     (nomove "" nil nil ,keystroke)))
11754       (let ((func (gnus-summary-make-marking-command-1
11755                    mark (car lway) lway name)))
11756         (setq func (eval func))
11757         (define-key map (nth 4 lway) func)))))
11758
11759 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11760   `(defun ,(intern
11761             (format "gnus-summary-put-mark-as-%s%s"
11762                     name (if (eq way 'nomove)
11763                              ""
11764                            (concat "-" (symbol-name way)))))
11765      (n)
11766      ,(format
11767        "Mark the current article as %s%s.
11768 If N, the prefix, then repeat N times.
11769 If N is negative, move in reverse order.
11770 The difference between N and the actual number of articles marked is
11771 returned."
11772        name (cadr lway))
11773      (interactive "p")
11774      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11775
11776 (defun gnus-summary-generic-mark (n mark move unread)
11777   "Mark N articles with MARK."
11778   (unless (eq major-mode 'gnus-summary-mode)
11779     (error "This command can only be used in the summary buffer"))
11780   (gnus-summary-show-thread)
11781   (let ((nummove
11782          (cond
11783           ((eq move 'next) 1)
11784           ((eq move 'prev) -1)
11785           (t 0))))
11786     (if (zerop nummove)
11787         (setq n 1)
11788       (when (< n 0)
11789         (setq n (abs n)
11790               nummove (* -1 nummove))))
11791     (while (and (> n 0)
11792                 (gnus-summary-mark-article nil mark)
11793                 (zerop (gnus-summary-next-subject nummove unread t)))
11794       (setq n (1- n)))
11795     (when (/= 0 n)
11796       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11797     (gnus-summary-recenter)
11798     (gnus-summary-position-point)
11799     (gnus-set-mode-line 'summary)
11800     n))
11801
11802 (defun gnus-summary-insert-articles (articles)
11803   (when (setq articles
11804               (gnus-sorted-difference articles
11805                                       (mapcar (lambda (h)
11806                                                 (mail-header-number h))
11807                                               gnus-newsgroup-headers)))
11808     (setq gnus-newsgroup-headers
11809           (gnus-merge 'list
11810                       gnus-newsgroup-headers
11811                       (gnus-fetch-headers articles)
11812                       'gnus-article-sort-by-number))
11813     ;; Suppress duplicates?
11814     (when gnus-suppress-duplicates
11815       (gnus-dup-suppress-articles))
11816
11817     ;; We might want to build some more threads first.
11818     (when (and gnus-fetch-old-headers
11819                (eq gnus-headers-retrieved-by 'nov))
11820       (if (eq gnus-fetch-old-headers 'invisible)
11821           (gnus-build-all-threads)
11822         (gnus-build-old-threads)))
11823     ;; Let the Gnus agent mark articles as read.
11824     (when gnus-agent
11825       (gnus-agent-get-undownloaded-list))
11826     ;; Remove list identifiers from subject
11827     (when gnus-list-identifiers
11828       (gnus-summary-remove-list-identifiers))
11829     ;; First and last article in this newsgroup.
11830     (when gnus-newsgroup-headers
11831       (setq gnus-newsgroup-begin
11832             (mail-header-number (car gnus-newsgroup-headers))
11833             gnus-newsgroup-end
11834             (mail-header-number
11835              (gnus-last-element gnus-newsgroup-headers))))
11836     (when gnus-use-scoring
11837       (gnus-possibly-score-headers))))
11838
11839 (defun gnus-summary-insert-old-articles (&optional all)
11840   "Insert all old articles in this group.
11841 If ALL is non-nil, already read articles become readable.
11842 If ALL is a number, fetch this number of articles."
11843   (interactive "P")
11844   (prog1
11845       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11846             older len)
11847         (setq older
11848               ;; Some nntp servers lie about their active range.  When
11849               ;; this happens, the active range can be in the millions.
11850               ;; Use a compressed range to avoid creating a huge list.
11851               (gnus-range-difference (list gnus-newsgroup-active) old))
11852         (setq len (gnus-range-length older))
11853         (cond
11854          ((null older) nil)
11855          ((numberp all)
11856           (if (< all len)
11857               (let ((older-range (nreverse older)))
11858                 (setq older nil)
11859
11860                 (while (> all 0)
11861                   (let* ((r (pop older-range))
11862                          (min (if (numberp r) r (car r)))
11863                          (max (if (numberp r) r (cdr r))))
11864                     (while (and (<= min max)
11865                                 (> all 0))
11866                       (push max older)
11867                       (setq all (1- all)
11868                             max (1- max))))))
11869             (setq older (gnus-uncompress-range older))))
11870          (all
11871           (setq older (gnus-uncompress-range older)))
11872          (t
11873           (when (and (numberp gnus-large-newsgroup)
11874                    (> len gnus-large-newsgroup))
11875               (let* ((cursor-in-echo-area nil)
11876                      (initial (gnus-parameter-large-newsgroup-initial
11877                                gnus-newsgroup-name))
11878                      (input
11879                       (read-string
11880                        (format
11881                         "How many articles from %s (%s %d): "
11882                         (gnus-limit-string
11883                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11884                         (if initial "max" "default")
11885                         len)
11886                        (if initial
11887                            (cons (number-to-string initial)
11888                                  0)))))
11889                 (unless (string-match "^[ \t]*$" input)
11890                   (setq all (string-to-number input))
11891                   (if (< all len)
11892                       (let ((older-range (nreverse older)))
11893                         (setq older nil)
11894
11895                         (while (> all 0)
11896                           (let* ((r (pop older-range))
11897                                  (min (if (numberp r) r (car r)))
11898                                  (max (if (numberp r) r (cdr r))))
11899                             (while (and (<= min max)
11900                                         (> all 0))
11901                               (push max older)
11902                               (setq all (1- all)
11903                                     max (1- max))))))))))
11904           (setq older (gnus-uncompress-range older))))
11905         (if (not older)
11906             (message "No old news.")
11907           (gnus-summary-insert-articles older)
11908           (gnus-summary-limit (gnus-sorted-nunion old older))))
11909     (gnus-summary-position-point)))
11910
11911 (defun gnus-summary-insert-new-articles ()
11912   "Insert all new articles in this group."
11913   (interactive)
11914   (prog1
11915       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11916             (old-active gnus-newsgroup-active)
11917             (nnmail-fetched-sources (list t))
11918             i new)
11919         (setq gnus-newsgroup-active
11920               (gnus-activate-group gnus-newsgroup-name 'scan))
11921         (setq i (cdr gnus-newsgroup-active))
11922         (while (> i (cdr old-active))
11923           (push i new)
11924           (decf i))
11925         (if (not new)
11926             (message "No gnus is bad news")
11927           (gnus-summary-insert-articles new)
11928           (setq gnus-newsgroup-unreads
11929                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11930           (gnus-summary-limit (gnus-sorted-nunion old new))))
11931     (gnus-summary-position-point)))
11932
11933 (gnus-summary-make-all-marking-commands)
11934
11935 (gnus-ems-redefine)
11936
11937 (provide 'gnus-sum)
11938
11939 (run-hooks 'gnus-sum-load-hook)
11940
11941 ;; Local Variables:
11942 ;; coding: iso-8859-1
11943 ;; End:
11944
11945 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11946 ;;; gnus-sum.el ends here