* gnus-sum.el (gnus-update-summary-mark-positions): Allow users to
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
48 (autoload 'mm-uu-dissect "mm-uu")
49 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
50   "Deuglify broken Outlook (Express) articles and redisplay."
51   t)
52 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
53 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
54 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
55
56 (defcustom gnus-kill-summary-on-exit t
57   "*If non-nil, kill the summary buffer when you exit from it.
58 If nil, the summary will become a \"*Dead Summary*\" buffer, and
59 it will be killed sometime later."
60   :group 'gnus-summary-exit
61   :type 'boolean)
62
63 (defcustom gnus-fetch-old-headers nil
64   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
65 If an unread article in the group refers to an older, already read (or
66 just marked as read) article, the old article will not normally be
67 displayed in the Summary buffer.  If this variable is t, Gnus
68 will attempt to grab the headers to the old articles, and thereby
69 build complete threads.  If it has the value `some', only enough
70 headers to connect otherwise loose threads will be displayed.  This
71 variable can also be a number.  In that case, no more than that number
72 of old headers will be fetched.  If it has the value `invisible', all
73 old headers will be fetched, but none will be displayed.
74
75 The server has to support NOV for any of this to work."
76   :group 'gnus-thread
77   :type '(choice (const :tag "off" nil)
78                  (const :tag "on" t)
79                  (const some)
80                  (const invisible)
81                  number
82                  (sexp :menu-tag "other" t)))
83
84 (defcustom gnus-refer-thread-limit 200
85   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
86 If t, fetch all the available old headers."
87   :group 'gnus-thread
88   :type '(choice number
89                  (sexp :menu-tag "other" t)))
90
91 (defcustom gnus-summary-make-false-root 'adopt
92   "*nil means that Gnus won't gather loose threads.
93 If the root of a thread has expired or been read in a previous
94 session, the information necessary to build a complete thread has been
95 lost.  Instead of having many small sub-threads from this original thread
96 scattered all over the summary buffer, Gnus can gather them.
97
98 If non-nil, Gnus will try to gather all loose sub-threads from an
99 original thread into one large thread.
100
101 If this variable is non-nil, it should be one of `none', `adopt',
102 `dummy' or `empty'.
103
104 If this variable is `none', Gnus will not make a false root, but just
105 present the sub-threads after another.
106 If this variable is `dummy', Gnus will create a dummy root that will
107 have all the sub-threads as children.
108 If this variable is `adopt', Gnus will make one of the \"children\"
109 the parent and mark all the step-children as such.
110 If this variable is `empty', the \"children\" are printed with empty
111 subject fields.  (Or rather, they will be printed with a string
112 given by the `gnus-summary-same-subject' variable.)"
113   :group 'gnus-thread
114   :type '(choice (const :tag "off" nil)
115                  (const none)
116                  (const dummy)
117                  (const adopt)
118                  (const empty)))
119
120 (defcustom gnus-summary-make-false-root-always nil
121   "Always make a false dummy root."
122   :version "21.4"
123   :group 'gnus-thread
124   :type 'boolean)
125
126 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
127   "*A regexp to match subjects to be excluded from loose thread gathering.
128 As loose thread gathering is done on subjects only, that means that
129 there can be many false gatherings performed.  By rooting out certain
130 common subjects, gathering might become saner."
131   :group 'gnus-thread
132   :type 'regexp)
133
134 (defcustom gnus-summary-gather-subject-limit nil
135   "*Maximum length of subject comparisons when gathering loose threads.
136 Use nil to compare full subjects.  Setting this variable to a low
137 number will help gather threads that have been corrupted by
138 newsreaders chopping off subject lines, but it might also mean that
139 unrelated articles that have subject that happen to begin with the
140 same few characters will be incorrectly gathered.
141
142 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
143 comparing subjects."
144   :group 'gnus-thread
145   :type '(choice (const :tag "off" nil)
146                  (const fuzzy)
147                  (sexp :menu-tag "on" t)))
148
149 (defcustom gnus-simplify-subject-functions nil
150   "List of functions taking a string argument that simplify subjects.
151 The functions are applied recursively.
152
153 Useful functions to put in this list include:
154 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
155 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
156   :group 'gnus-thread
157   :type '(repeat function))
158
159 (defcustom gnus-simplify-ignored-prefixes nil
160   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
161   :group 'gnus-thread
162   :type '(choice (const :tag "off" nil)
163                  regexp))
164
165 (defcustom gnus-build-sparse-threads nil
166   "*If non-nil, fill in the gaps in threads.
167 If `some', only fill in the gaps that are needed to tie loose threads
168 together.  If `more', fill in all leaf nodes that Gnus can find.  If
169 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
170   :group 'gnus-thread
171   :type '(choice (const :tag "off" nil)
172                  (const some)
173                  (const more)
174                  (sexp :menu-tag "all" t)))
175
176 (defcustom gnus-summary-thread-gathering-function
177   'gnus-gather-threads-by-subject
178   "*Function used for gathering loose threads.
179 There are two pre-defined functions: `gnus-gather-threads-by-subject',
180 which only takes Subjects into consideration; and
181 `gnus-gather-threads-by-references', which compared the References
182 headers of the articles to find matches."
183   :group 'gnus-thread
184   :type '(radio (function-item gnus-gather-threads-by-subject)
185                 (function-item gnus-gather-threads-by-references)
186                 (function :tag "other")))
187
188 (defcustom gnus-summary-same-subject ""
189   "*String indicating that the current article has the same subject as the previous.
190 This variable will only be used if the value of
191 `gnus-summary-make-false-root' is `empty'."
192   :group 'gnus-summary-format
193   :type 'string)
194
195 (defcustom gnus-summary-goto-unread t
196   "*If t, many commands will go to the next unread article.
197 This applies to marking commands as well as other commands that
198 \"naturally\" select the next article, like, for instance, `SPC' at
199 the end of an article.
200
201 If nil, the marking commands do NOT go to the next unread article
202 \(they go to the next article instead).  If `never', commands that
203 usually go to the next unread article, will go to the next article,
204 whether it is read or not."
205   :group 'gnus-summary-marks
206   :link '(custom-manual "(gnus)Setting Marks")
207   :type '(choice (const :tag "off" nil)
208                  (const never)
209                  (sexp :menu-tag "on" t)))
210
211 (defcustom gnus-summary-default-score 0
212   "*Default article score level.
213 All scores generated by the score files will be added to this score.
214 If this variable is nil, scoring will be disabled."
215   :group 'gnus-score-default
216   :type '(choice (const :tag "disable")
217                  integer))
218
219 (defcustom gnus-summary-default-high-score 0
220   "*Default threshold for a high scored article.
221 An article will be highlighted as high scored if its score is greater
222 than this score."
223   :version "21.4"
224   :group 'gnus-score-default
225   :type 'integer)
226
227 (defcustom gnus-summary-default-low-score 0
228   "*Default threshold for a low scored article.
229 An article will be highlighted as low scored if its score is smaller
230 than this score."
231   :version "21.4"
232   :group 'gnus-score-default
233   :type 'integer)
234
235 (defcustom gnus-summary-zcore-fuzz 0
236   "*Fuzziness factor for the zcore in the summary buffer.
237 Articles with scores closer than this to `gnus-summary-default-score'
238 will not be marked."
239   :group 'gnus-summary-format
240   :type 'integer)
241
242 (defcustom gnus-simplify-subject-fuzzy-regexp nil
243   "*Strings to be removed when doing fuzzy matches.
244 This can either be a regular expression or list of regular expressions
245 that will be removed from subject strings if fuzzy subject
246 simplification is selected."
247   :group 'gnus-thread
248   :type '(repeat regexp))
249
250 (defcustom gnus-show-threads t
251   "*If non-nil, display threads in summary mode."
252   :group 'gnus-thread
253   :type 'boolean)
254
255 (defcustom gnus-thread-hide-subtree nil
256   "*If non-nil, hide all threads initially.
257 This can be a predicate specifier which says which threads to hide.
258 If threads are hidden, you have to run the command
259 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
260 to expose hidden threads."
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" :size 0)))
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 "21.4"
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 "21.4"
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   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-souped-mark ?F
483   "*Mark used for souped articles."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-kill-file-mark ?X
488   "*Mark used for articles killed by kill files."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-low-score-mark ?Y
493   "*Mark used for articles with a low score."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-catchup-mark ?C
498   "*Mark used for articles that are caught up."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-replied-mark ?A
503   "*Mark used for articles that have been replied to."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-forwarded-mark ?F
508   "*Mark used for articles that have been forwarded."
509   :version "21.4"
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-recent-mark ?N
514   "*Mark used for articles that are recent."
515   :group 'gnus-summary-marks
516   :type 'character)
517
518 (defcustom gnus-cached-mark ?*
519   "*Mark used for articles that are in the cache."
520   :group 'gnus-summary-marks
521   :type 'character)
522
523 (defcustom gnus-saved-mark ?S
524   "*Mark used for articles that have been saved."
525   :group 'gnus-summary-marks
526   :type 'character)
527
528 (defcustom gnus-unseen-mark ?.
529   "*Mark used for articles that haven't been seen."
530   :version "21.4"
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-no-mark ?               ;Whitespace
535   "*Mark used for articles that have no other secondary mark."
536   :version "21.4"
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-ancient-mark ?O
541   "*Mark used for ancient articles."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-sparse-mark ?Q
546   "*Mark used for sparsely reffed articles."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-canceled-mark ?G
551   "*Mark used for canceled articles."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-duplicate-mark ?M
556   "*Mark used for duplicate articles."
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-undownloaded-mark ?-
561   "*Mark used for articles that weren't downloaded."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-downloaded-mark ?+
566   "*Mark used for articles that were downloaded."
567   :group 'gnus-summary-marks
568   :type 'character)
569
570 (defcustom gnus-downloadable-mark ?%
571   "*Mark used for articles that are to be downloaded."
572   :group 'gnus-summary-marks
573   :type 'character)
574
575 (defcustom gnus-unsendable-mark ?=
576   "*Mark used for articles that won't be sent."
577   :group 'gnus-summary-marks
578   :type 'character)
579
580 (defcustom gnus-score-over-mark ?+
581   "*Score mark used for articles with high scores."
582   :group 'gnus-summary-marks
583   :type 'character)
584
585 (defcustom gnus-score-below-mark ?-
586   "*Score mark used for articles with low scores."
587   :group 'gnus-summary-marks
588   :type 'character)
589
590 (defcustom gnus-empty-thread-mark ?     ;Whitespace
591   "*There is no thread under the article."
592   :group 'gnus-summary-marks
593   :type 'character)
594
595 (defcustom gnus-not-empty-thread-mark ?=
596   "*There is a thread under the article."
597   :group 'gnus-summary-marks
598   :type 'character)
599
600 (defcustom gnus-view-pseudo-asynchronously nil
601   "*If non-nil, Gnus will view pseudo-articles asynchronously."
602   :group 'gnus-extract-view
603   :type 'boolean)
604
605 (defcustom gnus-auto-expirable-marks
606   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
607         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
608         gnus-souped-mark gnus-duplicate-mark)
609   "*The list of marks converted into expiration if a group is auto-expirable."
610   :version "21.1"
611   :group 'gnus-summary
612   :type '(repeat character))
613
614 (defcustom gnus-inhibit-user-auto-expire t
615   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
616   :version "21.1"
617   :group 'gnus-summary
618   :type 'boolean)
619
620 (defcustom gnus-view-pseudos nil
621   "*If `automatic', pseudo-articles will be viewed automatically.
622 If `not-confirm', pseudos will be viewed automatically, and the user
623 will not be asked to confirm the command."
624   :group 'gnus-extract-view
625   :type '(choice (const :tag "off" nil)
626                  (const automatic)
627                  (const not-confirm)))
628
629 (defcustom gnus-view-pseudos-separately t
630   "*If non-nil, one pseudo-article will be created for each file to be viewed.
631 If nil, all files that use the same viewing command will be given as a
632 list of parameters to that command."
633   :group 'gnus-extract-view
634   :type 'boolean)
635
636 (defcustom gnus-insert-pseudo-articles t
637   "*If non-nil, insert pseudo-articles when decoding articles."
638   :group 'gnus-extract-view
639   :type 'boolean)
640
641 (defcustom gnus-summary-dummy-line-format
642   "   %(:                             :%) %S\n"
643   "*The format specification for the dummy roots in the summary buffer.
644 It works along the same lines as a normal formatting string,
645 with some simple extensions.
646
647 %S  The subject
648
649 General format specifiers can also be used.
650 See `(gnus)Formatting Variables'."
651   :link '(custom-manual "(gnus)Formatting Variables")
652   :group 'gnus-threading
653   :type 'string)
654
655 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
656   "*The format specification for the summary mode line.
657 It works along the same lines as a normal formatting string,
658 with some simple extensions:
659
660 %G  Group name
661 %p  Unprefixed group name
662 %A  Current article number
663 %z  Current article score
664 %V  Gnus version
665 %U  Number of unread articles in the group
666 %e  Number of unselected articles in the group
667 %Z  A string with unread/unselected article counts
668 %g  Shortish group name
669 %S  Subject of the current article
670 %u  User-defined spec
671 %s  Current score file name
672 %d  Number of dormant articles
673 %r  Number of articles that have been marked as read in this session
674 %E  Number of articles expunged by the score files"
675   :group 'gnus-summary-format
676   :type 'string)
677
678 (defcustom gnus-list-identifiers nil
679   "Regexp that matches list identifiers to be removed from subject.
680 This can also be a list of regexps."
681   :version "21.1"
682   :group 'gnus-summary-format
683   :group 'gnus-article-hiding
684   :type '(choice (const :tag "none" nil)
685                  (regexp :value ".*")
686                  (repeat :value (".*") regexp)))
687
688 (defcustom gnus-summary-mark-below 0
689   "*Mark all articles with a score below this variable as read.
690 This variable is local to each summary buffer and usually set by the
691 score file."
692   :group 'gnus-score-default
693   :type 'integer)
694
695 (defun gnus-widget-reversible-match (widget value)
696   "Ignoring WIDGET, convert VALUE to internal form.
697 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
698   ;; (debug value)
699   (or (symbolp value)
700       (and (listp value)
701            (eq (length value) 2)
702            (eq (nth 0 value) 'not)
703            (symbolp (nth 1 value)))))
704
705 (defun gnus-widget-reversible-to-internal (widget value)
706   "Ignoring WIDGET, convert VALUE to internal form.
707 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
708 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
709   ;; (debug value)
710   (if (atom value)
711       (list value nil)
712     (list (nth 1 value) t)))
713
714 (defun gnus-widget-reversible-to-external (widget value)
715   "Ignoring WIDGET, convert VALUE to external form.
716 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
717 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
718   ;; (debug value)
719   (if (nth 1 value)
720       (list 'not (nth 0 value))
721     (nth 0 value)))
722
723 (define-widget 'gnus-widget-reversible 'group
724   "A `group' that convert values."
725   :match 'gnus-widget-reversible-match
726   :value-to-internal 'gnus-widget-reversible-to-internal
727   :value-to-external 'gnus-widget-reversible-to-external)
728
729 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
730   "*List of functions used for sorting articles in the summary buffer.
731
732 Each function takes two articles and returns non-nil if the first
733 article should be sorted before the other.  If you use more than one
734 function, the primary sort function should be the last.  You should
735 probably always include `gnus-article-sort-by-number' in the list of
736 sorting functions -- preferably first.  Also note that sorting by date
737 is often much slower than sorting by number, and the sorting order is
738 very similar.  (Sorting by date means sorting by the time the message
739 was sent, sorting by number means sorting by arrival time.)
740
741 Each item can also be a list `(not F)' where F is a function;
742 this reverses the sort order.
743
744 Ready-made functions include `gnus-article-sort-by-number',
745 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
746 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
747 and `gnus-article-sort-by-score'.
748
749 When threading is turned on, the variable `gnus-thread-sort-functions'
750 controls how articles are sorted."
751   :group 'gnus-summary-sort
752   :type '(repeat (gnus-widget-reversible
753                   (choice (function-item gnus-article-sort-by-number)
754                           (function-item gnus-article-sort-by-author)
755                           (function-item gnus-article-sort-by-subject)
756                           (function-item gnus-article-sort-by-date)
757                           (function-item gnus-article-sort-by-score)
758                           (function-item gnus-article-sort-by-random)
759                           (function :tag "other"))
760                   (boolean :tag "Reverse order"))))
761
762
763 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
764   "*List of functions used for sorting threads in the summary buffer.
765 By default, threads are sorted by article number.
766
767 Each function takes two threads and returns non-nil if the first
768 thread should be sorted before the other.  If you use more than one
769 function, the primary sort function should be the last.  You should
770 probably always include `gnus-thread-sort-by-number' in the list of
771 sorting functions -- preferably first.  Also note that sorting by date
772 is often much slower than sorting by number, and the sorting order is
773 very similar.  (Sorting by date means sorting by the time the message
774 was sent, sorting by number means sorting by arrival time.)
775
776 Each list item can also be a list `(not F)' where F is a
777 function; this specifies reversed sort order.
778
779 Ready-made functions include `gnus-thread-sort-by-number',
780 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
781 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
782 `gnus-thread-sort-by-most-recent-number',
783 `gnus-thread-sort-by-most-recent-date',
784 `gnus-thread-sort-by-random', and
785 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
786
787 When threading is turned off, the variable
788 `gnus-article-sort-functions' controls how articles are sorted."
789   :group 'gnus-summary-sort
790   :type '(repeat 
791           (gnus-widget-reversible
792            (choice (function-item gnus-thread-sort-by-number)
793                    (function-item gnus-thread-sort-by-author)
794                    (function-item gnus-thread-sort-by-subject)
795                    (function-item gnus-thread-sort-by-date)
796                    (function-item gnus-thread-sort-by-score)
797                    (function-item gnus-thread-sort-by-most-recent-number)
798                    (function-item gnus-thread-sort-by-most-recent-date)
799                    (function-item gnus-thread-sort-by-random)
800                    (function-item gnus-thread-sort-by-total-score)
801                    (function :tag "other"))
802            (boolean :tag "Reverse order"))))
803
804 (defcustom gnus-thread-score-function '+
805   "*Function used for calculating the total score of a thread.
806
807 The function is called with the scores of the article and each
808 subthread and should then return the score of the thread.
809
810 Some functions you can use are `+', `max', or `min'."
811   :group 'gnus-summary-sort
812   :type 'function)
813
814 (defcustom gnus-summary-expunge-below nil
815   "All articles that have a score less than this variable will be expunged.
816 This variable is local to the summary buffers."
817   :group 'gnus-score-default
818   :type '(choice (const :tag "off" nil)
819                  integer))
820
821 (defcustom gnus-thread-expunge-below nil
822   "All threads that have a total score less than this variable will be expunged.
823 See `gnus-thread-score-function' for en explanation of what a
824 \"thread score\" is.
825
826 This variable is local to the summary buffers."
827   :group 'gnus-threading
828   :group 'gnus-score-default
829   :type '(choice (const :tag "off" nil)
830                  integer))
831
832 (defcustom gnus-summary-mode-hook nil
833   "*A hook for Gnus summary mode.
834 This hook is run before any variables are set in the summary buffer."
835   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
836   :group 'gnus-summary-various
837   :type 'hook)
838
839 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
840 (when (featurep 'xemacs)
841   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
842   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
843   (add-hook 'gnus-summary-mode-hook
844             'gnus-xmas-switch-horizontal-scrollbar-off))
845
846 (defcustom gnus-summary-menu-hook nil
847   "*Hook run after the creation of the summary mode menu."
848   :group 'gnus-summary-visual
849   :type 'hook)
850
851 (defcustom gnus-summary-exit-hook nil
852   "*A hook called on exit from the summary buffer.
853 It will be called with point in the group buffer."
854   :group 'gnus-summary-exit
855   :type 'hook)
856
857 (defcustom gnus-summary-prepare-hook nil
858   "*A hook called after the summary buffer has been generated.
859 If you want to modify the summary buffer, you can use this hook."
860   :group 'gnus-summary-various
861   :type 'hook)
862
863 (defcustom gnus-summary-prepared-hook nil
864   "*A hook called as the last thing after the summary buffer has been generated."
865   :group 'gnus-summary-various
866   :type 'hook)
867
868 (defcustom gnus-summary-generate-hook nil
869   "*A hook run just before generating the summary buffer.
870 This hook is commonly used to customize threading variables and the
871 like."
872   :group 'gnus-summary-various
873   :type 'hook)
874
875 (defcustom gnus-select-group-hook nil
876   "*A hook called when a newsgroup is selected.
877
878 If you'd like to simplify subjects like the
879 `gnus-summary-next-same-subject' command does, you can use the
880 following hook:
881
882  (add-hook gnus-select-group-hook
883            (lambda ()
884              (mapcar (lambda (header)
885                        (mail-header-set-subject
886                         header
887                         (gnus-simplify-subject
888                          (mail-header-subject header) 're-only)))
889                      gnus-newsgroup-headers)))"
890   :group 'gnus-group-select
891   :type 'hook)
892
893 (defcustom gnus-select-article-hook nil
894   "*A hook called when an article is selected."
895   :group 'gnus-summary-choose
896   :options '(gnus-agent-fetch-selected-article)
897   :type 'hook)
898
899 (defcustom gnus-visual-mark-article-hook
900   (list 'gnus-highlight-selected-summary)
901   "*Hook run after selecting an article in the summary buffer.
902 It is meant to be used for highlighting the article in some way.  It
903 is not run if `gnus-visual' is nil."
904   :group 'gnus-summary-visual
905   :type 'hook)
906
907 (defcustom gnus-parse-headers-hook nil
908   "*A hook called before parsing the headers."
909   :group 'gnus-various
910   :type 'hook)
911
912 (defcustom gnus-exit-group-hook nil
913   "*A hook called when exiting summary mode.
914 This hook is not called from the non-updating exit commands like `Q'."
915   :group 'gnus-various
916   :type 'hook)
917
918 (defcustom gnus-summary-update-hook
919   (list 'gnus-summary-highlight-line)
920   "*A hook called when a summary line is changed.
921 The hook will not be called if `gnus-visual' is nil.
922
923 The default function `gnus-summary-highlight-line' will
924 highlight the line according to the `gnus-summary-highlight'
925 variable."
926   :group 'gnus-summary-visual
927   :type 'hook)
928
929 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
930   "*A hook called when an article is selected for the first time.
931 The hook is intended to mark an article as read (or unread)
932 automatically when it is selected."
933   :group 'gnus-summary-choose
934   :type 'hook)
935
936 (defcustom gnus-group-no-more-groups-hook nil
937   "*A hook run when returning to group mode having no more (unread) groups."
938   :group 'gnus-group-select
939   :type 'hook)
940
941 (defcustom gnus-ps-print-hook nil
942   "*A hook run before ps-printing something from Gnus."
943   :group 'gnus-summary
944   :type 'hook)
945
946 (defcustom gnus-summary-article-move-hook nil
947   "*A hook called after an article is moved, copied, respooled, or crossposted."
948   :group 'gnus-summary
949   :type 'hook)
950
951 (defcustom gnus-summary-article-delete-hook nil
952   "*A hook called after an article is deleted."
953   :group 'gnus-summary
954   :type 'hook)
955
956 (defcustom gnus-summary-article-expire-hook nil
957   "*A hook called after an article is expired."
958   :group 'gnus-summary
959   :type 'hook)
960
961 (defcustom gnus-summary-display-arrow
962   (and (fboundp 'display-graphic-p)
963        (display-graphic-p))
964   "*If non-nil, display an arrow highlighting the current article."
965   :version "21.4"
966   :group 'gnus-summary
967   :type 'boolean)
968
969 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
970   "Face used for highlighting the current article in the summary buffer."
971   :group 'gnus-summary-visual
972   :type 'face)
973
974 (defvar gnus-tmp-downloaded nil)
975
976 (defcustom gnus-summary-highlight
977   '(((eq mark gnus-canceled-mark)
978      . gnus-summary-cancelled-face)
979     ((and uncached (> score default-high))
980      . gnus-summary-high-undownloaded-face)
981     ((and uncached (< score default-low))
982      . gnus-summary-low-undownloaded-face)
983     (uncached
984      . gnus-summary-normal-undownloaded-face)
985     ((and (> score default-high)
986           (or (eq mark gnus-dormant-mark)
987               (eq mark gnus-ticked-mark)))
988      . gnus-summary-high-ticked-face)
989     ((and (< score default-low)
990           (or (eq mark gnus-dormant-mark)
991               (eq mark gnus-ticked-mark)))
992      . gnus-summary-low-ticked-face)
993     ((or (eq mark gnus-dormant-mark)
994          (eq mark gnus-ticked-mark))
995      . gnus-summary-normal-ticked-face)
996     ((and (> score default-high) (eq mark gnus-ancient-mark))
997      . gnus-summary-high-ancient-face)
998     ((and (< score default-low) (eq mark gnus-ancient-mark))
999      . gnus-summary-low-ancient-face)
1000     ((eq mark gnus-ancient-mark)
1001      . gnus-summary-normal-ancient-face)
1002     ((and (> score default-high) (eq mark gnus-unread-mark))
1003      . gnus-summary-high-unread-face)
1004     ((and (< score default-low) (eq mark gnus-unread-mark))
1005      . gnus-summary-low-unread-face)
1006     ((eq mark gnus-unread-mark)
1007      . gnus-summary-normal-unread-face)
1008     ((> score default-high)
1009      . gnus-summary-high-read-face)
1010     ((< score default-low)
1011      . gnus-summary-low-read-face)
1012     (t
1013      . gnus-summary-normal-read-face))
1014   "*Controls the highlighting of summary buffer lines.
1015
1016 A list of (FORM . FACE) pairs.  When deciding how a a particular
1017 summary line should be displayed, each form is evaluated.  The content
1018 of the face field after the first true form is used.  You can change
1019 how those summary lines are displayed, by editing the face field.
1020
1021 You can use the following variables in the FORM field.
1022
1023 score:        The article's score
1024 default:      The default article score.
1025 default-high: The default score for high scored articles.
1026 default-low:  The default score for low scored articles.
1027 below:        The score below which articles are automatically marked as read.
1028 mark:         The article's mark.
1029 uncached:     Non-nil if the article is uncached."
1030   :group 'gnus-summary-visual
1031   :type '(repeat (cons (sexp :tag "Form" nil)
1032                        face)))
1033
1034 (defcustom gnus-alter-header-function nil
1035   "Function called to allow alteration of article header structures.
1036 The function is called with one parameter, the article header vector,
1037 which it may alter in any way."
1038   :type '(choice (const :tag "None" nil)
1039                  function)
1040   :group 'gnus-summary)
1041
1042 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1043   "Variable that says which function should be used to decode a string with encoded words.")
1044
1045 (defcustom gnus-extra-headers '(To Newsgroups)
1046   "*Extra headers to parse."
1047   :version "21.1"
1048   :group 'gnus-summary
1049   :type '(repeat symbol))
1050
1051 (defcustom gnus-ignored-from-addresses
1052   (and user-mail-address (regexp-quote user-mail-address))
1053   "*Regexp of From headers that may be suppressed in favor of To headers."
1054   :version "21.1"
1055   :group 'gnus-summary
1056   :type 'regexp)
1057
1058 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1059   "List of charsets that should be ignored.
1060 When these charsets are used in the \"charset\" parameter, the
1061 default charset will be used instead."
1062   :version "21.1"
1063   :type '(repeat symbol)
1064   :group 'gnus-charset)
1065
1066 (gnus-define-group-parameter
1067  ignored-charsets
1068  :type list
1069  :function-document
1070  "Return the ignored charsets of GROUP."
1071  :variable gnus-group-ignored-charsets-alist
1072  :variable-default
1073  '(("alt\\.chinese\\.text" iso-8859-1))
1074  :variable-document
1075  "Alist of regexps (to match group names) and charsets that should be ignored.
1076 When these charsets are used in the \"charset\" parameter, the
1077 default charset will be used instead."
1078  :variable-group gnus-charset
1079  :variable-type '(repeat (cons (regexp :tag "Group")
1080                                (repeat symbol)))
1081  :parameter-type '(choice :tag "Ignored charsets"
1082                           :value nil
1083                           (repeat (symbol)))
1084  :parameter-document       "\
1085 List of charsets that should be ignored.
1086
1087 When these charsets are used in the \"charset\" parameter, the
1088 default charset will be used instead.")
1089
1090 (defcustom gnus-group-highlight-words-alist nil
1091   "Alist of group regexps and highlight regexps.
1092 This variable uses the same syntax as `gnus-emphasis-alist'."
1093   :version "21.1"
1094   :type '(repeat (cons (regexp :tag "Group")
1095                        (repeat (list (regexp :tag "Highlight regexp")
1096                                      (number :tag "Group for entire word" 0)
1097                                      (number :tag "Group for displayed part" 0)
1098                                      (symbol :tag "Face"
1099                                              gnus-emphasis-highlight-words)))))
1100   :group 'gnus-summary-visual)
1101
1102 (defcustom gnus-summary-show-article-charset-alist
1103   nil
1104   "Alist of number and charset.
1105 The article will be shown with the charset corresponding to the
1106 numbered argument.
1107 For example: ((1 . cn-gb-2312) (2 . big5))."
1108   :version "21.1"
1109   :type '(repeat (cons (number :tag "Argument" 1)
1110                        (symbol :tag "Charset")))
1111   :group 'gnus-charset)
1112
1113 (defcustom gnus-preserve-marks t
1114   "Whether marks are preserved when moving, copying and respooling messages."
1115   :version "21.1"
1116   :type 'boolean
1117   :group 'gnus-summary-marks)
1118
1119 (defcustom gnus-alter-articles-to-read-function nil
1120   "Function to be called to alter the list of articles to be selected."
1121   :type '(choice (const nil) function)
1122   :group 'gnus-summary)
1123
1124 (defcustom gnus-orphan-score nil
1125   "*All orphans get this score added.  Set in the score file."
1126   :group 'gnus-score-default
1127   :type '(choice (const nil)
1128                  integer))
1129
1130 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1131   "*A regexp to match MIME parts when saving multiple parts of a
1132 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1133 This regexp will be used by default when prompting the user for which
1134 type of files to save."
1135   :group 'gnus-summary
1136   :type 'regexp)
1137
1138 (defcustom gnus-read-all-available-headers nil
1139   "Whether Gnus should parse all headers made available to it.
1140 This is mostly relevant for slow back ends where the user may
1141 wish to widen the summary buffer to include all headers
1142 that were fetched.  Say, for nnultimate groups."
1143   :version "21.4"
1144   :group 'gnus-summary
1145   :type '(choice boolean regexp))
1146
1147 (defcustom gnus-summary-muttprint-program "muttprint"
1148   "Command (and optional arguments) used to run Muttprint."
1149   :version "21.4"
1150   :group 'gnus-summary
1151   :type 'string)
1152
1153 (defcustom gnus-article-loose-mime t
1154   "If non-nil, don't require MIME-Version header.
1155 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1156 supply the MIME-Version header or deliberately strip it from the mail.
1157 If non-nil (the default), Gnus will treat some articles as MIME
1158 even if the MIME-Version header is missing."
1159   :version "21.4"
1160   :type 'boolean
1161   :group 'gnus-article-mime)
1162
1163 (defcustom gnus-article-emulate-mime t
1164   "If non-nil, use MIME emulation for uuencode and the like.
1165 This means that Gnus will search message bodies for text that look
1166 like uuencoded bits, yEncoded bits, and so on, and present that using
1167 the normal Gnus MIME machinery."
1168   :version "21.4"
1169   :type 'boolean
1170   :group 'gnus-article-mime)
1171
1172 ;;; Internal variables
1173
1174 (defvar gnus-summary-display-cache nil)
1175 (defvar gnus-article-mime-handles nil)
1176 (defvar gnus-article-decoded-p nil)
1177 (defvar gnus-article-charset nil)
1178 (defvar gnus-article-ignored-charsets nil)
1179 (defvar gnus-scores-exclude-files nil)
1180 (defvar gnus-page-broken nil)
1181
1182 (defvar gnus-original-article nil)
1183 (defvar gnus-article-internal-prepare-hook nil)
1184 (defvar gnus-newsgroup-process-stack nil)
1185
1186 (defvar gnus-thread-indent-array nil)
1187 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1188 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1189   "Function called to sort the articles within a thread after it has been gathered together.")
1190
1191 (defvar gnus-summary-save-parts-type-history nil)
1192 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1193
1194 ;; Avoid highlighting in kill files.
1195 (defvar gnus-summary-inhibit-highlight nil)
1196 (defvar gnus-newsgroup-selected-overlay nil)
1197 (defvar gnus-inhibit-limiting nil)
1198 (defvar gnus-newsgroup-adaptive-score-file nil)
1199 (defvar gnus-current-score-file nil)
1200 (defvar gnus-current-move-group nil)
1201 (defvar gnus-current-copy-group nil)
1202 (defvar gnus-current-crosspost-group nil)
1203 (defvar gnus-newsgroup-display nil)
1204
1205 (defvar gnus-newsgroup-dependencies nil)
1206 (defvar gnus-newsgroup-adaptive nil)
1207 (defvar gnus-summary-display-article-function nil)
1208 (defvar gnus-summary-highlight-line-function nil
1209   "Function called after highlighting a summary line.")
1210
1211 (defvar gnus-summary-line-format-alist
1212   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1213     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1214     (?s gnus-tmp-subject-or-nil ?s)
1215     (?n gnus-tmp-name ?s)
1216     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1217         ?s)
1218     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1219             gnus-tmp-from) ?s)
1220     (?F gnus-tmp-from ?s)
1221     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1222     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1223     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1224     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1225     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1226     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1227     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1228     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1229     (?L gnus-tmp-lines ?s)
1230     (?O gnus-tmp-downloaded ?c)
1231     (?I gnus-tmp-indentation ?s)
1232     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1233     (?R gnus-tmp-replied ?c)
1234     (?\[ gnus-tmp-opening-bracket ?c)
1235     (?\] gnus-tmp-closing-bracket ?c)
1236     (?\> (make-string gnus-tmp-level ? ) ?s)
1237     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1238     (?i gnus-tmp-score ?d)
1239     (?z gnus-tmp-score-char ?c)
1240     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1241     (?U gnus-tmp-unread ?c)
1242     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1243         ?s)
1244     (?t (gnus-summary-number-of-articles-in-thread
1245          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1246         ?d)
1247     (?e (gnus-summary-number-of-articles-in-thread
1248          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1249         ?c)
1250     (?u gnus-tmp-user-defined ?s)
1251     (?P (gnus-pick-line-number) ?d)
1252     (?B gnus-tmp-thread-tree-header-string ?s)
1253     (user-date (gnus-user-date
1254                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1255   "An alist of format specifications that can appear in summary lines.
1256 These are paired with what variables they correspond with, along with
1257 the type of the variable (string, integer, character, etc).")
1258
1259 (defvar gnus-summary-dummy-line-format-alist
1260   `((?S gnus-tmp-subject ?s)
1261     (?N gnus-tmp-number ?d)
1262     (?u gnus-tmp-user-defined ?s)))
1263
1264 (defvar gnus-summary-mode-line-format-alist
1265   `((?G gnus-tmp-group-name ?s)
1266     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1267     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1268     (?A gnus-tmp-article-number ?d)
1269     (?Z gnus-tmp-unread-and-unselected ?s)
1270     (?V gnus-version ?s)
1271     (?U gnus-tmp-unread-and-unticked ?d)
1272     (?S gnus-tmp-subject ?s)
1273     (?e gnus-tmp-unselected ?d)
1274     (?u gnus-tmp-user-defined ?s)
1275     (?d (length gnus-newsgroup-dormant) ?d)
1276     (?t (length gnus-newsgroup-marked) ?d)
1277     (?h (length gnus-newsgroup-spam-marked) ?d)
1278     (?r (length gnus-newsgroup-reads) ?d)
1279     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1280     (?E gnus-newsgroup-expunged-tally ?d)
1281     (?s (gnus-current-score-file-nondirectory) ?s)))
1282
1283 (defvar gnus-last-search-regexp nil
1284   "Default regexp for article search command.")
1285
1286 (defvar gnus-last-shell-command nil
1287   "Default shell command on article.")
1288
1289 (defvar gnus-newsgroup-agentized nil
1290   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1291 (defvar gnus-newsgroup-begin nil)
1292 (defvar gnus-newsgroup-end nil)
1293 (defvar gnus-newsgroup-last-rmail nil)
1294 (defvar gnus-newsgroup-last-mail nil)
1295 (defvar gnus-newsgroup-last-folder nil)
1296 (defvar gnus-newsgroup-last-file nil)
1297 (defvar gnus-newsgroup-auto-expire nil)
1298 (defvar gnus-newsgroup-active nil)
1299
1300 (defvar gnus-newsgroup-data nil)
1301 (defvar gnus-newsgroup-data-reverse nil)
1302 (defvar gnus-newsgroup-limit nil)
1303 (defvar gnus-newsgroup-limits nil)
1304 (defvar gnus-summary-use-undownloaded-faces nil)
1305
1306 (defvar gnus-newsgroup-unreads nil
1307   "Sorted list of unread articles in the current newsgroup.")
1308
1309 (defvar gnus-newsgroup-unselected nil
1310   "Sorted list of unselected unread articles in the current newsgroup.")
1311
1312 (defvar gnus-newsgroup-reads nil
1313   "Alist of read articles and article marks in the current newsgroup.")
1314
1315 (defvar gnus-newsgroup-expunged-tally nil)
1316
1317 (defvar gnus-newsgroup-marked nil
1318   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1319
1320 (defvar gnus-newsgroup-spam-marked nil
1321   "List of ranges of articles that have been marked as spam.")
1322
1323 (defvar gnus-newsgroup-killed nil
1324   "List of ranges of articles that have been through the scoring process.")
1325
1326 (defvar gnus-newsgroup-cached nil
1327   "Sorted list of articles that come from the article cache.")
1328
1329 (defvar gnus-newsgroup-saved nil
1330   "List of articles that have been saved.")
1331
1332 (defvar gnus-newsgroup-kill-headers nil)
1333
1334 (defvar gnus-newsgroup-replied nil
1335   "List of articles that have been replied to in the current newsgroup.")
1336
1337 (defvar gnus-newsgroup-forwarded nil
1338   "List of articles that have been forwarded in the current newsgroup.")
1339
1340 (defvar gnus-newsgroup-recent nil
1341   "List of articles that have are recent in the current newsgroup.")
1342
1343 (defvar gnus-newsgroup-expirable nil
1344   "Sorted list of articles in the current newsgroup that can be expired.")
1345
1346 (defvar gnus-newsgroup-processable nil
1347   "List of articles in the current newsgroup that can be processed.")
1348
1349 (defvar gnus-newsgroup-downloadable nil
1350   "Sorted list of articles in the current newsgroup that can be processed.")
1351
1352 (defvar gnus-newsgroup-unfetched nil
1353   "Sorted list of articles in the current newsgroup whose headers have
1354 not been fetched into the agent.
1355
1356 This list will always be a subset of gnus-newsgroup-undownloaded.")
1357
1358 (defvar gnus-newsgroup-undownloaded nil
1359   "List of articles in the current newsgroup that haven't been downloaded.")
1360
1361 (defvar gnus-newsgroup-unsendable nil
1362   "List of articles in the current newsgroup that won't be sent.")
1363
1364 (defvar gnus-newsgroup-bookmarks nil
1365   "List of articles in the current newsgroup that have bookmarks.")
1366
1367 (defvar gnus-newsgroup-dormant nil
1368   "Sorted list of dormant articles in the current newsgroup.")
1369
1370 (defvar gnus-newsgroup-unseen nil
1371   "List of unseen articles in the current newsgroup.")
1372
1373 (defvar gnus-newsgroup-seen nil
1374   "Range of seen articles in the current newsgroup.")
1375
1376 (defvar gnus-newsgroup-articles nil
1377   "List of articles in the current newsgroup.")
1378
1379 (defvar gnus-newsgroup-scored nil
1380   "List of scored articles in the current newsgroup.")
1381
1382 (defvar gnus-newsgroup-headers nil
1383   "List of article headers in the current newsgroup.")
1384
1385 (defvar gnus-newsgroup-threads nil)
1386
1387 (defvar gnus-newsgroup-prepared nil
1388   "Whether the current group has been prepared properly.")
1389
1390 (defvar gnus-newsgroup-ancient nil
1391   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1392
1393 (defvar gnus-newsgroup-sparse nil)
1394
1395 (defvar gnus-current-article nil)
1396 (defvar gnus-article-current nil)
1397 (defvar gnus-current-headers nil)
1398 (defvar gnus-have-all-headers nil)
1399 (defvar gnus-last-article nil)
1400 (defvar gnus-newsgroup-history nil)
1401 (defvar gnus-newsgroup-charset nil)
1402 (defvar gnus-newsgroup-ephemeral-charset nil)
1403 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1404
1405 (defvar gnus-article-before-search nil)
1406
1407 (defvar gnus-summary-local-variables
1408   '(gnus-newsgroup-name
1409     gnus-newsgroup-begin gnus-newsgroup-end
1410     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1411     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1412     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1413     gnus-newsgroup-unselected gnus-newsgroup-marked
1414     gnus-newsgroup-spam-marked
1415     gnus-newsgroup-reads gnus-newsgroup-saved
1416     gnus-newsgroup-replied gnus-newsgroup-forwarded
1417     gnus-newsgroup-recent
1418     gnus-newsgroup-expirable
1419     gnus-newsgroup-processable gnus-newsgroup-killed
1420     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1421     gnus-newsgroup-unfetched
1422     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1423     gnus-newsgroup-seen gnus-newsgroup-articles
1424     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1425     gnus-newsgroup-headers gnus-newsgroup-threads
1426     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1427     gnus-current-article gnus-current-headers gnus-have-all-headers
1428     gnus-last-article gnus-article-internal-prepare-hook
1429     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1430     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1431     gnus-thread-expunge-below
1432     gnus-score-alist gnus-current-score-file
1433     (gnus-summary-expunge-below . global)
1434     (gnus-summary-mark-below . global)
1435     (gnus-orphan-score . global)
1436     gnus-newsgroup-active gnus-scores-exclude-files
1437     gnus-newsgroup-history gnus-newsgroup-ancient
1438     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1439     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1440     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1441     (gnus-newsgroup-expunged-tally . 0)
1442     gnus-cache-removable-articles gnus-newsgroup-cached
1443     gnus-newsgroup-data gnus-newsgroup-data-reverse
1444     gnus-newsgroup-limit gnus-newsgroup-limits
1445     gnus-newsgroup-charset gnus-newsgroup-display
1446     gnus-summary-use-undownloaded-faces)
1447   "Variables that are buffer-local to the summary buffers.")
1448
1449 (defvar gnus-newsgroup-variables nil
1450   "A list of variables that have separate values in different newsgroups.
1451 A list of newsgroup (summary buffer) local variables, or cons of
1452 variables and their default expressions to be evalled (when the default
1453 values are not nil), that should be made global while the summary buffer
1454 is active.
1455
1456 Note: The default expressions will be evaluated (using function `eval')
1457 before assignment to the local variable rather than just assigned to it.
1458 If the default expression is the symbol `global', that symbol will not
1459 be evaluated but the global value of the local variable will be used
1460 instead.
1461
1462 These variables can be used to set variables in the group parameters
1463 while still allowing them to affect operations done in other buffers.
1464 For example:
1465
1466 \(setq gnus-newsgroup-variables
1467      '(message-use-followup-to
1468        (gnus-visible-headers .
1469          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1470 ")
1471
1472 ;; Byte-compiler warning.
1473 (eval-when-compile
1474   ;; Bind features so that require will believe that gnus-sum has
1475   ;; already been loaded (avoids infinite recursion)
1476   (let ((features (cons 'gnus-sum features)))
1477     ;; Several of the declarations in gnus-sum are needed to load the
1478     ;; following files. Right now, these definitions have been
1479     ;; compiled but not defined (evaluated).  We could either do a
1480     ;; eval-and-compile about all of the declarations or evaluate the
1481     ;; source file.
1482     (if (boundp 'gnus-newsgroup-variables)
1483         nil
1484       (load "gnus-sum.el" t t t))
1485     (require 'gnus)
1486     (require 'gnus-art)))
1487
1488 ;; MIME stuff.
1489
1490 (defvar gnus-decode-encoded-word-methods
1491   '(mail-decode-encoded-word-string)
1492   "List of methods used to decode encoded words.
1493
1494 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1495 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1496 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1497 whose names match REGEXP.
1498
1499 For example:
1500 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1501  mail-decode-encoded-word-string
1502  (\"chinese\" . rfc1843-decode-string))")
1503
1504 (defvar gnus-decode-encoded-word-methods-cache nil)
1505
1506 (defun gnus-multi-decode-encoded-word-string (string)
1507   "Apply the functions from `gnus-encoded-word-methods' that match."
1508   (unless (and gnus-decode-encoded-word-methods-cache
1509                (eq gnus-newsgroup-name
1510                    (car gnus-decode-encoded-word-methods-cache)))
1511     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1512     (mapcar (lambda (x)
1513               (if (symbolp x)
1514                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1515                 (if (and gnus-newsgroup-name
1516                          (string-match (car x) gnus-newsgroup-name))
1517                     (nconc gnus-decode-encoded-word-methods-cache
1518                            (list (cdr x))))))
1519             gnus-decode-encoded-word-methods))
1520   (let ((xlist gnus-decode-encoded-word-methods-cache))
1521     (pop xlist)
1522     (while xlist
1523       (setq string (funcall (pop xlist) string))))
1524   string)
1525
1526 ;; Subject simplification.
1527
1528 (defun gnus-simplify-whitespace (str)
1529   "Remove excessive whitespace from STR."
1530   ;; Multiple spaces.
1531   (while (string-match "[ \t][ \t]+" str)
1532     (setq str (concat (substring str 0 (match-beginning 0))
1533                         " "
1534                         (substring str (match-end 0)))))
1535   ;; Leading spaces.
1536   (when (string-match "^[ \t]+" str)
1537     (setq str (substring str (match-end 0))))
1538   ;; Trailing spaces.
1539   (when (string-match "[ \t]+$" str)
1540     (setq str (substring str 0 (match-beginning 0))))
1541   str)
1542
1543 (defun gnus-simplify-all-whitespace (str)
1544   "Remove all whitespace from STR."
1545   (while (string-match "[ \t\n]+" str)
1546     (setq str (replace-match "" nil nil str)))
1547   str)
1548
1549 (defsubst gnus-simplify-subject-re (subject)
1550   "Remove \"Re:\" from subject lines."
1551   (if (string-match message-subject-re-regexp subject)
1552       (substring subject (match-end 0))
1553     subject))
1554
1555 (defun gnus-simplify-subject (subject &optional re-only)
1556   "Remove `Re:' and words in parentheses.
1557 If RE-ONLY is non-nil, strip leading `Re:'s only."
1558   (let ((case-fold-search t))           ;Ignore case.
1559     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1560     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1561       (setq subject (substring subject (match-end 0))))
1562     ;; Remove uninteresting prefixes.
1563     (when (and (not re-only)
1564                gnus-simplify-ignored-prefixes
1565                (string-match gnus-simplify-ignored-prefixes subject))
1566       (setq subject (substring subject (match-end 0))))
1567     ;; Remove words in parentheses from end.
1568     (unless re-only
1569       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1570         (setq subject (substring subject 0 (match-beginning 0)))))
1571     ;; Return subject string.
1572     subject))
1573
1574 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1575 ;; all whitespace.
1576 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1577   (goto-char (point-min))
1578   (while (re-search-forward regexp nil t)
1579     (replace-match (or newtext ""))))
1580
1581 (defun gnus-simplify-buffer-fuzzy ()
1582   "Simplify string in the buffer fuzzily.
1583 The string in the accessible portion of the current buffer is simplified.
1584 It is assumed to be a single-line subject.
1585 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1586 matter is removed.  Additional things can be deleted by setting
1587 `gnus-simplify-subject-fuzzy-regexp'."
1588   (let ((case-fold-search t)
1589         (modified-tick))
1590     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1591
1592     (while (not (eq modified-tick (buffer-modified-tick)))
1593       (setq modified-tick (buffer-modified-tick))
1594       (cond
1595        ((listp gnus-simplify-subject-fuzzy-regexp)
1596         (mapcar 'gnus-simplify-buffer-fuzzy-step
1597                 gnus-simplify-subject-fuzzy-regexp))
1598        (gnus-simplify-subject-fuzzy-regexp
1599         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1600       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1601       (gnus-simplify-buffer-fuzzy-step
1602        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1603       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1604
1605     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1606     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1607     (gnus-simplify-buffer-fuzzy-step " $")
1608     (gnus-simplify-buffer-fuzzy-step "^ +")))
1609
1610 (defun gnus-simplify-subject-fuzzy (subject)
1611   "Simplify a subject string fuzzily.
1612 See `gnus-simplify-buffer-fuzzy' for details."
1613   (save-excursion
1614     (gnus-set-work-buffer)
1615     (let ((case-fold-search t))
1616       ;; Remove uninteresting prefixes.
1617       (when (and gnus-simplify-ignored-prefixes
1618                  (string-match gnus-simplify-ignored-prefixes subject))
1619         (setq subject (substring subject (match-end 0))))
1620       (insert subject)
1621       (inline (gnus-simplify-buffer-fuzzy))
1622       (buffer-string))))
1623
1624 (defsubst gnus-simplify-subject-fully (subject)
1625   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1626   (cond
1627    (gnus-simplify-subject-functions
1628     (gnus-map-function gnus-simplify-subject-functions subject))
1629    ((null gnus-summary-gather-subject-limit)
1630     (gnus-simplify-subject-re subject))
1631    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1632     (gnus-simplify-subject-fuzzy subject))
1633    ((numberp gnus-summary-gather-subject-limit)
1634     (gnus-limit-string (gnus-simplify-subject-re subject)
1635                        gnus-summary-gather-subject-limit))
1636    (t
1637     subject)))
1638
1639 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1640   "Check whether two subjects are equal.
1641 If optional argument SIMPLE-FIRST is t, first argument is already
1642 simplified."
1643   (cond
1644    ((null simple-first)
1645     (equal (gnus-simplify-subject-fully s1)
1646            (gnus-simplify-subject-fully s2)))
1647    (t
1648     (equal s1
1649            (gnus-simplify-subject-fully s2)))))
1650
1651 (defun gnus-summary-bubble-group ()
1652   "Increase the score of the current group.
1653 This is a handy function to add to `gnus-summary-exit-hook' to
1654 increase the score of each group you read."
1655   (gnus-group-add-score gnus-newsgroup-name))
1656
1657 \f
1658 ;;;
1659 ;;; Gnus summary mode
1660 ;;;
1661
1662 (put 'gnus-summary-mode 'mode-class 'special)
1663
1664 (defvar gnus-article-commands-menu)
1665
1666 ;; Non-orthogonal keys
1667
1668 (gnus-define-keys gnus-summary-mode-map
1669   " " gnus-summary-next-page
1670   "\177" gnus-summary-prev-page
1671   [delete] gnus-summary-prev-page
1672   [backspace] gnus-summary-prev-page
1673   "\r" gnus-summary-scroll-up
1674   "\M-\r" gnus-summary-scroll-down
1675   "n" gnus-summary-next-unread-article
1676   "p" gnus-summary-prev-unread-article
1677   "N" gnus-summary-next-article
1678   "P" gnus-summary-prev-article
1679   "\M-\C-n" gnus-summary-next-same-subject
1680   "\M-\C-p" gnus-summary-prev-same-subject
1681   "\M-n" gnus-summary-next-unread-subject
1682   "\M-p" gnus-summary-prev-unread-subject
1683   "." gnus-summary-first-unread-article
1684   "," gnus-summary-best-unread-article
1685   "\M-s" gnus-summary-search-article-forward
1686   "\M-r" gnus-summary-search-article-backward
1687   "<" gnus-summary-beginning-of-article
1688   ">" gnus-summary-end-of-article
1689   "j" gnus-summary-goto-article
1690   "^" gnus-summary-refer-parent-article
1691   "\M-^" gnus-summary-refer-article
1692   "u" gnus-summary-tick-article-forward
1693   "!" gnus-summary-tick-article-forward
1694   "U" gnus-summary-tick-article-backward
1695   "d" gnus-summary-mark-as-read-forward
1696   "D" gnus-summary-mark-as-read-backward
1697   "E" gnus-summary-mark-as-expirable
1698   "\M-u" gnus-summary-clear-mark-forward
1699   "\M-U" gnus-summary-clear-mark-backward
1700   "k" gnus-summary-kill-same-subject-and-select
1701   "\C-k" gnus-summary-kill-same-subject
1702   "\M-\C-k" gnus-summary-kill-thread
1703   "\M-\C-l" gnus-summary-lower-thread
1704   "e" gnus-summary-edit-article
1705   "#" gnus-summary-mark-as-processable
1706   "\M-#" gnus-summary-unmark-as-processable
1707   "\M-\C-t" gnus-summary-toggle-threads
1708   "\M-\C-s" gnus-summary-show-thread
1709   "\M-\C-h" gnus-summary-hide-thread
1710   "\M-\C-f" gnus-summary-next-thread
1711   "\M-\C-b" gnus-summary-prev-thread
1712   [(meta down)] gnus-summary-next-thread
1713   [(meta up)] gnus-summary-prev-thread
1714   "\M-\C-u" gnus-summary-up-thread
1715   "\M-\C-d" gnus-summary-down-thread
1716   "&" gnus-summary-execute-command
1717   "c" gnus-summary-catchup-and-exit
1718   "\C-w" gnus-summary-mark-region-as-read
1719   "\C-t" gnus-summary-toggle-truncation
1720   "?" gnus-summary-mark-as-dormant
1721   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1722   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1723   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1724   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1725   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1726   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1727   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1728   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1729   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1730   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1731   "=" gnus-summary-expand-window
1732   "\C-x\C-s" gnus-summary-reselect-current-group
1733   "\M-g" gnus-summary-rescan-group
1734   "w" gnus-summary-stop-page-breaking
1735   "\C-c\C-r" gnus-summary-caesar-message
1736   "f" gnus-summary-followup
1737   "F" gnus-summary-followup-with-original
1738   "C" gnus-summary-cancel-article
1739   "r" gnus-summary-reply
1740   "R" gnus-summary-reply-with-original
1741   "\C-c\C-f" gnus-summary-mail-forward
1742   "o" gnus-summary-save-article
1743   "\C-o" gnus-summary-save-article-mail
1744   "|" gnus-summary-pipe-output
1745   "\M-k" gnus-summary-edit-local-kill
1746   "\M-K" gnus-summary-edit-global-kill
1747   ;; "V" gnus-version
1748   "\C-c\C-d" gnus-summary-describe-group
1749   "q" gnus-summary-exit
1750   "Q" gnus-summary-exit-no-update
1751   "\C-c\C-i" gnus-info-find-node
1752   gnus-mouse-2 gnus-mouse-pick-article
1753   "m" gnus-summary-mail-other-window
1754   "a" gnus-summary-post-news
1755   "i" gnus-summary-news-other-window
1756   "x" gnus-summary-limit-to-unread
1757   "s" gnus-summary-isearch-article
1758   "t" gnus-summary-toggle-header
1759   "g" gnus-summary-show-article
1760   "l" gnus-summary-goto-last-article
1761   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1762   "\C-d" gnus-summary-enter-digest-group
1763   "\M-\C-d" gnus-summary-read-document
1764   "\M-\C-e" gnus-summary-edit-parameters
1765   "\M-\C-a" gnus-summary-customize-parameters
1766   "\C-c\C-b" gnus-bug
1767   "*" gnus-cache-enter-article
1768   "\M-*" gnus-cache-remove-article
1769   "\M-&" gnus-summary-universal-argument
1770   "\C-l" gnus-recenter
1771   "I" gnus-summary-increase-score
1772   "L" gnus-summary-lower-score
1773   "\M-i" gnus-symbolic-argument
1774   "h" gnus-summary-select-article-buffer
1775
1776   "b" gnus-article-view-part
1777   "\M-t" gnus-summary-toggle-display-buttonized
1778
1779   "V" gnus-summary-score-map
1780   "X" gnus-uu-extract-map
1781   "S" gnus-summary-send-map)
1782
1783 ;; Sort of orthogonal keymap
1784 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1785   "t" gnus-summary-tick-article-forward
1786   "!" gnus-summary-tick-article-forward
1787   "d" gnus-summary-mark-as-read-forward
1788   "r" gnus-summary-mark-as-read-forward
1789   "c" gnus-summary-clear-mark-forward
1790   " " gnus-summary-clear-mark-forward
1791   "e" gnus-summary-mark-as-expirable
1792   "x" gnus-summary-mark-as-expirable
1793   "?" gnus-summary-mark-as-dormant
1794   "b" gnus-summary-set-bookmark
1795   "B" gnus-summary-remove-bookmark
1796   "#" gnus-summary-mark-as-processable
1797   "\M-#" gnus-summary-unmark-as-processable
1798   "S" gnus-summary-limit-include-expunged
1799   "C" gnus-summary-catchup
1800   "H" gnus-summary-catchup-to-here
1801   "h" gnus-summary-catchup-from-here
1802   "\C-c" gnus-summary-catchup-all
1803   "k" gnus-summary-kill-same-subject-and-select
1804   "K" gnus-summary-kill-same-subject
1805   "P" gnus-uu-mark-map)
1806
1807 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1808   "c" gnus-summary-clear-above
1809   "u" gnus-summary-tick-above
1810   "m" gnus-summary-mark-above
1811   "k" gnus-summary-kill-below)
1812
1813 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1814   "/" gnus-summary-limit-to-subject
1815   "n" gnus-summary-limit-to-articles
1816   "w" gnus-summary-pop-limit
1817   "s" gnus-summary-limit-to-subject
1818   "a" gnus-summary-limit-to-author
1819   "u" gnus-summary-limit-to-unread
1820   "m" gnus-summary-limit-to-marks
1821   "M" gnus-summary-limit-exclude-marks
1822   "v" gnus-summary-limit-to-score
1823   "*" gnus-summary-limit-include-cached
1824   "D" gnus-summary-limit-include-dormant
1825   "T" gnus-summary-limit-include-thread
1826   "d" gnus-summary-limit-exclude-dormant
1827   "t" gnus-summary-limit-to-age
1828   "." gnus-summary-limit-to-unseen
1829   "x" gnus-summary-limit-to-extra
1830   "p" gnus-summary-limit-to-display-predicate
1831   "E" gnus-summary-limit-include-expunged
1832   "c" gnus-summary-limit-exclude-childless-dormant
1833   "C" gnus-summary-limit-mark-excluded-as-read
1834   "o" gnus-summary-insert-old-articles
1835   "N" gnus-summary-insert-new-articles
1836   "r" gnus-summary-limit-to-replied)
1837
1838 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1839   "n" gnus-summary-next-unread-article
1840   "p" gnus-summary-prev-unread-article
1841   "N" gnus-summary-next-article
1842   "P" gnus-summary-prev-article
1843   "\C-n" gnus-summary-next-same-subject
1844   "\C-p" gnus-summary-prev-same-subject
1845   "\M-n" gnus-summary-next-unread-subject
1846   "\M-p" gnus-summary-prev-unread-subject
1847   "f" gnus-summary-first-unread-article
1848   "b" gnus-summary-best-unread-article
1849   "j" gnus-summary-goto-article
1850   "g" gnus-summary-goto-subject
1851   "l" gnus-summary-goto-last-article
1852   "o" gnus-summary-pop-article)
1853
1854 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1855   "k" gnus-summary-kill-thread
1856   "l" gnus-summary-lower-thread
1857   "i" gnus-summary-raise-thread
1858   "T" gnus-summary-toggle-threads
1859   "t" gnus-summary-rethread-current
1860   "^" gnus-summary-reparent-thread
1861   "s" gnus-summary-show-thread
1862   "S" gnus-summary-show-all-threads
1863   "h" gnus-summary-hide-thread
1864   "H" gnus-summary-hide-all-threads
1865   "n" gnus-summary-next-thread
1866   "p" gnus-summary-prev-thread
1867   "u" gnus-summary-up-thread
1868   "o" gnus-summary-top-thread
1869   "d" gnus-summary-down-thread
1870   "#" gnus-uu-mark-thread
1871   "\M-#" gnus-uu-unmark-thread)
1872
1873 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1874   "g" gnus-summary-prepare
1875   "c" gnus-summary-insert-cached-articles
1876   "d" gnus-summary-insert-dormant-articles)
1877
1878 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1879   "c" gnus-summary-catchup-and-exit
1880   "C" gnus-summary-catchup-all-and-exit
1881   "E" gnus-summary-exit-no-update
1882   "Q" gnus-summary-exit
1883   "Z" gnus-summary-exit
1884   "n" gnus-summary-catchup-and-goto-next-group
1885   "R" gnus-summary-reselect-current-group
1886   "G" gnus-summary-rescan-group
1887   "N" gnus-summary-next-group
1888   "s" gnus-summary-save-newsrc
1889   "P" gnus-summary-prev-group)
1890
1891 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1892   " " gnus-summary-next-page
1893   "n" gnus-summary-next-page
1894   "\177" gnus-summary-prev-page
1895   [delete] gnus-summary-prev-page
1896   "p" gnus-summary-prev-page
1897   "\r" gnus-summary-scroll-up
1898   "\M-\r" gnus-summary-scroll-down
1899   "<" gnus-summary-beginning-of-article
1900   ">" gnus-summary-end-of-article
1901   "b" gnus-summary-beginning-of-article
1902   "e" gnus-summary-end-of-article
1903   "^" gnus-summary-refer-parent-article
1904   "r" gnus-summary-refer-parent-article
1905   "D" gnus-summary-enter-digest-group
1906   "R" gnus-summary-refer-references
1907   "T" gnus-summary-refer-thread
1908   "g" gnus-summary-show-article
1909   "s" gnus-summary-isearch-article
1910   "P" gnus-summary-print-article
1911   "M" gnus-mailing-list-insinuate
1912   "t" gnus-article-babel)
1913
1914 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1915   "b" gnus-article-add-buttons
1916   "B" gnus-article-add-buttons-to-head
1917   "o" gnus-article-treat-overstrike
1918   "e" gnus-article-emphasize
1919   "w" gnus-article-fill-cited-article
1920   "Q" gnus-article-fill-long-lines
1921   "C" gnus-article-capitalize-sentences
1922   "c" gnus-article-remove-cr
1923   "q" gnus-article-de-quoted-unreadable
1924   "6" gnus-article-de-base64-unreadable
1925   "Z" gnus-article-decode-HZ
1926   "A" gnus-article-treat-ansi-sequences
1927   "h" gnus-article-wash-html
1928   "u" gnus-article-unsplit-urls
1929   "s" gnus-summary-force-verify-and-decrypt
1930   "f" gnus-article-display-x-face
1931   "l" gnus-summary-stop-page-breaking
1932   "r" gnus-summary-caesar-message
1933   "m" gnus-summary-morse-message
1934   "t" gnus-summary-toggle-header
1935   "g" gnus-treat-smiley
1936   "v" gnus-summary-verbose-headers
1937   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1938   "p" gnus-article-verify-x-pgp-sig
1939   "d" gnus-article-treat-dumbquotes)
1940
1941 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1942   ;; mnemonic: deuglif*Y*
1943   "u" gnus-article-outlook-unwrap-lines
1944   "a" gnus-article-outlook-repair-attribution
1945   "c" gnus-article-outlook-rearrange-citation
1946   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1947
1948 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1949   "a" gnus-article-hide
1950   "h" gnus-article-hide-headers
1951   "b" gnus-article-hide-boring-headers
1952   "s" gnus-article-hide-signature
1953   "c" gnus-article-hide-citation
1954   "C" gnus-article-hide-citation-in-followups
1955   "l" gnus-article-hide-list-identifiers
1956   "B" gnus-article-strip-banner
1957   "P" gnus-article-hide-pem
1958   "\C-c" gnus-article-hide-citation-maybe)
1959
1960 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1961   "a" gnus-article-highlight
1962   "h" gnus-article-highlight-headers
1963   "c" gnus-article-highlight-citation
1964   "s" gnus-article-highlight-signature)
1965
1966 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1967   "f" gnus-article-treat-fold-headers
1968   "u" gnus-article-treat-unfold-headers
1969   "n" gnus-article-treat-fold-newsgroups)
1970
1971 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1972   "x" gnus-article-display-x-face
1973   "d" gnus-article-display-face
1974   "s" gnus-treat-smiley
1975   "D" gnus-article-remove-images
1976   "f" gnus-treat-from-picon
1977   "m" gnus-treat-mail-picon
1978   "n" gnus-treat-newsgroups-picon)
1979
1980 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1981   "w" gnus-article-decode-mime-words
1982   "c" gnus-article-decode-charset
1983   "v" gnus-mime-view-all-parts
1984   "b" gnus-article-view-part)
1985
1986 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1987   "z" gnus-article-date-ut
1988   "u" gnus-article-date-ut
1989   "l" gnus-article-date-local
1990   "p" gnus-article-date-english
1991   "e" gnus-article-date-lapsed
1992   "o" gnus-article-date-original
1993   "i" gnus-article-date-iso8601
1994   "s" gnus-article-date-user)
1995
1996 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1997   "t" gnus-article-remove-trailing-blank-lines
1998   "l" gnus-article-strip-leading-blank-lines
1999   "m" gnus-article-strip-multiple-blank-lines
2000   "a" gnus-article-strip-blank-lines
2001   "A" gnus-article-strip-all-blank-lines
2002   "s" gnus-article-strip-leading-space
2003   "e" gnus-article-strip-trailing-space
2004   "w" gnus-article-remove-leading-whitespace)
2005
2006 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2007   "v" gnus-version
2008   "f" gnus-summary-fetch-faq
2009   "d" gnus-summary-describe-group
2010   "h" gnus-summary-describe-briefly
2011   "i" gnus-info-find-node
2012   "c" gnus-group-fetch-charter
2013   "C" gnus-group-fetch-control)
2014
2015 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2016   "e" gnus-summary-expire-articles
2017   "\M-\C-e" gnus-summary-expire-articles-now
2018   "\177" gnus-summary-delete-article
2019   [delete] gnus-summary-delete-article
2020   [backspace] gnus-summary-delete-article
2021   "m" gnus-summary-move-article
2022   "r" gnus-summary-respool-article
2023   "w" gnus-summary-edit-article
2024   "c" gnus-summary-copy-article
2025   "B" gnus-summary-crosspost-article
2026   "q" gnus-summary-respool-query
2027   "t" gnus-summary-respool-trace
2028   "i" gnus-summary-import-article
2029   "I" gnus-summary-create-article
2030   "p" gnus-summary-article-posted-p)
2031
2032 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2033   "o" gnus-summary-save-article
2034   "m" gnus-summary-save-article-mail
2035   "F" gnus-summary-write-article-file
2036   "r" gnus-summary-save-article-rmail
2037   "f" gnus-summary-save-article-file
2038   "b" gnus-summary-save-article-body-file
2039   "h" gnus-summary-save-article-folder
2040   "v" gnus-summary-save-article-vm
2041   "p" gnus-summary-pipe-output
2042   "P" gnus-summary-muttprint
2043   "s" gnus-soup-add-article)
2044
2045 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2046   "b" gnus-summary-display-buttonized
2047   "m" gnus-summary-repair-multipart
2048   "v" gnus-article-view-part
2049   "o" gnus-article-save-part
2050   "c" gnus-article-copy-part
2051   "C" gnus-article-view-part-as-charset
2052   "e" gnus-article-view-part-externally
2053   "E" gnus-article-encrypt-body
2054   "i" gnus-article-inline-part
2055   "|" gnus-article-pipe-part)
2056
2057 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2058   "p" gnus-summary-mark-as-processable
2059   "u" gnus-summary-unmark-as-processable
2060   "U" gnus-summary-unmark-all-processable
2061   "v" gnus-uu-mark-over
2062   "s" gnus-uu-mark-series
2063   "r" gnus-uu-mark-region
2064   "g" gnus-uu-unmark-region
2065   "R" gnus-uu-mark-by-regexp
2066   "G" gnus-uu-unmark-by-regexp
2067   "t" gnus-uu-mark-thread
2068   "T" gnus-uu-unmark-thread
2069   "a" gnus-uu-mark-all
2070   "b" gnus-uu-mark-buffer
2071   "S" gnus-uu-mark-sparse
2072   "k" gnus-summary-kill-process-mark
2073   "y" gnus-summary-yank-process-mark
2074   "w" gnus-summary-save-process-mark
2075   "i" gnus-uu-invert-processable)
2076
2077 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2078   ;;"x" gnus-uu-extract-any
2079   "m" gnus-summary-save-parts
2080   "u" gnus-uu-decode-uu
2081   "U" gnus-uu-decode-uu-and-save
2082   "s" gnus-uu-decode-unshar
2083   "S" gnus-uu-decode-unshar-and-save
2084   "o" gnus-uu-decode-save
2085   "O" gnus-uu-decode-save
2086   "b" gnus-uu-decode-binhex
2087   "B" gnus-uu-decode-binhex
2088   "p" gnus-uu-decode-postscript
2089   "P" gnus-uu-decode-postscript-and-save)
2090
2091 (gnus-define-keys
2092     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2093   "u" gnus-uu-decode-uu-view
2094   "U" gnus-uu-decode-uu-and-save-view
2095   "s" gnus-uu-decode-unshar-view
2096   "S" gnus-uu-decode-unshar-and-save-view
2097   "o" gnus-uu-decode-save-view
2098   "O" gnus-uu-decode-save-view
2099   "b" gnus-uu-decode-binhex-view
2100   "B" gnus-uu-decode-binhex-view
2101   "p" gnus-uu-decode-postscript-view
2102   "P" gnus-uu-decode-postscript-and-save-view)
2103
2104 (defvar gnus-article-post-menu nil)
2105
2106 (defconst gnus-summary-menu-maxlen 20)
2107
2108 (defun gnus-summary-menu-split (menu)
2109   ;; If we have lots of elements, divide them into groups of 20
2110   ;; and make a pane (or submenu) for each one.
2111   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2112       (let ((menu menu) sublists next
2113             (i 1))
2114         (while menu
2115           ;; Pull off the next gnus-summary-menu-maxlen elements
2116           ;; and make them the next element of sublist.
2117           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2118           (if next
2119               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2120                       nil))
2121           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2122                                              (aref (car (last menu)) 0)) menu)
2123                                sublists))
2124           (setq i (1+ i))
2125           (setq menu next))
2126         (nreverse sublists))
2127     ;; Few elements--put them all in one pane.
2128     menu))
2129
2130 (defun gnus-summary-make-menu-bar ()
2131   (gnus-turn-off-edit-menu 'summary)
2132
2133   (unless (boundp 'gnus-summary-misc-menu)
2134
2135     (easy-menu-define
2136       gnus-summary-kill-menu gnus-summary-mode-map ""
2137       (cons
2138        "Score"
2139        (nconc
2140         (list
2141          ["Customize" gnus-score-customize t])
2142         (gnus-make-score-map 'increase)
2143         (gnus-make-score-map 'lower)
2144         '(("Mark"
2145            ["Kill below" gnus-summary-kill-below t]
2146            ["Mark above" gnus-summary-mark-above t]
2147            ["Tick above" gnus-summary-tick-above t]
2148            ["Clear above" gnus-summary-clear-above t])
2149           ["Current score" gnus-summary-current-score t]
2150           ["Set score" gnus-summary-set-score t]
2151           ["Switch current score file..." gnus-score-change-score-file t]
2152           ["Set mark below..." gnus-score-set-mark-below t]
2153           ["Set expunge below..." gnus-score-set-expunge-below t]
2154           ["Edit current score file" gnus-score-edit-current-scores t]
2155           ["Edit score file" gnus-score-edit-file t]
2156           ["Trace score" gnus-score-find-trace t]
2157           ["Find words" gnus-score-find-favourite-words t]
2158           ["Rescore buffer" gnus-summary-rescore t]
2159           ["Increase score..." gnus-summary-increase-score t]
2160           ["Lower score..." gnus-summary-lower-score t]))))
2161
2162     ;; Define both the Article menu in the summary buffer and the
2163     ;; equivalent Commands menu in the article buffer here for
2164     ;; consistency.
2165     (let ((innards
2166            `(("Hide"
2167               ["All" gnus-article-hide t]
2168               ["Headers" gnus-article-hide-headers t]
2169               ["Signature" gnus-article-hide-signature t]
2170               ["Citation" gnus-article-hide-citation t]
2171               ["List identifiers" gnus-article-hide-list-identifiers t]
2172               ["Banner" gnus-article-strip-banner t]
2173               ["Boring headers" gnus-article-hide-boring-headers t])
2174              ("Highlight"
2175               ["All" gnus-article-highlight t]
2176               ["Headers" gnus-article-highlight-headers t]
2177               ["Signature" gnus-article-highlight-signature t]
2178               ["Citation" gnus-article-highlight-citation t])
2179              ("MIME"
2180               ["Words" gnus-article-decode-mime-words t]
2181               ["Charset" gnus-article-decode-charset t]
2182               ["QP" gnus-article-de-quoted-unreadable t]
2183               ["Base64" gnus-article-de-base64-unreadable t]
2184               ["View MIME buttons" gnus-summary-display-buttonized t]
2185               ["View all" gnus-mime-view-all-parts t]
2186               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2187               ["Encrypt body" gnus-article-encrypt-body
2188                :active (not (gnus-group-read-only-p))
2189                ,@(if (featurep 'xemacs) nil
2190                    '(:help "Encrypt the message body on disk"))]
2191               ["Extract all parts..." gnus-summary-save-parts t]
2192               ("Multipart"
2193                ["Repair multipart" gnus-summary-repair-multipart t]
2194                ["Pipe part..." gnus-article-pipe-part t]
2195                ["Inline part" gnus-article-inline-part t]
2196                ["Encrypt body" gnus-article-encrypt-body
2197                 :active (not (gnus-group-read-only-p))
2198                ,@(if (featurep 'xemacs) nil
2199                    '(:help "Encrypt the message body on disk"))]
2200                ["View part externally" gnus-article-view-part-externally t]
2201                ["View part with charset..." gnus-article-view-part-as-charset t]
2202                ["Copy part" gnus-article-copy-part t]
2203                ["Save part..." gnus-article-save-part t]
2204                ["View part" gnus-article-view-part t]))
2205              ("Date"
2206               ["Local" gnus-article-date-local t]
2207               ["ISO8601" gnus-article-date-iso8601 t]
2208               ["UT" gnus-article-date-ut t]
2209               ["Original" gnus-article-date-original t]
2210               ["Lapsed" gnus-article-date-lapsed t]
2211               ["User-defined" gnus-article-date-user t])
2212              ("Display"
2213               ["Remove images" gnus-article-remove-images t]
2214               ["Toggle smiley" gnus-treat-smiley t]
2215               ["Show X-Face" gnus-article-display-x-face t]
2216               ["Show picons in From" gnus-treat-from-picon t]
2217               ["Show picons in mail headers" gnus-treat-mail-picon t]
2218               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2219               ("View as different encoding"
2220                ,@(gnus-summary-menu-split
2221                   (mapcar
2222                    (lambda (cs)
2223                      ;; Since easymenu under Emacs doesn't allow
2224                      ;; lambda forms for menu commands, we should
2225                      ;; provide intern'ed function symbols.
2226                      (let ((command (intern (format "\
2227 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2228                        (fset command
2229                              `(lambda ()
2230                                 (interactive)
2231                                 (let ((gnus-summary-show-article-charset-alist
2232                                        '((1 . ,cs))))
2233                                   (gnus-summary-show-article 1))))
2234                        `[,(symbol-name cs) ,command t]))
2235                    (sort (if (fboundp 'coding-system-list)
2236                              (coding-system-list)
2237                            (mapcar 'car mm-mime-mule-charset-alist))
2238                          'string<)))))
2239              ("Washing"
2240               ("Remove Blanks"
2241                ["Leading" gnus-article-strip-leading-blank-lines t]
2242                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2243                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2244                ["All of the above" gnus-article-strip-blank-lines t]
2245                ["All" gnus-article-strip-all-blank-lines t]
2246                ["Leading space" gnus-article-strip-leading-space t]
2247                ["Trailing space" gnus-article-strip-trailing-space t]
2248                ["Leading space in headers"
2249                 gnus-article-remove-leading-whitespace t])
2250               ["Overstrike" gnus-article-treat-overstrike t]
2251               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2252               ["Emphasis" gnus-article-emphasize t]
2253               ["Word wrap" gnus-article-fill-cited-article t]
2254               ["Fill long lines" gnus-article-fill-long-lines t]
2255               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2256               ["Remove CR" gnus-article-remove-cr t]
2257               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2258               ["Base64" gnus-article-de-base64-unreadable t]
2259               ["Rot 13" gnus-summary-caesar-message
2260                ,@(if (featurep 'xemacs) '(t)
2261                    '(:help "\"Caesar rotate\" article by 13"))]
2262               ["Morse decode" gnus-summary-morse-message t]
2263               ["Unix pipe..." gnus-summary-pipe-message t]
2264               ["Add buttons" gnus-article-add-buttons t]
2265               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2266               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2267               ["Verbose header" gnus-summary-verbose-headers t]
2268               ["Toggle header" gnus-summary-toggle-header t]
2269               ["Unfold headers" gnus-article-treat-unfold-headers t]
2270               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2271               ["Html" gnus-article-wash-html t]
2272               ["Unsplit URLs" gnus-article-unsplit-urls t]
2273               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2274               ["Decode HZ" gnus-article-decode-HZ t]
2275               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2276               ("(Outlook) Deuglify"
2277                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2278                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2279                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2280                ["Full (Outlook) deuglify"
2281                 gnus-article-outlook-deuglify-article t])
2282               )
2283              ("Output"
2284               ["Save in default format..." gnus-summary-save-article
2285                ,@(if (featurep 'xemacs) '(t)
2286                    '(:help "Save article using default method"))]
2287               ["Save in file..." gnus-summary-save-article-file
2288                ,@(if (featurep 'xemacs) '(t)
2289                    '(:help "Save article in file"))]
2290               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2291               ["Save in MH folder..." gnus-summary-save-article-folder t]
2292               ["Save in VM folder..." gnus-summary-save-article-vm t]
2293               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2294               ["Save body in file..." gnus-summary-save-article-body-file t]
2295               ["Pipe through a filter..." gnus-summary-pipe-output t]
2296               ["Add to SOUP packet" gnus-soup-add-article t]
2297               ["Print with Muttprint..." gnus-summary-muttprint t]
2298               ["Print" gnus-summary-print-article
2299                ,@(if (featurep 'xemacs) '(t)
2300                    '(:help "Generate and print a PostScript image"))])
2301              ("Copy, move,... (Backend)"
2302               ,@(if (featurep 'xemacs) nil
2303                   '(:help "Copying, moving, expiring articles..."))
2304               ["Respool article..." gnus-summary-respool-article t]
2305               ["Move article..." gnus-summary-move-article
2306                (gnus-check-backend-function
2307                 'request-move-article gnus-newsgroup-name)]
2308               ["Copy article..." gnus-summary-copy-article t]
2309               ["Crosspost article..." gnus-summary-crosspost-article
2310                (gnus-check-backend-function
2311                 'request-replace-article gnus-newsgroup-name)]
2312               ["Import file..." gnus-summary-import-article
2313                (gnus-check-backend-function
2314                 'request-accept-article gnus-newsgroup-name)]
2315               ["Create article..." gnus-summary-create-article
2316                (gnus-check-backend-function
2317                 'request-accept-article gnus-newsgroup-name)]
2318               ["Check if posted" gnus-summary-article-posted-p t]
2319               ["Edit article" gnus-summary-edit-article
2320                (not (gnus-group-read-only-p))]
2321               ["Delete article" gnus-summary-delete-article
2322                (gnus-check-backend-function
2323                 'request-expire-articles gnus-newsgroup-name)]
2324               ["Query respool" gnus-summary-respool-query t]
2325               ["Trace respool" gnus-summary-respool-trace t]
2326               ["Delete expirable articles" gnus-summary-expire-articles-now
2327                (gnus-check-backend-function
2328                 'request-expire-articles gnus-newsgroup-name)])
2329              ("Extract"
2330               ["Uudecode" gnus-uu-decode-uu
2331                ,@(if (featurep 'xemacs) '(t)
2332                    '(:help "Decode uuencoded article(s)"))]
2333               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2334               ["Unshar" gnus-uu-decode-unshar t]
2335               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2336               ["Save" gnus-uu-decode-save t]
2337               ["Binhex" gnus-uu-decode-binhex t]
2338               ["Postscript" gnus-uu-decode-postscript t]
2339               ["All MIME parts" gnus-summary-save-parts t])
2340              ("Cache"
2341               ["Enter article" gnus-cache-enter-article t]
2342               ["Remove article" gnus-cache-remove-article t])
2343              ["Translate" gnus-article-babel t]
2344              ["Select article buffer" gnus-summary-select-article-buffer t]
2345              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2346              ["Isearch article..." gnus-summary-isearch-article t]
2347              ["Beginning of the article" gnus-summary-beginning-of-article t]
2348              ["End of the article" gnus-summary-end-of-article t]
2349              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2350              ["Fetch referenced articles" gnus-summary-refer-references t]
2351              ["Fetch current thread" gnus-summary-refer-thread t]
2352              ["Fetch article with id..." gnus-summary-refer-article t]
2353              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2354              ["Redisplay" gnus-summary-show-article t]
2355              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2356       (easy-menu-define
2357         gnus-summary-article-menu gnus-summary-mode-map ""
2358         (cons "Article" innards))
2359
2360       (if (not (keymapp gnus-summary-article-menu))
2361           (easy-menu-define
2362             gnus-article-commands-menu gnus-article-mode-map ""
2363             (cons "Commands" innards))
2364         ;; in Emacs, don't share menu.
2365         (setq gnus-article-commands-menu
2366               (copy-keymap gnus-summary-article-menu))
2367         (define-key gnus-article-mode-map [menu-bar commands]
2368           (cons "Commands" gnus-article-commands-menu))))
2369
2370     (easy-menu-define
2371       gnus-summary-thread-menu gnus-summary-mode-map ""
2372       '("Threads"
2373         ["Find all messages in thread" gnus-summary-refer-thread t]
2374         ["Toggle threading" gnus-summary-toggle-threads t]
2375         ["Hide threads" gnus-summary-hide-all-threads t]
2376         ["Show threads" gnus-summary-show-all-threads t]
2377         ["Hide thread" gnus-summary-hide-thread t]
2378         ["Show thread" gnus-summary-show-thread t]
2379         ["Go to next thread" gnus-summary-next-thread t]
2380         ["Go to previous thread" gnus-summary-prev-thread t]
2381         ["Go down thread" gnus-summary-down-thread t]
2382         ["Go up thread" gnus-summary-up-thread t]
2383         ["Top of thread" gnus-summary-top-thread t]
2384         ["Mark thread as read" gnus-summary-kill-thread t]
2385         ["Lower thread score" gnus-summary-lower-thread t]
2386         ["Raise thread score" gnus-summary-raise-thread t]
2387         ["Rethread current" gnus-summary-rethread-current t]))
2388
2389     (easy-menu-define
2390       gnus-summary-post-menu gnus-summary-mode-map ""
2391       `("Post"
2392         ["Send a message (mail or news)" gnus-summary-post-news
2393          ,@(if (featurep 'xemacs) '(t)
2394              '(:help "Compose a new message (mail or news)"))]
2395         ["Followup" gnus-summary-followup
2396          ,@(if (featurep 'xemacs) '(t)
2397              '(:help "Post followup to this article"))]
2398         ["Followup and yank" gnus-summary-followup-with-original
2399          ,@(if (featurep 'xemacs) '(t)
2400              '(:help "Post followup to this article, quoting its contents"))]
2401         ["Supersede article" gnus-summary-supersede-article t]
2402         ["Cancel article" gnus-summary-cancel-article
2403          ,@(if (featurep 'xemacs) '(t)
2404              '(:help "Cancel an article you posted"))]
2405         ["Reply" gnus-summary-reply t]
2406         ["Reply and yank" gnus-summary-reply-with-original t]
2407         ["Wide reply" gnus-summary-wide-reply t]
2408         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2409          ,@(if (featurep 'xemacs) '(t)
2410              '(:help "Mail a reply, quoting this article"))]
2411         ["Very wide reply" gnus-summary-very-wide-reply t]
2412         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2413          ,@(if (featurep 'xemacs) '(t)
2414              '(:help "Mail a very wide reply, quoting this article"))]
2415         ["Mail forward" gnus-summary-mail-forward t]
2416         ["Post forward" gnus-summary-post-forward t]
2417         ["Digest and mail" gnus-uu-digest-mail-forward t]
2418         ["Digest and post" gnus-uu-digest-post-forward t]
2419         ["Resend message" gnus-summary-resend-message t]
2420         ["Resend message edit" gnus-summary-resend-message-edit t]
2421         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2422         ["Send a mail" gnus-summary-mail-other-window t]
2423         ["Create a local message" gnus-summary-news-other-window t]
2424         ["Uuencode and post" gnus-uu-post-news
2425          ,@(if (featurep 'xemacs) '(t)
2426              '(:help "Post a uuencoded article"))]
2427         ["Followup via news" gnus-summary-followup-to-mail t]
2428         ["Followup via news and yank"
2429          gnus-summary-followup-to-mail-with-original t]
2430         ;;("Draft"
2431         ;;["Send" gnus-summary-send-draft t]
2432         ;;["Send bounced" gnus-resend-bounced-mail t])
2433         ))
2434
2435     (cond
2436      ((not (keymapp gnus-summary-post-menu))
2437       (setq gnus-article-post-menu gnus-summary-post-menu))
2438      ((not gnus-article-post-menu)
2439       ;; Don't share post menu.
2440       (setq gnus-article-post-menu
2441             (copy-keymap gnus-summary-post-menu))))
2442     (define-key gnus-article-mode-map [menu-bar post]
2443       (cons "Post" gnus-article-post-menu))
2444
2445     (easy-menu-define
2446       gnus-summary-misc-menu gnus-summary-mode-map ""
2447       `("Gnus"
2448         ("Mark Read"
2449          ["Mark as read" gnus-summary-mark-as-read-forward t]
2450          ["Mark same subject and select"
2451           gnus-summary-kill-same-subject-and-select t]
2452          ["Mark same subject" gnus-summary-kill-same-subject t]
2453          ["Catchup" gnus-summary-catchup
2454           ,@(if (featurep 'xemacs) '(t)
2455               '(:help "Mark unread articles in this group as read"))]
2456          ["Catchup all" gnus-summary-catchup-all t]
2457          ["Catchup to here" gnus-summary-catchup-to-here t]
2458          ["Catchup from here" gnus-summary-catchup-from-here t]
2459          ["Catchup region" gnus-summary-mark-region-as-read
2460           (gnus-mark-active-p)]
2461          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2462         ("Mark Various"
2463          ["Tick" gnus-summary-tick-article-forward t]
2464          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2465          ["Remove marks" gnus-summary-clear-mark-forward t]
2466          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2467          ["Set bookmark" gnus-summary-set-bookmark t]
2468          ["Remove bookmark" gnus-summary-remove-bookmark t])
2469         ("Limit to"
2470          ["Marks..." gnus-summary-limit-to-marks t]
2471          ["Subject..." gnus-summary-limit-to-subject t]
2472          ["Author..." gnus-summary-limit-to-author t]
2473          ["Age..." gnus-summary-limit-to-age t]
2474          ["Extra..." gnus-summary-limit-to-extra t]
2475          ["Score..." gnus-summary-limit-to-score t]
2476          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2477          ["Unread" gnus-summary-limit-to-unread t]
2478          ["Unseen" gnus-summary-limit-to-unseen t]
2479          ["Replied" gnus-summary-limit-to-replied t]
2480          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2481          ["Next articles" gnus-summary-limit-to-articles t]
2482          ["Pop limit" gnus-summary-pop-limit t]
2483          ["Show dormant" gnus-summary-limit-include-dormant t]
2484          ["Hide childless dormant"
2485           gnus-summary-limit-exclude-childless-dormant t]
2486          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2487          ["Hide marked" gnus-summary-limit-exclude-marks t]
2488          ["Show expunged" gnus-summary-limit-include-expunged t])
2489         ("Process Mark"
2490          ["Set mark" gnus-summary-mark-as-processable t]
2491          ["Remove mark" gnus-summary-unmark-as-processable t]
2492          ["Remove all marks" gnus-summary-unmark-all-processable t]
2493          ["Invert marks" gnus-uu-invert-processable t]
2494          ["Mark above" gnus-uu-mark-over t]
2495          ["Mark series" gnus-uu-mark-series t]
2496          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2497          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2498          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2499          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2500          ["Mark all" gnus-uu-mark-all t]
2501          ["Mark buffer" gnus-uu-mark-buffer t]
2502          ["Mark sparse" gnus-uu-mark-sparse t]
2503          ["Mark thread" gnus-uu-mark-thread t]
2504          ["Unmark thread" gnus-uu-unmark-thread t]
2505          ("Process Mark Sets"
2506           ["Kill" gnus-summary-kill-process-mark t]
2507           ["Yank" gnus-summary-yank-process-mark
2508            gnus-newsgroup-process-stack]
2509           ["Save" gnus-summary-save-process-mark t]
2510           ["Run command on marked..." gnus-summary-universal-argument t]))
2511         ("Scroll article"
2512          ["Page forward" gnus-summary-next-page
2513           ,@(if (featurep 'xemacs) '(t)
2514               '(:help "Show next page of article"))]
2515          ["Page backward" gnus-summary-prev-page
2516           ,@(if (featurep 'xemacs) '(t)
2517               '(:help "Show previous page of article"))]
2518          ["Line forward" gnus-summary-scroll-up t])
2519         ("Move"
2520          ["Next unread article" gnus-summary-next-unread-article t]
2521          ["Previous unread article" gnus-summary-prev-unread-article t]
2522          ["Next article" gnus-summary-next-article t]
2523          ["Previous article" gnus-summary-prev-article t]
2524          ["Next unread subject" gnus-summary-next-unread-subject t]
2525          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2526          ["Next article same subject" gnus-summary-next-same-subject t]
2527          ["Previous article same subject" gnus-summary-prev-same-subject t]
2528          ["First unread article" gnus-summary-first-unread-article t]
2529          ["Best unread article" gnus-summary-best-unread-article t]
2530          ["Go to subject number..." gnus-summary-goto-subject t]
2531          ["Go to article number..." gnus-summary-goto-article t]
2532          ["Go to the last article" gnus-summary-goto-last-article t]
2533          ["Pop article off history" gnus-summary-pop-article t])
2534         ("Sort"
2535          ["Sort by number" gnus-summary-sort-by-number t]
2536          ["Sort by author" gnus-summary-sort-by-author t]
2537          ["Sort by subject" gnus-summary-sort-by-subject t]
2538          ["Sort by date" gnus-summary-sort-by-date t]
2539          ["Sort by score" gnus-summary-sort-by-score t]
2540          ["Sort by lines" gnus-summary-sort-by-lines t]
2541          ["Sort by characters" gnus-summary-sort-by-chars t]
2542          ["Randomize" gnus-summary-sort-by-random t]
2543          ["Original sort" gnus-summary-sort-by-original t])
2544         ("Help"
2545          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2546          ["Describe group" gnus-summary-describe-group t]
2547          ["Fetch charter" gnus-group-fetch-charter
2548           ,@(if (featurep 'xemacs) nil
2549               '(:help "Display the charter of the current group"))]
2550          ["Fetch control message" gnus-group-fetch-control
2551           ,@(if (featurep 'xemacs) nil
2552               '(:help "Display the archived control message for the current group"))]
2553          ["Read manual" gnus-info-find-node t])
2554         ("Modes"
2555          ["Pick and read" gnus-pick-mode t]
2556          ["Binary" gnus-binary-mode t])
2557         ("Regeneration"
2558          ["Regenerate" gnus-summary-prepare t]
2559          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2560          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2561          ["Toggle threading" gnus-summary-toggle-threads t])
2562         ["See old articles" gnus-summary-insert-old-articles t]
2563         ["See new articles" gnus-summary-insert-new-articles t]
2564         ["Filter articles..." gnus-summary-execute-command t]
2565         ["Run command on articles..." gnus-summary-universal-argument t]
2566         ["Search articles forward..." gnus-summary-search-article-forward t]
2567         ["Search articles backward..." gnus-summary-search-article-backward t]
2568         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2569         ["Expand window" gnus-summary-expand-window t]
2570         ["Expire expirable articles" gnus-summary-expire-articles
2571          (gnus-check-backend-function
2572           'request-expire-articles gnus-newsgroup-name)]
2573         ["Edit local kill file" gnus-summary-edit-local-kill t]
2574         ["Edit main kill file" gnus-summary-edit-global-kill t]
2575         ["Edit group parameters" gnus-summary-edit-parameters t]
2576         ["Customize group parameters" gnus-summary-customize-parameters t]
2577         ["Send a bug report" gnus-bug t]
2578         ("Exit"
2579          ["Catchup and exit" gnus-summary-catchup-and-exit
2580           ,@(if (featurep 'xemacs) '(t)
2581               '(:help "Mark unread articles in this group as read, then exit"))]
2582          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2583          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2584          ["Exit group" gnus-summary-exit
2585           ,@(if (featurep 'xemacs) '(t)
2586               '(:help "Exit current group, return to group selection mode"))]
2587          ["Exit group without updating" gnus-summary-exit-no-update t]
2588          ["Exit and goto next group" gnus-summary-next-group t]
2589          ["Exit and goto prev group" gnus-summary-prev-group t]
2590          ["Reselect group" gnus-summary-reselect-current-group t]
2591          ["Rescan group" gnus-summary-rescan-group t]
2592          ["Update dribble" gnus-summary-save-newsrc t])))
2593
2594     (gnus-run-hooks 'gnus-summary-menu-hook)))
2595
2596 (defvar gnus-summary-tool-bar-map nil)
2597
2598 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2599 (defun gnus-summary-make-tool-bar ()
2600   (if (and (fboundp 'tool-bar-add-item-from-menu)
2601            (default-value 'tool-bar-mode)
2602            (not gnus-summary-tool-bar-map))
2603       (setq gnus-summary-tool-bar-map
2604             (let ((tool-bar-map (make-sparse-keymap))
2605                   (load-path (mm-image-load-path)))
2606               (tool-bar-add-item-from-menu
2607                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2608               (tool-bar-add-item-from-menu
2609                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2610               (tool-bar-add-item-from-menu
2611                'gnus-summary-post-news "post" gnus-summary-mode-map)
2612               (tool-bar-add-item-from-menu
2613                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2614               (tool-bar-add-item-from-menu
2615                'gnus-summary-followup "followup" gnus-summary-mode-map)
2616               (tool-bar-add-item-from-menu
2617                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2618               (tool-bar-add-item-from-menu
2619                'gnus-summary-reply "reply" gnus-summary-mode-map)
2620               (tool-bar-add-item-from-menu
2621                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2622               (tool-bar-add-item-from-menu
2623                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2624               (tool-bar-add-item-from-menu
2625                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2626               (tool-bar-add-item-from-menu
2627                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2628               (tool-bar-add-item-from-menu
2629                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2630               (tool-bar-add-item-from-menu
2631                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2632               (tool-bar-add-item-from-menu
2633                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2634               (tool-bar-add-item-from-menu
2635                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2636               (tool-bar-add-item-from-menu
2637                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2638               tool-bar-map)))
2639   (if gnus-summary-tool-bar-map
2640       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2641
2642 (defun gnus-score-set-default (var value)
2643   "A version of set that updates the GNU Emacs menu-bar."
2644   (set var value)
2645   ;; It is the message that forces the active status to be updated.
2646   (message ""))
2647
2648 (defun gnus-make-score-map (type)
2649   "Make a summary score map of type TYPE."
2650   (if t
2651       nil
2652     (let ((headers '(("author" "from" string)
2653                      ("subject" "subject" string)
2654                      ("article body" "body" string)
2655                      ("article head" "head" string)
2656                      ("xref" "xref" string)
2657                      ("extra header" "extra" string)
2658                      ("lines" "lines" number)
2659                      ("followups to author" "followup" string)))
2660           (types '((number ("less than" <)
2661                            ("greater than" >)
2662                            ("equal" =))
2663                    (string ("substring" s)
2664                            ("exact string" e)
2665                            ("fuzzy string" f)
2666                            ("regexp" r))))
2667           (perms '(("temporary" (current-time-string))
2668                    ("permanent" nil)
2669                    ("immediate" now)))
2670           header)
2671       (list
2672        (apply
2673         'nconc
2674         (list
2675          (if (eq type 'lower)
2676              "Lower score"
2677            "Increase score"))
2678         (let (outh)
2679           (while headers
2680             (setq header (car headers))
2681             (setq outh
2682                   (cons
2683                    (apply
2684                     'nconc
2685                     (list (car header))
2686                     (let ((ts (cdr (assoc (nth 2 header) types)))
2687                           outt)
2688                       (while ts
2689                         (setq outt
2690                               (cons
2691                                (apply
2692                                 'nconc
2693                                 (list (caar ts))
2694                                 (let ((ps perms)
2695                                       outp)
2696                                   (while ps
2697                                     (setq outp
2698                                           (cons
2699                                            (vector
2700                                             (caar ps)
2701                                             (list
2702                                              'gnus-summary-score-entry
2703                                              (nth 1 header)
2704                                              (if (or (string= (nth 1 header)
2705                                                               "head")
2706                                                      (string= (nth 1 header)
2707                                                               "body"))
2708                                                  ""
2709                                                (list 'gnus-summary-header
2710                                                      (nth 1 header)))
2711                                              (list 'quote (nth 1 (car ts)))
2712                                              (list 'gnus-score-delta-default
2713                                                    nil)
2714                                              (nth 1 (car ps))
2715                                              t)
2716                                             t)
2717                                            outp))
2718                                     (setq ps (cdr ps)))
2719                                   (list (nreverse outp))))
2720                                outt))
2721                         (setq ts (cdr ts)))
2722                       (list (nreverse outt))))
2723                    outh))
2724             (setq headers (cdr headers)))
2725           (list (nreverse outh))))))))
2726
2727 \f
2728
2729 (defun gnus-summary-mode (&optional group)
2730   "Major mode for reading articles.
2731
2732 All normal editing commands are switched off.
2733 \\<gnus-summary-mode-map>
2734 Each line in this buffer represents one article.  To read an
2735 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2736 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2737 respectively.
2738
2739 You can also post articles and send mail from this buffer.  To
2740 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2741 of an article, type `\\[gnus-summary-reply]'.
2742
2743 There are approx. one gazillion commands you can execute in this
2744 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2745
2746 The following commands are available:
2747
2748 \\{gnus-summary-mode-map}"
2749   (interactive)
2750   (kill-all-local-variables)
2751   (when (gnus-visual-p 'summary-menu 'menu)
2752     (gnus-summary-make-menu-bar)
2753     (gnus-summary-make-tool-bar))
2754   (gnus-summary-make-local-variables)
2755   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2756     (gnus-summary-make-local-variables))
2757   (gnus-make-thread-indent-array)
2758   (gnus-simplify-mode-line)
2759   (setq major-mode 'gnus-summary-mode)
2760   (setq mode-name "Summary")
2761   (make-local-variable 'minor-mode-alist)
2762   (use-local-map gnus-summary-mode-map)
2763   (buffer-disable-undo)
2764   (setq buffer-read-only t              ;Disable modification
2765         show-trailing-whitespace nil)
2766   (setq truncate-lines t)
2767   (setq selective-display t)
2768   (setq selective-display-ellipses t)   ;Display `...'
2769   (gnus-summary-set-display-table)
2770   (gnus-set-default-directory)
2771   (setq gnus-newsgroup-name group)
2772   (make-local-variable 'gnus-summary-line-format)
2773   (make-local-variable 'gnus-summary-line-format-spec)
2774   (make-local-variable 'gnus-summary-dummy-line-format)
2775   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2776   (make-local-variable 'gnus-summary-mark-positions)
2777   (gnus-make-local-hook 'pre-command-hook)
2778   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2779   (gnus-run-hooks 'gnus-summary-mode-hook)
2780   (turn-on-gnus-mailing-list-mode)
2781   (mm-enable-multibyte)
2782   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2783   (gnus-update-summary-mark-positions))
2784
2785 (defun gnus-summary-make-local-variables ()
2786   "Make all the local summary buffer variables."
2787   (let (global)
2788     (dolist (local gnus-summary-local-variables)
2789       (if (consp local)
2790           (progn
2791             (if (eq (cdr local) 'global)
2792                 ;; Copy the global value of the variable.
2793                 (setq global (symbol-value (car local)))
2794               ;; Use the value from the list.
2795               (setq global (eval (cdr local))))
2796             (set (make-local-variable (car local)) global))
2797         ;; Simple nil-valued local variable.
2798         (set (make-local-variable local) nil)))))
2799
2800 (defun gnus-summary-clear-local-variables ()
2801   (let ((locals gnus-summary-local-variables))
2802     (while locals
2803       (if (consp (car locals))
2804           (and (vectorp (caar locals))
2805                (set (caar locals) nil))
2806         (and (vectorp (car locals))
2807              (set (car locals) nil)))
2808       (setq locals (cdr locals)))))
2809
2810 ;; Summary data functions.
2811
2812 (defmacro gnus-data-number (data)
2813   `(car ,data))
2814
2815 (defmacro gnus-data-set-number (data number)
2816   `(setcar ,data ,number))
2817
2818 (defmacro gnus-data-mark (data)
2819   `(nth 1 ,data))
2820
2821 (defmacro gnus-data-set-mark (data mark)
2822   `(setcar (nthcdr 1 ,data) ,mark))
2823
2824 (defmacro gnus-data-pos (data)
2825   `(nth 2 ,data))
2826
2827 (defmacro gnus-data-set-pos (data pos)
2828   `(setcar (nthcdr 2 ,data) ,pos))
2829
2830 (defmacro gnus-data-header (data)
2831   `(nth 3 ,data))
2832
2833 (defmacro gnus-data-set-header (data header)
2834   `(setf (nth 3 ,data) ,header))
2835
2836 (defmacro gnus-data-level (data)
2837   `(nth 4 ,data))
2838
2839 (defmacro gnus-data-unread-p (data)
2840   `(= (nth 1 ,data) gnus-unread-mark))
2841
2842 (defmacro gnus-data-read-p (data)
2843   `(/= (nth 1 ,data) gnus-unread-mark))
2844
2845 (defmacro gnus-data-pseudo-p (data)
2846   `(consp (nth 3 ,data)))
2847
2848 (defmacro gnus-data-find (number)
2849   `(assq ,number gnus-newsgroup-data))
2850
2851 (defmacro gnus-data-find-list (number &optional data)
2852   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2853      (memq (assq ,number bdata)
2854            bdata)))
2855
2856 (defmacro gnus-data-make (number mark pos header level)
2857   `(list ,number ,mark ,pos ,header ,level))
2858
2859 (defun gnus-data-enter (after-article number mark pos header level offset)
2860   (let ((data (gnus-data-find-list after-article)))
2861     (unless data
2862       (error "No such article: %d" after-article))
2863     (setcdr data (cons (gnus-data-make number mark pos header level)
2864                        (cdr data)))
2865     (setq gnus-newsgroup-data-reverse nil)
2866     (gnus-data-update-list (cddr data) offset)))
2867
2868 (defun gnus-data-enter-list (after-article list &optional offset)
2869   (when list
2870     (let ((data (and after-article (gnus-data-find-list after-article)))
2871           (ilist list))
2872       (if (not (or data
2873                    after-article))
2874           (let ((odata gnus-newsgroup-data))
2875             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2876             (when offset
2877               (gnus-data-update-list odata offset)))
2878         ;; Find the last element in the list to be spliced into the main
2879         ;; list.
2880         (setq list (last list))
2881         (if (not data)
2882             (progn
2883               (setcdr list gnus-newsgroup-data)
2884               (setq gnus-newsgroup-data ilist)
2885               (when offset
2886                 (gnus-data-update-list (cdr list) offset)))
2887           (setcdr list (cdr data))
2888           (setcdr data ilist)
2889           (when offset
2890             (gnus-data-update-list (cdr list) offset))))
2891       (setq gnus-newsgroup-data-reverse nil))))
2892
2893 (defun gnus-data-remove (article &optional offset)
2894   (let ((data gnus-newsgroup-data))
2895     (if (= (gnus-data-number (car data)) article)
2896         (progn
2897           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2898                 gnus-newsgroup-data-reverse nil)
2899           (when offset
2900             (gnus-data-update-list gnus-newsgroup-data offset)))
2901       (while (cdr data)
2902         (when (= (gnus-data-number (cadr data)) article)
2903           (setcdr data (cddr data))
2904           (when offset
2905             (gnus-data-update-list (cdr data) offset))
2906           (setq data nil
2907                 gnus-newsgroup-data-reverse nil))
2908         (setq data (cdr data))))))
2909
2910 (defmacro gnus-data-list (backward)
2911   `(if ,backward
2912        (or gnus-newsgroup-data-reverse
2913            (setq gnus-newsgroup-data-reverse
2914                  (reverse gnus-newsgroup-data)))
2915      gnus-newsgroup-data))
2916
2917 (defun gnus-data-update-list (data offset)
2918   "Add OFFSET to the POS of all data entries in DATA."
2919   (setq gnus-newsgroup-data-reverse nil)
2920   (while data
2921     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2922     (setq data (cdr data))))
2923
2924 (defun gnus-summary-article-pseudo-p (article)
2925   "Say whether this article is a pseudo article or not."
2926   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2927
2928 (defmacro gnus-summary-article-sparse-p (article)
2929   "Say whether this article is a sparse article or not."
2930   `(memq ,article gnus-newsgroup-sparse))
2931
2932 (defmacro gnus-summary-article-ancient-p (article)
2933   "Say whether this article is a sparse article or not."
2934   `(memq ,article gnus-newsgroup-ancient))
2935
2936 (defun gnus-article-parent-p (number)
2937   "Say whether this article is a parent or not."
2938   (let ((data (gnus-data-find-list number)))
2939     (and (cdr data)              ; There has to be an article after...
2940          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2941             (gnus-data-level (nth 1 data))))))
2942
2943 (defun gnus-article-children (number)
2944   "Return a list of all children to NUMBER."
2945   (let* ((data (gnus-data-find-list number))
2946          (level (gnus-data-level (car data)))
2947          children)
2948     (setq data (cdr data))
2949     (while (and data
2950                 (= (gnus-data-level (car data)) (1+ level)))
2951       (push (gnus-data-number (car data)) children)
2952       (setq data (cdr data)))
2953     children))
2954
2955 (defmacro gnus-summary-skip-intangible ()
2956   "If the current article is intangible, then jump to a different article."
2957   '(let ((to (get-text-property (point) 'gnus-intangible)))
2958      (and to (gnus-summary-goto-subject to))))
2959
2960 (defmacro gnus-summary-article-intangible-p ()
2961   "Say whether this article is intangible or not."
2962   '(get-text-property (point) 'gnus-intangible))
2963
2964 (defun gnus-article-read-p (article)
2965   "Say whether ARTICLE is read or not."
2966   (not (or (memq article gnus-newsgroup-marked)
2967            (memq article gnus-newsgroup-spam-marked)
2968            (memq article gnus-newsgroup-unreads)
2969            (memq article gnus-newsgroup-unselected)
2970            (memq article gnus-newsgroup-dormant))))
2971
2972 ;; Some summary mode macros.
2973
2974 (defmacro gnus-summary-article-number ()
2975   "The article number of the article on the current line.
2976 If there isn't an article number here, then we return the current
2977 article number."
2978   '(progn
2979      (gnus-summary-skip-intangible)
2980      (or (get-text-property (point) 'gnus-number)
2981          (gnus-summary-last-subject))))
2982
2983 (defmacro gnus-summary-article-header (&optional number)
2984   "Return the header of article NUMBER."
2985   `(gnus-data-header (gnus-data-find
2986                       ,(or number '(gnus-summary-article-number)))))
2987
2988 (defmacro gnus-summary-thread-level (&optional number)
2989   "Return the level of thread that starts with article NUMBER."
2990   `(if (and (eq gnus-summary-make-false-root 'dummy)
2991             (get-text-property (point) 'gnus-intangible))
2992        0
2993      (gnus-data-level (gnus-data-find
2994                        ,(or number '(gnus-summary-article-number))))))
2995
2996 (defmacro gnus-summary-article-mark (&optional number)
2997   "Return the mark of article NUMBER."
2998   `(gnus-data-mark (gnus-data-find
2999                     ,(or number '(gnus-summary-article-number)))))
3000
3001 (defmacro gnus-summary-article-pos (&optional number)
3002   "Return the position of the line of article NUMBER."
3003   `(gnus-data-pos (gnus-data-find
3004                    ,(or number '(gnus-summary-article-number)))))
3005
3006 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3007 (defmacro gnus-summary-article-subject (&optional number)
3008   "Return current subject string or nil if nothing."
3009   `(let ((headers
3010           ,(if number
3011                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3012              '(gnus-data-header (assq (gnus-summary-article-number)
3013                                       gnus-newsgroup-data)))))
3014      (and headers
3015           (vectorp headers)
3016           (mail-header-subject headers))))
3017
3018 (defmacro gnus-summary-article-score (&optional number)
3019   "Return current article score."
3020   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3021                   gnus-newsgroup-scored))
3022        gnus-summary-default-score 0))
3023
3024 (defun gnus-summary-article-children (&optional number)
3025   "Return a list of article numbers that are children of article NUMBER."
3026   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3027          (level (gnus-data-level (car data)))
3028          l children)
3029     (while (and (setq data (cdr data))
3030                 (> (setq l (gnus-data-level (car data))) level))
3031       (and (= (1+ level) l)
3032            (push (gnus-data-number (car data))
3033                  children)))
3034     (nreverse children)))
3035
3036 (defun gnus-summary-article-parent (&optional number)
3037   "Return the article number of the parent of article NUMBER."
3038   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3039                                     (gnus-data-list t)))
3040          (level (gnus-data-level (car data))))
3041     (if (zerop level)
3042         ()                              ; This is a root.
3043       ;; We search until we find an article with a level less than
3044       ;; this one.  That function has to be the parent.
3045       (while (and (setq data (cdr data))
3046                   (not (< (gnus-data-level (car data)) level))))
3047       (and data (gnus-data-number (car data))))))
3048
3049 (defun gnus-unread-mark-p (mark)
3050   "Say whether MARK is the unread mark."
3051   (= mark gnus-unread-mark))
3052
3053 (defun gnus-read-mark-p (mark)
3054   "Say whether MARK is one of the marks that mark as read.
3055 This is all marks except unread, ticked, dormant, and expirable."
3056   (not (or (= mark gnus-unread-mark)
3057            (= mark gnus-ticked-mark)
3058            (= mark gnus-spam-mark)
3059            (= mark gnus-dormant-mark)
3060            (= mark gnus-expirable-mark))))
3061
3062 (defmacro gnus-article-mark (number)
3063   "Return the MARK of article NUMBER.
3064 This macro should only be used when computing the mark the \"first\"
3065 time; i.e., when generating the summary lines.  After that,
3066 `gnus-summary-article-mark' should be used to examine the
3067 marks of articles."
3068   `(cond
3069     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3070     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3071     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3072     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3073     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3074     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3075     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3076     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3077            gnus-ancient-mark))))
3078
3079 ;; Saving hidden threads.
3080
3081 (defmacro gnus-save-hidden-threads (&rest forms)
3082   "Save hidden threads, eval FORMS, and restore the hidden threads."
3083   (let ((config (make-symbol "config")))
3084     `(let ((,config (gnus-hidden-threads-configuration)))
3085        (unwind-protect
3086            (save-excursion
3087              ,@forms)
3088          (gnus-restore-hidden-threads-configuration ,config)))))
3089 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3090 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3091
3092 (defun gnus-data-compute-positions ()
3093   "Compute the positions of all articles."
3094   (setq gnus-newsgroup-data-reverse nil)
3095   (let ((data gnus-newsgroup-data))
3096     (save-excursion
3097       (gnus-save-hidden-threads
3098         (gnus-summary-show-all-threads)
3099         (goto-char (point-min))
3100         (while data
3101           (while (get-text-property (point) 'gnus-intangible)
3102             (forward-line 1))
3103           (gnus-data-set-pos (car data) (+ (point) 3))
3104           (setq data (cdr data))
3105           (forward-line 1))))))
3106
3107 (defun gnus-hidden-threads-configuration ()
3108   "Return the current hidden threads configuration."
3109   (save-excursion
3110     (let (config)
3111       (goto-char (point-min))
3112       (while (search-forward "\r" nil t)
3113         (push (1- (point)) config))
3114       config)))
3115
3116 (defun gnus-restore-hidden-threads-configuration (config)
3117   "Restore hidden threads configuration from CONFIG."
3118   (save-excursion
3119     (let (point buffer-read-only)
3120       (while (setq point (pop config))
3121         (when (and (< point (point-max))
3122                    (goto-char point)
3123                    (eq (char-after) ?\n))
3124           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3125
3126 ;; Various summary mode internalish functions.
3127
3128 (defun gnus-mouse-pick-article (e)
3129   (interactive "e")
3130   (mouse-set-point e)
3131   (gnus-summary-next-page nil t))
3132
3133 (defun gnus-summary-set-display-table ()
3134   "Change the display table.
3135 Odd characters have a tendency to mess
3136 up nicely formatted displays - we make all possible glyphs
3137 display only a single character."
3138
3139   ;; We start from the standard display table, if any.
3140   (let ((table (or (copy-sequence standard-display-table)
3141                    (make-display-table)))
3142         (i 32))
3143     ;; Nix out all the control chars...
3144     (while (>= (setq i (1- i)) 0)
3145       (aset table i [??]))
3146    ;; ... but not newline and cr, of course.  (cr is necessary for the
3147     ;; selective display).
3148     (aset table ?\n nil)
3149     (aset table ?\r nil)
3150     ;; We keep TAB as well.
3151     (aset table ?\t nil)
3152     ;; We nix out any glyphs over 126 that are not set already.
3153     (let ((i 256))
3154       (while (>= (setq i (1- i)) 127)
3155         ;; Only modify if the entry is nil.
3156         (unless (aref table i)
3157           (aset table i [??]))))
3158     (setq buffer-display-table table)))
3159
3160 (defun gnus-summary-set-article-display-arrow (pos)
3161   "Update the overlay arrow to point to line at position POS."
3162   (when (and gnus-summary-display-arrow
3163              (boundp 'overlay-arrow-position)
3164              (boundp 'overlay-arrow-string))
3165     (save-excursion
3166       (goto-char pos)
3167       (beginning-of-line)
3168       (unless overlay-arrow-position
3169         (setq overlay-arrow-position (make-marker)))
3170       (setq overlay-arrow-string "=>"
3171             overlay-arrow-position (set-marker overlay-arrow-position
3172                                                (point)
3173                                                (current-buffer))))))
3174
3175 (defun gnus-summary-setup-buffer (group)
3176   "Initialize summary buffer."
3177   (let ((buffer (gnus-summary-buffer-name group))
3178         (dead-name (concat "*Dead Summary "
3179                            (gnus-group-decoded-name group) "*")))
3180     ;; If a dead summary buffer exists, we kill it.
3181     (when (gnus-buffer-live-p dead-name)
3182       (gnus-kill-buffer dead-name))
3183     (if (get-buffer buffer)
3184         (progn
3185           (set-buffer buffer)
3186           (setq gnus-summary-buffer (current-buffer))
3187           (not gnus-newsgroup-prepared))
3188       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3189       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3190       (gnus-summary-mode group)
3191       (when gnus-carpal
3192         (gnus-carpal-setup-buffer 'summary))
3193       (unless gnus-single-article-buffer
3194         (make-local-variable 'gnus-article-buffer)
3195         (make-local-variable 'gnus-article-current)
3196         (make-local-variable 'gnus-original-article-buffer))
3197       (setq gnus-newsgroup-name group)
3198       ;; Set any local variables in the group parameters.
3199       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3200       t)))
3201
3202 (defun gnus-set-global-variables ()
3203   "Set the global equivalents of the buffer-local variables.
3204 They are set to the latest values they had.  These reflect the summary
3205 buffer that was in action when the last article was fetched."
3206   (when (eq major-mode 'gnus-summary-mode)
3207     (setq gnus-summary-buffer (current-buffer))
3208     (let ((name gnus-newsgroup-name)
3209           (marked gnus-newsgroup-marked)
3210           (spam gnus-newsgroup-spam-marked)
3211           (unread gnus-newsgroup-unreads)
3212           (headers gnus-current-headers)
3213           (data gnus-newsgroup-data)
3214           (summary gnus-summary-buffer)
3215           (article-buffer gnus-article-buffer)
3216           (original gnus-original-article-buffer)
3217           (gac gnus-article-current)
3218           (reffed gnus-reffed-article-number)
3219           (score-file gnus-current-score-file)
3220           (default-charset gnus-newsgroup-charset)
3221           vlist)
3222       (let ((locals gnus-newsgroup-variables))
3223         (while locals
3224           (if (consp (car locals))
3225               (push (eval (caar locals)) vlist)
3226             (push (eval (car locals)) vlist))
3227           (setq locals (cdr locals)))
3228         (setq vlist (nreverse vlist)))
3229       (with-current-buffer gnus-group-buffer
3230         (setq gnus-newsgroup-name name
3231               gnus-newsgroup-marked marked
3232               gnus-newsgroup-spam-marked spam
3233               gnus-newsgroup-unreads unread
3234               gnus-current-headers headers
3235               gnus-newsgroup-data data
3236               gnus-article-current gac
3237               gnus-summary-buffer summary
3238               gnus-article-buffer article-buffer
3239               gnus-original-article-buffer original
3240               gnus-reffed-article-number reffed
3241               gnus-current-score-file score-file
3242               gnus-newsgroup-charset default-charset)
3243         (let ((locals gnus-newsgroup-variables))
3244           (while locals
3245             (if (consp (car locals))
3246                 (set (caar locals) (pop vlist))
3247               (set (car locals) (pop vlist)))
3248             (setq locals (cdr locals))))
3249         ;; The article buffer also has local variables.
3250         (when (gnus-buffer-live-p gnus-article-buffer)
3251           (set-buffer gnus-article-buffer)
3252           (setq gnus-summary-buffer summary))))))
3253
3254 (defun gnus-summary-article-unread-p (article)
3255   "Say whether ARTICLE is unread or not."
3256   (memq article gnus-newsgroup-unreads))
3257
3258 (defun gnus-summary-first-article-p (&optional article)
3259   "Return whether ARTICLE is the first article in the buffer."
3260   (if (not (setq article (or article (gnus-summary-article-number))))
3261       nil
3262     (eq article (caar gnus-newsgroup-data))))
3263
3264 (defun gnus-summary-last-article-p (&optional article)
3265   "Return whether ARTICLE is the last article in the buffer."
3266   (if (not (setq article (or article (gnus-summary-article-number))))
3267       ;; All non-existent numbers are the last article.  :-)
3268       t
3269     (not (cdr (gnus-data-find-list article)))))
3270
3271 (defun gnus-make-thread-indent-array ()
3272   (let ((n 200))
3273     (unless (and gnus-thread-indent-array
3274                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3275       (setq gnus-thread-indent-array (make-vector 201 "")
3276             gnus-thread-indent-array-level gnus-thread-indent-level)
3277       (while (>= n 0)
3278         (aset gnus-thread-indent-array n
3279               (make-string (* n gnus-thread-indent-level) ? ))
3280         (setq n (1- n))))))
3281
3282 (defun gnus-update-summary-mark-positions ()
3283   "Compute where the summary marks are to go."
3284   (save-excursion
3285     (when (gnus-buffer-exists-p gnus-summary-buffer)
3286       (set-buffer gnus-summary-buffer))
3287     (let ((spec gnus-summary-line-format-spec)
3288           pos)
3289       (save-excursion
3290         (gnus-set-work-buffer)
3291         (let ((gnus-tmp-unread ?Z)
3292               (gnus-replied-mark ?Z)
3293               (gnus-score-below-mark ?Z)
3294               (gnus-score-over-mark ?Z)
3295               (gnus-undownloaded-mark ?Z)
3296               (gnus-summary-line-format-spec spec)
3297               (gnus-newsgroup-downloadable '(0))
3298               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3299               case-fold-search ignores)
3300           ;; Here, all marks are bound to Z.
3301           (gnus-summary-insert-line header
3302                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3303           (goto-char (point-min))
3304           ;; Memorize the positions of the same characters as dummy marks.
3305           (while (re-search-forward "[A-D]" nil t)
3306             (push (point) ignores))
3307           (erase-buffer)
3308           ;; We use A-D as dummy marks in order to know column positions
3309           ;; where marks should be inserted.
3310           (setq gnus-tmp-unread ?A
3311                 gnus-replied-mark ?B
3312                 gnus-score-below-mark ?C
3313                 gnus-score-over-mark ?C
3314                 gnus-undownloaded-mark ?D)
3315           (gnus-summary-insert-line header
3316                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3317           ;; Ignore characters which aren't dummy marks.
3318           (dolist (p ignores)
3319             (delete-region (goto-char (1- p)) p)
3320             (insert ?Z))
3321           (goto-char (point-min))
3322           (setq pos (list (cons 'unread
3323                                 (and (search-forward "A" nil t)
3324                                      (- (point) (point-min) 1)))))
3325           (goto-char (point-min))
3326           (push (cons 'replied (and (search-forward "B" nil t)
3327                                     (- (point) (point-min) 1)))
3328                 pos)
3329           (goto-char (point-min))
3330           (push (cons 'score (and (search-forward "C" nil t)
3331                                   (- (point) (point-min) 1)))
3332                 pos)
3333           (goto-char (point-min))
3334           (push (cons 'download (and (search-forward "D" nil t)
3335                                      (- (point) (point-min) 1)))
3336                 pos)))
3337       (setq gnus-summary-mark-positions pos))))
3338
3339 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3340   "Insert a dummy root in the summary buffer."
3341   (beginning-of-line)
3342   (gnus-add-text-properties
3343    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3344    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3345
3346 (defun gnus-summary-extract-address-component (from)
3347   (or (car (funcall gnus-extract-address-components from))
3348       from))
3349
3350 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3351   (let ((mail-parse-charset gnus-newsgroup-charset)
3352         ; Is it really necessary to do this next part for each summary line?
3353         ; Luckily, doesn't seem to slow things down much.
3354         (mail-parse-ignored-charsets
3355          (with-current-buffer gnus-summary-buffer
3356            gnus-newsgroup-ignored-charsets)))
3357     (or
3358      (and gnus-ignored-from-addresses
3359           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3360           (let ((extra-headers (mail-header-extra header))
3361                 to
3362                 newsgroups)
3363             (cond
3364              ((setq to (cdr (assq 'To extra-headers)))
3365               (concat "-> "
3366                       (inline
3367                         (gnus-summary-extract-address-component
3368                          (funcall gnus-decode-encoded-word-function to)))))
3369              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3370               (concat "=> " newsgroups)))))
3371      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3372
3373 (defun gnus-summary-insert-line (gnus-tmp-header
3374                                  gnus-tmp-level gnus-tmp-current
3375                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3376                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3377                                  &optional gnus-tmp-dummy gnus-tmp-score
3378                                  gnus-tmp-process)
3379   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3380          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3381          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3382          (gnus-tmp-score-char
3383           (if (or (null gnus-summary-default-score)
3384                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3385                       gnus-summary-zcore-fuzz))
3386               ?                         ;Whitespace
3387             (if (< gnus-tmp-score gnus-summary-default-score)
3388                 gnus-score-below-mark gnus-score-over-mark)))
3389          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3390          (gnus-tmp-replied
3391           (cond (gnus-tmp-process gnus-process-mark)
3392                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3393                  gnus-cached-mark)
3394                 (gnus-tmp-replied gnus-replied-mark)
3395                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3396                  gnus-forwarded-mark)
3397                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3398                  gnus-saved-mark)
3399                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3400                  gnus-recent-mark)
3401                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3402                  gnus-unseen-mark)
3403                 (t gnus-no-mark)))
3404          (gnus-tmp-downloaded
3405           (cond (undownloaded
3406                  gnus-undownloaded-mark)
3407                 (gnus-newsgroup-agentized
3408                  gnus-downloaded-mark)
3409                 (t
3410                  gnus-no-mark)))
3411          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3412          (gnus-tmp-name
3413           (cond
3414            ((string-match "<[^>]+> *$" gnus-tmp-from)
3415             (let ((beg (match-beginning 0)))
3416               (or (and (string-match "^\".+\"" gnus-tmp-from)
3417                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3418                   (substring gnus-tmp-from 0 beg))))
3419            ((string-match "(.+)" gnus-tmp-from)
3420             (substring gnus-tmp-from
3421                        (1+ (match-beginning 0)) (1- (match-end 0))))
3422            (t gnus-tmp-from)))
3423          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3424          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3425          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3426          (buffer-read-only nil))
3427     (when (string= gnus-tmp-name "")
3428       (setq gnus-tmp-name gnus-tmp-from))
3429     (unless (numberp gnus-tmp-lines)
3430       (setq gnus-tmp-lines -1))
3431     (if (= gnus-tmp-lines -1)
3432         (setq gnus-tmp-lines "?")
3433       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3434       (gnus-put-text-property
3435      (point)
3436      (progn (eval gnus-summary-line-format-spec) (point))
3437        'gnus-number gnus-tmp-number)
3438     (when (gnus-visual-p 'summary-highlight 'highlight)
3439       (forward-line -1)
3440       (gnus-run-hooks 'gnus-summary-update-hook)
3441       (forward-line 1))))
3442
3443 (defun gnus-summary-update-line (&optional dont-update)
3444   "Update summary line after change."
3445   (when (and gnus-summary-default-score
3446              (not gnus-summary-inhibit-highlight))
3447     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3448            (article (gnus-summary-article-number))
3449            (score (gnus-summary-article-score article)))
3450       (unless dont-update
3451         (if (and gnus-summary-mark-below
3452                  (< (gnus-summary-article-score)
3453                     gnus-summary-mark-below))
3454             ;; This article has a low score, so we mark it as read.
3455             (when (memq article gnus-newsgroup-unreads)
3456               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3457           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3458             ;; This article was previously marked as read on account
3459             ;; of a low score, but now it has risen, so we mark it as
3460             ;; unread.
3461             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3462         (gnus-summary-update-mark
3463          (if (or (null gnus-summary-default-score)
3464                  (<= (abs (- score gnus-summary-default-score))
3465                      gnus-summary-zcore-fuzz))
3466              ?                          ;Whitespace
3467            (if (< score gnus-summary-default-score)
3468                gnus-score-below-mark gnus-score-over-mark))
3469          'score))
3470       ;; Do visual highlighting.
3471       (when (gnus-visual-p 'summary-highlight 'highlight)
3472         (gnus-run-hooks 'gnus-summary-update-hook)))))
3473
3474 (defvar gnus-tmp-new-adopts nil)
3475
3476 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3477   "Return the number of articles in THREAD.
3478 This may be 0 in some cases -- if none of the articles in
3479 the thread are to be displayed."
3480   (let* ((number
3481          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3482           (cond
3483            ((not (listp thread))
3484             1)
3485            ((and (consp thread) (cdr thread))
3486             (apply
3487              '+ 1 (mapcar
3488                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3489            ((null thread)
3490             1)
3491            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3492             1)
3493            (t 0))))
3494     (when (and level (zerop level) gnus-tmp-new-adopts)
3495       (incf number
3496             (apply '+ (mapcar
3497                        'gnus-summary-number-of-articles-in-thread
3498                        gnus-tmp-new-adopts))))
3499     (if char
3500         (if (> number 1) gnus-not-empty-thread-mark
3501           gnus-empty-thread-mark)
3502       number)))
3503
3504 (defsubst gnus-summary-line-message-size (head)
3505   "Return pretty-printed version of message size.
3506 This function is intended to be used in
3507 `gnus-summary-line-format-alist'."
3508   (let ((c (or (mail-header-chars head) -1)))
3509     (cond ((< c 0) "n/a")               ; chars not available
3510           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3511           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3512           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3513           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3514
3515
3516 (defun gnus-summary-set-local-parameters (group)
3517   "Go through the local params of GROUP and set all variable specs in that list."
3518   (let ((vars '(quit-config)))          ; Ignore quit-config.
3519     (dolist (elem (gnus-group-find-parameter group))
3520       (and (consp elem)                 ; Has to be a cons.
3521            (consp (cdr elem))           ; The cdr has to be a list.
3522            (symbolp (car elem))         ; Has to be a symbol in there.
3523            (not (memq (car elem) vars))
3524            (ignore-errors               ; So we set it.
3525              (push (car elem) vars)
3526              (make-local-variable (car elem))
3527              (set (car elem) (eval (nth 1 elem))))))))
3528
3529 (defun gnus-summary-read-group (group &optional show-all no-article
3530                                       kill-buffer no-display backward
3531                                       select-articles)
3532   "Start reading news in newsgroup GROUP.
3533 If SHOW-ALL is non-nil, already read articles are also listed.
3534 If NO-ARTICLE is non-nil, no article is selected initially.
3535 If NO-DISPLAY, don't generate a summary buffer."
3536   (let (result)
3537     (while (and group
3538                 (null (setq result
3539                             (let ((gnus-auto-select-next nil))
3540                               (or (gnus-summary-read-group-1
3541                                    group show-all no-article
3542                                    kill-buffer no-display
3543                                    select-articles)
3544                                   (setq show-all nil
3545                                         select-articles nil)))))
3546                 (eq gnus-auto-select-next 'quietly))
3547       (set-buffer gnus-group-buffer)
3548       ;; The entry function called above goes to the next
3549       ;; group automatically, so we go two groups back
3550       ;; if we are searching for the previous group.
3551       (when backward
3552         (gnus-group-prev-unread-group 2))
3553       (if (not (equal group (gnus-group-group-name)))
3554           (setq group (gnus-group-group-name))
3555         (setq group nil)))
3556     result))
3557
3558 (defun gnus-summary-read-group-1 (group show-all no-article
3559                                         kill-buffer no-display
3560                                         &optional select-articles)
3561   ;; Killed foreign groups can't be entered.
3562   ;;  (when (and (not (gnus-group-native-p group))
3563   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3564   ;;    (error "Dead non-native groups can't be entered"))
3565   (gnus-message 5 "Retrieving newsgroup: %s..."
3566                 (gnus-group-decoded-name group))
3567   (let* ((new-group (gnus-summary-setup-buffer group))
3568          (quit-config (gnus-group-quit-config group))
3569          (did-select (and new-group (gnus-select-newsgroup
3570                                      group show-all select-articles))))
3571     (cond
3572      ;; This summary buffer exists already, so we just select it.
3573      ((not new-group)
3574       (gnus-set-global-variables)
3575       (when kill-buffer
3576         (gnus-kill-or-deaden-summary kill-buffer))
3577       (gnus-configure-windows 'summary 'force)
3578       (gnus-set-mode-line 'summary)
3579       (gnus-summary-position-point)
3580       (message "")
3581       t)
3582      ;; We couldn't select this group.
3583      ((null did-select)
3584       (when (and (eq major-mode 'gnus-summary-mode)
3585                  (not (equal (current-buffer) kill-buffer)))
3586         (kill-buffer (current-buffer))
3587         (if (not quit-config)
3588             (progn
3589               ;; Update the info -- marks might need to be removed,
3590               ;; for instance.
3591               (gnus-summary-update-info)
3592               (set-buffer gnus-group-buffer)
3593               (gnus-group-jump-to-group group)
3594               (gnus-group-next-unread-group 1))
3595           (gnus-handle-ephemeral-exit quit-config)))
3596       (let ((grpinfo (gnus-get-info group)))
3597         (if (null (gnus-info-read grpinfo))
3598             (gnus-message 3 "Group %s contains no messages"
3599                           (gnus-group-decoded-name group))
3600           (gnus-message 3 "Can't select group")))
3601       nil)
3602      ;; The user did a `C-g' while prompting for number of articles,
3603      ;; so we exit this group.
3604      ((eq did-select 'quit)
3605       (and (eq major-mode 'gnus-summary-mode)
3606            (not (equal (current-buffer) kill-buffer))
3607            (kill-buffer (current-buffer)))
3608       (when kill-buffer
3609         (gnus-kill-or-deaden-summary kill-buffer))
3610       (if (not quit-config)
3611           (progn
3612             (set-buffer gnus-group-buffer)
3613             (gnus-group-jump-to-group group)
3614             (gnus-group-next-unread-group 1)
3615             (gnus-configure-windows 'group 'force))
3616         (gnus-handle-ephemeral-exit quit-config))
3617       ;; Finally signal the quit.
3618       (signal 'quit nil))
3619      ;; The group was successfully selected.
3620      (t
3621       (gnus-set-global-variables)
3622       ;; Save the active value in effect when the group was entered.
3623       (setq gnus-newsgroup-active
3624             (gnus-copy-sequence
3625              (gnus-active gnus-newsgroup-name)))
3626       ;; You can change the summary buffer in some way with this hook.
3627       (gnus-run-hooks 'gnus-select-group-hook)
3628       (when (memq 'summary (gnus-update-format-specifications
3629                             nil 'summary 'summary-mode 'summary-dummy))
3630         ;; The format specification for the summary line was updated,
3631         ;; so we need to update the mark positions as well.
3632         (gnus-update-summary-mark-positions))
3633       ;; Do score processing.
3634       (when gnus-use-scoring
3635         (gnus-possibly-score-headers))
3636       ;; Check whether to fill in the gaps in the threads.
3637       (when gnus-build-sparse-threads
3638         (gnus-build-sparse-threads))
3639       ;; Find the initial limit.
3640       (if show-all
3641           (let ((gnus-newsgroup-dormant nil))
3642             (gnus-summary-initial-limit show-all))
3643         (gnus-summary-initial-limit show-all))
3644       ;; Generate the summary buffer.
3645       (unless no-display
3646         (gnus-summary-prepare))
3647       (when gnus-use-trees
3648         (gnus-tree-open group)
3649         (setq gnus-summary-highlight-line-function
3650               'gnus-tree-highlight-article))
3651       ;; If the summary buffer is empty, but there are some low-scored
3652       ;; articles or some excluded dormants, we include these in the
3653       ;; buffer.
3654       (when (and (zerop (buffer-size))
3655                  (not no-display))
3656         (cond (gnus-newsgroup-dormant
3657                (gnus-summary-limit-include-dormant))
3658               ((and gnus-newsgroup-scored show-all)
3659                (gnus-summary-limit-include-expunged t))))
3660       ;; Function `gnus-apply-kill-file' must be called in this hook.
3661       (gnus-run-hooks 'gnus-apply-kill-hook)
3662       (if (and (zerop (buffer-size))
3663                (not no-display))
3664           (progn
3665             ;; This newsgroup is empty.
3666             (gnus-summary-catchup-and-exit nil t)
3667             (gnus-message 6 "No unread news")
3668             (when kill-buffer
3669               (gnus-kill-or-deaden-summary kill-buffer))
3670             ;; Return nil from this function.
3671             nil)
3672         ;; Hide conversation thread subtrees.  We cannot do this in
3673         ;; gnus-summary-prepare-hook since kill processing may not
3674         ;; work with hidden articles.
3675         (gnus-summary-maybe-hide-threads)
3676         (when kill-buffer
3677           (gnus-kill-or-deaden-summary kill-buffer))
3678         (gnus-summary-auto-select-subject)
3679         ;; Show first unread article if requested.
3680         (if (and (not no-article)
3681                  (not no-display)
3682                  gnus-newsgroup-unreads
3683                  gnus-auto-select-first)
3684             (progn
3685               (gnus-configure-windows 'summary)
3686               (let ((art (gnus-summary-article-number)))
3687                 (unless (and (not gnus-plugged)
3688                              (or (memq art gnus-newsgroup-undownloaded)
3689                                  (memq art gnus-newsgroup-downloadable)))
3690                   (gnus-summary-goto-article art))))
3691           ;; Don't select any articles.
3692           (gnus-summary-position-point)
3693           (gnus-configure-windows 'summary 'force)
3694           (gnus-set-mode-line 'summary))
3695         (when (and gnus-auto-center-group
3696                    (get-buffer-window gnus-group-buffer t))
3697           ;; Gotta use windows, because recenter does weird stuff if
3698           ;; the current buffer ain't the displayed window.
3699           (let ((owin (selected-window)))
3700             (select-window (get-buffer-window gnus-group-buffer t))
3701             (when (gnus-group-goto-group group)
3702               (recenter))
3703             (select-window owin)))
3704         ;; Mark this buffer as "prepared".
3705         (setq gnus-newsgroup-prepared t)
3706         (gnus-run-hooks 'gnus-summary-prepared-hook)
3707         (unless (gnus-ephemeral-group-p group)
3708           (gnus-group-update-group group))
3709         t)))))
3710
3711 (defun gnus-summary-auto-select-subject ()
3712   "Select the subject line on initial group entry."
3713   (goto-char (point-min))
3714   (cond
3715    ((eq gnus-auto-select-subject 'best)
3716     (gnus-summary-best-unread-subject))
3717    ((eq gnus-auto-select-subject 'unread)
3718     (gnus-summary-first-unread-subject))
3719    ((eq gnus-auto-select-subject 'unseen)
3720     (gnus-summary-first-unseen-subject))
3721    ((eq gnus-auto-select-subject 'unseen-or-unread)
3722     (gnus-summary-first-unseen-or-unread-subject))
3723    ((eq gnus-auto-select-subject 'first)
3724     ;; Do nothing.
3725     )
3726    ((functionp gnus-auto-select-subject)
3727     (funcall gnus-auto-select-subject))))
3728
3729 (defun gnus-summary-prepare ()
3730   "Generate the summary buffer."
3731   (interactive)
3732   (let ((buffer-read-only nil))
3733     (erase-buffer)
3734     (setq gnus-newsgroup-data nil
3735           gnus-newsgroup-data-reverse nil)
3736     (gnus-run-hooks 'gnus-summary-generate-hook)
3737     ;; Generate the buffer, either with threads or without.
3738     (when gnus-newsgroup-headers
3739       (gnus-summary-prepare-threads
3740        (if gnus-show-threads
3741            (gnus-sort-gathered-threads
3742             (funcall gnus-summary-thread-gathering-function
3743                      (gnus-sort-threads
3744                       (gnus-cut-threads (gnus-make-threads)))))
3745          ;; Unthreaded display.
3746          (gnus-sort-articles gnus-newsgroup-headers))))
3747     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3748     ;; Call hooks for modifying summary buffer.
3749     (goto-char (point-min))
3750     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3751
3752 (defsubst gnus-general-simplify-subject (subject)
3753   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3754   (setq subject
3755         (cond
3756          ;; Truncate the subject.
3757          (gnus-simplify-subject-functions
3758           (gnus-map-function gnus-simplify-subject-functions subject))
3759          ((numberp gnus-summary-gather-subject-limit)
3760           (setq subject (gnus-simplify-subject-re subject))
3761           (if (> (length subject) gnus-summary-gather-subject-limit)
3762               (substring subject 0 gnus-summary-gather-subject-limit)
3763             subject))
3764          ;; Fuzzily simplify it.
3765          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3766           (gnus-simplify-subject-fuzzy subject))
3767          ;; Just remove the leading "Re:".
3768          (t
3769           (gnus-simplify-subject-re subject))))
3770
3771   (if (and gnus-summary-gather-exclude-subject
3772            (string-match gnus-summary-gather-exclude-subject subject))
3773       nil                         ; This article shouldn't be gathered
3774     subject))
3775
3776 (defun gnus-summary-simplify-subject-query ()
3777   "Query where the respool algorithm would put this article."
3778   (interactive)
3779   (gnus-summary-select-article)
3780   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3781
3782 (defun gnus-gather-threads-by-subject (threads)
3783   "Gather threads by looking at Subject headers."
3784   (if (not gnus-summary-make-false-root)
3785       threads
3786     (let ((hashtb (gnus-make-hashtable 1024))
3787           (prev threads)
3788           (result threads)
3789           subject hthread whole-subject)
3790       (while threads
3791         (setq subject (gnus-general-simplify-subject
3792                        (setq whole-subject (mail-header-subject
3793                                             (caar threads)))))
3794         (when subject
3795           (if (setq hthread (gnus-gethash subject hashtb))
3796               (progn
3797                 ;; We enter a dummy root into the thread, if we
3798                 ;; haven't done that already.
3799                 (unless (stringp (caar hthread))
3800                   (setcar hthread (list whole-subject (car hthread))))
3801                 ;; We add this new gathered thread to this gathered
3802                 ;; thread.
3803                 (setcdr (car hthread)
3804                         (nconc (cdar hthread) (list (car threads))))
3805                 ;; Remove it from the list of threads.
3806                 (setcdr prev (cdr threads))
3807                 (setq threads prev))
3808             ;; Enter this thread into the hash table.
3809             (gnus-sethash subject
3810                           (if gnus-summary-make-false-root-always
3811                               (progn
3812                                 ;; If you want a dummy root above all
3813                                 ;; threads...
3814                                 (setcar threads (list whole-subject
3815                                                       (car threads)))
3816                                 threads)
3817                             threads)
3818                           hashtb)))
3819         (setq prev threads)
3820         (setq threads (cdr threads)))
3821       result)))
3822
3823 (defun gnus-gather-threads-by-references (threads)
3824   "Gather threads by looking at References headers."
3825   (let ((idhashtb (gnus-make-hashtable 1024))
3826         (thhashtb (gnus-make-hashtable 1024))
3827         (prev threads)
3828         (result threads)
3829         ids references id gthread gid entered ref)
3830     (while threads
3831       (when (setq references (mail-header-references (caar threads)))
3832         (setq id (mail-header-id (caar threads))
3833               ids (inline (gnus-split-references references))
3834               entered nil)
3835         (while (setq ref (pop ids))
3836           (setq ids (delete ref ids))
3837           (if (not (setq gid (gnus-gethash ref idhashtb)))
3838               (progn
3839                 (gnus-sethash ref id idhashtb)
3840                 (gnus-sethash id threads thhashtb))
3841             (setq gthread (gnus-gethash gid thhashtb))
3842             (unless entered
3843               ;; We enter a dummy root into the thread, if we
3844               ;; haven't done that already.
3845               (unless (stringp (caar gthread))
3846                 (setcar gthread (list (mail-header-subject (caar gthread))
3847                                       (car gthread))))
3848               ;; We add this new gathered thread to this gathered
3849               ;; thread.
3850               (setcdr (car gthread)
3851                       (nconc (cdar gthread) (list (car threads)))))
3852             ;; Add it into the thread hash table.
3853             (gnus-sethash id gthread thhashtb)
3854             (setq entered t)
3855             ;; Remove it from the list of threads.
3856             (setcdr prev (cdr threads))
3857             (setq threads prev))))
3858       (setq prev threads)
3859       (setq threads (cdr threads)))
3860     result))
3861
3862 (defun gnus-sort-gathered-threads (threads)
3863   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3864   (let ((result threads))
3865     (while threads
3866       (when (stringp (caar threads))
3867         (setcdr (car threads)
3868                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3869       (setq threads (cdr threads)))
3870     result))
3871
3872 (defun gnus-thread-loop-p (root thread)
3873   "Say whether ROOT is in THREAD."
3874   (let ((stack (list thread))
3875         (infloop 0)
3876         th)
3877     (while (setq thread (pop stack))
3878       (setq th (cdr thread))
3879       (while (and th
3880                   (not (eq (caar th) root)))
3881         (pop th))
3882       (if th
3883           ;; We have found a loop.
3884           (let (ref-dep)
3885             (setcdr thread (delq (car th) (cdr thread)))
3886             (if (boundp (setq ref-dep (intern "none"
3887                                               gnus-newsgroup-dependencies)))
3888                 (setcdr (symbol-value ref-dep)
3889                         (nconc (cdr (symbol-value ref-dep))
3890                                (list (car th))))
3891               (set ref-dep (list nil (car th))))
3892             (setq infloop 1
3893                   stack nil))
3894         ;; Push all the subthreads onto the stack.
3895         (push (cdr thread) stack)))
3896     infloop))
3897
3898 (defun gnus-make-threads ()
3899   "Go through the dependency hashtb and find the roots.  Return all threads."
3900   (let (threads)
3901     (while (catch 'infloop
3902              (mapatoms
3903               (lambda (refs)
3904                 ;; Deal with self-referencing References loops.
3905                 (when (and (car (symbol-value refs))
3906                            (not (zerop
3907                                  (apply
3908                                   '+
3909                                   (mapcar
3910                                    (lambda (thread)
3911                                      (gnus-thread-loop-p
3912                                       (car (symbol-value refs)) thread))
3913                                    (cdr (symbol-value refs)))))))
3914                   (setq threads nil)
3915                   (throw 'infloop t))
3916                 (unless (car (symbol-value refs))
3917                   ;; These threads do not refer back to any other
3918                   ;; articles, so they're roots.
3919                   (setq threads (append (cdr (symbol-value refs)) threads))))
3920               gnus-newsgroup-dependencies)))
3921     threads))
3922
3923 ;; Build the thread tree.
3924 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3925   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3926
3927 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3928 if it was already present.
3929
3930 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3931 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3932 Message-IDs will be renamed to a unique Message-ID before being
3933 entered.
3934
3935 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3936   (let* ((id (mail-header-id header))
3937          (id-dep (and id (intern id dependencies)))
3938          parent-id ref ref-dep ref-header replaced)
3939     ;; Enter this `header' in the `dependencies' table.
3940     (cond
3941      ((not id-dep)
3942       (setq header nil))
3943      ;; The first two cases do the normal part: enter a new `header'
3944      ;; in the `dependencies' table.
3945      ((not (boundp id-dep))
3946       (set id-dep (list header)))
3947      ((null (car (symbol-value id-dep)))
3948       (setcar (symbol-value id-dep) header))
3949
3950      ;; From here the `header' was already present in the
3951      ;; `dependencies' table.
3952      (force-new
3953       ;; Overrides an existing entry;
3954       ;; just set the header part of the entry.
3955       (setcar (symbol-value id-dep) header)
3956       (setq replaced t))
3957
3958      ;; Renames the existing `header' to a unique Message-ID.
3959      ((not gnus-summary-ignore-duplicates)
3960       ;; An article with this Message-ID has already been seen.
3961       ;; We rename the Message-ID.
3962       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3963            (list header))
3964       (mail-header-set-id header id))
3965
3966      ;; The last case ignores an existing entry, except it adds any
3967      ;; additional Xrefs (in case the two articles came from different
3968      ;; servers.
3969      ;; Also sets `header' to `nil' meaning that the `dependencies'
3970      ;; table was *not* modified.
3971      (t
3972       (mail-header-set-xref
3973        (car (symbol-value id-dep))
3974        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3975                    "")
3976                (or (mail-header-xref header) "")))
3977       (setq header nil)))
3978
3979     (when (and header (not replaced))
3980       ;; First check that we are not creating a References loop.
3981       (setq parent-id (gnus-parent-id (mail-header-references header)))
3982       (setq ref parent-id)
3983       (while (and ref
3984                   (setq ref-dep (intern-soft ref dependencies))
3985                   (boundp ref-dep)
3986                   (setq ref-header (car (symbol-value ref-dep))))
3987         (if (string= id ref)
3988             ;; Yuk!  This is a reference loop.  Make the article be a
3989             ;; root article.
3990             (progn
3991               (mail-header-set-references (car (symbol-value id-dep)) "none")
3992               (setq ref nil)
3993               (setq parent-id nil))
3994           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3995       (setq ref-dep (intern (or parent-id "none") dependencies))
3996       (if (boundp ref-dep)
3997           (setcdr (symbol-value ref-dep)
3998                   (nconc (cdr (symbol-value ref-dep))
3999                          (list (symbol-value id-dep))))
4000         (set ref-dep (list nil (symbol-value id-dep)))))
4001     header))
4002
4003 (defun gnus-extract-message-id-from-in-reply-to (string)
4004   (if (string-match "<[^>]+>" string)
4005       (substring string (match-beginning 0) (match-end 0))
4006     nil))
4007
4008 (defun gnus-build-sparse-threads ()
4009   (let ((headers gnus-newsgroup-headers)
4010         (mail-parse-charset gnus-newsgroup-charset)
4011         (gnus-summary-ignore-duplicates t)
4012         header references generation relations
4013         subject child end new-child date)
4014     ;; First we create an alist of generations/relations, where
4015     ;; generations is how much we trust the relation, and the relation
4016     ;; is parent/child.
4017     (gnus-message 7 "Making sparse threads...")
4018     (save-excursion
4019       (nnheader-set-temp-buffer " *gnus sparse threads*")
4020       (while (setq header (pop headers))
4021         (when (and (setq references (mail-header-references header))
4022                    (not (string= references "")))
4023           (insert references)
4024           (setq child (mail-header-id header)
4025                 subject (mail-header-subject header)
4026                 date (mail-header-date header)
4027                 generation 0)
4028           (while (search-backward ">" nil t)
4029             (setq end (1+ (point)))
4030             (when (search-backward "<" nil t)
4031               (setq new-child (buffer-substring (point) end))
4032               (push (list (incf generation)
4033                           child (setq child new-child)
4034                           subject date)
4035                     relations)))
4036           (when child
4037             (push (list (1+ generation) child nil subject) relations))
4038           (erase-buffer)))
4039       (kill-buffer (current-buffer)))
4040     ;; Sort over trustworthiness.
4041     (mapcar
4042      (lambda (relation)
4043        (when (gnus-dependencies-add-header
4044               (make-full-mail-header
4045                gnus-reffed-article-number
4046                (nth 3 relation) "" (or (nth 4 relation) "")
4047                (nth 1 relation)
4048                (or (nth 2 relation) "") 0 0 "")
4049               gnus-newsgroup-dependencies nil)
4050          (push gnus-reffed-article-number gnus-newsgroup-limit)
4051          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4052          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4053                gnus-newsgroup-reads)
4054          (decf gnus-reffed-article-number)))
4055      (sort relations 'car-less-than-car))
4056     (gnus-message 7 "Making sparse threads...done")))
4057
4058 (defun gnus-build-old-threads ()
4059   ;; Look at all the articles that refer back to old articles, and
4060   ;; fetch the headers for the articles that aren't there.  This will
4061   ;; build complete threads - if the roots haven't been expired by the
4062   ;; server, that is.
4063   (let ((mail-parse-charset gnus-newsgroup-charset)
4064         id heads)
4065     (mapatoms
4066      (lambda (refs)
4067        (when (not (car (symbol-value refs)))
4068          (setq heads (cdr (symbol-value refs)))
4069          (while heads
4070            (if (memq (mail-header-number (caar heads))
4071                      gnus-newsgroup-dormant)
4072                (setq heads (cdr heads))
4073              (setq id (symbol-name refs))
4074              (while (and (setq id (gnus-build-get-header id))
4075                          (not (car (gnus-id-to-thread id)))))
4076              (setq heads nil)))))
4077      gnus-newsgroup-dependencies)))
4078
4079 (defsubst gnus-remove-odd-characters (string)
4080   "Translate STRING into something that doesn't contain weird characters."
4081   (mm-subst-char-in-string
4082    ?\r ?\-
4083    (mm-subst-char-in-string ?\n ?\- string t) t))
4084
4085 ;; This function has to be called with point after the article number
4086 ;; on the beginning of the line.
4087 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4088   (let ((eol (point-at-eol))
4089         (buffer (current-buffer))
4090         header references in-reply-to)
4091
4092     ;; overview: [num subject from date id refs chars lines misc]
4093     (unwind-protect
4094         (let (x)
4095           (narrow-to-region (point) eol)
4096           (unless (eobp)
4097             (forward-char))
4098
4099           (setq header
4100                 (make-full-mail-header
4101                  number                 ; number
4102                  (condition-case ()     ; subject
4103                      (gnus-remove-odd-characters
4104                       (funcall gnus-decode-encoded-word-function
4105                                (setq x (nnheader-nov-field))))
4106                    (error x))
4107                  (condition-case ()     ; from
4108                      (gnus-remove-odd-characters
4109                       (funcall gnus-decode-encoded-word-function
4110                                (setq x (nnheader-nov-field))))
4111                    (error x))
4112                  (nnheader-nov-field)   ; date
4113                  (nnheader-nov-read-message-id number)  ; id
4114                  (setq references (nnheader-nov-field)) ; refs
4115                  (nnheader-nov-read-integer) ; chars
4116                  (nnheader-nov-read-integer) ; lines
4117                  (unless (eobp)
4118                    (if (looking-at "Xref: ")
4119                        (goto-char (match-end 0)))
4120                    (nnheader-nov-field)) ; Xref
4121                  (nnheader-nov-parse-extra)))) ; extra
4122
4123       (widen))
4124
4125     (when (and (string= references "")
4126                (setq in-reply-to (mail-header-extra header))
4127                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4128       (mail-header-set-references
4129        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4130
4131     (when gnus-alter-header-function
4132       (funcall gnus-alter-header-function header))
4133     (gnus-dependencies-add-header header dependencies force-new)))
4134
4135 (defun gnus-build-get-header (id)
4136   "Look through the buffer of NOV lines and find the header to ID.
4137 Enter this line into the dependencies hash table, and return
4138 the id of the parent article (if any)."
4139   (let ((deps gnus-newsgroup-dependencies)
4140         found header)
4141     (prog1
4142         (save-excursion
4143           (set-buffer nntp-server-buffer)
4144           (let ((case-fold-search nil))
4145             (goto-char (point-min))
4146             (while (and (not found)
4147                         (search-forward id nil t))
4148               (beginning-of-line)
4149               (setq found (looking-at
4150                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4151                                    (regexp-quote id))))
4152               (or found (beginning-of-line 2)))
4153             (when found
4154               (beginning-of-line)
4155               (and
4156                (setq header (gnus-nov-parse-line
4157                              (read (current-buffer)) deps))
4158                (gnus-parent-id (mail-header-references header))))))
4159       (when header
4160         (let ((number (mail-header-number header)))
4161           (push number gnus-newsgroup-limit)
4162           (push header gnus-newsgroup-headers)
4163           (if (memq number gnus-newsgroup-unselected)
4164               (progn
4165                 (setq gnus-newsgroup-unreads
4166                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4167                                                number))
4168                 (setq gnus-newsgroup-unselected
4169                       (delq number gnus-newsgroup-unselected)))
4170             (push number gnus-newsgroup-ancient)))))))
4171
4172 (defun gnus-build-all-threads ()
4173   "Read all the headers."
4174   (let ((gnus-summary-ignore-duplicates t)
4175         (mail-parse-charset gnus-newsgroup-charset)
4176         (dependencies gnus-newsgroup-dependencies)
4177         header article)
4178     (save-excursion
4179       (set-buffer nntp-server-buffer)
4180       (let ((case-fold-search nil))
4181         (goto-char (point-min))
4182         (while (not (eobp))
4183           (ignore-errors
4184             (setq article (read (current-buffer))
4185                   header (gnus-nov-parse-line article dependencies)))
4186           (when header
4187             (with-current-buffer gnus-summary-buffer
4188               (push header gnus-newsgroup-headers)
4189               (if (memq (setq article (mail-header-number header))
4190                         gnus-newsgroup-unselected)
4191                   (progn
4192                     (setq gnus-newsgroup-unreads
4193                           (gnus-add-to-sorted-list
4194                            gnus-newsgroup-unreads article))
4195                     (setq gnus-newsgroup-unselected
4196                           (delq article gnus-newsgroup-unselected)))
4197                 (push article gnus-newsgroup-ancient)))
4198             (forward-line 1)))))))
4199
4200 (defun gnus-summary-update-article-line (article header)
4201   "Update the line for ARTICLE using HEADER."
4202   (let* ((id (mail-header-id header))
4203          (thread (gnus-id-to-thread id)))
4204     (unless thread
4205       (error "Article in no thread"))
4206     ;; Update the thread.
4207     (setcar thread header)
4208     (gnus-summary-goto-subject article)
4209     (let* ((datal (gnus-data-find-list article))
4210            (data (car datal))
4211            (buffer-read-only nil)
4212            (level (gnus-summary-thread-level)))
4213       (gnus-delete-line)
4214       (let ((inserted (- (point)
4215                          (progn
4216                            (gnus-summary-insert-line
4217                             header level nil
4218                             (memq article gnus-newsgroup-undownloaded)
4219                             (gnus-article-mark article)
4220                             (memq article gnus-newsgroup-replied)
4221                             (memq article gnus-newsgroup-expirable)
4222                             ;; Only insert the Subject string when it's different
4223                             ;; from the previous Subject string.
4224                             (if (and
4225                                  gnus-show-threads
4226                                  (gnus-subject-equal
4227                                   (condition-case ()
4228                                       (mail-header-subject
4229                                        (gnus-data-header
4230                                         (cadr
4231                                          (gnus-data-find-list
4232                                           article
4233                                           (gnus-data-list t)))))
4234                                     ;; Error on the side of excessive subjects.
4235                                     (error ""))
4236                                   (mail-header-subject header)))
4237                                 ""
4238                               (mail-header-subject header))
4239                             nil (cdr (assq article gnus-newsgroup-scored))
4240                             (memq article gnus-newsgroup-processable))
4241                            (point)))))
4242         (when (cdr datal)
4243           (gnus-data-update-list
4244            (cdr datal)
4245            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4246
4247 (defun gnus-summary-update-article (article &optional iheader)
4248   "Update ARTICLE in the summary buffer."
4249   (set-buffer gnus-summary-buffer)
4250   (let* ((header (gnus-summary-article-header article))
4251          (id (mail-header-id header))
4252          (data (gnus-data-find article))
4253          (thread (gnus-id-to-thread id))
4254          (references (mail-header-references header))
4255          (parent
4256           (gnus-id-to-thread
4257            (or (gnus-parent-id
4258                 (when (and references
4259                            (not (equal "" references)))
4260                   references))
4261                "none")))
4262          (buffer-read-only nil)
4263          (old (car thread)))
4264     (when thread
4265       (unless iheader
4266         (setcar thread nil)
4267         (when parent
4268           (delq thread parent)))
4269       (if (gnus-summary-insert-subject id header)
4270           ;; Set the (possibly) new article number in the data structure.
4271           (gnus-data-set-number data (gnus-id-to-article id))
4272         (setcar thread old)
4273         nil))))
4274
4275 (defun gnus-rebuild-thread (id &optional line)
4276   "Rebuild the thread containing ID.
4277 If LINE, insert the rebuilt thread starting on line LINE."
4278   (let ((buffer-read-only nil)
4279         old-pos current thread data)
4280     (if (not gnus-show-threads)
4281         (setq thread (list (car (gnus-id-to-thread id))))
4282       ;; Get the thread this article is part of.
4283       (setq thread (gnus-remove-thread id)))
4284     (setq old-pos (point-at-bol))
4285     (setq current (save-excursion
4286                     (and (re-search-backward "[\r\n]" nil t)
4287                          (gnus-summary-article-number))))
4288     ;; If this is a gathered thread, we have to go some re-gathering.
4289     (when (stringp (car thread))
4290       (let ((subject (car thread))
4291             roots thr)
4292         (setq thread (cdr thread))
4293         (while thread
4294           (unless (memq (setq thr (gnus-id-to-thread
4295                                    (gnus-root-id
4296                                     (mail-header-id (caar thread)))))
4297                         roots)
4298             (push thr roots))
4299           (setq thread (cdr thread)))
4300         ;; We now have all (unique) roots.
4301         (if (= (length roots) 1)
4302             ;; All the loose roots are now one solid root.
4303             (setq thread (car roots))
4304           (setq thread (cons subject (gnus-sort-threads roots))))))
4305     (let (threads)
4306       ;; We then insert this thread into the summary buffer.
4307       (when line
4308         (goto-char (point-min))
4309         (forward-line (1- line)))
4310       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4311         (if gnus-show-threads
4312             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4313           (gnus-summary-prepare-unthreaded thread))
4314         (setq data (nreverse gnus-newsgroup-data))
4315         (setq threads gnus-newsgroup-threads))
4316       ;; We splice the new data into the data structure.
4317       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4318       ;;!!! then we want to insert at the beginning of the buffer.
4319       ;;!!! That happens to be true with Gnus now, but that may
4320       ;;!!! change in the future.  Perhaps.
4321       (gnus-data-enter-list
4322        (if line nil current) data (- (point) old-pos))
4323       (setq gnus-newsgroup-threads
4324             (nconc threads gnus-newsgroup-threads))
4325       (gnus-data-compute-positions))))
4326
4327 (defun gnus-number-to-header (number)
4328   "Return the header for article NUMBER."
4329   (let ((headers gnus-newsgroup-headers))
4330     (while (and headers
4331                 (not (= number (mail-header-number (car headers)))))
4332       (pop headers))
4333     (when headers
4334       (car headers))))
4335
4336 (defun gnus-parent-headers (in-headers &optional generation)
4337   "Return the headers of the GENERATIONeth parent of HEADERS."
4338   (unless generation
4339     (setq generation 1))
4340   (let ((parent t)
4341         (headers in-headers)
4342         references)
4343     (while (and parent
4344                 (not (zerop generation))
4345                 (setq references (mail-header-references headers)))
4346       (setq headers (if (and references
4347                              (setq parent (gnus-parent-id references)))
4348                         (car (gnus-id-to-thread parent))
4349                       nil))
4350       (decf generation))
4351     (and (not (eq headers in-headers))
4352          headers)))
4353
4354 (defun gnus-id-to-thread (id)
4355   "Return the (sub-)thread where ID appears."
4356   (gnus-gethash id gnus-newsgroup-dependencies))
4357
4358 (defun gnus-id-to-article (id)
4359   "Return the article number of ID."
4360   (let ((thread (gnus-id-to-thread id)))
4361     (when (and thread
4362                (car thread))
4363       (mail-header-number (car thread)))))
4364
4365 (defun gnus-id-to-header (id)
4366   "Return the article headers of ID."
4367   (car (gnus-id-to-thread id)))
4368
4369 (defun gnus-article-displayed-root-p (article)
4370   "Say whether ARTICLE is a root(ish) article."
4371   (let ((level (gnus-summary-thread-level article))
4372         (refs (mail-header-references  (gnus-summary-article-header article)))
4373         particle)
4374     (cond
4375      ((null level) nil)
4376      ((zerop level) t)
4377      ((null refs) t)
4378      ((null (gnus-parent-id refs)) t)
4379      ((and (= 1 level)
4380            (null (setq particle (gnus-id-to-article
4381                                  (gnus-parent-id refs))))
4382            (null (gnus-summary-thread-level particle)))))))
4383
4384 (defun gnus-root-id (id)
4385   "Return the id of the root of the thread where ID appears."
4386   (let (last-id prev)
4387     (while (and id (setq prev (car (gnus-id-to-thread id))))
4388       (setq last-id id
4389             id (gnus-parent-id (mail-header-references prev))))
4390     last-id))
4391
4392 (defun gnus-articles-in-thread (thread)
4393   "Return the list of articles in THREAD."
4394   (cons (mail-header-number (car thread))
4395         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4396
4397 (defun gnus-remove-thread (id &optional dont-remove)
4398   "Remove the thread that has ID in it."
4399   (let (headers thread last-id)
4400     ;; First go up in this thread until we find the root.
4401     (setq last-id (gnus-root-id id)
4402           headers (message-flatten-list (gnus-id-to-thread last-id)))
4403     ;; We have now found the real root of this thread.  It might have
4404     ;; been gathered into some loose thread, so we have to search
4405     ;; through the threads to find the thread we wanted.
4406     (let ((threads gnus-newsgroup-threads)
4407           sub)
4408       (while threads
4409         (setq sub (car threads))
4410         (if (stringp (car sub))
4411             ;; This is a gathered thread, so we look at the roots
4412             ;; below it to find whether this article is in this
4413             ;; gathered root.
4414             (progn
4415               (setq sub (cdr sub))
4416               (while sub
4417                 (when (member (caar sub) headers)
4418                   (setq thread (car threads)
4419                         threads nil
4420                         sub nil))
4421                 (setq sub (cdr sub))))
4422           ;; It's an ordinary thread, so we check it.
4423           (when (eq (car sub) (car headers))
4424             (setq thread sub
4425                   threads nil)))
4426         (setq threads (cdr threads)))
4427       ;; If this article is in no thread, then it's a root.
4428       (if thread
4429           (unless dont-remove
4430             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4431         (setq thread (gnus-id-to-thread last-id)))
4432       (when thread
4433         (prog1
4434             thread                      ; We return this thread.
4435           (unless dont-remove
4436             (if (stringp (car thread))
4437                 (progn
4438                   ;; If we use dummy roots, then we have to remove the
4439                   ;; dummy root as well.
4440                   (when (eq gnus-summary-make-false-root 'dummy)
4441                     ;; We go to the dummy root by going to
4442                     ;; the first sub-"thread", and then one line up.
4443                     (gnus-summary-goto-article
4444                      (mail-header-number (caadr thread)))
4445                     (forward-line -1)
4446                     (gnus-delete-line)
4447                     (gnus-data-compute-positions))
4448                   (setq thread (cdr thread))
4449                   (while thread
4450                     (gnus-remove-thread-1 (car thread))
4451                     (setq thread (cdr thread))))
4452               (gnus-remove-thread-1 thread))))))))
4453
4454 (defun gnus-remove-thread-1 (thread)
4455   "Remove the thread THREAD recursively."
4456   (let ((number (mail-header-number (pop thread)))
4457         d)
4458     (setq thread (reverse thread))
4459     (while thread
4460       (gnus-remove-thread-1 (pop thread)))
4461     (when (setq d (gnus-data-find number))
4462       (goto-char (gnus-data-pos d))
4463       (gnus-summary-show-thread)
4464       (gnus-data-remove
4465        number
4466        (- (point-at-bol)
4467           (prog1
4468               (1+ (point-at-eol))
4469             (gnus-delete-line)))))))
4470
4471 (defun gnus-sort-threads-1 (threads func)
4472   (sort (mapcar (lambda (thread)
4473                   (cons (car thread)
4474                         (and (cdr thread)
4475                              (gnus-sort-threads-1 (cdr thread) func))))
4476                 threads) func))
4477
4478 (defun gnus-sort-threads (threads)
4479   "Sort THREADS."
4480   (if (not gnus-thread-sort-functions)
4481       threads
4482     (gnus-message 8 "Sorting threads...")
4483     (let ((max-lisp-eval-depth 5000))
4484       (prog1 (gnus-sort-threads-1
4485          threads
4486          (gnus-make-sort-function gnus-thread-sort-functions))
4487         (gnus-message 8 "Sorting threads...done")))))
4488
4489 (defun gnus-sort-articles (articles)
4490   "Sort ARTICLES."
4491   (when gnus-article-sort-functions
4492     (gnus-message 7 "Sorting articles...")
4493     (prog1
4494         (setq gnus-newsgroup-headers
4495               (sort articles (gnus-make-sort-function
4496                               gnus-article-sort-functions)))
4497       (gnus-message 7 "Sorting articles...done"))))
4498
4499 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4500 (defmacro gnus-thread-header (thread)
4501   "Return header of first article in THREAD.
4502 Note that THREAD must never, ever be anything else than a variable -
4503 using some other form will lead to serious barfage."
4504   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4505   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4506   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4507         (vector thread) 2))
4508
4509 (defsubst gnus-article-sort-by-number (h1 h2)
4510   "Sort articles by article number."
4511   (< (mail-header-number h1)
4512      (mail-header-number h2)))
4513
4514 (defun gnus-thread-sort-by-number (h1 h2)
4515   "Sort threads by root article number."
4516   (gnus-article-sort-by-number
4517    (gnus-thread-header h1) (gnus-thread-header h2)))
4518
4519 (defsubst gnus-article-sort-by-random (h1 h2)
4520   "Sort articles by article number."
4521   (zerop (random 2)))
4522
4523 (defun gnus-thread-sort-by-random (h1 h2)
4524   "Sort threads by root article number."
4525   (gnus-article-sort-by-random
4526    (gnus-thread-header h1) (gnus-thread-header h2)))
4527
4528 (defsubst gnus-article-sort-by-lines (h1 h2)
4529   "Sort articles by article Lines header."
4530   (< (mail-header-lines h1)
4531      (mail-header-lines h2)))
4532
4533 (defun gnus-thread-sort-by-lines (h1 h2)
4534   "Sort threads by root article Lines header."
4535   (gnus-article-sort-by-lines
4536    (gnus-thread-header h1) (gnus-thread-header h2)))
4537
4538 (defsubst gnus-article-sort-by-chars (h1 h2)
4539   "Sort articles by octet length."
4540   (< (mail-header-chars h1)
4541      (mail-header-chars h2)))
4542
4543 (defun gnus-thread-sort-by-chars (h1 h2)
4544   "Sort threads by root article octet length."
4545   (gnus-article-sort-by-chars
4546    (gnus-thread-header h1) (gnus-thread-header h2)))
4547
4548 (defsubst gnus-article-sort-by-author (h1 h2)
4549   "Sort articles by root author."
4550   (string-lessp
4551    (let ((extract (funcall
4552                    gnus-extract-address-components
4553                    (mail-header-from h1))))
4554      (or (car extract) (cadr extract) ""))
4555    (let ((extract (funcall
4556                    gnus-extract-address-components
4557                    (mail-header-from h2))))
4558      (or (car extract) (cadr extract) ""))))
4559
4560 (defun gnus-thread-sort-by-author (h1 h2)
4561   "Sort threads by root author."
4562   (gnus-article-sort-by-author
4563    (gnus-thread-header h1)  (gnus-thread-header h2)))
4564
4565 (defsubst gnus-article-sort-by-subject (h1 h2)
4566   "Sort articles by root subject."
4567   (string-lessp
4568    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4569    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4570
4571 (defun gnus-thread-sort-by-subject (h1 h2)
4572   "Sort threads by root subject."
4573   (gnus-article-sort-by-subject
4574    (gnus-thread-header h1) (gnus-thread-header h2)))
4575
4576 (defsubst gnus-article-sort-by-date (h1 h2)
4577   "Sort articles by root article date."
4578   (time-less-p
4579    (gnus-date-get-time (mail-header-date h1))
4580    (gnus-date-get-time (mail-header-date h2))))
4581
4582 (defun gnus-thread-sort-by-date (h1 h2)
4583   "Sort threads by root article date."
4584   (gnus-article-sort-by-date
4585    (gnus-thread-header h1) (gnus-thread-header h2)))
4586
4587 (defsubst gnus-article-sort-by-score (h1 h2)
4588   "Sort articles by root article score.
4589 Unscored articles will be counted as having a score of zero."
4590   (> (or (cdr (assq (mail-header-number h1)
4591                     gnus-newsgroup-scored))
4592          gnus-summary-default-score 0)
4593      (or (cdr (assq (mail-header-number h2)
4594                     gnus-newsgroup-scored))
4595          gnus-summary-default-score 0)))
4596
4597 (defun gnus-thread-sort-by-score (h1 h2)
4598   "Sort threads by root article score."
4599   (gnus-article-sort-by-score
4600    (gnus-thread-header h1) (gnus-thread-header h2)))
4601
4602 (defun gnus-thread-sort-by-total-score (h1 h2)
4603   "Sort threads by the sum of all scores in the thread.
4604 Unscored articles will be counted as having a score of zero."
4605   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4606
4607 (defun gnus-thread-total-score (thread)
4608   ;; This function find the total score of THREAD.
4609   (cond
4610    ((null thread)
4611     0)
4612    ((consp thread)
4613     (if (stringp (car thread))
4614         (apply gnus-thread-score-function 0
4615                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4616       (gnus-thread-total-score-1 thread)))
4617    (t
4618     (gnus-thread-total-score-1 (list thread)))))
4619
4620 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4621   "Sort threads such that the thread with the most recently arrived article comes first."
4622   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4623
4624 (defun gnus-thread-highest-number (thread)
4625   "Return the highest article number in THREAD."
4626   (apply 'max (mapcar (lambda (header)
4627                         (mail-header-number header))
4628                       (message-flatten-list thread))))
4629
4630 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4631   "Sort threads such that the thread with the most recently dated article comes first."
4632   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4633
4634 (defun gnus-thread-latest-date (thread)
4635   "Return the highest article date in THREAD."
4636   (let ((previous-time 0))
4637     (apply 'max
4638            (mapcar
4639             (lambda (header)
4640               (setq previous-time
4641                     (condition-case ()
4642                         (time-to-seconds (mail-header-parse-date
4643                                           (mail-header-date header)))
4644                       (error previous-time))))
4645             (sort
4646              (message-flatten-list thread)
4647              (lambda (h1 h2)
4648                (< (mail-header-number h1)
4649                   (mail-header-number h2))))))))
4650
4651 (defun gnus-thread-total-score-1 (root)
4652   ;; This function find the total score of the thread below ROOT.
4653   (setq root (car root))
4654   (apply gnus-thread-score-function
4655          (or (append
4656               (mapcar 'gnus-thread-total-score
4657                       (cdr (gnus-id-to-thread (mail-header-id root))))
4658               (when (> (mail-header-number root) 0)
4659                 (list (or (cdr (assq (mail-header-number root)
4660                                      gnus-newsgroup-scored))
4661                           gnus-summary-default-score 0))))
4662              (list gnus-summary-default-score)
4663              '(0))))
4664
4665 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4666 (defvar gnus-tmp-prev-subject nil)
4667 (defvar gnus-tmp-false-parent nil)
4668 (defvar gnus-tmp-root-expunged nil)
4669 (defvar gnus-tmp-dummy-line nil)
4670
4671 (eval-when-compile (defvar gnus-tmp-header))
4672 (defun gnus-extra-header (type &optional header)
4673   "Return the extra header of TYPE."
4674   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4675       ""))
4676
4677 (defvar gnus-tmp-thread-tree-header-string "")
4678
4679 (defcustom gnus-sum-thread-tree-root "> "
4680   "With %B spec, used for the root of a thread.
4681 If nil, use subject instead."
4682   :version "21.4"
4683   :type '(radio (const :format "%v  " nil) (string :size 0))
4684   :group 'gnus-thread)
4685 (defcustom gnus-sum-thread-tree-false-root "> "
4686   "With %B spec, used for a false root of a thread.
4687 If nil, use subject instead."
4688   :version "21.4"
4689   :type '(radio (const :format "%v  " nil) (string :size 0))
4690   :group 'gnus-thread)
4691 (defcustom gnus-sum-thread-tree-single-indent ""
4692   "With %B spec, used for a thread with just one message.
4693 If nil, use subject instead."
4694   :version "21.4"
4695   :type '(radio (const :format "%v  " nil) (string :size 0))
4696   :group 'gnus-thread)
4697 (defcustom gnus-sum-thread-tree-vertical "| "
4698   "With %B spec, used for drawing a vertical line."
4699   :version "21.4"
4700   :type 'string
4701   :group 'gnus-thread)
4702 (defcustom gnus-sum-thread-tree-indent "  "
4703   "With %B spec, used for indenting."
4704   :version "21.4"
4705   :type 'string
4706   :group 'gnus-thread)
4707 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4708   "With %B spec, used for a leaf with brothers."
4709   :version "21.4"
4710   :type 'string
4711   :group 'gnus-thread)
4712 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4713   "With %B spec, used for a leaf without brothers."
4714   :version "21.4"
4715   :type 'string
4716   :group 'gnus-thread)
4717
4718 (defun gnus-summary-prepare-threads (threads)
4719   "Prepare summary buffer from THREADS and indentation LEVEL.
4720 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4721 or a straight list of headers."
4722   (gnus-message 7 "Generating summary...")
4723
4724   (setq gnus-newsgroup-threads threads)
4725   (beginning-of-line)
4726
4727   (let ((gnus-tmp-level 0)
4728         (default-score (or gnus-summary-default-score 0))
4729         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4730         (building-line-count gnus-summary-display-while-building)
4731         (building-count (integerp gnus-summary-display-while-building))
4732         thread number subject stack state gnus-tmp-gathered beg-match
4733         new-roots gnus-tmp-new-adopts thread-end simp-subject
4734         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4735         gnus-tmp-replied gnus-tmp-subject-or-nil
4736         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4737         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4738         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4739         tree-stack)
4740
4741     (setq gnus-tmp-prev-subject nil
4742           gnus-tmp-thread-tree-header-string "")
4743
4744     (if (vectorp (car threads))
4745         ;; If this is a straight (sic) list of headers, then a
4746         ;; threaded summary display isn't required, so we just create
4747         ;; an unthreaded one.
4748         (gnus-summary-prepare-unthreaded threads)
4749
4750       ;; Do the threaded display.
4751
4752       (if gnus-summary-display-while-building
4753           (switch-to-buffer (buffer-name)))
4754       (while (or threads stack gnus-tmp-new-adopts new-roots)
4755
4756         (if (and (= gnus-tmp-level 0)
4757                  (or (not stack)
4758                      (= (caar stack) 0))
4759                  (not gnus-tmp-false-parent)
4760                  (or gnus-tmp-new-adopts new-roots))
4761             (if gnus-tmp-new-adopts
4762                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4763                       thread (list (car gnus-tmp-new-adopts))
4764                       gnus-tmp-header (caar thread)
4765                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4766               (when new-roots
4767                 (setq thread (list (car new-roots))
4768                       gnus-tmp-header (caar thread)
4769                       new-roots (cdr new-roots))))
4770
4771           (if threads
4772               ;; If there are some threads, we do them before the
4773               ;; threads on the stack.
4774               (setq thread threads
4775                     gnus-tmp-header (caar thread))
4776             ;; There were no current threads, so we pop something off
4777             ;; the stack.
4778             (setq state (car stack)
4779                   gnus-tmp-level (car state)
4780                   tree-stack (cadr state)
4781                   thread (caddr state)
4782                   stack (cdr stack)
4783                   gnus-tmp-header (caar thread))))
4784
4785         (setq gnus-tmp-false-parent nil)
4786         (setq gnus-tmp-root-expunged nil)
4787         (setq thread-end nil)
4788
4789         (if (stringp gnus-tmp-header)
4790             ;; The header is a dummy root.
4791             (cond
4792              ((eq gnus-summary-make-false-root 'adopt)
4793               ;; We let the first article adopt the rest.
4794               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4795                                                (cddar thread)))
4796               (setq gnus-tmp-gathered
4797                     (nconc (mapcar
4798                             (lambda (h) (mail-header-number (car h)))
4799                             (cddar thread))
4800                            gnus-tmp-gathered))
4801               (setq thread (cons (list (caar thread)
4802                                        (cadar thread))
4803                                  (cdr thread)))
4804               (setq gnus-tmp-level -1
4805                     gnus-tmp-false-parent t))
4806              ((eq gnus-summary-make-false-root 'empty)
4807               ;; We print adopted articles with empty subject fields.
4808               (setq gnus-tmp-gathered
4809                     (nconc (mapcar
4810                             (lambda (h) (mail-header-number (car h)))
4811                             (cddar thread))
4812                            gnus-tmp-gathered))
4813               (setq gnus-tmp-level -1))
4814              ((eq gnus-summary-make-false-root 'dummy)
4815               ;; We remember that we probably want to output a dummy
4816               ;; root.
4817               (setq gnus-tmp-dummy-line gnus-tmp-header)
4818               (setq gnus-tmp-prev-subject gnus-tmp-header))
4819              (t
4820               ;; We do not make a root for the gathered
4821               ;; sub-threads at all.
4822               (setq gnus-tmp-level -1)))
4823
4824           (setq number (mail-header-number gnus-tmp-header)
4825                 subject (mail-header-subject gnus-tmp-header)
4826                 simp-subject (gnus-simplify-subject-fully subject))
4827
4828           (cond
4829            ;; If the thread has changed subject, we might want to make
4830            ;; this subthread into a root.
4831            ((and (null gnus-thread-ignore-subject)
4832                  (not (zerop gnus-tmp-level))
4833                  gnus-tmp-prev-subject
4834                  (not (string= gnus-tmp-prev-subject simp-subject)))
4835             (setq new-roots (nconc new-roots (list (car thread)))
4836                   thread-end t
4837                   gnus-tmp-header nil))
4838            ;; If the article lies outside the current limit,
4839            ;; then we do not display it.
4840            ((not (memq number gnus-newsgroup-limit))
4841             (setq gnus-tmp-gathered
4842                   (nconc (mapcar
4843                           (lambda (h) (mail-header-number (car h)))
4844                           (cdar thread))
4845                          gnus-tmp-gathered))
4846             (setq gnus-tmp-new-adopts (if (cdar thread)
4847                                           (append gnus-tmp-new-adopts
4848                                                   (cdar thread))
4849                                         gnus-tmp-new-adopts)
4850                   thread-end t
4851                   gnus-tmp-header nil)
4852             (when (zerop gnus-tmp-level)
4853               (setq gnus-tmp-root-expunged t)))
4854            ;; Perhaps this article is to be marked as read?
4855            ((and gnus-summary-mark-below
4856                  (< (or (cdr (assq number gnus-newsgroup-scored))
4857                         default-score)
4858                     gnus-summary-mark-below)
4859                  ;; Don't touch sparse articles.
4860                  (not (gnus-summary-article-sparse-p number))
4861                  (not (gnus-summary-article-ancient-p number)))
4862             (setq gnus-newsgroup-unreads
4863                   (delq number gnus-newsgroup-unreads))
4864             (if gnus-newsgroup-auto-expire
4865                 (setq gnus-newsgroup-expirable
4866                       (gnus-add-to-sorted-list
4867                        gnus-newsgroup-expirable number))
4868               (push (cons number gnus-low-score-mark)
4869                     gnus-newsgroup-reads))))
4870
4871           (when gnus-tmp-header
4872             ;; We may have an old dummy line to output before this
4873             ;; article.
4874             (when (and gnus-tmp-dummy-line
4875                        (gnus-subject-equal
4876                         gnus-tmp-dummy-line
4877                         (mail-header-subject gnus-tmp-header)))
4878               (gnus-summary-insert-dummy-line
4879                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4880               (setq gnus-tmp-dummy-line nil))
4881
4882             ;; Compute the mark.
4883             (setq gnus-tmp-unread (gnus-article-mark number))
4884
4885             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4886                                   gnus-tmp-header gnus-tmp-level)
4887                   gnus-newsgroup-data)
4888
4889             ;; Actually insert the line.
4890             (setq
4891              gnus-tmp-subject-or-nil
4892              (cond
4893               ((and gnus-thread-ignore-subject
4894                     gnus-tmp-prev-subject
4895                     (not (string= gnus-tmp-prev-subject simp-subject)))
4896                subject)
4897               ((zerop gnus-tmp-level)
4898                (if (and (eq gnus-summary-make-false-root 'empty)
4899                         (memq number gnus-tmp-gathered)
4900                         gnus-tmp-prev-subject
4901                         (string= gnus-tmp-prev-subject simp-subject))
4902                    gnus-summary-same-subject
4903                  subject))
4904               (t gnus-summary-same-subject)))
4905             (if (and (eq gnus-summary-make-false-root 'adopt)
4906                      (= gnus-tmp-level 1)
4907                      (memq number gnus-tmp-gathered))
4908                 (setq gnus-tmp-opening-bracket ?\<
4909                       gnus-tmp-closing-bracket ?\>)
4910               (setq gnus-tmp-opening-bracket ?\[
4911                     gnus-tmp-closing-bracket ?\]))
4912             (setq
4913              gnus-tmp-indentation
4914              (aref gnus-thread-indent-array gnus-tmp-level)
4915              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4916              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4917                                 gnus-summary-default-score 0)
4918              gnus-tmp-score-char
4919              (if (or (null gnus-summary-default-score)
4920                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4921                          gnus-summary-zcore-fuzz))
4922                  ?                      ;Whitespace
4923                (if (< gnus-tmp-score gnus-summary-default-score)
4924                    gnus-score-below-mark gnus-score-over-mark))
4925              gnus-tmp-replied
4926              (cond ((memq number gnus-newsgroup-processable)
4927                     gnus-process-mark)
4928                    ((memq number gnus-newsgroup-cached)
4929                     gnus-cached-mark)
4930                    ((memq number gnus-newsgroup-replied)
4931                     gnus-replied-mark)
4932                    ((memq number gnus-newsgroup-forwarded)
4933                     gnus-forwarded-mark)
4934                    ((memq number gnus-newsgroup-saved)
4935                     gnus-saved-mark)
4936                    ((memq number gnus-newsgroup-recent)
4937                     gnus-recent-mark)
4938                    ((memq number gnus-newsgroup-unseen)
4939                     gnus-unseen-mark)
4940                    (t gnus-no-mark))
4941              gnus-tmp-downloaded
4942              (cond ((memq number gnus-newsgroup-undownloaded)
4943                     gnus-undownloaded-mark)
4944                    (gnus-newsgroup-agentized
4945                     gnus-downloaded-mark)
4946                    (t
4947                     gnus-no-mark))
4948              gnus-tmp-from (mail-header-from gnus-tmp-header)
4949              gnus-tmp-name
4950              (cond
4951               ((string-match "<[^>]+> *$" gnus-tmp-from)
4952                (setq beg-match (match-beginning 0))
4953                (or (and (string-match "^\".+\"" gnus-tmp-from)
4954                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4955                    (substring gnus-tmp-from 0 beg-match)))
4956               ((string-match "(.+)" gnus-tmp-from)
4957                (substring gnus-tmp-from
4958                           (1+ (match-beginning 0)) (1- (match-end 0))))
4959               (t gnus-tmp-from))
4960
4961              ;; Do the %B string
4962              gnus-tmp-thread-tree-header-string
4963              (cond
4964               ((not gnus-show-threads) "")
4965               ((zerop gnus-tmp-level)
4966                (cond ((cdar thread)
4967                       (or gnus-sum-thread-tree-root subject))
4968                      (gnus-tmp-new-adopts
4969                       (or gnus-sum-thread-tree-false-root subject))
4970                      (t
4971                       (or gnus-sum-thread-tree-single-indent subject))))
4972               (t
4973                (concat (apply 'concat
4974                               (mapcar (lambda (item)
4975                                         (if (= item 1)
4976                                             gnus-sum-thread-tree-vertical
4977                                           gnus-sum-thread-tree-indent))
4978                                       (cdr (reverse tree-stack))))
4979                        (if (nth 1 thread)
4980                            gnus-sum-thread-tree-leaf-with-other
4981                          gnus-sum-thread-tree-single-leaf)))))
4982             (when (string= gnus-tmp-name "")
4983               (setq gnus-tmp-name gnus-tmp-from))
4984             (unless (numberp gnus-tmp-lines)
4985               (setq gnus-tmp-lines -1))
4986             (if (= gnus-tmp-lines -1)
4987                 (setq gnus-tmp-lines "?")
4988               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4989               (gnus-put-text-property
4990              (point)
4991              (progn (eval gnus-summary-line-format-spec) (point))
4992                'gnus-number number)
4993             (when gnus-visual-p
4994               (forward-line -1)
4995               (gnus-run-hooks 'gnus-summary-update-hook)
4996               (forward-line 1))
4997
4998             (setq gnus-tmp-prev-subject simp-subject)))
4999
5000         (when (nth 1 thread)
5001           (push (list (max 0 gnus-tmp-level)
5002                       (copy-sequence tree-stack)
5003                       (nthcdr 1 thread))
5004                 stack))
5005         (push (if (nth 1 thread) 1 0) tree-stack)
5006         (incf gnus-tmp-level)
5007         (setq threads (if thread-end nil (cdar thread)))
5008         (if gnus-summary-display-while-building
5009             (if building-count
5010                 (progn
5011                   ;; use a set frequency
5012                   (setq building-line-count (1- building-line-count))
5013                   (when (= building-line-count 0)
5014                     (sit-for 0)
5015                     (setq building-line-count
5016                           gnus-summary-display-while-building)))
5017               ;; always
5018               (sit-for 0)))
5019         (unless threads
5020           (setq gnus-tmp-level 0)))))
5021   (gnus-message 7 "Generating summary...done"))
5022
5023 (defun gnus-summary-prepare-unthreaded (headers)
5024   "Generate an unthreaded summary buffer based on HEADERS."
5025   (let (header number mark)
5026
5027     (beginning-of-line)
5028
5029     (while headers
5030       ;; We may have to root out some bad articles...
5031       (when (memq (setq number (mail-header-number
5032                                 (setq header (pop headers))))
5033                   gnus-newsgroup-limit)
5034         ;; Mark article as read when it has a low score.
5035         (when (and gnus-summary-mark-below
5036                    (< (or (cdr (assq number gnus-newsgroup-scored))
5037                           gnus-summary-default-score 0)
5038                       gnus-summary-mark-below)
5039                    (not (gnus-summary-article-ancient-p number)))
5040           (setq gnus-newsgroup-unreads
5041                 (delq number gnus-newsgroup-unreads))
5042           (if gnus-newsgroup-auto-expire
5043               (push number gnus-newsgroup-expirable)
5044             (push (cons number gnus-low-score-mark)
5045                   gnus-newsgroup-reads)))
5046
5047         (setq mark (gnus-article-mark number))
5048         (push (gnus-data-make number mark (1+ (point)) header 0)
5049               gnus-newsgroup-data)
5050         (gnus-summary-insert-line
5051          header 0 number
5052          (memq number gnus-newsgroup-undownloaded)
5053          mark (memq number gnus-newsgroup-replied)
5054          (memq number gnus-newsgroup-expirable)
5055          (mail-header-subject header) nil
5056          (cdr (assq number gnus-newsgroup-scored))
5057          (memq number gnus-newsgroup-processable))))))
5058
5059 (defun gnus-summary-remove-list-identifiers ()
5060   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5061   (let ((regexp (if (consp gnus-list-identifiers)
5062                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5063                   gnus-list-identifiers))
5064         changed subject)
5065     (when regexp
5066       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5067       (dolist (header gnus-newsgroup-headers)
5068         (setq subject (mail-header-subject header)
5069               changed nil)
5070         (while (string-match regexp subject)
5071           (setq subject
5072                 (concat (substring subject 0 (match-beginning 1))
5073                         (substring subject (match-end 0)))
5074                 changed t))
5075         (when changed
5076           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5077             (setq subject
5078                   (concat (substring subject 0 (match-beginning 1))
5079                           (substring subject (match-end 1)))))
5080           (mail-header-set-subject header subject))))))
5081
5082 (defun gnus-fetch-headers (articles)
5083   "Fetch headers of ARTICLES."
5084   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5085     (gnus-message 5 "Fetching headers for %s..." name)
5086     (prog1
5087         (if (eq 'nov
5088                 (setq gnus-headers-retrieved-by
5089                       (gnus-retrieve-headers
5090                        articles gnus-newsgroup-name
5091                        ;; We might want to fetch old headers, but
5092                        ;; not if there is only 1 article.
5093                        (and (or (and
5094                                  (not (eq gnus-fetch-old-headers 'some))
5095                                  (not (numberp gnus-fetch-old-headers)))
5096                                 (> (length articles) 1))
5097                             gnus-fetch-old-headers))))
5098             (gnus-get-newsgroup-headers-xover
5099              articles nil nil gnus-newsgroup-name t)
5100           (gnus-get-newsgroup-headers))
5101       (gnus-message 5 "Fetching headers for %s...done" name))))
5102
5103 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5104   "Select newsgroup GROUP.
5105 If READ-ALL is non-nil, all articles in the group are selected.
5106 If SELECT-ARTICLES, only select those articles from GROUP."
5107   (let* ((entry (gnus-group-entry group))
5108          ;;!!! Dirty hack; should be removed.
5109          (gnus-summary-ignore-duplicates
5110           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5111               t
5112             gnus-summary-ignore-duplicates))
5113          (info (nth 2 entry))
5114          articles fetched-articles cached)
5115
5116     (unless (gnus-check-server
5117              (set (make-local-variable 'gnus-current-select-method)
5118                   (gnus-find-method-for-group group)))
5119       (error "Couldn't open server"))
5120
5121     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5122         (gnus-activate-group group)     ; Or we can activate it...
5123         (progn                          ; Or we bug out.
5124           (when (equal major-mode 'gnus-summary-mode)
5125             (gnus-kill-buffer (current-buffer)))
5126           (error "Couldn't activate group %s: %s"
5127                  group (gnus-status-message group))))
5128
5129     (unless (gnus-request-group group t)
5130       (when (equal major-mode 'gnus-summary-mode)
5131         (gnus-kill-buffer (current-buffer)))
5132       (error "Couldn't request group %s: %s"
5133              group (gnus-status-message group)))
5134
5135     (when gnus-agent
5136       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5137       
5138       (setq gnus-summary-use-undownloaded-faces
5139             (gnus-agent-find-parameter
5140              group
5141              'agent-enable-undownloaded-faces)))
5142
5143     (setq gnus-newsgroup-name group
5144           gnus-newsgroup-unselected nil
5145           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5146
5147     (let ((display (gnus-group-find-parameter group 'display)))
5148       (setq gnus-newsgroup-display
5149             (cond
5150              ((not (zerop (or (car-safe read-all) 0)))
5151               ;; The user entered the group with C-u SPC/RET, let's show
5152               ;; all articles.
5153               'gnus-not-ignore)
5154              ((eq display 'all)
5155               'gnus-not-ignore)
5156              ((arrayp display)
5157               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5158              ((numberp display)
5159               ;; The following is probably the "correct" solution, but
5160               ;; it makes Gnus fetch all headers and then limit the
5161               ;; articles (which is slow), so instead we hack the
5162               ;; select-articles parameter instead. -- Simon Josefsson
5163               ;; <jas@kth.se>
5164               ;;
5165               ;; (gnus-byte-compile
5166               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5167               ;;                         display)))))
5168               (setq select-articles
5169                     (gnus-uncompress-range
5170                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5171                              (if (> tmp 0)
5172                                  tmp
5173                                1))
5174                            (cdr (gnus-active group)))))
5175               nil)
5176              (t
5177               nil))))
5178
5179     (gnus-summary-setup-default-charset)
5180
5181     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5182     (when (gnus-virtual-group-p group)
5183       (setq cached gnus-newsgroup-cached))
5184
5185     (setq gnus-newsgroup-unreads
5186           (gnus-sorted-ndifference
5187            (gnus-sorted-ndifference gnus-newsgroup-unreads
5188                                     gnus-newsgroup-marked)
5189            gnus-newsgroup-dormant))
5190
5191     (setq gnus-newsgroup-processable nil)
5192
5193     (gnus-update-read-articles group gnus-newsgroup-unreads)
5194
5195     ;; Adjust and set lists of article marks.
5196     (when info
5197       (gnus-adjust-marked-articles info))
5198     (if (setq articles select-articles)
5199         (setq gnus-newsgroup-unselected
5200               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5201       (setq articles (gnus-articles-to-read group read-all)))
5202
5203     (cond
5204      ((null articles)
5205       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5206       'quit)
5207      ((eq articles 0) nil)
5208      (t
5209       ;; Init the dependencies hash table.
5210       (setq gnus-newsgroup-dependencies
5211             (gnus-make-hashtable (length articles)))
5212       (gnus-set-global-variables)
5213       ;; Retrieve the headers and read them in.
5214
5215       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5216
5217       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5218       (when cached
5219         (setq gnus-newsgroup-cached cached))
5220
5221       ;; Suppress duplicates?
5222       (when gnus-suppress-duplicates
5223         (gnus-dup-suppress-articles))
5224
5225       ;; Set the initial limit.
5226       (setq gnus-newsgroup-limit (copy-sequence articles))
5227       ;; Remove canceled articles from the list of unread articles.
5228       (setq fetched-articles
5229             (mapcar (lambda (headers) (mail-header-number headers))
5230                     gnus-newsgroup-headers))
5231       (setq gnus-newsgroup-articles fetched-articles)
5232       (setq gnus-newsgroup-unreads
5233             (gnus-sorted-nintersection
5234              gnus-newsgroup-unreads fetched-articles))
5235       (gnus-compute-unseen-list)
5236
5237       ;; Removed marked articles that do not exist.
5238       (gnus-update-missing-marks
5239        (gnus-sorted-difference articles fetched-articles))
5240       ;; We might want to build some more threads first.
5241       (when (and gnus-fetch-old-headers
5242                  (eq gnus-headers-retrieved-by 'nov))
5243         (if (eq gnus-fetch-old-headers 'invisible)
5244             (gnus-build-all-threads)
5245           (gnus-build-old-threads)))
5246       ;; Let the Gnus agent mark articles as read.
5247       (when gnus-agent
5248         (gnus-agent-get-undownloaded-list))
5249       ;; Remove list identifiers from subject
5250       (when gnus-list-identifiers
5251         (gnus-summary-remove-list-identifiers))
5252       ;; Check whether auto-expire is to be done in this group.
5253       (setq gnus-newsgroup-auto-expire
5254             (gnus-group-auto-expirable-p group))
5255       ;; Set up the article buffer now, if necessary.
5256       (unless gnus-single-article-buffer
5257         (gnus-article-setup-buffer))
5258       ;; First and last article in this newsgroup.
5259       (when gnus-newsgroup-headers
5260         (setq gnus-newsgroup-begin
5261               (mail-header-number (car gnus-newsgroup-headers))
5262               gnus-newsgroup-end
5263               (mail-header-number
5264                (gnus-last-element gnus-newsgroup-headers))))
5265       ;; GROUP is successfully selected.
5266       (or gnus-newsgroup-headers t)))))
5267
5268 (defun gnus-compute-unseen-list ()
5269   ;; The `seen' marks are treated specially.
5270   (if (not gnus-newsgroup-seen)
5271       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5272     (setq gnus-newsgroup-unseen
5273           (gnus-inverse-list-range-intersection
5274            gnus-newsgroup-articles gnus-newsgroup-seen))))
5275
5276 (defun gnus-summary-display-make-predicate (display)
5277   (require 'gnus-agent)
5278   (when (= (length display) 1)
5279     (setq display (car display)))
5280   (unless gnus-summary-display-cache
5281     (dolist (elem (append '((unread . unread)
5282                             (read . read)
5283                             (unseen . unseen))
5284                           gnus-article-mark-lists))
5285       (push (cons (cdr elem)
5286                   (gnus-byte-compile
5287                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5288             gnus-summary-display-cache)))
5289   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5290         (gnus-category-predicate-cache gnus-summary-display-cache))
5291     (gnus-get-predicate display)))
5292
5293 ;; Uses the dynamically bound `number' variable.
5294 (eval-when-compile
5295   (defvar number))
5296 (defun gnus-article-marked-p (type &optional article)
5297   (let ((article (or article number)))
5298     (cond
5299      ((eq type 'tick)
5300       (memq article gnus-newsgroup-marked))
5301      ((eq type 'spam)
5302       (memq article gnus-newsgroup-spam-marked))
5303      ((eq type 'unsend)
5304       (memq article gnus-newsgroup-unsendable))
5305      ((eq type 'undownload)
5306       (memq article gnus-newsgroup-undownloaded))
5307      ((eq type 'download)
5308       (memq article gnus-newsgroup-downloadable))
5309      ((eq type 'unread)
5310       (memq article gnus-newsgroup-unreads))
5311      ((eq type 'read)
5312       (memq article gnus-newsgroup-reads))
5313      ((eq type 'dormant)
5314       (memq article gnus-newsgroup-dormant) )
5315      ((eq type 'expire)
5316       (memq article gnus-newsgroup-expirable))
5317      ((eq type 'reply)
5318       (memq article gnus-newsgroup-replied))
5319      ((eq type 'killed)
5320       (memq article gnus-newsgroup-killed))
5321      ((eq type 'bookmark)
5322       (assq article gnus-newsgroup-bookmarks))
5323      ((eq type 'score)
5324       (assq article gnus-newsgroup-scored))
5325      ((eq type 'save)
5326       (memq article gnus-newsgroup-saved))
5327      ((eq type 'cache)
5328       (memq article gnus-newsgroup-cached))
5329      ((eq type 'forward)
5330       (memq article gnus-newsgroup-forwarded))
5331      ((eq type 'seen)
5332       (not (memq article gnus-newsgroup-unseen)))
5333      ((eq type 'recent)
5334       (memq article gnus-newsgroup-recent))
5335      (t t))))
5336
5337 (defun gnus-articles-to-read (group &optional read-all)
5338   "Find out what articles the user wants to read."
5339   (let* ((articles
5340           ;; Select all articles if `read-all' is non-nil, or if there
5341           ;; are no unread articles.
5342           (if (or read-all
5343                   (and (zerop (length gnus-newsgroup-marked))
5344                        (zerop (length gnus-newsgroup-unreads)))
5345                   ;; Fetch all if the predicate is non-nil.
5346                   gnus-newsgroup-display)
5347               ;; We want to select the headers for all the articles in
5348               ;; the group, so we select either all the active
5349               ;; articles in the group, or (if that's nil), the
5350               ;; articles in the cache.
5351               (or
5352                (gnus-uncompress-range (gnus-active group))
5353                (gnus-cache-articles-in-group group))
5354             ;; Select only the "normal" subset of articles.
5355             (gnus-sorted-nunion
5356              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5357              gnus-newsgroup-unreads)))
5358          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5359          (scored (length scored-list))
5360          (number (length articles))
5361          (marked (+ (length gnus-newsgroup-marked)
5362                     (length gnus-newsgroup-dormant)))
5363          (select
5364           (cond
5365            ((numberp read-all)
5366             read-all)
5367            ((numberp gnus-newsgroup-display)
5368             gnus-newsgroup-display)
5369            (t
5370             (condition-case ()
5371                 (cond
5372                  ((and (or (<= scored marked) (= scored number))
5373                        (numberp gnus-large-newsgroup)
5374                        (> number gnus-large-newsgroup))
5375                   (let* ((cursor-in-echo-area nil)
5376                          (initial (gnus-parameter-large-newsgroup-initial
5377                                    gnus-newsgroup-name))
5378                          (input
5379                           (read-string
5380                            (format
5381                             "How many articles from %s (%s %d): "
5382                             (gnus-limit-string
5383                              (gnus-group-decoded-name gnus-newsgroup-name)
5384                              35)
5385                             (if initial "max" "default")
5386                             number)
5387                            (if initial
5388                                (cons (number-to-string initial)
5389                                      0)))))
5390                     (if (string-match "^[ \t]*$" input) number input)))
5391                  ((and (> scored marked) (< scored number)
5392                        (> (- scored number) 20))
5393                   (let ((input
5394                          (read-string
5395                           (format "%s %s (%d scored, %d total): "
5396                                   "How many articles from"
5397                                   (gnus-group-decoded-name group)
5398                                   scored number))))
5399                     (if (string-match "^[ \t]*$" input)
5400                         number input)))
5401                  (t number))
5402               (quit
5403                (message "Quit getting the articles to read")
5404                nil))))))
5405     (setq select (if (stringp select) (string-to-number select) select))
5406     (if (or (null select) (zerop select))
5407         select
5408       (if (and (not (zerop scored)) (<= (abs select) scored))
5409           (progn
5410             (setq articles (sort scored-list '<))
5411             (setq number (length articles)))
5412         (setq articles (copy-sequence articles)))
5413
5414       (when (< (abs select) number)
5415         (if (< select 0)
5416             ;; Select the N oldest articles.
5417             (setcdr (nthcdr (1- (abs select)) articles) nil)
5418           ;; Select the N most recent articles.
5419           (setq articles (nthcdr (- number select) articles))))
5420       (setq gnus-newsgroup-unselected
5421             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5422       (when gnus-alter-articles-to-read-function
5423         (setq articles
5424               (sort
5425                (funcall gnus-alter-articles-to-read-function
5426                         gnus-newsgroup-name articles)
5427                '<)))
5428       articles)))
5429
5430 (defun gnus-killed-articles (killed articles)
5431   (let (out)
5432     (while articles
5433       (when (inline (gnus-member-of-range (car articles) killed))
5434         (push (car articles) out))
5435       (setq articles (cdr articles)))
5436     out))
5437
5438 (defun gnus-uncompress-marks (marks)
5439   "Uncompress the mark ranges in MARKS."
5440   (let ((uncompressed '(score bookmark))
5441         out)
5442     (while marks
5443       (if (memq (caar marks) uncompressed)
5444           (push (car marks) out)
5445         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5446       (setq marks (cdr marks)))
5447     out))
5448
5449 (defun gnus-article-mark-to-type (mark)
5450   "Return the type of MARK."
5451   (or (cadr (assq mark gnus-article-special-mark-lists))
5452       'list))
5453
5454 (defun gnus-article-unpropagatable-p (mark)
5455   "Return whether MARK should be propagated to back end."
5456   (memq mark gnus-article-unpropagated-mark-lists))
5457
5458 (defun gnus-adjust-marked-articles (info)
5459   "Set all article lists and remove all marks that are no longer valid."
5460   (let* ((marked-lists (gnus-info-marks info))
5461          (active (gnus-active (gnus-info-group info)))
5462          (min (car active))
5463          (max (cdr active))
5464          (types gnus-article-mark-lists)
5465          marks var articles article mark mark-type
5466          bgn end)
5467
5468     (dolist (marks marked-lists)
5469       (setq mark (car marks)
5470             mark-type (gnus-article-mark-to-type mark)
5471             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5472
5473       ;; We set the variable according to the type of the marks list,
5474       ;; and then adjust the marks to a subset of the active articles.
5475       (cond
5476        ;; Adjust "simple" lists - compressed yet unsorted
5477        ((eq mark-type 'list)
5478         ;; Simultaneously uncompress and clip to active range
5479         ;; See gnus-uncompress-range for a description of possible marks
5480         (let (l lh)
5481           (if (not (cadr marks))
5482               (set var nil)
5483             (setq articles (if (numberp (cddr marks))
5484                                (list (cdr marks))
5485                              (cdr marks))
5486                   lh (cons nil nil)
5487                   l lh)
5488
5489             (while (setq article (pop articles))
5490               (cond ((consp article)
5491                      (setq bgn (max (car article) min)
5492                            end (min (cdr article) max))
5493                      (while (<= bgn end)
5494                        (setq l (setcdr l (cons bgn nil))
5495                              bgn (1+ bgn))))
5496                     ((and (<= min article)
5497                           (>= max article))
5498                      (setq l (setcdr l (cons article nil))))))
5499             (set var (cdr lh)))))
5500        ;; Adjust assocs.
5501        ((eq mark-type 'tuple)
5502         (set var (setq articles (cdr marks)))
5503         (when (not (listp (cdr (symbol-value var))))
5504           (set var (list (symbol-value var))))
5505         (when (not (listp (cdr articles)))
5506           (setq articles (list articles)))
5507         (while articles
5508           (when (or (not (consp (setq article (pop articles))))
5509                     (< (car article) min)
5510                     (> (car article) max))
5511             (set var (delq article (symbol-value var))))))
5512        ;; Adjust ranges (sloppily).
5513        ((eq mark-type 'range)
5514         (cond
5515          ((eq mark 'seen)
5516           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5517           ;; It should be (seen (NUM1 . NUM2)).
5518           (when (numberp (cddr marks))
5519             (setcdr marks (list (cdr marks))))
5520           (setq articles (cdr marks))
5521           (while (and articles
5522                       (or (and (consp (car articles))
5523                                (> min (cdar articles)))
5524                           (and (numberp (car articles))
5525                                (> min (car articles)))))
5526             (pop articles))
5527           (set var articles))))))))
5528
5529 (defun gnus-update-missing-marks (missing)
5530   "Go through the list of MISSING articles and remove them from the mark lists."
5531   (when missing
5532     (let (var m)
5533       ;; Go through all types.
5534       (dolist (elem gnus-article-mark-lists)
5535         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5536           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5537           (when (symbol-value var)
5538             ;; This list has articles.  So we delete all missing
5539             ;; articles from it.
5540             (setq m missing)
5541             (while m
5542               (set var (delq (pop m) (symbol-value var))))))))))
5543
5544 (defun gnus-update-marks ()
5545   "Enter the various lists of marked articles into the newsgroup info list."
5546   (let ((types gnus-article-mark-lists)
5547         (info (gnus-get-info gnus-newsgroup-name))
5548         type list newmarked symbol delta-marks)
5549     (when info
5550       ;; Add all marks lists to the list of marks lists.
5551       (while (setq type (pop types))
5552         (setq list (symbol-value
5553                     (setq symbol
5554                           (intern (format "gnus-newsgroup-%s" (car type))))))
5555
5556         (when list
5557           ;; Get rid of the entries of the articles that have the
5558           ;; default score.
5559           (when (and (eq (cdr type) 'score)
5560                      gnus-save-score
5561                      list)
5562             (let* ((arts list)
5563                    (prev (cons nil list))
5564                    (all prev))
5565               (while arts
5566                 (if (or (not (consp (car arts)))
5567                         (= (cdar arts) gnus-summary-default-score))
5568                     (setcdr prev (cdr arts))
5569                   (setq prev arts))
5570                 (setq arts (cdr arts)))
5571               (setq list (cdr all)))))
5572
5573         (when (eq (cdr type) 'seen)
5574           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5575
5576         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5577           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5578
5579         (when (and (gnus-check-backend-function
5580                     'request-set-mark gnus-newsgroup-name)
5581                    (not (gnus-article-unpropagatable-p (cdr type))))
5582           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5583                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5584                  (add (gnus-remove-from-range
5585                        (gnus-copy-sequence list) old)))
5586             (when add
5587               (push (list add 'add (list (cdr type))) delta-marks))
5588             (when del
5589               (push (list del 'del (list (cdr type))) delta-marks))))
5590
5591         (when list
5592           (push (cons (cdr type) list) newmarked)))
5593
5594       (when delta-marks
5595         (unless (gnus-check-group gnus-newsgroup-name)
5596           (error "Can't open server for %s" gnus-newsgroup-name))
5597         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5598
5599       ;; Enter these new marks into the info of the group.
5600       (if (nthcdr 3 info)
5601           (setcar (nthcdr 3 info) newmarked)
5602         ;; Add the marks lists to the end of the info.
5603         (when newmarked
5604           (setcdr (nthcdr 2 info) (list newmarked))))
5605
5606       ;; Cut off the end of the info if there's nothing else there.
5607       (let ((i 5))
5608         (while (and (> i 2)
5609                     (not (nth i info)))
5610           (when (nthcdr (decf i) info)
5611             (setcdr (nthcdr i info) nil)))))))
5612
5613 (defun gnus-set-mode-line (where)
5614   "Set the mode line of the article or summary buffers.
5615 If WHERE is `summary', the summary mode line format will be used."
5616   ;; Is this mode line one we keep updated?
5617   (when (and (memq where gnus-updated-mode-lines)
5618              (symbol-value
5619               (intern (format "gnus-%s-mode-line-format-spec" where))))
5620     (let (mode-string)
5621       (save-excursion
5622         ;; We evaluate this in the summary buffer since these
5623         ;; variables are buffer-local to that buffer.
5624         (set-buffer gnus-summary-buffer)
5625        ;; We bind all these variables that are used in the `eval' form
5626         ;; below.
5627         (let* ((mformat (symbol-value
5628                          (intern
5629                           (format "gnus-%s-mode-line-format-spec" where))))
5630                (gnus-tmp-group-name (gnus-group-decoded-name
5631                                      gnus-newsgroup-name))
5632                (gnus-tmp-article-number (or gnus-current-article 0))
5633                (gnus-tmp-unread gnus-newsgroup-unreads)
5634                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5635                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5636                (gnus-tmp-unread-and-unselected
5637                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5638                             (zerop gnus-tmp-unselected))
5639                        "")
5640                       ((zerop gnus-tmp-unselected)
5641                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5642                       (t (format "{%d(+%d) more}"
5643                                  gnus-tmp-unread-and-unticked
5644                                  gnus-tmp-unselected))))
5645                (gnus-tmp-subject
5646                 (if (and gnus-current-headers
5647                          (vectorp gnus-current-headers))
5648                     (gnus-mode-string-quote
5649                      (mail-header-subject gnus-current-headers))
5650                   ""))
5651                bufname-length max-len
5652                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5653           (setq mode-string (eval mformat))
5654           (setq bufname-length (if (string-match "%b" mode-string)
5655                                    (- (length
5656                                        (buffer-name
5657                                         (if (eq where 'summary)
5658                                             nil
5659                                           (get-buffer gnus-article-buffer))))
5660                                       2)
5661                                  0))
5662           (setq max-len (max 4 (if gnus-mode-non-string-length
5663                                    (- (window-width)
5664                                       gnus-mode-non-string-length
5665                                       bufname-length)
5666                                  (length mode-string))))
5667           ;; We might have to chop a bit of the string off...
5668           (when (> (length mode-string) max-len)
5669             (setq mode-string
5670                   (concat (truncate-string-to-width mode-string (- max-len 3))
5671                           "...")))
5672           ;; Pad the mode string a bit.
5673           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5674       ;; Update the mode line.
5675       (setq mode-line-buffer-identification
5676             (gnus-mode-line-buffer-identification (list mode-string)))
5677       (set-buffer-modified-p t))))
5678
5679 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5680   "Go through the HEADERS list and add all Xrefs to a hash table.
5681 The resulting hash table is returned, or nil if no Xrefs were found."
5682   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5683          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5684          (xref-hashtb (gnus-make-hashtable))
5685          start group entry number xrefs header)
5686     (while headers
5687       (setq header (pop headers))
5688       (when (and (setq xrefs (mail-header-xref header))
5689                  (not (memq (setq number (mail-header-number header))
5690                             unreads)))
5691         (setq start 0)
5692         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5693           (setq start (match-end 0))
5694           (setq group (if prefix
5695                           (concat prefix (substring xrefs (match-beginning 1)
5696                                                     (match-end 1)))
5697                         (substring xrefs (match-beginning 1) (match-end 1))))
5698           (setq number
5699                 (string-to-int (substring xrefs (match-beginning 2)
5700                                           (match-end 2))))
5701           (if (setq entry (gnus-gethash group xref-hashtb))
5702               (setcdr entry (cons number (cdr entry)))
5703             (gnus-sethash group (cons number nil) xref-hashtb)))))
5704     (and start xref-hashtb)))
5705
5706 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5707   "Look through all the headers and mark the Xrefs as read."
5708   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5709         name info xref-hashtb idlist method nth4)
5710     (save-excursion
5711       (set-buffer gnus-group-buffer)
5712       (when (setq xref-hashtb
5713                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5714         (mapatoms
5715          (lambda (group)
5716            (unless (string= from-newsgroup (setq name (symbol-name group)))
5717              (setq idlist (symbol-value group))
5718              ;; Dead groups are not updated.
5719              (and (prog1
5720                       (setq info (gnus-get-info name))
5721                     (when (stringp (setq nth4 (gnus-info-method info)))
5722                       (setq nth4 (gnus-server-to-method nth4))))
5723                   ;; Only do the xrefs if the group has the same
5724                   ;; select method as the group we have just read.
5725                   (or (gnus-methods-equal-p
5726                        nth4 (gnus-find-method-for-group from-newsgroup))
5727                       virtual
5728                       (equal nth4 (setq method (gnus-find-method-for-group
5729                                                 from-newsgroup)))
5730                       (and (equal (car nth4) (car method))
5731                            (equal (nth 1 nth4) (nth 1 method))))
5732                   gnus-use-cross-reference
5733                   (or (not (eq gnus-use-cross-reference t))
5734                       virtual
5735                       ;; Only do cross-references on subscribed
5736                       ;; groups, if that is what is wanted.
5737                       (<= (gnus-info-level info) gnus-level-subscribed))
5738                   (gnus-group-make-articles-read name idlist))))
5739          xref-hashtb)))))
5740
5741 (defun gnus-compute-read-articles (group articles)
5742   (let* ((entry (gnus-group-entry group))
5743          (info (nth 2 entry))
5744          (active (gnus-active group))
5745          ninfo)
5746     (when entry
5747       ;; First peel off all invalid article numbers.
5748       (when active
5749         (let ((ids articles)
5750               id first)
5751           (while (setq id (pop ids))
5752             (when (and first (> id (cdr active)))
5753               ;; We'll end up in this situation in one particular
5754               ;; obscure situation.  If you re-scan a group and get
5755               ;; a new article that is cross-posted to a different
5756               ;; group that has not been re-scanned, you might get
5757               ;; crossposted article that has a higher number than
5758               ;; Gnus believes possible.  So we re-activate this
5759               ;; group as well.  This might mean doing the
5760               ;; crossposting thingy will *increase* the number
5761               ;; of articles in some groups.  Tsk, tsk.
5762               (setq active (or (gnus-activate-group group) active)))
5763             (when (or (> id (cdr active))
5764                       (< id (car active)))
5765               (setq articles (delq id articles))))))
5766       ;; If the read list is nil, we init it.
5767       (if (and active
5768                (null (gnus-info-read info))
5769                (> (car active) 1))
5770           (setq ninfo (cons 1 (1- (car active))))
5771         (setq ninfo (gnus-info-read info)))
5772       ;; Then we add the read articles to the range.
5773       (gnus-add-to-range
5774        ninfo (setq articles (sort articles '<))))))
5775
5776 (defun gnus-group-make-articles-read (group articles)
5777   "Update the info of GROUP to say that ARTICLES are read."
5778   (let* ((num 0)
5779          (entry (gnus-group-entry group))
5780          (info (nth 2 entry))
5781          (active (gnus-active group))
5782          range)
5783     (when entry
5784       (setq range (gnus-compute-read-articles group articles))
5785       (with-current-buffer gnus-group-buffer
5786         (gnus-undo-register
5787           `(progn
5788              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5789              (gnus-info-set-read ',info ',(gnus-info-read info))
5790              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5791              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5792              (gnus-group-update-group ,group t))))
5793       ;; Add the read articles to the range.
5794       (gnus-info-set-read info range)
5795       (gnus-request-set-mark group (list (list range 'add '(read))))
5796       ;; Then we have to re-compute how many unread
5797       ;; articles there are in this group.
5798       (when active
5799         (cond
5800          ((not range)
5801           (setq num (- (1+ (cdr active)) (car active))))
5802          ((not (listp (cdr range)))
5803           (setq num (- (cdr active) (- (1+ (cdr range))
5804                                        (car range)))))
5805          (t
5806           (while range
5807             (if (numberp (car range))
5808                 (setq num (1+ num))
5809               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5810             (setq range (cdr range)))
5811           (setq num (- (cdr active) num))))
5812         ;; Update the number of unread articles.
5813         (setcar entry num)
5814         ;; Update the group buffer.
5815         (unless (gnus-ephemeral-group-p group)
5816           (gnus-group-update-group group t))))))
5817
5818 (defvar gnus-newsgroup-none-id 0)
5819
5820 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5821   (let ((cur nntp-server-buffer)
5822         (dependencies
5823          (or dependencies
5824              (with-current-buffer gnus-summary-buffer
5825                gnus-newsgroup-dependencies)))
5826         headers id end ref number
5827         (mail-parse-charset gnus-newsgroup-charset)
5828         (mail-parse-ignored-charsets
5829          (save-excursion (condition-case nil
5830                              (set-buffer gnus-summary-buffer)
5831                            (error))
5832                          gnus-newsgroup-ignored-charsets)))
5833     (save-excursion
5834       (set-buffer nntp-server-buffer)
5835       ;; Translate all TAB characters into SPACE characters.
5836       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5837       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5838       (ietf-drums-unfold-fws)
5839       (gnus-run-hooks 'gnus-parse-headers-hook)
5840       (let ((case-fold-search t)
5841             in-reply-to header p lines chars)
5842         (goto-char (point-min))
5843         ;; Search to the beginning of the next header.  Error messages
5844         ;; do not begin with 2 or 3.
5845         (while (re-search-forward "^[23][0-9]+ " nil t)
5846           (setq id nil
5847                 ref nil)
5848           ;; This implementation of this function, with nine
5849           ;; search-forwards instead of the one re-search-forward and
5850           ;; a case (which basically was the old function) is actually
5851           ;; about twice as fast, even though it looks messier.  You
5852           ;; can't have everything, I guess.  Speed and elegance
5853           ;; doesn't always go hand in hand.
5854           (setq
5855            header
5856            (vector
5857             ;; Number.
5858             (prog1
5859                 (setq number (read cur))
5860               (end-of-line)
5861               (setq p (point))
5862               (narrow-to-region (point)
5863                                 (or (and (search-forward "\n.\n" nil t)
5864                                          (- (point) 2))
5865                                     (point))))
5866             ;; Subject.
5867             (progn
5868               (goto-char p)
5869               (if (search-forward "\nsubject:" nil t)
5870                   (funcall gnus-decode-encoded-word-function
5871                            (nnheader-header-value))
5872                 "(none)"))
5873             ;; From.
5874             (progn
5875               (goto-char p)
5876               (if (search-forward "\nfrom:" nil t)
5877                   (funcall gnus-decode-encoded-word-function
5878                            (nnheader-header-value))
5879                 "(nobody)"))
5880             ;; Date.
5881             (progn
5882               (goto-char p)
5883               (if (search-forward "\ndate:" nil t)
5884                   (nnheader-header-value) ""))
5885             ;; Message-ID.
5886             (progn
5887               (goto-char p)
5888               (setq id (if (re-search-forward
5889                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5890                            ;; We do it this way to make sure the Message-ID
5891                            ;; is (somewhat) syntactically valid.
5892                            (buffer-substring (match-beginning 1)
5893                                              (match-end 1))
5894                          ;; If there was no message-id, we just fake one
5895                          ;; to make subsequent routines simpler.
5896                          (nnheader-generate-fake-message-id number))))
5897             ;; References.
5898             (progn
5899               (goto-char p)
5900               (if (search-forward "\nreferences:" nil t)
5901                   (progn
5902                     (setq end (point))
5903                     (prog1
5904                         (nnheader-header-value)
5905                       (setq ref
5906                             (buffer-substring
5907                              (progn
5908                                (end-of-line)
5909                                (search-backward ">" end t)
5910                                (1+ (point)))
5911                              (progn
5912                                (search-backward "<" end t)
5913                                (point))))))
5914                 ;; Get the references from the in-reply-to header if there
5915                 ;; were no references and the in-reply-to header looks
5916                 ;; promising.
5917                 (if (and (search-forward "\nin-reply-to:" nil t)
5918                          (setq in-reply-to (nnheader-header-value))
5919                          (string-match "<[^>]+>" in-reply-to))
5920                     (let (ref2)
5921                       (setq ref (substring in-reply-to (match-beginning 0)
5922                                            (match-end 0)))
5923                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5924                         (setq ref2 (substring in-reply-to (match-beginning 0)
5925                                               (match-end 0)))
5926                         (when (> (length ref2) (length ref))
5927                           (setq ref ref2)))
5928                       ref)
5929                   (setq ref nil))))
5930             ;; Chars.
5931             (progn
5932               (goto-char p)
5933               (if (search-forward "\nchars: " nil t)
5934                   (if (numberp (setq chars (ignore-errors (read cur))))
5935                       chars -1)
5936                 -1))
5937             ;; Lines.
5938             (progn
5939               (goto-char p)
5940               (if (search-forward "\nlines: " nil t)
5941                   (if (numberp (setq lines (ignore-errors (read cur))))
5942                       lines -1)
5943                 -1))
5944             ;; Xref.
5945             (progn
5946               (goto-char p)
5947               (and (search-forward "\nxref:" nil t)
5948                    (nnheader-header-value)))
5949             ;; Extra.
5950             (when gnus-extra-headers
5951               (let ((extra gnus-extra-headers)
5952                     out)
5953                 (while extra
5954                   (goto-char p)
5955                   (when (search-forward
5956                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5957                     (push (cons (car extra) (nnheader-header-value))
5958                           out))
5959                   (pop extra))
5960                 out))))
5961           (when (equal id ref)
5962             (setq ref nil))
5963
5964           (when gnus-alter-header-function
5965             (funcall gnus-alter-header-function header)
5966             (setq id (mail-header-id header)
5967                   ref (gnus-parent-id (mail-header-references header))))
5968
5969           (when (setq header
5970                       (gnus-dependencies-add-header
5971                        header dependencies force-new))
5972             (push header headers))
5973           (goto-char (point-max))
5974           (widen))
5975         (nreverse headers)))))
5976
5977 ;; Goes through the xover lines and returns a list of vectors
5978 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5979                                                   force-new dependencies
5980                                                   group also-fetch-heads)
5981   "Parse the news overview data in the server buffer.
5982 Return a list of headers that match SEQUENCE (see
5983 `nntp-retrieve-headers')."
5984   ;; Get the Xref when the users reads the articles since most/some
5985   ;; NNTP servers do not include Xrefs when using XOVER.
5986   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5987   (let ((mail-parse-charset gnus-newsgroup-charset)
5988         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5989         (cur nntp-server-buffer)
5990         (dependencies (or dependencies gnus-newsgroup-dependencies))
5991         (allp (cond
5992                ((eq gnus-read-all-available-headers t)
5993                 t)
5994                ((stringp gnus-read-all-available-headers)
5995                 (string-match gnus-read-all-available-headers group))
5996                (t
5997                 nil)))
5998         number headers header)
5999     (save-excursion
6000       (set-buffer nntp-server-buffer)
6001       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6002       ;; Allow the user to mangle the headers before parsing them.
6003       (gnus-run-hooks 'gnus-parse-headers-hook)
6004       (goto-char (point-min))
6005       (gnus-parse-without-error
6006         (while (and (or sequence allp)
6007                     (not (eobp)))
6008           (setq number (read cur))
6009           (when (not allp)
6010             (while (and sequence
6011                         (< (car sequence) number))
6012               (setq sequence (cdr sequence))))
6013           (when (and (or allp
6014                          (and sequence
6015                               (eq number (car sequence))))
6016                      (progn
6017                        (setq sequence (cdr sequence))
6018                        (setq header (inline
6019                                       (gnus-nov-parse-line
6020                                        number dependencies force-new)))))
6021             (push header headers))
6022           (forward-line 1)))
6023       ;; A common bug in inn is that if you have posted an article and
6024       ;; then retrieves the active file, it will answer correctly --
6025       ;; the new article is included.  However, a NOV entry for the
6026       ;; article may not have been generated yet, so this may fail.
6027       ;; We work around this problem by retrieving the last few
6028       ;; headers using HEAD.
6029       (if (or (not also-fetch-heads)
6030               (not sequence))
6031           ;; We (probably) got all the headers.
6032           (nreverse headers)
6033         (let ((gnus-nov-is-evil t))
6034           (nconc
6035            (nreverse headers)
6036            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6037              (gnus-get-newsgroup-headers))))))))
6038
6039 (defun gnus-article-get-xrefs ()
6040   "Fill in the Xref value in `gnus-current-headers', if necessary.
6041 This is meant to be called in `gnus-article-internal-prepare-hook'."
6042   (let ((headers (with-current-buffer gnus-summary-buffer
6043                    gnus-current-headers)))
6044     (or (not gnus-use-cross-reference)
6045         (not headers)
6046         (and (mail-header-xref headers)
6047              (not (string= (mail-header-xref headers) "")))
6048         (let ((case-fold-search t)
6049               xref)
6050           (save-restriction
6051             (nnheader-narrow-to-headers)
6052             (goto-char (point-min))
6053             (when (or (and (not (eobp))
6054                            (eq (downcase (char-after)) ?x)
6055                            (looking-at "Xref:"))
6056                       (search-forward "\nXref:" nil t))
6057               (goto-char (1+ (match-end 0)))
6058               (setq xref (buffer-substring (point) (point-at-eol)))
6059               (mail-header-set-xref headers xref)))))))
6060
6061 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6062   "Find article ID and insert the summary line for that article.
6063 OLD-HEADER can either be a header or a line number to insert
6064 the subject line on."
6065   (let* ((line (and (numberp old-header) old-header))
6066          (old-header (and (vectorp old-header) old-header))
6067          (header (cond ((and old-header use-old-header)
6068                         old-header)
6069                        ((and (numberp id)
6070                              (gnus-number-to-header id))
6071                         (gnus-number-to-header id))
6072                        (t
6073                         (gnus-read-header id))))
6074          (number (and (numberp id) id))
6075          d)
6076     (when header
6077       ;; Rebuild the thread that this article is part of and go to the
6078       ;; article we have fetched.
6079       (when (and (not gnus-show-threads)
6080                  old-header)
6081         (when (and number
6082                    (setq d (gnus-data-find (mail-header-number old-header))))
6083           (goto-char (gnus-data-pos d))
6084           (gnus-data-remove
6085            number
6086            (- (point-at-bol)
6087               (prog1
6088                   (1+ (point-at-eol))
6089                 (gnus-delete-line))))))
6090       ;; Remove list identifiers from subject.
6091       (when gnus-list-identifiers
6092         (let ((gnus-newsgroup-headers (list header)))
6093           (gnus-summary-remove-list-identifiers)))
6094       (when old-header
6095         (mail-header-set-number header (mail-header-number old-header)))
6096       (setq gnus-newsgroup-sparse
6097             (delq (setq number (mail-header-number header))
6098                   gnus-newsgroup-sparse))
6099       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6100       (push number gnus-newsgroup-limit)
6101       (gnus-rebuild-thread (mail-header-id header) line)
6102       (gnus-summary-goto-subject number nil t))
6103     (when (and (numberp number)
6104                (> number 0))
6105       ;; We have to update the boundaries even if we can't fetch the
6106       ;; article if ID is a number -- so that the next `P' or `N'
6107       ;; command will fetch the previous (or next) article even
6108       ;; if the one we tried to fetch this time has been canceled.
6109       (when (> number gnus-newsgroup-end)
6110         (setq gnus-newsgroup-end number))
6111       (when (< number gnus-newsgroup-begin)
6112         (setq gnus-newsgroup-begin number))
6113       (setq gnus-newsgroup-unselected
6114             (delq number gnus-newsgroup-unselected)))
6115     ;; Report back a success?
6116     (and header (mail-header-number header))))
6117
6118 ;;; Process/prefix in the summary buffer
6119
6120 (defun gnus-summary-work-articles (n)
6121   "Return a list of articles to be worked upon.
6122 The prefix argument, the list of process marked articles, and the
6123 current article will be taken into consideration."
6124   (save-excursion
6125     (set-buffer gnus-summary-buffer)
6126     (cond
6127      (n
6128       ;; A numerical prefix has been given.
6129       (setq n (prefix-numeric-value n))
6130       (let ((backward (< n 0))
6131             (n (abs (prefix-numeric-value n)))
6132             articles article)
6133         (save-excursion
6134           (while
6135               (and (> n 0)
6136                    (push (setq article (gnus-summary-article-number))
6137                          articles)
6138                    (if backward
6139                        (gnus-summary-find-prev nil article)
6140                      (gnus-summary-find-next nil article)))
6141             (decf n)))
6142         (nreverse articles)))
6143      ((and (gnus-region-active-p) (mark))
6144       (message "region active")
6145       ;; Work on the region between point and mark.
6146       (let ((max (max (point) (mark)))
6147             articles article)
6148         (save-excursion
6149           (goto-char (min (min (point) (mark))))
6150           (while
6151               (and
6152                (push (setq article (gnus-summary-article-number)) articles)
6153                (gnus-summary-find-next nil article)
6154                (< (point) max)))
6155           (nreverse articles))))
6156      (gnus-newsgroup-processable
6157       ;; There are process-marked articles present.
6158       ;; Save current state.
6159       (gnus-summary-save-process-mark)
6160       ;; Return the list.
6161       (reverse gnus-newsgroup-processable))
6162      (t
6163       ;; Just return the current article.
6164       (list (gnus-summary-article-number))))))
6165
6166 (defmacro gnus-summary-iterate (arg &rest forms)
6167   "Iterate over the process/prefixed articles and do FORMS.
6168 ARG is the interactive prefix given to the command.  FORMS will be
6169 executed with point over the summary line of the articles."
6170   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6171     `(let ((,articles (gnus-summary-work-articles ,arg)))
6172        (while ,articles
6173          (gnus-summary-goto-subject (car ,articles))
6174          ,@forms
6175          (pop ,articles)))))
6176
6177 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6178 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6179
6180 (defun gnus-summary-save-process-mark ()
6181   "Push the current set of process marked articles on the stack."
6182   (interactive)
6183   (push (copy-sequence gnus-newsgroup-processable)
6184         gnus-newsgroup-process-stack))
6185
6186 (defun gnus-summary-kill-process-mark ()
6187   "Push the current set of process marked articles on the stack and unmark."
6188   (interactive)
6189   (gnus-summary-save-process-mark)
6190   (gnus-summary-unmark-all-processable))
6191
6192 (defun gnus-summary-yank-process-mark ()
6193   "Pop the last process mark state off the stack and restore it."
6194   (interactive)
6195   (unless gnus-newsgroup-process-stack
6196     (error "Empty mark stack"))
6197   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6198
6199 (defun gnus-summary-process-mark-set (set)
6200   "Make SET into the current process marked articles."
6201   (gnus-summary-unmark-all-processable)
6202   (mapc 'gnus-summary-set-process-mark set))
6203
6204 ;;; Searching and stuff
6205
6206 (defun gnus-summary-search-group (&optional backward use-level)
6207   "Search for next unread newsgroup.
6208 If optional argument BACKWARD is non-nil, search backward instead."
6209   (save-excursion
6210     (set-buffer gnus-group-buffer)
6211     (when (gnus-group-search-forward
6212            backward nil (if use-level (gnus-group-group-level) nil))
6213       (gnus-group-group-name))))
6214
6215 (defun gnus-summary-best-group (&optional exclude-group)
6216   "Find the name of the best unread group.
6217 If EXCLUDE-GROUP, do not go to this group."
6218   (with-current-buffer gnus-group-buffer
6219     (save-excursion
6220       (gnus-group-best-unread-group exclude-group))))
6221
6222 (defun gnus-summary-find-next (&optional unread article backward)
6223   (if backward
6224       (gnus-summary-find-prev unread article)
6225     (let* ((dummy (gnus-summary-article-intangible-p))
6226            (article (or article (gnus-summary-article-number)))
6227            (data (gnus-data-find-list article))
6228            result)
6229       (when (and (not dummy)
6230                  (or (not gnus-summary-check-current)
6231                      (not unread)
6232                      (not (gnus-data-unread-p (car data)))))
6233         (setq data (cdr data)))
6234       (when (setq result
6235                   (if unread
6236                       (progn
6237                         (while data
6238                           (unless (memq (gnus-data-number (car data))
6239                                         (cond
6240                                          ((eq gnus-auto-goto-ignores
6241                                               'always-undownloaded)
6242                                           gnus-newsgroup-undownloaded)
6243                                          (gnus-plugged
6244                                           nil)
6245                                          ((eq gnus-auto-goto-ignores
6246                                               'unfetched)
6247                                           gnus-newsgroup-unfetched)
6248                                          ((eq gnus-auto-goto-ignores
6249                                               'undownloaded)
6250                                           gnus-newsgroup-undownloaded)))
6251                             (when (gnus-data-unread-p (car data))
6252                               (setq result (car data)
6253                                     data nil)))
6254                           (setq data (cdr data)))
6255                         result)
6256                     (car data)))
6257         (goto-char (gnus-data-pos result))
6258         (gnus-data-number result)))))
6259
6260 (defun gnus-summary-find-prev (&optional unread article)
6261   (let* ((eobp (eobp))
6262          (article (or article (gnus-summary-article-number)))
6263          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6264          result)
6265     (when (and (not eobp)
6266                (or (not gnus-summary-check-current)
6267                    (not unread)
6268                    (not (gnus-data-unread-p (car data)))))
6269       (setq data (cdr data)))
6270     (when (setq result
6271                 (if unread
6272                     (progn
6273                       (while data
6274                         (unless (memq (gnus-data-number (car data))
6275                                       (cond
6276                                        ((eq gnus-auto-goto-ignores
6277                                             'always-undownloaded)
6278                                         gnus-newsgroup-undownloaded)
6279                                        (gnus-plugged
6280                                         nil)
6281                                        ((eq gnus-auto-goto-ignores
6282                                             'unfetched)
6283                                         gnus-newsgroup-unfetched)
6284                                        ((eq gnus-auto-goto-ignores
6285                                             'undownloaded)
6286                                         gnus-newsgroup-undownloaded)))
6287                           (when (gnus-data-unread-p (car data))
6288                             (setq result (car data)
6289                                   data nil)))
6290                         (setq data (cdr data)))
6291                       result)
6292                   (car data)))
6293       (goto-char (gnus-data-pos result))
6294       (gnus-data-number result))))
6295
6296 (defun gnus-summary-find-subject (subject &optional unread backward article)
6297   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6298          (article (or article (gnus-summary-article-number)))
6299          (articles (gnus-data-list backward))
6300          (arts (gnus-data-find-list article articles))
6301          result)
6302     (when (or (not gnus-summary-check-current)
6303               (not unread)
6304               (not (gnus-data-unread-p (car arts))))
6305       (setq arts (cdr arts)))
6306     (while arts
6307       (and (or (not unread)
6308                (gnus-data-unread-p (car arts)))
6309            (vectorp (gnus-data-header (car arts)))
6310            (gnus-subject-equal
6311             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6312            (setq result (car arts)
6313                  arts nil))
6314       (setq arts (cdr arts)))
6315     (and result
6316          (goto-char (gnus-data-pos result))
6317          (gnus-data-number result))))
6318
6319 (defun gnus-summary-search-forward (&optional unread subject backward)
6320   "Search forward for an article.
6321 If UNREAD, look for unread articles.  If SUBJECT, look for
6322 articles with that subject.  If BACKWARD, search backward instead."
6323   (cond (subject (gnus-summary-find-subject subject unread backward))
6324         (backward (gnus-summary-find-prev unread))
6325         (t (gnus-summary-find-next unread))))
6326
6327 (defun gnus-recenter (&optional n)
6328   "Center point in window and redisplay frame.
6329 Also do horizontal recentering."
6330   (interactive "P")
6331   (when (and gnus-auto-center-summary
6332              (not (eq gnus-auto-center-summary 'vertical)))
6333     (gnus-horizontal-recenter))
6334   (recenter n))
6335
6336 (defun gnus-summary-recenter ()
6337   "Center point in the summary window.
6338 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6339 displayed, no centering will be performed."
6340   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6341   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6342   (interactive)
6343   ;; The user has to want it.
6344   (when gnus-auto-center-summary
6345     (let* ((top (cond ((< (window-height) 4) 0)
6346                       ((< (window-height) 7) 1)
6347                       (t (if (numberp gnus-auto-center-summary)
6348                              gnus-auto-center-summary
6349                            (/ (1- (window-height)) 2)))))
6350            (height (1- (window-height)))
6351            (bottom (save-excursion (goto-char (point-max))
6352                                    (forward-line (- height))
6353                                    (point)))
6354            (window (get-buffer-window (current-buffer))))
6355       (when (get-buffer-window gnus-article-buffer)
6356         ;; Only do recentering when the article buffer is displayed,
6357         ;; Set the window start to either `bottom', which is the biggest
6358         ;; possible valid number, or the second line from the top,
6359         ;; whichever is the least.
6360         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6361           (if (> bottom top-pos)
6362               ;; Keep the second line from the top visible
6363               (set-window-start window top-pos t)
6364             ;; Try to keep the bottom line visible; if it's partially
6365             ;; obscured, either scroll one more line to make it fully
6366             ;; visible, or revert to using TOP-POS.
6367             (save-excursion
6368               (goto-char (point-max))
6369               (forward-line -1)
6370               (let ((last-line-start (point)))
6371                 (goto-char bottom)
6372                 (set-window-start window (point) t)
6373                 (when (not (pos-visible-in-window-p last-line-start window))
6374                   (forward-line 1)
6375                   (set-window-start window (min (point) top-pos) t)))))))
6376       ;; Do horizontal recentering while we're at it.
6377       (when (and (get-buffer-window (current-buffer) t)
6378                  (not (eq gnus-auto-center-summary 'vertical)))
6379         (let ((selected (selected-window)))
6380           (select-window (get-buffer-window (current-buffer) t))
6381           (gnus-summary-position-point)
6382           (gnus-horizontal-recenter)
6383           (select-window selected))))))
6384
6385 (defun gnus-summary-jump-to-group (newsgroup)
6386   "Move point to NEWSGROUP in group mode buffer."
6387   ;; Keep update point of group mode buffer if visible.
6388   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6389       (save-window-excursion
6390         ;; Take care of tree window mode.
6391         (when (get-buffer-window gnus-group-buffer)
6392           (pop-to-buffer gnus-group-buffer))
6393         (gnus-group-jump-to-group newsgroup))
6394     (save-excursion
6395       ;; Take care of tree window mode.
6396       (if (get-buffer-window gnus-group-buffer)
6397           (pop-to-buffer gnus-group-buffer)
6398         (set-buffer gnus-group-buffer))
6399       (gnus-group-jump-to-group newsgroup))))
6400
6401 ;; This function returns a list of article numbers based on the
6402 ;; difference between the ranges of read articles in this group and
6403 ;; the range of active articles.
6404 (defun gnus-list-of-unread-articles (group)
6405   (let* ((read (gnus-info-read (gnus-get-info group)))
6406          (active (or (gnus-active group) (gnus-activate-group group)))
6407          (last (cdr active))
6408          first nlast unread)
6409     ;; If none are read, then all are unread.
6410     (if (not read)
6411         (setq first (car active))
6412       ;; If the range of read articles is a single range, then the
6413       ;; first unread article is the article after the last read
6414       ;; article.  Sounds logical, doesn't it?
6415       (if (and (not (listp (cdr read)))
6416                (or (< (car read) (car active))
6417                    (progn (setq read (list read))
6418                           nil)))
6419           (setq first (max (car active) (1+ (cdr read))))
6420         ;; `read' is a list of ranges.
6421         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6422                                   (caar read)))
6423                   1)
6424           (setq first (car active)))
6425         (while read
6426           (when first
6427             (while (< first nlast)
6428               (setq unread (cons first unread)
6429                     first (1+ first))))
6430           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6431           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6432           (setq read (cdr read)))))
6433     ;; And add the last unread articles.
6434     (while (<= first last)
6435       (setq unread (cons first unread)
6436             first (1+ first)))
6437     ;; Return the list of unread articles.
6438     (delq 0 (nreverse unread))))
6439
6440 (defun gnus-list-of-read-articles (group)
6441   "Return a list of unread, unticked and non-dormant articles."
6442   (let* ((info (gnus-get-info group))
6443          (marked (gnus-info-marks info))
6444          (active (gnus-active group)))
6445     (and info active
6446          (gnus-list-range-difference
6447           (gnus-list-range-difference
6448            (gnus-sorted-complement
6449             (gnus-uncompress-range active)
6450             (gnus-list-of-unread-articles group))
6451            (cdr (assq 'dormant marked)))
6452           (cdr (assq 'tick marked))))))
6453
6454 ;; This function returns a sequence of article numbers based on the
6455 ;; difference between the ranges of read articles in this group and
6456 ;; the range of active articles.
6457 (defun gnus-sequence-of-unread-articles (group)
6458   (let* ((read (gnus-info-read (gnus-get-info group)))
6459          (active (or (gnus-active group) (gnus-activate-group group)))
6460          (last (cdr active))
6461          first nlast unread)
6462     ;; If none are read, then all are unread.
6463     (if (not read)
6464         (setq first (car active))
6465       ;; If the range of read articles is a single range, then the
6466       ;; first unread article is the article after the last read
6467       ;; article.  Sounds logical, doesn't it?
6468       (if (and (not (listp (cdr read)))
6469                (or (< (car read) (car active))
6470                    (progn (setq read (list read))
6471                           nil)))
6472           (setq first (max (car active) (1+ (cdr read))))
6473         ;; `read' is a list of ranges.
6474         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6475                                   (caar read)))
6476                   1)
6477           (setq first (car active)))
6478         (while read
6479           (when first
6480             (push (cons first nlast) unread))
6481           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6482           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6483           (setq read (cdr read)))))
6484     ;; And add the last unread articles.
6485     (cond ((< first last)
6486            (push (cons first last) unread))
6487           ((= first last)
6488            (push first unread)))
6489     ;; Return the sequence of unread articles.
6490     (delq 0 (nreverse unread))))
6491
6492 ;; Various summary commands
6493
6494 (defun gnus-summary-select-article-buffer ()
6495   "Reconfigure windows to show article buffer."
6496   (interactive)
6497   (if (not (gnus-buffer-live-p gnus-article-buffer))
6498       (error "There is no article buffer for this summary buffer")
6499     (gnus-configure-windows 'article)
6500     (select-window (get-buffer-window gnus-article-buffer))))
6501
6502 (defun gnus-summary-universal-argument (arg)
6503   "Perform any operation on all articles that are process/prefixed."
6504   (interactive "P")
6505   (let ((articles (gnus-summary-work-articles arg))
6506         func article)
6507     (if (eq
6508          (setq
6509           func
6510           (key-binding
6511            (read-key-sequence
6512             (substitute-command-keys
6513              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6514          'undefined)
6515         (gnus-error 1 "Undefined key")
6516       (save-excursion
6517         (while articles
6518           (gnus-summary-goto-subject (setq article (pop articles)))
6519           (let (gnus-newsgroup-processable)
6520             (command-execute func))
6521           (gnus-summary-remove-process-mark article)))))
6522   (gnus-summary-position-point))
6523
6524 (defun gnus-summary-toggle-truncation (&optional arg)
6525   "Toggle truncation of summary lines.
6526 With ARG, turn line truncation on if ARG is positive."
6527   (interactive "P")
6528   (setq truncate-lines
6529         (if (null arg) (not truncate-lines)
6530           (> (prefix-numeric-value arg) 0)))
6531   (redraw-display))
6532
6533 (defun gnus-summary-find-for-reselect ()
6534   "Return the number of an article to stay on across a reselect.
6535 The current article is considered, then following articles, then previous
6536 articles.  An article is sought which is not cancelled and isn't a temporary
6537 insertion from another group.  If there's no such then return a dummy 0."
6538   (let (found)
6539     (dolist (rev '(nil t))
6540       (unless found      ; don't demand the reverse list if we don't need it
6541         (let ((data (gnus-data-find-list
6542                      (gnus-summary-article-number) (gnus-data-list rev))))
6543           (while (and data (not found))
6544             (if (and (< 0 (gnus-data-number (car data)))
6545                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6546                 (setq found (gnus-data-number (car data))))
6547             (setq data (cdr data))))))
6548     (or found 0)))
6549
6550 (defun gnus-summary-reselect-current-group (&optional all rescan)
6551   "Exit and then reselect the current newsgroup.
6552 The prefix argument ALL means to select all articles."
6553   (interactive "P")
6554   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6555     (error "Ephemeral groups can't be reselected"))
6556   (let ((current-subject (gnus-summary-find-for-reselect))
6557         (group gnus-newsgroup-name))
6558     (setq gnus-newsgroup-begin nil)
6559     (gnus-summary-exit nil 'leave-hidden)
6560     ;; We have to adjust the point of group mode buffer because
6561     ;; point was moved to the next unread newsgroup by exiting.
6562     (gnus-summary-jump-to-group group)
6563     (when rescan
6564       (save-excursion
6565         (gnus-group-get-new-news-this-group 1)))
6566     (gnus-group-read-group all t)
6567     (gnus-summary-goto-subject current-subject nil t)))
6568
6569 (defun gnus-summary-rescan-group (&optional all)
6570   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6571   (interactive "P")
6572   (gnus-summary-reselect-current-group all t))
6573
6574 (defun gnus-summary-update-info (&optional non-destructive)
6575   (save-excursion
6576     (let ((group gnus-newsgroup-name))
6577       (when group
6578         (when gnus-newsgroup-kill-headers
6579           (setq gnus-newsgroup-killed
6580                 (gnus-compress-sequence
6581                  (gnus-sorted-union
6582                   (gnus-list-range-intersection
6583                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6584                   gnus-newsgroup-unreads)
6585                  t)))
6586         (unless (listp (cdr gnus-newsgroup-killed))
6587           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6588         (let ((headers gnus-newsgroup-headers))
6589           ;; Set the new ranges of read articles.
6590           (with-current-buffer gnus-group-buffer
6591             (gnus-undo-force-boundary))
6592           (gnus-update-read-articles
6593            group (gnus-sorted-union
6594                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6595           ;; Set the current article marks.
6596           (let ((gnus-newsgroup-scored
6597                  (if (and (not gnus-save-score)
6598                           (not non-destructive))
6599                      nil
6600                    gnus-newsgroup-scored)))
6601             (save-excursion
6602               (gnus-update-marks)))
6603           ;; Do the cross-ref thing.
6604           (when gnus-use-cross-reference
6605             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6606           ;; Do not switch windows but change the buffer to work.
6607           (set-buffer gnus-group-buffer)
6608           (unless (gnus-ephemeral-group-p group)
6609             (gnus-group-update-group group)))))))
6610
6611 (defun gnus-summary-save-newsrc (&optional force)
6612   "Save the current number of read/marked articles in the dribble buffer.
6613 The dribble buffer will then be saved.
6614 If FORCE (the prefix), also save the .newsrc file(s)."
6615   (interactive "P")
6616   (gnus-summary-update-info t)
6617   (if force
6618       (gnus-save-newsrc-file)
6619     (gnus-dribble-save)))
6620
6621 (defun gnus-summary-exit (&optional temporary leave-hidden)
6622   "Exit reading current newsgroup, and then return to group selection mode.
6623 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6624   (interactive)
6625   (gnus-set-global-variables)
6626   (when (gnus-buffer-live-p gnus-article-buffer)
6627     (save-excursion
6628       (set-buffer gnus-article-buffer)
6629       (mm-destroy-parts gnus-article-mime-handles)
6630       ;; Set it to nil for safety reason.
6631       (setq gnus-article-mime-handle-alist nil)
6632       (setq gnus-article-mime-handles nil)))
6633   (gnus-kill-save-kill-buffer)
6634   (gnus-async-halt-prefetch)
6635   (let* ((group gnus-newsgroup-name)
6636          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6637          (gnus-group-is-exiting-p t)
6638          (mode major-mode)
6639          (group-point nil)
6640          (buf (current-buffer)))
6641     (unless quit-config
6642       ;; Do adaptive scoring, and possibly save score files.
6643       (when gnus-newsgroup-adaptive
6644         (gnus-score-adaptive))
6645       (when gnus-use-scoring
6646         (gnus-score-save)))
6647     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6648     ;; If we have several article buffers, we kill them at exit.
6649     (unless gnus-single-article-buffer
6650       (gnus-kill-buffer gnus-original-article-buffer)
6651       (setq gnus-article-current nil))
6652     (when gnus-use-cache
6653       (gnus-cache-possibly-remove-articles)
6654       (gnus-cache-save-buffers))
6655     (gnus-async-prefetch-remove-group group)
6656     (when gnus-suppress-duplicates
6657       (gnus-dup-enter-articles))
6658     (when gnus-use-trees
6659       (gnus-tree-close group))
6660     (when gnus-use-cache
6661       (gnus-cache-write-active))
6662     ;; Remove entries for this group.
6663     (nnmail-purge-split-history (gnus-group-real-name group))
6664     ;; Make all changes in this group permanent.
6665     (unless quit-config
6666       (gnus-run-hooks 'gnus-exit-group-hook)
6667       (gnus-summary-update-info))
6668     (gnus-close-group group)
6669     ;; Make sure where we were, and go to next newsgroup.
6670     (set-buffer gnus-group-buffer)
6671     (unless quit-config
6672       (gnus-group-jump-to-group group))
6673     (gnus-run-hooks 'gnus-summary-exit-hook)
6674     (unless (or quit-config
6675                 ;; If this group has disappeared from the summary
6676                 ;; buffer, don't skip forwards.
6677                 (not (string= group (gnus-group-group-name))))
6678       (gnus-group-next-unread-group 1))
6679     (setq group-point (point))
6680     (if temporary
6681         nil                             ;Nothing to do.
6682       ;; If we have several article buffers, we kill them at exit.
6683       (unless gnus-single-article-buffer
6684         (gnus-kill-buffer gnus-article-buffer)
6685         (gnus-kill-buffer gnus-original-article-buffer)
6686         (setq gnus-article-current nil))
6687       (set-buffer buf)
6688       (if (not gnus-kill-summary-on-exit)
6689           (progn
6690             (gnus-deaden-summary)
6691             (setq mode nil))
6692         ;; We set all buffer-local variables to nil.  It is unclear why
6693         ;; this is needed, but if we don't, buffer-local variables are
6694         ;; not garbage-collected, it seems.  This would the lead to en
6695         ;; ever-growing Emacs.
6696         (gnus-summary-clear-local-variables)
6697         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6698           (gnus-summary-clear-local-variables))
6699         (when (get-buffer gnus-article-buffer)
6700           (bury-buffer gnus-article-buffer))
6701         ;; We clear the global counterparts of the buffer-local
6702         ;; variables as well, just to be on the safe side.
6703         (set-buffer gnus-group-buffer)
6704         (gnus-summary-clear-local-variables)
6705         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6706           (gnus-summary-clear-local-variables))
6707         ;; Return to group mode buffer.
6708         (when (eq mode 'gnus-summary-mode)
6709           (gnus-kill-buffer buf)))
6710       (setq gnus-current-select-method gnus-select-method)
6711       (if leave-hidden
6712           (set-buffer gnus-group-buffer)
6713         (pop-to-buffer gnus-group-buffer))
6714       (if (not quit-config)
6715           (progn
6716             (goto-char group-point)
6717             (unless leave-hidden
6718               (gnus-configure-windows 'group 'force)))
6719         (gnus-handle-ephemeral-exit quit-config))
6720       ;; Clear the current group name.
6721       (unless quit-config
6722         (setq gnus-newsgroup-name nil)))))
6723
6724 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6725 (defun gnus-summary-exit-no-update (&optional no-questions)
6726   "Quit reading current newsgroup without updating read article info."
6727   (interactive)
6728   (let* ((group gnus-newsgroup-name)
6729          (gnus-group-is-exiting-p t)
6730          (gnus-group-is-exiting-without-update-p t)
6731          (quit-config (gnus-group-quit-config group)))
6732     (when (or no-questions
6733               gnus-expert-user
6734               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6735       (gnus-async-halt-prefetch)
6736       (run-hooks 'gnus-summary-prepare-exit-hook)
6737       (when (gnus-buffer-live-p gnus-article-buffer)
6738         (save-excursion
6739           (set-buffer gnus-article-buffer)
6740           (mm-destroy-parts gnus-article-mime-handles)
6741           ;; Set it to nil for safety reason.
6742           (setq gnus-article-mime-handle-alist nil)
6743           (setq gnus-article-mime-handles nil)))
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       (if (not gnus-kill-summary-on-exit)
6750           (gnus-deaden-summary)
6751         (gnus-close-group group)
6752         (gnus-summary-clear-local-variables)
6753         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6754           (gnus-summary-clear-local-variables))
6755         (set-buffer gnus-group-buffer)
6756         (gnus-summary-clear-local-variables)
6757         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6758           (gnus-summary-clear-local-variables))
6759         (gnus-kill-buffer gnus-summary-buffer))
6760       (unless gnus-single-article-buffer
6761         (setq gnus-article-current nil))
6762       (when gnus-use-trees
6763         (gnus-tree-close group))
6764       (gnus-async-prefetch-remove-group group)
6765       (when (get-buffer gnus-article-buffer)
6766         (bury-buffer gnus-article-buffer))
6767       ;; Return to the group buffer.
6768       (gnus-configure-windows 'group 'force)
6769       ;; Clear the current group name.
6770       (setq gnus-newsgroup-name nil)
6771       (unless (gnus-ephemeral-group-p group)
6772         (gnus-group-update-group group))
6773       (when (equal (gnus-group-group-name) group)
6774         (gnus-group-next-unread-group 1))
6775       (when quit-config
6776         (gnus-handle-ephemeral-exit quit-config)))))
6777
6778 (defun gnus-handle-ephemeral-exit (quit-config)
6779   "Handle movement when leaving an ephemeral group.
6780 The state which existed when entering the ephemeral is reset."
6781   (if (not (buffer-name (car quit-config)))
6782       (gnus-configure-windows 'group 'force)
6783     (set-buffer (car quit-config))
6784     (cond ((eq major-mode 'gnus-summary-mode)
6785            (gnus-set-global-variables))
6786           ((eq major-mode 'gnus-article-mode)
6787            (save-excursion
6788              ;; The `gnus-summary-buffer' variable may point
6789              ;; to the old summary buffer when using a single
6790              ;; article buffer.
6791              (unless (gnus-buffer-live-p gnus-summary-buffer)
6792                (set-buffer gnus-group-buffer))
6793              (set-buffer gnus-summary-buffer)
6794              (gnus-set-global-variables))))
6795     (if (or (eq (cdr quit-config) 'article)
6796             (eq (cdr quit-config) 'pick))
6797         (progn
6798           ;; The current article may be from the ephemeral group
6799           ;; thus it is best that we reload this article
6800           ;;
6801           ;; If we're exiting from a large digest, this can be
6802           ;; extremely slow.  So, it's better not to reload it. -- jh.
6803           ;;(gnus-summary-show-article)
6804           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6805               (gnus-configure-windows 'pick 'force)
6806             (gnus-configure-windows (cdr quit-config) 'force)))
6807       (gnus-configure-windows (cdr quit-config) 'force))
6808     (when (eq major-mode 'gnus-summary-mode)
6809       (gnus-summary-next-subject 1 nil t)
6810       (gnus-summary-recenter)
6811       (gnus-summary-position-point))))
6812
6813 ;;; Dead summaries.
6814
6815 (defvar gnus-dead-summary-mode-map nil)
6816
6817 (unless gnus-dead-summary-mode-map
6818   (setq gnus-dead-summary-mode-map (make-keymap))
6819   (suppress-keymap gnus-dead-summary-mode-map)
6820   (substitute-key-definition
6821    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6822   (dolist (key '("\C-d" "\r" "\177" [delete]))
6823     (define-key gnus-dead-summary-mode-map
6824       key 'gnus-summary-wake-up-the-dead))
6825   (dolist (key '("q" "Q"))
6826     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6827
6828 (defvar gnus-dead-summary-mode nil
6829   "Minor mode for Gnus summary buffers.")
6830
6831 (defun gnus-dead-summary-mode (&optional arg)
6832   "Minor mode for Gnus summary buffers."
6833   (interactive "P")
6834   (when (eq major-mode 'gnus-summary-mode)
6835     (make-local-variable 'gnus-dead-summary-mode)
6836     (setq gnus-dead-summary-mode
6837           (if (null arg) (not gnus-dead-summary-mode)
6838             (> (prefix-numeric-value arg) 0)))
6839     (when gnus-dead-summary-mode
6840       (add-minor-mode
6841        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6842
6843 (defun gnus-deaden-summary ()
6844   "Make the current summary buffer into a dead summary buffer."
6845   ;; Kill any previous dead summary buffer.
6846   (when (and gnus-dead-summary
6847              (buffer-name gnus-dead-summary))
6848     (with-current-buffer gnus-dead-summary
6849       (when gnus-dead-summary-mode
6850         (kill-buffer (current-buffer)))))
6851   ;; Make this the current dead summary.
6852   (setq gnus-dead-summary (current-buffer))
6853   (gnus-dead-summary-mode 1)
6854   (let ((name (buffer-name)))
6855     (when (string-match "Summary" name)
6856       (rename-buffer
6857        (concat (substring name 0 (match-beginning 0)) "Dead "
6858                (substring name (match-beginning 0)))
6859        t)
6860       (bury-buffer))))
6861
6862 (defun gnus-kill-or-deaden-summary (buffer)
6863   "Kill or deaden the summary BUFFER."
6864   (save-excursion
6865     (when (and (buffer-name buffer)
6866                (not gnus-single-article-buffer))
6867       (with-current-buffer buffer
6868         (gnus-kill-buffer gnus-article-buffer)
6869         (gnus-kill-buffer gnus-original-article-buffer)))
6870     (cond
6871      ;; Kill the buffer.
6872      (gnus-kill-summary-on-exit
6873       (when (and gnus-use-trees
6874                  (gnus-buffer-exists-p buffer))
6875         (save-excursion
6876           (set-buffer buffer)
6877           (gnus-tree-close gnus-newsgroup-name)))
6878       (gnus-kill-buffer buffer))
6879      ;; Deaden the buffer.
6880      ((gnus-buffer-exists-p buffer)
6881       (save-excursion
6882         (set-buffer buffer)
6883         (gnus-deaden-summary))))))
6884
6885 (defun gnus-summary-wake-up-the-dead (&rest args)
6886   "Wake up the dead summary buffer."
6887   (interactive)
6888   (gnus-dead-summary-mode -1)
6889   (let ((name (buffer-name)))
6890     (when (string-match "Dead " name)
6891       (rename-buffer
6892        (concat (substring name 0 (match-beginning 0))
6893                (substring name (match-end 0)))
6894        t)))
6895   (gnus-message 3 "This dead summary is now alive again"))
6896
6897 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6898 (defun gnus-summary-fetch-faq (&optional faq-dir)
6899   "Fetch the FAQ for the current group.
6900 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6901 in."
6902   (interactive
6903    (list
6904     (when current-prefix-arg
6905       (completing-read
6906        "FAQ dir: " (and (listp gnus-group-faq-directory)
6907                         (mapcar 'list
6908                                 gnus-group-faq-directory))))))
6909   (let (gnus-faq-buffer)
6910     (when (setq gnus-faq-buffer
6911                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6912       (gnus-configure-windows 'summary-faq))))
6913
6914 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6915 (defun gnus-summary-describe-group (&optional force)
6916   "Describe the current newsgroup."
6917   (interactive "P")
6918   (gnus-group-describe-group force gnus-newsgroup-name))
6919
6920 (defun gnus-summary-describe-briefly ()
6921   "Describe summary mode commands briefly."
6922   (interactive)
6923   (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")))
6924
6925 ;; Walking around group mode buffer from summary mode.
6926
6927 (defun gnus-summary-next-group (&optional no-article target-group backward)
6928   "Exit current newsgroup and then select next unread newsgroup.
6929 If prefix argument NO-ARTICLE is non-nil, no article is selected
6930 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6931 previous group instead."
6932   (interactive "P")
6933   ;; Stop pre-fetching.
6934   (gnus-async-halt-prefetch)
6935   (let ((current-group gnus-newsgroup-name)
6936         (current-buffer (current-buffer))
6937         entered)
6938     ;; First we semi-exit this group to update Xrefs and all variables.
6939     ;; We can't do a real exit, because the window conf must remain
6940     ;; the same in case the user is prompted for info, and we don't
6941     ;; want the window conf to change before that...
6942     (gnus-summary-exit t)
6943     (while (not entered)
6944       ;; Then we find what group we are supposed to enter.
6945       (set-buffer gnus-group-buffer)
6946       (gnus-group-jump-to-group current-group)
6947       (setq target-group
6948             (or target-group
6949                 (if (eq gnus-keep-same-level 'best)
6950                     (gnus-summary-best-group gnus-newsgroup-name)
6951                   (gnus-summary-search-group backward gnus-keep-same-level))))
6952       (if (not target-group)
6953           ;; There are no further groups, so we return to the group
6954           ;; buffer.
6955           (progn
6956             (gnus-message 5 "Returning to the group buffer")
6957             (setq entered t)
6958             (when (gnus-buffer-live-p current-buffer)
6959               (set-buffer current-buffer)
6960               (gnus-summary-exit))
6961             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6962         ;; We try to enter the target group.
6963         (gnus-group-jump-to-group target-group)
6964         (let ((unreads (gnus-group-group-unread)))
6965           (if (and (or (eq t unreads)
6966                        (and unreads (not (zerop unreads))))
6967                    (gnus-summary-read-group
6968                     target-group nil no-article
6969                     (and (buffer-name current-buffer) current-buffer)
6970                     nil backward))
6971               (setq entered t)
6972             (setq current-group target-group
6973                   target-group nil)))))))
6974
6975 (defun gnus-summary-prev-group (&optional no-article)
6976   "Exit current newsgroup and then select previous unread newsgroup.
6977 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6978   (interactive "P")
6979   (gnus-summary-next-group no-article nil t))
6980
6981 ;; Walking around summary lines.
6982
6983 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6984   "Go to the first subject satisfying any non-nil constraint.
6985 If UNREAD is non-nil, the article should be unread.
6986 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6987 If UNSEEN is non-nil, the article should be unseen.
6988 Returns the article selected or nil if there are no matching articles."
6989   (interactive "P")
6990   (cond
6991    ;; Empty summary.
6992    ((null gnus-newsgroup-data)
6993     (gnus-message 3 "No articles in the group")
6994     nil)
6995    ;; Pick the first article.
6996    ((not (or unread undownloaded unseen))
6997     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6998     (gnus-data-number (car gnus-newsgroup-data)))
6999    ;; Find the first unread article.
7000    (t
7001     (let ((data gnus-newsgroup-data))
7002       (while (and data
7003                   (let ((num (gnus-data-number (car data))))
7004                     (or (memq num gnus-newsgroup-unfetched)
7005                         (not (or (and unread
7006                                       (memq num gnus-newsgroup-unreads))
7007                                  (and undownloaded
7008                                       (memq num gnus-newsgroup-undownloaded))
7009                                  (and unseen
7010                                       (memq num gnus-newsgroup-unseen)))))))
7011         (setq data (cdr data)))
7012       (prog1
7013           (if data
7014               (progn
7015                 (goto-char (gnus-data-pos (car data)))
7016                 (gnus-data-number (car data)))
7017             (gnus-message 3 "No more%s articles"
7018                           (let* ((r (when unread " unread"))
7019                                  (d (when undownloaded " undownloaded"))
7020                                  (s (when unseen " unseen"))
7021                                  (l (delq nil (list r d s))))
7022                             (cond ((= 3 (length l))
7023                                    (concat r "," d ", or" s))
7024                                   ((= 2 (length l))
7025                                    (concat (car l) ", or" (cadr l)))
7026                                   ((= 1 (length l))
7027                                    (car l))
7028                                   (t
7029                                    ""))))
7030             nil
7031             )
7032         (gnus-summary-position-point))))))
7033
7034 (defun gnus-summary-next-subject (n &optional unread dont-display)
7035   "Go to next N'th summary line.
7036 If N is negative, go to the previous N'th subject line.
7037 If UNREAD is non-nil, only unread articles are selected.
7038 The difference between N and the actual number of steps taken is
7039 returned."
7040   (interactive "p")
7041   (let ((backward (< n 0))
7042         (n (abs n)))
7043     (while (and (> n 0)
7044                 (if backward
7045                     (gnus-summary-find-prev unread)
7046                   (gnus-summary-find-next unread)))
7047       (unless (zerop (setq n (1- n)))
7048         (gnus-summary-show-thread)))
7049     (when (/= 0 n)
7050       (gnus-message 7 "No more%s articles"
7051                     (if unread " unread" "")))
7052     (unless dont-display
7053       (gnus-summary-recenter)
7054       (gnus-summary-position-point))
7055     n))
7056
7057 (defun gnus-summary-next-unread-subject (n)
7058   "Go to next N'th unread summary line."
7059   (interactive "p")
7060   (gnus-summary-next-subject n t))
7061
7062 (defun gnus-summary-prev-subject (n &optional unread)
7063   "Go to previous N'th summary line.
7064 If optional argument UNREAD is non-nil, only unread article is selected."
7065   (interactive "p")
7066   (gnus-summary-next-subject (- n) unread))
7067
7068 (defun gnus-summary-prev-unread-subject (n)
7069   "Go to previous N'th unread summary line."
7070   (interactive "p")
7071   (gnus-summary-next-subject (- n) t))
7072
7073 (defun gnus-summary-goto-subjects (articles)
7074   "Insert the subject header for ARTICLES in the current buffer."
7075   (save-excursion
7076     (dolist (article articles)
7077       (gnus-summary-goto-subject article t)))
7078   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7079   (gnus-summary-position-point))
7080  
7081 (defun gnus-summary-goto-subject (article &optional force silent)
7082   "Go the subject line of ARTICLE.
7083 If FORCE, also allow jumping to articles not currently shown."
7084   (interactive "nArticle number: ")
7085   (unless (numberp article)
7086     (error "Article %s is not a number" article))
7087   (let ((b (point))
7088         (data (gnus-data-find article)))
7089     ;; We read in the article if we have to.
7090     (and (not data)
7091          force
7092          (gnus-summary-insert-subject
7093           article
7094           (if (or (numberp force) (vectorp force)) force)
7095           t)
7096          (setq data (gnus-data-find article)))
7097     (goto-char b)
7098     (if (not data)
7099         (progn
7100           (unless silent
7101             (gnus-message 3 "Can't find article %d" article))
7102           nil)
7103       (let ((pt (gnus-data-pos data)))
7104         (goto-char pt)
7105         (gnus-summary-set-article-display-arrow pt))
7106       (gnus-summary-position-point)
7107       article)))
7108
7109 ;; Walking around summary lines with displaying articles.
7110
7111 (defun gnus-summary-expand-window (&optional arg)
7112   "Make the summary buffer take up the entire Emacs frame.
7113 Given a prefix, will force an `article' buffer configuration."
7114   (interactive "P")
7115   (if arg
7116       (gnus-configure-windows 'article 'force)
7117     (gnus-configure-windows 'summary 'force)))
7118
7119 (defun gnus-summary-display-article (article &optional all-header)
7120   "Display ARTICLE in article buffer."
7121   (when (gnus-buffer-live-p gnus-article-buffer)
7122     (with-current-buffer gnus-article-buffer
7123       (mm-enable-multibyte)))
7124   (gnus-set-global-variables)
7125   (when (gnus-buffer-live-p gnus-article-buffer)
7126     (with-current-buffer gnus-article-buffer
7127       (setq gnus-article-charset gnus-newsgroup-charset)
7128       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7129       (mm-enable-multibyte)))
7130   (if (null article)
7131       nil
7132     (prog1
7133         (if gnus-summary-display-article-function
7134             (funcall gnus-summary-display-article-function article all-header)
7135           (gnus-article-prepare article all-header))
7136       (gnus-run-hooks 'gnus-select-article-hook)
7137       (when (and gnus-current-article
7138                  (not (zerop gnus-current-article)))
7139         (gnus-summary-goto-subject gnus-current-article))
7140       (gnus-summary-recenter)
7141       (when (and gnus-use-trees gnus-show-threads)
7142         (gnus-possibly-generate-tree article)
7143         (gnus-highlight-selected-tree article))
7144       ;; Successfully display article.
7145       (gnus-article-set-window-start
7146        (cdr (assq article gnus-newsgroup-bookmarks))))))
7147
7148 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7149   "Select the current article.
7150 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7151 non-nil, the article will be re-fetched even if it already present in
7152 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7153 be displayed."
7154   ;; Make sure we are in the summary buffer to work around bbdb bug.
7155   (unless (eq major-mode 'gnus-summary-mode)
7156     (set-buffer gnus-summary-buffer))
7157   (let ((article (or article (gnus-summary-article-number)))
7158         (all-headers (not (not all-headers))) ;Must be t or nil.
7159         gnus-summary-display-article-function)
7160     (and (not pseudo)
7161          (gnus-summary-article-pseudo-p article)
7162          (error "This is a pseudo-article"))
7163     (save-excursion
7164       (set-buffer gnus-summary-buffer)
7165       (if (or (and gnus-single-article-buffer
7166                    (or (null gnus-current-article)
7167                        (null gnus-article-current)
7168                        (null (get-buffer gnus-article-buffer))
7169                        (not (eq article (cdr gnus-article-current)))
7170                        (not (equal (car gnus-article-current)
7171                                    gnus-newsgroup-name))))
7172               (and (not gnus-single-article-buffer)
7173                    (or (null gnus-current-article)
7174                        (not (eq gnus-current-article article))))
7175               force)
7176           ;; The requested article is different from the current article.
7177           (progn
7178             (gnus-summary-display-article article all-headers)
7179             (when (gnus-buffer-live-p gnus-article-buffer)
7180               (with-current-buffer gnus-article-buffer
7181                 (if (not gnus-article-decoded-p) ;; a local variable
7182                     (mm-disable-multibyte))))
7183             (gnus-article-set-window-start
7184              (cdr (assq article gnus-newsgroup-bookmarks)))
7185             article)
7186         'old))))
7187
7188 (defun gnus-summary-force-verify-and-decrypt ()
7189   "Display buttons for signed/encrypted parts and verify/decrypt them."
7190   (interactive)
7191   (let ((mm-verify-option 'known)
7192         (mm-decrypt-option 'known)
7193         (gnus-article-emulate-mime t)
7194         (gnus-buttonized-mime-types (append (list "multipart/signed"
7195                                                   "multipart/encrypted")
7196                                             gnus-buttonized-mime-types)))
7197     (gnus-summary-select-article nil 'force)))
7198
7199 (defun gnus-summary-set-current-mark (&optional current-mark)
7200   "Obsolete function."
7201   nil)
7202
7203 (defun gnus-summary-next-article (&optional unread subject backward push)
7204   "Select the next article.
7205 If UNREAD, only unread articles are selected.
7206 If SUBJECT, only articles with SUBJECT are selected.
7207 If BACKWARD, the previous article is selected instead of the next."
7208   (interactive "P")
7209   (cond
7210    ;; Is there such an article?
7211    ((and (gnus-summary-search-forward unread subject backward)
7212          (or (gnus-summary-display-article (gnus-summary-article-number))
7213              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7214     (gnus-summary-position-point))
7215    ;; If not, we try the first unread, if that is wanted.
7216    ((and subject
7217          gnus-auto-select-same
7218          (gnus-summary-first-unread-article))
7219     (gnus-summary-position-point)
7220     (gnus-message 6 "Wrapped"))
7221    ;; Try to get next/previous article not displayed in this group.
7222    ((and gnus-auto-extend-newsgroup
7223          (not unread) (not subject))
7224     (gnus-summary-goto-article
7225      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7226      nil (count-lines (point-min) (point))))
7227    ;; Go to next/previous group.
7228    (t
7229     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7230       (gnus-summary-jump-to-group gnus-newsgroup-name))
7231     (let ((cmd last-command-char)
7232           (point
7233            (with-current-buffer gnus-group-buffer
7234              (point)))
7235           (group
7236            (if (eq gnus-keep-same-level 'best)
7237                (gnus-summary-best-group gnus-newsgroup-name)
7238              (gnus-summary-search-group backward gnus-keep-same-level))))
7239       ;; For some reason, the group window gets selected.  We change
7240       ;; it back.
7241       (select-window (get-buffer-window (current-buffer)))
7242       ;; Select next unread newsgroup automagically.
7243       (cond
7244        ((or (not gnus-auto-select-next)
7245             (not cmd))
7246         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7247        ((or (eq gnus-auto-select-next 'quietly)
7248             (and (eq gnus-auto-select-next 'slightly-quietly)
7249                  push)
7250             (and (eq gnus-auto-select-next 'almost-quietly)
7251                  (gnus-summary-last-article-p)))
7252         ;; Select quietly.
7253         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7254             (gnus-summary-exit)
7255           (gnus-message 7 "No more%s articles (%s)..."
7256                         (if unread " unread" "")
7257                         (if group (concat "selecting " group)
7258                           "exiting"))
7259           (gnus-summary-next-group nil group backward)))
7260        (t
7261         (when (gnus-key-press-event-p last-input-event)
7262           (gnus-summary-walk-group-buffer
7263            gnus-newsgroup-name cmd unread backward point))))))))
7264
7265 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7266   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7267                       (?\C-p (gnus-group-prev-unread-group 1))))
7268         (cursor-in-echo-area t)
7269         keve key group ended prompt)
7270     (save-excursion
7271       (set-buffer gnus-group-buffer)
7272       (goto-char start)
7273       (setq group
7274             (if (eq gnus-keep-same-level 'best)
7275                 (gnus-summary-best-group gnus-newsgroup-name)
7276               (gnus-summary-search-group backward gnus-keep-same-level))))
7277     (while (not ended)
7278       (setq prompt
7279             (format
7280              "No more%s articles%s " (if unread " unread" "")
7281              (if (and group
7282                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7283                  (format " (Type %s for %s [%s])"
7284                          (single-key-description cmd) group
7285                          (gnus-group-unread group))
7286                (format " (Type %s to exit %s)"
7287                        (single-key-description cmd)
7288                        gnus-newsgroup-name))))
7289       ;; Confirm auto selection.
7290       (setq key (car (setq keve (gnus-read-event-char prompt)))
7291             ended t)
7292       (cond
7293        ((assq key keystrokes)
7294         (let ((obuf (current-buffer)))
7295           (switch-to-buffer gnus-group-buffer)
7296           (when group
7297             (gnus-group-jump-to-group group))
7298           (eval (cadr (assq key keystrokes)))
7299           (setq group (gnus-group-group-name))
7300           (switch-to-buffer obuf))
7301         (setq ended nil))
7302        ((equal key cmd)
7303         (if (or (not group)
7304                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7305             (gnus-summary-exit)
7306           (gnus-summary-next-group nil group backward)))
7307        (t
7308         (push (cdr keve) unread-command-events))))))
7309
7310 (defun gnus-summary-next-unread-article ()
7311   "Select unread article after current one."
7312   (interactive)
7313   (gnus-summary-next-article
7314    (or (not (eq gnus-summary-goto-unread 'never))
7315        (gnus-summary-last-article-p (gnus-summary-article-number)))
7316    (and gnus-auto-select-same
7317         (gnus-summary-article-subject))))
7318
7319 (defun gnus-summary-prev-article (&optional unread subject)
7320   "Select the article after the current one.
7321 If UNREAD is non-nil, only unread articles are selected."
7322   (interactive "P")
7323   (gnus-summary-next-article unread subject t))
7324
7325 (defun gnus-summary-prev-unread-article ()
7326   "Select unread article before current one."
7327   (interactive)
7328   (gnus-summary-prev-article
7329    (or (not (eq gnus-summary-goto-unread 'never))
7330        (gnus-summary-first-article-p (gnus-summary-article-number)))
7331    (and gnus-auto-select-same
7332         (gnus-summary-article-subject))))
7333
7334 (defun gnus-summary-next-page (&optional lines circular stop)
7335   "Show next page of the selected article.
7336 If at the end of the current article, select the next article.
7337 LINES says how many lines should be scrolled up.
7338
7339 If CIRCULAR is non-nil, go to the start of the article instead of
7340 selecting the next article when reaching the end of the current
7341 article.
7342
7343 If STOP is non-nil, just stop when reaching the end of the message.
7344
7345 Also see the variable `gnus-article-skip-boring'."
7346   (interactive "P")
7347   (setq gnus-summary-buffer (current-buffer))
7348   (gnus-set-global-variables)
7349   (let ((article (gnus-summary-article-number))
7350         (article-window (get-buffer-window gnus-article-buffer t))
7351         endp)
7352     ;; If the buffer is empty, we have no article.
7353     (unless article
7354       (error "No article to select"))
7355     (gnus-configure-windows 'article)
7356     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7357         (if (and (eq gnus-summary-goto-unread 'never)
7358                  (not (gnus-summary-last-article-p article)))
7359             (gnus-summary-next-article)
7360           (gnus-summary-next-unread-article))
7361       (if (or (null gnus-current-article)
7362               (null gnus-article-current)
7363               (/= article (cdr gnus-article-current))
7364               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7365           ;; Selected subject is different from current article's.
7366           (gnus-summary-display-article article)
7367         (when article-window
7368           (gnus-eval-in-buffer-window gnus-article-buffer
7369             (setq endp (or (gnus-article-next-page lines)
7370                            (gnus-article-only-boring-p))))
7371           (when endp
7372             (cond (stop
7373                    (gnus-message 3 "End of message"))
7374                   (circular
7375                    (gnus-summary-beginning-of-article))
7376                   (lines
7377                    (gnus-message 3 "End of message"))
7378                   ((null lines)
7379                    (if (and (eq gnus-summary-goto-unread 'never)
7380                             (not (gnus-summary-last-article-p article)))
7381                        (gnus-summary-next-article)
7382                      (gnus-summary-next-unread-article))))))))
7383     (gnus-summary-recenter)
7384     (gnus-summary-position-point)))
7385
7386 (defun gnus-summary-prev-page (&optional lines move)
7387   "Show previous page of selected article.
7388 Argument LINES specifies lines to be scrolled down.
7389 If MOVE, move to the previous unread article if point is at
7390 the beginning of the buffer."
7391   (interactive "P")
7392   (let ((article (gnus-summary-article-number))
7393         (article-window (get-buffer-window gnus-article-buffer t))
7394         endp)
7395     (gnus-configure-windows 'article)
7396     (if (or (null gnus-current-article)
7397             (null gnus-article-current)
7398             (/= article (cdr gnus-article-current))
7399             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7400         ;; Selected subject is different from current article's.
7401         (gnus-summary-display-article article)
7402       (gnus-summary-recenter)
7403       (when article-window
7404         (gnus-eval-in-buffer-window gnus-article-buffer
7405           (setq endp (gnus-article-prev-page lines)))
7406         (when (and move endp)
7407           (cond (lines
7408                  (gnus-message 3 "Beginning of message"))
7409                 ((null lines)
7410                  (if (and (eq gnus-summary-goto-unread 'never)
7411                           (not (gnus-summary-first-article-p article)))
7412                      (gnus-summary-prev-article)
7413                    (gnus-summary-prev-unread-article))))))))
7414   (gnus-summary-position-point))
7415
7416 (defun gnus-summary-prev-page-or-article (&optional lines)
7417   "Show previous page of selected article.
7418 Argument LINES specifies lines to be scrolled down.
7419 If at the beginning of the article, go to the next article."
7420   (interactive "P")
7421   (gnus-summary-prev-page lines t))
7422
7423 (defun gnus-summary-scroll-up (lines)
7424   "Scroll up (or down) one line current article.
7425 Argument LINES specifies lines to be scrolled up (or down if negative)."
7426   (interactive "p")
7427   (gnus-configure-windows 'article)
7428   (gnus-summary-show-thread)
7429   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7430     (gnus-eval-in-buffer-window gnus-article-buffer
7431       (cond ((> lines 0)
7432              (when (gnus-article-next-page lines)
7433                (gnus-message 3 "End of message")))
7434             ((< lines 0)
7435              (gnus-article-prev-page (- lines))))))
7436   (gnus-summary-recenter)
7437   (gnus-summary-position-point))
7438
7439 (defun gnus-summary-scroll-down (lines)
7440   "Scroll down (or up) one line current article.
7441 Argument LINES specifies lines to be scrolled down (or up if negative)."
7442   (interactive "p")
7443   (gnus-summary-scroll-up (- lines)))
7444
7445 (defun gnus-summary-next-same-subject ()
7446   "Select next article which has the same subject as current one."
7447   (interactive)
7448   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7449
7450 (defun gnus-summary-prev-same-subject ()
7451   "Select previous article which has the same subject as current one."
7452   (interactive)
7453   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7454
7455 (defun gnus-summary-next-unread-same-subject ()
7456   "Select next unread article which has the same subject as current one."
7457   (interactive)
7458   (gnus-summary-next-article t (gnus-summary-article-subject)))
7459
7460 (defun gnus-summary-prev-unread-same-subject ()
7461   "Select previous unread article which has the same subject as current one."
7462   (interactive)
7463   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7464
7465 (defun gnus-summary-first-unread-article ()
7466   "Select the first unread article.
7467 Return nil if there are no unread articles."
7468   (interactive)
7469   (prog1
7470       (when (gnus-summary-first-subject t)
7471         (gnus-summary-show-thread)
7472         (gnus-summary-first-subject t)
7473         (gnus-summary-display-article (gnus-summary-article-number)))
7474     (gnus-summary-position-point)))
7475
7476 (defun gnus-summary-first-unread-subject ()
7477   "Place the point on the subject line of the first unread article.
7478 Return nil if there are no unread articles."
7479   (interactive)
7480   (prog1
7481       (when (gnus-summary-first-subject t)
7482         (gnus-summary-show-thread)
7483         (gnus-summary-first-subject t))
7484     (gnus-summary-position-point)))
7485
7486 (defun gnus-summary-first-unseen-subject ()
7487   "Place the point on the subject line of the first unseen article.
7488 Return nil if there are no unseen articles."
7489   (interactive)
7490   (prog1
7491       (when (gnus-summary-first-subject nil nil t)
7492         (gnus-summary-show-thread)
7493         (gnus-summary-first-subject nil nil t))
7494     (gnus-summary-position-point)))
7495
7496 (defun gnus-summary-first-unseen-or-unread-subject ()
7497   "Place the point on the subject line of the first unseen article or,
7498 if all article have been seen, on the subject line of the first unread
7499 article."
7500   (interactive)
7501   (prog1
7502       (unless (when (gnus-summary-first-subject nil nil t)
7503                 (gnus-summary-show-thread)
7504                 (gnus-summary-first-subject nil nil t))
7505         (when (gnus-summary-first-subject t)
7506           (gnus-summary-show-thread)
7507           (gnus-summary-first-subject t)))
7508     (gnus-summary-position-point)))
7509
7510 (defun gnus-summary-first-article ()
7511   "Select the first article.
7512 Return nil if there are no articles."
7513   (interactive)
7514   (prog1
7515       (when (gnus-summary-first-subject)
7516         (gnus-summary-show-thread)
7517         (gnus-summary-first-subject)
7518         (gnus-summary-display-article (gnus-summary-article-number)))
7519     (gnus-summary-position-point)))
7520
7521 (defun gnus-summary-best-unread-article (&optional arg)
7522   "Select the unread article with the highest score.
7523 If given a prefix argument, select the next unread article that has a
7524 score higher than the default score."
7525   (interactive "P")
7526   (let ((article (if arg
7527                      (gnus-summary-better-unread-subject)
7528                    (gnus-summary-best-unread-subject))))
7529     (if article
7530         (gnus-summary-goto-article article)
7531       (error "No unread articles"))))
7532
7533 (defun gnus-summary-best-unread-subject ()
7534   "Select the unread subject with the highest score."
7535   (interactive)
7536   (let ((best -1000000)
7537         (data gnus-newsgroup-data)
7538         article score)
7539     (while data
7540       (and (gnus-data-unread-p (car data))
7541            (> (setq score
7542                     (gnus-summary-article-score (gnus-data-number (car data))))
7543               best)
7544            (setq best score
7545                  article (gnus-data-number (car data))))
7546       (setq data (cdr data)))
7547     (when article
7548       (gnus-summary-goto-subject article))
7549     (gnus-summary-position-point)
7550     article))
7551
7552 (defun gnus-summary-better-unread-subject ()
7553   "Select the first unread subject that has a score over the default score."
7554   (interactive)
7555   (let ((data gnus-newsgroup-data)
7556         article score)
7557     (while (and (setq article (gnus-data-number (car data)))
7558                 (or (gnus-data-read-p (car data))
7559                     (not (> (gnus-summary-article-score article)
7560                             gnus-summary-default-score))))
7561       (setq data (cdr data)))
7562     (when article
7563       (gnus-summary-goto-subject article))
7564     (gnus-summary-position-point)
7565     article))
7566
7567 (defun gnus-summary-last-subject ()
7568   "Go to the last displayed subject line in the group."
7569   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7570     (when article
7571       (gnus-summary-goto-subject article))))
7572
7573 (defun gnus-summary-goto-article (article &optional all-headers force)
7574   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7575 If ALL-HEADERS is non-nil, no header lines are hidden.
7576 If FORCE, go to the article even if it isn't displayed.  If FORCE
7577 is a number, it is the line the article is to be displayed on."
7578   (interactive
7579    (list
7580     (completing-read
7581      "Article number or Message-ID: "
7582      (mapcar (lambda (number) (list (int-to-string number)))
7583              gnus-newsgroup-limit))
7584     current-prefix-arg
7585     t))
7586   (prog1
7587       (if (and (stringp article)
7588                (string-match "@\\|%40" article))
7589           (gnus-summary-refer-article article)
7590         (when (stringp article)
7591           (setq article (string-to-number article)))
7592         (if (gnus-summary-goto-subject article force)
7593             (gnus-summary-display-article article all-headers)
7594           (gnus-message 4 "Couldn't go to article %s" article) nil))
7595     (gnus-summary-position-point)))
7596
7597 (defun gnus-summary-goto-last-article ()
7598   "Go to the previously read article."
7599   (interactive)
7600   (prog1
7601       (when gnus-last-article
7602         (gnus-summary-goto-article gnus-last-article nil t))
7603     (gnus-summary-position-point)))
7604
7605 (defun gnus-summary-pop-article (number)
7606   "Pop one article off the history and go to the previous.
7607 NUMBER articles will be popped off."
7608   (interactive "p")
7609   (let (to)
7610     (setq gnus-newsgroup-history
7611           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7612     (if to
7613         (gnus-summary-goto-article (car to) nil t)
7614       (error "Article history empty")))
7615   (gnus-summary-position-point))
7616
7617 ;; Summary commands and functions for limiting the summary buffer.
7618
7619 (defun gnus-summary-limit-to-articles (n)
7620   "Limit the summary buffer to the next N articles.
7621 If not given a prefix, use the process marked articles instead."
7622   (interactive "P")
7623   (prog1
7624       (let ((articles (gnus-summary-work-articles n)))
7625         (setq gnus-newsgroup-processable nil)
7626         (gnus-summary-limit articles))
7627     (gnus-summary-position-point)))
7628
7629 (defun gnus-summary-pop-limit (&optional total)
7630   "Restore the previous limit.
7631 If given a prefix, remove all limits."
7632   (interactive "P")
7633   (when total
7634     (setq gnus-newsgroup-limits
7635           (list (mapcar (lambda (h) (mail-header-number h))
7636                         gnus-newsgroup-headers))))
7637   (unless gnus-newsgroup-limits
7638     (error "No limit to pop"))
7639   (prog1
7640       (gnus-summary-limit nil 'pop)
7641     (gnus-summary-position-point)))
7642
7643 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7644   "Limit the summary buffer to articles that have subjects that match a regexp.
7645 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7646   (interactive
7647    (list (read-string (if current-prefix-arg
7648                           "Exclude subject (regexp): "
7649                         "Limit to subject (regexp): "))
7650          nil current-prefix-arg))
7651   (unless header
7652     (setq header "subject"))
7653   (when (not (equal "" subject))
7654     (prog1
7655         (let ((articles (gnus-summary-find-matching
7656                          (or header "subject") subject 'all nil nil
7657                          not-matching)))
7658           (unless articles
7659             (error "Found no matches for \"%s\"" subject))
7660           (gnus-summary-limit articles))
7661       (gnus-summary-position-point))))
7662
7663 (defun gnus-summary-limit-to-author (from &optional not-matching)
7664   "Limit the summary buffer to articles that have authors that match a regexp.
7665 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7666   (interactive
7667    (list (read-string (if current-prefix-arg
7668                           "Exclude author (regexp): "
7669                         "Limit to author (regexp): "))
7670          current-prefix-arg))
7671   (gnus-summary-limit-to-subject from "from" not-matching))
7672
7673 (defun gnus-summary-limit-to-age (age &optional younger-p)
7674   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7675 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7676 articles that are younger than AGE days."
7677   (interactive
7678    (let ((younger current-prefix-arg)
7679          (days-got nil)
7680          days)
7681      (while (not days-got)
7682        (setq days (if younger
7683                       (read-string "Limit to articles younger than (in days, older when negative): ")
7684                     (read-string
7685                      "Limit to articles older than (in days, younger when negative): ")))
7686        (when (> (length days) 0)
7687          (setq days (read days)))
7688        (if (numberp days)
7689            (progn
7690              (setq days-got t)
7691              (when (< days 0)
7692                (setq younger (not younger))
7693                (setq days (* days -1))))
7694          (message "Please enter a number.")
7695          (sleep-for 1)))
7696      (list days younger)))
7697   (prog1
7698       (let ((data gnus-newsgroup-data)
7699             (cutoff (days-to-time age))
7700             articles d date is-younger)
7701         (while (setq d (pop data))
7702           (when (and (vectorp (gnus-data-header d))
7703                      (setq date (mail-header-date (gnus-data-header d))))
7704             (setq is-younger (time-less-p
7705                               (time-since (condition-case ()
7706                                               (date-to-time date)
7707                                             (error '(0 0))))
7708                               cutoff))
7709             (when (if younger-p
7710                       is-younger
7711                     (not is-younger))
7712               (push (gnus-data-number d) articles))))
7713         (gnus-summary-limit (nreverse articles)))
7714     (gnus-summary-position-point)))
7715
7716 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7717   "Limit the summary buffer to articles that match an 'extra' header."
7718   (interactive
7719    (let ((header
7720           (intern
7721            (gnus-completing-read-with-default
7722             (symbol-name (car gnus-extra-headers))
7723             (if current-prefix-arg
7724                 "Exclude extra header:"
7725               "Limit extra header:")
7726             (mapcar (lambda (x)
7727                       (cons (symbol-name x) x))
7728                     gnus-extra-headers)
7729             nil
7730             t))))
7731      (list header
7732            (read-string (format "%s header %s (regexp): "
7733                                 (if current-prefix-arg "Exclude" "Limit to")
7734                                 header))
7735            current-prefix-arg)))
7736   (when (not (equal "" regexp))
7737     (prog1
7738         (let ((articles (gnus-summary-find-matching
7739                          (cons 'extra header) regexp 'all nil nil
7740                          not-matching)))
7741           (unless articles
7742             (error "Found no matches for \"%s\"" regexp))
7743           (gnus-summary-limit articles))
7744       (gnus-summary-position-point))))
7745
7746 (defun gnus-summary-limit-to-display-predicate ()
7747   "Limit the summary buffer to the predicated in the `display' group parameter."
7748   (interactive)
7749   (unless gnus-newsgroup-display
7750     (error "There is no `display' group parameter"))
7751   (let (articles)
7752     (dolist (number gnus-newsgroup-articles)
7753       (when (funcall gnus-newsgroup-display)
7754         (push number articles)))
7755     (gnus-summary-limit articles))
7756   (gnus-summary-position-point))
7757
7758 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7759 (make-obsolete
7760  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7761
7762 (defun gnus-summary-limit-to-unread (&optional all)
7763   "Limit the summary buffer to articles that are not marked as read.
7764 If ALL is non-nil, limit strictly to unread articles."
7765   (interactive "P")
7766   (if all
7767       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7768     (gnus-summary-limit-to-marks
7769      ;; Concat all the marks that say that an article is read and have
7770      ;; those removed.
7771      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7772            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7773            gnus-low-score-mark gnus-expirable-mark
7774            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7775            gnus-duplicate-mark gnus-souped-mark)
7776      'reverse)))
7777
7778 (defun gnus-summary-limit-to-replied (&optional unreplied)
7779   "Limit the summary buffer to replied articles.
7780 If UNREPLIED (the prefix), limit to unreplied articles."
7781   (interactive "P")
7782   (if unreplied
7783       (gnus-summary-limit
7784        (gnus-set-difference gnus-newsgroup-articles
7785         gnus-newsgroup-replied))
7786     (gnus-summary-limit gnus-newsgroup-replied))
7787   (gnus-summary-position-point))
7788
7789 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7790 (make-obsolete 'gnus-summary-delete-marked-with
7791                'gnus-summary-limit-exclude-marks)
7792
7793 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7794   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7795 If REVERSE, limit the summary buffer to articles that are marked
7796 with MARKS.  MARKS can either be a string of marks or a list of marks.
7797 Returns how many articles were removed."
7798   (interactive "sMarks: ")
7799   (gnus-summary-limit-to-marks marks t))
7800
7801 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7802   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7803 If REVERSE (the prefix), limit the summary buffer to articles that are
7804 not marked with MARKS.  MARKS can either be a string of marks or a
7805 list of marks.
7806 Returns how many articles were removed."
7807   (interactive "sMarks: \nP")
7808   (prog1
7809       (let ((data gnus-newsgroup-data)
7810             (marks (if (listp marks) marks
7811                      (append marks nil))) ; Transform to list.
7812             articles)
7813         (while data
7814           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7815                   (memq (gnus-data-mark (car data)) marks))
7816             (push (gnus-data-number (car data)) articles))
7817           (setq data (cdr data)))
7818         (gnus-summary-limit articles))
7819     (gnus-summary-position-point)))
7820
7821 (defun gnus-summary-limit-to-score (score)
7822   "Limit to articles with score at or above SCORE."
7823   (interactive "NLimit to articles with score of at least: ")
7824   (let ((data gnus-newsgroup-data)
7825         articles)
7826     (while data
7827       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7828                 score)
7829         (push (gnus-data-number (car data)) articles))
7830       (setq data (cdr data)))
7831     (prog1
7832         (gnus-summary-limit articles)
7833       (gnus-summary-position-point))))
7834
7835 (defun gnus-summary-limit-to-unseen ()
7836   "Limit to unseen articles."
7837   (interactive)
7838   (prog1
7839       (gnus-summary-limit gnus-newsgroup-unseen)
7840     (gnus-summary-position-point)))
7841
7842 (defun gnus-summary-limit-include-thread (id)
7843   "Display all the hidden articles that is in the thread with ID in it.
7844 When called interactively, ID is the Message-ID of the current
7845 article."
7846   (interactive (list (mail-header-id (gnus-summary-article-header))))
7847   (let ((articles (gnus-articles-in-thread
7848                    (gnus-id-to-thread (gnus-root-id id)))))
7849     (prog1
7850         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7851       (gnus-summary-limit-include-matching-articles
7852        "subject"
7853        (regexp-quote (gnus-simplify-subject-re
7854                       (mail-header-subject (gnus-id-to-header id)))))
7855       (gnus-summary-position-point))))
7856
7857 (defun gnus-summary-limit-include-matching-articles (header regexp)
7858   "Display all the hidden articles that have HEADERs that match REGEXP."
7859   (interactive (list (read-string "Match on header: ")
7860                      (read-string "Regexp: ")))
7861   (let ((articles (gnus-find-matching-articles header regexp)))
7862     (prog1
7863         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7864       (gnus-summary-position-point))))
7865
7866 (defun gnus-summary-insert-dormant-articles ()
7867   "Insert all the dormant articles for this group into the current buffer."
7868   (interactive)
7869   (let ((gnus-verbose (max 6 gnus-verbose)))
7870     (if (not gnus-newsgroup-dormant)
7871         (gnus-message 3 "No cached articles for this group")
7872       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7873
7874 (defun gnus-summary-limit-include-dormant ()
7875   "Display all the hidden articles that are marked as dormant.
7876 Note that this command only works on a subset of the articles currently
7877 fetched for this group."
7878   (interactive)
7879   (unless gnus-newsgroup-dormant
7880     (error "There are no dormant articles in this group"))
7881   (prog1
7882       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7883     (gnus-summary-position-point)))
7884
7885 (defun gnus-summary-limit-exclude-dormant ()
7886   "Hide all dormant articles."
7887   (interactive)
7888   (prog1
7889       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7890     (gnus-summary-position-point)))
7891
7892 (defun gnus-summary-limit-exclude-childless-dormant ()
7893   "Hide all dormant articles that have no children."
7894   (interactive)
7895   (let ((data (gnus-data-list t))
7896         articles d children)
7897     ;; Find all articles that are either not dormant or have
7898     ;; children.
7899     (while (setq d (pop data))
7900       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7901                 (and (setq children
7902                            (gnus-article-children (gnus-data-number d)))
7903                      (let (found)
7904                        (while children
7905                          (when (memq (car children) articles)
7906                            (setq children nil
7907                                  found t))
7908                          (pop children))
7909                        found)))
7910         (push (gnus-data-number d) articles)))
7911     ;; Do the limiting.
7912     (prog1
7913         (gnus-summary-limit articles)
7914       (gnus-summary-position-point))))
7915
7916 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7917   "Mark all unread excluded articles as read.
7918 If ALL, mark even excluded ticked and dormants as read."
7919   (interactive "P")
7920   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7921   (let ((articles (gnus-sorted-ndifference
7922                    (sort
7923                     (mapcar (lambda (h) (mail-header-number h))
7924                             gnus-newsgroup-headers)
7925                     '<)
7926                    gnus-newsgroup-limit))
7927         article)
7928     (setq gnus-newsgroup-unreads
7929           (gnus-sorted-intersection gnus-newsgroup-unreads
7930                                     gnus-newsgroup-limit))
7931     (if all
7932         (setq gnus-newsgroup-dormant nil
7933               gnus-newsgroup-marked nil
7934               gnus-newsgroup-reads
7935               (nconc
7936                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7937                gnus-newsgroup-reads))
7938       (while (setq article (pop articles))
7939         (unless (or (memq article gnus-newsgroup-dormant)
7940                     (memq article gnus-newsgroup-marked))
7941           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7942
7943 (defun gnus-summary-limit (articles &optional pop)
7944   (if pop
7945       ;; We pop the previous limit off the stack and use that.
7946       (setq articles (car gnus-newsgroup-limits)
7947             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7948     ;; We use the new limit, so we push the old limit on the stack.
7949     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7950   ;; Set the limit.
7951   (setq gnus-newsgroup-limit articles)
7952   (let ((total (length gnus-newsgroup-data))
7953         (data (gnus-data-find-list (gnus-summary-article-number)))
7954         (gnus-summary-mark-below nil)   ; Inhibit this.
7955         found)
7956     ;; This will do all the work of generating the new summary buffer
7957     ;; according to the new limit.
7958     (gnus-summary-prepare)
7959     ;; Hide any threads, possibly.
7960     (gnus-summary-maybe-hide-threads)
7961     ;; Try to return to the article you were at, or one in the
7962     ;; neighborhood.
7963     (when data
7964       ;; We try to find some article after the current one.
7965       (while data
7966         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7967           (setq data nil
7968                 found t))
7969         (setq data (cdr data))))
7970     (unless found
7971       ;; If there is no data, that means that we were after the last
7972       ;; article.  The same goes when we can't find any articles
7973       ;; after the current one.
7974       (goto-char (point-max))
7975       (gnus-summary-find-prev))
7976     (gnus-set-mode-line 'summary)
7977     ;; We return how many articles were removed from the summary
7978     ;; buffer as a result of the new limit.
7979     (- total (length gnus-newsgroup-data))))
7980
7981 (defsubst gnus-invisible-cut-children (threads)
7982   (let ((num 0))
7983     (while threads
7984       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7985         (incf num))
7986       (pop threads))
7987     (< num 2)))
7988
7989 (defsubst gnus-cut-thread (thread)
7990   "Go forwards in the thread until we find an article that we want to display."
7991   (when (or (eq gnus-fetch-old-headers 'some)
7992             (eq gnus-fetch-old-headers 'invisible)
7993             (numberp gnus-fetch-old-headers)
7994             (eq gnus-build-sparse-threads 'some)
7995             (eq gnus-build-sparse-threads 'more))
7996     ;; Deal with old-fetched headers and sparse threads.
7997     (while (and
7998             thread
7999             (or
8000              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8001              (gnus-summary-article-ancient-p
8002               (mail-header-number (car thread))))
8003             (if (or (<= (length (cdr thread)) 1)
8004                     (eq gnus-fetch-old-headers 'invisible))
8005                 (setq gnus-newsgroup-limit
8006                       (delq (mail-header-number (car thread))
8007                             gnus-newsgroup-limit)
8008                       thread (cadr thread))
8009               (when (gnus-invisible-cut-children (cdr thread))
8010                 (let ((th (cdr thread)))
8011                   (while th
8012                     (if (memq (mail-header-number (caar th))
8013                               gnus-newsgroup-limit)
8014                         (setq thread (car th)
8015                               th nil)
8016                       (setq th (cdr th))))))))))
8017   thread)
8018
8019 (defun gnus-cut-threads (threads)
8020   "Cut off all uninteresting articles from the beginning of THREADS."
8021   (when (or (eq gnus-fetch-old-headers 'some)
8022             (eq gnus-fetch-old-headers 'invisible)
8023             (numberp gnus-fetch-old-headers)
8024             (eq gnus-build-sparse-threads 'some)
8025             (eq gnus-build-sparse-threads 'more))
8026     (let ((th threads))
8027       (while th
8028         (setcar th (gnus-cut-thread (car th)))
8029         (setq th (cdr th)))))
8030   ;; Remove nixed out threads.
8031   (delq nil threads))
8032
8033 (defun gnus-summary-initial-limit (&optional show-if-empty)
8034   "Figure out what the initial limit is supposed to be on group entry.
8035 This entails weeding out unwanted dormants, low-scored articles,
8036 fetch-old-headers verbiage, and so on."
8037   ;; Most groups have nothing to remove.
8038   (if (or gnus-inhibit-limiting
8039           (and (null gnus-newsgroup-dormant)
8040                (eq gnus-newsgroup-display 'gnus-not-ignore)
8041                (not (eq gnus-fetch-old-headers 'some))
8042                (not (numberp gnus-fetch-old-headers))
8043                (not (eq gnus-fetch-old-headers 'invisible))
8044                (null gnus-summary-expunge-below)
8045                (not (eq gnus-build-sparse-threads 'some))
8046                (not (eq gnus-build-sparse-threads 'more))
8047                (null gnus-thread-expunge-below)
8048                (not gnus-use-nocem)))
8049       ()                                ; Do nothing.
8050     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8051     (setq gnus-newsgroup-limit nil)
8052     (mapatoms
8053      (lambda (node)
8054        (unless (car (symbol-value node))
8055          ;; These threads have no parents -- they are roots.
8056          (let ((nodes (cdr (symbol-value node)))
8057                thread)
8058            (while nodes
8059              (if (and gnus-thread-expunge-below
8060                       (< (gnus-thread-total-score (car nodes))
8061                          gnus-thread-expunge-below))
8062                  (gnus-expunge-thread (pop nodes))
8063                (setq thread (pop nodes))
8064                (gnus-summary-limit-children thread))))))
8065      gnus-newsgroup-dependencies)
8066     ;; If this limitation resulted in an empty group, we might
8067     ;; pop the previous limit and use it instead.
8068     (when (and (not gnus-newsgroup-limit)
8069                show-if-empty)
8070       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8071     gnus-newsgroup-limit))
8072
8073 (defun gnus-summary-limit-children (thread)
8074   "Return 1 if this subthread is visible and 0 if it is not."
8075   ;; First we get the number of visible children to this thread.  This
8076   ;; is done by recursing down the thread using this function, so this
8077   ;; will really go down to a leaf article first, before slowly
8078   ;; working its way up towards the root.
8079   (when thread
8080     (let* ((max-lisp-eval-depth 5000)
8081            (children
8082            (if (cdr thread)
8083                (apply '+ (mapcar 'gnus-summary-limit-children
8084                                  (cdr thread)))
8085              0))
8086           (number (mail-header-number (car thread)))
8087           score)
8088       (if (and
8089            (not (memq number gnus-newsgroup-marked))
8090            (or
8091             ;; If this article is dormant and has absolutely no visible
8092             ;; children, then this article isn't visible.
8093             (and (memq number gnus-newsgroup-dormant)
8094                  (zerop children))
8095             ;; If this is "fetch-old-headered" and there is no
8096             ;; visible children, then we don't want this article.
8097             (and (or (eq gnus-fetch-old-headers 'some)
8098                      (numberp gnus-fetch-old-headers))
8099                  (gnus-summary-article-ancient-p number)
8100                  (zerop children))
8101             ;; If this is "fetch-old-headered" and `invisible', then
8102             ;; we don't want this article.
8103             (and (eq gnus-fetch-old-headers 'invisible)
8104                  (gnus-summary-article-ancient-p number))
8105             ;; If this is a sparsely inserted article with no children,
8106             ;; we don't want it.
8107             (and (eq gnus-build-sparse-threads 'some)
8108                  (gnus-summary-article-sparse-p number)
8109                  (zerop children))
8110             ;; If we use expunging, and this article is really
8111             ;; low-scored, then we don't want this article.
8112             (when (and gnus-summary-expunge-below
8113                        (< (setq score
8114                                 (or (cdr (assq number gnus-newsgroup-scored))
8115                                     gnus-summary-default-score))
8116                           gnus-summary-expunge-below))
8117               ;; We increase the expunge-tally here, but that has
8118               ;; nothing to do with the limits, really.
8119               (incf gnus-newsgroup-expunged-tally)
8120               ;; We also mark as read here, if that's wanted.
8121               (when (and gnus-summary-mark-below
8122                          (< score gnus-summary-mark-below))
8123                 (setq gnus-newsgroup-unreads
8124                       (delq number gnus-newsgroup-unreads))
8125                 (if gnus-newsgroup-auto-expire
8126                     (push number gnus-newsgroup-expirable)
8127                   (push (cons number gnus-low-score-mark)
8128                         gnus-newsgroup-reads)))
8129               t)
8130             ;; Do the `display' group parameter.
8131             (and gnus-newsgroup-display
8132                  (not (funcall gnus-newsgroup-display)))
8133             ;; Check NoCeM things.
8134             (when (and gnus-use-nocem
8135                        (gnus-nocem-unwanted-article-p
8136                         (mail-header-id (car thread))))
8137               (setq gnus-newsgroup-unreads
8138                     (delq number gnus-newsgroup-unreads))
8139               t)))
8140           ;; Nope, invisible article.
8141           0
8142         ;; Ok, this article is to be visible, so we add it to the limit
8143         ;; and return 1.
8144         (push number gnus-newsgroup-limit)
8145         1))))
8146
8147 (defun gnus-expunge-thread (thread)
8148   "Mark all articles in THREAD as read."
8149   (let* ((number (mail-header-number (car thread))))
8150     (incf gnus-newsgroup-expunged-tally)
8151     ;; We also mark as read here, if that's wanted.
8152     (setq gnus-newsgroup-unreads
8153           (delq number gnus-newsgroup-unreads))
8154     (if gnus-newsgroup-auto-expire
8155         (push number gnus-newsgroup-expirable)
8156       (push (cons number gnus-low-score-mark)
8157             gnus-newsgroup-reads)))
8158   ;; Go recursively through all subthreads.
8159   (mapcar 'gnus-expunge-thread (cdr thread)))
8160
8161 ;; Summary article oriented commands
8162
8163 (defun gnus-summary-refer-parent-article (n)
8164   "Refer parent article N times.
8165 If N is negative, go to ancestor -N instead.
8166 The difference between N and the number of articles fetched is returned."
8167   (interactive "p")
8168   (let ((skip 1)
8169         error header ref)
8170     (when (not (natnump n))
8171       (setq skip (abs n)
8172             n 1))
8173     (while (and (> n 0)
8174                 (not error))
8175       (setq header (gnus-summary-article-header))
8176       (if (and (eq (mail-header-number header)
8177                    (cdr gnus-article-current))
8178                (equal gnus-newsgroup-name
8179                       (car gnus-article-current)))
8180           ;; If we try to find the parent of the currently
8181           ;; displayed article, then we take a look at the actual
8182           ;; References header, since this is slightly more
8183           ;; reliable than the References field we got from the
8184           ;; server.
8185           (save-excursion
8186             (set-buffer gnus-original-article-buffer)
8187             (nnheader-narrow-to-headers)
8188             (unless (setq ref (message-fetch-field "references"))
8189               (when (setq ref (message-fetch-field "in-reply-to"))
8190                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8191             (widen))
8192         (setq ref
8193               ;; It's not the current article, so we take a bet on
8194               ;; the value we got from the server.
8195               (mail-header-references header)))
8196       (if (and ref
8197                (not (equal ref "")))
8198           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8199             (gnus-message 1 "Couldn't find parent"))
8200         (gnus-message 1 "No references in article %d"
8201                       (gnus-summary-article-number))
8202         (setq error t))
8203       (decf n))
8204     (gnus-summary-position-point)
8205     n))
8206
8207 (defun gnus-summary-refer-references ()
8208   "Fetch all articles mentioned in the References header.
8209 Return the number of articles fetched."
8210   (interactive)
8211   (let ((ref (mail-header-references (gnus-summary-article-header)))
8212         (current (gnus-summary-article-number))
8213         (n 0))
8214     (if (or (not ref)
8215             (equal ref ""))
8216         (error "No References in the current article")
8217       ;; For each Message-ID in the References header...
8218       (while (string-match "<[^>]*>" ref)
8219         (incf n)
8220         ;; ... fetch that article.
8221         (gnus-summary-refer-article
8222          (prog1 (match-string 0 ref)
8223            (setq ref (substring ref (match-end 0))))))
8224       (gnus-summary-goto-subject current)
8225       (gnus-summary-position-point)
8226       n)))
8227
8228 (defun gnus-summary-refer-thread (&optional limit)
8229   "Fetch all articles in the current thread.
8230 If LIMIT (the numerical prefix), fetch that many old headers instead
8231 of what's specified by the `gnus-refer-thread-limit' variable."
8232   (interactive "P")
8233   (let ((id (mail-header-id (gnus-summary-article-header)))
8234         (limit (if limit (prefix-numeric-value limit)
8235                  gnus-refer-thread-limit)))
8236     (unless (eq gnus-fetch-old-headers 'invisible)
8237       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8238       ;; Retrieve the headers and read them in.
8239       (if (eq (if (numberp limit)
8240                   (gnus-retrieve-headers
8241                    (list (min
8242                           (+ (mail-header-number
8243                               (gnus-summary-article-header))
8244                              limit)
8245                           gnus-newsgroup-end))
8246                    gnus-newsgroup-name (* limit 2))
8247                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8248                 ;; headers.
8249                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8250                                        gnus-newsgroup-name limit))
8251               'nov)
8252           (gnus-build-all-threads)
8253         (error "Can't fetch thread from back ends that don't support NOV"))
8254       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8255     (gnus-summary-limit-include-thread id)))
8256
8257 (defun gnus-summary-refer-article (message-id)
8258   "Fetch an article specified by MESSAGE-ID."
8259   (interactive "sMessage-ID: ")
8260   (when (and (stringp message-id)
8261              (not (zerop (length message-id))))
8262     (setq message-id (gnus-replace-in-string message-id " " ""))
8263     ;; Construct the correct Message-ID if necessary.
8264     ;; Suggested by tale@pawl.rpi.edu.
8265     (unless (string-match "^<" message-id)
8266       (setq message-id (concat "<" message-id)))
8267     (unless (string-match ">$" message-id)
8268       (setq message-id (concat message-id ">")))
8269     ;; People often post MIDs from URLs, so unhex it:
8270     (unless (string-match "@" message-id)
8271       (setq message-id (gnus-url-unhex-string message-id)))
8272     (let* ((header (gnus-id-to-header message-id))
8273            (sparse (and header
8274                         (gnus-summary-article-sparse-p
8275                          (mail-header-number header))
8276                         (memq (mail-header-number header)
8277                               gnus-newsgroup-limit)))
8278            number)
8279       (cond
8280        ;; If the article is present in the buffer we just go to it.
8281        ((and header
8282              (or (not (gnus-summary-article-sparse-p
8283                        (mail-header-number header)))
8284                  sparse))
8285         (prog1
8286             (gnus-summary-goto-article
8287              (mail-header-number header) nil t)
8288           (when sparse
8289             (gnus-summary-update-article (mail-header-number header)))))
8290        (t
8291         ;; We fetch the article.
8292         (catch 'found
8293           (dolist (gnus-override-method (gnus-refer-article-methods))
8294             (when (and (gnus-check-server gnus-override-method)
8295                        ;; Fetch the header,
8296                        (setq number (gnus-summary-insert-subject message-id)))
8297               ;; and display the article.
8298               (gnus-summary-select-article nil nil nil number)
8299               (throw 'found t)))
8300           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8301
8302 (defun gnus-refer-article-methods ()
8303   "Return a list of referable methods."
8304   (cond
8305    ;; No method, so we default to current and native.
8306    ((null gnus-refer-article-method)
8307     (list gnus-current-select-method gnus-select-method))
8308    ;; Current.
8309    ((eq 'current gnus-refer-article-method)
8310     (list gnus-current-select-method))
8311    ;; List of select methods.
8312    ((not (and (symbolp (car gnus-refer-article-method))
8313               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8314     (let (out)
8315       (dolist (method gnus-refer-article-method)
8316         (push (if (eq 'current method)
8317                   gnus-current-select-method
8318                 method)
8319               out))
8320       (nreverse out)))
8321    ;; One single select method.
8322    (t
8323     (list gnus-refer-article-method))))
8324
8325 (defun gnus-summary-edit-parameters ()
8326   "Edit the group parameters of the current group."
8327   (interactive)
8328   (gnus-group-edit-group gnus-newsgroup-name 'params))
8329
8330 (defun gnus-summary-customize-parameters ()
8331   "Customize the group parameters of the current group."
8332   (interactive)
8333   (gnus-group-customize gnus-newsgroup-name))
8334
8335 (defun gnus-summary-enter-digest-group (&optional force)
8336   "Enter an nndoc group based on the current article.
8337 If FORCE, force a digest interpretation.  If not, try
8338 to guess what the document format is."
8339   (interactive "P")
8340   (let ((conf gnus-current-window-configuration))
8341     (save-window-excursion
8342       (save-excursion
8343         (let (gnus-article-prepare-hook
8344               gnus-display-mime-function
8345               gnus-break-pages)
8346           (gnus-summary-select-article))))
8347     (setq gnus-current-window-configuration conf)
8348     (let* ((name (format "%s-%d"
8349                          (gnus-group-prefixed-name
8350                           gnus-newsgroup-name (list 'nndoc ""))
8351                          (with-current-buffer gnus-summary-buffer
8352                            gnus-current-article)))
8353            (ogroup gnus-newsgroup-name)
8354            (params (append (gnus-info-params (gnus-get-info ogroup))
8355                            (list (cons 'to-group ogroup))
8356                            (list (cons 'parent-group ogroup))
8357                            (list (cons 'save-article-group ogroup))))
8358            (case-fold-search t)
8359            (buf (current-buffer))
8360            dig to-address)
8361       (save-excursion
8362         (set-buffer gnus-original-article-buffer)
8363         ;; Have the digest group inherit the main mail address of
8364         ;; the parent article.
8365         (when (setq to-address (or (gnus-fetch-field "reply-to")
8366                                    (gnus-fetch-field "from")))
8367           (setq params (append
8368                         (list (cons 'to-address
8369                                     (funcall gnus-decode-encoded-word-function
8370                                              to-address))))))
8371         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8372         (insert-buffer-substring gnus-original-article-buffer)
8373         ;; Remove lines that may lead nndoc to misinterpret the
8374         ;; document type.
8375         (narrow-to-region
8376          (goto-char (point-min))
8377          (or (search-forward "\n\n" nil t) (point)))
8378         (goto-char (point-min))
8379         (delete-matching-lines "^Path:\\|^From ")
8380         (widen))
8381       (unwind-protect
8382           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8383                     (gnus-newsgroup-ephemeral-ignored-charsets
8384                      gnus-newsgroup-ignored-charsets))
8385                 (gnus-group-read-ephemeral-group
8386                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8387                               (nndoc-article-type
8388                                ,(if force 'mbox 'guess)))
8389                  t nil nil nil
8390                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8391                                                         "ADAPT")))))
8392               ;; Make all postings to this group go to the parent group.
8393               (nconc (gnus-info-params (gnus-get-info name))
8394                      params)
8395             ;; Couldn't select this doc group.
8396             (switch-to-buffer buf)
8397             (gnus-set-global-variables)
8398             (gnus-configure-windows 'summary)
8399             (gnus-message 3 "Article couldn't be entered?"))
8400         (kill-buffer dig)))))
8401
8402 (defun gnus-summary-read-document (n)
8403   "Open a new group based on the current article(s).
8404 This will allow you to read digests and other similar
8405 documents as newsgroups.
8406 Obeys the standard process/prefix convention."
8407   (interactive "P")
8408   (let* ((ogroup gnus-newsgroup-name)
8409          (params (append (gnus-info-params (gnus-get-info ogroup))
8410                          (list (cons 'to-group ogroup))))
8411          group egroup groups vgroup)
8412     (dolist (article (gnus-summary-work-articles n))
8413       (setq group (format "%s-%d" gnus-newsgroup-name article))
8414       (gnus-summary-remove-process-mark article)
8415       (when (gnus-summary-display-article article)
8416         (save-excursion
8417           (with-temp-buffer
8418             (insert-buffer-substring gnus-original-article-buffer)
8419             ;; Remove some headers that may lead nndoc to make
8420             ;; the wrong guess.
8421             (message-narrow-to-head)
8422             (goto-char (point-min))
8423             (delete-matching-lines "^Path:\\|^From ")
8424             (widen)
8425             (if (setq egroup
8426                       (gnus-group-read-ephemeral-group
8427                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8428                                      (nndoc-article-type guess))
8429                        t nil t))
8430                 (progn
8431             ;; Make all postings to this group go to the parent group.
8432                   (nconc (gnus-info-params (gnus-get-info egroup))
8433                          params)
8434                   (push egroup groups))
8435               ;; Couldn't select this doc group.
8436               (gnus-error 3 "Article couldn't be entered"))))))
8437     ;; Now we have selected all the documents.
8438     (cond
8439      ((not groups)
8440       (error "None of the articles could be interpreted as documents"))
8441      ((gnus-group-read-ephemeral-group
8442        (setq vgroup (format
8443                      "nnvirtual:%s-%s" gnus-newsgroup-name
8444                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8445        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8446        t
8447        (cons (current-buffer) 'summary)))
8448      (t
8449       (error "Couldn't select virtual nndoc group")))))
8450
8451 (defun gnus-summary-isearch-article (&optional regexp-p)
8452   "Do incremental search forward on the current article.
8453 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8454   (interactive "P")
8455   (gnus-summary-select-article)
8456   (gnus-configure-windows 'article)
8457   (gnus-eval-in-buffer-window gnus-article-buffer
8458     (save-restriction
8459       (widen)
8460       (isearch-forward regexp-p))))
8461
8462 (defun gnus-summary-search-article-forward (regexp &optional backward)
8463   "Search for an article containing REGEXP forward.
8464 If BACKWARD, search backward instead."
8465   (interactive
8466    (list (read-string
8467           (format "Search article %s (regexp%s): "
8468                   (if current-prefix-arg "backward" "forward")
8469                   (if gnus-last-search-regexp
8470                       (concat ", default " gnus-last-search-regexp)
8471                     "")))
8472          current-prefix-arg))
8473   (if (string-equal regexp "")
8474       (setq regexp (or gnus-last-search-regexp ""))
8475     (setq gnus-last-search-regexp regexp)
8476     (setq gnus-article-before-search gnus-current-article))
8477   ;; Intentionally set gnus-last-article.
8478   (setq gnus-last-article gnus-article-before-search)
8479   (let ((gnus-last-article gnus-last-article))
8480     (if (gnus-summary-search-article regexp backward)
8481         (gnus-summary-show-thread)
8482       (signal 'search-failed (list regexp)))))
8483
8484 (defun gnus-summary-search-article-backward (regexp)
8485   "Search for an article containing REGEXP backward."
8486   (interactive
8487    (list (read-string
8488           (format "Search article backward (regexp%s): "
8489                   (if gnus-last-search-regexp
8490                       (concat ", default " gnus-last-search-regexp)
8491                     "")))))
8492   (gnus-summary-search-article-forward regexp 'backward))
8493
8494 (defun gnus-summary-search-article (regexp &optional backward)
8495   "Search for an article containing REGEXP.
8496 Optional argument BACKWARD means do search for backward.
8497 `gnus-select-article-hook' is not called during the search."
8498   ;; We have to require this here to make sure that the following
8499   ;; dynamic binding isn't shadowed by autoloading.
8500   (require 'gnus-async)
8501   (require 'gnus-art)
8502   (let ((gnus-select-article-hook nil)  ;Disable hook.
8503         (gnus-article-prepare-hook nil)
8504         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8505         (gnus-use-article-prefetch nil)
8506         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8507         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8508         (gnus-visual nil)
8509         (gnus-keep-backlog nil)
8510         (gnus-break-pages nil)
8511         (gnus-summary-display-arrow nil)
8512         (gnus-updated-mode-lines nil)
8513         (gnus-auto-center-summary nil)
8514         (sum (current-buffer))
8515         (gnus-display-mime-function nil)
8516         (found nil)
8517         point)
8518     (gnus-save-hidden-threads
8519       (gnus-summary-select-article)
8520       (set-buffer gnus-article-buffer)
8521       (goto-char (window-point (get-buffer-window (current-buffer))))
8522       (when backward
8523         (forward-line -1))
8524       (while (not found)
8525         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8526         (if (if backward
8527                 (re-search-backward regexp nil t)
8528               (re-search-forward regexp nil t))
8529             ;; We found the regexp.
8530             (progn
8531               (setq found 'found)
8532               (beginning-of-line)
8533               (set-window-start
8534                (get-buffer-window (current-buffer))
8535                (point))
8536               (forward-line 1)
8537               (set-window-point
8538                (get-buffer-window (current-buffer))
8539                (point))
8540               (set-buffer sum)
8541               (setq point (point)))
8542           ;; We didn't find it, so we go to the next article.
8543           (set-buffer sum)
8544           (setq found 'not)
8545           (while (eq found 'not)
8546             (if (not (if backward (gnus-summary-find-prev)
8547                        (gnus-summary-find-next)))
8548                 ;; No more articles.
8549                 (setq found t)
8550               ;; Select the next article and adjust point.
8551               (unless (gnus-summary-article-sparse-p
8552                        (gnus-summary-article-number))
8553                 (setq found nil)
8554                 (gnus-summary-select-article)
8555                 (set-buffer gnus-article-buffer)
8556                 (widen)
8557                 (goto-char (if backward (point-max) (point-min))))))))
8558       (gnus-message 7 ""))
8559     ;; Return whether we found the regexp.
8560     (when (eq found 'found)
8561       (goto-char point)
8562       (gnus-summary-show-thread)
8563       (gnus-summary-goto-subject gnus-current-article)
8564       (gnus-summary-position-point)
8565       t)))
8566
8567 (defun gnus-find-matching-articles (header regexp)
8568   "Return a list of all articles that match REGEXP on HEADER.
8569 This search includes all articles in the current group that Gnus has
8570 fetched headers for, whether they are displayed or not."
8571   (let ((articles nil)
8572         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8573         (case-fold-search t))
8574     (dolist (header gnus-newsgroup-headers)
8575       (when (string-match regexp (funcall func header))
8576         (push (mail-header-number header) articles)))
8577     (nreverse articles)))
8578
8579 (defun gnus-summary-find-matching (header regexp &optional backward unread
8580                                           not-case-fold not-matching)
8581   "Return a list of all articles that match REGEXP on HEADER.
8582 The search stars on the current article and goes forwards unless
8583 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8584 If UNREAD is non-nil, only unread articles will
8585 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8586 in the comparisons. If NOT-MATCHING, return a list of all articles that
8587 not match REGEXP on HEADER."
8588   (let ((case-fold-search (not not-case-fold))
8589         articles d func)
8590     (if (consp header)
8591         (if (eq (car header) 'extra)
8592             (setq func
8593                   `(lambda (h)
8594                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8595                          "")))
8596           (error "%s is an invalid header" header))
8597       (unless (fboundp (intern (concat "mail-header-" header)))
8598         (error "%s is not a valid header" header))
8599       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8600     (dolist (d (if (eq backward 'all)
8601                    gnus-newsgroup-data
8602                  (gnus-data-find-list
8603                   (gnus-summary-article-number)
8604                   (gnus-data-list backward))))
8605       (when (and (or (not unread)       ; We want all articles...
8606                      (gnus-data-unread-p d)) ; Or just unreads.
8607                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8608                  (if not-matching
8609                      (not (string-match
8610                            regexp
8611                            (funcall func (gnus-data-header d))))
8612                    (string-match regexp
8613                                  (funcall func (gnus-data-header d)))))
8614         (push (gnus-data-number d) articles))) ; Success!
8615     (nreverse articles)))
8616
8617 (defun gnus-summary-execute-command (header regexp command &optional backward)
8618   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8619 If HEADER is an empty string (or nil), the match is done on the entire
8620 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8621   (interactive
8622    (list (let ((completion-ignore-case t))
8623            (completing-read
8624             "Header name: "
8625             (mapcar (lambda (header) (list (format "%s" header)))
8626                     (append
8627                      '("Number" "Subject" "From" "Lines" "Date"
8628                        "Message-ID" "Xref" "References" "Body")
8629                      gnus-extra-headers))
8630             nil 'require-match))
8631          (read-string "Regexp: ")
8632          (read-key-sequence "Command: ")
8633          current-prefix-arg))
8634   (when (equal header "Body")
8635     (setq header ""))
8636   ;; Hidden thread subtrees must be searched as well.
8637   (gnus-summary-show-all-threads)
8638   ;; We don't want to change current point nor window configuration.
8639   (save-excursion
8640     (save-window-excursion
8641       (let (gnus-visual
8642             gnus-treat-strip-trailing-blank-lines
8643             gnus-treat-strip-leading-blank-lines
8644             gnus-treat-strip-multiple-blank-lines
8645             gnus-treat-hide-boring-headers
8646             gnus-treat-fold-newsgroups
8647             gnus-article-prepare-hook)
8648         (gnus-message 6 "Executing %s..." (key-description command))
8649         ;; We'd like to execute COMMAND interactively so as to give arguments.
8650         (gnus-execute header regexp
8651                       `(call-interactively ',(key-binding command))
8652                       backward)
8653         (gnus-message 6 "Executing %s...done" (key-description command))))))
8654
8655 (defun gnus-summary-beginning-of-article ()
8656   "Scroll the article back to the beginning."
8657   (interactive)
8658   (gnus-summary-select-article)
8659   (gnus-configure-windows 'article)
8660   (gnus-eval-in-buffer-window gnus-article-buffer
8661     (widen)
8662     (goto-char (point-min))
8663     (when gnus-break-pages
8664       (gnus-narrow-to-page))))
8665
8666 (defun gnus-summary-end-of-article ()
8667   "Scroll to the end of the article."
8668   (interactive)
8669   (gnus-summary-select-article)
8670   (gnus-configure-windows 'article)
8671   (gnus-eval-in-buffer-window gnus-article-buffer
8672     (widen)
8673     (goto-char (point-max))
8674     (recenter -3)
8675     (when gnus-break-pages
8676       (when (re-search-backward page-delimiter nil t)
8677         (narrow-to-region (match-end 0) (point-max)))
8678       (gnus-narrow-to-page))))
8679
8680 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8681   "Truncate to LEN and quote all \"(\"'s in STRING."
8682   (gnus-replace-in-string (if (and len (> (length string) len))
8683                               (substring string 0 len)
8684                             string)
8685                           "[()]" "\\\\\\&"))
8686
8687 (defun gnus-summary-print-article (&optional filename n)
8688   "Generate and print a PostScript image of the process-marked (mail) articles.
8689
8690 If used interactively, print the current article if none are
8691 process-marked.  With prefix arg, prompt the user for the name of the
8692 file to save in.
8693
8694 When used from Lisp, accept two optional args FILENAME and N.  N means
8695 to print the next N articles.  If N is negative, print the N previous
8696 articles.  If N is nil and articles have been marked with the process
8697 mark, print these instead.
8698
8699 If the optional first argument FILENAME is nil, send the image to the
8700 printer.  If FILENAME is a string, save the PostScript image in a file with
8701 that name.  If FILENAME is a number, prompt the user for the name of the file
8702 to save in."
8703   (interactive (list (ps-print-preprint current-prefix-arg)))
8704   (dolist (article (gnus-summary-work-articles n))
8705     (gnus-summary-select-article nil nil 'pseudo article)
8706     (gnus-eval-in-buffer-window gnus-article-buffer
8707       (gnus-print-buffer))
8708     (gnus-summary-remove-process-mark article))
8709   (ps-despool filename))
8710
8711 (defun gnus-print-buffer ()
8712   (let ((buffer (generate-new-buffer " *print*")))
8713     (unwind-protect
8714         (progn
8715           (copy-to-buffer buffer (point-min) (point-max))
8716           (set-buffer buffer)
8717           (gnus-remove-text-with-property 'gnus-decoration)
8718           (when (gnus-visual-p 'article-highlight 'highlight)
8719             ;; Copy-to-buffer doesn't copy overlay.  So redo
8720             ;; highlight.
8721             (let ((gnus-article-buffer buffer))
8722               (gnus-article-highlight-citation t)
8723               (gnus-article-highlight-signature)
8724               (gnus-article-emphasize)
8725               (gnus-article-delete-invisible-text)))
8726           (let ((ps-left-header
8727                  (list
8728                   (concat "("
8729                           (gnus-summary-print-truncate-and-quote
8730                            (mail-header-subject gnus-current-headers)
8731                            66) ")")
8732                   (concat "("
8733                           (gnus-summary-print-truncate-and-quote
8734                            (mail-header-from gnus-current-headers)
8735                            45) ")")))
8736                 (ps-right-header
8737                  (list
8738                   "/pagenumberstring load"
8739                   (concat "("
8740                           (mail-header-date gnus-current-headers) ")"))))
8741             (gnus-run-hooks 'gnus-ps-print-hook)
8742             (save-excursion
8743               (if window-system
8744                   (ps-spool-buffer-with-faces)
8745                 (ps-spool-buffer)))))
8746       (kill-buffer buffer))))
8747
8748 (defun gnus-summary-show-article (&optional arg)
8749   "Force redisplaying of the current article.
8750 If ARG (the prefix) is a number, show the article with the charset
8751 defined in `gnus-summary-show-article-charset-alist', or the charset
8752 input.
8753 If ARG (the prefix) is non-nil and not a number, show the raw article
8754 without any article massaging functions being run.  Normally, the key
8755 strokes are `C-u g'."
8756   (interactive "P")
8757   (cond
8758    ((numberp arg)
8759     (gnus-summary-show-article t)
8760     (let ((gnus-newsgroup-charset
8761            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8762                (mm-read-coding-system
8763                 "View as charset: " ;; actually it is coding system.
8764                 (with-current-buffer gnus-article-buffer
8765                   (mm-detect-coding-region (point) (point-max))))))
8766           (gnus-newsgroup-ignored-charsets 'gnus-all))
8767       (gnus-summary-select-article nil 'force)
8768       (let ((deps gnus-newsgroup-dependencies)
8769             head header lines)
8770         (save-excursion
8771           (set-buffer gnus-original-article-buffer)
8772           (save-restriction
8773             (message-narrow-to-head)
8774             (setq head (buffer-string))
8775             (goto-char (point-min))
8776             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8777               (goto-char (point-max))
8778               (widen)
8779               (setq lines (1- (count-lines (point) (point-max))))))
8780           (with-temp-buffer
8781             (insert (format "211 %d Article retrieved.\n"
8782                             (cdr gnus-article-current)))
8783             (insert head)
8784             (if lines (insert (format "Lines: %d\n" lines)))
8785             (insert ".\n")
8786             (let ((nntp-server-buffer (current-buffer)))
8787               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8788         (gnus-data-set-header
8789          (gnus-data-find (cdr gnus-article-current))
8790          header)
8791         (gnus-summary-update-article-line
8792          (cdr gnus-article-current) header)
8793         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8794           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8795    ((not arg)
8796     ;; Select the article the normal way.
8797     (gnus-summary-select-article nil 'force))
8798    (t
8799     ;; We have to require this here to make sure that the following
8800     ;; dynamic binding isn't shadowed by autoloading.
8801     (require 'gnus-async)
8802     (require 'gnus-art)
8803     ;; Bind the article treatment functions to nil.
8804     (let ((gnus-have-all-headers t)
8805           gnus-article-prepare-hook
8806           gnus-article-decode-hook
8807           gnus-display-mime-function
8808           gnus-break-pages)
8809       ;; Destroy any MIME parts.
8810       (when (gnus-buffer-live-p gnus-article-buffer)
8811         (save-excursion
8812           (set-buffer gnus-article-buffer)
8813           (mm-destroy-parts gnus-article-mime-handles)
8814           ;; Set it to nil for safety reason.
8815           (setq gnus-article-mime-handle-alist nil)
8816           (setq gnus-article-mime-handles nil)))
8817       (gnus-summary-select-article nil 'force))))
8818   (gnus-summary-goto-subject gnus-current-article)
8819   (gnus-summary-position-point))
8820
8821 (defun gnus-summary-show-raw-article ()
8822   "Show the raw article without any article massaging functions being run."
8823   (interactive)
8824   (gnus-summary-show-article t))
8825
8826 (defun gnus-summary-verbose-headers (&optional arg)
8827   "Toggle permanent full header display.
8828 If ARG is a positive number, turn header display on.
8829 If ARG is a negative number, turn header display off."
8830   (interactive "P")
8831   (setq gnus-show-all-headers
8832         (cond ((or (not (numberp arg))
8833                    (zerop arg))
8834                (not gnus-show-all-headers))
8835               ((natnump arg)
8836                t)))
8837   (gnus-summary-show-article))
8838
8839 (defun gnus-summary-toggle-header (&optional arg)
8840   "Show the headers if they are hidden, or hide them if they are shown.
8841 If ARG is a positive number, show the entire header.
8842 If ARG is a negative number, hide the unwanted header lines."
8843   (interactive "P")
8844   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8845                      (get-buffer-window gnus-article-buffer t))))
8846     (with-current-buffer gnus-article-buffer
8847       (widen)
8848       (article-narrow-to-head)
8849       (let* ((buffer-read-only nil)
8850              (inhibit-point-motion-hooks t)
8851              (hidden (if (numberp arg)
8852                          (>= arg 0)
8853                        (or (not (looking-at "[^ \t\n]+:"))
8854                            (gnus-article-hidden-text-p 'headers))))
8855              s e)
8856         (delete-region (point-min) (point-max))
8857         (with-current-buffer gnus-original-article-buffer
8858           (goto-char (setq s (point-min)))
8859           (setq e (if (search-forward "\n\n" nil t)
8860                       (1- (point))
8861                     (point-max))))
8862         (insert-buffer-substring gnus-original-article-buffer s e)
8863         (run-hooks 'gnus-article-decode-hook)
8864         (if hidden
8865             (let ((gnus-treat-hide-headers nil)
8866                   (gnus-treat-hide-boring-headers nil))
8867               (gnus-delete-wash-type 'headers)
8868               (gnus-treat-article 'head))
8869           (gnus-treat-article 'head))
8870         (widen)
8871         (if window
8872             (set-window-start window (goto-char (point-min))))
8873         (if gnus-break-pages
8874             (gnus-narrow-to-page)
8875           (when (gnus-visual-p 'page-marker)
8876             (let ((buffer-read-only nil))
8877               (gnus-remove-text-with-property 'gnus-prev)
8878               (gnus-remove-text-with-property 'gnus-next))))
8879         (gnus-set-mode-line 'article)))))
8880
8881 (defun gnus-summary-show-all-headers ()
8882   "Make all header lines visible."
8883   (interactive)
8884   (gnus-summary-toggle-header 1))
8885
8886 (defun gnus-summary-caesar-message (&optional arg)
8887   "Caesar rotate the current article by 13.
8888 The numerical prefix specifies how many places to rotate each letter
8889 forward."
8890   (interactive "P")
8891   (gnus-summary-select-article)
8892   (let ((mail-header-separator ""))
8893     (gnus-eval-in-buffer-window gnus-article-buffer
8894       (save-restriction
8895         (widen)
8896         (let ((start (window-start))
8897               buffer-read-only)
8898           (message-caesar-buffer-body arg)
8899           (set-window-start (get-buffer-window (current-buffer)) start))))))
8900
8901 (autoload 'unmorse-region "morse"
8902   "Convert morse coded text in region to ordinary ASCII text."
8903   t)
8904
8905 (defun gnus-summary-morse-message (&optional arg)
8906   "Morse decode the current article."
8907   (interactive "P")
8908   (gnus-summary-select-article)
8909   (let ((mail-header-separator ""))
8910     (gnus-eval-in-buffer-window gnus-article-buffer
8911       (save-excursion
8912         (save-restriction
8913           (widen)
8914           (let ((pos (window-start))
8915                 buffer-read-only)
8916             (goto-char (point-min))
8917             (when (message-goto-body)
8918               (gnus-narrow-to-body))
8919             (goto-char (point-min))
8920             (while (search-forward "·" (point-max) t)
8921               (replace-match "."))
8922             (unmorse-region (point-min) (point-max))
8923             (widen)
8924             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8925
8926 (defun gnus-summary-stop-page-breaking ()
8927   "Stop page breaking in the current article."
8928   (interactive)
8929   (gnus-summary-select-article)
8930   (gnus-eval-in-buffer-window gnus-article-buffer
8931     (widen)
8932     (when (gnus-visual-p 'page-marker)
8933       (let ((buffer-read-only nil))
8934         (gnus-remove-text-with-property 'gnus-prev)
8935         (gnus-remove-text-with-property 'gnus-next))
8936       (setq gnus-page-broken nil))))
8937
8938 (defun gnus-summary-move-article (&optional n to-newsgroup
8939                                             select-method action)
8940   "Move the current article to a different newsgroup.
8941 If N is a positive number, move the N next articles.
8942 If N is a negative number, move the N previous articles.
8943 If N is nil and any articles have been marked with the process mark,
8944 move those articles instead.
8945 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8946 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8947 re-spool using this method.
8948
8949 When called interactively with TO-NEWSGROUP being nil, the value of
8950 the variable `gnus-move-split-methods' is used for finding a default
8951 for the target newsgroup.
8952
8953 For this function to work, both the current newsgroup and the
8954 newsgroup that you want to move to have to support the `request-move'
8955 and `request-accept' functions.
8956
8957 ACTION can be either `move' (the default), `crosspost' or `copy'."
8958   (interactive "P")
8959   (unless action
8960     (setq action 'move))
8961   ;; Check whether the source group supports the required functions.
8962   (cond ((and (eq action 'move)
8963               (not (gnus-check-backend-function
8964                     'request-move-article gnus-newsgroup-name)))
8965          (error "The current group does not support article moving"))
8966         ((and (eq action 'crosspost)
8967               (not (gnus-check-backend-function
8968                     'request-replace-article gnus-newsgroup-name)))
8969          (error "The current group does not support article editing")))
8970   (let ((articles (gnus-summary-work-articles n))
8971         (prefix (if (gnus-check-backend-function
8972                      'request-move-article gnus-newsgroup-name)
8973                     (funcall gnus-move-group-prefix-function
8974                              gnus-newsgroup-name)
8975                   ""))
8976         (names '((move "Move" "Moving")
8977                  (copy "Copy" "Copying")
8978                  (crosspost "Crosspost" "Crossposting")))
8979         (copy-buf (save-excursion
8980                     (nnheader-set-temp-buffer " *copy article*")))
8981         art-group to-method new-xref article to-groups)
8982     (unless (assq action names)
8983       (error "Unknown action %s" action))
8984     ;; Read the newsgroup name.
8985     (when (and (not to-newsgroup)
8986                (not select-method))
8987       (if (and gnus-move-split-methods
8988                (not
8989                 (and (memq gnus-current-article articles)
8990                      (gnus-buffer-live-p gnus-original-article-buffer))))
8991           ;; When `gnus-move-split-methods' is non-nil, we have to
8992           ;; select an article to give `gnus-read-move-group-name' an
8993           ;; opportunity to suggest an appropriate default.  However,
8994           ;; we needn't render or mark the article.
8995           (let ((gnus-display-mime-function nil)
8996                 (gnus-article-prepare-hook nil)
8997                 (gnus-mark-article-hook nil))
8998             (gnus-summary-select-article nil nil nil (car articles))))
8999       (setq to-newsgroup
9000             (gnus-read-move-group-name
9001              (cadr (assq action names))
9002              (symbol-value (intern (format "gnus-current-%s-group" action)))
9003              articles prefix))
9004       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9005     (setq to-method (or select-method
9006                         (gnus-server-to-method
9007                          (gnus-group-method to-newsgroup))))
9008     ;; Check the method we are to move this article to...
9009     (unless (gnus-check-backend-function
9010              'request-accept-article (car to-method))
9011       (error "%s does not support article copying" (car to-method)))
9012     (unless (gnus-check-server to-method)
9013       (error "Can't open server %s" (car to-method)))
9014     (gnus-message 6 "%s to %s: %s..."
9015                   (caddr (assq action names))
9016                   (or (car select-method) to-newsgroup) articles)
9017     (while articles
9018       (setq article (pop articles))
9019       (setq
9020        art-group
9021        (cond
9022         ;; Move the article.
9023         ((eq action 'move)
9024          ;; Remove this article from future suppression.
9025          (gnus-dup-unsuppress-article article)
9026          (gnus-request-move-article
9027           article                       ; Article to move
9028           gnus-newsgroup-name           ; From newsgroup
9029           (nth 1 (gnus-find-method-for-group
9030                   gnus-newsgroup-name)) ; Server
9031           (list 'gnus-request-accept-article
9032                 to-newsgroup (list 'quote select-method)
9033                 (not articles) t)       ; Accept form
9034           (not articles)))              ; Only save nov last time
9035         ;; Copy the article.
9036         ((eq action 'copy)
9037          (save-excursion
9038            (set-buffer copy-buf)
9039            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9040              (save-restriction
9041                (nnheader-narrow-to-headers)
9042                (dolist (hdr gnus-copy-article-ignored-headers)
9043                  (message-remove-header hdr t)))
9044              (gnus-request-accept-article
9045               to-newsgroup select-method (not articles) t))))
9046         ;; Crosspost the article.
9047         ((eq action 'crosspost)
9048          (let ((xref (message-tokenize-header
9049                       (mail-header-xref (gnus-summary-article-header article))
9050                       " ")))
9051            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9052                                   ":" (number-to-string article)))
9053            (unless xref
9054              (setq xref (list (system-name))))
9055            (setq new-xref
9056                  (concat
9057                   (mapconcat 'identity
9058                              (delete "Xref:" (delete new-xref xref))
9059                              " ")
9060                   " " new-xref))
9061            (save-excursion
9062              (set-buffer copy-buf)
9063              ;; First put the article in the destination group.
9064              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9065              (when (consp (setq art-group
9066                                 (gnus-request-accept-article
9067                                  to-newsgroup select-method (not articles))))
9068                (setq new-xref (concat new-xref " " (car art-group)
9069                                       ":"
9070                                       (number-to-string (cdr art-group))))
9071                ;; Now we have the new Xrefs header, so we insert
9072                ;; it and replace the new article.
9073                (nnheader-replace-header "Xref" new-xref)
9074                (gnus-request-replace-article
9075                 (cdr art-group) to-newsgroup (current-buffer))
9076                art-group))))))
9077       (cond
9078        ((not art-group)
9079         (gnus-message 1 "Couldn't %s article %s: %s"
9080                       (cadr (assq action names)) article
9081                       (nnheader-get-report (car to-method))))
9082        ((eq art-group 'junk)
9083         (when (eq action 'move)
9084           (gnus-summary-mark-article article gnus-canceled-mark)
9085           (gnus-message 4 "Deleted article %s" article)
9086           ;; run the delete hook
9087           (run-hook-with-args 'gnus-summary-article-delete-hook
9088                               action
9089                               (gnus-data-header
9090                                (assoc article (gnus-data-list nil)))
9091                               gnus-newsgroup-name nil
9092                               select-method)))
9093        (t
9094         (let* ((pto-group (gnus-group-prefixed-name
9095                            (car art-group) to-method))
9096                (info (gnus-get-info pto-group))
9097                (to-group (gnus-info-group info))
9098                to-marks)
9099           ;; Update the group that has been moved to.
9100           (when (and info
9101                      (memq action '(move copy)))
9102             (unless (member to-group to-groups)
9103               (push to-group to-groups))
9104
9105             (unless (memq article gnus-newsgroup-unreads)
9106               (push 'read to-marks)
9107               (gnus-info-set-read
9108                info (gnus-add-to-range (gnus-info-read info)
9109                                        (list (cdr art-group)))))
9110
9111             ;; See whether the article is to be put in the cache.
9112             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9113                              gnus-article-mark-lists
9114                            (delete '(expirable . expire)
9115                                    (copy-sequence gnus-article-mark-lists))))
9116                   (to-article (cdr art-group)))
9117
9118               ;; Enter the article into the cache in the new group,
9119               ;; if that is required.
9120               (when gnus-use-cache
9121                 (gnus-cache-possibly-enter-article
9122                  to-group to-article
9123                  (memq article gnus-newsgroup-marked)
9124                  (memq article gnus-newsgroup-dormant)
9125                  (memq article gnus-newsgroup-unreads)))
9126
9127               (when gnus-preserve-marks
9128                 ;; Copy any marks over to the new group.
9129                 (when (and (equal to-group gnus-newsgroup-name)
9130                            (not (memq article gnus-newsgroup-unreads)))
9131                   ;; Mark this article as read in this group.
9132                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9133                   (setcdr (gnus-active to-group) to-article)
9134                   (setcdr gnus-newsgroup-active to-article))
9135
9136                 (while marks
9137                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9138                     (when (memq article (symbol-value
9139                                          (intern (format "gnus-newsgroup-%s"
9140                                                          (caar marks)))))
9141                       (push (cdar marks) to-marks)
9142                       ;; If the other group is the same as this group,
9143                       ;; then we have to add the mark to the list.
9144                       (when (equal to-group gnus-newsgroup-name)
9145                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9146                              (cons to-article
9147                                    (symbol-value
9148                                     (intern (format "gnus-newsgroup-%s"
9149                                                     (caar marks)))))))
9150                       ;; Copy the marks to other group.
9151                       (gnus-add-marked-articles
9152                        to-group (cdar marks) (list to-article) info)))
9153                   (setq marks (cdr marks)))
9154
9155                 (gnus-request-set-mark
9156                  to-group (list (list (list to-article) 'add to-marks))))
9157
9158               (gnus-dribble-enter
9159                (concat "(gnus-group-set-info '"
9160                        (gnus-prin1-to-string (gnus-get-info to-group))
9161                        ")"))))
9162
9163           ;; Update the Xref header in this article to point to
9164           ;; the new crossposted article we have just created.
9165           (when (eq action 'crosspost)
9166             (save-excursion
9167               (set-buffer copy-buf)
9168               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9169               (nnheader-replace-header "Xref" new-xref)
9170               (gnus-request-replace-article
9171                article gnus-newsgroup-name (current-buffer))))
9172
9173           ;; run the move/copy/crosspost/respool hook
9174           (run-hook-with-args 'gnus-summary-article-move-hook
9175                               action
9176                               (gnus-data-header
9177                                (assoc article (gnus-data-list nil)))
9178                               gnus-newsgroup-name
9179                               to-newsgroup
9180                               select-method))
9181
9182         ;;;!!!Why is this necessary?
9183         (set-buffer gnus-summary-buffer)
9184         
9185         (gnus-summary-goto-subject article)
9186         (when (eq action 'move)
9187           (gnus-summary-mark-article article gnus-canceled-mark))))
9188       (gnus-summary-remove-process-mark article))
9189     ;; Re-activate all groups that have been moved to.
9190     (save-excursion
9191       (set-buffer gnus-group-buffer)
9192       (let ((gnus-group-marked to-groups))
9193         (gnus-group-get-new-news-this-group nil t)))
9194
9195     (gnus-kill-buffer copy-buf)
9196     (gnus-summary-position-point)
9197     (gnus-set-mode-line 'summary)))
9198
9199 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9200   "Copy the current article to some other group.
9201 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9202 When called interactively, if TO-NEWSGROUP is nil, use the value of
9203 the variable `gnus-move-split-methods' for finding a default target
9204 newsgroup.
9205 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9206 re-spool using this method."
9207   (interactive "P")
9208   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9209
9210 (defun gnus-summary-crosspost-article (&optional n)
9211   "Crosspost the current article to some other group."
9212   (interactive "P")
9213   (gnus-summary-move-article n nil nil 'crosspost))
9214
9215 (defcustom gnus-summary-respool-default-method nil
9216   "Default method type for respooling an article.
9217 If nil, use to the current newsgroup method."
9218   :type 'symbol
9219   :group 'gnus-summary-mail)
9220
9221 (defcustom gnus-summary-display-while-building nil
9222   "If non-nil, show and update the summary buffer as it's being built.
9223 If the value is t, update the buffer after every line is inserted.  If
9224 the value is an integer (N), update the display every N lines."
9225   :group 'gnus-thread
9226   :type '(choice (const :tag "off" nil)
9227                  number
9228                  (const :tag "frequently" t)))
9229
9230 (defun gnus-summary-respool-article (&optional n method)
9231   "Respool the current article.
9232 The article will be squeezed through the mail spooling process again,
9233 which means that it will be put in some mail newsgroup or other
9234 depending on `nnmail-split-methods'.
9235 If N is a positive number, respool the N next articles.
9236 If N is a negative number, respool the N previous articles.
9237 If N is nil and any articles have been marked with the process mark,
9238 respool those articles instead.
9239
9240 Respooling can be done both from mail groups and \"real\" newsgroups.
9241 In the former case, the articles in question will be moved from the
9242 current group into whatever groups they are destined to.  In the
9243 latter case, they will be copied into the relevant groups."
9244   (interactive
9245    (list current-prefix-arg
9246          (let* ((methods (gnus-methods-using 'respool))
9247                 (methname
9248                  (symbol-name (or gnus-summary-respool-default-method
9249                                   (car (gnus-find-method-for-group
9250                                         gnus-newsgroup-name)))))
9251                 (method
9252                  (gnus-completing-read-with-default
9253                   methname "What backend do you want to use when respooling?"
9254                   methods nil t nil 'gnus-mail-method-history))
9255                 ms)
9256            (cond
9257             ((zerop (length (setq ms (gnus-servers-using-backend
9258                                       (intern method)))))
9259              (list (intern method) ""))
9260             ((= 1 (length ms))
9261              (car ms))
9262             (t
9263              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9264                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9265                            ms-alist))))))))
9266   (unless method
9267     (error "No method given for respooling"))
9268   (if (assoc (symbol-name
9269               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9270              (gnus-methods-using 'respool))
9271       (gnus-summary-move-article n nil method)
9272     (gnus-summary-copy-article n nil method)))
9273
9274 (defun gnus-summary-import-article (file &optional edit)
9275   "Import an arbitrary file into a mail newsgroup."
9276   (interactive "fImport file: \nP")
9277   (let ((group gnus-newsgroup-name)
9278         (now (current-time))
9279         atts lines group-art)
9280     (unless (gnus-check-backend-function 'request-accept-article group)
9281       (error "%s does not support article importing" group))
9282     (or (file-readable-p file)
9283         (not (file-regular-p file))
9284         (error "Can't read %s" file))
9285     (save-excursion
9286       (set-buffer (gnus-get-buffer-create " *import file*"))
9287       (erase-buffer)
9288       (nnheader-insert-file-contents file)
9289       (goto-char (point-min))
9290       (if (nnheader-article-p)
9291           (save-restriction
9292             (goto-char (point-min))
9293             (search-forward "\n\n" nil t)
9294             (narrow-to-region (point-min) (1- (point)))
9295             (goto-char (point-min))
9296             (unless (re-search-forward "^date:" nil t)
9297               (goto-char (point-max))
9298               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9299        ;; This doesn't look like an article, so we fudge some headers.
9300         (setq atts (file-attributes file)
9301               lines (count-lines (point-min) (point-max)))
9302         (insert "From: " (read-string "From: ") "\n"
9303                 "Subject: " (read-string "Subject: ") "\n"
9304                 "Date: " (message-make-date (nth 5 atts)) "\n"
9305                 "Message-ID: " (message-make-message-id) "\n"
9306                 "Lines: " (int-to-string lines) "\n"
9307                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9308       (setq group-art (gnus-request-accept-article group nil t))
9309       (kill-buffer (current-buffer)))
9310     (setq gnus-newsgroup-active (gnus-activate-group group))
9311     (forward-line 1)
9312     (gnus-summary-goto-article (cdr group-art) nil t)
9313     (when edit
9314       (gnus-summary-edit-article))))
9315
9316 (defun gnus-summary-create-article ()
9317   "Create an article in a mail newsgroup."
9318   (interactive)
9319   (let ((group gnus-newsgroup-name)
9320         (now (current-time))
9321         group-art)
9322     (unless (gnus-check-backend-function 'request-accept-article group)
9323       (error "%s does not support article importing" group))
9324     (save-excursion
9325       (set-buffer (gnus-get-buffer-create " *import file*"))
9326       (erase-buffer)
9327       (goto-char (point-min))
9328       ;; This doesn't look like an article, so we fudge some headers.
9329       (insert "From: " (read-string "From: ") "\n"
9330               "Subject: " (read-string "Subject: ") "\n"
9331               "Date: " (message-make-date now) "\n"
9332               "Message-ID: " (message-make-message-id) "\n")
9333       (setq group-art (gnus-request-accept-article group nil t))
9334       (kill-buffer (current-buffer)))
9335     (setq gnus-newsgroup-active (gnus-activate-group group))
9336     (forward-line 1)
9337     (gnus-summary-goto-article (cdr group-art) nil t)
9338     (gnus-summary-edit-article)))
9339
9340 (defun gnus-summary-article-posted-p ()
9341   "Say whether the current (mail) article is available from news as well.
9342 This will be the case if the article has both been mailed and posted."
9343   (interactive)
9344   (let ((id (mail-header-references (gnus-summary-article-header)))
9345         (gnus-override-method (car (gnus-refer-article-methods))))
9346     (if (gnus-request-head id "")
9347         (gnus-message 2 "The current message was found on %s"
9348                       gnus-override-method)
9349       (gnus-message 2 "The current message couldn't be found on %s"
9350                     gnus-override-method)
9351       nil)))
9352
9353 (defun gnus-summary-expire-articles (&optional now)
9354   "Expire all articles that are marked as expirable in the current group."
9355   (interactive)
9356   (when (and (not gnus-group-is-exiting-without-update-p)
9357              (gnus-check-backend-function
9358               'request-expire-articles gnus-newsgroup-name))
9359     ;; This backend supports expiry.
9360     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9361            (expirable (if total
9362                           (progn
9363                             ;; We need to update the info for
9364                             ;; this group for `gnus-list-of-read-articles'
9365                             ;; to give us the right answer.
9366                             (gnus-run-hooks 'gnus-exit-group-hook)
9367                             (gnus-summary-update-info)
9368                             (gnus-list-of-read-articles gnus-newsgroup-name))
9369                         (setq gnus-newsgroup-expirable
9370                               (sort gnus-newsgroup-expirable '<))))
9371            (expiry-wait (if now 'immediate
9372                           (gnus-group-find-parameter
9373                            gnus-newsgroup-name 'expiry-wait)))
9374            (nnmail-expiry-target
9375             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9376                 nnmail-expiry-target))
9377            es)
9378       (when expirable
9379         ;; There are expirable articles in this group, so we run them
9380         ;; through the expiry process.
9381         (gnus-message 6 "Expiring articles...")
9382         (unless (gnus-check-group gnus-newsgroup-name)
9383           (error "Can't open server for %s" gnus-newsgroup-name))
9384         ;; The list of articles that weren't expired is returned.
9385         (save-excursion
9386           (if expiry-wait
9387               (let ((nnmail-expiry-wait-function nil)
9388                     (nnmail-expiry-wait expiry-wait))
9389                 (setq es (gnus-request-expire-articles
9390                           expirable gnus-newsgroup-name)))
9391             (setq es (gnus-request-expire-articles
9392                       expirable gnus-newsgroup-name)))
9393           (unless total
9394             (setq gnus-newsgroup-expirable es))
9395           ;; We go through the old list of expirable, and mark all
9396           ;; really expired articles as nonexistent.
9397           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9398             (let ((gnus-use-cache nil))
9399               (dolist (article expirable)
9400                 (when (and (not (memq article es))
9401                            (gnus-data-find article))
9402                   (gnus-summary-mark-article article gnus-canceled-mark)
9403                   (run-hook-with-args 'gnus-summary-article-expire-hook
9404                                       'delete
9405                                       (gnus-data-header
9406                                        (assoc article (gnus-data-list nil)))
9407                                       gnus-newsgroup-name
9408                                       nil
9409                                       nil))))))
9410         (gnus-message 6 "Expiring articles...done")))))
9411
9412 (defun gnus-summary-expire-articles-now ()
9413   "Expunge all expirable articles in the current group.
9414 This means that *all* articles that are marked as expirable will be
9415 deleted forever, right now."
9416   (interactive)
9417   (or gnus-expert-user
9418       (gnus-yes-or-no-p
9419        "Are you really, really, really sure you want to delete all these messages? ")
9420       (error "Phew!"))
9421   (gnus-summary-expire-articles t))
9422
9423 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9424 (defun gnus-summary-delete-article (&optional n)
9425   "Delete the N next (mail) articles.
9426 This command actually deletes articles.  This is not a marking
9427 command.  The article will disappear forever from your life, never to
9428 return.
9429
9430 If N is negative, delete backwards.
9431 If N is nil and articles have been marked with the process mark,
9432 delete these instead.
9433
9434 If `gnus-novice-user' is non-nil you will be asked for
9435 confirmation before the articles are deleted."
9436   (interactive "P")
9437   (unless (gnus-check-backend-function 'request-expire-articles
9438                                        gnus-newsgroup-name)
9439     (error "The current newsgroup does not support article deletion"))
9440   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9441     (error "Couldn't open server"))
9442   ;; Compute the list of articles to delete.
9443   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9444         (nnmail-expiry-target 'delete)
9445         not-deleted)
9446     (if (and gnus-novice-user
9447              (not (gnus-yes-or-no-p
9448                    (format "Do you really want to delete %s forever? "
9449                            (if (> (length articles) 1)
9450                                (format "these %s articles" (length articles))
9451                              "this article")))))
9452         ()
9453       ;; Delete the articles.
9454       (setq not-deleted (gnus-request-expire-articles
9455                          articles gnus-newsgroup-name 'force))
9456       (while articles
9457         (gnus-summary-remove-process-mark (car articles))
9458         ;; The backend might not have been able to delete the article
9459         ;; after all.
9460         (unless (memq (car articles) not-deleted)
9461           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9462         (let* ((article (car articles))
9463                (ghead  (gnus-data-header
9464                                     (assoc article (gnus-data-list nil)))))
9465           (run-hook-with-args 'gnus-summary-article-delete-hook
9466                               'delete ghead gnus-newsgroup-name nil
9467                               nil))
9468         (setq articles (cdr articles)))
9469       (when not-deleted
9470         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9471     (gnus-summary-position-point)
9472     (gnus-set-mode-line 'summary)
9473     not-deleted))
9474
9475 (defun gnus-summary-edit-article (&optional arg)
9476   "Edit the current article.
9477 This will have permanent effect only in mail groups.
9478 If ARG is nil, edit the decoded articles.
9479 If ARG is 1, edit the raw articles.
9480 If ARG is 2, edit the raw articles even in read-only groups.
9481 If ARG is 3, edit the articles with the current handles.
9482 Otherwise, allow editing of articles even in read-only
9483 groups."
9484   (interactive "P")
9485   (let (force raw current-handles)
9486     (cond
9487      ((null arg))
9488      ((eq arg 1)
9489       (setq raw t))
9490      ((eq arg 2)
9491       (setq raw t
9492             force t))
9493      ((eq arg 3)
9494       (setq current-handles
9495             (and (gnus-buffer-live-p gnus-article-buffer)
9496                  (with-current-buffer gnus-article-buffer
9497                    (prog1
9498                        gnus-article-mime-handles
9499                      (setq gnus-article-mime-handles nil))))))
9500      (t
9501       (setq force t)))
9502     (when (and raw (not force)
9503                (member gnus-newsgroup-name '("nndraft:delayed"
9504                                              "nndraft:drafts"
9505                                              "nndraft:queue")))
9506       (error "Can't edit the raw article in group %s"
9507              gnus-newsgroup-name))
9508     (save-excursion
9509       (set-buffer gnus-summary-buffer)
9510       (let ((mail-parse-charset gnus-newsgroup-charset)
9511             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9512         (gnus-set-global-variables)
9513         (when (and (not force)
9514                    (gnus-group-read-only-p))
9515           (error "The current newsgroup does not support article editing"))
9516         (gnus-summary-show-article t)
9517         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9518           (with-current-buffer gnus-article-buffer
9519             (mm-enable-multibyte)))
9520         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9521             (setq raw t))
9522         (gnus-article-edit-article
9523          (if raw 'ignore
9524            `(lambda ()
9525               (let ((mbl mml-buffer-list))
9526                 (setq mml-buffer-list nil)
9527                 (mime-to-mml ,'current-handles)
9528                 (let ((mbl1 mml-buffer-list))
9529                   (setq mml-buffer-list mbl)
9530                   (set (make-local-variable 'mml-buffer-list) mbl1))
9531                 (gnus-make-local-hook 'kill-buffer-hook)
9532                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9533          `(lambda (no-highlight)
9534             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9535                   (message-options message-options)
9536                   (message-options-set-recipient)
9537                   (mail-parse-ignored-charsets
9538                    ',gnus-newsgroup-ignored-charsets))
9539               ,(if (not raw) '(progn
9540                                 (mml-to-mime)
9541                                 (mml-destroy-buffers)
9542                                 (remove-hook 'kill-buffer-hook
9543                                              'mml-destroy-buffers t)
9544                                 (kill-local-variable 'mml-buffer-list)))
9545               (gnus-summary-edit-article-done
9546                ,(or (mail-header-references gnus-current-headers) "")
9547                ,(gnus-group-read-only-p)
9548                ,gnus-summary-buffer no-highlight))))))))
9549
9550 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9551
9552 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9553                                                  no-highlight)
9554   "Make edits to the current article permanent."
9555   (interactive)
9556   (save-excursion
9557    ;; The buffer restriction contains the entire article if it exists.
9558     (when (article-goto-body)
9559       (let ((lines (count-lines (point) (point-max)))
9560             (length (- (point-max) (point)))
9561             (case-fold-search t)
9562             (body (copy-marker (point))))
9563         (goto-char (point-min))
9564         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9565           (delete-region (match-beginning 1) (match-end 1))
9566           (insert (number-to-string length)))
9567         (goto-char (point-min))
9568         (when (re-search-forward
9569                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9570           (delete-region (match-beginning 1) (match-end 1))
9571           (insert (number-to-string length)))
9572         (goto-char (point-min))
9573         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9574           (delete-region (match-beginning 1) (match-end 1))
9575           (insert (number-to-string lines))))))
9576   ;; Replace the article.
9577   (let ((buf (current-buffer)))
9578     (with-temp-buffer
9579       (insert-buffer-substring buf)
9580
9581       (if (and (not read-only)
9582                (not (gnus-request-replace-article
9583                      (cdr gnus-article-current) (car gnus-article-current)
9584                      (current-buffer) t)))
9585           (error "Couldn't replace article")
9586         ;; Update the summary buffer.
9587         (if (and references
9588                  (equal (message-tokenize-header references " ")
9589                         (message-tokenize-header
9590                          (or (message-fetch-field "references") "") " ")))
9591             ;; We only have to update this line.
9592             (save-excursion
9593               (save-restriction
9594                 (message-narrow-to-head)
9595                 (let ((head (buffer-string))
9596                       header)
9597                   (with-temp-buffer
9598                     (insert (format "211 %d Article retrieved.\n"
9599                                     (cdr gnus-article-current)))
9600                     (insert head)
9601                     (insert ".\n")
9602                     (let ((nntp-server-buffer (current-buffer)))
9603                       (setq header (car (gnus-get-newsgroup-headers
9604                                          nil t))))
9605                     (save-excursion
9606                       (set-buffer gnus-summary-buffer)
9607                       (gnus-data-set-header
9608                        (gnus-data-find (cdr gnus-article-current))
9609                        header)
9610                       (gnus-summary-update-article-line
9611                        (cdr gnus-article-current) header)
9612                       (if (gnus-summary-goto-subject
9613                            (cdr gnus-article-current) nil t)
9614                           (gnus-summary-update-secondary-mark
9615                            (cdr gnus-article-current))))))))
9616           ;; Update threads.
9617           (set-buffer (or buffer gnus-summary-buffer))
9618           (gnus-summary-update-article (cdr gnus-article-current))
9619           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9620               (gnus-summary-update-secondary-mark
9621                (cdr gnus-article-current))))
9622         ;; Prettify the article buffer again.
9623         (unless no-highlight
9624           (save-excursion
9625             (set-buffer gnus-article-buffer)
9626             ;;;!!! Fix this -- article should be rehighlighted.
9627             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9628             (set-buffer gnus-original-article-buffer)
9629             (gnus-request-article
9630              (cdr gnus-article-current)
9631              (car gnus-article-current) (current-buffer))))
9632         ;; Prettify the summary buffer line.
9633         (when (gnus-visual-p 'summary-highlight 'highlight)
9634           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9635
9636 (defun gnus-summary-edit-wash (key)
9637   "Perform editing command KEY in the article buffer."
9638   (interactive
9639    (list
9640     (progn
9641       (message "%s" (concat (this-command-keys) "- "))
9642       (read-char))))
9643   (message "")
9644   (gnus-summary-edit-article)
9645   (execute-kbd-macro (concat (this-command-keys) key))
9646   (gnus-article-edit-done))
9647
9648 ;;; Respooling
9649
9650 (defun gnus-summary-respool-query (&optional silent trace)
9651   "Query where the respool algorithm would put this article."
9652   (interactive)
9653   (let (gnus-mark-article-hook)
9654     (gnus-summary-select-article)
9655     (save-excursion
9656       (set-buffer gnus-original-article-buffer)
9657       (let ((groups (nnmail-article-group 'identity trace)))
9658         (unless silent
9659           (if groups
9660               (message "This message would go to %s"
9661                        (mapconcat 'car groups ", "))
9662             (message "This message would go to no groups"))
9663           groups)))))
9664
9665 (defun gnus-summary-respool-trace ()
9666   "Trace where the respool algorithm would put this article.
9667 Display a buffer showing all fancy splitting patterns which matched."
9668   (interactive)
9669   (gnus-summary-respool-query nil t))
9670
9671 ;; Summary marking commands.
9672
9673 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9674   "Mark articles which has the same subject as read, and then select the next.
9675 If UNMARK is positive, remove any kind of mark.
9676 If UNMARK is negative, tick articles."
9677   (interactive "P")
9678   (when unmark
9679     (setq unmark (prefix-numeric-value unmark)))
9680   (let ((count
9681          (gnus-summary-mark-same-subject
9682           (gnus-summary-article-subject) unmark)))
9683     ;; Select next unread article.  If auto-select-same mode, should
9684     ;; select the first unread article.
9685     (gnus-summary-next-article t (and gnus-auto-select-same
9686                                       (gnus-summary-article-subject)))
9687     (gnus-message 7 "%d article%s marked as %s"
9688                   count (if (= count 1) " is" "s are")
9689                   (if unmark "unread" "read"))))
9690
9691 (defun gnus-summary-kill-same-subject (&optional unmark)
9692   "Mark articles which has the same subject as read.
9693 If UNMARK is positive, remove any kind of mark.
9694 If UNMARK is negative, tick articles."
9695   (interactive "P")
9696   (when unmark
9697     (setq unmark (prefix-numeric-value unmark)))
9698   (let ((count
9699          (gnus-summary-mark-same-subject
9700           (gnus-summary-article-subject) unmark)))
9701     ;; If marked as read, go to next unread subject.
9702     (when (null unmark)
9703       ;; Go to next unread subject.
9704       (gnus-summary-next-subject 1 t))
9705     (gnus-message 7 "%d articles are marked as %s"
9706                   count (if unmark "unread" "read"))))
9707
9708 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9709   "Mark articles with same SUBJECT as read, and return marked number.
9710 If optional argument UNMARK is positive, remove any kinds of marks.
9711 If optional argument UNMARK is negative, mark articles as unread instead."
9712   (let ((count 1))
9713     (save-excursion
9714       (cond
9715        ((null unmark)                   ; Mark as read.
9716         (while (and
9717                 (progn
9718                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9719                   (gnus-summary-show-thread) t)
9720                 (gnus-summary-find-subject subject))
9721           (setq count (1+ count))))
9722        ((> unmark 0)                    ; Tick.
9723         (while (and
9724                 (progn
9725                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9726                   (gnus-summary-show-thread) t)
9727                 (gnus-summary-find-subject subject))
9728           (setq count (1+ count))))
9729        (t                               ; Mark as unread.
9730         (while (and
9731                 (progn
9732                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9733                   (gnus-summary-show-thread) t)
9734                 (gnus-summary-find-subject subject))
9735           (setq count (1+ count)))))
9736       (gnus-set-mode-line 'summary)
9737       ;; Return the number of marked articles.
9738       count)))
9739
9740 (defun gnus-summary-mark-as-processable (n &optional unmark)
9741   "Set the process mark on the next N articles.
9742 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9743 the process mark instead.  The difference between N and the actual
9744 number of articles marked is returned."
9745   (interactive "P")
9746   (if (and (null n) (gnus-region-active-p))
9747       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9748     (setq n (prefix-numeric-value n))
9749     (let ((backward (< n 0))
9750           (n (abs n)))
9751       (while (and
9752               (> n 0)
9753               (if unmark
9754                   (gnus-summary-remove-process-mark
9755                    (gnus-summary-article-number))
9756                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9757               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9758         (setq n (1- n)))
9759       (when (/= 0 n)
9760         (gnus-message 7 "No more articles"))
9761       (gnus-summary-recenter)
9762       (gnus-summary-position-point)
9763       n)))
9764
9765 (defun gnus-summary-unmark-as-processable (n)
9766   "Remove the process mark from the next N articles.
9767 If N is negative, unmark backward instead.  The difference between N and
9768 the actual number of articles unmarked is returned."
9769   (interactive "P")
9770   (gnus-summary-mark-as-processable n t))
9771
9772 (defun gnus-summary-unmark-all-processable ()
9773   "Remove the process mark from all articles."
9774   (interactive)
9775   (save-excursion
9776     (while gnus-newsgroup-processable
9777       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9778   (gnus-summary-position-point))
9779
9780 (defun gnus-summary-add-mark (article type)
9781   "Mark ARTICLE with a mark of TYPE."
9782   (let ((vtype (car (assq type gnus-article-mark-lists)))
9783         var)
9784     (if (not vtype)
9785         (error "No such mark type: %s" type)
9786       (setq var (intern (format "gnus-newsgroup-%s" type)))
9787       (set var (cons article (symbol-value var)))
9788       (if (memq type '(processable cached replied forwarded recent saved))
9789           (gnus-summary-update-secondary-mark article)
9790         ;;; !!! This is bogus.  We should find out what primary
9791         ;;; !!! mark we want to set.
9792         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9793
9794 (defun gnus-summary-mark-as-expirable (n)
9795   "Mark N articles forward as expirable.
9796 If N is negative, mark backward instead.  The difference between N and
9797 the actual number of articles marked is returned."
9798   (interactive "p")
9799   (gnus-summary-mark-forward n gnus-expirable-mark))
9800
9801 (defun gnus-summary-mark-as-spam (n)
9802   "Mark N articles forward as spam.
9803 If N is negative, mark backward instead.  The difference between N and
9804 the actual number of articles marked is returned."
9805   (interactive "p")
9806   (gnus-summary-mark-forward n gnus-spam-mark))
9807
9808 (defun gnus-summary-mark-article-as-replied (article)
9809   "Mark ARTICLE as replied to and update the summary line.
9810 ARTICLE can also be a list of articles."
9811   (interactive (list (gnus-summary-article-number)))
9812   (let ((articles (if (listp article) article (list article))))
9813     (dolist (article articles)
9814       (unless (numberp article)
9815         (error "%s is not a number" article))
9816       (push article gnus-newsgroup-replied)
9817       (let ((buffer-read-only nil))
9818         (when (gnus-summary-goto-subject article nil t)
9819           (gnus-summary-update-secondary-mark article))))))
9820
9821 (defun gnus-summary-mark-article-as-forwarded (article)
9822   "Mark ARTICLE as forwarded and update the summary line.
9823 ARTICLE can also be a list of articles."
9824   (let ((articles (if (listp article) article (list article))))
9825     (dolist (article articles)
9826       (push article gnus-newsgroup-forwarded)
9827       (let ((buffer-read-only nil))
9828         (when (gnus-summary-goto-subject article nil t)
9829           (gnus-summary-update-secondary-mark article))))))
9830
9831 (defun gnus-summary-set-bookmark (article)
9832   "Set a bookmark in current article."
9833   (interactive (list (gnus-summary-article-number)))
9834   (when (or (not (get-buffer gnus-article-buffer))
9835             (not gnus-current-article)
9836             (not gnus-article-current)
9837             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9838     (error "No current article selected"))
9839   ;; Remove old bookmark, if one exists.
9840   (gnus-pull article gnus-newsgroup-bookmarks)
9841   ;; Set the new bookmark, which is on the form
9842   ;; (article-number . line-number-in-body).
9843   (push
9844    (cons article
9845          (with-current-buffer gnus-article-buffer
9846            (count-lines
9847             (min (point)
9848                  (save-excursion
9849                    (article-goto-body)
9850                    (point)))
9851             (point))))
9852    gnus-newsgroup-bookmarks)
9853   (gnus-message 6 "A bookmark has been added to the current article."))
9854
9855 (defun gnus-summary-remove-bookmark (article)
9856   "Remove the bookmark from the current article."
9857   (interactive (list (gnus-summary-article-number)))
9858   ;; Remove old bookmark, if one exists.
9859   (if (not (assq article gnus-newsgroup-bookmarks))
9860       (gnus-message 6 "No bookmark in current article.")
9861     (gnus-pull article gnus-newsgroup-bookmarks)
9862     (gnus-message 6 "Removed bookmark.")))
9863
9864 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9865 (defun gnus-summary-mark-as-dormant (n)
9866   "Mark N articles forward as dormant.
9867 If N is negative, mark backward instead.  The difference between N and
9868 the actual number of articles marked is returned."
9869   (interactive "p")
9870   (gnus-summary-mark-forward n gnus-dormant-mark))
9871
9872 (defun gnus-summary-set-process-mark (article)
9873   "Set the process mark on ARTICLE and update the summary line."
9874   (setq gnus-newsgroup-processable
9875         (cons article
9876               (delq article gnus-newsgroup-processable)))
9877   (when (gnus-summary-goto-subject article)
9878     (gnus-summary-show-thread)
9879     (gnus-summary-goto-subject article)
9880     (gnus-summary-update-secondary-mark article)))
9881
9882 (defun gnus-summary-remove-process-mark (article)
9883   "Remove the process mark from ARTICLE and update the summary line."
9884   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9885   (when (gnus-summary-goto-subject article)
9886     (gnus-summary-show-thread)
9887     (gnus-summary-goto-subject article)
9888     (gnus-summary-update-secondary-mark article)))
9889
9890 (defun gnus-summary-set-saved-mark (article)
9891   "Set the process mark on ARTICLE and update the summary line."
9892   (push article gnus-newsgroup-saved)
9893   (when (gnus-summary-goto-subject article)
9894     (gnus-summary-update-secondary-mark article)))
9895
9896 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9897   "Mark N articles as read forwards.
9898 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9899 The difference between N and the actual number of articles marked is
9900 returned.
9901 If NO-EXPIRE, auto-expiry will be inhibited."
9902   (interactive "p")
9903   (gnus-summary-show-thread)
9904   (let ((backward (< n 0))
9905         (gnus-summary-goto-unread
9906          (and gnus-summary-goto-unread
9907               (not (eq gnus-summary-goto-unread 'never))
9908               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9909                                     gnus-ticked-mark gnus-dormant-mark)))))
9910         (n (abs n))
9911         (mark (or mark gnus-del-mark)))
9912     (while (and (> n 0)
9913                 (gnus-summary-mark-article nil mark no-expire)
9914                 (zerop (gnus-summary-next-subject
9915                         (if backward -1 1)
9916                         (and gnus-summary-goto-unread
9917                              (not (eq gnus-summary-goto-unread 'never)))
9918                         t)))
9919       (setq n (1- n)))
9920     (when (/= 0 n)
9921       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9922     (gnus-summary-recenter)
9923     (gnus-summary-position-point)
9924     (gnus-set-mode-line 'summary)
9925     n))
9926
9927 (defun gnus-summary-mark-article-as-read (mark)
9928   "Mark the current article quickly as read with MARK."
9929   (let ((article (gnus-summary-article-number)))
9930     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9931     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9932     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9933     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9934     (push (cons article mark) gnus-newsgroup-reads)
9935     ;; Possibly remove from cache, if that is used.
9936     (when gnus-use-cache
9937       (gnus-cache-enter-remove-article article))
9938     ;; Allow the backend to change the mark.
9939     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9940     ;; Check for auto-expiry.
9941     (when (and gnus-newsgroup-auto-expire
9942                (memq mark gnus-auto-expirable-marks))
9943       (setq mark gnus-expirable-mark)
9944       ;; Let the backend know about the mark change.
9945       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9946       (push article gnus-newsgroup-expirable))
9947     ;; Set the mark in the buffer.
9948     (gnus-summary-update-mark mark 'unread)
9949     t))
9950
9951 (defun gnus-summary-mark-article-as-unread (mark)
9952   "Mark the current article quickly as unread with MARK."
9953   (let* ((article (gnus-summary-article-number))
9954          (old-mark (gnus-summary-article-mark article)))
9955     ;; Allow the backend to change the mark.
9956     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9957     (if (eq mark old-mark)
9958         t
9959       (if (<= article 0)
9960           (progn
9961             (gnus-error 1 "Can't mark negative article numbers")
9962             nil)
9963         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9964         (setq gnus-newsgroup-spam-marked
9965               (delq article gnus-newsgroup-spam-marked))
9966         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9967         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9968         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9969         (cond ((= mark gnus-ticked-mark)
9970                (setq gnus-newsgroup-marked
9971                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9972                                               article)))
9973               ((= mark gnus-spam-mark)
9974                (setq gnus-newsgroup-spam-marked
9975                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9976                                               article)))
9977               ((= mark gnus-dormant-mark)
9978                (setq gnus-newsgroup-dormant
9979                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9980                                               article)))
9981               (t
9982                (setq gnus-newsgroup-unreads
9983                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9984                                               article))))
9985         (gnus-pull article gnus-newsgroup-reads)
9986
9987         ;; See whether the article is to be put in the cache.
9988         (and gnus-use-cache
9989              (vectorp (gnus-summary-article-header article))
9990              (save-excursion
9991                (gnus-cache-possibly-enter-article
9992                 gnus-newsgroup-name article
9993                 (= mark gnus-ticked-mark)
9994                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9995
9996         ;; Fix the mark.
9997         (gnus-summary-update-mark mark 'unread)
9998         t))))
9999
10000 (defun gnus-summary-mark-article (&optional article mark no-expire)
10001   "Mark ARTICLE with MARK.  MARK can be any character.
10002 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10003 `??' (dormant) and `?E' (expirable).
10004 If MARK is nil, then the default character `?r' is used.
10005 If ARTICLE is nil, then the article on the current line will be
10006 marked.
10007 If NO-EXPIRE, auto-expiry will be inhibited."
10008   ;; The mark might be a string.
10009   (when (stringp mark)
10010     (setq mark (aref mark 0)))
10011   ;; If no mark is given, then we check auto-expiring.
10012   (when (null mark)
10013     (setq mark gnus-del-mark))
10014   (when (and (not no-expire)
10015              gnus-newsgroup-auto-expire
10016              (memq mark gnus-auto-expirable-marks))
10017     (setq mark gnus-expirable-mark))
10018   (let ((article (or article (gnus-summary-article-number)))
10019         (old-mark (gnus-summary-article-mark article)))
10020     ;; Allow the backend to change the mark.
10021     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10022     (if (eq mark old-mark)
10023         t
10024       (unless article
10025         (error "No article on current line"))
10026       (if (not (if (or (= mark gnus-unread-mark)
10027                        (= mark gnus-ticked-mark)
10028                        (= mark gnus-spam-mark)
10029                        (= mark gnus-dormant-mark))
10030                    (gnus-mark-article-as-unread article mark)
10031                  (gnus-mark-article-as-read article mark)))
10032           t
10033         ;; See whether the article is to be put in the cache.
10034         (and gnus-use-cache
10035              (not (= mark gnus-canceled-mark))
10036              (vectorp (gnus-summary-article-header article))
10037              (save-excursion
10038                (gnus-cache-possibly-enter-article
10039                 gnus-newsgroup-name article
10040                 (= mark gnus-ticked-mark)
10041                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10042
10043         (when (gnus-summary-goto-subject article nil t)
10044           (let ((buffer-read-only nil))
10045             (gnus-summary-show-thread)
10046             ;; Fix the mark.
10047             (gnus-summary-update-mark mark 'unread)
10048             t))))))
10049
10050 (defun gnus-summary-update-secondary-mark (article)
10051   "Update the secondary (read, process, cache) mark."
10052   (gnus-summary-update-mark
10053    (cond ((memq article gnus-newsgroup-processable)
10054           gnus-process-mark)
10055          ((memq article gnus-newsgroup-cached)
10056           gnus-cached-mark)
10057          ((memq article gnus-newsgroup-replied)
10058           gnus-replied-mark)
10059          ((memq article gnus-newsgroup-forwarded)
10060           gnus-forwarded-mark)
10061          ((memq article gnus-newsgroup-saved)
10062           gnus-saved-mark)
10063          ((memq article gnus-newsgroup-recent)
10064           gnus-recent-mark)
10065          ((memq article gnus-newsgroup-unseen)
10066           gnus-unseen-mark)
10067          (t gnus-no-mark))
10068    'replied)
10069   (when (gnus-visual-p 'summary-highlight 'highlight)
10070     (gnus-run-hooks 'gnus-summary-update-hook))
10071   t)
10072
10073 (defun gnus-summary-update-download-mark (article)
10074   "Update the download mark."
10075   (gnus-summary-update-mark
10076    (cond ((memq article gnus-newsgroup-undownloaded)
10077           gnus-undownloaded-mark)
10078          (gnus-newsgroup-agentized
10079           gnus-downloaded-mark)
10080          (t
10081           gnus-no-mark))
10082    'download)
10083   (gnus-summary-update-line t)
10084   t)
10085
10086 (defun gnus-summary-update-mark (mark type)
10087   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10088         (buffer-read-only nil))
10089     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10090     (when forward
10091       (when (looking-at "\r")
10092         (incf forward))
10093       (when (<= (+ forward (point)) (point-max))
10094         ;; Go to the right position on the line.
10095         (goto-char (+ forward (point)))
10096         ;; Replace the old mark with the new mark.
10097         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10098         ;; Optionally update the marks by some user rule.
10099         (when (eq type 'unread)
10100           (gnus-data-set-mark
10101            (gnus-data-find (gnus-summary-article-number)) mark)
10102           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10103
10104 (defun gnus-mark-article-as-read (article &optional mark)
10105   "Enter ARTICLE in the pertinent lists and remove it from others."
10106   ;; Make the article expirable.
10107   (let ((mark (or mark gnus-del-mark)))
10108     (setq gnus-newsgroup-expirable
10109           (if (= mark gnus-expirable-mark)
10110               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10111             (delq article gnus-newsgroup-expirable)))
10112     ;; Remove from unread and marked lists.
10113     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10114     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10115     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10116     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10117     (push (cons article mark) gnus-newsgroup-reads)
10118     ;; Possibly remove from cache, if that is used.
10119     (when gnus-use-cache
10120       (gnus-cache-enter-remove-article article))
10121     t))
10122
10123 (defun gnus-mark-article-as-unread (article &optional mark)
10124   "Enter ARTICLE in the pertinent lists and remove it from others."
10125   (let ((mark (or mark gnus-ticked-mark)))
10126     (if (<= article 0)
10127         (progn
10128           (gnus-error 1 "Can't mark negative article numbers")
10129           nil)
10130       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10131             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10132             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10133             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10134             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10135
10136       ;; Unsuppress duplicates?
10137       (when gnus-suppress-duplicates
10138         (gnus-dup-unsuppress-article article))
10139
10140       (cond ((= mark gnus-ticked-mark)
10141              (setq gnus-newsgroup-marked
10142                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10143             ((= mark gnus-spam-mark)
10144              (setq gnus-newsgroup-spam-marked
10145                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10146                                             article)))
10147             ((= mark gnus-dormant-mark)
10148              (setq gnus-newsgroup-dormant
10149                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10150             (t
10151              (setq gnus-newsgroup-unreads
10152                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10153       (gnus-pull article gnus-newsgroup-reads)
10154       t)))
10155
10156 (defalias 'gnus-summary-mark-as-unread-forward
10157   'gnus-summary-tick-article-forward)
10158 (make-obsolete 'gnus-summary-mark-as-unread-forward
10159                'gnus-summary-tick-article-forward)
10160 (defun gnus-summary-tick-article-forward (n)
10161   "Tick N articles forwards.
10162 If N is negative, tick backwards instead.
10163 The difference between N and the number of articles ticked is returned."
10164   (interactive "p")
10165   (gnus-summary-mark-forward n gnus-ticked-mark))
10166
10167 (defalias 'gnus-summary-mark-as-unread-backward
10168   'gnus-summary-tick-article-backward)
10169 (make-obsolete 'gnus-summary-mark-as-unread-backward
10170                'gnus-summary-tick-article-backward)
10171 (defun gnus-summary-tick-article-backward (n)
10172   "Tick N articles backwards.
10173 The difference between N and the number of articles ticked is returned."
10174   (interactive "p")
10175   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10176
10177 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10178 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10179 (defun gnus-summary-tick-article (&optional article clear-mark)
10180   "Mark current article as unread.
10181 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10182 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10183   (interactive)
10184   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10185                                        gnus-ticked-mark)))
10186
10187 (defun gnus-summary-mark-as-read-forward (n)
10188   "Mark N articles as read forwards.
10189 If N is negative, mark backwards instead.
10190 The difference between N and the actual number of articles marked is
10191 returned."
10192   (interactive "p")
10193   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10194
10195 (defun gnus-summary-mark-as-read-backward (n)
10196   "Mark the N articles as read backwards.
10197 The difference between N and the actual number of articles marked is
10198 returned."
10199   (interactive "p")
10200   (gnus-summary-mark-forward
10201    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10202
10203 (defun gnus-summary-mark-as-read (&optional article mark)
10204   "Mark current article as read.
10205 ARTICLE specifies the article to be marked as read.
10206 MARK specifies a string to be inserted at the beginning of the line."
10207   (gnus-summary-mark-article article mark))
10208
10209 (defun gnus-summary-clear-mark-forward (n)
10210   "Clear marks from N articles forward.
10211 If N is negative, clear backward instead.
10212 The difference between N and the number of marks cleared is returned."
10213   (interactive "p")
10214   (gnus-summary-mark-forward n gnus-unread-mark))
10215
10216 (defun gnus-summary-clear-mark-backward (n)
10217   "Clear marks from N articles backward.
10218 The difference between N and the number of marks cleared is returned."
10219   (interactive "p")
10220   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10221
10222 (defun gnus-summary-mark-unread-as-read ()
10223   "Intended to be used by `gnus-summary-mark-article-hook'."
10224   (when (memq gnus-current-article gnus-newsgroup-unreads)
10225     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10226
10227 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10228   "Intended to be used by `gnus-summary-mark-article-hook'."
10229   (let ((mark (gnus-summary-article-mark)))
10230     (when (or (gnus-unread-mark-p mark)
10231               (gnus-read-mark-p mark))
10232       (gnus-summary-mark-article gnus-current-article
10233                                  (or new-mark gnus-read-mark)))))
10234
10235 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10236   "Intended to be used by `gnus-summary-mark-article-hook'."
10237   (let ((mark (gnus-summary-article-mark)))
10238     (when (or (gnus-unread-mark-p mark)
10239               (gnus-read-mark-p mark))
10240       (gnus-summary-mark-article (gnus-summary-article-number)
10241                                  (or new-mark gnus-read-mark)))))
10242
10243 (defun gnus-summary-mark-unread-as-ticked ()
10244   "Intended to be used by `gnus-summary-mark-article-hook'."
10245   (when (memq gnus-current-article gnus-newsgroup-unreads)
10246     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10247
10248 (defun gnus-summary-mark-region-as-read (point mark all)
10249   "Mark all unread articles between point and mark as read.
10250 If given a prefix, mark all articles between point and mark as read,
10251 even ticked and dormant ones."
10252   (interactive "r\nP")
10253   (save-excursion
10254     (let (article)
10255       (goto-char point)
10256       (beginning-of-line)
10257       (while (and
10258               (< (point) mark)
10259               (progn
10260                 (when (or all
10261                           (memq (setq article (gnus-summary-article-number))
10262                                 gnus-newsgroup-unreads))
10263                   (gnus-summary-mark-article article gnus-del-mark))
10264                 t)
10265               (gnus-summary-find-next))))))
10266
10267 (defun gnus-summary-mark-below (score mark)
10268   "Mark articles with score less than SCORE with MARK."
10269   (interactive "P\ncMark: ")
10270   (setq score (if score
10271                   (prefix-numeric-value score)
10272                 (or gnus-summary-default-score 0)))
10273   (save-excursion
10274     (set-buffer gnus-summary-buffer)
10275     (goto-char (point-min))
10276     (while
10277         (progn
10278           (and (< (gnus-summary-article-score) score)
10279                (gnus-summary-mark-article nil mark))
10280           (gnus-summary-find-next)))))
10281
10282 (defun gnus-summary-kill-below (&optional score)
10283   "Mark articles with score below SCORE as read."
10284   (interactive "P")
10285   (gnus-summary-mark-below score gnus-killed-mark))
10286
10287 (defun gnus-summary-clear-above (&optional score)
10288   "Clear all marks from articles with score above SCORE."
10289   (interactive "P")
10290   (gnus-summary-mark-above score gnus-unread-mark))
10291
10292 (defun gnus-summary-tick-above (&optional score)
10293   "Tick all articles with score above SCORE."
10294   (interactive "P")
10295   (gnus-summary-mark-above score gnus-ticked-mark))
10296
10297 (defun gnus-summary-mark-above (score mark)
10298   "Mark articles with score over SCORE with MARK."
10299   (interactive "P\ncMark: ")
10300   (setq score (if score
10301                   (prefix-numeric-value score)
10302                 (or gnus-summary-default-score 0)))
10303   (save-excursion
10304     (set-buffer gnus-summary-buffer)
10305     (goto-char (point-min))
10306     (while (and (progn
10307                   (when (> (gnus-summary-article-score) score)
10308                     (gnus-summary-mark-article nil mark))
10309                   t)
10310                 (gnus-summary-find-next)))))
10311
10312 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10313 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10314 (defun gnus-summary-limit-include-expunged (&optional no-error)
10315   "Display all the hidden articles that were expunged for low scores."
10316   (interactive)
10317   (let ((buffer-read-only nil))
10318     (let ((scored gnus-newsgroup-scored)
10319           headers h)
10320       (while scored
10321         (unless (gnus-summary-article-header (caar scored))
10322           (and (setq h (gnus-number-to-header (caar scored)))
10323                (< (cdar scored) gnus-summary-expunge-below)
10324                (push h headers)))
10325         (setq scored (cdr scored)))
10326       (if (not headers)
10327           (when (not no-error)
10328             (error "No expunged articles hidden"))
10329         (goto-char (point-min))
10330         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10331         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10332         (mapcar (lambda (x) (push (mail-header-number x)
10333                                   gnus-newsgroup-limit))
10334                 headers)
10335         (gnus-summary-prepare-unthreaded (nreverse headers))
10336         (goto-char (point-min))
10337         (gnus-summary-position-point)
10338         t))))
10339
10340 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10341   "Mark all unread articles in this newsgroup as read.
10342 If prefix argument ALL is non-nil, ticked and dormant articles will
10343 also be marked as read.
10344 If QUIETLY is non-nil, no questions will be asked.
10345
10346 If TO-HERE is non-nil, it should be a point in the buffer.  All
10347 articles before (after, if REVERSE is set) this point will be marked
10348 as read.
10349
10350 Note that this function will only catch up the unread article
10351 in the current summary buffer limitation.
10352
10353 The number of articles marked as read is returned."
10354   (interactive "P")
10355   (prog1
10356       (save-excursion
10357         (when (or quietly
10358                   (not gnus-interactive-catchup) ;Without confirmation?
10359                   gnus-expert-user
10360                   (gnus-y-or-n-p
10361                    (if all
10362                        "Mark absolutely all articles as read? "
10363                      "Mark all unread articles as read? ")))
10364           (if (and not-mark
10365                    (not gnus-newsgroup-adaptive)
10366                    (not gnus-newsgroup-auto-expire)
10367                    (not gnus-suppress-duplicates)
10368                    (or (not gnus-use-cache)
10369                        (eq gnus-use-cache 'passive)))
10370               (progn
10371                 (when all
10372                   (setq gnus-newsgroup-marked nil
10373                         gnus-newsgroup-spam-marked nil
10374                         gnus-newsgroup-dormant nil))
10375                 (setq gnus-newsgroup-unreads
10376                       (gnus-sorted-nunion
10377                        (gnus-intersection gnus-newsgroup-unreads
10378                                           gnus-newsgroup-downloadable)
10379                        gnus-newsgroup-unfetched)))
10380             ;; We actually mark all articles as canceled, which we
10381             ;; have to do when using auto-expiry or adaptive scoring.
10382             (gnus-summary-show-all-threads)
10383             (if (and to-here reverse)
10384                 (progn
10385                   (goto-char to-here)
10386                   (gnus-summary-mark-current-read-and-unread-as-read
10387                    gnus-catchup-mark)
10388                   (while (gnus-summary-find-next (not all))
10389                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10390               (when (gnus-summary-first-subject (not all))
10391                 (while (and
10392                         (if to-here (< (point) to-here) t)
10393                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10394                         (gnus-summary-find-next (not all))))))
10395             (gnus-set-mode-line 'summary))
10396           t))
10397     (gnus-summary-position-point)))
10398
10399 (defun gnus-summary-catchup-to-here (&optional all)
10400   "Mark all unticked articles before the current one as read.
10401 If ALL is non-nil, also mark ticked and dormant articles as read."
10402   (interactive "P")
10403   (save-excursion
10404     (gnus-save-hidden-threads
10405       (let ((beg (point)))
10406         ;; We check that there are unread articles.
10407         (when (or all (gnus-summary-find-prev))
10408           (gnus-summary-catchup all t beg)))))
10409   (gnus-summary-position-point))
10410
10411 (defun gnus-summary-catchup-from-here (&optional all)
10412   "Mark all unticked articles after (and including) the current one as read.
10413 If ALL is non-nil, also mark ticked and dormant articles as read."
10414   (interactive "P")
10415   (save-excursion
10416     (gnus-save-hidden-threads
10417       (let ((beg (point)))
10418         ;; We check that there are unread articles.
10419         (when (or all (gnus-summary-find-next))
10420           (gnus-summary-catchup all t beg nil t)))))
10421   (gnus-summary-position-point))
10422
10423 (defun gnus-summary-catchup-all (&optional quietly)
10424   "Mark all articles in this newsgroup as read.
10425 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10426 instead, which marks only unread articles as read."
10427   (interactive "P")
10428   (gnus-summary-catchup t quietly))
10429
10430 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10431   "Mark all unread articles in this group as read, then exit.
10432 If prefix argument ALL is non-nil, all articles are marked as read.
10433 If QUIETLY is non-nil, no questions will be asked."
10434   (interactive "P")
10435   (when (gnus-summary-catchup all quietly nil 'fast)
10436     ;; Select next newsgroup or exit.
10437     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10438              (eq gnus-auto-select-next 'quietly))
10439         (gnus-summary-next-group nil)
10440       (gnus-summary-exit))))
10441
10442 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10443   "Mark all articles in this newsgroup as read, and then exit.
10444 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10445 instead, which marks only unread articles as read."
10446   (interactive "P")
10447   (gnus-summary-catchup-and-exit t quietly))
10448
10449 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10450   "Mark all articles in this group as read and select the next group.
10451 If given a prefix, mark all articles, unread as well as ticked, as
10452 read."
10453   (interactive "P")
10454   (save-excursion
10455     (gnus-summary-catchup all))
10456   (gnus-summary-next-group))
10457
10458 ;;;
10459 ;;; with article
10460 ;;;
10461
10462 (defmacro gnus-with-article (article &rest forms)
10463   "Select ARTICLE and perform FORMS in the original article buffer.
10464 Then replace the article with the result."
10465   `(progn
10466      ;; We don't want the article to be marked as read.
10467      (let (gnus-mark-article-hook)
10468        (gnus-summary-select-article t t nil ,article))
10469      (set-buffer gnus-original-article-buffer)
10470      ,@forms
10471      (if (not (gnus-check-backend-function
10472                'request-replace-article (car gnus-article-current)))
10473          (gnus-message 5 "Read-only group; not replacing")
10474        (unless (gnus-request-replace-article
10475                 ,article (car gnus-article-current)
10476                 (current-buffer) t)
10477          (error "Couldn't replace article")))
10478      ;; The cache and backlog have to be flushed somewhat.
10479      (when gnus-keep-backlog
10480        (gnus-backlog-remove-article
10481         (car gnus-article-current) (cdr gnus-article-current)))
10482      (when gnus-use-cache
10483        (gnus-cache-update-article
10484         (car gnus-article-current) (cdr gnus-article-current)))))
10485
10486 (put 'gnus-with-article 'lisp-indent-function 1)
10487 (put 'gnus-with-article 'edebug-form-spec '(form body))
10488
10489 ;; Thread-based commands.
10490
10491 (defun gnus-summary-articles-in-thread (&optional article)
10492   "Return a list of all articles in the current thread.
10493 If ARTICLE is non-nil, return all articles in the thread that starts
10494 with that article."
10495   (let* ((article (or article (gnus-summary-article-number)))
10496          (data (gnus-data-find-list article))
10497          (top-level (gnus-data-level (car data)))
10498          (top-subject
10499           (cond ((null gnus-thread-operation-ignore-subject)
10500                  (gnus-simplify-subject-re
10501                   (mail-header-subject (gnus-data-header (car data)))))
10502                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10503                  (gnus-simplify-subject-fuzzy
10504                   (mail-header-subject (gnus-data-header (car data)))))
10505                 (t nil)))
10506          (end-point (save-excursion
10507                       (if (gnus-summary-go-to-next-thread)
10508                           (point) (point-max))))
10509          articles)
10510     (while (and data
10511                 (< (gnus-data-pos (car data)) end-point))
10512       (when (or (not top-subject)
10513                 (string= top-subject
10514                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10515                              (gnus-simplify-subject-fuzzy
10516                               (mail-header-subject
10517                                (gnus-data-header (car data))))
10518                            (gnus-simplify-subject-re
10519                             (mail-header-subject
10520                              (gnus-data-header (car data)))))))
10521         (push (gnus-data-number (car data)) articles))
10522       (unless (and (setq data (cdr data))
10523                    (> (gnus-data-level (car data)) top-level))
10524         (setq data nil)))
10525     ;; Return the list of articles.
10526     (nreverse articles)))
10527
10528 (defun gnus-summary-rethread-current ()
10529   "Rethread the thread the current article is part of."
10530   (interactive)
10531   (let* ((gnus-show-threads t)
10532          (article (gnus-summary-article-number))
10533          (id (mail-header-id (gnus-summary-article-header)))
10534          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10535     (unless id
10536       (error "No article on the current line"))
10537     (gnus-rebuild-thread id)
10538     (gnus-summary-goto-subject article)))
10539
10540 (defun gnus-summary-reparent-thread ()
10541   "Make the current article child of the marked (or previous) article.
10542
10543 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10544 is non-nil or the Subject: of both articles are the same."
10545   (interactive)
10546   (unless (not (gnus-group-read-only-p))
10547     (error "The current newsgroup does not support article editing"))
10548   (unless (<= (length gnus-newsgroup-processable) 1)
10549     (error "No more than one article may be marked"))
10550   (save-window-excursion
10551     (let ((gnus-article-buffer " *reparent*")
10552           (current-article (gnus-summary-article-number))
10553           ;; First grab the marked article, otherwise one line up.
10554           (parent-article (if (not (null gnus-newsgroup-processable))
10555                               (car gnus-newsgroup-processable)
10556                             (save-excursion
10557                               (if (eq (forward-line -1) 0)
10558                                   (gnus-summary-article-number)
10559                                 (error "Beginning of summary buffer"))))))
10560       (unless (not (eq current-article parent-article))
10561         (error "An article may not be self-referential"))
10562       (let ((message-id (mail-header-id
10563                          (gnus-summary-article-header parent-article))))
10564         (unless (and message-id (not (equal message-id "")))
10565           (error "No message-id in desired parent"))
10566         (gnus-with-article current-article
10567           (save-restriction
10568             (goto-char (point-min))
10569             (message-narrow-to-head)
10570             (if (re-search-forward "^References: " nil t)
10571                 (progn
10572                   (re-search-forward "^[^ \t]" nil t)
10573                   (forward-line -1)
10574                   (end-of-line)
10575                   (insert " " message-id))
10576               (insert "References: " message-id "\n"))))
10577         (set-buffer gnus-summary-buffer)
10578         (gnus-summary-unmark-all-processable)
10579         (gnus-summary-update-article current-article)
10580         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10581             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10582         (gnus-summary-rethread-current)
10583         (gnus-message 3 "Article %d is now the child of article %d"
10584                       current-article parent-article)))))
10585
10586 (defun gnus-summary-toggle-threads (&optional arg)
10587   "Toggle showing conversation threads.
10588 If ARG is positive number, turn showing conversation threads on."
10589   (interactive "P")
10590   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10591     (setq gnus-show-threads
10592           (if (null arg) (not gnus-show-threads)
10593             (> (prefix-numeric-value arg) 0)))
10594     (gnus-summary-prepare)
10595     (gnus-summary-goto-subject current)
10596     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10597     (gnus-summary-position-point)))
10598
10599 (defun gnus-summary-show-all-threads ()
10600   "Show all threads."
10601   (interactive)
10602   (save-excursion
10603     (let ((buffer-read-only nil))
10604       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10605   (gnus-summary-position-point))
10606
10607 (defun gnus-summary-show-thread ()
10608   "Show thread subtrees.
10609 Returns nil if no thread was there to be shown."
10610   (interactive)
10611   (let ((buffer-read-only nil)
10612         (orig (point))
10613         (end (point-at-eol))
10614         ;; Leave point at bol
10615         (beg (progn (beginning-of-line) (point))))
10616     (prog1
10617         ;; Any hidden lines here?
10618         (search-forward "\r" end t)
10619       (subst-char-in-region beg end ?\^M ?\n t)
10620       (goto-char orig)
10621       (gnus-summary-position-point))))
10622
10623 (defun gnus-summary-maybe-hide-threads ()
10624   "If requested, hide the threads that should be hidden."
10625   (when (and gnus-show-threads
10626              gnus-thread-hide-subtree)
10627     (gnus-summary-hide-all-threads
10628      (if (or (consp gnus-thread-hide-subtree)
10629              (functionp gnus-thread-hide-subtree))
10630          (gnus-make-predicate gnus-thread-hide-subtree)
10631        nil))))
10632
10633 ;;; Hiding predicates.
10634
10635 (defun gnus-article-unread-p (header)
10636   (memq (mail-header-number header) gnus-newsgroup-unreads))
10637
10638 (defun gnus-article-unseen-p (header)
10639   (memq (mail-header-number header) gnus-newsgroup-unseen))
10640
10641 (defun gnus-map-articles (predicate articles)
10642   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10643   (apply 'gnus-or (mapcar predicate
10644                           (mapcar 'gnus-summary-article-header articles))))
10645
10646 (defun gnus-summary-hide-all-threads (&optional predicate)
10647   "Hide all thread subtrees.
10648 If PREDICATE is supplied, threads that satisfy this predicate
10649 will not be hidden."
10650   (interactive)
10651   (save-excursion
10652     (goto-char (point-min))
10653     (let ((end nil))
10654       (while (not end)
10655         (when (or (not predicate)
10656                   (gnus-map-articles
10657                    predicate (gnus-summary-article-children)))
10658             (gnus-summary-hide-thread))
10659         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10660   (gnus-summary-position-point))
10661
10662 (defun gnus-summary-hide-thread ()
10663   "Hide thread subtrees.
10664 If PREDICATE is supplied, threads that satisfy this predicate
10665 will not be hidden.
10666 Returns nil if no threads were there to be hidden."
10667   (interactive)
10668   (let ((buffer-read-only nil)
10669         (start (point))
10670         (article (gnus-summary-article-number)))
10671     (goto-char start)
10672     ;; Go forward until either the buffer ends or the subthread
10673     ;; ends.
10674     (when (and (not (eobp))
10675                (or (zerop (gnus-summary-next-thread 1 t))
10676                    (goto-char (point-max))))
10677       (prog1
10678           (if (and (> (point) start)
10679                    (search-backward "\n" start t))
10680               (progn
10681                 (subst-char-in-region start (point) ?\n ?\^M)
10682                 (gnus-summary-goto-subject article))
10683             (goto-char start)
10684             nil)))))
10685
10686 (defun gnus-summary-go-to-next-thread (&optional previous)
10687   "Go to the same level (or less) next thread.
10688 If PREVIOUS is non-nil, go to previous thread instead.
10689 Return the article number moved to, or nil if moving was impossible."
10690   (let ((level (gnus-summary-thread-level))
10691         (way (if previous -1 1))
10692         (beg (point)))
10693     (forward-line way)
10694     (while (and (not (eobp))
10695                 (< level (gnus-summary-thread-level)))
10696       (forward-line way))
10697     (if (eobp)
10698         (progn
10699           (goto-char beg)
10700           nil)
10701       (setq beg (point))
10702       (prog1
10703           (gnus-summary-article-number)
10704         (goto-char beg)))))
10705
10706 (defun gnus-summary-next-thread (n &optional silent)
10707   "Go to the same level next N'th thread.
10708 If N is negative, search backward instead.
10709 Returns the difference between N and the number of skips actually
10710 done.
10711
10712 If SILENT, don't output messages."
10713   (interactive "p")
10714   (let ((backward (< n 0))
10715         (n (abs n)))
10716     (while (and (> n 0)
10717                 (gnus-summary-go-to-next-thread backward))
10718       (decf n))
10719     (unless silent
10720       (gnus-summary-position-point))
10721     (when (and (not silent) (/= 0 n))
10722       (gnus-message 7 "No more threads"))
10723     n))
10724
10725 (defun gnus-summary-prev-thread (n)
10726   "Go to the same level previous N'th thread.
10727 Returns the difference between N and the number of skips actually
10728 done."
10729   (interactive "p")
10730   (gnus-summary-next-thread (- n)))
10731
10732 (defun gnus-summary-go-down-thread ()
10733   "Go down one level in the current thread."
10734   (let ((children (gnus-summary-article-children)))
10735     (when children
10736       (gnus-summary-goto-subject (car children)))))
10737
10738 (defun gnus-summary-go-up-thread ()
10739   "Go up one level in the current thread."
10740   (let ((parent (gnus-summary-article-parent)))
10741     (when parent
10742       (gnus-summary-goto-subject parent))))
10743
10744 (defun gnus-summary-down-thread (n)
10745   "Go down thread N steps.
10746 If N is negative, go up instead.
10747 Returns the difference between N and how many steps down that were
10748 taken."
10749   (interactive "p")
10750   (let ((up (< n 0))
10751         (n (abs n)))
10752     (while (and (> n 0)
10753                 (if up (gnus-summary-go-up-thread)
10754                   (gnus-summary-go-down-thread)))
10755       (setq n (1- n)))
10756     (gnus-summary-position-point)
10757     (when (/= 0 n)
10758       (gnus-message 7 "Can't go further"))
10759     n))
10760
10761 (defun gnus-summary-up-thread (n)
10762   "Go up thread N steps.
10763 If N is negative, go down instead.
10764 Returns the difference between N and how many steps down that were
10765 taken."
10766   (interactive "p")
10767   (gnus-summary-down-thread (- n)))
10768
10769 (defun gnus-summary-top-thread ()
10770   "Go to the top of the thread."
10771   (interactive)
10772   (while (gnus-summary-go-up-thread))
10773   (gnus-summary-article-number))
10774
10775 (defun gnus-summary-kill-thread (&optional unmark)
10776   "Mark articles under current thread as read.
10777 If the prefix argument is positive, remove any kinds of marks.
10778 If the prefix argument is negative, tick articles instead."
10779   (interactive "P")
10780   (when unmark
10781     (setq unmark (prefix-numeric-value unmark)))
10782   (let ((articles (gnus-summary-articles-in-thread)))
10783     (save-excursion
10784       ;; Expand the thread.
10785       (gnus-summary-show-thread)
10786       ;; Mark all the articles.
10787       (while articles
10788         (gnus-summary-goto-subject (car articles))
10789         (cond ((null unmark)
10790                (gnus-summary-mark-article-as-read gnus-killed-mark))
10791               ((> unmark 0)
10792                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10793               (t
10794                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10795         (setq articles (cdr articles))))
10796     ;; Hide killed subtrees.
10797     (and (null unmark)
10798          gnus-thread-hide-killed
10799          (gnus-summary-hide-thread))
10800     ;; If marked as read, go to next unread subject.
10801     (when (null unmark)
10802       ;; Go to next unread subject.
10803       (gnus-summary-next-subject 1 t)))
10804   (gnus-set-mode-line 'summary))
10805
10806 ;; Summary sorting commands
10807
10808 (defun gnus-summary-sort-by-number (&optional reverse)
10809   "Sort the summary buffer by article number.
10810 Argument REVERSE means reverse order."
10811   (interactive "P")
10812   (gnus-summary-sort 'number reverse))
10813
10814 (defun gnus-summary-sort-by-random (&optional reverse)
10815   "Randomize the order in the summary buffer.
10816 Argument REVERSE means to randomize in reverse order."
10817   (interactive "P")
10818   (gnus-summary-sort 'random reverse))
10819
10820 (defun gnus-summary-sort-by-author (&optional reverse)
10821   "Sort the summary buffer by author name alphabetically.
10822 If `case-fold-search' is non-nil, case of letters is ignored.
10823 Argument REVERSE means reverse order."
10824   (interactive "P")
10825   (gnus-summary-sort 'author reverse))
10826
10827 (defun gnus-summary-sort-by-subject (&optional reverse)
10828   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10829 If `case-fold-search' is non-nil, case of letters is ignored.
10830 Argument REVERSE means reverse order."
10831   (interactive "P")
10832   (gnus-summary-sort 'subject reverse))
10833
10834 (defun gnus-summary-sort-by-date (&optional reverse)
10835   "Sort the summary buffer by date.
10836 Argument REVERSE means reverse order."
10837   (interactive "P")
10838   (gnus-summary-sort 'date reverse))
10839
10840 (defun gnus-summary-sort-by-score (&optional reverse)
10841   "Sort the summary buffer by score.
10842 Argument REVERSE means reverse order."
10843   (interactive "P")
10844   (gnus-summary-sort 'score reverse))
10845
10846 (defun gnus-summary-sort-by-lines (&optional reverse)
10847   "Sort the summary buffer by the number of lines.
10848 Argument REVERSE means reverse order."
10849   (interactive "P")
10850   (gnus-summary-sort 'lines reverse))
10851
10852 (defun gnus-summary-sort-by-chars (&optional reverse)
10853   "Sort the summary buffer by article length.
10854 Argument REVERSE means reverse order."
10855   (interactive "P")
10856   (gnus-summary-sort 'chars reverse))
10857
10858 (defun gnus-summary-sort-by-original (&optional reverse)
10859   "Sort the summary buffer using the default sorting method.
10860 Argument REVERSE means reverse order."
10861   (interactive "P")
10862   (let* ((buffer-read-only)
10863          (gnus-summary-prepare-hook nil))
10864     ;; We do the sorting by regenerating the threads.
10865     (gnus-summary-prepare)
10866     ;; Hide subthreads if needed.
10867     (gnus-summary-maybe-hide-threads)))
10868
10869 (defun gnus-summary-sort (predicate reverse)
10870   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10871   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10872          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10873          (gnus-thread-sort-functions
10874           (if (not reverse)
10875               thread
10876             `(lambda (t1 t2)
10877                (,thread t2 t1))))
10878          (gnus-sort-gathered-threads-function
10879           gnus-thread-sort-functions)
10880          (gnus-article-sort-functions
10881           (if (not reverse)
10882               article
10883             `(lambda (t1 t2)
10884                (,article t2 t1))))
10885          (buffer-read-only)
10886          (gnus-summary-prepare-hook nil))
10887     ;; We do the sorting by regenerating the threads.
10888     (gnus-summary-prepare)
10889     ;; Hide subthreads if needed.
10890     (gnus-summary-maybe-hide-threads)))
10891
10892 ;; Summary saving commands.
10893
10894 (defun gnus-summary-save-article (&optional n not-saved)
10895   "Save the current article using the default saver function.
10896 If N is a positive number, save the N next articles.
10897 If N is a negative number, save the N previous articles.
10898 If N is nil and any articles have been marked with the process mark,
10899 save those articles instead.
10900 The variable `gnus-default-article-saver' specifies the saver function."
10901   (interactive "P")
10902   (let* ((articles (gnus-summary-work-articles n))
10903          (save-buffer (save-excursion
10904                         (nnheader-set-temp-buffer " *Gnus Save*")))
10905          (num (length articles))
10906          header file)
10907     (dolist (article articles)
10908       (setq header (gnus-summary-article-header article))
10909       (if (not (vectorp header))
10910           ;; This is a pseudo-article.
10911           (if (assq 'name header)
10912               (gnus-copy-file (cdr (assq 'name header)))
10913             (gnus-message 1 "Article %d is unsaveable" article))
10914         ;; This is a real article.
10915         (save-window-excursion
10916           (let ((gnus-display-mime-function nil)
10917                 (gnus-article-prepare-hook nil))
10918             (gnus-summary-select-article t nil nil article)))
10919         (save-excursion
10920           (set-buffer save-buffer)
10921           (erase-buffer)
10922           (insert-buffer-substring gnus-original-article-buffer))
10923         (setq file (gnus-article-save save-buffer file num))
10924         (gnus-summary-remove-process-mark article)
10925         (unless not-saved
10926           (gnus-summary-set-saved-mark article))))
10927     (gnus-kill-buffer save-buffer)
10928     (gnus-summary-position-point)
10929     (gnus-set-mode-line 'summary)
10930     n))
10931
10932 (defun gnus-summary-pipe-output (&optional arg headers)
10933   "Pipe the current article to a subprocess.
10934 If N is a positive number, pipe the N next articles.
10935 If N is a negative number, pipe the N previous articles.
10936 If N is nil and any articles have been marked with the process mark,
10937 pipe those articles instead.
10938 If HEADERS (the symbolic prefix), include the headers, too."
10939   (interactive (gnus-interactive "P\ny"))
10940   (require 'gnus-art)
10941   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10942         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10943     (gnus-summary-save-article arg t))
10944   (let ((buffer (get-buffer "*Shell Command Output*")))
10945     (when (and buffer
10946                (not (zerop (buffer-size buffer))))
10947       (gnus-configure-windows 'pipe))))
10948
10949 (defun gnus-summary-save-article-mail (&optional arg)
10950   "Append the current article to an mail file.
10951 If N is a positive number, save the N next articles.
10952 If N is a negative number, save the N previous articles.
10953 If N is nil and any articles have been marked with the process mark,
10954 save those articles instead."
10955   (interactive "P")
10956   (require 'gnus-art)
10957   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10958     (gnus-summary-save-article arg)))
10959
10960 (defun gnus-summary-save-article-rmail (&optional arg)
10961   "Append the current article to an rmail file.
10962 If N is a positive number, save the N next articles.
10963 If N is a negative number, save the N previous articles.
10964 If N is nil and any articles have been marked with the process mark,
10965 save those articles instead."
10966   (interactive "P")
10967   (require 'gnus-art)
10968   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10969     (gnus-summary-save-article arg)))
10970
10971 (defun gnus-summary-save-article-file (&optional arg)
10972   "Append the current article to a file.
10973 If N is a positive number, save the N next articles.
10974 If N is a negative number, save the N previous articles.
10975 If N is nil and any articles have been marked with the process mark,
10976 save those articles instead."
10977   (interactive "P")
10978   (require 'gnus-art)
10979   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10980     (gnus-summary-save-article arg)))
10981
10982 (defun gnus-summary-write-article-file (&optional arg)
10983   "Write the current article to a file, deleting the previous file.
10984 If N is a positive number, save the N next articles.
10985 If N is a negative number, save the N previous articles.
10986 If N is nil and any articles have been marked with the process mark,
10987 save those articles instead."
10988   (interactive "P")
10989   (require 'gnus-art)
10990   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10991     (gnus-summary-save-article arg)))
10992
10993 (defun gnus-summary-save-article-body-file (&optional arg)
10994   "Append the current article body to a file.
10995 If N is a positive number, save the N next articles.
10996 If N is a negative number, save the N previous articles.
10997 If N is nil and any articles have been marked with the process mark,
10998 save those articles instead."
10999   (interactive "P")
11000   (require 'gnus-art)
11001   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11002     (gnus-summary-save-article arg)))
11003
11004 (defun gnus-summary-muttprint (&optional arg)
11005   "Print the current article using Muttprint.
11006 If N is a positive number, save the N next articles.
11007 If N is a negative number, save the N previous articles.
11008 If N is nil and any articles have been marked with the process mark,
11009 save those articles instead."
11010   (interactive "P")
11011   (require 'gnus-art)
11012   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11013     (gnus-summary-save-article arg t)))
11014
11015 (defun gnus-summary-pipe-message (program)
11016   "Pipe the current article through PROGRAM."
11017   (interactive "sProgram: ")
11018   (gnus-summary-select-article)
11019   (let ((mail-header-separator ""))
11020     (gnus-eval-in-buffer-window gnus-article-buffer
11021       (save-restriction
11022         (widen)
11023         (let ((start (window-start))
11024               buffer-read-only)
11025           (message-pipe-buffer-body program)
11026           (set-window-start (get-buffer-window (current-buffer)) start))))))
11027
11028 (defun gnus-get-split-value (methods)
11029   "Return a value based on the split METHODS."
11030   (let (split-name method result match)
11031     (when methods
11032       (save-excursion
11033         (set-buffer gnus-original-article-buffer)
11034         (save-restriction
11035           (nnheader-narrow-to-headers)
11036           (while (and methods (not split-name))
11037             (goto-char (point-min))
11038             (setq method (pop methods))
11039             (setq match (car method))
11040             (when (cond
11041                    ((stringp match)
11042                     ;; Regular expression.
11043                     (ignore-errors
11044                       (re-search-forward match nil t)))
11045                    ((functionp match)
11046                     ;; Function.
11047                     (save-restriction
11048                       (widen)
11049                       (setq result (funcall match gnus-newsgroup-name))))
11050                    ((consp match)
11051                     ;; Form.
11052                     (save-restriction
11053                       (widen)
11054                       (setq result (eval match)))))
11055               (setq split-name (cdr method))
11056               (cond ((stringp result)
11057                      (push (expand-file-name
11058                             result gnus-article-save-directory)
11059                            split-name))
11060                     ((consp result)
11061                      (setq split-name (append result split-name)))))))))
11062     (nreverse split-name)))
11063
11064 (defun gnus-valid-move-group-p (group)
11065   (and (boundp group)
11066        (symbol-name group)
11067        (symbol-value group)
11068        (gnus-get-function (gnus-find-method-for-group
11069                            (symbol-name group)) 'request-accept-article t)))
11070
11071 (defun gnus-read-move-group-name (prompt default articles prefix)
11072   "Read a group name."
11073   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11074          (minibuffer-confirm-incomplete nil) ; XEmacs
11075          (prom
11076           (format "%s %s to:"
11077                   prompt
11078                   (if (> (length articles) 1)
11079                       (format "these %d articles" (length articles))
11080                     "this article")))
11081          (to-newsgroup
11082           (cond
11083            ((null split-name)
11084             (gnus-completing-read-with-default
11085              default prom
11086              gnus-active-hashtb
11087              'gnus-valid-move-group-p
11088              nil prefix
11089              'gnus-group-history))
11090            ((= 1 (length split-name))
11091             (gnus-completing-read-with-default
11092              (car split-name) prom
11093              gnus-active-hashtb
11094              'gnus-valid-move-group-p
11095              nil nil
11096              'gnus-group-history))
11097            (t
11098             (gnus-completing-read-with-default
11099              nil prom
11100              (mapcar 'list (nreverse split-name))
11101              nil nil nil
11102              'gnus-group-history))))
11103          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11104     (when to-newsgroup
11105       (if (or (string= to-newsgroup "")
11106               (string= to-newsgroup prefix))
11107           (setq to-newsgroup default))
11108       (unless to-newsgroup
11109         (error "No group name entered"))
11110       (or (gnus-active to-newsgroup)
11111           (gnus-activate-group to-newsgroup nil nil to-method)
11112           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11113                                      to-newsgroup))
11114               (or (and (gnus-request-create-group to-newsgroup to-method)
11115                        (gnus-activate-group
11116                         to-newsgroup nil nil to-method)
11117                        (gnus-subscribe-group to-newsgroup))
11118                   (error "Couldn't create group %s" to-newsgroup)))
11119           (error "No such group: %s" to-newsgroup)))
11120     to-newsgroup))
11121
11122 (defun gnus-summary-save-parts (type dir n &optional reverse)
11123   "Save parts matching TYPE to DIR.
11124 If REVERSE, save parts that do not match TYPE."
11125   (interactive
11126    (list (read-string "Save parts of type: "
11127                       (or (car gnus-summary-save-parts-type-history)
11128                           gnus-summary-save-parts-default-mime)
11129                       'gnus-summary-save-parts-type-history)
11130          (setq gnus-summary-save-parts-last-directory
11131                (read-file-name "Save to directory: "
11132                                gnus-summary-save-parts-last-directory
11133                                nil t))
11134          current-prefix-arg))
11135   (gnus-summary-iterate n
11136     (let ((gnus-display-mime-function nil)
11137           (gnus-inhibit-treatment t))
11138       (gnus-summary-select-article))
11139     (save-excursion
11140       (set-buffer gnus-article-buffer)
11141       (let ((handles (or gnus-article-mime-handles
11142                          (mm-dissect-buffer nil gnus-article-loose-mime)
11143                          (and gnus-article-emulate-mime
11144                               (mm-uu-dissect)))))
11145         (when handles
11146           (gnus-summary-save-parts-1 type dir handles reverse)
11147           (unless gnus-article-mime-handles ;; Don't destroy this case.
11148             (mm-destroy-parts handles)))))))
11149
11150 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11151   (if (stringp (car handle))
11152       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11153               (cdr handle))
11154     (when (if reverse
11155               (not (string-match type (mm-handle-media-type handle)))
11156             (string-match type (mm-handle-media-type handle)))
11157       (let ((file (expand-file-name
11158                    (gnus-map-function
11159                     mm-file-name-rewrite-functions
11160                     (file-name-nondirectory
11161                      (or
11162                       (mail-content-type-get
11163                        (mm-handle-disposition handle) 'filename)
11164                       (mail-content-type-get
11165                        (mm-handle-type handle) 'name)
11166                       (concat gnus-newsgroup-name
11167                               "." (number-to-string
11168                                    (cdr gnus-article-current))))))
11169                    dir)))
11170         (unless (file-exists-p file)
11171           (mm-save-part-to-file handle file))))))
11172
11173 ;; Summary extract commands
11174
11175 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11176   (let ((buffer-read-only nil)
11177         (article (gnus-summary-article-number))
11178         after-article b e)
11179     (unless (gnus-summary-goto-subject article)
11180       (error "No such article: %d" article))
11181     (gnus-summary-position-point)
11182     ;; If all commands are to be bunched up on one line, we collect
11183     ;; them here.
11184     (unless gnus-view-pseudos-separately
11185       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11186             files action)
11187         (while ps
11188           (setq action (cdr (assq 'action (car ps))))
11189           (setq files (list (cdr (assq 'name (car ps)))))
11190           (while (and ps (cdr ps)
11191                       (string= (or action "1")
11192                                (or (cdr (assq 'action (cadr ps))) "2")))
11193             (push (cdr (assq 'name (cadr ps))) files)
11194             (setcdr ps (cddr ps)))
11195           (when files
11196             (when (not (string-match "%s" action))
11197               (push " " files))
11198             (push " " files)
11199             (when (assq 'execute (car ps))
11200               (setcdr (assq 'execute (car ps))
11201                       (funcall (if (string-match "%s" action)
11202                                    'format 'concat)
11203                                action
11204                                (mapconcat
11205                                 (lambda (f)
11206                                   (if (equal f " ")
11207                                       f
11208                                     (shell-quote-argument f)))
11209                                 files " ")))))
11210           (setq ps (cdr ps)))))
11211     (if (and gnus-view-pseudos (not not-view))
11212         (while pslist
11213           (when (assq 'execute (car pslist))
11214             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11215                                   (eq gnus-view-pseudos 'not-confirm)))
11216           (setq pslist (cdr pslist)))
11217       (save-excursion
11218         (while pslist
11219           (setq after-article (or (cdr (assq 'article (car pslist)))
11220                                   (gnus-summary-article-number)))
11221           (gnus-summary-goto-subject after-article)
11222           (forward-line 1)
11223           (setq b (point))
11224           (insert "    " (file-name-nondirectory
11225                           (cdr (assq 'name (car pslist))))
11226                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11227           (setq e (point))
11228           (forward-line -1)             ; back to `b'
11229           (gnus-add-text-properties
11230            b (1- e) (list 'gnus-number gnus-reffed-article-number
11231                           gnus-mouse-face-prop gnus-mouse-face))
11232           (gnus-data-enter
11233            after-article gnus-reffed-article-number
11234            gnus-unread-mark b (car pslist) 0 (- e b))
11235           (setq gnus-newsgroup-unreads
11236                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11237                                          gnus-reffed-article-number))
11238           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11239           (setq pslist (cdr pslist)))))))
11240
11241 (defun gnus-pseudos< (p1 p2)
11242   (let ((c1 (cdr (assq 'action p1)))
11243         (c2 (cdr (assq 'action p2))))
11244     (and c1 c2 (string< c1 c2))))
11245
11246 (defun gnus-request-pseudo-article (props)
11247   (cond ((assq 'execute props)
11248          (gnus-execute-command (cdr (assq 'execute props)))))
11249   (let ((gnus-current-article (gnus-summary-article-number)))
11250     (gnus-run-hooks 'gnus-mark-article-hook)))
11251
11252 (defun gnus-execute-command (command &optional automatic)
11253   (save-excursion
11254     (gnus-article-setup-buffer)
11255     (set-buffer gnus-article-buffer)
11256     (setq buffer-read-only nil)
11257     (let ((command (if automatic command
11258                      (read-string "Command: " (cons command 0)))))
11259       (erase-buffer)
11260       (insert "$ " command "\n\n")
11261       (if gnus-view-pseudo-asynchronously
11262           (start-process "gnus-execute" (current-buffer) shell-file-name
11263                          shell-command-switch command)
11264         (call-process shell-file-name nil t nil
11265                       shell-command-switch command)))))
11266
11267 ;; Summary kill commands.
11268
11269 (defun gnus-summary-edit-global-kill (article)
11270   "Edit the \"global\" kill file."
11271   (interactive (list (gnus-summary-article-number)))
11272   (gnus-group-edit-global-kill article))
11273
11274 (defun gnus-summary-edit-local-kill ()
11275   "Edit a local kill file applied to the current newsgroup."
11276   (interactive)
11277   (setq gnus-current-headers (gnus-summary-article-header))
11278   (gnus-group-edit-local-kill
11279    (gnus-summary-article-number) gnus-newsgroup-name))
11280
11281 ;;; Header reading.
11282
11283 (defun gnus-read-header (id &optional header)
11284   "Read the headers of article ID and enter them into the Gnus system."
11285   (let ((group gnus-newsgroup-name)
11286         (gnus-override-method
11287          (or
11288           gnus-override-method
11289           (and (gnus-news-group-p gnus-newsgroup-name)
11290                (car (gnus-refer-article-methods)))))
11291         where)
11292     ;; First we check to see whether the header in question is already
11293     ;; fetched.
11294     (if (stringp id)
11295         ;; This is a Message-ID.
11296         (setq header (or header (gnus-id-to-header id)))
11297       ;; This is an article number.
11298       (setq header (or header (gnus-summary-article-header id))))
11299     (if (and header
11300              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11301         ;; We have found the header.
11302         header
11303       ;; We have to really fetch the header to this article.
11304       (save-excursion
11305         (set-buffer nntp-server-buffer)
11306         (when (setq where (gnus-request-head id group))
11307           (nnheader-fold-continuation-lines)
11308           (goto-char (point-max))
11309           (insert ".\n")
11310           (goto-char (point-min))
11311           (insert "211 ")
11312           (princ (cond
11313                   ((numberp id) id)
11314                   ((cdr where) (cdr where))
11315                   (header (mail-header-number header))
11316                   (t gnus-reffed-article-number))
11317                  (current-buffer))
11318           (insert " Article retrieved.\n"))
11319         (if (or (not where)
11320                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11321             ()                          ; Malformed head.
11322           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11323             (when (and (stringp id)
11324                        (not (string= (gnus-group-real-name group)
11325                                      (car where))))
11326               ;; If we fetched by Message-ID and the article came
11327               ;; from a different group, we fudge some bogus article
11328               ;; numbers for this article.
11329               (mail-header-set-number header gnus-reffed-article-number))
11330             (save-excursion
11331               (set-buffer gnus-summary-buffer)
11332               (decf gnus-reffed-article-number)
11333               (gnus-remove-header (mail-header-number header))
11334               (push header gnus-newsgroup-headers)
11335               (setq gnus-current-headers header)
11336               (push (mail-header-number header) gnus-newsgroup-limit)))
11337           header)))))
11338
11339 (defun gnus-remove-header (number)
11340   "Remove header NUMBER from `gnus-newsgroup-headers'."
11341   (if (and gnus-newsgroup-headers
11342            (= number (mail-header-number (car gnus-newsgroup-headers))))
11343       (pop gnus-newsgroup-headers)
11344     (let ((headers gnus-newsgroup-headers))
11345       (while (and (cdr headers)
11346                   (not (= number (mail-header-number (cadr headers)))))
11347         (pop headers))
11348       (when (cdr headers)
11349         (setcdr headers (cddr headers))))))
11350
11351 ;;;
11352 ;;; summary highlights
11353 ;;;
11354
11355 (defun gnus-highlight-selected-summary ()
11356   "Highlight selected article in summary buffer."
11357   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11358   (when gnus-summary-selected-face
11359     (save-excursion
11360       (let* ((beg (point-at-bol))
11361              (end (point-at-eol))
11362              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11363              (from (if (get-text-property beg gnus-mouse-face-prop)
11364                        beg
11365                      (or (next-single-property-change
11366                           beg gnus-mouse-face-prop nil end)
11367                          beg)))
11368              (to
11369               (if (= from end)
11370                   (- from 2)
11371                 (or (next-single-property-change
11372                      from gnus-mouse-face-prop nil end)
11373                     end))))
11374         ;; If no mouse-face prop on line we will have to = from = end,
11375         ;; so we highlight the entire line instead.
11376         (when (= (+ to 2) from)
11377           (setq from beg)
11378           (setq to end))
11379         (if gnus-newsgroup-selected-overlay
11380             ;; Move old overlay.
11381             (gnus-move-overlay
11382              gnus-newsgroup-selected-overlay from to (current-buffer))
11383           ;; Create new overlay.
11384           (gnus-overlay-put
11385            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11386            'face gnus-summary-selected-face))))))
11387
11388 (defvar gnus-summary-highlight-line-cached nil)
11389 (defvar gnus-summary-highlight-line-trigger nil)
11390
11391 (defun gnus-summary-highlight-line-0 ()
11392   (if (and (eq gnus-summary-highlight-line-trigger
11393                gnus-summary-highlight)
11394            gnus-summary-highlight-line-cached)
11395       gnus-summary-highlight-line-cached
11396     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11397           gnus-summary-highlight-line-cached
11398           (let* ((cond (list 'cond))
11399                  (c cond)
11400                  (list gnus-summary-highlight))
11401             (while list
11402               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11403                               nil))
11404               (setq c (cdr c)
11405                     list (cdr list)))
11406             (gnus-byte-compile (list 'lambda nil cond))))))
11407
11408 (defun gnus-summary-highlight-line ()
11409   "Highlight current line according to `gnus-summary-highlight'."
11410   (let* ((beg (point-at-bol))
11411          (article (or (gnus-summary-article-number) gnus-current-article))
11412          (score (or (cdr (assq article
11413                                gnus-newsgroup-scored))
11414                     gnus-summary-default-score 0))
11415          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11416          (inhibit-read-only t)
11417          (default gnus-summary-default-score)
11418          (default-high gnus-summary-default-high-score)
11419          (default-low gnus-summary-default-low-score)
11420          (uncached (and gnus-summary-use-undownloaded-faces
11421                         (memq article gnus-newsgroup-undownloaded)
11422                         (not (memq article gnus-newsgroup-cached)))))
11423     (let ((face (funcall (gnus-summary-highlight-line-0))))
11424       (unless (eq face (get-text-property beg 'face))
11425         (gnus-put-text-property-excluding-characters-with-faces
11426          beg (point-at-eol) 'face
11427          (setq face (if (boundp face) (symbol-value face) face)))
11428         (when gnus-summary-highlight-line-function
11429           (funcall gnus-summary-highlight-line-function article face))))))
11430
11431 (defun gnus-update-read-articles (group unread &optional compute)
11432   "Update the list of read articles in GROUP.
11433 UNREAD is a sorted list."
11434   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11435         (info (gnus-get-info group))
11436         (prev 1)
11437         read)
11438     (if (or (not info) (not active))
11439         ;; There is no info on this group if it was, in fact,
11440         ;; killed.  Gnus stores no information on killed groups, so
11441         ;; there's nothing to be done.
11442         ;; One could store the information somewhere temporarily,
11443         ;; perhaps...  Hmmm...
11444         ()
11445       ;; Remove any negative articles numbers.
11446       (while (and unread (< (car unread) 0))
11447         (setq unread (cdr unread)))
11448       ;; Remove any expired article numbers
11449       (while (and unread (< (car unread) (car active)))
11450         (setq unread (cdr unread)))
11451       ;; Compute the ranges of read articles by looking at the list of
11452       ;; unread articles.
11453       (while unread
11454         (when (/= (car unread) prev)
11455           (push (if (= prev (1- (car unread))) prev
11456                   (cons prev (1- (car unread))))
11457                 read))
11458         (setq prev (1+ (car unread)))
11459         (setq unread (cdr unread)))
11460       (when (<= prev (cdr active))
11461         (push (cons prev (cdr active)) read))
11462       (setq read (if (> (length read) 1) (nreverse read) read))
11463       (if compute
11464           read
11465         (save-excursion
11466           (let (setmarkundo)
11467             ;; Propagate the read marks to the backend.
11468             (when (gnus-check-backend-function 'request-set-mark group)
11469               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11470                     (add (gnus-remove-from-range read (gnus-info-read info))))
11471                 (when (or add del)
11472                   (unless (gnus-check-group group)
11473                     (error "Can't open server for %s" group))
11474                   (gnus-request-set-mark
11475                    group (delq nil (list (if add (list add 'add '(read)))
11476                                          (if del (list del 'del '(read))))))
11477                   (setq setmarkundo
11478                         `(gnus-request-set-mark
11479                           ,group
11480                           ',(delq nil (list
11481                                        (if del (list del 'add '(read)))
11482                                        (if add (list add 'del '(read))))))))))
11483             (set-buffer gnus-group-buffer)
11484             (gnus-undo-register
11485               `(progn
11486                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11487                  (gnus-info-set-read ',info ',(gnus-info-read info))
11488                  (gnus-get-unread-articles-in-group ',info
11489                                                     (gnus-active ,group))
11490                  (gnus-group-update-group ,group t)
11491                  ,setmarkundo))))
11492         ;; Enter this list into the group info.
11493         (gnus-info-set-read info read)
11494         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11495         (gnus-get-unread-articles-in-group info (gnus-active group))
11496         t))))
11497
11498 (defun gnus-offer-save-summaries ()
11499   "Offer to save all active summary buffers."
11500   (let (buffers)
11501     ;; Go through all buffers and find all summaries.
11502     (dolist (buffer (buffer-list))
11503       (when (and (setq buffer (buffer-name buffer))
11504                  (string-match "Summary" buffer)
11505                  (with-current-buffer buffer
11506                    ;; We check that this is, indeed, a summary buffer.
11507                    (and (eq major-mode 'gnus-summary-mode)
11508                         ;; Also make sure this isn't bogus.
11509                         gnus-newsgroup-prepared
11510                         ;; Also make sure that this isn't a
11511                         ;; dead summary buffer.
11512                         (not gnus-dead-summary-mode))))
11513         (push buffer buffers)))
11514     ;; Go through all these summary buffers and offer to save them.
11515     (when buffers
11516       (save-excursion
11517         (map-y-or-n-p
11518          "Update summary buffer %s? "
11519          (lambda (buf)
11520            (switch-to-buffer buf)
11521            (gnus-summary-exit))
11522          buffers)))))
11523
11524 (defun gnus-summary-setup-default-charset ()
11525   "Setup newsgroup default charset."
11526   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11527       (setq gnus-newsgroup-charset nil)
11528     (let* ((ignored-charsets
11529             (or gnus-newsgroup-ephemeral-ignored-charsets
11530                 (append
11531                  (and gnus-newsgroup-name
11532                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11533                  gnus-newsgroup-ignored-charsets))))
11534       (setq gnus-newsgroup-charset
11535             (or gnus-newsgroup-ephemeral-charset
11536                 (and gnus-newsgroup-name
11537                      (gnus-parameter-charset gnus-newsgroup-name))
11538                 gnus-default-charset))
11539       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11540            ignored-charsets))))
11541
11542 ;;;
11543 ;;; Mime Commands
11544 ;;;
11545
11546 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11547   "Display the current article buffer fully MIME-buttonized.
11548 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11549 treated as multipart/mixed."
11550   (interactive "P")
11551   (require 'gnus-art)
11552   (let ((gnus-unbuttonized-mime-types nil)
11553         (gnus-mime-display-multipart-as-mixed show-all-parts))
11554     (gnus-summary-show-article)))
11555
11556 (defun gnus-summary-repair-multipart (article)
11557   "Add a Content-Type header to a multipart article without one."
11558   (interactive (list (gnus-summary-article-number)))
11559   (gnus-with-article article
11560     (message-narrow-to-head)
11561     (message-remove-header "Mime-Version")
11562     (goto-char (point-max))
11563     (insert "Mime-Version: 1.0\n")
11564     (widen)
11565     (when (search-forward "\n--" nil t)
11566       (let ((separator (buffer-substring (point) (point-at-eol))))
11567         (message-narrow-to-head)
11568         (message-remove-header "Content-Type")
11569         (goto-char (point-max))
11570         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11571                         separator))
11572         (widen))))
11573   (let (gnus-mark-article-hook)
11574     (gnus-summary-select-article t t nil article)))
11575
11576 (defun gnus-summary-toggle-display-buttonized ()
11577   "Toggle the buttonizing of the article buffer."
11578   (interactive)
11579   (require 'gnus-art)
11580   (if (setq gnus-inhibit-mime-unbuttonizing
11581             (not gnus-inhibit-mime-unbuttonizing))
11582       (let ((gnus-unbuttonized-mime-types nil))
11583         (gnus-summary-show-article))
11584     (gnus-summary-show-article)))
11585
11586 ;;;
11587 ;;; Generic summary marking commands
11588 ;;;
11589
11590 (defvar gnus-summary-marking-alist
11591   '((read gnus-del-mark "d")
11592     (unread gnus-unread-mark "u")
11593     (ticked gnus-ticked-mark "!")
11594     (dormant gnus-dormant-mark "?")
11595     (expirable gnus-expirable-mark "e"))
11596   "An alist of names/marks/keystrokes.")
11597
11598 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11599 (defvar gnus-summary-mark-map)
11600
11601 (defun gnus-summary-make-all-marking-commands ()
11602   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11603   (dolist (elem gnus-summary-marking-alist)
11604     (apply 'gnus-summary-make-marking-command elem)))
11605
11606 (defun gnus-summary-make-marking-command (name mark keystroke)
11607   (let ((map (make-sparse-keymap)))
11608     (define-key gnus-summary-generic-mark-map keystroke map)
11609     (dolist (lway `((next "next" next nil "n")
11610                     (next-unread "next unread" next t "N")
11611                     (prev "previous" prev nil "p")
11612                     (prev-unread "previous unread" prev t "P")
11613                     (nomove "" nil nil ,keystroke)))
11614       (let ((func (gnus-summary-make-marking-command-1
11615                    mark (car lway) lway name)))
11616         (setq func (eval func))
11617         (define-key map (nth 4 lway) func)))))
11618
11619 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11620   `(defun ,(intern
11621             (format "gnus-summary-put-mark-as-%s%s"
11622                     name (if (eq way 'nomove)
11623                              ""
11624                            (concat "-" (symbol-name way)))))
11625      (n)
11626      ,(format
11627        "Mark the current article as %s%s.
11628 If N, the prefix, then repeat N times.
11629 If N is negative, move in reverse order.
11630 The difference between N and the actual number of articles marked is
11631 returned."
11632        name (cadr lway))
11633      (interactive "p")
11634      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11635
11636 (defun gnus-summary-generic-mark (n mark move unread)
11637   "Mark N articles with MARK."
11638   (unless (eq major-mode 'gnus-summary-mode)
11639     (error "This command can only be used in the summary buffer"))
11640   (gnus-summary-show-thread)
11641   (let ((nummove
11642          (cond
11643           ((eq move 'next) 1)
11644           ((eq move 'prev) -1)
11645           (t 0))))
11646     (if (zerop nummove)
11647         (setq n 1)
11648       (when (< n 0)
11649         (setq n (abs n)
11650               nummove (* -1 nummove))))
11651     (while (and (> n 0)
11652                 (gnus-summary-mark-article nil mark)
11653                 (zerop (gnus-summary-next-subject nummove unread t)))
11654       (setq n (1- n)))
11655     (when (/= 0 n)
11656       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11657     (gnus-summary-recenter)
11658     (gnus-summary-position-point)
11659     (gnus-set-mode-line 'summary)
11660     n))
11661
11662 (defun gnus-summary-insert-articles (articles)
11663   (when (setq articles
11664               (gnus-sorted-difference articles
11665                                       (mapcar (lambda (h)
11666                                                 (mail-header-number h))
11667                                               gnus-newsgroup-headers)))
11668     (setq gnus-newsgroup-headers
11669           (gnus-merge 'list
11670                       gnus-newsgroup-headers
11671                       (gnus-fetch-headers articles)
11672                       'gnus-article-sort-by-number))
11673     ;; Suppress duplicates?
11674     (when gnus-suppress-duplicates
11675       (gnus-dup-suppress-articles))
11676
11677     ;; We might want to build some more threads first.
11678     (when (and gnus-fetch-old-headers
11679                (eq gnus-headers-retrieved-by 'nov))
11680       (if (eq gnus-fetch-old-headers 'invisible)
11681           (gnus-build-all-threads)
11682         (gnus-build-old-threads)))
11683     ;; Let the Gnus agent mark articles as read.
11684     (when gnus-agent
11685       (gnus-agent-get-undownloaded-list))
11686     ;; Remove list identifiers from subject
11687     (when gnus-list-identifiers
11688       (gnus-summary-remove-list-identifiers))
11689     ;; First and last article in this newsgroup.
11690     (when gnus-newsgroup-headers
11691       (setq gnus-newsgroup-begin
11692             (mail-header-number (car gnus-newsgroup-headers))
11693             gnus-newsgroup-end
11694             (mail-header-number
11695              (gnus-last-element gnus-newsgroup-headers))))
11696     (when gnus-use-scoring
11697       (gnus-possibly-score-headers))))
11698
11699 (defun gnus-summary-insert-old-articles (&optional all)
11700   "Insert all old articles in this group.
11701 If ALL is non-nil, already read articles become readable.
11702 If ALL is a number, fetch this number of articles."
11703   (interactive "P")
11704   (prog1
11705       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11706             older len)
11707         (setq older
11708               ;; Some nntp servers lie about their active range.  When
11709               ;; this happens, the active range can be in the millions.
11710               ;; Use a compressed range to avoid creating a huge list.
11711               (gnus-range-difference (list gnus-newsgroup-active) old))
11712         (setq len (gnus-range-length older))
11713         (cond
11714          ((null older) nil)
11715          ((numberp all)
11716           (if (< all len)
11717               (let ((older-range (nreverse older)))
11718                 (setq older nil)
11719
11720                 (while (> all 0)
11721                   (let* ((r (pop older-range))
11722                          (min (if (numberp r) r (car r)))
11723                          (max (if (numberp r) r (cdr r))))
11724                     (while (and (<= min max)
11725                                 (> all 0))
11726                       (push max older)
11727                       (setq all (1- all)
11728                             max (1- max))))))
11729             (setq older (gnus-uncompress-range older))))
11730          (all
11731           (setq older (gnus-uncompress-range older)))
11732          (t
11733           (when (and (numberp gnus-large-newsgroup)
11734                    (> len gnus-large-newsgroup))
11735               (let* ((cursor-in-echo-area nil)
11736                      (initial (gnus-parameter-large-newsgroup-initial
11737                                gnus-newsgroup-name))
11738                      (input
11739                       (read-string
11740                        (format
11741                         "How many articles from %s (%s %d): "
11742                         (gnus-limit-string
11743                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11744                         (if initial "max" "default")
11745                         len)
11746                        (if initial
11747                            (cons (number-to-string initial)
11748                                  0)))))
11749                 (unless (string-match "^[ \t]*$" input)
11750                   (setq all (string-to-number input))
11751                   (if (< all len)
11752                       (let ((older-range (nreverse older)))
11753                         (setq older nil)
11754
11755                         (while (> all 0)
11756                           (let* ((r (pop older-range))
11757                                  (min (if (numberp r) r (car r)))
11758                                  (max (if (numberp r) r (cdr r))))
11759                             (while (and (<= min max)
11760                                         (> all 0))
11761                               (push max older)
11762                               (setq all (1- all)
11763                                     max (1- max))))))))))
11764           (setq older (gnus-uncompress-range older))))
11765         (if (not older)
11766             (message "No old news.")
11767           (gnus-summary-insert-articles older)
11768           (gnus-summary-limit (gnus-sorted-nunion old older))))
11769     (gnus-summary-position-point)))
11770
11771 (defun gnus-summary-insert-new-articles ()
11772   "Insert all new articles in this group."
11773   (interactive)
11774   (prog1
11775       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11776             (old-active gnus-newsgroup-active)
11777             (nnmail-fetched-sources (list t))
11778             i new)
11779         (setq gnus-newsgroup-active
11780               (gnus-activate-group gnus-newsgroup-name 'scan))
11781         (setq i (cdr gnus-newsgroup-active))
11782         (while (> i (cdr old-active))
11783           (push i new)
11784           (decf i))
11785         (if (not new)
11786             (message "No gnus is bad news")
11787           (gnus-summary-insert-articles new)
11788           (setq gnus-newsgroup-unreads
11789                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11790           (gnus-summary-limit (gnus-sorted-nunion old new))))
11791     (gnus-summary-position-point)))
11792
11793 (gnus-summary-make-all-marking-commands)
11794
11795 (gnus-ems-redefine)
11796
11797 (provide 'gnus-sum)
11798
11799 (run-hooks 'gnus-sum-load-hook)
11800
11801 ;; Local Variables:
11802 ;; coding: iso-8859-1
11803 ;; End:
11804
11805 ;;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11806 ;;; gnus-sum.el ends here