2005-10-29 Sat Jari Aalto <jari dot aalto A T cante dot net>
[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 (defsubst gnus-article-sort-by-date-reverse (h1 h2)
4634   "Sort articles in reverse order by root article date."
4635   (gnus-article-sort-by-date h2 h1))
4636
4637 (defun gnus-thread-sort-by-date (h1 h2)
4638   "Sort threads by root article date."
4639   (gnus-article-sort-by-date
4640    (gnus-thread-header h1) (gnus-thread-header h2)))
4641
4642 (defsubst gnus-article-sort-by-score (h1 h2)
4643   "Sort articles by root article score.
4644 Unscored articles will be counted as having a score of zero."
4645   (> (or (cdr (assq (mail-header-number h1)
4646                     gnus-newsgroup-scored))
4647          gnus-summary-default-score 0)
4648      (or (cdr (assq (mail-header-number h2)
4649                     gnus-newsgroup-scored))
4650          gnus-summary-default-score 0)))
4651
4652 (defun gnus-thread-sort-by-score (h1 h2)
4653   "Sort threads by root article score."
4654   (gnus-article-sort-by-score
4655    (gnus-thread-header h1) (gnus-thread-header h2)))
4656
4657 (defun gnus-thread-sort-by-total-score (h1 h2)
4658   "Sort threads by the sum of all scores in the thread.
4659 Unscored articles will be counted as having a score of zero."
4660   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4661
4662 (defun gnus-thread-total-score (thread)
4663   ;; This function find the total score of THREAD.
4664   (cond
4665    ((null thread)
4666     0)
4667    ((consp thread)
4668     (if (stringp (car thread))
4669         (apply gnus-thread-score-function 0
4670                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4671       (gnus-thread-total-score-1 thread)))
4672    (t
4673     (gnus-thread-total-score-1 (list thread)))))
4674
4675 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4676   "Sort threads such that the thread with the most recently arrived article comes first."
4677   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4678
4679 (defun gnus-thread-highest-number (thread)
4680   "Return the highest article number in THREAD."
4681   (apply 'max (mapcar (lambda (header)
4682                         (mail-header-number header))
4683                       (message-flatten-list thread))))
4684
4685 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4686   "Sort threads such that the thread with the most recently dated article comes first."
4687   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4688
4689 (defun gnus-thread-latest-date (thread)
4690   "Return the highest article date in THREAD."
4691   (let ((previous-time 0))
4692     (apply 'max
4693            (mapcar
4694             (lambda (header)
4695               (setq previous-time
4696                     (condition-case ()
4697                         (time-to-seconds (mail-header-parse-date
4698                                           (mail-header-date header)))
4699                       (error previous-time))))
4700             (sort
4701              (message-flatten-list thread)
4702              (lambda (h1 h2)
4703                (< (mail-header-number h1)
4704                   (mail-header-number h2))))))))
4705
4706 (defun gnus-thread-total-score-1 (root)
4707   ;; This function find the total score of the thread below ROOT.
4708   (setq root (car root))
4709   (apply gnus-thread-score-function
4710          (or (append
4711               (mapcar 'gnus-thread-total-score
4712                       (cdr (gnus-id-to-thread (mail-header-id root))))
4713               (when (> (mail-header-number root) 0)
4714                 (list (or (cdr (assq (mail-header-number root)
4715                                      gnus-newsgroup-scored))
4716                           gnus-summary-default-score 0))))
4717              (list gnus-summary-default-score)
4718              '(0))))
4719
4720 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4721 (defvar gnus-tmp-prev-subject nil)
4722 (defvar gnus-tmp-false-parent nil)
4723 (defvar gnus-tmp-root-expunged nil)
4724 (defvar gnus-tmp-dummy-line nil)
4725
4726 (eval-when-compile (defvar gnus-tmp-header))
4727 (defun gnus-extra-header (type &optional header)
4728   "Return the extra header of TYPE."
4729   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4730       ""))
4731
4732 (defvar gnus-tmp-thread-tree-header-string "")
4733
4734 (defcustom gnus-sum-thread-tree-root "> "
4735   "With %B spec, used for the root of a thread.
4736 If nil, use subject instead."
4737   :version "22.1"
4738   :type '(radio (const :format "%v  " nil) string)
4739   :group 'gnus-thread)
4740
4741 (defcustom gnus-sum-thread-tree-false-root "> "
4742   "With %B spec, used for a false root of a thread.
4743 If nil, use subject instead."
4744   :version "22.1"
4745   :type '(radio (const :format "%v  " nil) string)
4746   :group 'gnus-thread)
4747
4748 (defcustom gnus-sum-thread-tree-single-indent ""
4749   "With %B spec, used for a thread with just one message.
4750 If nil, use subject instead."
4751   :version "22.1"
4752   :type '(radio (const :format "%v  " nil) string)
4753   :group 'gnus-thread)
4754
4755 (defcustom gnus-sum-thread-tree-vertical "| "
4756   "With %B spec, used for drawing a vertical line."
4757   :version "22.1"
4758   :type 'string
4759   :group 'gnus-thread)
4760
4761 (defcustom gnus-sum-thread-tree-indent "  "
4762   "With %B spec, used for indenting."
4763   :version "22.1"
4764   :type 'string
4765   :group 'gnus-thread)
4766
4767 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4768   "With %B spec, used for a leaf with brothers."
4769   :version "22.1"
4770   :type 'string
4771   :group 'gnus-thread)
4772
4773 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4774   "With %B spec, used for a leaf without brothers."
4775   :version "22.1"
4776   :type 'string
4777   :group 'gnus-thread)
4778
4779 (defun gnus-summary-prepare-threads (threads)
4780   "Prepare summary buffer from THREADS and indentation LEVEL.
4781 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4782 or a straight list of headers."
4783   (gnus-message 7 "Generating summary...")
4784
4785   (setq gnus-newsgroup-threads threads)
4786   (beginning-of-line)
4787
4788   (let ((gnus-tmp-level 0)
4789         (default-score (or gnus-summary-default-score 0))
4790         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4791         (building-line-count gnus-summary-display-while-building)
4792         (building-count (integerp gnus-summary-display-while-building))
4793         thread number subject stack state gnus-tmp-gathered beg-match
4794         new-roots gnus-tmp-new-adopts thread-end simp-subject
4795         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4796         gnus-tmp-replied gnus-tmp-subject-or-nil
4797         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4798         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4799         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4800         tree-stack)
4801
4802     (setq gnus-tmp-prev-subject nil
4803           gnus-tmp-thread-tree-header-string "")
4804
4805     (if (vectorp (car threads))
4806         ;; If this is a straight (sic) list of headers, then a
4807         ;; threaded summary display isn't required, so we just create
4808         ;; an unthreaded one.
4809         (gnus-summary-prepare-unthreaded threads)
4810
4811       ;; Do the threaded display.
4812
4813       (if gnus-summary-display-while-building
4814           (switch-to-buffer (buffer-name)))
4815       (while (or threads stack gnus-tmp-new-adopts new-roots)
4816
4817         (if (and (= gnus-tmp-level 0)
4818                  (or (not stack)
4819                      (= (caar stack) 0))
4820                  (not gnus-tmp-false-parent)
4821                  (or gnus-tmp-new-adopts new-roots))
4822             (if gnus-tmp-new-adopts
4823                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4824                       thread (list (car gnus-tmp-new-adopts))
4825                       gnus-tmp-header (caar thread)
4826                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4827               (when new-roots
4828                 (setq thread (list (car new-roots))
4829                       gnus-tmp-header (caar thread)
4830                       new-roots (cdr new-roots))))
4831
4832           (if threads
4833               ;; If there are some threads, we do them before the
4834               ;; threads on the stack.
4835               (setq thread threads
4836                     gnus-tmp-header (caar thread))
4837             ;; There were no current threads, so we pop something off
4838             ;; the stack.
4839             (setq state (car stack)
4840                   gnus-tmp-level (car state)
4841                   tree-stack (cadr state)
4842                   thread (caddr state)
4843                   stack (cdr stack)
4844                   gnus-tmp-header (caar thread))))
4845
4846         (setq gnus-tmp-false-parent nil)
4847         (setq gnus-tmp-root-expunged nil)
4848         (setq thread-end nil)
4849
4850         (if (stringp gnus-tmp-header)
4851             ;; The header is a dummy root.
4852             (cond
4853              ((eq gnus-summary-make-false-root 'adopt)
4854               ;; We let the first article adopt the rest.
4855               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4856                                                (cddar thread)))
4857               (setq gnus-tmp-gathered
4858                     (nconc (mapcar
4859                             (lambda (h) (mail-header-number (car h)))
4860                             (cddar thread))
4861                            gnus-tmp-gathered))
4862               (setq thread (cons (list (caar thread)
4863                                        (cadar thread))
4864                                  (cdr thread)))
4865               (setq gnus-tmp-level -1
4866                     gnus-tmp-false-parent t))
4867              ((eq gnus-summary-make-false-root 'empty)
4868               ;; We print adopted articles with empty subject fields.
4869               (setq gnus-tmp-gathered
4870                     (nconc (mapcar
4871                             (lambda (h) (mail-header-number (car h)))
4872                             (cddar thread))
4873                            gnus-tmp-gathered))
4874               (setq gnus-tmp-level -1))
4875              ((eq gnus-summary-make-false-root 'dummy)
4876               ;; We remember that we probably want to output a dummy
4877               ;; root.
4878               (setq gnus-tmp-dummy-line gnus-tmp-header)
4879               (setq gnus-tmp-prev-subject gnus-tmp-header))
4880              (t
4881               ;; We do not make a root for the gathered
4882               ;; sub-threads at all.
4883               (setq gnus-tmp-level -1)))
4884
4885           (setq number (mail-header-number gnus-tmp-header)
4886                 subject (mail-header-subject gnus-tmp-header)
4887                 simp-subject (gnus-simplify-subject-fully subject))
4888
4889           (cond
4890            ;; If the thread has changed subject, we might want to make
4891            ;; this subthread into a root.
4892            ((and (null gnus-thread-ignore-subject)
4893                  (not (zerop gnus-tmp-level))
4894                  gnus-tmp-prev-subject
4895                  (not (string= gnus-tmp-prev-subject simp-subject)))
4896             (setq new-roots (nconc new-roots (list (car thread)))
4897                   thread-end t
4898                   gnus-tmp-header nil))
4899            ;; If the article lies outside the current limit,
4900            ;; then we do not display it.
4901            ((not (memq number gnus-newsgroup-limit))
4902             (setq gnus-tmp-gathered
4903                   (nconc (mapcar
4904                           (lambda (h) (mail-header-number (car h)))
4905                           (cdar thread))
4906                          gnus-tmp-gathered))
4907             (setq gnus-tmp-new-adopts (if (cdar thread)
4908                                           (append gnus-tmp-new-adopts
4909                                                   (cdar thread))
4910                                         gnus-tmp-new-adopts)
4911                   thread-end t
4912                   gnus-tmp-header nil)
4913             (when (zerop gnus-tmp-level)
4914               (setq gnus-tmp-root-expunged t)))
4915            ;; Perhaps this article is to be marked as read?
4916            ((and gnus-summary-mark-below
4917                  (< (or (cdr (assq number gnus-newsgroup-scored))
4918                         default-score)
4919                     gnus-summary-mark-below)
4920                  ;; Don't touch sparse articles.
4921                  (not (gnus-summary-article-sparse-p number))
4922                  (not (gnus-summary-article-ancient-p number)))
4923             (setq gnus-newsgroup-unreads
4924                   (delq number gnus-newsgroup-unreads))
4925             (if gnus-newsgroup-auto-expire
4926                 (setq gnus-newsgroup-expirable
4927                       (gnus-add-to-sorted-list
4928                        gnus-newsgroup-expirable number))
4929               (push (cons number gnus-low-score-mark)
4930                     gnus-newsgroup-reads))))
4931
4932           (when gnus-tmp-header
4933             ;; We may have an old dummy line to output before this
4934             ;; article.
4935             (when (and gnus-tmp-dummy-line
4936                        (gnus-subject-equal
4937                         gnus-tmp-dummy-line
4938                         (mail-header-subject gnus-tmp-header)))
4939               (gnus-summary-insert-dummy-line
4940                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4941               (setq gnus-tmp-dummy-line nil))
4942
4943             ;; Compute the mark.
4944             (setq gnus-tmp-unread (gnus-article-mark number))
4945
4946             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4947                                   gnus-tmp-header gnus-tmp-level)
4948                   gnus-newsgroup-data)
4949
4950             ;; Actually insert the line.
4951             (setq
4952              gnus-tmp-subject-or-nil
4953              (cond
4954               ((and gnus-thread-ignore-subject
4955                     gnus-tmp-prev-subject
4956                     (not (string= gnus-tmp-prev-subject simp-subject)))
4957                subject)
4958               ((zerop gnus-tmp-level)
4959                (if (and (eq gnus-summary-make-false-root 'empty)
4960                         (memq number gnus-tmp-gathered)
4961                         gnus-tmp-prev-subject
4962                         (string= gnus-tmp-prev-subject simp-subject))
4963                    gnus-summary-same-subject
4964                  subject))
4965               (t gnus-summary-same-subject)))
4966             (if (and (eq gnus-summary-make-false-root 'adopt)
4967                      (= gnus-tmp-level 1)
4968                      (memq number gnus-tmp-gathered))
4969                 (setq gnus-tmp-opening-bracket ?\<
4970                       gnus-tmp-closing-bracket ?\>)
4971               (setq gnus-tmp-opening-bracket ?\[
4972                     gnus-tmp-closing-bracket ?\]))
4973             (setq
4974              gnus-tmp-indentation
4975              (aref gnus-thread-indent-array gnus-tmp-level)
4976              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4977              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4978                                 gnus-summary-default-score 0)
4979              gnus-tmp-score-char
4980              (if (or (null gnus-summary-default-score)
4981                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4982                          gnus-summary-zcore-fuzz))
4983                  ?                      ;Whitespace
4984                (if (< gnus-tmp-score gnus-summary-default-score)
4985                    gnus-score-below-mark gnus-score-over-mark))
4986              gnus-tmp-replied
4987              (cond ((memq number gnus-newsgroup-processable)
4988                     gnus-process-mark)
4989                    ((memq number gnus-newsgroup-cached)
4990                     gnus-cached-mark)
4991                    ((memq number gnus-newsgroup-replied)
4992                     gnus-replied-mark)
4993                    ((memq number gnus-newsgroup-forwarded)
4994                     gnus-forwarded-mark)
4995                    ((memq number gnus-newsgroup-saved)
4996                     gnus-saved-mark)
4997                    ((memq number gnus-newsgroup-recent)
4998                     gnus-recent-mark)
4999                    ((memq number gnus-newsgroup-unseen)
5000                     gnus-unseen-mark)
5001                    (t gnus-no-mark))
5002              gnus-tmp-downloaded
5003              (cond ((memq number gnus-newsgroup-undownloaded)
5004                     gnus-undownloaded-mark)
5005                    (gnus-newsgroup-agentized
5006                     gnus-downloaded-mark)
5007                    (t
5008                     gnus-no-mark))
5009              gnus-tmp-from (mail-header-from gnus-tmp-header)
5010              gnus-tmp-name
5011              (cond
5012               ((string-match "<[^>]+> *$" gnus-tmp-from)
5013                (setq beg-match (match-beginning 0))
5014                (or (and (string-match "^\".+\"" gnus-tmp-from)
5015                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5016                    (substring gnus-tmp-from 0 beg-match)))
5017               ((string-match "(.+)" gnus-tmp-from)
5018                (substring gnus-tmp-from
5019                           (1+ (match-beginning 0)) (1- (match-end 0))))
5020               (t gnus-tmp-from))
5021
5022              ;; Do the %B string
5023              gnus-tmp-thread-tree-header-string
5024              (cond
5025               ((not gnus-show-threads) "")
5026               ((zerop gnus-tmp-level)
5027                (cond ((cdar thread)
5028                       (or gnus-sum-thread-tree-root subject))
5029                      (gnus-tmp-new-adopts
5030                       (or gnus-sum-thread-tree-false-root subject))
5031                      (t
5032                       (or gnus-sum-thread-tree-single-indent subject))))
5033               (t
5034                (concat (apply 'concat
5035                               (mapcar (lambda (item)
5036                                         (if (= item 1)
5037                                             gnus-sum-thread-tree-vertical
5038                                           gnus-sum-thread-tree-indent))
5039                                       (cdr (reverse tree-stack))))
5040                        (if (nth 1 thread)
5041                            gnus-sum-thread-tree-leaf-with-other
5042                          gnus-sum-thread-tree-single-leaf)))))
5043             (when (string= gnus-tmp-name "")
5044               (setq gnus-tmp-name gnus-tmp-from))
5045             (unless (numberp gnus-tmp-lines)
5046               (setq gnus-tmp-lines -1))
5047             (if (= gnus-tmp-lines -1)
5048                 (setq gnus-tmp-lines "?")
5049               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5050               (gnus-put-text-property
5051              (point)
5052              (progn (eval gnus-summary-line-format-spec) (point))
5053                'gnus-number number)
5054             (when gnus-visual-p
5055               (forward-line -1)
5056               (gnus-run-hooks 'gnus-summary-update-hook)
5057               (forward-line 1))
5058
5059             (setq gnus-tmp-prev-subject simp-subject)))
5060
5061         (when (nth 1 thread)
5062           (push (list (max 0 gnus-tmp-level)
5063                       (copy-sequence tree-stack)
5064                       (nthcdr 1 thread))
5065                 stack))
5066         (push (if (nth 1 thread) 1 0) tree-stack)
5067         (incf gnus-tmp-level)
5068         (setq threads (if thread-end nil (cdar thread)))
5069         (if gnus-summary-display-while-building
5070             (if building-count
5071                 (progn
5072                   ;; use a set frequency
5073                   (setq building-line-count (1- building-line-count))
5074                   (when (= building-line-count 0)
5075                     (sit-for 0)
5076                     (setq building-line-count
5077                           gnus-summary-display-while-building)))
5078               ;; always
5079               (sit-for 0)))
5080         (unless threads
5081           (setq gnus-tmp-level 0)))))
5082   (gnus-message 7 "Generating summary...done"))
5083
5084 (defun gnus-summary-prepare-unthreaded (headers)
5085   "Generate an unthreaded summary buffer based on HEADERS."
5086   (let (header number mark)
5087
5088     (beginning-of-line)
5089
5090     (while headers
5091       ;; We may have to root out some bad articles...
5092       (when (memq (setq number (mail-header-number
5093                                 (setq header (pop headers))))
5094                   gnus-newsgroup-limit)
5095         ;; Mark article as read when it has a low score.
5096         (when (and gnus-summary-mark-below
5097                    (< (or (cdr (assq number gnus-newsgroup-scored))
5098                           gnus-summary-default-score 0)
5099                       gnus-summary-mark-below)
5100                    (not (gnus-summary-article-ancient-p number)))
5101           (setq gnus-newsgroup-unreads
5102                 (delq number gnus-newsgroup-unreads))
5103           (if gnus-newsgroup-auto-expire
5104               (push number gnus-newsgroup-expirable)
5105             (push (cons number gnus-low-score-mark)
5106                   gnus-newsgroup-reads)))
5107
5108         (setq mark (gnus-article-mark number))
5109         (push (gnus-data-make number mark (1+ (point)) header 0)
5110               gnus-newsgroup-data)
5111         (gnus-summary-insert-line
5112          header 0 number
5113          (memq number gnus-newsgroup-undownloaded)
5114          mark (memq number gnus-newsgroup-replied)
5115          (memq number gnus-newsgroup-expirable)
5116          (mail-header-subject header) nil
5117          (cdr (assq number gnus-newsgroup-scored))
5118          (memq number gnus-newsgroup-processable))))))
5119
5120 (defun gnus-summary-remove-list-identifiers ()
5121   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5122   (let ((regexp (if (consp gnus-list-identifiers)
5123                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5124                   gnus-list-identifiers))
5125         changed subject)
5126     (when regexp
5127       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5128       (dolist (header gnus-newsgroup-headers)
5129         (setq subject (mail-header-subject header)
5130               changed nil)
5131         (while (string-match regexp subject)
5132           (setq subject
5133                 (concat (substring subject 0 (match-beginning 1))
5134                         (substring subject (match-end 0)))
5135                 changed t))
5136         (when changed
5137           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5138             (setq subject
5139                   (concat (substring subject 0 (match-beginning 1))
5140                           (substring subject (match-end 1)))))
5141           (mail-header-set-subject header subject))))))
5142
5143 (defun gnus-fetch-headers (articles)
5144   "Fetch headers of ARTICLES."
5145   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5146     (gnus-message 5 "Fetching headers for %s..." name)
5147     (prog1
5148         (if (eq 'nov
5149                 (setq gnus-headers-retrieved-by
5150                       (gnus-retrieve-headers
5151                        articles gnus-newsgroup-name
5152                        ;; We might want to fetch old headers, but
5153                        ;; not if there is only 1 article.
5154                        (and (or (and
5155                                  (not (eq gnus-fetch-old-headers 'some))
5156                                  (not (numberp gnus-fetch-old-headers)))
5157                                 (> (length articles) 1))
5158                             gnus-fetch-old-headers))))
5159             (gnus-get-newsgroup-headers-xover
5160              articles nil nil gnus-newsgroup-name t)
5161           (gnus-get-newsgroup-headers))
5162       (gnus-message 5 "Fetching headers for %s...done" name))))
5163
5164 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5165   "Select newsgroup GROUP.
5166 If READ-ALL is non-nil, all articles in the group are selected.
5167 If SELECT-ARTICLES, only select those articles from GROUP."
5168   (let* ((entry (gnus-group-entry group))
5169          ;;!!! Dirty hack; should be removed.
5170          (gnus-summary-ignore-duplicates
5171           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5172               t
5173             gnus-summary-ignore-duplicates))
5174          (info (nth 2 entry))
5175          articles fetched-articles cached)
5176
5177     (unless (gnus-check-server
5178              (set (make-local-variable 'gnus-current-select-method)
5179                   (gnus-find-method-for-group group)))
5180       (error "Couldn't open server"))
5181
5182     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5183         (gnus-activate-group group)     ; Or we can activate it...
5184         (progn                          ; Or we bug out.
5185           (when (equal major-mode 'gnus-summary-mode)
5186             (gnus-kill-buffer (current-buffer)))
5187           (error "Couldn't activate group %s: %s"
5188                  group (gnus-status-message group))))
5189
5190     (unless (gnus-request-group group t)
5191       (when (equal major-mode 'gnus-summary-mode)
5192         (gnus-kill-buffer (current-buffer)))
5193       (error "Couldn't request group %s: %s"
5194              group (gnus-status-message group)))
5195
5196     (when gnus-agent
5197       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5198
5199       (setq gnus-summary-use-undownloaded-faces
5200             (gnus-agent-find-parameter
5201              group
5202              'agent-enable-undownloaded-faces)))
5203
5204     (setq gnus-newsgroup-name group
5205           gnus-newsgroup-unselected nil
5206           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5207
5208     (let ((display (gnus-group-find-parameter group 'display)))
5209       (setq gnus-newsgroup-display
5210             (cond
5211              ((not (zerop (or (car-safe read-all) 0)))
5212               ;; The user entered the group with C-u SPC/RET, let's show
5213               ;; all articles.
5214               'gnus-not-ignore)
5215              ((eq display 'all)
5216               'gnus-not-ignore)
5217              ((arrayp display)
5218               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5219              ((numberp display)
5220               ;; The following is probably the "correct" solution, but
5221               ;; it makes Gnus fetch all headers and then limit the
5222               ;; articles (which is slow), so instead we hack the
5223               ;; select-articles parameter instead. -- Simon Josefsson
5224               ;; <jas@kth.se>
5225               ;;
5226               ;; (gnus-byte-compile
5227               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5228               ;;                         display)))))
5229               (setq select-articles
5230                     (gnus-uncompress-range
5231                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5232                              (if (> tmp 0)
5233                                  tmp
5234                                1))
5235                            (cdr (gnus-active group)))))
5236               nil)
5237              (t
5238               nil))))
5239
5240     (gnus-summary-setup-default-charset)
5241
5242     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5243     (when (gnus-virtual-group-p group)
5244       (setq cached gnus-newsgroup-cached))
5245
5246     (setq gnus-newsgroup-unreads
5247           (gnus-sorted-ndifference
5248            (gnus-sorted-ndifference gnus-newsgroup-unreads
5249                                     gnus-newsgroup-marked)
5250            gnus-newsgroup-dormant))
5251
5252     (setq gnus-newsgroup-processable nil)
5253
5254     (gnus-update-read-articles group gnus-newsgroup-unreads)
5255
5256     ;; Adjust and set lists of article marks.
5257     (when info
5258       (gnus-adjust-marked-articles info))
5259     (if (setq articles select-articles)
5260         (setq gnus-newsgroup-unselected
5261               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5262       (setq articles (gnus-articles-to-read group read-all)))
5263
5264     (cond
5265      ((null articles)
5266       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5267       'quit)
5268      ((eq articles 0) nil)
5269      (t
5270       ;; Init the dependencies hash table.
5271       (setq gnus-newsgroup-dependencies
5272             (gnus-make-hashtable (length articles)))
5273       (gnus-set-global-variables)
5274       ;; Retrieve the headers and read them in.
5275
5276       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5277
5278       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5279       (when cached
5280         (setq gnus-newsgroup-cached cached))
5281
5282       ;; Suppress duplicates?
5283       (when gnus-suppress-duplicates
5284         (gnus-dup-suppress-articles))
5285
5286       ;; Set the initial limit.
5287       (setq gnus-newsgroup-limit (copy-sequence articles))
5288       ;; Remove canceled articles from the list of unread articles.
5289       (setq fetched-articles
5290             (mapcar (lambda (headers) (mail-header-number headers))
5291                     gnus-newsgroup-headers))
5292       (setq gnus-newsgroup-articles fetched-articles)
5293       (setq gnus-newsgroup-unreads
5294             (gnus-sorted-nintersection
5295              gnus-newsgroup-unreads fetched-articles))
5296       (gnus-compute-unseen-list)
5297
5298       ;; Removed marked articles that do not exist.
5299       (gnus-update-missing-marks
5300        (gnus-sorted-difference articles fetched-articles))
5301       ;; We might want to build some more threads first.
5302       (when (and gnus-fetch-old-headers
5303                  (eq gnus-headers-retrieved-by 'nov))
5304         (if (eq gnus-fetch-old-headers 'invisible)
5305             (gnus-build-all-threads)
5306           (gnus-build-old-threads)))
5307       ;; Let the Gnus agent mark articles as read.
5308       (when gnus-agent
5309         (gnus-agent-get-undownloaded-list))
5310       ;; Remove list identifiers from subject
5311       (when gnus-list-identifiers
5312         (gnus-summary-remove-list-identifiers))
5313       ;; Check whether auto-expire is to be done in this group.
5314       (setq gnus-newsgroup-auto-expire
5315             (gnus-group-auto-expirable-p group))
5316       ;; Set up the article buffer now, if necessary.
5317       (unless gnus-single-article-buffer
5318         (gnus-article-setup-buffer))
5319       ;; First and last article in this newsgroup.
5320       (when gnus-newsgroup-headers
5321         (setq gnus-newsgroup-begin
5322               (mail-header-number (car gnus-newsgroup-headers))
5323               gnus-newsgroup-end
5324               (mail-header-number
5325                (gnus-last-element gnus-newsgroup-headers))))
5326       ;; GROUP is successfully selected.
5327       (or gnus-newsgroup-headers t)))))
5328
5329 (defun gnus-compute-unseen-list ()
5330   ;; The `seen' marks are treated specially.
5331   (if (not gnus-newsgroup-seen)
5332       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5333     (setq gnus-newsgroup-unseen
5334           (gnus-inverse-list-range-intersection
5335            gnus-newsgroup-articles gnus-newsgroup-seen))))
5336
5337 (defun gnus-summary-display-make-predicate (display)
5338   (require 'gnus-agent)
5339   (when (= (length display) 1)
5340     (setq display (car display)))
5341   (unless gnus-summary-display-cache
5342     (dolist (elem (append '((unread . unread)
5343                             (read . read)
5344                             (unseen . unseen))
5345                           gnus-article-mark-lists))
5346       (push (cons (cdr elem)
5347                   (gnus-byte-compile
5348                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5349             gnus-summary-display-cache)))
5350   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5351         (gnus-category-predicate-cache gnus-summary-display-cache))
5352     (gnus-get-predicate display)))
5353
5354 ;; Uses the dynamically bound `number' variable.
5355 (eval-when-compile
5356   (defvar number))
5357 (defun gnus-article-marked-p (type &optional article)
5358   (let ((article (or article number)))
5359     (cond
5360      ((eq type 'tick)
5361       (memq article gnus-newsgroup-marked))
5362      ((eq type 'spam)
5363       (memq article gnus-newsgroup-spam-marked))
5364      ((eq type 'unsend)
5365       (memq article gnus-newsgroup-unsendable))
5366      ((eq type 'undownload)
5367       (memq article gnus-newsgroup-undownloaded))
5368      ((eq type 'download)
5369       (memq article gnus-newsgroup-downloadable))
5370      ((eq type 'unread)
5371       (memq article gnus-newsgroup-unreads))
5372      ((eq type 'read)
5373       (memq article gnus-newsgroup-reads))
5374      ((eq type 'dormant)
5375       (memq article gnus-newsgroup-dormant) )
5376      ((eq type 'expire)
5377       (memq article gnus-newsgroup-expirable))
5378      ((eq type 'reply)
5379       (memq article gnus-newsgroup-replied))
5380      ((eq type 'killed)
5381       (memq article gnus-newsgroup-killed))
5382      ((eq type 'bookmark)
5383       (assq article gnus-newsgroup-bookmarks))
5384      ((eq type 'score)
5385       (assq article gnus-newsgroup-scored))
5386      ((eq type 'save)
5387       (memq article gnus-newsgroup-saved))
5388      ((eq type 'cache)
5389       (memq article gnus-newsgroup-cached))
5390      ((eq type 'forward)
5391       (memq article gnus-newsgroup-forwarded))
5392      ((eq type 'seen)
5393       (not (memq article gnus-newsgroup-unseen)))
5394      ((eq type 'recent)
5395       (memq article gnus-newsgroup-recent))
5396      (t t))))
5397
5398 (defun gnus-articles-to-read (group &optional read-all)
5399   "Find out what articles the user wants to read."
5400   (let* ((articles
5401           ;; Select all articles if `read-all' is non-nil, or if there
5402           ;; are no unread articles.
5403           (if (or read-all
5404                   (and (zerop (length gnus-newsgroup-marked))
5405                        (zerop (length gnus-newsgroup-unreads)))
5406                   ;; Fetch all if the predicate is non-nil.
5407                   gnus-newsgroup-display)
5408               ;; We want to select the headers for all the articles in
5409               ;; the group, so we select either all the active
5410               ;; articles in the group, or (if that's nil), the
5411               ;; articles in the cache.
5412               (or
5413                (gnus-uncompress-range (gnus-active group))
5414                (gnus-cache-articles-in-group group))
5415             ;; Select only the "normal" subset of articles.
5416             (gnus-sorted-nunion
5417              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5418              gnus-newsgroup-unreads)))
5419          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5420          (scored (length scored-list))
5421          (number (length articles))
5422          (marked (+ (length gnus-newsgroup-marked)
5423                     (length gnus-newsgroup-dormant)))
5424          (select
5425           (cond
5426            ((numberp read-all)
5427             read-all)
5428            ((numberp gnus-newsgroup-display)
5429             gnus-newsgroup-display)
5430            (t
5431             (condition-case ()
5432                 (cond
5433                  ((and (or (<= scored marked) (= scored number))
5434                        (numberp gnus-large-newsgroup)
5435                        (> number gnus-large-newsgroup))
5436                   (let* ((cursor-in-echo-area nil)
5437                          (initial (gnus-parameter-large-newsgroup-initial
5438                                    gnus-newsgroup-name))
5439                          (input
5440                           (read-string
5441                            (format
5442                             "How many articles from %s (%s %d): "
5443                             (gnus-limit-string
5444                              (gnus-group-decoded-name gnus-newsgroup-name)
5445                              35)
5446                             (if initial "max" "default")
5447                             number)
5448                            (if initial
5449                                (cons (number-to-string initial)
5450                                      0)))))
5451                     (if (string-match "^[ \t]*$" input) number input)))
5452                  ((and (> scored marked) (< scored number)
5453                        (> (- scored number) 20))
5454                   (let ((input
5455                          (read-string
5456                           (format "%s %s (%d scored, %d total): "
5457                                   "How many articles from"
5458                                   (gnus-group-decoded-name group)
5459                                   scored number))))
5460                     (if (string-match "^[ \t]*$" input)
5461                         number input)))
5462                  (t number))
5463               (quit
5464                (message "Quit getting the articles to read")
5465                nil))))))
5466     (setq select (if (stringp select) (string-to-number select) select))
5467     (if (or (null select) (zerop select))
5468         select
5469       (if (and (not (zerop scored)) (<= (abs select) scored))
5470           (progn
5471             (setq articles (sort scored-list '<))
5472             (setq number (length articles)))
5473         (setq articles (copy-sequence articles)))
5474
5475       (when (< (abs select) number)
5476         (if (< select 0)
5477             ;; Select the N oldest articles.
5478             (setcdr (nthcdr (1- (abs select)) articles) nil)
5479           ;; Select the N most recent articles.
5480           (setq articles (nthcdr (- number select) articles))))
5481       (setq gnus-newsgroup-unselected
5482             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5483       (when gnus-alter-articles-to-read-function
5484         (setq articles
5485               (sort
5486                (funcall gnus-alter-articles-to-read-function
5487                         gnus-newsgroup-name articles)
5488                '<)))
5489       articles)))
5490
5491 (defun gnus-killed-articles (killed articles)
5492   (let (out)
5493     (while articles
5494       (when (inline (gnus-member-of-range (car articles) killed))
5495         (push (car articles) out))
5496       (setq articles (cdr articles)))
5497     out))
5498
5499 (defun gnus-uncompress-marks (marks)
5500   "Uncompress the mark ranges in MARKS."
5501   (let ((uncompressed '(score bookmark))
5502         out)
5503     (while marks
5504       (if (memq (caar marks) uncompressed)
5505           (push (car marks) out)
5506         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5507       (setq marks (cdr marks)))
5508     out))
5509
5510 (defun gnus-article-mark-to-type (mark)
5511   "Return the type of MARK."
5512   (or (cadr (assq mark gnus-article-special-mark-lists))
5513       'list))
5514
5515 (defun gnus-article-unpropagatable-p (mark)
5516   "Return whether MARK should be propagated to back end."
5517   (memq mark gnus-article-unpropagated-mark-lists))
5518
5519 (defun gnus-adjust-marked-articles (info)
5520   "Set all article lists and remove all marks that are no longer valid."
5521   (let* ((marked-lists (gnus-info-marks info))
5522          (active (gnus-active (gnus-info-group info)))
5523          (min (car active))
5524          (max (cdr active))
5525          (types gnus-article-mark-lists)
5526          marks var articles article mark mark-type
5527          bgn end)
5528
5529     (dolist (marks marked-lists)
5530       (setq mark (car marks)
5531             mark-type (gnus-article-mark-to-type mark)
5532             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5533
5534       ;; We set the variable according to the type of the marks list,
5535       ;; and then adjust the marks to a subset of the active articles.
5536       (cond
5537        ;; Adjust "simple" lists - compressed yet unsorted
5538        ((eq mark-type 'list)
5539         ;; Simultaneously uncompress and clip to active range
5540         ;; See gnus-uncompress-range for a description of possible marks
5541         (let (l lh)
5542           (if (not (cadr marks))
5543               (set var nil)
5544             (setq articles (if (numberp (cddr marks))
5545                                (list (cdr marks))
5546                              (cdr marks))
5547                   lh (cons nil nil)
5548                   l lh)
5549
5550             (while (setq article (pop articles))
5551               (cond ((consp article)
5552                      (setq bgn (max (car article) min)
5553                            end (min (cdr article) max))
5554                      (while (<= bgn end)
5555                        (setq l (setcdr l (cons bgn nil))
5556                              bgn (1+ bgn))))
5557                     ((and (<= min article)
5558                           (>= max article))
5559                      (setq l (setcdr l (cons article nil))))))
5560             (set var (cdr lh)))))
5561        ;; Adjust assocs.
5562        ((eq mark-type 'tuple)
5563         (set var (setq articles (cdr marks)))
5564         (when (not (listp (cdr (symbol-value var))))
5565           (set var (list (symbol-value var))))
5566         (when (not (listp (cdr articles)))
5567           (setq articles (list articles)))
5568         (while articles
5569           (when (or (not (consp (setq article (pop articles))))
5570                     (< (car article) min)
5571                     (> (car article) max))
5572             (set var (delq article (symbol-value var))))))
5573        ;; Adjust ranges (sloppily).
5574        ((eq mark-type 'range)
5575         (cond
5576          ((eq mark 'seen)
5577           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5578           ;; It should be (seen (NUM1 . NUM2)).
5579           (when (numberp (cddr marks))
5580             (setcdr marks (list (cdr marks))))
5581           (setq articles (cdr marks))
5582           (while (and articles
5583                       (or (and (consp (car articles))
5584                                (> min (cdar articles)))
5585                           (and (numberp (car articles))
5586                                (> min (car articles)))))
5587             (pop articles))
5588           (set var articles))))))))
5589
5590 (defun gnus-update-missing-marks (missing)
5591   "Go through the list of MISSING articles and remove them from the mark lists."
5592   (when missing
5593     (let (var m)
5594       ;; Go through all types.
5595       (dolist (elem gnus-article-mark-lists)
5596         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5597           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5598           (when (symbol-value var)
5599             ;; This list has articles.  So we delete all missing
5600             ;; articles from it.
5601             (setq m missing)
5602             (while m
5603               (set var (delq (pop m) (symbol-value var))))))))))
5604
5605 (defun gnus-update-marks ()
5606   "Enter the various lists of marked articles into the newsgroup info list."
5607   (let ((types gnus-article-mark-lists)
5608         (info (gnus-get-info gnus-newsgroup-name))
5609         type list newmarked symbol delta-marks)
5610     (when info
5611       ;; Add all marks lists to the list of marks lists.
5612       (while (setq type (pop types))
5613         (setq list (symbol-value
5614                     (setq symbol
5615                           (intern (format "gnus-newsgroup-%s" (car type))))))
5616
5617         (when list
5618           ;; Get rid of the entries of the articles that have the
5619           ;; default score.
5620           (when (and (eq (cdr type) 'score)
5621                      gnus-save-score
5622                      list)
5623             (let* ((arts list)
5624                    (prev (cons nil list))
5625                    (all prev))
5626               (while arts
5627                 (if (or (not (consp (car arts)))
5628                         (= (cdar arts) gnus-summary-default-score))
5629                     (setcdr prev (cdr arts))
5630                   (setq prev arts))
5631                 (setq arts (cdr arts)))
5632               (setq list (cdr all)))))
5633
5634         (when (eq (cdr type) 'seen)
5635           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5636
5637         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5638           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5639
5640         (when (and (gnus-check-backend-function
5641                     'request-set-mark gnus-newsgroup-name)
5642                    (not (gnus-article-unpropagatable-p (cdr type))))
5643           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5644                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5645                  (add (gnus-remove-from-range
5646                        (gnus-copy-sequence list) old)))
5647             (when add
5648               (push (list add 'add (list (cdr type))) delta-marks))
5649             (when del
5650               (push (list del 'del (list (cdr type))) delta-marks))))
5651
5652         (when list
5653           (push (cons (cdr type) list) newmarked)))
5654
5655       (when delta-marks
5656         (unless (gnus-check-group gnus-newsgroup-name)
5657           (error "Can't open server for %s" gnus-newsgroup-name))
5658         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5659
5660       ;; Enter these new marks into the info of the group.
5661       (if (nthcdr 3 info)
5662           (setcar (nthcdr 3 info) newmarked)
5663         ;; Add the marks lists to the end of the info.
5664         (when newmarked
5665           (setcdr (nthcdr 2 info) (list newmarked))))
5666
5667       ;; Cut off the end of the info if there's nothing else there.
5668       (let ((i 5))
5669         (while (and (> i 2)
5670                     (not (nth i info)))
5671           (when (nthcdr (decf i) info)
5672             (setcdr (nthcdr i info) nil)))))))
5673
5674 (defun gnus-set-mode-line (where)
5675   "Set the mode line of the article or summary buffers.
5676 If WHERE is `summary', the summary mode line format will be used."
5677   ;; Is this mode line one we keep updated?
5678   (when (and (memq where gnus-updated-mode-lines)
5679              (symbol-value
5680               (intern (format "gnus-%s-mode-line-format-spec" where))))
5681     (let (mode-string)
5682       (save-excursion
5683         ;; We evaluate this in the summary buffer since these
5684         ;; variables are buffer-local to that buffer.
5685         (set-buffer gnus-summary-buffer)
5686        ;; We bind all these variables that are used in the `eval' form
5687         ;; below.
5688         (let* ((mformat (symbol-value
5689                          (intern
5690                           (format "gnus-%s-mode-line-format-spec" where))))
5691                (gnus-tmp-group-name (gnus-group-decoded-name
5692                                      gnus-newsgroup-name))
5693                (gnus-tmp-article-number (or gnus-current-article 0))
5694                (gnus-tmp-unread gnus-newsgroup-unreads)
5695                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5696                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5697                (gnus-tmp-unread-and-unselected
5698                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5699                             (zerop gnus-tmp-unselected))
5700                        "")
5701                       ((zerop gnus-tmp-unselected)
5702                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5703                       (t (format "{%d(+%d) more}"
5704                                  gnus-tmp-unread-and-unticked
5705                                  gnus-tmp-unselected))))
5706                (gnus-tmp-subject
5707                 (if (and gnus-current-headers
5708                          (vectorp gnus-current-headers))
5709                     (gnus-mode-string-quote
5710                      (mail-header-subject gnus-current-headers))
5711                   ""))
5712                bufname-length max-len
5713                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5714           (setq mode-string (eval mformat))
5715           (setq bufname-length (if (string-match "%b" mode-string)
5716                                    (- (length
5717                                        (buffer-name
5718                                         (if (eq where 'summary)
5719                                             nil
5720                                           (get-buffer gnus-article-buffer))))
5721                                       2)
5722                                  0))
5723           (setq max-len (max 4 (if gnus-mode-non-string-length
5724                                    (- (window-width)
5725                                       gnus-mode-non-string-length
5726                                       bufname-length)
5727                                  (length mode-string))))
5728           ;; We might have to chop a bit of the string off...
5729           (when (> (length mode-string) max-len)
5730             (setq mode-string
5731                   (concat (truncate-string-to-width mode-string (- max-len 3))
5732                           "...")))
5733           ;; Pad the mode string a bit.
5734           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5735       ;; Update the mode line.
5736       (setq mode-line-buffer-identification
5737             (gnus-mode-line-buffer-identification (list mode-string)))
5738       (set-buffer-modified-p t))))
5739
5740 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5741   "Go through the HEADERS list and add all Xrefs to a hash table.
5742 The resulting hash table is returned, or nil if no Xrefs were found."
5743   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5744          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5745          (xref-hashtb (gnus-make-hashtable))
5746          start group entry number xrefs header)
5747     (while headers
5748       (setq header (pop headers))
5749       (when (and (setq xrefs (mail-header-xref header))
5750                  (not (memq (setq number (mail-header-number header))
5751                             unreads)))
5752         (setq start 0)
5753         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5754           (setq start (match-end 0))
5755           (setq group (if prefix
5756                           (concat prefix (substring xrefs (match-beginning 1)
5757                                                     (match-end 1)))
5758                         (substring xrefs (match-beginning 1) (match-end 1))))
5759           (setq number
5760                 (string-to-number (substring xrefs (match-beginning 2)
5761                                           (match-end 2))))
5762           (if (setq entry (gnus-gethash group xref-hashtb))
5763               (setcdr entry (cons number (cdr entry)))
5764             (gnus-sethash group (cons number nil) xref-hashtb)))))
5765     (and start xref-hashtb)))
5766
5767 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5768   "Look through all the headers and mark the Xrefs as read."
5769   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5770         name info xref-hashtb idlist method nth4)
5771     (save-excursion
5772       (set-buffer gnus-group-buffer)
5773       (when (setq xref-hashtb
5774                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5775         (mapatoms
5776          (lambda (group)
5777            (unless (string= from-newsgroup (setq name (symbol-name group)))
5778              (setq idlist (symbol-value group))
5779              ;; Dead groups are not updated.
5780              (and (prog1
5781                       (setq info (gnus-get-info name))
5782                     (when (stringp (setq nth4 (gnus-info-method info)))
5783                       (setq nth4 (gnus-server-to-method nth4))))
5784                   ;; Only do the xrefs if the group has the same
5785                   ;; select method as the group we have just read.
5786                   (or (gnus-methods-equal-p
5787                        nth4 (gnus-find-method-for-group from-newsgroup))
5788                       virtual
5789                       (equal nth4 (setq method (gnus-find-method-for-group
5790                                                 from-newsgroup)))
5791                       (and (equal (car nth4) (car method))
5792                            (equal (nth 1 nth4) (nth 1 method))))
5793                   gnus-use-cross-reference
5794                   (or (not (eq gnus-use-cross-reference t))
5795                       virtual
5796                       ;; Only do cross-references on subscribed
5797                       ;; groups, if that is what is wanted.
5798                       (<= (gnus-info-level info) gnus-level-subscribed))
5799                   (gnus-group-make-articles-read name idlist))))
5800          xref-hashtb)))))
5801
5802 (defun gnus-compute-read-articles (group articles)
5803   (let* ((entry (gnus-group-entry group))
5804          (info (nth 2 entry))
5805          (active (gnus-active group))
5806          ninfo)
5807     (when entry
5808       ;; First peel off all invalid article numbers.
5809       (when active
5810         (let ((ids articles)
5811               id first)
5812           (while (setq id (pop ids))
5813             (when (and first (> id (cdr active)))
5814               ;; We'll end up in this situation in one particular
5815               ;; obscure situation.  If you re-scan a group and get
5816               ;; a new article that is cross-posted to a different
5817               ;; group that has not been re-scanned, you might get
5818               ;; crossposted article that has a higher number than
5819               ;; Gnus believes possible.  So we re-activate this
5820               ;; group as well.  This might mean doing the
5821               ;; crossposting thingy will *increase* the number
5822               ;; of articles in some groups.  Tsk, tsk.
5823               (setq active (or (gnus-activate-group group) active)))
5824             (when (or (> id (cdr active))
5825                       (< id (car active)))
5826               (setq articles (delq id articles))))))
5827       ;; If the read list is nil, we init it.
5828       (if (and active
5829                (null (gnus-info-read info))
5830                (> (car active) 1))
5831           (setq ninfo (cons 1 (1- (car active))))
5832         (setq ninfo (gnus-info-read info)))
5833       ;; Then we add the read articles to the range.
5834       (gnus-add-to-range
5835        ninfo (setq articles (sort articles '<))))))
5836
5837 (defun gnus-group-make-articles-read (group articles)
5838   "Update the info of GROUP to say that ARTICLES are read."
5839   (let* ((num 0)
5840          (entry (gnus-group-entry group))
5841          (info (nth 2 entry))
5842          (active (gnus-active group))
5843          range)
5844     (when entry
5845       (setq range (gnus-compute-read-articles group articles))
5846       (with-current-buffer gnus-group-buffer
5847         (gnus-undo-register
5848           `(progn
5849              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5850              (gnus-info-set-read ',info ',(gnus-info-read info))
5851              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5852              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5853              (gnus-group-update-group ,group t))))
5854       ;; Add the read articles to the range.
5855       (gnus-info-set-read info range)
5856       (gnus-request-set-mark group (list (list range 'add '(read))))
5857       ;; Then we have to re-compute how many unread
5858       ;; articles there are in this group.
5859       (when active
5860         (cond
5861          ((not range)
5862           (setq num (- (1+ (cdr active)) (car active))))
5863          ((not (listp (cdr range)))
5864           (setq num (- (cdr active) (- (1+ (cdr range))
5865                                        (car range)))))
5866          (t
5867           (while range
5868             (if (numberp (car range))
5869                 (setq num (1+ num))
5870               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5871             (setq range (cdr range)))
5872           (setq num (- (cdr active) num))))
5873         ;; Update the number of unread articles.
5874         (setcar entry num)
5875         ;; Update the group buffer.
5876         (unless (gnus-ephemeral-group-p group)
5877           (gnus-group-update-group group t))))))
5878
5879 (defvar gnus-newsgroup-none-id 0)
5880
5881 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5882   (let ((cur nntp-server-buffer)
5883         (dependencies
5884          (or dependencies
5885              (with-current-buffer gnus-summary-buffer
5886                gnus-newsgroup-dependencies)))
5887         headers id end ref number
5888         (mail-parse-charset gnus-newsgroup-charset)
5889         (mail-parse-ignored-charsets
5890          (save-excursion (condition-case nil
5891                              (set-buffer gnus-summary-buffer)
5892                            (error))
5893                          gnus-newsgroup-ignored-charsets)))
5894     (save-excursion
5895       (set-buffer nntp-server-buffer)
5896       ;; Translate all TAB characters into SPACE characters.
5897       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5898       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5899       (ietf-drums-unfold-fws)
5900       (gnus-run-hooks 'gnus-parse-headers-hook)
5901       (let ((case-fold-search t)
5902             in-reply-to header p lines chars)
5903         (goto-char (point-min))
5904         ;; Search to the beginning of the next header.  Error messages
5905         ;; do not begin with 2 or 3.
5906         (while (re-search-forward "^[23][0-9]+ " nil t)
5907           (setq id nil
5908                 ref nil)
5909           ;; This implementation of this function, with nine
5910           ;; search-forwards instead of the one re-search-forward and
5911           ;; a case (which basically was the old function) is actually
5912           ;; about twice as fast, even though it looks messier.  You
5913           ;; can't have everything, I guess.  Speed and elegance
5914           ;; doesn't always go hand in hand.
5915           (setq
5916            header
5917            (vector
5918             ;; Number.
5919             (prog1
5920                 (setq number (read cur))
5921               (end-of-line)
5922               (setq p (point))
5923               (narrow-to-region (point)
5924                                 (or (and (search-forward "\n.\n" nil t)
5925                                          (- (point) 2))
5926                                     (point))))
5927             ;; Subject.
5928             (progn
5929               (goto-char p)
5930               (if (search-forward "\nsubject:" nil t)
5931                   (funcall gnus-decode-encoded-word-function
5932                            (nnheader-header-value))
5933                 "(none)"))
5934             ;; From.
5935             (progn
5936               (goto-char p)
5937               (if (search-forward "\nfrom:" nil t)
5938                   (funcall gnus-decode-encoded-word-function
5939                            (nnheader-header-value))
5940                 "(nobody)"))
5941             ;; Date.
5942             (progn
5943               (goto-char p)
5944               (if (search-forward "\ndate:" nil t)
5945                   (nnheader-header-value) ""))
5946             ;; Message-ID.
5947             (progn
5948               (goto-char p)
5949               (setq id (if (re-search-forward
5950                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5951                            ;; We do it this way to make sure the Message-ID
5952                            ;; is (somewhat) syntactically valid.
5953                            (buffer-substring (match-beginning 1)
5954                                              (match-end 1))
5955                          ;; If there was no message-id, we just fake one
5956                          ;; to make subsequent routines simpler.
5957                          (nnheader-generate-fake-message-id number))))
5958             ;; References.
5959             (progn
5960               (goto-char p)
5961               (if (search-forward "\nreferences:" nil t)
5962                   (progn
5963                     (setq end (point))
5964                     (prog1
5965                         (nnheader-header-value)
5966                       (setq ref
5967                             (buffer-substring
5968                              (progn
5969                                (end-of-line)
5970                                (search-backward ">" end t)
5971                                (1+ (point)))
5972                              (progn
5973                                (search-backward "<" end t)
5974                                (point))))))
5975                 ;; Get the references from the in-reply-to header if there
5976                 ;; were no references and the in-reply-to header looks
5977                 ;; promising.
5978                 (if (and (search-forward "\nin-reply-to:" nil t)
5979                          (setq in-reply-to (nnheader-header-value))
5980                          (string-match "<[^>]+>" in-reply-to))
5981                     (let (ref2)
5982                       (setq ref (substring in-reply-to (match-beginning 0)
5983                                            (match-end 0)))
5984                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5985                         (setq ref2 (substring in-reply-to (match-beginning 0)
5986                                               (match-end 0)))
5987                         (when (> (length ref2) (length ref))
5988                           (setq ref ref2)))
5989                       ref)
5990                   (setq ref nil))))
5991             ;; Chars.
5992             (progn
5993               (goto-char p)
5994               (if (search-forward "\nchars: " nil t)
5995                   (if (numberp (setq chars (ignore-errors (read cur))))
5996                       chars -1)
5997                 -1))
5998             ;; Lines.
5999             (progn
6000               (goto-char p)
6001               (if (search-forward "\nlines: " nil t)
6002                   (if (numberp (setq lines (ignore-errors (read cur))))
6003                       lines -1)
6004                 -1))
6005             ;; Xref.
6006             (progn
6007               (goto-char p)
6008               (and (search-forward "\nxref:" nil t)
6009                    (nnheader-header-value)))
6010             ;; Extra.
6011             (when gnus-extra-headers
6012               (let ((extra gnus-extra-headers)
6013                     out)
6014                 (while extra
6015                   (goto-char p)
6016                   (when (search-forward
6017                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6018                     (push (cons (car extra) (nnheader-header-value))
6019                           out))
6020                   (pop extra))
6021                 out))))
6022           (when (equal id ref)
6023             (setq ref nil))
6024
6025           (when gnus-alter-header-function
6026             (funcall gnus-alter-header-function header)
6027             (setq id (mail-header-id header)
6028                   ref (gnus-parent-id (mail-header-references header))))
6029
6030           (when (setq header
6031                       (gnus-dependencies-add-header
6032                        header dependencies force-new))
6033             (push header headers))
6034           (goto-char (point-max))
6035           (widen))
6036         (nreverse headers)))))
6037
6038 ;; Goes through the xover lines and returns a list of vectors
6039 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6040                                                   force-new dependencies
6041                                                   group also-fetch-heads)
6042   "Parse the news overview data in the server buffer.
6043 Return a list of headers that match SEQUENCE (see
6044 `nntp-retrieve-headers')."
6045   ;; Get the Xref when the users reads the articles since most/some
6046   ;; NNTP servers do not include Xrefs when using XOVER.
6047   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6048   (let ((mail-parse-charset gnus-newsgroup-charset)
6049         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6050         (cur nntp-server-buffer)
6051         (dependencies (or dependencies gnus-newsgroup-dependencies))
6052         (allp (cond
6053                ((eq gnus-read-all-available-headers t)
6054                 t)
6055                ((stringp gnus-read-all-available-headers)
6056                 (string-match gnus-read-all-available-headers group))
6057                (t
6058                 nil)))
6059         number headers header)
6060     (save-excursion
6061       (set-buffer nntp-server-buffer)
6062       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6063       ;; Allow the user to mangle the headers before parsing them.
6064       (gnus-run-hooks 'gnus-parse-headers-hook)
6065       (goto-char (point-min))
6066       (gnus-parse-without-error
6067         (while (and (or sequence allp)
6068                     (not (eobp)))
6069           (setq number (read cur))
6070           (when (not allp)
6071             (while (and sequence
6072                         (< (car sequence) number))
6073               (setq sequence (cdr sequence))))
6074           (when (and (or allp
6075                          (and sequence
6076                               (eq number (car sequence))))
6077                      (progn
6078                        (setq sequence (cdr sequence))
6079                        (setq header (inline
6080                                       (gnus-nov-parse-line
6081                                        number dependencies force-new)))))
6082             (push header headers))
6083           (forward-line 1)))
6084       ;; A common bug in inn is that if you have posted an article and
6085       ;; then retrieves the active file, it will answer correctly --
6086       ;; the new article is included.  However, a NOV entry for the
6087       ;; article may not have been generated yet, so this may fail.
6088       ;; We work around this problem by retrieving the last few
6089       ;; headers using HEAD.
6090       (if (or (not also-fetch-heads)
6091               (not sequence))
6092           ;; We (probably) got all the headers.
6093           (nreverse headers)
6094         (let ((gnus-nov-is-evil t))
6095           (nconc
6096            (nreverse headers)
6097            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6098              (gnus-get-newsgroup-headers))))))))
6099
6100 (defun gnus-article-get-xrefs ()
6101   "Fill in the Xref value in `gnus-current-headers', if necessary.
6102 This is meant to be called in `gnus-article-internal-prepare-hook'."
6103   (let ((headers (with-current-buffer gnus-summary-buffer
6104                    gnus-current-headers)))
6105     (or (not gnus-use-cross-reference)
6106         (not headers)
6107         (and (mail-header-xref headers)
6108              (not (string= (mail-header-xref headers) "")))
6109         (let ((case-fold-search t)
6110               xref)
6111           (save-restriction
6112             (nnheader-narrow-to-headers)
6113             (goto-char (point-min))
6114             (when (or (and (not (eobp))
6115                            (eq (downcase (char-after)) ?x)
6116                            (looking-at "Xref:"))
6117                       (search-forward "\nXref:" nil t))
6118               (goto-char (1+ (match-end 0)))
6119               (setq xref (buffer-substring (point) (point-at-eol)))
6120               (mail-header-set-xref headers xref)))))))
6121
6122 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6123   "Find article ID and insert the summary line for that article.
6124 OLD-HEADER can either be a header or a line number to insert
6125 the subject line on."
6126   (let* ((line (and (numberp old-header) old-header))
6127          (old-header (and (vectorp old-header) old-header))
6128          (header (cond ((and old-header use-old-header)
6129                         old-header)
6130                        ((and (numberp id)
6131                              (gnus-number-to-header id))
6132                         (gnus-number-to-header id))
6133                        (t
6134                         (gnus-read-header id))))
6135          (number (and (numberp id) id))
6136          d)
6137     (when header
6138       ;; Rebuild the thread that this article is part of and go to the
6139       ;; article we have fetched.
6140       (when (and (not gnus-show-threads)
6141                  old-header)
6142         (when (and number
6143                    (setq d (gnus-data-find (mail-header-number old-header))))
6144           (goto-char (gnus-data-pos d))
6145           (gnus-data-remove
6146            number
6147            (- (point-at-bol)
6148               (prog1
6149                   (1+ (point-at-eol))
6150                 (gnus-delete-line))))))
6151       ;; Remove list identifiers from subject.
6152       (when gnus-list-identifiers
6153         (let ((gnus-newsgroup-headers (list header)))
6154           (gnus-summary-remove-list-identifiers)))
6155       (when old-header
6156         (mail-header-set-number header (mail-header-number old-header)))
6157       (setq gnus-newsgroup-sparse
6158             (delq (setq number (mail-header-number header))
6159                   gnus-newsgroup-sparse))
6160       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6161       (push number gnus-newsgroup-limit)
6162       (gnus-rebuild-thread (mail-header-id header) line)
6163       (gnus-summary-goto-subject number nil t))
6164     (when (and (numberp number)
6165                (> number 0))
6166       ;; We have to update the boundaries even if we can't fetch the
6167       ;; article if ID is a number -- so that the next `P' or `N'
6168       ;; command will fetch the previous (or next) article even
6169       ;; if the one we tried to fetch this time has been canceled.
6170       (when (> number gnus-newsgroup-end)
6171         (setq gnus-newsgroup-end number))
6172       (when (< number gnus-newsgroup-begin)
6173         (setq gnus-newsgroup-begin number))
6174       (setq gnus-newsgroup-unselected
6175             (delq number gnus-newsgroup-unselected)))
6176     ;; Report back a success?
6177     (and header (mail-header-number header))))
6178
6179 ;;; Process/prefix in the summary buffer
6180
6181 (defun gnus-summary-work-articles (n)
6182   "Return a list of articles to be worked upon.
6183 The prefix argument, the list of process marked articles, and the
6184 current article will be taken into consideration."
6185   (save-excursion
6186     (set-buffer gnus-summary-buffer)
6187     (cond
6188      (n
6189       ;; A numerical prefix has been given.
6190       (setq n (prefix-numeric-value n))
6191       (let ((backward (< n 0))
6192             (n (abs (prefix-numeric-value n)))
6193             articles article)
6194         (save-excursion
6195           (while
6196               (and (> n 0)
6197                    (push (setq article (gnus-summary-article-number))
6198                          articles)
6199                    (if backward
6200                        (gnus-summary-find-prev nil article)
6201                      (gnus-summary-find-next nil article)))
6202             (decf n)))
6203         (nreverse articles)))
6204      ((and (gnus-region-active-p) (mark))
6205       (message "region active")
6206       ;; Work on the region between point and mark.
6207       (let ((max (max (point) (mark)))
6208             articles article)
6209         (save-excursion
6210           (goto-char (min (min (point) (mark))))
6211           (while
6212               (and
6213                (push (setq article (gnus-summary-article-number)) articles)
6214                (gnus-summary-find-next nil article)
6215                (< (point) max)))
6216           (nreverse articles))))
6217      (gnus-newsgroup-processable
6218       ;; There are process-marked articles present.
6219       ;; Save current state.
6220       (gnus-summary-save-process-mark)
6221       ;; Return the list.
6222       (reverse gnus-newsgroup-processable))
6223      (t
6224       ;; Just return the current article.
6225       (list (gnus-summary-article-number))))))
6226
6227 (defmacro gnus-summary-iterate (arg &rest forms)
6228   "Iterate over the process/prefixed articles and do FORMS.
6229 ARG is the interactive prefix given to the command.  FORMS will be
6230 executed with point over the summary line of the articles."
6231   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6232     `(let ((,articles (gnus-summary-work-articles ,arg)))
6233        (while ,articles
6234          (gnus-summary-goto-subject (car ,articles))
6235          ,@forms
6236          (pop ,articles)))))
6237
6238 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6239 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6240
6241 (defun gnus-summary-save-process-mark ()
6242   "Push the current set of process marked articles on the stack."
6243   (interactive)
6244   (push (copy-sequence gnus-newsgroup-processable)
6245         gnus-newsgroup-process-stack))
6246
6247 (defun gnus-summary-kill-process-mark ()
6248   "Push the current set of process marked articles on the stack and unmark."
6249   (interactive)
6250   (gnus-summary-save-process-mark)
6251   (gnus-summary-unmark-all-processable))
6252
6253 (defun gnus-summary-yank-process-mark ()
6254   "Pop the last process mark state off the stack and restore it."
6255   (interactive)
6256   (unless gnus-newsgroup-process-stack
6257     (error "Empty mark stack"))
6258   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6259
6260 (defun gnus-summary-process-mark-set (set)
6261   "Make SET into the current process marked articles."
6262   (gnus-summary-unmark-all-processable)
6263   (mapc 'gnus-summary-set-process-mark set))
6264
6265 ;;; Searching and stuff
6266
6267 (defun gnus-summary-search-group (&optional backward use-level)
6268   "Search for next unread newsgroup.
6269 If optional argument BACKWARD is non-nil, search backward instead."
6270   (save-excursion
6271     (set-buffer gnus-group-buffer)
6272     (when (gnus-group-search-forward
6273            backward nil (if use-level (gnus-group-group-level) nil))
6274       (gnus-group-group-name))))
6275
6276 (defun gnus-summary-best-group (&optional exclude-group)
6277   "Find the name of the best unread group.
6278 If EXCLUDE-GROUP, do not go to this group."
6279   (with-current-buffer gnus-group-buffer
6280     (save-excursion
6281       (gnus-group-best-unread-group exclude-group))))
6282
6283 (defun gnus-summary-find-next (&optional unread article backward)
6284   (if backward
6285       (gnus-summary-find-prev unread article)
6286     (let* ((dummy (gnus-summary-article-intangible-p))
6287            (article (or article (gnus-summary-article-number)))
6288            (data (gnus-data-find-list article))
6289            result)
6290       (when (and (not dummy)
6291                  (or (not gnus-summary-check-current)
6292                      (not unread)
6293                      (not (gnus-data-unread-p (car data)))))
6294         (setq data (cdr data)))
6295       (when (setq result
6296                   (if unread
6297                       (progn
6298                         (while data
6299                           (unless (memq (gnus-data-number (car data))
6300                                         (cond
6301                                          ((eq gnus-auto-goto-ignores
6302                                               'always-undownloaded)
6303                                           gnus-newsgroup-undownloaded)
6304                                          (gnus-plugged
6305                                           nil)
6306                                          ((eq gnus-auto-goto-ignores
6307                                               'unfetched)
6308                                           gnus-newsgroup-unfetched)
6309                                          ((eq gnus-auto-goto-ignores
6310                                               'undownloaded)
6311                                           gnus-newsgroup-undownloaded)))
6312                             (when (gnus-data-unread-p (car data))
6313                               (setq result (car data)
6314                                     data nil)))
6315                           (setq data (cdr data)))
6316                         result)
6317                     (car data)))
6318         (goto-char (gnus-data-pos result))
6319         (gnus-data-number result)))))
6320
6321 (defun gnus-summary-find-prev (&optional unread article)
6322   (let* ((eobp (eobp))
6323          (article (or article (gnus-summary-article-number)))
6324          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6325          result)
6326     (when (and (not eobp)
6327                (or (not gnus-summary-check-current)
6328                    (not unread)
6329                    (not (gnus-data-unread-p (car data)))))
6330       (setq data (cdr data)))
6331     (when (setq result
6332                 (if unread
6333                     (progn
6334                       (while data
6335                         (unless (memq (gnus-data-number (car data))
6336                                       (cond
6337                                        ((eq gnus-auto-goto-ignores
6338                                             'always-undownloaded)
6339                                         gnus-newsgroup-undownloaded)
6340                                        (gnus-plugged
6341                                         nil)
6342                                        ((eq gnus-auto-goto-ignores
6343                                             'unfetched)
6344                                         gnus-newsgroup-unfetched)
6345                                        ((eq gnus-auto-goto-ignores
6346                                             'undownloaded)
6347                                         gnus-newsgroup-undownloaded)))
6348                           (when (gnus-data-unread-p (car data))
6349                             (setq result (car data)
6350                                   data nil)))
6351                         (setq data (cdr data)))
6352                       result)
6353                   (car data)))
6354       (goto-char (gnus-data-pos result))
6355       (gnus-data-number result))))
6356
6357 (defun gnus-summary-find-subject (subject &optional unread backward article)
6358   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6359          (article (or article (gnus-summary-article-number)))
6360          (articles (gnus-data-list backward))
6361          (arts (gnus-data-find-list article articles))
6362          result)
6363     (when (or (not gnus-summary-check-current)
6364               (not unread)
6365               (not (gnus-data-unread-p (car arts))))
6366       (setq arts (cdr arts)))
6367     (while arts
6368       (and (or (not unread)
6369                (gnus-data-unread-p (car arts)))
6370            (vectorp (gnus-data-header (car arts)))
6371            (gnus-subject-equal
6372             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6373            (setq result (car arts)
6374                  arts nil))
6375       (setq arts (cdr arts)))
6376     (and result
6377          (goto-char (gnus-data-pos result))
6378          (gnus-data-number result))))
6379
6380 (defun gnus-summary-search-forward (&optional unread subject backward)
6381   "Search forward for an article.
6382 If UNREAD, look for unread articles.  If SUBJECT, look for
6383 articles with that subject.  If BACKWARD, search backward instead."
6384   (cond (subject (gnus-summary-find-subject subject unread backward))
6385         (backward (gnus-summary-find-prev unread))
6386         (t (gnus-summary-find-next unread))))
6387
6388 (defun gnus-recenter (&optional n)
6389   "Center point in window and redisplay frame.
6390 Also do horizontal recentering."
6391   (interactive "P")
6392   (when (and gnus-auto-center-summary
6393              (not (eq gnus-auto-center-summary 'vertical)))
6394     (gnus-horizontal-recenter))
6395   (recenter n))
6396
6397 (defun gnus-summary-recenter ()
6398   "Center point in the summary window.
6399 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6400 displayed, no centering will be performed."
6401   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6402   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6403   (interactive)
6404   ;; The user has to want it.
6405   (when gnus-auto-center-summary
6406     (let* ((top (cond ((< (window-height) 4) 0)
6407                       ((< (window-height) 7) 1)
6408                       (t (if (numberp gnus-auto-center-summary)
6409                              gnus-auto-center-summary
6410                            (/ (1- (window-height)) 2)))))
6411            (height (1- (window-height)))
6412            (bottom (save-excursion (goto-char (point-max))
6413                                    (forward-line (- height))
6414                                    (point)))
6415            (window (get-buffer-window (current-buffer))))
6416       (when (get-buffer-window gnus-article-buffer)
6417         ;; Only do recentering when the article buffer is displayed,
6418         ;; Set the window start to either `bottom', which is the biggest
6419         ;; possible valid number, or the second line from the top,
6420         ;; whichever is the least.
6421         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6422           (if (> bottom top-pos)
6423               ;; Keep the second line from the top visible
6424               (set-window-start window top-pos t)
6425             ;; Try to keep the bottom line visible; if it's partially
6426             ;; obscured, either scroll one more line to make it fully
6427             ;; visible, or revert to using TOP-POS.
6428             (save-excursion
6429               (goto-char (point-max))
6430               (forward-line -1)
6431               (let ((last-line-start (point)))
6432                 (goto-char bottom)
6433                 (set-window-start window (point) t)
6434                 (when (not (pos-visible-in-window-p last-line-start window))
6435                   (forward-line 1)
6436                   (set-window-start window (min (point) top-pos) t)))))))
6437       ;; Do horizontal recentering while we're at it.
6438       (when (and (get-buffer-window (current-buffer) t)
6439                  (not (eq gnus-auto-center-summary 'vertical)))
6440         (let ((selected (selected-window)))
6441           (select-window (get-buffer-window (current-buffer) t))
6442           (gnus-summary-position-point)
6443           (gnus-horizontal-recenter)
6444           (select-window selected))))))
6445
6446 (defun gnus-summary-jump-to-group (newsgroup)
6447   "Move point to NEWSGROUP in group mode buffer."
6448   ;; Keep update point of group mode buffer if visible.
6449   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6450       (save-window-excursion
6451         ;; Take care of tree window mode.
6452         (when (get-buffer-window gnus-group-buffer)
6453           (pop-to-buffer gnus-group-buffer))
6454         (gnus-group-jump-to-group newsgroup))
6455     (save-excursion
6456       ;; Take care of tree window mode.
6457       (if (get-buffer-window gnus-group-buffer)
6458           (pop-to-buffer gnus-group-buffer)
6459         (set-buffer gnus-group-buffer))
6460       (gnus-group-jump-to-group newsgroup))))
6461
6462 ;; This function returns a list of article numbers based on the
6463 ;; difference between the ranges of read articles in this group and
6464 ;; the range of active articles.
6465 (defun gnus-list-of-unread-articles (group)
6466   (let* ((read (gnus-info-read (gnus-get-info group)))
6467          (active (or (gnus-active group) (gnus-activate-group group)))
6468          (last (or (cdr active)
6469                    (error "Group %s couldn't be activated " group)))
6470          first nlast unread)
6471     ;; If none are read, then all are unread.
6472     (if (not read)
6473         (setq first (car active))
6474       ;; If the range of read articles is a single range, then the
6475       ;; first unread article is the article after the last read
6476       ;; article.  Sounds logical, doesn't it?
6477       (if (and (not (listp (cdr read)))
6478                (or (< (car read) (car active))
6479                    (progn (setq read (list read))
6480                           nil)))
6481           (setq first (max (car active) (1+ (cdr read))))
6482         ;; `read' is a list of ranges.
6483         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6484                                   (caar read)))
6485                   1)
6486           (setq first (car active)))
6487         (while read
6488           (when first
6489             (while (< first nlast)
6490               (setq unread (cons first unread)
6491                     first (1+ first))))
6492           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6493           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6494           (setq read (cdr read)))))
6495     ;; And add the last unread articles.
6496     (while (<= first last)
6497       (setq unread (cons first unread)
6498             first (1+ first)))
6499     ;; Return the list of unread articles.
6500     (delq 0 (nreverse unread))))
6501
6502 (defun gnus-list-of-read-articles (group)
6503   "Return a list of unread, unticked and non-dormant articles."
6504   (let* ((info (gnus-get-info group))
6505          (marked (gnus-info-marks info))
6506          (active (gnus-active group)))
6507     (and info active
6508          (gnus-list-range-difference
6509           (gnus-list-range-difference
6510            (gnus-sorted-complement
6511             (gnus-uncompress-range active)
6512             (gnus-list-of-unread-articles group))
6513            (cdr (assq 'dormant marked)))
6514           (cdr (assq 'tick marked))))))
6515
6516 ;; This function returns a sequence of article numbers based on the
6517 ;; difference between the ranges of read articles in this group and
6518 ;; the range of active articles.
6519 (defun gnus-sequence-of-unread-articles (group)
6520   (let* ((read (gnus-info-read (gnus-get-info group)))
6521          (active (or (gnus-active group) (gnus-activate-group group)))
6522          (last (cdr active))
6523          first nlast unread)
6524     ;; If none are read, then all are unread.
6525     (if (not read)
6526         (setq first (car active))
6527       ;; If the range of read articles is a single range, then the
6528       ;; first unread article is the article after the last read
6529       ;; article.  Sounds logical, doesn't it?
6530       (if (and (not (listp (cdr read)))
6531                (or (< (car read) (car active))
6532                    (progn (setq read (list read))
6533                           nil)))
6534           (setq first (max (car active) (1+ (cdr read))))
6535         ;; `read' is a list of ranges.
6536         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6537                                   (caar read)))
6538                   1)
6539           (setq first (car active)))
6540         (while read
6541           (when first
6542             (push (cons first nlast) unread))
6543           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6544           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6545           (setq read (cdr read)))))
6546     ;; And add the last unread articles.
6547     (cond ((< first last)
6548            (push (cons first last) unread))
6549           ((= first last)
6550            (push first unread)))
6551     ;; Return the sequence of unread articles.
6552     (delq 0 (nreverse unread))))
6553
6554 ;; Various summary commands
6555
6556 (defun gnus-summary-select-article-buffer ()
6557   "Reconfigure windows to show article buffer."
6558   (interactive)
6559   (if (not (gnus-buffer-live-p gnus-article-buffer))
6560       (error "There is no article buffer for this summary buffer")
6561     (gnus-configure-windows 'article)
6562     (select-window (get-buffer-window gnus-article-buffer))))
6563
6564 (defun gnus-summary-universal-argument (arg)
6565   "Perform any operation on all articles that are process/prefixed."
6566   (interactive "P")
6567   (let ((articles (gnus-summary-work-articles arg))
6568         func article)
6569     (if (eq
6570          (setq
6571           func
6572           (key-binding
6573            (read-key-sequence
6574             (substitute-command-keys
6575              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6576          'undefined)
6577         (gnus-error 1 "Undefined key")
6578       (save-excursion
6579         (while articles
6580           (gnus-summary-goto-subject (setq article (pop articles)))
6581           (let (gnus-newsgroup-processable)
6582             (command-execute func))
6583           (gnus-summary-remove-process-mark article)))))
6584   (gnus-summary-position-point))
6585
6586 (defun gnus-summary-toggle-truncation (&optional arg)
6587   "Toggle truncation of summary lines.
6588 With ARG, turn line truncation on if ARG is positive."
6589   (interactive "P")
6590   (setq truncate-lines
6591         (if (null arg) (not truncate-lines)
6592           (> (prefix-numeric-value arg) 0)))
6593   (redraw-display))
6594
6595 (defun gnus-summary-find-for-reselect ()
6596   "Return the number of an article to stay on across a reselect.
6597 The current article is considered, then following articles, then previous
6598 articles.  An article is sought which is not cancelled and isn't a temporary
6599 insertion from another group.  If there's no such then return a dummy 0."
6600   (let (found)
6601     (dolist (rev '(nil t))
6602       (unless found      ; don't demand the reverse list if we don't need it
6603         (let ((data (gnus-data-find-list
6604                      (gnus-summary-article-number) (gnus-data-list rev))))
6605           (while (and data (not found))
6606             (if (and (< 0 (gnus-data-number (car data)))
6607                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6608                 (setq found (gnus-data-number (car data))))
6609             (setq data (cdr data))))))
6610     (or found 0)))
6611
6612 (defun gnus-summary-reselect-current-group (&optional all rescan)
6613   "Exit and then reselect the current newsgroup.
6614 The prefix argument ALL means to select all articles."
6615   (interactive "P")
6616   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6617     (error "Ephemeral groups can't be reselected"))
6618   (let ((current-subject (gnus-summary-find-for-reselect))
6619         (group gnus-newsgroup-name))
6620     (setq gnus-newsgroup-begin nil)
6621     (gnus-summary-exit nil 'leave-hidden)
6622     ;; We have to adjust the point of group mode buffer because
6623     ;; point was moved to the next unread newsgroup by exiting.
6624     (gnus-summary-jump-to-group group)
6625     (when rescan
6626       (save-excursion
6627         (gnus-group-get-new-news-this-group 1)))
6628     (gnus-group-read-group all t)
6629     (gnus-summary-goto-subject current-subject nil t)))
6630
6631 (defun gnus-summary-rescan-group (&optional all)
6632   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6633   (interactive "P")
6634   (gnus-summary-reselect-current-group all t))
6635
6636 (defun gnus-summary-update-info (&optional non-destructive)
6637   (save-excursion
6638     (let ((group gnus-newsgroup-name))
6639       (when group
6640         (when gnus-newsgroup-kill-headers
6641           (setq gnus-newsgroup-killed
6642                 (gnus-compress-sequence
6643                  (gnus-sorted-union
6644                   (gnus-list-range-intersection
6645                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6646                   gnus-newsgroup-unreads)
6647                  t)))
6648         (unless (listp (cdr gnus-newsgroup-killed))
6649           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6650         (let ((headers gnus-newsgroup-headers))
6651           ;; Set the new ranges of read articles.
6652           (with-current-buffer gnus-group-buffer
6653             (gnus-undo-force-boundary))
6654           (gnus-update-read-articles
6655            group (gnus-sorted-union
6656                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6657           ;; Set the current article marks.
6658           (let ((gnus-newsgroup-scored
6659                  (if (and (not gnus-save-score)
6660                           (not non-destructive))
6661                      nil
6662                    gnus-newsgroup-scored)))
6663             (save-excursion
6664               (gnus-update-marks)))
6665           ;; Do the cross-ref thing.
6666           (when gnus-use-cross-reference
6667             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6668           ;; Do not switch windows but change the buffer to work.
6669           (set-buffer gnus-group-buffer)
6670           (unless (gnus-ephemeral-group-p group)
6671             (gnus-group-update-group group)))))))
6672
6673 (defun gnus-summary-save-newsrc (&optional force)
6674   "Save the current number of read/marked articles in the dribble buffer.
6675 The dribble buffer will then be saved.
6676 If FORCE (the prefix), also save the .newsrc file(s)."
6677   (interactive "P")
6678   (gnus-summary-update-info t)
6679   (if force
6680       (gnus-save-newsrc-file)
6681     (gnus-dribble-save)))
6682
6683 (defun gnus-summary-exit (&optional temporary leave-hidden)
6684   "Exit reading current newsgroup, and then return to group selection mode.
6685 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6686   (interactive)
6687   (gnus-set-global-variables)
6688   (when (gnus-buffer-live-p gnus-article-buffer)
6689     (save-excursion
6690       (set-buffer gnus-article-buffer)
6691       (mm-destroy-parts gnus-article-mime-handles)
6692       ;; Set it to nil for safety reason.
6693       (setq gnus-article-mime-handle-alist nil)
6694       (setq gnus-article-mime-handles nil)))
6695   (gnus-kill-save-kill-buffer)
6696   (gnus-async-halt-prefetch)
6697   (let* ((group gnus-newsgroup-name)
6698          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6699          (gnus-group-is-exiting-p t)
6700          (mode major-mode)
6701          (group-point nil)
6702          (buf (current-buffer)))
6703     (unless quit-config
6704       ;; Do adaptive scoring, and possibly save score files.
6705       (when gnus-newsgroup-adaptive
6706         (gnus-score-adaptive))
6707       (when gnus-use-scoring
6708         (gnus-score-save)))
6709     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6710     ;; If we have several article buffers, we kill them at exit.
6711     (unless gnus-single-article-buffer
6712       (gnus-kill-buffer gnus-original-article-buffer)
6713       (setq gnus-article-current nil))
6714     (when gnus-use-cache
6715       (gnus-cache-possibly-remove-articles)
6716       (gnus-cache-save-buffers))
6717     (gnus-async-prefetch-remove-group group)
6718     (when gnus-suppress-duplicates
6719       (gnus-dup-enter-articles))
6720     (when gnus-use-trees
6721       (gnus-tree-close group))
6722     (when gnus-use-cache
6723       (gnus-cache-write-active))
6724     ;; Remove entries for this group.
6725     (nnmail-purge-split-history (gnus-group-real-name group))
6726     ;; Make all changes in this group permanent.
6727     (unless quit-config
6728       (gnus-run-hooks 'gnus-exit-group-hook)
6729       (gnus-summary-update-info))
6730     (gnus-close-group group)
6731     ;; Make sure where we were, and go to next newsgroup.
6732     (set-buffer gnus-group-buffer)
6733     (unless quit-config
6734       (gnus-group-jump-to-group group))
6735     (gnus-run-hooks 'gnus-summary-exit-hook)
6736     (unless (or quit-config
6737                 ;; If this group has disappeared from the summary
6738                 ;; buffer, don't skip forwards.
6739                 (not (string= group (gnus-group-group-name))))
6740       (gnus-group-next-unread-group 1))
6741     (setq group-point (point))
6742     (if temporary
6743         nil                             ;Nothing to do.
6744       ;; If we have several article buffers, we kill them at exit.
6745       (unless gnus-single-article-buffer
6746         (gnus-kill-buffer gnus-article-buffer)
6747         (gnus-kill-buffer gnus-original-article-buffer)
6748         (setq gnus-article-current nil))
6749       (set-buffer buf)
6750       (if (not gnus-kill-summary-on-exit)
6751           (progn
6752             (gnus-deaden-summary)
6753             (setq mode nil))
6754         ;; We set all buffer-local variables to nil.  It is unclear why
6755         ;; this is needed, but if we don't, buffer-local variables are
6756         ;; not garbage-collected, it seems.  This would the lead to en
6757         ;; ever-growing Emacs.
6758         (gnus-summary-clear-local-variables)
6759         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6760           (gnus-summary-clear-local-variables))
6761         (when (get-buffer gnus-article-buffer)
6762           (bury-buffer gnus-article-buffer))
6763         ;; Return to group mode buffer.
6764         (when (eq mode 'gnus-summary-mode)
6765           (gnus-kill-buffer buf)))
6766       (setq gnus-current-select-method gnus-select-method)
6767       (set-buffer gnus-group-buffer)
6768       (if quit-config
6769           (gnus-handle-ephemeral-exit quit-config)
6770         (goto-char group-point)
6771         ;; If gnus-group-buffer is already displayed, make sure we also move
6772         ;; the cursor in the window that displays it.
6773         (let ((win (get-buffer-window (current-buffer) 0)))
6774           (if win (set-window-point win (point))))
6775         (unless leave-hidden
6776           (gnus-configure-windows 'group 'force)))
6777       ;; Clear the current group name.
6778       (unless quit-config
6779         (setq gnus-newsgroup-name nil)))))
6780
6781 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6782 (defun gnus-summary-exit-no-update (&optional no-questions)
6783   "Quit reading current newsgroup without updating read article info."
6784   (interactive)
6785   (let* ((group gnus-newsgroup-name)
6786          (gnus-group-is-exiting-p t)
6787          (gnus-group-is-exiting-without-update-p t)
6788          (quit-config (gnus-group-quit-config group)))
6789     (when (or no-questions
6790               gnus-expert-user
6791               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6792       (gnus-async-halt-prefetch)
6793       (run-hooks 'gnus-summary-prepare-exit-hook)
6794       (when (gnus-buffer-live-p gnus-article-buffer)
6795         (save-excursion
6796           (set-buffer gnus-article-buffer)
6797           (mm-destroy-parts gnus-article-mime-handles)
6798           ;; Set it to nil for safety reason.
6799           (setq gnus-article-mime-handle-alist nil)
6800           (setq gnus-article-mime-handles nil)))
6801       ;; If we have several article buffers, we kill them at exit.
6802       (unless gnus-single-article-buffer
6803         (gnus-kill-buffer gnus-article-buffer)
6804         (gnus-kill-buffer gnus-original-article-buffer)
6805         (setq gnus-article-current nil))
6806       (if (not gnus-kill-summary-on-exit)
6807           (gnus-deaden-summary)
6808         (gnus-close-group group)
6809         (gnus-summary-clear-local-variables)
6810         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6811           (gnus-summary-clear-local-variables))
6812         (gnus-kill-buffer gnus-summary-buffer))
6813       (unless gnus-single-article-buffer
6814         (setq gnus-article-current nil))
6815       (when gnus-use-trees
6816         (gnus-tree-close group))
6817       (gnus-async-prefetch-remove-group group)
6818       (when (get-buffer gnus-article-buffer)
6819         (bury-buffer gnus-article-buffer))
6820       ;; Return to the group buffer.
6821       (gnus-configure-windows 'group 'force)
6822       ;; Clear the current group name.
6823       (setq gnus-newsgroup-name nil)
6824       (unless (gnus-ephemeral-group-p group)
6825         (gnus-group-update-group group))
6826       (when (equal (gnus-group-group-name) group)
6827         (gnus-group-next-unread-group 1))
6828       (when quit-config
6829         (gnus-handle-ephemeral-exit quit-config)))))
6830
6831 (defun gnus-handle-ephemeral-exit (quit-config)
6832   "Handle movement when leaving an ephemeral group.
6833 The state which existed when entering the ephemeral is reset."
6834   (if (not (buffer-name (car quit-config)))
6835       (gnus-configure-windows 'group 'force)
6836     (set-buffer (car quit-config))
6837     (cond ((eq major-mode 'gnus-summary-mode)
6838            (gnus-set-global-variables))
6839           ((eq major-mode 'gnus-article-mode)
6840            (save-excursion
6841              ;; The `gnus-summary-buffer' variable may point
6842              ;; to the old summary buffer when using a single
6843              ;; article buffer.
6844              (unless (gnus-buffer-live-p gnus-summary-buffer)
6845                (set-buffer gnus-group-buffer))
6846              (set-buffer gnus-summary-buffer)
6847              (gnus-set-global-variables))))
6848     (if (or (eq (cdr quit-config) 'article)
6849             (eq (cdr quit-config) 'pick))
6850         (progn
6851           ;; The current article may be from the ephemeral group
6852           ;; thus it is best that we reload this article
6853           ;;
6854           ;; If we're exiting from a large digest, this can be
6855           ;; extremely slow.  So, it's better not to reload it. -- jh.
6856           ;;(gnus-summary-show-article)
6857           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6858               (gnus-configure-windows 'pick 'force)
6859             (gnus-configure-windows (cdr quit-config) 'force)))
6860       (gnus-configure-windows (cdr quit-config) 'force))
6861     (when (eq major-mode 'gnus-summary-mode)
6862       (gnus-summary-next-subject 1 nil t)
6863       (gnus-summary-recenter)
6864       (gnus-summary-position-point))))
6865
6866 ;;; Dead summaries.
6867
6868 (defvar gnus-dead-summary-mode-map nil)
6869
6870 (unless gnus-dead-summary-mode-map
6871   (setq gnus-dead-summary-mode-map (make-keymap))
6872   (suppress-keymap gnus-dead-summary-mode-map)
6873   (substitute-key-definition
6874    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6875   (dolist (key '("\C-d" "\r" "\177" [delete]))
6876     (define-key gnus-dead-summary-mode-map
6877       key 'gnus-summary-wake-up-the-dead))
6878   (dolist (key '("q" "Q"))
6879     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6880
6881 (defvar gnus-dead-summary-mode nil
6882   "Minor mode for Gnus summary buffers.")
6883
6884 (defun gnus-dead-summary-mode (&optional arg)
6885   "Minor mode for Gnus summary buffers."
6886   (interactive "P")
6887   (when (eq major-mode 'gnus-summary-mode)
6888     (make-local-variable 'gnus-dead-summary-mode)
6889     (setq gnus-dead-summary-mode
6890           (if (null arg) (not gnus-dead-summary-mode)
6891             (> (prefix-numeric-value arg) 0)))
6892     (when gnus-dead-summary-mode
6893       (add-minor-mode
6894        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6895
6896 (defun gnus-deaden-summary ()
6897   "Make the current summary buffer into a dead summary buffer."
6898   ;; Kill any previous dead summary buffer.
6899   (when (and gnus-dead-summary
6900              (buffer-name gnus-dead-summary))
6901     (with-current-buffer gnus-dead-summary
6902       (when gnus-dead-summary-mode
6903         (kill-buffer (current-buffer)))))
6904   ;; Make this the current dead summary.
6905   (setq gnus-dead-summary (current-buffer))
6906   (gnus-dead-summary-mode 1)
6907   (let ((name (buffer-name)))
6908     (when (string-match "Summary" name)
6909       (rename-buffer
6910        (concat (substring name 0 (match-beginning 0)) "Dead "
6911                (substring name (match-beginning 0)))
6912        t)
6913       (bury-buffer))))
6914
6915 (defun gnus-kill-or-deaden-summary (buffer)
6916   "Kill or deaden the summary BUFFER."
6917   (save-excursion
6918     (when (and (buffer-name buffer)
6919                (not gnus-single-article-buffer))
6920       (with-current-buffer buffer
6921         (gnus-kill-buffer gnus-article-buffer)
6922         (gnus-kill-buffer gnus-original-article-buffer)))
6923     (cond
6924      ;; Kill the buffer.
6925      (gnus-kill-summary-on-exit
6926       (when (and gnus-use-trees
6927                  (gnus-buffer-exists-p buffer))
6928         (save-excursion
6929           (set-buffer buffer)
6930           (gnus-tree-close gnus-newsgroup-name)))
6931       (gnus-kill-buffer buffer))
6932      ;; Deaden the buffer.
6933      ((gnus-buffer-exists-p buffer)
6934       (save-excursion
6935         (set-buffer buffer)
6936         (gnus-deaden-summary))))))
6937
6938 (defun gnus-summary-wake-up-the-dead (&rest args)
6939   "Wake up the dead summary buffer."
6940   (interactive)
6941   (gnus-dead-summary-mode -1)
6942   (let ((name (buffer-name)))
6943     (when (string-match "Dead " name)
6944       (rename-buffer
6945        (concat (substring name 0 (match-beginning 0))
6946                (substring name (match-end 0)))
6947        t)))
6948   (gnus-message 3 "This dead summary is now alive again"))
6949
6950 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6951 (defun gnus-summary-fetch-faq (&optional faq-dir)
6952   "Fetch the FAQ for the current group.
6953 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6954 in."
6955   (interactive
6956    (list
6957     (when current-prefix-arg
6958       (completing-read
6959        "FAQ dir: " (and (listp gnus-group-faq-directory)
6960                         (mapcar 'list
6961                                 gnus-group-faq-directory))))))
6962   (let (gnus-faq-buffer)
6963     (when (setq gnus-faq-buffer
6964                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6965       (gnus-configure-windows 'summary-faq))))
6966
6967 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6968 (defun gnus-summary-describe-group (&optional force)
6969   "Describe the current newsgroup."
6970   (interactive "P")
6971   (gnus-group-describe-group force gnus-newsgroup-name))
6972
6973 (defun gnus-summary-describe-briefly ()
6974   "Describe summary mode commands briefly."
6975   (interactive)
6976   (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")))
6977
6978 ;; Walking around group mode buffer from summary mode.
6979
6980 (defun gnus-summary-next-group (&optional no-article target-group backward)
6981   "Exit current newsgroup and then select next unread newsgroup.
6982 If prefix argument NO-ARTICLE is non-nil, no article is selected
6983 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6984 previous group instead."
6985   (interactive "P")
6986   ;; Stop pre-fetching.
6987   (gnus-async-halt-prefetch)
6988   (let ((current-group gnus-newsgroup-name)
6989         (current-buffer (current-buffer))
6990         entered)
6991     ;; First we semi-exit this group to update Xrefs and all variables.
6992     ;; We can't do a real exit, because the window conf must remain
6993     ;; the same in case the user is prompted for info, and we don't
6994     ;; want the window conf to change before that...
6995     (gnus-summary-exit t)
6996     (while (not entered)
6997       ;; Then we find what group we are supposed to enter.
6998       (set-buffer gnus-group-buffer)
6999       (gnus-group-jump-to-group current-group)
7000       (setq target-group
7001             (or target-group
7002                 (if (eq gnus-keep-same-level 'best)
7003                     (gnus-summary-best-group gnus-newsgroup-name)
7004                   (gnus-summary-search-group backward gnus-keep-same-level))))
7005       (if (not target-group)
7006           ;; There are no further groups, so we return to the group
7007           ;; buffer.
7008           (progn
7009             (gnus-message 5 "Returning to the group buffer")
7010             (setq entered t)
7011             (when (gnus-buffer-live-p current-buffer)
7012               (set-buffer current-buffer)
7013               (gnus-summary-exit))
7014             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7015         ;; We try to enter the target group.
7016         (gnus-group-jump-to-group target-group)
7017         (let ((unreads (gnus-group-group-unread)))
7018           (if (and (or (eq t unreads)
7019                        (and unreads (not (zerop unreads))))
7020                    (gnus-summary-read-group
7021                     target-group nil no-article
7022                     (and (buffer-name current-buffer) current-buffer)
7023                     nil backward))
7024               (setq entered t)
7025             (setq current-group target-group
7026                   target-group nil)))))))
7027
7028 (defun gnus-summary-prev-group (&optional no-article)
7029   "Exit current newsgroup and then select previous unread newsgroup.
7030 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7031   (interactive "P")
7032   (gnus-summary-next-group no-article nil t))
7033
7034 ;; Walking around summary lines.
7035
7036 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7037   "Go to the first subject satisfying any non-nil constraint.
7038 If UNREAD is non-nil, the article should be unread.
7039 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7040 If UNSEEN is non-nil, the article should be unseen.
7041 Returns the article selected or nil if there are no matching articles."
7042   (interactive "P")
7043   (cond
7044    ;; Empty summary.
7045    ((null gnus-newsgroup-data)
7046     (gnus-message 3 "No articles in the group")
7047     nil)
7048    ;; Pick the first article.
7049    ((not (or unread undownloaded unseen))
7050     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7051     (gnus-data-number (car gnus-newsgroup-data)))
7052    ;; Find the first unread article.
7053    (t
7054     (let ((data gnus-newsgroup-data))
7055       (while (and data
7056                   (let ((num (gnus-data-number (car data))))
7057                     (or (memq num gnus-newsgroup-unfetched)
7058                         (not (or (and unread
7059                                       (memq num gnus-newsgroup-unreads))
7060                                  (and undownloaded
7061                                       (memq num gnus-newsgroup-undownloaded))
7062                                  (and unseen
7063                                       (memq num gnus-newsgroup-unseen)))))))
7064         (setq data (cdr data)))
7065       (prog1
7066           (if data
7067               (progn
7068                 (goto-char (gnus-data-pos (car data)))
7069                 (gnus-data-number (car data)))
7070             (gnus-message 3 "No more%s articles"
7071                           (let* ((r (when unread " unread"))
7072                                  (d (when undownloaded " undownloaded"))
7073                                  (s (when unseen " unseen"))
7074                                  (l (delq nil (list r d s))))
7075                             (cond ((= 3 (length l))
7076                                    (concat r "," d ", or" s))
7077                                   ((= 2 (length l))
7078                                    (concat (car l) ", or" (cadr l)))
7079                                   ((= 1 (length l))
7080                                    (car l))
7081                                   (t
7082                                    ""))))
7083             nil
7084             )
7085         (gnus-summary-position-point))))))
7086
7087 (defun gnus-summary-next-subject (n &optional unread dont-display)
7088   "Go to next N'th summary line.
7089 If N is negative, go to the previous N'th subject line.
7090 If UNREAD is non-nil, only unread articles are selected.
7091 The difference between N and the actual number of steps taken is
7092 returned."
7093   (interactive "p")
7094   (let ((backward (< n 0))
7095         (n (abs n)))
7096     (while (and (> n 0)
7097                 (if backward
7098                     (gnus-summary-find-prev unread)
7099                   (gnus-summary-find-next unread)))
7100       (unless (zerop (setq n (1- n)))
7101         (gnus-summary-show-thread)))
7102     (when (/= 0 n)
7103       (gnus-message 7 "No more%s articles"
7104                     (if unread " unread" "")))
7105     (unless dont-display
7106       (gnus-summary-recenter)
7107       (gnus-summary-position-point))
7108     n))
7109
7110 (defun gnus-summary-next-unread-subject (n)
7111   "Go to next N'th unread summary line."
7112   (interactive "p")
7113   (gnus-summary-next-subject n t))
7114
7115 (defun gnus-summary-prev-subject (n &optional unread)
7116   "Go to previous N'th summary line.
7117 If optional argument UNREAD is non-nil, only unread article is selected."
7118   (interactive "p")
7119   (gnus-summary-next-subject (- n) unread))
7120
7121 (defun gnus-summary-prev-unread-subject (n)
7122   "Go to previous N'th unread summary line."
7123   (interactive "p")
7124   (gnus-summary-next-subject (- n) t))
7125
7126 (defun gnus-summary-goto-subjects (articles)
7127   "Insert the subject header for ARTICLES in the current buffer."
7128   (save-excursion
7129     (dolist (article articles)
7130       (gnus-summary-goto-subject article t)))
7131   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7132   (gnus-summary-position-point))
7133
7134 (defun gnus-summary-goto-subject (article &optional force silent)
7135   "Go the subject line of ARTICLE.
7136 If FORCE, also allow jumping to articles not currently shown."
7137   (interactive "nArticle number: ")
7138   (unless (numberp article)
7139     (error "Article %s is not a number" article))
7140   (let ((b (point))
7141         (data (gnus-data-find article)))
7142     ;; We read in the article if we have to.
7143     (and (not data)
7144          force
7145          (gnus-summary-insert-subject
7146           article
7147           (if (or (numberp force) (vectorp force)) force)
7148           t)
7149          (setq data (gnus-data-find article)))
7150     (goto-char b)
7151     (if (not data)
7152         (progn
7153           (unless silent
7154             (gnus-message 3 "Can't find article %d" article))
7155           nil)
7156       (let ((pt (gnus-data-pos data)))
7157         (goto-char pt)
7158         (gnus-summary-set-article-display-arrow pt))
7159       (gnus-summary-position-point)
7160       article)))
7161
7162 ;; Walking around summary lines with displaying articles.
7163
7164 (defun gnus-summary-expand-window (&optional arg)
7165   "Make the summary buffer take up the entire Emacs frame.
7166 Given a prefix, will force an `article' buffer configuration."
7167   (interactive "P")
7168   (if arg
7169       (gnus-configure-windows 'article 'force)
7170     (gnus-configure-windows 'summary 'force)))
7171
7172 (defun gnus-summary-display-article (article &optional all-header)
7173   "Display ARTICLE in article buffer."
7174   (when (gnus-buffer-live-p gnus-article-buffer)
7175     (with-current-buffer gnus-article-buffer
7176       (mm-enable-multibyte)))
7177   (gnus-set-global-variables)
7178   (when (gnus-buffer-live-p gnus-article-buffer)
7179     (with-current-buffer gnus-article-buffer
7180       (setq gnus-article-charset gnus-newsgroup-charset)
7181       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7182       (mm-enable-multibyte)))
7183   (if (null article)
7184       nil
7185     (prog1
7186         (if gnus-summary-display-article-function
7187             (funcall gnus-summary-display-article-function article all-header)
7188           (gnus-article-prepare article all-header))
7189       (gnus-run-hooks 'gnus-select-article-hook)
7190       (when (and gnus-current-article
7191                  (not (zerop gnus-current-article)))
7192         (gnus-summary-goto-subject gnus-current-article))
7193       (gnus-summary-recenter)
7194       (when (and gnus-use-trees gnus-show-threads)
7195         (gnus-possibly-generate-tree article)
7196         (gnus-highlight-selected-tree article))
7197       ;; Successfully display article.
7198       (gnus-article-set-window-start
7199        (cdr (assq article gnus-newsgroup-bookmarks))))))
7200
7201 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7202   "Select the current article.
7203 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7204 non-nil, the article will be re-fetched even if it already present in
7205 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7206 be displayed."
7207   ;; Make sure we are in the summary buffer to work around bbdb bug.
7208   (unless (eq major-mode 'gnus-summary-mode)
7209     (set-buffer gnus-summary-buffer))
7210   (let ((article (or article (gnus-summary-article-number)))
7211         (all-headers (not (not all-headers))) ;Must be t or nil.
7212         gnus-summary-display-article-function)
7213     (and (not pseudo)
7214          (gnus-summary-article-pseudo-p article)
7215          (error "This is a pseudo-article"))
7216     (save-excursion
7217       (set-buffer gnus-summary-buffer)
7218       (if (or (and gnus-single-article-buffer
7219                    (or (null gnus-current-article)
7220                        (null gnus-article-current)
7221                        (null (get-buffer gnus-article-buffer))
7222                        (not (eq article (cdr gnus-article-current)))
7223                        (not (equal (car gnus-article-current)
7224                                    gnus-newsgroup-name))))
7225               (and (not gnus-single-article-buffer)
7226                    (or (null gnus-current-article)
7227                        (not (eq gnus-current-article article))))
7228               force)
7229           ;; The requested article is different from the current article.
7230           (progn
7231             (gnus-summary-display-article article all-headers)
7232             (when (gnus-buffer-live-p gnus-article-buffer)
7233               (with-current-buffer gnus-article-buffer
7234                 (if (not gnus-article-decoded-p) ;; a local variable
7235                     (mm-disable-multibyte))))
7236             (gnus-article-set-window-start
7237              (cdr (assq article gnus-newsgroup-bookmarks)))
7238             article)
7239         'old))))
7240
7241 (defun gnus-summary-force-verify-and-decrypt ()
7242   "Display buttons for signed/encrypted parts and verify/decrypt them."
7243   (interactive)
7244   (let ((mm-verify-option 'known)
7245         (mm-decrypt-option 'known)
7246         (gnus-article-emulate-mime t)
7247         (gnus-buttonized-mime-types (append (list "multipart/signed"
7248                                                   "multipart/encrypted")
7249                                             gnus-buttonized-mime-types)))
7250     (gnus-summary-select-article nil 'force)))
7251
7252 (defun gnus-summary-set-current-mark (&optional current-mark)
7253   "Obsolete function."
7254   nil)
7255
7256 (defun gnus-summary-next-article (&optional unread subject backward push)
7257   "Select the next article.
7258 If UNREAD, only unread articles are selected.
7259 If SUBJECT, only articles with SUBJECT are selected.
7260 If BACKWARD, the previous article is selected instead of the next."
7261   (interactive "P")
7262   (cond
7263    ;; Is there such an article?
7264    ((and (gnus-summary-search-forward unread subject backward)
7265          (or (gnus-summary-display-article (gnus-summary-article-number))
7266              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7267     (gnus-summary-position-point))
7268    ;; If not, we try the first unread, if that is wanted.
7269    ((and subject
7270          gnus-auto-select-same
7271          (gnus-summary-first-unread-article))
7272     (gnus-summary-position-point)
7273     (gnus-message 6 "Wrapped"))
7274    ;; Try to get next/previous article not displayed in this group.
7275    ((and gnus-auto-extend-newsgroup
7276          (not unread) (not subject))
7277     (gnus-summary-goto-article
7278      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7279      nil (count-lines (point-min) (point))))
7280    ;; Go to next/previous group.
7281    (t
7282     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7283       (gnus-summary-jump-to-group gnus-newsgroup-name))
7284     (let ((cmd last-command-char)
7285           (point
7286            (with-current-buffer gnus-group-buffer
7287              (point)))
7288           (group
7289            (if (eq gnus-keep-same-level 'best)
7290                (gnus-summary-best-group gnus-newsgroup-name)
7291              (gnus-summary-search-group backward gnus-keep-same-level))))
7292       ;; For some reason, the group window gets selected.  We change
7293       ;; it back.
7294       (select-window (get-buffer-window (current-buffer)))
7295       ;; Select next unread newsgroup automagically.
7296       (cond
7297        ((or (not gnus-auto-select-next)
7298             (not cmd))
7299         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7300        ((or (eq gnus-auto-select-next 'quietly)
7301             (and (eq gnus-auto-select-next 'slightly-quietly)
7302                  push)
7303             (and (eq gnus-auto-select-next 'almost-quietly)
7304                  (gnus-summary-last-article-p)))
7305         ;; Select quietly.
7306         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7307             (gnus-summary-exit)
7308           (gnus-message 7 "No more%s articles (%s)..."
7309                         (if unread " unread" "")
7310                         (if group (concat "selecting " group)
7311                           "exiting"))
7312           (gnus-summary-next-group nil group backward)))
7313        (t
7314         (when (gnus-key-press-event-p last-input-event)
7315           (gnus-summary-walk-group-buffer
7316            gnus-newsgroup-name cmd unread backward point))))))))
7317
7318 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7319   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7320                       (?\C-p (gnus-group-prev-unread-group 1))))
7321         (cursor-in-echo-area t)
7322         keve key group ended prompt)
7323     (save-excursion
7324       (set-buffer gnus-group-buffer)
7325       (goto-char start)
7326       (setq group
7327             (if (eq gnus-keep-same-level 'best)
7328                 (gnus-summary-best-group gnus-newsgroup-name)
7329               (gnus-summary-search-group backward gnus-keep-same-level))))
7330     (while (not ended)
7331       (setq prompt
7332             (format
7333              "No more%s articles%s " (if unread " unread" "")
7334              (if (and group
7335                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7336                  (format " (Type %s for %s [%s])"
7337                          (single-key-description cmd)
7338                          (gnus-group-decoded-name group)
7339                          (gnus-group-unread group))
7340                (format " (Type %s to exit %s)"
7341                        (single-key-description cmd)
7342                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7343       ;; Confirm auto selection.
7344       (setq key (car (setq keve (gnus-read-event-char prompt)))
7345             ended t)
7346       (cond
7347        ((assq key keystrokes)
7348         (let ((obuf (current-buffer)))
7349           (switch-to-buffer gnus-group-buffer)
7350           (when group
7351             (gnus-group-jump-to-group group))
7352           (eval (cadr (assq key keystrokes)))
7353           (setq group (gnus-group-group-name))
7354           (switch-to-buffer obuf))
7355         (setq ended nil))
7356        ((equal key cmd)
7357         (if (or (not group)
7358                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7359             (gnus-summary-exit)
7360           (gnus-summary-next-group nil group backward)))
7361        (t
7362         (push (cdr keve) unread-command-events))))))
7363
7364 (defun gnus-summary-next-unread-article ()
7365   "Select unread article after current one."
7366   (interactive)
7367   (gnus-summary-next-article
7368    (or (not (eq gnus-summary-goto-unread 'never))
7369        (gnus-summary-last-article-p (gnus-summary-article-number)))
7370    (and gnus-auto-select-same
7371         (gnus-summary-article-subject))))
7372
7373 (defun gnus-summary-prev-article (&optional unread subject)
7374   "Select the article after the current one.
7375 If UNREAD is non-nil, only unread articles are selected."
7376   (interactive "P")
7377   (gnus-summary-next-article unread subject t))
7378
7379 (defun gnus-summary-prev-unread-article ()
7380   "Select unread article before current one."
7381   (interactive)
7382   (gnus-summary-prev-article
7383    (or (not (eq gnus-summary-goto-unread 'never))
7384        (gnus-summary-first-article-p (gnus-summary-article-number)))
7385    (and gnus-auto-select-same
7386         (gnus-summary-article-subject))))
7387
7388 (defun gnus-summary-next-page (&optional lines circular stop)
7389   "Show next page of the selected article.
7390 If at the end of the current article, select the next article.
7391 LINES says how many lines should be scrolled up.
7392
7393 If CIRCULAR is non-nil, go to the start of the article instead of
7394 selecting the next article when reaching the end of the current
7395 article.
7396
7397 If STOP is non-nil, just stop when reaching the end of the message.
7398
7399 Also see the variable `gnus-article-skip-boring'."
7400   (interactive "P")
7401   (setq gnus-summary-buffer (current-buffer))
7402   (gnus-set-global-variables)
7403   (let ((article (gnus-summary-article-number))
7404         (article-window (get-buffer-window gnus-article-buffer t))
7405         endp)
7406     ;; If the buffer is empty, we have no article.
7407     (unless article
7408       (error "No article to select"))
7409     (gnus-configure-windows 'article)
7410     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7411         (if (and (eq gnus-summary-goto-unread 'never)
7412                  (not (gnus-summary-last-article-p article)))
7413             (gnus-summary-next-article)
7414           (gnus-summary-next-unread-article))
7415       (if (or (null gnus-current-article)
7416               (null gnus-article-current)
7417               (/= article (cdr gnus-article-current))
7418               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7419           ;; Selected subject is different from current article's.
7420           (gnus-summary-display-article article)
7421         (when article-window
7422           (gnus-eval-in-buffer-window gnus-article-buffer
7423             (setq endp (or (gnus-article-next-page lines)
7424                            (gnus-article-only-boring-p))))
7425           (when endp
7426             (cond (stop
7427                    (gnus-message 3 "End of message"))
7428                   (circular
7429                    (gnus-summary-beginning-of-article))
7430                   (lines
7431                    (gnus-message 3 "End of message"))
7432                   ((null lines)
7433                    (if (and (eq gnus-summary-goto-unread 'never)
7434                             (not (gnus-summary-last-article-p article)))
7435                        (gnus-summary-next-article)
7436                      (gnus-summary-next-unread-article))))))))
7437     (gnus-summary-recenter)
7438     (gnus-summary-position-point)))
7439
7440 (defun gnus-summary-prev-page (&optional lines move)
7441   "Show previous page of selected article.
7442 Argument LINES specifies lines to be scrolled down.
7443 If MOVE, move to the previous unread article if point is at
7444 the beginning of the buffer."
7445   (interactive "P")
7446   (let ((article (gnus-summary-article-number))
7447         (article-window (get-buffer-window gnus-article-buffer t))
7448         endp)
7449     (gnus-configure-windows 'article)
7450     (if (or (null gnus-current-article)
7451             (null gnus-article-current)
7452             (/= article (cdr gnus-article-current))
7453             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7454         ;; Selected subject is different from current article's.
7455         (gnus-summary-display-article article)
7456       (gnus-summary-recenter)
7457       (when article-window
7458         (gnus-eval-in-buffer-window gnus-article-buffer
7459           (setq endp (gnus-article-prev-page lines)))
7460         (when (and move endp)
7461           (cond (lines
7462                  (gnus-message 3 "Beginning of message"))
7463                 ((null lines)
7464                  (if (and (eq gnus-summary-goto-unread 'never)
7465                           (not (gnus-summary-first-article-p article)))
7466                      (gnus-summary-prev-article)
7467                    (gnus-summary-prev-unread-article))))))))
7468   (gnus-summary-position-point))
7469
7470 (defun gnus-summary-prev-page-or-article (&optional lines)
7471   "Show previous page of selected article.
7472 Argument LINES specifies lines to be scrolled down.
7473 If at the beginning of the article, go to the next article."
7474   (interactive "P")
7475   (gnus-summary-prev-page lines t))
7476
7477 (defun gnus-summary-scroll-up (lines)
7478   "Scroll up (or down) one line current article.
7479 Argument LINES specifies lines to be scrolled up (or down if negative)."
7480   (interactive "p")
7481   (gnus-configure-windows 'article)
7482   (gnus-summary-show-thread)
7483   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7484     (gnus-eval-in-buffer-window gnus-article-buffer
7485       (cond ((> lines 0)
7486              (when (gnus-article-next-page lines)
7487                (gnus-message 3 "End of message")))
7488             ((< lines 0)
7489              (gnus-article-prev-page (- lines))))))
7490   (gnus-summary-recenter)
7491   (gnus-summary-position-point))
7492
7493 (defun gnus-summary-scroll-down (lines)
7494   "Scroll down (or up) one line current article.
7495 Argument LINES specifies lines to be scrolled down (or up if negative)."
7496   (interactive "p")
7497   (gnus-summary-scroll-up (- lines)))
7498
7499 (defun gnus-summary-next-same-subject ()
7500   "Select next article which has the same subject as current one."
7501   (interactive)
7502   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7503
7504 (defun gnus-summary-prev-same-subject ()
7505   "Select previous article which has the same subject as current one."
7506   (interactive)
7507   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7508
7509 (defun gnus-summary-next-unread-same-subject ()
7510   "Select next unread article which has the same subject as current one."
7511   (interactive)
7512   (gnus-summary-next-article t (gnus-summary-article-subject)))
7513
7514 (defun gnus-summary-prev-unread-same-subject ()
7515   "Select previous unread article which has the same subject as current one."
7516   (interactive)
7517   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7518
7519 (defun gnus-summary-first-unread-article ()
7520   "Select the first unread article.
7521 Return nil if there are no unread articles."
7522   (interactive)
7523   (prog1
7524       (when (gnus-summary-first-subject t)
7525         (gnus-summary-show-thread)
7526         (gnus-summary-first-subject t)
7527         (gnus-summary-display-article (gnus-summary-article-number)))
7528     (gnus-summary-position-point)))
7529
7530 (defun gnus-summary-first-unread-subject ()
7531   "Place the point on the subject line of the first unread article.
7532 Return nil if there are no unread articles."
7533   (interactive)
7534   (prog1
7535       (when (gnus-summary-first-subject t)
7536         (gnus-summary-show-thread)
7537         (gnus-summary-first-subject t))
7538     (gnus-summary-position-point)))
7539
7540 (defun gnus-summary-first-unseen-subject ()
7541   "Place the point on the subject line of the first unseen article.
7542 Return nil if there are no unseen articles."
7543   (interactive)
7544   (prog1
7545       (when (gnus-summary-first-subject nil nil t)
7546         (gnus-summary-show-thread)
7547         (gnus-summary-first-subject nil nil t))
7548     (gnus-summary-position-point)))
7549
7550 (defun gnus-summary-first-unseen-or-unread-subject ()
7551   "Place the point on the subject line of the first unseen article or,
7552 if all article have been seen, on the subject line of the first unread
7553 article."
7554   (interactive)
7555   (prog1
7556       (unless (when (gnus-summary-first-subject nil nil t)
7557                 (gnus-summary-show-thread)
7558                 (gnus-summary-first-subject nil nil t))
7559         (when (gnus-summary-first-subject t)
7560           (gnus-summary-show-thread)
7561           (gnus-summary-first-subject t)))
7562     (gnus-summary-position-point)))
7563
7564 (defun gnus-summary-first-article ()
7565   "Select the first article.
7566 Return nil if there are no articles."
7567   (interactive)
7568   (prog1
7569       (when (gnus-summary-first-subject)
7570         (gnus-summary-show-thread)
7571         (gnus-summary-first-subject)
7572         (gnus-summary-display-article (gnus-summary-article-number)))
7573     (gnus-summary-position-point)))
7574
7575 (defun gnus-summary-best-unread-article (&optional arg)
7576   "Select the unread article with the highest score.
7577 If given a prefix argument, select the next unread article that has a
7578 score higher than the default score."
7579   (interactive "P")
7580   (let ((article (if arg
7581                      (gnus-summary-better-unread-subject)
7582                    (gnus-summary-best-unread-subject))))
7583     (if article
7584         (gnus-summary-goto-article article)
7585       (error "No unread articles"))))
7586
7587 (defun gnus-summary-best-unread-subject ()
7588   "Select the unread subject with the highest score."
7589   (interactive)
7590   (let ((best -1000000)
7591         (data gnus-newsgroup-data)
7592         article score)
7593     (while data
7594       (and (gnus-data-unread-p (car data))
7595            (> (setq score
7596                     (gnus-summary-article-score (gnus-data-number (car data))))
7597               best)
7598            (setq best score
7599                  article (gnus-data-number (car data))))
7600       (setq data (cdr data)))
7601     (when article
7602       (gnus-summary-goto-subject article))
7603     (gnus-summary-position-point)
7604     article))
7605
7606 (defun gnus-summary-better-unread-subject ()
7607   "Select the first unread subject that has a score over the default score."
7608   (interactive)
7609   (let ((data gnus-newsgroup-data)
7610         article score)
7611     (while (and (setq article (gnus-data-number (car data)))
7612                 (or (gnus-data-read-p (car data))
7613                     (not (> (gnus-summary-article-score article)
7614                             gnus-summary-default-score))))
7615       (setq data (cdr data)))
7616     (when article
7617       (gnus-summary-goto-subject article))
7618     (gnus-summary-position-point)
7619     article))
7620
7621 (defun gnus-summary-last-subject ()
7622   "Go to the last displayed subject line in the group."
7623   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7624     (when article
7625       (gnus-summary-goto-subject article))))
7626
7627 (defun gnus-summary-goto-article (article &optional all-headers force)
7628   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7629 If ALL-HEADERS is non-nil, no header lines are hidden.
7630 If FORCE, go to the article even if it isn't displayed.  If FORCE
7631 is a number, it is the line the article is to be displayed on."
7632   (interactive
7633    (list
7634     (completing-read
7635      "Article number or Message-ID: "
7636      (mapcar (lambda (number) (list (int-to-string number)))
7637              gnus-newsgroup-limit))
7638     current-prefix-arg
7639     t))
7640   (prog1
7641       (if (and (stringp article)
7642                (string-match "@\\|%40" article))
7643           (gnus-summary-refer-article article)
7644         (when (stringp article)
7645           (setq article (string-to-number article)))
7646         (if (gnus-summary-goto-subject article force)
7647             (gnus-summary-display-article article all-headers)
7648           (gnus-message 4 "Couldn't go to article %s" article) nil))
7649     (gnus-summary-position-point)))
7650
7651 (defun gnus-summary-goto-last-article ()
7652   "Go to the previously read article."
7653   (interactive)
7654   (prog1
7655       (when gnus-last-article
7656         (gnus-summary-goto-article gnus-last-article nil t))
7657     (gnus-summary-position-point)))
7658
7659 (defun gnus-summary-pop-article (number)
7660   "Pop one article off the history and go to the previous.
7661 NUMBER articles will be popped off."
7662   (interactive "p")
7663   (let (to)
7664     (setq gnus-newsgroup-history
7665           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7666     (if to
7667         (gnus-summary-goto-article (car to) nil t)
7668       (error "Article history empty")))
7669   (gnus-summary-position-point))
7670
7671 ;; Summary commands and functions for limiting the summary buffer.
7672
7673 (defun gnus-summary-limit-to-articles (n)
7674   "Limit the summary buffer to the next N articles.
7675 If not given a prefix, use the process marked articles instead."
7676   (interactive "P")
7677   (prog1
7678       (let ((articles (gnus-summary-work-articles n)))
7679         (setq gnus-newsgroup-processable nil)
7680         (gnus-summary-limit articles))
7681     (gnus-summary-position-point)))
7682
7683 (defun gnus-summary-pop-limit (&optional total)
7684   "Restore the previous limit.
7685 If given a prefix, remove all limits."
7686   (interactive "P")
7687   (when total
7688     (setq gnus-newsgroup-limits
7689           (list (mapcar (lambda (h) (mail-header-number h))
7690                         gnus-newsgroup-headers))))
7691   (unless gnus-newsgroup-limits
7692     (error "No limit to pop"))
7693   (prog1
7694       (gnus-summary-limit nil 'pop)
7695     (gnus-summary-position-point)))
7696
7697 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7698   "Limit the summary buffer to articles that have subjects that match a regexp.
7699 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7700   (interactive
7701    (list (read-string (if current-prefix-arg
7702                           "Exclude subject (regexp): "
7703                         "Limit to subject (regexp): "))
7704          nil current-prefix-arg))
7705   (unless header
7706     (setq header "subject"))
7707   (when (not (equal "" subject))
7708     (prog1
7709         (let ((articles (gnus-summary-find-matching
7710                          (or header "subject") subject 'all nil nil
7711                          not-matching)))
7712           (unless articles
7713             (error "Found no matches for \"%s\"" subject))
7714           (gnus-summary-limit articles))
7715       (gnus-summary-position-point))))
7716
7717 (defun gnus-summary-limit-to-author (from &optional not-matching)
7718   "Limit the summary buffer to articles that have authors that match a regexp.
7719 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7720   (interactive
7721    (list (read-string (if current-prefix-arg
7722                           "Exclude author (regexp): "
7723                         "Limit to author (regexp): "))
7724          current-prefix-arg))
7725   (gnus-summary-limit-to-subject from "from" not-matching))
7726
7727 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7728   "Limit the summary buffer to articles with the given RECIPIENT.
7729
7730 If NOT-MATCHING, exclude RECIPIENT.
7731
7732 To and Cc headers are checked.  You need to include them in
7733 `nnmail-extra-headers'."
7734   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7735   (interactive
7736    (list (read-string (format "%s recipient (regexp): "
7737                               (if current-prefix-arg "Exclude" "Limit to")))
7738          current-prefix-arg))
7739   (when (not (equal "" recipient))
7740     (prog1 (let* ((to
7741                    (if (memq 'To nnmail-extra-headers)
7742                        (gnus-summary-find-matching
7743                         (cons 'extra 'To) recipient 'all nil nil
7744                         not-matching)
7745                      (gnus-message
7746                       1 "`To' isn't present in `nnmail-extra-headers'")
7747                      (sit-for 1)
7748                      nil))
7749                   (cc
7750                    (if (memq 'Cc nnmail-extra-headers)
7751                        (gnus-summary-find-matching
7752                         (cons 'extra 'Cc) recipient 'all nil nil
7753                         not-matching)
7754                      (gnus-message
7755                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7756                      (sit-for 1)
7757                      nil))
7758                   (articles
7759                    (if not-matching
7760                        ;; We need the numbers that are in both lists:
7761                        (mapcar (lambda (a)
7762                                  (and (memq a to) a))
7763                                cc)
7764                      (nconc to cc))))
7765              (unless articles
7766                (error "Found no matches for \"%s\"" recipient))
7767              (gnus-summary-limit articles))
7768       (gnus-summary-position-point))))
7769
7770 (defun gnus-summary-limit-to-age (age &optional younger-p)
7771   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7772 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7773 articles that are younger than AGE days."
7774   (interactive
7775    (let ((younger current-prefix-arg)
7776          (days-got nil)
7777          days)
7778      (while (not days-got)
7779        (setq days (if younger
7780                       (read-string "Limit to articles younger than (in days, older when negative): ")
7781                     (read-string
7782                      "Limit to articles older than (in days, younger when negative): ")))
7783        (when (> (length days) 0)
7784          (setq days (read days)))
7785        (if (numberp days)
7786            (progn
7787              (setq days-got t)
7788              (when (< days 0)
7789                (setq younger (not younger))
7790                (setq days (* days -1))))
7791          (message "Please enter a number.")
7792          (sleep-for 1)))
7793      (list days younger)))
7794   (prog1
7795       (let ((data gnus-newsgroup-data)
7796             (cutoff (days-to-time age))
7797             articles d date is-younger)
7798         (while (setq d (pop data))
7799           (when (and (vectorp (gnus-data-header d))
7800                      (setq date (mail-header-date (gnus-data-header d))))
7801             (setq is-younger (time-less-p
7802                               (time-since (condition-case ()
7803                                               (date-to-time date)
7804                                             (error '(0 0))))
7805                               cutoff))
7806             (when (if younger-p
7807                       is-younger
7808                     (not is-younger))
7809               (push (gnus-data-number d) articles))))
7810         (gnus-summary-limit (nreverse articles)))
7811     (gnus-summary-position-point)))
7812
7813 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7814   "Limit the summary buffer to articles that match an 'extra' header."
7815   (interactive
7816    (let ((header
7817           (intern
7818            (gnus-completing-read-with-default
7819             (symbol-name (car gnus-extra-headers))
7820             (if current-prefix-arg
7821                 "Exclude extra header"
7822               "Limit extra header")
7823             (mapcar (lambda (x)
7824                       (cons (symbol-name x) x))
7825                     gnus-extra-headers)
7826             nil
7827             t))))
7828      (list header
7829            (read-string (format "%s header %s (regexp): "
7830                                 (if current-prefix-arg "Exclude" "Limit to")
7831                                 header))
7832            current-prefix-arg)))
7833   (when (not (equal "" regexp))
7834     (prog1
7835         (let ((articles (gnus-summary-find-matching
7836                          (cons 'extra header) regexp 'all nil nil
7837                          not-matching)))
7838           (unless articles
7839             (error "Found no matches for \"%s\"" regexp))
7840           (gnus-summary-limit articles))
7841       (gnus-summary-position-point))))
7842
7843 (defun gnus-summary-limit-to-display-predicate ()
7844   "Limit the summary buffer to the predicated in the `display' group parameter."
7845   (interactive)
7846   (unless gnus-newsgroup-display
7847     (error "There is no `display' group parameter"))
7848   (let (articles)
7849     (dolist (number gnus-newsgroup-articles)
7850       (when (funcall gnus-newsgroup-display)
7851         (push number articles)))
7852     (gnus-summary-limit articles))
7853   (gnus-summary-position-point))
7854
7855 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7856 (make-obsolete
7857  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7858
7859 (defun gnus-summary-limit-to-unread (&optional all)
7860   "Limit the summary buffer to articles that are not marked as read.
7861 If ALL is non-nil, limit strictly to unread articles."
7862   (interactive "P")
7863   (if all
7864       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7865     (gnus-summary-limit-to-marks
7866      ;; Concat all the marks that say that an article is read and have
7867      ;; those removed.
7868      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7869            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7870            gnus-low-score-mark gnus-expirable-mark
7871            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7872            gnus-duplicate-mark gnus-souped-mark)
7873      'reverse)))
7874
7875 (defun gnus-summary-limit-to-replied (&optional unreplied)
7876   "Limit the summary buffer to replied articles.
7877 If UNREPLIED (the prefix), limit to unreplied articles."
7878   (interactive "P")
7879   (if unreplied
7880       (gnus-summary-limit
7881        (gnus-set-difference gnus-newsgroup-articles
7882         gnus-newsgroup-replied))
7883     (gnus-summary-limit gnus-newsgroup-replied))
7884   (gnus-summary-position-point))
7885
7886 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7887 (make-obsolete 'gnus-summary-delete-marked-with
7888                'gnus-summary-limit-exclude-marks)
7889
7890 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7891   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7892 If REVERSE, limit the summary buffer to articles that are marked
7893 with MARKS.  MARKS can either be a string of marks or a list of marks.
7894 Returns how many articles were removed."
7895   (interactive "sMarks: ")
7896   (gnus-summary-limit-to-marks marks t))
7897
7898 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7899   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7900 If REVERSE (the prefix), limit the summary buffer to articles that are
7901 not marked with MARKS.  MARKS can either be a string of marks or a
7902 list of marks.
7903 Returns how many articles were removed."
7904   (interactive "sMarks: \nP")
7905   (prog1
7906       (let ((data gnus-newsgroup-data)
7907             (marks (if (listp marks) marks
7908                      (append marks nil))) ; Transform to list.
7909             articles)
7910         (while data
7911           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7912                   (memq (gnus-data-mark (car data)) marks))
7913             (push (gnus-data-number (car data)) articles))
7914           (setq data (cdr data)))
7915         (gnus-summary-limit articles))
7916     (gnus-summary-position-point)))
7917
7918 (defun gnus-summary-limit-to-score (score)
7919   "Limit to articles with score at or above SCORE."
7920   (interactive "NLimit to articles with score of at least: ")
7921   (let ((data gnus-newsgroup-data)
7922         articles)
7923     (while data
7924       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7925                 score)
7926         (push (gnus-data-number (car data)) articles))
7927       (setq data (cdr data)))
7928     (prog1
7929         (gnus-summary-limit articles)
7930       (gnus-summary-position-point))))
7931
7932 (defun gnus-summary-limit-to-unseen ()
7933   "Limit to unseen articles."
7934   (interactive)
7935   (prog1
7936       (gnus-summary-limit gnus-newsgroup-unseen)
7937     (gnus-summary-position-point)))
7938
7939 (defun gnus-summary-limit-include-thread (id)
7940   "Display all the hidden articles that is in the thread with ID in it.
7941 When called interactively, ID is the Message-ID of the current
7942 article."
7943   (interactive (list (mail-header-id (gnus-summary-article-header))))
7944   (let ((articles (gnus-articles-in-thread
7945                    (gnus-id-to-thread (gnus-root-id id)))))
7946     (prog1
7947         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7948       (gnus-summary-limit-include-matching-articles
7949        "subject"
7950        (regexp-quote (gnus-simplify-subject-re
7951                       (mail-header-subject (gnus-id-to-header id)))))
7952       (gnus-summary-position-point))))
7953
7954 (defun gnus-summary-limit-include-matching-articles (header regexp)
7955   "Display all the hidden articles that have HEADERs that match REGEXP."
7956   (interactive (list (read-string "Match on header: ")
7957                      (read-string "Regexp: ")))
7958   (let ((articles (gnus-find-matching-articles header regexp)))
7959     (prog1
7960         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7961       (gnus-summary-position-point))))
7962
7963 (defun gnus-summary-insert-dormant-articles ()
7964   "Insert all the dormant articles for this group into the current buffer."
7965   (interactive)
7966   (let ((gnus-verbose (max 6 gnus-verbose)))
7967     (if (not gnus-newsgroup-dormant)
7968         (gnus-message 3 "No cached articles for this group")
7969       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7970
7971 (defun gnus-summary-limit-include-dormant ()
7972   "Display all the hidden articles that are marked as dormant.
7973 Note that this command only works on a subset of the articles currently
7974 fetched for this group."
7975   (interactive)
7976   (unless gnus-newsgroup-dormant
7977     (error "There are no dormant articles in this group"))
7978   (prog1
7979       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7980     (gnus-summary-position-point)))
7981
7982 (defun gnus-summary-limit-exclude-dormant ()
7983   "Hide all dormant articles."
7984   (interactive)
7985   (prog1
7986       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7987     (gnus-summary-position-point)))
7988
7989 (defun gnus-summary-limit-exclude-childless-dormant ()
7990   "Hide all dormant articles that have no children."
7991   (interactive)
7992   (let ((data (gnus-data-list t))
7993         articles d children)
7994     ;; Find all articles that are either not dormant or have
7995     ;; children.
7996     (while (setq d (pop data))
7997       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7998                 (and (setq children
7999                            (gnus-article-children (gnus-data-number d)))
8000                      (let (found)
8001                        (while children
8002                          (when (memq (car children) articles)
8003                            (setq children nil
8004                                  found t))
8005                          (pop children))
8006                        found)))
8007         (push (gnus-data-number d) articles)))
8008     ;; Do the limiting.
8009     (prog1
8010         (gnus-summary-limit articles)
8011       (gnus-summary-position-point))))
8012
8013 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8014   "Mark all unread excluded articles as read.
8015 If ALL, mark even excluded ticked and dormants as read."
8016   (interactive "P")
8017   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8018   (let ((articles (gnus-sorted-ndifference
8019                    (sort
8020                     (mapcar (lambda (h) (mail-header-number h))
8021                             gnus-newsgroup-headers)
8022                     '<)
8023                    gnus-newsgroup-limit))
8024         article)
8025     (setq gnus-newsgroup-unreads
8026           (gnus-sorted-intersection gnus-newsgroup-unreads
8027                                     gnus-newsgroup-limit))
8028     (if all
8029         (setq gnus-newsgroup-dormant nil
8030               gnus-newsgroup-marked nil
8031               gnus-newsgroup-reads
8032               (nconc
8033                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8034                gnus-newsgroup-reads))
8035       (while (setq article (pop articles))
8036         (unless (or (memq article gnus-newsgroup-dormant)
8037                     (memq article gnus-newsgroup-marked))
8038           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8039
8040 (defun gnus-summary-limit (articles &optional pop)
8041   (if pop
8042       ;; We pop the previous limit off the stack and use that.
8043       (setq articles (car gnus-newsgroup-limits)
8044             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8045     ;; We use the new limit, so we push the old limit on the stack.
8046     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8047   ;; Set the limit.
8048   (setq gnus-newsgroup-limit articles)
8049   (let ((total (length gnus-newsgroup-data))
8050         (data (gnus-data-find-list (gnus-summary-article-number)))
8051         (gnus-summary-mark-below nil)   ; Inhibit this.
8052         found)
8053     ;; This will do all the work of generating the new summary buffer
8054     ;; according to the new limit.
8055     (gnus-summary-prepare)
8056     ;; Hide any threads, possibly.
8057     (gnus-summary-maybe-hide-threads)
8058     ;; Try to return to the article you were at, or one in the
8059     ;; neighborhood.
8060     (when data
8061       ;; We try to find some article after the current one.
8062       (while data
8063         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8064           (setq data nil
8065                 found t))
8066         (setq data (cdr data))))
8067     (unless found
8068       ;; If there is no data, that means that we were after the last
8069       ;; article.  The same goes when we can't find any articles
8070       ;; after the current one.
8071       (goto-char (point-max))
8072       (gnus-summary-find-prev))
8073     (gnus-set-mode-line 'summary)
8074     ;; We return how many articles were removed from the summary
8075     ;; buffer as a result of the new limit.
8076     (- total (length gnus-newsgroup-data))))
8077
8078 (defsubst gnus-invisible-cut-children (threads)
8079   (let ((num 0))
8080     (while threads
8081       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8082         (incf num))
8083       (pop threads))
8084     (< num 2)))
8085
8086 (defsubst gnus-cut-thread (thread)
8087   "Go forwards in the thread until we find an article that we want to display."
8088   (when (or (eq gnus-fetch-old-headers 'some)
8089             (eq gnus-fetch-old-headers 'invisible)
8090             (numberp gnus-fetch-old-headers)
8091             (eq gnus-build-sparse-threads 'some)
8092             (eq gnus-build-sparse-threads 'more))
8093     ;; Deal with old-fetched headers and sparse threads.
8094     (while (and
8095             thread
8096             (or
8097              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8098              (gnus-summary-article-ancient-p
8099               (mail-header-number (car thread))))
8100             (if (or (<= (length (cdr thread)) 1)
8101                     (eq gnus-fetch-old-headers 'invisible))
8102                 (setq gnus-newsgroup-limit
8103                       (delq (mail-header-number (car thread))
8104                             gnus-newsgroup-limit)
8105                       thread (cadr thread))
8106               (when (gnus-invisible-cut-children (cdr thread))
8107                 (let ((th (cdr thread)))
8108                   (while th
8109                     (if (memq (mail-header-number (caar th))
8110                               gnus-newsgroup-limit)
8111                         (setq thread (car th)
8112                               th nil)
8113                       (setq th (cdr th))))))))))
8114   thread)
8115
8116 (defun gnus-cut-threads (threads)
8117   "Cut off all uninteresting articles from the beginning of THREADS."
8118   (when (or (eq gnus-fetch-old-headers 'some)
8119             (eq gnus-fetch-old-headers 'invisible)
8120             (numberp gnus-fetch-old-headers)
8121             (eq gnus-build-sparse-threads 'some)
8122             (eq gnus-build-sparse-threads 'more))
8123     (let ((th threads))
8124       (while th
8125         (setcar th (gnus-cut-thread (car th)))
8126         (setq th (cdr th)))))
8127   ;; Remove nixed out threads.
8128   (delq nil threads))
8129
8130 (defun gnus-summary-initial-limit (&optional show-if-empty)
8131   "Figure out what the initial limit is supposed to be on group entry.
8132 This entails weeding out unwanted dormants, low-scored articles,
8133 fetch-old-headers verbiage, and so on."
8134   ;; Most groups have nothing to remove.
8135   (if (or gnus-inhibit-limiting
8136           (and (null gnus-newsgroup-dormant)
8137                (eq gnus-newsgroup-display 'gnus-not-ignore)
8138                (not (eq gnus-fetch-old-headers 'some))
8139                (not (numberp gnus-fetch-old-headers))
8140                (not (eq gnus-fetch-old-headers 'invisible))
8141                (null gnus-summary-expunge-below)
8142                (not (eq gnus-build-sparse-threads 'some))
8143                (not (eq gnus-build-sparse-threads 'more))
8144                (null gnus-thread-expunge-below)
8145                (not gnus-use-nocem)))
8146       ()                                ; Do nothing.
8147     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8148     (setq gnus-newsgroup-limit nil)
8149     (mapatoms
8150      (lambda (node)
8151        (unless (car (symbol-value node))
8152          ;; These threads have no parents -- they are roots.
8153          (let ((nodes (cdr (symbol-value node)))
8154                thread)
8155            (while nodes
8156              (if (and gnus-thread-expunge-below
8157                       (< (gnus-thread-total-score (car nodes))
8158                          gnus-thread-expunge-below))
8159                  (gnus-expunge-thread (pop nodes))
8160                (setq thread (pop nodes))
8161                (gnus-summary-limit-children thread))))))
8162      gnus-newsgroup-dependencies)
8163     ;; If this limitation resulted in an empty group, we might
8164     ;; pop the previous limit and use it instead.
8165     (when (and (not gnus-newsgroup-limit)
8166                show-if-empty)
8167       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8168     gnus-newsgroup-limit))
8169
8170 (defun gnus-summary-limit-children (thread)
8171   "Return 1 if this subthread is visible and 0 if it is not."
8172   ;; First we get the number of visible children to this thread.  This
8173   ;; is done by recursing down the thread using this function, so this
8174   ;; will really go down to a leaf article first, before slowly
8175   ;; working its way up towards the root.
8176   (when thread
8177     (let* ((max-lisp-eval-depth 5000)
8178            (children
8179            (if (cdr thread)
8180                (apply '+ (mapcar 'gnus-summary-limit-children
8181                                  (cdr thread)))
8182              0))
8183           (number (mail-header-number (car thread)))
8184           score)
8185       (if (and
8186            (not (memq number gnus-newsgroup-marked))
8187            (or
8188             ;; If this article is dormant and has absolutely no visible
8189             ;; children, then this article isn't visible.
8190             (and (memq number gnus-newsgroup-dormant)
8191                  (zerop children))
8192             ;; If this is "fetch-old-headered" and there is no
8193             ;; visible children, then we don't want this article.
8194             (and (or (eq gnus-fetch-old-headers 'some)
8195                      (numberp gnus-fetch-old-headers))
8196                  (gnus-summary-article-ancient-p number)
8197                  (zerop children))
8198             ;; If this is "fetch-old-headered" and `invisible', then
8199             ;; we don't want this article.
8200             (and (eq gnus-fetch-old-headers 'invisible)
8201                  (gnus-summary-article-ancient-p number))
8202             ;; If this is a sparsely inserted article with no children,
8203             ;; we don't want it.
8204             (and (eq gnus-build-sparse-threads 'some)
8205                  (gnus-summary-article-sparse-p number)
8206                  (zerop children))
8207             ;; If we use expunging, and this article is really
8208             ;; low-scored, then we don't want this article.
8209             (when (and gnus-summary-expunge-below
8210                        (< (setq score
8211                                 (or (cdr (assq number gnus-newsgroup-scored))
8212                                     gnus-summary-default-score))
8213                           gnus-summary-expunge-below))
8214               ;; We increase the expunge-tally here, but that has
8215               ;; nothing to do with the limits, really.
8216               (incf gnus-newsgroup-expunged-tally)
8217               ;; We also mark as read here, if that's wanted.
8218               (when (and gnus-summary-mark-below
8219                          (< score gnus-summary-mark-below))
8220                 (setq gnus-newsgroup-unreads
8221                       (delq number gnus-newsgroup-unreads))
8222                 (if gnus-newsgroup-auto-expire
8223                     (push number gnus-newsgroup-expirable)
8224                   (push (cons number gnus-low-score-mark)
8225                         gnus-newsgroup-reads)))
8226               t)
8227             ;; Do the `display' group parameter.
8228             (and gnus-newsgroup-display
8229                  (not (funcall gnus-newsgroup-display)))
8230             ;; Check NoCeM things.
8231             (when (and gnus-use-nocem
8232                        (gnus-nocem-unwanted-article-p
8233                         (mail-header-id (car thread))))
8234               (setq gnus-newsgroup-unreads
8235                     (delq number gnus-newsgroup-unreads))
8236               t)))
8237           ;; Nope, invisible article.
8238           0
8239         ;; Ok, this article is to be visible, so we add it to the limit
8240         ;; and return 1.
8241         (push number gnus-newsgroup-limit)
8242         1))))
8243
8244 (defun gnus-expunge-thread (thread)
8245   "Mark all articles in THREAD as read."
8246   (let* ((number (mail-header-number (car thread))))
8247     (incf gnus-newsgroup-expunged-tally)
8248     ;; We also mark as read here, if that's wanted.
8249     (setq gnus-newsgroup-unreads
8250           (delq number gnus-newsgroup-unreads))
8251     (if gnus-newsgroup-auto-expire
8252         (push number gnus-newsgroup-expirable)
8253       (push (cons number gnus-low-score-mark)
8254             gnus-newsgroup-reads)))
8255   ;; Go recursively through all subthreads.
8256   (mapcar 'gnus-expunge-thread (cdr thread)))
8257
8258 ;; Summary article oriented commands
8259
8260 (defun gnus-summary-refer-parent-article (n)
8261   "Refer parent article N times.
8262 If N is negative, go to ancestor -N instead.
8263 The difference between N and the number of articles fetched is returned."
8264   (interactive "p")
8265   (let ((skip 1)
8266         error header ref)
8267     (when (not (natnump n))
8268       (setq skip (abs n)
8269             n 1))
8270     (while (and (> n 0)
8271                 (not error))
8272       (setq header (gnus-summary-article-header))
8273       (if (and (eq (mail-header-number header)
8274                    (cdr gnus-article-current))
8275                (equal gnus-newsgroup-name
8276                       (car gnus-article-current)))
8277           ;; If we try to find the parent of the currently
8278           ;; displayed article, then we take a look at the actual
8279           ;; References header, since this is slightly more
8280           ;; reliable than the References field we got from the
8281           ;; server.
8282           (save-excursion
8283             (set-buffer gnus-original-article-buffer)
8284             (nnheader-narrow-to-headers)
8285             (unless (setq ref (message-fetch-field "references"))
8286               (when (setq ref (message-fetch-field "in-reply-to"))
8287                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8288             (widen))
8289         (setq ref
8290               ;; It's not the current article, so we take a bet on
8291               ;; the value we got from the server.
8292               (mail-header-references header)))
8293       (if (and ref
8294                (not (equal ref "")))
8295           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8296             (gnus-message 1 "Couldn't find parent"))
8297         (gnus-message 1 "No references in article %d"
8298                       (gnus-summary-article-number))
8299         (setq error t))
8300       (decf n))
8301     (gnus-summary-position-point)
8302     n))
8303
8304 (defun gnus-summary-refer-references ()
8305   "Fetch all articles mentioned in the References header.
8306 Return the number of articles fetched."
8307   (interactive)
8308   (let ((ref (mail-header-references (gnus-summary-article-header)))
8309         (current (gnus-summary-article-number))
8310         (n 0))
8311     (if (or (not ref)
8312             (equal ref ""))
8313         (error "No References in the current article")
8314       ;; For each Message-ID in the References header...
8315       (while (string-match "<[^>]*>" ref)
8316         (incf n)
8317         ;; ... fetch that article.
8318         (gnus-summary-refer-article
8319          (prog1 (match-string 0 ref)
8320            (setq ref (substring ref (match-end 0))))))
8321       (gnus-summary-goto-subject current)
8322       (gnus-summary-position-point)
8323       n)))
8324
8325 (defun gnus-summary-refer-thread (&optional limit)
8326   "Fetch all articles in the current thread.
8327 If LIMIT (the numerical prefix), fetch that many old headers instead
8328 of what's specified by the `gnus-refer-thread-limit' variable."
8329   (interactive "P")
8330   (let ((id (mail-header-id (gnus-summary-article-header)))
8331         (limit (if limit (prefix-numeric-value limit)
8332                  gnus-refer-thread-limit)))
8333     (unless (eq gnus-fetch-old-headers 'invisible)
8334       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8335       ;; Retrieve the headers and read them in.
8336       (if (eq (if (numberp limit)
8337                   (gnus-retrieve-headers
8338                    (list (min
8339                           (+ (mail-header-number
8340                               (gnus-summary-article-header))
8341                              limit)
8342                           gnus-newsgroup-end))
8343                    gnus-newsgroup-name (* limit 2))
8344                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8345                 ;; headers.
8346                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8347                                        gnus-newsgroup-name limit))
8348               'nov)
8349           (gnus-build-all-threads)
8350         (error "Can't fetch thread from back ends that don't support NOV"))
8351       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8352     (gnus-summary-limit-include-thread id)))
8353
8354 (defun gnus-summary-refer-article (message-id)
8355   "Fetch an article specified by MESSAGE-ID."
8356   (interactive "sMessage-ID: ")
8357   (when (and (stringp message-id)
8358              (not (zerop (length message-id))))
8359     (setq message-id (gnus-replace-in-string message-id " " ""))
8360     ;; Construct the correct Message-ID if necessary.
8361     ;; Suggested by tale@pawl.rpi.edu.
8362     (unless (string-match "^<" message-id)
8363       (setq message-id (concat "<" message-id)))
8364     (unless (string-match ">$" message-id)
8365       (setq message-id (concat message-id ">")))
8366     ;; People often post MIDs from URLs, so unhex it:
8367     (unless (string-match "@" message-id)
8368       (setq message-id (gnus-url-unhex-string message-id)))
8369     (let* ((header (gnus-id-to-header message-id))
8370            (sparse (and header
8371                         (gnus-summary-article-sparse-p
8372                          (mail-header-number header))
8373                         (memq (mail-header-number header)
8374                               gnus-newsgroup-limit)))
8375            number)
8376       (cond
8377        ;; If the article is present in the buffer we just go to it.
8378        ((and header
8379              (or (not (gnus-summary-article-sparse-p
8380                        (mail-header-number header)))
8381                  sparse))
8382         (prog1
8383             (gnus-summary-goto-article
8384              (mail-header-number header) nil t)
8385           (when sparse
8386             (gnus-summary-update-article (mail-header-number header)))))
8387        (t
8388         ;; We fetch the article.
8389         (catch 'found
8390           (dolist (gnus-override-method (gnus-refer-article-methods))
8391             (when (and (gnus-check-server gnus-override-method)
8392                        ;; Fetch the header,
8393                        (setq number (gnus-summary-insert-subject message-id)))
8394               ;; and display the article.
8395               (gnus-summary-select-article nil nil nil number)
8396               (throw 'found t)))
8397           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8398
8399 (defun gnus-refer-article-methods ()
8400   "Return a list of referable methods."
8401   (cond
8402    ;; No method, so we default to current and native.
8403    ((null gnus-refer-article-method)
8404     (list gnus-current-select-method gnus-select-method))
8405    ;; Current.
8406    ((eq 'current gnus-refer-article-method)
8407     (list gnus-current-select-method))
8408    ;; List of select methods.
8409    ((not (and (symbolp (car gnus-refer-article-method))
8410               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8411     (let (out)
8412       (dolist (method gnus-refer-article-method)
8413         (push (if (eq 'current method)
8414                   gnus-current-select-method
8415                 method)
8416               out))
8417       (nreverse out)))
8418    ;; One single select method.
8419    (t
8420     (list gnus-refer-article-method))))
8421
8422 (defun gnus-summary-edit-parameters ()
8423   "Edit the group parameters of the current group."
8424   (interactive)
8425   (gnus-group-edit-group gnus-newsgroup-name 'params))
8426
8427 (defun gnus-summary-customize-parameters ()
8428   "Customize the group parameters of the current group."
8429   (interactive)
8430   (gnus-group-customize gnus-newsgroup-name))
8431
8432 (defun gnus-summary-enter-digest-group (&optional force)
8433   "Enter an nndoc group based on the current article.
8434 If FORCE, force a digest interpretation.  If not, try
8435 to guess what the document format is."
8436   (interactive "P")
8437   (let ((conf gnus-current-window-configuration))
8438     (save-window-excursion
8439       (save-excursion
8440         (let (gnus-article-prepare-hook
8441               gnus-display-mime-function
8442               gnus-break-pages)
8443           (gnus-summary-select-article))))
8444     (setq gnus-current-window-configuration conf)
8445     (let* ((name (format "%s-%d"
8446                          (gnus-group-prefixed-name
8447                           gnus-newsgroup-name (list 'nndoc ""))
8448                          (with-current-buffer gnus-summary-buffer
8449                            gnus-current-article)))
8450            (ogroup gnus-newsgroup-name)
8451            (params (append (gnus-info-params (gnus-get-info ogroup))
8452                            (list (cons 'to-group ogroup))
8453                            (list (cons 'parent-group ogroup))
8454                            (list (cons 'save-article-group ogroup))))
8455            (case-fold-search t)
8456            (buf (current-buffer))
8457            dig to-address)
8458       (save-excursion
8459         (set-buffer gnus-original-article-buffer)
8460         ;; Have the digest group inherit the main mail address of
8461         ;; the parent article.
8462         (when (setq to-address (or (gnus-fetch-field "reply-to")
8463                                    (gnus-fetch-field "from")))
8464           (setq params (append
8465                         (list (cons 'to-address
8466                                     (funcall gnus-decode-encoded-word-function
8467                                              to-address))))))
8468         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8469         (insert-buffer-substring gnus-original-article-buffer)
8470         ;; Remove lines that may lead nndoc to misinterpret the
8471         ;; document type.
8472         (narrow-to-region
8473          (goto-char (point-min))
8474          (or (search-forward "\n\n" nil t) (point)))
8475         (goto-char (point-min))
8476         (delete-matching-lines "^Path:\\|^From ")
8477         (widen))
8478       (unwind-protect
8479           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8480                     (gnus-newsgroup-ephemeral-ignored-charsets
8481                      gnus-newsgroup-ignored-charsets))
8482                 (gnus-group-read-ephemeral-group
8483                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8484                               (nndoc-article-type
8485                                ,(if force 'mbox 'guess)))
8486                  t nil nil nil
8487                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8488                                                         "ADAPT")))))
8489               ;; Make all postings to this group go to the parent group.
8490               (nconc (gnus-info-params (gnus-get-info name))
8491                      params)
8492             ;; Couldn't select this doc group.
8493             (switch-to-buffer buf)
8494             (gnus-set-global-variables)
8495             (gnus-configure-windows 'summary)
8496             (gnus-message 3 "Article couldn't be entered?"))
8497         (kill-buffer dig)))))
8498
8499 (defun gnus-summary-read-document (n)
8500   "Open a new group based on the current article(s).
8501 This will allow you to read digests and other similar
8502 documents as newsgroups.
8503 Obeys the standard process/prefix convention."
8504   (interactive "P")
8505   (let* ((ogroup gnus-newsgroup-name)
8506          (params (append (gnus-info-params (gnus-get-info ogroup))
8507                          (list (cons 'to-group ogroup))))
8508          group egroup groups vgroup)
8509     (dolist (article (gnus-summary-work-articles n))
8510       (setq group (format "%s-%d" gnus-newsgroup-name article))
8511       (gnus-summary-remove-process-mark article)
8512       (when (gnus-summary-display-article article)
8513         (save-excursion
8514           (with-temp-buffer
8515             (insert-buffer-substring gnus-original-article-buffer)
8516             ;; Remove some headers that may lead nndoc to make
8517             ;; the wrong guess.
8518             (message-narrow-to-head)
8519             (goto-char (point-min))
8520             (delete-matching-lines "^Path:\\|^From ")
8521             (widen)
8522             (if (setq egroup
8523                       (gnus-group-read-ephemeral-group
8524                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8525                                      (nndoc-article-type guess))
8526                        t nil t))
8527                 (progn
8528             ;; Make all postings to this group go to the parent group.
8529                   (nconc (gnus-info-params (gnus-get-info egroup))
8530                          params)
8531                   (push egroup groups))
8532               ;; Couldn't select this doc group.
8533               (gnus-error 3 "Article couldn't be entered"))))))
8534     ;; Now we have selected all the documents.
8535     (cond
8536      ((not groups)
8537       (error "None of the articles could be interpreted as documents"))
8538      ((gnus-group-read-ephemeral-group
8539        (setq vgroup (format
8540                      "nnvirtual:%s-%s" gnus-newsgroup-name
8541                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8542        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8543        t
8544        (cons (current-buffer) 'summary)))
8545      (t
8546       (error "Couldn't select virtual nndoc group")))))
8547
8548 (defun gnus-summary-isearch-article (&optional regexp-p)
8549   "Do incremental search forward on the current article.
8550 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8551   (interactive "P")
8552   (gnus-summary-select-article)
8553   (gnus-configure-windows 'article)
8554   (gnus-eval-in-buffer-window gnus-article-buffer
8555     (save-restriction
8556       (widen)
8557       (isearch-forward regexp-p))))
8558
8559 (defun gnus-summary-search-article-forward (regexp &optional backward)
8560   "Search for an article containing REGEXP forward.
8561 If BACKWARD, search backward instead."
8562   (interactive
8563    (list (read-string
8564           (format "Search article %s (regexp%s): "
8565                   (if current-prefix-arg "backward" "forward")
8566                   (if gnus-last-search-regexp
8567                       (concat ", default " gnus-last-search-regexp)
8568                     "")))
8569          current-prefix-arg))
8570   (if (string-equal regexp "")
8571       (setq regexp (or gnus-last-search-regexp ""))
8572     (setq gnus-last-search-regexp regexp)
8573     (setq gnus-article-before-search gnus-current-article))
8574   ;; Intentionally set gnus-last-article.
8575   (setq gnus-last-article gnus-article-before-search)
8576   (let ((gnus-last-article gnus-last-article))
8577     (if (gnus-summary-search-article regexp backward)
8578         (gnus-summary-show-thread)
8579       (signal 'search-failed (list regexp)))))
8580
8581 (defun gnus-summary-search-article-backward (regexp)
8582   "Search for an article containing REGEXP backward."
8583   (interactive
8584    (list (read-string
8585           (format "Search article backward (regexp%s): "
8586                   (if gnus-last-search-regexp
8587                       (concat ", default " gnus-last-search-regexp)
8588                     "")))))
8589   (gnus-summary-search-article-forward regexp 'backward))
8590
8591 (defun gnus-summary-search-article (regexp &optional backward)
8592   "Search for an article containing REGEXP.
8593 Optional argument BACKWARD means do search for backward.
8594 `gnus-select-article-hook' is not called during the search."
8595   ;; We have to require this here to make sure that the following
8596   ;; dynamic binding isn't shadowed by autoloading.
8597   (require 'gnus-async)
8598   (require 'gnus-art)
8599   (let ((gnus-select-article-hook nil)  ;Disable hook.
8600         (gnus-article-prepare-hook nil)
8601         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8602         (gnus-use-article-prefetch nil)
8603         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8604         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8605         (gnus-visual nil)
8606         (gnus-keep-backlog nil)
8607         (gnus-break-pages nil)
8608         (gnus-summary-display-arrow nil)
8609         (gnus-updated-mode-lines nil)
8610         (gnus-auto-center-summary nil)
8611         (sum (current-buffer))
8612         (gnus-display-mime-function nil)
8613         (found nil)
8614         point)
8615     (gnus-save-hidden-threads
8616       (gnus-summary-select-article)
8617       (set-buffer gnus-article-buffer)
8618       (goto-char (window-point (get-buffer-window (current-buffer))))
8619       (when backward
8620         (forward-line -1))
8621       (while (not found)
8622         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8623         (if (if backward
8624                 (re-search-backward regexp nil t)
8625               (re-search-forward regexp nil t))
8626             ;; We found the regexp.
8627             (progn
8628               (setq found 'found)
8629               (beginning-of-line)
8630               (set-window-start
8631                (get-buffer-window (current-buffer))
8632                (point))
8633               (forward-line 1)
8634               (set-window-point
8635                (get-buffer-window (current-buffer))
8636                (point))
8637               (set-buffer sum)
8638               (setq point (point)))
8639           ;; We didn't find it, so we go to the next article.
8640           (set-buffer sum)
8641           (setq found 'not)
8642           (while (eq found 'not)
8643             (if (not (if backward (gnus-summary-find-prev)
8644                        (gnus-summary-find-next)))
8645                 ;; No more articles.
8646                 (setq found t)
8647               ;; Select the next article and adjust point.
8648               (unless (gnus-summary-article-sparse-p
8649                        (gnus-summary-article-number))
8650                 (setq found nil)
8651                 (gnus-summary-select-article)
8652                 (set-buffer gnus-article-buffer)
8653                 (widen)
8654                 (goto-char (if backward (point-max) (point-min))))))))
8655       (gnus-message 7 ""))
8656     ;; Return whether we found the regexp.
8657     (when (eq found 'found)
8658       (goto-char point)
8659       (gnus-summary-show-thread)
8660       (gnus-summary-goto-subject gnus-current-article)
8661       (gnus-summary-position-point)
8662       t)))
8663
8664 (defun gnus-find-matching-articles (header regexp)
8665   "Return a list of all articles that match REGEXP on HEADER.
8666 This search includes all articles in the current group that Gnus has
8667 fetched headers for, whether they are displayed or not."
8668   (let ((articles nil)
8669         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8670         (case-fold-search t))
8671     (dolist (header gnus-newsgroup-headers)
8672       (when (string-match regexp (funcall func header))
8673         (push (mail-header-number header) articles)))
8674     (nreverse articles)))
8675
8676 (defun gnus-summary-find-matching (header regexp &optional backward unread
8677                                           not-case-fold not-matching)
8678   "Return a list of all articles that match REGEXP on HEADER.
8679 The search stars on the current article and goes forwards unless
8680 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8681 If UNREAD is non-nil, only unread articles will
8682 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8683 in the comparisons. If NOT-MATCHING, return a list of all articles that
8684 not match REGEXP on HEADER."
8685   (let ((case-fold-search (not not-case-fold))
8686         articles d func)
8687     (if (consp header)
8688         (if (eq (car header) 'extra)
8689             (setq func
8690                   `(lambda (h)
8691                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8692                          "")))
8693           (error "%s is an invalid header" header))
8694       (unless (fboundp (intern (concat "mail-header-" header)))
8695         (error "%s is not a valid header" header))
8696       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8697     (dolist (d (if (eq backward 'all)
8698                    gnus-newsgroup-data
8699                  (gnus-data-find-list
8700                   (gnus-summary-article-number)
8701                   (gnus-data-list backward))))
8702       (when (and (or (not unread)       ; We want all articles...
8703                      (gnus-data-unread-p d)) ; Or just unreads.
8704                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8705                  (if not-matching
8706                      (not (string-match
8707                            regexp
8708                            (funcall func (gnus-data-header d))))
8709                    (string-match regexp
8710                                  (funcall func (gnus-data-header d)))))
8711         (push (gnus-data-number d) articles))) ; Success!
8712     (nreverse articles)))
8713
8714 (defun gnus-summary-execute-command (header regexp command &optional backward)
8715   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8716 If HEADER is an empty string (or nil), the match is done on the entire
8717 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8718   (interactive
8719    (list (let ((completion-ignore-case t))
8720            (completing-read
8721             "Header name: "
8722             (mapcar (lambda (header) (list (format "%s" header)))
8723                     (append
8724                      '("Number" "Subject" "From" "Lines" "Date"
8725                        "Message-ID" "Xref" "References" "Body")
8726                      gnus-extra-headers))
8727             nil 'require-match))
8728          (read-string "Regexp: ")
8729          (read-key-sequence "Command: ")
8730          current-prefix-arg))
8731   (when (equal header "Body")
8732     (setq header ""))
8733   ;; Hidden thread subtrees must be searched as well.
8734   (gnus-summary-show-all-threads)
8735   ;; We don't want to change current point nor window configuration.
8736   (save-excursion
8737     (save-window-excursion
8738       (let (gnus-visual
8739             gnus-treat-strip-trailing-blank-lines
8740             gnus-treat-strip-leading-blank-lines
8741             gnus-treat-strip-multiple-blank-lines
8742             gnus-treat-hide-boring-headers
8743             gnus-treat-fold-newsgroups
8744             gnus-article-prepare-hook)
8745         (gnus-message 6 "Executing %s..." (key-description command))
8746         ;; We'd like to execute COMMAND interactively so as to give arguments.
8747         (gnus-execute header regexp
8748                       `(call-interactively ',(key-binding command))
8749                       backward)
8750         (gnus-message 6 "Executing %s...done" (key-description command))))))
8751
8752 (defun gnus-summary-beginning-of-article ()
8753   "Scroll the article back to the beginning."
8754   (interactive)
8755   (gnus-summary-select-article)
8756   (gnus-configure-windows 'article)
8757   (gnus-eval-in-buffer-window gnus-article-buffer
8758     (widen)
8759     (goto-char (point-min))
8760     (when gnus-break-pages
8761       (gnus-narrow-to-page))))
8762
8763 (defun gnus-summary-end-of-article ()
8764   "Scroll to the end of the article."
8765   (interactive)
8766   (gnus-summary-select-article)
8767   (gnus-configure-windows 'article)
8768   (gnus-eval-in-buffer-window gnus-article-buffer
8769     (widen)
8770     (goto-char (point-max))
8771     (recenter -3)
8772     (when gnus-break-pages
8773       (when (re-search-backward page-delimiter nil t)
8774         (narrow-to-region (match-end 0) (point-max)))
8775       (gnus-narrow-to-page))))
8776
8777 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8778   "Truncate to LEN and quote all \"(\"'s in STRING."
8779   (gnus-replace-in-string (if (and len (> (length string) len))
8780                               (substring string 0 len)
8781                             string)
8782                           "[()]" "\\\\\\&"))
8783
8784 (defun gnus-summary-print-article (&optional filename n)
8785   "Generate and print a PostScript image of the process-marked (mail) articles.
8786
8787 If used interactively, print the current article if none are
8788 process-marked.  With prefix arg, prompt the user for the name of the
8789 file to save in.
8790
8791 When used from Lisp, accept two optional args FILENAME and N.  N means
8792 to print the next N articles.  If N is negative, print the N previous
8793 articles.  If N is nil and articles have been marked with the process
8794 mark, print these instead.
8795
8796 If the optional first argument FILENAME is nil, send the image to the
8797 printer.  If FILENAME is a string, save the PostScript image in a file with
8798 that name.  If FILENAME is a number, prompt the user for the name of the file
8799 to save in."
8800   (interactive (list (ps-print-preprint current-prefix-arg)))
8801   (dolist (article (gnus-summary-work-articles n))
8802     (gnus-summary-select-article nil nil 'pseudo article)
8803     (gnus-eval-in-buffer-window gnus-article-buffer
8804       (gnus-print-buffer))
8805     (gnus-summary-remove-process-mark article))
8806   (ps-despool filename))
8807
8808 (defun gnus-print-buffer ()
8809   (let ((buffer (generate-new-buffer " *print*")))
8810     (unwind-protect
8811         (progn
8812           (copy-to-buffer buffer (point-min) (point-max))
8813           (set-buffer buffer)
8814           (gnus-remove-text-with-property 'gnus-decoration)
8815           (when (gnus-visual-p 'article-highlight 'highlight)
8816             ;; Copy-to-buffer doesn't copy overlay.  So redo
8817             ;; highlight.
8818             (let ((gnus-article-buffer buffer))
8819               (gnus-article-highlight-citation t)
8820               (gnus-article-highlight-signature)
8821               (gnus-article-emphasize)
8822               (gnus-article-delete-invisible-text)))
8823           (let ((ps-left-header
8824                  (list
8825                   (concat "("
8826                           (gnus-summary-print-truncate-and-quote
8827                            (mail-header-subject gnus-current-headers)
8828                            66) ")")
8829                   (concat "("
8830                           (gnus-summary-print-truncate-and-quote
8831                            (mail-header-from gnus-current-headers)
8832                            45) ")")))
8833                 (ps-right-header
8834                  (list
8835                   "/pagenumberstring load"
8836                   (concat "("
8837                           (mail-header-date gnus-current-headers) ")"))))
8838             (gnus-run-hooks 'gnus-ps-print-hook)
8839             (save-excursion
8840               (if window-system
8841                   (ps-spool-buffer-with-faces)
8842                 (ps-spool-buffer)))))
8843       (kill-buffer buffer))))
8844
8845 (defun gnus-summary-show-article (&optional arg)
8846   "Force redisplaying of the current article.
8847 If ARG (the prefix) is a number, show the article with the charset
8848 defined in `gnus-summary-show-article-charset-alist', or the charset
8849 input.
8850 If ARG (the prefix) is non-nil and not a number, show the raw article
8851 without any article massaging functions being run.  Normally, the key
8852 strokes are `C-u g'."
8853   (interactive "P")
8854   (cond
8855    ((numberp arg)
8856     (gnus-summary-show-article t)
8857     (let ((gnus-newsgroup-charset
8858            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8859                (mm-read-coding-system
8860                 "View as charset: " ;; actually it is coding system.
8861                 (with-current-buffer gnus-article-buffer
8862                   (mm-detect-coding-region (point) (point-max))))))
8863           (gnus-newsgroup-ignored-charsets 'gnus-all))
8864       (gnus-summary-select-article nil 'force)
8865       (let ((deps gnus-newsgroup-dependencies)
8866             head header lines)
8867         (save-excursion
8868           (set-buffer gnus-original-article-buffer)
8869           (save-restriction
8870             (message-narrow-to-head)
8871             (setq head (buffer-string))
8872             (goto-char (point-min))
8873             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8874               (goto-char (point-max))
8875               (widen)
8876               (setq lines (1- (count-lines (point) (point-max))))))
8877           (with-temp-buffer
8878             (insert (format "211 %d Article retrieved.\n"
8879                             (cdr gnus-article-current)))
8880             (insert head)
8881             (if lines (insert (format "Lines: %d\n" lines)))
8882             (insert ".\n")
8883             (let ((nntp-server-buffer (current-buffer)))
8884               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8885         (gnus-data-set-header
8886          (gnus-data-find (cdr gnus-article-current))
8887          header)
8888         (gnus-summary-update-article-line
8889          (cdr gnus-article-current) header)
8890         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8891           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8892    ((not arg)
8893     ;; Select the article the normal way.
8894     (gnus-summary-select-article nil 'force))
8895    (t
8896     ;; We have to require this here to make sure that the following
8897     ;; dynamic binding isn't shadowed by autoloading.
8898     (require 'gnus-async)
8899     (require 'gnus-art)
8900     ;; Bind the article treatment functions to nil.
8901     (let ((gnus-have-all-headers t)
8902           gnus-article-prepare-hook
8903           gnus-article-decode-hook
8904           gnus-display-mime-function
8905           gnus-break-pages)
8906       ;; Destroy any MIME parts.
8907       (when (gnus-buffer-live-p gnus-article-buffer)
8908         (save-excursion
8909           (set-buffer gnus-article-buffer)
8910           (mm-destroy-parts gnus-article-mime-handles)
8911           ;; Set it to nil for safety reason.
8912           (setq gnus-article-mime-handle-alist nil)
8913           (setq gnus-article-mime-handles nil)))
8914       (gnus-summary-select-article nil 'force))))
8915   (gnus-summary-goto-subject gnus-current-article)
8916   (gnus-summary-position-point))
8917
8918 (defun gnus-summary-show-raw-article ()
8919   "Show the raw article without any article massaging functions being run."
8920   (interactive)
8921   (gnus-summary-show-article t))
8922
8923 (defun gnus-summary-verbose-headers (&optional arg)
8924   "Toggle permanent full header display.
8925 If ARG is a positive number, turn header display on.
8926 If ARG is a negative number, turn header display off."
8927   (interactive "P")
8928   (setq gnus-show-all-headers
8929         (cond ((or (not (numberp arg))
8930                    (zerop arg))
8931                (not gnus-show-all-headers))
8932               ((natnump arg)
8933                t)))
8934   (gnus-summary-show-article))
8935
8936 (defun gnus-summary-toggle-header (&optional arg)
8937   "Show the headers if they are hidden, or hide them if they are shown.
8938 If ARG is a positive number, show the entire header.
8939 If ARG is a negative number, hide the unwanted header lines."
8940   (interactive "P")
8941   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8942                      (get-buffer-window gnus-article-buffer t))))
8943     (with-current-buffer gnus-article-buffer
8944       (widen)
8945       (article-narrow-to-head)
8946       (let* ((buffer-read-only nil)
8947              (inhibit-point-motion-hooks t)
8948              (hidden (if (numberp arg)
8949                          (>= arg 0)
8950                        (or (not (looking-at "[^ \t\n]+:"))
8951                            (gnus-article-hidden-text-p 'headers))))
8952              s e)
8953         (delete-region (point-min) (point-max))
8954         (with-current-buffer gnus-original-article-buffer
8955           (goto-char (setq s (point-min)))
8956           (setq e (if (search-forward "\n\n" nil t)
8957                       (1- (point))
8958                     (point-max))))
8959         (insert-buffer-substring gnus-original-article-buffer s e)
8960         (run-hooks 'gnus-article-decode-hook)
8961         (if hidden
8962             (let ((gnus-treat-hide-headers nil)
8963                   (gnus-treat-hide-boring-headers nil))
8964               (gnus-delete-wash-type 'headers)
8965               (gnus-treat-article 'head))
8966           (gnus-treat-article 'head))
8967         (widen)
8968         (if window
8969             (set-window-start window (goto-char (point-min))))
8970         (if gnus-break-pages
8971             (gnus-narrow-to-page)
8972           (when (gnus-visual-p 'page-marker)
8973             (let ((buffer-read-only nil))
8974               (gnus-remove-text-with-property 'gnus-prev)
8975               (gnus-remove-text-with-property 'gnus-next))))
8976         (gnus-set-mode-line 'article)))))
8977
8978 (defun gnus-summary-show-all-headers ()
8979   "Make all header lines visible."
8980   (interactive)
8981   (gnus-summary-toggle-header 1))
8982
8983 (defun gnus-summary-caesar-message (&optional arg)
8984   "Caesar rotate the current article by 13.
8985 The numerical prefix specifies how many places to rotate each letter
8986 forward."
8987   (interactive "P")
8988   (gnus-summary-select-article)
8989   (let ((mail-header-separator ""))
8990     (gnus-eval-in-buffer-window gnus-article-buffer
8991       (save-restriction
8992         (widen)
8993         (let ((start (window-start))
8994               buffer-read-only)
8995           (message-caesar-buffer-body arg)
8996           (set-window-start (get-buffer-window (current-buffer)) start)))))
8997   ;; Create buttons and stuff...
8998   (gnus-treat-article nil))
8999
9000 (defun gnus-summary-idna-message (&optional arg)
9001   "Decode IDNA encoded domain names in the current articles.
9002 IDNA encoded domain names looks like `xn--bar'.  If a string
9003 remain unencoded after running this function, it is likely an
9004 invalid IDNA string (`xn--bar' is invalid).
9005
9006 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9007 installed for this command to work."
9008   (interactive "P")
9009   (if (not (and (condition-case nil (require 'idna)
9010                   (file-error))
9011                 (mm-coding-system-p 'utf-8)
9012                 (executable-find (symbol-value 'idna-program))))
9013       (gnus-message
9014        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9015     (gnus-summary-select-article)
9016     (let ((mail-header-separator ""))
9017       (gnus-eval-in-buffer-window gnus-article-buffer
9018         (save-restriction
9019           (widen)
9020           (let ((start (window-start))
9021                 buffer-read-only)
9022             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9023               (replace-match (idna-to-unicode (match-string 1))))
9024             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9025
9026 (autoload 'unmorse-region "morse"
9027   "Convert morse coded text in region to ordinary ASCII text."
9028   t)
9029
9030 (defun gnus-summary-morse-message (&optional arg)
9031   "Morse decode the current article."
9032   (interactive "P")
9033   (gnus-summary-select-article)
9034   (let ((mail-header-separator ""))
9035     (gnus-eval-in-buffer-window gnus-article-buffer
9036       (save-excursion
9037         (save-restriction
9038           (widen)
9039           (let ((pos (window-start))
9040                 buffer-read-only)
9041             (goto-char (point-min))
9042             (when (message-goto-body)
9043               (gnus-narrow-to-body))
9044             (goto-char (point-min))
9045             (while (search-forward "·" (point-max) t)
9046               (replace-match "."))
9047             (unmorse-region (point-min) (point-max))
9048             (widen)
9049             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9050
9051 (defun gnus-summary-stop-page-breaking ()
9052   "Stop page breaking in the current article."
9053   (interactive)
9054   (gnus-summary-select-article)
9055   (gnus-eval-in-buffer-window gnus-article-buffer
9056     (widen)
9057     (when (gnus-visual-p 'page-marker)
9058       (let ((buffer-read-only nil))
9059         (gnus-remove-text-with-property 'gnus-prev)
9060         (gnus-remove-text-with-property 'gnus-next))
9061       (setq gnus-page-broken nil))))
9062
9063 (defun gnus-summary-move-article (&optional n to-newsgroup
9064                                             select-method action)
9065   "Move the current article to a different newsgroup.
9066 If N is a positive number, move the N next articles.
9067 If N is a negative number, move the N previous articles.
9068 If N is nil and any articles have been marked with the process mark,
9069 move those articles instead.
9070 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9071 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9072 re-spool using this method.
9073
9074 When called interactively with TO-NEWSGROUP being nil, the value of
9075 the variable `gnus-move-split-methods' is used for finding a default
9076 for the target newsgroup.
9077
9078 For this function to work, both the current newsgroup and the
9079 newsgroup that you want to move to have to support the `request-move'
9080 and `request-accept' functions.
9081
9082 ACTION can be either `move' (the default), `crosspost' or `copy'."
9083   (interactive "P")
9084   (unless action
9085     (setq action 'move))
9086   ;; Check whether the source group supports the required functions.
9087   (cond ((and (eq action 'move)
9088               (not (gnus-check-backend-function
9089                     'request-move-article gnus-newsgroup-name)))
9090          (error "The current group does not support article moving"))
9091         ((and (eq action 'crosspost)
9092               (not (gnus-check-backend-function
9093                     'request-replace-article gnus-newsgroup-name)))
9094          (error "The current group does not support article editing")))
9095   (let ((articles (gnus-summary-work-articles n))
9096         (prefix (if (gnus-check-backend-function
9097                      'request-move-article gnus-newsgroup-name)
9098                     (funcall gnus-move-group-prefix-function
9099                              gnus-newsgroup-name)
9100                   ""))
9101         (names '((move "Move" "Moving")
9102                  (copy "Copy" "Copying")
9103                  (crosspost "Crosspost" "Crossposting")))
9104         (copy-buf (save-excursion
9105                     (nnheader-set-temp-buffer " *copy article*")))
9106         art-group to-method new-xref article to-groups articles-to-update-marks)
9107     (unless (assq action names)
9108       (error "Unknown action %s" action))
9109     ;; Read the newsgroup name.
9110     (when (and (not to-newsgroup)
9111                (not select-method))
9112       (if (and gnus-move-split-methods
9113                (not
9114                 (and (memq gnus-current-article articles)
9115                      (gnus-buffer-live-p gnus-original-article-buffer))))
9116           ;; When `gnus-move-split-methods' is non-nil, we have to
9117           ;; select an article to give `gnus-read-move-group-name' an
9118           ;; opportunity to suggest an appropriate default.  However,
9119           ;; we needn't render or mark the article.
9120           (let ((gnus-display-mime-function nil)
9121                 (gnus-article-prepare-hook nil)
9122                 (gnus-mark-article-hook nil))
9123             (gnus-summary-select-article nil nil nil (car articles))))
9124       (setq to-newsgroup
9125             (gnus-read-move-group-name
9126              (cadr (assq action names))
9127              (symbol-value (intern (format "gnus-current-%s-group" action)))
9128              articles prefix))
9129       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9130     (setq to-method (or select-method
9131                         (gnus-server-to-method
9132                          (gnus-group-method to-newsgroup))))
9133     ;; Check the method we are to move this article to...
9134     (unless (gnus-check-backend-function
9135              'request-accept-article (car to-method))
9136       (error "%s does not support article copying" (car to-method)))
9137     (unless (gnus-check-server to-method)
9138       (error "Can't open server %s" (car to-method)))
9139     (gnus-message 6 "%s to %s: %s..."
9140                   (caddr (assq action names))
9141                   (or (car select-method) to-newsgroup) articles)
9142     (while articles
9143       (setq article (pop articles))
9144       (setq
9145        art-group
9146        (cond
9147         ;; Move the article.
9148         ((eq action 'move)
9149          ;; Remove this article from future suppression.
9150          (gnus-dup-unsuppress-article article)
9151          (let* ((from-method (gnus-find-method-for-group
9152                               gnus-newsgroup-name))
9153                 (to-method (gnus-find-method-for-group
9154                             to-newsgroup))
9155                 (move-is-internal (gnus-method-equal from-method to-method)))
9156          (gnus-request-move-article
9157           article                       ; Article to move
9158           gnus-newsgroup-name           ; From newsgroup
9159           (nth 1 (gnus-find-method-for-group
9160                   gnus-newsgroup-name)) ; Server
9161           (list 'gnus-request-accept-article
9162                 to-newsgroup (list 'quote select-method)
9163                 (not articles) t)       ; Accept form
9164           (not articles)                ; Only save nov last time
9165           move-is-internal)))           ; is this move internal?
9166         ;; Copy the article.
9167         ((eq action 'copy)
9168          (save-excursion
9169            (set-buffer copy-buf)
9170            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9171              (save-restriction
9172                (nnheader-narrow-to-headers)
9173                (dolist (hdr gnus-copy-article-ignored-headers)
9174                  (message-remove-header hdr t)))
9175              (gnus-request-accept-article
9176               to-newsgroup select-method (not articles) t))))
9177         ;; Crosspost the article.
9178         ((eq action 'crosspost)
9179          (let ((xref (message-tokenize-header
9180                       (mail-header-xref (gnus-summary-article-header article))
9181                       " ")))
9182            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9183                                   ":" (number-to-string article)))
9184            (unless xref
9185              (setq xref (list (system-name))))
9186            (setq new-xref
9187                  (concat
9188                   (mapconcat 'identity
9189                              (delete "Xref:" (delete new-xref xref))
9190                              " ")
9191                   " " new-xref))
9192            (save-excursion
9193              (set-buffer copy-buf)
9194              ;; First put the article in the destination group.
9195              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9196              (when (consp (setq art-group
9197                                 (gnus-request-accept-article
9198                                  to-newsgroup select-method (not articles))))
9199                (setq new-xref (concat new-xref " " (car art-group)
9200                                       ":"
9201                                       (number-to-string (cdr art-group))))
9202                ;; Now we have the new Xrefs header, so we insert
9203                ;; it and replace the new article.
9204                (nnheader-replace-header "Xref" new-xref)
9205                (gnus-request-replace-article
9206                 (cdr art-group) to-newsgroup (current-buffer))
9207                art-group))))))
9208       (cond
9209        ((not art-group)
9210         (gnus-message 1 "Couldn't %s article %s: %s"
9211                       (cadr (assq action names)) article
9212                       (nnheader-get-report (car to-method))))
9213        ((eq art-group 'junk)
9214         (when (eq action 'move)
9215           (gnus-summary-mark-article article gnus-canceled-mark)
9216           (gnus-message 4 "Deleted article %s" article)
9217           ;; run the delete hook
9218           (run-hook-with-args 'gnus-summary-article-delete-hook
9219                               action
9220                               (gnus-data-header
9221                                (assoc article (gnus-data-list nil)))
9222                               gnus-newsgroup-name nil
9223                               select-method)))
9224        (t
9225         (let* ((pto-group (gnus-group-prefixed-name
9226                            (car art-group) to-method))
9227                (info (gnus-get-info pto-group))
9228                (to-group (gnus-info-group info))
9229                to-marks)
9230           ;; Update the group that has been moved to.
9231           (when (and info
9232                      (memq action '(move copy)))
9233             (unless (member to-group to-groups)
9234               (push to-group to-groups))
9235
9236             (unless (memq article gnus-newsgroup-unreads)
9237               (push 'read to-marks)
9238               (gnus-info-set-read
9239                info (gnus-add-to-range (gnus-info-read info)
9240                                        (list (cdr art-group)))))
9241
9242             ;; See whether the article is to be put in the cache.
9243             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9244                              gnus-article-mark-lists
9245                            (delete '(expirable . expire)
9246                                    (copy-sequence gnus-article-mark-lists))))
9247                   (to-article (cdr art-group)))
9248
9249               ;; Enter the article into the cache in the new group,
9250               ;; if that is required.
9251               (when gnus-use-cache
9252                 (gnus-cache-possibly-enter-article
9253                  to-group to-article
9254                  (memq article gnus-newsgroup-marked)
9255                  (memq article gnus-newsgroup-dormant)
9256                  (memq article gnus-newsgroup-unreads)))
9257
9258               (when gnus-preserve-marks
9259                 ;; Copy any marks over to the new group.
9260                 (when (and (equal to-group gnus-newsgroup-name)
9261                            (not (memq article gnus-newsgroup-unreads)))
9262                   ;; Mark this article as read in this group.
9263                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9264                   (setcdr (gnus-active to-group) to-article)
9265                   (setcdr gnus-newsgroup-active to-article))
9266
9267                 (while marks
9268                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9269                     (when (memq article (symbol-value
9270                                          (intern (format "gnus-newsgroup-%s"
9271                                                          (caar marks)))))
9272                       (push (cdar marks) to-marks)
9273                       ;; If the other group is the same as this group,
9274                       ;; then we have to add the mark to the list.
9275                       (when (equal to-group gnus-newsgroup-name)
9276                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9277                              (cons to-article
9278                                    (symbol-value
9279                                     (intern (format "gnus-newsgroup-%s"
9280                                                     (caar marks)))))))
9281                       ;; Copy the marks to other group.
9282                       (gnus-add-marked-articles
9283                        to-group (cdar marks) (list to-article) info)))
9284                   (setq marks (cdr marks)))
9285
9286                 (gnus-request-set-mark
9287                  to-group (list (list (list to-article) 'add to-marks))))
9288
9289               (gnus-dribble-enter
9290                (concat "(gnus-group-set-info '"
9291                        (gnus-prin1-to-string (gnus-get-info to-group))
9292                        ")"))))
9293
9294           ;; Update the Xref header in this article to point to
9295           ;; the new crossposted article we have just created.
9296           (when (eq action 'crosspost)
9297             (save-excursion
9298               (set-buffer copy-buf)
9299               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9300               (nnheader-replace-header "Xref" new-xref)
9301               (gnus-request-replace-article
9302                article gnus-newsgroup-name (current-buffer))))
9303
9304           ;; run the move/copy/crosspost/respool hook
9305           (run-hook-with-args 'gnus-summary-article-move-hook
9306                               action
9307                               (gnus-data-header
9308                                (assoc article (gnus-data-list nil)))
9309                               gnus-newsgroup-name
9310                               to-newsgroup
9311                               select-method))
9312
9313         ;;;!!!Why is this necessary?
9314         (set-buffer gnus-summary-buffer)
9315         
9316         (gnus-summary-goto-subject article)
9317         (when (eq action 'move)
9318           (gnus-summary-mark-article article gnus-canceled-mark))))
9319       (push article articles-to-update-marks))
9320
9321     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9322     ;; Re-activate all groups that have been moved to.
9323     (save-excursion
9324       (set-buffer gnus-group-buffer)
9325       (let ((gnus-group-marked to-groups))
9326         (gnus-group-get-new-news-this-group nil t)))
9327     
9328     (gnus-kill-buffer copy-buf)
9329     (gnus-summary-position-point)
9330     (gnus-set-mode-line 'summary)))
9331
9332 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9333   "Copy the current article to some other group.
9334 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9335 When called interactively, if TO-NEWSGROUP is nil, use the value of
9336 the variable `gnus-move-split-methods' for finding a default target
9337 newsgroup.
9338 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9339 re-spool using this method."
9340   (interactive "P")
9341   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9342
9343 (defun gnus-summary-crosspost-article (&optional n)
9344   "Crosspost the current article to some other group."
9345   (interactive "P")
9346   (gnus-summary-move-article n nil nil 'crosspost))
9347
9348 (defcustom gnus-summary-respool-default-method nil
9349   "Default method type for respooling an article.
9350 If nil, use to the current newsgroup method."
9351   :type 'symbol
9352   :group 'gnus-summary-mail)
9353
9354 (defcustom gnus-summary-display-while-building nil
9355   "If non-nil, show and update the summary buffer as it's being built.
9356 If the value is t, update the buffer after every line is inserted.  If
9357 the value is an integer (N), update the display every N lines."
9358   :version "22.1"
9359   :group 'gnus-thread
9360   :type '(choice (const :tag "off" nil)
9361                  number
9362                  (const :tag "frequently" t)))
9363
9364 (defun gnus-summary-respool-article (&optional n method)
9365   "Respool the current article.
9366 The article will be squeezed through the mail spooling process again,
9367 which means that it will be put in some mail newsgroup or other
9368 depending on `nnmail-split-methods'.
9369 If N is a positive number, respool the N next articles.
9370 If N is a negative number, respool the N previous articles.
9371 If N is nil and any articles have been marked with the process mark,
9372 respool those articles instead.
9373
9374 Respooling can be done both from mail groups and \"real\" newsgroups.
9375 In the former case, the articles in question will be moved from the
9376 current group into whatever groups they are destined to.  In the
9377 latter case, they will be copied into the relevant groups."
9378   (interactive
9379    (list current-prefix-arg
9380          (let* ((methods (gnus-methods-using 'respool))
9381                 (methname
9382                  (symbol-name (or gnus-summary-respool-default-method
9383                                   (car (gnus-find-method-for-group
9384                                         gnus-newsgroup-name)))))
9385                 (method
9386                  (gnus-completing-read-with-default
9387                   methname "Backend to use when respooling"
9388                   methods nil t nil 'gnus-mail-method-history))
9389                 ms)
9390            (cond
9391             ((zerop (length (setq ms (gnus-servers-using-backend
9392                                       (intern method)))))
9393              (list (intern method) ""))
9394             ((= 1 (length ms))
9395              (car ms))
9396             (t
9397              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9398                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9399                            ms-alist))))))))
9400   (unless method
9401     (error "No method given for respooling"))
9402   (if (assoc (symbol-name
9403               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9404              (gnus-methods-using 'respool))
9405       (gnus-summary-move-article n nil method)
9406     (gnus-summary-copy-article n nil method)))
9407
9408 (defun gnus-summary-import-article (file &optional edit)
9409   "Import an arbitrary file into a mail newsgroup."
9410   (interactive "fImport file: \nP")
9411   (let ((group gnus-newsgroup-name)
9412         (now (current-time))
9413         atts lines group-art)
9414     (unless (gnus-check-backend-function 'request-accept-article group)
9415       (error "%s does not support article importing" group))
9416     (or (file-readable-p file)
9417         (not (file-regular-p file))
9418         (error "Can't read %s" file))
9419     (save-excursion
9420       (set-buffer (gnus-get-buffer-create " *import file*"))
9421       (erase-buffer)
9422       (nnheader-insert-file-contents file)
9423       (goto-char (point-min))
9424       (if (nnheader-article-p)
9425           (save-restriction
9426             (goto-char (point-min))
9427             (search-forward "\n\n" nil t)
9428             (narrow-to-region (point-min) (1- (point)))
9429             (goto-char (point-min))
9430             (unless (re-search-forward "^date:" nil t)
9431               (goto-char (point-max))
9432               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9433        ;; This doesn't look like an article, so we fudge some headers.
9434         (setq atts (file-attributes file)
9435               lines (count-lines (point-min) (point-max)))
9436         (insert "From: " (read-string "From: ") "\n"
9437                 "Subject: " (read-string "Subject: ") "\n"
9438                 "Date: " (message-make-date (nth 5 atts)) "\n"
9439                 "Message-ID: " (message-make-message-id) "\n"
9440                 "Lines: " (int-to-string lines) "\n"
9441                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9442       (setq group-art (gnus-request-accept-article group nil t))
9443       (kill-buffer (current-buffer)))
9444     (setq gnus-newsgroup-active (gnus-activate-group group))
9445     (forward-line 1)
9446     (gnus-summary-goto-article (cdr group-art) nil t)
9447     (when edit
9448       (gnus-summary-edit-article))))
9449
9450 (defun gnus-summary-create-article ()
9451   "Create an article in a mail newsgroup."
9452   (interactive)
9453   (let ((group gnus-newsgroup-name)
9454         (now (current-time))
9455         group-art)
9456     (unless (gnus-check-backend-function 'request-accept-article group)
9457       (error "%s does not support article importing" group))
9458     (save-excursion
9459       (set-buffer (gnus-get-buffer-create " *import file*"))
9460       (erase-buffer)
9461       (goto-char (point-min))
9462       ;; This doesn't look like an article, so we fudge some headers.
9463       (insert "From: " (read-string "From: ") "\n"
9464               "Subject: " (read-string "Subject: ") "\n"
9465               "Date: " (message-make-date now) "\n"
9466               "Message-ID: " (message-make-message-id) "\n")
9467       (setq group-art (gnus-request-accept-article group nil t))
9468       (kill-buffer (current-buffer)))
9469     (setq gnus-newsgroup-active (gnus-activate-group group))
9470     (forward-line 1)
9471     (gnus-summary-goto-article (cdr group-art) nil t)
9472     (gnus-summary-edit-article)))
9473
9474 (defun gnus-summary-article-posted-p ()
9475   "Say whether the current (mail) article is available from news as well.
9476 This will be the case if the article has both been mailed and posted."
9477   (interactive)
9478   (let ((id (mail-header-references (gnus-summary-article-header)))
9479         (gnus-override-method (car (gnus-refer-article-methods))))
9480     (if (gnus-request-head id "")
9481         (gnus-message 2 "The current message was found on %s"
9482                       gnus-override-method)
9483       (gnus-message 2 "The current message couldn't be found on %s"
9484                     gnus-override-method)
9485       nil)))
9486
9487 (defun gnus-summary-expire-articles (&optional now)
9488   "Expire all articles that are marked as expirable in the current group."
9489   (interactive)
9490   (when (and (not gnus-group-is-exiting-without-update-p)
9491              (gnus-check-backend-function
9492               'request-expire-articles gnus-newsgroup-name))
9493     ;; This backend supports expiry.
9494     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9495            (expirable (if total
9496                           (progn
9497                             ;; We need to update the info for
9498                             ;; this group for `gnus-list-of-read-articles'
9499                             ;; to give us the right answer.
9500                             (gnus-run-hooks 'gnus-exit-group-hook)
9501                             (gnus-summary-update-info)
9502                             (gnus-list-of-read-articles gnus-newsgroup-name))
9503                         (setq gnus-newsgroup-expirable
9504                               (sort gnus-newsgroup-expirable '<))))
9505            (expiry-wait (if now 'immediate
9506                           (gnus-group-find-parameter
9507                            gnus-newsgroup-name 'expiry-wait)))
9508            (nnmail-expiry-target
9509             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9510                 nnmail-expiry-target))
9511            es)
9512       (when expirable
9513         ;; There are expirable articles in this group, so we run them
9514         ;; through the expiry process.
9515         (gnus-message 6 "Expiring articles...")
9516         (unless (gnus-check-group gnus-newsgroup-name)
9517           (error "Can't open server for %s" gnus-newsgroup-name))
9518         ;; The list of articles that weren't expired is returned.
9519         (save-excursion
9520           (if expiry-wait
9521               (let ((nnmail-expiry-wait-function nil)
9522                     (nnmail-expiry-wait expiry-wait))
9523                 (setq es (gnus-request-expire-articles
9524                           expirable gnus-newsgroup-name)))
9525             (setq es (gnus-request-expire-articles
9526                       expirable gnus-newsgroup-name)))
9527           (unless total
9528             (setq gnus-newsgroup-expirable es))
9529           ;; We go through the old list of expirable, and mark all
9530           ;; really expired articles as nonexistent.
9531           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9532             (let ((gnus-use-cache nil))
9533               (dolist (article expirable)
9534                 (when (and (not (memq article es))
9535                            (gnus-data-find article))
9536                   (gnus-summary-mark-article article gnus-canceled-mark)
9537                   (run-hook-with-args 'gnus-summary-article-expire-hook
9538                                       'delete
9539                                       (gnus-data-header
9540                                        (assoc article (gnus-data-list nil)))
9541                                       gnus-newsgroup-name
9542                                       nil
9543                                       nil))))))
9544         (gnus-message 6 "Expiring articles...done")))))
9545
9546 (defun gnus-summary-expire-articles-now ()
9547   "Expunge all expirable articles in the current group.
9548 This means that *all* articles that are marked as expirable will be
9549 deleted forever, right now."
9550   (interactive)
9551   (or gnus-expert-user
9552       (gnus-yes-or-no-p
9553        "Are you really, really, really sure you want to delete all these messages? ")
9554       (error "Phew!"))
9555   (gnus-summary-expire-articles t))
9556
9557 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9558 (defun gnus-summary-delete-article (&optional n)
9559   "Delete the N next (mail) articles.
9560 This command actually deletes articles.  This is not a marking
9561 command.  The article will disappear forever from your life, never to
9562 return.
9563
9564 If N is negative, delete backwards.
9565 If N is nil and articles have been marked with the process mark,
9566 delete these instead.
9567
9568 If `gnus-novice-user' is non-nil you will be asked for
9569 confirmation before the articles are deleted."
9570   (interactive "P")
9571   (unless (gnus-check-backend-function 'request-expire-articles
9572                                        gnus-newsgroup-name)
9573     (error "The current newsgroup does not support article deletion"))
9574   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9575     (error "Couldn't open server"))
9576   ;; Compute the list of articles to delete.
9577   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9578         (nnmail-expiry-target 'delete)
9579         not-deleted)
9580     (if (and gnus-novice-user
9581              (not (gnus-yes-or-no-p
9582                    (format "Do you really want to delete %s forever? "
9583                            (if (> (length articles) 1)
9584                                (format "these %s articles" (length articles))
9585                              "this article")))))
9586         ()
9587       ;; Delete the articles.
9588       (setq not-deleted (gnus-request-expire-articles
9589                          articles gnus-newsgroup-name 'force))
9590       (while articles
9591         (gnus-summary-remove-process-mark (car articles))
9592         ;; The backend might not have been able to delete the article
9593         ;; after all.
9594         (unless (memq (car articles) not-deleted)
9595           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9596         (let* ((article (car articles))
9597                (ghead  (gnus-data-header
9598                                     (assoc article (gnus-data-list nil)))))
9599           (run-hook-with-args 'gnus-summary-article-delete-hook
9600                               'delete ghead gnus-newsgroup-name nil
9601                               nil))
9602         (setq articles (cdr articles)))
9603       (when not-deleted
9604         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9605     (gnus-summary-position-point)
9606     (gnus-set-mode-line 'summary)
9607     not-deleted))
9608
9609 (defun gnus-summary-edit-article (&optional arg)
9610   "Edit the current article.
9611 This will have permanent effect only in mail groups.
9612 If ARG is nil, edit the decoded articles.
9613 If ARG is 1, edit the raw articles.
9614 If ARG is 2, edit the raw articles even in read-only groups.
9615 If ARG is 3, edit the articles with the current handles.
9616 Otherwise, allow editing of articles even in read-only
9617 groups."
9618   (interactive "P")
9619   (let (force raw current-handles)
9620     (cond
9621      ((null arg))
9622      ((eq arg 1)
9623       (setq raw t))
9624      ((eq arg 2)
9625       (setq raw t
9626             force t))
9627      ((eq arg 3)
9628       (setq current-handles
9629             (and (gnus-buffer-live-p gnus-article-buffer)
9630                  (with-current-buffer gnus-article-buffer
9631                    (prog1
9632                        gnus-article-mime-handles
9633                      (setq gnus-article-mime-handles nil))))))
9634      (t
9635       (setq force t)))
9636     (when (and raw (not force)
9637                (member gnus-newsgroup-name '("nndraft:delayed"
9638                                              "nndraft:drafts"
9639                                              "nndraft:queue")))
9640       (error "Can't edit the raw article in group %s"
9641              gnus-newsgroup-name))
9642     (save-excursion
9643       (set-buffer gnus-summary-buffer)
9644       (let ((mail-parse-charset gnus-newsgroup-charset)
9645             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9646         (gnus-set-global-variables)
9647         (when (and (not force)
9648                    (gnus-group-read-only-p))
9649           (error "The current newsgroup does not support article editing"))
9650         (gnus-summary-show-article t)
9651         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9652           (with-current-buffer gnus-article-buffer
9653             (mm-enable-multibyte)))
9654         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9655             (setq raw t))
9656         (gnus-article-edit-article
9657          (if raw 'ignore
9658            `(lambda ()
9659               (let ((mbl mml-buffer-list))
9660                 (setq mml-buffer-list nil)
9661                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9662                   (mime-to-mml ,'current-handles))
9663                 (let ((mbl1 mml-buffer-list))
9664                   (setq mml-buffer-list mbl)
9665                   (set (make-local-variable 'mml-buffer-list) mbl1))
9666                 (gnus-make-local-hook 'kill-buffer-hook)
9667                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9668          `(lambda (no-highlight)
9669             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9670                   (message-options message-options)
9671                   (message-options-set-recipient)
9672                   (mail-parse-ignored-charsets
9673                    ',gnus-newsgroup-ignored-charsets))
9674               ,(if (not raw) '(progn
9675                                 (mml-to-mime)
9676                                 (mml-destroy-buffers)
9677                                 (remove-hook 'kill-buffer-hook
9678                                              'mml-destroy-buffers t)
9679                                 (kill-local-variable 'mml-buffer-list)))
9680               (gnus-summary-edit-article-done
9681                ,(or (mail-header-references gnus-current-headers) "")
9682                ,(gnus-group-read-only-p)
9683                ,gnus-summary-buffer no-highlight))))))))
9684
9685 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9686
9687 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9688                                                  no-highlight)
9689   "Make edits to the current article permanent."
9690   (interactive)
9691   (save-excursion
9692    ;; The buffer restriction contains the entire article if it exists.
9693     (when (article-goto-body)
9694       (let ((lines (count-lines (point) (point-max)))
9695             (length (- (point-max) (point)))
9696             (case-fold-search t)
9697             (body (copy-marker (point))))
9698         (goto-char (point-min))
9699         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9700           (delete-region (match-beginning 1) (match-end 1))
9701           (insert (number-to-string length)))
9702         (goto-char (point-min))
9703         (when (re-search-forward
9704                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9705           (delete-region (match-beginning 1) (match-end 1))
9706           (insert (number-to-string length)))
9707         (goto-char (point-min))
9708         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9709           (delete-region (match-beginning 1) (match-end 1))
9710           (insert (number-to-string lines))))))
9711   ;; Replace the article.
9712   (let ((buf (current-buffer)))
9713     (with-temp-buffer
9714       (insert-buffer-substring buf)
9715
9716       (if (and (not read-only)
9717                (not (gnus-request-replace-article
9718                      (cdr gnus-article-current) (car gnus-article-current)
9719                      (current-buffer) t)))
9720           (error "Couldn't replace article")
9721         ;; Update the summary buffer.
9722         (if (and references
9723                  (equal (message-tokenize-header references " ")
9724                         (message-tokenize-header
9725                          (or (message-fetch-field "references") "") " ")))
9726             ;; We only have to update this line.
9727             (save-excursion
9728               (save-restriction
9729                 (message-narrow-to-head)
9730                 (let ((head (buffer-substring-no-properties
9731                              (point-min) (point-max)))
9732                       header)
9733                   (with-temp-buffer
9734                     (insert (format "211 %d Article retrieved.\n"
9735                                     (cdr gnus-article-current)))
9736                     (insert head)
9737                     (insert ".\n")
9738                     (let ((nntp-server-buffer (current-buffer)))
9739                       (setq header (car (gnus-get-newsgroup-headers
9740                                          nil t))))
9741                     (save-excursion
9742                       (set-buffer gnus-summary-buffer)
9743                       (gnus-data-set-header
9744                        (gnus-data-find (cdr gnus-article-current))
9745                        header)
9746                       (gnus-summary-update-article-line
9747                        (cdr gnus-article-current) header)
9748                       (if (gnus-summary-goto-subject
9749                            (cdr gnus-article-current) nil t)
9750                           (gnus-summary-update-secondary-mark
9751                            (cdr gnus-article-current))))))))
9752           ;; Update threads.
9753           (set-buffer (or buffer gnus-summary-buffer))
9754           (gnus-summary-update-article (cdr gnus-article-current))
9755           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9756               (gnus-summary-update-secondary-mark
9757                (cdr gnus-article-current))))
9758         ;; Prettify the article buffer again.
9759         (unless no-highlight
9760           (save-excursion
9761             (set-buffer gnus-article-buffer)
9762             ;;;!!! Fix this -- article should be rehighlighted.
9763             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9764             (set-buffer gnus-original-article-buffer)
9765             (gnus-request-article
9766              (cdr gnus-article-current)
9767              (car gnus-article-current) (current-buffer))))
9768         ;; Prettify the summary buffer line.
9769         (when (gnus-visual-p 'summary-highlight 'highlight)
9770           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9771
9772 (defun gnus-summary-edit-wash (key)
9773   "Perform editing command KEY in the article buffer."
9774   (interactive
9775    (list
9776     (progn
9777       (message "%s" (concat (this-command-keys) "- "))
9778       (read-char))))
9779   (message "")
9780   (gnus-summary-edit-article)
9781   (execute-kbd-macro (concat (this-command-keys) key))
9782   (gnus-article-edit-done))
9783
9784 ;;; Respooling
9785
9786 (defun gnus-summary-respool-query (&optional silent trace)
9787   "Query where the respool algorithm would put this article."
9788   (interactive)
9789   (let (gnus-mark-article-hook)
9790     (gnus-summary-select-article)
9791     (save-excursion
9792       (set-buffer gnus-original-article-buffer)
9793       (let ((groups (nnmail-article-group 'identity trace)))
9794         (unless silent
9795           (if groups
9796               (message "This message would go to %s"
9797                        (mapconcat 'car groups ", "))
9798             (message "This message would go to no groups"))
9799           groups)))))
9800
9801 (defun gnus-summary-respool-trace ()
9802   "Trace where the respool algorithm would put this article.
9803 Display a buffer showing all fancy splitting patterns which matched."
9804   (interactive)
9805   (gnus-summary-respool-query nil t))
9806
9807 ;; Summary marking commands.
9808
9809 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9810   "Mark articles which has the same subject as read, and then select the next.
9811 If UNMARK is positive, remove any kind of mark.
9812 If UNMARK is negative, tick articles."
9813   (interactive "P")
9814   (when unmark
9815     (setq unmark (prefix-numeric-value unmark)))
9816   (let ((count
9817          (gnus-summary-mark-same-subject
9818           (gnus-summary-article-subject) unmark)))
9819     ;; Select next unread article.  If auto-select-same mode, should
9820     ;; select the first unread article.
9821     (gnus-summary-next-article t (and gnus-auto-select-same
9822                                       (gnus-summary-article-subject)))
9823     (gnus-message 7 "%d article%s marked as %s"
9824                   count (if (= count 1) " is" "s are")
9825                   (if unmark "unread" "read"))))
9826
9827 (defun gnus-summary-kill-same-subject (&optional unmark)
9828   "Mark articles which has the same subject as read.
9829 If UNMARK is positive, remove any kind of mark.
9830 If UNMARK is negative, tick articles."
9831   (interactive "P")
9832   (when unmark
9833     (setq unmark (prefix-numeric-value unmark)))
9834   (let ((count
9835          (gnus-summary-mark-same-subject
9836           (gnus-summary-article-subject) unmark)))
9837     ;; If marked as read, go to next unread subject.
9838     (when (null unmark)
9839       ;; Go to next unread subject.
9840       (gnus-summary-next-subject 1 t))
9841     (gnus-message 7 "%d articles are marked as %s"
9842                   count (if unmark "unread" "read"))))
9843
9844 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9845   "Mark articles with same SUBJECT as read, and return marked number.
9846 If optional argument UNMARK is positive, remove any kinds of marks.
9847 If optional argument UNMARK is negative, mark articles as unread instead."
9848   (let ((count 1))
9849     (save-excursion
9850       (cond
9851        ((null unmark)                   ; Mark as read.
9852         (while (and
9853                 (progn
9854                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9855                   (gnus-summary-show-thread) t)
9856                 (gnus-summary-find-subject subject))
9857           (setq count (1+ count))))
9858        ((> unmark 0)                    ; Tick.
9859         (while (and
9860                 (progn
9861                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9862                   (gnus-summary-show-thread) t)
9863                 (gnus-summary-find-subject subject))
9864           (setq count (1+ count))))
9865        (t                               ; Mark as unread.
9866         (while (and
9867                 (progn
9868                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9869                   (gnus-summary-show-thread) t)
9870                 (gnus-summary-find-subject subject))
9871           (setq count (1+ count)))))
9872       (gnus-set-mode-line 'summary)
9873       ;; Return the number of marked articles.
9874       count)))
9875
9876 (defun gnus-summary-mark-as-processable (n &optional unmark)
9877   "Set the process mark on the next N articles.
9878 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9879 the process mark instead.  The difference between N and the actual
9880 number of articles marked is returned."
9881   (interactive "P")
9882   (if (and (null n) (gnus-region-active-p))
9883       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9884     (setq n (prefix-numeric-value n))
9885     (let ((backward (< n 0))
9886           (n (abs n)))
9887       (while (and
9888               (> n 0)
9889               (if unmark
9890                   (gnus-summary-remove-process-mark
9891                    (gnus-summary-article-number))
9892                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9893               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9894         (setq n (1- n)))
9895       (when (/= 0 n)
9896         (gnus-message 7 "No more articles"))
9897       (gnus-summary-recenter)
9898       (gnus-summary-position-point)
9899       n)))
9900
9901 (defun gnus-summary-unmark-as-processable (n)
9902   "Remove the process mark from the next N articles.
9903 If N is negative, unmark backward instead.  The difference between N and
9904 the actual number of articles unmarked is returned."
9905   (interactive "P")
9906   (gnus-summary-mark-as-processable n t))
9907
9908 (defun gnus-summary-unmark-all-processable ()
9909   "Remove the process mark from all articles."
9910   (interactive)
9911   (save-excursion
9912     (while gnus-newsgroup-processable
9913       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9914   (gnus-summary-position-point))
9915
9916 (defun gnus-summary-add-mark (article type)
9917   "Mark ARTICLE with a mark of TYPE."
9918   (let ((vtype (car (assq type gnus-article-mark-lists)))
9919         var)
9920     (if (not vtype)
9921         (error "No such mark type: %s" type)
9922       (setq var (intern (format "gnus-newsgroup-%s" type)))
9923       (set var (cons article (symbol-value var)))
9924       (if (memq type '(processable cached replied forwarded recent saved))
9925           (gnus-summary-update-secondary-mark article)
9926         ;;; !!! This is bogus.  We should find out what primary
9927         ;;; !!! mark we want to set.
9928         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9929
9930 (defun gnus-summary-mark-as-expirable (n)
9931   "Mark N articles forward as expirable.
9932 If N is negative, mark backward instead.  The difference between N and
9933 the actual number of articles marked is returned."
9934   (interactive "p")
9935   (gnus-summary-mark-forward n gnus-expirable-mark))
9936
9937 (defun gnus-summary-mark-as-spam (n)
9938   "Mark N articles forward as spam.
9939 If N is negative, mark backward instead.  The difference between N and
9940 the actual number of articles marked is returned."
9941   (interactive "p")
9942   (gnus-summary-mark-forward n gnus-spam-mark))
9943
9944 (defun gnus-summary-mark-article-as-replied (article)
9945   "Mark ARTICLE as replied to and update the summary line.
9946 ARTICLE can also be a list of articles."
9947   (interactive (list (gnus-summary-article-number)))
9948   (let ((articles (if (listp article) article (list article))))
9949     (dolist (article articles)
9950       (unless (numberp article)
9951         (error "%s is not a number" article))
9952       (push article gnus-newsgroup-replied)
9953       (let ((buffer-read-only nil))
9954         (when (gnus-summary-goto-subject article nil t)
9955           (gnus-summary-update-secondary-mark article))))))
9956
9957 (defun gnus-summary-mark-article-as-forwarded (article)
9958   "Mark ARTICLE as forwarded and update the summary line.
9959 ARTICLE can also be a list of articles."
9960   (let ((articles (if (listp article) article (list article))))
9961     (dolist (article articles)
9962       (push article gnus-newsgroup-forwarded)
9963       (let ((buffer-read-only nil))
9964         (when (gnus-summary-goto-subject article nil t)
9965           (gnus-summary-update-secondary-mark article))))))
9966
9967 (defun gnus-summary-set-bookmark (article)
9968   "Set a bookmark in current article."
9969   (interactive (list (gnus-summary-article-number)))
9970   (when (or (not (get-buffer gnus-article-buffer))
9971             (not gnus-current-article)
9972             (not gnus-article-current)
9973             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9974     (error "No current article selected"))
9975   ;; Remove old bookmark, if one exists.
9976   (gnus-pull article gnus-newsgroup-bookmarks)
9977   ;; Set the new bookmark, which is on the form
9978   ;; (article-number . line-number-in-body).
9979   (push
9980    (cons article
9981          (with-current-buffer gnus-article-buffer
9982            (count-lines
9983             (min (point)
9984                  (save-excursion
9985                    (article-goto-body)
9986                    (point)))
9987             (point))))
9988    gnus-newsgroup-bookmarks)
9989   (gnus-message 6 "A bookmark has been added to the current article."))
9990
9991 (defun gnus-summary-remove-bookmark (article)
9992   "Remove the bookmark from the current article."
9993   (interactive (list (gnus-summary-article-number)))
9994   ;; Remove old bookmark, if one exists.
9995   (if (not (assq article gnus-newsgroup-bookmarks))
9996       (gnus-message 6 "No bookmark in current article.")
9997     (gnus-pull article gnus-newsgroup-bookmarks)
9998     (gnus-message 6 "Removed bookmark.")))
9999
10000 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10001 (defun gnus-summary-mark-as-dormant (n)
10002   "Mark N articles forward as dormant.
10003 If N is negative, mark backward instead.  The difference between N and
10004 the actual number of articles marked is returned."
10005   (interactive "p")
10006   (gnus-summary-mark-forward n gnus-dormant-mark))
10007
10008 (defun gnus-summary-set-process-mark (article)
10009   "Set the process mark on ARTICLE and update the summary line."
10010   (setq gnus-newsgroup-processable
10011         (cons article
10012               (delq article gnus-newsgroup-processable)))
10013   (when (gnus-summary-goto-subject article)
10014     (gnus-summary-show-thread)
10015     (gnus-summary-goto-subject article)
10016     (gnus-summary-update-secondary-mark article)))
10017
10018 (defun gnus-summary-remove-process-mark (&rest articles)
10019   "Remove the process mark from ARTICLES and update the summary line."
10020   (dolist (article articles)
10021     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10022     (when (gnus-summary-goto-subject article)
10023       (gnus-summary-show-thread)
10024       (gnus-summary-goto-subject article)
10025       (gnus-summary-update-secondary-mark article))))
10026
10027 (defun gnus-summary-set-saved-mark (article)
10028   "Set the process mark on ARTICLE and update the summary line."
10029   (push article gnus-newsgroup-saved)
10030   (when (gnus-summary-goto-subject article)
10031     (gnus-summary-update-secondary-mark article)))
10032
10033 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10034   "Mark N articles as read forwards.
10035 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10036 The difference between N and the actual number of articles marked is
10037 returned.
10038 If NO-EXPIRE, auto-expiry will be inhibited."
10039   (interactive "p")
10040   (gnus-summary-show-thread)
10041   (let ((backward (< n 0))
10042         (gnus-summary-goto-unread
10043          (and gnus-summary-goto-unread
10044               (not (eq gnus-summary-goto-unread 'never))
10045               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10046                                     gnus-ticked-mark gnus-dormant-mark)))))
10047         (n (abs n))
10048         (mark (or mark gnus-del-mark)))
10049     (while (and (> n 0)
10050                 (gnus-summary-mark-article nil mark no-expire)
10051                 (zerop (gnus-summary-next-subject
10052                         (if backward -1 1)
10053                         (and gnus-summary-goto-unread
10054                              (not (eq gnus-summary-goto-unread 'never)))
10055                         t)))
10056       (setq n (1- n)))
10057     (when (/= 0 n)
10058       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10059     (gnus-summary-recenter)
10060     (gnus-summary-position-point)
10061     (gnus-set-mode-line 'summary)
10062     n))
10063
10064 (defun gnus-summary-mark-article-as-read (mark)
10065   "Mark the current article quickly as read with MARK."
10066   (let ((article (gnus-summary-article-number)))
10067     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10068     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10069     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10070     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10071     (push (cons article mark) gnus-newsgroup-reads)
10072     ;; Possibly remove from cache, if that is used.
10073     (when gnus-use-cache
10074       (gnus-cache-enter-remove-article article))
10075     ;; Allow the backend to change the mark.
10076     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10077     ;; Check for auto-expiry.
10078     (when (and gnus-newsgroup-auto-expire
10079                (memq mark gnus-auto-expirable-marks))
10080       (setq mark gnus-expirable-mark)
10081       ;; Let the backend know about the mark change.
10082       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10083       (push article gnus-newsgroup-expirable))
10084     ;; Set the mark in the buffer.
10085     (gnus-summary-update-mark mark 'unread)
10086     t))
10087
10088 (defun gnus-summary-mark-article-as-unread (mark)
10089   "Mark the current article quickly as unread with MARK."
10090   (let* ((article (gnus-summary-article-number))
10091          (old-mark (gnus-summary-article-mark article)))
10092     ;; Allow the backend to change the mark.
10093     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10094     (if (eq mark old-mark)
10095         t
10096       (if (<= article 0)
10097           (progn
10098             (gnus-error 1 "Can't mark negative article numbers")
10099             nil)
10100         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10101         (setq gnus-newsgroup-spam-marked
10102               (delq article gnus-newsgroup-spam-marked))
10103         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10104         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10105         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10106         (cond ((= mark gnus-ticked-mark)
10107                (setq gnus-newsgroup-marked
10108                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10109                                               article)))
10110               ((= mark gnus-spam-mark)
10111                (setq gnus-newsgroup-spam-marked
10112                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10113                                               article)))
10114               ((= mark gnus-dormant-mark)
10115                (setq gnus-newsgroup-dormant
10116                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10117                                               article)))
10118               (t
10119                (setq gnus-newsgroup-unreads
10120                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10121                                               article))))
10122         (gnus-pull article gnus-newsgroup-reads)
10123
10124         ;; See whether the article is to be put in the cache.
10125         (and gnus-use-cache
10126              (vectorp (gnus-summary-article-header article))
10127              (save-excursion
10128                (gnus-cache-possibly-enter-article
10129                 gnus-newsgroup-name article
10130                 (= mark gnus-ticked-mark)
10131                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10132
10133         ;; Fix the mark.
10134         (gnus-summary-update-mark mark 'unread)
10135         t))))
10136
10137 (defun gnus-summary-mark-article (&optional article mark no-expire)
10138   "Mark ARTICLE with MARK.  MARK can be any character.
10139 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10140 `??' (dormant) and `?E' (expirable).
10141 If MARK is nil, then the default character `?r' is used.
10142 If ARTICLE is nil, then the article on the current line will be
10143 marked.
10144 If NO-EXPIRE, auto-expiry will be inhibited."
10145   ;; The mark might be a string.
10146   (when (stringp mark)
10147     (setq mark (aref mark 0)))
10148   ;; If no mark is given, then we check auto-expiring.
10149   (when (null mark)
10150     (setq mark gnus-del-mark))
10151   (when (and (not no-expire)
10152              gnus-newsgroup-auto-expire
10153              (memq mark gnus-auto-expirable-marks))
10154     (setq mark gnus-expirable-mark))
10155   (let ((article (or article (gnus-summary-article-number)))
10156         (old-mark (gnus-summary-article-mark article)))
10157     ;; Allow the backend to change the mark.
10158     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10159     (if (eq mark old-mark)
10160         t
10161       (unless article
10162         (error "No article on current line"))
10163       (if (not (if (or (= mark gnus-unread-mark)
10164                        (= mark gnus-ticked-mark)
10165                        (= mark gnus-spam-mark)
10166                        (= mark gnus-dormant-mark))
10167                    (gnus-mark-article-as-unread article mark)
10168                  (gnus-mark-article-as-read article mark)))
10169           t
10170         ;; See whether the article is to be put in the cache.
10171         (and gnus-use-cache
10172              (not (= mark gnus-canceled-mark))
10173              (vectorp (gnus-summary-article-header article))
10174              (save-excursion
10175                (gnus-cache-possibly-enter-article
10176                 gnus-newsgroup-name article
10177                 (= mark gnus-ticked-mark)
10178                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10179
10180         (when (gnus-summary-goto-subject article nil t)
10181           (let ((buffer-read-only nil))
10182             (gnus-summary-show-thread)
10183             ;; Fix the mark.
10184             (gnus-summary-update-mark mark 'unread)
10185             t))))))
10186
10187 (defun gnus-summary-update-secondary-mark (article)
10188   "Update the secondary (read, process, cache) mark."
10189   (gnus-summary-update-mark
10190    (cond ((memq article gnus-newsgroup-processable)
10191           gnus-process-mark)
10192          ((memq article gnus-newsgroup-cached)
10193           gnus-cached-mark)
10194          ((memq article gnus-newsgroup-replied)
10195           gnus-replied-mark)
10196          ((memq article gnus-newsgroup-forwarded)
10197           gnus-forwarded-mark)
10198          ((memq article gnus-newsgroup-saved)
10199           gnus-saved-mark)
10200          ((memq article gnus-newsgroup-recent)
10201           gnus-recent-mark)
10202          ((memq article gnus-newsgroup-unseen)
10203           gnus-unseen-mark)
10204          (t gnus-no-mark))
10205    'replied)
10206   (when (gnus-visual-p 'summary-highlight 'highlight)
10207     (gnus-run-hooks 'gnus-summary-update-hook))
10208   t)
10209
10210 (defun gnus-summary-update-download-mark (article)
10211   "Update the download mark."
10212   (gnus-summary-update-mark
10213    (cond ((memq article gnus-newsgroup-undownloaded)
10214           gnus-undownloaded-mark)
10215          (gnus-newsgroup-agentized
10216           gnus-downloaded-mark)
10217          (t
10218           gnus-no-mark))
10219    'download)
10220   (gnus-summary-update-line t)
10221   t)
10222
10223 (defun gnus-summary-update-mark (mark type)
10224   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10225         (buffer-read-only nil))
10226     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10227     (when forward
10228       (when (looking-at "\r")
10229         (incf forward))
10230       (when (<= (+ forward (point)) (point-max))
10231         ;; Go to the right position on the line.
10232         (goto-char (+ forward (point)))
10233         ;; Replace the old mark with the new mark.
10234         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10235         ;; Optionally update the marks by some user rule.
10236         (when (eq type 'unread)
10237           (gnus-data-set-mark
10238            (gnus-data-find (gnus-summary-article-number)) mark)
10239           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10240
10241 (defun gnus-mark-article-as-read (article &optional mark)
10242   "Enter ARTICLE in the pertinent lists and remove it from others."
10243   ;; Make the article expirable.
10244   (let ((mark (or mark gnus-del-mark)))
10245     (setq gnus-newsgroup-expirable
10246           (if (= mark gnus-expirable-mark)
10247               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10248             (delq article gnus-newsgroup-expirable)))
10249     ;; Remove from unread and marked lists.
10250     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10251     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10252     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10253     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10254     (push (cons article mark) gnus-newsgroup-reads)
10255     ;; Possibly remove from cache, if that is used.
10256     (when gnus-use-cache
10257       (gnus-cache-enter-remove-article article))
10258     t))
10259
10260 (defun gnus-mark-article-as-unread (article &optional mark)
10261   "Enter ARTICLE in the pertinent lists and remove it from others."
10262   (let ((mark (or mark gnus-ticked-mark)))
10263     (if (<= article 0)
10264         (progn
10265           (gnus-error 1 "Can't mark negative article numbers")
10266           nil)
10267       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10268             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10269             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10270             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10271             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10272
10273       ;; Unsuppress duplicates?
10274       (when gnus-suppress-duplicates
10275         (gnus-dup-unsuppress-article article))
10276
10277       (cond ((= mark gnus-ticked-mark)
10278              (setq gnus-newsgroup-marked
10279                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10280             ((= mark gnus-spam-mark)
10281              (setq gnus-newsgroup-spam-marked
10282                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10283                                             article)))
10284             ((= mark gnus-dormant-mark)
10285              (setq gnus-newsgroup-dormant
10286                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10287             (t
10288              (setq gnus-newsgroup-unreads
10289                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10290       (gnus-pull article gnus-newsgroup-reads)
10291       t)))
10292
10293 (defalias 'gnus-summary-mark-as-unread-forward
10294   'gnus-summary-tick-article-forward)
10295 (make-obsolete 'gnus-summary-mark-as-unread-forward
10296                'gnus-summary-tick-article-forward)
10297 (defun gnus-summary-tick-article-forward (n)
10298   "Tick N articles forwards.
10299 If N is negative, tick backwards instead.
10300 The difference between N and the number of articles ticked is returned."
10301   (interactive "p")
10302   (gnus-summary-mark-forward n gnus-ticked-mark))
10303
10304 (defalias 'gnus-summary-mark-as-unread-backward
10305   'gnus-summary-tick-article-backward)
10306 (make-obsolete 'gnus-summary-mark-as-unread-backward
10307                'gnus-summary-tick-article-backward)
10308 (defun gnus-summary-tick-article-backward (n)
10309   "Tick N articles backwards.
10310 The difference between N and the number of articles ticked is returned."
10311   (interactive "p")
10312   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10313
10314 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10315 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10316 (defun gnus-summary-tick-article (&optional article clear-mark)
10317   "Mark current article as unread.
10318 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10319 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10320   (interactive)
10321   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10322                                        gnus-ticked-mark)))
10323
10324 (defun gnus-summary-mark-as-read-forward (n)
10325   "Mark N articles as read forwards.
10326 If N is negative, mark backwards instead.
10327 The difference between N and the actual number of articles marked is
10328 returned."
10329   (interactive "p")
10330   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10331
10332 (defun gnus-summary-mark-as-read-backward (n)
10333   "Mark the N articles as read backwards.
10334 The difference between N and the actual number of articles marked is
10335 returned."
10336   (interactive "p")
10337   (gnus-summary-mark-forward
10338    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10339
10340 (defun gnus-summary-mark-as-read (&optional article mark)
10341   "Mark current article as read.
10342 ARTICLE specifies the article to be marked as read.
10343 MARK specifies a string to be inserted at the beginning of the line."
10344   (gnus-summary-mark-article article mark))
10345
10346 (defun gnus-summary-clear-mark-forward (n)
10347   "Clear marks from N articles forward.
10348 If N is negative, clear backward instead.
10349 The difference between N and the number of marks cleared is returned."
10350   (interactive "p")
10351   (gnus-summary-mark-forward n gnus-unread-mark))
10352
10353 (defun gnus-summary-clear-mark-backward (n)
10354   "Clear marks from N articles backward.
10355 The difference between N and the number of marks cleared is returned."
10356   (interactive "p")
10357   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10358
10359 (defun gnus-summary-mark-unread-as-read ()
10360   "Intended to be used by `gnus-summary-mark-article-hook'."
10361   (when (memq gnus-current-article gnus-newsgroup-unreads)
10362     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10363
10364 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10365   "Intended to be used by `gnus-summary-mark-article-hook'."
10366   (let ((mark (gnus-summary-article-mark)))
10367     (when (or (gnus-unread-mark-p mark)
10368               (gnus-read-mark-p mark))
10369       (gnus-summary-mark-article gnus-current-article
10370                                  (or new-mark gnus-read-mark)))))
10371
10372 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10373   "Intended to be used by `gnus-summary-mark-article-hook'."
10374   (let ((mark (gnus-summary-article-mark)))
10375     (when (or (gnus-unread-mark-p mark)
10376               (gnus-read-mark-p mark))
10377       (gnus-summary-mark-article (gnus-summary-article-number)
10378                                  (or new-mark gnus-read-mark)))))
10379
10380 (defun gnus-summary-mark-unread-as-ticked ()
10381   "Intended to be used by `gnus-summary-mark-article-hook'."
10382   (when (memq gnus-current-article gnus-newsgroup-unreads)
10383     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10384
10385 (defun gnus-summary-mark-region-as-read (point mark all)
10386   "Mark all unread articles between point and mark as read.
10387 If given a prefix, mark all articles between point and mark as read,
10388 even ticked and dormant ones."
10389   (interactive "r\nP")
10390   (save-excursion
10391     (let (article)
10392       (goto-char point)
10393       (beginning-of-line)
10394       (while (and
10395               (< (point) mark)
10396               (progn
10397                 (when (or all
10398                           (memq (setq article (gnus-summary-article-number))
10399                                 gnus-newsgroup-unreads))
10400                   (gnus-summary-mark-article article gnus-del-mark))
10401                 t)
10402               (gnus-summary-find-next))))))
10403
10404 (defun gnus-summary-mark-below (score mark)
10405   "Mark articles with score less than SCORE with MARK."
10406   (interactive "P\ncMark: ")
10407   (setq score (if score
10408                   (prefix-numeric-value score)
10409                 (or gnus-summary-default-score 0)))
10410   (save-excursion
10411     (set-buffer gnus-summary-buffer)
10412     (goto-char (point-min))
10413     (while
10414         (progn
10415           (and (< (gnus-summary-article-score) score)
10416                (gnus-summary-mark-article nil mark))
10417           (gnus-summary-find-next)))))
10418
10419 (defun gnus-summary-kill-below (&optional score)
10420   "Mark articles with score below SCORE as read."
10421   (interactive "P")
10422   (gnus-summary-mark-below score gnus-killed-mark))
10423
10424 (defun gnus-summary-clear-above (&optional score)
10425   "Clear all marks from articles with score above SCORE."
10426   (interactive "P")
10427   (gnus-summary-mark-above score gnus-unread-mark))
10428
10429 (defun gnus-summary-tick-above (&optional score)
10430   "Tick all articles with score above SCORE."
10431   (interactive "P")
10432   (gnus-summary-mark-above score gnus-ticked-mark))
10433
10434 (defun gnus-summary-mark-above (score mark)
10435   "Mark articles with score over SCORE with MARK."
10436   (interactive "P\ncMark: ")
10437   (setq score (if score
10438                   (prefix-numeric-value score)
10439                 (or gnus-summary-default-score 0)))
10440   (save-excursion
10441     (set-buffer gnus-summary-buffer)
10442     (goto-char (point-min))
10443     (while (and (progn
10444                   (when (> (gnus-summary-article-score) score)
10445                     (gnus-summary-mark-article nil mark))
10446                   t)
10447                 (gnus-summary-find-next)))))
10448
10449 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10450 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10451 (defun gnus-summary-limit-include-expunged (&optional no-error)
10452   "Display all the hidden articles that were expunged for low scores."
10453   (interactive)
10454   (let ((buffer-read-only nil))
10455     (let ((scored gnus-newsgroup-scored)
10456           headers h)
10457       (while scored
10458         (unless (gnus-summary-article-header (caar scored))
10459           (and (setq h (gnus-number-to-header (caar scored)))
10460                (< (cdar scored) gnus-summary-expunge-below)
10461                (push h headers)))
10462         (setq scored (cdr scored)))
10463       (if (not headers)
10464           (when (not no-error)
10465             (error "No expunged articles hidden"))
10466         (goto-char (point-min))
10467         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10468         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10469         (mapcar (lambda (x) (push (mail-header-number x)
10470                                   gnus-newsgroup-limit))
10471                 headers)
10472         (gnus-summary-prepare-unthreaded (nreverse headers))
10473         (goto-char (point-min))
10474         (gnus-summary-position-point)
10475         t))))
10476
10477 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10478   "Mark all unread articles in this newsgroup as read.
10479 If prefix argument ALL is non-nil, ticked and dormant articles will
10480 also be marked as read.
10481 If QUIETLY is non-nil, no questions will be asked.
10482
10483 If TO-HERE is non-nil, it should be a point in the buffer.  All
10484 articles before (after, if REVERSE is set) this point will be marked
10485 as read.
10486
10487 Note that this function will only catch up the unread article
10488 in the current summary buffer limitation.
10489
10490 The number of articles marked as read is returned."
10491   (interactive "P")
10492   (prog1
10493       (save-excursion
10494         (when (or quietly
10495                   (not gnus-interactive-catchup) ;Without confirmation?
10496                   gnus-expert-user
10497                   (gnus-y-or-n-p
10498                    (if all
10499                        "Mark absolutely all articles as read? "
10500                      "Mark all unread articles as read? ")))
10501           (if (and not-mark
10502                    (not gnus-newsgroup-adaptive)
10503                    (not gnus-newsgroup-auto-expire)
10504                    (not gnus-suppress-duplicates)
10505                    (or (not gnus-use-cache)
10506                        (eq gnus-use-cache 'passive)))
10507               (progn
10508                 (when all
10509                   (setq gnus-newsgroup-marked nil
10510                         gnus-newsgroup-spam-marked nil
10511                         gnus-newsgroup-dormant nil))
10512                 (setq gnus-newsgroup-unreads
10513                       (gnus-sorted-nunion
10514                        (gnus-intersection gnus-newsgroup-unreads
10515                                           gnus-newsgroup-downloadable)
10516                        gnus-newsgroup-unfetched)))
10517             ;; We actually mark all articles as canceled, which we
10518             ;; have to do when using auto-expiry or adaptive scoring.
10519             (gnus-summary-show-all-threads)
10520             (if (and to-here reverse)
10521                 (progn
10522                   (goto-char to-here)
10523                   (gnus-summary-mark-current-read-and-unread-as-read
10524                    gnus-catchup-mark)
10525                   (while (gnus-summary-find-next (not all))
10526                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10527               (when (gnus-summary-first-subject (not all))
10528                 (while (and
10529                         (if to-here (< (point) to-here) t)
10530                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10531                         (gnus-summary-find-next (not all))))))
10532             (gnus-set-mode-line 'summary))
10533           t))
10534     (gnus-summary-position-point)))
10535
10536 (defun gnus-summary-catchup-to-here (&optional all)
10537   "Mark all unticked articles before the current one as read.
10538 If ALL is non-nil, also mark ticked and dormant articles as read."
10539   (interactive "P")
10540   (save-excursion
10541     (gnus-save-hidden-threads
10542       (let ((beg (point)))
10543         ;; We check that there are unread articles.
10544         (when (or all (gnus-summary-find-prev))
10545           (gnus-summary-catchup all t beg)))))
10546   (gnus-summary-position-point))
10547
10548 (defun gnus-summary-catchup-from-here (&optional all)
10549   "Mark all unticked articles after (and including) the current one as read.
10550 If ALL is non-nil, also mark ticked and dormant articles as read."
10551   (interactive "P")
10552   (save-excursion
10553     (gnus-save-hidden-threads
10554       (let ((beg (point)))
10555         ;; We check that there are unread articles.
10556         (when (or all (gnus-summary-find-next))
10557           (gnus-summary-catchup all t beg nil t)))))
10558   (gnus-summary-position-point))
10559
10560 (defun gnus-summary-catchup-all (&optional quietly)
10561   "Mark all articles in this newsgroup as read.
10562 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10563 instead, which marks only unread articles as read."
10564   (interactive "P")
10565   (gnus-summary-catchup t quietly))
10566
10567 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10568   "Mark all unread articles in this group as read, then exit.
10569 If prefix argument ALL is non-nil, all articles are marked as read.
10570 If QUIETLY is non-nil, no questions will be asked."
10571   (interactive "P")
10572   (when (gnus-summary-catchup all quietly nil 'fast)
10573     ;; Select next newsgroup or exit.
10574     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10575              (eq gnus-auto-select-next 'quietly))
10576         (gnus-summary-next-group nil)
10577       (gnus-summary-exit))))
10578
10579 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10580   "Mark all articles in this newsgroup as read, and then exit.
10581 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10582 instead, which marks only unread articles as read."
10583   (interactive "P")
10584   (gnus-summary-catchup-and-exit t quietly))
10585
10586 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10587   "Mark all articles in this group as read and select the next group.
10588 If given a prefix, mark all articles, unread as well as ticked, as
10589 read."
10590   (interactive "P")
10591   (save-excursion
10592     (gnus-summary-catchup all))
10593   (gnus-summary-next-group))
10594
10595 ;;;
10596 ;;; with article
10597 ;;;
10598
10599 (defmacro gnus-with-article (article &rest forms)
10600   "Select ARTICLE and perform FORMS in the original article buffer.
10601 Then replace the article with the result."
10602   `(progn
10603      ;; We don't want the article to be marked as read.
10604      (let (gnus-mark-article-hook)
10605        (gnus-summary-select-article t t nil ,article))
10606      (set-buffer gnus-original-article-buffer)
10607      ,@forms
10608      (if (not (gnus-check-backend-function
10609                'request-replace-article (car gnus-article-current)))
10610          (gnus-message 5 "Read-only group; not replacing")
10611        (unless (gnus-request-replace-article
10612                 ,article (car gnus-article-current)
10613                 (current-buffer) t)
10614          (error "Couldn't replace article")))
10615      ;; The cache and backlog have to be flushed somewhat.
10616      (when gnus-keep-backlog
10617        (gnus-backlog-remove-article
10618         (car gnus-article-current) (cdr gnus-article-current)))
10619      (when gnus-use-cache
10620        (gnus-cache-update-article
10621         (car gnus-article-current) (cdr gnus-article-current)))))
10622
10623 (put 'gnus-with-article 'lisp-indent-function 1)
10624 (put 'gnus-with-article 'edebug-form-spec '(form body))
10625
10626 ;; Thread-based commands.
10627
10628 (defun gnus-summary-articles-in-thread (&optional article)
10629   "Return a list of all articles in the current thread.
10630 If ARTICLE is non-nil, return all articles in the thread that starts
10631 with that article."
10632   (let* ((article (or article (gnus-summary-article-number)))
10633          (data (gnus-data-find-list article))
10634          (top-level (gnus-data-level (car data)))
10635          (top-subject
10636           (cond ((null gnus-thread-operation-ignore-subject)
10637                  (gnus-simplify-subject-re
10638                   (mail-header-subject (gnus-data-header (car data)))))
10639                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10640                  (gnus-simplify-subject-fuzzy
10641                   (mail-header-subject (gnus-data-header (car data)))))
10642                 (t nil)))
10643          (end-point (save-excursion
10644                       (if (gnus-summary-go-to-next-thread)
10645                           (point) (point-max))))
10646          articles)
10647     (while (and data
10648                 (< (gnus-data-pos (car data)) end-point))
10649       (when (or (not top-subject)
10650                 (string= top-subject
10651                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10652                              (gnus-simplify-subject-fuzzy
10653                               (mail-header-subject
10654                                (gnus-data-header (car data))))
10655                            (gnus-simplify-subject-re
10656                             (mail-header-subject
10657                              (gnus-data-header (car data)))))))
10658         (push (gnus-data-number (car data)) articles))
10659       (unless (and (setq data (cdr data))
10660                    (> (gnus-data-level (car data)) top-level))
10661         (setq data nil)))
10662     ;; Return the list of articles.
10663     (nreverse articles)))
10664
10665 (defun gnus-summary-rethread-current ()
10666   "Rethread the thread the current article is part of."
10667   (interactive)
10668   (let* ((gnus-show-threads t)
10669          (article (gnus-summary-article-number))
10670          (id (mail-header-id (gnus-summary-article-header)))
10671          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10672     (unless id
10673       (error "No article on the current line"))
10674     (gnus-rebuild-thread id)
10675     (gnus-summary-goto-subject article)))
10676
10677 (defun gnus-summary-reparent-thread ()
10678   "Make the current article child of the marked (or previous) article.
10679
10680 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10681 is non-nil or the Subject: of both articles are the same."
10682   (interactive)
10683   (unless (not (gnus-group-read-only-p))
10684     (error "The current newsgroup does not support article editing"))
10685   (unless (<= (length gnus-newsgroup-processable) 1)
10686     (error "No more than one article may be marked"))
10687   (save-window-excursion
10688     (let ((gnus-article-buffer " *reparent*")
10689           (current-article (gnus-summary-article-number))
10690           ;; First grab the marked article, otherwise one line up.
10691           (parent-article (if (not (null gnus-newsgroup-processable))
10692                               (car gnus-newsgroup-processable)
10693                             (save-excursion
10694                               (if (eq (forward-line -1) 0)
10695                                   (gnus-summary-article-number)
10696                                 (error "Beginning of summary buffer"))))))
10697       (unless (not (eq current-article parent-article))
10698         (error "An article may not be self-referential"))
10699       (let ((message-id (mail-header-id
10700                          (gnus-summary-article-header parent-article))))
10701         (unless (and message-id (not (equal message-id "")))
10702           (error "No message-id in desired parent"))
10703         (gnus-with-article current-article
10704           (save-restriction
10705             (goto-char (point-min))
10706             (message-narrow-to-head)
10707             (if (re-search-forward "^References: " nil t)
10708                 (progn
10709                   (re-search-forward "^[^ \t]" nil t)
10710                   (forward-line -1)
10711                   (end-of-line)
10712                   (insert " " message-id))
10713               (insert "References: " message-id "\n"))))
10714         (set-buffer gnus-summary-buffer)
10715         (gnus-summary-unmark-all-processable)
10716         (gnus-summary-update-article current-article)
10717         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10718             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10719         (gnus-summary-rethread-current)
10720         (gnus-message 3 "Article %d is now the child of article %d"
10721                       current-article parent-article)))))
10722
10723 (defun gnus-summary-toggle-threads (&optional arg)
10724   "Toggle showing conversation threads.
10725 If ARG is positive number, turn showing conversation threads on."
10726   (interactive "P")
10727   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10728     (setq gnus-show-threads
10729           (if (null arg) (not gnus-show-threads)
10730             (> (prefix-numeric-value arg) 0)))
10731     (gnus-summary-prepare)
10732     (gnus-summary-goto-subject current)
10733     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10734     (gnus-summary-position-point)))
10735
10736 (defun gnus-summary-show-all-threads ()
10737   "Show all threads."
10738   (interactive)
10739   (save-excursion
10740     (let ((buffer-read-only nil))
10741       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10742   (gnus-summary-position-point))
10743
10744 (defun gnus-summary-show-thread ()
10745   "Show thread subtrees.
10746 Returns nil if no thread was there to be shown."
10747   (interactive)
10748   (let ((buffer-read-only nil)
10749         (orig (point))
10750         (end (point-at-eol))
10751         ;; Leave point at bol
10752         (beg (progn (beginning-of-line) (point))))
10753     (prog1
10754         ;; Any hidden lines here?
10755         (search-forward "\r" end t)
10756       (subst-char-in-region beg end ?\^M ?\n t)
10757       (goto-char orig)
10758       (gnus-summary-position-point))))
10759
10760 (defun gnus-summary-maybe-hide-threads ()
10761   "If requested, hide the threads that should be hidden."
10762   (when (and gnus-show-threads
10763              gnus-thread-hide-subtree)
10764     (gnus-summary-hide-all-threads
10765      (if (or (consp gnus-thread-hide-subtree)
10766              (functionp gnus-thread-hide-subtree))
10767          (gnus-make-predicate gnus-thread-hide-subtree)
10768        nil))))
10769
10770 ;;; Hiding predicates.
10771
10772 (defun gnus-article-unread-p (header)
10773   (memq (mail-header-number header) gnus-newsgroup-unreads))
10774
10775 (defun gnus-article-unseen-p (header)
10776   (memq (mail-header-number header) gnus-newsgroup-unseen))
10777
10778 (defun gnus-map-articles (predicate articles)
10779   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10780   (apply 'gnus-or (mapcar predicate
10781                           (mapcar 'gnus-summary-article-header articles))))
10782
10783 (defun gnus-summary-hide-all-threads (&optional predicate)
10784   "Hide all thread subtrees.
10785 If PREDICATE is supplied, threads that satisfy this predicate
10786 will not be hidden."
10787   (interactive)
10788   (save-excursion
10789     (goto-char (point-min))
10790     (let ((end nil))
10791       (while (not end)
10792         (when (or (not predicate)
10793                   (gnus-map-articles
10794                    predicate (gnus-summary-article-children)))
10795             (gnus-summary-hide-thread))
10796         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10797   (gnus-summary-position-point))
10798
10799 (defun gnus-summary-hide-thread ()
10800   "Hide thread subtrees.
10801 If PREDICATE is supplied, threads that satisfy this predicate
10802 will not be hidden.
10803 Returns nil if no threads were there to be hidden."
10804   (interactive)
10805   (let ((buffer-read-only nil)
10806         (start (point))
10807         (article (gnus-summary-article-number)))
10808     (goto-char start)
10809     ;; Go forward until either the buffer ends or the subthread
10810     ;; ends.
10811     (when (and (not (eobp))
10812                (or (zerop (gnus-summary-next-thread 1 t))
10813                    (goto-char (point-max))))
10814       (prog1
10815           (if (and (> (point) start)
10816                    (search-backward "\n" start t))
10817               (progn
10818                 (subst-char-in-region start (point) ?\n ?\^M)
10819                 (gnus-summary-goto-subject article))
10820             (goto-char start)
10821             nil)))))
10822
10823 (defun gnus-summary-go-to-next-thread (&optional previous)
10824   "Go to the same level (or less) next thread.
10825 If PREVIOUS is non-nil, go to previous thread instead.
10826 Return the article number moved to, or nil if moving was impossible."
10827   (let ((level (gnus-summary-thread-level))
10828         (way (if previous -1 1))
10829         (beg (point)))
10830     (forward-line way)
10831     (while (and (not (eobp))
10832                 (< level (gnus-summary-thread-level)))
10833       (forward-line way))
10834     (if (eobp)
10835         (progn
10836           (goto-char beg)
10837           nil)
10838       (setq beg (point))
10839       (prog1
10840           (gnus-summary-article-number)
10841         (goto-char beg)))))
10842
10843 (defun gnus-summary-next-thread (n &optional silent)
10844   "Go to the same level next N'th thread.
10845 If N is negative, search backward instead.
10846 Returns the difference between N and the number of skips actually
10847 done.
10848
10849 If SILENT, don't output messages."
10850   (interactive "p")
10851   (let ((backward (< n 0))
10852         (n (abs n)))
10853     (while (and (> n 0)
10854                 (gnus-summary-go-to-next-thread backward))
10855       (decf n))
10856     (unless silent
10857       (gnus-summary-position-point))
10858     (when (and (not silent) (/= 0 n))
10859       (gnus-message 7 "No more threads"))
10860     n))
10861
10862 (defun gnus-summary-prev-thread (n)
10863   "Go to the same level previous N'th thread.
10864 Returns the difference between N and the number of skips actually
10865 done."
10866   (interactive "p")
10867   (gnus-summary-next-thread (- n)))
10868
10869 (defun gnus-summary-go-down-thread ()
10870   "Go down one level in the current thread."
10871   (let ((children (gnus-summary-article-children)))
10872     (when children
10873       (gnus-summary-goto-subject (car children)))))
10874
10875 (defun gnus-summary-go-up-thread ()
10876   "Go up one level in the current thread."
10877   (let ((parent (gnus-summary-article-parent)))
10878     (when parent
10879       (gnus-summary-goto-subject parent))))
10880
10881 (defun gnus-summary-down-thread (n)
10882   "Go down thread N steps.
10883 If N is negative, go up instead.
10884 Returns the difference between N and how many steps down that were
10885 taken."
10886   (interactive "p")
10887   (let ((up (< n 0))
10888         (n (abs n)))
10889     (while (and (> n 0)
10890                 (if up (gnus-summary-go-up-thread)
10891                   (gnus-summary-go-down-thread)))
10892       (setq n (1- n)))
10893     (gnus-summary-position-point)
10894     (when (/= 0 n)
10895       (gnus-message 7 "Can't go further"))
10896     n))
10897
10898 (defun gnus-summary-up-thread (n)
10899   "Go up thread N steps.
10900 If N is negative, go down instead.
10901 Returns the difference between N and how many steps down that were
10902 taken."
10903   (interactive "p")
10904   (gnus-summary-down-thread (- n)))
10905
10906 (defun gnus-summary-top-thread ()
10907   "Go to the top of the thread."
10908   (interactive)
10909   (while (gnus-summary-go-up-thread))
10910   (gnus-summary-article-number))
10911
10912 (defun gnus-summary-kill-thread (&optional unmark)
10913   "Mark articles under current thread as read.
10914 If the prefix argument is positive, remove any kinds of marks.
10915 If the prefix argument is negative, tick articles instead."
10916   (interactive "P")
10917   (when unmark
10918     (setq unmark (prefix-numeric-value unmark)))
10919   (let ((articles (gnus-summary-articles-in-thread)))
10920     (save-excursion
10921       ;; Expand the thread.
10922       (gnus-summary-show-thread)
10923       ;; Mark all the articles.
10924       (while articles
10925         (gnus-summary-goto-subject (car articles))
10926         (cond ((null unmark)
10927                (gnus-summary-mark-article-as-read gnus-killed-mark))
10928               ((> unmark 0)
10929                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10930               (t
10931                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10932         (setq articles (cdr articles))))
10933     ;; Hide killed subtrees.
10934     (and (null unmark)
10935          gnus-thread-hide-killed
10936          (gnus-summary-hide-thread))
10937     ;; If marked as read, go to next unread subject.
10938     (when (null unmark)
10939       ;; Go to next unread subject.
10940       (gnus-summary-next-subject 1 t)))
10941   (gnus-set-mode-line 'summary))
10942
10943 ;; Summary sorting commands
10944
10945 (defun gnus-summary-sort-by-number (&optional reverse)
10946   "Sort the summary buffer by article number.
10947 Argument REVERSE means reverse order."
10948   (interactive "P")
10949   (gnus-summary-sort 'number reverse))
10950
10951 (defun gnus-summary-sort-by-random (&optional reverse)
10952   "Randomize the order in the summary buffer.
10953 Argument REVERSE means to randomize in reverse order."
10954   (interactive "P")
10955   (gnus-summary-sort 'random reverse))
10956
10957 (defun gnus-summary-sort-by-author (&optional reverse)
10958   "Sort the summary buffer by author name alphabetically.
10959 If `case-fold-search' is non-nil, case of letters is ignored.
10960 Argument REVERSE means reverse order."
10961   (interactive "P")
10962   (gnus-summary-sort 'author reverse))
10963
10964 (defun gnus-summary-sort-by-recipient (&optional reverse)
10965   "Sort the summary buffer by recipient name alphabetically.
10966 If `case-fold-search' is non-nil, case of letters is ignored.
10967 Argument REVERSE means reverse order."
10968   (interactive "P")
10969   (gnus-summary-sort 'recipient reverse))
10970
10971 (defun gnus-summary-sort-by-subject (&optional reverse)
10972   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10973 If `case-fold-search' is non-nil, case of letters is ignored.
10974 Argument REVERSE means reverse order."
10975   (interactive "P")
10976   (gnus-summary-sort 'subject reverse))
10977
10978 (defun gnus-summary-sort-by-date (&optional reverse)
10979   "Sort the summary buffer by date.
10980 Argument REVERSE means reverse order."
10981   (interactive "P")
10982   (gnus-summary-sort 'date reverse))
10983
10984 (defun gnus-summary-sort-by-score (&optional reverse)
10985   "Sort the summary buffer by score.
10986 Argument REVERSE means reverse order."
10987   (interactive "P")
10988   (gnus-summary-sort 'score reverse))
10989
10990 (defun gnus-summary-sort-by-lines (&optional reverse)
10991   "Sort the summary buffer by the number of lines.
10992 Argument REVERSE means reverse order."
10993   (interactive "P")
10994   (gnus-summary-sort 'lines reverse))
10995
10996 (defun gnus-summary-sort-by-chars (&optional reverse)
10997   "Sort the summary buffer by article length.
10998 Argument REVERSE means reverse order."
10999   (interactive "P")
11000   (gnus-summary-sort 'chars reverse))
11001
11002 (defun gnus-summary-sort-by-original (&optional reverse)
11003   "Sort the summary buffer using the default sorting method.
11004 Argument REVERSE means reverse order."
11005   (interactive "P")
11006   (let* ((buffer-read-only)
11007          (gnus-summary-prepare-hook nil))
11008     ;; We do the sorting by regenerating the threads.
11009     (gnus-summary-prepare)
11010     ;; Hide subthreads if needed.
11011     (gnus-summary-maybe-hide-threads)))
11012
11013 (defun gnus-summary-sort (predicate reverse)
11014   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11015   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11016          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11017          (gnus-thread-sort-functions
11018           (if (not reverse)
11019               thread
11020             `(lambda (t1 t2)
11021                (,thread t2 t1))))
11022          (gnus-sort-gathered-threads-function
11023           gnus-thread-sort-functions)
11024          (gnus-article-sort-functions
11025           (if (not reverse)
11026               article
11027             `(lambda (t1 t2)
11028                (,article t2 t1))))
11029          (buffer-read-only)
11030          (gnus-summary-prepare-hook nil))
11031     ;; We do the sorting by regenerating the threads.
11032     (gnus-summary-prepare)
11033     ;; Hide subthreads if needed.
11034     (gnus-summary-maybe-hide-threads)))
11035
11036 ;; Summary saving commands.
11037
11038 (defun gnus-summary-save-article (&optional n not-saved)
11039   "Save the current article using the default saver function.
11040 If N is a positive number, save the N next articles.
11041 If N is a negative number, save the N previous articles.
11042 If N is nil and any articles have been marked with the process mark,
11043 save those articles instead.
11044 The variable `gnus-default-article-saver' specifies the saver function."
11045   (interactive "P")
11046   (let* ((articles (gnus-summary-work-articles n))
11047          (save-buffer (save-excursion
11048                         (nnheader-set-temp-buffer " *Gnus Save*")))
11049          (num (length articles))
11050          header file)
11051     (dolist (article articles)
11052       (setq header (gnus-summary-article-header article))
11053       (if (not (vectorp header))
11054           ;; This is a pseudo-article.
11055           (if (assq 'name header)
11056               (gnus-copy-file (cdr (assq 'name header)))
11057             (gnus-message 1 "Article %d is unsaveable" article))
11058         ;; This is a real article.
11059         (save-window-excursion
11060           (let ((gnus-display-mime-function nil)
11061                 (gnus-article-prepare-hook nil))
11062             (gnus-summary-select-article t nil nil article)))
11063         (save-excursion
11064           (set-buffer save-buffer)
11065           (erase-buffer)
11066           (insert-buffer-substring gnus-original-article-buffer))
11067         (setq file (gnus-article-save save-buffer file num))
11068         (gnus-summary-remove-process-mark article)
11069         (unless not-saved
11070           (gnus-summary-set-saved-mark article))))
11071     (gnus-kill-buffer save-buffer)
11072     (gnus-summary-position-point)
11073     (gnus-set-mode-line 'summary)
11074     n))
11075
11076 (defun gnus-summary-pipe-output (&optional arg headers)
11077   "Pipe the current article to a subprocess.
11078 If N is a positive number, pipe the N next articles.
11079 If N is a negative number, pipe the N previous articles.
11080 If N is nil and any articles have been marked with the process mark,
11081 pipe those articles instead.
11082 If HEADERS (the symbolic prefix), include the headers, too."
11083   (interactive (gnus-interactive "P\ny"))
11084   (require 'gnus-art)
11085   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11086         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11087     (gnus-summary-save-article arg t))
11088   (let ((buffer (get-buffer "*Shell Command Output*")))
11089     (when (and buffer
11090                (not (zerop (buffer-size buffer))))
11091       (gnus-configure-windows 'pipe))))
11092
11093 (defun gnus-summary-save-article-mail (&optional arg)
11094   "Append the current article to an mail file.
11095 If N is a positive number, save the N next articles.
11096 If N is a negative number, save the N previous articles.
11097 If N is nil and any articles have been marked with the process mark,
11098 save those articles instead."
11099   (interactive "P")
11100   (require 'gnus-art)
11101   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11102     (gnus-summary-save-article arg)))
11103
11104 (defun gnus-summary-save-article-rmail (&optional arg)
11105   "Append the current article to an rmail file.
11106 If N is a positive number, save the N next articles.
11107 If N is a negative number, save the N previous articles.
11108 If N is nil and any articles have been marked with the process mark,
11109 save those articles instead."
11110   (interactive "P")
11111   (require 'gnus-art)
11112   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11113     (gnus-summary-save-article arg)))
11114
11115 (defun gnus-summary-save-article-file (&optional arg)
11116   "Append the current article to a file.
11117 If N is a positive number, save the N next articles.
11118 If N is a negative number, save the N previous articles.
11119 If N is nil and any articles have been marked with the process mark,
11120 save those articles instead."
11121   (interactive "P")
11122   (require 'gnus-art)
11123   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11124     (gnus-summary-save-article arg)))
11125
11126 (defun gnus-summary-write-article-file (&optional arg)
11127   "Write the current article to a file, deleting the previous file.
11128 If N is a positive number, save the N next articles.
11129 If N is a negative number, save the N previous articles.
11130 If N is nil and any articles have been marked with the process mark,
11131 save those articles instead."
11132   (interactive "P")
11133   (require 'gnus-art)
11134   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11135     (gnus-summary-save-article arg)))
11136
11137 (defun gnus-summary-save-article-body-file (&optional arg)
11138   "Append the current article body to a file.
11139 If N is a positive number, save the N next articles.
11140 If N is a negative number, save the N previous articles.
11141 If N is nil and any articles have been marked with the process mark,
11142 save those articles instead."
11143   (interactive "P")
11144   (require 'gnus-art)
11145   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11146     (gnus-summary-save-article arg)))
11147
11148 (defun gnus-summary-muttprint (&optional arg)
11149   "Print the current article using Muttprint.
11150 If N is a positive number, save the N next articles.
11151 If N is a negative number, save the N previous articles.
11152 If N is nil and any articles have been marked with the process mark,
11153 save those articles instead."
11154   (interactive "P")
11155   (require 'gnus-art)
11156   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11157     (gnus-summary-save-article arg t)))
11158
11159 (defun gnus-summary-pipe-message (program)
11160   "Pipe the current article through PROGRAM."
11161   (interactive "sProgram: ")
11162   (gnus-summary-select-article)
11163   (let ((mail-header-separator ""))
11164     (gnus-eval-in-buffer-window gnus-article-buffer
11165       (save-restriction
11166         (widen)
11167         (let ((start (window-start))
11168               buffer-read-only)
11169           (message-pipe-buffer-body program)
11170           (set-window-start (get-buffer-window (current-buffer)) start))))))
11171
11172 (defun gnus-get-split-value (methods)
11173   "Return a value based on the split METHODS."
11174   (let (split-name method result match)
11175     (when methods
11176       (save-excursion
11177         (set-buffer gnus-original-article-buffer)
11178         (save-restriction
11179           (nnheader-narrow-to-headers)
11180           (while (and methods (not split-name))
11181             (goto-char (point-min))
11182             (setq method (pop methods))
11183             (setq match (car method))
11184             (when (cond
11185                    ((stringp match)
11186                     ;; Regular expression.
11187                     (ignore-errors
11188                       (re-search-forward match nil t)))
11189                    ((functionp match)
11190                     ;; Function.
11191                     (save-restriction
11192                       (widen)
11193                       (setq result (funcall match gnus-newsgroup-name))))
11194                    ((consp match)
11195                     ;; Form.
11196                     (save-restriction
11197                       (widen)
11198                       (setq result (eval match)))))
11199               (setq split-name (cdr method))
11200               (cond ((stringp result)
11201                      (push (expand-file-name
11202                             result gnus-article-save-directory)
11203                            split-name))
11204                     ((consp result)
11205                      (setq split-name (append result split-name)))))))))
11206     (nreverse split-name)))
11207
11208 (defun gnus-valid-move-group-p (group)
11209   (and (boundp group)
11210        (symbol-name group)
11211        (symbol-value group)
11212        (gnus-get-function (gnus-find-method-for-group
11213                            (symbol-name group)) 'request-accept-article t)))
11214
11215 (defun gnus-read-move-group-name (prompt default articles prefix)
11216   "Read a group name."
11217   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11218          (minibuffer-confirm-incomplete nil) ; XEmacs
11219          (prom
11220           (format "%s %s to"
11221                   prompt
11222                   (if (> (length articles) 1)
11223                       (format "these %d articles" (length articles))
11224                     "this article")))
11225          (to-newsgroup
11226           (cond
11227            ((null split-name)
11228             (gnus-completing-read-with-default
11229              default prom
11230              gnus-active-hashtb
11231              'gnus-valid-move-group-p
11232              nil prefix
11233              'gnus-group-history))
11234            ((= 1 (length split-name))
11235             (gnus-completing-read-with-default
11236              (car split-name) prom
11237              gnus-active-hashtb
11238              'gnus-valid-move-group-p
11239              nil nil
11240              'gnus-group-history))
11241            (t
11242             (gnus-completing-read-with-default
11243              nil prom
11244              (mapcar 'list (nreverse split-name))
11245              nil nil nil
11246              'gnus-group-history))))
11247          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11248     (when to-newsgroup
11249       (if (or (string= to-newsgroup "")
11250               (string= to-newsgroup prefix))
11251           (setq to-newsgroup default))
11252       (unless to-newsgroup
11253         (error "No group name entered"))
11254       (or (gnus-active to-newsgroup)
11255           (gnus-activate-group to-newsgroup nil nil to-method)
11256           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11257                                      to-newsgroup))
11258               (or (and (gnus-request-create-group to-newsgroup to-method)
11259                        (gnus-activate-group
11260                         to-newsgroup nil nil to-method)
11261                        (gnus-subscribe-group to-newsgroup))
11262                   (error "Couldn't create group %s" to-newsgroup)))
11263           (error "No such group: %s" to-newsgroup)))
11264     to-newsgroup))
11265
11266 (defun gnus-summary-save-parts (type dir n &optional reverse)
11267   "Save parts matching TYPE to DIR.
11268 If REVERSE, save parts that do not match TYPE."
11269   (interactive
11270    (list (read-string "Save parts of type: "
11271                       (or (car gnus-summary-save-parts-type-history)
11272                           gnus-summary-save-parts-default-mime)
11273                       'gnus-summary-save-parts-type-history)
11274          (setq gnus-summary-save-parts-last-directory
11275                (read-file-name "Save to directory: "
11276                                gnus-summary-save-parts-last-directory
11277                                nil t))
11278          current-prefix-arg))
11279   (gnus-summary-iterate n
11280     (let ((gnus-display-mime-function nil)
11281           (gnus-inhibit-treatment t))
11282       (gnus-summary-select-article))
11283     (save-excursion
11284       (set-buffer gnus-article-buffer)
11285       (let ((handles (or gnus-article-mime-handles
11286                          (mm-dissect-buffer nil gnus-article-loose-mime)
11287                          (and gnus-article-emulate-mime
11288                               (mm-uu-dissect)))))
11289         (when handles
11290           (gnus-summary-save-parts-1 type dir handles reverse)
11291           (unless gnus-article-mime-handles ;; Don't destroy this case.
11292             (mm-destroy-parts handles)))))))
11293
11294 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11295   (if (stringp (car handle))
11296       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11297               (cdr handle))
11298     (when (if reverse
11299               (not (string-match type (mm-handle-media-type handle)))
11300             (string-match type (mm-handle-media-type handle)))
11301       (let ((file (expand-file-name
11302                    (gnus-map-function
11303                     mm-file-name-rewrite-functions
11304                     (file-name-nondirectory
11305                      (or
11306                       (mail-content-type-get
11307                        (mm-handle-disposition handle) 'filename)
11308                       (mail-content-type-get
11309                        (mm-handle-type handle) 'name)
11310                       (concat gnus-newsgroup-name
11311                               "." (number-to-string
11312                                    (cdr gnus-article-current))))))
11313                    dir)))
11314         (unless (file-exists-p file)
11315           (mm-save-part-to-file handle file))))))
11316
11317 ;; Summary extract commands
11318
11319 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11320   (let ((buffer-read-only nil)
11321         (article (gnus-summary-article-number))
11322         after-article b e)
11323     (unless (gnus-summary-goto-subject article)
11324       (error "No such article: %d" article))
11325     (gnus-summary-position-point)
11326     ;; If all commands are to be bunched up on one line, we collect
11327     ;; them here.
11328     (unless gnus-view-pseudos-separately
11329       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11330             files action)
11331         (while ps
11332           (setq action (cdr (assq 'action (car ps))))
11333           (setq files (list (cdr (assq 'name (car ps)))))
11334           (while (and ps (cdr ps)
11335                       (string= (or action "1")
11336                                (or (cdr (assq 'action (cadr ps))) "2")))
11337             (push (cdr (assq 'name (cadr ps))) files)
11338             (setcdr ps (cddr ps)))
11339           (when files
11340             (when (not (string-match "%s" action))
11341               (push " " files))
11342             (push " " files)
11343             (when (assq 'execute (car ps))
11344               (setcdr (assq 'execute (car ps))
11345                       (funcall (if (string-match "%s" action)
11346                                    'format 'concat)
11347                                action
11348                                (mapconcat
11349                                 (lambda (f)
11350                                   (if (equal f " ")
11351                                       f
11352                                     (shell-quote-argument f)))
11353                                 files " ")))))
11354           (setq ps (cdr ps)))))
11355     (if (and gnus-view-pseudos (not not-view))
11356         (while pslist
11357           (when (assq 'execute (car pslist))
11358             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11359                                   (eq gnus-view-pseudos 'not-confirm)))
11360           (setq pslist (cdr pslist)))
11361       (save-excursion
11362         (while pslist
11363           (setq after-article (or (cdr (assq 'article (car pslist)))
11364                                   (gnus-summary-article-number)))
11365           (gnus-summary-goto-subject after-article)
11366           (forward-line 1)
11367           (setq b (point))
11368           (insert "    " (file-name-nondirectory
11369                           (cdr (assq 'name (car pslist))))
11370                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11371           (setq e (point))
11372           (forward-line -1)             ; back to `b'
11373           (gnus-add-text-properties
11374            b (1- e) (list 'gnus-number gnus-reffed-article-number
11375                           gnus-mouse-face-prop gnus-mouse-face))
11376           (gnus-data-enter
11377            after-article gnus-reffed-article-number
11378            gnus-unread-mark b (car pslist) 0 (- e b))
11379           (setq gnus-newsgroup-unreads
11380                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11381                                          gnus-reffed-article-number))
11382           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11383           (setq pslist (cdr pslist)))))))
11384
11385 (defun gnus-pseudos< (p1 p2)
11386   (let ((c1 (cdr (assq 'action p1)))
11387         (c2 (cdr (assq 'action p2))))
11388     (and c1 c2 (string< c1 c2))))
11389
11390 (defun gnus-request-pseudo-article (props)
11391   (cond ((assq 'execute props)
11392          (gnus-execute-command (cdr (assq 'execute props)))))
11393   (let ((gnus-current-article (gnus-summary-article-number)))
11394     (gnus-run-hooks 'gnus-mark-article-hook)))
11395
11396 (defun gnus-execute-command (command &optional automatic)
11397   (save-excursion
11398     (gnus-article-setup-buffer)
11399     (set-buffer gnus-article-buffer)
11400     (setq buffer-read-only nil)
11401     (let ((command (if automatic command
11402                      (read-string "Command: " (cons command 0)))))
11403       (erase-buffer)
11404       (insert "$ " command "\n\n")
11405       (if gnus-view-pseudo-asynchronously
11406           (start-process "gnus-execute" (current-buffer) shell-file-name
11407                          shell-command-switch command)
11408         (call-process shell-file-name nil t nil
11409                       shell-command-switch command)))))
11410
11411 ;; Summary kill commands.
11412
11413 (defun gnus-summary-edit-global-kill (article)
11414   "Edit the \"global\" kill file."
11415   (interactive (list (gnus-summary-article-number)))
11416   (gnus-group-edit-global-kill article))
11417
11418 (defun gnus-summary-edit-local-kill ()
11419   "Edit a local kill file applied to the current newsgroup."
11420   (interactive)
11421   (setq gnus-current-headers (gnus-summary-article-header))
11422   (gnus-group-edit-local-kill
11423    (gnus-summary-article-number) gnus-newsgroup-name))
11424
11425 ;;; Header reading.
11426
11427 (defun gnus-read-header (id &optional header)
11428   "Read the headers of article ID and enter them into the Gnus system."
11429   (let ((group gnus-newsgroup-name)
11430         (gnus-override-method
11431          (or
11432           gnus-override-method
11433           (and (gnus-news-group-p gnus-newsgroup-name)
11434                (car (gnus-refer-article-methods)))))
11435         where)
11436     ;; First we check to see whether the header in question is already
11437     ;; fetched.
11438     (if (stringp id)
11439         ;; This is a Message-ID.
11440         (setq header (or header (gnus-id-to-header id)))
11441       ;; This is an article number.
11442       (setq header (or header (gnus-summary-article-header id))))
11443     (if (and header
11444              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11445         ;; We have found the header.
11446         header
11447       ;; We have to really fetch the header to this article.
11448       (save-excursion
11449         (set-buffer nntp-server-buffer)
11450         (when (setq where (gnus-request-head id group))
11451           (nnheader-fold-continuation-lines)
11452           (goto-char (point-max))
11453           (insert ".\n")
11454           (goto-char (point-min))
11455           (insert "211 ")
11456           (princ (cond
11457                   ((numberp id) id)
11458                   ((cdr where) (cdr where))
11459                   (header (mail-header-number header))
11460                   (t gnus-reffed-article-number))
11461                  (current-buffer))
11462           (insert " Article retrieved.\n"))
11463         (if (or (not where)
11464                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11465             ()                          ; Malformed head.
11466           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11467             (when (and (stringp id)
11468                        (not (string= (gnus-group-real-name group)
11469                                      (car where))))
11470               ;; If we fetched by Message-ID and the article came
11471               ;; from a different group, we fudge some bogus article
11472               ;; numbers for this article.
11473               (mail-header-set-number header gnus-reffed-article-number))
11474             (save-excursion
11475               (set-buffer gnus-summary-buffer)
11476               (decf gnus-reffed-article-number)
11477               (gnus-remove-header (mail-header-number header))
11478               (push header gnus-newsgroup-headers)
11479               (setq gnus-current-headers header)
11480               (push (mail-header-number header) gnus-newsgroup-limit)))
11481           header)))))
11482
11483 (defun gnus-remove-header (number)
11484   "Remove header NUMBER from `gnus-newsgroup-headers'."
11485   (if (and gnus-newsgroup-headers
11486            (= number (mail-header-number (car gnus-newsgroup-headers))))
11487       (pop gnus-newsgroup-headers)
11488     (let ((headers gnus-newsgroup-headers))
11489       (while (and (cdr headers)
11490                   (not (= number (mail-header-number (cadr headers)))))
11491         (pop headers))
11492       (when (cdr headers)
11493         (setcdr headers (cddr headers))))))
11494
11495 ;;;
11496 ;;; summary highlights
11497 ;;;
11498
11499 (defun gnus-highlight-selected-summary ()
11500   "Highlight selected article in summary buffer."
11501   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11502   (when gnus-summary-selected-face
11503     (save-excursion
11504       (let* ((beg (point-at-bol))
11505              (end (point-at-eol))
11506              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11507              (from (if (get-text-property beg gnus-mouse-face-prop)
11508                        beg
11509                      (or (next-single-property-change
11510                           beg gnus-mouse-face-prop nil end)
11511                          beg)))
11512              (to
11513               (if (= from end)
11514                   (- from 2)
11515                 (or (next-single-property-change
11516                      from gnus-mouse-face-prop nil end)
11517                     end))))
11518         ;; If no mouse-face prop on line we will have to = from = end,
11519         ;; so we highlight the entire line instead.
11520         (when (= (+ to 2) from)
11521           (setq from beg)
11522           (setq to end))
11523         (if gnus-newsgroup-selected-overlay
11524             ;; Move old overlay.
11525             (gnus-move-overlay
11526              gnus-newsgroup-selected-overlay from to (current-buffer))
11527           ;; Create new overlay.
11528           (gnus-overlay-put
11529            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11530            'face gnus-summary-selected-face))))))
11531
11532 (defvar gnus-summary-highlight-line-cached nil)
11533 (defvar gnus-summary-highlight-line-trigger nil)
11534
11535 (defun gnus-summary-highlight-line-0 ()
11536   (if (and (eq gnus-summary-highlight-line-trigger
11537                gnus-summary-highlight)
11538            gnus-summary-highlight-line-cached)
11539       gnus-summary-highlight-line-cached
11540     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11541           gnus-summary-highlight-line-cached
11542           (let* ((cond (list 'cond))
11543                  (c cond)
11544                  (list gnus-summary-highlight))
11545             (while list
11546               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11547                               nil))
11548               (setq c (cdr c)
11549                     list (cdr list)))
11550             (gnus-byte-compile (list 'lambda nil cond))))))
11551
11552 (defun gnus-summary-highlight-line ()
11553   "Highlight current line according to `gnus-summary-highlight'."
11554   (let* ((beg (point-at-bol))
11555          (article (or (gnus-summary-article-number) gnus-current-article))
11556          (score (or (cdr (assq article
11557                                gnus-newsgroup-scored))
11558                     gnus-summary-default-score 0))
11559          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11560          (inhibit-read-only t)
11561          (default gnus-summary-default-score)
11562          (default-high gnus-summary-default-high-score)
11563          (default-low gnus-summary-default-low-score)
11564          (uncached (and gnus-summary-use-undownloaded-faces
11565                         (memq article gnus-newsgroup-undownloaded)
11566                         (not (memq article gnus-newsgroup-cached)))))
11567     (let ((face (funcall (gnus-summary-highlight-line-0))))
11568       (unless (eq face (get-text-property beg 'face))
11569         (gnus-put-text-property-excluding-characters-with-faces
11570          beg (point-at-eol) 'face
11571          (setq face (if (boundp face) (symbol-value face) face)))
11572         (when gnus-summary-highlight-line-function
11573           (funcall gnus-summary-highlight-line-function article face))))))
11574
11575 (defun gnus-update-read-articles (group unread &optional compute)
11576   "Update the list of read articles in GROUP.
11577 UNREAD is a sorted list."
11578   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11579         (info (gnus-get-info group))
11580         (prev 1)
11581         read)
11582     (if (or (not info) (not active))
11583         ;; There is no info on this group if it was, in fact,
11584         ;; killed.  Gnus stores no information on killed groups, so
11585         ;; there's nothing to be done.
11586         ;; One could store the information somewhere temporarily,
11587         ;; perhaps...  Hmmm...
11588         ()
11589       ;; Remove any negative articles numbers.
11590       (while (and unread (< (car unread) 0))
11591         (setq unread (cdr unread)))
11592       ;; Remove any expired article numbers
11593       (while (and unread (< (car unread) (car active)))
11594         (setq unread (cdr unread)))
11595       ;; Compute the ranges of read articles by looking at the list of
11596       ;; unread articles.
11597       (while unread
11598         (when (/= (car unread) prev)
11599           (push (if (= prev (1- (car unread))) prev
11600                   (cons prev (1- (car unread))))
11601                 read))
11602         (setq prev (1+ (car unread)))
11603         (setq unread (cdr unread)))
11604       (when (<= prev (cdr active))
11605         (push (cons prev (cdr active)) read))
11606       (setq read (if (> (length read) 1) (nreverse read) read))
11607       (if compute
11608           read
11609         (save-excursion
11610           (let (setmarkundo)
11611             ;; Propagate the read marks to the backend.
11612             (when (gnus-check-backend-function 'request-set-mark group)
11613               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11614                     (add (gnus-remove-from-range read (gnus-info-read info))))
11615                 (when (or add del)
11616                   (unless (gnus-check-group group)
11617                     (error "Can't open server for %s" group))
11618                   (gnus-request-set-mark
11619                    group (delq nil (list (if add (list add 'add '(read)))
11620                                          (if del (list del 'del '(read))))))
11621                   (setq setmarkundo
11622                         `(gnus-request-set-mark
11623                           ,group
11624                           ',(delq nil (list
11625                                        (if del (list del 'add '(read)))
11626                                        (if add (list add 'del '(read))))))))))
11627             (set-buffer gnus-group-buffer)
11628             (gnus-undo-register
11629               `(progn
11630                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11631                  (gnus-info-set-read ',info ',(gnus-info-read info))
11632                  (gnus-get-unread-articles-in-group ',info
11633                                                     (gnus-active ,group))
11634                  (gnus-group-update-group ,group t)
11635                  ,setmarkundo))))
11636         ;; Enter this list into the group info.
11637         (gnus-info-set-read info read)
11638         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11639         (gnus-get-unread-articles-in-group info (gnus-active group))
11640         t))))
11641
11642 (defun gnus-offer-save-summaries ()
11643   "Offer to save all active summary buffers."
11644   (let (buffers)
11645     ;; Go through all buffers and find all summaries.
11646     (dolist (buffer (buffer-list))
11647       (when (and (setq buffer (buffer-name buffer))
11648                  (string-match "Summary" buffer)
11649                  (with-current-buffer buffer
11650                    ;; We check that this is, indeed, a summary buffer.
11651                    (and (eq major-mode 'gnus-summary-mode)
11652                         ;; Also make sure this isn't bogus.
11653                         gnus-newsgroup-prepared
11654                         ;; Also make sure that this isn't a
11655                         ;; dead summary buffer.
11656                         (not gnus-dead-summary-mode))))
11657         (push buffer buffers)))
11658     ;; Go through all these summary buffers and offer to save them.
11659     (when buffers
11660       (save-excursion
11661         (map-y-or-n-p
11662          "Update summary buffer %s? "
11663          (lambda (buf)
11664            (switch-to-buffer buf)
11665            (gnus-summary-exit))
11666          buffers)))))
11667
11668 (defun gnus-summary-setup-default-charset ()
11669   "Setup newsgroup default charset."
11670   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11671       (setq gnus-newsgroup-charset nil)
11672     (let* ((ignored-charsets
11673             (or gnus-newsgroup-ephemeral-ignored-charsets
11674                 (append
11675                  (and gnus-newsgroup-name
11676                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11677                  gnus-newsgroup-ignored-charsets))))
11678       (setq gnus-newsgroup-charset
11679             (or gnus-newsgroup-ephemeral-charset
11680                 (and gnus-newsgroup-name
11681                      (gnus-parameter-charset gnus-newsgroup-name))
11682                 gnus-default-charset))
11683       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11684            ignored-charsets))))
11685
11686 ;;;
11687 ;;; Mime Commands
11688 ;;;
11689
11690 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11691   "Display the current article buffer fully MIME-buttonized.
11692 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11693 treated as multipart/mixed."
11694   (interactive "P")
11695   (require 'gnus-art)
11696   (let ((gnus-unbuttonized-mime-types nil)
11697         (gnus-mime-display-multipart-as-mixed show-all-parts))
11698     (gnus-summary-show-article)))
11699
11700 (defun gnus-summary-repair-multipart (article)
11701   "Add a Content-Type header to a multipart article without one."
11702   (interactive (list (gnus-summary-article-number)))
11703   (gnus-with-article article
11704     (message-narrow-to-head)
11705     (message-remove-header "Mime-Version")
11706     (goto-char (point-max))
11707     (insert "Mime-Version: 1.0\n")
11708     (widen)
11709     (when (search-forward "\n--" nil t)
11710       (let ((separator (buffer-substring (point) (point-at-eol))))
11711         (message-narrow-to-head)
11712         (message-remove-header "Content-Type")
11713         (goto-char (point-max))
11714         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11715                         separator))
11716         (widen))))
11717   (let (gnus-mark-article-hook)
11718     (gnus-summary-select-article t t nil article)))
11719
11720 (defun gnus-summary-toggle-display-buttonized ()
11721   "Toggle the buttonizing of the article buffer."
11722   (interactive)
11723   (require 'gnus-art)
11724   (if (setq gnus-inhibit-mime-unbuttonizing
11725             (not gnus-inhibit-mime-unbuttonizing))
11726       (let ((gnus-unbuttonized-mime-types nil))
11727         (gnus-summary-show-article))
11728     (gnus-summary-show-article)))
11729
11730 ;;;
11731 ;;; Generic summary marking commands
11732 ;;;
11733
11734 (defvar gnus-summary-marking-alist
11735   '((read gnus-del-mark "d")
11736     (unread gnus-unread-mark "u")
11737     (ticked gnus-ticked-mark "!")
11738     (dormant gnus-dormant-mark "?")
11739     (expirable gnus-expirable-mark "e"))
11740   "An alist of names/marks/keystrokes.")
11741
11742 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11743 (defvar gnus-summary-mark-map)
11744
11745 (defun gnus-summary-make-all-marking-commands ()
11746   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11747   (dolist (elem gnus-summary-marking-alist)
11748     (apply 'gnus-summary-make-marking-command elem)))
11749
11750 (defun gnus-summary-make-marking-command (name mark keystroke)
11751   (let ((map (make-sparse-keymap)))
11752     (define-key gnus-summary-generic-mark-map keystroke map)
11753     (dolist (lway `((next "next" next nil "n")
11754                     (next-unread "next unread" next t "N")
11755                     (prev "previous" prev nil "p")
11756                     (prev-unread "previous unread" prev t "P")
11757                     (nomove "" nil nil ,keystroke)))
11758       (let ((func (gnus-summary-make-marking-command-1
11759                    mark (car lway) lway name)))
11760         (setq func (eval func))
11761         (define-key map (nth 4 lway) func)))))
11762
11763 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11764   `(defun ,(intern
11765             (format "gnus-summary-put-mark-as-%s%s"
11766                     name (if (eq way 'nomove)
11767                              ""
11768                            (concat "-" (symbol-name way)))))
11769      (n)
11770      ,(format
11771        "Mark the current article as %s%s.
11772 If N, the prefix, then repeat N times.
11773 If N is negative, move in reverse order.
11774 The difference between N and the actual number of articles marked is
11775 returned."
11776        name (cadr lway))
11777      (interactive "p")
11778      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11779
11780 (defun gnus-summary-generic-mark (n mark move unread)
11781   "Mark N articles with MARK."
11782   (unless (eq major-mode 'gnus-summary-mode)
11783     (error "This command can only be used in the summary buffer"))
11784   (gnus-summary-show-thread)
11785   (let ((nummove
11786          (cond
11787           ((eq move 'next) 1)
11788           ((eq move 'prev) -1)
11789           (t 0))))
11790     (if (zerop nummove)
11791         (setq n 1)
11792       (when (< n 0)
11793         (setq n (abs n)
11794               nummove (* -1 nummove))))
11795     (while (and (> n 0)
11796                 (gnus-summary-mark-article nil mark)
11797                 (zerop (gnus-summary-next-subject nummove unread t)))
11798       (setq n (1- n)))
11799     (when (/= 0 n)
11800       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11801     (gnus-summary-recenter)
11802     (gnus-summary-position-point)
11803     (gnus-set-mode-line 'summary)
11804     n))
11805
11806 (defun gnus-summary-insert-articles (articles)
11807   (when (setq articles
11808               (gnus-sorted-difference articles
11809                                       (mapcar (lambda (h)
11810                                                 (mail-header-number h))
11811                                               gnus-newsgroup-headers)))
11812     (setq gnus-newsgroup-headers
11813           (gnus-merge 'list
11814                       gnus-newsgroup-headers
11815                       (gnus-fetch-headers articles)
11816                       'gnus-article-sort-by-number))
11817     ;; Suppress duplicates?
11818     (when gnus-suppress-duplicates
11819       (gnus-dup-suppress-articles))
11820
11821     ;; We might want to build some more threads first.
11822     (when (and gnus-fetch-old-headers
11823                (eq gnus-headers-retrieved-by 'nov))
11824       (if (eq gnus-fetch-old-headers 'invisible)
11825           (gnus-build-all-threads)
11826         (gnus-build-old-threads)))
11827     ;; Let the Gnus agent mark articles as read.
11828     (when gnus-agent
11829       (gnus-agent-get-undownloaded-list))
11830     ;; Remove list identifiers from subject
11831     (when gnus-list-identifiers
11832       (gnus-summary-remove-list-identifiers))
11833     ;; First and last article in this newsgroup.
11834     (when gnus-newsgroup-headers
11835       (setq gnus-newsgroup-begin
11836             (mail-header-number (car gnus-newsgroup-headers))
11837             gnus-newsgroup-end
11838             (mail-header-number
11839              (gnus-last-element gnus-newsgroup-headers))))
11840     (when gnus-use-scoring
11841       (gnus-possibly-score-headers))))
11842
11843 (defun gnus-summary-insert-old-articles (&optional all)
11844   "Insert all old articles in this group.
11845 If ALL is non-nil, already read articles become readable.
11846 If ALL is a number, fetch this number of articles."
11847   (interactive "P")
11848   (prog1
11849       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11850             older len)
11851         (setq older
11852               ;; Some nntp servers lie about their active range.  When
11853               ;; this happens, the active range can be in the millions.
11854               ;; Use a compressed range to avoid creating a huge list.
11855               (gnus-range-difference (list gnus-newsgroup-active) old))
11856         (setq len (gnus-range-length older))
11857         (cond
11858          ((null older) nil)
11859          ((numberp all)
11860           (if (< all len)
11861               (let ((older-range (nreverse older)))
11862                 (setq older nil)
11863
11864                 (while (> all 0)
11865                   (let* ((r (pop older-range))
11866                          (min (if (numberp r) r (car r)))
11867                          (max (if (numberp r) r (cdr r))))
11868                     (while (and (<= min max)
11869                                 (> all 0))
11870                       (push max older)
11871                       (setq all (1- all)
11872                             max (1- max))))))
11873             (setq older (gnus-uncompress-range older))))
11874          (all
11875           (setq older (gnus-uncompress-range older)))
11876          (t
11877           (when (and (numberp gnus-large-newsgroup)
11878                    (> len gnus-large-newsgroup))
11879               (let* ((cursor-in-echo-area nil)
11880                      (initial (gnus-parameter-large-newsgroup-initial
11881                                gnus-newsgroup-name))
11882                      (input
11883                       (read-string
11884                        (format
11885                         "How many articles from %s (%s %d): "
11886                         (gnus-limit-string
11887                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11888                         (if initial "max" "default")
11889                         len)
11890                        (if initial
11891                            (cons (number-to-string initial)
11892                                  0)))))
11893                 (unless (string-match "^[ \t]*$" input)
11894                   (setq all (string-to-number input))
11895                   (if (< all len)
11896                       (let ((older-range (nreverse older)))
11897                         (setq older nil)
11898
11899                         (while (> all 0)
11900                           (let* ((r (pop older-range))
11901                                  (min (if (numberp r) r (car r)))
11902                                  (max (if (numberp r) r (cdr r))))
11903                             (while (and (<= min max)
11904                                         (> all 0))
11905                               (push max older)
11906                               (setq all (1- all)
11907                                     max (1- max))))))))))
11908           (setq older (gnus-uncompress-range older))))
11909         (if (not older)
11910             (message "No old news.")
11911           (gnus-summary-insert-articles older)
11912           (gnus-summary-limit (gnus-sorted-nunion old older))))
11913     (gnus-summary-position-point)))
11914
11915 (defun gnus-summary-insert-new-articles ()
11916   "Insert all new articles in this group."
11917   (interactive)
11918   (prog1
11919       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11920             (old-active gnus-newsgroup-active)
11921             (nnmail-fetched-sources (list t))
11922             i new)
11923         (setq gnus-newsgroup-active
11924               (gnus-activate-group gnus-newsgroup-name 'scan))
11925         (setq i (cdr gnus-newsgroup-active))
11926         (while (> i (cdr old-active))
11927           (push i new)
11928           (decf i))
11929         (if (not new)
11930             (message "No gnus is bad news")
11931           (gnus-summary-insert-articles new)
11932           (setq gnus-newsgroup-unreads
11933                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11934           (gnus-summary-limit (gnus-sorted-nunion old new))))
11935     (gnus-summary-position-point)))
11936
11937 (gnus-summary-make-all-marking-commands)
11938
11939 (gnus-ems-redefine)
11940
11941 (provide 'gnus-sum)
11942
11943 (run-hooks 'gnus-sum-load-hook)
11944
11945 ;; Local Variables:
11946 ;; coding: iso-8859-1
11947 ;; End:
11948
11949 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11950 ;;; gnus-sum.el ends here