Synch to No Gnus 200409231508.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-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 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile
32   (require 'cl)
33   (defvar tool-bar-map))
34
35 (require 'gnus)
36 (require 'gnus-group)
37 (require 'gnus-spec)
38 (require 'gnus-range)
39 (require 'gnus-int)
40 (require 'gnus-undo)
41 (require 'gnus-util)
42 (require 'nnoo)
43
44 (eval-when-compile
45   (require 'mime-play)
46   (require 'static))
47
48 (eval-and-compile
49   (autoload 'pgg-decrypt-region "pgg" nil t)
50   (autoload 'pgg-verify-region "pgg" nil t))
51
52 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
53 (autoload 'gnus-cache-write-active "gnus-cache")
54 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
55 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
56 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
57 (autoload 'mm-uu-dissect "mm-uu")
58 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
59   "Deuglify broken Outlook (Express) articles and redisplay."
60   t)
61 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
62 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
63 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-fetch-old-headers nil
73   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
74 If an unread article in the group refers to an older, already read (or
75 just marked as read) article, the old article will not normally be
76 displayed in the Summary buffer.  If this variable is t, Gnus
77 will attempt to grab the headers to the old articles, and thereby
78 build complete threads.  If it has the value `some', only enough
79 headers to connect otherwise loose threads will be displayed.  This
80 variable can also be a number.  In that case, no more than that number
81 of old headers will be fetched.  If it has the value `invisible', all
82 old headers will be fetched, but none will be displayed.
83
84 The server has to support NOV for any of this to work."
85   :group 'gnus-thread
86   :type '(choice (const :tag "off" nil)
87                  (const :tag "on" t)
88                  (const some)
89                  (const invisible)
90                  number
91                  (sexp :menu-tag "other" t)))
92
93 (defcustom gnus-refer-thread-limit 200
94   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
95 If t, fetch all the available old headers."
96   :group 'gnus-thread
97   :type '(choice number
98                  (sexp :menu-tag "other" t)))
99
100 (defcustom gnus-summary-make-false-root 'adopt
101   "*nil means that Gnus won't gather loose threads.
102 If the root of a thread has expired or been read in a previous
103 session, the information necessary to build a complete thread has been
104 lost.  Instead of having many small sub-threads from this original thread
105 scattered all over the summary buffer, Gnus can gather them.
106
107 If non-nil, Gnus will try to gather all loose sub-threads from an
108 original thread into one large thread.
109
110 If this variable is non-nil, it should be one of `none', `adopt',
111 `dummy' or `empty'.
112
113 If this variable is `none', Gnus will not make a false root, but just
114 present the sub-threads after another.
115 If this variable is `dummy', Gnus will create a dummy root that will
116 have all the sub-threads as children.
117 If this variable is `adopt', Gnus will make one of the \"children\"
118 the parent and mark all the step-children as such.
119 If this variable is `empty', the \"children\" are printed with empty
120 subject fields.  (Or rather, they will be printed with a string
121 given by the `gnus-summary-same-subject' variable.)"
122   :group 'gnus-thread
123   :type '(choice (const :tag "off" nil)
124                  (const none)
125                  (const dummy)
126                  (const adopt)
127                  (const empty)))
128
129 (defcustom gnus-summary-make-false-root-always nil
130   "Always make a false dummy root."
131   :version "21.4"
132   :group 'gnus-thread
133   :type 'boolean)
134
135 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
136   "*A regexp to match subjects to be excluded from loose thread gathering.
137 As loose thread gathering is done on subjects only, that means that
138 there can be many false gatherings performed.  By rooting out certain
139 common subjects, gathering might become saner."
140   :group 'gnus-thread
141   :type 'regexp)
142
143 (defcustom gnus-summary-gather-subject-limit nil
144   "*Maximum length of subject comparisons when gathering loose threads.
145 Use nil to compare full subjects.  Setting this variable to a low
146 number will help gather threads that have been corrupted by
147 newsreaders chopping off subject lines, but it might also mean that
148 unrelated articles that have subject that happen to begin with the
149 same few characters will be incorrectly gathered.
150
151 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
152 comparing subjects."
153   :group 'gnus-thread
154   :type '(choice (const :tag "off" nil)
155                  (const fuzzy)
156                  (sexp :menu-tag "on" t)))
157
158 (defcustom gnus-simplify-subject-functions nil
159   "List of functions taking a string argument that simplify subjects.
160 The functions are applied recursively.
161
162 Useful functions to put in this list include:
163 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
164 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
165   :group 'gnus-thread
166   :type '(repeat function))
167
168 (defcustom gnus-simplify-ignored-prefixes nil
169   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
170   :group 'gnus-thread
171   :type '(choice (const :tag "off" nil)
172                  regexp))
173
174 (defcustom gnus-build-sparse-threads nil
175   "*If non-nil, fill in the gaps in threads.
176 If `some', only fill in the gaps that are needed to tie loose threads
177 together.  If `more', fill in all leaf nodes that Gnus can find.  If
178 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
179   :group 'gnus-thread
180   :type '(choice (const :tag "off" nil)
181                  (const some)
182                  (const more)
183                  (sexp :menu-tag "all" t)))
184
185 (defcustom gnus-summary-thread-gathering-function
186   'gnus-gather-threads-by-subject
187   "*Function used for gathering loose threads.
188 There are two pre-defined functions: `gnus-gather-threads-by-subject',
189 which only takes Subjects into consideration; and
190 `gnus-gather-threads-by-references', which compared the References
191 headers of the articles to find matches."
192   :group 'gnus-thread
193   :type '(radio (function-item gnus-gather-threads-by-subject)
194                 (function-item gnus-gather-threads-by-references)
195                 (function :tag "other")))
196
197 (defcustom gnus-summary-same-subject ""
198   "*String indicating that the current article has the same subject as the previous.
199 This variable will only be used if the value of
200 `gnus-summary-make-false-root' is `empty'."
201   :group 'gnus-summary-format
202   :type 'string)
203
204 (defcustom gnus-summary-goto-unread t
205   "*If t, many commands will go to the next unread article.
206 This applies to marking commands as well as other commands that
207 \"naturally\" select the next article, like, for instance, `SPC' at
208 the end of an article.
209
210 If nil, the marking commands do NOT go to the next unread article
211 \(they go to the next article instead).  If `never', commands that
212 usually go to the next unread article, will go to the next article,
213 whether it is read or not."
214   :group 'gnus-summary-marks
215   :link '(custom-manual "(gnus)Setting Marks")
216   :type '(choice (const :tag "off" nil)
217                  (const never)
218                  (sexp :menu-tag "on" t)))
219
220 (defcustom gnus-summary-default-score 0
221   "*Default article score level.
222 All scores generated by the score files will be added to this score.
223 If this variable is nil, scoring will be disabled."
224   :group 'gnus-score-default
225   :type '(choice (const :tag "disable")
226                  integer))
227
228 (defcustom gnus-summary-default-high-score 0
229   "*Default threshold for a high scored article.
230 An article will be highlighted as high scored if its score is greater
231 than this score."
232   :version "21.4"
233   :group 'gnus-score-default
234   :type 'integer)
235
236 (defcustom gnus-summary-default-low-score 0
237   "*Default threshold for a low scored article.
238 An article will be highlighted as low scored if its score is smaller
239 than this score."
240   :version "21.4"
241   :group 'gnus-score-default
242   :type 'integer)
243
244 (defcustom gnus-summary-zcore-fuzz 0
245   "*Fuzziness factor for the zcore in the summary buffer.
246 Articles with scores closer than this to `gnus-summary-default-score'
247 will not be marked."
248   :group 'gnus-summary-format
249   :type 'integer)
250
251 (defcustom gnus-simplify-subject-fuzzy-regexp nil
252   "*Strings to be removed when doing fuzzy matches.
253 This can either be a regular expression or list of regular expressions
254 that will be removed from subject strings if fuzzy subject
255 simplification is selected."
256   :group 'gnus-thread
257   :type '(repeat regexp))
258
259 (defcustom gnus-show-threads t
260   "*If non-nil, display threads in summary mode."
261   :group 'gnus-thread
262   :type 'boolean)
263
264 (defcustom gnus-thread-hide-subtree nil
265   "*If non-nil, hide all threads initially.
266 This can be a predicate specifier which says which threads to hide.
267 If threads are hidden, you have to run the command
268 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
269 to expose hidden threads."
270   :group 'gnus-thread
271   :type '(radio (sexp :format "Non-nil\n"
272                       :match (lambda (widget value)
273                                (not (or (consp value) (functionp value))))
274                       :value t)
275                 (const nil)
276                 (sexp :tag "Predicate specifier" :size 0)))
277
278 (defcustom gnus-thread-hide-killed t
279   "*If non-nil, hide killed threads automatically."
280   :group 'gnus-thread
281   :type 'boolean)
282
283 (defcustom gnus-thread-ignore-subject t
284   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
285 If nil, articles that have different subjects from their parents will
286 start separate threads."
287   :group 'gnus-thread
288   :type 'boolean)
289
290 (defcustom gnus-thread-operation-ignore-subject t
291   "*If non-nil, subjects will be ignored when doing thread commands.
292 This affects commands like `gnus-summary-kill-thread' and
293 `gnus-summary-lower-thread'.
294
295 If this variable is nil, articles in the same thread with different
296 subjects will not be included in the operation in question.  If this
297 variable is `fuzzy', only articles that have subjects that are fuzzily
298 equal will be included."
299   :group 'gnus-thread
300   :type '(choice (const :tag "off" nil)
301                  (const fuzzy)
302                  (sexp :tag "on" t)))
303
304 (defcustom gnus-thread-indent-level 4
305   "*Number that says how much each sub-thread should be indented."
306   :group 'gnus-thread
307   :type 'integer)
308
309 (defcustom gnus-auto-extend-newsgroup t
310   "*If non-nil, extend newsgroup forward and backward when requested."
311   :group 'gnus-summary-choose
312   :type 'boolean)
313
314 (defcustom gnus-auto-select-first t
315   "*If non-nil, select the article under point.
316 Which article this is is controlled by the `gnus-auto-select-subject'
317 variable.
318
319 If you want to prevent automatic selection of articles in some
320 newsgroups, set the variable to nil in `gnus-select-group-hook'."
321   :group 'gnus-group-select
322   :type '(choice (const :tag "none" nil)
323                  (sexp :menu-tag "first" t)))
324
325 (defcustom gnus-auto-select-subject 'unread
326   "*Says what subject to place under point when entering a group.
327
328 This variable can either be the symbols `first' (place point on the
329 first subject), `unread' (place point on the subject line of the first
330 unread article), `best' (place point on the subject line of the
331 higest-scored article), `unseen' (place point on the subject line of
332 the first unseen article), 'unseen-or-unread' (place point on the subject
333 line of the first unseen article or, if all article have been seen, on the
334 subject line of the first unread article), or a function to be called to
335 place point on some subject line."
336   :version "21.4"
337   :group 'gnus-group-select
338   :type '(choice (const best)
339                  (const unread)
340                  (const first)
341                  (const unseen)
342                  (const unseen-or-unread)))
343
344 (defcustom gnus-dont-select-after-jump-to-other-group nil
345   "If non-nil, don't select the first unread article after entering the
346 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
347 it is depend on the value of `gnus-auto-select-first' whether to select
348 or not."
349   :group 'gnus-group-select
350   :type 'boolean)
351
352 (defcustom gnus-auto-select-next t
353   "*If non-nil, offer to go to the next group from the end of the previous.
354 If the value is t and the next newsgroup is empty, Gnus will exit
355 summary mode and go back to group mode.  If the value is neither nil
356 nor t, Gnus will select the following unread newsgroup.  In
357 particular, if the value is the symbol `quietly', the next unread
358 newsgroup will be selected without any confirmation, and if it is
359 `almost-quietly', the next group will be selected without any
360 confirmation if you are located on the last article in the group.
361 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
362 will go to the next group without confirmation."
363   :group 'gnus-summary-maneuvering
364   :type '(choice (const :tag "off" nil)
365                  (const quietly)
366                  (const almost-quietly)
367                  (const slightly-quietly)
368                  (sexp :menu-tag "on" t)))
369
370 (defcustom gnus-auto-select-same nil
371   "*If non-nil, select the next article with the same subject.
372 If there are no more articles with the same subject, go to
373 the first unread article."
374   :group 'gnus-summary-maneuvering
375   :type 'boolean)
376
377 (defcustom gnus-auto-goto-ignores 'unfetched
378   "*Says how to handle unfetched articles when maneuvering.
379
380 This variable can either be the symbols nil (maneuver to any
381 article), `undownloaded' (maneuvering while unplugged ignores articles
382 that have not been fetched), `always-undownloaded' (maneuvering always
383 ignores articles that have not been fetched), `unfetched' (maneuvering
384 ignores articles whose headers have not been fetched).
385
386 NOTE: The list of unfetched articles will always be nil when plugged
387 and, when unplugged, a subset of the undownloaded article list."
388   :version "21.4"
389   :group 'gnus-summary-maneuvering
390   :type '(choice (const :tag "None" nil)
391                  (const :tag "Undownloaded when unplugged" undownloaded)
392                  (const :tag "Undownloaded" always-undownloaded)
393                  (const :tag "Unfetched" unfetched)))
394
395 (defcustom gnus-summary-check-current nil
396   "*If non-nil, consider the current article when moving.
397 The \"unread\" movement commands will stay on the same line if the
398 current article is unread."
399   :group 'gnus-summary-maneuvering
400   :type 'boolean)
401
402 (defcustom gnus-auto-center-summary 2
403   "*If non-nil, always center the current summary buffer.
404 In particular, if `vertical' do only vertical recentering.  If non-nil
405 and non-`vertical', do both horizontal and vertical recentering."
406   :group 'gnus-summary-maneuvering
407   :type '(choice (const :tag "none" nil)
408                  (const vertical)
409                  (integer :tag "height")
410                  (sexp :menu-tag "both" t)))
411
412 (defvar gnus-auto-center-group t
413   "*If non-nil, always center the group buffer.")
414
415 (defcustom gnus-show-all-headers nil
416   "*If non-nil, don't hide any headers."
417   :group 'gnus-article-hiding
418   :group 'gnus-article-headers
419   :type 'boolean)
420
421 (defcustom gnus-summary-ignore-duplicates nil
422   "*If non-nil, ignore articles with identical Message-ID headers."
423   :group 'gnus-summary
424   :type 'boolean)
425
426 (defcustom gnus-single-article-buffer t
427   "*If non-nil, display all articles in the same buffer.
428 If nil, each group will get its own article buffer."
429   :group 'gnus-article-various
430   :type 'boolean)
431
432 (defcustom gnus-break-pages t
433   "*If non-nil, do page breaking on articles.
434 The page delimiter is specified by the `gnus-page-delimiter'
435 variable."
436   :group 'gnus-article-various
437   :type 'boolean)
438
439 (defcustom gnus-show-mime t
440   "*If non-nil, do mime processing of articles.
441 The articles will simply be fed to the function given by
442 `gnus-article-display-method-for-mime'."
443   :group 'gnus-article-mime
444   :type 'boolean)
445
446 (defcustom gnus-move-split-methods nil
447   "*Variable used to suggest where articles are to be moved to.
448 It uses the same syntax as the `gnus-split-methods' variable.
449 However, whereas `gnus-split-methods' specifies file names as targets,
450 this variable specifies group names."
451   :group 'gnus-summary-mail
452   :type '(repeat (choice (list :value (fun) function)
453                          (cons :value ("" "") regexp (repeat string))
454                          (sexp :value nil))))
455
456 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
457   "Function used to compute default prefix for article move/copy/etc prompts.
458 The function should take one argument, a group name, and return a
459 string with the suggested prefix."
460   :group 'gnus-summary-mail
461   :type 'function)
462
463 (defcustom gnus-unread-mark ?\ ;;;Whitespace
464   "*Mark used for unread articles."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-ticked-mark ?!
469   "*Mark used for ticked articles."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-dormant-mark ??
474   "*Mark used for dormant articles."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-del-mark ?r
479   "*Mark used for del'd articles."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-read-mark ?R
484   "*Mark used for read articles."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-expirable-mark ?E
489   "*Mark used for expirable articles."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-killed-mark ?K
494   "*Mark used for killed articles."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-spam-mark ?$
499   "*Mark used for spam articles."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-souped-mark ?F
504   "*Mark used for souped articles."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-kill-file-mark ?X
509   "*Mark used for articles killed by kill files."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-low-score-mark ?Y
514   "*Mark used for articles with a low score."
515   :group 'gnus-summary-marks
516   :type 'character)
517
518 (defcustom gnus-catchup-mark ?C
519   "*Mark used for articles that are caught up."
520   :group 'gnus-summary-marks
521   :type 'character)
522
523 (defcustom gnus-replied-mark ?A
524   "*Mark used for articles that have been replied to."
525   :group 'gnus-summary-marks
526   :type 'character)
527
528 (defcustom gnus-forwarded-mark ?F
529   "*Mark used for articles that have been forwarded."
530   :version "21.4"
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-recent-mark ?N
535   "*Mark used for articles that are recent."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-cached-mark ?*
540   "*Mark used for articles that are in the cache."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-saved-mark ?S
545   "*Mark used for articles that have been saved."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-unseen-mark ?.
550   "*Mark used for articles that haven't been seen."
551   :version "21.4"
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-no-mark ?\ ;;;Whitespace
556   "*Mark used for articles that have no other secondary mark."
557   :version "21.4"
558   :group 'gnus-summary-marks
559   :type 'character)
560
561 (defcustom gnus-ancient-mark ?O
562   "*Mark used for ancient articles."
563   :group 'gnus-summary-marks
564   :type 'character)
565
566 (defcustom gnus-sparse-mark ?Q
567   "*Mark used for sparsely reffed articles."
568   :group 'gnus-summary-marks
569   :type 'character)
570
571 (defcustom gnus-canceled-mark ?G
572   "*Mark used for canceled articles."
573   :group 'gnus-summary-marks
574   :type 'character)
575
576 (defcustom gnus-duplicate-mark ?M
577   "*Mark used for duplicate articles."
578   :group 'gnus-summary-marks
579   :type 'character)
580
581 (defcustom gnus-undownloaded-mark ?-
582   "*Mark used for articles that weren't downloaded."
583   :group 'gnus-summary-marks
584   :type 'character)
585
586 (defcustom gnus-downloaded-mark ?+
587   "*Mark used for articles that were downloaded."
588   :group 'gnus-summary-marks
589   :type 'character)
590
591 (defcustom gnus-downloadable-mark ?%
592   "*Mark used for articles that are to be downloaded."
593   :group 'gnus-summary-marks
594   :type 'character)
595
596 (defcustom gnus-unsendable-mark ?=
597   "*Mark used for articles that won't be sent."
598   :group 'gnus-summary-marks
599   :type 'character)
600
601 (defcustom gnus-score-over-mark ?+
602   "*Score mark used for articles with high scores."
603   :group 'gnus-summary-marks
604   :type 'character)
605
606 (defcustom gnus-score-below-mark ?-
607   "*Score mark used for articles with low scores."
608   :group 'gnus-summary-marks
609   :type 'character)
610
611 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
612   "*There is no thread under the article."
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-not-empty-thread-mark ?=
617   "*There is a thread under the article."
618   :group 'gnus-summary-marks
619   :type 'character)
620
621 (defcustom gnus-view-pseudo-asynchronously nil
622   "*If non-nil, Gnus will view pseudo-articles asynchronously."
623   :group 'gnus-extract-view
624   :type 'boolean)
625
626 (defcustom gnus-auto-expirable-marks
627   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
628         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
629         gnus-souped-mark gnus-duplicate-mark)
630   "*The list of marks converted into expiration if a group is auto-expirable."
631   :version "21.1"
632   :group 'gnus-summary
633   :type '(repeat character))
634
635 (defcustom gnus-inhibit-user-auto-expire t
636   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
637   :version "21.1"
638   :group 'gnus-summary
639   :type 'boolean)
640
641 (defcustom gnus-view-pseudos nil
642   "*If `automatic', pseudo-articles will be viewed automatically.
643 If `not-confirm', pseudos will be viewed automatically, and the user
644 will not be asked to confirm the command."
645   :group 'gnus-extract-view
646   :type '(choice (const :tag "off" nil)
647                  (const automatic)
648                  (const not-confirm)))
649
650 (defcustom gnus-view-pseudos-separately t
651   "*If non-nil, one pseudo-article will be created for each file to be viewed.
652 If nil, all files that use the same viewing command will be given as a
653 list of parameters to that command."
654   :group 'gnus-extract-view
655   :type 'boolean)
656
657 (defcustom gnus-insert-pseudo-articles t
658   "*If non-nil, insert pseudo-articles when decoding articles."
659   :group 'gnus-extract-view
660   :type 'boolean)
661
662 (defcustom gnus-summary-dummy-line-format
663   "   %(:                             :%) %S\n"
664   "*The format specification for the dummy roots in the summary buffer.
665 It works along the same lines as a normal formatting string,
666 with some simple extensions.
667
668 %S  The subject
669
670 General format specifiers can also be used.
671 See `(gnus)Formatting Variables'."
672   :link '(custom-manual "(gnus)Formatting Variables")
673   :group 'gnus-threading
674   :type 'string)
675
676 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
677   "*The format specification for the summary mode line.
678 It works along the same lines as a normal formatting string,
679 with some simple extensions:
680
681 %G  Group name
682 %p  Unprefixed group name
683 %A  Current article number
684 %z  Current article score
685 %V  Gnus version
686 %U  Number of unread articles in the group
687 %e  Number of unselected articles in the group
688 %Z  A string with unread/unselected article counts
689 %g  Shortish group name
690 %S  Subject of the current article
691 %u  User-defined spec
692 %s  Current score file name
693 %d  Number of dormant articles
694 %r  Number of articles that have been marked as read in this session
695 %E  Number of articles expunged by the score files"
696   :group 'gnus-summary-format
697   :type 'string)
698
699 (defcustom gnus-list-identifiers nil
700   "Regexp that matches list identifiers to be removed from subject.
701 This can also be a list of regexps."
702   :version "21.1"
703   :group 'gnus-summary-format
704   :group 'gnus-article-hiding
705   :type '(choice (const :tag "none" nil)
706                  (regexp :value ".*")
707                  (repeat :value (".*") regexp)))
708
709 (defcustom gnus-summary-mark-below 0
710   "*Mark all articles with a score below this variable as read.
711 This variable is local to each summary buffer and usually set by the
712 score file."
713   :group 'gnus-score-default
714   :type 'integer)
715
716 (defun gnus-widget-reversible-match (widget value)
717   "Ignoring WIDGET, convert VALUE to internal form.
718 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
719   ;; (debug value)
720   (or (symbolp value)
721       (and (listp value)
722            (eq (length value) 2)
723            (eq (nth 0 value) 'not)
724            (symbolp (nth 1 value)))))
725
726 (defun gnus-widget-reversible-to-internal (widget value)
727   "Ignoring WIDGET, convert VALUE to internal form.
728 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
729 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
730   ;; (debug value)
731   (if (atom value)
732       (list value nil)
733     (list (nth 1 value) t)))
734
735 (defun gnus-widget-reversible-to-external (widget value)
736   "Ignoring WIDGET, convert VALUE to external form.
737 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
738 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
739   ;; (debug value)
740   (if (nth 1 value)
741       (list 'not (nth 0 value))
742     (nth 0 value)))
743
744 (define-widget 'gnus-widget-reversible 'group
745   "A `group' that convert values."
746   :match 'gnus-widget-reversible-match
747   :value-to-internal 'gnus-widget-reversible-to-internal
748   :value-to-external 'gnus-widget-reversible-to-external)
749
750 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
751   "*List of functions used for sorting articles in the summary buffer.
752
753 Each function takes two articles and returns non-nil if the first
754 article should be sorted before the other.  If you use more than one
755 function, the primary sort function should be the last.  You should
756 probably always include `gnus-article-sort-by-number' in the list of
757 sorting functions -- preferably first.  Also note that sorting by date
758 is often much slower than sorting by number, and the sorting order is
759 very similar.  (Sorting by date means sorting by the time the message
760 was sent, sorting by number means sorting by arrival time.)
761
762 Each item can also be a list `(not F)' where F is a function;
763 this reverses the sort order.
764
765 Ready-made functions include `gnus-article-sort-by-number',
766 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
767 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
768 and `gnus-article-sort-by-score'.
769
770 When threading is turned on, the variable `gnus-thread-sort-functions'
771 controls how articles are sorted."
772   :group 'gnus-summary-sort
773   :type '(repeat (gnus-widget-reversible
774                   (choice (function-item gnus-article-sort-by-number)
775                           (function-item gnus-article-sort-by-author)
776                           (function-item gnus-article-sort-by-subject)
777                           (function-item gnus-article-sort-by-date)
778                           (function-item gnus-article-sort-by-score)
779                           (function-item gnus-article-sort-by-random)
780                           (function :tag "other"))
781                   (boolean :tag "Reverse order"))))
782
783
784 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
785   "*List of functions used for sorting threads in the summary buffer.
786 By default, threads are sorted by article number.
787
788 Each function takes two threads and returns non-nil if the first
789 thread should be sorted before the other.  If you use more than one
790 function, the primary sort function should be the last.  You should
791 probably always include `gnus-thread-sort-by-number' in the list of
792 sorting functions -- preferably first.  Also note that sorting by date
793 is often much slower than sorting by number, and the sorting order is
794 very similar.  (Sorting by date means sorting by the time the message
795 was sent, sorting by number means sorting by arrival time.)
796
797 Each list item can also be a list `(not F)' where F is a
798 function; this specifies reversed sort order.
799
800 Ready-made functions include `gnus-thread-sort-by-number',
801 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
802 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
803 `gnus-thread-sort-by-most-recent-number',
804 `gnus-thread-sort-by-most-recent-date',
805 `gnus-thread-sort-by-random', and
806 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
807
808 When threading is turned off, the variable
809 `gnus-article-sort-functions' controls how articles are sorted."
810   :group 'gnus-summary-sort
811   :type '(repeat 
812           (gnus-widget-reversible
813            (choice (function-item gnus-thread-sort-by-number)
814                    (function-item gnus-thread-sort-by-author)
815                    (function-item gnus-thread-sort-by-subject)
816                    (function-item gnus-thread-sort-by-date)
817                    (function-item gnus-thread-sort-by-score)
818                    (function-item gnus-thread-sort-by-most-recent-number)
819                    (function-item gnus-thread-sort-by-most-recent-date)
820                    (function-item gnus-thread-sort-by-random)
821                    (function-item gnus-thread-sort-by-total-score)
822                    (function :tag "other"))
823            (boolean :tag "Reverse order"))))
824
825 (defcustom gnus-thread-score-function '+
826   "*Function used for calculating the total score of a thread.
827
828 The function is called with the scores of the article and each
829 subthread and should then return the score of the thread.
830
831 Some functions you can use are `+', `max', or `min'."
832   :group 'gnus-summary-sort
833   :type 'function)
834
835 (defcustom gnus-summary-expunge-below nil
836   "All articles that have a score less than this variable will be expunged.
837 This variable is local to the summary buffers."
838   :group 'gnus-score-default
839   :type '(choice (const :tag "off" nil)
840                  integer))
841
842 (defcustom gnus-thread-expunge-below nil
843   "All threads that have a total score less than this variable will be expunged.
844 See `gnus-thread-score-function' for en explanation of what a
845 \"thread score\" is.
846
847 This variable is local to the summary buffers."
848   :group 'gnus-threading
849   :group 'gnus-score-default
850   :type '(choice (const :tag "off" nil)
851                  integer))
852
853 (defcustom gnus-summary-mode-hook nil
854   "*A hook for Gnus summary mode.
855 This hook is run before any variables are set in the summary buffer."
856   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
857   :group 'gnus-summary-various
858   :type 'hook)
859
860 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
861 (when (featurep 'xemacs)
862   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
863   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
864   (add-hook 'gnus-summary-mode-hook
865             'gnus-xmas-switch-horizontal-scrollbar-off))
866
867 (defcustom gnus-summary-menu-hook nil
868   "*Hook run after the creation of the summary mode menu."
869   :group 'gnus-summary-visual
870   :type 'hook)
871
872 (defcustom gnus-summary-exit-hook nil
873   "*A hook called on exit from the summary buffer.
874 It will be called with point in the group buffer."
875   :group 'gnus-summary-exit
876   :type 'hook)
877
878 (defcustom gnus-summary-prepare-hook nil
879   "*A hook called after the summary buffer has been generated.
880 If you want to modify the summary buffer, you can use this hook."
881   :group 'gnus-summary-various
882   :type 'hook)
883
884 (defcustom gnus-summary-prepared-hook nil
885   "*A hook called as the last thing after the summary buffer has been generated."
886   :group 'gnus-summary-various
887   :type 'hook)
888
889 (defcustom gnus-summary-generate-hook nil
890   "*A hook run just before generating the summary buffer.
891 This hook is commonly used to customize threading variables and the
892 like."
893   :group 'gnus-summary-various
894   :type 'hook)
895
896 (defcustom gnus-select-group-hook nil
897   "*A hook called when a newsgroup is selected.
898
899 If you'd like to simplify subjects like the
900 `gnus-summary-next-same-subject' command does, you can use the
901 following hook:
902
903  (add-hook gnus-select-group-hook
904            (lambda ()
905              (mapcar (lambda (header)
906                        (mail-header-set-subject
907                         header
908                         (gnus-simplify-subject
909                          (mail-header-subject header) 're-only)))
910                      gnus-newsgroup-headers)))"
911   :group 'gnus-group-select
912   :type 'hook)
913
914 (defcustom gnus-select-article-hook nil
915   "*A hook called when an article is selected."
916   :group 'gnus-summary-choose
917   :options '(gnus-agent-fetch-selected-article)
918   :type 'hook)
919
920 (defcustom gnus-visual-mark-article-hook
921   (list 'gnus-highlight-selected-summary)
922   "*Hook run after selecting an article in the summary buffer.
923 It is meant to be used for highlighting the article in some way.  It
924 is not run if `gnus-visual' is nil."
925   :group 'gnus-summary-visual
926   :type 'hook)
927
928 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
929   "*A hook called before parsing the headers."
930   :group 'gnus-various
931   :type 'hook)
932
933 (defcustom gnus-exit-group-hook nil
934   "*A hook called when exiting summary mode.
935 This hook is not called from the non-updating exit commands like `Q'."
936   :group 'gnus-various
937   :type 'hook)
938
939 (defcustom gnus-summary-update-hook
940   (list 'gnus-summary-highlight-line)
941   "*A hook called when a summary line is changed.
942 The hook will not be called if `gnus-visual' is nil.
943
944 The default function `gnus-summary-highlight-line' will
945 highlight the line according to the `gnus-summary-highlight'
946 variable."
947   :group 'gnus-summary-visual
948   :type 'hook)
949
950 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
951   "*A hook called when an article is selected for the first time.
952 The hook is intended to mark an article as read (or unread)
953 automatically when it is selected."
954   :group 'gnus-summary-choose
955   :type 'hook)
956
957 (defcustom gnus-group-no-more-groups-hook nil
958   "*A hook run when returning to group mode having no more (unread) groups."
959   :group 'gnus-group-select
960   :type 'hook)
961
962 (defcustom gnus-ps-print-hook nil
963   "*A hook run before ps-printing something from Gnus."
964   :group 'gnus-summary
965   :type 'hook)
966
967 (defcustom gnus-summary-article-move-hook nil
968   "*A hook called after an article is moved, copied, respooled, or crossposted."
969   :group 'gnus-summary
970   :type 'hook)
971
972 (defcustom gnus-summary-article-delete-hook nil
973   "*A hook called after an article is deleted."
974   :group 'gnus-summary
975   :type 'hook)
976
977 (defcustom gnus-summary-article-expire-hook nil
978   "*A hook called after an article is expired."
979   :group 'gnus-summary
980   :type 'hook)
981
982 (defcustom gnus-summary-display-arrow
983   (and (fboundp 'display-graphic-p)
984        (display-graphic-p))
985   "*If non-nil, display an arrow highlighting the current article."
986   :version "21.4"
987   :group 'gnus-summary
988   :type 'boolean)
989
990 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
991   "Face used for highlighting the current article in the summary buffer."
992   :group 'gnus-summary-visual
993   :type 'face)
994
995 (defvar gnus-tmp-downloaded nil)
996
997 (defcustom gnus-summary-highlight
998   '(((eq mark gnus-canceled-mark)
999      . gnus-summary-cancelled-face)
1000     ((and uncached (> score default-high))
1001      . gnus-summary-high-undownloaded-face)
1002     ((and uncached (< score default-low))
1003      . gnus-summary-low-undownloaded-face)
1004     (uncached
1005      . gnus-summary-normal-undownloaded-face)
1006     ((and (> score default-high)
1007           (or (eq mark gnus-dormant-mark)
1008               (eq mark gnus-ticked-mark)))
1009      . gnus-summary-high-ticked-face)
1010     ((and (< score default-low)
1011           (or (eq mark gnus-dormant-mark)
1012               (eq mark gnus-ticked-mark)))
1013      . gnus-summary-low-ticked-face)
1014     ((or (eq mark gnus-dormant-mark)
1015          (eq mark gnus-ticked-mark))
1016      . gnus-summary-normal-ticked-face)
1017     ((and (> score default-high) (eq mark gnus-ancient-mark))
1018      . gnus-summary-high-ancient-face)
1019     ((and (< score default-low) (eq mark gnus-ancient-mark))
1020      . gnus-summary-low-ancient-face)
1021     ((eq mark gnus-ancient-mark)
1022      . gnus-summary-normal-ancient-face)
1023     ((and (> score default-high) (eq mark gnus-unread-mark))
1024      . gnus-summary-high-unread-face)
1025     ((and (< score default-low) (eq mark gnus-unread-mark))
1026      . gnus-summary-low-unread-face)
1027     ((eq mark gnus-unread-mark)
1028      . gnus-summary-normal-unread-face)
1029     ((> score default-high)
1030      . gnus-summary-high-read-face)
1031     ((< score default-low)
1032      . gnus-summary-low-read-face)
1033     (t
1034      . gnus-summary-normal-read-face))
1035   "*Controls the highlighting of summary buffer lines.
1036
1037 A list of (FORM . FACE) pairs.  When deciding how a a particular
1038 summary line should be displayed, each form is evaluated.  The content
1039 of the face field after the first true form is used.  You can change
1040 how those summary lines are displayed, by editing the face field.
1041
1042 You can use the following variables in the FORM field.
1043
1044 score:        The article's score
1045 default:      The default article score.
1046 default-high: The default score for high scored articles.
1047 default-low:  The default score for low scored articles.
1048 below:        The score below which articles are automatically marked as read.
1049 mark:         The article's mark.
1050 uncached:     Non-nil if the article is uncached."
1051   :group 'gnus-summary-visual
1052   :type '(repeat (cons (sexp :tag "Form" nil)
1053                        face)))
1054
1055 (defcustom gnus-alter-header-function nil
1056   "Function called to allow alteration of article header structures.
1057 The function is called with one parameter, the article header vector,
1058 which it may alter in any way."
1059   :type '(choice (const :tag "None" nil)
1060                  function)
1061   :group 'gnus-summary)
1062
1063 (defvar gnus-decode-encoded-word-function
1064   (mime-find-field-decoder 'From 'nov)
1065   "Variable that says which function should be used to decode a string with encoded words.")
1066
1067 (defcustom gnus-extra-headers '(To Newsgroups)
1068   "*Extra headers to parse."
1069   :version "21.1"
1070   :group 'gnus-summary
1071   :type '(repeat symbol))
1072
1073 (defcustom gnus-ignored-from-addresses
1074   (and user-mail-address (regexp-quote user-mail-address))
1075   "*Regexp of From headers that may be suppressed in favor of To headers."
1076   :version "21.1"
1077   :group 'gnus-summary
1078   :type 'regexp)
1079
1080 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1081   "List of charsets that should be ignored.
1082 When these charsets are used in the \"charset\" parameter, the
1083 default charset will be used instead."
1084   :version "21.1"
1085   :type '(repeat symbol)
1086   :group 'gnus-charset)
1087
1088 (gnus-define-group-parameter
1089  ignored-charsets
1090  :type list
1091  :function-document
1092  "Return the ignored charsets of GROUP."
1093  :variable gnus-group-ignored-charsets-alist
1094  :variable-default
1095  '(("alt\\.chinese\\.text" iso-8859-1))
1096  :variable-document
1097  "Alist of regexps (to match group names) and charsets that should be ignored.
1098 When these charsets are used in the \"charset\" parameter, the
1099 default charset will be used instead."
1100  :variable-group gnus-charset
1101  :variable-type '(repeat (cons (regexp :tag "Group")
1102                                (repeat symbol)))
1103  :parameter-type '(choice :tag "Ignored charsets"
1104                           :value nil
1105                           (repeat (symbol)))
1106  :parameter-document       "\
1107 List of charsets that should be ignored.
1108
1109 When these charsets are used in the \"charset\" parameter, the
1110 default charset will be used instead.")
1111
1112 (defcustom gnus-group-highlight-words-alist nil
1113   "Alist of group regexps and highlight regexps.
1114 This variable uses the same syntax as `gnus-emphasis-alist'."
1115   :version "21.1"
1116   :type '(repeat (cons (regexp :tag "Group")
1117                        (repeat (list (regexp :tag "Highlight regexp")
1118                                      (number :tag "Group for entire word" 0)
1119                                      (number :tag "Group for displayed part" 0)
1120                                      (symbol :tag "Face"
1121                                              gnus-emphasis-highlight-words)))))
1122   :group 'gnus-summary-visual)
1123
1124 (defcustom gnus-use-wheel nil
1125   "Use Intelli-mouse on summary movement"
1126   :type 'boolean
1127   :group 'gnus-summary-maneuvering)
1128
1129 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1130   "Amount to scroll messages by spinning the mouse wheel.
1131 This is actually a cons cell, where the first item is the amount to scroll
1132 on a normal wheel event, and the second is the amount to scroll when the
1133 wheel is moved with the shift key depressed."
1134   :type '(cons (integer :tag "Shift") integer)
1135   :group 'gnus-summary-maneuvering)
1136
1137 (defcustom gnus-wheel-edge-resistance 2
1138   "How hard it should be to change the current article
1139 by moving the mouse over the edge of the article window."
1140   :type 'integer
1141   :group 'gnus-summary-maneuvering)
1142
1143 (defcustom gnus-summary-show-article-charset-alist
1144   nil
1145   "Alist of number and charset.
1146 The article will be shown with the charset corresponding to the
1147 numbered argument.
1148 For example: ((1 . cn-gb-2312) (2 . big5))."
1149   :version "21.1"
1150   :type '(repeat (cons (number :tag "Argument" 1)
1151                        (symbol :tag "Charset")))
1152   :group 'gnus-charset)
1153
1154 (defcustom gnus-preserve-marks t
1155   "Whether marks are preserved when moving, copying and respooling messages."
1156   :version "21.1"
1157   :type 'boolean
1158   :group 'gnus-summary-marks)
1159
1160 (defcustom gnus-alter-articles-to-read-function nil
1161   "Function to be called to alter the list of articles to be selected."
1162   :type '(choice (const nil) function)
1163   :group 'gnus-summary)
1164
1165 (defcustom gnus-orphan-score nil
1166   "*All orphans get this score added.  Set in the score file."
1167   :group 'gnus-score-default
1168   :type '(choice (const nil)
1169                  integer))
1170
1171 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1172   "*A regexp to match MIME parts when saving multiple parts of a
1173 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1174 This regexp will be used by default when prompting the user for which
1175 type of files to save."
1176   :group 'gnus-summary
1177   :type 'regexp)
1178
1179 (defcustom gnus-read-all-available-headers nil
1180   "Whether Gnus should parse all headers made available to it.
1181 This is mostly relevant for slow back ends where the user may
1182 wish to widen the summary buffer to include all headers
1183 that were fetched.  Say, for nnultimate groups."
1184   :version "21.4"
1185   :group 'gnus-summary
1186   :type '(choice boolean regexp))
1187
1188 (defcustom gnus-summary-muttprint-program "muttprint"
1189   "Command (and optional arguments) used to run Muttprint."
1190   :version "21.4"
1191   :group 'gnus-summary
1192   :type 'string)
1193
1194 (defcustom gnus-article-loose-mime t
1195   "If non-nil, don't require MIME-Version header.
1196 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1197 supply the MIME-Version header or deliberately strip it from the mail.
1198 If non-nil (the default), Gnus will treat some articles as MIME
1199 even if the MIME-Version header is missing."
1200   :version "21.4"
1201   :type 'boolean
1202   :group 'gnus-article-mime)
1203
1204 (defcustom gnus-article-emulate-mime t
1205   "If non-nil, use MIME emulation for uuencode and the like.
1206 This means that Gnus will search message bodies for text that look
1207 like uuencoded bits, yEncoded bits, and so on, and present that using
1208 the normal Gnus MIME machinery."
1209   :version "21.4"
1210   :type 'boolean
1211   :group 'gnus-article-mime)
1212
1213 ;;; Internal variables
1214
1215 (defvar gnus-summary-display-cache nil)
1216 (defvar gnus-article-mime-handles nil)
1217 (defvar gnus-article-decoded-p nil)
1218 (defvar gnus-article-charset nil)
1219 (defvar gnus-article-ignored-charsets nil)
1220 (defvar gnus-scores-exclude-files nil)
1221 (defvar gnus-page-broken nil)
1222
1223 (defvar gnus-original-article nil)
1224 (defvar gnus-article-internal-prepare-hook nil)
1225 (defvar gnus-newsgroup-process-stack nil)
1226
1227 (defvar gnus-thread-indent-array nil)
1228 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1229 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1230   "Function called to sort the articles within a thread after it has been gathered together.")
1231
1232 (defvar gnus-summary-save-parts-type-history nil)
1233 (defvar gnus-summary-save-parts-last-directory nil)
1234
1235 ;; Avoid highlighting in kill files.
1236 (defvar gnus-summary-inhibit-highlight nil)
1237 (defvar gnus-newsgroup-selected-overlay nil)
1238 (defvar gnus-inhibit-limiting nil)
1239 (defvar gnus-newsgroup-adaptive-score-file nil)
1240 (defvar gnus-current-score-file nil)
1241 (defvar gnus-current-move-group nil)
1242 (defvar gnus-current-copy-group nil)
1243 (defvar gnus-current-crosspost-group nil)
1244 (defvar gnus-newsgroup-display nil)
1245
1246 (defvar gnus-newsgroup-dependencies nil)
1247 (defvar gnus-newsgroup-adaptive nil)
1248 (defvar gnus-summary-display-article-function nil)
1249 (defvar gnus-summary-highlight-line-function nil
1250   "Function called after highlighting a summary line.")
1251
1252 (defvar gnus-summary-line-format-alist
1253   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1254     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1255     (?s gnus-tmp-subject-or-nil ?s)
1256     (?n gnus-tmp-name ?s)
1257     (?A (std11-address-string
1258          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1259     (?a (or (std11-full-name-string
1260              (car (mime-entity-read-field gnus-tmp-header 'From)))
1261             gnus-tmp-from) ?s)
1262     (?F gnus-tmp-from ?s)
1263     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1264     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1265     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1266     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1267     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1268     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1269     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1270     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1271     (?L gnus-tmp-lines ?s)
1272     (?O gnus-tmp-downloaded ?c)
1273     (?I gnus-tmp-indentation ?s)
1274     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1275     (?R gnus-tmp-replied ?c)
1276     (?\[ gnus-tmp-opening-bracket ?c)
1277     (?\] gnus-tmp-closing-bracket ?c)
1278     (?\> (make-string gnus-tmp-level ? ) ?s)
1279     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1280     (?i gnus-tmp-score ?d)
1281     (?z gnus-tmp-score-char ?c)
1282     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1283     (?U gnus-tmp-unread ?c)
1284     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1285         ?s)
1286     (?t (gnus-summary-number-of-articles-in-thread
1287          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1288         ?d)
1289     (?e (gnus-summary-number-of-articles-in-thread
1290          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1291         ?c)
1292     (?u gnus-tmp-user-defined ?s)
1293     (?P (gnus-pick-line-number) ?d)
1294     (?B gnus-tmp-thread-tree-header-string ?s)
1295     (user-date (gnus-user-date
1296                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1297   "An alist of format specifications that can appear in summary lines.
1298 These are paired with what variables they correspond with, along with
1299 the type of the variable (string, integer, character, etc).")
1300
1301 (defvar gnus-summary-dummy-line-format-alist
1302   `((?S gnus-tmp-subject ?s)
1303     (?N gnus-tmp-number ?d)
1304     (?u gnus-tmp-user-defined ?s)))
1305
1306 (defvar gnus-summary-mode-line-format-alist
1307   `((?G gnus-tmp-group-name ?s)
1308     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1309     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1310     (?A gnus-tmp-article-number ?d)
1311     (?Z gnus-tmp-unread-and-unselected ?s)
1312     (?V gnus-version ?s)
1313     (?U gnus-tmp-unread-and-unticked ?d)
1314     (?S gnus-tmp-subject ?s)
1315     (?e gnus-tmp-unselected ?d)
1316     (?u gnus-tmp-user-defined ?s)
1317     (?d (length gnus-newsgroup-dormant) ?d)
1318     (?t (length gnus-newsgroup-marked) ?d)
1319     (?h (length gnus-newsgroup-spam-marked) ?d)
1320     (?r (length gnus-newsgroup-reads) ?d)
1321     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1322     (?E gnus-newsgroup-expunged-tally ?d)
1323     (?s (gnus-current-score-file-nondirectory) ?s)))
1324
1325 (defvar gnus-last-search-regexp nil
1326   "Default regexp for article search command.")
1327
1328 (defvar gnus-summary-search-article-matched-data nil
1329   "Last matched data of article search command.  It is the local variable
1330 in `gnus-article-buffer' which consists of the list of start position,
1331 end position and text.")
1332
1333 (defvar gnus-last-shell-command nil
1334   "Default shell command on article.")
1335
1336 (defvar gnus-newsgroup-agentized nil
1337   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1338 (defvar gnus-newsgroup-begin nil)
1339 (defvar gnus-newsgroup-end nil)
1340 (defvar gnus-newsgroup-last-rmail nil)
1341 (defvar gnus-newsgroup-last-mail nil)
1342 (defvar gnus-newsgroup-last-folder nil)
1343 (defvar gnus-newsgroup-last-file nil)
1344 (defvar gnus-newsgroup-auto-expire nil)
1345 (defvar gnus-newsgroup-active nil)
1346
1347 (defvar gnus-newsgroup-data nil)
1348 (defvar gnus-newsgroup-data-reverse nil)
1349 (defvar gnus-newsgroup-limit nil)
1350 (defvar gnus-newsgroup-limits nil)
1351 (defvar gnus-summary-use-undownloaded-faces nil)
1352
1353 (defvar gnus-newsgroup-unreads nil
1354   "Sorted list of unread articles in the current newsgroup.")
1355
1356 (defvar gnus-newsgroup-unselected nil
1357   "Sorted list of unselected unread articles in the current newsgroup.")
1358
1359 (defvar gnus-newsgroup-reads nil
1360   "Alist of read articles and article marks in the current newsgroup.")
1361
1362 (defvar gnus-newsgroup-expunged-tally nil)
1363
1364 (defvar gnus-newsgroup-marked nil
1365   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1366
1367 (defvar gnus-newsgroup-spam-marked nil
1368   "List of ranges of articles that have been marked as spam.")
1369
1370 (defvar gnus-newsgroup-killed nil
1371   "List of ranges of articles that have been through the scoring process.")
1372
1373 (defvar gnus-newsgroup-cached nil
1374   "Sorted list of articles that come from the article cache.")
1375
1376 (defvar gnus-newsgroup-saved nil
1377   "List of articles that have been saved.")
1378
1379 (defvar gnus-newsgroup-kill-headers nil)
1380
1381 (defvar gnus-newsgroup-replied nil
1382   "List of articles that have been replied to in the current newsgroup.")
1383
1384 (defvar gnus-newsgroup-forwarded nil
1385   "List of articles that have been forwarded in the current newsgroup.")
1386
1387 (defvar gnus-newsgroup-recent nil
1388   "List of articles that have are recent in the current newsgroup.")
1389
1390 (defvar gnus-newsgroup-expirable nil
1391   "Sorted list of articles in the current newsgroup that can be expired.")
1392
1393 (defvar gnus-newsgroup-processable nil
1394   "List of articles in the current newsgroup that can be processed.")
1395
1396 (defvar gnus-newsgroup-downloadable nil
1397   "Sorted list of articles in the current newsgroup that can be processed.")
1398
1399 (defvar gnus-newsgroup-unfetched nil
1400   "Sorted list of articles in the current newsgroup whose headers have
1401 not been fetched into the agent.
1402
1403 This list will always be a subset of gnus-newsgroup-undownloaded.")
1404
1405 (defvar gnus-newsgroup-undownloaded nil
1406   "List of articles in the current newsgroup that haven't been downloaded.")
1407
1408 (defvar gnus-newsgroup-unsendable nil
1409   "List of articles in the current newsgroup that won't be sent.")
1410
1411 (defvar gnus-newsgroup-bookmarks nil
1412   "List of articles in the current newsgroup that have bookmarks.")
1413
1414 (defvar gnus-newsgroup-dormant nil
1415   "Sorted list of dormant articles in the current newsgroup.")
1416
1417 (defvar gnus-newsgroup-unseen nil
1418   "List of unseen articles in the current newsgroup.")
1419
1420 (defvar gnus-newsgroup-seen nil
1421   "Range of seen articles in the current newsgroup.")
1422
1423 (defvar gnus-newsgroup-articles nil
1424   "List of articles in the current newsgroup.")
1425
1426 (defvar gnus-newsgroup-scored nil
1427   "List of scored articles in the current newsgroup.")
1428
1429 (defvar gnus-newsgroup-incorporated nil
1430   "List of incorporated articles in the current newsgroup.")
1431
1432 (defvar gnus-newsgroup-headers nil
1433   "List of article headers in the current newsgroup.")
1434
1435 (defvar gnus-newsgroup-threads nil)
1436
1437 (defvar gnus-newsgroup-prepared nil
1438   "Whether the current group has been prepared properly.")
1439
1440 (defvar gnus-newsgroup-ancient nil
1441   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1442
1443 (defvar gnus-newsgroup-sparse nil)
1444
1445 (defvar gnus-current-article nil)
1446 (defvar gnus-article-current nil)
1447 (defvar gnus-current-headers nil)
1448 (defvar gnus-have-all-headers nil)
1449 (defvar gnus-last-article nil)
1450 (defvar gnus-newsgroup-history nil)
1451 (defvar gnus-newsgroup-charset nil)
1452 (defvar gnus-newsgroup-ephemeral-charset nil)
1453 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1454
1455 (defvar gnus-article-before-search nil)
1456
1457 (defvar gnus-summary-local-variables
1458   '(gnus-newsgroup-name
1459     gnus-newsgroup-begin gnus-newsgroup-end
1460     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1461     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1462     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1463     gnus-newsgroup-unselected gnus-newsgroup-marked
1464     gnus-newsgroup-spam-marked
1465     gnus-newsgroup-reads gnus-newsgroup-saved
1466     gnus-newsgroup-replied gnus-newsgroup-forwarded
1467     gnus-newsgroup-recent
1468     gnus-newsgroup-expirable
1469     gnus-newsgroup-processable gnus-newsgroup-killed
1470     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1471     gnus-newsgroup-unfetched
1472     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1473     gnus-newsgroup-seen gnus-newsgroup-articles
1474     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1475     gnus-newsgroup-headers gnus-newsgroup-threads
1476     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1477     gnus-current-article gnus-current-headers gnus-have-all-headers
1478     gnus-last-article gnus-article-internal-prepare-hook
1479     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1480     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1481     gnus-thread-expunge-below
1482     gnus-score-alist gnus-current-score-file
1483     (gnus-summary-expunge-below . global)
1484     (gnus-summary-mark-below . global)
1485     (gnus-orphan-score . global)
1486     gnus-newsgroup-active gnus-scores-exclude-files
1487     gnus-newsgroup-history gnus-newsgroup-ancient
1488     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1489     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1490     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1491     (gnus-newsgroup-expunged-tally . 0)
1492     gnus-cache-removable-articles gnus-newsgroup-cached
1493     gnus-newsgroup-data gnus-newsgroup-data-reverse
1494     gnus-newsgroup-limit gnus-newsgroup-limits
1495     gnus-newsgroup-charset gnus-newsgroup-display
1496     gnus-summary-use-undownloaded-faces
1497     gnus-newsgroup-incorporated)
1498   "Variables that are buffer-local to the summary buffers.")
1499
1500 (defvar gnus-newsgroup-variables nil
1501   "A list of variables that have separate values in different newsgroups.
1502 A list of newsgroup (summary buffer) local variables, or cons of
1503 variables and their default expressions to be evalled (when the default
1504 values are not nil), that should be made global while the summary buffer
1505 is active.
1506
1507 Note: The default expressions will be evaluated (using function `eval')
1508 before assignment to the local variable rather than just assigned to it.
1509 If the default expression is the symbol `global', that symbol will not
1510 be evaluated but the global value of the local variable will be used
1511 instead.
1512
1513 These variables can be used to set variables in the group parameters
1514 while still allowing them to affect operations done in other buffers.
1515 For example:
1516
1517 \(setq gnus-newsgroup-variables
1518      '(message-use-followup-to
1519        (gnus-visible-headers .
1520          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1521 ")
1522
1523 ;; Byte-compiler warning.
1524 (eval-when-compile
1525   ;; Bind features so that require will believe that gnus-sum has
1526   ;; already been loaded (avoids infinite recursion)
1527   (let ((features (cons 'gnus-sum features)))
1528     ;; Several of the declarations in gnus-sum are needed to load the
1529     ;; following files. Right now, these definitions have been
1530     ;; compiled but not defined (evaluated).  We could either do a
1531     ;; eval-and-compile about all of the declarations or evaluate the
1532     ;; source file.
1533     (if (boundp 'gnus-newsgroup-variables)
1534         nil
1535       (load "gnus-sum.el" t t t))
1536     (require 'gnus)
1537     (require 'gnus-art)))
1538
1539 ;; Subject simplification.
1540
1541 (defun gnus-simplify-whitespace (str)
1542   "Remove excessive whitespace from STR."
1543   ;; Multiple spaces.
1544   (while (string-match "[ \t][ \t]+" str)
1545     (setq str (concat (substring str 0 (match-beginning 0))
1546                         " "
1547                         (substring str (match-end 0)))))
1548   ;; Leading spaces.
1549   (when (string-match "^[ \t]+" str)
1550     (setq str (substring str (match-end 0))))
1551   ;; Trailing spaces.
1552   (when (string-match "[ \t]+$" str)
1553     (setq str (substring str 0 (match-beginning 0))))
1554   str)
1555
1556 (defun gnus-simplify-all-whitespace (str)
1557   "Remove all whitespace from STR."
1558   (while (string-match "[ \t\n]+" str)
1559     (setq str (replace-match "" nil nil str)))
1560   str)
1561
1562 (defsubst gnus-simplify-subject-re (subject)
1563   "Remove \"Re:\" from subject lines."
1564   (if (string-match message-subject-re-regexp subject)
1565       (substring subject (match-end 0))
1566     subject))
1567
1568 (defun gnus-simplify-subject (subject &optional re-only)
1569   "Remove `Re:' and words in parentheses.
1570 If RE-ONLY is non-nil, strip leading `Re:'s only."
1571   (let ((case-fold-search t))           ;Ignore case.
1572     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1573     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1574       (setq subject (substring subject (match-end 0))))
1575     ;; Remove uninteresting prefixes.
1576     (when (and (not re-only)
1577                gnus-simplify-ignored-prefixes
1578                (string-match gnus-simplify-ignored-prefixes subject))
1579       (setq subject (substring subject (match-end 0))))
1580     ;; Remove words in parentheses from end.
1581     (unless re-only
1582       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1583         (setq subject (substring subject 0 (match-beginning 0)))))
1584     ;; Return subject string.
1585     subject))
1586
1587 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1588 ;; all whitespace.
1589 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1590   (goto-char (point-min))
1591   (while (re-search-forward regexp nil t)
1592     (replace-match (or newtext ""))))
1593
1594 (defun gnus-simplify-buffer-fuzzy ()
1595   "Simplify string in the buffer fuzzily.
1596 The string in the accessible portion of the current buffer is simplified.
1597 It is assumed to be a single-line subject.
1598 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1599 matter is removed.  Additional things can be deleted by setting
1600 `gnus-simplify-subject-fuzzy-regexp'."
1601   (let ((case-fold-search t)
1602         (modified-tick))
1603     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1604
1605     (while (not (eq modified-tick (buffer-modified-tick)))
1606       (setq modified-tick (buffer-modified-tick))
1607       (cond
1608        ((listp gnus-simplify-subject-fuzzy-regexp)
1609         (mapcar 'gnus-simplify-buffer-fuzzy-step
1610                 gnus-simplify-subject-fuzzy-regexp))
1611        (gnus-simplify-subject-fuzzy-regexp
1612         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1613       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1614       (gnus-simplify-buffer-fuzzy-step
1615        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1616       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1617
1618     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1619     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1620     (gnus-simplify-buffer-fuzzy-step " $")
1621     (gnus-simplify-buffer-fuzzy-step "^ +")))
1622
1623 (defun gnus-simplify-subject-fuzzy (subject)
1624   "Simplify a subject string fuzzily.
1625 See `gnus-simplify-buffer-fuzzy' for details."
1626   (save-excursion
1627     (gnus-set-work-buffer)
1628     (let ((case-fold-search t))
1629       ;; Remove uninteresting prefixes.
1630       (when (and gnus-simplify-ignored-prefixes
1631                  (string-match gnus-simplify-ignored-prefixes subject))
1632         (setq subject (substring subject (match-end 0))))
1633       (insert subject)
1634       (inline (gnus-simplify-buffer-fuzzy))
1635       (buffer-string))))
1636
1637 (defsubst gnus-simplify-subject-fully (subject)
1638   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1639   (cond
1640    (gnus-simplify-subject-functions
1641     (gnus-map-function gnus-simplify-subject-functions subject))
1642    ((null gnus-summary-gather-subject-limit)
1643     (gnus-simplify-subject-re subject))
1644    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1645     (gnus-simplify-subject-fuzzy subject))
1646    ((numberp gnus-summary-gather-subject-limit)
1647     (gnus-limit-string (gnus-simplify-subject-re subject)
1648                        gnus-summary-gather-subject-limit))
1649    (t
1650     subject)))
1651
1652 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1653   "Check whether two subjects are equal.
1654 If optional argument SIMPLE-FIRST is t, first argument is already
1655 simplified."
1656   (cond
1657    ((null simple-first)
1658     (equal (gnus-simplify-subject-fully s1)
1659            (gnus-simplify-subject-fully s2)))
1660    (t
1661     (equal s1
1662            (gnus-simplify-subject-fully s2)))))
1663
1664 (defun gnus-summary-bubble-group ()
1665   "Increase the score of the current group.
1666 This is a handy function to add to `gnus-summary-exit-hook' to
1667 increase the score of each group you read."
1668   (gnus-group-add-score gnus-newsgroup-name))
1669
1670 \f
1671 ;;;
1672 ;;; Gnus summary mode
1673 ;;;
1674
1675 (put 'gnus-summary-mode 'mode-class 'special)
1676
1677 (defvar gnus-article-commands-menu)
1678
1679 ;; Non-orthogonal keys
1680
1681 (gnus-define-keys gnus-summary-mode-map
1682   " " gnus-summary-next-page
1683   "\177" gnus-summary-prev-page
1684   [delete] gnus-summary-prev-page
1685   [backspace] gnus-summary-prev-page
1686   "\r" gnus-summary-scroll-up
1687   "\M-\r" gnus-summary-scroll-down
1688   "n" gnus-summary-next-unread-article
1689   "p" gnus-summary-prev-unread-article
1690   "N" gnus-summary-next-article
1691   "P" gnus-summary-prev-article
1692   "\M-\C-n" gnus-summary-next-same-subject
1693   "\M-\C-p" gnus-summary-prev-same-subject
1694   "\M-n" gnus-summary-next-unread-subject
1695   "\M-p" gnus-summary-prev-unread-subject
1696   "." gnus-summary-first-unread-article
1697   "," gnus-summary-best-unread-article
1698   "\M-s" gnus-summary-search-article-forward
1699   "\M-r" gnus-summary-search-article-backward
1700   "<" gnus-summary-beginning-of-article
1701   ">" gnus-summary-end-of-article
1702   "j" gnus-summary-goto-article
1703   "^" gnus-summary-refer-parent-article
1704   "\M-^" gnus-summary-refer-article
1705   "u" gnus-summary-tick-article-forward
1706   "!" gnus-summary-tick-article-forward
1707   "U" gnus-summary-tick-article-backward
1708   "d" gnus-summary-mark-as-read-forward
1709   "D" gnus-summary-mark-as-read-backward
1710   "E" gnus-summary-mark-as-expirable
1711   "\M-u" gnus-summary-clear-mark-forward
1712   "\M-U" gnus-summary-clear-mark-backward
1713   "k" gnus-summary-kill-same-subject-and-select
1714   "\C-k" gnus-summary-kill-same-subject
1715   "\M-\C-k" gnus-summary-kill-thread
1716   "\M-\C-l" gnus-summary-lower-thread
1717   "e" gnus-summary-edit-article
1718   "#" gnus-summary-mark-as-processable
1719   "\M-#" gnus-summary-unmark-as-processable
1720   "\M-\C-t" gnus-summary-toggle-threads
1721   "\M-\C-s" gnus-summary-show-thread
1722   "\M-\C-h" gnus-summary-hide-thread
1723   "\M-\C-f" gnus-summary-next-thread
1724   "\M-\C-b" gnus-summary-prev-thread
1725   [(meta down)] gnus-summary-next-thread
1726   [(meta up)] gnus-summary-prev-thread
1727   "\M-\C-u" gnus-summary-up-thread
1728   "\M-\C-d" gnus-summary-down-thread
1729   "&" gnus-summary-execute-command
1730   "c" gnus-summary-catchup-and-exit
1731   "\C-w" gnus-summary-mark-region-as-read
1732   "\C-t" gnus-summary-toggle-truncation
1733   "?" gnus-summary-mark-as-dormant
1734   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1735   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1736   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1737   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1738   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1739   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1740   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1741   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1742   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1743   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1744   "=" gnus-summary-expand-window
1745   "\C-x\C-s" gnus-summary-reselect-current-group
1746   "\M-g" gnus-summary-rescan-group
1747   "w" gnus-summary-stop-page-breaking
1748   "\C-c\C-r" gnus-summary-caesar-message
1749   "\M-t" gnus-summary-toggle-mime
1750   "f" gnus-summary-followup
1751   "F" gnus-summary-followup-with-original
1752   "C" gnus-summary-cancel-article
1753   "r" gnus-summary-reply
1754   "R" gnus-summary-reply-with-original
1755   "\C-c\C-f" gnus-summary-mail-forward
1756   "o" gnus-summary-save-article
1757   "\C-o" gnus-summary-save-article-mail
1758   "|" gnus-summary-pipe-output
1759   "\M-k" gnus-summary-edit-local-kill
1760   "\M-K" gnus-summary-edit-global-kill
1761   ;; "V" gnus-version
1762   "\C-c\C-d" gnus-summary-describe-group
1763   "q" gnus-summary-exit
1764   "Q" gnus-summary-exit-no-update
1765   "\C-c\C-i" gnus-info-find-node
1766   gnus-mouse-2 gnus-mouse-pick-article
1767   "m" gnus-summary-mail-other-window
1768   "a" gnus-summary-post-news
1769   "i" gnus-summary-news-other-window
1770   "x" gnus-summary-limit-to-unread
1771   "s" gnus-summary-isearch-article
1772   "t" gnus-summary-toggle-header
1773   "g" gnus-summary-show-article
1774   "l" gnus-summary-goto-last-article
1775   "v" gnus-summary-preview-mime-message
1776   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1777   "\C-d" gnus-summary-enter-digest-group
1778   "\M-\C-d" gnus-summary-read-document
1779   "\M-\C-e" gnus-summary-edit-parameters
1780   "\M-\C-a" gnus-summary-customize-parameters
1781   "\C-c\C-b" gnus-bug
1782   "\C-c\C-n" gnus-namazu-search
1783   "*" gnus-cache-enter-article
1784   "\M-*" gnus-cache-remove-article
1785   "\M-&" gnus-summary-universal-argument
1786   "\C-l" gnus-recenter
1787   "I" gnus-summary-increase-score
1788   "L" gnus-summary-lower-score
1789   "\M-i" gnus-symbolic-argument
1790   "h" gnus-summary-select-article-buffer
1791
1792   "V" gnus-summary-score-map
1793   "X" gnus-uu-extract-map
1794   "S" gnus-summary-send-map)
1795
1796   ;; Sort of orthogonal keymap
1797 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1798   "t" gnus-summary-tick-article-forward
1799   "!" gnus-summary-tick-article-forward
1800   "d" gnus-summary-mark-as-read-forward
1801   "r" gnus-summary-mark-as-read-forward
1802   "c" gnus-summary-clear-mark-forward
1803   " " gnus-summary-clear-mark-forward
1804   "e" gnus-summary-mark-as-expirable
1805   "x" gnus-summary-mark-as-expirable
1806   "?" gnus-summary-mark-as-dormant
1807   "b" gnus-summary-set-bookmark
1808   "B" gnus-summary-remove-bookmark
1809   "#" gnus-summary-mark-as-processable
1810   "\M-#" gnus-summary-unmark-as-processable
1811   "S" gnus-summary-limit-include-expunged
1812   "C" gnus-summary-catchup
1813   "H" gnus-summary-catchup-to-here
1814   "h" gnus-summary-catchup-from-here
1815   "\C-c" gnus-summary-catchup-all
1816   "k" gnus-summary-kill-same-subject-and-select
1817   "K" gnus-summary-kill-same-subject
1818   "P" gnus-uu-mark-map)
1819
1820 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1821   "c" gnus-summary-clear-above
1822   "u" gnus-summary-tick-above
1823   "m" gnus-summary-mark-above
1824   "k" gnus-summary-kill-below)
1825
1826 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1827   "/" gnus-summary-limit-to-subject
1828   "n" gnus-summary-limit-to-articles
1829   "w" gnus-summary-pop-limit
1830   "s" gnus-summary-limit-to-subject
1831   "a" gnus-summary-limit-to-author
1832   "u" gnus-summary-limit-to-unread
1833   "m" gnus-summary-limit-to-marks
1834   "M" gnus-summary-limit-exclude-marks
1835   "v" gnus-summary-limit-to-score
1836   "*" gnus-summary-limit-include-cached
1837   "D" gnus-summary-limit-include-dormant
1838   "T" gnus-summary-limit-include-thread
1839   "d" gnus-summary-limit-exclude-dormant
1840   "t" gnus-summary-limit-to-age
1841   "." gnus-summary-limit-to-unseen
1842   "x" gnus-summary-limit-to-extra
1843   "p" gnus-summary-limit-to-display-predicate
1844   "E" gnus-summary-limit-include-expunged
1845   "c" gnus-summary-limit-exclude-childless-dormant
1846   "C" gnus-summary-limit-mark-excluded-as-read
1847   "o" gnus-summary-insert-old-articles
1848   "N" gnus-summary-insert-new-articles
1849   "r" gnus-summary-limit-to-replied)
1850
1851 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1852   "n" gnus-summary-next-unread-article
1853   "p" gnus-summary-prev-unread-article
1854   "N" gnus-summary-next-article
1855   "P" gnus-summary-prev-article
1856   "\C-n" gnus-summary-next-same-subject
1857   "\C-p" gnus-summary-prev-same-subject
1858   "\M-n" gnus-summary-next-unread-subject
1859   "\M-p" gnus-summary-prev-unread-subject
1860   "f" gnus-summary-first-unread-article
1861   "b" gnus-summary-best-unread-article
1862   "j" gnus-summary-goto-article
1863   "g" gnus-summary-goto-subject
1864   "l" gnus-summary-goto-last-article
1865   "o" gnus-summary-pop-article)
1866
1867 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1868   "k" gnus-summary-kill-thread
1869   "l" gnus-summary-lower-thread
1870   "i" gnus-summary-raise-thread
1871   "T" gnus-summary-toggle-threads
1872   "t" gnus-summary-rethread-current
1873   "^" gnus-summary-reparent-thread
1874   "s" gnus-summary-show-thread
1875   "S" gnus-summary-show-all-threads
1876   "h" gnus-summary-hide-thread
1877   "H" gnus-summary-hide-all-threads
1878   "n" gnus-summary-next-thread
1879   "p" gnus-summary-prev-thread
1880   "u" gnus-summary-up-thread
1881   "o" gnus-summary-top-thread
1882   "d" gnus-summary-down-thread
1883   "#" gnus-uu-mark-thread
1884   "\M-#" gnus-uu-unmark-thread)
1885
1886 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1887   "g" gnus-summary-prepare
1888   "c" gnus-summary-insert-cached-articles
1889   "d" gnus-summary-insert-dormant-articles)
1890
1891 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1892   "c" gnus-summary-catchup-and-exit
1893   "C" gnus-summary-catchup-all-and-exit
1894   "E" gnus-summary-exit-no-update
1895   "J" gnus-summary-jump-to-other-group
1896   "Q" gnus-summary-exit
1897   "Z" gnus-summary-exit
1898   "n" gnus-summary-catchup-and-goto-next-group
1899   "R" gnus-summary-reselect-current-group
1900   "G" gnus-summary-rescan-group
1901   "N" gnus-summary-next-group
1902   "s" gnus-summary-save-newsrc
1903   "P" gnus-summary-prev-group)
1904
1905 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1906   " " gnus-summary-next-page
1907   "n" gnus-summary-next-page
1908   "\177" gnus-summary-prev-page
1909   [delete] gnus-summary-prev-page
1910   "p" gnus-summary-prev-page
1911   "\r" gnus-summary-scroll-up
1912   "\M-\r" gnus-summary-scroll-down
1913   "<" gnus-summary-beginning-of-article
1914   ">" gnus-summary-end-of-article
1915   "b" gnus-summary-beginning-of-article
1916   "e" gnus-summary-end-of-article
1917   "^" gnus-summary-refer-parent-article
1918   "r" gnus-summary-refer-parent-article
1919   "D" gnus-summary-enter-digest-group
1920   "R" gnus-summary-refer-references
1921   "T" gnus-summary-refer-thread
1922   "g" gnus-summary-show-article
1923   "s" gnus-summary-isearch-article
1924   "P" gnus-summary-print-article
1925   "M" gnus-mailing-list-insinuate
1926   "t" gnus-article-babel)
1927
1928 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1929   "b" gnus-article-add-buttons
1930   "B" gnus-article-add-buttons-to-head
1931   "o" gnus-article-treat-overstrike
1932   "e" gnus-article-emphasize
1933   "w" gnus-article-fill-cited-article
1934   "Q" gnus-article-fill-long-lines
1935   "C" gnus-article-capitalize-sentences
1936   "c" gnus-article-remove-cr
1937   "Z" gnus-article-decode-HZ
1938   "A" gnus-article-treat-ansi-sequences
1939   "h" gnus-article-wash-html
1940   "u" gnus-article-unsplit-urls
1941   "f" gnus-article-display-x-face
1942   "l" gnus-summary-stop-page-breaking
1943   "r" gnus-summary-caesar-message
1944   "m" gnus-summary-morse-message
1945   "t" gnus-summary-toggle-header
1946   "g" gnus-treat-smiley
1947   "v" gnus-summary-verbose-headers
1948   "m" gnus-summary-toggle-mime
1949   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1950   "p" gnus-article-verify-x-pgp-sig
1951   "d" gnus-article-treat-dumbquotes)
1952
1953 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1954   ;; mnemonic: deuglif*Y*
1955   "u" gnus-article-outlook-unwrap-lines
1956   "a" gnus-article-outlook-repair-attribution
1957   "c" gnus-article-outlook-rearrange-citation
1958   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1959
1960 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1961   "a" gnus-article-hide
1962   "h" gnus-article-hide-headers
1963   "b" gnus-article-hide-boring-headers
1964   "s" gnus-article-hide-signature
1965   "c" gnus-article-hide-citation
1966   "C" gnus-article-hide-citation-in-followups
1967   "l" gnus-article-hide-list-identifiers
1968   "B" gnus-article-strip-banner
1969   "P" gnus-article-hide-pem
1970   "\C-c" gnus-article-hide-citation-maybe)
1971
1972 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1973   "a" gnus-article-highlight
1974   "h" gnus-article-highlight-headers
1975   "c" gnus-article-highlight-citation
1976   "s" gnus-article-highlight-signature)
1977
1978 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1979   "f" gnus-article-treat-fold-headers
1980   "u" gnus-article-treat-unfold-headers
1981   "n" gnus-article-treat-fold-newsgroups)
1982
1983 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1984   "x" gnus-article-display-x-face
1985   "d" gnus-article-display-face
1986   "s" gnus-treat-smiley
1987   "D" gnus-article-remove-images
1988   "f" gnus-treat-from-picon
1989   "m" gnus-treat-mail-picon
1990   "n" gnus-treat-newsgroups-picon)
1991
1992 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1993   "z" gnus-article-date-ut
1994   "u" gnus-article-date-ut
1995   "l" gnus-article-date-local
1996   "p" gnus-article-date-english
1997   "e" gnus-article-date-lapsed
1998   "o" gnus-article-date-original
1999   "i" gnus-article-date-iso8601
2000   "s" gnus-article-date-user)
2001
2002 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2003   "t" gnus-article-remove-trailing-blank-lines
2004   "l" gnus-article-strip-leading-blank-lines
2005   "m" gnus-article-strip-multiple-blank-lines
2006   "a" gnus-article-strip-blank-lines
2007   "A" gnus-article-strip-all-blank-lines
2008   "s" gnus-article-strip-leading-space
2009   "e" gnus-article-strip-trailing-space
2010   "w" gnus-article-remove-leading-whitespace)
2011
2012 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2013   "v" gnus-version
2014   "f" gnus-summary-fetch-faq
2015   "d" gnus-summary-describe-group
2016   "h" gnus-summary-describe-briefly
2017   "i" gnus-info-find-node
2018   "c" gnus-group-fetch-charter
2019   "C" gnus-group-fetch-control)
2020
2021 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2022   "e" gnus-summary-expire-articles
2023   "\M-\C-e" gnus-summary-expire-articles-now
2024   "\177" gnus-summary-delete-article
2025   [delete] gnus-summary-delete-article
2026   [backspace] gnus-summary-delete-article
2027   "m" gnus-summary-move-article
2028   "r" gnus-summary-respool-article
2029   "w" gnus-summary-edit-article
2030   "c" gnus-summary-copy-article
2031   "B" gnus-summary-crosspost-article
2032   "q" gnus-summary-respool-query
2033   "t" gnus-summary-respool-trace
2034   "i" gnus-summary-import-article
2035   "I" gnus-summary-create-article
2036   "p" gnus-summary-article-posted-p)
2037
2038 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2039   "o" gnus-summary-save-article
2040   "m" gnus-summary-save-article-mail
2041   "F" gnus-summary-write-article-file
2042   "r" gnus-summary-save-article-rmail
2043   "f" gnus-summary-save-article-file
2044   "b" gnus-summary-save-article-body-file
2045   "h" gnus-summary-save-article-folder
2046   "v" gnus-summary-save-article-vm
2047   "p" gnus-summary-pipe-output
2048   "P" gnus-summary-muttprint
2049   "s" gnus-soup-add-article)
2050
2051 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2052   "b" gnus-summary-display-buttonized
2053   "m" gnus-summary-repair-multipart
2054   "v" gnus-article-view-part
2055   "o" gnus-article-save-part
2056   "c" gnus-article-copy-part
2057   "C" gnus-article-view-part-as-charset
2058   "e" gnus-article-view-part-externally
2059   "E" gnus-article-encrypt-body
2060   "i" gnus-article-inline-part
2061   "|" gnus-article-pipe-part)
2062
2063 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2064   "p" gnus-summary-mark-as-processable
2065   "u" gnus-summary-unmark-as-processable
2066   "U" gnus-summary-unmark-all-processable
2067   "v" gnus-uu-mark-over
2068   "s" gnus-uu-mark-series
2069   "r" gnus-uu-mark-region
2070   "g" gnus-uu-unmark-region
2071   "R" gnus-uu-mark-by-regexp
2072   "G" gnus-uu-unmark-by-regexp
2073   "t" gnus-uu-mark-thread
2074   "T" gnus-uu-unmark-thread
2075   "a" gnus-uu-mark-all
2076   "b" gnus-uu-mark-buffer
2077   "S" gnus-uu-mark-sparse
2078   "k" gnus-summary-kill-process-mark
2079   "y" gnus-summary-yank-process-mark
2080   "w" gnus-summary-save-process-mark
2081   "i" gnus-uu-invert-processable)
2082
2083 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2084   ;;"x" gnus-uu-extract-any
2085   "m" gnus-summary-save-parts
2086   "u" gnus-uu-decode-uu
2087   "U" gnus-uu-decode-uu-and-save
2088   "s" gnus-uu-decode-unshar
2089   "S" gnus-uu-decode-unshar-and-save
2090   "o" gnus-uu-decode-save
2091   "O" gnus-uu-decode-save
2092   "b" gnus-uu-decode-binhex
2093   "B" gnus-uu-decode-binhex
2094   "p" gnus-uu-decode-postscript
2095   "P" gnus-uu-decode-postscript-and-save)
2096
2097 (gnus-define-keys
2098     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2099   "u" gnus-uu-decode-uu-view
2100   "U" gnus-uu-decode-uu-and-save-view
2101   "s" gnus-uu-decode-unshar-view
2102   "S" gnus-uu-decode-unshar-and-save-view
2103   "o" gnus-uu-decode-save-view
2104   "O" gnus-uu-decode-save-view
2105   "b" gnus-uu-decode-binhex-view
2106   "B" gnus-uu-decode-binhex-view
2107   "p" gnus-uu-decode-postscript-view
2108   "P" gnus-uu-decode-postscript-and-save-view)
2109
2110 (defvar gnus-article-post-menu nil)
2111
2112 (defconst gnus-summary-menu-maxlen 20)
2113
2114 (defun gnus-summary-menu-split (menu)
2115   ;; If we have lots of elements, divide them into groups of 20
2116   ;; and make a pane (or submenu) for each one.
2117   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2118       (let ((menu menu) sublists next
2119             (i 1))
2120         (while menu
2121           ;; Pull off the next gnus-summary-menu-maxlen elements
2122           ;; and make them the next element of sublist.
2123           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2124           (if next
2125               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2126                       nil))
2127           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2128                                              (aref (car (last menu)) 0)) menu)
2129                                sublists))
2130           (setq i (1+ i))
2131           (setq menu next))
2132         (nreverse sublists))
2133     ;; Few elements--put them all in one pane.
2134     menu))
2135
2136 (defun gnus-summary-make-menu-bar ()
2137   (gnus-turn-off-edit-menu 'summary)
2138
2139   (unless (boundp 'gnus-summary-misc-menu)
2140
2141     (easy-menu-define
2142       gnus-summary-kill-menu gnus-summary-mode-map ""
2143       (cons
2144        "Score"
2145        (nconc
2146         (list
2147          ["Customize" gnus-score-customize t])
2148         (gnus-make-score-map 'increase)
2149         (gnus-make-score-map 'lower)
2150         '(("Mark"
2151            ["Kill below" gnus-summary-kill-below t]
2152            ["Mark above" gnus-summary-mark-above t]
2153            ["Tick above" gnus-summary-tick-above t]
2154            ["Clear above" gnus-summary-clear-above t])
2155           ["Current score" gnus-summary-current-score t]
2156           ["Set score" gnus-summary-set-score t]
2157           ["Switch current score file..." gnus-score-change-score-file t]
2158           ["Set mark below..." gnus-score-set-mark-below t]
2159           ["Set expunge below..." gnus-score-set-expunge-below t]
2160           ["Edit current score file" gnus-score-edit-current-scores t]
2161           ["Edit score file" gnus-score-edit-file t]
2162           ["Trace score" gnus-score-find-trace t]
2163           ["Find words" gnus-score-find-favourite-words t]
2164           ["Rescore buffer" gnus-summary-rescore t]
2165           ["Increase score..." gnus-summary-increase-score t]
2166           ["Lower score..." gnus-summary-lower-score t]))))
2167
2168     ;; Define both the Article menu in the summary buffer and the
2169     ;; equivalent Commands menu in the article buffer here for
2170     ;; consistency.
2171     (let ((innards
2172            `(("Hide"
2173               ["All" gnus-article-hide t]
2174               ["Headers" gnus-article-hide-headers t]
2175               ["Signature" gnus-article-hide-signature t]
2176               ["Citation" gnus-article-hide-citation t]
2177               ["List identifiers" gnus-article-hide-list-identifiers t]
2178               ["Banner" gnus-article-strip-banner t]
2179               ["Boring headers" gnus-article-hide-boring-headers t])
2180              ("Highlight"
2181               ["All" gnus-article-highlight t]
2182               ["Headers" gnus-article-highlight-headers t]
2183               ["Signature" gnus-article-highlight-signature t]
2184               ["Citation" gnus-article-highlight-citation t])
2185              ("Date"
2186               ["Local" gnus-article-date-local t]
2187               ["ISO8601" gnus-article-date-iso8601 t]
2188               ["UT" gnus-article-date-ut t]
2189               ["Original" gnus-article-date-original t]
2190               ["Lapsed" gnus-article-date-lapsed t]
2191               ["User-defined" gnus-article-date-user t])
2192              ("Display"
2193               ["Remove images" gnus-article-remove-images t]
2194               ["Toggle smiley" gnus-treat-smiley t]
2195               ["Show X-Face" gnus-article-display-x-face t]
2196               ["Show picons in From" gnus-treat-from-picon t]
2197               ["Show picons in mail headers" gnus-treat-mail-picon t]
2198               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2199               ("View as different encoding"
2200                ,@(gnus-summary-menu-split
2201                   (mapcar
2202                    (lambda (cs)
2203                      ;; Since easymenu under Emacs doesn't allow
2204                      ;; lambda forms for menu commands, we should
2205                      ;; provide intern'ed function symbols.
2206                      (let ((command (intern (format "\
2207 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2208                        (fset command
2209                              `(lambda ()
2210                                 (interactive)
2211                                 (let ((gnus-summary-show-article-charset-alist
2212                                        '((1 . ,cs))))
2213                                   (gnus-summary-show-article 1))))
2214                        `[,(symbol-name cs) ,command t]))
2215                    (sort (if (fboundp 'coding-system-list)
2216                              (coding-system-list)
2217                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2218                            )
2219                          'string<)))))
2220              ("Washing"
2221               ("Remove Blanks"
2222                ["Leading" gnus-article-strip-leading-blank-lines t]
2223                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2224                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2225                ["All of the above" gnus-article-strip-blank-lines t]
2226                ["All" gnus-article-strip-all-blank-lines t]
2227                ["Leading space" gnus-article-strip-leading-space t]
2228                ["Trailing space" gnus-article-strip-trailing-space t]
2229                ["Leading space in headers"
2230                 gnus-article-remove-leading-whitespace t])
2231               ["Overstrike" gnus-article-treat-overstrike t]
2232               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2233               ["Emphasis" gnus-article-emphasize t]
2234               ["Word wrap" gnus-article-fill-cited-article t]
2235               ["Fill long lines" gnus-article-fill-long-lines t]
2236               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2237               ["Remove CR" gnus-article-remove-cr t]
2238               ["Rot 13" gnus-summary-caesar-message
2239                ,@(if (featurep 'xemacs) '(t)
2240                    '(:help "\"Caesar rotate\" article by 13"))]
2241               ["Morse decode" gnus-summary-morse-message t]
2242               ["Unix pipe..." gnus-summary-pipe-message t]
2243               ["Add buttons" gnus-article-add-buttons t]
2244               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2245               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2246               ["Toggle MIME" gnus-summary-toggle-mime t]
2247               ["Verbose header" gnus-summary-verbose-headers t]
2248               ["Toggle header" gnus-summary-toggle-header t]
2249               ["Unfold headers" gnus-article-treat-unfold-headers t]
2250               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2251               ["Html" gnus-article-wash-html t]
2252               ["Unsplit URLs" gnus-article-unsplit-urls t]
2253               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2254               ["Decode HZ" gnus-article-decode-HZ t]
2255               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2256               ("(Outlook) Deuglify"
2257                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2258                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2259                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2260                ["Full (Outlook) deuglify"
2261                 gnus-article-outlook-deuglify-article t])
2262               )
2263              ("Output"
2264               ["Save in default format..." gnus-summary-save-article
2265                ,@(if (featurep 'xemacs) '(t)
2266                    '(:help "Save article using default method"))]
2267               ["Save in file..." gnus-summary-save-article-file
2268                ,@(if (featurep 'xemacs) '(t)
2269                    '(:help "Save article in file"))]
2270               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2271               ["Save in MH folder..." gnus-summary-save-article-folder t]
2272               ["Save in VM folder..." gnus-summary-save-article-vm t]
2273               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2274               ["Save body in file..." gnus-summary-save-article-body-file t]
2275               ["Pipe through a filter..." gnus-summary-pipe-output t]
2276               ["Add to SOUP packet" gnus-soup-add-article t]
2277               ["Print with Muttprint..." gnus-summary-muttprint t]
2278               ["Print" gnus-summary-print-article
2279                ,@(if (featurep 'xemacs) '(t)
2280                    '(:help "Generate and print a PostScript image"))])
2281              ("Copy, move,... (Backend)"
2282               ,@(if (featurep 'xemacs) nil
2283                   '(:help "Copying, moving, expiring articles..."))
2284               ["Respool article..." gnus-summary-respool-article t]
2285               ["Move article..." gnus-summary-move-article
2286                (gnus-check-backend-function
2287                 'request-move-article gnus-newsgroup-name)]
2288               ["Copy article..." gnus-summary-copy-article t]
2289               ["Crosspost article..." gnus-summary-crosspost-article
2290                (gnus-check-backend-function
2291                 'request-replace-article gnus-newsgroup-name)]
2292               ["Import file..." gnus-summary-import-article
2293                (gnus-check-backend-function
2294                 'request-accept-article gnus-newsgroup-name)]
2295               ["Create article..." gnus-summary-create-article
2296                (gnus-check-backend-function
2297                 'request-accept-article gnus-newsgroup-name)]
2298               ["Check if posted" gnus-summary-article-posted-p t]
2299               ["Edit article" gnus-summary-edit-article
2300                (not (gnus-group-read-only-p))]
2301               ["Delete article" gnus-summary-delete-article
2302                (gnus-check-backend-function
2303                 'request-expire-articles gnus-newsgroup-name)]
2304               ["Query respool" gnus-summary-respool-query t]
2305               ["Trace respool" gnus-summary-respool-trace t]
2306               ["Delete expirable articles" gnus-summary-expire-articles-now
2307                (gnus-check-backend-function
2308                 'request-expire-articles gnus-newsgroup-name)])
2309              ("Extract"
2310               ["Uudecode" gnus-uu-decode-uu
2311                ,@(if (featurep 'xemacs) '(t)
2312                    '(:help "Decode uuencoded article(s)"))]
2313               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2314               ["Unshar" gnus-uu-decode-unshar t]
2315               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2316               ["Save" gnus-uu-decode-save t]
2317               ["Binhex" gnus-uu-decode-binhex t]
2318               ["Postscript" gnus-uu-decode-postscript t])
2319              ("Cache"
2320               ["Enter article" gnus-cache-enter-article t]
2321               ["Remove article" gnus-cache-remove-article t])
2322              ["Translate" gnus-article-babel t]
2323              ["Select article buffer" gnus-summary-select-article-buffer t]
2324              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2325              ["Isearch article..." gnus-summary-isearch-article t]
2326              ["Beginning of the article" gnus-summary-beginning-of-article t]
2327              ["End of the article" gnus-summary-end-of-article t]
2328              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2329              ["Fetch referenced articles" gnus-summary-refer-references t]
2330              ["Fetch current thread" gnus-summary-refer-thread t]
2331              ["Fetch article with id..." gnus-summary-refer-article t]
2332              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2333              ["Redisplay" gnus-summary-show-article t]
2334              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2335       (easy-menu-define
2336         gnus-summary-article-menu gnus-summary-mode-map ""
2337         (cons "Article" innards))
2338
2339       (if (not (keymapp gnus-summary-article-menu))
2340           (easy-menu-define
2341             gnus-article-commands-menu gnus-article-mode-map ""
2342             (cons "Commands" innards))
2343         ;; in Emacs, don't share menu.
2344         (setq gnus-article-commands-menu
2345               (copy-keymap gnus-summary-article-menu))
2346         (define-key gnus-article-mode-map [menu-bar commands]
2347           (cons "Commands" gnus-article-commands-menu))))
2348
2349     (easy-menu-define
2350       gnus-summary-thread-menu gnus-summary-mode-map ""
2351       '("Threads"
2352         ["Find all messages in thread" gnus-summary-refer-thread t]
2353         ["Toggle threading" gnus-summary-toggle-threads t]
2354         ["Hide threads" gnus-summary-hide-all-threads t]
2355         ["Show threads" gnus-summary-show-all-threads t]
2356         ["Hide thread" gnus-summary-hide-thread t]
2357         ["Show thread" gnus-summary-show-thread t]
2358         ["Go to next thread" gnus-summary-next-thread t]
2359         ["Go to previous thread" gnus-summary-prev-thread t]
2360         ["Go down thread" gnus-summary-down-thread t]
2361         ["Go up thread" gnus-summary-up-thread t]
2362         ["Top of thread" gnus-summary-top-thread t]
2363         ["Mark thread as read" gnus-summary-kill-thread t]
2364         ["Lower thread score" gnus-summary-lower-thread t]
2365         ["Raise thread score" gnus-summary-raise-thread t]
2366         ["Rethread current" gnus-summary-rethread-current t]))
2367
2368     (easy-menu-define
2369       gnus-summary-post-menu gnus-summary-mode-map ""
2370       `("Post"
2371         ["Send a message (mail or news)" gnus-summary-post-news
2372          ,@(if (featurep 'xemacs) '(t)
2373              '(:help "Compose a new message (mail or news)"))]
2374         ["Followup" gnus-summary-followup
2375          ,@(if (featurep 'xemacs) '(t)
2376              '(:help "Post followup to this article"))]
2377         ["Followup and yank" gnus-summary-followup-with-original
2378          ,@(if (featurep 'xemacs) '(t)
2379              '(:help "Post followup to this article, quoting its contents"))]
2380         ["Supersede article" gnus-summary-supersede-article t]
2381         ["Cancel article" gnus-summary-cancel-article
2382          ,@(if (featurep 'xemacs) '(t)
2383              '(:help "Cancel an article you posted"))]
2384         ["Reply" gnus-summary-reply t]
2385         ["Reply and yank" gnus-summary-reply-with-original t]
2386         ["Wide reply" gnus-summary-wide-reply t]
2387         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2388          ,@(if (featurep 'xemacs) '(t)
2389              '(:help "Mail a reply, quoting this article"))]
2390         ["Very wide reply" gnus-summary-very-wide-reply t]
2391         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2392          ,@(if (featurep 'xemacs) '(t)
2393              '(:help "Mail a very wide reply, quoting this article"))]
2394         ["Mail forward" gnus-summary-mail-forward t]
2395         ["Post forward" gnus-summary-post-forward t]
2396         ["Digest and mail" gnus-summary-digest-mail-forward t]
2397         ["Digest and post" gnus-summary-digest-post-forward t]
2398         ["Resend message" gnus-summary-resend-message t]
2399         ["Resend message edit" gnus-summary-resend-message-edit t]
2400         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2401         ["Send a mail" gnus-summary-mail-other-window t]
2402         ["Create a local message" gnus-summary-news-other-window t]
2403         ["Uuencode and post" gnus-uu-post-news
2404          ,@(if (featurep 'xemacs) '(t)
2405              '(:help "Post a uuencoded article"))]
2406         ["Followup via news" gnus-summary-followup-to-mail t]
2407         ["Followup via news and yank"
2408          gnus-summary-followup-to-mail-with-original t]
2409         ;;("Draft"
2410         ;;["Send" gnus-summary-send-draft t]
2411         ;;["Send bounced" gnus-resend-bounced-mail t])
2412         ))
2413
2414     (cond
2415      ((not (keymapp gnus-summary-post-menu))
2416       (setq gnus-article-post-menu gnus-summary-post-menu))
2417      ((not gnus-article-post-menu)
2418       ;; Don't share post menu.
2419       (setq gnus-article-post-menu
2420             (copy-keymap gnus-summary-post-menu))))
2421     (define-key gnus-article-mode-map [menu-bar post]
2422       (cons "Post" gnus-article-post-menu))
2423
2424     (easy-menu-define
2425       gnus-summary-misc-menu gnus-summary-mode-map ""
2426       `("Gnus"
2427         ("Mark Read"
2428          ["Mark as read" gnus-summary-mark-as-read-forward t]
2429          ["Mark same subject and select"
2430           gnus-summary-kill-same-subject-and-select t]
2431          ["Mark same subject" gnus-summary-kill-same-subject t]
2432          ["Catchup" gnus-summary-catchup
2433           ,@(if (featurep 'xemacs) '(t)
2434               '(:help "Mark unread articles in this group as read"))]
2435          ["Catchup all" gnus-summary-catchup-all t]
2436          ["Catchup to here" gnus-summary-catchup-to-here t]
2437          ["Catchup from here" gnus-summary-catchup-from-here t]
2438          ["Catchup region" gnus-summary-mark-region-as-read
2439           (gnus-mark-active-p)]
2440          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2441         ("Mark Various"
2442          ["Tick" gnus-summary-tick-article-forward t]
2443          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2444          ["Remove marks" gnus-summary-clear-mark-forward t]
2445          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2446          ["Set bookmark" gnus-summary-set-bookmark t]
2447          ["Remove bookmark" gnus-summary-remove-bookmark t])
2448         ("Limit to"
2449          ["Marks..." gnus-summary-limit-to-marks t]
2450          ["Subject..." gnus-summary-limit-to-subject t]
2451          ["Author..." gnus-summary-limit-to-author t]
2452          ["Age..." gnus-summary-limit-to-age t]
2453          ["Extra..." gnus-summary-limit-to-extra t]
2454          ["Score..." gnus-summary-limit-to-score t]
2455          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2456          ["Unread" gnus-summary-limit-to-unread t]
2457          ["Unseen" gnus-summary-limit-to-unseen t]
2458          ["Replied" gnus-summary-limit-to-replied t]
2459          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2460          ["Next articles" gnus-summary-limit-to-articles t]
2461          ["Pop limit" gnus-summary-pop-limit t]
2462          ["Show dormant" gnus-summary-limit-include-dormant t]
2463          ["Hide childless dormant"
2464           gnus-summary-limit-exclude-childless-dormant t]
2465          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2466          ["Hide marked" gnus-summary-limit-exclude-marks t]
2467          ["Show expunged" gnus-summary-limit-include-expunged t])
2468         ("Process Mark"
2469          ["Set mark" gnus-summary-mark-as-processable t]
2470          ["Remove mark" gnus-summary-unmark-as-processable t]
2471          ["Remove all marks" gnus-summary-unmark-all-processable t]
2472          ["Invert marks" gnus-uu-invert-processable t]
2473          ["Mark above" gnus-uu-mark-over t]
2474          ["Mark series" gnus-uu-mark-series t]
2475          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2476          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2477          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2478          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2479          ["Mark all" gnus-uu-mark-all t]
2480          ["Mark buffer" gnus-uu-mark-buffer t]
2481          ["Mark sparse" gnus-uu-mark-sparse t]
2482          ["Mark thread" gnus-uu-mark-thread t]
2483          ["Unmark thread" gnus-uu-unmark-thread t]
2484          ("Process Mark Sets"
2485           ["Kill" gnus-summary-kill-process-mark t]
2486           ["Yank" gnus-summary-yank-process-mark
2487            gnus-newsgroup-process-stack]
2488           ["Save" gnus-summary-save-process-mark t]
2489           ["Run command on marked..." gnus-summary-universal-argument t]))
2490         ("Scroll article"
2491          ["Page forward" gnus-summary-next-page
2492           ,@(if (featurep 'xemacs) '(t)
2493               '(:help "Show next page of article"))]
2494          ["Page backward" gnus-summary-prev-page
2495           ,@(if (featurep 'xemacs) '(t)
2496               '(:help "Show previous page of article"))]
2497          ["Line forward" gnus-summary-scroll-up t])
2498         ("Move"
2499          ["Next unread article" gnus-summary-next-unread-article t]
2500          ["Previous unread article" gnus-summary-prev-unread-article t]
2501          ["Next article" gnus-summary-next-article t]
2502          ["Previous article" gnus-summary-prev-article t]
2503          ["Next unread subject" gnus-summary-next-unread-subject t]
2504          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2505          ["Next article same subject" gnus-summary-next-same-subject t]
2506          ["Previous article same subject" gnus-summary-prev-same-subject t]
2507          ["First unread article" gnus-summary-first-unread-article t]
2508          ["Best unread article" gnus-summary-best-unread-article t]
2509          ["Go to subject number..." gnus-summary-goto-subject t]
2510          ["Go to article number..." gnus-summary-goto-article t]
2511          ["Go to the last article" gnus-summary-goto-last-article t]
2512          ["Pop article off history" gnus-summary-pop-article t])
2513         ("Sort"
2514          ["Sort by number" gnus-summary-sort-by-number t]
2515          ["Sort by author" gnus-summary-sort-by-author t]
2516          ["Sort by subject" gnus-summary-sort-by-subject t]
2517          ["Sort by date" gnus-summary-sort-by-date t]
2518          ["Sort by score" gnus-summary-sort-by-score t]
2519          ["Sort by lines" gnus-summary-sort-by-lines t]
2520          ["Sort by characters" gnus-summary-sort-by-chars t]
2521          ["Randomize" gnus-summary-sort-by-random t]
2522          ["Original sort" gnus-summary-sort-by-original t])
2523         ("Help"
2524          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2525          ["Describe group" gnus-summary-describe-group t]
2526          ["Fetch charter" gnus-group-fetch-charter
2527           ,@(if (featurep 'xemacs) nil
2528               '(:help "Display the charter of the current group"))]
2529          ["Fetch control message" gnus-group-fetch-control
2530           ,@(if (featurep 'xemacs) nil
2531               '(:help "Display the archived control message for the current group"))]
2532          ["Read manual" gnus-info-find-node t])
2533         ("Modes"
2534          ["Pick and read" gnus-pick-mode t]
2535          ["Binary" gnus-binary-mode t])
2536         ("Regeneration"
2537          ["Regenerate" gnus-summary-prepare t]
2538          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2539          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2540          ["Toggle threading" gnus-summary-toggle-threads t])
2541         ["See old articles" gnus-summary-insert-old-articles t]
2542         ["See new articles" gnus-summary-insert-new-articles t]
2543         ["Filter articles..." gnus-summary-execute-command t]
2544         ["Run command on articles..." gnus-summary-universal-argument t]
2545         ["Search articles forward..." gnus-summary-search-article-forward t]
2546         ["Search articles backward..." gnus-summary-search-article-backward t]
2547         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2548         ["Expand window" gnus-summary-expand-window t]
2549         ["Expire expirable articles" gnus-summary-expire-articles
2550          (gnus-check-backend-function
2551           'request-expire-articles gnus-newsgroup-name)]
2552         ["Edit local kill file" gnus-summary-edit-local-kill t]
2553         ["Edit main kill file" gnus-summary-edit-global-kill t]
2554         ["Edit group parameters" gnus-summary-edit-parameters t]
2555         ["Customize group parameters" gnus-summary-customize-parameters t]
2556         ["Send a bug report" gnus-bug t]
2557         ("Exit"
2558          ["Catchup and exit" gnus-summary-catchup-and-exit
2559           ,@(if (featurep 'xemacs) '(t)
2560               '(:help "Mark unread articles in this group as read, then exit"))]
2561          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2562          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2563          ["Exit group" gnus-summary-exit
2564           ,@(if (featurep 'xemacs) '(t)
2565               '(:help "Exit current group, return to group selection mode"))]
2566          ["Exit group without updating" gnus-summary-exit-no-update t]
2567          ["Exit and goto next group" gnus-summary-next-group t]
2568          ["Exit and goto prev group" gnus-summary-prev-group t]
2569          ["Reselect group" gnus-summary-reselect-current-group t]
2570          ["Rescan group" gnus-summary-rescan-group t]
2571          ["Update dribble" gnus-summary-save-newsrc t])))
2572
2573     (gnus-run-hooks 'gnus-summary-menu-hook)))
2574
2575 (defvar gnus-summary-tool-bar-map nil)
2576
2577 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2578 (defun gnus-summary-make-tool-bar ()
2579   (if (and (fboundp 'tool-bar-add-item-from-menu)
2580            (default-value 'tool-bar-mode)
2581            (not gnus-summary-tool-bar-map))
2582       (setq gnus-summary-tool-bar-map
2583             (let ((tool-bar-map (make-sparse-keymap))
2584                   (load-path (mm-image-load-path)))
2585               (tool-bar-add-item-from-menu
2586                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2587               (tool-bar-add-item-from-menu
2588                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2589               (tool-bar-add-item-from-menu
2590                'gnus-summary-post-news "post" gnus-summary-mode-map)
2591               (tool-bar-add-item-from-menu
2592                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2593               (tool-bar-add-item-from-menu
2594                'gnus-summary-followup "followup" gnus-summary-mode-map)
2595               (tool-bar-add-item-from-menu
2596                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2597               (tool-bar-add-item-from-menu
2598                'gnus-summary-reply "reply" gnus-summary-mode-map)
2599               (tool-bar-add-item-from-menu
2600                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2601               (tool-bar-add-item-from-menu
2602                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2603               (tool-bar-add-item-from-menu
2604                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2605               (tool-bar-add-item-from-menu
2606                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2607               (tool-bar-add-item-from-menu
2608                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2609               (tool-bar-add-item-from-menu
2610                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2611               (tool-bar-add-item-from-menu
2612                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2613               (tool-bar-add-item-from-menu
2614                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2615               (tool-bar-add-item-from-menu
2616                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2617               tool-bar-map)))
2618   (if gnus-summary-tool-bar-map
2619       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2620
2621 (defun gnus-score-set-default (var value)
2622   "A version of set that updates the GNU Emacs menu-bar."
2623   (set var value)
2624   ;; It is the message that forces the active status to be updated.
2625   (message ""))
2626
2627 (defun gnus-make-score-map (type)
2628   "Make a summary score map of type TYPE."
2629   (if t
2630       nil
2631     (let ((headers '(("author" "from" string)
2632                      ("subject" "subject" string)
2633                      ("article body" "body" string)
2634                      ("article head" "head" string)
2635                      ("xref" "xref" string)
2636                      ("extra header" "extra" string)
2637                      ("lines" "lines" number)
2638                      ("followups to author" "followup" string)))
2639           (types '((number ("less than" <)
2640                            ("greater than" >)
2641                            ("equal" =))
2642                    (string ("substring" s)
2643                            ("exact string" e)
2644                            ("fuzzy string" f)
2645                            ("regexp" r))))
2646           (perms '(("temporary" (current-time-string))
2647                    ("permanent" nil)
2648                    ("immediate" now)))
2649           header)
2650       (list
2651        (apply
2652         'nconc
2653         (list
2654          (if (eq type 'lower)
2655              "Lower score"
2656            "Increase score"))
2657         (let (outh)
2658           (while headers
2659             (setq header (car headers))
2660             (setq outh
2661                   (cons
2662                    (apply
2663                     'nconc
2664                     (list (car header))
2665                     (let ((ts (cdr (assoc (nth 2 header) types)))
2666                           outt)
2667                       (while ts
2668                         (setq outt
2669                               (cons
2670                                (apply
2671                                 'nconc
2672                                 (list (caar ts))
2673                                 (let ((ps perms)
2674                                       outp)
2675                                   (while ps
2676                                     (setq outp
2677                                           (cons
2678                                            (vector
2679                                             (caar ps)
2680                                             (list
2681                                              'gnus-summary-score-entry
2682                                              (nth 1 header)
2683                                              (if (or (string= (nth 1 header)
2684                                                               "head")
2685                                                      (string= (nth 1 header)
2686                                                               "body"))
2687                                                  ""
2688                                                (list 'gnus-summary-header
2689                                                      (nth 1 header)))
2690                                              (list 'quote (nth 1 (car ts)))
2691                                              (list 'gnus-score-delta-default
2692                                                    nil)
2693                                              (nth 1 (car ps))
2694                                              t)
2695                                             t)
2696                                            outp))
2697                                     (setq ps (cdr ps)))
2698                                   (list (nreverse outp))))
2699                                outt))
2700                         (setq ts (cdr ts)))
2701                       (list (nreverse outt))))
2702                    outh))
2703             (setq headers (cdr headers)))
2704           (list (nreverse outh))))))))
2705
2706 \f
2707
2708 (defun gnus-summary-mode (&optional group)
2709   "Major mode for reading articles.
2710
2711 All normal editing commands are switched off.
2712 \\<gnus-summary-mode-map>
2713 Each line in this buffer represents one article.  To read an
2714 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2715 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2716 respectively.
2717
2718 You can also post articles and send mail from this buffer.  To
2719 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2720 of an article, type `\\[gnus-summary-reply]'.
2721
2722 There are approx. one gazillion commands you can execute in this
2723 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2724
2725 The following commands are available:
2726
2727 \\{gnus-summary-mode-map}"
2728   (interactive)
2729   (kill-all-local-variables)
2730   (when (gnus-visual-p 'summary-menu 'menu)
2731     (gnus-summary-make-menu-bar)
2732     (gnus-summary-make-tool-bar))
2733   (gnus-summary-make-local-variables)
2734   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2735     (gnus-summary-make-local-variables))
2736   (gnus-make-thread-indent-array)
2737   (gnus-simplify-mode-line)
2738   (setq major-mode 'gnus-summary-mode)
2739   (setq mode-name "Summary")
2740   (make-local-variable 'minor-mode-alist)
2741   (use-local-map gnus-summary-mode-map)
2742   (buffer-disable-undo)
2743   (setq buffer-read-only t              ;Disable modification
2744         show-trailing-whitespace nil)
2745   (setq truncate-lines t)
2746   (setq selective-display t)
2747   (setq selective-display-ellipses t)   ;Display `...'
2748   (gnus-summary-set-display-table)
2749   (gnus-set-default-directory)
2750   (setq gnus-newsgroup-name group)
2751   (unless (gnus-news-group-p group)
2752     (setq gnus-newsgroup-incorporated
2753           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2754   (make-local-variable 'gnus-summary-line-format)
2755   (make-local-variable 'gnus-summary-line-format-spec)
2756   (make-local-variable 'gnus-summary-dummy-line-format)
2757   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2758   (make-local-variable 'gnus-summary-mark-positions)
2759   (gnus-make-local-hook 'pre-command-hook)
2760   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2761   (gnus-run-hooks 'gnus-summary-mode-hook)
2762   (turn-on-gnus-mailing-list-mode)
2763   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2764   (gnus-update-summary-mark-positions))
2765
2766 (defun gnus-summary-make-local-variables ()
2767   "Make all the local summary buffer variables."
2768   (let (global)
2769     (dolist (local gnus-summary-local-variables)
2770       (if (consp local)
2771           (progn
2772             (if (eq (cdr local) 'global)
2773                 ;; Copy the global value of the variable.
2774                 (setq global (symbol-value (car local)))
2775               ;; Use the value from the list.
2776               (setq global (eval (cdr local))))
2777             (set (make-local-variable (car local)) global))
2778         ;; Simple nil-valued local variable.
2779         (set (make-local-variable local) nil)))))
2780
2781 (defun gnus-summary-clear-local-variables ()
2782   (let ((locals gnus-summary-local-variables))
2783     (while locals
2784       (if (consp (car locals))
2785           (and (vectorp (caar locals))
2786                (set (caar locals) nil))
2787         (and (vectorp (car locals))
2788              (set (car locals) nil)))
2789       (setq locals (cdr locals)))))
2790
2791 ;; Summary data functions.
2792
2793 (defmacro gnus-data-number (data)
2794   `(car ,data))
2795
2796 (defmacro gnus-data-set-number (data number)
2797   `(setcar ,data ,number))
2798
2799 (defmacro gnus-data-mark (data)
2800   `(nth 1 ,data))
2801
2802 (defmacro gnus-data-set-mark (data mark)
2803   `(setcar (nthcdr 1 ,data) ,mark))
2804
2805 (defmacro gnus-data-pos (data)
2806   `(nth 2 ,data))
2807
2808 (defmacro gnus-data-set-pos (data pos)
2809   `(setcar (nthcdr 2 ,data) ,pos))
2810
2811 (defmacro gnus-data-header (data)
2812   `(nth 3 ,data))
2813
2814 (defmacro gnus-data-set-header (data header)
2815   `(setcar (nthcdr 3 ,data) ,header))
2816
2817 (defmacro gnus-data-level (data)
2818   `(nth 4 ,data))
2819
2820 (defmacro gnus-data-unread-p (data)
2821   `(= (nth 1 ,data) gnus-unread-mark))
2822
2823 (defmacro gnus-data-read-p (data)
2824   `(/= (nth 1 ,data) gnus-unread-mark))
2825
2826 (defmacro gnus-data-pseudo-p (data)
2827   `(consp (nth 3 ,data)))
2828
2829 (defmacro gnus-data-find (number)
2830   `(assq ,number gnus-newsgroup-data))
2831
2832 (defmacro gnus-data-find-list (number &optional data)
2833   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2834      (memq (assq ,number bdata)
2835            bdata)))
2836
2837 (defmacro gnus-data-make (number mark pos header level)
2838   `(list ,number ,mark ,pos ,header ,level))
2839
2840 (defun gnus-data-enter (after-article number mark pos header level offset)
2841   (let ((data (gnus-data-find-list after-article)))
2842     (unless data
2843       (error "No such article: %d" after-article))
2844     (setcdr data (cons (gnus-data-make number mark pos header level)
2845                        (cdr data)))
2846     (setq gnus-newsgroup-data-reverse nil)
2847     (gnus-data-update-list (cddr data) offset)))
2848
2849 (defun gnus-data-enter-list (after-article list &optional offset)
2850   (when list
2851     (let ((data (and after-article (gnus-data-find-list after-article)))
2852           (ilist list))
2853       (if (not (or data
2854                    after-article))
2855           (let ((odata gnus-newsgroup-data))
2856             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2857             (when offset
2858               (gnus-data-update-list odata offset)))
2859         ;; Find the last element in the list to be spliced into the main
2860         ;; list.
2861         (while (cdr list)
2862           (setq list (cdr list)))
2863         (if (not data)
2864             (progn
2865               (setcdr list gnus-newsgroup-data)
2866               (setq gnus-newsgroup-data ilist)
2867               (when offset
2868                 (gnus-data-update-list (cdr list) offset)))
2869           (setcdr list (cdr data))
2870           (setcdr data ilist)
2871           (when offset
2872             (gnus-data-update-list (cdr list) offset))))
2873       (setq gnus-newsgroup-data-reverse nil))))
2874
2875 (defun gnus-data-remove (article &optional offset)
2876   (let ((data gnus-newsgroup-data))
2877     (if (= (gnus-data-number (car data)) article)
2878         (progn
2879           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2880                 gnus-newsgroup-data-reverse nil)
2881           (when offset
2882             (gnus-data-update-list gnus-newsgroup-data offset)))
2883       (while (cdr data)
2884         (when (= (gnus-data-number (cadr data)) article)
2885           (setcdr data (cddr data))
2886           (when offset
2887             (gnus-data-update-list (cdr data) offset))
2888           (setq data nil
2889                 gnus-newsgroup-data-reverse nil))
2890         (setq data (cdr data))))))
2891
2892 (defmacro gnus-data-list (backward)
2893   `(if ,backward
2894        (or gnus-newsgroup-data-reverse
2895            (setq gnus-newsgroup-data-reverse
2896                  (reverse gnus-newsgroup-data)))
2897      gnus-newsgroup-data))
2898
2899 (defun gnus-data-update-list (data offset)
2900   "Add OFFSET to the POS of all data entries in DATA."
2901   (setq gnus-newsgroup-data-reverse nil)
2902   (while data
2903     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2904     (setq data (cdr data))))
2905
2906 (defun gnus-summary-article-pseudo-p (article)
2907   "Say whether this article is a pseudo article or not."
2908   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2909
2910 (defmacro gnus-summary-article-sparse-p (article)
2911   "Say whether this article is a sparse article or not."
2912   `(memq ,article gnus-newsgroup-sparse))
2913
2914 (defmacro gnus-summary-article-ancient-p (article)
2915   "Say whether this article is a sparse article or not."
2916   `(memq ,article gnus-newsgroup-ancient))
2917
2918 (defun gnus-article-parent-p (number)
2919   "Say whether this article is a parent or not."
2920   (let ((data (gnus-data-find-list number)))
2921     (and (cdr data)                     ; There has to be an article after...
2922          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2923             (gnus-data-level (nth 1 data))))))
2924
2925 (defun gnus-article-children (number)
2926   "Return a list of all children to NUMBER."
2927   (let* ((data (gnus-data-find-list number))
2928          (level (gnus-data-level (car data)))
2929          children)
2930     (setq data (cdr data))
2931     (while (and data
2932                 (= (gnus-data-level (car data)) (1+ level)))
2933       (push (gnus-data-number (car data)) children)
2934       (setq data (cdr data)))
2935     children))
2936
2937 (defmacro gnus-summary-skip-intangible ()
2938   "If the current article is intangible, then jump to a different article."
2939   '(let ((to (get-text-property (point) 'gnus-intangible)))
2940      (and to (gnus-summary-goto-subject to))))
2941
2942 (defmacro gnus-summary-article-intangible-p ()
2943   "Say whether this article is intangible or not."
2944   '(get-text-property (point) 'gnus-intangible))
2945
2946 (defun gnus-article-read-p (article)
2947   "Say whether ARTICLE is read or not."
2948   (not (or (memq article gnus-newsgroup-marked)
2949            (memq article gnus-newsgroup-spam-marked)
2950            (memq article gnus-newsgroup-unreads)
2951            (memq article gnus-newsgroup-unselected)
2952            (memq article gnus-newsgroup-dormant))))
2953
2954 ;; Some summary mode macros.
2955
2956 (defmacro gnus-summary-article-number ()
2957   "The article number of the article on the current line.
2958 If there isn't an article number here, then we return the current
2959 article number."
2960   '(progn
2961      (gnus-summary-skip-intangible)
2962      (or (get-text-property (point) 'gnus-number)
2963          (gnus-summary-last-subject))))
2964
2965 (defmacro gnus-summary-article-header (&optional number)
2966   "Return the header of article NUMBER."
2967   `(gnus-data-header (gnus-data-find
2968                       ,(or number '(gnus-summary-article-number)))))
2969
2970 (defmacro gnus-summary-thread-level (&optional number)
2971   "Return the level of thread that starts with article NUMBER."
2972   `(if (and (eq gnus-summary-make-false-root 'dummy)
2973             (get-text-property (point) 'gnus-intangible))
2974        0
2975      (gnus-data-level (gnus-data-find
2976                        ,(or number '(gnus-summary-article-number))))))
2977
2978 (defmacro gnus-summary-article-mark (&optional number)
2979   "Return the mark of article NUMBER."
2980   `(gnus-data-mark (gnus-data-find
2981                     ,(or number '(gnus-summary-article-number)))))
2982
2983 (defmacro gnus-summary-article-pos (&optional number)
2984   "Return the position of the line of article NUMBER."
2985   `(gnus-data-pos (gnus-data-find
2986                    ,(or number '(gnus-summary-article-number)))))
2987
2988 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2989 (defmacro gnus-summary-article-subject (&optional number)
2990   "Return current subject string or nil if nothing."
2991   `(let ((headers
2992           ,(if number
2993                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2994              '(gnus-data-header (assq (gnus-summary-article-number)
2995                                       gnus-newsgroup-data)))))
2996      (and headers
2997           (vectorp headers)
2998           (mail-header-subject headers))))
2999
3000 (defmacro gnus-summary-article-score (&optional number)
3001   "Return current article score."
3002   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3003                   gnus-newsgroup-scored))
3004        gnus-summary-default-score 0))
3005
3006 (defun gnus-summary-article-children (&optional number)
3007   "Return a list of article numbers that are children of article NUMBER."
3008   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3009          (level (gnus-data-level (car data)))
3010          l children)
3011     (while (and (setq data (cdr data))
3012                 (> (setq l (gnus-data-level (car data))) level))
3013       (and (= (1+ level) l)
3014            (push (gnus-data-number (car data))
3015                  children)))
3016     (nreverse children)))
3017
3018 (defun gnus-summary-article-parent (&optional number)
3019   "Return the article number of the parent of article NUMBER."
3020   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3021                                     (gnus-data-list t)))
3022          (level (gnus-data-level (car data))))
3023     (if (zerop level)
3024         ()                              ; This is a root.
3025       ;; We search until we find an article with a level less than
3026       ;; this one.  That function has to be the parent.
3027       (while (and (setq data (cdr data))
3028                   (not (< (gnus-data-level (car data)) level))))
3029       (and data (gnus-data-number (car data))))))
3030
3031 (defun gnus-unread-mark-p (mark)
3032   "Say whether MARK is the unread mark."
3033   (= mark gnus-unread-mark))
3034
3035 (defun gnus-read-mark-p (mark)
3036   "Say whether MARK is one of the marks that mark as read.
3037 This is all marks except unread, ticked, dormant, and expirable."
3038   (not (or (= mark gnus-unread-mark)
3039            (= mark gnus-ticked-mark)
3040            (= mark gnus-spam-mark)
3041            (= mark gnus-dormant-mark)
3042            (= mark gnus-expirable-mark))))
3043
3044 (defmacro gnus-article-mark (number)
3045   "Return the MARK of article NUMBER.
3046 This macro should only be used when computing the mark the \"first\"
3047 time; i.e., when generating the summary lines.  After that,
3048 `gnus-summary-article-mark' should be used to examine the
3049 marks of articles."
3050   `(cond
3051     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3052     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3053     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3054     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3055     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3056     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3057     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3058     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3059            gnus-ancient-mark))))
3060
3061 ;; Saving hidden threads.
3062
3063 (defmacro gnus-save-hidden-threads (&rest forms)
3064   "Save hidden threads, eval FORMS, and restore the hidden threads."
3065   (let ((config (make-symbol "config")))
3066     `(let ((,config (gnus-hidden-threads-configuration)))
3067        (unwind-protect
3068            (save-excursion
3069              ,@forms)
3070          (gnus-restore-hidden-threads-configuration ,config)))))
3071 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3072 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3073
3074 (defun gnus-data-compute-positions ()
3075   "Compute the positions of all articles."
3076   (setq gnus-newsgroup-data-reverse nil)
3077   (let ((data gnus-newsgroup-data))
3078     (save-excursion
3079       (gnus-save-hidden-threads
3080         (gnus-summary-show-all-threads)
3081         (goto-char (point-min))
3082         (while data
3083           (while (get-text-property (point) 'gnus-intangible)
3084             (forward-line 1))
3085           (gnus-data-set-pos (car data) (+ (point) 3))
3086           (setq data (cdr data))
3087           (forward-line 1))))))
3088
3089 (defun gnus-hidden-threads-configuration ()
3090   "Return the current hidden threads configuration."
3091   (save-excursion
3092     (let (config)
3093       (goto-char (point-min))
3094       (while (search-forward "\r" nil t)
3095         (push (1- (point)) config))
3096       config)))
3097
3098 (defun gnus-restore-hidden-threads-configuration (config)
3099   "Restore hidden threads configuration from CONFIG."
3100   (save-excursion
3101     (let (point buffer-read-only)
3102       (while (setq point (pop config))
3103         (when (and (< point (point-max))
3104                    (goto-char point)
3105                    (eq (char-after) ?\n))
3106           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3107
3108 ;; Various summary mode internalish functions.
3109
3110 (defun gnus-mouse-pick-article (e)
3111   (interactive "e")
3112   (mouse-set-point e)
3113   (gnus-summary-next-page nil t))
3114
3115 (defun gnus-summary-set-display-table ()
3116   "Change the display table.
3117 Odd characters have a tendency to mess
3118 up nicely formatted displays - we make all possible glyphs
3119 display only a single character."
3120
3121   ;; We start from the standard display table, if any.
3122   (let ((table (or (copy-sequence standard-display-table)
3123                    (make-display-table)))
3124         (i 32))
3125     ;; Nix out all the control chars...
3126     (while (>= (setq i (1- i)) 0)
3127       (aset table i [??]))
3128     ;; ... but not newline and cr, of course.  (cr is necessary for the
3129     ;; selective display).
3130     (aset table ?\n nil)
3131     (aset table ?\r nil)
3132     ;; We keep TAB as well.
3133     (aset table ?\t nil)
3134     ;; We nix out any glyphs over 126 that are not set already.
3135     (let ((i 256))
3136       (while (>= (setq i (1- i)) 127)
3137         ;; Only modify if the entry is nil.
3138         (unless (aref table i)
3139           (aset table i [??]))))
3140     (setq buffer-display-table table)))
3141
3142 (defun gnus-summary-set-article-display-arrow (pos)
3143   "Update the overlay arrow to point to line at position POS."
3144   (when (and gnus-summary-display-arrow
3145              (boundp 'overlay-arrow-position)
3146              (boundp 'overlay-arrow-string))
3147     (save-excursion
3148       (goto-char pos)
3149       (beginning-of-line)
3150       (unless overlay-arrow-position
3151         (setq overlay-arrow-position (make-marker)))
3152       (setq overlay-arrow-string "=>"
3153             overlay-arrow-position (set-marker overlay-arrow-position
3154                                                (point)
3155                                                (current-buffer))))))
3156
3157 (defun gnus-summary-setup-buffer (group)
3158   "Initialize summary buffer."
3159   (let ((buffer (gnus-summary-buffer-name group))
3160         (dead-name (concat "*Dead Summary "
3161                            (gnus-group-decoded-name group) "*")))
3162     ;; If a dead summary buffer exists, we kill it.
3163     (when (gnus-buffer-live-p dead-name)
3164       (gnus-kill-buffer dead-name))
3165     (if (get-buffer buffer)
3166         (progn
3167           (set-buffer buffer)
3168           (setq gnus-summary-buffer (current-buffer))
3169           (not gnus-newsgroup-prepared))
3170       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3171       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3172       (gnus-summary-mode group)
3173       (when gnus-carpal
3174         (gnus-carpal-setup-buffer 'summary))
3175       (unless gnus-single-article-buffer
3176         (make-local-variable 'gnus-article-buffer)
3177         (make-local-variable 'gnus-article-current)
3178         (make-local-variable 'gnus-original-article-buffer))
3179       (setq gnus-newsgroup-name group)
3180       ;; Set any local variables in the group parameters.
3181       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3182       t)))
3183
3184 (defun gnus-set-global-variables ()
3185   "Set the global equivalents of the buffer-local variables.
3186 They are set to the latest values they had.  These reflect the summary
3187 buffer that was in action when the last article was fetched."
3188   (when (eq major-mode 'gnus-summary-mode)
3189     (setq gnus-summary-buffer (current-buffer))
3190     (let ((name gnus-newsgroup-name)
3191           (marked gnus-newsgroup-marked)
3192           (spam gnus-newsgroup-spam-marked)
3193           (unread gnus-newsgroup-unreads)
3194           (headers gnus-current-headers)
3195           (data gnus-newsgroup-data)
3196           (summary gnus-summary-buffer)
3197           (article-buffer gnus-article-buffer)
3198           (original gnus-original-article-buffer)
3199           (gac gnus-article-current)
3200           (reffed gnus-reffed-article-number)
3201           (score-file gnus-current-score-file)
3202           (default-charset gnus-newsgroup-charset)
3203           vlist)
3204       (let ((locals gnus-newsgroup-variables))
3205         (while locals
3206           (if (consp (car locals))
3207               (push (eval (caar locals)) vlist)
3208             (push (eval (car locals)) vlist))
3209           (setq locals (cdr locals)))
3210         (setq vlist (nreverse vlist)))
3211       (with-current-buffer gnus-group-buffer
3212         (setq gnus-newsgroup-name name
3213               gnus-newsgroup-marked marked
3214               gnus-newsgroup-spam-marked spam
3215               gnus-newsgroup-unreads unread
3216               gnus-current-headers headers
3217               gnus-newsgroup-data data
3218               gnus-article-current gac
3219               gnus-summary-buffer summary
3220               gnus-article-buffer article-buffer
3221               gnus-original-article-buffer original
3222               gnus-reffed-article-number reffed
3223               gnus-current-score-file score-file
3224               gnus-newsgroup-charset default-charset)
3225         (let ((locals gnus-newsgroup-variables))
3226           (while locals
3227             (if (consp (car locals))
3228                 (set (caar locals) (pop vlist))
3229               (set (car locals) (pop vlist)))
3230             (setq locals (cdr locals))))
3231         ;; The article buffer also has local variables.
3232         (when (gnus-buffer-live-p gnus-article-buffer)
3233           (set-buffer gnus-article-buffer)
3234           (setq gnus-summary-buffer summary))))))
3235
3236 (defun gnus-summary-article-unread-p (article)
3237   "Say whether ARTICLE is unread or not."
3238   (memq article gnus-newsgroup-unreads))
3239
3240 (defun gnus-summary-first-article-p (&optional article)
3241   "Return whether ARTICLE is the first article in the buffer."
3242   (if (not (setq article (or article (gnus-summary-article-number))))
3243       nil
3244     (eq article (caar gnus-newsgroup-data))))
3245
3246 (defun gnus-summary-last-article-p (&optional article)
3247   "Return whether ARTICLE is the last article in the buffer."
3248   (if (not (setq article (or article (gnus-summary-article-number))))
3249       ;; All non-existent numbers are the last article.  :-)
3250       t
3251     (not (cdr (gnus-data-find-list article)))))
3252
3253 (defun gnus-make-thread-indent-array ()
3254   (let ((n 200))
3255     (unless (and gnus-thread-indent-array
3256                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3257       (setq gnus-thread-indent-array (make-vector 201 "")
3258             gnus-thread-indent-array-level gnus-thread-indent-level)
3259       (while (>= n 0)
3260         (aset gnus-thread-indent-array n
3261               (make-string (* n gnus-thread-indent-level) ? ))
3262         (setq n (1- n))))))
3263
3264 (defun gnus-update-summary-mark-positions ()
3265   "Compute where the summary marks are to go."
3266   (save-excursion
3267     (when (gnus-buffer-exists-p gnus-summary-buffer)
3268       (set-buffer gnus-summary-buffer))
3269     (let ((gnus-replied-mark 129)
3270           (gnus-score-below-mark 130)
3271           (gnus-score-over-mark 130)
3272           (gnus-undownloaded-mark 131)
3273           (spec gnus-summary-line-format-spec)
3274           gnus-visual pos)
3275       (save-excursion
3276         (gnus-set-work-buffer)
3277         (let ((gnus-summary-line-format-spec spec)
3278               (gnus-newsgroup-downloadable '(0)))
3279           (gnus-summary-insert-line
3280            (make-full-mail-header 0 "" "nobody"
3281                                   "05 Apr 2001 23:33:09 +0400"
3282                                   "" "" 0 0 "" nil)
3283            0 nil t 128 t nil "" nil 1)
3284           (goto-char (point-min))
3285           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3286                                              (- (point) (point-min) 1)))))
3287           (goto-char (point-min))
3288           (push (cons 'replied (and (search-forward "\201" nil t)
3289                                     (- (point) (point-min) 1)))
3290                 pos)
3291           (goto-char (point-min))
3292           (push (cons 'score (and (search-forward "\202" nil t)
3293                                   (- (point) (point-min) 1)))
3294                 pos)
3295           (goto-char (point-min))
3296           (push (cons 'download
3297                       (and (search-forward "\203" nil t)
3298                            (- (point) (point-min) 1)))
3299                 pos)))
3300       (setq gnus-summary-mark-positions pos))))
3301
3302 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3303   "Insert a dummy root in the summary buffer."
3304   (beginning-of-line)
3305   (gnus-add-text-properties
3306    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3307    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3308
3309 (defun gnus-summary-extract-address-component (from)
3310   (or (car (funcall gnus-extract-address-components from))
3311       from))
3312
3313 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3314   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3315                                 default-mime-charset)))
3316     ;; Is it really necessary to do this next part for each summary line?
3317     ;; Luckily, doesn't seem to slow things down much.
3318     (or
3319      (and gnus-ignored-from-addresses
3320           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3321           (let ((extra-headers (mail-header-extra header))
3322                 to
3323                 newsgroups)
3324             (cond
3325              ((setq to (cdr (assq 'To extra-headers)))
3326               (concat "-> "
3327                       (inline
3328                         (gnus-summary-extract-address-component
3329                          (funcall gnus-decode-encoded-word-function to)))))
3330              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3331               (concat "=> " newsgroups)))))
3332      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3333
3334 (defun gnus-summary-insert-line (gnus-tmp-header
3335                                  gnus-tmp-level gnus-tmp-current
3336                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3337                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3338                                  &optional gnus-tmp-dummy gnus-tmp-score
3339                                  gnus-tmp-process)
3340   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3341          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3342          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3343          (gnus-tmp-score-char
3344           (if (or (null gnus-summary-default-score)
3345                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3346                       gnus-summary-zcore-fuzz))
3347               ?\ ;;;Whitespace
3348             (if (< gnus-tmp-score gnus-summary-default-score)
3349                 gnus-score-below-mark gnus-score-over-mark)))
3350          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3351          (gnus-tmp-replied
3352           (cond (gnus-tmp-process gnus-process-mark)
3353                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3354                  gnus-cached-mark)
3355                 (gnus-tmp-replied gnus-replied-mark)
3356                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3357                  gnus-forwarded-mark)
3358                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3359                  gnus-saved-mark)
3360                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3361                  gnus-recent-mark)
3362                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3363                  gnus-unseen-mark)
3364                 (t gnus-no-mark)))
3365          (gnus-tmp-downloaded
3366           (cond (undownloaded
3367                  gnus-undownloaded-mark)
3368                 (gnus-newsgroup-agentized
3369                  gnus-downloaded-mark)
3370                 (t
3371                  gnus-no-mark)))
3372          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3373          (gnus-tmp-name
3374           (cond
3375            ((string-match "<[^>]+> *$" gnus-tmp-from)
3376             (let ((beg (match-beginning 0)))
3377               (or (and (string-match "^\".+\"" gnus-tmp-from)
3378                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3379                   (substring gnus-tmp-from 0 beg))))
3380            ((string-match "(.+)" gnus-tmp-from)
3381             (substring gnus-tmp-from
3382                        (1+ (match-beginning 0)) (1- (match-end 0))))
3383            (t gnus-tmp-from)))
3384          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3385          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3386          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3387          (buffer-read-only nil))
3388     (when (string= gnus-tmp-name "")
3389       (setq gnus-tmp-name gnus-tmp-from))
3390     (unless (numberp gnus-tmp-lines)
3391       (setq gnus-tmp-lines -1))
3392     (if (= gnus-tmp-lines -1)
3393         (setq gnus-tmp-lines "?")
3394       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3395     (gnus-put-text-property-excluding-characters-with-faces
3396      (point)
3397      (progn (eval gnus-summary-line-format-spec) (point))
3398      'gnus-number gnus-tmp-number)
3399     (when (gnus-visual-p 'summary-highlight 'highlight)
3400       (forward-line -1)
3401       (gnus-run-hooks 'gnus-summary-update-hook)
3402       (forward-line 1))))
3403
3404 (defun gnus-summary-update-line (&optional dont-update)
3405   "Update summary line after change."
3406   (when (and gnus-summary-default-score
3407              (not gnus-summary-inhibit-highlight))
3408     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3409            (article (gnus-summary-article-number))
3410            (score (gnus-summary-article-score article)))
3411       (unless dont-update
3412         (if (and gnus-summary-mark-below
3413                  (< (gnus-summary-article-score)
3414                     gnus-summary-mark-below))
3415             ;; This article has a low score, so we mark it as read.
3416             (when (memq article gnus-newsgroup-unreads)
3417               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3418           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3419             ;; This article was previously marked as read on account
3420             ;; of a low score, but now it has risen, so we mark it as
3421             ;; unread.
3422             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3423         (gnus-summary-update-mark
3424          (if (or (null gnus-summary-default-score)
3425                  (<= (abs (- score gnus-summary-default-score))
3426                      gnus-summary-zcore-fuzz))
3427              ?\ ;;;Whitespace
3428            (if (< score gnus-summary-default-score)
3429                gnus-score-below-mark gnus-score-over-mark))
3430          'score))
3431       ;; Do visual highlighting.
3432       (when (gnus-visual-p 'summary-highlight 'highlight)
3433         (gnus-run-hooks 'gnus-summary-update-hook)))))
3434
3435 (defvar gnus-tmp-new-adopts nil)
3436
3437 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3438   "Return the number of articles in THREAD.
3439 This may be 0 in some cases -- if none of the articles in
3440 the thread are to be displayed."
3441   (let* ((number
3442           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3443           (cond
3444            ((not (listp thread))
3445             1)
3446            ((and (consp thread) (cdr thread))
3447             (apply
3448              '+ 1 (mapcar
3449                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3450            ((null thread)
3451             1)
3452            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3453             1)
3454            (t 0))))
3455     (when (and level (zerop level) gnus-tmp-new-adopts)
3456       (incf number
3457             (apply '+ (mapcar
3458                        'gnus-summary-number-of-articles-in-thread
3459                        gnus-tmp-new-adopts))))
3460     (if char
3461         (if (> number 1) gnus-not-empty-thread-mark
3462           gnus-empty-thread-mark)
3463       number)))
3464
3465 (defsubst gnus-summary-line-message-size (head)
3466   "Return pretty-printed version of message size.
3467 This function is intended to be used in
3468 `gnus-summary-line-format-alist'."
3469   (let ((c (or (mail-header-chars head) -1)))
3470     (cond ((< c 0) "n/a")               ; chars not available
3471           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3472           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3473           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3474           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3475
3476
3477 (defun gnus-summary-set-local-parameters (group)
3478   "Go through the local params of GROUP and set all variable specs in that list."
3479   (let ((params (gnus-group-find-parameter group))
3480         (vars '(quit-config))           ; Ignore quit-config.
3481         elem)
3482     (while params
3483       (setq elem (car params)
3484             params (cdr params))
3485       (and (consp elem)                 ; Has to be a cons.
3486            (consp (cdr elem))           ; The cdr has to be a list.
3487            (symbolp (car elem))         ; Has to be a symbol in there.
3488            (not (memq (car elem) vars))
3489            (ignore-errors               ; So we set it.
3490              (push (car elem) vars)
3491              (make-local-variable (car elem))
3492              (set (car elem) (eval (nth 1 elem))))))))
3493
3494 (defun gnus-summary-read-group (group &optional show-all no-article
3495                                       kill-buffer no-display backward
3496                                       select-articles)
3497   "Start reading news in newsgroup GROUP.
3498 If SHOW-ALL is non-nil, already read articles are also listed.
3499 If NO-ARTICLE is non-nil, no article is selected initially.
3500 If NO-DISPLAY, don't generate a summary buffer."
3501   (let (result)
3502     (while (and group
3503                 (null (setq result
3504                             (let ((gnus-auto-select-next nil))
3505                               (or (gnus-summary-read-group-1
3506                                    group show-all no-article
3507                                    kill-buffer no-display
3508                                    select-articles)
3509                                   (setq show-all nil
3510                                         select-articles nil)))))
3511                 (eq gnus-auto-select-next 'quietly))
3512       (set-buffer gnus-group-buffer)
3513       ;; The entry function called above goes to the next
3514       ;; group automatically, so we go two groups back
3515       ;; if we are searching for the previous group.
3516       (when backward
3517         (gnus-group-prev-unread-group 2))
3518       (if (not (equal group (gnus-group-group-name)))
3519           (setq group (gnus-group-group-name))
3520         (setq group nil)))
3521     result))
3522
3523 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3524   "Directly jump to the other GROUP from summary buffer.
3525 If SHOW-ALL is non-nil, already read articles are also listed."
3526   (interactive
3527    (if (eq gnus-summary-buffer (current-buffer))
3528        (list (completing-read
3529               "Group: " gnus-active-hashtb nil t
3530               (when (and gnus-newsgroup-name
3531                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3532                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3533               'gnus-group-history)
3534              current-prefix-arg)
3535      (error "%s must be invoked from a gnus summary buffer." this-command)))
3536   (unless (or (zerop (length group))
3537               (and gnus-newsgroup-name
3538                    (string-equal gnus-newsgroup-name group)))
3539     (gnus-summary-exit)
3540     (gnus-summary-read-group group show-all
3541                              gnus-dont-select-after-jump-to-other-group)))
3542
3543 (defun gnus-summary-read-group-1 (group show-all no-article
3544                                         kill-buffer no-display
3545                                         &optional select-articles)
3546   ;; Killed foreign groups can't be entered.
3547   ;;  (when (and (not (gnus-group-native-p group))
3548   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3549   ;;    (error "Dead non-native groups can't be entered"))
3550   (gnus-message 5 "Retrieving newsgroup: %s..."
3551                 (gnus-group-decoded-name group))
3552   (let* ((new-group (gnus-summary-setup-buffer group))
3553          (quit-config (gnus-group-quit-config group))
3554          (did-select (and new-group (gnus-select-newsgroup
3555                                      group show-all select-articles))))
3556     (cond
3557      ;; This summary buffer exists already, so we just select it.
3558      ((not new-group)
3559       (gnus-set-global-variables)
3560       (when kill-buffer
3561         (gnus-kill-or-deaden-summary kill-buffer))
3562       (gnus-configure-windows 'summary 'force)
3563       (gnus-set-mode-line 'summary)
3564       (gnus-summary-position-point)
3565       (message "")
3566       t)
3567      ;; We couldn't select this group.
3568      ((null did-select)
3569       (when (and (eq major-mode 'gnus-summary-mode)
3570                  (not (equal (current-buffer) kill-buffer)))
3571         (kill-buffer (current-buffer))
3572         (if (not quit-config)
3573             (progn
3574               ;; Update the info -- marks might need to be removed,
3575               ;; for instance.
3576               (gnus-summary-update-info)
3577               (set-buffer gnus-group-buffer)
3578               (gnus-group-jump-to-group group)
3579               (gnus-group-next-unread-group 1))
3580           (gnus-handle-ephemeral-exit quit-config)))
3581       (let ((grpinfo (gnus-get-info group)))
3582         (if (null (gnus-info-read grpinfo))
3583             (gnus-message 3 "Group %s contains no messages"
3584                           (gnus-group-decoded-name group))
3585           (gnus-message 3 "Can't select group")))
3586       nil)
3587      ;; The user did a `C-g' while prompting for number of articles,
3588      ;; so we exit this group.
3589      ((eq did-select 'quit)
3590       (and (eq major-mode 'gnus-summary-mode)
3591            (not (equal (current-buffer) kill-buffer))
3592            (kill-buffer (current-buffer)))
3593       (when kill-buffer
3594         (gnus-kill-or-deaden-summary kill-buffer))
3595       (if (not quit-config)
3596           (progn
3597             (set-buffer gnus-group-buffer)
3598             (gnus-group-jump-to-group group)
3599             (gnus-group-next-unread-group 1)
3600             (gnus-configure-windows 'group 'force))
3601         (gnus-handle-ephemeral-exit quit-config))
3602       ;; Finally signal the quit.
3603       (signal 'quit nil))
3604      ;; The group was successfully selected.
3605      (t
3606       (gnus-set-global-variables)
3607       ;; Save the active value in effect when the group was entered.
3608       (setq gnus-newsgroup-active
3609             (gnus-copy-sequence
3610              (gnus-active gnus-newsgroup-name)))
3611       ;; You can change the summary buffer in some way with this hook.
3612       (gnus-run-hooks 'gnus-select-group-hook)
3613       (gnus-update-format-specifications
3614        nil 'summary 'summary-mode 'summary-dummy)
3615       (gnus-update-summary-mark-positions)
3616       ;; Do score processing.
3617       (when gnus-use-scoring
3618         (gnus-possibly-score-headers))
3619       ;; Check whether to fill in the gaps in the threads.
3620       (when gnus-build-sparse-threads
3621         (gnus-build-sparse-threads))
3622       ;; Find the initial limit.
3623       (if show-all
3624           (let ((gnus-newsgroup-dormant nil))
3625             (gnus-summary-initial-limit show-all))
3626         (gnus-summary-initial-limit show-all))
3627       ;; Generate the summary buffer.
3628       (unless no-display
3629         (gnus-summary-prepare))
3630       (when gnus-use-trees
3631         (gnus-tree-open group)
3632         (setq gnus-summary-highlight-line-function
3633               'gnus-tree-highlight-article))
3634       ;; If the summary buffer is empty, but there are some low-scored
3635       ;; articles or some excluded dormants, we include these in the
3636       ;; buffer.
3637       (when (and (zerop (buffer-size))
3638                  (not no-display))
3639         (cond (gnus-newsgroup-dormant
3640                (gnus-summary-limit-include-dormant))
3641               ((and gnus-newsgroup-scored show-all)
3642                (gnus-summary-limit-include-expunged t))))
3643       ;; Function `gnus-apply-kill-file' must be called in this hook.
3644       (gnus-run-hooks 'gnus-apply-kill-hook)
3645       (if (and (zerop (buffer-size))
3646                (not no-display))
3647           (progn
3648             ;; This newsgroup is empty.
3649             (gnus-summary-catchup-and-exit nil t)
3650             (gnus-message 6 "No unread news")
3651             (when kill-buffer
3652               (gnus-kill-or-deaden-summary kill-buffer))
3653             ;; Return nil from this function.
3654             nil)
3655         ;; Hide conversation thread subtrees.  We cannot do this in
3656         ;; gnus-summary-prepare-hook since kill processing may not
3657         ;; work with hidden articles.
3658         (gnus-summary-maybe-hide-threads)
3659         (when kill-buffer
3660           (gnus-kill-or-deaden-summary kill-buffer))
3661         (gnus-summary-auto-select-subject)
3662         ;; Show first unread article if requested.
3663         (if (and (not no-article)
3664                  (not no-display)
3665                  gnus-newsgroup-unreads
3666                  gnus-auto-select-first)
3667             (progn
3668               (gnus-configure-windows 'summary)
3669               (let ((art (gnus-summary-article-number)))
3670                 (unless (and (not gnus-plugged)
3671                              (or (memq art gnus-newsgroup-undownloaded)
3672                                  (memq art gnus-newsgroup-downloadable)))
3673                   (gnus-summary-goto-article art))))
3674           ;; Don't select any articles.
3675           (gnus-summary-position-point)
3676           (gnus-configure-windows 'summary 'force)
3677           (gnus-set-mode-line 'summary))
3678         (when (and gnus-auto-center-group
3679                    (get-buffer-window gnus-group-buffer t))
3680           ;; Gotta use windows, because recenter does weird stuff if
3681           ;; the current buffer ain't the displayed window.
3682           (let ((owin (selected-window)))
3683             (select-window (get-buffer-window gnus-group-buffer t))
3684             (when (gnus-group-goto-group group)
3685               (recenter))
3686             (select-window owin)))
3687         ;; Mark this buffer as "prepared".
3688         (setq gnus-newsgroup-prepared t)
3689         (gnus-run-hooks 'gnus-summary-prepared-hook)
3690         (unless (gnus-ephemeral-group-p group)
3691           (gnus-group-update-group group))
3692         t)))))
3693
3694 (defun gnus-summary-auto-select-subject ()
3695   "Select the subject line on initial group entry."
3696   (goto-char (point-min))
3697   (cond
3698    ((eq gnus-auto-select-subject 'best)
3699     (gnus-summary-best-unread-subject))
3700    ((eq gnus-auto-select-subject 'unread)
3701     (gnus-summary-first-unread-subject))
3702    ((eq gnus-auto-select-subject 'unseen)
3703     (gnus-summary-first-unseen-subject))
3704    ((eq gnus-auto-select-subject 'unseen-or-unread)
3705     (gnus-summary-first-unseen-or-unread-subject))
3706    ((eq gnus-auto-select-subject 'first)
3707     ;; Do nothing.
3708     )
3709    ((functionp gnus-auto-select-subject)
3710     (funcall gnus-auto-select-subject))))
3711
3712 (defun gnus-summary-prepare ()
3713   "Generate the summary buffer."
3714   (interactive)
3715   (let ((buffer-read-only nil))
3716     (erase-buffer)
3717     (setq gnus-newsgroup-data nil
3718           gnus-newsgroup-data-reverse nil)
3719     (gnus-run-hooks 'gnus-summary-generate-hook)
3720     ;; Generate the buffer, either with threads or without.
3721     (when gnus-newsgroup-headers
3722       (gnus-summary-prepare-threads
3723        (if gnus-show-threads
3724            (gnus-sort-gathered-threads
3725             (funcall gnus-summary-thread-gathering-function
3726                      (gnus-sort-threads
3727                       (gnus-cut-threads (gnus-make-threads)))))
3728          ;; Unthreaded display.
3729          (gnus-sort-articles gnus-newsgroup-headers))))
3730     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3731     ;; Call hooks for modifying summary buffer.
3732     (goto-char (point-min))
3733     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3734
3735 (defsubst gnus-general-simplify-subject (subject)
3736   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3737   (setq subject
3738         (cond
3739          ;; Truncate the subject.
3740          (gnus-simplify-subject-functions
3741           (gnus-map-function gnus-simplify-subject-functions subject))
3742          ((numberp gnus-summary-gather-subject-limit)
3743           (setq subject (gnus-simplify-subject-re subject))
3744           (if (> (length subject) gnus-summary-gather-subject-limit)
3745               (substring subject 0 gnus-summary-gather-subject-limit)
3746             subject))
3747          ;; Fuzzily simplify it.
3748          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3749           (gnus-simplify-subject-fuzzy subject))
3750          ;; Just remove the leading "Re:".
3751          (t
3752           (gnus-simplify-subject-re subject))))
3753
3754   (if (and gnus-summary-gather-exclude-subject
3755            (string-match gnus-summary-gather-exclude-subject subject))
3756       nil                               ; This article shouldn't be gathered
3757     subject))
3758
3759 (defun gnus-summary-simplify-subject-query ()
3760   "Query where the respool algorithm would put this article."
3761   (interactive)
3762   (gnus-summary-select-article)
3763   (message "%s"
3764            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3765
3766 (defun gnus-gather-threads-by-subject (threads)
3767   "Gather threads by looking at Subject headers."
3768   (if (not gnus-summary-make-false-root)
3769       threads
3770     (let ((hashtb (gnus-make-hashtable 1024))
3771           (prev threads)
3772           (result threads)
3773           subject hthread whole-subject)
3774       (while threads
3775         (setq subject (gnus-general-simplify-subject
3776                        (setq whole-subject (mail-header-subject
3777                                             (caar threads)))))
3778         (when subject
3779           (if (setq hthread (gnus-gethash subject hashtb))
3780               (progn
3781                 ;; We enter a dummy root into the thread, if we
3782                 ;; haven't done that already.
3783                 (unless (stringp (caar hthread))
3784                   (setcar hthread (list whole-subject (car hthread))))
3785                 ;; We add this new gathered thread to this gathered
3786                 ;; thread.
3787                 (setcdr (car hthread)
3788                         (nconc (cdar hthread) (list (car threads))))
3789                 ;; Remove it from the list of threads.
3790                 (setcdr prev (cdr threads))
3791                 (setq threads prev))
3792             ;; Enter this thread into the hash table.
3793             (gnus-sethash subject
3794                           (if gnus-summary-make-false-root-always
3795                               (progn
3796                                 ;; If you want a dummy root above all
3797                                 ;; threads...
3798                                 (setcar threads (list whole-subject
3799                                                       (car threads)))
3800                                 threads)
3801                             threads)
3802                           hashtb)))
3803         (setq prev threads)
3804         (setq threads (cdr threads)))
3805       result)))
3806
3807 (defun gnus-gather-threads-by-references (threads)
3808   "Gather threads by looking at References headers."
3809   (let ((idhashtb (gnus-make-hashtable 1024))
3810         (thhashtb (gnus-make-hashtable 1024))
3811         (prev threads)
3812         (result threads)
3813         ids references id gthread gid entered ref)
3814     (while threads
3815       (when (setq references (mail-header-references (caar threads)))
3816         (setq id (mail-header-id (caar threads))
3817               ids (inline (gnus-split-references references))
3818               entered nil)
3819         (while (setq ref (pop ids))
3820           (setq ids (delete ref ids))
3821           (if (not (setq gid (gnus-gethash ref idhashtb)))
3822               (progn
3823                 (gnus-sethash ref id idhashtb)
3824                 (gnus-sethash id threads thhashtb))
3825             (setq gthread (gnus-gethash gid thhashtb))
3826             (unless entered
3827               ;; We enter a dummy root into the thread, if we
3828               ;; haven't done that already.
3829               (unless (stringp (caar gthread))
3830                 (setcar gthread (list (mail-header-subject (caar gthread))
3831                                       (car gthread))))
3832               ;; We add this new gathered thread to this gathered
3833               ;; thread.
3834               (setcdr (car gthread)
3835                       (nconc (cdar gthread) (list (car threads)))))
3836             ;; Add it into the thread hash table.
3837             (gnus-sethash id gthread thhashtb)
3838             (setq entered t)
3839             ;; Remove it from the list of threads.
3840             (setcdr prev (cdr threads))
3841             (setq threads prev))))
3842       (setq prev threads)
3843       (setq threads (cdr threads)))
3844     result))
3845
3846 (defun gnus-sort-gathered-threads (threads)
3847   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3848   (let ((result threads))
3849     (while threads
3850       (when (stringp (caar threads))
3851         (setcdr (car threads)
3852                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3853       (setq threads (cdr threads)))
3854     result))
3855
3856 (defun gnus-thread-loop-p (root thread)
3857   "Say whether ROOT is in THREAD."
3858   (let ((stack (list thread))
3859         (infloop 0)
3860         th)
3861     (while (setq thread (pop stack))
3862       (setq th (cdr thread))
3863       (while (and th
3864                   (not (eq (caar th) root)))
3865         (pop th))
3866       (if th
3867           ;; We have found a loop.
3868           (let (ref-dep)
3869             (setcdr thread (delq (car th) (cdr thread)))
3870             (if (boundp (setq ref-dep (intern "none"
3871                                               gnus-newsgroup-dependencies)))
3872                 (setcdr (symbol-value ref-dep)
3873                         (nconc (cdr (symbol-value ref-dep))
3874                                (list (car th))))
3875               (set ref-dep (list nil (car th))))
3876             (setq infloop 1
3877                   stack nil))
3878         ;; Push all the subthreads onto the stack.
3879         (push (cdr thread) stack)))
3880     infloop))
3881
3882 (defun gnus-make-threads ()
3883   "Go through the dependency hashtb and find the roots.  Return all threads."
3884   (let (threads)
3885     (while (catch 'infloop
3886              (mapatoms
3887               (lambda (refs)
3888                 ;; Deal with self-referencing References loops.
3889                 (when (and (car (symbol-value refs))
3890                            (not (zerop
3891                                  (apply
3892                                   '+
3893                                   (mapcar
3894                                    (lambda (thread)
3895                                      (gnus-thread-loop-p
3896                                       (car (symbol-value refs)) thread))
3897                                    (cdr (symbol-value refs)))))))
3898                   (setq threads nil)
3899                   (throw 'infloop t))
3900                 (unless (car (symbol-value refs))
3901                   ;; These threads do not refer back to any other articles,
3902                   ;; so they're roots.
3903                   (setq threads (append (cdr (symbol-value refs)) threads))))
3904               gnus-newsgroup-dependencies)))
3905     threads))
3906
3907 ;; Build the thread tree.
3908 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3909   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3910
3911 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3912 if it was already present.
3913
3914 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3915 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3916 Message-IDs will be renamed to a unique Message-ID before being
3917 entered.
3918
3919 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3920   (let* ((id (mail-header-id header))
3921          (id-dep (and id (intern id dependencies)))
3922          parent-id ref ref-dep ref-header replaced)
3923     ;; Enter this `header' in the `dependencies' table.
3924     (cond
3925      ((not id-dep)
3926       (setq header nil))
3927      ;; The first two cases do the normal part: enter a new `header'
3928      ;; in the `dependencies' table.
3929      ((not (boundp id-dep))
3930       (set id-dep (list header)))
3931      ((null (car (symbol-value id-dep)))
3932       (setcar (symbol-value id-dep) header))
3933
3934      ;; From here the `header' was already present in the
3935      ;; `dependencies' table.
3936      (force-new
3937       ;; Overrides an existing entry;
3938       ;; just set the header part of the entry.
3939       (setcar (symbol-value id-dep) header)
3940       (setq replaced t))
3941
3942      ;; Renames the existing `header' to a unique Message-ID.
3943      ((not gnus-summary-ignore-duplicates)
3944       ;; An article with this Message-ID has already been seen.
3945       ;; We rename the Message-ID.
3946       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3947            (list header))
3948       (mail-header-set-id header id))
3949
3950      ;; The last case ignores an existing entry, except it adds any
3951      ;; additional Xrefs (in case the two articles came from different
3952      ;; servers.
3953      ;; Also sets `header' to `nil' meaning that the `dependencies'
3954      ;; table was *not* modified.
3955      (t
3956       (mail-header-set-xref
3957        (car (symbol-value id-dep))
3958        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3959                    "")
3960                (or (mail-header-xref header) "")))
3961       (setq header nil)))
3962
3963     (when (and header (not replaced))
3964       ;; First check that we are not creating a References loop.
3965       (setq parent-id (gnus-parent-id (mail-header-references header)))
3966       (setq ref parent-id)
3967       (while (and ref
3968                   (setq ref-dep (intern-soft ref dependencies))
3969                   (boundp ref-dep)
3970                   (setq ref-header (car (symbol-value ref-dep))))
3971         (if (string= id ref)
3972             ;; Yuk!  This is a reference loop.  Make the article be a
3973             ;; root article.
3974             (progn
3975               (mail-header-set-references (car (symbol-value id-dep)) "none")
3976               (setq ref nil)
3977               (setq parent-id nil))
3978           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3979       (setq ref-dep (intern (or parent-id "none") dependencies))
3980       (if (boundp ref-dep)
3981           (setcdr (symbol-value ref-dep)
3982                   (nconc (cdr (symbol-value ref-dep))
3983                          (list (symbol-value id-dep))))
3984         (set ref-dep (list nil (symbol-value id-dep)))))
3985     header))
3986
3987 (defun gnus-extract-message-id-from-in-reply-to (string)
3988   (if (string-match "<[^>]+>" string)
3989       (substring string (match-beginning 0) (match-end 0))
3990     nil))
3991
3992 (defun gnus-build-sparse-threads ()
3993   (let ((headers gnus-newsgroup-headers)
3994         (mail-parse-charset gnus-newsgroup-charset)
3995         (gnus-summary-ignore-duplicates t)
3996         header references generation relations
3997         subject child end new-child date)
3998     ;; First we create an alist of generations/relations, where
3999     ;; generations is how much we trust the relation, and the relation
4000     ;; is parent/child.
4001     (gnus-message 7 "Making sparse threads...")
4002     (save-excursion
4003       (nnheader-set-temp-buffer " *gnus sparse threads*")
4004       (while (setq header (pop headers))
4005         (when (and (setq references (mail-header-references header))
4006                    (not (string= references "")))
4007           (insert references)
4008           (setq child (mail-header-id header)
4009                 subject (mail-header-subject header)
4010                 date (mail-header-date header)
4011                 generation 0)
4012           (while (search-backward ">" nil t)
4013             (setq end (1+ (point)))
4014             (when (search-backward "<" nil t)
4015               (setq new-child (buffer-substring (point) end))
4016               (push (list (incf generation)
4017                           child (setq child new-child)
4018                           subject date)
4019                     relations)))
4020           (when child
4021             (push (list (1+ generation) child nil subject) relations))
4022           (erase-buffer)))
4023       (kill-buffer (current-buffer)))
4024     ;; Sort over trustworthiness.
4025     (mapcar
4026      (lambda (relation)
4027        (when (gnus-dependencies-add-header
4028               (make-full-mail-header-from-decoded-header
4029                gnus-reffed-article-number
4030                (nth 3 relation) "" (or (nth 4 relation) "")
4031                (nth 1 relation)
4032                (or (nth 2 relation) "") 0 0 "")
4033               gnus-newsgroup-dependencies nil)
4034          (push gnus-reffed-article-number gnus-newsgroup-limit)
4035          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4036          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4037                gnus-newsgroup-reads)
4038          (decf gnus-reffed-article-number)))
4039      (sort relations 'car-less-than-car))
4040     (gnus-message 7 "Making sparse threads...done")))
4041
4042 (defun gnus-build-old-threads ()
4043   ;; Look at all the articles that refer back to old articles, and
4044   ;; fetch the headers for the articles that aren't there.  This will
4045   ;; build complete threads - if the roots haven't been expired by the
4046   ;; server, that is.
4047   (let ((mail-parse-charset gnus-newsgroup-charset)
4048         id heads)
4049     (mapatoms
4050      (lambda (refs)
4051        (when (not (car (symbol-value refs)))
4052          (setq heads (cdr (symbol-value refs)))
4053          (while heads
4054            (if (memq (mail-header-number (caar heads))
4055                      gnus-newsgroup-dormant)
4056                (setq heads (cdr heads))
4057              (setq id (symbol-name refs))
4058              (while (and (setq id (gnus-build-get-header id))
4059                          (not (car (gnus-id-to-thread id)))))
4060              (setq heads nil)))))
4061      gnus-newsgroup-dependencies)))
4062
4063 (defsubst gnus-remove-odd-characters (string)
4064   "Translate STRING into something that doesn't contain weird characters."
4065   (mm-subst-char-in-string
4066    ?\r ?\-
4067    (mm-subst-char-in-string ?\n ?\- string t) t))
4068
4069 ;; This function has to be called with point after the article number
4070 ;; on the beginning of the line.
4071 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4072   (let ((eol (point-at-eol))
4073         (buffer (current-buffer))
4074         header references in-reply-to)
4075
4076     ;; overview: [num subject from date id refs chars lines misc]
4077     (unwind-protect
4078         (progn
4079           (narrow-to-region (point) eol)
4080           (unless (eobp)
4081             (forward-char))
4082
4083           (setq header
4084                 (make-full-mail-header
4085                  number                         ; number
4086                  (nnheader-nov-field)           ; subject
4087                  (nnheader-nov-field)           ; from
4088                  (nnheader-nov-field)           ; date
4089                  (nnheader-nov-read-message-id number)  ; id
4090                  (setq references (nnheader-nov-field)) ; refs
4091                  (nnheader-nov-read-integer)    ; chars
4092                  (nnheader-nov-read-integer)    ; lines
4093                  (unless (eobp)
4094                    (if (looking-at "Xref: ")
4095                        (goto-char (match-end 0)))
4096                    (nnheader-nov-field))        ; Xref
4097                  (nnheader-nov-parse-extra))))  ; extra
4098
4099       (widen))
4100
4101     (when (and (string= references "")
4102                (setq in-reply-to (mail-header-extra header))
4103                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4104       (mail-header-set-references
4105        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4106
4107     (when gnus-alter-header-function
4108       (funcall gnus-alter-header-function header))
4109     (gnus-dependencies-add-header header dependencies force-new)))
4110
4111 (defun gnus-build-get-header (id)
4112   "Look through the buffer of NOV lines and find the header to ID.
4113 Enter this line into the dependencies hash table, and return
4114 the id of the parent article (if any)."
4115   (let ((deps gnus-newsgroup-dependencies)
4116         found header)
4117     (prog1
4118         (save-excursion
4119           (set-buffer nntp-server-buffer)
4120           (let ((case-fold-search nil))
4121             (goto-char (point-min))
4122             (while (and (not found)
4123                         (search-forward id nil t))
4124               (beginning-of-line)
4125               (setq found (looking-at
4126                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4127                                    (regexp-quote id))))
4128               (or found (beginning-of-line 2)))
4129             (when found
4130               (beginning-of-line)
4131               (and
4132                (setq header (gnus-nov-parse-line
4133                              (read (current-buffer)) deps))
4134                (gnus-parent-id (mail-header-references header))))))
4135       (when header
4136         (let ((number (mail-header-number header)))
4137           (push number gnus-newsgroup-limit)
4138           (push header gnus-newsgroup-headers)
4139           (if (memq number gnus-newsgroup-unselected)
4140               (progn
4141                 (setq gnus-newsgroup-unreads
4142                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4143                                                number))
4144                 (setq gnus-newsgroup-unselected
4145                       (delq number gnus-newsgroup-unselected)))
4146             (push number gnus-newsgroup-ancient)))))))
4147
4148 (defun gnus-build-all-threads ()
4149   "Read all the headers."
4150   (let ((gnus-summary-ignore-duplicates t)
4151         (mail-parse-charset gnus-newsgroup-charset)
4152         (dependencies gnus-newsgroup-dependencies)
4153         header article)
4154     (save-excursion
4155       (set-buffer nntp-server-buffer)
4156       (let ((case-fold-search nil))
4157         (goto-char (point-min))
4158         (while (not (eobp))
4159           (ignore-errors
4160             (setq article (read (current-buffer))
4161                   header (gnus-nov-parse-line article dependencies)))
4162           (when header
4163             (with-current-buffer gnus-summary-buffer
4164               (push header gnus-newsgroup-headers)
4165               (if (memq (setq article (mail-header-number header))
4166                         gnus-newsgroup-unselected)
4167                   (progn
4168                     (setq gnus-newsgroup-unreads
4169                           (gnus-add-to-sorted-list
4170                            gnus-newsgroup-unreads article))
4171                     (setq gnus-newsgroup-unselected
4172                           (delq article gnus-newsgroup-unselected)))
4173                 (push article gnus-newsgroup-ancient)))
4174             (forward-line 1)))))))
4175
4176 (defun gnus-summary-update-article-line (article header)
4177   "Update the line for ARTICLE using HEADER."
4178   (let* ((id (mail-header-id header))
4179          (thread (gnus-id-to-thread id)))
4180     (unless thread
4181       (error "Article in no thread"))
4182     ;; Update the thread.
4183     (setcar thread header)
4184     (gnus-summary-goto-subject article)
4185     (let* ((datal (gnus-data-find-list article))
4186            (data (car datal))
4187            (buffer-read-only nil)
4188            (level (gnus-summary-thread-level)))
4189       (gnus-delete-line)
4190       (let ((inserted (- (point)
4191                          (progn
4192                            (gnus-summary-insert-line
4193                             header level nil
4194                             (memq article gnus-newsgroup-undownloaded)
4195                             (gnus-article-mark article)
4196                             (memq article gnus-newsgroup-replied)
4197                             (memq article gnus-newsgroup-expirable)
4198                             ;; Only insert the Subject string when it's different
4199                             ;; from the previous Subject string.
4200                             (if (and
4201                                  gnus-show-threads
4202                                  (gnus-subject-equal
4203                                   (condition-case ()
4204                                       (mail-header-subject
4205                                        (gnus-data-header
4206                                         (cadr
4207                                          (gnus-data-find-list
4208                                           article
4209                                           (gnus-data-list t)))))
4210                                     ;; Error on the side of excessive subjects.
4211                                     (error ""))
4212                                   (mail-header-subject header)))
4213                                 ""
4214                               (mail-header-subject header))
4215                             nil (cdr (assq article gnus-newsgroup-scored))
4216                             (memq article gnus-newsgroup-processable))
4217                            (point)))))
4218         (when (cdr datal)
4219           (gnus-data-update-list
4220            (cdr datal)
4221            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4222
4223 (defun gnus-summary-update-article (article &optional iheader)
4224   "Update ARTICLE in the summary buffer."
4225   (set-buffer gnus-summary-buffer)
4226   (let* ((header (gnus-summary-article-header article))
4227          (id (mail-header-id header))
4228          (data (gnus-data-find article))
4229          (thread (gnus-id-to-thread id))
4230          (references (mail-header-references header))
4231          (parent
4232           (gnus-id-to-thread
4233            (or (gnus-parent-id
4234                 (when (and references
4235                            (not (equal "" references)))
4236                   references))
4237                "none")))
4238          (buffer-read-only nil)
4239          (old (car thread)))
4240     (when thread
4241       (unless iheader
4242         (setcar thread nil)
4243         (when parent
4244           (delq thread parent)))
4245       (if (gnus-summary-insert-subject id header)
4246           ;; Set the (possibly) new article number in the data structure.
4247           (gnus-data-set-number data (gnus-id-to-article id))
4248         (setcar thread old)
4249         nil))))
4250
4251 (defun gnus-rebuild-thread (id &optional line)
4252   "Rebuild the thread containing ID.
4253 If LINE, insert the rebuilt thread starting on line LINE."
4254   (let ((buffer-read-only nil)
4255         old-pos current thread data)
4256     (if (not gnus-show-threads)
4257         (setq thread (list (car (gnus-id-to-thread id))))
4258       ;; Get the thread this article is part of.
4259       (setq thread (gnus-remove-thread id)))
4260     (setq old-pos (point-at-bol))
4261     (setq current (save-excursion
4262                     (and (re-search-backward "[\r\n]" nil t)
4263                          (gnus-summary-article-number))))
4264     ;; If this is a gathered thread, we have to go some re-gathering.
4265     (when (stringp (car thread))
4266       (let ((subject (car thread))
4267             roots thr)
4268         (setq thread (cdr thread))
4269         (while thread
4270           (unless (memq (setq thr (gnus-id-to-thread
4271                                    (gnus-root-id
4272                                     (mail-header-id (caar thread)))))
4273                         roots)
4274             (push thr roots))
4275           (setq thread (cdr thread)))
4276         ;; We now have all (unique) roots.
4277         (if (= (length roots) 1)
4278             ;; All the loose roots are now one solid root.
4279             (setq thread (car roots))
4280           (setq thread (cons subject (gnus-sort-threads roots))))))
4281     (let (threads)
4282       ;; We then insert this thread into the summary buffer.
4283       (when line
4284         (goto-char (point-min))
4285         (forward-line (1- line)))
4286       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4287         (if gnus-show-threads
4288             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4289           (gnus-summary-prepare-unthreaded thread))
4290         (setq data (nreverse gnus-newsgroup-data))
4291         (setq threads gnus-newsgroup-threads))
4292       ;; We splice the new data into the data structure.
4293       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4294       ;;!!! then we want to insert at the beginning of the buffer.
4295       ;;!!! That happens to be true with Gnus now, but that may
4296       ;;!!! change in the future.  Perhaps.
4297       (gnus-data-enter-list
4298        (if line nil current) data (- (point) old-pos))
4299       (setq gnus-newsgroup-threads
4300             (nconc threads gnus-newsgroup-threads))
4301       (gnus-data-compute-positions))))
4302
4303 (defun gnus-number-to-header (number)
4304   "Return the header for article NUMBER."
4305   (let ((headers gnus-newsgroup-headers))
4306     (while (and headers
4307                 (not (= number (mail-header-number (car headers)))))
4308       (pop headers))
4309     (when headers
4310       (car headers))))
4311
4312 (defun gnus-parent-headers (in-headers &optional generation)
4313   "Return the headers of the GENERATIONeth parent of HEADERS."
4314   (unless generation
4315     (setq generation 1))
4316   (let ((parent t)
4317         (headers in-headers)
4318         references)
4319     (while (and parent
4320                 (not (zerop generation))
4321                 (setq references (mail-header-references headers)))
4322       (setq headers (if (and references
4323                              (setq parent (gnus-parent-id references)))
4324                         (car (gnus-id-to-thread parent))
4325                       nil))
4326       (decf generation))
4327     (and (not (eq headers in-headers))
4328          headers)))
4329
4330 (defun gnus-id-to-thread (id)
4331   "Return the (sub-)thread where ID appears."
4332   (gnus-gethash id gnus-newsgroup-dependencies))
4333
4334 (defun gnus-id-to-article (id)
4335   "Return the article number of ID."
4336   (let ((thread (gnus-id-to-thread id)))
4337     (when (and thread
4338                (car thread))
4339       (mail-header-number (car thread)))))
4340
4341 (defun gnus-id-to-header (id)
4342   "Return the article headers of ID."
4343   (car (gnus-id-to-thread id)))
4344
4345 (defun gnus-article-displayed-root-p (article)
4346   "Say whether ARTICLE is a root(ish) article."
4347   (let ((level (gnus-summary-thread-level article))
4348         (refs (mail-header-references  (gnus-summary-article-header article)))
4349         particle)
4350     (cond
4351      ((null level) nil)
4352      ((zerop level) t)
4353      ((null refs) t)
4354      ((null (gnus-parent-id refs)) t)
4355      ((and (= 1 level)
4356            (null (setq particle (gnus-id-to-article
4357                                  (gnus-parent-id refs))))
4358            (null (gnus-summary-thread-level particle)))))))
4359
4360 (defun gnus-root-id (id)
4361   "Return the id of the root of the thread where ID appears."
4362   (let (last-id prev)
4363     (while (and id (setq prev (car (gnus-id-to-thread id))))
4364       (setq last-id id
4365             id (gnus-parent-id (mail-header-references prev))))
4366     last-id))
4367
4368 (defun gnus-articles-in-thread (thread)
4369   "Return the list of articles in THREAD."
4370   (cons (mail-header-number (car thread))
4371         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4372
4373 (defun gnus-remove-thread (id &optional dont-remove)
4374   "Remove the thread that has ID in it."
4375   (let (headers thread last-id)
4376     ;; First go up in this thread until we find the root.
4377     (setq last-id (gnus-root-id id)
4378           headers (message-flatten-list (gnus-id-to-thread last-id)))
4379     ;; We have now found the real root of this thread.  It might have
4380     ;; been gathered into some loose thread, so we have to search
4381     ;; through the threads to find the thread we wanted.
4382     (let ((threads gnus-newsgroup-threads)
4383           sub)
4384       (while threads
4385         (setq sub (car threads))
4386         (if (stringp (car sub))
4387             ;; This is a gathered thread, so we look at the roots
4388             ;; below it to find whether this article is in this
4389             ;; gathered root.
4390             (progn
4391               (setq sub (cdr sub))
4392               (while sub
4393                 (when (member (caar sub) headers)
4394                   (setq thread (car threads)
4395                         threads nil
4396                         sub nil))
4397                 (setq sub (cdr sub))))
4398           ;; It's an ordinary thread, so we check it.
4399           (when (eq (car sub) (car headers))
4400             (setq thread sub
4401                   threads nil)))
4402         (setq threads (cdr threads)))
4403       ;; If this article is in no thread, then it's a root.
4404       (if thread
4405           (unless dont-remove
4406             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4407         (setq thread (gnus-id-to-thread last-id)))
4408       (when thread
4409         (prog1
4410             thread                      ; We return this thread.
4411           (unless dont-remove
4412             (if (stringp (car thread))
4413                 (progn
4414                   ;; If we use dummy roots, then we have to remove the
4415                   ;; dummy root as well.
4416                   (when (eq gnus-summary-make-false-root 'dummy)
4417                     ;; We go to the dummy root by going to
4418                     ;; the first sub-"thread", and then one line up.
4419                     (gnus-summary-goto-article
4420                      (mail-header-number (caadr thread)))
4421                     (forward-line -1)
4422                     (gnus-delete-line)
4423                     (gnus-data-compute-positions))
4424                   (setq thread (cdr thread))
4425                   (while thread
4426                     (gnus-remove-thread-1 (car thread))
4427                     (setq thread (cdr thread))))
4428               (gnus-remove-thread-1 thread))))))))
4429
4430 (defun gnus-remove-thread-1 (thread)
4431   "Remove the thread THREAD recursively."
4432   (let ((number (mail-header-number (pop thread)))
4433         d)
4434     (setq thread (reverse thread))
4435     (while thread
4436       (gnus-remove-thread-1 (pop thread)))
4437     (when (setq d (gnus-data-find number))
4438       (goto-char (gnus-data-pos d))
4439       (gnus-summary-show-thread)
4440       (gnus-data-remove
4441        number
4442        (- (point-at-bol)
4443           (prog1
4444               (1+ (point-at-eol))
4445             (gnus-delete-line)))))))
4446
4447 (defun gnus-sort-threads-1 (threads func)
4448   (sort (mapcar (lambda (thread)
4449                   (cons (car thread)
4450                         (and (cdr thread)
4451                              (gnus-sort-threads-1 (cdr thread) func))))
4452                 threads) func))
4453
4454 (defun gnus-sort-threads (threads)
4455   "Sort THREADS."
4456   (if (not gnus-thread-sort-functions)
4457       threads
4458     (gnus-message 8 "Sorting threads...")
4459     (let ((max-lisp-eval-depth 5000))
4460       (prog1 (gnus-sort-threads-1
4461          threads
4462          (gnus-make-sort-function gnus-thread-sort-functions))
4463         (gnus-message 8 "Sorting threads...done")))))
4464
4465 (defun gnus-sort-articles (articles)
4466   "Sort ARTICLES."
4467   (when gnus-article-sort-functions
4468     (gnus-message 7 "Sorting articles...")
4469     (prog1
4470         (setq gnus-newsgroup-headers
4471               (sort articles (gnus-make-sort-function
4472                               gnus-article-sort-functions)))
4473       (gnus-message 7 "Sorting articles...done"))))
4474
4475 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4476 (defmacro gnus-thread-header (thread)
4477   "Return header of first article in THREAD.
4478 Note that THREAD must never, ever be anything else than a variable -
4479 using some other form will lead to serious barfage."
4480   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4481   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4482   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4483         (vector thread) 2))
4484
4485 (defsubst gnus-article-sort-by-number (h1 h2)
4486   "Sort articles by article number."
4487   (< (mail-header-number h1)
4488      (mail-header-number h2)))
4489
4490 (defun gnus-thread-sort-by-number (h1 h2)
4491   "Sort threads by root article number."
4492   (gnus-article-sort-by-number
4493    (gnus-thread-header h1) (gnus-thread-header h2)))
4494
4495 (defsubst gnus-article-sort-by-random (h1 h2)
4496   "Sort articles by article number."
4497   (zerop (random 2)))
4498
4499 (defun gnus-thread-sort-by-random (h1 h2)
4500   "Sort threads by root article number."
4501   (gnus-article-sort-by-random
4502    (gnus-thread-header h1) (gnus-thread-header h2)))
4503
4504 (defsubst gnus-article-sort-by-lines (h1 h2)
4505   "Sort articles by article Lines header."
4506   (< (mail-header-lines h1)
4507      (mail-header-lines h2)))
4508
4509 (defun gnus-thread-sort-by-lines (h1 h2)
4510   "Sort threads by root article Lines header."
4511   (gnus-article-sort-by-lines
4512    (gnus-thread-header h1) (gnus-thread-header h2)))
4513
4514 (defsubst gnus-article-sort-by-chars (h1 h2)
4515   "Sort articles by octet length."
4516   (< (mail-header-chars h1)
4517      (mail-header-chars h2)))
4518
4519 (defun gnus-thread-sort-by-chars (h1 h2)
4520   "Sort threads by root article octet length."
4521   (gnus-article-sort-by-chars
4522    (gnus-thread-header h1) (gnus-thread-header h2)))
4523
4524 (defsubst gnus-article-sort-by-author (h1 h2)
4525   "Sort articles by root author."
4526   (string-lessp
4527    (let ((addr (car (mime-entity-read-field h1 'From))))
4528      (or (std11-full-name-string addr)
4529          (std11-address-string addr)
4530          ""))
4531    (let ((addr (car (mime-entity-read-field h2 'From))))
4532      (or (std11-full-name-string addr)
4533          (std11-address-string addr)
4534          ""))
4535    ))
4536
4537 (defun gnus-thread-sort-by-author (h1 h2)
4538   "Sort threads by root author."
4539   (gnus-article-sort-by-author
4540    (gnus-thread-header h1)  (gnus-thread-header h2)))
4541
4542 (defsubst gnus-article-sort-by-subject (h1 h2)
4543   "Sort articles by root subject."
4544   (string-lessp
4545    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4546    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4547
4548 (defun gnus-thread-sort-by-subject (h1 h2)
4549   "Sort threads by root subject."
4550   (gnus-article-sort-by-subject
4551    (gnus-thread-header h1) (gnus-thread-header h2)))
4552
4553 (defsubst gnus-article-sort-by-date (h1 h2)
4554   "Sort articles by root article date."
4555   (time-less-p
4556    (gnus-date-get-time (mail-header-date h1))
4557    (gnus-date-get-time (mail-header-date h2))))
4558
4559 (defun gnus-thread-sort-by-date (h1 h2)
4560   "Sort threads by root article date."
4561   (gnus-article-sort-by-date
4562    (gnus-thread-header h1) (gnus-thread-header h2)))
4563
4564 (defsubst gnus-article-sort-by-score (h1 h2)
4565   "Sort articles by root article score.
4566 Unscored articles will be counted as having a score of zero."
4567   (> (or (cdr (assq (mail-header-number h1)
4568                     gnus-newsgroup-scored))
4569          gnus-summary-default-score 0)
4570      (or (cdr (assq (mail-header-number h2)
4571                     gnus-newsgroup-scored))
4572          gnus-summary-default-score 0)))
4573
4574 (defun gnus-thread-sort-by-score (h1 h2)
4575   "Sort threads by root article score."
4576   (gnus-article-sort-by-score
4577    (gnus-thread-header h1) (gnus-thread-header h2)))
4578
4579 (defun gnus-thread-sort-by-total-score (h1 h2)
4580   "Sort threads by the sum of all scores in the thread.
4581 Unscored articles will be counted as having a score of zero."
4582   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4583
4584 (defun gnus-thread-total-score (thread)
4585   ;; This function find the total score of THREAD.
4586   (cond
4587    ((null thread)
4588     0)
4589    ((consp thread)
4590     (if (stringp (car thread))
4591         (apply gnus-thread-score-function 0
4592                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4593       (gnus-thread-total-score-1 thread)))
4594    (t
4595     (gnus-thread-total-score-1 (list thread)))))
4596
4597 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4598   "Sort threads such that the thread with the most recently arrived article comes first."
4599   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4600
4601 (defun gnus-thread-highest-number (thread)
4602   "Return the highest article number in THREAD."
4603   (apply 'max (mapcar (lambda (header)
4604                         (mail-header-number header))
4605                       (message-flatten-list thread))))
4606
4607 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4608   "Sort threads such that the thread with the most recently dated article comes first."
4609   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4610
4611 (defun gnus-thread-latest-date (thread)
4612   "Return the highest article date in THREAD."
4613   (let ((previous-time 0))
4614     (apply 'max
4615            (mapcar
4616             (lambda (header)
4617               (setq previous-time
4618                     (condition-case ()
4619                         (time-to-seconds (mail-header-parse-date
4620                                           (mail-header-date header)))
4621                       (error previous-time))))
4622             (sort
4623              (message-flatten-list thread)
4624              (lambda (h1 h2)
4625                (< (mail-header-number h1)
4626                   (mail-header-number h2))))))))
4627
4628 (defun gnus-thread-total-score-1 (root)
4629   ;; This function find the total score of the thread below ROOT.
4630   (setq root (car root))
4631   (apply gnus-thread-score-function
4632          (or (append
4633               (mapcar 'gnus-thread-total-score
4634                       (cdr (gnus-id-to-thread (mail-header-id root))))
4635               (when (> (mail-header-number root) 0)
4636                 (list (or (cdr (assq (mail-header-number root)
4637                                      gnus-newsgroup-scored))
4638                           gnus-summary-default-score 0))))
4639              (list gnus-summary-default-score)
4640              '(0))))
4641
4642 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4643 (defvar gnus-tmp-prev-subject nil)
4644 (defvar gnus-tmp-false-parent nil)
4645 (defvar gnus-tmp-root-expunged nil)
4646 (defvar gnus-tmp-dummy-line nil)
4647
4648 (eval-when-compile (defvar gnus-tmp-header))
4649 (defun gnus-extra-header (type &optional header)
4650   "Return the extra header of TYPE."
4651   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4652       ""))
4653
4654 (defvar gnus-tmp-thread-tree-header-string "")
4655
4656 (defcustom gnus-sum-thread-tree-root "> "
4657   "With %B spec, used for the root of a thread.
4658 If nil, use subject instead."
4659   :version "21.4"
4660   :type '(radio (const :format "%v  " nil) (string :size 0))
4661   :group 'gnus-thread)
4662 (defcustom gnus-sum-thread-tree-false-root "> "
4663   "With %B spec, used for a false root of a thread.
4664 If nil, use subject instead."
4665   :version "21.4"
4666   :type '(radio (const :format "%v  " nil) (string :size 0))
4667   :group 'gnus-thread)
4668 (defcustom gnus-sum-thread-tree-single-indent ""
4669   "With %B spec, used for a thread with just one message.
4670 If nil, use subject instead."
4671   :version "21.4"
4672   :type '(radio (const :format "%v  " nil) (string :size 0))
4673   :group 'gnus-thread)
4674 (defcustom gnus-sum-thread-tree-vertical "| "
4675   "With %B spec, used for drawing a vertical line."
4676   :version "21.4"
4677   :type 'string
4678   :group 'gnus-thread)
4679 (defcustom gnus-sum-thread-tree-indent "  "
4680   "With %B spec, used for indenting."
4681   :version "21.4"
4682   :type 'string
4683   :group 'gnus-thread)
4684 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4685   "With %B spec, used for a leaf with brothers."
4686   :version "21.4"
4687   :type 'string
4688   :group 'gnus-thread)
4689 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4690   "With %B spec, used for a leaf without brothers."
4691   :version "21.4"
4692   :type 'string
4693   :group 'gnus-thread)
4694
4695 (defun gnus-summary-prepare-threads (threads)
4696   "Prepare summary buffer from THREADS and indentation LEVEL.
4697 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4698 or a straight list of headers."
4699   (gnus-message 7 "Generating summary...")
4700
4701   (setq gnus-newsgroup-threads threads)
4702   (beginning-of-line)
4703
4704   (let ((gnus-tmp-level 0)
4705         (default-score (or gnus-summary-default-score 0))
4706         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4707         (building-line-count gnus-summary-display-while-building)
4708         (building-count (integerp gnus-summary-display-while-building))
4709         thread number subject stack state gnus-tmp-gathered beg-match
4710         new-roots gnus-tmp-new-adopts thread-end simp-subject
4711         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4712         gnus-tmp-replied gnus-tmp-subject-or-nil
4713         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4714         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4715         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4716         tree-stack)
4717
4718     (setq gnus-tmp-prev-subject nil
4719           gnus-tmp-thread-tree-header-string "")
4720
4721     (if (vectorp (car threads))
4722         ;; If this is a straight (sic) list of headers, then a
4723         ;; threaded summary display isn't required, so we just create
4724         ;; an unthreaded one.
4725         (gnus-summary-prepare-unthreaded threads)
4726
4727       ;; Do the threaded display.
4728
4729       (if gnus-summary-display-while-building
4730           (switch-to-buffer (buffer-name)))
4731       (while (or threads stack gnus-tmp-new-adopts new-roots)
4732
4733         (if (and (= gnus-tmp-level 0)
4734                  (or (not stack)
4735                      (= (caar stack) 0))
4736                  (not gnus-tmp-false-parent)
4737                  (or gnus-tmp-new-adopts new-roots))
4738             (if gnus-tmp-new-adopts
4739                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4740                       thread (list (car gnus-tmp-new-adopts))
4741                       gnus-tmp-header (caar thread)
4742                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4743               (when new-roots
4744                 (setq thread (list (car new-roots))
4745                       gnus-tmp-header (caar thread)
4746                       new-roots (cdr new-roots))))
4747
4748           (if threads
4749               ;; If there are some threads, we do them before the
4750               ;; threads on the stack.
4751               (setq thread threads
4752                     gnus-tmp-header (caar thread))
4753             ;; There were no current threads, so we pop something off
4754             ;; the stack.
4755             (setq state (car stack)
4756                   gnus-tmp-level (car state)
4757                   tree-stack (cadr state)
4758                   thread (caddr state)
4759                   stack (cdr stack)
4760                   gnus-tmp-header (caar thread))))
4761
4762         (setq gnus-tmp-false-parent nil)
4763         (setq gnus-tmp-root-expunged nil)
4764         (setq thread-end nil)
4765
4766         (if (stringp gnus-tmp-header)
4767             ;; The header is a dummy root.
4768             (cond
4769              ((eq gnus-summary-make-false-root 'adopt)
4770               ;; We let the first article adopt the rest.
4771               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4772                                                (cddar thread)))
4773               (setq gnus-tmp-gathered
4774                     (nconc (mapcar
4775                             (lambda (h) (mail-header-number (car h)))
4776                             (cddar thread))
4777                            gnus-tmp-gathered))
4778               (setq thread (cons (list (caar thread)
4779                                        (cadar thread))
4780                                  (cdr thread)))
4781               (setq gnus-tmp-level -1
4782                     gnus-tmp-false-parent t))
4783              ((eq gnus-summary-make-false-root 'empty)
4784               ;; We print adopted articles with empty subject fields.
4785               (setq gnus-tmp-gathered
4786                     (nconc (mapcar
4787                             (lambda (h) (mail-header-number (car h)))
4788                             (cddar thread))
4789                            gnus-tmp-gathered))
4790               (setq gnus-tmp-level -1))
4791              ((eq gnus-summary-make-false-root 'dummy)
4792               ;; We remember that we probably want to output a dummy
4793               ;; root.
4794               (setq gnus-tmp-dummy-line gnus-tmp-header)
4795               (setq gnus-tmp-prev-subject gnus-tmp-header))
4796              (t
4797               ;; We do not make a root for the gathered
4798               ;; sub-threads at all.
4799               (setq gnus-tmp-level -1)))
4800
4801           (setq number (mail-header-number gnus-tmp-header)
4802                 subject (mail-header-subject gnus-tmp-header)
4803                 simp-subject (gnus-simplify-subject-fully subject))
4804
4805           (cond
4806            ;; If the thread has changed subject, we might want to make
4807            ;; this subthread into a root.
4808            ((and (null gnus-thread-ignore-subject)
4809                  (not (zerop gnus-tmp-level))
4810                  gnus-tmp-prev-subject
4811                  (not (string= gnus-tmp-prev-subject simp-subject)))
4812             (setq new-roots (nconc new-roots (list (car thread)))
4813                   thread-end t
4814                   gnus-tmp-header nil))
4815            ;; If the article lies outside the current limit,
4816            ;; then we do not display it.
4817            ((not (memq number gnus-newsgroup-limit))
4818             (setq gnus-tmp-gathered
4819                   (nconc (mapcar
4820                           (lambda (h) (mail-header-number (car h)))
4821                           (cdar thread))
4822                          gnus-tmp-gathered))
4823             (setq gnus-tmp-new-adopts (if (cdar thread)
4824                                           (append gnus-tmp-new-adopts
4825                                                   (cdar thread))
4826                                         gnus-tmp-new-adopts)
4827                   thread-end t
4828                   gnus-tmp-header nil)
4829             (when (zerop gnus-tmp-level)
4830               (setq gnus-tmp-root-expunged t)))
4831            ;; Perhaps this article is to be marked as read?
4832            ((and gnus-summary-mark-below
4833                  (< (or (cdr (assq number gnus-newsgroup-scored))
4834                         default-score)
4835                     gnus-summary-mark-below)
4836                  ;; Don't touch sparse articles.
4837                  (not (gnus-summary-article-sparse-p number))
4838                  (not (gnus-summary-article-ancient-p number)))
4839             (setq gnus-newsgroup-unreads
4840                   (delq number gnus-newsgroup-unreads))
4841             (if gnus-newsgroup-auto-expire
4842                 (setq gnus-newsgroup-expirable
4843                       (gnus-add-to-sorted-list
4844                        gnus-newsgroup-expirable number))
4845               (push (cons number gnus-low-score-mark)
4846                     gnus-newsgroup-reads))))
4847
4848           (when gnus-tmp-header
4849             ;; We may have an old dummy line to output before this
4850             ;; article.
4851             (when (and gnus-tmp-dummy-line
4852                        (gnus-subject-equal
4853                         gnus-tmp-dummy-line
4854                         (mail-header-subject gnus-tmp-header)))
4855               (gnus-summary-insert-dummy-line
4856                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4857               (setq gnus-tmp-dummy-line nil))
4858
4859             ;; Compute the mark.
4860             (setq gnus-tmp-unread (gnus-article-mark number))
4861
4862             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4863                                   gnus-tmp-header gnus-tmp-level)
4864                   gnus-newsgroup-data)
4865
4866             ;; Actually insert the line.
4867             (setq
4868              gnus-tmp-subject-or-nil
4869              (cond
4870               ((and gnus-thread-ignore-subject
4871                     gnus-tmp-prev-subject
4872                     (not (string= gnus-tmp-prev-subject simp-subject)))
4873                subject)
4874               ((zerop gnus-tmp-level)
4875                (if (and (eq gnus-summary-make-false-root 'empty)
4876                         (memq number gnus-tmp-gathered)
4877                         gnus-tmp-prev-subject
4878                         (string= gnus-tmp-prev-subject simp-subject))
4879                    gnus-summary-same-subject
4880                  subject))
4881               (t gnus-summary-same-subject)))
4882             (if (and (eq gnus-summary-make-false-root 'adopt)
4883                      (= gnus-tmp-level 1)
4884                      (memq number gnus-tmp-gathered))
4885                 (setq gnus-tmp-opening-bracket ?\<
4886                       gnus-tmp-closing-bracket ?\>)
4887               (setq gnus-tmp-opening-bracket ?\[
4888                     gnus-tmp-closing-bracket ?\]))
4889             (setq
4890              gnus-tmp-indentation
4891              (aref gnus-thread-indent-array gnus-tmp-level)
4892              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4893              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4894                                 gnus-summary-default-score 0)
4895              gnus-tmp-score-char
4896              (if (or (null gnus-summary-default-score)
4897                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4898                          gnus-summary-zcore-fuzz))
4899                  ?\ ;;;Whitespace
4900                (if (< gnus-tmp-score gnus-summary-default-score)
4901                    gnus-score-below-mark gnus-score-over-mark))
4902              gnus-tmp-replied
4903              (cond ((memq number gnus-newsgroup-processable)
4904                     gnus-process-mark)
4905                    ((memq number gnus-newsgroup-cached)
4906                     gnus-cached-mark)
4907                    ((memq number gnus-newsgroup-replied)
4908                     gnus-replied-mark)
4909                    ((memq number gnus-newsgroup-forwarded)
4910                     gnus-forwarded-mark)
4911                    ((memq number gnus-newsgroup-saved)
4912                     gnus-saved-mark)
4913                    ((memq number gnus-newsgroup-recent)
4914                     gnus-recent-mark)
4915                    ((memq number gnus-newsgroup-unseen)
4916                     gnus-unseen-mark)
4917                    (t gnus-no-mark))
4918              gnus-tmp-downloaded
4919              (cond ((memq number gnus-newsgroup-undownloaded)
4920                     gnus-undownloaded-mark)
4921                    (gnus-newsgroup-agentized
4922                     gnus-downloaded-mark)
4923                    (t
4924                     gnus-no-mark))
4925              gnus-tmp-from (mail-header-from gnus-tmp-header)
4926              gnus-tmp-name
4927              (cond
4928               ((string-match "<[^>]+> *$" gnus-tmp-from)
4929                (setq beg-match (match-beginning 0))
4930                (or (and (string-match "^\".+\"" gnus-tmp-from)
4931                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4932                    (substring gnus-tmp-from 0 beg-match)))
4933               ((string-match "(.+)" gnus-tmp-from)
4934                (substring gnus-tmp-from
4935                           (1+ (match-beginning 0)) (1- (match-end 0))))
4936               (t gnus-tmp-from))
4937
4938              ;; Do the %B string
4939              gnus-tmp-thread-tree-header-string
4940              (cond
4941               ((not gnus-show-threads) "")
4942               ((zerop gnus-tmp-level)
4943                (cond ((cdar thread)
4944                       (or gnus-sum-thread-tree-root subject))
4945                      (gnus-tmp-new-adopts
4946                       (or gnus-sum-thread-tree-false-root subject))
4947                      (t
4948                       (or gnus-sum-thread-tree-single-indent subject))))
4949               (t
4950                (concat (apply 'concat
4951                               (mapcar (lambda (item)
4952                                         (if (= item 1)
4953                                             gnus-sum-thread-tree-vertical
4954                                           gnus-sum-thread-tree-indent))
4955                                       (cdr (reverse tree-stack))))
4956                        (if (nth 1 thread)
4957                            gnus-sum-thread-tree-leaf-with-other
4958                          gnus-sum-thread-tree-single-leaf)))))
4959             (when (string= gnus-tmp-name "")
4960               (setq gnus-tmp-name gnus-tmp-from))
4961             (unless (numberp gnus-tmp-lines)
4962               (setq gnus-tmp-lines -1))
4963             (if (= gnus-tmp-lines -1)
4964                 (setq gnus-tmp-lines "?")
4965               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4966               (gnus-put-text-property
4967              (point)
4968              (progn (eval gnus-summary-line-format-spec) (point))
4969                'gnus-number number)
4970             (when gnus-visual-p
4971               (forward-line -1)
4972               (gnus-run-hooks 'gnus-summary-update-hook)
4973               (forward-line 1))
4974
4975             (setq gnus-tmp-prev-subject simp-subject)))
4976
4977         (when (nth 1 thread)
4978           (push (list (max 0 gnus-tmp-level)
4979                       (copy-sequence tree-stack)
4980                       (nthcdr 1 thread))
4981                 stack))
4982         (push (if (nth 1 thread) 1 0) tree-stack)
4983         (incf gnus-tmp-level)
4984         (setq threads (if thread-end nil (cdar thread)))
4985         (if gnus-summary-display-while-building
4986             (if building-count
4987                 (progn
4988                   ;; use a set frequency
4989                   (setq building-line-count (1- building-line-count))
4990                   (when (= building-line-count 0)
4991                     (sit-for 0)
4992                     (setq building-line-count
4993                           gnus-summary-display-while-building)))
4994               ;; always
4995               (sit-for 0)))
4996         (unless threads
4997           (setq gnus-tmp-level 0)))))
4998   (gnus-message 7 "Generating summary...done"))
4999
5000 (defun gnus-summary-prepare-unthreaded (headers)
5001   "Generate an unthreaded summary buffer based on HEADERS."
5002   (let (header number mark)
5003
5004     (beginning-of-line)
5005
5006     (while headers
5007       ;; We may have to root out some bad articles...
5008       (when (memq (setq number (mail-header-number
5009                                 (setq header (pop headers))))
5010                   gnus-newsgroup-limit)
5011         ;; Mark article as read when it has a low score.
5012         (when (and gnus-summary-mark-below
5013                    (< (or (cdr (assq number gnus-newsgroup-scored))
5014                           gnus-summary-default-score 0)
5015                       gnus-summary-mark-below)
5016                    (not (gnus-summary-article-ancient-p number)))
5017           (setq gnus-newsgroup-unreads
5018                 (delq number gnus-newsgroup-unreads))
5019           (if gnus-newsgroup-auto-expire
5020               (push number gnus-newsgroup-expirable)
5021             (push (cons number gnus-low-score-mark)
5022                   gnus-newsgroup-reads)))
5023
5024         (setq mark (gnus-article-mark number))
5025         (push (gnus-data-make number mark (1+ (point)) header 0)
5026               gnus-newsgroup-data)
5027         (gnus-summary-insert-line
5028          header 0 number
5029          (memq number gnus-newsgroup-undownloaded)
5030          mark (memq number gnus-newsgroup-replied)
5031          (memq number gnus-newsgroup-expirable)
5032          (mail-header-subject header) nil
5033          (cdr (assq number gnus-newsgroup-scored))
5034          (memq number gnus-newsgroup-processable))))))
5035
5036 (defun gnus-summary-remove-list-identifiers ()
5037   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5038   (let ((regexp (if (consp gnus-list-identifiers)
5039                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5040                   gnus-list-identifiers))
5041         changed subject)
5042     (when regexp
5043       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5044       (dolist (header gnus-newsgroup-headers)
5045         (setq subject (mail-header-subject header)
5046               changed nil)
5047         (while (string-match regexp subject)
5048           (setq subject
5049                 (concat (substring subject 0 (match-beginning 1))
5050                         (substring subject (match-end 0)))
5051                 changed t))
5052         (when changed
5053           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5054             (setq subject
5055                   (concat (substring subject 0 (match-beginning 1))
5056                           (substring subject (match-end 1)))))
5057           (mail-header-set-subject header subject))))))
5058
5059 (defun gnus-fetch-headers (articles)
5060   "Fetch headers of ARTICLES."
5061   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5062     (gnus-message 5 "Fetching headers for %s..." name)
5063     (prog1
5064         (if (eq 'nov
5065                 (setq gnus-headers-retrieved-by
5066                       (gnus-retrieve-headers
5067                        articles gnus-newsgroup-name
5068                        ;; We might want to fetch old headers, but
5069                        ;; not if there is only 1 article.
5070                        (and (or (and
5071                                  (not (eq gnus-fetch-old-headers 'some))
5072                                  (not (numberp gnus-fetch-old-headers)))
5073                                 (> (length articles) 1))
5074                             gnus-fetch-old-headers))))
5075             (gnus-get-newsgroup-headers-xover
5076              articles nil nil gnus-newsgroup-name t)
5077           (gnus-get-newsgroup-headers))
5078       (gnus-message 5 "Fetching headers for %s...done" name))))
5079
5080 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5081   "Select newsgroup GROUP.
5082 If READ-ALL is non-nil, all articles in the group are selected.
5083 If SELECT-ARTICLES, only select those articles from GROUP."
5084   (let* ((entry (gnus-group-entry group))
5085          ;;!!! Dirty hack; should be removed.
5086          (gnus-summary-ignore-duplicates
5087           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5088               t
5089             gnus-summary-ignore-duplicates))
5090          (info (nth 2 entry))
5091          articles fetched-articles cached)
5092
5093     (unless (gnus-check-server
5094              (set (make-local-variable 'gnus-current-select-method)
5095                   (gnus-find-method-for-group group)))
5096       (error "Couldn't open server"))
5097
5098     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5099         (gnus-activate-group group)     ; Or we can activate it...
5100         (progn                          ; Or we bug out.
5101           (when (equal major-mode 'gnus-summary-mode)
5102             (gnus-kill-buffer (current-buffer)))
5103           (error "Couldn't activate group %s: %s"
5104                  group (gnus-status-message group))))
5105
5106     (unless (gnus-request-group group t)
5107       (when (equal major-mode 'gnus-summary-mode)
5108         (gnus-kill-buffer (current-buffer)))
5109       (error "Couldn't request group %s: %s"
5110              group (gnus-status-message group)))
5111
5112     (when gnus-agent
5113       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5114       
5115       (setq gnus-summary-use-undownloaded-faces
5116             (gnus-agent-find-parameter
5117              group
5118              'agent-enable-undownloaded-faces)))
5119
5120     (setq gnus-newsgroup-name group
5121           gnus-newsgroup-unselected nil
5122           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5123
5124     (let ((display (gnus-group-find-parameter group 'display)))
5125       (setq gnus-newsgroup-display
5126             (cond
5127              ((not (zerop (or (car-safe read-all) 0)))
5128               ;; The user entered the group with C-u SPC/RET, let's show
5129               ;; all articles.
5130               'gnus-not-ignore)
5131              ((eq display 'all)
5132               'gnus-not-ignore)
5133              ((arrayp display)
5134               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5135              ((numberp display)
5136               ;; The following is probably the "correct" solution, but
5137               ;; it makes Gnus fetch all headers and then limit the
5138               ;; articles (which is slow), so instead we hack the
5139               ;; select-articles parameter instead. -- Simon Josefsson
5140               ;; <jas@kth.se>
5141               ;;
5142               ;; (gnus-byte-compile
5143               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5144               ;;                         display)))))
5145               (setq select-articles
5146                     (gnus-uncompress-range
5147                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5148                              (if (> tmp 0)
5149                                  tmp
5150                                1))
5151                            (cdr (gnus-active group)))))
5152               nil)
5153              (t
5154               nil))))
5155
5156     (gnus-summary-setup-default-charset)
5157
5158     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5159     (when (gnus-virtual-group-p group)
5160       (setq cached gnus-newsgroup-cached))
5161
5162     (setq gnus-newsgroup-unreads
5163           (gnus-sorted-ndifference
5164            (gnus-sorted-ndifference gnus-newsgroup-unreads
5165                                     gnus-newsgroup-marked)
5166            gnus-newsgroup-dormant))
5167
5168     (setq gnus-newsgroup-processable nil)
5169
5170     (gnus-update-read-articles group gnus-newsgroup-unreads)
5171
5172     ;; Adjust and set lists of article marks.
5173     (when info
5174       (gnus-adjust-marked-articles info))
5175     (if (setq articles select-articles)
5176         (setq gnus-newsgroup-unselected
5177               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5178       (setq articles (gnus-articles-to-read group read-all)))
5179
5180     (cond
5181      ((null articles)
5182       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5183       'quit)
5184      ((eq articles 0) nil)
5185      (t
5186       ;; Init the dependencies hash table.
5187       (setq gnus-newsgroup-dependencies
5188             (gnus-make-hashtable (length articles)))
5189       (gnus-set-global-variables)
5190       ;; Retrieve the headers and read them in.
5191
5192       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5193
5194       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5195       (when cached
5196         (setq gnus-newsgroup-cached cached))
5197
5198       ;; Suppress duplicates?
5199       (when gnus-suppress-duplicates
5200         (gnus-dup-suppress-articles))
5201
5202       ;; Set the initial limit.
5203       (setq gnus-newsgroup-limit (copy-sequence articles))
5204       ;; Remove canceled articles from the list of unread articles.
5205       (setq fetched-articles
5206             (mapcar (lambda (headers) (mail-header-number headers))
5207                     gnus-newsgroup-headers))
5208       (setq gnus-newsgroup-articles fetched-articles)
5209       (setq gnus-newsgroup-unreads
5210             (gnus-sorted-nintersection
5211              gnus-newsgroup-unreads fetched-articles))
5212       (gnus-compute-unseen-list)
5213
5214       ;; Removed marked articles that do not exist.
5215       (gnus-update-missing-marks
5216        (gnus-sorted-difference articles fetched-articles))
5217       ;; We might want to build some more threads first.
5218       (when (and gnus-fetch-old-headers
5219                  (eq gnus-headers-retrieved-by 'nov))
5220         (if (eq gnus-fetch-old-headers 'invisible)
5221             (gnus-build-all-threads)
5222           (gnus-build-old-threads)))
5223       ;; Let the Gnus agent mark articles as read.
5224       (when gnus-agent
5225         (gnus-agent-get-undownloaded-list))
5226       ;; Remove list identifiers from subject
5227       (when gnus-list-identifiers
5228         (gnus-summary-remove-list-identifiers))
5229       ;; Check whether auto-expire is to be done in this group.
5230       (setq gnus-newsgroup-auto-expire
5231             (gnus-group-auto-expirable-p group))
5232       ;; Set up the article buffer now, if necessary.
5233       (unless gnus-single-article-buffer
5234         (gnus-article-setup-buffer))
5235       ;; First and last article in this newsgroup.
5236       (when gnus-newsgroup-headers
5237         (setq gnus-newsgroup-begin
5238               (mail-header-number (car gnus-newsgroup-headers))
5239               gnus-newsgroup-end
5240               (mail-header-number
5241                (gnus-last-element gnus-newsgroup-headers))))
5242       ;; GROUP is successfully selected.
5243       (or gnus-newsgroup-headers t)))))
5244
5245 (defun gnus-compute-unseen-list ()
5246   ;; The `seen' marks are treated specially.
5247   (if (not gnus-newsgroup-seen)
5248       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5249     (setq gnus-newsgroup-unseen
5250           (gnus-inverse-list-range-intersection
5251            gnus-newsgroup-articles gnus-newsgroup-seen))))
5252
5253 (defun gnus-summary-display-make-predicate (display)
5254   (require 'gnus-agent)
5255   (when (= (length display) 1)
5256     (setq display (car display)))
5257   (unless gnus-summary-display-cache
5258     (dolist (elem (append '((unread . unread)
5259                             (read . read)
5260                             (unseen . unseen))
5261                           gnus-article-mark-lists))
5262       (push (cons (cdr elem)
5263                   (gnus-byte-compile
5264                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5265             gnus-summary-display-cache)))
5266   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5267         (gnus-category-predicate-cache gnus-summary-display-cache))
5268     (gnus-get-predicate display)))
5269
5270 ;; Uses the dynamically bound `number' variable.
5271 (eval-when-compile
5272   (defvar number))
5273 (defun gnus-article-marked-p (type &optional article)
5274   (let ((article (or article number)))
5275     (cond
5276      ((eq type 'tick)
5277       (memq article gnus-newsgroup-marked))
5278      ((eq type 'spam)
5279       (memq article gnus-newsgroup-spam-marked))
5280      ((eq type 'unsend)
5281       (memq article gnus-newsgroup-unsendable))
5282      ((eq type 'undownload)
5283       (memq article gnus-newsgroup-undownloaded))
5284      ((eq type 'download)
5285       (memq article gnus-newsgroup-downloadable))
5286      ((eq type 'unread)
5287       (memq article gnus-newsgroup-unreads))
5288      ((eq type 'read)
5289       (memq article gnus-newsgroup-reads))
5290      ((eq type 'dormant)
5291       (memq article gnus-newsgroup-dormant) )
5292      ((eq type 'expire)
5293       (memq article gnus-newsgroup-expirable))
5294      ((eq type 'reply)
5295       (memq article gnus-newsgroup-replied))
5296      ((eq type 'killed)
5297       (memq article gnus-newsgroup-killed))
5298      ((eq type 'bookmark)
5299       (assq article gnus-newsgroup-bookmarks))
5300      ((eq type 'score)
5301       (assq article gnus-newsgroup-scored))
5302      ((eq type 'save)
5303       (memq article gnus-newsgroup-saved))
5304      ((eq type 'cache)
5305       (memq article gnus-newsgroup-cached))
5306      ((eq type 'forward)
5307       (memq article gnus-newsgroup-forwarded))
5308      ((eq type 'seen)
5309       (not (memq article gnus-newsgroup-unseen)))
5310      ((eq type 'recent)
5311       (memq article gnus-newsgroup-recent))
5312      (t t))))
5313
5314 (defun gnus-articles-to-read (group &optional read-all)
5315   "Find out what articles the user wants to read."
5316   (let* ((articles
5317           ;; Select all articles if `read-all' is non-nil, or if there
5318           ;; are no unread articles.
5319           (if (or read-all
5320                   (and (zerop (length gnus-newsgroup-marked))
5321                        (zerop (length gnus-newsgroup-unreads)))
5322                   ;; Fetch all if the predicate is non-nil.
5323                   gnus-newsgroup-display)
5324               ;; We want to select the headers for all the articles in
5325               ;; the group, so we select either all the active
5326               ;; articles in the group, or (if that's nil), the
5327               ;; articles in the cache.
5328               (or
5329                (gnus-uncompress-range (gnus-active group))
5330                (gnus-cache-articles-in-group group))
5331             ;; Select only the "normal" subset of articles.
5332             (gnus-sorted-nunion
5333              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5334              gnus-newsgroup-unreads)))
5335          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5336          (scored (length scored-list))
5337          (number (length articles))
5338          (marked (+ (length gnus-newsgroup-marked)
5339                     (length gnus-newsgroup-dormant)))
5340          (select
5341           (cond
5342            ((numberp read-all)
5343             read-all)
5344            ((numberp gnus-newsgroup-display)
5345             gnus-newsgroup-display)
5346            (t
5347             (condition-case ()
5348                 (cond
5349                  ((and (or (<= scored marked) (= scored number))
5350                        (numberp gnus-large-newsgroup)
5351                        (> number gnus-large-newsgroup))
5352                   (let* ((cursor-in-echo-area nil)
5353                          (initial (gnus-parameter-large-newsgroup-initial
5354                                    gnus-newsgroup-name))
5355                          (input
5356                           (read-string
5357                            (format
5358                             "How many articles from %s (%s %d): "
5359                             (gnus-limit-string
5360                              (gnus-group-decoded-name gnus-newsgroup-name)
5361                              35)
5362                             (if initial "max" "default")
5363                             number)
5364                            (if initial
5365                                (cons (number-to-string initial)
5366                                      0)))))
5367                     (if (string-match "^[ \t]*$" input) number input)))
5368                  ((and (> scored marked) (< scored number)
5369                        (> (- scored number) 20))
5370                   (let ((input
5371                          (read-string
5372                           (format "%s %s (%d scored, %d total): "
5373                                   "How many articles from"
5374                                   (gnus-group-decoded-name group)
5375                                   scored number))))
5376                     (if (string-match "^[ \t]*$" input)
5377                         number input)))
5378                  (t number))
5379               (quit
5380                (message "Quit getting the articles to read")
5381                nil))))))
5382     (setq select (if (stringp select) (string-to-number select) select))
5383     (if (or (null select) (zerop select))
5384         select
5385       (if (and (not (zerop scored)) (<= (abs select) scored))
5386           (progn
5387             (setq articles (sort scored-list '<))
5388             (setq number (length articles)))
5389         (setq articles (copy-sequence articles)))
5390
5391       (when (< (abs select) number)
5392         (if (< select 0)
5393             ;; Select the N oldest articles.
5394             (setcdr (nthcdr (1- (abs select)) articles) nil)
5395           ;; Select the N most recent articles.
5396           (setq articles (nthcdr (- number select) articles))))
5397       (setq gnus-newsgroup-unselected
5398             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5399       (when gnus-alter-articles-to-read-function
5400         (setq articles
5401               (sort
5402                (funcall gnus-alter-articles-to-read-function
5403                         gnus-newsgroup-name articles)
5404                '<)))
5405       articles)))
5406
5407 (defun gnus-killed-articles (killed articles)
5408   (let (out)
5409     (while articles
5410       (when (inline (gnus-member-of-range (car articles) killed))
5411         (push (car articles) out))
5412       (setq articles (cdr articles)))
5413     out))
5414
5415 (defun gnus-uncompress-marks (marks)
5416   "Uncompress the mark ranges in MARKS."
5417   (let ((uncompressed '(score bookmark))
5418         out)
5419     (while marks
5420       (if (memq (caar marks) uncompressed)
5421           (push (car marks) out)
5422         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5423       (setq marks (cdr marks)))
5424     out))
5425
5426 (defun gnus-article-mark-to-type (mark)
5427   "Return the type of MARK."
5428   (or (cadr (assq mark gnus-article-special-mark-lists))
5429       'list))
5430
5431 (defun gnus-article-unpropagatable-p (mark)
5432   "Return whether MARK should be propagated to back end."
5433   (memq mark gnus-article-unpropagated-mark-lists))
5434
5435 (defun gnus-adjust-marked-articles (info)
5436   "Set all article lists and remove all marks that are no longer valid."
5437   (let* ((marked-lists (gnus-info-marks info))
5438          (active (gnus-active (gnus-info-group info)))
5439          (min (car active))
5440          (max (cdr active))
5441          (types gnus-article-mark-lists)
5442          marks var articles article mark mark-type
5443          bgn end)
5444
5445     (dolist (marks marked-lists)
5446       (setq mark (car marks)
5447             mark-type (gnus-article-mark-to-type mark)
5448             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5449
5450       ;; We set the variable according to the type of the marks list,
5451       ;; and then adjust the marks to a subset of the active articles.
5452       (cond
5453        ;; Adjust "simple" lists - compressed yet unsorted
5454        ((eq mark-type 'list)
5455         ;; Simultaneously uncompress and clip to active range
5456         ;; See gnus-uncompress-range for a description of possible marks
5457         (let (l lh)
5458           (if (not (cadr marks))
5459               (set var nil)
5460             (setq articles (if (numberp (cddr marks))
5461                                (list (cdr marks))
5462                              (cdr marks))
5463                   lh (cons nil nil)
5464                   l lh)
5465
5466             (while (setq article (pop articles))
5467               (cond ((consp article)
5468                      (setq bgn (max (car article) min)
5469                            end (min (cdr article) max))
5470                      (while (<= bgn end)
5471                        (setq l (setcdr l (cons bgn nil))
5472                              bgn (1+ bgn))))
5473                     ((and (<= min article)
5474                           (>= max article))
5475                      (setq l (setcdr l (cons article nil))))))
5476             (set var (cdr lh)))))
5477        ;; Adjust assocs.
5478        ((eq mark-type 'tuple)
5479         (set var (setq articles (cdr marks)))
5480         (when (not (listp (cdr (symbol-value var))))
5481           (set var (list (symbol-value var))))
5482         (when (not (listp (cdr articles)))
5483           (setq articles (list articles)))
5484         (while articles
5485           (when (or (not (consp (setq article (pop articles))))
5486                     (< (car article) min)
5487                     (> (car article) max))
5488             (set var (delq article (symbol-value var))))))
5489        ;; Adjust ranges (sloppily).
5490        ((eq mark-type 'range)
5491         (cond
5492          ((eq mark 'seen)
5493           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5494           ;; It should be (seen (NUM1 . NUM2)).
5495           (when (numberp (cddr marks))
5496             (setcdr marks (list (cdr marks))))
5497           (setq articles (cdr marks))
5498           (while (and articles
5499                       (or (and (consp (car articles))
5500                                (> min (cdar articles)))
5501                           (and (numberp (car articles))
5502                                (> min (car articles)))))
5503             (pop articles))
5504           (set var articles))))))))
5505
5506 (defun gnus-update-missing-marks (missing)
5507   "Go through the list of MISSING articles and remove them from the mark lists."
5508   (when missing
5509     (let (var m)
5510       ;; Go through all types.
5511       (dolist (elem gnus-article-mark-lists)
5512         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5513           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5514           (when (symbol-value var)
5515             ;; This list has articles.  So we delete all missing
5516             ;; articles from it.
5517             (setq m missing)
5518             (while m
5519               (set var (delq (pop m) (symbol-value var))))))))))
5520
5521 (defun gnus-update-marks ()
5522   "Enter the various lists of marked articles into the newsgroup info list."
5523   (let ((types gnus-article-mark-lists)
5524         (info (gnus-get-info gnus-newsgroup-name))
5525         type list newmarked symbol delta-marks)
5526     (when info
5527       ;; Add all marks lists to the list of marks lists.
5528       (while (setq type (pop types))
5529         (setq list (symbol-value
5530                     (setq symbol
5531                           (intern (format "gnus-newsgroup-%s" (car type))))))
5532
5533         (when list
5534           ;; Get rid of the entries of the articles that have the
5535           ;; default score.
5536           (when (and (eq (cdr type) 'score)
5537                      gnus-save-score
5538                      list)
5539             (let* ((arts list)
5540                    (prev (cons nil list))
5541                    (all prev))
5542               (while arts
5543                 (if (or (not (consp (car arts)))
5544                         (= (cdar arts) gnus-summary-default-score))
5545                     (setcdr prev (cdr arts))
5546                   (setq prev arts))
5547                 (setq arts (cdr arts)))
5548               (setq list (cdr all)))))
5549
5550         (when (eq (cdr type) 'seen)
5551           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5552
5553         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5554           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5555
5556         (when (and (gnus-check-backend-function
5557                     'request-set-mark gnus-newsgroup-name)
5558                    (not (gnus-article-unpropagatable-p (cdr type))))
5559           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5560                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5561                  (add (gnus-remove-from-range
5562                        (gnus-copy-sequence list) old)))
5563             (when add
5564               (push (list add 'add (list (cdr type))) delta-marks))
5565             (when del
5566               (push (list del 'del (list (cdr type))) delta-marks))))
5567
5568         (when list
5569           (push (cons (cdr type) list) newmarked)))
5570
5571       (when delta-marks
5572         (unless (gnus-check-group gnus-newsgroup-name)
5573           (error "Can't open server for %s" gnus-newsgroup-name))
5574         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5575
5576       ;; Enter these new marks into the info of the group.
5577       (if (nthcdr 3 info)
5578           (setcar (nthcdr 3 info) newmarked)
5579         ;; Add the marks lists to the end of the info.
5580         (when newmarked
5581           (setcdr (nthcdr 2 info) (list newmarked))))
5582
5583       ;; Cut off the end of the info if there's nothing else there.
5584       (let ((i 5))
5585         (while (and (> i 2)
5586                     (not (nth i info)))
5587           (when (nthcdr (decf i) info)
5588             (setcdr (nthcdr i info) nil)))))))
5589
5590 (defun gnus-set-mode-line (where)
5591   "Set the mode line of the article or summary buffers.
5592 If WHERE is `summary', the summary mode line format will be used."
5593   ;; Is this mode line one we keep updated?
5594   (when (and (memq where gnus-updated-mode-lines)
5595              (symbol-value
5596               (intern (format "gnus-%s-mode-line-format-spec" where))))
5597     (let (mode-string)
5598       (save-excursion
5599         ;; We evaluate this in the summary buffer since these
5600         ;; variables are buffer-local to that buffer.
5601         (set-buffer gnus-summary-buffer)
5602         ;; We bind all these variables that are used in the `eval' form
5603         ;; below.
5604         (let* ((mformat (symbol-value
5605                          (intern
5606                           (format "gnus-%s-mode-line-format-spec" where))))
5607                (gnus-tmp-group-name (gnus-group-decoded-name
5608                                      gnus-newsgroup-name))
5609                (gnus-tmp-article-number (or gnus-current-article 0))
5610                (gnus-tmp-unread gnus-newsgroup-unreads)
5611                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5612                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5613                (gnus-tmp-unread-and-unselected
5614                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5615                             (zerop gnus-tmp-unselected))
5616                        "")
5617                       ((zerop gnus-tmp-unselected)
5618                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5619                       (t (format "{%d(+%d) more}"
5620                                  gnus-tmp-unread-and-unticked
5621                                  gnus-tmp-unselected))))
5622                (gnus-tmp-subject
5623                 (if (and gnus-current-headers
5624                          (vectorp gnus-current-headers))
5625                     (gnus-mode-string-quote
5626                      (mail-header-subject gnus-current-headers))
5627                   ""))
5628                bufname-length max-len
5629                gnus-tmp-header);; passed as argument to any user-format-funcs
5630           (setq mode-string (eval mformat))
5631           (setq bufname-length (if (string-match "%b" mode-string)
5632                                    (- (length
5633                                        (buffer-name
5634                                         (if (eq where 'summary)
5635                                             nil
5636                                           (get-buffer gnus-article-buffer))))
5637                                       2)
5638                                  0))
5639           (setq max-len (max 4 (if gnus-mode-non-string-length
5640                                    (- (window-width)
5641                                       gnus-mode-non-string-length
5642                                       bufname-length)
5643                                  (length mode-string))))
5644           ;; We might have to chop a bit of the string off...
5645           (when (> (length mode-string) max-len)
5646             (setq mode-string
5647                   (concat (gnus-truncate-string mode-string (- max-len 3))
5648                           "...")))
5649           ;; Pad the mode string a bit.
5650           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5651       ;; Update the mode line.
5652       (setq mode-line-buffer-identification
5653             (gnus-mode-line-buffer-identification (list mode-string)))
5654       (set-buffer-modified-p t))))
5655
5656 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5657   "Go through the HEADERS list and add all Xrefs to a hash table.
5658 The resulting hash table is returned, or nil if no Xrefs were found."
5659   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5660          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5661          (xref-hashtb (gnus-make-hashtable))
5662          start group entry number xrefs header)
5663     (while headers
5664       (setq header (pop headers))
5665       (when (and (setq xrefs (mail-header-xref header))
5666                  (not (memq (setq number (mail-header-number header))
5667                             unreads)))
5668         (setq start 0)
5669         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5670           (setq start (match-end 0))
5671           (setq group (if prefix
5672                           (concat prefix (substring xrefs (match-beginning 1)
5673                                                     (match-end 1)))
5674                         (substring xrefs (match-beginning 1) (match-end 1))))
5675           (setq number
5676                 (string-to-int (substring xrefs (match-beginning 2)
5677                                           (match-end 2))))
5678           (if (setq entry (gnus-gethash group xref-hashtb))
5679               (setcdr entry (cons number (cdr entry)))
5680             (gnus-sethash group (cons number nil) xref-hashtb)))))
5681     (and start xref-hashtb)))
5682
5683 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5684   "Look through all the headers and mark the Xrefs as read."
5685   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5686         name info xref-hashtb idlist method nth4)
5687     (save-excursion
5688       (set-buffer gnus-group-buffer)
5689       (when (setq xref-hashtb
5690                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5691         (mapatoms
5692          (lambda (group)
5693            (unless (string= from-newsgroup (setq name (symbol-name group)))
5694              (setq idlist (symbol-value group))
5695              ;; Dead groups are not updated.
5696              (and (prog1
5697                       (setq info (gnus-get-info name))
5698                     (when (stringp (setq nth4 (gnus-info-method info)))
5699                       (setq nth4 (gnus-server-to-method nth4))))
5700                   ;; Only do the xrefs if the group has the same
5701                   ;; select method as the group we have just read.
5702                   (or (gnus-methods-equal-p
5703                        nth4 (gnus-find-method-for-group from-newsgroup))
5704                       virtual
5705                       (equal nth4 (setq method (gnus-find-method-for-group
5706                                                 from-newsgroup)))
5707                       (and (equal (car nth4) (car method))
5708                            (equal (nth 1 nth4) (nth 1 method))))
5709                   gnus-use-cross-reference
5710                   (or (not (eq gnus-use-cross-reference t))
5711                       virtual
5712                       ;; Only do cross-references on subscribed
5713                       ;; groups, if that is what is wanted.
5714                       (<= (gnus-info-level info) gnus-level-subscribed))
5715                   (gnus-group-make-articles-read name idlist))))
5716          xref-hashtb)))))
5717
5718 (defun gnus-compute-read-articles (group articles)
5719   (let* ((entry (gnus-group-entry group))
5720          (info (nth 2 entry))
5721          (active (gnus-active group))
5722          ninfo)
5723     (when entry
5724       ;; First peel off all invalid article numbers.
5725       (when active
5726         (let ((ids articles)
5727               id first)
5728           (while (setq id (pop ids))
5729             (when (and first (> id (cdr active)))
5730               ;; We'll end up in this situation in one particular
5731               ;; obscure situation.  If you re-scan a group and get
5732               ;; a new article that is cross-posted to a different
5733               ;; group that has not been re-scanned, you might get
5734               ;; crossposted article that has a higher number than
5735               ;; Gnus believes possible.  So we re-activate this
5736               ;; group as well.  This might mean doing the
5737               ;; crossposting thingy will *increase* the number
5738               ;; of articles in some groups.  Tsk, tsk.
5739               (setq active (or (gnus-activate-group group) active)))
5740             (when (or (> id (cdr active))
5741                       (< id (car active)))
5742               (setq articles (delq id articles))))))
5743       ;; If the read list is nil, we init it.
5744       (if (and active
5745                (null (gnus-info-read info))
5746                (> (car active) 1))
5747           (setq ninfo (cons 1 (1- (car active))))
5748         (setq ninfo (gnus-info-read info)))
5749       ;; Then we add the read articles to the range.
5750       (gnus-add-to-range
5751        ninfo (setq articles (sort articles '<))))))
5752
5753 (defun gnus-group-make-articles-read (group articles)
5754   "Update the info of GROUP to say that ARTICLES are read."
5755   (let* ((num 0)
5756          (entry (gnus-group-entry group))
5757          (info (nth 2 entry))
5758          (active (gnus-active group))
5759          range)
5760     (when entry
5761       (setq range (gnus-compute-read-articles group articles))
5762       (with-current-buffer gnus-group-buffer
5763         (gnus-undo-register
5764           `(progn
5765              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5766              (gnus-info-set-read ',info ',(gnus-info-read info))
5767              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5768              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5769              (gnus-group-update-group ,group t))))
5770       ;; Add the read articles to the range.
5771       (gnus-info-set-read info range)
5772       (gnus-request-set-mark group (list (list range 'add '(read))))
5773       ;; Then we have to re-compute how many unread
5774       ;; articles there are in this group.
5775       (when active
5776         (cond
5777          ((not range)
5778           (setq num (- (1+ (cdr active)) (car active))))
5779          ((not (listp (cdr range)))
5780           (setq num (- (cdr active) (- (1+ (cdr range))
5781                                        (car range)))))
5782          (t
5783           (while range
5784             (if (numberp (car range))
5785                 (setq num (1+ num))
5786               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5787             (setq range (cdr range)))
5788           (setq num (- (cdr active) num))))
5789         ;; Update the number of unread articles.
5790         (setcar entry num)
5791         ;; Update the group buffer.
5792         (unless (gnus-ephemeral-group-p group)
5793           (gnus-group-update-group group t))))))
5794
5795 (defvar gnus-newsgroup-none-id 0)
5796
5797 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5798   (let ((cur nntp-server-buffer)
5799         (dependencies
5800          (or dependencies
5801              (with-current-buffer gnus-summary-buffer
5802                gnus-newsgroup-dependencies)))
5803         headers id end ref number
5804         (mail-parse-charset gnus-newsgroup-charset)
5805         (mail-parse-ignored-charsets
5806          (save-excursion (condition-case nil
5807                              (set-buffer gnus-summary-buffer)
5808                            (error))
5809                          gnus-newsgroup-ignored-charsets)))
5810     (save-excursion
5811       (set-buffer nntp-server-buffer)
5812       ;; Translate all TAB characters into SPACE characters.
5813       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5814       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5815       (ietf-drums-unfold-fws)
5816       (gnus-run-hooks 'gnus-parse-headers-hook)
5817       (let ((case-fold-search t)
5818             in-reply-to header p lines chars ctype)
5819         (goto-char (point-min))
5820         ;; Search to the beginning of the next header.  Error messages
5821         ;; do not begin with 2 or 3.
5822         (while (re-search-forward "^[23][0-9]+ " nil t)
5823           (setq id nil
5824                 ref nil)
5825           ;; This implementation of this function, with nine
5826           ;; search-forwards instead of the one re-search-forward and
5827           ;; a case (which basically was the old function) is actually
5828           ;; about twice as fast, even though it looks messier.  You
5829           ;; can't have everything, I guess.  Speed and elegance
5830           ;; doesn't always go hand in hand.
5831           (setq
5832            header
5833            (make-full-mail-header
5834             ;; Number.
5835             (prog1
5836                 (setq number (read cur))
5837               (end-of-line)
5838               (setq p (point))
5839               (narrow-to-region (point)
5840                                 (or (and (search-forward "\n.\n" nil t)
5841                                          (- (point) 2))
5842                                     (point))))
5843             ;; Subject.
5844             (progn
5845               (goto-char p)
5846               (if (search-forward "\nsubject:" nil t)
5847                   (nnheader-header-value)
5848                 "(none)"))
5849             ;; From.
5850             (progn
5851               (goto-char p)
5852               (if (search-forward "\nfrom:" nil t)
5853                   (nnheader-header-value)
5854                 "(nobody)"))
5855             ;; Date.
5856             (progn
5857               (goto-char p)
5858               (if (search-forward "\ndate:" nil t)
5859                   (nnheader-header-value) ""))
5860             ;; Message-ID.
5861             (progn
5862               (goto-char p)
5863               (setq id (if (re-search-forward
5864                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5865                            ;; We do it this way to make sure the Message-ID
5866                            ;; is (somewhat) syntactically valid.
5867                            (buffer-substring (match-beginning 1)
5868                                              (match-end 1))
5869                          ;; If there was no message-id, we just fake one
5870                          ;; to make subsequent routines simpler.
5871                          (nnheader-generate-fake-message-id number))))
5872             ;; References.
5873             (progn
5874               (goto-char p)
5875               (if (search-forward "\nreferences:" nil t)
5876                   (progn
5877                     (setq end (point))
5878                     (prog1
5879                         (nnheader-header-value)
5880                       (setq ref
5881                             (buffer-substring
5882                              (progn
5883                                ;; (end-of-line)
5884                                (search-backward ">" end t)
5885                                (1+ (point)))
5886                              (progn
5887                                (search-backward "<" end t)
5888                                (point))))))
5889                 ;; Get the references from the in-reply-to header if there
5890                 ;; were no references and the in-reply-to header looks
5891                 ;; promising.
5892                 (if (and (search-forward "\nin-reply-to:" nil t)
5893                          (setq in-reply-to (nnheader-header-value))
5894                          (string-match "<[^>]+>" in-reply-to))
5895                     (let (ref2)
5896                       (setq ref (substring in-reply-to (match-beginning 0)
5897                                            (match-end 0)))
5898                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5899                         (setq ref2 (substring in-reply-to (match-beginning 0)
5900                                               (match-end 0)))
5901                         (when (> (length ref2) (length ref))
5902                           (setq ref ref2)))
5903                       ref)
5904                   (setq ref nil))))
5905             ;; Chars.
5906             (progn
5907               (goto-char p)
5908               (if (search-forward "\nchars: " nil t)
5909                   (if (numberp (setq chars (ignore-errors (read cur))))
5910                       chars -1)
5911                 -1))
5912             ;; Lines.
5913             (progn
5914               (goto-char p)
5915               (if (search-forward "\nlines: " nil t)
5916                   (if (numberp (setq lines (ignore-errors (read cur))))
5917                       lines -1)
5918                 -1))
5919             ;; Xref.
5920             (progn
5921               (goto-char p)
5922               (and (search-forward "\nxref:" nil t)
5923                    (nnheader-header-value)))
5924             ;; Extra.
5925             (when gnus-extra-headers
5926               (let ((extra gnus-extra-headers)
5927                     out)
5928                 (while extra
5929                   (goto-char p)
5930                   (when (search-forward
5931                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5932                     (push (cons (car extra) (nnheader-header-value)) out))
5933                   (pop extra))
5934                 out))))
5935           (goto-char p)
5936           (if (and (search-forward "\ncontent-type: " nil t)
5937                    (setq ctype (nnheader-header-value)))
5938               (mime-entity-set-content-type-internal
5939                header (mime-parse-Content-Type ctype)))
5940           (when (equal id ref)
5941             (setq ref nil))
5942
5943           (when gnus-alter-header-function
5944             (funcall gnus-alter-header-function header)
5945             (setq id (mail-header-id header)
5946                   ref (gnus-parent-id (mail-header-references header))))
5947
5948           (when (setq header
5949                       (gnus-dependencies-add-header
5950                        header dependencies force-new))
5951             (push header headers))
5952           (goto-char (point-max))
5953           (widen))
5954         (nreverse headers)))))
5955
5956 ;; Goes through the xover lines and returns a list of vectors
5957 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5958                                                   force-new dependencies
5959                                                   group also-fetch-heads)
5960   "Parse the news overview data in the server buffer.
5961 Return a list of headers that match SEQUENCE (see
5962 `nntp-retrieve-headers')."
5963   ;; Get the Xref when the users reads the articles since most/some
5964   ;; NNTP servers do not include Xrefs when using XOVER.
5965   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5966   (let ((mail-parse-charset gnus-newsgroup-charset)
5967         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5968         (cur nntp-server-buffer)
5969         (dependencies (or dependencies gnus-newsgroup-dependencies))
5970         (allp (cond
5971                ((eq gnus-read-all-available-headers t)
5972                 t)
5973                ((stringp gnus-read-all-available-headers)
5974                 (string-match gnus-read-all-available-headers group))
5975                (t
5976                 nil)))
5977         number headers header)
5978     (save-excursion
5979       (set-buffer nntp-server-buffer)
5980       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5981       ;; Allow the user to mangle the headers before parsing them.
5982       (gnus-run-hooks 'gnus-parse-headers-hook)
5983       (goto-char (point-min))
5984       (gnus-parse-without-error
5985         (while (and (or sequence allp)
5986                     (not (eobp)))
5987           (setq number (read cur))
5988           (when (not allp)
5989             (while (and sequence
5990                         (< (car sequence) number))
5991               (setq sequence (cdr sequence))))
5992           (when (and (or allp
5993                          (and sequence
5994                               (eq number (car sequence))))
5995                      (progn
5996                        (setq sequence (cdr sequence))
5997                        (setq header (inline
5998                                       (gnus-nov-parse-line
5999                                        number dependencies force-new)))))
6000             (push header headers))
6001           (forward-line 1)))
6002       ;; A common bug in inn is that if you have posted an article and
6003       ;; then retrieves the active file, it will answer correctly --
6004       ;; the new article is included.  However, a NOV entry for the
6005       ;; article may not have been generated yet, so this may fail.
6006       ;; We work around this problem by retrieving the last few
6007       ;; headers using HEAD.
6008       (if (or (not also-fetch-heads)
6009               (not sequence))
6010           ;; We (probably) got all the headers.
6011           (nreverse headers)
6012         (let ((gnus-nov-is-evil t))
6013           (nconc
6014            (nreverse headers)
6015            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6016              (gnus-get-newsgroup-headers))))))))
6017
6018 (defun gnus-article-get-xrefs ()
6019   "Fill in the Xref value in `gnus-current-headers', if necessary.
6020 This is meant to be called in `gnus-article-internal-prepare-hook'."
6021   (let ((headers (with-current-buffer gnus-summary-buffer
6022                    gnus-current-headers)))
6023     (or (not gnus-use-cross-reference)
6024         (not headers)
6025         (and (mail-header-xref headers)
6026              (not (string= (mail-header-xref headers) "")))
6027         (let ((case-fold-search t)
6028               xref)
6029           (save-restriction
6030             (nnheader-narrow-to-headers)
6031             (goto-char (point-min))
6032             (when (or (and (not (eobp))
6033                            (eq (downcase (char-after)) ?x)
6034                            (looking-at "Xref:"))
6035                       (search-forward "\nXref:" nil t))
6036               (goto-char (1+ (match-end 0)))
6037               (setq xref (buffer-substring (point) (point-at-eol)))
6038               (mail-header-set-xref headers xref)))))))
6039
6040 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6041   "Find article ID and insert the summary line for that article.
6042 OLD-HEADER can either be a header or a line number to insert
6043 the subject line on."
6044   (let* ((line (and (numberp old-header) old-header))
6045          (old-header (and (vectorp old-header) old-header))
6046          (header (cond ((and old-header use-old-header)
6047                         old-header)
6048                        ((and (numberp id)
6049                              (gnus-number-to-header id))
6050                         (gnus-number-to-header id))
6051                        (t
6052                         (gnus-read-header id))))
6053          (number (and (numberp id) id))
6054          d)
6055     (when header
6056       ;; Rebuild the thread that this article is part of and go to the
6057       ;; article we have fetched.
6058       (when (and (not gnus-show-threads)
6059                  old-header)
6060         (when (and number
6061                    (setq d (gnus-data-find (mail-header-number old-header))))
6062           (goto-char (gnus-data-pos d))
6063           (gnus-data-remove
6064            number
6065            (- (point-at-bol)
6066               (prog1
6067                   (1+ (point-at-eol))
6068                 (gnus-delete-line))))))
6069       ;; Remove list identifiers from subject.
6070       (when gnus-list-identifiers
6071         (let ((gnus-newsgroup-headers (list header)))
6072           (gnus-summary-remove-list-identifiers)
6073           (setq header (car gnus-newsgroup-headers))))
6074       (when old-header
6075         (mail-header-set-number header (mail-header-number old-header)))
6076       (setq gnus-newsgroup-sparse
6077             (delq (setq number (mail-header-number header))
6078                   gnus-newsgroup-sparse))
6079       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6080       (push number gnus-newsgroup-limit)
6081       (gnus-rebuild-thread (mail-header-id header) line)
6082       (gnus-summary-goto-subject number nil t))
6083     (when (and (numberp number)
6084                (> number 0))
6085       ;; We have to update the boundaries even if we can't fetch the
6086       ;; article if ID is a number -- so that the next `P' or `N'
6087       ;; command will fetch the previous (or next) article even
6088       ;; if the one we tried to fetch this time has been canceled.
6089       (when (> number gnus-newsgroup-end)
6090         (setq gnus-newsgroup-end number))
6091       (when (< number gnus-newsgroup-begin)
6092         (setq gnus-newsgroup-begin number))
6093       (setq gnus-newsgroup-unselected
6094             (delq number gnus-newsgroup-unselected)))
6095     ;; Report back a success?
6096     (and header (mail-header-number header))))
6097
6098 ;;; Process/prefix in the summary buffer
6099
6100 (defun gnus-summary-work-articles (n)
6101   "Return a list of articles to be worked upon.
6102 The prefix argument, the list of process marked articles, and the
6103 current article will be taken into consideration."
6104   (save-excursion
6105     (set-buffer gnus-summary-buffer)
6106     (cond
6107      (n
6108       ;; A numerical prefix has been given.
6109       (setq n (prefix-numeric-value n))
6110       (let ((backward (< n 0))
6111             (n (abs (prefix-numeric-value n)))
6112             articles article)
6113         (save-excursion
6114           (while
6115               (and (> n 0)
6116                    (push (setq article (gnus-summary-article-number))
6117                          articles)
6118                    (if backward
6119                        (gnus-summary-find-prev nil article)
6120                      (gnus-summary-find-next nil article)))
6121             (decf n)))
6122         (nreverse articles)))
6123      ((and (gnus-region-active-p) (mark))
6124       (message "region active")
6125       ;; Work on the region between point and mark.
6126       (let ((max (max (point) (mark)))
6127             articles article)
6128         (save-excursion
6129           (goto-char (min (point) (mark)))
6130           (while
6131               (and
6132                (push (setq article (gnus-summary-article-number)) articles)
6133                (gnus-summary-find-next nil article)
6134                (< (point) max)))
6135           (nreverse articles))))
6136      (gnus-newsgroup-processable
6137       ;; There are process-marked articles present.
6138       ;; Save current state.
6139       (gnus-summary-save-process-mark)
6140       ;; Return the list.
6141       (reverse gnus-newsgroup-processable))
6142      (t
6143       ;; Just return the current article.
6144       (list (gnus-summary-article-number))))))
6145
6146 (defmacro gnus-summary-iterate (arg &rest forms)
6147   "Iterate over the process/prefixed articles and do FORMS.
6148 ARG is the interactive prefix given to the command.  FORMS will be
6149 executed with point over the summary line of the articles."
6150   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6151     `(let ((,articles (gnus-summary-work-articles ,arg)))
6152        (while ,articles
6153          (gnus-summary-goto-subject (car ,articles))
6154          ,@forms
6155          (pop ,articles)))))
6156
6157 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6158 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6159
6160 (defun gnus-summary-save-process-mark ()
6161   "Push the current set of process marked articles on the stack."
6162   (interactive)
6163   (push (copy-sequence gnus-newsgroup-processable)
6164         gnus-newsgroup-process-stack))
6165
6166 (defun gnus-summary-kill-process-mark ()
6167   "Push the current set of process marked articles on the stack and unmark."
6168   (interactive)
6169   (gnus-summary-save-process-mark)
6170   (gnus-summary-unmark-all-processable))
6171
6172 (defun gnus-summary-yank-process-mark ()
6173   "Pop the last process mark state off the stack and restore it."
6174   (interactive)
6175   (unless gnus-newsgroup-process-stack
6176     (error "Empty mark stack"))
6177   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6178
6179 (defun gnus-summary-process-mark-set (set)
6180   "Make SET into the current process marked articles."
6181   (gnus-summary-unmark-all-processable)
6182   (while set
6183     (gnus-summary-set-process-mark (pop set))))
6184
6185 ;;; Searching and stuff
6186
6187 (defun gnus-summary-search-group (&optional backward use-level)
6188   "Search for next unread newsgroup.
6189 If optional argument BACKWARD is non-nil, search backward instead."
6190   (save-excursion
6191     (set-buffer gnus-group-buffer)
6192     (when (gnus-group-search-forward
6193            backward nil (if use-level (gnus-group-group-level) nil))
6194       (gnus-group-group-name))))
6195
6196 (defun gnus-summary-best-group (&optional exclude-group)
6197   "Find the name of the best unread group.
6198 If EXCLUDE-GROUP, do not go to this group."
6199   (with-current-buffer gnus-group-buffer
6200     (save-excursion
6201       (gnus-group-best-unread-group exclude-group))))
6202
6203 (defun gnus-summary-find-next (&optional unread article backward)
6204   (if backward
6205       (gnus-summary-find-prev unread article)
6206     (let* ((dummy (gnus-summary-article-intangible-p))
6207            (article (or article (gnus-summary-article-number)))
6208            (data (gnus-data-find-list article))
6209            result)
6210       (when (and (not dummy)
6211                  (or (not gnus-summary-check-current)
6212                      (not unread)
6213                      (not (gnus-data-unread-p (car data)))))
6214         (setq data (cdr data)))
6215       (when (setq result
6216                   (if unread
6217                       (progn
6218                         (while data
6219                           (unless (memq (gnus-data-number (car data))
6220                                         (cond
6221                                          ((eq gnus-auto-goto-ignores
6222                                               'always-undownloaded)
6223                                           gnus-newsgroup-undownloaded)
6224                                          (gnus-plugged
6225                                           nil)
6226                                          ((eq gnus-auto-goto-ignores
6227                                               'unfetched)
6228                                           gnus-newsgroup-unfetched)
6229                                          ((eq gnus-auto-goto-ignores
6230                                               'undownloaded)
6231                                           gnus-newsgroup-undownloaded)))
6232                             (when (gnus-data-unread-p (car data))
6233                               (setq result (car data)
6234                                     data nil)))
6235                           (setq data (cdr data)))
6236                         result)
6237                     (car data)))
6238         (goto-char (gnus-data-pos result))
6239         (gnus-data-number result)))))
6240
6241 (defun gnus-summary-find-prev (&optional unread article)
6242   (let* ((eobp (eobp))
6243          (article (or article (gnus-summary-article-number)))
6244          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6245          result)
6246     (when (and (not eobp)
6247                (or (not gnus-summary-check-current)
6248                    (not unread)
6249                    (not (gnus-data-unread-p (car data)))))
6250       (setq data (cdr data)))
6251     (when (setq result
6252                 (if unread
6253                     (progn
6254                       (while data
6255                         (unless (memq (gnus-data-number (car data))
6256                                       (cond
6257                                        ((eq gnus-auto-goto-ignores
6258                                             'always-undownloaded)
6259                                         gnus-newsgroup-undownloaded)
6260                                        (gnus-plugged
6261                                         nil)
6262                                        ((eq gnus-auto-goto-ignores
6263                                             'unfetched)
6264                                         gnus-newsgroup-unfetched)
6265                                        ((eq gnus-auto-goto-ignores
6266                                             'undownloaded)
6267                                         gnus-newsgroup-undownloaded)))
6268                           (when (gnus-data-unread-p (car data))
6269                             (setq result (car data)
6270                                   data nil)))
6271                         (setq data (cdr data)))
6272                       result)
6273                   (car data)))
6274       (goto-char (gnus-data-pos result))
6275       (gnus-data-number result))))
6276
6277 (defun gnus-summary-find-subject (subject &optional unread backward article)
6278   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6279          (article (or article (gnus-summary-article-number)))
6280          (articles (gnus-data-list backward))
6281          (arts (gnus-data-find-list article articles))
6282          result)
6283     (when (or (not gnus-summary-check-current)
6284               (not unread)
6285               (not (gnus-data-unread-p (car arts))))
6286       (setq arts (cdr arts)))
6287     (while arts
6288       (and (or (not unread)
6289                (gnus-data-unread-p (car arts)))
6290            (vectorp (gnus-data-header (car arts)))
6291            (gnus-subject-equal
6292             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6293            (setq result (car arts)
6294                  arts nil))
6295       (setq arts (cdr arts)))
6296     (and result
6297          (goto-char (gnus-data-pos result))
6298          (gnus-data-number result))))
6299
6300 (defun gnus-summary-search-forward (&optional unread subject backward)
6301   "Search forward for an article.
6302 If UNREAD, look for unread articles.  If SUBJECT, look for
6303 articles with that subject.  If BACKWARD, search backward instead."
6304   (cond (subject (gnus-summary-find-subject subject unread backward))
6305         (backward (gnus-summary-find-prev unread))
6306         (t (gnus-summary-find-next unread))))
6307
6308 (defun gnus-recenter (&optional n)
6309   "Center point in window and redisplay frame.
6310 Also do horizontal recentering."
6311   (interactive "P")
6312   (when (and gnus-auto-center-summary
6313              (not (eq gnus-auto-center-summary 'vertical)))
6314     (gnus-horizontal-recenter))
6315   (recenter n))
6316
6317 (defun gnus-summary-recenter ()
6318   "Center point in the summary window.
6319 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6320 displayed, no centering will be performed."
6321   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6322   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6323   (interactive)
6324   ;; The user has to want it.
6325   (when gnus-auto-center-summary
6326     (let* ((top (cond ((< (window-height) 4) 0)
6327                       ((< (window-height) 7) 1)
6328                       (t (if (numberp gnus-auto-center-summary)
6329                              gnus-auto-center-summary
6330                            (/ (1- (window-height)) 2)))))
6331            (height (1- (window-height)))
6332            (bottom (save-excursion (goto-char (point-max))
6333                                    (forward-line (- height))
6334                                    (point)))
6335            (window (get-buffer-window (current-buffer))))
6336       (when (get-buffer-window gnus-article-buffer)
6337         ;; Only do recentering when the article buffer is displayed,
6338         ;; Set the window start to either `bottom', which is the biggest
6339         ;; possible valid number, or the second line from the top,
6340         ;; whichever is the least.
6341         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6342           (if (> bottom top-pos)
6343               ;; Keep the second line from the top visible
6344               (set-window-start window top-pos t)
6345             ;; Try to keep the bottom line visible; if it's partially
6346             ;; obscured, either scroll one more line to make it fully
6347             ;; visible, or revert to using TOP-POS.
6348             (save-excursion
6349               (goto-char (point-max))
6350               (forward-line -1)
6351               (let ((last-line-start (point)))
6352                 (goto-char bottom)
6353                 (set-window-start window (point) t)
6354                 (when (not (pos-visible-in-window-p last-line-start window))
6355                   (forward-line 1)
6356                   (set-window-start window (min (point) top-pos) t)))))))
6357       ;; Do horizontal recentering while we're at it.
6358       (when (and (get-buffer-window (current-buffer) t)
6359                  (not (eq gnus-auto-center-summary 'vertical)))
6360         (let ((selected (selected-window)))
6361           (select-window (get-buffer-window (current-buffer) t))
6362           (gnus-summary-position-point)
6363           (gnus-horizontal-recenter)
6364           (select-window selected))))))
6365
6366 (defun gnus-summary-jump-to-group (newsgroup)
6367   "Move point to NEWSGROUP in group mode buffer."
6368   ;; Keep update point of group mode buffer if visible.
6369   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6370       (save-window-excursion
6371         ;; Take care of tree window mode.
6372         (when (get-buffer-window gnus-group-buffer)
6373           (pop-to-buffer gnus-group-buffer))
6374         (gnus-group-jump-to-group newsgroup))
6375     (save-excursion
6376       ;; Take care of tree window mode.
6377       (if (get-buffer-window gnus-group-buffer)
6378           (pop-to-buffer gnus-group-buffer)
6379         (set-buffer gnus-group-buffer))
6380       (gnus-group-jump-to-group newsgroup))))
6381
6382 ;; This function returns a list of article numbers based on the
6383 ;; difference between the ranges of read articles in this group and
6384 ;; the range of active articles.
6385 (defun gnus-list-of-unread-articles (group)
6386   (let* ((read (gnus-info-read (gnus-get-info group)))
6387          (active (or (gnus-active group) (gnus-activate-group group)))
6388          (last (cdr active))
6389          first nlast unread)
6390     ;; If none are read, then all are unread.
6391     (if (not read)
6392         (setq first (car active))
6393       ;; If the range of read articles is a single range, then the
6394       ;; first unread article is the article after the last read
6395       ;; article.  Sounds logical, doesn't it?
6396       (if (and (not (listp (cdr read)))
6397                (or (< (car read) (car active))
6398                    (progn (setq read (list read))
6399                           nil)))
6400           (setq first (max (car active) (1+ (cdr read))))
6401         ;; `read' is a list of ranges.
6402         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6403                                   (caar read)))
6404                   1)
6405           (setq first (car active)))
6406         (while read
6407           (when first
6408             (while (< first nlast)
6409               (setq unread (cons first unread)
6410                     first (1+ first))))
6411           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6412           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6413           (setq read (cdr read)))))
6414     ;; And add the last unread articles.
6415     (while (<= first last)
6416       (setq unread (cons first unread)
6417             first (1+ first)))
6418     ;; Return the list of unread articles.
6419     (delq 0 (nreverse unread))))
6420
6421 (defun gnus-list-of-read-articles (group)
6422   "Return a list of unread, unticked and non-dormant articles."
6423   (let* ((info (gnus-get-info group))
6424          (marked (gnus-info-marks info))
6425          (active (gnus-active group)))
6426     (and info active
6427          (gnus-list-range-difference
6428           (gnus-list-range-difference
6429            (gnus-sorted-complement
6430             (gnus-uncompress-range active)
6431             (gnus-list-of-unread-articles group))
6432            (cdr (assq 'dormant marked)))
6433           (cdr (assq 'tick marked))))))
6434
6435 ;; This function returns a sequence of article numbers based on the
6436 ;; difference between the ranges of read articles in this group and
6437 ;; the range of active articles.
6438 (defun gnus-sequence-of-unread-articles (group)
6439   (let* ((read (gnus-info-read (gnus-get-info group)))
6440          (active (or (gnus-active group) (gnus-activate-group group)))
6441          (last (cdr active))
6442          first nlast unread)
6443     ;; If none are read, then all are unread.
6444     (if (not read)
6445         (setq first (car active))
6446       ;; If the range of read articles is a single range, then the
6447       ;; first unread article is the article after the last read
6448       ;; article.  Sounds logical, doesn't it?
6449       (if (and (not (listp (cdr read)))
6450                (or (< (car read) (car active))
6451                    (progn (setq read (list read))
6452                           nil)))
6453           (setq first (max (car active) (1+ (cdr read))))
6454         ;; `read' is a list of ranges.
6455         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6456                                   (caar read)))
6457                   1)
6458           (setq first (car active)))
6459         (while read
6460           (when first
6461             (push (cons first nlast) unread))
6462           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6463           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6464           (setq read (cdr read)))))
6465     ;; And add the last unread articles.
6466     (cond ((< first last)
6467            (push (cons first last) unread))
6468           ((= first last)
6469            (push first unread)))
6470     ;; Return the sequence of unread articles.
6471     (delq 0 (nreverse unread))))
6472
6473 ;; Various summary commands
6474
6475 (defun gnus-summary-select-article-buffer ()
6476   "Reconfigure windows to show article buffer."
6477   (interactive)
6478   (if (not (gnus-buffer-live-p gnus-article-buffer))
6479       (error "There is no article buffer for this summary buffer")
6480     (gnus-configure-windows 'article)
6481     (select-window (get-buffer-window gnus-article-buffer))))
6482
6483 (defun gnus-summary-universal-argument (arg)
6484   "Perform any operation on all articles that are process/prefixed."
6485   (interactive "P")
6486   (let ((articles (gnus-summary-work-articles arg))
6487         func article)
6488     (if (eq
6489          (setq
6490           func
6491           (key-binding
6492            (read-key-sequence
6493             (substitute-command-keys
6494              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6495          'undefined)
6496         (gnus-error 1 "Undefined key")
6497       (save-excursion
6498         (while articles
6499           (gnus-summary-goto-subject (setq article (pop articles)))
6500           (let (gnus-newsgroup-processable)
6501             (command-execute func))
6502           (gnus-summary-remove-process-mark article)))))
6503   (gnus-summary-position-point))
6504
6505 (defun gnus-summary-toggle-truncation (&optional arg)
6506   "Toggle truncation of summary lines.
6507 With ARG, turn line truncation on if ARG is positive."
6508   (interactive "P")
6509   (setq truncate-lines
6510         (if (null arg) (not truncate-lines)
6511           (> (prefix-numeric-value arg) 0)))
6512   (redraw-display))
6513
6514 (defun gnus-summary-find-for-reselect ()
6515   "Return the number of an article to stay on across a reselect.
6516 The current article is considered, then following articles, then previous
6517 articles.  An article is sought which is not cancelled and isn't a temporary
6518 insertion from another group.  If there's no such then return a dummy 0."
6519   (let (found)
6520     (dolist (rev '(nil t))
6521       (unless found      ; don't demand the reverse list if we don't need it
6522         (let ((data (gnus-data-find-list
6523                      (gnus-summary-article-number) (gnus-data-list rev))))
6524           (while (and data (not found))
6525             (if (and (< 0 (gnus-data-number (car data)))
6526                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6527                 (setq found (gnus-data-number (car data))))
6528             (setq data (cdr data))))))
6529     (or found 0)))
6530
6531 (defun gnus-summary-reselect-current-group (&optional all rescan)
6532   "Exit and then reselect the current newsgroup.
6533 The prefix argument ALL means to select all articles."
6534   (interactive "P")
6535   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6536     (error "Ephemeral groups can't be reselected"))
6537   (let ((current-subject (gnus-summary-find-for-reselect))
6538         (group gnus-newsgroup-name))
6539     (setq gnus-newsgroup-begin nil)
6540     (gnus-summary-exit nil 'leave-hidden)
6541     ;; We have to adjust the point of group mode buffer because
6542     ;; point was moved to the next unread newsgroup by exiting.
6543     (gnus-summary-jump-to-group group)
6544     (when rescan
6545       (save-excursion
6546         (save-window-excursion
6547           ;; Don't show group contents.
6548           (set-window-start (selected-window) (point-max))
6549           (gnus-group-get-new-news-this-group 1))))
6550     (gnus-group-read-group all t)
6551     (gnus-summary-goto-subject current-subject nil t)))
6552
6553 (defun gnus-summary-rescan-group (&optional all)
6554   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6555   (interactive "P")
6556   (gnus-summary-reselect-current-group all t))
6557
6558 (defun gnus-summary-update-info (&optional non-destructive)
6559   (save-excursion
6560     (let ((group gnus-newsgroup-name))
6561       (when group
6562         (when gnus-newsgroup-kill-headers
6563           (setq gnus-newsgroup-killed
6564                 (gnus-compress-sequence
6565                  (gnus-sorted-union
6566                   (gnus-list-range-intersection
6567                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6568                   gnus-newsgroup-unreads)
6569                  t)))
6570         (unless (listp (cdr gnus-newsgroup-killed))
6571           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6572         (let ((headers gnus-newsgroup-headers))
6573           ;; Set the new ranges of read articles.
6574           (with-current-buffer gnus-group-buffer
6575             (gnus-undo-force-boundary))
6576           (gnus-update-read-articles
6577            group (gnus-sorted-union
6578                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6579           ;; Set the current article marks.
6580           (let ((gnus-newsgroup-scored
6581                  (if (and (not gnus-save-score)
6582                           (not non-destructive))
6583                      nil
6584                    gnus-newsgroup-scored)))
6585             (save-excursion
6586               (gnus-update-marks)))
6587           ;; Do the cross-ref thing.
6588           (when gnus-use-cross-reference
6589             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6590           ;; Do not switch windows but change the buffer to work.
6591           (set-buffer gnus-group-buffer)
6592           (unless (gnus-ephemeral-group-p group)
6593             (gnus-group-update-group group)))))))
6594
6595 (defun gnus-summary-save-newsrc (&optional force)
6596   "Save the current number of read/marked articles in the dribble buffer.
6597 The dribble buffer will then be saved.
6598 If FORCE (the prefix), also save the .newsrc file(s)."
6599   (interactive "P")
6600   (gnus-summary-update-info t)
6601   (if force
6602       (gnus-save-newsrc-file)
6603     (gnus-dribble-save)))
6604
6605 (defun gnus-summary-exit (&optional temporary leave-hidden)
6606   "Exit reading current newsgroup, and then return to group selection mode.
6607 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6608   (interactive)
6609   (gnus-set-global-variables)
6610   (gnus-kill-save-kill-buffer)
6611   (gnus-async-halt-prefetch)
6612   (let* ((group gnus-newsgroup-name)
6613          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6614          (gnus-group-is-exiting-p t)
6615          (mode major-mode)
6616          (group-point nil)
6617          (buf (current-buffer)))
6618     (unless quit-config
6619       ;; Do adaptive scoring, and possibly save score files.
6620       (when gnus-newsgroup-adaptive
6621         (gnus-score-adaptive))
6622       (when gnus-use-scoring
6623         (gnus-score-save)))
6624     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6625     ;; If we have several article buffers, we kill them at exit.
6626     (unless gnus-single-article-buffer
6627       (gnus-kill-buffer gnus-original-article-buffer)
6628       (setq gnus-article-current nil))
6629     (when gnus-use-cache
6630       (gnus-cache-possibly-remove-articles)
6631       (gnus-cache-save-buffers))
6632     (gnus-async-prefetch-remove-group group)
6633     (when gnus-suppress-duplicates
6634       (gnus-dup-enter-articles))
6635     (when gnus-use-trees
6636       (gnus-tree-close group))
6637     (when gnus-use-cache
6638       (gnus-cache-write-active))
6639     ;; Remove entries for this group.
6640     (nnmail-purge-split-history (gnus-group-real-name group))
6641     ;; Make all changes in this group permanent.
6642     (unless quit-config
6643       (gnus-run-hooks 'gnus-exit-group-hook)
6644       (gnus-summary-update-info))
6645     (gnus-close-group group)
6646     ;; Make sure where we were, and go to next newsgroup.
6647     (set-buffer gnus-group-buffer)
6648     (unless quit-config
6649       (gnus-group-jump-to-group group))
6650     (gnus-run-hooks 'gnus-summary-exit-hook)
6651     (unless (or quit-config
6652                 ;; If this group has disappeared from the summary
6653                 ;; buffer, don't skip forwards.
6654                 (not (string= group (gnus-group-group-name))))
6655       (gnus-group-next-unread-group 1))
6656     (setq group-point (point))
6657     (if temporary
6658         nil                             ;Nothing to do.
6659       ;; If we have several article buffers, we kill them at exit.
6660       (unless gnus-single-article-buffer
6661         (gnus-kill-buffer gnus-article-buffer)
6662         (gnus-kill-buffer gnus-original-article-buffer)
6663         (setq gnus-article-current nil))
6664       (set-buffer buf)
6665       (if (not gnus-kill-summary-on-exit)
6666           (progn
6667             (gnus-deaden-summary)
6668             (setq mode nil))
6669         ;; We set all buffer-local variables to nil.  It is unclear why
6670         ;; this is needed, but if we don't, buffer-local variables are
6671         ;; not garbage-collected, it seems.  This would the lead to en
6672         ;; ever-growing Emacs.
6673         (gnus-summary-clear-local-variables)
6674         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6675           (gnus-summary-clear-local-variables))
6676         (when (get-buffer gnus-article-buffer)
6677           (bury-buffer gnus-article-buffer))
6678         ;; We clear the global counterparts of the buffer-local
6679         ;; variables as well, just to be on the safe side.
6680         (set-buffer gnus-group-buffer)
6681         (gnus-summary-clear-local-variables)
6682         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6683           (gnus-summary-clear-local-variables))
6684         ;; Return to group mode buffer.
6685         (when (eq mode 'gnus-summary-mode)
6686           (gnus-kill-buffer buf)))
6687       (setq gnus-current-select-method gnus-select-method)
6688       (if leave-hidden
6689           (set-buffer gnus-group-buffer)
6690         (pop-to-buffer gnus-group-buffer))
6691       (if (not quit-config)
6692           (progn
6693             (goto-char group-point)
6694             (unless leave-hidden
6695               (gnus-configure-windows 'group 'force))
6696             (unless (pos-visible-in-window-p)
6697               (forward-line (/ (static-if (featurep 'xemacs)
6698                                    (window-displayed-height)
6699                                  (1- (window-height)))
6700                                -2))
6701               (set-window-start (selected-window) (point))
6702               (goto-char group-point)))
6703         (gnus-handle-ephemeral-exit quit-config))
6704       ;; Clear the current group name.
6705       (unless quit-config
6706         (setq gnus-newsgroup-name nil)))))
6707
6708 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6709 (defun gnus-summary-exit-no-update (&optional no-questions)
6710   "Quit reading current newsgroup without updating read article info."
6711   (interactive)
6712   (let* ((group gnus-newsgroup-name)
6713          (gnus-group-is-exiting-p t)
6714          (gnus-group-is-exiting-without-update-p t)
6715          (quit-config (gnus-group-quit-config group)))
6716     (when (or no-questions
6717               gnus-expert-user
6718               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6719       (gnus-async-halt-prefetch)
6720       (run-hooks 'gnus-summary-prepare-exit-hook)
6721       ;; If we have several article buffers, we kill them at exit.
6722       (unless gnus-single-article-buffer
6723         (gnus-kill-buffer gnus-article-buffer)
6724         (gnus-kill-buffer gnus-original-article-buffer)
6725         (setq gnus-article-current nil))
6726       (if (not gnus-kill-summary-on-exit)
6727           (gnus-deaden-summary)
6728         (gnus-close-group group)
6729         (gnus-summary-clear-local-variables)
6730         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6731           (gnus-summary-clear-local-variables))
6732         (set-buffer gnus-group-buffer)
6733         (gnus-summary-clear-local-variables)
6734         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6735           (gnus-summary-clear-local-variables))
6736         (gnus-kill-buffer gnus-summary-buffer))
6737       (unless gnus-single-article-buffer
6738         (setq gnus-article-current nil))
6739       (when gnus-use-trees
6740         (gnus-tree-close group))
6741       (gnus-async-prefetch-remove-group group)
6742       (when (get-buffer gnus-article-buffer)
6743         (bury-buffer gnus-article-buffer))
6744       ;; Return to the group buffer.
6745       (gnus-configure-windows 'group 'force)
6746       ;; Clear the current group name.
6747       (setq gnus-newsgroup-name nil)
6748       (unless (gnus-ephemeral-group-p group)
6749         (gnus-group-update-group group))
6750       (when (equal (gnus-group-group-name) group)
6751         (gnus-group-next-unread-group 1))
6752       (when quit-config
6753         (gnus-handle-ephemeral-exit quit-config)))))
6754
6755 (defun gnus-handle-ephemeral-exit (quit-config)
6756   "Handle movement when leaving an ephemeral group.
6757 The state which existed when entering the ephemeral is reset."
6758   (if (not (buffer-name (car quit-config)))
6759       (gnus-configure-windows 'group 'force)
6760     (set-buffer (car quit-config))
6761     (cond ((eq major-mode 'gnus-summary-mode)
6762            (gnus-set-global-variables))
6763           ((eq major-mode 'gnus-article-mode)
6764            (save-excursion
6765              ;; The `gnus-summary-buffer' variable may point
6766              ;; to the old summary buffer when using a single
6767              ;; article buffer.
6768              (unless (gnus-buffer-live-p gnus-summary-buffer)
6769                (set-buffer gnus-group-buffer))
6770              (set-buffer gnus-summary-buffer)
6771              (gnus-set-global-variables))))
6772     (if (or (eq (cdr quit-config) 'article)
6773             (eq (cdr quit-config) 'pick))
6774         (progn
6775           ;; The current article may be from the ephemeral group
6776           ;; thus it is best that we reload this article
6777           ;;
6778           ;; If we're exiting from a large digest, this can be
6779           ;; extremely slow.  So, it's better not to reload it. -- jh.
6780           ;;(gnus-summary-show-article)
6781           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6782               (gnus-configure-windows 'pick 'force)
6783             (gnus-configure-windows (cdr quit-config) 'force)))
6784       (gnus-configure-windows (cdr quit-config) 'force))
6785     (when (eq major-mode 'gnus-summary-mode)
6786       (gnus-summary-next-subject 1 nil t)
6787       (gnus-summary-recenter)
6788       (gnus-summary-position-point))))
6789
6790 (defun gnus-summary-preview-mime-message ()
6791   "MIME decode and play this message."
6792   (interactive)
6793   (let ((gnus-break-pages nil)
6794         (gnus-show-mime t))
6795     (gnus-summary-select-article gnus-show-all-headers t))
6796   (let ((w (get-buffer-window gnus-article-buffer)))
6797     (when w
6798       (select-window (get-buffer-window gnus-article-buffer)))))
6799
6800 ;;; Dead summaries.
6801
6802 (defvar gnus-dead-summary-mode-map nil)
6803
6804 (unless gnus-dead-summary-mode-map
6805   (setq gnus-dead-summary-mode-map (make-keymap))
6806   (suppress-keymap gnus-dead-summary-mode-map)
6807   (substitute-key-definition
6808    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6809   (dolist (key '("\C-d" "\r" "\177" [delete]))
6810     (define-key gnus-dead-summary-mode-map
6811       key 'gnus-summary-wake-up-the-dead))
6812   (dolist (key '("q" "Q"))
6813     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6814
6815 (defvar gnus-dead-summary-mode nil
6816   "Minor mode for Gnus summary buffers.")
6817
6818 (defun gnus-dead-summary-mode (&optional arg)
6819   "Minor mode for Gnus summary buffers."
6820   (interactive "P")
6821   (when (eq major-mode 'gnus-summary-mode)
6822     (make-local-variable 'gnus-dead-summary-mode)
6823     (setq gnus-dead-summary-mode
6824           (if (null arg) (not gnus-dead-summary-mode)
6825             (> (prefix-numeric-value arg) 0)))
6826     (when gnus-dead-summary-mode
6827       (add-minor-mode
6828        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6829
6830 (defun gnus-deaden-summary ()
6831   "Make the current summary buffer into a dead summary buffer."
6832   ;; Kill any previous dead summary buffer.
6833   (when (and gnus-dead-summary
6834              (buffer-name gnus-dead-summary))
6835     (with-current-buffer gnus-dead-summary
6836       (when gnus-dead-summary-mode
6837         (kill-buffer (current-buffer)))))
6838   ;; Make this the current dead summary.
6839   (setq gnus-dead-summary (current-buffer))
6840   (gnus-dead-summary-mode 1)
6841   (let ((name (buffer-name)))
6842     (when (string-match "Summary" name)
6843       (rename-buffer
6844        (concat (substring name 0 (match-beginning 0)) "Dead "
6845                (substring name (match-beginning 0)))
6846        t)
6847       (bury-buffer))))
6848
6849 (defun gnus-kill-or-deaden-summary (buffer)
6850   "Kill or deaden the summary BUFFER."
6851   (save-excursion
6852     (when (and (buffer-name buffer)
6853                (not gnus-single-article-buffer))
6854       (with-current-buffer buffer
6855         (gnus-kill-buffer gnus-article-buffer)
6856         (gnus-kill-buffer gnus-original-article-buffer)))
6857     (cond
6858      ;; Kill the buffer.
6859      (gnus-kill-summary-on-exit
6860       (when (and gnus-use-trees
6861                  (gnus-buffer-exists-p buffer))
6862         (save-excursion
6863           (set-buffer buffer)
6864           (gnus-tree-close gnus-newsgroup-name)))
6865       (gnus-kill-buffer buffer))
6866      ;; Deaden the buffer.
6867      ((gnus-buffer-exists-p buffer)
6868       (save-excursion
6869         (set-buffer buffer)
6870         (gnus-deaden-summary))))))
6871
6872 (defun gnus-summary-wake-up-the-dead (&rest args)
6873   "Wake up the dead summary buffer."
6874   (interactive)
6875   (gnus-dead-summary-mode -1)
6876   (let ((name (buffer-name)))
6877     (when (string-match "Dead " name)
6878       (rename-buffer
6879        (concat (substring name 0 (match-beginning 0))
6880                (substring name (match-end 0)))
6881        t)))
6882   (gnus-message 3 "This dead summary is now alive again"))
6883
6884 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6885 (defun gnus-summary-fetch-faq (&optional faq-dir)
6886   "Fetch the FAQ for the current group.
6887 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6888 in."
6889   (interactive
6890    (list
6891     (when current-prefix-arg
6892       (completing-read
6893        "FAQ dir: " (and (listp gnus-group-faq-directory)
6894                         (mapcar 'list
6895                                 gnus-group-faq-directory))))))
6896   (let (gnus-faq-buffer)
6897     (when (setq gnus-faq-buffer
6898                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6899       (gnus-configure-windows 'summary-faq))))
6900
6901 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6902 (defun gnus-summary-describe-group (&optional force)
6903   "Describe the current newsgroup."
6904   (interactive "P")
6905   (gnus-group-describe-group force gnus-newsgroup-name))
6906
6907 (defun gnus-summary-describe-briefly ()
6908   "Describe summary mode commands briefly."
6909   (interactive)
6910   (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")))
6911
6912 ;; Walking around group mode buffer from summary mode.
6913
6914 (defun gnus-summary-next-group (&optional no-article target-group backward)
6915   "Exit current newsgroup and then select next unread newsgroup.
6916 If prefix argument NO-ARTICLE is non-nil, no article is selected
6917 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6918 previous group instead."
6919   (interactive "P")
6920   ;; Stop pre-fetching.
6921   (gnus-async-halt-prefetch)
6922   (let ((current-group gnus-newsgroup-name)
6923         (current-buffer (current-buffer))
6924         entered)
6925     ;; First we semi-exit this group to update Xrefs and all variables.
6926     ;; We can't do a real exit, because the window conf must remain
6927     ;; the same in case the user is prompted for info, and we don't
6928     ;; want the window conf to change before that...
6929     (gnus-summary-exit t)
6930     (while (not entered)
6931       ;; Then we find what group we are supposed to enter.
6932       (set-buffer gnus-group-buffer)
6933       (gnus-group-jump-to-group current-group)
6934       (setq target-group
6935             (or target-group
6936                 (if (eq gnus-keep-same-level 'best)
6937                     (gnus-summary-best-group gnus-newsgroup-name)
6938                   (gnus-summary-search-group backward gnus-keep-same-level))))
6939       (if (not target-group)
6940           ;; There are no further groups, so we return to the group
6941           ;; buffer.
6942           (progn
6943             (gnus-message 5 "Returning to the group buffer")
6944             (setq entered t)
6945             (when (gnus-buffer-live-p current-buffer)
6946               (set-buffer current-buffer)
6947               (gnus-summary-exit))
6948             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6949         ;; We try to enter the target group.
6950         (gnus-group-jump-to-group target-group)
6951         (let ((unreads (gnus-group-group-unread)))
6952           (if (and (or (eq t unreads)
6953                        (and unreads (not (zerop unreads))))
6954                    (gnus-summary-read-group
6955                     target-group nil no-article
6956                     (and (buffer-name current-buffer) current-buffer)
6957                     nil backward))
6958               (setq entered t)
6959             (setq current-group target-group
6960                   target-group nil)))))))
6961
6962 (defun gnus-summary-prev-group (&optional no-article)
6963   "Exit current newsgroup and then select previous unread newsgroup.
6964 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6965   (interactive "P")
6966   (gnus-summary-next-group no-article nil t))
6967
6968 ;; Walking around summary lines.
6969
6970 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6971   "Go to the first subject satisfying any non-nil constraint.
6972 If UNREAD is non-nil, the article should be unread.
6973 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6974 If UNSEEN is non-nil, the article should be unseen.
6975 Returns the article selected or nil if there are no matching articles."
6976   (interactive "P")
6977   (cond
6978    ;; Empty summary.
6979    ((null gnus-newsgroup-data)
6980     (gnus-message 3 "No articles in the group")
6981     nil)
6982    ;; Pick the first article.
6983    ((not (or unread undownloaded unseen))
6984     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6985     (gnus-data-number (car gnus-newsgroup-data)))
6986    ;; Find the first unread article.
6987    (t
6988     (let ((data gnus-newsgroup-data))
6989       (while (and data
6990                   (let ((num (gnus-data-number (car data))))
6991                     (or (memq num gnus-newsgroup-unfetched)
6992                         (not (or (and unread
6993                                       (memq num gnus-newsgroup-unreads))
6994                                  (and undownloaded
6995                                       (memq num gnus-newsgroup-undownloaded))
6996                                  (and unseen
6997                                       (memq num gnus-newsgroup-unseen)))))))
6998         (setq data (cdr data)))
6999       (prog1
7000           (if data
7001               (progn
7002                 (goto-char (gnus-data-pos (car data)))
7003                 (gnus-data-number (car data)))
7004             (gnus-message 3 "No more%s articles"
7005                           (let* ((r (when unread " unread"))
7006                                  (d (when undownloaded " undownloaded"))
7007                                  (s (when unseen " unseen"))
7008                                  (l (delq nil (list r d s))))
7009                             (cond ((= 3 (length l))
7010                                    (concat r "," d ", or" s))
7011                                   ((= 2 (length l))
7012                                    (concat (car l) ", or" (cadr l)))
7013                                   ((= 1 (length l))
7014                                    (car l))
7015                                   (t
7016                                    ""))))
7017             nil
7018             )
7019         (gnus-summary-position-point))))))
7020
7021 (defun gnus-summary-next-subject (n &optional unread dont-display)
7022   "Go to next N'th summary line.
7023 If N is negative, go to the previous N'th subject line.
7024 If UNREAD is non-nil, only unread articles are selected.
7025 The difference between N and the actual number of steps taken is
7026 returned."
7027   (interactive "p")
7028   (let ((backward (< n 0))
7029         (n (abs n)))
7030     (while (and (> n 0)
7031                 (if backward
7032                     (gnus-summary-find-prev unread)
7033                   (gnus-summary-find-next unread)))
7034       (unless (zerop (setq n (1- n)))
7035         (gnus-summary-show-thread)))
7036     (when (/= 0 n)
7037       (gnus-message 7 "No more%s articles"
7038                     (if unread " unread" "")))
7039     (unless dont-display
7040       (gnus-summary-recenter)
7041       (gnus-summary-position-point))
7042     n))
7043
7044 (defun gnus-summary-next-unread-subject (n)
7045   "Go to next N'th unread summary line."
7046   (interactive "p")
7047   (gnus-summary-next-subject n t))
7048
7049 (defun gnus-summary-prev-subject (n &optional unread)
7050   "Go to previous N'th summary line.
7051 If optional argument UNREAD is non-nil, only unread article is selected."
7052   (interactive "p")
7053   (gnus-summary-next-subject (- n) unread))
7054
7055 (defun gnus-summary-prev-unread-subject (n)
7056   "Go to previous N'th unread summary line."
7057   (interactive "p")
7058   (gnus-summary-next-subject (- n) t))
7059
7060 (defun gnus-summary-goto-subjects (articles)
7061   "Insert the subject header for ARTICLES in the current buffer."
7062   (save-excursion
7063     (dolist (article articles)
7064       (gnus-summary-goto-subject article t)))
7065   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7066   (gnus-summary-position-point))
7067  
7068 (defun gnus-summary-goto-subject (article &optional force silent)
7069   "Go the subject line of ARTICLE.
7070 If FORCE, also allow jumping to articles not currently shown."
7071   (interactive "nArticle number: ")
7072   (unless (numberp article)
7073     (error "Article %s is not a number" article))
7074   (let ((b (point))
7075         (data (gnus-data-find article)))
7076     ;; We read in the article if we have to.
7077     (and (not data)
7078          force
7079          (gnus-summary-insert-subject
7080           article
7081           (if (or (numberp force) (vectorp force)) force)
7082           t)
7083          (setq data (gnus-data-find article)))
7084     (goto-char b)
7085     (if (not data)
7086         (progn
7087           (unless silent
7088             (gnus-message 3 "Can't find article %d" article))
7089           nil)
7090       (let ((pt (gnus-data-pos data)))
7091         (goto-char pt)
7092         (gnus-summary-set-article-display-arrow pt))
7093       (gnus-summary-position-point)
7094       article)))
7095
7096 ;; Walking around summary lines with displaying articles.
7097
7098 (defun gnus-summary-expand-window (&optional arg)
7099   "Make the summary buffer take up the entire Emacs frame.
7100 Given a prefix, will force an `article' buffer configuration."
7101   (interactive "P")
7102   (if arg
7103       (gnus-configure-windows 'article 'force)
7104     (gnus-configure-windows 'summary 'force)))
7105
7106 (defun gnus-summary-display-article (article &optional all-header)
7107   "Display ARTICLE in article buffer."
7108   (when (gnus-buffer-live-p gnus-article-buffer)
7109     (with-current-buffer gnus-article-buffer
7110       (set-buffer-multibyte t)))
7111   (gnus-set-global-variables)
7112   (when (gnus-buffer-live-p gnus-article-buffer)
7113     (with-current-buffer gnus-article-buffer
7114       (setq gnus-article-charset gnus-newsgroup-charset)
7115       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7116       (set-buffer-multibyte t)))
7117   (if (null article)
7118       nil
7119     (prog1
7120         (if gnus-summary-display-article-function
7121             (funcall gnus-summary-display-article-function article all-header)
7122           (gnus-article-prepare article all-header))
7123       (with-current-buffer gnus-article-buffer
7124         (set (make-local-variable 'gnus-summary-search-article-matched-data)
7125              nil))
7126       (gnus-run-hooks 'gnus-select-article-hook)
7127       (when (and gnus-current-article
7128                  (not (zerop gnus-current-article)))
7129         (gnus-summary-goto-subject gnus-current-article))
7130       (gnus-summary-recenter)
7131       (when (and gnus-use-trees gnus-show-threads)
7132         (gnus-possibly-generate-tree article)
7133         (gnus-highlight-selected-tree article))
7134       ;; Successfully display article.
7135       (gnus-article-set-window-start
7136        (cdr (assq article gnus-newsgroup-bookmarks))))))
7137
7138 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7139   "Select the current article.
7140 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7141 non-nil, the article will be re-fetched even if it already present in
7142 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7143 be displayed."
7144   ;; Make sure we are in the summary buffer to work around bbdb bug.
7145   (unless (eq major-mode 'gnus-summary-mode)
7146     (set-buffer gnus-summary-buffer))
7147   (let ((article (or article (gnus-summary-article-number)))
7148         (all-headers (not (not all-headers))) ;Must be T or NIL.
7149         gnus-summary-display-article-function)
7150     (and (not pseudo)
7151          (gnus-summary-article-pseudo-p article)
7152          (error "This is a pseudo-article"))
7153     (save-excursion
7154       (set-buffer gnus-summary-buffer)
7155       (if (or (and gnus-single-article-buffer
7156                    (or (null gnus-current-article)
7157                        (null gnus-article-current)
7158                        (null (get-buffer gnus-article-buffer))
7159                        (not (eq article (cdr gnus-article-current)))
7160                        (not (equal (car gnus-article-current)
7161                                    gnus-newsgroup-name))))
7162               (and (not gnus-single-article-buffer)
7163                    (or (null gnus-current-article)
7164                        (not (eq gnus-current-article article))))
7165               force)
7166           ;; The requested article is different from the current article.
7167           (progn
7168             (gnus-summary-display-article article all-headers)
7169             (gnus-article-set-window-start
7170              (cdr (assq article gnus-newsgroup-bookmarks)))
7171             article)
7172         'old))))
7173
7174 (defun gnus-summary-force-verify-and-decrypt ()
7175   "Display buttons for signed/encrypted parts and verify/decrypt them."
7176   (interactive)
7177   (let ((mm-verify-option 'known)
7178         (mm-decrypt-option 'known)
7179         (gnus-article-emulate-mime t)
7180         (mm-fill-flowed nil)
7181         (gnus-buttonized-mime-types (append (list "multipart/signed"
7182                                                   "multipart/encrypted")
7183                                             gnus-buttonized-mime-types)))
7184     (gnus-summary-select-article nil 'force)))
7185
7186 (defun gnus-summary-set-current-mark (&optional current-mark)
7187   "Obsolete function."
7188   nil)
7189
7190 (defun gnus-summary-next-article (&optional unread subject backward push)
7191   "Select the next article.
7192 If UNREAD, only unread articles are selected.
7193 If SUBJECT, only articles with SUBJECT are selected.
7194 If BACKWARD, the previous article is selected instead of the next."
7195   (interactive "P")
7196   (cond
7197    ;; Is there such an article?
7198    ((and (gnus-summary-search-forward unread subject backward)
7199          (or (gnus-summary-display-article (gnus-summary-article-number))
7200              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7201     (gnus-summary-position-point))
7202    ;; If not, we try the first unread, if that is wanted.
7203    ((and subject
7204          gnus-auto-select-same
7205          (gnus-summary-first-unread-article))
7206     (gnus-summary-position-point)
7207     (gnus-message 6 "Wrapped"))
7208    ;; Try to get next/previous article not displayed in this group.
7209    ((and gnus-auto-extend-newsgroup
7210          (not unread) (not subject))
7211     (gnus-summary-goto-article
7212      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7213      nil (count-lines (point-min) (point))))
7214    ;; Go to next/previous group.
7215    (t
7216     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7217       (gnus-summary-jump-to-group gnus-newsgroup-name))
7218     (let ((cmd last-command-char)
7219           (point
7220            (with-current-buffer gnus-group-buffer
7221              (point)))
7222           (group
7223            (if (eq gnus-keep-same-level 'best)
7224                (gnus-summary-best-group gnus-newsgroup-name)
7225              (gnus-summary-search-group backward gnus-keep-same-level))))
7226       ;; For some reason, the group window gets selected.  We change
7227       ;; it back.
7228       (select-window (get-buffer-window (current-buffer)))
7229       ;; Select next unread newsgroup automagically.
7230       (cond
7231        ((or (not gnus-auto-select-next)
7232             (not cmd))
7233         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7234        ((or (eq gnus-auto-select-next 'quietly)
7235             (and (eq gnus-auto-select-next 'slightly-quietly)
7236                  push)
7237             (and (eq gnus-auto-select-next 'almost-quietly)
7238                  (gnus-summary-last-article-p)))
7239         ;; Select quietly.
7240         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7241             (gnus-summary-exit)
7242           (gnus-message 7 "No more%s articles (%s)..."
7243                         (if unread " unread" "")
7244                         (if group (concat "selecting " group)
7245                           "exiting"))
7246           (gnus-summary-next-group nil group backward)))
7247        (t
7248         (when (gnus-key-press-event-p last-input-event)
7249           (gnus-summary-walk-group-buffer
7250            gnus-newsgroup-name cmd unread backward point))))))))
7251
7252 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7253   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7254                       (?\C-p (gnus-group-prev-unread-group 1))))
7255         (cursor-in-echo-area t)
7256         keve key group ended prompt)
7257     (save-excursion
7258       (set-buffer gnus-group-buffer)
7259       (goto-char start)
7260       (setq group
7261             (if (eq gnus-keep-same-level 'best)
7262                 (gnus-summary-best-group gnus-newsgroup-name)
7263               (gnus-summary-search-group backward gnus-keep-same-level))))
7264     (while (not ended)
7265       (setq prompt
7266             (format
7267              "No more%s articles%s " (if unread " unread" "")
7268              (if (and group
7269                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7270                  (format " (Type %s for %s [%s])"
7271                          (single-key-description cmd) group
7272                          (gnus-group-unread group))
7273                (format " (Type %s to exit %s)"
7274                        (single-key-description cmd)
7275                        gnus-newsgroup-name))))
7276       ;; Confirm auto selection.
7277       (setq key (car (setq keve (gnus-read-event-char prompt)))
7278             ended t)
7279       (cond
7280        ((assq key keystrokes)
7281         (let ((obuf (current-buffer)))
7282           (switch-to-buffer gnus-group-buffer)
7283           (when group
7284             (gnus-group-jump-to-group group))
7285           (eval (cadr (assq key keystrokes)))
7286           (setq group (gnus-group-group-name))
7287           (switch-to-buffer obuf))
7288         (setq ended nil))
7289        ((equal key cmd)
7290         (if (or (not group)
7291                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7292             (gnus-summary-exit)
7293           (gnus-summary-next-group nil group backward)))
7294        (t
7295         (push (cdr keve) unread-command-events))))))
7296
7297 (defun gnus-summary-next-unread-article ()
7298   "Select unread article after current one."
7299   (interactive)
7300   (gnus-summary-next-article
7301    (or (not (eq gnus-summary-goto-unread 'never))
7302        (gnus-summary-last-article-p (gnus-summary-article-number)))
7303    (and gnus-auto-select-same
7304         (gnus-summary-article-subject))))
7305
7306 (defun gnus-summary-prev-article (&optional unread subject)
7307   "Select the article after the current one.
7308 If UNREAD is non-nil, only unread articles are selected."
7309   (interactive "P")
7310   (gnus-summary-next-article unread subject t))
7311
7312 (defun gnus-summary-prev-unread-article ()
7313   "Select unread article before current one."
7314   (interactive)
7315   (gnus-summary-prev-article
7316    (or (not (eq gnus-summary-goto-unread 'never))
7317        (gnus-summary-first-article-p (gnus-summary-article-number)))
7318    (and gnus-auto-select-same
7319         (gnus-summary-article-subject))))
7320
7321 (defun gnus-summary-next-page (&optional lines circular stop)
7322   "Show next page of the selected article.
7323 If at the end of the current article, select the next article.
7324 LINES says how many lines should be scrolled up.
7325
7326 If CIRCULAR is non-nil, go to the start of the article instead of
7327 selecting the next article when reaching the end of the current
7328 article.
7329
7330 If STOP is non-nil, just stop when reaching the end of the message.
7331
7332 Also see the variable `gnus-article-skip-boring'."
7333   (interactive "P")
7334   (setq gnus-summary-buffer (current-buffer))
7335   (gnus-set-global-variables)
7336   (let ((article (gnus-summary-article-number))
7337         (article-window (get-buffer-window gnus-article-buffer t))
7338         endp)
7339     ;; If the buffer is empty, we have no article.
7340     (unless article
7341       (error "No article to select"))
7342     (gnus-configure-windows 'article)
7343     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7344         (if (and (eq gnus-summary-goto-unread 'never)
7345                  (not (gnus-summary-last-article-p article)))
7346             (gnus-summary-next-article)
7347           (gnus-summary-next-unread-article))
7348       (if (or (null gnus-current-article)
7349               (null gnus-article-current)
7350               (/= article (cdr gnus-article-current))
7351               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7352           ;; Selected subject is different from current article's.
7353           (gnus-summary-display-article article)
7354         (when article-window
7355           (gnus-eval-in-buffer-window gnus-article-buffer
7356             (setq endp (or (gnus-article-next-page lines)
7357                            (gnus-article-only-boring-p))))
7358           (when endp
7359             (cond (stop
7360                    (gnus-message 3 "End of message"))
7361                   (circular
7362                    (gnus-summary-beginning-of-article))
7363                   (lines
7364                    (gnus-message 3 "End of message"))
7365                   ((null lines)
7366                    (if (and (eq gnus-summary-goto-unread 'never)
7367                             (not (gnus-summary-last-article-p article)))
7368                        (gnus-summary-next-article)
7369                      (gnus-summary-next-unread-article))))))))
7370     (gnus-summary-recenter)
7371     (gnus-summary-position-point)))
7372
7373 (defun gnus-summary-prev-page (&optional lines move)
7374   "Show previous page of selected article.
7375 Argument LINES specifies lines to be scrolled down.
7376 If MOVE, move to the previous unread article if point is at
7377 the beginning of the buffer."
7378   (interactive "P")
7379   (let ((article (gnus-summary-article-number))
7380         (article-window (get-buffer-window gnus-article-buffer t))
7381         endp)
7382     (gnus-configure-windows 'article)
7383     (if (or (null gnus-current-article)
7384             (null gnus-article-current)
7385             (/= article (cdr gnus-article-current))
7386             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7387         ;; Selected subject is different from current article's.
7388         (gnus-summary-display-article article)
7389       (gnus-summary-recenter)
7390       (when article-window
7391         (gnus-eval-in-buffer-window gnus-article-buffer
7392           (setq endp (gnus-article-prev-page lines)))
7393         (when (and move endp)
7394           (cond (lines
7395                  (gnus-message 3 "Beginning of message"))
7396                 ((null lines)
7397                  (if (and (eq gnus-summary-goto-unread 'never)
7398                           (not (gnus-summary-first-article-p article)))
7399                      (gnus-summary-prev-article)
7400                    (gnus-summary-prev-unread-article))))))))
7401   (gnus-summary-position-point))
7402
7403 (defun gnus-summary-prev-page-or-article (&optional lines)
7404   "Show previous page of selected article.
7405 Argument LINES specifies lines to be scrolled down.
7406 If at the beginning of the article, go to the next article."
7407   (interactive "P")
7408   (gnus-summary-prev-page lines t))
7409
7410 (defun gnus-summary-scroll-up (lines)
7411   "Scroll up (or down) one line current article.
7412 Argument LINES specifies lines to be scrolled up (or down if negative)."
7413   (interactive "p")
7414   (gnus-configure-windows 'article)
7415   (gnus-summary-show-thread)
7416   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7417     (gnus-eval-in-buffer-window gnus-article-buffer
7418       (cond ((> lines 0)
7419              (when (gnus-article-next-page lines)
7420                (gnus-message 3 "End of message")))
7421             ((< lines 0)
7422              (gnus-article-prev-page (- lines))))))
7423   (gnus-summary-recenter)
7424   (gnus-summary-position-point))
7425
7426 (defun gnus-summary-scroll-down (lines)
7427   "Scroll down (or up) one line current article.
7428 Argument LINES specifies lines to be scrolled down (or up if negative)."
7429   (interactive "p")
7430   (gnus-summary-scroll-up (- lines)))
7431
7432 (defun gnus-summary-next-same-subject ()
7433   "Select next article which has the same subject as current one."
7434   (interactive)
7435   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7436
7437 (defun gnus-summary-prev-same-subject ()
7438   "Select previous article which has the same subject as current one."
7439   (interactive)
7440   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7441
7442 (defun gnus-summary-next-unread-same-subject ()
7443   "Select next unread article which has the same subject as current one."
7444   (interactive)
7445   (gnus-summary-next-article t (gnus-summary-article-subject)))
7446
7447 (defun gnus-summary-prev-unread-same-subject ()
7448   "Select previous unread article which has the same subject as current one."
7449   (interactive)
7450   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7451
7452 (defun gnus-summary-first-unread-article ()
7453   "Select the first unread article.
7454 Return nil if there are no unread articles."
7455   (interactive)
7456   (prog1
7457       (when (gnus-summary-first-subject t)
7458         (gnus-summary-show-thread)
7459         (gnus-summary-first-subject t)
7460         (gnus-summary-display-article (gnus-summary-article-number)))
7461     (gnus-summary-position-point)))
7462
7463 (defun gnus-summary-first-unread-subject ()
7464   "Place the point on the subject line of the first unread article.
7465 Return nil if there are no unread articles."
7466   (interactive)
7467   (prog1
7468       (when (gnus-summary-first-subject t)
7469         (gnus-summary-show-thread)
7470         (gnus-summary-first-subject t))
7471     (gnus-summary-position-point)))
7472
7473 (defun gnus-summary-first-unseen-subject ()
7474   "Place the point on the subject line of the first unseen article.
7475 Return nil if there are no unseen articles."
7476   (interactive)
7477   (prog1
7478       (when (gnus-summary-first-subject nil nil t)
7479         (gnus-summary-show-thread)
7480         (gnus-summary-first-subject nil nil t))
7481     (gnus-summary-position-point)))
7482
7483 (defun gnus-summary-first-unseen-or-unread-subject ()
7484   "Place the point on the subject line of the first unseen article or,
7485 if all article have been seen, on the subject line of the first unread
7486 article."
7487   (interactive)
7488   (prog1
7489       (unless (when (gnus-summary-first-subject nil nil t)
7490                 (gnus-summary-show-thread)
7491                 (gnus-summary-first-subject nil nil t))
7492         (when (gnus-summary-first-subject t)
7493           (gnus-summary-show-thread)
7494           (gnus-summary-first-subject t)))
7495     (gnus-summary-position-point)))
7496
7497 (defun gnus-summary-first-article ()
7498   "Select the first article.
7499 Return nil if there are no articles."
7500   (interactive)
7501   (prog1
7502       (when (gnus-summary-first-subject)
7503         (gnus-summary-show-thread)
7504         (gnus-summary-first-subject)
7505         (gnus-summary-display-article (gnus-summary-article-number)))
7506     (gnus-summary-position-point)))
7507
7508 (defun gnus-summary-best-unread-article (&optional arg)
7509   "Select the unread article with the highest score.
7510 If given a prefix argument, select the next unread article that has a
7511 score higher than the default score."
7512   (interactive "P")
7513   (let ((article (if arg
7514                      (gnus-summary-better-unread-subject)
7515                    (gnus-summary-best-unread-subject))))
7516     (if article
7517         (gnus-summary-goto-article article)
7518       (error "No unread articles"))))
7519
7520 (defun gnus-summary-best-unread-subject ()
7521   "Select the unread subject with the highest score."
7522   (interactive)
7523   (let ((best -1000000)
7524         (data gnus-newsgroup-data)
7525         article score)
7526     (while data
7527       (and (gnus-data-unread-p (car data))
7528            (> (setq score
7529                     (gnus-summary-article-score (gnus-data-number (car data))))
7530               best)
7531            (setq best score
7532                  article (gnus-data-number (car data))))
7533       (setq data (cdr data)))
7534     (when article
7535       (gnus-summary-goto-subject article))
7536     (gnus-summary-position-point)
7537     article))
7538
7539 (defun gnus-summary-better-unread-subject ()
7540   "Select the first unread subject that has a score over the default score."
7541   (interactive)
7542   (let ((data gnus-newsgroup-data)
7543         article score)
7544     (while (and (setq article (gnus-data-number (car data)))
7545                 (or (gnus-data-read-p (car data))
7546                     (not (> (gnus-summary-article-score article)
7547                             gnus-summary-default-score))))
7548       (setq data (cdr data)))
7549     (when article
7550       (gnus-summary-goto-subject article))
7551     (gnus-summary-position-point)
7552     article))
7553
7554 (defun gnus-summary-last-subject ()
7555   "Go to the last displayed subject line in the group."
7556   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7557     (when article
7558       (gnus-summary-goto-subject article))))
7559
7560 (defun gnus-summary-goto-article (article &optional all-headers force)
7561   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7562 If ALL-HEADERS is non-nil, no header lines are hidden.
7563 If FORCE, go to the article even if it isn't displayed.  If FORCE
7564 is a number, it is the line the article is to be displayed on."
7565   (interactive
7566    (list
7567     (completing-read
7568      "Article number or Message-ID: "
7569      (mapcar (lambda (number) (list (int-to-string number)))
7570              gnus-newsgroup-limit))
7571     current-prefix-arg
7572     t))
7573   (prog1
7574       (if (and (stringp article)
7575                (string-match "@\\|%40" article))
7576           (gnus-summary-refer-article article)
7577         (when (stringp article)
7578           (setq article (string-to-number article)))
7579         (if (gnus-summary-goto-subject article force)
7580             (gnus-summary-display-article article all-headers)
7581           (gnus-message 4 "Couldn't go to article %s" article) nil))
7582     (gnus-summary-position-point)))
7583
7584 (defun gnus-summary-goto-last-article ()
7585   "Go to the previously read article."
7586   (interactive)
7587   (prog1
7588       (when gnus-last-article
7589         (gnus-summary-goto-article gnus-last-article nil t))
7590     (gnus-summary-position-point)))
7591
7592 (defun gnus-summary-pop-article (number)
7593   "Pop one article off the history and go to the previous.
7594 NUMBER articles will be popped off."
7595   (interactive "p")
7596   (let (to)
7597     (setq gnus-newsgroup-history
7598           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7599     (if to
7600         (gnus-summary-goto-article (car to) nil t)
7601       (error "Article history empty")))
7602   (gnus-summary-position-point))
7603
7604 ;; Summary commands and functions for limiting the summary buffer.
7605
7606 (defun gnus-summary-limit-to-articles (n)
7607   "Limit the summary buffer to the next N articles.
7608 If not given a prefix, use the process marked articles instead."
7609   (interactive "P")
7610   (prog1
7611       (let ((articles (gnus-summary-work-articles n)))
7612         (setq gnus-newsgroup-processable nil)
7613         (gnus-summary-limit articles))
7614     (gnus-summary-position-point)))
7615
7616 (defun gnus-summary-pop-limit (&optional total)
7617   "Restore the previous limit.
7618 If given a prefix, remove all limits."
7619   (interactive "P")
7620   (when total
7621     (setq gnus-newsgroup-limits
7622           (list (mapcar (lambda (h) (mail-header-number h))
7623                         gnus-newsgroup-headers))))
7624   (unless gnus-newsgroup-limits
7625     (error "No limit to pop"))
7626   (prog1
7627       (gnus-summary-limit nil 'pop)
7628     (gnus-summary-position-point)))
7629
7630 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7631   "Limit the summary buffer to articles that have subjects that match a regexp.
7632 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7633   (interactive
7634    (list (read-string (if current-prefix-arg
7635                           "Exclude subject (regexp): "
7636                         "Limit to subject (regexp): "))
7637          nil current-prefix-arg))
7638   (unless header
7639     (setq header "subject"))
7640   (when (not (equal "" subject))
7641     (prog1
7642         (let ((articles (gnus-summary-find-matching
7643                          (or header "subject") subject 'all nil nil
7644                          not-matching)))
7645           (unless articles
7646             (error "Found no matches for \"%s\"" subject))
7647           (gnus-summary-limit articles))
7648       (gnus-summary-position-point))))
7649
7650 (defun gnus-summary-limit-to-author (from &optional not-matching)
7651   "Limit the summary buffer to articles that have authors that match a regexp.
7652 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7653   (interactive
7654    (list (read-string (if current-prefix-arg
7655                           "Exclude author (regexp): "
7656                         "Limit to author (regexp): "))
7657          current-prefix-arg))
7658   (gnus-summary-limit-to-subject from "from" not-matching))
7659
7660 (defun gnus-summary-limit-to-age (age &optional younger-p)
7661   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7662 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7663 articles that are younger than AGE days."
7664   (interactive
7665    (let ((younger current-prefix-arg)
7666          (days-got nil)
7667          days)
7668      (while (not days-got)
7669        (setq days (if younger
7670                       (read-string "Limit to articles younger than (in days, older when negative): ")
7671                     (read-string
7672                      "Limit to articles older than (in days, younger when negative): ")))
7673        (when (> (length days) 0)
7674          (setq days (read days)))
7675        (if (numberp days)
7676            (progn
7677              (setq days-got t)
7678              (when (< days 0)
7679                (setq younger (not younger))
7680                (setq days (* days -1))))
7681          (message "Please enter a number.")
7682          (sleep-for 1)))
7683      (list days younger)))
7684   (prog1
7685       (let ((data gnus-newsgroup-data)
7686             (cutoff (days-to-time age))
7687             articles d date is-younger)
7688         (while (setq d (pop data))
7689           (when (and (vectorp (gnus-data-header d))
7690                      (setq date (mail-header-date (gnus-data-header d))))
7691             (setq is-younger (time-less-p
7692                               (time-since (condition-case ()
7693                                               (date-to-time date)
7694                                             (error '(0 0))))
7695                               cutoff))
7696             (when (if younger-p
7697                       is-younger
7698                     (not is-younger))
7699               (push (gnus-data-number d) articles))))
7700         (gnus-summary-limit (nreverse articles)))
7701     (gnus-summary-position-point)))
7702
7703 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7704   "Limit the summary buffer to articles that match an 'extra' header."
7705   (interactive
7706    (let ((header
7707           (intern
7708            (gnus-completing-read-with-default
7709             (symbol-name (car gnus-extra-headers))
7710             (if current-prefix-arg
7711                 "Exclude extra header:"
7712               "Limit extra header:")
7713             (mapcar (lambda (x)
7714                       (cons (symbol-name x) x))
7715                     gnus-extra-headers)
7716             nil
7717             t))))
7718      (list header
7719            (read-string (format "%s header %s (regexp): "
7720                                 (if current-prefix-arg "Exclude" "Limit to")
7721                                 header))
7722            current-prefix-arg)))
7723   (when (not (equal "" regexp))
7724     (prog1
7725         (let ((articles (gnus-summary-find-matching
7726                          (cons 'extra header) regexp 'all nil nil
7727                          not-matching)))
7728           (unless articles
7729             (error "Found no matches for \"%s\"" regexp))
7730           (gnus-summary-limit articles))
7731       (gnus-summary-position-point))))
7732
7733 (defun gnus-summary-limit-to-display-predicate ()
7734   "Limit the summary buffer to the predicated in the `display' group parameter."
7735   (interactive)
7736   (unless gnus-newsgroup-display
7737     (error "There is no `display' group parameter"))
7738   (let (articles)
7739     (dolist (number gnus-newsgroup-articles)
7740       (when (funcall gnus-newsgroup-display)
7741         (push number articles)))
7742     (gnus-summary-limit articles))
7743   (gnus-summary-position-point))
7744
7745 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7746 (make-obsolete
7747  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7748
7749 (defun gnus-summary-limit-to-unread (&optional all)
7750   "Limit the summary buffer to articles that are not marked as read.
7751 If ALL is non-nil, limit strictly to unread articles."
7752   (interactive "P")
7753   (if all
7754       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7755     (gnus-summary-limit-to-marks
7756      ;; Concat all the marks that say that an article is read and have
7757      ;; those removed.
7758      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7759            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7760            gnus-low-score-mark gnus-expirable-mark
7761            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7762            gnus-duplicate-mark gnus-souped-mark)
7763      'reverse)))
7764
7765 (defun gnus-summary-limit-to-replied (&optional unreplied)
7766   "Limit the summary buffer to replied articles.
7767 If UNREPLIED (the prefix), limit to unreplied articles."
7768   (interactive "P")
7769   (if unreplied
7770       (gnus-summary-limit
7771        (gnus-set-difference gnus-newsgroup-articles
7772         gnus-newsgroup-replied))
7773     (gnus-summary-limit gnus-newsgroup-replied))
7774   (gnus-summary-position-point))
7775
7776 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7777 (make-obsolete 'gnus-summary-delete-marked-with
7778                'gnus-summary-limit-exclude-marks)
7779
7780 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7781   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7782 If REVERSE, limit the summary buffer to articles that are marked
7783 with MARKS.  MARKS can either be a string of marks or a list of marks.
7784 Returns how many articles were removed."
7785   (interactive "sMarks: ")
7786   (gnus-summary-limit-to-marks marks t))
7787
7788 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7789   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7790 If REVERSE (the prefix), limit the summary buffer to articles that are
7791 not marked with MARKS.  MARKS can either be a string of marks or a
7792 list of marks.
7793 Returns how many articles were removed."
7794   (interactive "sMarks: \nP")
7795   (prog1
7796       (let ((data gnus-newsgroup-data)
7797             (marks (if (listp marks) marks
7798                      (append marks nil))) ; Transform to list.
7799             articles)
7800         (while data
7801           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7802                   (memq (gnus-data-mark (car data)) marks))
7803             (push (gnus-data-number (car data)) articles))
7804           (setq data (cdr data)))
7805         (gnus-summary-limit articles))
7806     (gnus-summary-position-point)))
7807
7808 (defun gnus-summary-limit-to-score (score)
7809   "Limit to articles with score at or above SCORE."
7810   (interactive "NLimit to articles with score of at least: ")
7811   (let ((data gnus-newsgroup-data)
7812         articles)
7813     (while data
7814       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7815                 score)
7816         (push (gnus-data-number (car data)) articles))
7817       (setq data (cdr data)))
7818     (prog1
7819         (gnus-summary-limit articles)
7820       (gnus-summary-position-point))))
7821
7822 (defun gnus-summary-limit-to-unseen ()
7823   "Limit to unseen articles."
7824   (interactive)
7825   (prog1
7826       (gnus-summary-limit gnus-newsgroup-unseen)
7827     (gnus-summary-position-point)))
7828
7829 (defun gnus-summary-limit-include-thread (id)
7830   "Display all the hidden articles that is in the thread with ID in it.
7831 When called interactively, ID is the Message-ID of the current
7832 article."
7833   (interactive (list (mail-header-id (gnus-summary-article-header))))
7834   (let ((articles (gnus-articles-in-thread
7835                    (gnus-id-to-thread (gnus-root-id id)))))
7836     (prog1
7837         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7838       (gnus-summary-limit-include-matching-articles
7839        "subject"
7840        (regexp-quote (gnus-simplify-subject-re
7841                       (mail-header-subject (gnus-id-to-header id)))))
7842       (gnus-summary-position-point))))
7843
7844 (defun gnus-summary-limit-include-matching-articles (header regexp)
7845   "Display all the hidden articles that have HEADERs that match REGEXP."
7846   (interactive (list (read-string "Match on header: ")
7847                      (read-string "Regexp: ")))
7848   (let ((articles (gnus-find-matching-articles header regexp)))
7849     (prog1
7850         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7851       (gnus-summary-position-point))))
7852
7853 (defun gnus-summary-insert-dormant-articles ()
7854   "Insert all the dormant articles for this group into the current buffer."
7855   (interactive)
7856   (let ((gnus-verbose (max 6 gnus-verbose)))
7857     (if (not gnus-newsgroup-dormant)
7858         (gnus-message 3 "No cached articles for this group")
7859       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7860
7861 (defun gnus-summary-limit-include-dormant ()
7862   "Display all the hidden articles that are marked as dormant.
7863 Note that this command only works on a subset of the articles currently
7864 fetched for this group."
7865   (interactive)
7866   (unless gnus-newsgroup-dormant
7867     (error "There are no dormant articles in this group"))
7868   (prog1
7869       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7870     (gnus-summary-position-point)))
7871
7872 (defun gnus-summary-limit-exclude-dormant ()
7873   "Hide all dormant articles."
7874   (interactive)
7875   (prog1
7876       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7877     (gnus-summary-position-point)))
7878
7879 (defun gnus-summary-limit-exclude-childless-dormant ()
7880   "Hide all dormant articles that have no children."
7881   (interactive)
7882   (let ((data (gnus-data-list t))
7883         articles d children)
7884     ;; Find all articles that are either not dormant or have
7885     ;; children.
7886     (while (setq d (pop data))
7887       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7888                 (and (setq children
7889                            (gnus-article-children (gnus-data-number d)))
7890                      (let (found)
7891                        (while children
7892                          (when (memq (car children) articles)
7893                            (setq children nil
7894                                  found t))
7895                          (pop children))
7896                        found)))
7897         (push (gnus-data-number d) articles)))
7898     ;; Do the limiting.
7899     (prog1
7900         (gnus-summary-limit articles)
7901       (gnus-summary-position-point))))
7902
7903 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7904   "Mark all unread excluded articles as read.
7905 If ALL, mark even excluded ticked and dormants as read."
7906   (interactive "P")
7907   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7908   (let ((articles (gnus-sorted-ndifference
7909                    (sort
7910                     (mapcar (lambda (h) (mail-header-number h))
7911                             gnus-newsgroup-headers)
7912                     '<)
7913                    gnus-newsgroup-limit))
7914         article)
7915     (setq gnus-newsgroup-unreads
7916           (gnus-sorted-intersection gnus-newsgroup-unreads
7917                                     gnus-newsgroup-limit))
7918     (if all
7919         (setq gnus-newsgroup-dormant nil
7920               gnus-newsgroup-marked nil
7921               gnus-newsgroup-reads
7922               (nconc
7923                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7924                gnus-newsgroup-reads))
7925       (while (setq article (pop articles))
7926         (unless (or (memq article gnus-newsgroup-dormant)
7927                     (memq article gnus-newsgroup-marked))
7928           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7929
7930 (defun gnus-summary-limit (articles &optional pop)
7931   (if pop
7932       ;; We pop the previous limit off the stack and use that.
7933       (setq articles (car gnus-newsgroup-limits)
7934             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7935     ;; We use the new limit, so we push the old limit on the stack.
7936     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7937   ;; Set the limit.
7938   (setq gnus-newsgroup-limit articles)
7939   (let ((total (length gnus-newsgroup-data))
7940         (data (gnus-data-find-list (gnus-summary-article-number)))
7941         (gnus-summary-mark-below nil)   ; Inhibit this.
7942         found)
7943     ;; This will do all the work of generating the new summary buffer
7944     ;; according to the new limit.
7945     (gnus-summary-prepare)
7946     ;; Hide any threads, possibly.
7947     (gnus-summary-maybe-hide-threads)
7948     ;; Try to return to the article you were at, or one in the
7949     ;; neighborhood.
7950     (when data
7951       ;; We try to find some article after the current one.
7952       (while data
7953         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7954           (setq data nil
7955                 found t))
7956         (setq data (cdr data))))
7957     (unless found
7958       ;; If there is no data, that means that we were after the last
7959       ;; article.  The same goes when we can't find any articles
7960       ;; after the current one.
7961       (goto-char (point-max))
7962       (gnus-summary-find-prev))
7963     (gnus-set-mode-line 'summary)
7964     ;; We return how many articles were removed from the summary
7965     ;; buffer as a result of the new limit.
7966     (- total (length gnus-newsgroup-data))))
7967
7968 (defsubst gnus-invisible-cut-children (threads)
7969   (let ((num 0))
7970     (while threads
7971       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7972         (incf num))
7973       (pop threads))
7974     (< num 2)))
7975
7976 (defsubst gnus-cut-thread (thread)
7977   "Go forwards in the thread until we find an article that we want to display."
7978   (when (or (eq gnus-fetch-old-headers 'some)
7979             (eq gnus-fetch-old-headers 'invisible)
7980             (numberp gnus-fetch-old-headers)
7981             (eq gnus-build-sparse-threads 'some)
7982             (eq gnus-build-sparse-threads 'more))
7983     ;; Deal with old-fetched headers and sparse threads.
7984     (while (and
7985             thread
7986             (or
7987              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7988              (gnus-summary-article-ancient-p
7989               (mail-header-number (car thread))))
7990             (if (or (<= (length (cdr thread)) 1)
7991                     (eq gnus-fetch-old-headers 'invisible))
7992                 (setq gnus-newsgroup-limit
7993                       (delq (mail-header-number (car thread))
7994                             gnus-newsgroup-limit)
7995                       thread (cadr thread))
7996               (when (gnus-invisible-cut-children (cdr thread))
7997                 (let ((th (cdr thread)))
7998                   (while th
7999                     (if (memq (mail-header-number (caar th))
8000                               gnus-newsgroup-limit)
8001                         (setq thread (car th)
8002                               th nil)
8003                       (setq th (cdr th))))))))))
8004   thread)
8005
8006 (defun gnus-cut-threads (threads)
8007   "Cut off all uninteresting articles from the beginning of THREADS."
8008   (when (or (eq gnus-fetch-old-headers 'some)
8009             (eq gnus-fetch-old-headers 'invisible)
8010             (numberp gnus-fetch-old-headers)
8011             (eq gnus-build-sparse-threads 'some)
8012             (eq gnus-build-sparse-threads 'more))
8013     (let ((th threads))
8014       (while th
8015         (setcar th (gnus-cut-thread (car th)))
8016         (setq th (cdr th)))))
8017   ;; Remove nixed out threads.
8018   (delq nil threads))
8019
8020 (defun gnus-summary-initial-limit (&optional show-if-empty)
8021   "Figure out what the initial limit is supposed to be on group entry.
8022 This entails weeding out unwanted dormants, low-scored articles,
8023 fetch-old-headers verbiage, and so on."
8024   ;; Most groups have nothing to remove.
8025   (if (or gnus-inhibit-limiting
8026           (and (null gnus-newsgroup-dormant)
8027                (eq gnus-newsgroup-display 'gnus-not-ignore)
8028                (not (eq gnus-fetch-old-headers 'some))
8029                (not (numberp gnus-fetch-old-headers))
8030                (not (eq gnus-fetch-old-headers 'invisible))
8031                (null gnus-summary-expunge-below)
8032                (not (eq gnus-build-sparse-threads 'some))
8033                (not (eq gnus-build-sparse-threads 'more))
8034                (null gnus-thread-expunge-below)
8035                (not gnus-use-nocem)))
8036       ()                                ; Do nothing.
8037     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8038     (setq gnus-newsgroup-limit nil)
8039     (mapatoms
8040      (lambda (node)
8041        (unless (car (symbol-value node))
8042          ;; These threads have no parents -- they are roots.
8043          (let ((nodes (cdr (symbol-value node)))
8044                thread)
8045            (while nodes
8046              (if (and gnus-thread-expunge-below
8047                       (< (gnus-thread-total-score (car nodes))
8048                          gnus-thread-expunge-below))
8049                  (gnus-expunge-thread (pop nodes))
8050                (setq thread (pop nodes))
8051                (gnus-summary-limit-children thread))))))
8052      gnus-newsgroup-dependencies)
8053     ;; If this limitation resulted in an empty group, we might
8054     ;; pop the previous limit and use it instead.
8055     (when (and (not gnus-newsgroup-limit)
8056                show-if-empty)
8057       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8058     gnus-newsgroup-limit))
8059
8060 (defun gnus-summary-limit-children (thread)
8061   "Return 1 if this subthread is visible and 0 if it is not."
8062   ;; First we get the number of visible children to this thread.  This
8063   ;; is done by recursing down the thread using this function, so this
8064   ;; will really go down to a leaf article first, before slowly
8065   ;; working its way up towards the root.
8066   (when thread
8067     (let* ((max-lisp-eval-depth 5000)
8068            (children
8069            (if (cdr thread)
8070                (apply '+ (mapcar 'gnus-summary-limit-children
8071                                  (cdr thread)))
8072              0))
8073           (number (mail-header-number (car thread)))
8074           score)
8075       (if (and
8076            (not (memq number gnus-newsgroup-marked))
8077            (or
8078             ;; If this article is dormant and has absolutely no visible
8079             ;; children, then this article isn't visible.
8080             (and (memq number gnus-newsgroup-dormant)
8081                  (zerop children))
8082             ;; If this is "fetch-old-headered" and there is no
8083             ;; visible children, then we don't want this article.
8084             (and (or (eq gnus-fetch-old-headers 'some)
8085                      (numberp gnus-fetch-old-headers))
8086                  (gnus-summary-article-ancient-p number)
8087                  (zerop children))
8088             ;; If this is "fetch-old-headered" and `invisible', then
8089             ;; we don't want this article.
8090             (and (eq gnus-fetch-old-headers 'invisible)
8091                  (gnus-summary-article-ancient-p number))
8092             ;; If this is a sparsely inserted article with no children,
8093             ;; we don't want it.
8094             (and (eq gnus-build-sparse-threads 'some)
8095                  (gnus-summary-article-sparse-p number)
8096                  (zerop children))
8097             ;; If we use expunging, and this article is really
8098             ;; low-scored, then we don't want this article.
8099             (when (and gnus-summary-expunge-below
8100                        (< (setq score
8101                                 (or (cdr (assq number gnus-newsgroup-scored))
8102                                     gnus-summary-default-score))
8103                           gnus-summary-expunge-below))
8104               ;; We increase the expunge-tally here, but that has
8105               ;; nothing to do with the limits, really.
8106               (incf gnus-newsgroup-expunged-tally)
8107               ;; We also mark as read here, if that's wanted.
8108               (when (and gnus-summary-mark-below
8109                          (< score gnus-summary-mark-below))
8110                 (setq gnus-newsgroup-unreads
8111                       (delq number gnus-newsgroup-unreads))
8112                 (if gnus-newsgroup-auto-expire
8113                     (push number gnus-newsgroup-expirable)
8114                   (push (cons number gnus-low-score-mark)
8115                         gnus-newsgroup-reads)))
8116               t)
8117             ;; Do the `display' group parameter.
8118             (and gnus-newsgroup-display
8119                  (not (funcall gnus-newsgroup-display)))
8120             ;; Check NoCeM things.
8121             (when (and gnus-use-nocem
8122                        (gnus-nocem-unwanted-article-p
8123                         (mail-header-id (car thread))))
8124               (setq gnus-newsgroup-unreads
8125                     (delq number gnus-newsgroup-unreads))
8126               t)))
8127           ;; Nope, invisible article.
8128           0
8129         ;; Ok, this article is to be visible, so we add it to the limit
8130         ;; and return 1.
8131         (push number gnus-newsgroup-limit)
8132         1))))
8133
8134 (defun gnus-expunge-thread (thread)
8135   "Mark all articles in THREAD as read."
8136   (let* ((number (mail-header-number (car thread))))
8137     (incf gnus-newsgroup-expunged-tally)
8138     ;; We also mark as read here, if that's wanted.
8139     (setq gnus-newsgroup-unreads
8140           (delq number gnus-newsgroup-unreads))
8141     (if gnus-newsgroup-auto-expire
8142         (push number gnus-newsgroup-expirable)
8143       (push (cons number gnus-low-score-mark)
8144             gnus-newsgroup-reads)))
8145   ;; Go recursively through all subthreads.
8146   (mapcar 'gnus-expunge-thread (cdr thread)))
8147
8148 ;; Summary article oriented commands
8149
8150 (defun gnus-summary-refer-parent-article (n)
8151   "Refer parent article N times.
8152 If N is negative, go to ancestor -N instead.
8153 The difference between N and the number of articles fetched is returned."
8154   (interactive "p")
8155   (let ((skip 1)
8156         error header ref)
8157     (when (not (natnump n))
8158       (setq skip (abs n)
8159             n 1))
8160     (while (and (> n 0)
8161                 (not error))
8162       (setq header (gnus-summary-article-header))
8163       (if (and (eq (mail-header-number header)
8164                    (cdr gnus-article-current))
8165                (equal gnus-newsgroup-name
8166                       (car gnus-article-current)))
8167           ;; If we try to find the parent of the currently
8168           ;; displayed article, then we take a look at the actual
8169           ;; References header, since this is slightly more
8170           ;; reliable than the References field we got from the
8171           ;; server.
8172           (save-excursion
8173             (set-buffer gnus-original-article-buffer)
8174             (nnheader-narrow-to-headers)
8175             (unless (setq ref (message-fetch-field "references"))
8176               (when (setq ref (message-fetch-field "in-reply-to"))
8177                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8178             (widen))
8179         (setq ref
8180               ;; It's not the current article, so we take a bet on
8181               ;; the value we got from the server.
8182               (mail-header-references header)))
8183       (if (and ref
8184                (not (equal ref "")))
8185           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8186             (gnus-message 1 "Couldn't find parent"))
8187         (gnus-message 1 "No references in article %d"
8188                       (gnus-summary-article-number))
8189         (setq error t))
8190       (decf n))
8191     (gnus-summary-position-point)
8192     n))
8193
8194 (defun gnus-summary-refer-references ()
8195   "Fetch all articles mentioned in the References header.
8196 Return the number of articles fetched."
8197   (interactive)
8198   (let ((ref (mail-header-references (gnus-summary-article-header)))
8199         (current (gnus-summary-article-number))
8200         (n 0))
8201     (if (or (not ref)
8202             (equal ref ""))
8203         (error "No References in the current article")
8204       ;; For each Message-ID in the References header...
8205       (while (string-match "<[^>]*>" ref)
8206         (incf n)
8207         ;; ... fetch that article.
8208         (gnus-summary-refer-article
8209          (prog1 (match-string 0 ref)
8210            (setq ref (substring ref (match-end 0))))))
8211       (gnus-summary-goto-subject current)
8212       (gnus-summary-position-point)
8213       n)))
8214
8215 (defun gnus-summary-refer-thread (&optional limit)
8216   "Fetch all articles in the current thread.
8217 If LIMIT (the numerical prefix), fetch that many old headers instead
8218 of what's specified by the `gnus-refer-thread-limit' variable."
8219   (interactive "P")
8220   (let ((id (mail-header-id (gnus-summary-article-header)))
8221         (limit (if limit (prefix-numeric-value limit)
8222                  gnus-refer-thread-limit)))
8223     (unless (eq gnus-fetch-old-headers 'invisible)
8224       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8225       ;; Retrieve the headers and read them in.
8226       (if (eq (if (numberp limit)
8227                   (gnus-retrieve-headers
8228                    (list (min
8229                           (+ (mail-header-number
8230                               (gnus-summary-article-header))
8231                              limit)
8232                           gnus-newsgroup-end))
8233                    gnus-newsgroup-name (* limit 2))
8234                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8235                 ;; headers.
8236                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8237                                        gnus-newsgroup-name limit))
8238               'nov)
8239           (gnus-build-all-threads)
8240         (error "Can't fetch thread from back ends that don't support NOV"))
8241       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8242     (gnus-summary-limit-include-thread id)))
8243
8244 (defun gnus-summary-refer-article (message-id)
8245   "Fetch an article specified by MESSAGE-ID."
8246   (interactive "sMessage-ID: ")
8247   (when (and (stringp message-id)
8248              (not (zerop (length message-id))))
8249     (setq message-id (gnus-replace-in-string message-id " " ""))
8250     ;; Construct the correct Message-ID if necessary.
8251     ;; Suggested by tale@pawl.rpi.edu.
8252     (unless (string-match "^<" message-id)
8253       (setq message-id (concat "<" message-id)))
8254     (unless (string-match ">$" message-id)
8255       (setq message-id (concat message-id ">")))
8256     ;; People often post MIDs from URLs, so unhex it:
8257     (unless (string-match "@" message-id)
8258       (setq message-id (gnus-url-unhex-string message-id)))
8259     (let* ((header (gnus-id-to-header message-id))
8260            (sparse (and header
8261                         (gnus-summary-article-sparse-p
8262                          (mail-header-number header))
8263                         (memq (mail-header-number header)
8264                               gnus-newsgroup-limit)))
8265            number)
8266       (cond
8267        ;; If the article is present in the buffer we just go to it.
8268        ((and header
8269              (or (not (gnus-summary-article-sparse-p
8270                        (mail-header-number header)))
8271                  sparse))
8272         (prog1
8273             (gnus-summary-goto-article
8274              (mail-header-number header) nil t)
8275           (when sparse
8276             (gnus-summary-update-article (mail-header-number header)))))
8277        (t
8278         ;; We fetch the article.
8279         (catch 'found
8280           (dolist (gnus-override-method (gnus-refer-article-methods))
8281             (when (and (gnus-check-server gnus-override-method)
8282                        ;; Fetch the header,
8283                        (setq number (gnus-summary-insert-subject message-id)))
8284               ;; and display the article.
8285               (gnus-summary-select-article nil nil nil number)
8286               (throw 'found t)))
8287           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8288
8289 (defun gnus-refer-article-methods ()
8290   "Return a list of referable methods."
8291   (cond
8292    ;; No method, so we default to current and native.
8293    ((null gnus-refer-article-method)
8294     (list gnus-current-select-method gnus-select-method))
8295    ;; Current.
8296    ((eq 'current gnus-refer-article-method)
8297     (list gnus-current-select-method))
8298    ;; List of select methods.
8299    ((not (and (symbolp (car gnus-refer-article-method))
8300               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8301     (let (out)
8302       (dolist (method gnus-refer-article-method)
8303         (push (if (eq 'current method)
8304                   gnus-current-select-method
8305                 method)
8306               out))
8307       (nreverse out)))
8308    ;; One single select method.
8309    (t
8310     (list gnus-refer-article-method))))
8311
8312 (defun gnus-summary-edit-parameters ()
8313   "Edit the group parameters of the current group."
8314   (interactive)
8315   (gnus-group-edit-group gnus-newsgroup-name 'params))
8316
8317 (defun gnus-summary-customize-parameters ()
8318   "Customize the group parameters of the current group."
8319   (interactive)
8320   (gnus-group-customize gnus-newsgroup-name))
8321
8322 (defun gnus-summary-enter-digest-group (&optional force)
8323   "Enter an nndoc group based on the current article.
8324 If FORCE, force a digest interpretation.  If not, try
8325 to guess what the document format is."
8326   (interactive "P")
8327   (let ((conf gnus-current-window-configuration))
8328     (save-window-excursion
8329       (save-excursion
8330         (let (gnus-article-prepare-hook
8331               gnus-display-mime-function
8332               gnus-break-pages)
8333           (gnus-summary-select-article))))
8334     (setq gnus-current-window-configuration conf)
8335     (let* ((name (format "%s-%d"
8336                          (gnus-group-prefixed-name
8337                           gnus-newsgroup-name (list 'nndoc ""))
8338                          (with-current-buffer gnus-summary-buffer
8339                            gnus-current-article)))
8340            (ogroup gnus-newsgroup-name)
8341            (params (append (gnus-info-params (gnus-get-info ogroup))
8342                            (list (cons 'to-group ogroup))
8343                            (list (cons 'parent-group ogroup))
8344                            (list (cons 'save-article-group ogroup))))
8345            (case-fold-search t)
8346            (buf (current-buffer))
8347            dig to-address)
8348       (save-excursion
8349         (set-buffer gnus-original-article-buffer)
8350         ;; Have the digest group inherit the main mail address of
8351         ;; the parent article.
8352         (when (setq to-address (or (gnus-fetch-field "reply-to")
8353                                    (gnus-fetch-field "from")))
8354           (setq params (append
8355                         (list (cons 'to-address
8356                                     (funcall gnus-decode-encoded-word-function
8357                                              to-address))))))
8358         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8359         (insert-buffer-substring gnus-original-article-buffer)
8360         ;; Remove lines that may lead nndoc to misinterpret the
8361         ;; document type.
8362         (narrow-to-region
8363          (goto-char (point-min))
8364          (or (search-forward "\n\n" nil t) (point)))
8365         (goto-char (point-min))
8366         (delete-matching-lines "^Path:\\|^From ")
8367         (widen))
8368       (unwind-protect
8369           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8370                     (gnus-newsgroup-ephemeral-ignored-charsets
8371                      gnus-newsgroup-ignored-charsets))
8372                 (gnus-group-read-ephemeral-group
8373                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8374                               (nndoc-article-type
8375                                ,(if force 'mbox 'guess)))
8376                  t nil nil nil
8377                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8378                                                         "ADAPT")))))
8379               ;; Make all postings to this group go to the parent group.
8380               (nconc (gnus-info-params (gnus-get-info name))
8381                      params)
8382             ;; Couldn't select this doc group.
8383             (switch-to-buffer buf)
8384             (gnus-set-global-variables)
8385             (gnus-configure-windows 'summary)
8386             (gnus-message 3 "Article couldn't be entered?"))
8387         (kill-buffer dig)))))
8388
8389 (defun gnus-summary-read-document (n)
8390   "Open a new group based on the current article(s).
8391 This will allow you to read digests and other similar
8392 documents as newsgroups.
8393 Obeys the standard process/prefix convention."
8394   (interactive "P")
8395   (let* ((articles (gnus-summary-work-articles n))
8396          (ogroup gnus-newsgroup-name)
8397          (params (append (gnus-info-params (gnus-get-info ogroup))
8398                          (list (cons 'to-group ogroup))))
8399          article group egroup groups vgroup)
8400     (while (setq article (pop articles))
8401       (setq group (format "%s-%d" gnus-newsgroup-name article))
8402       (gnus-summary-remove-process-mark article)
8403       (when (gnus-summary-display-article article)
8404         (save-excursion
8405           (with-temp-buffer
8406             (insert-buffer-substring gnus-original-article-buffer)
8407             ;; Remove some headers that may lead nndoc to make
8408             ;; the wrong guess.
8409             (message-narrow-to-head)
8410             (goto-char (point-min))
8411             (delete-matching-lines "^Path:\\|^From ")
8412             (widen)
8413             (if (setq egroup
8414                       (gnus-group-read-ephemeral-group
8415                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8416                                      (nndoc-article-type guess))
8417                        t nil t))
8418                 (progn
8419                   ;; Make all postings to this group go to the parent group.
8420                   (nconc (gnus-info-params (gnus-get-info egroup))
8421                          params)
8422                   (push egroup groups))
8423               ;; Couldn't select this doc group.
8424               (gnus-error 3 "Article couldn't be entered"))))))
8425     ;; Now we have selected all the documents.
8426     (cond
8427      ((not groups)
8428       (error "None of the articles could be interpreted as documents"))
8429      ((gnus-group-read-ephemeral-group
8430        (setq vgroup (format
8431                      "nnvirtual:%s-%s" gnus-newsgroup-name
8432                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8433        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8434        t
8435        (cons (current-buffer) 'summary)))
8436      (t
8437       (error "Couldn't select virtual nndoc group")))))
8438
8439 (defun gnus-summary-isearch-article (&optional regexp-p)
8440   "Do incremental search forward on the current article.
8441 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8442   (interactive "P")
8443   (let* ((gnus-inhibit-treatment t)
8444          (old (gnus-summary-select-article)))
8445     (gnus-configure-windows 'article)
8446     (gnus-eval-in-buffer-window gnus-article-buffer
8447       (save-restriction
8448         (widen)
8449         (when (eq 'old old)
8450           (gnus-article-show-all-headers))
8451         (goto-char (point-min))
8452         (isearch-forward regexp-p)))))
8453
8454 (defun gnus-summary-search-article-forward (regexp &optional backward)
8455   "Search for an article containing REGEXP forward.
8456 If BACKWARD, search backward instead."
8457   (interactive
8458    (list (read-string
8459           (format "Search article %s (regexp%s): "
8460                   (if current-prefix-arg "backward" "forward")
8461                   (if gnus-last-search-regexp
8462                       (concat ", default " gnus-last-search-regexp)
8463                     "")))
8464          current-prefix-arg))
8465   (if (string-equal regexp "")
8466       (setq regexp (or gnus-last-search-regexp ""))
8467     (setq gnus-last-search-regexp regexp)
8468     (setq gnus-article-before-search gnus-current-article))
8469   ;; Intentionally set gnus-last-article.
8470   (setq gnus-last-article gnus-article-before-search)
8471   (let ((gnus-last-article gnus-last-article))
8472     (if (gnus-summary-search-article regexp backward)
8473         (gnus-summary-show-thread)
8474       (signal 'search-failed (list regexp)))))
8475
8476 (defun gnus-summary-search-article-backward (regexp)
8477   "Search for an article containing REGEXP backward."
8478   (interactive
8479    (list (read-string
8480           (format "Search article backward (regexp%s): "
8481                   (if gnus-last-search-regexp
8482                       (concat ", default " gnus-last-search-regexp)
8483                     "")))))
8484   (gnus-summary-search-article-forward regexp 'backward))
8485
8486 (eval-when-compile
8487   (defmacro gnus-summary-search-article-position-point (regexp backward)
8488     "Dehighlight the last matched text and goto the beginning position."
8489     (` (if (and gnus-summary-search-article-matched-data
8490                 (let ((text (caddr gnus-summary-search-article-matched-data))
8491                       (inhibit-read-only t)
8492                       buffer-read-only)
8493                   (delete-region
8494                    (goto-char (car gnus-summary-search-article-matched-data))
8495                    (cadr gnus-summary-search-article-matched-data))
8496                   (insert text)
8497                   (string-match (, regexp) text)))
8498            (if (, backward) (beginning-of-line) (end-of-line))
8499          (goto-char (if (, backward) (point-max) (point-min))))))
8500
8501   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8502     "Place point where X-Face image is displayed."
8503     (if (featurep 'xemacs)
8504         (` (let ((end (if (search-forward "\n\n" nil t)
8505                           (goto-char (1- (point)))
8506                         (point-min)))
8507                  extent)
8508              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8509              (unless (and (re-search-forward "^From:" end t)
8510                           (setq extent (extent-at (point)))
8511                           (extent-begin-glyph extent))
8512                (goto-char (, opoint)))))
8513       (` (let ((end (if (search-forward "\n\n" nil t)
8514                         (goto-char (1- (point)))
8515                       (point-min)))
8516                (start (or (search-backward "\n\n" nil t) (point-min))))
8517            (goto-char
8518             (or (text-property-any start end 'x-face-image t);; x-face-e21
8519                 (text-property-any start end 'x-face-mule-bitmap-image t)
8520                 (, opoint)))))))
8521
8522   (defmacro gnus-summary-search-article-highlight-matched-text
8523     (backward treated x-face)
8524     "Highlight matched text in the function `gnus-summary-search-article'."
8525     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8526              (end (set-marker (make-marker) (match-end 0)))
8527              (inhibit-read-only t)
8528              buffer-read-only)
8529          (unless treated
8530            (let ((,@
8531                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8532                     (mapcar
8533                      (lambda (item) (setq items (delq item items)))
8534                      '(gnus-treat-buttonize
8535                        gnus-treat-fill-article
8536                        gnus-treat-fill-long-lines
8537                        gnus-treat-emphasize
8538                        gnus-treat-highlight-headers
8539                        gnus-treat-highlight-citation
8540                        gnus-treat-highlight-signature
8541                        gnus-treat-overstrike
8542                        gnus-treat-display-x-face
8543                        gnus-treat-buttonize-head
8544                        gnus-treat-decode-article-as-default-mime-charset))
8545                     (static-if (featurep 'xemacs)
8546                         items
8547                       (cons '(x-face-mule-delete-x-face-field
8548                               (quote never))
8549                             items))))
8550                  (gnus-treat-display-x-face
8551                   (when (, x-face) gnus-treat-display-x-face)))
8552              (gnus-article-prepare-mime-display)))
8553          (goto-char (if (, backward) start end))
8554          (when (, x-face)
8555            (gnus-summary-search-article-highlight-goto-x-face (point)))
8556          (setq gnus-summary-search-article-matched-data
8557                (list start end (buffer-substring start end)))
8558          (unless (eq start end);; matched text has been deleted. :-<
8559            (put-text-property start end 'face
8560                               (or (find-face 'isearch)
8561                                   'secondary-selection))))))
8562   )
8563
8564 (defun gnus-summary-search-article (regexp &optional backward)
8565   "Search for an article containing REGEXP.
8566 Optional argument BACKWARD means do search for backward.
8567 `gnus-select-article-hook' is not called during the search."
8568   ;; We have to require this here to make sure that the following
8569   ;; dynamic binding isn't shadowed by autoloading.
8570   (require 'gnus-async)
8571   (require 'gnus-art)
8572   (let ((gnus-select-article-hook nil)  ;Disable hook.
8573         (gnus-article-prepare-hook nil)
8574         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8575         (gnus-use-article-prefetch nil)
8576         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8577         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8578         (gnus-visual nil)
8579         (gnus-keep-backlog nil)
8580         (gnus-break-pages nil)
8581         (gnus-summary-display-arrow nil)
8582         (gnus-updated-mode-lines nil)
8583         (gnus-auto-center-summary nil)
8584         (sum (current-buffer))
8585         (found nil)
8586         point treated)
8587     (gnus-save-hidden-threads
8588       (static-if (featurep 'xemacs)
8589           (let ((gnus-inhibit-treatment t))
8590             (setq treated (eq 'old (gnus-summary-select-article)))
8591             (when (and treated
8592                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8593                                  (window-live-p (get-buffer-window
8594                                                  gnus-article-buffer t)))))
8595               (gnus-summary-select-article nil t)
8596               (setq treated nil)))
8597         (let ((gnus-inhibit-treatment t)
8598               (x-face-mule-delete-x-face-field 'never))
8599           (setq treated (eq 'old (gnus-summary-select-article)))
8600           (when (and treated
8601                      (not
8602                       (and (gnus-buffer-live-p gnus-article-buffer)
8603                            (window-live-p (get-buffer-window
8604                                            gnus-article-buffer t))
8605                            (or (not (string-match "^\\^X-Face:" regexp))
8606                                (with-current-buffer gnus-article-buffer
8607                                  gnus-summary-search-article-matched-data)))))
8608             (gnus-summary-select-article nil t)
8609             (setq treated nil))))
8610       (set-buffer gnus-article-buffer)
8611       (widen)
8612       (if treated
8613           (progn
8614             (gnus-article-show-all-headers)
8615             (gnus-summary-search-article-position-point regexp backward))
8616         (goto-char (if backward (point-max) (point-min))))
8617       (while (not found)
8618         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8619         (if (if backward
8620                 (re-search-backward regexp nil t)
8621               (re-search-forward regexp nil t))
8622             ;; We found the regexp.
8623             (progn
8624               (gnus-summary-search-article-highlight-matched-text
8625                backward treated (string-match "^\\^X-Face:" regexp))
8626               (setq found 'found)
8627               (forward-line
8628                (/ (- 2 (window-height
8629                         (get-buffer-window gnus-article-buffer t)))
8630                   2))
8631               (set-window-start
8632                (get-buffer-window (current-buffer))
8633                (point))
8634               (set-buffer sum)
8635               (setq point (point)))
8636           ;; We didn't find it, so we go to the next article.
8637           (set-buffer sum)
8638           (setq found 'not)
8639           (while (eq found 'not)
8640             (if (not (if backward (gnus-summary-find-prev)
8641                        (gnus-summary-find-next)))
8642                 ;; No more articles.
8643                 (setq found t)
8644               ;; Select the next article and adjust point.
8645               (unless (gnus-summary-article-sparse-p
8646                        (gnus-summary-article-number))
8647                 (setq found nil)
8648                 (let ((gnus-inhibit-treatment t))
8649                   (gnus-summary-select-article))
8650                 (setq treated nil)
8651                 (set-buffer gnus-article-buffer)
8652                 (widen)
8653                 (goto-char (if backward (point-max) (point-min))))))))
8654       (gnus-message 7 ""))
8655     ;; Return whether we found the regexp.
8656     (when (eq found 'found)
8657       (goto-char point)
8658       (gnus-summary-show-thread)
8659       (gnus-summary-goto-subject gnus-current-article)
8660       (gnus-summary-position-point)
8661       t)))
8662
8663 (defun gnus-find-matching-articles (header regexp)
8664   "Return a list of all articles that match REGEXP on HEADER.
8665 This search includes all articles in the current group that Gnus has
8666 fetched headers for, whether they are displayed or not."
8667   (let ((articles nil)
8668         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8669         (case-fold-search t))
8670     (dolist (header gnus-newsgroup-headers)
8671       (when (string-match regexp (funcall func header))
8672         (push (mail-header-number header) articles)))
8673     (nreverse articles)))
8674
8675 (defun gnus-summary-find-matching (header regexp &optional backward unread
8676                                           not-case-fold not-matching)
8677   "Return a list of all articles that match REGEXP on HEADER.
8678 The search stars on the current article and goes forwards unless
8679 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8680 If UNREAD is non-nil, only unread articles will
8681 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8682 in the comparisons. If NOT-MATCHING, return a list of all articles that
8683 not match REGEXP on HEADER."
8684   (let ((case-fold-search (not not-case-fold))
8685         articles d func)
8686     (if (consp header)
8687         (if (eq (car header) 'extra)
8688             (setq func
8689                   `(lambda (h)
8690                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8691                          "")))
8692           (error "%s is an invalid header" header))
8693       (unless (fboundp (intern (concat "mail-header-" header)))
8694         (error "%s is not a valid header" header))
8695       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8696     (dolist (d (if (eq backward 'all)
8697                    gnus-newsgroup-data
8698                  (gnus-data-find-list
8699                   (gnus-summary-article-number)
8700                   (gnus-data-list backward))))
8701       (when (and (or (not unread)       ; We want all articles...
8702                      (gnus-data-unread-p d)) ; Or just unreads.
8703                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8704                  (if not-matching
8705                      (not (string-match
8706                            regexp
8707                            (funcall func (gnus-data-header d))))
8708                    (string-match regexp
8709                                  (funcall func (gnus-data-header d)))))
8710         (push (gnus-data-number d) articles))) ; Success!
8711     (nreverse articles)))
8712
8713 (defun gnus-summary-execute-command (header regexp command &optional backward)
8714   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8715 If HEADER is an empty string (or nil), the match is done on the entire
8716 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8717   (interactive
8718    (list (let ((completion-ignore-case t))
8719            (completing-read
8720             "Header name: "
8721             (mapcar (lambda (header) (list (format "%s" header)))
8722                     (append
8723                      '("Number" "Subject" "From" "Lines" "Date"
8724                        "Message-ID" "Xref" "References" "Body")
8725                      gnus-extra-headers))
8726             nil 'require-match))
8727          (read-string "Regexp: ")
8728          (read-key-sequence "Command: ")
8729          current-prefix-arg))
8730   (when (equal header "Body")
8731     (setq header ""))
8732   ;; Hidden thread subtrees must be searched as well.
8733   (gnus-summary-show-all-threads)
8734   ;; We don't want to change current point nor window configuration.
8735   (save-excursion
8736     (save-window-excursion
8737       (let (gnus-visual
8738             gnus-treat-strip-trailing-blank-lines
8739             gnus-treat-strip-leading-blank-lines
8740             gnus-treat-strip-multiple-blank-lines
8741             gnus-treat-hide-boring-headers
8742             gnus-treat-fold-newsgroups
8743             gnus-article-prepare-hook)
8744         (gnus-message 6 "Executing %s..." (key-description command))
8745         ;; We'd like to execute COMMAND interactively so as to give arguments.
8746         (gnus-execute header regexp
8747                       `(call-interactively ',(key-binding command))
8748                       backward)
8749         (gnus-message 6 "Executing %s...done" (key-description command))))))
8750
8751 (defun gnus-summary-beginning-of-article ()
8752   "Scroll the article back to the beginning."
8753   (interactive)
8754   (gnus-summary-select-article)
8755   (gnus-configure-windows 'article)
8756   (gnus-eval-in-buffer-window gnus-article-buffer
8757     (widen)
8758     (goto-char (point-min))
8759     (when gnus-break-pages
8760       (gnus-narrow-to-page))))
8761
8762 (defun gnus-summary-end-of-article ()
8763   "Scroll to the end of the article."
8764   (interactive)
8765   (gnus-summary-select-article)
8766   (gnus-configure-windows 'article)
8767   (gnus-eval-in-buffer-window gnus-article-buffer
8768     (widen)
8769     (goto-char (point-max))
8770     (recenter -3)
8771     (when gnus-break-pages
8772       (when (re-search-backward page-delimiter nil t)
8773         (narrow-to-region (match-end 0) (point-max)))
8774       (gnus-narrow-to-page))))
8775
8776 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8777   "Truncate to LEN and quote all \"(\"'s in STRING."
8778   (gnus-replace-in-string (if (and len (> (length string) len))
8779                               (substring string 0 len)
8780                             string)
8781                           "[()]" "\\\\\\&"))
8782
8783 (defun gnus-summary-print-article (&optional filename n)
8784   "Generate and print a PostScript image of the process-marked (mail) articles.
8785
8786 If used interactively, print the current article if none are
8787 process-marked.  With prefix arg, prompt the user for the name of the
8788 file to save in.
8789
8790 When used from Lisp, accept two optional args FILENAME and N.  N means
8791 to print the next N articles.  If N is negative, print the N previous
8792 articles.  If N is nil and articles have been marked with the process
8793 mark, print these instead.
8794
8795 If the optional first argument FILENAME is nil, send the image to the
8796 printer.  If FILENAME is a string, save the PostScript image in a file with
8797 that name.  If FILENAME is a number, prompt the user for the name of the file
8798 to save in."
8799   (interactive (list (ps-print-preprint current-prefix-arg)))
8800   (dolist (article (gnus-summary-work-articles n))
8801     (gnus-summary-select-article nil nil 'pseudo article)
8802     (gnus-eval-in-buffer-window gnus-article-buffer
8803       (gnus-print-buffer))
8804     (gnus-summary-remove-process-mark article))
8805   (ps-despool filename))
8806
8807 (defun gnus-print-buffer ()
8808   (let ((buffer (generate-new-buffer " *print*")))
8809     (unwind-protect
8810         (progn
8811           (copy-to-buffer buffer (point-min) (point-max))
8812           (set-buffer buffer)
8813           (gnus-remove-text-with-property 'gnus-decoration)
8814           (when (gnus-visual-p 'article-highlight 'highlight)
8815             ;; Copy-to-buffer doesn't copy overlay.  So redo
8816             ;; highlight.
8817             (let ((gnus-article-buffer buffer))
8818               (gnus-article-highlight-citation t)
8819               (gnus-article-highlight-signature)
8820               (gnus-article-emphasize)
8821               (gnus-article-delete-invisible-text)))
8822           (let ((ps-left-header
8823                  (list
8824                   (concat "("
8825                           (gnus-summary-print-truncate-and-quote
8826                            (mail-header-subject gnus-current-headers)
8827                            66) ")")
8828                   (concat "("
8829                           (gnus-summary-print-truncate-and-quote
8830                            (mail-header-from gnus-current-headers)
8831                            45) ")")))
8832                 (ps-right-header
8833                  (list
8834                   "/pagenumberstring load"
8835                   (concat "("
8836                           (mail-header-date gnus-current-headers) ")"))))
8837             (gnus-run-hooks 'gnus-ps-print-hook)
8838             (save-excursion
8839               (if window-system
8840                   (ps-spool-buffer-with-faces)
8841                 (ps-spool-buffer)))))
8842       (kill-buffer buffer))))
8843
8844 (defun gnus-summary-show-article (&optional arg)
8845   "Force redisplaying of the current article.
8846 If ARG (the prefix) is a number, show the article with the charset
8847 defined in `gnus-summary-show-article-charset-alist', or the charset
8848 input.
8849 If ARG (the prefix) is non-nil and not a number, show the raw article
8850 without any article massaging functions being run.  Normally, the key
8851 strokes are `C-u g'."
8852   (interactive "P")
8853   (cond
8854    ((numberp arg)
8855     (gnus-summary-show-article t)
8856     (let* ((gnus-newsgroup-charset
8857             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8858                 (mm-read-coding-system
8859                  "View as charset: " ;; actually it is coding system.
8860                  (save-excursion
8861                    (set-buffer gnus-article-buffer)
8862                    (mm-detect-coding-region (point) (point-max))))))
8863            (default-mime-charset gnus-newsgroup-charset)
8864            (gnus-newsgroup-ignored-charsets 'gnus-all))
8865       (gnus-summary-select-article nil 'force)
8866       (let ((deps gnus-newsgroup-dependencies)
8867             head header lines)
8868         (save-excursion
8869           (set-buffer gnus-original-article-buffer)
8870           (save-restriction
8871             (message-narrow-to-head)
8872             (setq head (buffer-string))
8873             (goto-char (point-min))
8874             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8875               (goto-char (point-max))
8876               (widen)
8877               (setq lines (1- (count-lines (point) (point-max))))))
8878           (with-temp-buffer
8879             (insert (format "211 %d Article retrieved.\n"
8880                             (cdr gnus-article-current)))
8881             (insert head)
8882             (if lines (insert (format "Lines: %d\n" lines)))
8883             (insert ".\n")
8884             (let ((nntp-server-buffer (current-buffer)))
8885               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8886         (gnus-data-set-header
8887          (gnus-data-find (cdr gnus-article-current))
8888          header)
8889         (gnus-summary-update-article-line
8890          (cdr gnus-article-current) header)
8891         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8892           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8893    ((not arg)
8894     ;; Select the article the normal way.
8895     (gnus-summary-select-article nil 'force))
8896    (t
8897     ;; We have to require this here to make sure that the following
8898     ;; dynamic binding isn't shadowed by autoloading.
8899     (require 'gnus-async)
8900     (require 'gnus-art)
8901     ;; Bind the article treatment functions to nil.
8902     (let ((gnus-have-all-headers t)
8903           gnus-article-prepare-hook
8904           gnus-article-decode-hook
8905           gnus-break-pages
8906           gnus-show-mime
8907           (gnus-inhibit-treatment t))
8908       (gnus-summary-select-article nil 'force))))
8909   (gnus-summary-goto-subject gnus-current-article)
8910   (gnus-summary-position-point))
8911
8912 (defun gnus-summary-show-raw-article ()
8913   "Show the raw article without any article massaging functions being run."
8914   (interactive)
8915   (gnus-summary-show-article t))
8916
8917 (defun gnus-summary-verbose-headers (&optional arg)
8918   "Toggle permanent full header display.
8919 If ARG is a positive number, turn header display on.
8920 If ARG is a negative number, turn header display off."
8921   (interactive "P")
8922   (setq gnus-show-all-headers
8923         (cond ((or (not (numberp arg))
8924                    (zerop arg))
8925                (not gnus-show-all-headers))
8926               ((natnump arg)
8927                t)))
8928   (gnus-summary-show-article))
8929
8930 (defun gnus-summary-toggle-header (&optional arg)
8931   "Show the headers if they are hidden, or hide them if they are shown.
8932 If ARG is a positive number, show the entire header.
8933 If ARG is a negative number, hide the unwanted header lines."
8934   (interactive "P")
8935   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8936                      (get-buffer-window gnus-article-buffer t))))
8937     (with-current-buffer gnus-article-buffer
8938       (widen)
8939       (article-narrow-to-head)
8940       (let* ((buffer-read-only nil)
8941              (inhibit-point-motion-hooks t)
8942              (hidden (if (numberp arg)
8943                          (>= arg 0)
8944                        (gnus-article-hidden-text-p 'headers)))
8945              s e)
8946         (delete-region (point-min) (point-max))
8947         (with-current-buffer gnus-original-article-buffer
8948           (goto-char (setq s (point-min)))
8949           (setq e (if (search-forward "\n\n" nil t)
8950                       (1- (point))
8951                     (point-max))))
8952         (insert-buffer-substring gnus-original-article-buffer s e)
8953         ;; In T-gnus, gnus-article-decode-hook doesn't contain
8954         ;; article-decode-encoded-words by default.
8955         (article-decode-encoded-words)
8956         (run-hooks 'gnus-article-decode-hook)
8957         (if hidden
8958             (let ((gnus-treat-hide-headers nil)
8959                   (gnus-treat-hide-boring-headers nil))
8960               (gnus-delete-wash-type 'headers)
8961               (gnus-treat-article 'head))
8962           (gnus-treat-article 'head))
8963         (widen)
8964         (if window
8965             (set-window-start window (goto-char (point-min))))
8966         (if gnus-break-pages
8967             (gnus-narrow-to-page)
8968           (when (gnus-visual-p 'page-marker)
8969             (let ((buffer-read-only nil))
8970               (gnus-remove-text-with-property 'gnus-prev)
8971               (gnus-remove-text-with-property 'gnus-next))))
8972         (gnus-set-mode-line 'article)))))
8973
8974 (defun gnus-summary-show-all-headers ()
8975   "Make all header lines visible."
8976   (interactive)
8977   (gnus-summary-toggle-header 1))
8978
8979 (defun gnus-summary-toggle-mime (&optional arg)
8980   "Toggle MIME processing.
8981 If ARG is a positive number, turn MIME processing on."
8982   (interactive "P")
8983   (setq gnus-show-mime
8984         (if (null arg)
8985             (not gnus-show-mime)
8986           (> (prefix-numeric-value arg) 0)))
8987   (gnus-summary-select-article t 'force))
8988
8989 (defun gnus-summary-caesar-message (&optional arg)
8990   "Caesar rotate the current article by 13.
8991 The numerical prefix specifies how many places to rotate each letter
8992 forward."
8993   (interactive "P")
8994   (gnus-summary-select-article)
8995   (let ((mail-header-separator ""))
8996     (gnus-eval-in-buffer-window gnus-article-buffer
8997       (save-restriction
8998         (widen)
8999         (let ((start (window-start))
9000               buffer-read-only)
9001           (message-caesar-buffer-body arg)
9002           (set-window-start (get-buffer-window (current-buffer)) start))))))
9003
9004 (autoload 'unmorse-region "morse"
9005   "Convert morse coded text in region to ordinary ASCII text."
9006   t)
9007
9008 (defun gnus-summary-morse-message (&optional arg)
9009   "Morse decode the current article."
9010   (interactive "P")
9011   (gnus-summary-select-article)
9012   (let ((mail-header-separator ""))
9013     (gnus-eval-in-buffer-window gnus-article-buffer
9014       (save-excursion
9015         (save-restriction
9016           (widen)
9017           (let ((pos (window-start))
9018                 buffer-read-only)
9019             (goto-char (point-min))
9020             (when (message-goto-body)
9021               (gnus-narrow-to-body))
9022             (goto-char (point-min))
9023             (while (search-forward "·" (point-max) t)
9024               (replace-match "."))
9025             (unmorse-region (point-min) (point-max))
9026             (widen)
9027             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9028
9029 (defun gnus-summary-stop-page-breaking ()
9030   "Stop page breaking in the current article."
9031   (interactive)
9032   (gnus-summary-select-article)
9033   (gnus-eval-in-buffer-window gnus-article-buffer
9034     (widen)
9035     (when (gnus-visual-p 'page-marker)
9036       (let ((buffer-read-only nil))
9037         (gnus-remove-text-with-property 'gnus-prev)
9038         (gnus-remove-text-with-property 'gnus-next))
9039       (setq gnus-page-broken nil))))
9040
9041 (defun gnus-summary-move-article (&optional n to-newsgroup
9042                                             select-method action)
9043   "Move the current article to a different newsgroup.
9044 If N is a positive number, move the N next articles.
9045 If N is a negative number, move the N previous articles.
9046 If N is nil and any articles have been marked with the process mark,
9047 move those articles instead.
9048 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9049 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9050 re-spool using this method.
9051
9052 When called interactively with TO-NEWSGROUP being nil, the value of
9053 the variable `gnus-move-split-methods' is used for finding a default
9054 for the target newsgroup.
9055
9056 For this function to work, both the current newsgroup and the
9057 newsgroup that you want to move to have to support the `request-move'
9058 and `request-accept' functions.
9059
9060 ACTION can be either `move' (the default), `crosspost' or `copy'."
9061   (interactive "P")
9062   (unless action
9063     (setq action 'move))
9064   ;; Check whether the source group supports the required functions.
9065   (cond ((and (eq action 'move)
9066               (not (gnus-check-backend-function
9067                     'request-move-article gnus-newsgroup-name)))
9068          (error "The current group does not support article moving"))
9069         ((and (eq action 'crosspost)
9070               (not (gnus-check-backend-function
9071                     'request-replace-article gnus-newsgroup-name)))
9072          (error "The current group does not support article editing")))
9073   (let ((articles (gnus-summary-work-articles n))
9074         (prefix (if (gnus-check-backend-function
9075                      'request-move-article gnus-newsgroup-name)
9076                     (funcall gnus-move-group-prefix-function
9077                              gnus-newsgroup-name)
9078                   ""))
9079         (names '((move "Move" "Moving")
9080                  (copy "Copy" "Copying")
9081                  (crosspost "Crosspost" "Crossposting")))
9082         (copy-buf (save-excursion
9083                     (nnheader-set-temp-buffer " *copy article*")))
9084         art-group to-method new-xref article to-groups)
9085     (unless (assq action names)
9086       (error "Unknown action %s" action))
9087     ;; Read the newsgroup name.
9088     (when (and (not to-newsgroup)
9089                (not select-method))
9090       (if (and gnus-move-split-methods
9091                (not
9092                 (and (memq gnus-current-article articles)
9093                      (gnus-buffer-live-p gnus-original-article-buffer))))
9094           ;; When `gnus-move-split-methods' is non-nil, we have to
9095           ;; select an article to give `gnus-read-move-group-name' an
9096           ;; opportunity to suggest an appropriate default.  However,
9097           ;; we needn't render or mark the article.
9098           (let ((gnus-display-mime-function nil)
9099                 (gnus-article-prepare-hook nil)
9100                 (gnus-mark-article-hook nil))
9101             (gnus-summary-select-article nil nil nil (car articles))))
9102       (setq to-newsgroup
9103             (gnus-read-move-group-name
9104              (cadr (assq action names))
9105              (symbol-value (intern (format "gnus-current-%s-group" action)))
9106              articles prefix))
9107       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9108     (setq to-method (or select-method
9109                         (gnus-server-to-method
9110                          (gnus-group-method to-newsgroup))))
9111     ;; Check the method we are to move this article to...
9112     (unless (gnus-check-backend-function
9113              'request-accept-article (car to-method))
9114       (error "%s does not support article copying" (car to-method)))
9115     (unless (gnus-check-server to-method)
9116       (error "Can't open server %s" (car to-method)))
9117     (gnus-message 6 "%s to %s: %s..."
9118                   (caddr (assq action names))
9119                   (or (car select-method) to-newsgroup) articles)
9120     (while articles
9121       (setq article (pop articles))
9122       (setq
9123        art-group
9124        (cond
9125         ;; Move the article.
9126         ((eq action 'move)
9127          ;; Remove this article from future suppression.
9128          (gnus-dup-unsuppress-article article)
9129          (gnus-request-move-article
9130           article                       ; Article to move
9131           gnus-newsgroup-name           ; From newsgroup
9132           (nth 1 (gnus-find-method-for-group
9133                   gnus-newsgroup-name)) ; Server
9134           (list 'gnus-request-accept-article
9135                 to-newsgroup (list 'quote select-method)
9136                 (not articles) t)       ; Accept form
9137           (not articles)))              ; Only save nov last time
9138         ;; Copy the article.
9139         ((eq action 'copy)
9140          (save-excursion
9141            (set-buffer copy-buf)
9142            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9143              (gnus-request-accept-article
9144               to-newsgroup select-method (not articles) t))))
9145         ;; Crosspost the article.
9146         ((eq action 'crosspost)
9147          (let ((xref (message-tokenize-header
9148                       (mail-header-xref (gnus-summary-article-header article))
9149                       " ")))
9150            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9151                                   ":" (number-to-string article)))
9152            (unless xref
9153              (setq xref (list (system-name))))
9154            (setq new-xref
9155                  (concat
9156                   (mapconcat 'identity
9157                              (delete "Xref:" (delete new-xref xref))
9158                              " ")
9159                   " " new-xref))
9160            (save-excursion
9161              (set-buffer copy-buf)
9162              ;; First put the article in the destination group.
9163              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9164              (when (consp (setq art-group
9165                                 (gnus-request-accept-article
9166                                  to-newsgroup select-method (not articles))))
9167                (setq new-xref (concat new-xref " " (car art-group)
9168                                       ":"
9169                                       (number-to-string (cdr art-group))))
9170                ;; Now we have the new Xrefs header, so we insert
9171                ;; it and replace the new article.
9172                (nnheader-replace-header "Xref" new-xref)
9173                (gnus-request-replace-article
9174                 (cdr art-group) to-newsgroup (current-buffer))
9175                art-group))))))
9176       (cond
9177        ((not art-group)
9178         (gnus-message 1 "Couldn't %s article %s: %s"
9179                       (cadr (assq action names)) article
9180                       (nnheader-get-report (car to-method))))
9181        ((eq art-group 'junk)
9182         (when (eq action 'move)
9183           (gnus-summary-mark-article article gnus-canceled-mark)
9184           (gnus-message 4 "Deleted article %s" article)
9185           ;; run the delete hook
9186           (run-hook-with-args 'gnus-summary-article-delete-hook
9187                               action
9188                               (gnus-data-header
9189                                (assoc article (gnus-data-list nil)))
9190                               gnus-newsgroup-name nil
9191                               select-method)))
9192        (t
9193         (let* ((pto-group (gnus-group-prefixed-name
9194                            (car art-group) to-method))
9195                (info (gnus-get-info pto-group))
9196                (to-group (gnus-info-group info))
9197                to-marks)
9198           ;; Update the group that has been moved to.
9199           (when (and info
9200                      (memq action '(move copy)))
9201             (unless (member to-group to-groups)
9202               (push to-group to-groups))
9203
9204             (unless (memq article gnus-newsgroup-unreads)
9205               (push 'read to-marks)
9206               (gnus-info-set-read
9207                info (gnus-add-to-range (gnus-info-read info)
9208                                        (list (cdr art-group)))))
9209
9210             ;; See whether the article is to be put in the cache.
9211             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9212                              gnus-article-mark-lists
9213                            (delete '(expirable . expire)
9214                                    (copy-sequence gnus-article-mark-lists))))
9215                   (to-article (cdr art-group)))
9216
9217               ;; Enter the article into the cache in the new group,
9218               ;; if that is required.
9219               (when gnus-use-cache
9220                 (gnus-cache-possibly-enter-article
9221                  to-group to-article
9222                  (let ((header (copy-sequence
9223                                 (gnus-summary-article-header article))))
9224                    (mail-header-set-number header to-article)
9225                    header)
9226                  (memq article gnus-newsgroup-marked)
9227                  (memq article gnus-newsgroup-dormant)
9228                  (memq article gnus-newsgroup-unreads)))
9229
9230               (when gnus-preserve-marks
9231                 ;; Copy any marks over to the new group.
9232                 (when (and (equal to-group gnus-newsgroup-name)
9233                            (not (memq article gnus-newsgroup-unreads)))
9234                   ;; Mark this article as read in this group.
9235                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9236                   (setcdr (gnus-active to-group) to-article)
9237                   (setcdr gnus-newsgroup-active to-article))
9238
9239                 (while marks
9240                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9241                     (when (memq article (symbol-value
9242                                          (intern (format "gnus-newsgroup-%s"
9243                                                          (caar marks)))))
9244                       (push (cdar marks) to-marks)
9245                       ;; If the other group is the same as this group,
9246                       ;; then we have to add the mark to the list.
9247                       (when (equal to-group gnus-newsgroup-name)
9248                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9249                              (cons to-article
9250                                    (symbol-value
9251                                     (intern (format "gnus-newsgroup-%s"
9252                                                     (caar marks)))))))
9253                       ;; Copy the marks to other group.
9254                       (gnus-add-marked-articles
9255                        to-group (cdar marks) (list to-article) info)))
9256                   (setq marks (cdr marks)))
9257
9258                 (gnus-request-set-mark
9259                  to-group (list (list (list to-article) 'add to-marks))))
9260
9261               (gnus-dribble-enter
9262                (concat "(gnus-group-set-info '"
9263                        (gnus-prin1-to-string (gnus-get-info to-group))
9264                        ")"))))
9265
9266           ;; Update the Xref header in this article to point to
9267           ;; the new crossposted article we have just created.
9268           (when (eq action 'crosspost)
9269             (save-excursion
9270               (set-buffer copy-buf)
9271               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9272               (nnheader-replace-header "Xref" new-xref)
9273               (gnus-request-replace-article
9274                article gnus-newsgroup-name (current-buffer))))
9275
9276           ;; run the move/copy/crosspost/respool hook
9277           (run-hook-with-args 'gnus-summary-article-move-hook
9278                               action
9279                               (gnus-data-header
9280                                (assoc article (gnus-data-list nil)))
9281                               gnus-newsgroup-name
9282                               to-newsgroup
9283                               select-method))
9284
9285         ;;;!!!Why is this necessary?
9286         (set-buffer gnus-summary-buffer)
9287         
9288         (gnus-summary-goto-subject article)
9289         (when (eq action 'move)
9290           (gnus-summary-mark-article article gnus-canceled-mark))))
9291       (gnus-summary-remove-process-mark article))
9292     ;; Re-activate all groups that have been moved to.
9293     (save-excursion
9294       (set-buffer gnus-group-buffer)
9295       (let ((gnus-group-marked to-groups))
9296         (gnus-group-get-new-news-this-group nil t)))
9297
9298     (gnus-kill-buffer copy-buf)
9299     (gnus-summary-position-point)
9300     (gnus-set-mode-line 'summary)))
9301
9302 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9303   "Copy the current article to some other group.
9304 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9305 When called interactively, if TO-NEWSGROUP is nil, use the value of
9306 the variable `gnus-move-split-methods' for finding a default target
9307 newsgroup.
9308 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9309 re-spool using this method."
9310   (interactive "P")
9311   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9312
9313 (defun gnus-summary-crosspost-article (&optional n)
9314   "Crosspost the current article to some other group."
9315   (interactive "P")
9316   (gnus-summary-move-article n nil nil 'crosspost))
9317
9318 (defcustom gnus-summary-respool-default-method nil
9319   "Default method type for respooling an article.
9320 If nil, use to the current newsgroup method."
9321   :type 'symbol
9322   :group 'gnus-summary-mail)
9323
9324 (defcustom gnus-summary-display-while-building nil
9325   "If non-nil, show and update the summary buffer as it's being built.
9326 If the value is t, update the buffer after every line is inserted.  If
9327 the value is an integer (N), update the display every N lines."
9328   :group 'gnus-thread
9329   :type '(choice (const :tag "off" nil)
9330                  number
9331                  (const :tag "frequently" t)))
9332
9333 (defun gnus-summary-respool-article (&optional n method)
9334   "Respool the current article.
9335 The article will be squeezed through the mail spooling process again,
9336 which means that it will be put in some mail newsgroup or other
9337 depending on `nnmail-split-methods'.
9338 If N is a positive number, respool the N next articles.
9339 If N is a negative number, respool the N previous articles.
9340 If N is nil and any articles have been marked with the process mark,
9341 respool those articles instead.
9342
9343 Respooling can be done both from mail groups and \"real\" newsgroups.
9344 In the former case, the articles in question will be moved from the
9345 current group into whatever groups they are destined to.  In the
9346 latter case, they will be copied into the relevant groups."
9347   (interactive
9348    (list current-prefix-arg
9349          (let* ((methods (gnus-methods-using 'respool))
9350                 (methname
9351                  (symbol-name (or gnus-summary-respool-default-method
9352                                   (car (gnus-find-method-for-group
9353                                         gnus-newsgroup-name)))))
9354                 (method
9355                  (gnus-completing-read-with-default
9356                   methname "What backend do you want to use when respooling?"
9357                   methods nil t nil 'gnus-mail-method-history))
9358                 ms)
9359            (cond
9360             ((zerop (length (setq ms (gnus-servers-using-backend
9361                                       (intern method)))))
9362              (list (intern method) ""))
9363             ((= 1 (length ms))
9364              (car ms))
9365             (t
9366              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9367                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9368                            ms-alist))))))))
9369   (unless method
9370     (error "No method given for respooling"))
9371   (if (assoc (symbol-name
9372               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9373              (gnus-methods-using 'respool))
9374       (gnus-summary-move-article n nil method)
9375     (gnus-summary-copy-article n nil method)))
9376
9377 (defun gnus-summary-import-article (file &optional edit)
9378   "Import an arbitrary file into a mail newsgroup."
9379   (interactive "fImport file: \nP")
9380   (let ((group gnus-newsgroup-name)
9381         (now (current-time))
9382         atts lines group-art)
9383     (unless (gnus-check-backend-function 'request-accept-article group)
9384       (error "%s does not support article importing" group))
9385     (or (file-readable-p file)
9386         (not (file-regular-p file))
9387         (error "Can't read %s" file))
9388     (save-excursion
9389       (set-buffer (gnus-get-buffer-create " *import file*"))
9390       (erase-buffer)
9391       (nnheader-insert-file-contents file)
9392       (goto-char (point-min))
9393       (if (nnheader-article-p)
9394           (save-restriction
9395             (goto-char (point-min))
9396             (search-forward "\n\n" nil t)
9397             (narrow-to-region (point-min) (1- (point)))
9398             (goto-char (point-min))
9399             (unless (re-search-forward "^date:" nil t)
9400               (goto-char (point-max))
9401               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9402         ;; This doesn't look like an article, so we fudge some headers.
9403         (setq atts (file-attributes file)
9404               lines (count-lines (point-min) (point-max)))
9405         (insert "From: " (read-string "From: ") "\n"
9406                 "Subject: " (read-string "Subject: ") "\n"
9407                 "Date: " (message-make-date (nth 5 atts)) "\n"
9408                 "Message-ID: " (message-make-message-id) "\n"
9409                 "Lines: " (int-to-string lines) "\n"
9410                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9411       (setq group-art (gnus-request-accept-article group nil t))
9412       (kill-buffer (current-buffer)))
9413     (setq gnus-newsgroup-active (gnus-activate-group group))
9414     (forward-line 1)
9415     (gnus-summary-goto-article (cdr group-art) nil t)
9416     (when edit
9417       (gnus-summary-edit-article))))
9418
9419 (defun gnus-summary-create-article ()
9420   "Create an article in a mail newsgroup."
9421   (interactive)
9422   (let ((group gnus-newsgroup-name)
9423         (now (current-time))
9424         group-art)
9425     (unless (gnus-check-backend-function 'request-accept-article group)
9426       (error "%s does not support article importing" group))
9427     (save-excursion
9428       (set-buffer (gnus-get-buffer-create " *import file*"))
9429       (erase-buffer)
9430       (goto-char (point-min))
9431       ;; This doesn't look like an article, so we fudge some headers.
9432       (insert "From: " (read-string "From: ") "\n"
9433               "Subject: " (read-string "Subject: ") "\n"
9434               "Date: " (message-make-date now) "\n"
9435               "Message-ID: " (message-make-message-id) "\n")
9436       (setq group-art (gnus-request-accept-article group nil t))
9437       (kill-buffer (current-buffer)))
9438     (setq gnus-newsgroup-active (gnus-activate-group group))
9439     (forward-line 1)
9440     (gnus-summary-goto-article (cdr group-art) nil t)
9441     (gnus-summary-edit-article)))
9442
9443 (defun gnus-summary-article-posted-p ()
9444   "Say whether the current (mail) article is available from news as well.
9445 This will be the case if the article has both been mailed and posted."
9446   (interactive)
9447   (let ((id (mail-header-references (gnus-summary-article-header)))
9448         (gnus-override-method (car (gnus-refer-article-methods))))
9449     (if (gnus-request-head id "")
9450         (gnus-message 2 "The current message was found on %s"
9451                       gnus-override-method)
9452       (gnus-message 2 "The current message couldn't be found on %s"
9453                     gnus-override-method)
9454       nil)))
9455
9456 (defun gnus-summary-expire-articles (&optional now)
9457   "Expire all articles that are marked as expirable in the current group."
9458   (interactive)
9459   (when (and (not gnus-group-is-exiting-without-update-p)
9460              (gnus-check-backend-function
9461               'request-expire-articles gnus-newsgroup-name))
9462     ;; This backend supports expiry.
9463     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9464            (expirable (if total
9465                           (progn
9466                             ;; We need to update the info for
9467                             ;; this group for `gnus-list-of-read-articles'
9468                             ;; to give us the right answer.
9469                             (gnus-run-hooks 'gnus-exit-group-hook)
9470                             (gnus-summary-update-info)
9471                             (gnus-list-of-read-articles gnus-newsgroup-name))
9472                         (setq gnus-newsgroup-expirable
9473                               (sort gnus-newsgroup-expirable '<))))
9474            (expiry-wait (if now 'immediate
9475                           (gnus-group-find-parameter
9476                            gnus-newsgroup-name 'expiry-wait)))
9477            (nnmail-expiry-target
9478             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9479                 nnmail-expiry-target))
9480            es)
9481       (when expirable
9482         ;; There are expirable articles in this group, so we run them
9483         ;; through the expiry process.
9484         (gnus-message 6 "Expiring articles...")
9485         (unless (gnus-check-group gnus-newsgroup-name)
9486           (error "Can't open server for %s" gnus-newsgroup-name))
9487         ;; The list of articles that weren't expired is returned.
9488         (save-excursion
9489           (if expiry-wait
9490               (let ((nnmail-expiry-wait-function nil)
9491                     (nnmail-expiry-wait expiry-wait))
9492                 (setq es (gnus-request-expire-articles
9493                           expirable gnus-newsgroup-name)))
9494             (setq es (gnus-request-expire-articles
9495                       expirable gnus-newsgroup-name)))
9496           (unless total
9497             (setq gnus-newsgroup-expirable es))
9498           ;; We go through the old list of expirable, and mark all
9499           ;; really expired articles as nonexistent.
9500           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9501             (let ((gnus-use-cache nil))
9502               (dolist (article expirable)
9503                 (when (and (not (memq article es))
9504                            (gnus-data-find article))
9505                   (gnus-summary-mark-article article gnus-canceled-mark)
9506                   (run-hook-with-args 'gnus-summary-article-expire-hook
9507                                       'delete
9508                                       (gnus-data-header
9509                                        (assoc article (gnus-data-list nil)))
9510                                       gnus-newsgroup-name
9511                                       nil
9512                                       nil))))))
9513         (gnus-message 6 "Expiring articles...done")))))
9514
9515 (defun gnus-summary-expire-articles-now ()
9516   "Expunge all expirable articles in the current group.
9517 This means that *all* articles that are marked as expirable will be
9518 deleted forever, right now."
9519   (interactive)
9520   (or gnus-expert-user
9521       (gnus-yes-or-no-p
9522        "Are you really, really, really sure you want to delete all these messages? ")
9523       (error "Phew!"))
9524   (gnus-summary-expire-articles t))
9525
9526 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9527 (defun gnus-summary-delete-article (&optional n)
9528   "Delete the N next (mail) articles.
9529 This command actually deletes articles.  This is not a marking
9530 command.  The article will disappear forever from your life, never to
9531 return.
9532
9533 If N is negative, delete backwards.
9534 If N is nil and articles have been marked with the process mark,
9535 delete these instead.
9536
9537 If `gnus-novice-user' is non-nil you will be asked for
9538 confirmation before the articles are deleted."
9539   (interactive "P")
9540   (unless (gnus-check-backend-function 'request-expire-articles
9541                                        gnus-newsgroup-name)
9542     (error "The current newsgroup does not support article deletion"))
9543   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9544     (error "Couldn't open server"))
9545   ;; Compute the list of articles to delete.
9546   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9547         (nnmail-expiry-target 'delete)
9548         not-deleted)
9549     (if (and gnus-novice-user
9550              (not (gnus-yes-or-no-p
9551                    (format "Do you really want to delete %s forever? "
9552                            (if (> (length articles) 1)
9553                                (format "these %s articles" (length articles))
9554                              "this article")))))
9555         ()
9556       ;; Delete the articles.
9557       (setq not-deleted (gnus-request-expire-articles
9558                          articles gnus-newsgroup-name 'force))
9559       (while articles
9560         (gnus-summary-remove-process-mark (car articles))
9561         ;; The backend might not have been able to delete the article
9562         ;; after all.
9563         (unless (memq (car articles) not-deleted)
9564           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9565         (let* ((article (car articles))
9566                (ghead  (gnus-data-header
9567                                     (assoc article (gnus-data-list nil)))))
9568           (run-hook-with-args 'gnus-summary-article-delete-hook
9569                               'delete ghead gnus-newsgroup-name nil
9570                               nil))
9571         (setq articles (cdr articles)))
9572       (when not-deleted
9573         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9574     (gnus-summary-position-point)
9575     (gnus-set-mode-line 'summary)
9576     not-deleted))
9577
9578 (defun gnus-summary-edit-article (&optional force)
9579   "Edit the current article.
9580 This will have permanent effect only in mail groups.
9581 If FORCE is non-nil, allow editing of articles even in read-only
9582 groups."
9583   (interactive "P")
9584   (save-excursion
9585     (set-buffer gnus-summary-buffer)
9586     (let ((mail-parse-charset gnus-newsgroup-charset)
9587           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9588       (gnus-set-global-variables)
9589       (when (and (not force)
9590                  (gnus-group-read-only-p))
9591         (error "The current newsgroup does not support article editing"))
9592       (gnus-summary-show-article t)
9593       (gnus-article-edit-article
9594        'ignore
9595        `(lambda (no-highlight)
9596           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9597                 (message-options message-options)
9598                 (message-options-set-recipient)
9599                 (mail-parse-ignored-charsets
9600                  ',gnus-newsgroup-ignored-charsets))
9601             (gnus-summary-edit-article-done
9602              ,(or (mail-header-references gnus-current-headers) "")
9603              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9604
9605 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9606
9607 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9608                                                  no-highlight)
9609   "Make edits to the current article permanent."
9610   (interactive)
9611   (save-excursion
9612     ;; The buffer restriction contains the entire article if it exists.
9613     (when (article-goto-body)
9614       (let ((lines (count-lines (point) (point-max)))
9615             (length (- (point-max) (point)))
9616             (case-fold-search t)
9617             (body (copy-marker (point))))
9618         (goto-char (point-min))
9619         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9620           (delete-region (match-beginning 1) (match-end 1))
9621           (insert (number-to-string length)))
9622         (goto-char (point-min))
9623         (when (re-search-forward
9624                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9625           (delete-region (match-beginning 1) (match-end 1))
9626           (insert (number-to-string length)))
9627         (goto-char (point-min))
9628         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9629           (delete-region (match-beginning 1) (match-end 1))
9630           (insert (number-to-string lines))))))
9631   ;; Replace the article.
9632   (let ((buf (current-buffer)))
9633     (with-temp-buffer
9634       (insert-buffer-substring buf)
9635
9636       (if (and (not read-only)
9637                (not (gnus-request-replace-article
9638                      (cdr gnus-article-current) (car gnus-article-current)
9639                      (current-buffer) t)))
9640           (error "Couldn't replace article")
9641         ;; Update the summary buffer.
9642         (if (and references
9643                  (equal (message-tokenize-header references " ")
9644                         (message-tokenize-header
9645                          (or (message-fetch-field "references") "") " ")))
9646             ;; We only have to update this line.
9647             (save-excursion
9648               (save-restriction
9649                 (message-narrow-to-head)
9650                 (let ((head (buffer-string))
9651                       header)
9652                   (with-temp-buffer
9653                     (insert (format "211 %d Article retrieved.\n"
9654                                     (cdr gnus-article-current)))
9655                     (insert head)
9656                     (insert ".\n")
9657                     (let ((nntp-server-buffer (current-buffer)))
9658                       (setq header (car (gnus-get-newsgroup-headers
9659                                          nil t))))
9660                     (save-excursion
9661                       (set-buffer gnus-summary-buffer)
9662                       (gnus-data-set-header
9663                        (gnus-data-find (cdr gnus-article-current))
9664                        header)
9665                       (gnus-summary-update-article-line
9666                        (cdr gnus-article-current) header)
9667                       (if (gnus-summary-goto-subject
9668                            (cdr gnus-article-current) nil t)
9669                           (gnus-summary-update-secondary-mark
9670                            (cdr gnus-article-current))))))))
9671           ;; Update threads.
9672           (set-buffer (or buffer gnus-summary-buffer))
9673           (gnus-summary-update-article (cdr gnus-article-current))
9674           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9675               (gnus-summary-update-secondary-mark
9676                (cdr gnus-article-current))))
9677         ;; Prettify the article buffer again.
9678         (unless no-highlight
9679           (save-excursion
9680             (set-buffer gnus-article-buffer)
9681             ;;;!!! Fix this -- article should be rehighlighted.
9682             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9683             (set-buffer gnus-original-article-buffer)
9684             (gnus-request-article
9685              (cdr gnus-article-current)
9686              (car gnus-article-current) (current-buffer))))
9687         ;; Prettify the summary buffer line.
9688         (when (gnus-visual-p 'summary-highlight 'highlight)
9689           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9690
9691 (defun gnus-summary-edit-wash (key)
9692   "Perform editing command KEY in the article buffer."
9693   (interactive
9694    (list
9695     (progn
9696       (message "%s" (concat (this-command-keys) "- "))
9697       (read-char))))
9698   (message "")
9699   (gnus-summary-edit-article)
9700   (execute-kbd-macro (concat (this-command-keys) key))
9701   (gnus-article-edit-done))
9702
9703 ;;; Respooling
9704
9705 (defun gnus-summary-respool-query (&optional silent trace)
9706   "Query where the respool algorithm would put this article."
9707   (interactive)
9708   (let (gnus-mark-article-hook)
9709     (gnus-summary-select-article)
9710     (save-excursion
9711       (set-buffer gnus-original-article-buffer)
9712       (let ((groups (nnmail-article-group 'identity trace)))
9713         (unless silent
9714           (if groups
9715               (message "This message would go to %s"
9716                        (mapconcat 'car groups ", "))
9717             (message "This message would go to no groups"))
9718           groups)))))
9719
9720 (defun gnus-summary-respool-trace ()
9721   "Trace where the respool algorithm would put this article.
9722 Display a buffer showing all fancy splitting patterns which matched."
9723   (interactive)
9724   (gnus-summary-respool-query nil t))
9725
9726 ;; Summary marking commands.
9727
9728 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9729   "Mark articles which has the same subject as read, and then select the next.
9730 If UNMARK is positive, remove any kind of mark.
9731 If UNMARK is negative, tick articles."
9732   (interactive "P")
9733   (when unmark
9734     (setq unmark (prefix-numeric-value unmark)))
9735   (let ((count
9736          (gnus-summary-mark-same-subject
9737           (gnus-summary-article-subject) unmark)))
9738     ;; Select next unread article.  If auto-select-same mode, should
9739     ;; select the first unread article.
9740     (gnus-summary-next-article t (and gnus-auto-select-same
9741                                       (gnus-summary-article-subject)))
9742     (gnus-message 7 "%d article%s marked as %s"
9743                   count (if (= count 1) " is" "s are")
9744                   (if unmark "unread" "read"))))
9745
9746 (defun gnus-summary-kill-same-subject (&optional unmark)
9747   "Mark articles which has the same subject as read.
9748 If UNMARK is positive, remove any kind of mark.
9749 If UNMARK is negative, tick articles."
9750   (interactive "P")
9751   (when unmark
9752     (setq unmark (prefix-numeric-value unmark)))
9753   (let ((count
9754          (gnus-summary-mark-same-subject
9755           (gnus-summary-article-subject) unmark)))
9756     ;; If marked as read, go to next unread subject.
9757     (when (null unmark)
9758       ;; Go to next unread subject.
9759       (gnus-summary-next-subject 1 t))
9760     (gnus-message 7 "%d articles are marked as %s"
9761                   count (if unmark "unread" "read"))))
9762
9763 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9764   "Mark articles with same SUBJECT as read, and return marked number.
9765 If optional argument UNMARK is positive, remove any kinds of marks.
9766 If optional argument UNMARK is negative, mark articles as unread instead."
9767   (let ((count 1))
9768     (save-excursion
9769       (cond
9770        ((null unmark)                   ; Mark as read.
9771         (while (and
9772                 (progn
9773                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9774                   (gnus-summary-show-thread) t)
9775                 (gnus-summary-find-subject subject))
9776           (setq count (1+ count))))
9777        ((> unmark 0)                    ; Tick.
9778         (while (and
9779                 (progn
9780                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9781                   (gnus-summary-show-thread) t)
9782                 (gnus-summary-find-subject subject))
9783           (setq count (1+ count))))
9784        (t                               ; Mark as unread.
9785         (while (and
9786                 (progn
9787                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9788                   (gnus-summary-show-thread) t)
9789                 (gnus-summary-find-subject subject))
9790           (setq count (1+ count)))))
9791       (gnus-set-mode-line 'summary)
9792       ;; Return the number of marked articles.
9793       count)))
9794
9795 (defun gnus-summary-mark-as-processable (n &optional unmark)
9796   "Set the process mark on the next N articles.
9797 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9798 the process mark instead.  The difference between N and the actual
9799 number of articles marked is returned."
9800   (interactive "P")
9801   (if (and (null n) (gnus-region-active-p))
9802       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9803     (setq n (prefix-numeric-value n))
9804     (let ((backward (< n 0))
9805           (n (abs n)))
9806       (while (and
9807               (> n 0)
9808               (if unmark
9809                   (gnus-summary-remove-process-mark
9810                    (gnus-summary-article-number))
9811                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9812               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9813         (setq n (1- n)))
9814       (when (/= 0 n)
9815         (gnus-message 7 "No more articles"))
9816       (gnus-summary-recenter)
9817       (gnus-summary-position-point)
9818       n)))
9819
9820 (defun gnus-summary-unmark-as-processable (n)
9821   "Remove the process mark from the next N articles.
9822 If N is negative, unmark backward instead.  The difference between N and
9823 the actual number of articles unmarked is returned."
9824   (interactive "P")
9825   (gnus-summary-mark-as-processable n t))
9826
9827 (defun gnus-summary-unmark-all-processable ()
9828   "Remove the process mark from all articles."
9829   (interactive)
9830   (save-excursion
9831     (while gnus-newsgroup-processable
9832       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9833   (gnus-summary-position-point))
9834
9835 (defun gnus-summary-add-mark (article type)
9836   "Mark ARTICLE with a mark of TYPE."
9837   (let ((vtype (car (assq type gnus-article-mark-lists)))
9838         var)
9839     (if (not vtype)
9840         (error "No such mark type: %s" type)
9841       (setq var (intern (format "gnus-newsgroup-%s" type)))
9842       (set var (cons article (symbol-value var)))
9843       (if (memq type '(processable cached replied forwarded recent saved))
9844           (gnus-summary-update-secondary-mark article)
9845         ;;; !!! This is bogus.  We should find out what primary
9846         ;;; !!! mark we want to set.
9847         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9848
9849 (defun gnus-summary-mark-as-expirable (n)
9850   "Mark N articles forward as expirable.
9851 If N is negative, mark backward instead.  The difference between N and
9852 the actual number of articles marked is returned."
9853   (interactive "p")
9854   (gnus-summary-mark-forward n gnus-expirable-mark))
9855
9856 (defun gnus-summary-mark-as-spam (n)
9857   "Mark N articles forward as spam.
9858 If N is negative, mark backward instead.  The difference between N and
9859 the actual number of articles marked is returned."
9860   (interactive "p")
9861   (gnus-summary-mark-forward n gnus-spam-mark))
9862
9863 (defun gnus-summary-mark-article-as-replied (article)
9864   "Mark ARTICLE as replied to and update the summary line.
9865 ARTICLE can also be a list of articles."
9866   (interactive (list (gnus-summary-article-number)))
9867   (let ((articles (if (listp article) article (list article))))
9868     (dolist (article articles)
9869       (unless (numberp article)
9870         (error "%s is not a number" article))
9871       (push article gnus-newsgroup-replied)
9872       (let ((buffer-read-only nil))
9873         (when (gnus-summary-goto-subject article nil t)
9874           (gnus-summary-update-secondary-mark article))))))
9875
9876 (defun gnus-summary-mark-article-as-forwarded (article)
9877   "Mark ARTICLE as forwarded and update the summary line.
9878 ARTICLE can also be a list of articles."
9879   (let ((articles (if (listp article) article (list article))))
9880     (dolist (article articles)
9881       (push article gnus-newsgroup-forwarded)
9882       (let ((buffer-read-only nil))
9883         (when (gnus-summary-goto-subject article nil t)
9884           (gnus-summary-update-secondary-mark article))))))
9885
9886 (defun gnus-summary-set-bookmark (article)
9887   "Set a bookmark in current article."
9888   (interactive (list (gnus-summary-article-number)))
9889   (when (or (not (get-buffer gnus-article-buffer))
9890             (not gnus-current-article)
9891             (not gnus-article-current)
9892             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9893     (error "No current article selected"))
9894   ;; Remove old bookmark, if one exists.
9895   (gnus-pull article gnus-newsgroup-bookmarks)
9896   ;; Set the new bookmark, which is on the form
9897   ;; (article-number . line-number-in-body).
9898   (push
9899    (cons article
9900          (with-current-buffer gnus-article-buffer
9901            (count-lines
9902             (min (point)
9903                  (save-excursion
9904                    (article-goto-body)
9905                    (point)))
9906             (point))))
9907    gnus-newsgroup-bookmarks)
9908   (gnus-message 6 "A bookmark has been added to the current article."))
9909
9910 (defun gnus-summary-remove-bookmark (article)
9911   "Remove the bookmark from the current article."
9912   (interactive (list (gnus-summary-article-number)))
9913   ;; Remove old bookmark, if one exists.
9914   (if (not (assq article gnus-newsgroup-bookmarks))
9915       (gnus-message 6 "No bookmark in current article.")
9916     (gnus-pull article gnus-newsgroup-bookmarks)
9917     (gnus-message 6 "Removed bookmark.")))
9918
9919 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9920 (defun gnus-summary-mark-as-dormant (n)
9921   "Mark N articles forward as dormant.
9922 If N is negative, mark backward instead.  The difference between N and
9923 the actual number of articles marked is returned."
9924   (interactive "p")
9925   (gnus-summary-mark-forward n gnus-dormant-mark))
9926
9927 (defun gnus-summary-set-process-mark (article)
9928   "Set the process mark on ARTICLE and update the summary line."
9929   (setq gnus-newsgroup-processable
9930         (cons article
9931               (delq article gnus-newsgroup-processable)))
9932   (when (gnus-summary-goto-subject article)
9933     (gnus-summary-show-thread)
9934     (gnus-summary-goto-subject article)
9935     (gnus-summary-update-secondary-mark article)))
9936
9937 (defun gnus-summary-remove-process-mark (article)
9938   "Remove the process mark from ARTICLE and update the summary line."
9939   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9940   (when (gnus-summary-goto-subject article)
9941     (gnus-summary-show-thread)
9942     (gnus-summary-goto-subject article)
9943     (gnus-summary-update-secondary-mark article)))
9944
9945 (defun gnus-summary-set-saved-mark (article)
9946   "Set the process mark on ARTICLE and update the summary line."
9947   (push article gnus-newsgroup-saved)
9948   (when (gnus-summary-goto-subject article)
9949     (gnus-summary-update-secondary-mark article)))
9950
9951 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9952   "Mark N articles as read forwards.
9953 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9954 The difference between N and the actual number of articles marked is
9955 returned.
9956 If NO-EXPIRE, auto-expiry will be inhibited."
9957   (interactive "p")
9958   (gnus-summary-show-thread)
9959   (let ((backward (< n 0))
9960         (gnus-summary-goto-unread
9961          (and gnus-summary-goto-unread
9962               (not (eq gnus-summary-goto-unread 'never))
9963               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9964                                     gnus-ticked-mark gnus-dormant-mark)))))
9965         (n (abs n))
9966         (mark (or mark gnus-del-mark)))
9967     (while (and (> n 0)
9968                 (gnus-summary-mark-article nil mark no-expire)
9969                 (zerop (gnus-summary-next-subject
9970                         (if backward -1 1)
9971                         (and gnus-summary-goto-unread
9972                              (not (eq gnus-summary-goto-unread 'never)))
9973                         t)))
9974       (setq n (1- n)))
9975     (when (/= 0 n)
9976       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9977     (gnus-summary-recenter)
9978     (gnus-summary-position-point)
9979     (gnus-set-mode-line 'summary)
9980     n))
9981
9982 (defun gnus-summary-mark-article-as-read (mark)
9983   "Mark the current article quickly as read with MARK."
9984   (let ((article (gnus-summary-article-number)))
9985     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9986     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9987     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9988     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9989     (push (cons article mark) gnus-newsgroup-reads)
9990     ;; Possibly remove from cache, if that is used.
9991     (when gnus-use-cache
9992       (gnus-cache-enter-remove-article article))
9993     ;; Allow the backend to change the mark.
9994     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9995     ;; Check for auto-expiry.
9996     (when (and gnus-newsgroup-auto-expire
9997                (memq mark gnus-auto-expirable-marks))
9998       (setq mark gnus-expirable-mark)
9999       ;; Let the backend know about the mark change.
10000       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10001       (push article gnus-newsgroup-expirable))
10002     ;; Set the mark in the buffer.
10003     (gnus-summary-update-mark mark 'unread)
10004     t))
10005
10006 (defun gnus-summary-mark-article-as-unread (mark)
10007   "Mark the current article quickly as unread with MARK."
10008   (let* ((article (gnus-summary-article-number))
10009          (old-mark (gnus-summary-article-mark article)))
10010     ;; Allow the backend to change the mark.
10011     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10012     (if (eq mark old-mark)
10013         t
10014       (if (<= article 0)
10015           (progn
10016             (gnus-error 1 "Can't mark negative article numbers")
10017             nil)
10018         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10019         (setq gnus-newsgroup-spam-marked
10020               (delq article gnus-newsgroup-spam-marked))
10021         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10022         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10023         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10024         (cond ((= mark gnus-ticked-mark)
10025                (setq gnus-newsgroup-marked
10026                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10027                                               article)))
10028               ((= mark gnus-spam-mark)
10029                (setq gnus-newsgroup-spam-marked
10030                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10031                                               article)))
10032               ((= mark gnus-dormant-mark)
10033                (setq gnus-newsgroup-dormant
10034                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10035                                               article)))
10036               (t
10037                (setq gnus-newsgroup-unreads
10038                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10039                                               article))))
10040         (gnus-pull article gnus-newsgroup-reads)
10041
10042         ;; See whether the article is to be put in the cache.
10043         (and gnus-use-cache
10044              (vectorp (gnus-summary-article-header article))
10045              (save-excursion
10046                (gnus-cache-possibly-enter-article
10047                 gnus-newsgroup-name article
10048                 (gnus-summary-article-header article)
10049                 (= mark gnus-ticked-mark)
10050                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10051
10052         ;; Fix the mark.
10053         (gnus-summary-update-mark mark 'unread)
10054         t))))
10055
10056 (defun gnus-summary-mark-article (&optional article mark no-expire)
10057   "Mark ARTICLE with MARK.  MARK can be any character.
10058 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10059 `??' (dormant) and `?E' (expirable).
10060 If MARK is nil, then the default character `?r' is used.
10061 If ARTICLE is nil, then the article on the current line will be
10062 marked.
10063 If NO-EXPIRE, auto-expiry will be inhibited."
10064   ;; The mark might be a string.
10065   (when (stringp mark)
10066     (setq mark (aref mark 0)))
10067   ;; If no mark is given, then we check auto-expiring.
10068   (when (null mark)
10069     (setq mark gnus-del-mark))
10070   (when (and (not no-expire)
10071              gnus-newsgroup-auto-expire
10072              (memq mark gnus-auto-expirable-marks))
10073     (setq mark gnus-expirable-mark))
10074   (let ((article (or article (gnus-summary-article-number)))
10075         (old-mark (gnus-summary-article-mark article)))
10076     ;; Allow the backend to change the mark.
10077     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10078     (if (eq mark old-mark)
10079         t
10080       (unless article
10081         (error "No article on current line"))
10082       (if (not (if (or (= mark gnus-unread-mark)
10083                        (= mark gnus-ticked-mark)
10084                        (= mark gnus-spam-mark)
10085                        (= mark gnus-dormant-mark))
10086                    (gnus-mark-article-as-unread article mark)
10087                  (gnus-mark-article-as-read article mark)))
10088           t
10089         ;; See whether the article is to be put in the cache.
10090         (and gnus-use-cache
10091              (not (= mark gnus-canceled-mark))
10092              (vectorp (gnus-summary-article-header article))
10093              (save-excursion
10094                (gnus-cache-possibly-enter-article
10095                 gnus-newsgroup-name article
10096                 (gnus-summary-article-header article)
10097                 (= mark gnus-ticked-mark)
10098                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10099
10100         (when (gnus-summary-goto-subject article nil t)
10101           (let ((buffer-read-only nil))
10102             (gnus-summary-show-thread)
10103             ;; Fix the mark.
10104             (gnus-summary-update-mark mark 'unread)
10105             t))))))
10106
10107 (defun gnus-summary-update-secondary-mark (article)
10108   "Update the secondary (read, process, cache) mark."
10109   (gnus-summary-update-mark
10110    (cond ((memq article gnus-newsgroup-processable)
10111           gnus-process-mark)
10112          ((memq article gnus-newsgroup-cached)
10113           gnus-cached-mark)
10114          ((memq article gnus-newsgroup-replied)
10115           gnus-replied-mark)
10116          ((memq article gnus-newsgroup-forwarded)
10117           gnus-forwarded-mark)
10118          ((memq article gnus-newsgroup-saved)
10119           gnus-saved-mark)
10120          ((memq article gnus-newsgroup-recent)
10121           gnus-recent-mark)
10122          ((memq article gnus-newsgroup-unseen)
10123           gnus-unseen-mark)
10124          (t gnus-no-mark))
10125    'replied)
10126   (when (gnus-visual-p 'summary-highlight 'highlight)
10127     (gnus-run-hooks 'gnus-summary-update-hook))
10128   t)
10129
10130 (defun gnus-summary-update-download-mark (article)
10131   "Update the download mark."
10132   (gnus-summary-update-mark
10133    (cond ((memq article gnus-newsgroup-undownloaded)
10134           gnus-undownloaded-mark)
10135          (gnus-newsgroup-agentized
10136           gnus-downloaded-mark)
10137          (t
10138           gnus-no-mark))
10139    'download)
10140   (gnus-summary-update-line t)
10141   t)
10142
10143 (defun gnus-summary-update-mark (mark type)
10144   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10145         (buffer-read-only nil))
10146     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10147     (when forward
10148       (when (looking-at "\r")
10149         (incf forward))
10150       (when (<= (+ forward (point)) (point-max))
10151         ;; Go to the right position on the line.
10152         (goto-char (+ forward (point)))
10153         ;; Replace the old mark with the new mark.
10154         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10155         ;; Optionally update the marks by some user rule.
10156         (when (eq type 'unread)
10157           (gnus-data-set-mark
10158            (gnus-data-find (gnus-summary-article-number)) mark)
10159           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10160
10161 (defun gnus-mark-article-as-read (article &optional mark)
10162   "Enter ARTICLE in the pertinent lists and remove it from others."
10163   ;; Make the article expirable.
10164   (let ((mark (or mark gnus-del-mark)))
10165     (setq gnus-newsgroup-expirable
10166           (if (= mark gnus-expirable-mark)
10167               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10168             (delq article gnus-newsgroup-expirable)))
10169     ;; Remove from unread and marked lists.
10170     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10171     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10172     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10173     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10174     (push (cons article mark) gnus-newsgroup-reads)
10175     ;; Possibly remove from cache, if that is used.
10176     (when gnus-use-cache
10177       (gnus-cache-enter-remove-article article))
10178     t))
10179
10180 (defun gnus-mark-article-as-unread (article &optional mark)
10181   "Enter ARTICLE in the pertinent lists and remove it from others."
10182   (let ((mark (or mark gnus-ticked-mark)))
10183     (if (<= article 0)
10184         (progn
10185           (gnus-error 1 "Can't mark negative article numbers")
10186           nil)
10187       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10188             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10189             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10190             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10191             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10192
10193       ;; Unsuppress duplicates?
10194       (when gnus-suppress-duplicates
10195         (gnus-dup-unsuppress-article article))
10196
10197       (cond ((= mark gnus-ticked-mark)
10198              (setq gnus-newsgroup-marked
10199                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10200             ((= mark gnus-spam-mark)
10201              (setq gnus-newsgroup-spam-marked
10202                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10203                                             article)))
10204             ((= mark gnus-dormant-mark)
10205              (setq gnus-newsgroup-dormant
10206                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10207             (t
10208              (setq gnus-newsgroup-unreads
10209                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10210       (gnus-pull article gnus-newsgroup-reads)
10211       t)))
10212
10213 (defalias 'gnus-summary-mark-as-unread-forward
10214   'gnus-summary-tick-article-forward)
10215 (make-obsolete 'gnus-summary-mark-as-unread-forward
10216                'gnus-summary-tick-article-forward)
10217 (defun gnus-summary-tick-article-forward (n)
10218   "Tick N articles forwards.
10219 If N is negative, tick backwards instead.
10220 The difference between N and the number of articles ticked is returned."
10221   (interactive "p")
10222   (gnus-summary-mark-forward n gnus-ticked-mark))
10223
10224 (defalias 'gnus-summary-mark-as-unread-backward
10225   'gnus-summary-tick-article-backward)
10226 (make-obsolete 'gnus-summary-mark-as-unread-backward
10227                'gnus-summary-tick-article-backward)
10228 (defun gnus-summary-tick-article-backward (n)
10229   "Tick N articles backwards.
10230 The difference between N and the number of articles ticked is returned."
10231   (interactive "p")
10232   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10233
10234 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10235 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10236 (defun gnus-summary-tick-article (&optional article clear-mark)
10237   "Mark current article as unread.
10238 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10239 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10240   (interactive)
10241   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10242                                        gnus-ticked-mark)))
10243
10244 (defun gnus-summary-mark-as-read-forward (n)
10245   "Mark N articles as read forwards.
10246 If N is negative, mark backwards instead.
10247 The difference between N and the actual number of articles marked is
10248 returned."
10249   (interactive "p")
10250   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10251
10252 (defun gnus-summary-mark-as-read-backward (n)
10253   "Mark the N articles as read backwards.
10254 The difference between N and the actual number of articles marked is
10255 returned."
10256   (interactive "p")
10257   (gnus-summary-mark-forward
10258    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10259
10260 (defun gnus-summary-mark-as-read (&optional article mark)
10261   "Mark current article as read.
10262 ARTICLE specifies the article to be marked as read.
10263 MARK specifies a string to be inserted at the beginning of the line."
10264   (gnus-summary-mark-article article mark))
10265
10266 (defun gnus-summary-clear-mark-forward (n)
10267   "Clear marks from N articles forward.
10268 If N is negative, clear backward instead.
10269 The difference between N and the number of marks cleared is returned."
10270   (interactive "p")
10271   (gnus-summary-mark-forward n gnus-unread-mark))
10272
10273 (defun gnus-summary-clear-mark-backward (n)
10274   "Clear marks from N articles backward.
10275 The difference between N and the number of marks cleared is returned."
10276   (interactive "p")
10277   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10278
10279 (defun gnus-summary-mark-unread-as-read ()
10280   "Intended to be used by `gnus-summary-mark-article-hook'."
10281   (when (memq gnus-current-article gnus-newsgroup-unreads)
10282     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10283
10284 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10285   "Intended to be used by `gnus-summary-mark-article-hook'."
10286   (let ((mark (gnus-summary-article-mark)))
10287     (when (or (gnus-unread-mark-p mark)
10288               (gnus-read-mark-p mark))
10289       (gnus-summary-mark-article gnus-current-article
10290                                  (or new-mark gnus-read-mark)))))
10291
10292 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10293   "Intended to be used by `gnus-summary-mark-article-hook'."
10294   (let ((mark (gnus-summary-article-mark)))
10295     (when (or (gnus-unread-mark-p mark)
10296               (gnus-read-mark-p mark))
10297       (gnus-summary-mark-article (gnus-summary-article-number)
10298                                  (or new-mark gnus-read-mark)))))
10299
10300 (defun gnus-summary-mark-unread-as-ticked ()
10301   "Intended to be used by `gnus-summary-mark-article-hook'."
10302   (when (memq gnus-current-article gnus-newsgroup-unreads)
10303     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10304
10305 (defun gnus-summary-mark-region-as-read (point mark all)
10306   "Mark all unread articles between point and mark as read.
10307 If given a prefix, mark all articles between point and mark as read,
10308 even ticked and dormant ones."
10309   (interactive "r\nP")
10310   (save-excursion
10311     (let (article)
10312       (goto-char point)
10313       (beginning-of-line)
10314       (while (and
10315               (< (point) mark)
10316               (progn
10317                 (when (or all
10318                           (memq (setq article (gnus-summary-article-number))
10319                                 gnus-newsgroup-unreads))
10320                   (gnus-summary-mark-article article gnus-del-mark))
10321                 t)
10322               (gnus-summary-find-next))))))
10323
10324 (defun gnus-summary-mark-below (score mark)
10325   "Mark articles with score less than SCORE with MARK."
10326   (interactive "P\ncMark: ")
10327   (setq score (if score
10328                   (prefix-numeric-value score)
10329                 (or gnus-summary-default-score 0)))
10330   (save-excursion
10331     (set-buffer gnus-summary-buffer)
10332     (goto-char (point-min))
10333     (while
10334         (progn
10335           (and (< (gnus-summary-article-score) score)
10336                (gnus-summary-mark-article nil mark))
10337           (gnus-summary-find-next)))))
10338
10339 (defun gnus-summary-kill-below (&optional score)
10340   "Mark articles with score below SCORE as read."
10341   (interactive "P")
10342   (gnus-summary-mark-below score gnus-killed-mark))
10343
10344 (defun gnus-summary-clear-above (&optional score)
10345   "Clear all marks from articles with score above SCORE."
10346   (interactive "P")
10347   (gnus-summary-mark-above score gnus-unread-mark))
10348
10349 (defun gnus-summary-tick-above (&optional score)
10350   "Tick all articles with score above SCORE."
10351   (interactive "P")
10352   (gnus-summary-mark-above score gnus-ticked-mark))
10353
10354 (defun gnus-summary-mark-above (score mark)
10355   "Mark articles with score over SCORE with MARK."
10356   (interactive "P\ncMark: ")
10357   (setq score (if score
10358                   (prefix-numeric-value score)
10359                 (or gnus-summary-default-score 0)))
10360   (save-excursion
10361     (set-buffer gnus-summary-buffer)
10362     (goto-char (point-min))
10363     (while (and (progn
10364                   (when (> (gnus-summary-article-score) score)
10365                     (gnus-summary-mark-article nil mark))
10366                   t)
10367                 (gnus-summary-find-next)))))
10368
10369 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10370 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10371 (defun gnus-summary-limit-include-expunged (&optional no-error)
10372   "Display all the hidden articles that were expunged for low scores."
10373   (interactive)
10374   (let ((buffer-read-only nil))
10375     (let ((scored gnus-newsgroup-scored)
10376           headers h)
10377       (while scored
10378         (unless (gnus-summary-article-header (caar scored))
10379           (and (setq h (gnus-number-to-header (caar scored)))
10380                (< (cdar scored) gnus-summary-expunge-below)
10381                (push h headers)))
10382         (setq scored (cdr scored)))
10383       (if (not headers)
10384           (when (not no-error)
10385             (error "No expunged articles hidden"))
10386         (goto-char (point-min))
10387         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10388         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10389         (mapcar (lambda (x) (push (mail-header-number x)
10390                                   gnus-newsgroup-limit))
10391                 headers)
10392         (gnus-summary-prepare-unthreaded (nreverse headers))
10393         (goto-char (point-min))
10394         (gnus-summary-position-point)
10395         t))))
10396
10397 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10398   "Mark all unread articles in this newsgroup as read.
10399 If prefix argument ALL is non-nil, ticked and dormant articles will
10400 also be marked as read.
10401 If QUIETLY is non-nil, no questions will be asked.
10402
10403 If TO-HERE is non-nil, it should be a point in the buffer.  All
10404 articles before (after, if REVERSE is set) this point will be marked
10405 as read.
10406
10407 Note that this function will only catch up the unread article
10408 in the current summary buffer limitation.
10409
10410 The number of articles marked as read is returned."
10411   (interactive "P")
10412   (prog1
10413       (save-excursion
10414         (when (or quietly
10415                   (not gnus-interactive-catchup) ;Without confirmation?
10416                   gnus-expert-user
10417                   (gnus-y-or-n-p
10418                    (if all
10419                        "Mark absolutely all articles as read? "
10420                      "Mark all unread articles as read? ")))
10421           (if (and not-mark
10422                    (not gnus-newsgroup-adaptive)
10423                    (not gnus-newsgroup-auto-expire)
10424                    (not gnus-suppress-duplicates)
10425                    (or (not gnus-use-cache)
10426                        (eq gnus-use-cache 'passive)))
10427               (progn
10428                 (when all
10429                   (setq gnus-newsgroup-marked nil
10430                         gnus-newsgroup-spam-marked nil
10431                         gnus-newsgroup-dormant nil))
10432                 (setq gnus-newsgroup-unreads
10433                       (gnus-sorted-nunion
10434                        (gnus-intersection gnus-newsgroup-unreads
10435                                           gnus-newsgroup-downloadable)
10436                        gnus-newsgroup-unfetched)))
10437             ;; We actually mark all articles as canceled, which we
10438             ;; have to do when using auto-expiry or adaptive scoring.
10439             (gnus-summary-show-all-threads)
10440             (if (and to-here reverse)
10441                 (progn
10442                   (goto-char to-here)
10443                   (gnus-summary-mark-current-read-and-unread-as-read
10444                    gnus-catchup-mark)
10445                   (while (gnus-summary-find-next (not all))
10446                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10447               (when (gnus-summary-first-subject (not all))
10448                 (while (and
10449                         (if to-here (< (point) to-here) t)
10450                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10451                         (gnus-summary-find-next (not all))))))
10452             (gnus-set-mode-line 'summary))
10453           t))
10454     (gnus-summary-position-point)))
10455
10456 (defun gnus-summary-catchup-to-here (&optional all)
10457   "Mark all unticked articles before the current one as read.
10458 If ALL is non-nil, also mark ticked and dormant articles as read."
10459   (interactive "P")
10460   (save-excursion
10461     (gnus-save-hidden-threads
10462       (let ((beg (point)))
10463         ;; We check that there are unread articles.
10464         (when (or all (gnus-summary-find-prev))
10465           (gnus-summary-catchup all t beg)))))
10466   (gnus-summary-position-point))
10467
10468 (defun gnus-summary-catchup-from-here (&optional all)
10469   "Mark all unticked articles after (and including) the current one as read.
10470 If ALL is non-nil, also mark ticked and dormant articles as read."
10471   (interactive "P")
10472   (save-excursion
10473     (gnus-save-hidden-threads
10474       (let ((beg (point)))
10475         ;; We check that there are unread articles.
10476         (when (or all (gnus-summary-find-next))
10477           (gnus-summary-catchup all t beg nil t)))))
10478   (gnus-summary-position-point))
10479
10480 (defun gnus-summary-catchup-all (&optional quietly)
10481   "Mark all articles in this newsgroup as read.
10482 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10483 instead, which marks only unread articles as read."
10484   (interactive "P")
10485   (gnus-summary-catchup t quietly))
10486
10487 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10488   "Mark all unread articles in this group as read, then exit.
10489 If prefix argument ALL is non-nil, all articles are marked as read.
10490 If QUIETLY is non-nil, no questions will be asked."
10491   (interactive "P")
10492   (when (gnus-summary-catchup all quietly nil 'fast)
10493     ;; Select next newsgroup or exit.
10494     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10495              (eq gnus-auto-select-next 'quietly))
10496         (gnus-summary-next-group nil)
10497       (gnus-summary-exit))))
10498
10499 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10500   "Mark all articles in this newsgroup as read, and then exit.
10501 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10502 instead, which marks only unread articles as read."
10503   (interactive "P")
10504   (gnus-summary-catchup-and-exit t quietly))
10505
10506 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10507   "Mark all articles in this group as read and select the next group.
10508 If given a prefix, mark all articles, unread as well as ticked, as
10509 read."
10510   (interactive "P")
10511   (save-excursion
10512     (gnus-summary-catchup all))
10513   (gnus-summary-next-group))
10514
10515 ;;;
10516 ;;; with article
10517 ;;;
10518
10519 (defmacro gnus-with-article (article &rest forms)
10520   "Select ARTICLE and perform FORMS in the original article buffer.
10521 Then replace the article with the result."
10522   `(progn
10523      ;; We don't want the article to be marked as read.
10524      (let (gnus-mark-article-hook)
10525        (gnus-summary-select-article t t nil ,article))
10526      (set-buffer gnus-original-article-buffer)
10527      ,@forms
10528      (if (not (gnus-check-backend-function
10529                'request-replace-article (car gnus-article-current)))
10530          (gnus-message 5 "Read-only group; not replacing")
10531        (unless (gnus-request-replace-article
10532                 ,article (car gnus-article-current)
10533                 (current-buffer) t)
10534          (error "Couldn't replace article")))
10535      ;; The cache and backlog have to be flushed somewhat.
10536      (when gnus-keep-backlog
10537        (gnus-backlog-remove-article
10538         (car gnus-article-current) (cdr gnus-article-current)))
10539      (when gnus-use-cache
10540        (gnus-cache-update-article
10541         (car gnus-article-current) (cdr gnus-article-current)))))
10542
10543 (put 'gnus-with-article 'lisp-indent-function 1)
10544 (put 'gnus-with-article 'edebug-form-spec '(form body))
10545
10546 ;; Thread-based commands.
10547
10548 (defun gnus-summary-articles-in-thread (&optional article)
10549   "Return a list of all articles in the current thread.
10550 If ARTICLE is non-nil, return all articles in the thread that starts
10551 with that article."
10552   (let* ((article (or article (gnus-summary-article-number)))
10553          (data (gnus-data-find-list article))
10554          (top-level (gnus-data-level (car data)))
10555          (top-subject
10556           (cond ((null gnus-thread-operation-ignore-subject)
10557                  (gnus-simplify-subject-re
10558                   (mail-header-subject (gnus-data-header (car data)))))
10559                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10560                  (gnus-simplify-subject-fuzzy
10561                   (mail-header-subject (gnus-data-header (car data)))))
10562                 (t nil)))
10563          (end-point (save-excursion
10564                       (if (gnus-summary-go-to-next-thread)
10565                           (point) (point-max))))
10566          articles)
10567     (while (and data
10568                 (< (gnus-data-pos (car data)) end-point))
10569       (when (or (not top-subject)
10570                 (string= top-subject
10571                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10572                              (gnus-simplify-subject-fuzzy
10573                               (mail-header-subject
10574                                (gnus-data-header (car data))))
10575                            (gnus-simplify-subject-re
10576                             (mail-header-subject
10577                              (gnus-data-header (car data)))))))
10578         (push (gnus-data-number (car data)) articles))
10579       (unless (and (setq data (cdr data))
10580                    (> (gnus-data-level (car data)) top-level))
10581         (setq data nil)))
10582     ;; Return the list of articles.
10583     (nreverse articles)))
10584
10585 (defun gnus-summary-rethread-current ()
10586   "Rethread the thread the current article is part of."
10587   (interactive)
10588   (let* ((gnus-show-threads t)
10589          (article (gnus-summary-article-number))
10590          (id (mail-header-id (gnus-summary-article-header)))
10591          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10592     (unless id
10593       (error "No article on the current line"))
10594     (gnus-rebuild-thread id)
10595     (gnus-summary-goto-subject article)))
10596
10597 (defun gnus-summary-reparent-thread ()
10598   "Make the current article child of the marked (or previous) article.
10599
10600 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10601 is non-nil or the Subject: of both articles are the same."
10602   (interactive)
10603   (unless (not (gnus-group-read-only-p))
10604     (error "The current newsgroup does not support article editing"))
10605   (unless (<= (length gnus-newsgroup-processable) 1)
10606     (error "No more than one article may be marked"))
10607   (save-window-excursion
10608     (let ((gnus-article-buffer " *reparent*")
10609           (current-article (gnus-summary-article-number))
10610           ;; First grab the marked article, otherwise one line up.
10611           (parent-article (if (not (null gnus-newsgroup-processable))
10612                               (car gnus-newsgroup-processable)
10613                             (save-excursion
10614                               (if (eq (forward-line -1) 0)
10615                                   (gnus-summary-article-number)
10616                                 (error "Beginning of summary buffer"))))))
10617       (unless (not (eq current-article parent-article))
10618         (error "An article may not be self-referential"))
10619       (let ((message-id (mail-header-id
10620                          (gnus-summary-article-header parent-article))))
10621         (unless (and message-id (not (equal message-id "")))
10622           (error "No message-id in desired parent"))
10623         (gnus-with-article current-article
10624           (save-restriction
10625             (goto-char (point-min))
10626             (message-narrow-to-head)
10627             (if (re-search-forward "^References: " nil t)
10628                 (progn
10629                   (re-search-forward "^[^ \t]" nil t)
10630                   (forward-line -1)
10631                   (end-of-line)
10632                   (insert " " message-id))
10633               (insert "References: " message-id "\n"))))
10634         (set-buffer gnus-summary-buffer)
10635         (gnus-summary-unmark-all-processable)
10636         (gnus-summary-update-article current-article)
10637         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10638             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10639         (gnus-summary-rethread-current)
10640         (gnus-message 3 "Article %d is now the child of article %d"
10641                       current-article parent-article)))))
10642
10643 (defun gnus-summary-toggle-threads (&optional arg)
10644   "Toggle showing conversation threads.
10645 If ARG is positive number, turn showing conversation threads on."
10646   (interactive "P")
10647   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10648     (setq gnus-show-threads
10649           (if (null arg) (not gnus-show-threads)
10650             (> (prefix-numeric-value arg) 0)))
10651     (gnus-summary-prepare)
10652     (gnus-summary-goto-subject current)
10653     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10654     (gnus-summary-position-point)))
10655
10656 (defun gnus-summary-show-all-threads ()
10657   "Show all threads."
10658   (interactive)
10659   (save-excursion
10660     (let ((buffer-read-only nil))
10661       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10662   (gnus-summary-position-point))
10663
10664 (defun gnus-summary-show-thread ()
10665   "Show thread subtrees.
10666 Returns nil if no thread was there to be shown."
10667   (interactive)
10668   (let ((buffer-read-only nil)
10669         (orig (point))
10670         (end (point-at-eol))
10671         ;; Leave point at bol
10672         (beg (progn (beginning-of-line) (point))))
10673     (prog1
10674         ;; Any hidden lines here?
10675         (search-forward "\r" end t)
10676       (subst-char-in-region beg end ?\^M ?\n t)
10677       (goto-char orig)
10678       (gnus-summary-position-point))))
10679
10680 (defun gnus-summary-maybe-hide-threads ()
10681   "If requested, hide the threads that should be hidden."
10682   (when (and gnus-show-threads
10683              gnus-thread-hide-subtree)
10684     (gnus-summary-hide-all-threads
10685      (if (or (consp gnus-thread-hide-subtree)
10686              (functionp gnus-thread-hide-subtree))
10687          (gnus-make-predicate gnus-thread-hide-subtree)
10688        nil))))
10689
10690 ;;; Hiding predicates.
10691
10692 (defun gnus-article-unread-p (header)
10693   (memq (mail-header-number header) gnus-newsgroup-unreads))
10694
10695 (defun gnus-article-unseen-p (header)
10696   (memq (mail-header-number header) gnus-newsgroup-unseen))
10697
10698 (defun gnus-map-articles (predicate articles)
10699   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10700   (apply 'gnus-or (mapcar predicate
10701                           (mapcar 'gnus-summary-article-header articles))))
10702
10703 (defun gnus-summary-hide-all-threads (&optional predicate)
10704   "Hide all thread subtrees.
10705 If PREDICATE is supplied, threads that satisfy this predicate
10706 will not be hidden."
10707   (interactive)
10708   (save-excursion
10709     (goto-char (point-min))
10710     (let ((end nil))
10711       (while (not end)
10712         (when (or (not predicate)
10713                   (gnus-map-articles
10714                    predicate (gnus-summary-article-children)))
10715             (gnus-summary-hide-thread))
10716         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10717   (gnus-summary-position-point))
10718
10719 (defun gnus-summary-hide-thread ()
10720   "Hide thread subtrees.
10721 If PREDICATE is supplied, threads that satisfy this predicate
10722 will not be hidden.
10723 Returns nil if no threads were there to be hidden."
10724   (interactive)
10725   (let ((buffer-read-only nil)
10726         (start (point))
10727         (article (gnus-summary-article-number)))
10728     (goto-char start)
10729     ;; Go forward until either the buffer ends or the subthread
10730     ;; ends.
10731     (when (and (not (eobp))
10732                (or (zerop (gnus-summary-next-thread 1 t))
10733                    (goto-char (point-max))))
10734       (prog1
10735           (if (and (> (point) start)
10736                    (search-backward "\n" start t))
10737               (progn
10738                 (subst-char-in-region start (point) ?\n ?\^M)
10739                 (gnus-summary-goto-subject article))
10740             (goto-char start)
10741             nil)))))
10742
10743 (defun gnus-summary-go-to-next-thread (&optional previous)
10744   "Go to the same level (or less) next thread.
10745 If PREVIOUS is non-nil, go to previous thread instead.
10746 Return the article number moved to, or nil if moving was impossible."
10747   (let ((level (gnus-summary-thread-level))
10748         (way (if previous -1 1))
10749         (beg (point)))
10750     (forward-line way)
10751     (while (and (not (eobp))
10752                 (< level (gnus-summary-thread-level)))
10753       (forward-line way))
10754     (if (eobp)
10755         (progn
10756           (goto-char beg)
10757           nil)
10758       (setq beg (point))
10759       (prog1
10760           (gnus-summary-article-number)
10761         (goto-char beg)))))
10762
10763 (defun gnus-summary-next-thread (n &optional silent)
10764   "Go to the same level next N'th thread.
10765 If N is negative, search backward instead.
10766 Returns the difference between N and the number of skips actually
10767 done.
10768
10769 If SILENT, don't output messages."
10770   (interactive "p")
10771   (let ((backward (< n 0))
10772         (n (abs n)))
10773     (while (and (> n 0)
10774                 (gnus-summary-go-to-next-thread backward))
10775       (decf n))
10776     (unless silent
10777       (gnus-summary-position-point))
10778     (when (and (not silent) (/= 0 n))
10779       (gnus-message 7 "No more threads"))
10780     n))
10781
10782 (defun gnus-summary-prev-thread (n)
10783   "Go to the same level previous N'th thread.
10784 Returns the difference between N and the number of skips actually
10785 done."
10786   (interactive "p")
10787   (gnus-summary-next-thread (- n)))
10788
10789 (defun gnus-summary-go-down-thread ()
10790   "Go down one level in the current thread."
10791   (let ((children (gnus-summary-article-children)))
10792     (when children
10793       (gnus-summary-goto-subject (car children)))))
10794
10795 (defun gnus-summary-go-up-thread ()
10796   "Go up one level in the current thread."
10797   (let ((parent (gnus-summary-article-parent)))
10798     (when parent
10799       (gnus-summary-goto-subject parent))))
10800
10801 (defun gnus-summary-down-thread (n)
10802   "Go down thread N steps.
10803 If N is negative, go up instead.
10804 Returns the difference between N and how many steps down that were
10805 taken."
10806   (interactive "p")
10807   (let ((up (< n 0))
10808         (n (abs n)))
10809     (while (and (> n 0)
10810                 (if up (gnus-summary-go-up-thread)
10811                   (gnus-summary-go-down-thread)))
10812       (setq n (1- n)))
10813     (gnus-summary-position-point)
10814     (when (/= 0 n)
10815       (gnus-message 7 "Can't go further"))
10816     n))
10817
10818 (defun gnus-summary-up-thread (n)
10819   "Go up thread N steps.
10820 If N is negative, go down instead.
10821 Returns the difference between N and how many steps down that were
10822 taken."
10823   (interactive "p")
10824   (gnus-summary-down-thread (- n)))
10825
10826 (defun gnus-summary-top-thread ()
10827   "Go to the top of the thread."
10828   (interactive)
10829   (while (gnus-summary-go-up-thread))
10830   (gnus-summary-article-number))
10831
10832 (defun gnus-summary-kill-thread (&optional unmark)
10833   "Mark articles under current thread as read.
10834 If the prefix argument is positive, remove any kinds of marks.
10835 If the prefix argument is negative, tick articles instead."
10836   (interactive "P")
10837   (when unmark
10838     (setq unmark (prefix-numeric-value unmark)))
10839   (let ((articles (gnus-summary-articles-in-thread)))
10840     (save-excursion
10841       ;; Expand the thread.
10842       (gnus-summary-show-thread)
10843       ;; Mark all the articles.
10844       (while articles
10845         (gnus-summary-goto-subject (car articles))
10846         (cond ((null unmark)
10847                (gnus-summary-mark-article-as-read gnus-killed-mark))
10848               ((> unmark 0)
10849                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10850               (t
10851                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10852         (setq articles (cdr articles))))
10853     ;; Hide killed subtrees.
10854     (and (null unmark)
10855          gnus-thread-hide-killed
10856          (gnus-summary-hide-thread))
10857     ;; If marked as read, go to next unread subject.
10858     (when (null unmark)
10859       ;; Go to next unread subject.
10860       (gnus-summary-next-subject 1 t)))
10861   (gnus-set-mode-line 'summary))
10862
10863 ;; Summary sorting commands
10864
10865 (defun gnus-summary-sort-by-number (&optional reverse)
10866   "Sort the summary buffer by article number.
10867 Argument REVERSE means reverse order."
10868   (interactive "P")
10869   (gnus-summary-sort 'number reverse))
10870
10871 (defun gnus-summary-sort-by-random (&optional reverse)
10872   "Randomize the order in the summary buffer.
10873 Argument REVERSE means to randomize in reverse order."
10874   (interactive "P")
10875   (gnus-summary-sort 'random reverse))
10876
10877 (defun gnus-summary-sort-by-author (&optional reverse)
10878   "Sort the summary buffer by author name alphabetically.
10879 If `case-fold-search' is non-nil, case of letters is ignored.
10880 Argument REVERSE means reverse order."
10881   (interactive "P")
10882   (gnus-summary-sort 'author reverse))
10883
10884 (defun gnus-summary-sort-by-subject (&optional reverse)
10885   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10886 If `case-fold-search' is non-nil, case of letters is ignored.
10887 Argument REVERSE means reverse order."
10888   (interactive "P")
10889   (gnus-summary-sort 'subject reverse))
10890
10891 (defun gnus-summary-sort-by-date (&optional reverse)
10892   "Sort the summary buffer by date.
10893 Argument REVERSE means reverse order."
10894   (interactive "P")
10895   (gnus-summary-sort 'date reverse))
10896
10897 (defun gnus-summary-sort-by-score (&optional reverse)
10898   "Sort the summary buffer by score.
10899 Argument REVERSE means reverse order."
10900   (interactive "P")
10901   (gnus-summary-sort 'score reverse))
10902
10903 (defun gnus-summary-sort-by-lines (&optional reverse)
10904   "Sort the summary buffer by the number of lines.
10905 Argument REVERSE means reverse order."
10906   (interactive "P")
10907   (gnus-summary-sort 'lines reverse))
10908
10909 (defun gnus-summary-sort-by-chars (&optional reverse)
10910   "Sort the summary buffer by article length.
10911 Argument REVERSE means reverse order."
10912   (interactive "P")
10913   (gnus-summary-sort 'chars reverse))
10914
10915 (defun gnus-summary-sort-by-original (&optional reverse)
10916   "Sort the summary buffer using the default sorting method.
10917 Argument REVERSE means reverse order."
10918   (interactive "P")
10919   (let* ((buffer-read-only)
10920          (gnus-summary-prepare-hook nil))
10921     ;; We do the sorting by regenerating the threads.
10922     (gnus-summary-prepare)
10923     ;; Hide subthreads if needed.
10924     (gnus-summary-maybe-hide-threads)))
10925
10926 (defun gnus-summary-sort (predicate reverse)
10927   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10928   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10929          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10930          (gnus-thread-sort-functions
10931           (if (not reverse)
10932               thread
10933             `(lambda (t1 t2)
10934                (,thread t2 t1))))
10935          (gnus-sort-gathered-threads-function
10936           gnus-thread-sort-functions)
10937          (gnus-article-sort-functions
10938           (if (not reverse)
10939               article
10940             `(lambda (t1 t2)
10941                (,article t2 t1))))
10942          (buffer-read-only)
10943          (gnus-summary-prepare-hook nil))
10944     ;; We do the sorting by regenerating the threads.
10945     (gnus-summary-prepare)
10946     ;; Hide subthreads if needed.
10947     (gnus-summary-maybe-hide-threads)))
10948
10949 ;; Summary saving commands.
10950
10951 (defun gnus-summary-save-article (&optional n not-saved)
10952   "Save the current article using the default saver function.
10953 If N is a positive number, save the N next articles.
10954 If N is a negative number, save the N previous articles.
10955 If N is nil and any articles have been marked with the process mark,
10956 save those articles instead.
10957 The variable `gnus-default-article-saver' specifies the saver function."
10958   (interactive "P")
10959   (let* ((articles (gnus-summary-work-articles n))
10960          (save-buffer (save-excursion
10961                         (nnheader-set-temp-buffer " *Gnus Save*")))
10962          (num (length articles))
10963          header file)
10964     (dolist (article articles)
10965       (setq header (gnus-summary-article-header article))
10966       (if (not (vectorp header))
10967           ;; This is a pseudo-article.
10968           (if (assq 'name header)
10969               (gnus-copy-file (cdr (assq 'name header)))
10970             (gnus-message 1 "Article %d is unsaveable" article))
10971         ;; This is a real article.
10972         (save-window-excursion
10973           (let ((gnus-display-mime-function nil)
10974                 (gnus-article-prepare-hook nil))
10975             (gnus-summary-select-article t nil nil article)))
10976         (save-excursion
10977           (set-buffer save-buffer)
10978           (erase-buffer)
10979           (insert-buffer-substring gnus-original-article-buffer))
10980         (setq file (gnus-article-save save-buffer file num))
10981         (gnus-summary-remove-process-mark article)
10982         (unless not-saved
10983           (gnus-summary-set-saved-mark article))))
10984     (gnus-kill-buffer save-buffer)
10985     (gnus-summary-position-point)
10986     (gnus-set-mode-line 'summary)
10987     n))
10988
10989 (defun gnus-summary-pipe-output (&optional arg headers)
10990   "Pipe the current article to a subprocess.
10991 If N is a positive number, pipe the N next articles.
10992 If N is a negative number, pipe the N previous articles.
10993 If N is nil and any articles have been marked with the process mark,
10994 pipe those articles instead.
10995 If HEADERS (the symbolic prefix), include the headers, too."
10996   (interactive (gnus-interactive "P\ny"))
10997   (require 'gnus-art)
10998   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10999         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11000     (gnus-summary-save-article arg t))
11001   (let ((buffer (get-buffer "*Shell Command Output*")))
11002     (when (and buffer
11003                (not (zerop (buffer-size buffer))))
11004       (gnus-configure-windows 'pipe))))
11005
11006 (defun gnus-summary-save-article-mail (&optional arg)
11007   "Append the current article to an mail file.
11008 If N is a positive number, save the N next articles.
11009 If N is a negative number, save the N previous articles.
11010 If N is nil and any articles have been marked with the process mark,
11011 save those articles instead."
11012   (interactive "P")
11013   (require 'gnus-art)
11014   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11015     (gnus-summary-save-article arg)))
11016
11017 (defun gnus-summary-save-article-rmail (&optional arg)
11018   "Append the current article to an rmail file.
11019 If N is a positive number, save the N next articles.
11020 If N is a negative number, save the N previous articles.
11021 If N is nil and any articles have been marked with the process mark,
11022 save those articles instead."
11023   (interactive "P")
11024   (require 'gnus-art)
11025   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11026     (gnus-summary-save-article arg)))
11027
11028 (defun gnus-summary-save-article-file (&optional arg)
11029   "Append the current article to a file.
11030 If N is a positive number, save the N next articles.
11031 If N is a negative number, save the N previous articles.
11032 If N is nil and any articles have been marked with the process mark,
11033 save those articles instead."
11034   (interactive "P")
11035   (require 'gnus-art)
11036   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11037     (gnus-summary-save-article arg)))
11038
11039 (defun gnus-summary-write-article-file (&optional arg)
11040   "Write the current article to a file, deleting the previous file.
11041 If N is a positive number, save the N next articles.
11042 If N is a negative number, save the N previous articles.
11043 If N is nil and any articles have been marked with the process mark,
11044 save those articles instead."
11045   (interactive "P")
11046   (require 'gnus-art)
11047   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11048     (gnus-summary-save-article arg)))
11049
11050 (defun gnus-summary-save-article-body-file (&optional arg)
11051   "Append the current article body to a file.
11052 If N is a positive number, save the N next articles.
11053 If N is a negative number, save the N previous articles.
11054 If N is nil and any articles have been marked with the process mark,
11055 save those articles instead."
11056   (interactive "P")
11057   (require 'gnus-art)
11058   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11059     (gnus-summary-save-article arg)))
11060
11061 (defun gnus-summary-muttprint (&optional arg)
11062   "Print the current article using Muttprint.
11063 If N is a positive number, save the N next articles.
11064 If N is a negative number, save the N previous articles.
11065 If N is nil and any articles have been marked with the process mark,
11066 save those articles instead."
11067   (interactive "P")
11068   (require 'gnus-art)
11069   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11070     (gnus-summary-save-article arg t)))
11071
11072 (defun gnus-summary-pipe-message (program)
11073   "Pipe the current article through PROGRAM."
11074   (interactive "sProgram: ")
11075   (gnus-summary-select-article)
11076   (let ((mail-header-separator ""))
11077     (gnus-eval-in-buffer-window gnus-article-buffer
11078       (save-restriction
11079         (widen)
11080         (let ((start (window-start))
11081               buffer-read-only)
11082           (message-pipe-buffer-body program)
11083           (set-window-start (get-buffer-window (current-buffer)) start))))))
11084
11085 (defun gnus-get-split-value (methods)
11086   "Return a value based on the split METHODS."
11087   (let (split-name method result match)
11088     (when methods
11089       (save-excursion
11090         (set-buffer gnus-original-article-buffer)
11091         (save-restriction
11092           (nnheader-narrow-to-headers)
11093           (while (and methods (not split-name))
11094             (goto-char (point-min))
11095             (setq method (pop methods))
11096             (setq match (car method))
11097             (when (cond
11098                    ((stringp match)
11099                     ;; Regular expression.
11100                     (ignore-errors
11101                       (re-search-forward match nil t)))
11102                    ((functionp match)
11103                     ;; Function.
11104                     (save-restriction
11105                       (widen)
11106                       (setq result (funcall match gnus-newsgroup-name))))
11107                    ((consp match)
11108                     ;; Form.
11109                     (save-restriction
11110                       (widen)
11111                       (setq result (eval match)))))
11112               (setq split-name (cdr method))
11113               (cond ((stringp result)
11114                      (push (expand-file-name
11115                             result gnus-article-save-directory)
11116                            split-name))
11117                     ((consp result)
11118                      (setq split-name (append result split-name)))))))))
11119     (nreverse split-name)))
11120
11121 (defun gnus-valid-move-group-p (group)
11122   (and (boundp group)
11123        (symbol-name group)
11124        (symbol-value group)
11125        (gnus-get-function (gnus-find-method-for-group
11126                            (symbol-name group)) 'request-accept-article t)))
11127
11128 (defun gnus-read-move-group-name (prompt default articles prefix)
11129   "Read a group name."
11130   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11131          (minibuffer-confirm-incomplete nil) ; XEmacs
11132          (prom
11133           (format "%s %s to:"
11134                   prompt
11135                   (if (> (length articles) 1)
11136                       (format "these %d articles" (length articles))
11137                     "this article")))
11138          (to-newsgroup
11139           (cond
11140            ((null split-name)
11141             (gnus-completing-read-with-default
11142              default prom
11143              gnus-active-hashtb
11144              'gnus-valid-move-group-p
11145              nil prefix
11146              'gnus-group-history))
11147            ((= 1 (length split-name))
11148             (gnus-completing-read-with-default
11149              (car split-name) prom
11150              gnus-active-hashtb
11151              'gnus-valid-move-group-p
11152              nil nil
11153              'gnus-group-history))
11154            (t
11155             (gnus-completing-read-with-default
11156              nil prom
11157              (mapcar 'list (nreverse split-name))
11158              nil nil nil
11159              'gnus-group-history))))
11160          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11161     (when to-newsgroup
11162       (if (or (string= to-newsgroup "")
11163               (string= to-newsgroup prefix))
11164           (setq to-newsgroup default))
11165       (unless to-newsgroup
11166         (error "No group name entered"))
11167       (or (gnus-active to-newsgroup)
11168           (gnus-activate-group to-newsgroup nil nil to-method)
11169           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11170                                      to-newsgroup))
11171               (or (and (gnus-request-create-group to-newsgroup to-method)
11172                        (gnus-activate-group
11173                         to-newsgroup nil nil to-method)
11174                        (gnus-subscribe-group to-newsgroup))
11175                   (error "Couldn't create group %s" to-newsgroup)))
11176           (error "No such group: %s" to-newsgroup)))
11177     to-newsgroup))
11178
11179 (defun gnus-summary-save-parts (type dir n &optional reverse)
11180   "Save parts matching TYPE to DIR.
11181 If REVERSE, save parts that do not match TYPE."
11182   (interactive
11183    (list (read-string "Save parts of type: "
11184                       (or (car gnus-summary-save-parts-type-history)
11185                           gnus-summary-save-parts-default-mime)
11186                       'gnus-summary-save-parts-type-history)
11187          (setq gnus-summary-save-parts-last-directory
11188                (read-file-name "Save to directory: "
11189                                gnus-summary-save-parts-last-directory
11190                                nil t))
11191          current-prefix-arg))
11192   (gnus-summary-iterate n
11193     (let ((gnus-display-mime-function nil)
11194           (gnus-inhibit-treatment t))
11195       (gnus-summary-select-article))
11196     (save-excursion
11197       (set-buffer gnus-article-buffer)
11198       (let ((handles (or gnus-article-mime-handles
11199                          (mm-dissect-buffer nil gnus-article-loose-mime)
11200                          (and gnus-article-emulate-mime
11201                               (mm-uu-dissect)))))
11202         (when handles
11203           (gnus-summary-save-parts-1 type dir handles reverse)
11204           (unless gnus-article-mime-handles ;; Don't destroy this case.
11205             (mm-destroy-parts handles)))))))
11206
11207 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11208   (if (stringp (car handle))
11209       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11210               (cdr handle))
11211     (when (if reverse
11212               (not (string-match type (mm-handle-media-type handle)))
11213             (string-match type (mm-handle-media-type handle)))
11214       (let ((file (expand-file-name
11215                    (gnus-map-function
11216                     mm-file-name-rewrite-functions
11217                     (file-name-nondirectory
11218                      (or
11219                       (mail-content-type-get
11220                        (mm-handle-disposition handle) 'filename)
11221                       (mail-content-type-get
11222                        (mm-handle-type handle) 'name)
11223                       (concat gnus-newsgroup-name
11224                               "." (number-to-string
11225                                    (cdr gnus-article-current))))))
11226                    dir)))
11227         (unless (file-exists-p file)
11228           (mm-save-part-to-file handle file))))))
11229
11230 ;; Summary extract commands
11231
11232 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11233   (let ((buffer-read-only nil)
11234         (article (gnus-summary-article-number))
11235         after-article b e)
11236     (unless (gnus-summary-goto-subject article)
11237       (error "No such article: %d" article))
11238     (gnus-summary-position-point)
11239     ;; If all commands are to be bunched up on one line, we collect
11240     ;; them here.
11241     (unless gnus-view-pseudos-separately
11242       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11243             files action)
11244         (while ps
11245           (setq action (cdr (assq 'action (car ps))))
11246           (setq files (list (cdr (assq 'name (car ps)))))
11247           (while (and ps (cdr ps)
11248                       (string= (or action "1")
11249                                (or (cdr (assq 'action (cadr ps))) "2")))
11250             (push (cdr (assq 'name (cadr ps))) files)
11251             (setcdr ps (cddr ps)))
11252           (when files
11253             (when (not (string-match "%s" action))
11254               (push " " files))
11255             (push " " files)
11256             (when (assq 'execute (car ps))
11257               (setcdr (assq 'execute (car ps))
11258                       (funcall (if (string-match "%s" action)
11259                                    'format 'concat)
11260                                action
11261                                (mapconcat
11262                                 (lambda (f)
11263                                   (if (equal f " ")
11264                                       f
11265                                     (shell-quote-argument f)))
11266                                 files " ")))))
11267           (setq ps (cdr ps)))))
11268     (if (and gnus-view-pseudos (not not-view))
11269         (while pslist
11270           (when (assq 'execute (car pslist))
11271             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11272                                   (eq gnus-view-pseudos 'not-confirm)))
11273           (setq pslist (cdr pslist)))
11274       (save-excursion
11275         (while pslist
11276           (setq after-article (or (cdr (assq 'article (car pslist)))
11277                                   (gnus-summary-article-number)))
11278           (gnus-summary-goto-subject after-article)
11279           (forward-line 1)
11280           (setq b (point))
11281           (insert "    " (file-name-nondirectory
11282                           (cdr (assq 'name (car pslist))))
11283                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11284           (setq e (point))
11285           (forward-line -1)             ; back to `b'
11286           (gnus-add-text-properties
11287            b (1- e) (list 'gnus-number gnus-reffed-article-number
11288                           gnus-mouse-face-prop gnus-mouse-face))
11289           (gnus-data-enter
11290            after-article gnus-reffed-article-number
11291            gnus-unread-mark b (car pslist) 0 (- e b))
11292           (setq gnus-newsgroup-unreads
11293                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11294                                          gnus-reffed-article-number))
11295           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11296           (setq pslist (cdr pslist)))))))
11297
11298 (defun gnus-pseudos< (p1 p2)
11299   (let ((c1 (cdr (assq 'action p1)))
11300         (c2 (cdr (assq 'action p2))))
11301     (and c1 c2 (string< c1 c2))))
11302
11303 (defun gnus-request-pseudo-article (props)
11304   (cond ((assq 'execute props)
11305          (gnus-execute-command (cdr (assq 'execute props)))))
11306   (let ((gnus-current-article (gnus-summary-article-number)))
11307     (gnus-run-hooks 'gnus-mark-article-hook)))
11308
11309 (defun gnus-execute-command (command &optional automatic)
11310   (save-excursion
11311     (gnus-article-setup-buffer)
11312     (set-buffer gnus-article-buffer)
11313     (setq buffer-read-only nil)
11314     (let ((command (if automatic command
11315                      (read-string "Command: " (cons command 0)))))
11316       (erase-buffer)
11317       (insert "$ " command "\n\n")
11318       (if gnus-view-pseudo-asynchronously
11319           (start-process "gnus-execute" (current-buffer) shell-file-name
11320                          shell-command-switch command)
11321         (call-process shell-file-name nil t nil
11322                       shell-command-switch command)))))
11323
11324 ;; Summary kill commands.
11325
11326 (defun gnus-summary-edit-global-kill (article)
11327   "Edit the \"global\" kill file."
11328   (interactive (list (gnus-summary-article-number)))
11329   (gnus-group-edit-global-kill article))
11330
11331 (defun gnus-summary-edit-local-kill ()
11332   "Edit a local kill file applied to the current newsgroup."
11333   (interactive)
11334   (setq gnus-current-headers (gnus-summary-article-header))
11335   (gnus-group-edit-local-kill
11336    (gnus-summary-article-number) gnus-newsgroup-name))
11337
11338 ;;; Header reading.
11339
11340 (defun gnus-read-header (id &optional header)
11341   "Read the headers of article ID and enter them into the Gnus system."
11342   (let ((group gnus-newsgroup-name)
11343         (gnus-override-method
11344          (or
11345           gnus-override-method
11346           (and (gnus-news-group-p gnus-newsgroup-name)
11347                (car (gnus-refer-article-methods)))))
11348         where)
11349     ;; First we check to see whether the header in question is already
11350     ;; fetched.
11351     (if (stringp id)
11352         ;; This is a Message-ID.
11353         (setq header (or header (gnus-id-to-header id)))
11354       ;; This is an article number.
11355       (setq header (or header (gnus-summary-article-header id))))
11356     (if (and header
11357              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11358         ;; We have found the header.
11359         header
11360       ;; We have to really fetch the header to this article.
11361       (save-excursion
11362         (set-buffer nntp-server-buffer)
11363         (when (setq where (gnus-request-head id group))
11364           (nnheader-fold-continuation-lines)
11365           (goto-char (point-max))
11366           (insert ".\n")
11367           (goto-char (point-min))
11368           (insert "211 ")
11369           (princ (cond
11370                   ((numberp id) id)
11371                   ((cdr where) (cdr where))
11372                   (header (mail-header-number header))
11373                   (t gnus-reffed-article-number))
11374                  (current-buffer))
11375           (insert " Article retrieved.\n"))
11376         (if (or (not where)
11377                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11378             ()                          ; Malformed head.
11379           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11380             (when (and (stringp id)
11381                        (not (string= (gnus-group-real-name group)
11382                                      (car where))))
11383               ;; If we fetched by Message-ID and the article came
11384               ;; from a different group, we fudge some bogus article
11385               ;; numbers for this article.
11386               (mail-header-set-number header gnus-reffed-article-number))
11387             (save-excursion
11388               (set-buffer gnus-summary-buffer)
11389               (decf gnus-reffed-article-number)
11390               (gnus-remove-header (mail-header-number header))
11391               (push header gnus-newsgroup-headers)
11392               (setq gnus-current-headers header)
11393               (push (mail-header-number header) gnus-newsgroup-limit)))
11394           header)))))
11395
11396 (defun gnus-remove-header (number)
11397   "Remove header NUMBER from `gnus-newsgroup-headers'."
11398   (if (and gnus-newsgroup-headers
11399            (= number (mail-header-number (car gnus-newsgroup-headers))))
11400       (pop gnus-newsgroup-headers)
11401     (let ((headers gnus-newsgroup-headers))
11402       (while (and (cdr headers)
11403                   (not (= number (mail-header-number (cadr headers)))))
11404         (pop headers))
11405       (when (cdr headers)
11406         (setcdr headers (cddr headers))))))
11407
11408 ;;;
11409 ;;; summary highlights
11410 ;;;
11411
11412 (defun gnus-highlight-selected-summary ()
11413   "Highlight selected article in summary buffer."
11414   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11415   (when gnus-summary-selected-face
11416     (save-excursion
11417       (let* ((beg (point-at-bol))
11418              (end (point-at-eol))
11419              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11420              (from (if (get-text-property beg gnus-mouse-face-prop)
11421                        beg
11422                      (or (next-single-property-change
11423                           beg gnus-mouse-face-prop nil end)
11424                          beg)))
11425              (to
11426               (if (= from end)
11427                   (- from 2)
11428                 (or (next-single-property-change
11429                      from gnus-mouse-face-prop nil end)
11430                     end))))
11431         ;; If no mouse-face prop on line we will have to = from = end,
11432         ;; so we highlight the entire line instead.
11433         (when (= (+ to 2) from)
11434           (setq from beg)
11435           (setq to end))
11436         (if gnus-newsgroup-selected-overlay
11437             ;; Move old overlay.
11438             (gnus-move-overlay
11439              gnus-newsgroup-selected-overlay from to (current-buffer))
11440           ;; Create new overlay.
11441           (gnus-overlay-put
11442            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11443            'face gnus-summary-selected-face))))))
11444
11445 (defvar gnus-summary-highlight-line-cached nil)
11446 (defvar gnus-summary-highlight-line-trigger nil)
11447
11448 (defun gnus-summary-highlight-line-0 ()
11449   (if (and (eq gnus-summary-highlight-line-trigger
11450                gnus-summary-highlight)
11451            gnus-summary-highlight-line-cached)
11452       gnus-summary-highlight-line-cached
11453     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11454           gnus-summary-highlight-line-cached
11455           (let* ((cond (list 'cond))
11456                  (c cond)
11457                  (list gnus-summary-highlight))
11458             (while list
11459               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11460                               nil))
11461               (setq c (cdr c)
11462                     list (cdr list)))
11463             (gnus-byte-compile (list 'lambda nil cond))))))
11464
11465 (defun gnus-summary-highlight-line ()
11466   "Highlight current line according to `gnus-summary-highlight'."
11467   (let* ((beg (point-at-bol))
11468          (article (or (gnus-summary-article-number) gnus-current-article))
11469          (score (or (cdr (assq article
11470                                gnus-newsgroup-scored))
11471                     gnus-summary-default-score 0))
11472          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11473          (inhibit-read-only t)
11474          (default gnus-summary-default-score)
11475          (default-high gnus-summary-default-high-score)
11476          (default-low gnus-summary-default-low-score)
11477          (uncached (and gnus-summary-use-undownloaded-faces
11478                         (memq article gnus-newsgroup-undownloaded)
11479                         (not (memq article gnus-newsgroup-cached)))))
11480     (let ((face (funcall (gnus-summary-highlight-line-0))))
11481       (unless (eq face (get-text-property beg 'face))
11482         (gnus-put-text-property-excluding-characters-with-faces
11483          beg (point-at-eol) 'face
11484          (setq face (if (boundp face) (symbol-value face) face)))
11485         (when gnus-summary-highlight-line-function
11486           (funcall gnus-summary-highlight-line-function article face))))))
11487
11488 (defun gnus-update-read-articles (group unread &optional compute)
11489   "Update the list of read articles in GROUP.
11490 UNREAD is a sorted list."
11491   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11492         (info (gnus-get-info group))
11493         (prev 1)
11494         read)
11495     (if (or (not info) (not active))
11496         ;; There is no info on this group if it was, in fact,
11497         ;; killed.  Gnus stores no information on killed groups, so
11498         ;; there's nothing to be done.
11499         ;; One could store the information somewhere temporarily,
11500         ;; perhaps...  Hmmm...
11501         ()
11502       ;; Remove any negative articles numbers.
11503       (while (and unread (< (car unread) 0))
11504         (setq unread (cdr unread)))
11505       ;; Remove any expired article numbers
11506       (while (and unread (< (car unread) (car active)))
11507         (setq unread (cdr unread)))
11508       ;; Compute the ranges of read articles by looking at the list of
11509       ;; unread articles.
11510       (while unread
11511         (when (/= (car unread) prev)
11512           (push (if (= prev (1- (car unread))) prev
11513                   (cons prev (1- (car unread))))
11514                 read))
11515         (setq prev (1+ (car unread)))
11516         (setq unread (cdr unread)))
11517       (when (<= prev (cdr active))
11518         (push (cons prev (cdr active)) read))
11519       (setq read (if (> (length read) 1) (nreverse read) read))
11520       (if compute
11521           read
11522         (save-excursion
11523           (let (setmarkundo)
11524             ;; Propagate the read marks to the backend.
11525             (when (gnus-check-backend-function 'request-set-mark group)
11526               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11527                     (add (gnus-remove-from-range read (gnus-info-read info))))
11528                 (when (or add del)
11529                   (unless (gnus-check-group group)
11530                     (error "Can't open server for %s" group))
11531                   (gnus-request-set-mark
11532                    group (delq nil (list (if add (list add 'add '(read)))
11533                                          (if del (list del 'del '(read))))))
11534                   (setq setmarkundo
11535                         `(gnus-request-set-mark
11536                           ,group
11537                           ',(delq nil (list
11538                                        (if del (list del 'add '(read)))
11539                                        (if add (list add 'del '(read))))))))))
11540             (set-buffer gnus-group-buffer)
11541             (gnus-undo-register
11542               `(progn
11543                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11544                  (gnus-info-set-read ',info ',(gnus-info-read info))
11545                  (gnus-get-unread-articles-in-group ',info
11546                                                     (gnus-active ,group))
11547                  (gnus-group-update-group ,group t)
11548                  ,setmarkundo))))
11549         ;; Enter this list into the group info.
11550         (gnus-info-set-read info read)
11551         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11552         (gnus-get-unread-articles-in-group info (gnus-active group))
11553         t))))
11554
11555 (defun gnus-offer-save-summaries ()
11556   "Offer to save all active summary buffers."
11557   (let (buffers)
11558     ;; Go through all buffers and find all summaries.
11559     (dolist (buffer (buffer-list))
11560       (when (and (setq buffer (buffer-name buffer))
11561                  (string-match "Summary" buffer)
11562                  (with-current-buffer buffer
11563                    ;; We check that this is, indeed, a summary buffer.
11564                    (and (eq major-mode 'gnus-summary-mode)
11565                         ;; Also make sure this isn't bogus.
11566                         gnus-newsgroup-prepared
11567                         ;; Also make sure that this isn't a
11568                         ;; dead summary buffer.
11569                         (not gnus-dead-summary-mode))))
11570         (push buffer buffers)))
11571     ;; Go through all these summary buffers and offer to save them.
11572     (when buffers
11573       (save-excursion
11574         (map-y-or-n-p
11575          "Update summary buffer %s? "
11576          (lambda (buf)
11577            (switch-to-buffer buf)
11578            (gnus-summary-exit))
11579          buffers)))))
11580
11581
11582 ;;; @ for mime-partial
11583 ;;;
11584
11585 (defun gnus-request-partial-message ()
11586   (save-excursion
11587     (let ((number (gnus-summary-article-number))
11588           (group gnus-newsgroup-name)
11589           (mother gnus-article-buffer))
11590       (set-buffer (get-buffer-create " *Partial Article*"))
11591       (erase-buffer)
11592       (setq mime-preview-buffer mother)
11593       (gnus-request-article-this-buffer number group)
11594       (mime-parse-buffer)
11595       )))
11596
11597 (autoload 'mime-combine-message/partial-pieces-automatically
11598   "mime-partial"
11599   "Internal method to combine message/partial messages automatically.")
11600
11601 (mime-add-condition
11602  'action '((type . message)(subtype . partial)
11603            (major-mode . gnus-original-article-mode)
11604            (method . mime-combine-message/partial-pieces-automatically)
11605            (summary-buffer-exp . gnus-summary-buffer)
11606            (request-partial-message-method . gnus-request-partial-message)
11607            ))
11608
11609
11610 ;;; @ for message/rfc822
11611 ;;;
11612
11613 (defun gnus-mime-extract-message/rfc822 (entity situation)
11614   "Burst a forwarded article."
11615   (save-excursion
11616     (set-buffer gnus-summary-buffer)
11617     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11618                                    gnus-newsgroup-name 'gnus-group-history))
11619            (gnus-group-marked (list group))
11620            article info)
11621       (with-temp-buffer
11622         (mime-insert-entity-content entity)
11623         (setq article (gnus-request-accept-article group)))
11624       (when (and (consp article)
11625                  (numberp (setq article (cdr article))))
11626         (setq info (gnus-get-info group))
11627         (gnus-info-set-read info
11628                             (gnus-remove-from-range (gnus-info-read info)
11629                                                     (list article)))
11630         (when (string-equal group gnus-newsgroup-name)
11631           (forward-line 1)
11632           (let (gnus-show-threads)
11633             (gnus-summary-goto-subject article t))
11634           (gnus-summary-clear-mark-forward 1))
11635         (set-buffer gnus-group-buffer)
11636         (gnus-group-get-new-news-this-group nil t)))))
11637
11638 (mime-add-condition
11639  'action '((type . message)(subtype . rfc822)
11640            (major-mode . gnus-original-article-mode)
11641            (method . gnus-mime-extract-message/rfc822)
11642            (mode . "extract")
11643            ))
11644
11645 (mime-add-condition
11646  'action '((type . message)(subtype . news)
11647            (major-mode . gnus-original-article-mode)
11648            (method . gnus-mime-extract-message/rfc822)
11649            (mode . "extract")
11650            ))
11651
11652 (defun gnus-mime-extract-multipart (entity situation)
11653   (let ((children (mime-entity-children entity))
11654         mime-acting-situation-to-override
11655         f)
11656     (while children
11657       (mime-play-entity (car children)
11658                         (cons (assq 'mode situation)
11659                               mime-acting-situation-to-override))
11660       (setq children (cdr children)))
11661     (if (setq f (cdr (assq 'after-method
11662                            mime-acting-situation-to-override)))
11663         (eval f)
11664       )))
11665
11666 (mime-add-condition
11667  'action '((type . multipart)
11668            (method . gnus-mime-extract-multipart)
11669            (mode . "extract")
11670            )
11671  'with-default)
11672
11673
11674 ;;; @ end
11675 ;;;
11676
11677 (defun gnus-summary-inherit-default-charset ()
11678   "Import `default-mime-charset' from summary buffer.
11679 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11680 of FLIM is used."
11681   (if (buffer-live-p gnus-summary-buffer)
11682       (let (d-m-c d-m-c-u)
11683         (with-current-buffer gnus-summary-buffer
11684           (setq d-m-c (if (local-variable-p 'default-mime-charset
11685                                             gnus-summary-buffer)
11686                           default-mime-charset
11687                         t)
11688                 ;; LIMIT
11689                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11690                                               gnus-summary-buffer)
11691                             (symbol-value 'default-mime-charset-unlimited)
11692                           t)))
11693         (if (eq t d-m-c)
11694             (kill-local-variable 'default-mime-charset)
11695           (set (make-local-variable 'default-mime-charset) d-m-c))
11696         (if (eq t d-m-c-u)
11697             (kill-local-variable 'default-mime-charset-unlimited)
11698           (set (make-local-variable 'default-mime-charset-unlimited)
11699                d-m-c-u)))))
11700
11701 (defun gnus-summary-setup-default-charset ()
11702   "Setup newsgroup default charset."
11703   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11704       (progn
11705         (setq gnus-newsgroup-charset nil)
11706         (set (make-local-variable 'default-mime-charset) nil)
11707         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11708           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11709     (let ((ignored-charsets
11710            (or gnus-newsgroup-ephemeral-ignored-charsets
11711                (append
11712                 (and gnus-newsgroup-name
11713                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11714                 gnus-newsgroup-ignored-charsets)))
11715           charset)
11716       (setq gnus-newsgroup-charset
11717             (or gnus-newsgroup-ephemeral-charset
11718                 (when (and gnus-newsgroup-name
11719                            (setq charset (gnus-parameter-charset
11720                                           gnus-newsgroup-name)))
11721                   (make-local-variable 'default-mime-charset)
11722                   (setq default-mime-charset charset))
11723                 gnus-default-charset))
11724       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11725            ignored-charsets))))
11726
11727 ;;;
11728 ;;; Mime Commands
11729 ;;;
11730
11731 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11732   "Display the current article buffer fully MIME-buttonized.
11733 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11734 treated as multipart/mixed."
11735   (interactive "P")
11736   (require 'gnus-art)
11737   (let ((gnus-unbuttonized-mime-types nil)
11738         (gnus-mime-display-multipart-as-mixed show-all-parts))
11739     (gnus-summary-show-article)))
11740
11741 (defun gnus-summary-repair-multipart (article)
11742   "Add a Content-Type header to a multipart article without one."
11743   (interactive (list (gnus-summary-article-number)))
11744   (gnus-with-article article
11745     (message-narrow-to-head)
11746     (message-remove-header "Mime-Version")
11747     (goto-char (point-max))
11748     (insert "Mime-Version: 1.0\n")
11749     (widen)
11750     (when (search-forward "\n--" nil t)
11751       (let ((separator (buffer-substring (point) (point-at-eol))))
11752         (message-narrow-to-head)
11753         (message-remove-header "Content-Type")
11754         (goto-char (point-max))
11755         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11756                         separator))
11757         (widen))))
11758   (let (gnus-mark-article-hook)
11759     (gnus-summary-select-article t t nil article)))
11760
11761 (defun gnus-summary-toggle-display-buttonized ()
11762   "Toggle the buttonizing of the article buffer."
11763   (interactive)
11764   (require 'gnus-art)
11765   (if (setq gnus-inhibit-mime-unbuttonizing
11766             (not gnus-inhibit-mime-unbuttonizing))
11767       (let ((gnus-unbuttonized-mime-types nil))
11768         (gnus-summary-show-article))
11769     (gnus-summary-show-article)))
11770
11771 ;;;
11772 ;;; Intelli-mouse commmands
11773 ;;;
11774
11775 (defun gnus-wheel-summary-scroll (event)
11776   (interactive "e")
11777   (let ((amount (if (memq 'shift (event-modifiers event))
11778                     (car gnus-wheel-scroll-amount)
11779                   (cdr gnus-wheel-scroll-amount)))
11780         (direction (- (* (static-if (featurep 'xemacs)
11781                              (event-button event)
11782                            (cond ((eq 'mouse-4 (event-basic-type event))
11783                                   4)
11784                                  ((eq 'mouse-5 (event-basic-type event))
11785                                   5)))
11786                          2) 9))
11787         edge)
11788     (gnus-summary-scroll-up (* amount direction))
11789     (when (gnus-eval-in-buffer-window gnus-article-buffer
11790             (save-restriction
11791               (widen)
11792               (and (if (< 0 direction)
11793                        (gnus-article-next-page 0)
11794                      (gnus-article-prev-page 0)
11795                      (bobp))
11796                    (if (setq edge (get-text-property
11797                                    (point-min) 'gnus-wheel-edge))
11798                        (setq edge (* edge direction))
11799                      (setq edge -1))
11800                    (or (plusp edge)
11801                        (let ((buffer-read-only nil)
11802                              (inhibit-read-only t))
11803                          (put-text-property (point-min) (point-max)
11804                                             'gnus-wheel-edge direction)
11805                          nil))
11806                    (or (> edge gnus-wheel-edge-resistance)
11807                        (let ((buffer-read-only nil)
11808                              (inhibit-read-only t))
11809                          (put-text-property (point-min) (point-max)
11810                                             'gnus-wheel-edge
11811                                             (* (1+ edge) direction))
11812                          nil))
11813                    (eq last-command 'gnus-wheel-summary-scroll))))
11814       (gnus-summary-next-article nil nil (minusp direction)))))
11815
11816 (defun gnus-wheel-install ()
11817   "Enable mouse wheel support on summary window."
11818   (when gnus-use-wheel
11819     (let ((keys
11820            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11821       (dolist (key keys)
11822         (define-key gnus-summary-mode-map key
11823           'gnus-wheel-summary-scroll)))))
11824
11825 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11826
11827 ;;;
11828 ;;; Traditional PGP commmands
11829 ;;;
11830
11831 (defun gnus-summary-decrypt-article (&optional force)
11832   "Decrypt the current article in traditional PGP way.
11833 This will have permanent effect only in mail groups.
11834 If FORCE is non-nil, allow editing of articles even in read-only
11835 groups."
11836   (interactive "P")
11837   (gnus-summary-select-article t)
11838   (gnus-eval-in-buffer-window gnus-article-buffer
11839     (save-excursion
11840       (save-restriction
11841         (widen)
11842         (goto-char (point-min))
11843         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11844           (error "Not a traditional PGP message!"))
11845         (let ((armor-start (match-beginning 0)))
11846           (if (and (pgg-decrypt-region armor-start (point-max))
11847                    (or force (not (gnus-group-read-only-p))))
11848               (let ((inhibit-read-only t)
11849                     buffer-read-only)
11850                 (delete-region armor-start
11851                                (progn
11852                                  (re-search-forward "^-+END PGP" nil t)
11853                                  (beginning-of-line 2)
11854                                  (point)))
11855                 (insert-buffer-substring pgg-output-buffer))))))))
11856
11857 (defun gnus-summary-verify-article ()
11858   "Verify the current article in traditional PGP way."
11859   (interactive)
11860   (save-excursion
11861     (set-buffer gnus-original-article-buffer)
11862     (goto-char (point-min))
11863     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11864       (error "Not a traditional PGP message!"))
11865     (re-search-forward "^-+END PGP" nil t)
11866     (beginning-of-line 2)
11867     (call-interactively (function pgg-verify-region))))
11868
11869 ;;;
11870 ;;; Generic summary marking commands
11871 ;;;
11872
11873 (defvar gnus-summary-marking-alist
11874   '((read gnus-del-mark "d")
11875     (unread gnus-unread-mark "u")
11876     (ticked gnus-ticked-mark "!")
11877     (dormant gnus-dormant-mark "?")
11878     (expirable gnus-expirable-mark "e"))
11879   "An alist of names/marks/keystrokes.")
11880
11881 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11882 (defvar gnus-summary-mark-map)
11883
11884 (defun gnus-summary-make-all-marking-commands ()
11885   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11886   (dolist (elem gnus-summary-marking-alist)
11887     (apply 'gnus-summary-make-marking-command elem)))
11888
11889 (defun gnus-summary-make-marking-command (name mark keystroke)
11890   (let ((map (make-sparse-keymap)))
11891     (define-key gnus-summary-generic-mark-map keystroke map)
11892     (dolist (lway `((next "next" next nil "n")
11893                     (next-unread "next unread" next t "N")
11894                     (prev "previous" prev nil "p")
11895                     (prev-unread "previous unread" prev t "P")
11896                     (nomove "" nil nil ,keystroke)))
11897       (let ((func (gnus-summary-make-marking-command-1
11898                    mark (car lway) lway name)))
11899         (setq func (eval func))
11900         (define-key map (nth 4 lway) func)))))
11901
11902 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11903   `(defun ,(intern
11904             (format "gnus-summary-put-mark-as-%s%s"
11905                     name (if (eq way 'nomove)
11906                              ""
11907                            (concat "-" (symbol-name way)))))
11908      (n)
11909      ,(format
11910        "Mark the current article as %s%s.
11911 If N, the prefix, then repeat N times.
11912 If N is negative, move in reverse order.
11913 The difference between N and the actual number of articles marked is
11914 returned."
11915        name (car (cdr lway)))
11916      (interactive "p")
11917      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11918
11919 (defun gnus-summary-generic-mark (n mark move unread)
11920   "Mark N articles with MARK."
11921   (unless (eq major-mode 'gnus-summary-mode)
11922     (error "This command can only be used in the summary buffer"))
11923   (gnus-summary-show-thread)
11924   (let ((nummove
11925          (cond
11926           ((eq move 'next) 1)
11927           ((eq move 'prev) -1)
11928           (t 0))))
11929     (if (zerop nummove)
11930         (setq n 1)
11931       (when (< n 0)
11932         (setq n (abs n)
11933               nummove (* -1 nummove))))
11934     (while (and (> n 0)
11935                 (gnus-summary-mark-article nil mark)
11936                 (zerop (gnus-summary-next-subject nummove unread t)))
11937       (setq n (1- n)))
11938     (when (/= 0 n)
11939       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11940     (gnus-summary-recenter)
11941     (gnus-summary-position-point)
11942     (gnus-set-mode-line 'summary)
11943     n))
11944
11945 (defun gnus-summary-insert-articles (articles)
11946   (when (setq articles
11947               (gnus-sorted-difference articles
11948                                       (mapcar (lambda (h)
11949                                                 (mail-header-number h))
11950                                               gnus-newsgroup-headers)))
11951     (setq gnus-newsgroup-headers
11952           (gnus-merge 'list
11953                       gnus-newsgroup-headers
11954                       (gnus-fetch-headers articles)
11955                       'gnus-article-sort-by-number))
11956     ;; Suppress duplicates?
11957     (when gnus-suppress-duplicates
11958       (gnus-dup-suppress-articles))
11959
11960     ;; We might want to build some more threads first.
11961     (when (and gnus-fetch-old-headers
11962                (eq gnus-headers-retrieved-by 'nov))
11963       (if (eq gnus-fetch-old-headers 'invisible)
11964           (gnus-build-all-threads)
11965         (gnus-build-old-threads)))
11966     ;; Let the Gnus agent mark articles as read.
11967     (when gnus-agent
11968       (gnus-agent-get-undownloaded-list))
11969     ;; Remove list identifiers from subject
11970     (when gnus-list-identifiers
11971       (gnus-summary-remove-list-identifiers))
11972     ;; First and last article in this newsgroup.
11973     (when gnus-newsgroup-headers
11974       (setq gnus-newsgroup-begin
11975             (mail-header-number (car gnus-newsgroup-headers))
11976             gnus-newsgroup-end
11977             (mail-header-number
11978              (gnus-last-element gnus-newsgroup-headers))))
11979     (when gnus-use-scoring
11980       (gnus-possibly-score-headers))))
11981
11982 (defun gnus-summary-insert-old-articles (&optional all)
11983   "Insert all old articles in this group.
11984 If ALL is non-nil, already read articles become readable.
11985 If ALL is a number, fetch this number of articles."
11986   (interactive "P")
11987   (prog1
11988       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11989             older len)
11990         (setq older
11991               ;; Some nntp servers lie about their active range.  When
11992               ;; this happens, the active range can be in the millions.
11993               ;; Use a compressed range to avoid creating a huge list.
11994               (gnus-range-difference (list gnus-newsgroup-active) old))
11995         (setq len (gnus-range-length older))
11996         (cond
11997          ((null older) nil)
11998          ((numberp all)
11999           (if (< all len)
12000               (let ((older-range (nreverse older)))
12001                 (setq older nil)
12002
12003                 (while (> all 0)
12004                   (let* ((r (pop older-range))
12005                          (min (if (numberp r) r (car r)))
12006                          (max (if (numberp r) r (cdr r))))
12007                     (while (and (<= min max)
12008                                 (> all 0))
12009                       (push max older)
12010                       (setq all (1- all)
12011                             max (1- max))))))
12012             (setq older (gnus-uncompress-range older))))
12013          (all
12014           (setq older (gnus-uncompress-range older)))
12015          (t
12016           (when (and (numberp gnus-large-newsgroup)
12017                    (> len gnus-large-newsgroup))
12018               (let* ((cursor-in-echo-area nil)
12019                      (initial (gnus-parameter-large-newsgroup-initial
12020                                gnus-newsgroup-name))
12021                      (input
12022                       (read-string
12023                        (format
12024                         "How many articles from %s (%s %d): "
12025                         (gnus-limit-string
12026                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12027                         (if initial "max" "default")
12028                         len)
12029                        (if initial
12030                            (cons (number-to-string initial)
12031                                  0)))))
12032                 (unless (string-match "^[ \t]*$" input)
12033                   (setq all (string-to-number input))
12034                   (if (< all len)
12035                       (let ((older-range (nreverse older)))
12036                         (setq older nil)
12037
12038                         (while (> all 0)
12039                           (let* ((r (pop older-range))
12040                                  (min (if (numberp r) r (car r)))
12041                                  (max (if (numberp r) r (cdr r))))
12042                             (while (and (<= min max)
12043                                         (> all 0))
12044                               (push max older)
12045                               (setq all (1- all)
12046                                     max (1- max))))))))))
12047           (setq older (gnus-uncompress-range older))))
12048         (if (not older)
12049             (message "No old news.")
12050           (gnus-summary-insert-articles older)
12051           (gnus-summary-limit (gnus-sorted-nunion old older))))
12052     (gnus-summary-position-point)))
12053
12054 (defun gnus-summary-insert-new-articles ()
12055   "Insert all new articles in this group."
12056   (interactive)
12057   (prog1
12058       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12059             (old-active gnus-newsgroup-active)
12060             (nnmail-fetched-sources (list t))
12061             i new)
12062         (setq gnus-newsgroup-active
12063               (gnus-activate-group gnus-newsgroup-name 'scan))
12064         (setq i (cdr gnus-newsgroup-active))
12065         (while (> i (cdr old-active))
12066           (push i new)
12067           (decf i))
12068         (if (not new)
12069             (message "No gnus is bad news")
12070           (gnus-summary-insert-articles new)
12071           (setq gnus-newsgroup-unreads
12072                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12073           (gnus-summary-limit (gnus-sorted-nunion old new))))
12074     (gnus-summary-position-point)))
12075
12076 (gnus-summary-make-all-marking-commands)
12077
12078 (gnus-ems-redefine)
12079
12080 (provide 'gnus-sum)
12081
12082 (run-hooks 'gnus-sum-load-hook)
12083
12084 ;; Local Variables:
12085 ;; coding: iso-8859-1
12086 ;; End:
12087
12088 ;;; gnus-sum.el ends here