Autoload password-read-and-add and password-cache-remove.
[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 non-nil, 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 some)
88                  number
89                  (sexp :menu-tag "other" t)))
90
91 (defcustom gnus-refer-thread-limit 200
92   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
93 If t, fetch all the available old headers."
94   :group 'gnus-thread
95   :type '(choice number
96                  (sexp :menu-tag "other" t)))
97
98 (defcustom gnus-summary-make-false-root 'adopt
99   "*nil means that Gnus won't gather loose threads.
100 If the root of a thread has expired or been read in a previous
101 session, the information necessary to build a complete thread has been
102 lost.  Instead of having many small sub-threads from this original thread
103 scattered all over the summary buffer, Gnus can gather them.
104
105 If non-nil, Gnus will try to gather all loose sub-threads from an
106 original thread into one large thread.
107
108 If this variable is non-nil, it should be one of `none', `adopt',
109 `dummy' or `empty'.
110
111 If this variable is `none', Gnus will not make a false root, but just
112 present the sub-threads after another.
113 If this variable is `dummy', Gnus will create a dummy root that will
114 have all the sub-threads as children.
115 If this variable is `adopt', Gnus will make one of the \"children\"
116 the parent and mark all the step-children as such.
117 If this variable is `empty', the \"children\" are printed with empty
118 subject fields.  (Or rather, they will be printed with a string
119 given by the `gnus-summary-same-subject' variable.)"
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const none)
123                  (const dummy)
124                  (const adopt)
125                  (const empty)))
126
127 (defcustom gnus-summary-make-false-root-always nil
128   "Always make a false dummy root."
129   :group 'gnus-thread
130   :type 'boolean)
131
132 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
133   "*A regexp to match subjects to be excluded from loose thread gathering.
134 As loose thread gathering is done on subjects only, that means that
135 there can be many false gatherings performed.  By rooting out certain
136 common subjects, gathering might become saner."
137   :group 'gnus-thread
138   :type 'regexp)
139
140 (defcustom gnus-summary-gather-subject-limit nil
141   "*Maximum length of subject comparisons when gathering loose threads.
142 Use nil to compare full subjects.  Setting this variable to a low
143 number will help gather threads that have been corrupted by
144 newsreaders chopping off subject lines, but it might also mean that
145 unrelated articles that have subject that happen to begin with the
146 same few characters will be incorrectly gathered.
147
148 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
149 comparing subjects."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const fuzzy)
153                  (sexp :menu-tag "on" t)))
154
155 (defcustom gnus-simplify-subject-functions nil
156   "List of functions taking a string argument that simplify subjects.
157 The functions are applied recursively.
158
159 Useful functions to put in this list include:
160 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
161 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
162   :group 'gnus-thread
163   :type '(repeat function))
164
165 (defcustom gnus-simplify-ignored-prefixes nil
166   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  regexp))
170
171 (defcustom gnus-build-sparse-threads nil
172   "*If non-nil, fill in the gaps in threads.
173 If `some', only fill in the gaps that are needed to tie loose threads
174 together.  If `more', fill in all leaf nodes that Gnus can find.  If
175 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
176   :group 'gnus-thread
177   :type '(choice (const :tag "off" nil)
178                  (const some)
179                  (const more)
180                  (sexp :menu-tag "all" t)))
181
182 (defcustom gnus-summary-thread-gathering-function
183   'gnus-gather-threads-by-subject
184   "*Function used for gathering loose threads.
185 There are two pre-defined functions: `gnus-gather-threads-by-subject',
186 which only takes Subjects into consideration; and
187 `gnus-gather-threads-by-references', which compared the References
188 headers of the articles to find matches."
189   :group 'gnus-thread
190   :type '(radio (function-item gnus-gather-threads-by-subject)
191                 (function-item gnus-gather-threads-by-references)
192                 (function :tag "other")))
193
194 (defcustom gnus-summary-same-subject ""
195   "*String indicating that the current article has the same subject as the previous.
196 This variable will only be used if the value of
197 `gnus-summary-make-false-root' is `empty'."
198   :group 'gnus-summary-format
199   :type 'string)
200
201 (defcustom gnus-summary-goto-unread t
202   "*If t, many commands will go to the next unread article.
203 This applies to marking commands as well as other commands that
204 \"naturally\" select the next article, like, for instance, `SPC' at
205 the end of an article.
206
207 If nil, the marking commands do NOT go to the next unread article
208 \(they go to the next article instead).  If `never', commands that
209 usually go to the next unread article, will go to the next article,
210 whether it is read or not."
211   :group 'gnus-summary-marks
212   :link '(custom-manual "(gnus)Setting Marks")
213   :type '(choice (const :tag "off" nil)
214                  (const never)
215                  (sexp :menu-tag "on" t)))
216
217 (defcustom gnus-summary-default-score 0
218   "*Default article score level.
219 All scores generated by the score files will be added to this score.
220 If this variable is nil, scoring will be disabled."
221   :group 'gnus-score-default
222   :type '(choice (const :tag "disable")
223                  integer))
224
225 (defcustom gnus-summary-default-high-score 0
226   "*Default threshold for a high scored article.
227 An article will be highlighted as high scored if its score is greater
228 than this score."
229   :group 'gnus-score-default
230   :type 'integer)
231
232 (defcustom gnus-summary-default-low-score 0
233   "*Default threshold for a low scored article.
234 An article will be highlighted as low scored if its score is smaller
235 than this score."
236   :group 'gnus-score-default
237   :type 'integer)
238
239 (defcustom gnus-summary-zcore-fuzz 0
240   "*Fuzziness factor for the zcore in the summary buffer.
241 Articles with scores closer than this to `gnus-summary-default-score'
242 will not be marked."
243   :group 'gnus-summary-format
244   :type 'integer)
245
246 (defcustom gnus-simplify-subject-fuzzy-regexp nil
247   "*Strings to be removed when doing fuzzy matches.
248 This can either be a regular expression or list of regular expressions
249 that will be removed from subject strings if fuzzy subject
250 simplification is selected."
251   :group 'gnus-thread
252   :type '(repeat regexp))
253
254 (defcustom gnus-show-threads t
255   "*If non-nil, display threads in summary mode."
256   :group 'gnus-thread
257   :type 'boolean)
258
259 (defcustom gnus-thread-hide-subtree nil
260   "*If non-nil, hide all threads initially.
261 This can be a predicate specifier which says which threads to hide.
262 If threads are hidden, you have to run the command
263 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
264 to expose hidden threads."
265   :group 'gnus-thread
266   :type '(radio (sexp :format "Non-nil\n"
267                       :match (lambda (widget value)
268                                (not (or (consp value) (functionp value))))
269                       :value t)
270                 (const nil)
271                 (sexp :tag "Predicate specifier" :size 0)))
272
273 (defcustom gnus-thread-hide-killed t
274   "*If non-nil, hide killed threads automatically."
275   :group 'gnus-thread
276   :type 'boolean)
277
278 (defcustom gnus-thread-ignore-subject t
279   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
280 If nil, articles that have different subjects from their parents will
281 start separate threads."
282   :group 'gnus-thread
283   :type 'boolean)
284
285 (defcustom gnus-thread-operation-ignore-subject t
286   "*If non-nil, subjects will be ignored when doing thread commands.
287 This affects commands like `gnus-summary-kill-thread' and
288 `gnus-summary-lower-thread'.
289
290 If this variable is nil, articles in the same thread with different
291 subjects will not be included in the operation in question.  If this
292 variable is `fuzzy', only articles that have subjects that are fuzzily
293 equal will be included."
294   :group 'gnus-thread
295   :type '(choice (const :tag "off" nil)
296                  (const fuzzy)
297                  (sexp :tag "on" t)))
298
299 (defcustom gnus-thread-indent-level 4
300   "*Number that says how much each sub-thread should be indented."
301   :group 'gnus-thread
302   :type 'integer)
303
304 (defcustom gnus-auto-extend-newsgroup t
305   "*If non-nil, extend newsgroup forward and backward when requested."
306   :group 'gnus-summary-choose
307   :type 'boolean)
308
309 (defcustom gnus-auto-select-first t
310   "*If non-nil, select the article under point.
311 Which article this is is controlled by the `gnus-auto-select-subject'
312 variable.
313
314 If you want to prevent automatic selection of articles in some
315 newsgroups, set the variable to nil in `gnus-select-group-hook'."
316   :group 'gnus-group-select
317   :type '(choice (const :tag "none" nil)
318                  (sexp :menu-tag "first" t)))
319
320 (defcustom gnus-auto-select-subject 'unread
321   "*Says what subject to place under point when entering a group.
322
323 This variable can either be the symbols `first' (place point on the
324 first subject), `unread' (place point on the subject line of the first
325 unread article), `best' (place point on the subject line of the
326 higest-scored article), `unseen' (place point on the subject line of
327 the first unseen article), 'unseen-or-unread' (place point on the subject
328 line of the first unseen article or, if all article have been seen, on the
329 subject line of the first unread article), or a function to be called to
330 place point on some subject line."
331   :group 'gnus-group-select
332   :type '(choice (const best)
333                  (const unread)
334                  (const first)
335                  (const unseen)
336                  (const unseen-or-unread)))
337
338 (defcustom gnus-dont-select-after-jump-to-other-group nil
339   "If non-nil, don't select the first unread article after entering the
340 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
341 it is depend on the value of `gnus-auto-select-first' whether to select
342 or not."
343   :group 'gnus-group-select
344   :type 'boolean)
345
346 (defcustom gnus-auto-select-next t
347   "*If non-nil, offer to go to the next group from the end of the previous.
348 If the value is t and the next newsgroup is empty, Gnus will exit
349 summary mode and go back to group mode.  If the value is neither nil
350 nor t, Gnus will select the following unread newsgroup.  In
351 particular, if the value is the symbol `quietly', the next unread
352 newsgroup will be selected without any confirmation, and if it is
353 `almost-quietly', the next group will be selected without any
354 confirmation if you are located on the last article in the group.
355 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
356 will go to the next group without confirmation."
357   :group 'gnus-summary-maneuvering
358   :type '(choice (const :tag "off" nil)
359                  (const quietly)
360                  (const almost-quietly)
361                  (const slightly-quietly)
362                  (sexp :menu-tag "on" t)))
363
364 (defcustom gnus-auto-select-same nil
365   "*If non-nil, select the next article with the same subject.
366 If there are no more articles with the same subject, go to
367 the first unread article."
368   :group 'gnus-summary-maneuvering
369   :type 'boolean)
370
371 (defcustom gnus-auto-goto-ignores 'unfetched
372   "*Says how to handle unfetched articles when maneuvering.
373
374 This variable can either be the symbols nil (maneuver to any
375 article), `undownloaded' (maneuvering while unplugged ignores articles
376 that have not been fetched), `always-undownloaded' (maneuvering always
377 ignores articles that have not been fetched), `unfetched' (maneuvering
378 ignores articles whose headers have not been fetched).
379
380 NOTE: The list of unfetched articles will always be nil when plugged
381 and, when unplugged, a subset of the undownloaded article list."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "None" nil)
384                  (const :tag "Undownloaded when unplugged" undownloaded)
385                  (const :tag "Undownloaded" always-undownloaded)
386                  (const :tag "Unfetched" unfetched)))
387
388 (defcustom gnus-summary-check-current nil
389   "*If non-nil, consider the current article when moving.
390 The \"unread\" movement commands will stay on the same line if the
391 current article is unread."
392   :group 'gnus-summary-maneuvering
393   :type 'boolean)
394
395 (defcustom gnus-auto-center-summary 2
396   "*If non-nil, always center the current summary buffer.
397 In particular, if `vertical' do only vertical recentering.  If non-nil
398 and non-`vertical', do both horizontal and vertical recentering."
399   :group 'gnus-summary-maneuvering
400   :type '(choice (const :tag "none" nil)
401                  (const vertical)
402                  (integer :tag "height")
403                  (sexp :menu-tag "both" t)))
404
405 (defvar gnus-auto-center-group t
406   "*If non-nil, always center the group buffer.")
407
408 (defcustom gnus-show-all-headers nil
409   "*If non-nil, don't hide any headers."
410   :group 'gnus-article-hiding
411   :group 'gnus-article-headers
412   :type 'boolean)
413
414 (defcustom gnus-summary-ignore-duplicates nil
415   "*If non-nil, ignore articles with identical Message-ID headers."
416   :group 'gnus-summary
417   :type 'boolean)
418
419 (defcustom gnus-single-article-buffer t
420   "*If non-nil, display all articles in the same buffer.
421 If nil, each group will get its own article buffer."
422   :group 'gnus-article-various
423   :type 'boolean)
424
425 (defcustom gnus-break-pages t
426   "*If non-nil, do page breaking on articles.
427 The page delimiter is specified by the `gnus-page-delimiter'
428 variable."
429   :group 'gnus-article-various
430   :type 'boolean)
431
432 (defcustom gnus-show-mime t
433   "*If non-nil, do mime processing of articles.
434 The articles will simply be fed to the function given by
435 `gnus-article-display-method-for-mime'."
436   :group 'gnus-article-mime
437   :type 'boolean)
438
439 (defcustom gnus-move-split-methods nil
440   "*Variable used to suggest where articles are to be moved to.
441 It uses the same syntax as the `gnus-split-methods' variable.
442 However, whereas `gnus-split-methods' specifies file names as targets,
443 this variable specifies group names."
444   :group 'gnus-summary-mail
445   :type '(repeat (choice (list :value (fun) function)
446                          (cons :value ("" "") regexp (repeat string))
447                          (sexp :value nil))))
448
449 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
450   "Function used to compute default prefix for article move/copy/etc prompts.
451 The function should take one argument, a group name, and return a
452 string with the suggested prefix."
453   :group 'gnus-summary-mail
454   :type 'function)
455
456 (defcustom gnus-unread-mark ?\ ;;;Whitespace
457   "*Mark used for unread articles."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-ticked-mark ?!
462   "*Mark used for ticked articles."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-dormant-mark ??
467   "*Mark used for dormant articles."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-del-mark ?r
472   "*Mark used for del'd articles."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-read-mark ?R
477   "*Mark used for read articles."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-expirable-mark ?E
482   "*Mark used for expirable articles."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-killed-mark ?K
487   "*Mark used for killed articles."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-spam-mark ?$
492   "*Mark used for spam articles."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-souped-mark ?F
497   "*Mark used for souped articles."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-kill-file-mark ?X
502   "*Mark used for articles killed by kill files."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-low-score-mark ?Y
507   "*Mark used for articles with a low score."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-catchup-mark ?C
512   "*Mark used for articles that are caught up."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-replied-mark ?A
517   "*Mark used for articles that have been replied to."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-forwarded-mark ?F
522   "*Mark used for articles that have been forwarded."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-recent-mark ?N
527   "*Mark used for articles that are recent."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-cached-mark ?*
532   "*Mark used for articles that are in the cache."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-saved-mark ?S
537   "*Mark used for articles that have been saved."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-unseen-mark ?.
542   "*Mark used for articles that haven't been seen."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-no-mark ?\ ;;;Whitespace
547   "*Mark used for articles that have no other secondary mark."
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-ancient-mark ?O
552   "*Mark used for ancient articles."
553   :group 'gnus-summary-marks
554   :type 'character)
555
556 (defcustom gnus-sparse-mark ?Q
557   "*Mark used for sparsely reffed articles."
558   :group 'gnus-summary-marks
559   :type 'character)
560
561 (defcustom gnus-canceled-mark ?G
562   "*Mark used for canceled articles."
563   :group 'gnus-summary-marks
564   :type 'character)
565
566 (defcustom gnus-duplicate-mark ?M
567   "*Mark used for duplicate articles."
568   :group 'gnus-summary-marks
569   :type 'character)
570
571 (defcustom gnus-undownloaded-mark ?-
572   "*Mark used for articles that weren't downloaded."
573   :group 'gnus-summary-marks
574   :type 'character)
575
576 (defcustom gnus-downloaded-mark ?+
577   "*Mark used for articles that were downloaded."
578   :group 'gnus-summary-marks
579   :type 'character)
580
581 (defcustom gnus-downloadable-mark ?%
582   "*Mark used for articles that are to be downloaded."
583   :group 'gnus-summary-marks
584   :type 'character)
585
586 (defcustom gnus-unsendable-mark ?=
587   "*Mark used for articles that won't be sent."
588   :group 'gnus-summary-marks
589   :type 'character)
590
591 (defcustom gnus-score-over-mark ?+
592   "*Score mark used for articles with high scores."
593   :group 'gnus-summary-marks
594   :type 'character)
595
596 (defcustom gnus-score-below-mark ?-
597   "*Score mark used for articles with low scores."
598   :group 'gnus-summary-marks
599   :type 'character)
600
601 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
602   "*There is no thread under the article."
603   :group 'gnus-summary-marks
604   :type 'character)
605
606 (defcustom gnus-not-empty-thread-mark ?=
607   "*There is a thread under the article."
608   :group 'gnus-summary-marks
609   :type 'character)
610
611 (defcustom gnus-view-pseudo-asynchronously nil
612   "*If non-nil, Gnus will view pseudo-articles asynchronously."
613   :group 'gnus-extract-view
614   :type 'boolean)
615
616 (defcustom gnus-auto-expirable-marks
617   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
618         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
619         gnus-souped-mark gnus-duplicate-mark)
620   "*The list of marks converted into expiration if a group is auto-expirable."
621   :version "21.1"
622   :group 'gnus-summary
623   :type '(repeat character))
624
625 (defcustom gnus-inhibit-user-auto-expire t
626   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
627   :version "21.1"
628   :group 'gnus-summary
629   :type 'boolean)
630
631 (defcustom gnus-view-pseudos nil
632   "*If `automatic', pseudo-articles will be viewed automatically.
633 If `not-confirm', pseudos will be viewed automatically, and the user
634 will not be asked to confirm the command."
635   :group 'gnus-extract-view
636   :type '(choice (const :tag "off" nil)
637                  (const automatic)
638                  (const not-confirm)))
639
640 (defcustom gnus-view-pseudos-separately t
641   "*If non-nil, one pseudo-article will be created for each file to be viewed.
642 If nil, all files that use the same viewing command will be given as a
643 list of parameters to that command."
644   :group 'gnus-extract-view
645   :type 'boolean)
646
647 (defcustom gnus-insert-pseudo-articles t
648   "*If non-nil, insert pseudo-articles when decoding articles."
649   :group 'gnus-extract-view
650   :type 'boolean)
651
652 (defcustom gnus-summary-dummy-line-format
653   "   %(:                             :%) %S\n"
654   "*The format specification for the dummy roots in the summary buffer.
655 It works along the same lines as a normal formatting string,
656 with some simple extensions.
657
658 %S  The subject
659
660 General format specifiers can also be used.
661 See `(gnus)Formatting Variables'."
662   :link '(custom-manual "(gnus)Formatting Variables")
663   :group 'gnus-threading
664   :type 'string)
665
666 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
667   "*The format specification for the summary mode line.
668 It works along the same lines as a normal formatting string,
669 with some simple extensions:
670
671 %G  Group name
672 %p  Unprefixed group name
673 %A  Current article number
674 %z  Current article score
675 %V  Gnus version
676 %U  Number of unread articles in the group
677 %e  Number of unselected articles in the group
678 %Z  A string with unread/unselected article counts
679 %g  Shortish group name
680 %S  Subject of the current article
681 %u  User-defined spec
682 %s  Current score file name
683 %d  Number of dormant articles
684 %r  Number of articles that have been marked as read in this session
685 %E  Number of articles expunged by the score files"
686   :group 'gnus-summary-format
687   :type 'string)
688
689 (defcustom gnus-list-identifiers nil
690   "Regexp that matches list identifiers to be removed from subject.
691 This can also be a list of regexps."
692   :version "21.1"
693   :group 'gnus-summary-format
694   :group 'gnus-article-hiding
695   :type '(choice (const :tag "none" nil)
696                  (regexp :value ".*")
697                  (repeat :value (".*") regexp)))
698
699 (defcustom gnus-summary-mark-below 0
700   "*Mark all articles with a score below this variable as read.
701 This variable is local to each summary buffer and usually set by the
702 score file."
703   :group 'gnus-score-default
704   :type 'integer)
705
706 (defun gnus-widget-reversible-match (widget value)
707   "Ignoring WIDGET, convert VALUE to internal form.
708 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
709   ;; (debug value)
710   (or (symbolp value)
711       (and (listp value)
712            (eq (length value) 2)
713            (eq (nth 0 value) 'not)
714            (symbolp (nth 1 value)))))
715
716 (defun gnus-widget-reversible-to-internal (widget value)
717   "Ignoring WIDGET, convert VALUE to internal form.
718 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
719 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
720   ;; (debug value)
721   (if (atom value)
722       (list value nil)
723     (list (nth 1 value) t)))
724
725 (defun gnus-widget-reversible-to-external (widget value)
726   "Ignoring WIDGET, convert VALUE to external form.
727 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
728 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
729   ;; (debug value)
730   (if (nth 1 value)
731       (list 'not (nth 0 value))
732     (nth 0 value)))
733
734 (define-widget 'gnus-widget-reversible 'group
735   "A `group' that convert values."
736   :match 'gnus-widget-reversible-match
737   :value-to-internal 'gnus-widget-reversible-to-internal
738   :value-to-external 'gnus-widget-reversible-to-external)
739
740 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
741   "*List of functions used for sorting articles in the summary buffer.
742
743 Each function takes two articles and returns non-nil if the first
744 article should be sorted before the other.  If you use more than one
745 function, the primary sort function should be the last.  You should
746 probably always include `gnus-article-sort-by-number' in the list of
747 sorting functions -- preferably first.  Also note that sorting by date
748 is often much slower than sorting by number, and the sorting order is
749 very similar.  (Sorting by date means sorting by the time the message
750 was sent, sorting by number means sorting by arrival time.)
751
752 Each item can also be a list `(not F)' where F is a function;
753 this reverses the sort order.
754
755 Ready-made functions include `gnus-article-sort-by-number',
756 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
757 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
758 and `gnus-article-sort-by-score'.
759
760 When threading is turned on, the variable `gnus-thread-sort-functions'
761 controls how articles are sorted."
762   :group 'gnus-summary-sort
763   :type '(repeat (gnus-widget-reversible
764                   (choice (function-item gnus-article-sort-by-number)
765                           (function-item gnus-article-sort-by-author)
766                           (function-item gnus-article-sort-by-subject)
767                           (function-item gnus-article-sort-by-date)
768                           (function-item gnus-article-sort-by-score)
769                           (function-item gnus-article-sort-by-random)
770                           (function :tag "other"))
771                   (boolean :tag "Reverse order"))))
772
773
774 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
775   "*List of functions used for sorting threads in the summary buffer.
776 By default, threads are sorted by article number.
777
778 Each function takes two threads and returns non-nil if the first
779 thread should be sorted before the other.  If you use more than one
780 function, the primary sort function should be the last.  You should
781 probably always include `gnus-thread-sort-by-number' in the list of
782 sorting functions -- preferably first.  Also note that sorting by date
783 is often much slower than sorting by number, and the sorting order is
784 very similar.  (Sorting by date means sorting by the time the message
785 was sent, sorting by number means sorting by arrival time.)
786
787 Each list item can also be a list `(not F)' where F is a
788 function; this specifies reversed sort order.
789
790 Ready-made functions include `gnus-thread-sort-by-number',
791 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
792 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
793 `gnus-thread-sort-by-most-recent-number',
794 `gnus-thread-sort-by-most-recent-date',
795 `gnus-thread-sort-by-random', and
796 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
797
798 When threading is turned off, the variable
799 `gnus-article-sort-functions' controls how articles are sorted."
800   :group 'gnus-summary-sort
801   :type '(repeat 
802           (gnus-widget-reversible
803            (choice (function-item gnus-thread-sort-by-number)
804                    (function-item gnus-thread-sort-by-author)
805                    (function-item gnus-thread-sort-by-subject)
806                    (function-item gnus-thread-sort-by-date)
807                    (function-item gnus-thread-sort-by-score)
808                    (function-item gnus-thread-sort-by-most-recent-number)
809                    (function-item gnus-thread-sort-by-most-recent-date)
810                    (function-item gnus-thread-sort-by-random)
811                    (function-item gnus-thread-sort-by-total-score)
812                    (function :tag "other"))
813            (boolean :tag "Reverse order"))))
814
815 (defcustom gnus-thread-score-function '+
816   "*Function used for calculating the total score of a thread.
817
818 The function is called with the scores of the article and each
819 subthread and should then return the score of the thread.
820
821 Some functions you can use are `+', `max', or `min'."
822   :group 'gnus-summary-sort
823   :type 'function)
824
825 (defcustom gnus-summary-expunge-below nil
826   "All articles that have a score less than this variable will be expunged.
827 This variable is local to the summary buffers."
828   :group 'gnus-score-default
829   :type '(choice (const :tag "off" nil)
830                  integer))
831
832 (defcustom gnus-thread-expunge-below nil
833   "All threads that have a total score less than this variable will be expunged.
834 See `gnus-thread-score-function' for en explanation of what a
835 \"thread score\" is.
836
837 This variable is local to the summary buffers."
838   :group 'gnus-threading
839   :group 'gnus-score-default
840   :type '(choice (const :tag "off" nil)
841                  integer))
842
843 (defcustom gnus-summary-mode-hook nil
844   "*A hook for Gnus summary mode.
845 This hook is run before any variables are set in the summary buffer."
846   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
847   :group 'gnus-summary-various
848   :type 'hook)
849
850 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
851 (when (featurep 'xemacs)
852   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
853   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
854   (add-hook 'gnus-summary-mode-hook
855             'gnus-xmas-switch-horizontal-scrollbar-off))
856
857 (defcustom gnus-summary-menu-hook nil
858   "*Hook run after the creation of the summary mode menu."
859   :group 'gnus-summary-visual
860   :type 'hook)
861
862 (defcustom gnus-summary-exit-hook nil
863   "*A hook called on exit from the summary buffer.
864 It will be called with point in the group buffer."
865   :group 'gnus-summary-exit
866   :type 'hook)
867
868 (defcustom gnus-summary-prepare-hook nil
869   "*A hook called after the summary buffer has been generated.
870 If you want to modify the summary buffer, you can use this hook."
871   :group 'gnus-summary-various
872   :type 'hook)
873
874 (defcustom gnus-summary-prepared-hook nil
875   "*A hook called as the last thing after the summary buffer has been generated."
876   :group 'gnus-summary-various
877   :type 'hook)
878
879 (defcustom gnus-summary-generate-hook nil
880   "*A hook run just before generating the summary buffer.
881 This hook is commonly used to customize threading variables and the
882 like."
883   :group 'gnus-summary-various
884   :type 'hook)
885
886 (defcustom gnus-select-group-hook nil
887   "*A hook called when a newsgroup is selected.
888
889 If you'd like to simplify subjects like the
890 `gnus-summary-next-same-subject' command does, you can use the
891 following hook:
892
893  (add-hook gnus-select-group-hook
894            (lambda ()
895              (mapcar (lambda (header)
896                        (mail-header-set-subject
897                         header
898                         (gnus-simplify-subject
899                          (mail-header-subject header) 're-only)))
900                      gnus-newsgroup-headers)))"
901   :group 'gnus-group-select
902   :type 'hook)
903
904 (defcustom gnus-select-article-hook nil
905   "*A hook called when an article is selected."
906   :group 'gnus-summary-choose
907   :options '(gnus-agent-fetch-selected-article)
908   :type 'hook)
909
910 (defcustom gnus-visual-mark-article-hook
911   (list 'gnus-highlight-selected-summary)
912   "*Hook run after selecting an article in the summary buffer.
913 It is meant to be used for highlighting the article in some way.  It
914 is not run if `gnus-visual' is nil."
915   :group 'gnus-summary-visual
916   :type 'hook)
917
918 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
919   "*A hook called before parsing the headers."
920   :group 'gnus-various
921   :type 'hook)
922
923 (defcustom gnus-exit-group-hook nil
924   "*A hook called when exiting summary mode.
925 This hook is not called from the non-updating exit commands like `Q'."
926   :group 'gnus-various
927   :type 'hook)
928
929 (defcustom gnus-summary-update-hook
930   (list 'gnus-summary-highlight-line)
931   "*A hook called when a summary line is changed.
932 The hook will not be called if `gnus-visual' is nil.
933
934 The default function `gnus-summary-highlight-line' will
935 highlight the line according to the `gnus-summary-highlight'
936 variable."
937   :group 'gnus-summary-visual
938   :type 'hook)
939
940 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
941   "*A hook called when an article is selected for the first time.
942 The hook is intended to mark an article as read (or unread)
943 automatically when it is selected."
944   :group 'gnus-summary-choose
945   :type 'hook)
946
947 (defcustom gnus-group-no-more-groups-hook nil
948   "*A hook run when returning to group mode having no more (unread) groups."
949   :group 'gnus-group-select
950   :type 'hook)
951
952 (defcustom gnus-ps-print-hook nil
953   "*A hook run before ps-printing something from Gnus."
954   :group 'gnus-summary
955   :type 'hook)
956
957 (defcustom gnus-summary-article-move-hook nil
958   "*A hook called after an article is moved, copied, respooled, or crossposted."
959   :group 'gnus-summary
960   :type 'hook)
961
962 (defcustom gnus-summary-article-delete-hook nil
963   "*A hook called after an article is deleted."
964   :group 'gnus-summary
965   :type 'hook)
966
967 (defcustom gnus-summary-article-expire-hook nil
968   "*A hook called after an article is expired."
969   :group 'gnus-summary
970   :type 'hook)
971
972 (defcustom gnus-summary-display-arrow
973   (and (fboundp 'display-graphic-p)
974        (display-graphic-p))
975   "*If non-nil, display an arrow highlighting the current article."
976   :version "21.1"
977   :group 'gnus-summary
978   :type 'boolean)
979
980 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
981   "Face used for highlighting the current article in the summary buffer."
982   :group 'gnus-summary-visual
983   :type 'face)
984
985 (defvar gnus-tmp-downloaded nil)
986
987 (defcustom gnus-summary-highlight
988   '(((eq mark gnus-canceled-mark)
989      . gnus-summary-cancelled-face)
990     ((and uncached (> score default-high))
991      . gnus-summary-high-undownloaded-face)
992     ((and uncached (< score default-low))
993      . gnus-summary-low-undownloaded-face)
994     (uncached
995      . gnus-summary-normal-undownloaded-face)
996     ((and (> score default-high)
997           (or (eq mark gnus-dormant-mark)
998               (eq mark gnus-ticked-mark)))
999      . gnus-summary-high-ticked-face)
1000     ((and (< score default-low)
1001           (or (eq mark gnus-dormant-mark)
1002               (eq mark gnus-ticked-mark)))
1003      . gnus-summary-low-ticked-face)
1004     ((or (eq mark gnus-dormant-mark)
1005          (eq mark gnus-ticked-mark))
1006      . gnus-summary-normal-ticked-face)
1007     ((and (> score default-high) (eq mark gnus-ancient-mark))
1008      . gnus-summary-high-ancient-face)
1009     ((and (< score default-low) (eq mark gnus-ancient-mark))
1010      . gnus-summary-low-ancient-face)
1011     ((eq mark gnus-ancient-mark)
1012      . gnus-summary-normal-ancient-face)
1013     ((and (> score default-high) (eq mark gnus-unread-mark))
1014      . gnus-summary-high-unread-face)
1015     ((and (< score default-low) (eq mark gnus-unread-mark))
1016      . gnus-summary-low-unread-face)
1017     ((eq mark gnus-unread-mark)
1018      . gnus-summary-normal-unread-face)
1019     ((> score default-high)
1020      . gnus-summary-high-read-face)
1021     ((< score default-low)
1022      . gnus-summary-low-read-face)
1023     (t
1024      . gnus-summary-normal-read-face))
1025   "*Controls the highlighting of summary buffer lines.
1026
1027 A list of (FORM . FACE) pairs.  When deciding how a a particular
1028 summary line should be displayed, each form is evaluated.  The content
1029 of the face field after the first true form is used.  You can change
1030 how those summary lines are displayed, by editing the face field.
1031
1032 You can use the following variables in the FORM field.
1033
1034 score:        The article's score
1035 default:      The default article score.
1036 default-high: The default score for high scored articles.
1037 default-low:  The default score for low scored articles.
1038 below:        The score below which articles are automatically marked as read.
1039 mark:         The article's mark.
1040 uncached:     Non-nil if the article is uncached."
1041   :group 'gnus-summary-visual
1042   :type '(repeat (cons (sexp :tag "Form" nil)
1043                        face)))
1044
1045 (defcustom gnus-alter-header-function nil
1046   "Function called to allow alteration of article header structures.
1047 The function is called with one parameter, the article header vector,
1048 which it may alter in any way."
1049   :type '(choice (const :tag "None" nil)
1050                  function)
1051   :group 'gnus-summary)
1052
1053 (defvar gnus-decode-encoded-word-function
1054   (mime-find-field-decoder 'From 'nov)
1055   "Variable that says which function should be used to decode a string with encoded words.")
1056
1057 (defcustom gnus-extra-headers '(To Newsgroups)
1058   "*Extra headers to parse."
1059   :version "21.1"
1060   :group 'gnus-summary
1061   :type '(repeat symbol))
1062
1063 (defcustom gnus-ignored-from-addresses
1064   (and user-mail-address (regexp-quote user-mail-address))
1065   "*Regexp of From headers that may be suppressed in favor of To headers."
1066   :version "21.1"
1067   :group 'gnus-summary
1068   :type 'regexp)
1069
1070 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1071   "List of charsets that should be ignored.
1072 When these charsets are used in the \"charset\" parameter, the
1073 default charset will be used instead."
1074   :version "21.1"
1075   :type '(repeat symbol)
1076   :group 'gnus-charset)
1077
1078 (gnus-define-group-parameter
1079  ignored-charsets
1080  :type list
1081  :function-document
1082  "Return the ignored charsets of GROUP."
1083  :variable gnus-group-ignored-charsets-alist
1084  :variable-default
1085  '(("alt\\.chinese\\.text" iso-8859-1))
1086  :variable-document
1087  "Alist of regexps (to match group names) and charsets that should be ignored.
1088 When these charsets are used in the \"charset\" parameter, the
1089 default charset will be used instead."
1090  :variable-group gnus-charset
1091  :variable-type '(repeat (cons (regexp :tag "Group")
1092                                (repeat symbol)))
1093  :parameter-type '(choice :tag "Ignored charsets"
1094                           :value nil
1095                           (repeat (symbol)))
1096  :parameter-document       "\
1097 List of charsets that should be ignored.
1098
1099 When these charsets are used in the \"charset\" parameter, the
1100 default charset will be used instead.")
1101
1102 (defcustom gnus-group-highlight-words-alist nil
1103   "Alist of group regexps and highlight regexps.
1104 This variable uses the same syntax as `gnus-emphasis-alist'."
1105   :version "21.1"
1106   :type '(repeat (cons (regexp :tag "Group")
1107                        (repeat (list (regexp :tag "Highlight regexp")
1108                                      (number :tag "Group for entire word" 0)
1109                                      (number :tag "Group for displayed part" 0)
1110                                      (symbol :tag "Face"
1111                                              gnus-emphasis-highlight-words)))))
1112   :group 'gnus-summary-visual)
1113
1114 (defcustom gnus-use-wheel nil
1115   "Use Intelli-mouse on summary movement"
1116   :type 'boolean
1117   :group 'gnus-summary-maneuvering)
1118
1119 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1120   "Amount to scroll messages by spinning the mouse wheel.
1121 This is actually a cons cell, where the first item is the amount to scroll
1122 on a normal wheel event, and the second is the amount to scroll when the
1123 wheel is moved with the shift key depressed."
1124   :type '(cons (integer :tag "Shift") integer)
1125   :group 'gnus-summary-maneuvering)
1126
1127 (defcustom gnus-wheel-edge-resistance 2
1128   "How hard it should be to change the current article
1129 by moving the mouse over the edge of the article window."
1130   :type 'integer
1131   :group 'gnus-summary-maneuvering)
1132
1133 (defcustom gnus-summary-show-article-charset-alist
1134   nil
1135   "Alist of number and charset.
1136 The article will be shown with the charset corresponding to the
1137 numbered argument.
1138 For example: ((1 . cn-gb-2312) (2 . big5))."
1139   :version "21.1"
1140   :type '(repeat (cons (number :tag "Argument" 1)
1141                        (symbol :tag "Charset")))
1142   :group 'gnus-charset)
1143
1144 (defcustom gnus-preserve-marks t
1145   "Whether marks are preserved when moving, copying and respooling messages."
1146   :version "21.1"
1147   :type 'boolean
1148   :group 'gnus-summary-marks)
1149
1150 (defcustom gnus-alter-articles-to-read-function nil
1151   "Function to be called to alter the list of articles to be selected."
1152   :type '(choice (const nil) function)
1153   :group 'gnus-summary)
1154
1155 (defcustom gnus-orphan-score nil
1156   "*All orphans get this score added.  Set in the score file."
1157   :group 'gnus-score-default
1158   :type '(choice (const nil)
1159                  integer))
1160
1161 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1162   "*A regexp to match MIME parts when saving multiple parts of a
1163 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1164 This regexp will be used by default when prompting the user for which
1165 type of files to save."
1166   :group 'gnus-summary
1167   :type 'regexp)
1168
1169 (defcustom gnus-read-all-available-headers nil
1170   "Whether Gnus should parse all headers made available to it.
1171 This is mostly relevant for slow back ends where the user may
1172 wish to widen the summary buffer to include all headers
1173 that were fetched.  Say, for nnultimate groups."
1174   :group 'gnus-summary
1175   :type '(choice boolean regexp))
1176
1177 (defcustom gnus-summary-muttprint-program "muttprint"
1178   "Command (and optional arguments) used to run Muttprint."
1179   :version "21.3"
1180   :group 'gnus-summary
1181   :type 'string)
1182
1183 (defcustom gnus-article-loose-mime t
1184   "If non-nil, don't require MIME-Version header.
1185 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1186 supply the MIME-Version header or deliberately strip it from the mail.
1187 If non-nil (the default), Gnus will treat some articles as MIME
1188 even if the MIME-Version header is missing."
1189   :version "21.3"
1190   :type 'boolean
1191   :group 'gnus-article-mime)
1192
1193 (defcustom gnus-article-emulate-mime t
1194   "If non-nil, use MIME emulation for uuencode and the like.
1195 This means that Gnus will search message bodies for text that look
1196 like uuencoded bits, yEncoded bits, and so on, and present that using
1197 the normal Gnus MIME machinery."
1198   :type 'boolean
1199   :group 'gnus-article-mime)
1200
1201 ;;; Internal variables
1202
1203 (defvar gnus-summary-display-cache nil)
1204 (defvar gnus-article-mime-handles nil)
1205 (defvar gnus-article-decoded-p nil)
1206 (defvar gnus-article-charset nil)
1207 (defvar gnus-article-ignored-charsets nil)
1208 (defvar gnus-scores-exclude-files nil)
1209 (defvar gnus-page-broken nil)
1210
1211 (defvar gnus-original-article nil)
1212 (defvar gnus-article-internal-prepare-hook nil)
1213 (defvar gnus-newsgroup-process-stack nil)
1214
1215 (defvar gnus-thread-indent-array nil)
1216 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1217 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1218   "Function called to sort the articles within a thread after it has been gathered together.")
1219
1220 (defvar gnus-summary-save-parts-type-history nil)
1221 (defvar gnus-summary-save-parts-last-directory nil)
1222
1223 ;; Avoid highlighting in kill files.
1224 (defvar gnus-summary-inhibit-highlight nil)
1225 (defvar gnus-newsgroup-selected-overlay nil)
1226 (defvar gnus-inhibit-limiting nil)
1227 (defvar gnus-newsgroup-adaptive-score-file nil)
1228 (defvar gnus-current-score-file nil)
1229 (defvar gnus-current-move-group nil)
1230 (defvar gnus-current-copy-group nil)
1231 (defvar gnus-current-crosspost-group nil)
1232 (defvar gnus-newsgroup-display nil)
1233
1234 (defvar gnus-newsgroup-dependencies nil)
1235 (defvar gnus-newsgroup-adaptive nil)
1236 (defvar gnus-summary-display-article-function nil)
1237 (defvar gnus-summary-highlight-line-function nil
1238   "Function called after highlighting a summary line.")
1239
1240 (defvar gnus-summary-line-format-alist
1241   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1242     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1243     (?s gnus-tmp-subject-or-nil ?s)
1244     (?n gnus-tmp-name ?s)
1245     (?A (std11-address-string
1246          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1247     (?a (or (std11-full-name-string
1248              (car (mime-entity-read-field gnus-tmp-header 'From)))
1249             gnus-tmp-from) ?s)
1250     (?F gnus-tmp-from ?s)
1251     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1252     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1253     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1254     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1255     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1256     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1257     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1258     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1259     (?L gnus-tmp-lines ?s)
1260     (?O gnus-tmp-downloaded ?c)
1261     (?I gnus-tmp-indentation ?s)
1262     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1263     (?R gnus-tmp-replied ?c)
1264     (?\[ gnus-tmp-opening-bracket ?c)
1265     (?\] gnus-tmp-closing-bracket ?c)
1266     (?\> (make-string gnus-tmp-level ? ) ?s)
1267     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1268     (?i gnus-tmp-score ?d)
1269     (?z gnus-tmp-score-char ?c)
1270     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1271     (?U gnus-tmp-unread ?c)
1272     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1273         ?s)
1274     (?t (gnus-summary-number-of-articles-in-thread
1275          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1276         ?d)
1277     (?e (gnus-summary-number-of-articles-in-thread
1278          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1279         ?c)
1280     (?u gnus-tmp-user-defined ?s)
1281     (?P (gnus-pick-line-number) ?d)
1282     (?B gnus-tmp-thread-tree-header-string ?s)
1283     (user-date (gnus-user-date
1284                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1285   "An alist of format specifications that can appear in summary lines.
1286 These are paired with what variables they correspond with, along with
1287 the type of the variable (string, integer, character, etc).")
1288
1289 (defvar gnus-summary-dummy-line-format-alist
1290   `((?S gnus-tmp-subject ?s)
1291     (?N gnus-tmp-number ?d)
1292     (?u gnus-tmp-user-defined ?s)))
1293
1294 (defvar gnus-summary-mode-line-format-alist
1295   `((?G gnus-tmp-group-name ?s)
1296     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1297     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1298     (?A gnus-tmp-article-number ?d)
1299     (?Z gnus-tmp-unread-and-unselected ?s)
1300     (?V gnus-version ?s)
1301     (?U gnus-tmp-unread-and-unticked ?d)
1302     (?S gnus-tmp-subject ?s)
1303     (?e gnus-tmp-unselected ?d)
1304     (?u gnus-tmp-user-defined ?s)
1305     (?d (length gnus-newsgroup-dormant) ?d)
1306     (?t (length gnus-newsgroup-marked) ?d)
1307     (?h (length gnus-newsgroup-spam-marked) ?d)
1308     (?r (length gnus-newsgroup-reads) ?d)
1309     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1310     (?E gnus-newsgroup-expunged-tally ?d)
1311     (?s (gnus-current-score-file-nondirectory) ?s)))
1312
1313 (defvar gnus-last-search-regexp nil
1314   "Default regexp for article search command.")
1315
1316 (defvar gnus-summary-search-article-matched-data nil
1317   "Last matched data of article search command.  It is the local variable
1318 in `gnus-article-buffer' which consists of the list of start position,
1319 end position and text.")
1320
1321 (defvar gnus-last-shell-command nil
1322   "Default shell command on article.")
1323
1324 (defvar gnus-newsgroup-agentized nil
1325   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1326 (defvar gnus-newsgroup-begin nil)
1327 (defvar gnus-newsgroup-end nil)
1328 (defvar gnus-newsgroup-last-rmail nil)
1329 (defvar gnus-newsgroup-last-mail nil)
1330 (defvar gnus-newsgroup-last-folder nil)
1331 (defvar gnus-newsgroup-last-file nil)
1332 (defvar gnus-newsgroup-auto-expire nil)
1333 (defvar gnus-newsgroup-active nil)
1334
1335 (defvar gnus-newsgroup-data nil)
1336 (defvar gnus-newsgroup-data-reverse nil)
1337 (defvar gnus-newsgroup-limit nil)
1338 (defvar gnus-newsgroup-limits nil)
1339 (defvar gnus-summary-use-undownloaded-faces nil)
1340
1341 (defvar gnus-newsgroup-unreads nil
1342   "Sorted list of unread articles in the current newsgroup.")
1343
1344 (defvar gnus-newsgroup-unselected nil
1345   "Sorted list of unselected unread articles in the current newsgroup.")
1346
1347 (defvar gnus-newsgroup-reads nil
1348   "Alist of read articles and article marks in the current newsgroup.")
1349
1350 (defvar gnus-newsgroup-expunged-tally nil)
1351
1352 (defvar gnus-newsgroup-marked nil
1353   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1354
1355 (defvar gnus-newsgroup-spam-marked nil
1356   "List of ranges of articles that have been marked as spam.")
1357
1358 (defvar gnus-newsgroup-killed nil
1359   "List of ranges of articles that have been through the scoring process.")
1360
1361 (defvar gnus-newsgroup-cached nil
1362   "Sorted list of articles that come from the article cache.")
1363
1364 (defvar gnus-newsgroup-saved nil
1365   "List of articles that have been saved.")
1366
1367 (defvar gnus-newsgroup-kill-headers nil)
1368
1369 (defvar gnus-newsgroup-replied nil
1370   "List of articles that have been replied to in the current newsgroup.")
1371
1372 (defvar gnus-newsgroup-forwarded nil
1373   "List of articles that have been forwarded in the current newsgroup.")
1374
1375 (defvar gnus-newsgroup-recent nil
1376   "List of articles that have are recent in the current newsgroup.")
1377
1378 (defvar gnus-newsgroup-expirable nil
1379   "Sorted list of articles in the current newsgroup that can be expired.")
1380
1381 (defvar gnus-newsgroup-processable nil
1382   "List of articles in the current newsgroup that can be processed.")
1383
1384 (defvar gnus-newsgroup-downloadable nil
1385   "Sorted list of articles in the current newsgroup that can be processed.")
1386
1387 (defvar gnus-newsgroup-unfetched nil
1388   "Sorted list of articles in the current newsgroup whose headers have
1389 not been fetched into the agent.
1390
1391 This list will always be a subset of gnus-newsgroup-undownloaded.")
1392
1393 (defvar gnus-newsgroup-undownloaded nil
1394   "List of articles in the current newsgroup that haven't been downloaded.")
1395
1396 (defvar gnus-newsgroup-unsendable nil
1397   "List of articles in the current newsgroup that won't be sent.")
1398
1399 (defvar gnus-newsgroup-bookmarks nil
1400   "List of articles in the current newsgroup that have bookmarks.")
1401
1402 (defvar gnus-newsgroup-dormant nil
1403   "Sorted list of dormant articles in the current newsgroup.")
1404
1405 (defvar gnus-newsgroup-unseen nil
1406   "List of unseen articles in the current newsgroup.")
1407
1408 (defvar gnus-newsgroup-seen nil
1409   "Range of seen articles in the current newsgroup.")
1410
1411 (defvar gnus-newsgroup-articles nil
1412   "List of articles in the current newsgroup.")
1413
1414 (defvar gnus-newsgroup-scored nil
1415   "List of scored articles in the current newsgroup.")
1416
1417 (defvar gnus-newsgroup-incorporated nil
1418   "List of incorporated articles in the current newsgroup.")
1419
1420 (defvar gnus-newsgroup-headers nil
1421   "List of article headers in the current newsgroup.")
1422
1423 (defvar gnus-newsgroup-threads nil)
1424
1425 (defvar gnus-newsgroup-prepared nil
1426   "Whether the current group has been prepared properly.")
1427
1428 (defvar gnus-newsgroup-ancient nil
1429   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1430
1431 (defvar gnus-newsgroup-sparse nil)
1432
1433 (defvar gnus-current-article nil)
1434 (defvar gnus-article-current nil)
1435 (defvar gnus-current-headers nil)
1436 (defvar gnus-have-all-headers nil)
1437 (defvar gnus-last-article nil)
1438 (defvar gnus-newsgroup-history nil)
1439 (defvar gnus-newsgroup-charset nil)
1440 (defvar gnus-newsgroup-ephemeral-charset nil)
1441 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1442
1443 (defvar gnus-article-before-search nil)
1444
1445 (defvar gnus-summary-local-variables
1446   '(gnus-newsgroup-name
1447     gnus-newsgroup-begin gnus-newsgroup-end
1448     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1449     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1450     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1451     gnus-newsgroup-unselected gnus-newsgroup-marked
1452     gnus-newsgroup-spam-marked
1453     gnus-newsgroup-reads gnus-newsgroup-saved
1454     gnus-newsgroup-replied gnus-newsgroup-forwarded
1455     gnus-newsgroup-recent
1456     gnus-newsgroup-expirable
1457     gnus-newsgroup-processable gnus-newsgroup-killed
1458     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1459     gnus-newsgroup-unfetched
1460     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1461     gnus-newsgroup-seen gnus-newsgroup-articles
1462     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1463     gnus-newsgroup-headers gnus-newsgroup-threads
1464     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1465     gnus-current-article gnus-current-headers gnus-have-all-headers
1466     gnus-last-article gnus-article-internal-prepare-hook
1467     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1468     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1469     gnus-thread-expunge-below
1470     gnus-score-alist gnus-current-score-file
1471     (gnus-summary-expunge-below . global)
1472     (gnus-summary-mark-below . global)
1473     (gnus-orphan-score . global)
1474     gnus-newsgroup-active gnus-scores-exclude-files
1475     gnus-newsgroup-history gnus-newsgroup-ancient
1476     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1477     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1478     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1479     (gnus-newsgroup-expunged-tally . 0)
1480     gnus-cache-removable-articles gnus-newsgroup-cached
1481     gnus-newsgroup-data gnus-newsgroup-data-reverse
1482     gnus-newsgroup-limit gnus-newsgroup-limits
1483     gnus-newsgroup-charset gnus-newsgroup-display
1484     gnus-summary-use-undownloaded-faces
1485     gnus-newsgroup-incorporated)
1486   "Variables that are buffer-local to the summary buffers.")
1487
1488 (defvar gnus-newsgroup-variables nil
1489   "A list of variables that have separate values in different newsgroups.
1490 A list of newsgroup (summary buffer) local variables, or cons of
1491 variables and their default expressions to be evalled (when the default
1492 values are not nil), that should be made global while the summary buffer
1493 is active.
1494
1495 Note: The default expressions will be evaluated (using function `eval')
1496 before assignment to the local variable rather than just assigned to it.
1497 If the default expression is the symbol `global', that symbol will not
1498 be evaluated but the global value of the local variable will be used
1499 instead.
1500
1501 These variables can be used to set variables in the group parameters
1502 while still allowing them to affect operations done in other buffers.
1503 For example:
1504
1505 \(setq gnus-newsgroup-variables
1506      '(message-use-followup-to
1507        (gnus-visible-headers .
1508          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1509 ")
1510
1511 ;; Byte-compiler warning.
1512 (eval-when-compile
1513   ;; Bind features so that require will believe that gnus-sum has
1514   ;; already been loaded (avoids infinite recursion)
1515   (let ((features (cons 'gnus-sum features)))
1516     ;; Several of the declarations in gnus-sum are needed to load the
1517     ;; following files. Right now, these definitions have been
1518     ;; compiled but not defined (evaluated).  We could either do a
1519     ;; eval-and-compile about all of the declarations or evaluate the
1520     ;; source file.
1521     (if (boundp 'gnus-newsgroup-variables)
1522         nil
1523       (load "gnus-sum.el" t t t))
1524     (require 'gnus)
1525     (require 'gnus-art)))
1526
1527 ;; Subject simplification.
1528
1529 (defun gnus-simplify-whitespace (str)
1530   "Remove excessive whitespace from STR."
1531   ;; Multiple spaces.
1532   (while (string-match "[ \t][ \t]+" str)
1533     (setq str (concat (substring str 0 (match-beginning 0))
1534                         " "
1535                         (substring str (match-end 0)))))
1536   ;; Leading spaces.
1537   (when (string-match "^[ \t]+" str)
1538     (setq str (substring str (match-end 0))))
1539   ;; Trailing spaces.
1540   (when (string-match "[ \t]+$" str)
1541     (setq str (substring str 0 (match-beginning 0))))
1542   str)
1543
1544 (defun gnus-simplify-all-whitespace (str)
1545   "Remove all whitespace from STR."
1546   (while (string-match "[ \t\n]+" str)
1547     (setq str (replace-match "" nil nil str)))
1548   str)
1549
1550 (defsubst gnus-simplify-subject-re (subject)
1551   "Remove \"Re:\" from subject lines."
1552   (if (string-match message-subject-re-regexp subject)
1553       (substring subject (match-end 0))
1554     subject))
1555
1556 (defun gnus-simplify-subject (subject &optional re-only)
1557   "Remove `Re:' and words in parentheses.
1558 If RE-ONLY is non-nil, strip leading `Re:'s only."
1559   (let ((case-fold-search t))           ;Ignore case.
1560     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1561     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1562       (setq subject (substring subject (match-end 0))))
1563     ;; Remove uninteresting prefixes.
1564     (when (and (not re-only)
1565                gnus-simplify-ignored-prefixes
1566                (string-match gnus-simplify-ignored-prefixes subject))
1567       (setq subject (substring subject (match-end 0))))
1568     ;; Remove words in parentheses from end.
1569     (unless re-only
1570       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1571         (setq subject (substring subject 0 (match-beginning 0)))))
1572     ;; Return subject string.
1573     subject))
1574
1575 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1576 ;; all whitespace.
1577 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1578   (goto-char (point-min))
1579   (while (re-search-forward regexp nil t)
1580     (replace-match (or newtext ""))))
1581
1582 (defun gnus-simplify-buffer-fuzzy ()
1583   "Simplify string in the buffer fuzzily.
1584 The string in the accessible portion of the current buffer is simplified.
1585 It is assumed to be a single-line subject.
1586 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1587 matter is removed.  Additional things can be deleted by setting
1588 `gnus-simplify-subject-fuzzy-regexp'."
1589   (let ((case-fold-search t)
1590         (modified-tick))
1591     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1592
1593     (while (not (eq modified-tick (buffer-modified-tick)))
1594       (setq modified-tick (buffer-modified-tick))
1595       (cond
1596        ((listp gnus-simplify-subject-fuzzy-regexp)
1597         (mapcar 'gnus-simplify-buffer-fuzzy-step
1598                 gnus-simplify-subject-fuzzy-regexp))
1599        (gnus-simplify-subject-fuzzy-regexp
1600         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1601       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1602       (gnus-simplify-buffer-fuzzy-step
1603        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1604       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1605
1606     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1607     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1608     (gnus-simplify-buffer-fuzzy-step " $")
1609     (gnus-simplify-buffer-fuzzy-step "^ +")))
1610
1611 (defun gnus-simplify-subject-fuzzy (subject)
1612   "Simplify a subject string fuzzily.
1613 See `gnus-simplify-buffer-fuzzy' for details."
1614   (save-excursion
1615     (gnus-set-work-buffer)
1616     (let ((case-fold-search t))
1617       ;; Remove uninteresting prefixes.
1618       (when (and gnus-simplify-ignored-prefixes
1619                  (string-match gnus-simplify-ignored-prefixes subject))
1620         (setq subject (substring subject (match-end 0))))
1621       (insert subject)
1622       (inline (gnus-simplify-buffer-fuzzy))
1623       (buffer-string))))
1624
1625 (defsubst gnus-simplify-subject-fully (subject)
1626   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1627   (cond
1628    (gnus-simplify-subject-functions
1629     (gnus-map-function gnus-simplify-subject-functions subject))
1630    ((null gnus-summary-gather-subject-limit)
1631     (gnus-simplify-subject-re subject))
1632    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1633     (gnus-simplify-subject-fuzzy subject))
1634    ((numberp gnus-summary-gather-subject-limit)
1635     (gnus-limit-string (gnus-simplify-subject-re subject)
1636                        gnus-summary-gather-subject-limit))
1637    (t
1638     subject)))
1639
1640 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1641   "Check whether two subjects are equal.
1642 If optional argument SIMPLE-FIRST is t, first argument is already
1643 simplified."
1644   (cond
1645    ((null simple-first)
1646     (equal (gnus-simplify-subject-fully s1)
1647            (gnus-simplify-subject-fully s2)))
1648    (t
1649     (equal s1
1650            (gnus-simplify-subject-fully s2)))))
1651
1652 (defun gnus-summary-bubble-group ()
1653   "Increase the score of the current group.
1654 This is a handy function to add to `gnus-summary-exit-hook' to
1655 increase the score of each group you read."
1656   (gnus-group-add-score gnus-newsgroup-name))
1657
1658 \f
1659 ;;;
1660 ;;; Gnus summary mode
1661 ;;;
1662
1663 (put 'gnus-summary-mode 'mode-class 'special)
1664
1665 (defvar gnus-article-commands-menu)
1666
1667 ;; Non-orthogonal keys
1668
1669 (gnus-define-keys gnus-summary-mode-map
1670   " " gnus-summary-next-page
1671   "\177" gnus-summary-prev-page
1672   [delete] gnus-summary-prev-page
1673   [backspace] gnus-summary-prev-page
1674   "\r" gnus-summary-scroll-up
1675   "\M-\r" gnus-summary-scroll-down
1676   "n" gnus-summary-next-unread-article
1677   "p" gnus-summary-prev-unread-article
1678   "N" gnus-summary-next-article
1679   "P" gnus-summary-prev-article
1680   "\M-\C-n" gnus-summary-next-same-subject
1681   "\M-\C-p" gnus-summary-prev-same-subject
1682   "\M-n" gnus-summary-next-unread-subject
1683   "\M-p" gnus-summary-prev-unread-subject
1684   "." gnus-summary-first-unread-article
1685   "," gnus-summary-best-unread-article
1686   "\M-s" gnus-summary-search-article-forward
1687   "\M-r" gnus-summary-search-article-backward
1688   "<" gnus-summary-beginning-of-article
1689   ">" gnus-summary-end-of-article
1690   "j" gnus-summary-goto-article
1691   "^" gnus-summary-refer-parent-article
1692   "\M-^" gnus-summary-refer-article
1693   "u" gnus-summary-tick-article-forward
1694   "!" gnus-summary-tick-article-forward
1695   "U" gnus-summary-tick-article-backward
1696   "d" gnus-summary-mark-as-read-forward
1697   "D" gnus-summary-mark-as-read-backward
1698   "E" gnus-summary-mark-as-expirable
1699   "\M-u" gnus-summary-clear-mark-forward
1700   "\M-U" gnus-summary-clear-mark-backward
1701   "k" gnus-summary-kill-same-subject-and-select
1702   "\C-k" gnus-summary-kill-same-subject
1703   "\M-\C-k" gnus-summary-kill-thread
1704   "\M-\C-l" gnus-summary-lower-thread
1705   "e" gnus-summary-edit-article
1706   "#" gnus-summary-mark-as-processable
1707   "\M-#" gnus-summary-unmark-as-processable
1708   "\M-\C-t" gnus-summary-toggle-threads
1709   "\M-\C-s" gnus-summary-show-thread
1710   "\M-\C-h" gnus-summary-hide-thread
1711   "\M-\C-f" gnus-summary-next-thread
1712   "\M-\C-b" gnus-summary-prev-thread
1713   [(meta down)] gnus-summary-next-thread
1714   [(meta up)] gnus-summary-prev-thread
1715   "\M-\C-u" gnus-summary-up-thread
1716   "\M-\C-d" gnus-summary-down-thread
1717   "&" gnus-summary-execute-command
1718   "c" gnus-summary-catchup-and-exit
1719   "\C-w" gnus-summary-mark-region-as-read
1720   "\C-t" gnus-summary-toggle-truncation
1721   "?" gnus-summary-mark-as-dormant
1722   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1723   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1724   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1725   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1726   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1727   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1728   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1729   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1730   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1731   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1732   "=" gnus-summary-expand-window
1733   "\C-x\C-s" gnus-summary-reselect-current-group
1734   "\M-g" gnus-summary-rescan-group
1735   "w" gnus-summary-stop-page-breaking
1736   "\C-c\C-r" gnus-summary-caesar-message
1737   "\M-t" gnus-summary-toggle-mime
1738   "f" gnus-summary-followup
1739   "F" gnus-summary-followup-with-original
1740   "C" gnus-summary-cancel-article
1741   "r" gnus-summary-reply
1742   "R" gnus-summary-reply-with-original
1743   "\C-c\C-f" gnus-summary-mail-forward
1744   "o" gnus-summary-save-article
1745   "\C-o" gnus-summary-save-article-mail
1746   "|" gnus-summary-pipe-output
1747   "\M-k" gnus-summary-edit-local-kill
1748   "\M-K" gnus-summary-edit-global-kill
1749   ;; "V" gnus-version
1750   "\C-c\C-d" gnus-summary-describe-group
1751   "q" gnus-summary-exit
1752   "Q" gnus-summary-exit-no-update
1753   "\C-c\C-i" gnus-info-find-node
1754   gnus-mouse-2 gnus-mouse-pick-article
1755   "m" gnus-summary-mail-other-window
1756   "a" gnus-summary-post-news
1757   "i" gnus-summary-news-other-window
1758   "x" gnus-summary-limit-to-unread
1759   "s" gnus-summary-isearch-article
1760   "t" gnus-summary-toggle-header
1761   "g" gnus-summary-show-article
1762   "l" gnus-summary-goto-last-article
1763   "v" gnus-summary-preview-mime-message
1764   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1765   "\C-d" gnus-summary-enter-digest-group
1766   "\M-\C-d" gnus-summary-read-document
1767   "\M-\C-e" gnus-summary-edit-parameters
1768   "\M-\C-a" gnus-summary-customize-parameters
1769   "\C-c\C-b" gnus-bug
1770   "\C-c\C-n" gnus-namazu-search
1771   "*" gnus-cache-enter-article
1772   "\M-*" gnus-cache-remove-article
1773   "\M-&" gnus-summary-universal-argument
1774   "\C-l" gnus-recenter
1775   "I" gnus-summary-increase-score
1776   "L" gnus-summary-lower-score
1777   "\M-i" gnus-symbolic-argument
1778   "h" gnus-summary-select-article-buffer
1779
1780   "V" gnus-summary-score-map
1781   "X" gnus-uu-extract-map
1782   "S" gnus-summary-send-map)
1783
1784   ;; Sort of orthogonal keymap
1785 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1786   "t" gnus-summary-tick-article-forward
1787   "!" gnus-summary-tick-article-forward
1788   "d" gnus-summary-mark-as-read-forward
1789   "r" gnus-summary-mark-as-read-forward
1790   "c" gnus-summary-clear-mark-forward
1791   " " gnus-summary-clear-mark-forward
1792   "e" gnus-summary-mark-as-expirable
1793   "x" gnus-summary-mark-as-expirable
1794   "?" gnus-summary-mark-as-dormant
1795   "b" gnus-summary-set-bookmark
1796   "B" gnus-summary-remove-bookmark
1797   "#" gnus-summary-mark-as-processable
1798   "\M-#" gnus-summary-unmark-as-processable
1799   "S" gnus-summary-limit-include-expunged
1800   "C" gnus-summary-catchup
1801   "H" gnus-summary-catchup-to-here
1802   "h" gnus-summary-catchup-from-here
1803   "\C-c" gnus-summary-catchup-all
1804   "k" gnus-summary-kill-same-subject-and-select
1805   "K" gnus-summary-kill-same-subject
1806   "P" gnus-uu-mark-map)
1807
1808 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1809   "c" gnus-summary-clear-above
1810   "u" gnus-summary-tick-above
1811   "m" gnus-summary-mark-above
1812   "k" gnus-summary-kill-below)
1813
1814 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1815   "/" gnus-summary-limit-to-subject
1816   "n" gnus-summary-limit-to-articles
1817   "w" gnus-summary-pop-limit
1818   "s" gnus-summary-limit-to-subject
1819   "a" gnus-summary-limit-to-author
1820   "u" gnus-summary-limit-to-unread
1821   "m" gnus-summary-limit-to-marks
1822   "M" gnus-summary-limit-exclude-marks
1823   "v" gnus-summary-limit-to-score
1824   "*" gnus-summary-limit-include-cached
1825   "D" gnus-summary-limit-include-dormant
1826   "T" gnus-summary-limit-include-thread
1827   "d" gnus-summary-limit-exclude-dormant
1828   "t" gnus-summary-limit-to-age
1829   "." gnus-summary-limit-to-unseen
1830   "x" gnus-summary-limit-to-extra
1831   "p" gnus-summary-limit-to-display-predicate
1832   "E" gnus-summary-limit-include-expunged
1833   "c" gnus-summary-limit-exclude-childless-dormant
1834   "C" gnus-summary-limit-mark-excluded-as-read
1835   "o" gnus-summary-insert-old-articles
1836   "N" gnus-summary-insert-new-articles
1837   "r" gnus-summary-limit-to-replied)
1838
1839 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1840   "n" gnus-summary-next-unread-article
1841   "p" gnus-summary-prev-unread-article
1842   "N" gnus-summary-next-article
1843   "P" gnus-summary-prev-article
1844   "\C-n" gnus-summary-next-same-subject
1845   "\C-p" gnus-summary-prev-same-subject
1846   "\M-n" gnus-summary-next-unread-subject
1847   "\M-p" gnus-summary-prev-unread-subject
1848   "f" gnus-summary-first-unread-article
1849   "b" gnus-summary-best-unread-article
1850   "j" gnus-summary-goto-article
1851   "g" gnus-summary-goto-subject
1852   "l" gnus-summary-goto-last-article
1853   "o" gnus-summary-pop-article)
1854
1855 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1856   "k" gnus-summary-kill-thread
1857   "l" gnus-summary-lower-thread
1858   "i" gnus-summary-raise-thread
1859   "T" gnus-summary-toggle-threads
1860   "t" gnus-summary-rethread-current
1861   "^" gnus-summary-reparent-thread
1862   "s" gnus-summary-show-thread
1863   "S" gnus-summary-show-all-threads
1864   "h" gnus-summary-hide-thread
1865   "H" gnus-summary-hide-all-threads
1866   "n" gnus-summary-next-thread
1867   "p" gnus-summary-prev-thread
1868   "u" gnus-summary-up-thread
1869   "o" gnus-summary-top-thread
1870   "d" gnus-summary-down-thread
1871   "#" gnus-uu-mark-thread
1872   "\M-#" gnus-uu-unmark-thread)
1873
1874 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1875   "g" gnus-summary-prepare
1876   "c" gnus-summary-insert-cached-articles
1877   "d" gnus-summary-insert-dormant-articles)
1878
1879 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1880   "c" gnus-summary-catchup-and-exit
1881   "C" gnus-summary-catchup-all-and-exit
1882   "E" gnus-summary-exit-no-update
1883   "J" gnus-summary-jump-to-other-group
1884   "Q" gnus-summary-exit
1885   "Z" gnus-summary-exit
1886   "n" gnus-summary-catchup-and-goto-next-group
1887   "R" gnus-summary-reselect-current-group
1888   "G" gnus-summary-rescan-group
1889   "N" gnus-summary-next-group
1890   "s" gnus-summary-save-newsrc
1891   "P" gnus-summary-prev-group)
1892
1893 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1894   " " gnus-summary-next-page
1895   "n" gnus-summary-next-page
1896   "\177" gnus-summary-prev-page
1897   [delete] gnus-summary-prev-page
1898   "p" gnus-summary-prev-page
1899   "\r" gnus-summary-scroll-up
1900   "\M-\r" gnus-summary-scroll-down
1901   "<" gnus-summary-beginning-of-article
1902   ">" gnus-summary-end-of-article
1903   "b" gnus-summary-beginning-of-article
1904   "e" gnus-summary-end-of-article
1905   "^" gnus-summary-refer-parent-article
1906   "r" gnus-summary-refer-parent-article
1907   "D" gnus-summary-enter-digest-group
1908   "R" gnus-summary-refer-references
1909   "T" gnus-summary-refer-thread
1910   "g" gnus-summary-show-article
1911   "s" gnus-summary-isearch-article
1912   "P" gnus-summary-print-article
1913   "M" gnus-mailing-list-insinuate
1914   "t" gnus-article-babel)
1915
1916 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1917   "b" gnus-article-add-buttons
1918   "B" gnus-article-add-buttons-to-head
1919   "o" gnus-article-treat-overstrike
1920   "e" gnus-article-emphasize
1921   "w" gnus-article-fill-cited-article
1922   "Q" gnus-article-fill-long-lines
1923   "C" gnus-article-capitalize-sentences
1924   "c" gnus-article-remove-cr
1925   "Z" gnus-article-decode-HZ
1926   "A" gnus-article-treat-ansi-sequences
1927   "h" gnus-article-wash-html
1928   "u" gnus-article-unsplit-urls
1929   "f" gnus-article-display-x-face
1930   "l" gnus-summary-stop-page-breaking
1931   "r" gnus-summary-caesar-message
1932   "m" gnus-summary-morse-message
1933   "t" gnus-summary-toggle-header
1934   "g" gnus-treat-smiley
1935   "v" gnus-summary-verbose-headers
1936   "m" gnus-summary-toggle-mime
1937   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1938   "p" gnus-article-verify-x-pgp-sig
1939   "d" gnus-article-treat-dumbquotes)
1940
1941 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1942   ;; mnemonic: deuglif*Y*
1943   "u" gnus-article-outlook-unwrap-lines
1944   "a" gnus-article-outlook-repair-attribution
1945   "c" gnus-article-outlook-rearrange-citation
1946   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1947
1948 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1949   "a" gnus-article-hide
1950   "h" gnus-article-hide-headers
1951   "b" gnus-article-hide-boring-headers
1952   "s" gnus-article-hide-signature
1953   "c" gnus-article-hide-citation
1954   "C" gnus-article-hide-citation-in-followups
1955   "l" gnus-article-hide-list-identifiers
1956   "B" gnus-article-strip-banner
1957   "P" gnus-article-hide-pem
1958   "\C-c" gnus-article-hide-citation-maybe)
1959
1960 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1961   "a" gnus-article-highlight
1962   "h" gnus-article-highlight-headers
1963   "c" gnus-article-highlight-citation
1964   "s" gnus-article-highlight-signature)
1965
1966 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1967   "f" gnus-article-treat-fold-headers
1968   "u" gnus-article-treat-unfold-headers
1969   "n" gnus-article-treat-fold-newsgroups)
1970
1971 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1972   "x" gnus-article-display-x-face
1973   "d" gnus-article-display-face
1974   "s" gnus-treat-smiley
1975   "D" gnus-article-remove-images
1976   "f" gnus-treat-from-picon
1977   "m" gnus-treat-mail-picon
1978   "n" gnus-treat-newsgroups-picon)
1979
1980 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1981   "z" gnus-article-date-ut
1982   "u" gnus-article-date-ut
1983   "l" gnus-article-date-local
1984   "p" gnus-article-date-english
1985   "e" gnus-article-date-lapsed
1986   "o" gnus-article-date-original
1987   "i" gnus-article-date-iso8601
1988   "s" gnus-article-date-user)
1989
1990 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1991   "t" gnus-article-remove-trailing-blank-lines
1992   "l" gnus-article-strip-leading-blank-lines
1993   "m" gnus-article-strip-multiple-blank-lines
1994   "a" gnus-article-strip-blank-lines
1995   "A" gnus-article-strip-all-blank-lines
1996   "s" gnus-article-strip-leading-space
1997   "e" gnus-article-strip-trailing-space
1998   "w" gnus-article-remove-leading-whitespace)
1999
2000 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2001   "v" gnus-version
2002   "f" gnus-summary-fetch-faq
2003   "d" gnus-summary-describe-group
2004   "h" gnus-summary-describe-briefly
2005   "i" gnus-info-find-node
2006   "c" gnus-group-fetch-charter
2007   "C" gnus-group-fetch-control)
2008
2009 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2010   "e" gnus-summary-expire-articles
2011   "\M-\C-e" gnus-summary-expire-articles-now
2012   "\177" gnus-summary-delete-article
2013   [delete] gnus-summary-delete-article
2014   [backspace] gnus-summary-delete-article
2015   "m" gnus-summary-move-article
2016   "r" gnus-summary-respool-article
2017   "w" gnus-summary-edit-article
2018   "c" gnus-summary-copy-article
2019   "B" gnus-summary-crosspost-article
2020   "q" gnus-summary-respool-query
2021   "t" gnus-summary-respool-trace
2022   "i" gnus-summary-import-article
2023   "I" gnus-summary-create-article
2024   "p" gnus-summary-article-posted-p)
2025
2026 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2027   "o" gnus-summary-save-article
2028   "m" gnus-summary-save-article-mail
2029   "F" gnus-summary-write-article-file
2030   "r" gnus-summary-save-article-rmail
2031   "f" gnus-summary-save-article-file
2032   "b" gnus-summary-save-article-body-file
2033   "h" gnus-summary-save-article-folder
2034   "v" gnus-summary-save-article-vm
2035   "p" gnus-summary-pipe-output
2036   "P" gnus-summary-muttprint
2037   "s" gnus-soup-add-article)
2038
2039 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2040   "b" gnus-summary-display-buttonized
2041   "m" gnus-summary-repair-multipart
2042   "v" gnus-article-view-part
2043   "o" gnus-article-save-part
2044   "c" gnus-article-copy-part
2045   "C" gnus-article-view-part-as-charset
2046   "e" gnus-article-view-part-externally
2047   "E" gnus-article-encrypt-body
2048   "i" gnus-article-inline-part
2049   "|" gnus-article-pipe-part)
2050
2051 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2052   "p" gnus-summary-mark-as-processable
2053   "u" gnus-summary-unmark-as-processable
2054   "U" gnus-summary-unmark-all-processable
2055   "v" gnus-uu-mark-over
2056   "s" gnus-uu-mark-series
2057   "r" gnus-uu-mark-region
2058   "g" gnus-uu-unmark-region
2059   "R" gnus-uu-mark-by-regexp
2060   "G" gnus-uu-unmark-by-regexp
2061   "t" gnus-uu-mark-thread
2062   "T" gnus-uu-unmark-thread
2063   "a" gnus-uu-mark-all
2064   "b" gnus-uu-mark-buffer
2065   "S" gnus-uu-mark-sparse
2066   "k" gnus-summary-kill-process-mark
2067   "y" gnus-summary-yank-process-mark
2068   "w" gnus-summary-save-process-mark
2069   "i" gnus-uu-invert-processable)
2070
2071 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2072   ;;"x" gnus-uu-extract-any
2073   "m" gnus-summary-save-parts
2074   "u" gnus-uu-decode-uu
2075   "U" gnus-uu-decode-uu-and-save
2076   "s" gnus-uu-decode-unshar
2077   "S" gnus-uu-decode-unshar-and-save
2078   "o" gnus-uu-decode-save
2079   "O" gnus-uu-decode-save
2080   "b" gnus-uu-decode-binhex
2081   "B" gnus-uu-decode-binhex
2082   "p" gnus-uu-decode-postscript
2083   "P" gnus-uu-decode-postscript-and-save)
2084
2085 (gnus-define-keys
2086     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2087   "u" gnus-uu-decode-uu-view
2088   "U" gnus-uu-decode-uu-and-save-view
2089   "s" gnus-uu-decode-unshar-view
2090   "S" gnus-uu-decode-unshar-and-save-view
2091   "o" gnus-uu-decode-save-view
2092   "O" gnus-uu-decode-save-view
2093   "b" gnus-uu-decode-binhex-view
2094   "B" gnus-uu-decode-binhex-view
2095   "p" gnus-uu-decode-postscript-view
2096   "P" gnus-uu-decode-postscript-and-save-view)
2097
2098 (defvar gnus-article-post-menu nil)
2099
2100 (defconst gnus-summary-menu-maxlen 20)
2101
2102 (defun gnus-summary-menu-split (menu)
2103   ;; If we have lots of elements, divide them into groups of 20
2104   ;; and make a pane (or submenu) for each one.
2105   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2106       (let ((menu menu) sublists next
2107             (i 1))
2108         (while menu
2109           ;; Pull off the next gnus-summary-menu-maxlen elements
2110           ;; and make them the next element of sublist.
2111           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2112           (if next
2113               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2114                       nil))
2115           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2116                                              (aref (car (last menu)) 0)) menu)
2117                                sublists))
2118           (setq i (1+ i))
2119           (setq menu next))
2120         (nreverse sublists))
2121     ;; Few elements--put them all in one pane.
2122     menu))
2123
2124 (defun gnus-summary-make-menu-bar ()
2125   (gnus-turn-off-edit-menu 'summary)
2126
2127   (unless (boundp 'gnus-summary-misc-menu)
2128
2129     (easy-menu-define
2130       gnus-summary-kill-menu gnus-summary-mode-map ""
2131       (cons
2132        "Score"
2133        (nconc
2134         (list
2135          ["Customize" gnus-score-customize t])
2136         (gnus-make-score-map 'increase)
2137         (gnus-make-score-map 'lower)
2138         '(("Mark"
2139            ["Kill below" gnus-summary-kill-below t]
2140            ["Mark above" gnus-summary-mark-above t]
2141            ["Tick above" gnus-summary-tick-above t]
2142            ["Clear above" gnus-summary-clear-above t])
2143           ["Current score" gnus-summary-current-score t]
2144           ["Set score" gnus-summary-set-score t]
2145           ["Switch current score file..." gnus-score-change-score-file t]
2146           ["Set mark below..." gnus-score-set-mark-below t]
2147           ["Set expunge below..." gnus-score-set-expunge-below t]
2148           ["Edit current score file" gnus-score-edit-current-scores t]
2149           ["Edit score file" gnus-score-edit-file t]
2150           ["Trace score" gnus-score-find-trace t]
2151           ["Find words" gnus-score-find-favourite-words t]
2152           ["Rescore buffer" gnus-summary-rescore t]
2153           ["Increase score..." gnus-summary-increase-score t]
2154           ["Lower score..." gnus-summary-lower-score t]))))
2155
2156     ;; Define both the Article menu in the summary buffer and the
2157     ;; equivalent Commands menu in the article buffer here for
2158     ;; consistency.
2159     (let ((innards
2160            `(("Hide"
2161               ["All" gnus-article-hide t]
2162               ["Headers" gnus-article-hide-headers t]
2163               ["Signature" gnus-article-hide-signature t]
2164               ["Citation" gnus-article-hide-citation t]
2165               ["List identifiers" gnus-article-hide-list-identifiers t]
2166               ["Banner" gnus-article-strip-banner t]
2167               ["Boring headers" gnus-article-hide-boring-headers t])
2168              ("Highlight"
2169               ["All" gnus-article-highlight t]
2170               ["Headers" gnus-article-highlight-headers t]
2171               ["Signature" gnus-article-highlight-signature t]
2172               ["Citation" gnus-article-highlight-citation t])
2173              ("Date"
2174               ["Local" gnus-article-date-local t]
2175               ["ISO8601" gnus-article-date-iso8601 t]
2176               ["UT" gnus-article-date-ut t]
2177               ["Original" gnus-article-date-original t]
2178               ["Lapsed" gnus-article-date-lapsed t]
2179               ["User-defined" gnus-article-date-user t])
2180              ("Display"
2181               ["Remove images" gnus-article-remove-images t]
2182               ["Toggle smiley" gnus-treat-smiley t]
2183               ["Show X-Face" gnus-article-display-x-face t]
2184               ["Show picons in From" gnus-treat-from-picon t]
2185               ["Show picons in mail headers" gnus-treat-mail-picon t]
2186               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2187               ("View as different encoding"
2188                ,@(gnus-summary-menu-split
2189                   (mapcar
2190                    (lambda (cs)
2191                      ;; Since easymenu under Emacs doesn't allow
2192                      ;; lambda forms for menu commands, we should
2193                      ;; provide intern'ed function symbols.
2194                      (let ((command (intern (format "\
2195 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2196                        (fset command
2197                              `(lambda ()
2198                                 (interactive)
2199                                 (let ((gnus-summary-show-article-charset-alist
2200                                        '((1 . ,cs))))
2201                                   (gnus-summary-show-article 1))))
2202                        `[,(symbol-name cs) ,command t]))
2203                    (sort (if (fboundp 'coding-system-list)
2204                              (coding-system-list)
2205                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2206                            )
2207                          'string<)))))
2208              ("Washing"
2209               ("Remove Blanks"
2210                ["Leading" gnus-article-strip-leading-blank-lines t]
2211                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2212                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2213                ["All of the above" gnus-article-strip-blank-lines t]
2214                ["All" gnus-article-strip-all-blank-lines t]
2215                ["Leading space" gnus-article-strip-leading-space t]
2216                ["Trailing space" gnus-article-strip-trailing-space t]
2217                ["Leading space in headers"
2218                 gnus-article-remove-leading-whitespace t])
2219               ["Overstrike" gnus-article-treat-overstrike t]
2220               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2221               ["Emphasis" gnus-article-emphasize t]
2222               ["Word wrap" gnus-article-fill-cited-article t]
2223               ["Fill long lines" gnus-article-fill-long-lines t]
2224               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2225               ["Remove CR" gnus-article-remove-cr t]
2226               ["Rot 13" gnus-summary-caesar-message
2227                ,@(if (featurep 'xemacs) '(t)
2228                    '(:help "\"Caesar rotate\" article by 13"))]
2229               ["Morse decode" gnus-summary-morse-message t]
2230               ["Unix pipe..." gnus-summary-pipe-message t]
2231               ["Add buttons" gnus-article-add-buttons t]
2232               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2233               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2234               ["Toggle MIME" gnus-summary-toggle-mime t]
2235               ["Verbose header" gnus-summary-verbose-headers t]
2236               ["Toggle header" gnus-summary-toggle-header t]
2237               ["Unfold headers" gnus-article-treat-unfold-headers t]
2238               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2239               ["Html" gnus-article-wash-html t]
2240               ["Unsplit URLs" gnus-article-unsplit-urls t]
2241               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2242               ["Decode HZ" gnus-article-decode-HZ t]
2243               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2244               ("(Outlook) Deuglify"
2245                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2246                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2247                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2248                ["Full (Outlook) deuglify"
2249                 gnus-article-outlook-deuglify-article t])
2250               )
2251              ("Output"
2252               ["Save in default format..." gnus-summary-save-article
2253                ,@(if (featurep 'xemacs) '(t)
2254                    '(:help "Save article using default method"))]
2255               ["Save in file..." gnus-summary-save-article-file
2256                ,@(if (featurep 'xemacs) '(t)
2257                    '(:help "Save article in file"))]
2258               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2259               ["Save in MH folder..." gnus-summary-save-article-folder t]
2260               ["Save in VM folder..." gnus-summary-save-article-vm t]
2261               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2262               ["Save body in file..." gnus-summary-save-article-body-file t]
2263               ["Pipe through a filter..." gnus-summary-pipe-output t]
2264               ["Add to SOUP packet" gnus-soup-add-article t]
2265               ["Print with Muttprint..." gnus-summary-muttprint t]
2266               ["Print" gnus-summary-print-article
2267                ,@(if (featurep 'xemacs) '(t)
2268                    '(:help "Generate and print a PostScript image"))])
2269              ("Copy, move,... (Backend)"
2270               ,@(if (featurep 'xemacs) nil
2271                   '(:help "Copying, moving, expiring articles..."))
2272               ["Respool article..." gnus-summary-respool-article t]
2273               ["Move article..." gnus-summary-move-article
2274                (gnus-check-backend-function
2275                 'request-move-article gnus-newsgroup-name)]
2276               ["Copy article..." gnus-summary-copy-article t]
2277               ["Crosspost article..." gnus-summary-crosspost-article
2278                (gnus-check-backend-function
2279                 'request-replace-article gnus-newsgroup-name)]
2280               ["Import file..." gnus-summary-import-article
2281                (gnus-check-backend-function
2282                 'request-accept-article gnus-newsgroup-name)]
2283               ["Create article..." gnus-summary-create-article
2284                (gnus-check-backend-function
2285                 'request-accept-article gnus-newsgroup-name)]
2286               ["Check if posted" gnus-summary-article-posted-p t]
2287               ["Edit article" gnus-summary-edit-article
2288                (not (gnus-group-read-only-p))]
2289               ["Delete article" gnus-summary-delete-article
2290                (gnus-check-backend-function
2291                 'request-expire-articles gnus-newsgroup-name)]
2292               ["Query respool" gnus-summary-respool-query t]
2293               ["Trace respool" gnus-summary-respool-trace t]
2294               ["Delete expirable articles" gnus-summary-expire-articles-now
2295                (gnus-check-backend-function
2296                 'request-expire-articles gnus-newsgroup-name)])
2297              ("Extract"
2298               ["Uudecode" gnus-uu-decode-uu
2299                ,@(if (featurep 'xemacs) '(t)
2300                    '(:help "Decode uuencoded article(s)"))]
2301               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2302               ["Unshar" gnus-uu-decode-unshar t]
2303               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2304               ["Save" gnus-uu-decode-save t]
2305               ["Binhex" gnus-uu-decode-binhex t]
2306               ["Postscript" gnus-uu-decode-postscript t])
2307              ("Cache"
2308               ["Enter article" gnus-cache-enter-article t]
2309               ["Remove article" gnus-cache-remove-article t])
2310              ["Translate" gnus-article-babel t]
2311              ["Select article buffer" gnus-summary-select-article-buffer t]
2312              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2313              ["Isearch article..." gnus-summary-isearch-article t]
2314              ["Beginning of the article" gnus-summary-beginning-of-article t]
2315              ["End of the article" gnus-summary-end-of-article t]
2316              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2317              ["Fetch referenced articles" gnus-summary-refer-references t]
2318              ["Fetch current thread" gnus-summary-refer-thread t]
2319              ["Fetch article with id..." gnus-summary-refer-article t]
2320              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2321              ["Redisplay" gnus-summary-show-article t]
2322              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2323       (easy-menu-define
2324         gnus-summary-article-menu gnus-summary-mode-map ""
2325         (cons "Article" innards))
2326
2327       (if (not (keymapp gnus-summary-article-menu))
2328           (easy-menu-define
2329             gnus-article-commands-menu gnus-article-mode-map ""
2330             (cons "Commands" innards))
2331         ;; in Emacs, don't share menu.
2332         (setq gnus-article-commands-menu
2333               (copy-keymap gnus-summary-article-menu))
2334         (define-key gnus-article-mode-map [menu-bar commands]
2335           (cons "Commands" gnus-article-commands-menu))))
2336
2337     (easy-menu-define
2338       gnus-summary-thread-menu gnus-summary-mode-map ""
2339       '("Threads"
2340         ["Find all messages in thread" gnus-summary-refer-thread t]
2341         ["Toggle threading" gnus-summary-toggle-threads t]
2342         ["Hide threads" gnus-summary-hide-all-threads t]
2343         ["Show threads" gnus-summary-show-all-threads t]
2344         ["Hide thread" gnus-summary-hide-thread t]
2345         ["Show thread" gnus-summary-show-thread t]
2346         ["Go to next thread" gnus-summary-next-thread t]
2347         ["Go to previous thread" gnus-summary-prev-thread t]
2348         ["Go down thread" gnus-summary-down-thread t]
2349         ["Go up thread" gnus-summary-up-thread t]
2350         ["Top of thread" gnus-summary-top-thread t]
2351         ["Mark thread as read" gnus-summary-kill-thread t]
2352         ["Lower thread score" gnus-summary-lower-thread t]
2353         ["Raise thread score" gnus-summary-raise-thread t]
2354         ["Rethread current" gnus-summary-rethread-current t]))
2355
2356     (easy-menu-define
2357       gnus-summary-post-menu gnus-summary-mode-map ""
2358       `("Post"
2359         ["Send a message (mail or news)" gnus-summary-post-news
2360          ,@(if (featurep 'xemacs) '(t)
2361              '(:help "Compose a new message (mail or news)"))]
2362         ["Followup" gnus-summary-followup
2363          ,@(if (featurep 'xemacs) '(t)
2364              '(:help "Post followup to this article"))]
2365         ["Followup and yank" gnus-summary-followup-with-original
2366          ,@(if (featurep 'xemacs) '(t)
2367              '(:help "Post followup to this article, quoting its contents"))]
2368         ["Supersede article" gnus-summary-supersede-article t]
2369         ["Cancel article" gnus-summary-cancel-article
2370          ,@(if (featurep 'xemacs) '(t)
2371              '(:help "Cancel an article you posted"))]
2372         ["Reply" gnus-summary-reply t]
2373         ["Reply and yank" gnus-summary-reply-with-original t]
2374         ["Wide reply" gnus-summary-wide-reply t]
2375         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2376          ,@(if (featurep 'xemacs) '(t)
2377              '(:help "Mail a reply, quoting this article"))]
2378         ["Very wide reply" gnus-summary-very-wide-reply t]
2379         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2380          ,@(if (featurep 'xemacs) '(t)
2381              '(:help "Mail a very wide reply, quoting this article"))]
2382         ["Mail forward" gnus-summary-mail-forward t]
2383         ["Post forward" gnus-summary-post-forward t]
2384         ["Digest and mail" gnus-summary-digest-mail-forward t]
2385         ["Digest and post" gnus-summary-digest-post-forward t]
2386         ["Resend message" gnus-summary-resend-message t]
2387         ["Resend message edit" gnus-summary-resend-message-edit t]
2388         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2389         ["Send a mail" gnus-summary-mail-other-window t]
2390         ["Create a local message" gnus-summary-news-other-window t]
2391         ["Uuencode and post" gnus-uu-post-news
2392          ,@(if (featurep 'xemacs) '(t)
2393              '(:help "Post a uuencoded article"))]
2394         ["Followup via news" gnus-summary-followup-to-mail t]
2395         ["Followup via news and yank"
2396          gnus-summary-followup-to-mail-with-original t]
2397         ;;("Draft"
2398         ;;["Send" gnus-summary-send-draft t]
2399         ;;["Send bounced" gnus-resend-bounced-mail t])
2400         ))
2401
2402     (cond
2403      ((not (keymapp gnus-summary-post-menu))
2404       (setq gnus-article-post-menu gnus-summary-post-menu))
2405      ((not gnus-article-post-menu)
2406       ;; Don't share post menu.
2407       (setq gnus-article-post-menu
2408             (copy-keymap gnus-summary-post-menu))))
2409     (define-key gnus-article-mode-map [menu-bar post]
2410       (cons "Post" gnus-article-post-menu))
2411
2412     (easy-menu-define
2413       gnus-summary-misc-menu gnus-summary-mode-map ""
2414       `("Gnus"
2415         ("Mark Read"
2416          ["Mark as read" gnus-summary-mark-as-read-forward t]
2417          ["Mark same subject and select"
2418           gnus-summary-kill-same-subject-and-select t]
2419          ["Mark same subject" gnus-summary-kill-same-subject t]
2420          ["Catchup" gnus-summary-catchup
2421           ,@(if (featurep 'xemacs) '(t)
2422               '(:help "Mark unread articles in this group as read"))]
2423          ["Catchup all" gnus-summary-catchup-all t]
2424          ["Catchup to here" gnus-summary-catchup-to-here t]
2425          ["Catchup from here" gnus-summary-catchup-from-here t]
2426          ["Catchup region" gnus-summary-mark-region-as-read
2427           (gnus-mark-active-p)]
2428          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2429         ("Mark Various"
2430          ["Tick" gnus-summary-tick-article-forward t]
2431          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2432          ["Remove marks" gnus-summary-clear-mark-forward t]
2433          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2434          ["Set bookmark" gnus-summary-set-bookmark t]
2435          ["Remove bookmark" gnus-summary-remove-bookmark t])
2436         ("Limit to"
2437          ["Marks..." gnus-summary-limit-to-marks t]
2438          ["Subject..." gnus-summary-limit-to-subject t]
2439          ["Author..." gnus-summary-limit-to-author t]
2440          ["Age..." gnus-summary-limit-to-age t]
2441          ["Extra..." gnus-summary-limit-to-extra t]
2442          ["Score..." gnus-summary-limit-to-score t]
2443          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2444          ["Unread" gnus-summary-limit-to-unread t]
2445          ["Unseen" gnus-summary-limit-to-unseen t]
2446          ["Replied" gnus-summary-limit-to-replied t]
2447          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2448          ["Next articles" gnus-summary-limit-to-articles t]
2449          ["Pop limit" gnus-summary-pop-limit t]
2450          ["Show dormant" gnus-summary-limit-include-dormant t]
2451          ["Hide childless dormant"
2452           gnus-summary-limit-exclude-childless-dormant t]
2453          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2454          ["Hide marked" gnus-summary-limit-exclude-marks t]
2455          ["Show expunged" gnus-summary-limit-include-expunged t])
2456         ("Process Mark"
2457          ["Set mark" gnus-summary-mark-as-processable t]
2458          ["Remove mark" gnus-summary-unmark-as-processable t]
2459          ["Remove all marks" gnus-summary-unmark-all-processable t]
2460          ["Invert marks" gnus-uu-invert-processable t]
2461          ["Mark above" gnus-uu-mark-over t]
2462          ["Mark series" gnus-uu-mark-series t]
2463          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2464          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2465          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2466          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2467          ["Mark all" gnus-uu-mark-all t]
2468          ["Mark buffer" gnus-uu-mark-buffer t]
2469          ["Mark sparse" gnus-uu-mark-sparse t]
2470          ["Mark thread" gnus-uu-mark-thread t]
2471          ["Unmark thread" gnus-uu-unmark-thread t]
2472          ("Process Mark Sets"
2473           ["Kill" gnus-summary-kill-process-mark t]
2474           ["Yank" gnus-summary-yank-process-mark
2475            gnus-newsgroup-process-stack]
2476           ["Save" gnus-summary-save-process-mark t]
2477           ["Run command on marked..." gnus-summary-universal-argument t]))
2478         ("Scroll article"
2479          ["Page forward" gnus-summary-next-page
2480           ,@(if (featurep 'xemacs) '(t)
2481               '(:help "Show next page of article"))]
2482          ["Page backward" gnus-summary-prev-page
2483           ,@(if (featurep 'xemacs) '(t)
2484               '(:help "Show previous page of article"))]
2485          ["Line forward" gnus-summary-scroll-up t])
2486         ("Move"
2487          ["Next unread article" gnus-summary-next-unread-article t]
2488          ["Previous unread article" gnus-summary-prev-unread-article t]
2489          ["Next article" gnus-summary-next-article t]
2490          ["Previous article" gnus-summary-prev-article t]
2491          ["Next unread subject" gnus-summary-next-unread-subject t]
2492          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2493          ["Next article same subject" gnus-summary-next-same-subject t]
2494          ["Previous article same subject" gnus-summary-prev-same-subject t]
2495          ["First unread article" gnus-summary-first-unread-article t]
2496          ["Best unread article" gnus-summary-best-unread-article t]
2497          ["Go to subject number..." gnus-summary-goto-subject t]
2498          ["Go to article number..." gnus-summary-goto-article t]
2499          ["Go to the last article" gnus-summary-goto-last-article t]
2500          ["Pop article off history" gnus-summary-pop-article t])
2501         ("Sort"
2502          ["Sort by number" gnus-summary-sort-by-number t]
2503          ["Sort by author" gnus-summary-sort-by-author t]
2504          ["Sort by subject" gnus-summary-sort-by-subject t]
2505          ["Sort by date" gnus-summary-sort-by-date t]
2506          ["Sort by score" gnus-summary-sort-by-score t]
2507          ["Sort by lines" gnus-summary-sort-by-lines t]
2508          ["Sort by characters" gnus-summary-sort-by-chars t]
2509          ["Randomize" gnus-summary-sort-by-random t]
2510          ["Original sort" gnus-summary-sort-by-original t])
2511         ("Help"
2512          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2513          ["Describe group" gnus-summary-describe-group t]
2514          ["Fetch charter" gnus-group-fetch-charter
2515           ,@(if (featurep 'xemacs) nil
2516               '(:help "Display the charter of the current group"))]
2517          ["Fetch control message" gnus-group-fetch-control
2518           ,@(if (featurep 'xemacs) nil
2519               '(:help "Display the archived control message for the current group"))]
2520          ["Read manual" gnus-info-find-node t])
2521         ("Modes"
2522          ["Pick and read" gnus-pick-mode t]
2523          ["Binary" gnus-binary-mode t])
2524         ("Regeneration"
2525          ["Regenerate" gnus-summary-prepare t]
2526          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2527          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2528          ["Toggle threading" gnus-summary-toggle-threads t])
2529         ["See old articles" gnus-summary-insert-old-articles t]
2530         ["See new articles" gnus-summary-insert-new-articles t]
2531         ["Filter articles..." gnus-summary-execute-command t]
2532         ["Run command on articles..." gnus-summary-universal-argument t]
2533         ["Search articles forward..." gnus-summary-search-article-forward t]
2534         ["Search articles backward..." gnus-summary-search-article-backward t]
2535         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2536         ["Expand window" gnus-summary-expand-window t]
2537         ["Expire expirable articles" gnus-summary-expire-articles
2538          (gnus-check-backend-function
2539           'request-expire-articles gnus-newsgroup-name)]
2540         ["Edit local kill file" gnus-summary-edit-local-kill t]
2541         ["Edit main kill file" gnus-summary-edit-global-kill t]
2542         ["Edit group parameters" gnus-summary-edit-parameters t]
2543         ["Customize group parameters" gnus-summary-customize-parameters t]
2544         ["Send a bug report" gnus-bug t]
2545         ("Exit"
2546          ["Catchup and exit" gnus-summary-catchup-and-exit
2547           ,@(if (featurep 'xemacs) '(t)
2548               '(:help "Mark unread articles in this group as read, then exit"))]
2549          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2550          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2551          ["Exit group" gnus-summary-exit
2552           ,@(if (featurep 'xemacs) '(t)
2553               '(:help "Exit current group, return to group selection mode"))]
2554          ["Exit group without updating" gnus-summary-exit-no-update t]
2555          ["Exit and goto next group" gnus-summary-next-group t]
2556          ["Exit and goto prev group" gnus-summary-prev-group t]
2557          ["Reselect group" gnus-summary-reselect-current-group t]
2558          ["Rescan group" gnus-summary-rescan-group t]
2559          ["Update dribble" gnus-summary-save-newsrc t])))
2560
2561     (gnus-run-hooks 'gnus-summary-menu-hook)))
2562
2563 (defvar gnus-summary-tool-bar-map nil)
2564
2565 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2566 (defun gnus-summary-make-tool-bar ()
2567   (if (and (fboundp 'tool-bar-add-item-from-menu)
2568            (default-value 'tool-bar-mode)
2569            (not gnus-summary-tool-bar-map))
2570       (setq gnus-summary-tool-bar-map
2571             (let ((tool-bar-map (make-sparse-keymap))
2572                   (load-path (mm-image-load-path)))
2573               (tool-bar-add-item-from-menu
2574                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2575               (tool-bar-add-item-from-menu
2576                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2577               (tool-bar-add-item-from-menu
2578                'gnus-summary-post-news "post" gnus-summary-mode-map)
2579               (tool-bar-add-item-from-menu
2580                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2581               (tool-bar-add-item-from-menu
2582                'gnus-summary-followup "followup" gnus-summary-mode-map)
2583               (tool-bar-add-item-from-menu
2584                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2585               (tool-bar-add-item-from-menu
2586                'gnus-summary-reply "reply" gnus-summary-mode-map)
2587               (tool-bar-add-item-from-menu
2588                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2589               (tool-bar-add-item-from-menu
2590                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2591               (tool-bar-add-item-from-menu
2592                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2593               (tool-bar-add-item-from-menu
2594                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2595               (tool-bar-add-item-from-menu
2596                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2597               (tool-bar-add-item-from-menu
2598                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2599               (tool-bar-add-item-from-menu
2600                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2601               (tool-bar-add-item-from-menu
2602                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2603               (tool-bar-add-item-from-menu
2604                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2605               tool-bar-map)))
2606   (if gnus-summary-tool-bar-map
2607       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2608
2609 (defun gnus-score-set-default (var value)
2610   "A version of set that updates the GNU Emacs menu-bar."
2611   (set var value)
2612   ;; It is the message that forces the active status to be updated.
2613   (message ""))
2614
2615 (defun gnus-make-score-map (type)
2616   "Make a summary score map of type TYPE."
2617   (if t
2618       nil
2619     (let ((headers '(("author" "from" string)
2620                      ("subject" "subject" string)
2621                      ("article body" "body" string)
2622                      ("article head" "head" string)
2623                      ("xref" "xref" string)
2624                      ("extra header" "extra" string)
2625                      ("lines" "lines" number)
2626                      ("followups to author" "followup" string)))
2627           (types '((number ("less than" <)
2628                            ("greater than" >)
2629                            ("equal" =))
2630                    (string ("substring" s)
2631                            ("exact string" e)
2632                            ("fuzzy string" f)
2633                            ("regexp" r))))
2634           (perms '(("temporary" (current-time-string))
2635                    ("permanent" nil)
2636                    ("immediate" now)))
2637           header)
2638       (list
2639        (apply
2640         'nconc
2641         (list
2642          (if (eq type 'lower)
2643              "Lower score"
2644            "Increase score"))
2645         (let (outh)
2646           (while headers
2647             (setq header (car headers))
2648             (setq outh
2649                   (cons
2650                    (apply
2651                     'nconc
2652                     (list (car header))
2653                     (let ((ts (cdr (assoc (nth 2 header) types)))
2654                           outt)
2655                       (while ts
2656                         (setq outt
2657                               (cons
2658                                (apply
2659                                 'nconc
2660                                 (list (caar ts))
2661                                 (let ((ps perms)
2662                                       outp)
2663                                   (while ps
2664                                     (setq outp
2665                                           (cons
2666                                            (vector
2667                                             (caar ps)
2668                                             (list
2669                                              'gnus-summary-score-entry
2670                                              (nth 1 header)
2671                                              (if (or (string= (nth 1 header)
2672                                                               "head")
2673                                                      (string= (nth 1 header)
2674                                                               "body"))
2675                                                  ""
2676                                                (list 'gnus-summary-header
2677                                                      (nth 1 header)))
2678                                              (list 'quote (nth 1 (car ts)))
2679                                              (list 'gnus-score-delta-default
2680                                                    nil)
2681                                              (nth 1 (car ps))
2682                                              t)
2683                                             t)
2684                                            outp))
2685                                     (setq ps (cdr ps)))
2686                                   (list (nreverse outp))))
2687                                outt))
2688                         (setq ts (cdr ts)))
2689                       (list (nreverse outt))))
2690                    outh))
2691             (setq headers (cdr headers)))
2692           (list (nreverse outh))))))))
2693
2694 \f
2695
2696 (defun gnus-summary-mode (&optional group)
2697   "Major mode for reading articles.
2698
2699 All normal editing commands are switched off.
2700 \\<gnus-summary-mode-map>
2701 Each line in this buffer represents one article.  To read an
2702 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2703 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2704 respectively.
2705
2706 You can also post articles and send mail from this buffer.  To
2707 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2708 of an article, type `\\[gnus-summary-reply]'.
2709
2710 There are approx. one gazillion commands you can execute in this
2711 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2712
2713 The following commands are available:
2714
2715 \\{gnus-summary-mode-map}"
2716   (interactive)
2717   (kill-all-local-variables)
2718   (when (gnus-visual-p 'summary-menu 'menu)
2719     (gnus-summary-make-menu-bar)
2720     (gnus-summary-make-tool-bar))
2721   (gnus-summary-make-local-variables)
2722   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2723     (gnus-summary-make-local-variables))
2724   (gnus-make-thread-indent-array)
2725   (gnus-simplify-mode-line)
2726   (setq major-mode 'gnus-summary-mode)
2727   (setq mode-name "Summary")
2728   (make-local-variable 'minor-mode-alist)
2729   (use-local-map gnus-summary-mode-map)
2730   (buffer-disable-undo)
2731   (setq buffer-read-only t              ;Disable modification
2732         show-trailing-whitespace nil)
2733   (setq truncate-lines t)
2734   (setq selective-display t)
2735   (setq selective-display-ellipses t)   ;Display `...'
2736   (gnus-summary-set-display-table)
2737   (gnus-set-default-directory)
2738   (setq gnus-newsgroup-name group)
2739   (unless (gnus-news-group-p group)
2740     (setq gnus-newsgroup-incorporated
2741           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2742   (make-local-variable 'gnus-summary-line-format)
2743   (make-local-variable 'gnus-summary-line-format-spec)
2744   (make-local-variable 'gnus-summary-dummy-line-format)
2745   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2746   (make-local-variable 'gnus-summary-mark-positions)
2747   (gnus-make-local-hook 'pre-command-hook)
2748   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2749   (gnus-run-hooks 'gnus-summary-mode-hook)
2750   (turn-on-gnus-mailing-list-mode)
2751   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2752   (gnus-update-summary-mark-positions))
2753
2754 (defun gnus-summary-make-local-variables ()
2755   "Make all the local summary buffer variables."
2756   (let (global)
2757     (dolist (local gnus-summary-local-variables)
2758       (if (consp local)
2759           (progn
2760             (if (eq (cdr local) 'global)
2761                 ;; Copy the global value of the variable.
2762                 (setq global (symbol-value (car local)))
2763               ;; Use the value from the list.
2764               (setq global (eval (cdr local))))
2765             (set (make-local-variable (car local)) global))
2766         ;; Simple nil-valued local variable.
2767         (set (make-local-variable local) nil)))))
2768
2769 (defun gnus-summary-clear-local-variables ()
2770   (let ((locals gnus-summary-local-variables))
2771     (while locals
2772       (if (consp (car locals))
2773           (and (vectorp (caar locals))
2774                (set (caar locals) nil))
2775         (and (vectorp (car locals))
2776              (set (car locals) nil)))
2777       (setq locals (cdr locals)))))
2778
2779 ;; Summary data functions.
2780
2781 (defmacro gnus-data-number (data)
2782   `(car ,data))
2783
2784 (defmacro gnus-data-set-number (data number)
2785   `(setcar ,data ,number))
2786
2787 (defmacro gnus-data-mark (data)
2788   `(nth 1 ,data))
2789
2790 (defmacro gnus-data-set-mark (data mark)
2791   `(setcar (nthcdr 1 ,data) ,mark))
2792
2793 (defmacro gnus-data-pos (data)
2794   `(nth 2 ,data))
2795
2796 (defmacro gnus-data-set-pos (data pos)
2797   `(setcar (nthcdr 2 ,data) ,pos))
2798
2799 (defmacro gnus-data-header (data)
2800   `(nth 3 ,data))
2801
2802 (defmacro gnus-data-set-header (data header)
2803   `(setcar (nthcdr 3 ,data) ,header))
2804
2805 (defmacro gnus-data-level (data)
2806   `(nth 4 ,data))
2807
2808 (defmacro gnus-data-unread-p (data)
2809   `(= (nth 1 ,data) gnus-unread-mark))
2810
2811 (defmacro gnus-data-read-p (data)
2812   `(/= (nth 1 ,data) gnus-unread-mark))
2813
2814 (defmacro gnus-data-pseudo-p (data)
2815   `(consp (nth 3 ,data)))
2816
2817 (defmacro gnus-data-find (number)
2818   `(assq ,number gnus-newsgroup-data))
2819
2820 (defmacro gnus-data-find-list (number &optional data)
2821   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2822      (memq (assq ,number bdata)
2823            bdata)))
2824
2825 (defmacro gnus-data-make (number mark pos header level)
2826   `(list ,number ,mark ,pos ,header ,level))
2827
2828 (defun gnus-data-enter (after-article number mark pos header level offset)
2829   (let ((data (gnus-data-find-list after-article)))
2830     (unless data
2831       (error "No such article: %d" after-article))
2832     (setcdr data (cons (gnus-data-make number mark pos header level)
2833                        (cdr data)))
2834     (setq gnus-newsgroup-data-reverse nil)
2835     (gnus-data-update-list (cddr data) offset)))
2836
2837 (defun gnus-data-enter-list (after-article list &optional offset)
2838   (when list
2839     (let ((data (and after-article (gnus-data-find-list after-article)))
2840           (ilist list))
2841       (if (not (or data
2842                    after-article))
2843           (let ((odata gnus-newsgroup-data))
2844             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2845             (when offset
2846               (gnus-data-update-list odata offset)))
2847         ;; Find the last element in the list to be spliced into the main
2848         ;; list.
2849         (while (cdr list)
2850           (setq list (cdr list)))
2851         (if (not data)
2852             (progn
2853               (setcdr list gnus-newsgroup-data)
2854               (setq gnus-newsgroup-data ilist)
2855               (when offset
2856                 (gnus-data-update-list (cdr list) offset)))
2857           (setcdr list (cdr data))
2858           (setcdr data ilist)
2859           (when offset
2860             (gnus-data-update-list (cdr list) offset))))
2861       (setq gnus-newsgroup-data-reverse nil))))
2862
2863 (defun gnus-data-remove (article &optional offset)
2864   (let ((data gnus-newsgroup-data))
2865     (if (= (gnus-data-number (car data)) article)
2866         (progn
2867           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2868                 gnus-newsgroup-data-reverse nil)
2869           (when offset
2870             (gnus-data-update-list gnus-newsgroup-data offset)))
2871       (while (cdr data)
2872         (when (= (gnus-data-number (cadr data)) article)
2873           (setcdr data (cddr data))
2874           (when offset
2875             (gnus-data-update-list (cdr data) offset))
2876           (setq data nil
2877                 gnus-newsgroup-data-reverse nil))
2878         (setq data (cdr data))))))
2879
2880 (defmacro gnus-data-list (backward)
2881   `(if ,backward
2882        (or gnus-newsgroup-data-reverse
2883            (setq gnus-newsgroup-data-reverse
2884                  (reverse gnus-newsgroup-data)))
2885      gnus-newsgroup-data))
2886
2887 (defun gnus-data-update-list (data offset)
2888   "Add OFFSET to the POS of all data entries in DATA."
2889   (setq gnus-newsgroup-data-reverse nil)
2890   (while data
2891     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2892     (setq data (cdr data))))
2893
2894 (defun gnus-summary-article-pseudo-p (article)
2895   "Say whether this article is a pseudo article or not."
2896   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2897
2898 (defmacro gnus-summary-article-sparse-p (article)
2899   "Say whether this article is a sparse article or not."
2900   `(memq ,article gnus-newsgroup-sparse))
2901
2902 (defmacro gnus-summary-article-ancient-p (article)
2903   "Say whether this article is a sparse article or not."
2904   `(memq ,article gnus-newsgroup-ancient))
2905
2906 (defun gnus-article-parent-p (number)
2907   "Say whether this article is a parent or not."
2908   (let ((data (gnus-data-find-list number)))
2909     (and (cdr data)                     ; There has to be an article after...
2910          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2911             (gnus-data-level (nth 1 data))))))
2912
2913 (defun gnus-article-children (number)
2914   "Return a list of all children to NUMBER."
2915   (let* ((data (gnus-data-find-list number))
2916          (level (gnus-data-level (car data)))
2917          children)
2918     (setq data (cdr data))
2919     (while (and data
2920                 (= (gnus-data-level (car data)) (1+ level)))
2921       (push (gnus-data-number (car data)) children)
2922       (setq data (cdr data)))
2923     children))
2924
2925 (defmacro gnus-summary-skip-intangible ()
2926   "If the current article is intangible, then jump to a different article."
2927   '(let ((to (get-text-property (point) 'gnus-intangible)))
2928      (and to (gnus-summary-goto-subject to))))
2929
2930 (defmacro gnus-summary-article-intangible-p ()
2931   "Say whether this article is intangible or not."
2932   '(get-text-property (point) 'gnus-intangible))
2933
2934 (defun gnus-article-read-p (article)
2935   "Say whether ARTICLE is read or not."
2936   (not (or (memq article gnus-newsgroup-marked)
2937            (memq article gnus-newsgroup-spam-marked)
2938            (memq article gnus-newsgroup-unreads)
2939            (memq article gnus-newsgroup-unselected)
2940            (memq article gnus-newsgroup-dormant))))
2941
2942 ;; Some summary mode macros.
2943
2944 (defmacro gnus-summary-article-number ()
2945   "The article number of the article on the current line.
2946 If there isn't an article number here, then we return the current
2947 article number."
2948   '(progn
2949      (gnus-summary-skip-intangible)
2950      (or (get-text-property (point) 'gnus-number)
2951          (gnus-summary-last-subject))))
2952
2953 (defmacro gnus-summary-article-header (&optional number)
2954   "Return the header of article NUMBER."
2955   `(gnus-data-header (gnus-data-find
2956                       ,(or number '(gnus-summary-article-number)))))
2957
2958 (defmacro gnus-summary-thread-level (&optional number)
2959   "Return the level of thread that starts with article NUMBER."
2960   `(if (and (eq gnus-summary-make-false-root 'dummy)
2961             (get-text-property (point) 'gnus-intangible))
2962        0
2963      (gnus-data-level (gnus-data-find
2964                        ,(or number '(gnus-summary-article-number))))))
2965
2966 (defmacro gnus-summary-article-mark (&optional number)
2967   "Return the mark of article NUMBER."
2968   `(gnus-data-mark (gnus-data-find
2969                     ,(or number '(gnus-summary-article-number)))))
2970
2971 (defmacro gnus-summary-article-pos (&optional number)
2972   "Return the position of the line of article NUMBER."
2973   `(gnus-data-pos (gnus-data-find
2974                    ,(or number '(gnus-summary-article-number)))))
2975
2976 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2977 (defmacro gnus-summary-article-subject (&optional number)
2978   "Return current subject string or nil if nothing."
2979   `(let ((headers
2980           ,(if number
2981                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2982              '(gnus-data-header (assq (gnus-summary-article-number)
2983                                       gnus-newsgroup-data)))))
2984      (and headers
2985           (vectorp headers)
2986           (mail-header-subject headers))))
2987
2988 (defmacro gnus-summary-article-score (&optional number)
2989   "Return current article score."
2990   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2991                   gnus-newsgroup-scored))
2992        gnus-summary-default-score 0))
2993
2994 (defun gnus-summary-article-children (&optional number)
2995   "Return a list of article numbers that are children of article NUMBER."
2996   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2997          (level (gnus-data-level (car data)))
2998          l children)
2999     (while (and (setq data (cdr data))
3000                 (> (setq l (gnus-data-level (car data))) level))
3001       (and (= (1+ level) l)
3002            (push (gnus-data-number (car data))
3003                  children)))
3004     (nreverse children)))
3005
3006 (defun gnus-summary-article-parent (&optional number)
3007   "Return the article number of the parent of article NUMBER."
3008   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3009                                     (gnus-data-list t)))
3010          (level (gnus-data-level (car data))))
3011     (if (zerop level)
3012         ()                              ; This is a root.
3013       ;; We search until we find an article with a level less than
3014       ;; this one.  That function has to be the parent.
3015       (while (and (setq data (cdr data))
3016                   (not (< (gnus-data-level (car data)) level))))
3017       (and data (gnus-data-number (car data))))))
3018
3019 (defun gnus-unread-mark-p (mark)
3020   "Say whether MARK is the unread mark."
3021   (= mark gnus-unread-mark))
3022
3023 (defun gnus-read-mark-p (mark)
3024   "Say whether MARK is one of the marks that mark as read.
3025 This is all marks except unread, ticked, dormant, and expirable."
3026   (not (or (= mark gnus-unread-mark)
3027            (= mark gnus-ticked-mark)
3028            (= mark gnus-spam-mark)
3029            (= mark gnus-dormant-mark)
3030            (= mark gnus-expirable-mark))))
3031
3032 (defmacro gnus-article-mark (number)
3033   "Return the MARK of article NUMBER.
3034 This macro should only be used when computing the mark the \"first\"
3035 time; i.e., when generating the summary lines.  After that,
3036 `gnus-summary-article-mark' should be used to examine the
3037 marks of articles."
3038   `(cond
3039     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3040     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3041     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3042     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3043     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3044     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3045     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3046     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3047            gnus-ancient-mark))))
3048
3049 ;; Saving hidden threads.
3050
3051 (defmacro gnus-save-hidden-threads (&rest forms)
3052   "Save hidden threads, eval FORMS, and restore the hidden threads."
3053   (let ((config (make-symbol "config")))
3054     `(let ((,config (gnus-hidden-threads-configuration)))
3055        (unwind-protect
3056            (save-excursion
3057              ,@forms)
3058          (gnus-restore-hidden-threads-configuration ,config)))))
3059 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3060 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3061
3062 (defun gnus-data-compute-positions ()
3063   "Compute the positions of all articles."
3064   (setq gnus-newsgroup-data-reverse nil)
3065   (let ((data gnus-newsgroup-data))
3066     (save-excursion
3067       (gnus-save-hidden-threads
3068         (gnus-summary-show-all-threads)
3069         (goto-char (point-min))
3070         (while data
3071           (while (get-text-property (point) 'gnus-intangible)
3072             (forward-line 1))
3073           (gnus-data-set-pos (car data) (+ (point) 3))
3074           (setq data (cdr data))
3075           (forward-line 1))))))
3076
3077 (defun gnus-hidden-threads-configuration ()
3078   "Return the current hidden threads configuration."
3079   (save-excursion
3080     (let (config)
3081       (goto-char (point-min))
3082       (while (search-forward "\r" nil t)
3083         (push (1- (point)) config))
3084       config)))
3085
3086 (defun gnus-restore-hidden-threads-configuration (config)
3087   "Restore hidden threads configuration from CONFIG."
3088   (save-excursion
3089     (let (point buffer-read-only)
3090       (while (setq point (pop config))
3091         (when (and (< point (point-max))
3092                    (goto-char point)
3093                    (eq (char-after) ?\n))
3094           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3095
3096 ;; Various summary mode internalish functions.
3097
3098 (defun gnus-mouse-pick-article (e)
3099   (interactive "e")
3100   (mouse-set-point e)
3101   (gnus-summary-next-page nil t))
3102
3103 (defun gnus-summary-set-display-table ()
3104   "Change the display table.
3105 Odd characters have a tendency to mess
3106 up nicely formatted displays - we make all possible glyphs
3107 display only a single character."
3108
3109   ;; We start from the standard display table, if any.
3110   (let ((table (or (copy-sequence standard-display-table)
3111                    (make-display-table)))
3112         (i 32))
3113     ;; Nix out all the control chars...
3114     (while (>= (setq i (1- i)) 0)
3115       (aset table i [??]))
3116     ;; ... but not newline and cr, of course.  (cr is necessary for the
3117     ;; selective display).
3118     (aset table ?\n nil)
3119     (aset table ?\r nil)
3120     ;; We keep TAB as well.
3121     (aset table ?\t nil)
3122     ;; We nix out any glyphs over 126 that are not set already.
3123     (let ((i 256))
3124       (while (>= (setq i (1- i)) 127)
3125         ;; Only modify if the entry is nil.
3126         (unless (aref table i)
3127           (aset table i [??]))))
3128     (setq buffer-display-table table)))
3129
3130 (defun gnus-summary-set-article-display-arrow (pos)
3131   "Update the overlay arrow to point to line at position POS."
3132   (when (and gnus-summary-display-arrow
3133              (boundp 'overlay-arrow-position)
3134              (boundp 'overlay-arrow-string))
3135     (save-excursion
3136       (goto-char pos)
3137       (beginning-of-line)
3138       (unless overlay-arrow-position
3139         (setq overlay-arrow-position (make-marker)))
3140       (setq overlay-arrow-string "=>"
3141             overlay-arrow-position (set-marker overlay-arrow-position
3142                                                (point)
3143                                                (current-buffer))))))
3144
3145 (defun gnus-summary-setup-buffer (group)
3146   "Initialize summary buffer."
3147   (let ((buffer (gnus-summary-buffer-name group))
3148         (dead-name (concat "*Dead Summary "
3149                            (gnus-group-decoded-name group) "*")))
3150     ;; If a dead summary buffer exists, we kill it.
3151     (when (gnus-buffer-live-p dead-name)
3152       (gnus-kill-buffer dead-name))
3153     (if (get-buffer buffer)
3154         (progn
3155           (set-buffer buffer)
3156           (setq gnus-summary-buffer (current-buffer))
3157           (not gnus-newsgroup-prepared))
3158       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3159       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3160       (gnus-summary-mode group)
3161       (when gnus-carpal
3162         (gnus-carpal-setup-buffer 'summary))
3163       (unless gnus-single-article-buffer
3164         (make-local-variable 'gnus-article-buffer)
3165         (make-local-variable 'gnus-article-current)
3166         (make-local-variable 'gnus-original-article-buffer))
3167       (setq gnus-newsgroup-name group)
3168       ;; Set any local variables in the group parameters.
3169       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3170       t)))
3171
3172 (defun gnus-set-global-variables ()
3173   "Set the global equivalents of the buffer-local variables.
3174 They are set to the latest values they had.  These reflect the summary
3175 buffer that was in action when the last article was fetched."
3176   (when (eq major-mode 'gnus-summary-mode)
3177     (setq gnus-summary-buffer (current-buffer))
3178     (let ((name gnus-newsgroup-name)
3179           (marked gnus-newsgroup-marked)
3180           (spam gnus-newsgroup-spam-marked)
3181           (unread gnus-newsgroup-unreads)
3182           (headers gnus-current-headers)
3183           (data gnus-newsgroup-data)
3184           (summary gnus-summary-buffer)
3185           (article-buffer gnus-article-buffer)
3186           (original gnus-original-article-buffer)
3187           (gac gnus-article-current)
3188           (reffed gnus-reffed-article-number)
3189           (score-file gnus-current-score-file)
3190           (default-charset gnus-newsgroup-charset)
3191           vlist)
3192       (let ((locals gnus-newsgroup-variables))
3193         (while locals
3194           (if (consp (car locals))
3195               (push (eval (caar locals)) vlist)
3196             (push (eval (car locals)) vlist))
3197           (setq locals (cdr locals)))
3198         (setq vlist (nreverse vlist)))
3199       (with-current-buffer gnus-group-buffer
3200         (setq gnus-newsgroup-name name
3201               gnus-newsgroup-marked marked
3202               gnus-newsgroup-spam-marked spam
3203               gnus-newsgroup-unreads unread
3204               gnus-current-headers headers
3205               gnus-newsgroup-data data
3206               gnus-article-current gac
3207               gnus-summary-buffer summary
3208               gnus-article-buffer article-buffer
3209               gnus-original-article-buffer original
3210               gnus-reffed-article-number reffed
3211               gnus-current-score-file score-file
3212               gnus-newsgroup-charset default-charset)
3213         (let ((locals gnus-newsgroup-variables))
3214           (while locals
3215             (if (consp (car locals))
3216                 (set (caar locals) (pop vlist))
3217               (set (car locals) (pop vlist)))
3218             (setq locals (cdr locals))))
3219         ;; The article buffer also has local variables.
3220         (when (gnus-buffer-live-p gnus-article-buffer)
3221           (set-buffer gnus-article-buffer)
3222           (setq gnus-summary-buffer summary))))))
3223
3224 (defun gnus-summary-article-unread-p (article)
3225   "Say whether ARTICLE is unread or not."
3226   (memq article gnus-newsgroup-unreads))
3227
3228 (defun gnus-summary-first-article-p (&optional article)
3229   "Return whether ARTICLE is the first article in the buffer."
3230   (if (not (setq article (or article (gnus-summary-article-number))))
3231       nil
3232     (eq article (caar gnus-newsgroup-data))))
3233
3234 (defun gnus-summary-last-article-p (&optional article)
3235   "Return whether ARTICLE is the last article in the buffer."
3236   (if (not (setq article (or article (gnus-summary-article-number))))
3237       ;; All non-existent numbers are the last article.  :-)
3238       t
3239     (not (cdr (gnus-data-find-list article)))))
3240
3241 (defun gnus-make-thread-indent-array ()
3242   (let ((n 200))
3243     (unless (and gnus-thread-indent-array
3244                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3245       (setq gnus-thread-indent-array (make-vector 201 "")
3246             gnus-thread-indent-array-level gnus-thread-indent-level)
3247       (while (>= n 0)
3248         (aset gnus-thread-indent-array n
3249               (make-string (* n gnus-thread-indent-level) ? ))
3250         (setq n (1- n))))))
3251
3252 (defun gnus-update-summary-mark-positions ()
3253   "Compute where the summary marks are to go."
3254   (save-excursion
3255     (when (gnus-buffer-exists-p gnus-summary-buffer)
3256       (set-buffer gnus-summary-buffer))
3257     (let ((gnus-replied-mark 129)
3258           (gnus-score-below-mark 130)
3259           (gnus-score-over-mark 130)
3260           (gnus-undownloaded-mark 131)
3261           (spec gnus-summary-line-format-spec)
3262           gnus-visual pos)
3263       (save-excursion
3264         (gnus-set-work-buffer)
3265         (let ((gnus-summary-line-format-spec spec)
3266               (gnus-newsgroup-downloadable '(0)))
3267           (gnus-summary-insert-line
3268            (make-full-mail-header 0 "" "nobody"
3269                                   "05 Apr 2001 23:33:09 +0400"
3270                                   "" "" 0 0 "" nil)
3271            0 nil t 128 t nil "" nil 1)
3272           (goto-char (point-min))
3273           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3274                                              (- (point) (point-min) 1)))))
3275           (goto-char (point-min))
3276           (push (cons 'replied (and (search-forward "\201" nil t)
3277                                     (- (point) (point-min) 1)))
3278                 pos)
3279           (goto-char (point-min))
3280           (push (cons 'score (and (search-forward "\202" nil t)
3281                                   (- (point) (point-min) 1)))
3282                 pos)
3283           (goto-char (point-min))
3284           (push (cons 'download
3285                       (and (search-forward "\203" nil t)
3286                            (- (point) (point-min) 1)))
3287                 pos)))
3288       (setq gnus-summary-mark-positions pos))))
3289
3290 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3291   "Insert a dummy root in the summary buffer."
3292   (beginning-of-line)
3293   (gnus-add-text-properties
3294    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3295    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3296
3297 (defun gnus-summary-extract-address-component (from)
3298   (or (car (funcall gnus-extract-address-components from))
3299       from))
3300
3301 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3302   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3303                                 default-mime-charset)))
3304     ;; Is it really necessary to do this next part for each summary line?
3305     ;; Luckily, doesn't seem to slow things down much.
3306     (or
3307      (and gnus-ignored-from-addresses
3308           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3309           (let ((extra-headers (mail-header-extra header))
3310                 to
3311                 newsgroups)
3312             (cond
3313              ((setq to (cdr (assq 'To extra-headers)))
3314               (concat "-> "
3315                       (inline
3316                         (gnus-summary-extract-address-component
3317                          (funcall gnus-decode-encoded-word-function to)))))
3318              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3319               (concat "=> " newsgroups)))))
3320      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3321
3322 (defun gnus-summary-insert-line (gnus-tmp-header
3323                                  gnus-tmp-level gnus-tmp-current
3324                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3325                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3326                                  &optional gnus-tmp-dummy gnus-tmp-score
3327                                  gnus-tmp-process)
3328   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3329          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3330          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3331          (gnus-tmp-score-char
3332           (if (or (null gnus-summary-default-score)
3333                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3334                       gnus-summary-zcore-fuzz))
3335               ?\ ;;;Whitespace
3336             (if (< gnus-tmp-score gnus-summary-default-score)
3337                 gnus-score-below-mark gnus-score-over-mark)))
3338          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3339          (gnus-tmp-replied
3340           (cond (gnus-tmp-process gnus-process-mark)
3341                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3342                  gnus-cached-mark)
3343                 (gnus-tmp-replied gnus-replied-mark)
3344                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3345                  gnus-forwarded-mark)
3346                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3347                  gnus-saved-mark)
3348                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3349                  gnus-recent-mark)
3350                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3351                  gnus-unseen-mark)
3352                 (t gnus-no-mark)))
3353          (gnus-tmp-downloaded
3354           (cond (undownloaded
3355                  gnus-undownloaded-mark)
3356                 (gnus-newsgroup-agentized
3357                  gnus-downloaded-mark)
3358                 (t
3359                  gnus-no-mark)))
3360          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3361          (gnus-tmp-name
3362           (cond
3363            ((string-match "<[^>]+> *$" gnus-tmp-from)
3364             (let ((beg (match-beginning 0)))
3365               (or (and (string-match "^\".+\"" gnus-tmp-from)
3366                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3367                   (substring gnus-tmp-from 0 beg))))
3368            ((string-match "(.+)" gnus-tmp-from)
3369             (substring gnus-tmp-from
3370                        (1+ (match-beginning 0)) (1- (match-end 0))))
3371            (t gnus-tmp-from)))
3372          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3373          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3374          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3375          (buffer-read-only nil))
3376     (when (string= gnus-tmp-name "")
3377       (setq gnus-tmp-name gnus-tmp-from))
3378     (unless (numberp gnus-tmp-lines)
3379       (setq gnus-tmp-lines -1))
3380     (if (= gnus-tmp-lines -1)
3381         (setq gnus-tmp-lines "?")
3382       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3383     (gnus-put-text-property-excluding-characters-with-faces
3384      (point)
3385      (progn (eval gnus-summary-line-format-spec) (point))
3386      'gnus-number gnus-tmp-number)
3387     (when (gnus-visual-p 'summary-highlight 'highlight)
3388       (forward-line -1)
3389       (gnus-run-hooks 'gnus-summary-update-hook)
3390       (forward-line 1))))
3391
3392 (defun gnus-summary-update-line (&optional dont-update)
3393   "Update summary line after change."
3394   (when (and gnus-summary-default-score
3395              (not gnus-summary-inhibit-highlight))
3396     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3397            (article (gnus-summary-article-number))
3398            (score (gnus-summary-article-score article)))
3399       (unless dont-update
3400         (if (and gnus-summary-mark-below
3401                  (< (gnus-summary-article-score)
3402                     gnus-summary-mark-below))
3403             ;; This article has a low score, so we mark it as read.
3404             (when (memq article gnus-newsgroup-unreads)
3405               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3406           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3407             ;; This article was previously marked as read on account
3408             ;; of a low score, but now it has risen, so we mark it as
3409             ;; unread.
3410             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3411         (gnus-summary-update-mark
3412          (if (or (null gnus-summary-default-score)
3413                  (<= (abs (- score gnus-summary-default-score))
3414                      gnus-summary-zcore-fuzz))
3415              ?\ ;;;Whitespace
3416            (if (< score gnus-summary-default-score)
3417                gnus-score-below-mark gnus-score-over-mark))
3418          'score))
3419       ;; Do visual highlighting.
3420       (when (gnus-visual-p 'summary-highlight 'highlight)
3421         (gnus-run-hooks 'gnus-summary-update-hook)))))
3422
3423 (defvar gnus-tmp-new-adopts nil)
3424
3425 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3426   "Return the number of articles in THREAD.
3427 This may be 0 in some cases -- if none of the articles in
3428 the thread are to be displayed."
3429   (let* ((number
3430           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3431           (cond
3432            ((not (listp thread))
3433             1)
3434            ((and (consp thread) (cdr thread))
3435             (apply
3436              '+ 1 (mapcar
3437                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3438            ((null thread)
3439             1)
3440            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3441             1)
3442            (t 0))))
3443     (when (and level (zerop level) gnus-tmp-new-adopts)
3444       (incf number
3445             (apply '+ (mapcar
3446                        'gnus-summary-number-of-articles-in-thread
3447                        gnus-tmp-new-adopts))))
3448     (if char
3449         (if (> number 1) gnus-not-empty-thread-mark
3450           gnus-empty-thread-mark)
3451       number)))
3452
3453 (defsubst gnus-summary-line-message-size (head)
3454   "Return pretty-printed version of message size.
3455 This function is intended to be used in
3456 `gnus-summary-line-format-alist'."
3457   (let ((c (or (mail-header-chars head) -1)))
3458     (cond ((< c 0) "n/a")               ; chars not available
3459           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3460           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3461           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3462           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3463
3464
3465 (defun gnus-summary-set-local-parameters (group)
3466   "Go through the local params of GROUP and set all variable specs in that list."
3467   (let ((params (gnus-group-find-parameter group))
3468         (vars '(quit-config))           ; Ignore quit-config.
3469         elem)
3470     (while params
3471       (setq elem (car params)
3472             params (cdr params))
3473       (and (consp elem)                 ; Has to be a cons.
3474            (consp (cdr elem))           ; The cdr has to be a list.
3475            (symbolp (car elem))         ; Has to be a symbol in there.
3476            (not (memq (car elem) vars))
3477            (ignore-errors               ; So we set it.
3478              (push (car elem) vars)
3479              (make-local-variable (car elem))
3480              (set (car elem) (eval (nth 1 elem))))))))
3481
3482 (defun gnus-summary-read-group (group &optional show-all no-article
3483                                       kill-buffer no-display backward
3484                                       select-articles)
3485   "Start reading news in newsgroup GROUP.
3486 If SHOW-ALL is non-nil, already read articles are also listed.
3487 If NO-ARTICLE is non-nil, no article is selected initially.
3488 If NO-DISPLAY, don't generate a summary buffer."
3489   (let (result)
3490     (while (and group
3491                 (null (setq result
3492                             (let ((gnus-auto-select-next nil))
3493                               (or (gnus-summary-read-group-1
3494                                    group show-all no-article
3495                                    kill-buffer no-display
3496                                    select-articles)
3497                                   (setq show-all nil
3498                                         select-articles nil)))))
3499                 (eq gnus-auto-select-next 'quietly))
3500       (set-buffer gnus-group-buffer)
3501       ;; The entry function called above goes to the next
3502       ;; group automatically, so we go two groups back
3503       ;; if we are searching for the previous group.
3504       (when backward
3505         (gnus-group-prev-unread-group 2))
3506       (if (not (equal group (gnus-group-group-name)))
3507           (setq group (gnus-group-group-name))
3508         (setq group nil)))
3509     result))
3510
3511 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3512   "Directly jump to the other GROUP from summary buffer.
3513 If SHOW-ALL is non-nil, already read articles are also listed."
3514   (interactive
3515    (if (eq gnus-summary-buffer (current-buffer))
3516        (list (completing-read
3517               "Group: " gnus-active-hashtb nil t
3518               (when (and gnus-newsgroup-name
3519                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3520                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3521               'gnus-group-history)
3522              current-prefix-arg)
3523      (error "%s must be invoked from a gnus summary buffer." this-command)))
3524   (unless (or (zerop (length group))
3525               (and gnus-newsgroup-name
3526                    (string-equal gnus-newsgroup-name group)))
3527     (gnus-summary-exit)
3528     (gnus-summary-read-group group show-all
3529                              gnus-dont-select-after-jump-to-other-group)))
3530
3531 (defun gnus-summary-read-group-1 (group show-all no-article
3532                                         kill-buffer no-display
3533                                         &optional select-articles)
3534   ;; Killed foreign groups can't be entered.
3535   ;;  (when (and (not (gnus-group-native-p group))
3536   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3537   ;;    (error "Dead non-native groups can't be entered"))
3538   (gnus-message 5 "Retrieving newsgroup: %s..."
3539                 (gnus-group-decoded-name group))
3540   (let* ((new-group (gnus-summary-setup-buffer group))
3541          (quit-config (gnus-group-quit-config group))
3542          (did-select (and new-group (gnus-select-newsgroup
3543                                      group show-all select-articles))))
3544     (cond
3545      ;; This summary buffer exists already, so we just select it.
3546      ((not new-group)
3547       (gnus-set-global-variables)
3548       (when kill-buffer
3549         (gnus-kill-or-deaden-summary kill-buffer))
3550       (gnus-configure-windows 'summary 'force)
3551       (gnus-set-mode-line 'summary)
3552       (gnus-summary-position-point)
3553       (message "")
3554       t)
3555      ;; We couldn't select this group.
3556      ((null did-select)
3557       (when (and (eq major-mode 'gnus-summary-mode)
3558                  (not (equal (current-buffer) kill-buffer)))
3559         (kill-buffer (current-buffer))
3560         (if (not quit-config)
3561             (progn
3562               ;; Update the info -- marks might need to be removed,
3563               ;; for instance.
3564               (gnus-summary-update-info)
3565               (set-buffer gnus-group-buffer)
3566               (gnus-group-jump-to-group group)
3567               (gnus-group-next-unread-group 1))
3568           (gnus-handle-ephemeral-exit quit-config)))
3569       (let ((grpinfo (gnus-get-info group)))
3570         (if (null (gnus-info-read grpinfo))
3571             (gnus-message 3 "Group %s contains no messages"
3572                           (gnus-group-decoded-name group))
3573           (gnus-message 3 "Can't select group")))
3574       nil)
3575      ;; The user did a `C-g' while prompting for number of articles,
3576      ;; so we exit this group.
3577      ((eq did-select 'quit)
3578       (and (eq major-mode 'gnus-summary-mode)
3579            (not (equal (current-buffer) kill-buffer))
3580            (kill-buffer (current-buffer)))
3581       (when kill-buffer
3582         (gnus-kill-or-deaden-summary kill-buffer))
3583       (if (not quit-config)
3584           (progn
3585             (set-buffer gnus-group-buffer)
3586             (gnus-group-jump-to-group group)
3587             (gnus-group-next-unread-group 1)
3588             (gnus-configure-windows 'group 'force))
3589         (gnus-handle-ephemeral-exit quit-config))
3590       ;; Finally signal the quit.
3591       (signal 'quit nil))
3592      ;; The group was successfully selected.
3593      (t
3594       (gnus-set-global-variables)
3595       ;; Save the active value in effect when the group was entered.
3596       (setq gnus-newsgroup-active
3597             (gnus-copy-sequence
3598              (gnus-active gnus-newsgroup-name)))
3599       ;; You can change the summary buffer in some way with this hook.
3600       (gnus-run-hooks 'gnus-select-group-hook)
3601       (gnus-update-format-specifications
3602        nil 'summary 'summary-mode 'summary-dummy)
3603       (gnus-update-summary-mark-positions)
3604       ;; Do score processing.
3605       (when gnus-use-scoring
3606         (gnus-possibly-score-headers))
3607       ;; Check whether to fill in the gaps in the threads.
3608       (when gnus-build-sparse-threads
3609         (gnus-build-sparse-threads))
3610       ;; Find the initial limit.
3611       (if show-all
3612           (let ((gnus-newsgroup-dormant nil))
3613             (gnus-summary-initial-limit show-all))
3614         (gnus-summary-initial-limit show-all))
3615       ;; Generate the summary buffer.
3616       (unless no-display
3617         (gnus-summary-prepare))
3618       (when gnus-use-trees
3619         (gnus-tree-open group)
3620         (setq gnus-summary-highlight-line-function
3621               'gnus-tree-highlight-article))
3622       ;; If the summary buffer is empty, but there are some low-scored
3623       ;; articles or some excluded dormants, we include these in the
3624       ;; buffer.
3625       (when (and (zerop (buffer-size))
3626                  (not no-display))
3627         (cond (gnus-newsgroup-dormant
3628                (gnus-summary-limit-include-dormant))
3629               ((and gnus-newsgroup-scored show-all)
3630                (gnus-summary-limit-include-expunged t))))
3631       ;; Function `gnus-apply-kill-file' must be called in this hook.
3632       (gnus-run-hooks 'gnus-apply-kill-hook)
3633       (if (and (zerop (buffer-size))
3634                (not no-display))
3635           (progn
3636             ;; This newsgroup is empty.
3637             (gnus-summary-catchup-and-exit nil t)
3638             (gnus-message 6 "No unread news")
3639             (when kill-buffer
3640               (gnus-kill-or-deaden-summary kill-buffer))
3641             ;; Return nil from this function.
3642             nil)
3643         ;; Hide conversation thread subtrees.  We cannot do this in
3644         ;; gnus-summary-prepare-hook since kill processing may not
3645         ;; work with hidden articles.
3646         (gnus-summary-maybe-hide-threads)
3647         (when kill-buffer
3648           (gnus-kill-or-deaden-summary kill-buffer))
3649         (gnus-summary-auto-select-subject)
3650         ;; Show first unread article if requested.
3651         (if (and (not no-article)
3652                  (not no-display)
3653                  gnus-newsgroup-unreads
3654                  gnus-auto-select-first)
3655             (progn
3656               (gnus-configure-windows 'summary)
3657               (let ((art (gnus-summary-article-number)))
3658                 (unless (and (not gnus-plugged)
3659                              (or (memq art gnus-newsgroup-undownloaded)
3660                                  (memq art gnus-newsgroup-downloadable)))
3661                   (gnus-summary-goto-article art))))
3662           ;; Don't select any articles.
3663           (gnus-summary-position-point)
3664           (gnus-configure-windows 'summary 'force)
3665           (gnus-set-mode-line 'summary))
3666         (when (and gnus-auto-center-group
3667                    (get-buffer-window gnus-group-buffer t))
3668           ;; Gotta use windows, because recenter does weird stuff if
3669           ;; the current buffer ain't the displayed window.
3670           (let ((owin (selected-window)))
3671             (select-window (get-buffer-window gnus-group-buffer t))
3672             (when (gnus-group-goto-group group)
3673               (recenter))
3674             (select-window owin)))
3675         ;; Mark this buffer as "prepared".
3676         (setq gnus-newsgroup-prepared t)
3677         (gnus-run-hooks 'gnus-summary-prepared-hook)
3678         (unless (gnus-ephemeral-group-p group)
3679           (gnus-group-update-group group))
3680         t)))))
3681
3682 (defun gnus-summary-auto-select-subject ()
3683   "Select the subject line on initial group entry."
3684   (goto-char (point-min))
3685   (cond
3686    ((eq gnus-auto-select-subject 'best)
3687     (gnus-summary-best-unread-subject))
3688    ((eq gnus-auto-select-subject 'unread)
3689     (gnus-summary-first-unread-subject))
3690    ((eq gnus-auto-select-subject 'unseen)
3691     (gnus-summary-first-unseen-subject))
3692    ((eq gnus-auto-select-subject 'unseen-or-unread)
3693     (gnus-summary-first-unseen-or-unread-subject))
3694    ((eq gnus-auto-select-subject 'first)
3695     ;; Do nothing.
3696     )
3697    ((functionp gnus-auto-select-subject)
3698     (funcall gnus-auto-select-subject))))
3699
3700 (defun gnus-summary-prepare ()
3701   "Generate the summary buffer."
3702   (interactive)
3703   (let ((buffer-read-only nil))
3704     (erase-buffer)
3705     (setq gnus-newsgroup-data nil
3706           gnus-newsgroup-data-reverse nil)
3707     (gnus-run-hooks 'gnus-summary-generate-hook)
3708     ;; Generate the buffer, either with threads or without.
3709     (when gnus-newsgroup-headers
3710       (gnus-summary-prepare-threads
3711        (if gnus-show-threads
3712            (gnus-sort-gathered-threads
3713             (funcall gnus-summary-thread-gathering-function
3714                      (gnus-sort-threads
3715                       (gnus-cut-threads (gnus-make-threads)))))
3716          ;; Unthreaded display.
3717          (gnus-sort-articles gnus-newsgroup-headers))))
3718     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3719     ;; Call hooks for modifying summary buffer.
3720     (goto-char (point-min))
3721     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3722
3723 (defsubst gnus-general-simplify-subject (subject)
3724   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3725   (setq subject
3726         (cond
3727          ;; Truncate the subject.
3728          (gnus-simplify-subject-functions
3729           (gnus-map-function gnus-simplify-subject-functions subject))
3730          ((numberp gnus-summary-gather-subject-limit)
3731           (setq subject (gnus-simplify-subject-re subject))
3732           (if (> (length subject) gnus-summary-gather-subject-limit)
3733               (substring subject 0 gnus-summary-gather-subject-limit)
3734             subject))
3735          ;; Fuzzily simplify it.
3736          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3737           (gnus-simplify-subject-fuzzy subject))
3738          ;; Just remove the leading "Re:".
3739          (t
3740           (gnus-simplify-subject-re subject))))
3741
3742   (if (and gnus-summary-gather-exclude-subject
3743            (string-match gnus-summary-gather-exclude-subject subject))
3744       nil                               ; This article shouldn't be gathered
3745     subject))
3746
3747 (defun gnus-summary-simplify-subject-query ()
3748   "Query where the respool algorithm would put this article."
3749   (interactive)
3750   (gnus-summary-select-article)
3751   (message "%s"
3752            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3753
3754 (defun gnus-gather-threads-by-subject (threads)
3755   "Gather threads by looking at Subject headers."
3756   (if (not gnus-summary-make-false-root)
3757       threads
3758     (let ((hashtb (gnus-make-hashtable 1024))
3759           (prev threads)
3760           (result threads)
3761           subject hthread whole-subject)
3762       (while threads
3763         (setq subject (gnus-general-simplify-subject
3764                        (setq whole-subject (mail-header-subject
3765                                             (caar threads)))))
3766         (when subject
3767           (if (setq hthread (gnus-gethash subject hashtb))
3768               (progn
3769                 ;; We enter a dummy root into the thread, if we
3770                 ;; haven't done that already.
3771                 (unless (stringp (caar hthread))
3772                   (setcar hthread (list whole-subject (car hthread))))
3773                 ;; We add this new gathered thread to this gathered
3774                 ;; thread.
3775                 (setcdr (car hthread)
3776                         (nconc (cdar hthread) (list (car threads))))
3777                 ;; Remove it from the list of threads.
3778                 (setcdr prev (cdr threads))
3779                 (setq threads prev))
3780             ;; Enter this thread into the hash table.
3781             (gnus-sethash subject
3782                           (if gnus-summary-make-false-root-always
3783                               (progn
3784                                 ;; If you want a dummy root above all
3785                                 ;; threads...
3786                                 (setcar threads (list whole-subject
3787                                                       (car threads)))
3788                                 threads)
3789                             threads)
3790                           hashtb)))
3791         (setq prev threads)
3792         (setq threads (cdr threads)))
3793       result)))
3794
3795 (defun gnus-gather-threads-by-references (threads)
3796   "Gather threads by looking at References headers."
3797   (let ((idhashtb (gnus-make-hashtable 1024))
3798         (thhashtb (gnus-make-hashtable 1024))
3799         (prev threads)
3800         (result threads)
3801         ids references id gthread gid entered ref)
3802     (while threads
3803       (when (setq references (mail-header-references (caar threads)))
3804         (setq id (mail-header-id (caar threads))
3805               ids (inline (gnus-split-references references))
3806               entered nil)
3807         (while (setq ref (pop ids))
3808           (setq ids (delete ref ids))
3809           (if (not (setq gid (gnus-gethash ref idhashtb)))
3810               (progn
3811                 (gnus-sethash ref id idhashtb)
3812                 (gnus-sethash id threads thhashtb))
3813             (setq gthread (gnus-gethash gid thhashtb))
3814             (unless entered
3815               ;; We enter a dummy root into the thread, if we
3816               ;; haven't done that already.
3817               (unless (stringp (caar gthread))
3818                 (setcar gthread (list (mail-header-subject (caar gthread))
3819                                       (car gthread))))
3820               ;; We add this new gathered thread to this gathered
3821               ;; thread.
3822               (setcdr (car gthread)
3823                       (nconc (cdar gthread) (list (car threads)))))
3824             ;; Add it into the thread hash table.
3825             (gnus-sethash id gthread thhashtb)
3826             (setq entered t)
3827             ;; Remove it from the list of threads.
3828             (setcdr prev (cdr threads))
3829             (setq threads prev))))
3830       (setq prev threads)
3831       (setq threads (cdr threads)))
3832     result))
3833
3834 (defun gnus-sort-gathered-threads (threads)
3835   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3836   (let ((result threads))
3837     (while threads
3838       (when (stringp (caar threads))
3839         (setcdr (car threads)
3840                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3841       (setq threads (cdr threads)))
3842     result))
3843
3844 (defun gnus-thread-loop-p (root thread)
3845   "Say whether ROOT is in THREAD."
3846   (let ((stack (list thread))
3847         (infloop 0)
3848         th)
3849     (while (setq thread (pop stack))
3850       (setq th (cdr thread))
3851       (while (and th
3852                   (not (eq (caar th) root)))
3853         (pop th))
3854       (if th
3855           ;; We have found a loop.
3856           (let (ref-dep)
3857             (setcdr thread (delq (car th) (cdr thread)))
3858             (if (boundp (setq ref-dep (intern "none"
3859                                               gnus-newsgroup-dependencies)))
3860                 (setcdr (symbol-value ref-dep)
3861                         (nconc (cdr (symbol-value ref-dep))
3862                                (list (car th))))
3863               (set ref-dep (list nil (car th))))
3864             (setq infloop 1
3865                   stack nil))
3866         ;; Push all the subthreads onto the stack.
3867         (push (cdr thread) stack)))
3868     infloop))
3869
3870 (defun gnus-make-threads ()
3871   "Go through the dependency hashtb and find the roots.  Return all threads."
3872   (let (threads)
3873     (while (catch 'infloop
3874              (mapatoms
3875               (lambda (refs)
3876                 ;; Deal with self-referencing References loops.
3877                 (when (and (car (symbol-value refs))
3878                            (not (zerop
3879                                  (apply
3880                                   '+
3881                                   (mapcar
3882                                    (lambda (thread)
3883                                      (gnus-thread-loop-p
3884                                       (car (symbol-value refs)) thread))
3885                                    (cdr (symbol-value refs)))))))
3886                   (setq threads nil)
3887                   (throw 'infloop t))
3888                 (unless (car (symbol-value refs))
3889                   ;; These threads do not refer back to any other articles,
3890                   ;; so they're roots.
3891                   (setq threads (append (cdr (symbol-value refs)) threads))))
3892               gnus-newsgroup-dependencies)))
3893     threads))
3894
3895 ;; Build the thread tree.
3896 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3897   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3898
3899 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3900 if it was already present.
3901
3902 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3903 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3904 Message-IDs will be renamed to a unique Message-ID before being
3905 entered.
3906
3907 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3908   (let* ((id (mail-header-id header))
3909          (id-dep (and id (intern id dependencies)))
3910          parent-id ref ref-dep ref-header replaced)
3911     ;; Enter this `header' in the `dependencies' table.
3912     (cond
3913      ((not id-dep)
3914       (setq header nil))
3915      ;; The first two cases do the normal part: enter a new `header'
3916      ;; in the `dependencies' table.
3917      ((not (boundp id-dep))
3918       (set id-dep (list header)))
3919      ((null (car (symbol-value id-dep)))
3920       (setcar (symbol-value id-dep) header))
3921
3922      ;; From here the `header' was already present in the
3923      ;; `dependencies' table.
3924      (force-new
3925       ;; Overrides an existing entry;
3926       ;; just set the header part of the entry.
3927       (setcar (symbol-value id-dep) header)
3928       (setq replaced t))
3929
3930      ;; Renames the existing `header' to a unique Message-ID.
3931      ((not gnus-summary-ignore-duplicates)
3932       ;; An article with this Message-ID has already been seen.
3933       ;; We rename the Message-ID.
3934       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3935            (list header))
3936       (mail-header-set-id header id))
3937
3938      ;; The last case ignores an existing entry, except it adds any
3939      ;; additional Xrefs (in case the two articles came from different
3940      ;; servers.
3941      ;; Also sets `header' to `nil' meaning that the `dependencies'
3942      ;; table was *not* modified.
3943      (t
3944       (mail-header-set-xref
3945        (car (symbol-value id-dep))
3946        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3947                    "")
3948                (or (mail-header-xref header) "")))
3949       (setq header nil)))
3950
3951     (when (and header (not replaced))
3952       ;; First check that we are not creating a References loop.
3953       (setq parent-id (gnus-parent-id (mail-header-references header)))
3954       (setq ref parent-id)
3955       (while (and ref
3956                   (setq ref-dep (intern-soft ref dependencies))
3957                   (boundp ref-dep)
3958                   (setq ref-header (car (symbol-value ref-dep))))
3959         (if (string= id ref)
3960             ;; Yuk!  This is a reference loop.  Make the article be a
3961             ;; root article.
3962             (progn
3963               (mail-header-set-references (car (symbol-value id-dep)) "none")
3964               (setq ref nil)
3965               (setq parent-id nil))
3966           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3967       (setq ref-dep (intern (or parent-id "none") dependencies))
3968       (if (boundp ref-dep)
3969           (setcdr (symbol-value ref-dep)
3970                   (nconc (cdr (symbol-value ref-dep))
3971                          (list (symbol-value id-dep))))
3972         (set ref-dep (list nil (symbol-value id-dep)))))
3973     header))
3974
3975 (defun gnus-extract-message-id-from-in-reply-to (string)
3976   (if (string-match "<[^>]+>" string)
3977       (substring string (match-beginning 0) (match-end 0))
3978     nil))
3979
3980 (defun gnus-build-sparse-threads ()
3981   (let ((headers gnus-newsgroup-headers)
3982         (mail-parse-charset gnus-newsgroup-charset)
3983         (gnus-summary-ignore-duplicates t)
3984         header references generation relations
3985         subject child end new-child date)
3986     ;; First we create an alist of generations/relations, where
3987     ;; generations is how much we trust the relation, and the relation
3988     ;; is parent/child.
3989     (gnus-message 7 "Making sparse threads...")
3990     (save-excursion
3991       (nnheader-set-temp-buffer " *gnus sparse threads*")
3992       (while (setq header (pop headers))
3993         (when (and (setq references (mail-header-references header))
3994                    (not (string= references "")))
3995           (insert references)
3996           (setq child (mail-header-id header)
3997                 subject (mail-header-subject header)
3998                 date (mail-header-date header)
3999                 generation 0)
4000           (while (search-backward ">" nil t)
4001             (setq end (1+ (point)))
4002             (when (search-backward "<" nil t)
4003               (setq new-child (buffer-substring (point) end))
4004               (push (list (incf generation)
4005                           child (setq child new-child)
4006                           subject date)
4007                     relations)))
4008           (when child
4009             (push (list (1+ generation) child nil subject) relations))
4010           (erase-buffer)))
4011       (kill-buffer (current-buffer)))
4012     ;; Sort over trustworthiness.
4013     (mapcar
4014      (lambda (relation)
4015        (when (gnus-dependencies-add-header
4016               (make-full-mail-header-from-decoded-header
4017                gnus-reffed-article-number
4018                (nth 3 relation) "" (or (nth 4 relation) "")
4019                (nth 1 relation)
4020                (or (nth 2 relation) "") 0 0 "")
4021               gnus-newsgroup-dependencies nil)
4022          (push gnus-reffed-article-number gnus-newsgroup-limit)
4023          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4024          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4025                gnus-newsgroup-reads)
4026          (decf gnus-reffed-article-number)))
4027      (sort relations 'car-less-than-car))
4028     (gnus-message 7 "Making sparse threads...done")))
4029
4030 (defun gnus-build-old-threads ()
4031   ;; Look at all the articles that refer back to old articles, and
4032   ;; fetch the headers for the articles that aren't there.  This will
4033   ;; build complete threads - if the roots haven't been expired by the
4034   ;; server, that is.
4035   (let ((mail-parse-charset gnus-newsgroup-charset)
4036         id heads)
4037     (mapatoms
4038      (lambda (refs)
4039        (when (not (car (symbol-value refs)))
4040          (setq heads (cdr (symbol-value refs)))
4041          (while heads
4042            (if (memq (mail-header-number (caar heads))
4043                      gnus-newsgroup-dormant)
4044                (setq heads (cdr heads))
4045              (setq id (symbol-name refs))
4046              (while (and (setq id (gnus-build-get-header id))
4047                          (not (car (gnus-id-to-thread id)))))
4048              (setq heads nil)))))
4049      gnus-newsgroup-dependencies)))
4050
4051 (defsubst gnus-remove-odd-characters (string)
4052   "Translate STRING into something that doesn't contain weird characters."
4053   (mm-subst-char-in-string
4054    ?\r ?\-
4055    (mm-subst-char-in-string ?\n ?\- string t) t))
4056
4057 ;; This function has to be called with point after the article number
4058 ;; on the beginning of the line.
4059 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4060   (let ((eol (point-at-eol))
4061         (buffer (current-buffer))
4062         header references in-reply-to)
4063
4064     ;; overview: [num subject from date id refs chars lines misc]
4065     (unwind-protect
4066         (progn
4067           (narrow-to-region (point) eol)
4068           (unless (eobp)
4069             (forward-char))
4070
4071           (setq header
4072                 (make-full-mail-header
4073                  number                         ; number
4074                  (nnheader-nov-field)           ; subject
4075                  (nnheader-nov-field)           ; from
4076                  (nnheader-nov-field)           ; date
4077                  (nnheader-nov-read-message-id number)  ; id
4078                  (setq references (nnheader-nov-field)) ; refs
4079                  (nnheader-nov-read-integer)    ; chars
4080                  (nnheader-nov-read-integer)    ; lines
4081                  (unless (eobp)
4082                    (if (looking-at "Xref: ")
4083                        (goto-char (match-end 0)))
4084                    (nnheader-nov-field))        ; Xref
4085                  (nnheader-nov-parse-extra))))  ; extra
4086
4087       (widen))
4088
4089     (when (and (string= references "")
4090                (setq in-reply-to (mail-header-extra header))
4091                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4092       (mail-header-set-references
4093        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4094
4095     (when gnus-alter-header-function
4096       (funcall gnus-alter-header-function header))
4097     (gnus-dependencies-add-header header dependencies force-new)))
4098
4099 (defun gnus-build-get-header (id)
4100   "Look through the buffer of NOV lines and find the header to ID.
4101 Enter this line into the dependencies hash table, and return
4102 the id of the parent article (if any)."
4103   (let ((deps gnus-newsgroup-dependencies)
4104         found header)
4105     (prog1
4106         (save-excursion
4107           (set-buffer nntp-server-buffer)
4108           (let ((case-fold-search nil))
4109             (goto-char (point-min))
4110             (while (and (not found)
4111                         (search-forward id nil t))
4112               (beginning-of-line)
4113               (setq found (looking-at
4114                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4115                                    (regexp-quote id))))
4116               (or found (beginning-of-line 2)))
4117             (when found
4118               (beginning-of-line)
4119               (and
4120                (setq header (gnus-nov-parse-line
4121                              (read (current-buffer)) deps))
4122                (gnus-parent-id (mail-header-references header))))))
4123       (when header
4124         (let ((number (mail-header-number header)))
4125           (push number gnus-newsgroup-limit)
4126           (push header gnus-newsgroup-headers)
4127           (if (memq number gnus-newsgroup-unselected)
4128               (progn
4129                 (setq gnus-newsgroup-unreads
4130                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4131                                                number))
4132                 (setq gnus-newsgroup-unselected
4133                       (delq number gnus-newsgroup-unselected)))
4134             (push number gnus-newsgroup-ancient)))))))
4135
4136 (defun gnus-build-all-threads ()
4137   "Read all the headers."
4138   (let ((gnus-summary-ignore-duplicates t)
4139         (mail-parse-charset gnus-newsgroup-charset)
4140         (dependencies gnus-newsgroup-dependencies)
4141         header article)
4142     (save-excursion
4143       (set-buffer nntp-server-buffer)
4144       (let ((case-fold-search nil))
4145         (goto-char (point-min))
4146         (while (not (eobp))
4147           (ignore-errors
4148             (setq article (read (current-buffer))
4149                   header (gnus-nov-parse-line article dependencies)))
4150           (when header
4151             (with-current-buffer gnus-summary-buffer
4152               (push header gnus-newsgroup-headers)
4153               (if (memq (setq article (mail-header-number header))
4154                         gnus-newsgroup-unselected)
4155                   (progn
4156                     (setq gnus-newsgroup-unreads
4157                           (gnus-add-to-sorted-list
4158                            gnus-newsgroup-unreads article))
4159                     (setq gnus-newsgroup-unselected
4160                           (delq article gnus-newsgroup-unselected)))
4161                 (push article gnus-newsgroup-ancient)))
4162             (forward-line 1)))))))
4163
4164 (defun gnus-summary-update-article-line (article header)
4165   "Update the line for ARTICLE using HEADER."
4166   (let* ((id (mail-header-id header))
4167          (thread (gnus-id-to-thread id)))
4168     (unless thread
4169       (error "Article in no thread"))
4170     ;; Update the thread.
4171     (setcar thread header)
4172     (gnus-summary-goto-subject article)
4173     (let* ((datal (gnus-data-find-list article))
4174            (data (car datal))
4175            (buffer-read-only nil)
4176            (level (gnus-summary-thread-level)))
4177       (gnus-delete-line)
4178       (let ((inserted (- (point)
4179                          (progn
4180                            (gnus-summary-insert-line
4181                             header level nil
4182                             (memq article gnus-newsgroup-undownloaded)
4183                             (gnus-article-mark article)
4184                             (memq article gnus-newsgroup-replied)
4185                             (memq article gnus-newsgroup-expirable)
4186                             ;; Only insert the Subject string when it's different
4187                             ;; from the previous Subject string.
4188                             (if (and
4189                                  gnus-show-threads
4190                                  (gnus-subject-equal
4191                                   (condition-case ()
4192                                       (mail-header-subject
4193                                        (gnus-data-header
4194                                         (cadr
4195                                          (gnus-data-find-list
4196                                           article
4197                                           (gnus-data-list t)))))
4198                                     ;; Error on the side of excessive subjects.
4199                                     (error ""))
4200                                   (mail-header-subject header)))
4201                                 ""
4202                               (mail-header-subject header))
4203                             nil (cdr (assq article gnus-newsgroup-scored))
4204                             (memq article gnus-newsgroup-processable))
4205                            (point)))))
4206         (when (cdr datal)
4207           (gnus-data-update-list
4208            (cdr datal)
4209            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4210
4211 (defun gnus-summary-update-article (article &optional iheader)
4212   "Update ARTICLE in the summary buffer."
4213   (set-buffer gnus-summary-buffer)
4214   (let* ((header (gnus-summary-article-header article))
4215          (id (mail-header-id header))
4216          (data (gnus-data-find article))
4217          (thread (gnus-id-to-thread id))
4218          (references (mail-header-references header))
4219          (parent
4220           (gnus-id-to-thread
4221            (or (gnus-parent-id
4222                 (when (and references
4223                            (not (equal "" references)))
4224                   references))
4225                "none")))
4226          (buffer-read-only nil)
4227          (old (car thread)))
4228     (when thread
4229       (unless iheader
4230         (setcar thread nil)
4231         (when parent
4232           (delq thread parent)))
4233       (if (gnus-summary-insert-subject id header)
4234           ;; Set the (possibly) new article number in the data structure.
4235           (gnus-data-set-number data (gnus-id-to-article id))
4236         (setcar thread old)
4237         nil))))
4238
4239 (defun gnus-rebuild-thread (id &optional line)
4240   "Rebuild the thread containing ID.
4241 If LINE, insert the rebuilt thread starting on line LINE."
4242   (let ((buffer-read-only nil)
4243         old-pos current thread data)
4244     (if (not gnus-show-threads)
4245         (setq thread (list (car (gnus-id-to-thread id))))
4246       ;; Get the thread this article is part of.
4247       (setq thread (gnus-remove-thread id)))
4248     (setq old-pos (point-at-bol))
4249     (setq current (save-excursion
4250                     (and (re-search-backward "[\r\n]" nil t)
4251                          (gnus-summary-article-number))))
4252     ;; If this is a gathered thread, we have to go some re-gathering.
4253     (when (stringp (car thread))
4254       (let ((subject (car thread))
4255             roots thr)
4256         (setq thread (cdr thread))
4257         (while thread
4258           (unless (memq (setq thr (gnus-id-to-thread
4259                                    (gnus-root-id
4260                                     (mail-header-id (caar thread)))))
4261                         roots)
4262             (push thr roots))
4263           (setq thread (cdr thread)))
4264         ;; We now have all (unique) roots.
4265         (if (= (length roots) 1)
4266             ;; All the loose roots are now one solid root.
4267             (setq thread (car roots))
4268           (setq thread (cons subject (gnus-sort-threads roots))))))
4269     (let (threads)
4270       ;; We then insert this thread into the summary buffer.
4271       (when line
4272         (goto-char (point-min))
4273         (forward-line (1- line)))
4274       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4275         (if gnus-show-threads
4276             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4277           (gnus-summary-prepare-unthreaded thread))
4278         (setq data (nreverse gnus-newsgroup-data))
4279         (setq threads gnus-newsgroup-threads))
4280       ;; We splice the new data into the data structure.
4281       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4282       ;;!!! then we want to insert at the beginning of the buffer.
4283       ;;!!! That happens to be true with Gnus now, but that may
4284       ;;!!! change in the future.  Perhaps.
4285       (gnus-data-enter-list
4286        (if line nil current) data (- (point) old-pos))
4287       (setq gnus-newsgroup-threads
4288             (nconc threads gnus-newsgroup-threads))
4289       (gnus-data-compute-positions))))
4290
4291 (defun gnus-number-to-header (number)
4292   "Return the header for article NUMBER."
4293   (let ((headers gnus-newsgroup-headers))
4294     (while (and headers
4295                 (not (= number (mail-header-number (car headers)))))
4296       (pop headers))
4297     (when headers
4298       (car headers))))
4299
4300 (defun gnus-parent-headers (in-headers &optional generation)
4301   "Return the headers of the GENERATIONeth parent of HEADERS."
4302   (unless generation
4303     (setq generation 1))
4304   (let ((parent t)
4305         (headers in-headers)
4306         references)
4307     (while (and parent
4308                 (not (zerop generation))
4309                 (setq references (mail-header-references headers)))
4310       (setq headers (if (and references
4311                              (setq parent (gnus-parent-id references)))
4312                         (car (gnus-id-to-thread parent))
4313                       nil))
4314       (decf generation))
4315     (and (not (eq headers in-headers))
4316          headers)))
4317
4318 (defun gnus-id-to-thread (id)
4319   "Return the (sub-)thread where ID appears."
4320   (gnus-gethash id gnus-newsgroup-dependencies))
4321
4322 (defun gnus-id-to-article (id)
4323   "Return the article number of ID."
4324   (let ((thread (gnus-id-to-thread id)))
4325     (when (and thread
4326                (car thread))
4327       (mail-header-number (car thread)))))
4328
4329 (defun gnus-id-to-header (id)
4330   "Return the article headers of ID."
4331   (car (gnus-id-to-thread id)))
4332
4333 (defun gnus-article-displayed-root-p (article)
4334   "Say whether ARTICLE is a root(ish) article."
4335   (let ((level (gnus-summary-thread-level article))
4336         (refs (mail-header-references  (gnus-summary-article-header article)))
4337         particle)
4338     (cond
4339      ((null level) nil)
4340      ((zerop level) t)
4341      ((null refs) t)
4342      ((null (gnus-parent-id refs)) t)
4343      ((and (= 1 level)
4344            (null (setq particle (gnus-id-to-article
4345                                  (gnus-parent-id refs))))
4346            (null (gnus-summary-thread-level particle)))))))
4347
4348 (defun gnus-root-id (id)
4349   "Return the id of the root of the thread where ID appears."
4350   (let (last-id prev)
4351     (while (and id (setq prev (car (gnus-id-to-thread id))))
4352       (setq last-id id
4353             id (gnus-parent-id (mail-header-references prev))))
4354     last-id))
4355
4356 (defun gnus-articles-in-thread (thread)
4357   "Return the list of articles in THREAD."
4358   (cons (mail-header-number (car thread))
4359         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4360
4361 (defun gnus-remove-thread (id &optional dont-remove)
4362   "Remove the thread that has ID in it."
4363   (let (headers thread last-id)
4364     ;; First go up in this thread until we find the root.
4365     (setq last-id (gnus-root-id id)
4366           headers (message-flatten-list (gnus-id-to-thread last-id)))
4367     ;; We have now found the real root of this thread.  It might have
4368     ;; been gathered into some loose thread, so we have to search
4369     ;; through the threads to find the thread we wanted.
4370     (let ((threads gnus-newsgroup-threads)
4371           sub)
4372       (while threads
4373         (setq sub (car threads))
4374         (if (stringp (car sub))
4375             ;; This is a gathered thread, so we look at the roots
4376             ;; below it to find whether this article is in this
4377             ;; gathered root.
4378             (progn
4379               (setq sub (cdr sub))
4380               (while sub
4381                 (when (member (caar sub) headers)
4382                   (setq thread (car threads)
4383                         threads nil
4384                         sub nil))
4385                 (setq sub (cdr sub))))
4386           ;; It's an ordinary thread, so we check it.
4387           (when (eq (car sub) (car headers))
4388             (setq thread sub
4389                   threads nil)))
4390         (setq threads (cdr threads)))
4391       ;; If this article is in no thread, then it's a root.
4392       (if thread
4393           (unless dont-remove
4394             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4395         (setq thread (gnus-id-to-thread last-id)))
4396       (when thread
4397         (prog1
4398             thread                      ; We return this thread.
4399           (unless dont-remove
4400             (if (stringp (car thread))
4401                 (progn
4402                   ;; If we use dummy roots, then we have to remove the
4403                   ;; dummy root as well.
4404                   (when (eq gnus-summary-make-false-root 'dummy)
4405                     ;; We go to the dummy root by going to
4406                     ;; the first sub-"thread", and then one line up.
4407                     (gnus-summary-goto-article
4408                      (mail-header-number (caadr thread)))
4409                     (forward-line -1)
4410                     (gnus-delete-line)
4411                     (gnus-data-compute-positions))
4412                   (setq thread (cdr thread))
4413                   (while thread
4414                     (gnus-remove-thread-1 (car thread))
4415                     (setq thread (cdr thread))))
4416               (gnus-remove-thread-1 thread))))))))
4417
4418 (defun gnus-remove-thread-1 (thread)
4419   "Remove the thread THREAD recursively."
4420   (let ((number (mail-header-number (pop thread)))
4421         d)
4422     (setq thread (reverse thread))
4423     (while thread
4424       (gnus-remove-thread-1 (pop thread)))
4425     (when (setq d (gnus-data-find number))
4426       (goto-char (gnus-data-pos d))
4427       (gnus-summary-show-thread)
4428       (gnus-data-remove
4429        number
4430        (- (point-at-bol)
4431           (prog1
4432               (1+ (point-at-eol))
4433             (gnus-delete-line)))))))
4434
4435 (defun gnus-sort-threads-1 (threads func)
4436   (sort (mapcar (lambda (thread)
4437                   (cons (car thread)
4438                         (and (cdr thread)
4439                              (gnus-sort-threads-1 (cdr thread) func))))
4440                 threads) func))
4441
4442 (defun gnus-sort-threads (threads)
4443   "Sort THREADS."
4444   (if (not gnus-thread-sort-functions)
4445       threads
4446     (gnus-message 8 "Sorting threads...")
4447     (let ((max-lisp-eval-depth 5000))
4448       (prog1 (gnus-sort-threads-1
4449          threads
4450          (gnus-make-sort-function gnus-thread-sort-functions))
4451         (gnus-message 8 "Sorting threads...done")))))
4452
4453 (defun gnus-sort-articles (articles)
4454   "Sort ARTICLES."
4455   (when gnus-article-sort-functions
4456     (gnus-message 7 "Sorting articles...")
4457     (prog1
4458         (setq gnus-newsgroup-headers
4459               (sort articles (gnus-make-sort-function
4460                               gnus-article-sort-functions)))
4461       (gnus-message 7 "Sorting articles...done"))))
4462
4463 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4464 (defmacro gnus-thread-header (thread)
4465   "Return header of first article in THREAD.
4466 Note that THREAD must never, ever be anything else than a variable -
4467 using some other form will lead to serious barfage."
4468   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4469   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4470   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4471         (vector thread) 2))
4472
4473 (defsubst gnus-article-sort-by-number (h1 h2)
4474   "Sort articles by article number."
4475   (< (mail-header-number h1)
4476      (mail-header-number h2)))
4477
4478 (defun gnus-thread-sort-by-number (h1 h2)
4479   "Sort threads by root article number."
4480   (gnus-article-sort-by-number
4481    (gnus-thread-header h1) (gnus-thread-header h2)))
4482
4483 (defsubst gnus-article-sort-by-random (h1 h2)
4484   "Sort articles by article number."
4485   (zerop (random 2)))
4486
4487 (defun gnus-thread-sort-by-random (h1 h2)
4488   "Sort threads by root article number."
4489   (gnus-article-sort-by-random
4490    (gnus-thread-header h1) (gnus-thread-header h2)))
4491
4492 (defsubst gnus-article-sort-by-lines (h1 h2)
4493   "Sort articles by article Lines header."
4494   (< (mail-header-lines h1)
4495      (mail-header-lines h2)))
4496
4497 (defun gnus-thread-sort-by-lines (h1 h2)
4498   "Sort threads by root article Lines header."
4499   (gnus-article-sort-by-lines
4500    (gnus-thread-header h1) (gnus-thread-header h2)))
4501
4502 (defsubst gnus-article-sort-by-chars (h1 h2)
4503   "Sort articles by octet length."
4504   (< (mail-header-chars h1)
4505      (mail-header-chars h2)))
4506
4507 (defun gnus-thread-sort-by-chars (h1 h2)
4508   "Sort threads by root article octet length."
4509   (gnus-article-sort-by-chars
4510    (gnus-thread-header h1) (gnus-thread-header h2)))
4511
4512 (defsubst gnus-article-sort-by-author (h1 h2)
4513   "Sort articles by root author."
4514   (string-lessp
4515    (let ((addr (car (mime-entity-read-field h1 'From))))
4516      (or (std11-full-name-string addr)
4517          (std11-address-string addr)
4518          ""))
4519    (let ((addr (car (mime-entity-read-field h2 'From))))
4520      (or (std11-full-name-string addr)
4521          (std11-address-string addr)
4522          ""))
4523    ))
4524
4525 (defun gnus-thread-sort-by-author (h1 h2)
4526   "Sort threads by root author."
4527   (gnus-article-sort-by-author
4528    (gnus-thread-header h1)  (gnus-thread-header h2)))
4529
4530 (defsubst gnus-article-sort-by-subject (h1 h2)
4531   "Sort articles by root subject."
4532   (string-lessp
4533    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4534    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4535
4536 (defun gnus-thread-sort-by-subject (h1 h2)
4537   "Sort threads by root subject."
4538   (gnus-article-sort-by-subject
4539    (gnus-thread-header h1) (gnus-thread-header h2)))
4540
4541 (defsubst gnus-article-sort-by-date (h1 h2)
4542   "Sort articles by root article date."
4543   (time-less-p
4544    (gnus-date-get-time (mail-header-date h1))
4545    (gnus-date-get-time (mail-header-date h2))))
4546
4547 (defun gnus-thread-sort-by-date (h1 h2)
4548   "Sort threads by root article date."
4549   (gnus-article-sort-by-date
4550    (gnus-thread-header h1) (gnus-thread-header h2)))
4551
4552 (defsubst gnus-article-sort-by-score (h1 h2)
4553   "Sort articles by root article score.
4554 Unscored articles will be counted as having a score of zero."
4555   (> (or (cdr (assq (mail-header-number h1)
4556                     gnus-newsgroup-scored))
4557          gnus-summary-default-score 0)
4558      (or (cdr (assq (mail-header-number h2)
4559                     gnus-newsgroup-scored))
4560          gnus-summary-default-score 0)))
4561
4562 (defun gnus-thread-sort-by-score (h1 h2)
4563   "Sort threads by root article score."
4564   (gnus-article-sort-by-score
4565    (gnus-thread-header h1) (gnus-thread-header h2)))
4566
4567 (defun gnus-thread-sort-by-total-score (h1 h2)
4568   "Sort threads by the sum of all scores in the thread.
4569 Unscored articles will be counted as having a score of zero."
4570   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4571
4572 (defun gnus-thread-total-score (thread)
4573   ;; This function find the total score of THREAD.
4574   (cond
4575    ((null thread)
4576     0)
4577    ((consp thread)
4578     (if (stringp (car thread))
4579         (apply gnus-thread-score-function 0
4580                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4581       (gnus-thread-total-score-1 thread)))
4582    (t
4583     (gnus-thread-total-score-1 (list thread)))))
4584
4585 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4586   "Sort threads such that the thread with the most recently arrived article comes first."
4587   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4588
4589 (defun gnus-thread-highest-number (thread)
4590   "Return the highest article number in THREAD."
4591   (apply 'max (mapcar (lambda (header)
4592                         (mail-header-number header))
4593                       (message-flatten-list thread))))
4594
4595 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4596   "Sort threads such that the thread with the most recently dated article comes first."
4597   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4598
4599 (defun gnus-thread-latest-date (thread)
4600   "Return the highest article date in THREAD."
4601   (let ((previous-time 0))
4602     (apply 'max
4603            (mapcar
4604             (lambda (header)
4605               (setq previous-time
4606                     (condition-case ()
4607                         (time-to-seconds (mail-header-parse-date
4608                                           (mail-header-date header)))
4609                       (error previous-time))))
4610             (sort
4611              (message-flatten-list thread)
4612              (lambda (h1 h2)
4613                (< (mail-header-number h1)
4614                   (mail-header-number h2))))))))
4615
4616 (defun gnus-thread-total-score-1 (root)
4617   ;; This function find the total score of the thread below ROOT.
4618   (setq root (car root))
4619   (apply gnus-thread-score-function
4620          (or (append
4621               (mapcar 'gnus-thread-total-score
4622                       (cdr (gnus-id-to-thread (mail-header-id root))))
4623               (when (> (mail-header-number root) 0)
4624                 (list (or (cdr (assq (mail-header-number root)
4625                                      gnus-newsgroup-scored))
4626                           gnus-summary-default-score 0))))
4627              (list gnus-summary-default-score)
4628              '(0))))
4629
4630 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4631 (defvar gnus-tmp-prev-subject nil)
4632 (defvar gnus-tmp-false-parent nil)
4633 (defvar gnus-tmp-root-expunged nil)
4634 (defvar gnus-tmp-dummy-line nil)
4635
4636 (eval-when-compile (defvar gnus-tmp-header))
4637 (defun gnus-extra-header (type &optional header)
4638   "Return the extra header of TYPE."
4639   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4640       ""))
4641
4642 (defvar gnus-tmp-thread-tree-header-string "")
4643
4644 (defcustom gnus-sum-thread-tree-root "> "
4645   "With %B spec, used for the root of a thread.
4646 If nil, use subject instead."
4647   :type '(radio (const :format "%v  " nil) (string :size 0))
4648   :group 'gnus-thread)
4649 (defcustom gnus-sum-thread-tree-false-root "> "
4650   "With %B spec, used for a false root of a thread.
4651 If nil, use subject instead."
4652   :type '(radio (const :format "%v  " nil) (string :size 0))
4653   :group 'gnus-thread)
4654 (defcustom gnus-sum-thread-tree-single-indent ""
4655   "With %B spec, used for a thread with just one message.
4656 If nil, use subject instead."
4657   :type '(radio (const :format "%v  " nil) (string :size 0))
4658   :group 'gnus-thread)
4659 (defcustom gnus-sum-thread-tree-vertical "| "
4660   "With %B spec, used for drawing a vertical line."
4661   :type 'string
4662   :group 'gnus-thread)
4663 (defcustom gnus-sum-thread-tree-indent "  "
4664   "With %B spec, used for indenting."
4665   :type 'string
4666   :group 'gnus-thread)
4667 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4668   "With %B spec, used for a leaf with brothers."
4669   :type 'string
4670   :group 'gnus-thread)
4671 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4672   "With %B spec, used for a leaf without brothers."
4673   :type 'string
4674   :group 'gnus-thread)
4675
4676 (defun gnus-summary-prepare-threads (threads)
4677   "Prepare summary buffer from THREADS and indentation LEVEL.
4678 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4679 or a straight list of headers."
4680   (gnus-message 7 "Generating summary...")
4681
4682   (setq gnus-newsgroup-threads threads)
4683   (beginning-of-line)
4684
4685   (let ((gnus-tmp-level 0)
4686         (default-score (or gnus-summary-default-score 0))
4687         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4688         (building-line-count gnus-summary-display-while-building)
4689         (building-count (integerp gnus-summary-display-while-building))
4690         thread number subject stack state gnus-tmp-gathered beg-match
4691         new-roots gnus-tmp-new-adopts thread-end simp-subject
4692         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4693         gnus-tmp-replied gnus-tmp-subject-or-nil
4694         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4695         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4696         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4697         tree-stack)
4698
4699     (setq gnus-tmp-prev-subject nil
4700           gnus-tmp-thread-tree-header-string "")
4701
4702     (if (vectorp (car threads))
4703         ;; If this is a straight (sic) list of headers, then a
4704         ;; threaded summary display isn't required, so we just create
4705         ;; an unthreaded one.
4706         (gnus-summary-prepare-unthreaded threads)
4707
4708       ;; Do the threaded display.
4709
4710       (if gnus-summary-display-while-building
4711           (switch-to-buffer (buffer-name)))
4712       (while (or threads stack gnus-tmp-new-adopts new-roots)
4713
4714         (if (and (= gnus-tmp-level 0)
4715                  (or (not stack)
4716                      (= (caar stack) 0))
4717                  (not gnus-tmp-false-parent)
4718                  (or gnus-tmp-new-adopts new-roots))
4719             (if gnus-tmp-new-adopts
4720                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4721                       thread (list (car gnus-tmp-new-adopts))
4722                       gnus-tmp-header (caar thread)
4723                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4724               (when new-roots
4725                 (setq thread (list (car new-roots))
4726                       gnus-tmp-header (caar thread)
4727                       new-roots (cdr new-roots))))
4728
4729           (if threads
4730               ;; If there are some threads, we do them before the
4731               ;; threads on the stack.
4732               (setq thread threads
4733                     gnus-tmp-header (caar thread))
4734             ;; There were no current threads, so we pop something off
4735             ;; the stack.
4736             (setq state (car stack)
4737                   gnus-tmp-level (car state)
4738                   tree-stack (cadr state)
4739                   thread (caddr state)
4740                   stack (cdr stack)
4741                   gnus-tmp-header (caar thread))))
4742
4743         (setq gnus-tmp-false-parent nil)
4744         (setq gnus-tmp-root-expunged nil)
4745         (setq thread-end nil)
4746
4747         (if (stringp gnus-tmp-header)
4748             ;; The header is a dummy root.
4749             (cond
4750              ((eq gnus-summary-make-false-root 'adopt)
4751               ;; We let the first article adopt the rest.
4752               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4753                                                (cddar thread)))
4754               (setq gnus-tmp-gathered
4755                     (nconc (mapcar
4756                             (lambda (h) (mail-header-number (car h)))
4757                             (cddar thread))
4758                            gnus-tmp-gathered))
4759               (setq thread (cons (list (caar thread)
4760                                        (cadar thread))
4761                                  (cdr thread)))
4762               (setq gnus-tmp-level -1
4763                     gnus-tmp-false-parent t))
4764              ((eq gnus-summary-make-false-root 'empty)
4765               ;; We print adopted articles with empty subject fields.
4766               (setq gnus-tmp-gathered
4767                     (nconc (mapcar
4768                             (lambda (h) (mail-header-number (car h)))
4769                             (cddar thread))
4770                            gnus-tmp-gathered))
4771               (setq gnus-tmp-level -1))
4772              ((eq gnus-summary-make-false-root 'dummy)
4773               ;; We remember that we probably want to output a dummy
4774               ;; root.
4775               (setq gnus-tmp-dummy-line gnus-tmp-header)
4776               (setq gnus-tmp-prev-subject gnus-tmp-header))
4777              (t
4778               ;; We do not make a root for the gathered
4779               ;; sub-threads at all.
4780               (setq gnus-tmp-level -1)))
4781
4782           (setq number (mail-header-number gnus-tmp-header)
4783                 subject (mail-header-subject gnus-tmp-header)
4784                 simp-subject (gnus-simplify-subject-fully subject))
4785
4786           (cond
4787            ;; If the thread has changed subject, we might want to make
4788            ;; this subthread into a root.
4789            ((and (null gnus-thread-ignore-subject)
4790                  (not (zerop gnus-tmp-level))
4791                  gnus-tmp-prev-subject
4792                  (not (string= gnus-tmp-prev-subject simp-subject)))
4793             (setq new-roots (nconc new-roots (list (car thread)))
4794                   thread-end t
4795                   gnus-tmp-header nil))
4796            ;; If the article lies outside the current limit,
4797            ;; then we do not display it.
4798            ((not (memq number gnus-newsgroup-limit))
4799             (setq gnus-tmp-gathered
4800                   (nconc (mapcar
4801                           (lambda (h) (mail-header-number (car h)))
4802                           (cdar thread))
4803                          gnus-tmp-gathered))
4804             (setq gnus-tmp-new-adopts (if (cdar thread)
4805                                           (append gnus-tmp-new-adopts
4806                                                   (cdar thread))
4807                                         gnus-tmp-new-adopts)
4808                   thread-end t
4809                   gnus-tmp-header nil)
4810             (when (zerop gnus-tmp-level)
4811               (setq gnus-tmp-root-expunged t)))
4812            ;; Perhaps this article is to be marked as read?
4813            ((and gnus-summary-mark-below
4814                  (< (or (cdr (assq number gnus-newsgroup-scored))
4815                         default-score)
4816                     gnus-summary-mark-below)
4817                  ;; Don't touch sparse articles.
4818                  (not (gnus-summary-article-sparse-p number))
4819                  (not (gnus-summary-article-ancient-p number)))
4820             (setq gnus-newsgroup-unreads
4821                   (delq number gnus-newsgroup-unreads))
4822             (if gnus-newsgroup-auto-expire
4823                 (setq gnus-newsgroup-expirable
4824                       (gnus-add-to-sorted-list
4825                        gnus-newsgroup-expirable number))
4826               (push (cons number gnus-low-score-mark)
4827                     gnus-newsgroup-reads))))
4828
4829           (when gnus-tmp-header
4830             ;; We may have an old dummy line to output before this
4831             ;; article.
4832             (when (and gnus-tmp-dummy-line
4833                        (gnus-subject-equal
4834                         gnus-tmp-dummy-line
4835                         (mail-header-subject gnus-tmp-header)))
4836               (gnus-summary-insert-dummy-line
4837                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4838               (setq gnus-tmp-dummy-line nil))
4839
4840             ;; Compute the mark.
4841             (setq gnus-tmp-unread (gnus-article-mark number))
4842
4843             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4844                                   gnus-tmp-header gnus-tmp-level)
4845                   gnus-newsgroup-data)
4846
4847             ;; Actually insert the line.
4848             (setq
4849              gnus-tmp-subject-or-nil
4850              (cond
4851               ((and gnus-thread-ignore-subject
4852                     gnus-tmp-prev-subject
4853                     (not (string= gnus-tmp-prev-subject simp-subject)))
4854                subject)
4855               ((zerop gnus-tmp-level)
4856                (if (and (eq gnus-summary-make-false-root 'empty)
4857                         (memq number gnus-tmp-gathered)
4858                         gnus-tmp-prev-subject
4859                         (string= gnus-tmp-prev-subject simp-subject))
4860                    gnus-summary-same-subject
4861                  subject))
4862               (t gnus-summary-same-subject)))
4863             (if (and (eq gnus-summary-make-false-root 'adopt)
4864                      (= gnus-tmp-level 1)
4865                      (memq number gnus-tmp-gathered))
4866                 (setq gnus-tmp-opening-bracket ?\<
4867                       gnus-tmp-closing-bracket ?\>)
4868               (setq gnus-tmp-opening-bracket ?\[
4869                     gnus-tmp-closing-bracket ?\]))
4870             (setq
4871              gnus-tmp-indentation
4872              (aref gnus-thread-indent-array gnus-tmp-level)
4873              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4874              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4875                                 gnus-summary-default-score 0)
4876              gnus-tmp-score-char
4877              (if (or (null gnus-summary-default-score)
4878                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4879                          gnus-summary-zcore-fuzz))
4880                  ?\ ;;;Whitespace
4881                (if (< gnus-tmp-score gnus-summary-default-score)
4882                    gnus-score-below-mark gnus-score-over-mark))
4883              gnus-tmp-replied
4884              (cond ((memq number gnus-newsgroup-processable)
4885                     gnus-process-mark)
4886                    ((memq number gnus-newsgroup-cached)
4887                     gnus-cached-mark)
4888                    ((memq number gnus-newsgroup-replied)
4889                     gnus-replied-mark)
4890                    ((memq number gnus-newsgroup-forwarded)
4891                     gnus-forwarded-mark)
4892                    ((memq number gnus-newsgroup-saved)
4893                     gnus-saved-mark)
4894                    ((memq number gnus-newsgroup-recent)
4895                     gnus-recent-mark)
4896                    ((memq number gnus-newsgroup-unseen)
4897                     gnus-unseen-mark)
4898                    (t gnus-no-mark))
4899              gnus-tmp-downloaded
4900              (cond ((memq number gnus-newsgroup-undownloaded)
4901                     gnus-undownloaded-mark)
4902                    (gnus-newsgroup-agentized
4903                     gnus-downloaded-mark)
4904                    (t
4905                     gnus-no-mark))
4906              gnus-tmp-from (mail-header-from gnus-tmp-header)
4907              gnus-tmp-name
4908              (cond
4909               ((string-match "<[^>]+> *$" gnus-tmp-from)
4910                (setq beg-match (match-beginning 0))
4911                (or (and (string-match "^\".+\"" gnus-tmp-from)
4912                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4913                    (substring gnus-tmp-from 0 beg-match)))
4914               ((string-match "(.+)" gnus-tmp-from)
4915                (substring gnus-tmp-from
4916                           (1+ (match-beginning 0)) (1- (match-end 0))))
4917               (t gnus-tmp-from))
4918
4919              ;; Do the %B string
4920              gnus-tmp-thread-tree-header-string
4921              (cond
4922               ((not gnus-show-threads) "")
4923               ((zerop gnus-tmp-level)
4924                (cond ((cdar thread)
4925                       (or gnus-sum-thread-tree-root subject))
4926                      (gnus-tmp-new-adopts
4927                       (or gnus-sum-thread-tree-false-root subject))
4928                      (t
4929                       (or gnus-sum-thread-tree-single-indent subject))))
4930               (t
4931                (concat (apply 'concat
4932                               (mapcar (lambda (item)
4933                                         (if (= item 1)
4934                                             gnus-sum-thread-tree-vertical
4935                                           gnus-sum-thread-tree-indent))
4936                                       (cdr (reverse tree-stack))))
4937                        (if (nth 1 thread)
4938                            gnus-sum-thread-tree-leaf-with-other
4939                          gnus-sum-thread-tree-single-leaf)))))
4940             (when (string= gnus-tmp-name "")
4941               (setq gnus-tmp-name gnus-tmp-from))
4942             (unless (numberp gnus-tmp-lines)
4943               (setq gnus-tmp-lines -1))
4944             (if (= gnus-tmp-lines -1)
4945                 (setq gnus-tmp-lines "?")
4946               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4947               (gnus-put-text-property
4948              (point)
4949              (progn (eval gnus-summary-line-format-spec) (point))
4950                'gnus-number number)
4951             (when gnus-visual-p
4952               (forward-line -1)
4953               (gnus-run-hooks 'gnus-summary-update-hook)
4954               (forward-line 1))
4955
4956             (setq gnus-tmp-prev-subject simp-subject)))
4957
4958         (when (nth 1 thread)
4959           (push (list (max 0 gnus-tmp-level)
4960                       (copy-sequence tree-stack)
4961                       (nthcdr 1 thread))
4962                 stack))
4963         (push (if (nth 1 thread) 1 0) tree-stack)
4964         (incf gnus-tmp-level)
4965         (setq threads (if thread-end nil (cdar thread)))
4966         (if gnus-summary-display-while-building
4967             (if building-count
4968                 (progn
4969                   ;; use a set frequency
4970                   (setq building-line-count (1- building-line-count))
4971                   (when (= building-line-count 0)
4972                     (sit-for 0)
4973                     (setq building-line-count
4974                           gnus-summary-display-while-building)))
4975               ;; always
4976               (sit-for 0)))
4977         (unless threads
4978           (setq gnus-tmp-level 0)))))
4979   (gnus-message 7 "Generating summary...done"))
4980
4981 (defun gnus-summary-prepare-unthreaded (headers)
4982   "Generate an unthreaded summary buffer based on HEADERS."
4983   (let (header number mark)
4984
4985     (beginning-of-line)
4986
4987     (while headers
4988       ;; We may have to root out some bad articles...
4989       (when (memq (setq number (mail-header-number
4990                                 (setq header (pop headers))))
4991                   gnus-newsgroup-limit)
4992         ;; Mark article as read when it has a low score.
4993         (when (and gnus-summary-mark-below
4994                    (< (or (cdr (assq number gnus-newsgroup-scored))
4995                           gnus-summary-default-score 0)
4996                       gnus-summary-mark-below)
4997                    (not (gnus-summary-article-ancient-p number)))
4998           (setq gnus-newsgroup-unreads
4999                 (delq number gnus-newsgroup-unreads))
5000           (if gnus-newsgroup-auto-expire
5001               (push number gnus-newsgroup-expirable)
5002             (push (cons number gnus-low-score-mark)
5003                   gnus-newsgroup-reads)))
5004
5005         (setq mark (gnus-article-mark number))
5006         (push (gnus-data-make number mark (1+ (point)) header 0)
5007               gnus-newsgroup-data)
5008         (gnus-summary-insert-line
5009          header 0 number
5010          (memq number gnus-newsgroup-undownloaded)
5011          mark (memq number gnus-newsgroup-replied)
5012          (memq number gnus-newsgroup-expirable)
5013          (mail-header-subject header) nil
5014          (cdr (assq number gnus-newsgroup-scored))
5015          (memq number gnus-newsgroup-processable))))))
5016
5017 (defun gnus-summary-remove-list-identifiers ()
5018   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5019   (let ((regexp (if (consp gnus-list-identifiers)
5020                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5021                   gnus-list-identifiers))
5022         changed subject)
5023     (when regexp
5024       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5025       (dolist (header gnus-newsgroup-headers)
5026         (setq subject (mail-header-subject header)
5027               changed nil)
5028         (while (string-match regexp subject)
5029           (setq subject
5030                 (concat (substring subject 0 (match-beginning 1))
5031                         (substring subject (match-end 0)))
5032                 changed t))
5033         (when changed
5034           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5035             (setq subject
5036                   (concat (substring subject 0 (match-beginning 1))
5037                           (substring subject (match-end 1)))))
5038           (mail-header-set-subject header subject))))))
5039
5040 (defun gnus-fetch-headers (articles)
5041   "Fetch headers of ARTICLES."
5042   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5043     (gnus-message 5 "Fetching headers for %s..." name)
5044     (prog1
5045         (if (eq 'nov
5046                 (setq gnus-headers-retrieved-by
5047                       (gnus-retrieve-headers
5048                        articles gnus-newsgroup-name
5049                        ;; We might want to fetch old headers, but
5050                        ;; not if there is only 1 article.
5051                        (and (or (and
5052                                  (not (eq gnus-fetch-old-headers 'some))
5053                                  (not (numberp gnus-fetch-old-headers)))
5054                                 (> (length articles) 1))
5055                             gnus-fetch-old-headers))))
5056             (gnus-get-newsgroup-headers-xover
5057              articles nil nil gnus-newsgroup-name t)
5058           (gnus-get-newsgroup-headers))
5059       (gnus-message 5 "Fetching headers for %s...done" name))))
5060
5061 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5062   "Select newsgroup GROUP.
5063 If READ-ALL is non-nil, all articles in the group are selected.
5064 If SELECT-ARTICLES, only select those articles from GROUP."
5065   (let* ((entry (gnus-group-entry group))
5066          ;;!!! Dirty hack; should be removed.
5067          (gnus-summary-ignore-duplicates
5068           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5069               t
5070             gnus-summary-ignore-duplicates))
5071          (info (nth 2 entry))
5072          articles fetched-articles cached)
5073
5074     (unless (gnus-check-server
5075              (set (make-local-variable 'gnus-current-select-method)
5076                   (gnus-find-method-for-group group)))
5077       (error "Couldn't open server"))
5078
5079     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5080         (gnus-activate-group group)     ; Or we can activate it...
5081         (progn                          ; Or we bug out.
5082           (when (equal major-mode 'gnus-summary-mode)
5083             (gnus-kill-buffer (current-buffer)))
5084           (error "Couldn't activate group %s: %s"
5085                  group (gnus-status-message group))))
5086
5087     (unless (gnus-request-group group t)
5088       (when (equal major-mode 'gnus-summary-mode)
5089         (gnus-kill-buffer (current-buffer)))
5090       (error "Couldn't request group %s: %s"
5091              group (gnus-status-message group)))
5092
5093     (when gnus-agent
5094       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5095       
5096       (setq gnus-summary-use-undownloaded-faces
5097             (gnus-agent-find-parameter
5098              group
5099              'agent-enable-undownloaded-faces)))
5100
5101     (setq gnus-newsgroup-name group
5102           gnus-newsgroup-unselected nil
5103           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5104
5105     (let ((display (gnus-group-find-parameter group 'display)))
5106       (setq gnus-newsgroup-display
5107             (cond
5108              ((not (zerop (or (car-safe read-all) 0)))
5109               ;; The user entered the group with C-u SPC/RET, let's show
5110               ;; all articles.
5111               'gnus-not-ignore)
5112              ((eq display 'all)
5113               'gnus-not-ignore)
5114              ((arrayp display)
5115               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5116              ((numberp display)
5117               ;; The following is probably the "correct" solution, but
5118               ;; it makes Gnus fetch all headers and then limit the
5119               ;; articles (which is slow), so instead we hack the
5120               ;; select-articles parameter instead. -- Simon Josefsson
5121               ;; <jas@kth.se>
5122               ;;
5123               ;; (gnus-byte-compile
5124               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5125               ;;                         display)))))
5126               (setq select-articles
5127                     (gnus-uncompress-range
5128                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5129                              (if (> tmp 0)
5130                                  tmp
5131                                1))
5132                            (cdr (gnus-active group)))))
5133               nil)
5134              (t
5135               nil))))
5136
5137     (gnus-summary-setup-default-charset)
5138
5139     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5140     (when (gnus-virtual-group-p group)
5141       (setq cached gnus-newsgroup-cached))
5142
5143     (setq gnus-newsgroup-unreads
5144           (gnus-sorted-ndifference
5145            (gnus-sorted-ndifference gnus-newsgroup-unreads
5146                                     gnus-newsgroup-marked)
5147            gnus-newsgroup-dormant))
5148
5149     (setq gnus-newsgroup-processable nil)
5150
5151     (gnus-update-read-articles group gnus-newsgroup-unreads)
5152
5153     ;; Adjust and set lists of article marks.
5154     (when info
5155       (gnus-adjust-marked-articles info))
5156     (if (setq articles select-articles)
5157         (setq gnus-newsgroup-unselected
5158               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5159       (setq articles (gnus-articles-to-read group read-all)))
5160
5161     (cond
5162      ((null articles)
5163       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5164       'quit)
5165      ((eq articles 0) nil)
5166      (t
5167       ;; Init the dependencies hash table.
5168       (setq gnus-newsgroup-dependencies
5169             (gnus-make-hashtable (length articles)))
5170       (gnus-set-global-variables)
5171       ;; Retrieve the headers and read them in.
5172
5173       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5174
5175       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5176       (when cached
5177         (setq gnus-newsgroup-cached cached))
5178
5179       ;; Suppress duplicates?
5180       (when gnus-suppress-duplicates
5181         (gnus-dup-suppress-articles))
5182
5183       ;; Set the initial limit.
5184       (setq gnus-newsgroup-limit (copy-sequence articles))
5185       ;; Remove canceled articles from the list of unread articles.
5186       (setq fetched-articles
5187             (mapcar (lambda (headers) (mail-header-number headers))
5188                     gnus-newsgroup-headers))
5189       (setq gnus-newsgroup-articles fetched-articles)
5190       (setq gnus-newsgroup-unreads
5191             (gnus-sorted-nintersection
5192              gnus-newsgroup-unreads fetched-articles))
5193       (gnus-compute-unseen-list)
5194
5195       ;; Removed marked articles that do not exist.
5196       (gnus-update-missing-marks
5197        (gnus-sorted-difference articles fetched-articles))
5198       ;; We might want to build some more threads first.
5199       (when (and gnus-fetch-old-headers
5200                  (eq gnus-headers-retrieved-by 'nov))
5201         (if (eq gnus-fetch-old-headers 'invisible)
5202             (gnus-build-all-threads)
5203           (gnus-build-old-threads)))
5204       ;; Let the Gnus agent mark articles as read.
5205       (when gnus-agent
5206         (gnus-agent-get-undownloaded-list))
5207       ;; Remove list identifiers from subject
5208       (when gnus-list-identifiers
5209         (gnus-summary-remove-list-identifiers))
5210       ;; Check whether auto-expire is to be done in this group.
5211       (setq gnus-newsgroup-auto-expire
5212             (gnus-group-auto-expirable-p group))
5213       ;; Set up the article buffer now, if necessary.
5214       (unless gnus-single-article-buffer
5215         (gnus-article-setup-buffer))
5216       ;; First and last article in this newsgroup.
5217       (when gnus-newsgroup-headers
5218         (setq gnus-newsgroup-begin
5219               (mail-header-number (car gnus-newsgroup-headers))
5220               gnus-newsgroup-end
5221               (mail-header-number
5222                (gnus-last-element gnus-newsgroup-headers))))
5223       ;; GROUP is successfully selected.
5224       (or gnus-newsgroup-headers t)))))
5225
5226 (defun gnus-compute-unseen-list ()
5227   ;; The `seen' marks are treated specially.
5228   (if (not gnus-newsgroup-seen)
5229       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5230     (setq gnus-newsgroup-unseen
5231           (gnus-inverse-list-range-intersection
5232            gnus-newsgroup-articles gnus-newsgroup-seen))))
5233
5234 (defun gnus-summary-display-make-predicate (display)
5235   (require 'gnus-agent)
5236   (when (= (length display) 1)
5237     (setq display (car display)))
5238   (unless gnus-summary-display-cache
5239     (dolist (elem (append '((unread . unread)
5240                             (read . read)
5241                             (unseen . unseen))
5242                           gnus-article-mark-lists))
5243       (push (cons (cdr elem)
5244                   (gnus-byte-compile
5245                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5246             gnus-summary-display-cache)))
5247   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5248         (gnus-category-predicate-cache gnus-summary-display-cache))
5249     (gnus-get-predicate display)))
5250
5251 ;; Uses the dynamically bound `number' variable.
5252 (eval-when-compile
5253   (defvar number))
5254 (defun gnus-article-marked-p (type &optional article)
5255   (let ((article (or article number)))
5256     (cond
5257      ((eq type 'tick)
5258       (memq article gnus-newsgroup-marked))
5259      ((eq type 'spam)
5260       (memq article gnus-newsgroup-spam-marked))
5261      ((eq type 'unsend)
5262       (memq article gnus-newsgroup-unsendable))
5263      ((eq type 'undownload)
5264       (memq article gnus-newsgroup-undownloaded))
5265      ((eq type 'download)
5266       (memq article gnus-newsgroup-downloadable))
5267      ((eq type 'unread)
5268       (memq article gnus-newsgroup-unreads))
5269      ((eq type 'read)
5270       (memq article gnus-newsgroup-reads))
5271      ((eq type 'dormant)
5272       (memq article gnus-newsgroup-dormant) )
5273      ((eq type 'expire)
5274       (memq article gnus-newsgroup-expirable))
5275      ((eq type 'reply)
5276       (memq article gnus-newsgroup-replied))
5277      ((eq type 'killed)
5278       (memq article gnus-newsgroup-killed))
5279      ((eq type 'bookmark)
5280       (assq article gnus-newsgroup-bookmarks))
5281      ((eq type 'score)
5282       (assq article gnus-newsgroup-scored))
5283      ((eq type 'save)
5284       (memq article gnus-newsgroup-saved))
5285      ((eq type 'cache)
5286       (memq article gnus-newsgroup-cached))
5287      ((eq type 'forward)
5288       (memq article gnus-newsgroup-forwarded))
5289      ((eq type 'seen)
5290       (not (memq article gnus-newsgroup-unseen)))
5291      ((eq type 'recent)
5292       (memq article gnus-newsgroup-recent))
5293      (t t))))
5294
5295 (defun gnus-articles-to-read (group &optional read-all)
5296   "Find out what articles the user wants to read."
5297   (let* ((articles
5298           ;; Select all articles if `read-all' is non-nil, or if there
5299           ;; are no unread articles.
5300           (if (or read-all
5301                   (and (zerop (length gnus-newsgroup-marked))
5302                        (zerop (length gnus-newsgroup-unreads)))
5303                   ;; Fetch all if the predicate is non-nil.
5304                   gnus-newsgroup-display)
5305               ;; We want to select the headers for all the articles in
5306               ;; the group, so we select either all the active
5307               ;; articles in the group, or (if that's nil), the
5308               ;; articles in the cache.
5309               (or
5310                (gnus-uncompress-range (gnus-active group))
5311                (gnus-cache-articles-in-group group))
5312             ;; Select only the "normal" subset of articles.
5313             (gnus-sorted-nunion
5314              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5315              gnus-newsgroup-unreads)))
5316          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5317          (scored (length scored-list))
5318          (number (length articles))
5319          (marked (+ (length gnus-newsgroup-marked)
5320                     (length gnus-newsgroup-dormant)))
5321          (select
5322           (cond
5323            ((numberp read-all)
5324             read-all)
5325            ((numberp gnus-newsgroup-display)
5326             gnus-newsgroup-display)
5327            (t
5328             (condition-case ()
5329                 (cond
5330                  ((and (or (<= scored marked) (= scored number))
5331                        (numberp gnus-large-newsgroup)
5332                        (> number gnus-large-newsgroup))
5333                   (let* ((cursor-in-echo-area nil)
5334                          (initial (gnus-parameter-large-newsgroup-initial
5335                                    gnus-newsgroup-name))
5336                          (input
5337                           (read-string
5338                            (format
5339                             "How many articles from %s (%s %d): "
5340                             (gnus-limit-string
5341                              (gnus-group-decoded-name gnus-newsgroup-name)
5342                              35)
5343                             (if initial "max" "default")
5344                             number)
5345                            (if initial
5346                                (cons (number-to-string initial)
5347                                      0)))))
5348                     (if (string-match "^[ \t]*$" input) number input)))
5349                  ((and (> scored marked) (< scored number)
5350                        (> (- scored number) 20))
5351                   (let ((input
5352                          (read-string
5353                           (format "%s %s (%d scored, %d total): "
5354                                   "How many articles from"
5355                                   (gnus-group-decoded-name group)
5356                                   scored number))))
5357                     (if (string-match "^[ \t]*$" input)
5358                         number input)))
5359                  (t number))
5360               (quit
5361                (message "Quit getting the articles to read")
5362                nil))))))
5363     (setq select (if (stringp select) (string-to-number select) select))
5364     (if (or (null select) (zerop select))
5365         select
5366       (if (and (not (zerop scored)) (<= (abs select) scored))
5367           (progn
5368             (setq articles (sort scored-list '<))
5369             (setq number (length articles)))
5370         (setq articles (copy-sequence articles)))
5371
5372       (when (< (abs select) number)
5373         (if (< select 0)
5374             ;; Select the N oldest articles.
5375             (setcdr (nthcdr (1- (abs select)) articles) nil)
5376           ;; Select the N most recent articles.
5377           (setq articles (nthcdr (- number select) articles))))
5378       (setq gnus-newsgroup-unselected
5379             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5380       (when gnus-alter-articles-to-read-function
5381         (setq articles
5382               (sort
5383                (funcall gnus-alter-articles-to-read-function
5384                         gnus-newsgroup-name articles)
5385                '<)))
5386       articles)))
5387
5388 (defun gnus-killed-articles (killed articles)
5389   (let (out)
5390     (while articles
5391       (when (inline (gnus-member-of-range (car articles) killed))
5392         (push (car articles) out))
5393       (setq articles (cdr articles)))
5394     out))
5395
5396 (defun gnus-uncompress-marks (marks)
5397   "Uncompress the mark ranges in MARKS."
5398   (let ((uncompressed '(score bookmark))
5399         out)
5400     (while marks
5401       (if (memq (caar marks) uncompressed)
5402           (push (car marks) out)
5403         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5404       (setq marks (cdr marks)))
5405     out))
5406
5407 (defun gnus-article-mark-to-type (mark)
5408   "Return the type of MARK."
5409   (or (cadr (assq mark gnus-article-special-mark-lists))
5410       'list))
5411
5412 (defun gnus-article-unpropagatable-p (mark)
5413   "Return whether MARK should be propagated to back end."
5414   (memq mark gnus-article-unpropagated-mark-lists))
5415
5416 (defun gnus-adjust-marked-articles (info)
5417   "Set all article lists and remove all marks that are no longer valid."
5418   (let* ((marked-lists (gnus-info-marks info))
5419          (active (gnus-active (gnus-info-group info)))
5420          (min (car active))
5421          (max (cdr active))
5422          (types gnus-article-mark-lists)
5423          marks var articles article mark mark-type
5424          bgn end)
5425
5426     (dolist (marks marked-lists)
5427       (setq mark (car marks)
5428             mark-type (gnus-article-mark-to-type mark)
5429             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5430
5431       ;; We set the variable according to the type of the marks list,
5432       ;; and then adjust the marks to a subset of the active articles.
5433       (cond
5434        ;; Adjust "simple" lists - compressed yet unsorted
5435        ((eq mark-type 'list)
5436         ;; Simultaneously uncompress and clip to active range
5437         ;; See gnus-uncompress-range for a description of possible marks
5438         (let (l lh)
5439           (if (not (cadr marks))
5440               (set var nil)
5441             (setq articles (if (numberp (cddr marks))
5442                                (list (cdr marks))
5443                              (cdr marks))
5444                   lh (cons nil nil)
5445                   l lh)
5446
5447             (while (setq article (pop articles))
5448               (cond ((consp article)
5449                      (setq bgn (max (car article) min)
5450                            end (min (cdr article) max))
5451                      (while (<= bgn end)
5452                        (setq l (setcdr l (cons bgn nil))
5453                              bgn (1+ bgn))))
5454                     ((and (<= min article)
5455                           (>= max article))
5456                      (setq l (setcdr l (cons article nil))))))
5457             (set var (cdr lh)))))
5458        ;; Adjust assocs.
5459        ((eq mark-type 'tuple)
5460         (set var (setq articles (cdr marks)))
5461         (when (not (listp (cdr (symbol-value var))))
5462           (set var (list (symbol-value var))))
5463         (when (not (listp (cdr articles)))
5464           (setq articles (list articles)))
5465         (while articles
5466           (when (or (not (consp (setq article (pop articles))))
5467                     (< (car article) min)
5468                     (> (car article) max))
5469             (set var (delq article (symbol-value var))))))
5470        ;; Adjust ranges (sloppily).
5471        ((eq mark-type 'range)
5472         (cond
5473          ((eq mark 'seen)
5474           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5475           ;; It should be (seen (NUM1 . NUM2)).
5476           (when (numberp (cddr marks))
5477             (setcdr marks (list (cdr marks))))
5478           (setq articles (cdr marks))
5479           (while (and articles
5480                       (or (and (consp (car articles))
5481                                (> min (cdar articles)))
5482                           (and (numberp (car articles))
5483                                (> min (car articles)))))
5484             (pop articles))
5485           (set var articles))))))))
5486
5487 (defun gnus-update-missing-marks (missing)
5488   "Go through the list of MISSING articles and remove them from the mark lists."
5489   (when missing
5490     (let (var m)
5491       ;; Go through all types.
5492       (dolist (elem gnus-article-mark-lists)
5493         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5494           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5495           (when (symbol-value var)
5496             ;; This list has articles.  So we delete all missing
5497             ;; articles from it.
5498             (setq m missing)
5499             (while m
5500               (set var (delq (pop m) (symbol-value var))))))))))
5501
5502 (defun gnus-update-marks ()
5503   "Enter the various lists of marked articles into the newsgroup info list."
5504   (let ((types gnus-article-mark-lists)
5505         (info (gnus-get-info gnus-newsgroup-name))
5506         type list newmarked symbol delta-marks)
5507     (when info
5508       ;; Add all marks lists to the list of marks lists.
5509       (while (setq type (pop types))
5510         (setq list (symbol-value
5511                     (setq symbol
5512                           (intern (format "gnus-newsgroup-%s" (car type))))))
5513
5514         (when list
5515           ;; Get rid of the entries of the articles that have the
5516           ;; default score.
5517           (when (and (eq (cdr type) 'score)
5518                      gnus-save-score
5519                      list)
5520             (let* ((arts list)
5521                    (prev (cons nil list))
5522                    (all prev))
5523               (while arts
5524                 (if (or (not (consp (car arts)))
5525                         (= (cdar arts) gnus-summary-default-score))
5526                     (setcdr prev (cdr arts))
5527                   (setq prev arts))
5528                 (setq arts (cdr arts)))
5529               (setq list (cdr all)))))
5530
5531         (when (eq (cdr type) 'seen)
5532           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5533
5534         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5535           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5536
5537         (when (and (gnus-check-backend-function
5538                     'request-set-mark gnus-newsgroup-name)
5539                    (not (gnus-article-unpropagatable-p (cdr type))))
5540           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5541                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5542                  (add (gnus-remove-from-range
5543                        (gnus-copy-sequence list) old)))
5544             (when add
5545               (push (list add 'add (list (cdr type))) delta-marks))
5546             (when del
5547               (push (list del 'del (list (cdr type))) delta-marks))))
5548
5549         (when list
5550           (push (cons (cdr type) list) newmarked)))
5551
5552       (when delta-marks
5553         (unless (gnus-check-group gnus-newsgroup-name)
5554           (error "Can't open server for %s" gnus-newsgroup-name))
5555         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5556
5557       ;; Enter these new marks into the info of the group.
5558       (if (nthcdr 3 info)
5559           (setcar (nthcdr 3 info) newmarked)
5560         ;; Add the marks lists to the end of the info.
5561         (when newmarked
5562           (setcdr (nthcdr 2 info) (list newmarked))))
5563
5564       ;; Cut off the end of the info if there's nothing else there.
5565       (let ((i 5))
5566         (while (and (> i 2)
5567                     (not (nth i info)))
5568           (when (nthcdr (decf i) info)
5569             (setcdr (nthcdr i info) nil)))))))
5570
5571 (defun gnus-set-mode-line (where)
5572   "Set the mode line of the article or summary buffers.
5573 If WHERE is `summary', the summary mode line format will be used."
5574   ;; Is this mode line one we keep updated?
5575   (when (and (memq where gnus-updated-mode-lines)
5576              (symbol-value
5577               (intern (format "gnus-%s-mode-line-format-spec" where))))
5578     (let (mode-string)
5579       (save-excursion
5580         ;; We evaluate this in the summary buffer since these
5581         ;; variables are buffer-local to that buffer.
5582         (set-buffer gnus-summary-buffer)
5583         ;; We bind all these variables that are used in the `eval' form
5584         ;; below.
5585         (let* ((mformat (symbol-value
5586                          (intern
5587                           (format "gnus-%s-mode-line-format-spec" where))))
5588                (gnus-tmp-group-name (gnus-group-decoded-name
5589                                      gnus-newsgroup-name))
5590                (gnus-tmp-article-number (or gnus-current-article 0))
5591                (gnus-tmp-unread gnus-newsgroup-unreads)
5592                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5593                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5594                (gnus-tmp-unread-and-unselected
5595                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5596                             (zerop gnus-tmp-unselected))
5597                        "")
5598                       ((zerop gnus-tmp-unselected)
5599                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5600                       (t (format "{%d(+%d) more}"
5601                                  gnus-tmp-unread-and-unticked
5602                                  gnus-tmp-unselected))))
5603                (gnus-tmp-subject
5604                 (if (and gnus-current-headers
5605                          (vectorp gnus-current-headers))
5606                     (gnus-mode-string-quote
5607                      (mail-header-subject gnus-current-headers))
5608                   ""))
5609                bufname-length max-len
5610                gnus-tmp-header);; passed as argument to any user-format-funcs
5611           (setq mode-string (eval mformat))
5612           (setq bufname-length (if (string-match "%b" mode-string)
5613                                    (- (length
5614                                        (buffer-name
5615                                         (if (eq where 'summary)
5616                                             nil
5617                                           (get-buffer gnus-article-buffer))))
5618                                       2)
5619                                  0))
5620           (setq max-len (max 4 (if gnus-mode-non-string-length
5621                                    (- (window-width)
5622                                       gnus-mode-non-string-length
5623                                       bufname-length)
5624                                  (length mode-string))))
5625           ;; We might have to chop a bit of the string off...
5626           (when (> (length mode-string) max-len)
5627             (setq mode-string
5628                   (concat (gnus-truncate-string mode-string (- max-len 3))
5629                           "...")))
5630           ;; Pad the mode string a bit.
5631           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5632       ;; Update the mode line.
5633       (setq mode-line-buffer-identification
5634             (gnus-mode-line-buffer-identification (list mode-string)))
5635       (set-buffer-modified-p t))))
5636
5637 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5638   "Go through the HEADERS list and add all Xrefs to a hash table.
5639 The resulting hash table is returned, or nil if no Xrefs were found."
5640   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5641          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5642          (xref-hashtb (gnus-make-hashtable))
5643          start group entry number xrefs header)
5644     (while headers
5645       (setq header (pop headers))
5646       (when (and (setq xrefs (mail-header-xref header))
5647                  (not (memq (setq number (mail-header-number header))
5648                             unreads)))
5649         (setq start 0)
5650         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5651           (setq start (match-end 0))
5652           (setq group (if prefix
5653                           (concat prefix (substring xrefs (match-beginning 1)
5654                                                     (match-end 1)))
5655                         (substring xrefs (match-beginning 1) (match-end 1))))
5656           (setq number
5657                 (string-to-int (substring xrefs (match-beginning 2)
5658                                           (match-end 2))))
5659           (if (setq entry (gnus-gethash group xref-hashtb))
5660               (setcdr entry (cons number (cdr entry)))
5661             (gnus-sethash group (cons number nil) xref-hashtb)))))
5662     (and start xref-hashtb)))
5663
5664 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5665   "Look through all the headers and mark the Xrefs as read."
5666   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5667         name info xref-hashtb idlist method nth4)
5668     (save-excursion
5669       (set-buffer gnus-group-buffer)
5670       (when (setq xref-hashtb
5671                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5672         (mapatoms
5673          (lambda (group)
5674            (unless (string= from-newsgroup (setq name (symbol-name group)))
5675              (setq idlist (symbol-value group))
5676              ;; Dead groups are not updated.
5677              (and (prog1
5678                       (setq info (gnus-get-info name))
5679                     (when (stringp (setq nth4 (gnus-info-method info)))
5680                       (setq nth4 (gnus-server-to-method nth4))))
5681                   ;; Only do the xrefs if the group has the same
5682                   ;; select method as the group we have just read.
5683                   (or (gnus-methods-equal-p
5684                        nth4 (gnus-find-method-for-group from-newsgroup))
5685                       virtual
5686                       (equal nth4 (setq method (gnus-find-method-for-group
5687                                                 from-newsgroup)))
5688                       (and (equal (car nth4) (car method))
5689                            (equal (nth 1 nth4) (nth 1 method))))
5690                   gnus-use-cross-reference
5691                   (or (not (eq gnus-use-cross-reference t))
5692                       virtual
5693                       ;; Only do cross-references on subscribed
5694                       ;; groups, if that is what is wanted.
5695                       (<= (gnus-info-level info) gnus-level-subscribed))
5696                   (gnus-group-make-articles-read name idlist))))
5697          xref-hashtb)))))
5698
5699 (defun gnus-compute-read-articles (group articles)
5700   (let* ((entry (gnus-group-entry group))
5701          (info (nth 2 entry))
5702          (active (gnus-active group))
5703          ninfo)
5704     (when entry
5705       ;; First peel off all invalid article numbers.
5706       (when active
5707         (let ((ids articles)
5708               id first)
5709           (while (setq id (pop ids))
5710             (when (and first (> id (cdr active)))
5711               ;; We'll end up in this situation in one particular
5712               ;; obscure situation.  If you re-scan a group and get
5713               ;; a new article that is cross-posted to a different
5714               ;; group that has not been re-scanned, you might get
5715               ;; crossposted article that has a higher number than
5716               ;; Gnus believes possible.  So we re-activate this
5717               ;; group as well.  This might mean doing the
5718               ;; crossposting thingy will *increase* the number
5719               ;; of articles in some groups.  Tsk, tsk.
5720               (setq active (or (gnus-activate-group group) active)))
5721             (when (or (> id (cdr active))
5722                       (< id (car active)))
5723               (setq articles (delq id articles))))))
5724       ;; If the read list is nil, we init it.
5725       (if (and active
5726                (null (gnus-info-read info))
5727                (> (car active) 1))
5728           (setq ninfo (cons 1 (1- (car active))))
5729         (setq ninfo (gnus-info-read info)))
5730       ;; Then we add the read articles to the range.
5731       (gnus-add-to-range
5732        ninfo (setq articles (sort articles '<))))))
5733
5734 (defun gnus-group-make-articles-read (group articles)
5735   "Update the info of GROUP to say that ARTICLES are read."
5736   (let* ((num 0)
5737          (entry (gnus-group-entry group))
5738          (info (nth 2 entry))
5739          (active (gnus-active group))
5740          range)
5741     (when entry
5742       (setq range (gnus-compute-read-articles group articles))
5743       (with-current-buffer gnus-group-buffer
5744         (gnus-undo-register
5745           `(progn
5746              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5747              (gnus-info-set-read ',info ',(gnus-info-read info))
5748              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5749              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5750              (gnus-group-update-group ,group t))))
5751       ;; Add the read articles to the range.
5752       (gnus-info-set-read info range)
5753       (gnus-request-set-mark group (list (list range 'add '(read))))
5754       ;; Then we have to re-compute how many unread
5755       ;; articles there are in this group.
5756       (when active
5757         (cond
5758          ((not range)
5759           (setq num (- (1+ (cdr active)) (car active))))
5760          ((not (listp (cdr range)))
5761           (setq num (- (cdr active) (- (1+ (cdr range))
5762                                        (car range)))))
5763          (t
5764           (while range
5765             (if (numberp (car range))
5766                 (setq num (1+ num))
5767               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5768             (setq range (cdr range)))
5769           (setq num (- (cdr active) num))))
5770         ;; Update the number of unread articles.
5771         (setcar entry num)
5772         ;; Update the group buffer.
5773         (unless (gnus-ephemeral-group-p group)
5774           (gnus-group-update-group group t))))))
5775
5776 (defvar gnus-newsgroup-none-id 0)
5777
5778 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5779   (let ((cur nntp-server-buffer)
5780         (dependencies
5781          (or dependencies
5782              (with-current-buffer gnus-summary-buffer
5783                gnus-newsgroup-dependencies)))
5784         headers id end ref number
5785         (mail-parse-charset gnus-newsgroup-charset)
5786         (mail-parse-ignored-charsets
5787          (save-excursion (condition-case nil
5788                              (set-buffer gnus-summary-buffer)
5789                            (error))
5790                          gnus-newsgroup-ignored-charsets)))
5791     (save-excursion
5792       (set-buffer nntp-server-buffer)
5793       ;; Translate all TAB characters into SPACE characters.
5794       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5795       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5796       (ietf-drums-unfold-fws)
5797       (gnus-run-hooks 'gnus-parse-headers-hook)
5798       (let ((case-fold-search t)
5799             in-reply-to header p lines chars ctype)
5800         (goto-char (point-min))
5801         ;; Search to the beginning of the next header.  Error messages
5802         ;; do not begin with 2 or 3.
5803         (while (re-search-forward "^[23][0-9]+ " nil t)
5804           (setq id nil
5805                 ref nil)
5806           ;; This implementation of this function, with nine
5807           ;; search-forwards instead of the one re-search-forward and
5808           ;; a case (which basically was the old function) is actually
5809           ;; about twice as fast, even though it looks messier.  You
5810           ;; can't have everything, I guess.  Speed and elegance
5811           ;; doesn't always go hand in hand.
5812           (setq
5813            header
5814            (make-full-mail-header
5815             ;; Number.
5816             (prog1
5817                 (setq number (read cur))
5818               (end-of-line)
5819               (setq p (point))
5820               (narrow-to-region (point)
5821                                 (or (and (search-forward "\n.\n" nil t)
5822                                          (- (point) 2))
5823                                     (point))))
5824             ;; Subject.
5825             (progn
5826               (goto-char p)
5827               (if (search-forward "\nsubject:" nil t)
5828                   (nnheader-header-value)
5829                 "(none)"))
5830             ;; From.
5831             (progn
5832               (goto-char p)
5833               (if (search-forward "\nfrom:" nil t)
5834                   (nnheader-header-value)
5835                 "(nobody)"))
5836             ;; Date.
5837             (progn
5838               (goto-char p)
5839               (if (search-forward "\ndate:" nil t)
5840                   (nnheader-header-value) ""))
5841             ;; Message-ID.
5842             (progn
5843               (goto-char p)
5844               (setq id (if (re-search-forward
5845                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5846                            ;; We do it this way to make sure the Message-ID
5847                            ;; is (somewhat) syntactically valid.
5848                            (buffer-substring (match-beginning 1)
5849                                              (match-end 1))
5850                          ;; If there was no message-id, we just fake one
5851                          ;; to make subsequent routines simpler.
5852                          (nnheader-generate-fake-message-id number))))
5853             ;; References.
5854             (progn
5855               (goto-char p)
5856               (if (search-forward "\nreferences:" nil t)
5857                   (progn
5858                     (setq end (point))
5859                     (prog1
5860                         (nnheader-header-value)
5861                       (setq ref
5862                             (buffer-substring
5863                              (progn
5864                                ;; (end-of-line)
5865                                (search-backward ">" end t)
5866                                (1+ (point)))
5867                              (progn
5868                                (search-backward "<" end t)
5869                                (point))))))
5870                 ;; Get the references from the in-reply-to header if there
5871                 ;; were no references and the in-reply-to header looks
5872                 ;; promising.
5873                 (if (and (search-forward "\nin-reply-to:" nil t)
5874                          (setq in-reply-to (nnheader-header-value))
5875                          (string-match "<[^>]+>" in-reply-to))
5876                     (let (ref2)
5877                       (setq ref (substring in-reply-to (match-beginning 0)
5878                                            (match-end 0)))
5879                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5880                         (setq ref2 (substring in-reply-to (match-beginning 0)
5881                                               (match-end 0)))
5882                         (when (> (length ref2) (length ref))
5883                           (setq ref ref2)))
5884                       ref)
5885                   (setq ref nil))))
5886             ;; Chars.
5887             (progn
5888               (goto-char p)
5889               (if (search-forward "\nchars: " nil t)
5890                   (if (numberp (setq chars (ignore-errors (read cur))))
5891                       chars -1)
5892                 -1))
5893             ;; Lines.
5894             (progn
5895               (goto-char p)
5896               (if (search-forward "\nlines: " nil t)
5897                   (if (numberp (setq lines (ignore-errors (read cur))))
5898                       lines -1)
5899                 -1))
5900             ;; Xref.
5901             (progn
5902               (goto-char p)
5903               (and (search-forward "\nxref:" nil t)
5904                    (nnheader-header-value)))
5905             ;; Extra.
5906             (when gnus-extra-headers
5907               (let ((extra gnus-extra-headers)
5908                     out)
5909                 (while extra
5910                   (goto-char p)
5911                   (when (search-forward
5912                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5913                     (push (cons (car extra) (nnheader-header-value)) out))
5914                   (pop extra))
5915                 out))))
5916           (goto-char p)
5917           (if (and (search-forward "\ncontent-type: " nil t)
5918                    (setq ctype (nnheader-header-value)))
5919               (mime-entity-set-content-type-internal
5920                header (mime-parse-Content-Type ctype)))
5921           (when (equal id ref)
5922             (setq ref nil))
5923
5924           (when gnus-alter-header-function
5925             (funcall gnus-alter-header-function header)
5926             (setq id (mail-header-id header)
5927                   ref (gnus-parent-id (mail-header-references header))))
5928
5929           (when (setq header
5930                       (gnus-dependencies-add-header
5931                        header dependencies force-new))
5932             (push header headers))
5933           (goto-char (point-max))
5934           (widen))
5935         (nreverse headers)))))
5936
5937 ;; Goes through the xover lines and returns a list of vectors
5938 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5939                                                   force-new dependencies
5940                                                   group also-fetch-heads)
5941   "Parse the news overview data in the server buffer.
5942 Return a list of headers that match SEQUENCE (see
5943 `nntp-retrieve-headers')."
5944   ;; Get the Xref when the users reads the articles since most/some
5945   ;; NNTP servers do not include Xrefs when using XOVER.
5946   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5947   (let ((mail-parse-charset gnus-newsgroup-charset)
5948         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5949         (cur nntp-server-buffer)
5950         (dependencies (or dependencies gnus-newsgroup-dependencies))
5951         (allp (cond
5952                ((eq gnus-read-all-available-headers t)
5953                 t)
5954                ((stringp gnus-read-all-available-headers)
5955                 (string-match gnus-read-all-available-headers group))
5956                (t
5957                 nil)))
5958         number headers header)
5959     (save-excursion
5960       (set-buffer nntp-server-buffer)
5961       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5962       ;; Allow the user to mangle the headers before parsing them.
5963       (gnus-run-hooks 'gnus-parse-headers-hook)
5964       (goto-char (point-min))
5965       (gnus-parse-without-error
5966         (while (and (or sequence allp)
5967                     (not (eobp)))
5968           (setq number (read cur))
5969           (when (not allp)
5970             (while (and sequence
5971                         (< (car sequence) number))
5972               (setq sequence (cdr sequence))))
5973           (when (and (or allp
5974                          (and sequence
5975                               (eq number (car sequence))))
5976                      (progn
5977                        (setq sequence (cdr sequence))
5978                        (setq header (inline
5979                                       (gnus-nov-parse-line
5980                                        number dependencies force-new)))))
5981             (push header headers))
5982           (forward-line 1)))
5983       ;; A common bug in inn is that if you have posted an article and
5984       ;; then retrieves the active file, it will answer correctly --
5985       ;; the new article is included.  However, a NOV entry for the
5986       ;; article may not have been generated yet, so this may fail.
5987       ;; We work around this problem by retrieving the last few
5988       ;; headers using HEAD.
5989       (if (or (not also-fetch-heads)
5990               (not sequence))
5991           ;; We (probably) got all the headers.
5992           (nreverse headers)
5993         (let ((gnus-nov-is-evil t))
5994           (nconc
5995            (nreverse headers)
5996            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5997              (gnus-get-newsgroup-headers))))))))
5998
5999 (defun gnus-article-get-xrefs ()
6000   "Fill in the Xref value in `gnus-current-headers', if necessary.
6001 This is meant to be called in `gnus-article-internal-prepare-hook'."
6002   (let ((headers (with-current-buffer gnus-summary-buffer
6003                    gnus-current-headers)))
6004     (or (not gnus-use-cross-reference)
6005         (not headers)
6006         (and (mail-header-xref headers)
6007              (not (string= (mail-header-xref headers) "")))
6008         (let ((case-fold-search t)
6009               xref)
6010           (save-restriction
6011             (nnheader-narrow-to-headers)
6012             (goto-char (point-min))
6013             (when (or (and (not (eobp))
6014                            (eq (downcase (char-after)) ?x)
6015                            (looking-at "Xref:"))
6016                       (search-forward "\nXref:" nil t))
6017               (goto-char (1+ (match-end 0)))
6018               (setq xref (buffer-substring (point) (point-at-eol)))
6019               (mail-header-set-xref headers xref)))))))
6020
6021 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6022   "Find article ID and insert the summary line for that article.
6023 OLD-HEADER can either be a header or a line number to insert
6024 the subject line on."
6025   (let* ((line (and (numberp old-header) old-header))
6026          (old-header (and (vectorp old-header) old-header))
6027          (header (cond ((and old-header use-old-header)
6028                         old-header)
6029                        ((and (numberp id)
6030                              (gnus-number-to-header id))
6031                         (gnus-number-to-header id))
6032                        (t
6033                         (gnus-read-header id))))
6034          (number (and (numberp id) id))
6035          d)
6036     (when header
6037       ;; Rebuild the thread that this article is part of and go to the
6038       ;; article we have fetched.
6039       (when (and (not gnus-show-threads)
6040                  old-header)
6041         (when (and number
6042                    (setq d (gnus-data-find (mail-header-number old-header))))
6043           (goto-char (gnus-data-pos d))
6044           (gnus-data-remove
6045            number
6046            (- (point-at-bol)
6047               (prog1
6048                   (1+ (point-at-eol))
6049                 (gnus-delete-line))))))
6050       ;; Remove list identifiers from subject.
6051       (when gnus-list-identifiers
6052         (let ((gnus-newsgroup-headers (list header)))
6053           (gnus-summary-remove-list-identifiers)
6054           (setq header (car gnus-newsgroup-headers))))
6055       (when old-header
6056         (mail-header-set-number header (mail-header-number old-header)))
6057       (setq gnus-newsgroup-sparse
6058             (delq (setq number (mail-header-number header))
6059                   gnus-newsgroup-sparse))
6060       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6061       (push number gnus-newsgroup-limit)
6062       (gnus-rebuild-thread (mail-header-id header) line)
6063       (gnus-summary-goto-subject number nil t))
6064     (when (and (numberp number)
6065                (> number 0))
6066       ;; We have to update the boundaries even if we can't fetch the
6067       ;; article if ID is a number -- so that the next `P' or `N'
6068       ;; command will fetch the previous (or next) article even
6069       ;; if the one we tried to fetch this time has been canceled.
6070       (when (> number gnus-newsgroup-end)
6071         (setq gnus-newsgroup-end number))
6072       (when (< number gnus-newsgroup-begin)
6073         (setq gnus-newsgroup-begin number))
6074       (setq gnus-newsgroup-unselected
6075             (delq number gnus-newsgroup-unselected)))
6076     ;; Report back a success?
6077     (and header (mail-header-number header))))
6078
6079 ;;; Process/prefix in the summary buffer
6080
6081 (defun gnus-summary-work-articles (n)
6082   "Return a list of articles to be worked upon.
6083 The prefix argument, the list of process marked articles, and the
6084 current article will be taken into consideration."
6085   (save-excursion
6086     (set-buffer gnus-summary-buffer)
6087     (cond
6088      (n
6089       ;; A numerical prefix has been given.
6090       (setq n (prefix-numeric-value n))
6091       (let ((backward (< n 0))
6092             (n (abs (prefix-numeric-value n)))
6093             articles article)
6094         (save-excursion
6095           (while
6096               (and (> n 0)
6097                    (push (setq article (gnus-summary-article-number))
6098                          articles)
6099                    (if backward
6100                        (gnus-summary-find-prev nil article)
6101                      (gnus-summary-find-next nil article)))
6102             (decf n)))
6103         (nreverse articles)))
6104      ((and (gnus-region-active-p) (mark))
6105       (message "region active")
6106       ;; Work on the region between point and mark.
6107       (let ((max (max (point) (mark)))
6108             articles article)
6109         (save-excursion
6110           (goto-char (min (point) (mark)))
6111           (while
6112               (and
6113                (push (setq article (gnus-summary-article-number)) articles)
6114                (gnus-summary-find-next nil article)
6115                (< (point) max)))
6116           (nreverse articles))))
6117      (gnus-newsgroup-processable
6118       ;; There are process-marked articles present.
6119       ;; Save current state.
6120       (gnus-summary-save-process-mark)
6121       ;; Return the list.
6122       (reverse gnus-newsgroup-processable))
6123      (t
6124       ;; Just return the current article.
6125       (list (gnus-summary-article-number))))))
6126
6127 (defmacro gnus-summary-iterate (arg &rest forms)
6128   "Iterate over the process/prefixed articles and do FORMS.
6129 ARG is the interactive prefix given to the command.  FORMS will be
6130 executed with point over the summary line of the articles."
6131   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6132     `(let ((,articles (gnus-summary-work-articles ,arg)))
6133        (while ,articles
6134          (gnus-summary-goto-subject (car ,articles))
6135          ,@forms
6136          (pop ,articles)))))
6137
6138 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6139 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6140
6141 (defun gnus-summary-save-process-mark ()
6142   "Push the current set of process marked articles on the stack."
6143   (interactive)
6144   (push (copy-sequence gnus-newsgroup-processable)
6145         gnus-newsgroup-process-stack))
6146
6147 (defun gnus-summary-kill-process-mark ()
6148   "Push the current set of process marked articles on the stack and unmark."
6149   (interactive)
6150   (gnus-summary-save-process-mark)
6151   (gnus-summary-unmark-all-processable))
6152
6153 (defun gnus-summary-yank-process-mark ()
6154   "Pop the last process mark state off the stack and restore it."
6155   (interactive)
6156   (unless gnus-newsgroup-process-stack
6157     (error "Empty mark stack"))
6158   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6159
6160 (defun gnus-summary-process-mark-set (set)
6161   "Make SET into the current process marked articles."
6162   (gnus-summary-unmark-all-processable)
6163   (while set
6164     (gnus-summary-set-process-mark (pop set))))
6165
6166 ;;; Searching and stuff
6167
6168 (defun gnus-summary-search-group (&optional backward use-level)
6169   "Search for next unread newsgroup.
6170 If optional argument BACKWARD is non-nil, search backward instead."
6171   (save-excursion
6172     (set-buffer gnus-group-buffer)
6173     (when (gnus-group-search-forward
6174            backward nil (if use-level (gnus-group-group-level) nil))
6175       (gnus-group-group-name))))
6176
6177 (defun gnus-summary-best-group (&optional exclude-group)
6178   "Find the name of the best unread group.
6179 If EXCLUDE-GROUP, do not go to this group."
6180   (with-current-buffer gnus-group-buffer
6181     (save-excursion
6182       (gnus-group-best-unread-group exclude-group))))
6183
6184 (defun gnus-summary-find-next (&optional unread article backward)
6185   (if backward
6186       (gnus-summary-find-prev unread article)
6187     (let* ((dummy (gnus-summary-article-intangible-p))
6188            (article (or article (gnus-summary-article-number)))
6189            (data (gnus-data-find-list article))
6190            result)
6191       (when (and (not dummy)
6192                  (or (not gnus-summary-check-current)
6193                      (not unread)
6194                      (not (gnus-data-unread-p (car data)))))
6195         (setq data (cdr data)))
6196       (when (setq result
6197                   (if unread
6198                       (progn
6199                         (while data
6200                           (unless (memq (gnus-data-number (car data))
6201                                         (cond
6202                                          ((eq gnus-auto-goto-ignores
6203                                               'always-undownloaded)
6204                                           gnus-newsgroup-undownloaded)
6205                                          (gnus-plugged
6206                                           nil)
6207                                          ((eq gnus-auto-goto-ignores
6208                                               'unfetched)
6209                                           gnus-newsgroup-unfetched)
6210                                          ((eq gnus-auto-goto-ignores
6211                                               'undownloaded)
6212                                           gnus-newsgroup-undownloaded)))
6213                             (when (gnus-data-unread-p (car data))
6214                               (setq result (car data)
6215                                     data nil)))
6216                           (setq data (cdr data)))
6217                         result)
6218                     (car data)))
6219         (goto-char (gnus-data-pos result))
6220         (gnus-data-number result)))))
6221
6222 (defun gnus-summary-find-prev (&optional unread article)
6223   (let* ((eobp (eobp))
6224          (article (or article (gnus-summary-article-number)))
6225          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6226          result)
6227     (when (and (not eobp)
6228                (or (not gnus-summary-check-current)
6229                    (not unread)
6230                    (not (gnus-data-unread-p (car data)))))
6231       (setq data (cdr data)))
6232     (when (setq result
6233                 (if unread
6234                     (progn
6235                       (while data
6236                         (unless (memq (gnus-data-number (car data))
6237                                       (cond
6238                                        ((eq gnus-auto-goto-ignores
6239                                             'always-undownloaded)
6240                                         gnus-newsgroup-undownloaded)
6241                                        (gnus-plugged
6242                                         nil)
6243                                        ((eq gnus-auto-goto-ignores
6244                                             'unfetched)
6245                                         gnus-newsgroup-unfetched)
6246                                        ((eq gnus-auto-goto-ignores
6247                                             'undownloaded)
6248                                         gnus-newsgroup-undownloaded)))
6249                           (when (gnus-data-unread-p (car data))
6250                             (setq result (car data)
6251                                   data nil)))
6252                         (setq data (cdr data)))
6253                       result)
6254                   (car data)))
6255       (goto-char (gnus-data-pos result))
6256       (gnus-data-number result))))
6257
6258 (defun gnus-summary-find-subject (subject &optional unread backward article)
6259   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6260          (article (or article (gnus-summary-article-number)))
6261          (articles (gnus-data-list backward))
6262          (arts (gnus-data-find-list article articles))
6263          result)
6264     (when (or (not gnus-summary-check-current)
6265               (not unread)
6266               (not (gnus-data-unread-p (car arts))))
6267       (setq arts (cdr arts)))
6268     (while arts
6269       (and (or (not unread)
6270                (gnus-data-unread-p (car arts)))
6271            (vectorp (gnus-data-header (car arts)))
6272            (gnus-subject-equal
6273             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6274            (setq result (car arts)
6275                  arts nil))
6276       (setq arts (cdr arts)))
6277     (and result
6278          (goto-char (gnus-data-pos result))
6279          (gnus-data-number result))))
6280
6281 (defun gnus-summary-search-forward (&optional unread subject backward)
6282   "Search forward for an article.
6283 If UNREAD, look for unread articles.  If SUBJECT, look for
6284 articles with that subject.  If BACKWARD, search backward instead."
6285   (cond (subject (gnus-summary-find-subject subject unread backward))
6286         (backward (gnus-summary-find-prev unread))
6287         (t (gnus-summary-find-next unread))))
6288
6289 (defun gnus-recenter (&optional n)
6290   "Center point in window and redisplay frame.
6291 Also do horizontal recentering."
6292   (interactive "P")
6293   (when (and gnus-auto-center-summary
6294              (not (eq gnus-auto-center-summary 'vertical)))
6295     (gnus-horizontal-recenter))
6296   (recenter n))
6297
6298 (defun gnus-summary-recenter ()
6299   "Center point in the summary window.
6300 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6301 displayed, no centering will be performed."
6302   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6303   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6304   (interactive)
6305   ;; The user has to want it.
6306   (when gnus-auto-center-summary
6307     (let* ((top (cond ((< (window-height) 4) 0)
6308                       ((< (window-height) 7) 1)
6309                       (t (if (numberp gnus-auto-center-summary)
6310                              gnus-auto-center-summary
6311                            (/ (1- (window-height)) 2)))))
6312            (height (1- (window-height)))
6313            (bottom (save-excursion (goto-char (point-max))
6314                                    (forward-line (- height))
6315                                    (point)))
6316            (window (get-buffer-window (current-buffer))))
6317       (when (get-buffer-window gnus-article-buffer)
6318         ;; Only do recentering when the article buffer is displayed,
6319         ;; Set the window start to either `bottom', which is the biggest
6320         ;; possible valid number, or the second line from the top,
6321         ;; whichever is the least.
6322         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6323           (if (> bottom top-pos)
6324               ;; Keep the second line from the top visible
6325               (set-window-start window top-pos t)
6326             ;; Try to keep the bottom line visible; if it's partially
6327             ;; obscured, either scroll one more line to make it fully
6328             ;; visible, or revert to using TOP-POS.
6329             (save-excursion
6330               (goto-char (point-max))
6331               (forward-line -1)
6332               (let ((last-line-start (point)))
6333                 (goto-char bottom)
6334                 (set-window-start window (point) t)
6335                 (when (not (pos-visible-in-window-p last-line-start window))
6336                   (forward-line 1)
6337                   (set-window-start window (min (point) top-pos) t)))))))
6338       ;; Do horizontal recentering while we're at it.
6339       (when (and (get-buffer-window (current-buffer) t)
6340                  (not (eq gnus-auto-center-summary 'vertical)))
6341         (let ((selected (selected-window)))
6342           (select-window (get-buffer-window (current-buffer) t))
6343           (gnus-summary-position-point)
6344           (gnus-horizontal-recenter)
6345           (select-window selected))))))
6346
6347 (defun gnus-summary-jump-to-group (newsgroup)
6348   "Move point to NEWSGROUP in group mode buffer."
6349   ;; Keep update point of group mode buffer if visible.
6350   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6351       (save-window-excursion
6352         ;; Take care of tree window mode.
6353         (when (get-buffer-window gnus-group-buffer)
6354           (pop-to-buffer gnus-group-buffer))
6355         (gnus-group-jump-to-group newsgroup))
6356     (save-excursion
6357       ;; Take care of tree window mode.
6358       (if (get-buffer-window gnus-group-buffer)
6359           (pop-to-buffer gnus-group-buffer)
6360         (set-buffer gnus-group-buffer))
6361       (gnus-group-jump-to-group newsgroup))))
6362
6363 ;; This function returns a list of article numbers based on the
6364 ;; difference between the ranges of read articles in this group and
6365 ;; the range of active articles.
6366 (defun gnus-list-of-unread-articles (group)
6367   (let* ((read (gnus-info-read (gnus-get-info group)))
6368          (active (or (gnus-active group) (gnus-activate-group group)))
6369          (last (cdr active))
6370          first nlast unread)
6371     ;; If none are read, then all are unread.
6372     (if (not read)
6373         (setq first (car active))
6374       ;; If the range of read articles is a single range, then the
6375       ;; first unread article is the article after the last read
6376       ;; article.  Sounds logical, doesn't it?
6377       (if (and (not (listp (cdr read)))
6378                (or (< (car read) (car active))
6379                    (progn (setq read (list read))
6380                           nil)))
6381           (setq first (max (car active) (1+ (cdr read))))
6382         ;; `read' is a list of ranges.
6383         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6384                                   (caar read)))
6385                   1)
6386           (setq first (car active)))
6387         (while read
6388           (when first
6389             (while (< first nlast)
6390               (setq unread (cons first unread)
6391                     first (1+ first))))
6392           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6393           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6394           (setq read (cdr read)))))
6395     ;; And add the last unread articles.
6396     (while (<= first last)
6397       (setq unread (cons first unread)
6398             first (1+ first)))
6399     ;; Return the list of unread articles.
6400     (delq 0 (nreverse unread))))
6401
6402 (defun gnus-list-of-read-articles (group)
6403   "Return a list of unread, unticked and non-dormant articles."
6404   (let* ((info (gnus-get-info group))
6405          (marked (gnus-info-marks info))
6406          (active (gnus-active group)))
6407     (and info active
6408          (gnus-list-range-difference
6409           (gnus-list-range-difference
6410            (gnus-sorted-complement
6411             (gnus-uncompress-range active)
6412             (gnus-list-of-unread-articles group))
6413            (cdr (assq 'dormant marked)))
6414           (cdr (assq 'tick marked))))))
6415
6416 ;; This function returns a sequence of article numbers based on the
6417 ;; difference between the ranges of read articles in this group and
6418 ;; the range of active articles.
6419 (defun gnus-sequence-of-unread-articles (group)
6420   (let* ((read (gnus-info-read (gnus-get-info group)))
6421          (active (or (gnus-active group) (gnus-activate-group group)))
6422          (last (cdr active))
6423          first nlast unread)
6424     ;; If none are read, then all are unread.
6425     (if (not read)
6426         (setq first (car active))
6427       ;; If the range of read articles is a single range, then the
6428       ;; first unread article is the article after the last read
6429       ;; article.  Sounds logical, doesn't it?
6430       (if (and (not (listp (cdr read)))
6431                (or (< (car read) (car active))
6432                    (progn (setq read (list read))
6433                           nil)))
6434           (setq first (max (car active) (1+ (cdr read))))
6435         ;; `read' is a list of ranges.
6436         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6437                                   (caar read)))
6438                   1)
6439           (setq first (car active)))
6440         (while read
6441           (when first
6442             (push (cons first nlast) unread))
6443           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6444           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6445           (setq read (cdr read)))))
6446     ;; And add the last unread articles.
6447     (cond ((< first last)
6448            (push (cons first last) unread))
6449           ((= first last)
6450            (push first unread)))
6451     ;; Return the sequence of unread articles.
6452     (delq 0 (nreverse unread))))
6453
6454 ;; Various summary commands
6455
6456 (defun gnus-summary-select-article-buffer ()
6457   "Reconfigure windows to show article buffer."
6458   (interactive)
6459   (if (not (gnus-buffer-live-p gnus-article-buffer))
6460       (error "There is no article buffer for this summary buffer")
6461     (gnus-configure-windows 'article)
6462     (select-window (get-buffer-window gnus-article-buffer))))
6463
6464 (defun gnus-summary-universal-argument (arg)
6465   "Perform any operation on all articles that are process/prefixed."
6466   (interactive "P")
6467   (let ((articles (gnus-summary-work-articles arg))
6468         func article)
6469     (if (eq
6470          (setq
6471           func
6472           (key-binding
6473            (read-key-sequence
6474             (substitute-command-keys
6475              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6476          'undefined)
6477         (gnus-error 1 "Undefined key")
6478       (save-excursion
6479         (while articles
6480           (gnus-summary-goto-subject (setq article (pop articles)))
6481           (let (gnus-newsgroup-processable)
6482             (command-execute func))
6483           (gnus-summary-remove-process-mark article)))))
6484   (gnus-summary-position-point))
6485
6486 (defun gnus-summary-toggle-truncation (&optional arg)
6487   "Toggle truncation of summary lines.
6488 With ARG, turn line truncation on if ARG is positive."
6489   (interactive "P")
6490   (setq truncate-lines
6491         (if (null arg) (not truncate-lines)
6492           (> (prefix-numeric-value arg) 0)))
6493   (redraw-display))
6494
6495 (defun gnus-summary-find-for-reselect ()
6496   "Return the number of an article to stay on across a reselect.
6497 The current article is considered, then following articles, then previous
6498 articles.  An article is sought which is not cancelled and isn't a temporary
6499 insertion from another group.  If there's no such then return a dummy 0."
6500   (let (found)
6501     (dolist (rev '(nil t))
6502       (unless found      ; don't demand the reverse list if we don't need it
6503         (let ((data (gnus-data-find-list
6504                      (gnus-summary-article-number) (gnus-data-list rev))))
6505           (while (and data (not found))
6506             (if (and (< 0 (gnus-data-number (car data)))
6507                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6508                 (setq found (gnus-data-number (car data))))
6509             (setq data (cdr data))))))
6510     (or found 0)))
6511
6512 (defun gnus-summary-reselect-current-group (&optional all rescan)
6513   "Exit and then reselect the current newsgroup.
6514 The prefix argument ALL means to select all articles."
6515   (interactive "P")
6516   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6517     (error "Ephemeral groups can't be reselected"))
6518   (let ((current-subject (gnus-summary-find-for-reselect))
6519         (group gnus-newsgroup-name))
6520     (setq gnus-newsgroup-begin nil)
6521     (gnus-summary-exit nil 'leave-hidden)
6522     ;; We have to adjust the point of group mode buffer because
6523     ;; point was moved to the next unread newsgroup by exiting.
6524     (gnus-summary-jump-to-group group)
6525     (when rescan
6526       (save-excursion
6527         (save-window-excursion
6528           ;; Don't show group contents.
6529           (set-window-start (selected-window) (point-max))
6530           (gnus-group-get-new-news-this-group 1))))
6531     (gnus-group-read-group all t)
6532     (gnus-summary-goto-subject current-subject nil t)))
6533
6534 (defun gnus-summary-rescan-group (&optional all)
6535   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6536   (interactive "P")
6537   (gnus-summary-reselect-current-group all t))
6538
6539 (defun gnus-summary-update-info (&optional non-destructive)
6540   (save-excursion
6541     (let ((group gnus-newsgroup-name))
6542       (when group
6543         (when gnus-newsgroup-kill-headers
6544           (setq gnus-newsgroup-killed
6545                 (gnus-compress-sequence
6546                  (gnus-sorted-union
6547                   (gnus-list-range-intersection
6548                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6549                   gnus-newsgroup-unreads)
6550                  t)))
6551         (unless (listp (cdr gnus-newsgroup-killed))
6552           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6553         (let ((headers gnus-newsgroup-headers))
6554           ;; Set the new ranges of read articles.
6555           (with-current-buffer gnus-group-buffer
6556             (gnus-undo-force-boundary))
6557           (gnus-update-read-articles
6558            group (gnus-sorted-union
6559                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6560           ;; Set the current article marks.
6561           (let ((gnus-newsgroup-scored
6562                  (if (and (not gnus-save-score)
6563                           (not non-destructive))
6564                      nil
6565                    gnus-newsgroup-scored)))
6566             (save-excursion
6567               (gnus-update-marks)))
6568           ;; Do the cross-ref thing.
6569           (when gnus-use-cross-reference
6570             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6571           ;; Do not switch windows but change the buffer to work.
6572           (set-buffer gnus-group-buffer)
6573           (unless (gnus-ephemeral-group-p group)
6574             (gnus-group-update-group group)))))))
6575
6576 (defun gnus-summary-save-newsrc (&optional force)
6577   "Save the current number of read/marked articles in the dribble buffer.
6578 The dribble buffer will then be saved.
6579 If FORCE (the prefix), also save the .newsrc file(s)."
6580   (interactive "P")
6581   (gnus-summary-update-info t)
6582   (if force
6583       (gnus-save-newsrc-file)
6584     (gnus-dribble-save)))
6585
6586 (defun gnus-summary-exit (&optional temporary leave-hidden)
6587   "Exit reading current newsgroup, and then return to group selection mode.
6588 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6589   (interactive)
6590   (gnus-set-global-variables)
6591   (gnus-kill-save-kill-buffer)
6592   (gnus-async-halt-prefetch)
6593   (let* ((group gnus-newsgroup-name)
6594          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6595          (gnus-group-is-exiting-p t)
6596          (mode major-mode)
6597          (group-point nil)
6598          (buf (current-buffer)))
6599     (unless quit-config
6600       ;; Do adaptive scoring, and possibly save score files.
6601       (when gnus-newsgroup-adaptive
6602         (gnus-score-adaptive))
6603       (when gnus-use-scoring
6604         (gnus-score-save)))
6605     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6606     ;; If we have several article buffers, we kill them at exit.
6607     (unless gnus-single-article-buffer
6608       (gnus-kill-buffer gnus-original-article-buffer)
6609       (setq gnus-article-current nil))
6610     (when gnus-use-cache
6611       (gnus-cache-possibly-remove-articles)
6612       (gnus-cache-save-buffers))
6613     (gnus-async-prefetch-remove-group group)
6614     (when gnus-suppress-duplicates
6615       (gnus-dup-enter-articles))
6616     (when gnus-use-trees
6617       (gnus-tree-close group))
6618     (when gnus-use-cache
6619       (gnus-cache-write-active))
6620     ;; Remove entries for this group.
6621     (nnmail-purge-split-history (gnus-group-real-name group))
6622     ;; Make all changes in this group permanent.
6623     (unless quit-config
6624       (gnus-run-hooks 'gnus-exit-group-hook)
6625       (gnus-summary-update-info))
6626     (gnus-close-group group)
6627     ;; Make sure where we were, and go to next newsgroup.
6628     (set-buffer gnus-group-buffer)
6629     (unless quit-config
6630       (gnus-group-jump-to-group group))
6631     (gnus-run-hooks 'gnus-summary-exit-hook)
6632     (unless (or quit-config
6633                 ;; If this group has disappeared from the summary
6634                 ;; buffer, don't skip forwards.
6635                 (not (string= group (gnus-group-group-name))))
6636       (gnus-group-next-unread-group 1))
6637     (setq group-point (point))
6638     (if temporary
6639         nil                             ;Nothing to do.
6640       ;; If we have several article buffers, we kill them at exit.
6641       (unless gnus-single-article-buffer
6642         (gnus-kill-buffer gnus-article-buffer)
6643         (gnus-kill-buffer gnus-original-article-buffer)
6644         (setq gnus-article-current nil))
6645       (set-buffer buf)
6646       (if (not gnus-kill-summary-on-exit)
6647           (progn
6648             (gnus-deaden-summary)
6649             (setq mode nil))
6650         ;; We set all buffer-local variables to nil.  It is unclear why
6651         ;; this is needed, but if we don't, buffer-local variables are
6652         ;; not garbage-collected, it seems.  This would the lead to en
6653         ;; ever-growing Emacs.
6654         (gnus-summary-clear-local-variables)
6655         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6656           (gnus-summary-clear-local-variables))
6657         (when (get-buffer gnus-article-buffer)
6658           (bury-buffer gnus-article-buffer))
6659         ;; We clear the global counterparts of the buffer-local
6660         ;; variables as well, just to be on the safe side.
6661         (set-buffer gnus-group-buffer)
6662         (gnus-summary-clear-local-variables)
6663         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6664           (gnus-summary-clear-local-variables))
6665         ;; Return to group mode buffer.
6666         (when (eq mode 'gnus-summary-mode)
6667           (gnus-kill-buffer buf)))
6668       (setq gnus-current-select-method gnus-select-method)
6669       (if leave-hidden
6670           (set-buffer gnus-group-buffer)
6671         (pop-to-buffer gnus-group-buffer))
6672       (if (not quit-config)
6673           (progn
6674             (goto-char group-point)
6675             (unless leave-hidden
6676               (gnus-configure-windows 'group 'force))
6677             (unless (pos-visible-in-window-p)
6678               (forward-line (/ (static-if (featurep 'xemacs)
6679                                    (window-displayed-height)
6680                                  (1- (window-height)))
6681                                -2))
6682               (set-window-start (selected-window) (point))
6683               (goto-char group-point)))
6684         (gnus-handle-ephemeral-exit quit-config))
6685       ;; Clear the current group name.
6686       (unless quit-config
6687         (setq gnus-newsgroup-name nil)))))
6688
6689 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6690 (defun gnus-summary-exit-no-update (&optional no-questions)
6691   "Quit reading current newsgroup without updating read article info."
6692   (interactive)
6693   (let* ((group gnus-newsgroup-name)
6694          (gnus-group-is-exiting-p t)
6695          (gnus-group-is-exiting-without-update-p t)
6696          (quit-config (gnus-group-quit-config group)))
6697     (when (or no-questions
6698               gnus-expert-user
6699               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6700       (gnus-async-halt-prefetch)
6701       (run-hooks 'gnus-summary-prepare-exit-hook)
6702       ;; If we have several article buffers, we kill them at exit.
6703       (unless gnus-single-article-buffer
6704         (gnus-kill-buffer gnus-article-buffer)
6705         (gnus-kill-buffer gnus-original-article-buffer)
6706         (setq gnus-article-current nil))
6707       (if (not gnus-kill-summary-on-exit)
6708           (gnus-deaden-summary)
6709         (gnus-close-group group)
6710         (gnus-summary-clear-local-variables)
6711         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6712           (gnus-summary-clear-local-variables))
6713         (set-buffer gnus-group-buffer)
6714         (gnus-summary-clear-local-variables)
6715         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6716           (gnus-summary-clear-local-variables))
6717         (gnus-kill-buffer gnus-summary-buffer))
6718       (unless gnus-single-article-buffer
6719         (setq gnus-article-current nil))
6720       (when gnus-use-trees
6721         (gnus-tree-close group))
6722       (gnus-async-prefetch-remove-group group)
6723       (when (get-buffer gnus-article-buffer)
6724         (bury-buffer gnus-article-buffer))
6725       ;; Return to the group buffer.
6726       (gnus-configure-windows 'group 'force)
6727       ;; Clear the current group name.
6728       (setq gnus-newsgroup-name nil)
6729       (unless (gnus-ephemeral-group-p group)
6730         (gnus-group-update-group group))
6731       (when (equal (gnus-group-group-name) group)
6732         (gnus-group-next-unread-group 1))
6733       (when quit-config
6734         (gnus-handle-ephemeral-exit quit-config)))))
6735
6736 (defun gnus-handle-ephemeral-exit (quit-config)
6737   "Handle movement when leaving an ephemeral group.
6738 The state which existed when entering the ephemeral is reset."
6739   (if (not (buffer-name (car quit-config)))
6740       (gnus-configure-windows 'group 'force)
6741     (set-buffer (car quit-config))
6742     (cond ((eq major-mode 'gnus-summary-mode)
6743            (gnus-set-global-variables))
6744           ((eq major-mode 'gnus-article-mode)
6745            (save-excursion
6746              ;; The `gnus-summary-buffer' variable may point
6747              ;; to the old summary buffer when using a single
6748              ;; article buffer.
6749              (unless (gnus-buffer-live-p gnus-summary-buffer)
6750                (set-buffer gnus-group-buffer))
6751              (set-buffer gnus-summary-buffer)
6752              (gnus-set-global-variables))))
6753     (if (or (eq (cdr quit-config) 'article)
6754             (eq (cdr quit-config) 'pick))
6755         (progn
6756           ;; The current article may be from the ephemeral group
6757           ;; thus it is best that we reload this article
6758           ;;
6759           ;; If we're exiting from a large digest, this can be
6760           ;; extremely slow.  So, it's better not to reload it. -- jh.
6761           ;;(gnus-summary-show-article)
6762           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6763               (gnus-configure-windows 'pick 'force)
6764             (gnus-configure-windows (cdr quit-config) 'force)))
6765       (gnus-configure-windows (cdr quit-config) 'force))
6766     (when (eq major-mode 'gnus-summary-mode)
6767       (gnus-summary-next-subject 1 nil t)
6768       (gnus-summary-recenter)
6769       (gnus-summary-position-point))))
6770
6771 (defun gnus-summary-preview-mime-message ()
6772   "MIME decode and play this message."
6773   (interactive)
6774   (let ((gnus-break-pages nil)
6775         (gnus-show-mime t))
6776     (gnus-summary-select-article gnus-show-all-headers t))
6777   (let ((w (get-buffer-window gnus-article-buffer)))
6778     (when w
6779       (select-window (get-buffer-window gnus-article-buffer)))))
6780
6781 ;;; Dead summaries.
6782
6783 (defvar gnus-dead-summary-mode-map nil)
6784
6785 (unless gnus-dead-summary-mode-map
6786   (setq gnus-dead-summary-mode-map (make-keymap))
6787   (suppress-keymap gnus-dead-summary-mode-map)
6788   (substitute-key-definition
6789    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6790   (dolist (key '("\C-d" "\r" "\177" [delete]))
6791     (define-key gnus-dead-summary-mode-map
6792       key 'gnus-summary-wake-up-the-dead))
6793   (dolist (key '("q" "Q"))
6794     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6795
6796 (defvar gnus-dead-summary-mode nil
6797   "Minor mode for Gnus summary buffers.")
6798
6799 (defun gnus-dead-summary-mode (&optional arg)
6800   "Minor mode for Gnus summary buffers."
6801   (interactive "P")
6802   (when (eq major-mode 'gnus-summary-mode)
6803     (make-local-variable 'gnus-dead-summary-mode)
6804     (setq gnus-dead-summary-mode
6805           (if (null arg) (not gnus-dead-summary-mode)
6806             (> (prefix-numeric-value arg) 0)))
6807     (when gnus-dead-summary-mode
6808       (add-minor-mode
6809        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6810
6811 (defun gnus-deaden-summary ()
6812   "Make the current summary buffer into a dead summary buffer."
6813   ;; Kill any previous dead summary buffer.
6814   (when (and gnus-dead-summary
6815              (buffer-name gnus-dead-summary))
6816     (with-current-buffer gnus-dead-summary
6817       (when gnus-dead-summary-mode
6818         (kill-buffer (current-buffer)))))
6819   ;; Make this the current dead summary.
6820   (setq gnus-dead-summary (current-buffer))
6821   (gnus-dead-summary-mode 1)
6822   (let ((name (buffer-name)))
6823     (when (string-match "Summary" name)
6824       (rename-buffer
6825        (concat (substring name 0 (match-beginning 0)) "Dead "
6826                (substring name (match-beginning 0)))
6827        t)
6828       (bury-buffer))))
6829
6830 (defun gnus-kill-or-deaden-summary (buffer)
6831   "Kill or deaden the summary BUFFER."
6832   (save-excursion
6833     (when (and (buffer-name buffer)
6834                (not gnus-single-article-buffer))
6835       (with-current-buffer buffer
6836         (gnus-kill-buffer gnus-article-buffer)
6837         (gnus-kill-buffer gnus-original-article-buffer)))
6838     (cond
6839      ;; Kill the buffer.
6840      (gnus-kill-summary-on-exit
6841       (when (and gnus-use-trees
6842                  (gnus-buffer-exists-p buffer))
6843         (save-excursion
6844           (set-buffer buffer)
6845           (gnus-tree-close gnus-newsgroup-name)))
6846       (gnus-kill-buffer buffer))
6847      ;; Deaden the buffer.
6848      ((gnus-buffer-exists-p buffer)
6849       (save-excursion
6850         (set-buffer buffer)
6851         (gnus-deaden-summary))))))
6852
6853 (defun gnus-summary-wake-up-the-dead (&rest args)
6854   "Wake up the dead summary buffer."
6855   (interactive)
6856   (gnus-dead-summary-mode -1)
6857   (let ((name (buffer-name)))
6858     (when (string-match "Dead " name)
6859       (rename-buffer
6860        (concat (substring name 0 (match-beginning 0))
6861                (substring name (match-end 0)))
6862        t)))
6863   (gnus-message 3 "This dead summary is now alive again"))
6864
6865 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6866 (defun gnus-summary-fetch-faq (&optional faq-dir)
6867   "Fetch the FAQ for the current group.
6868 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6869 in."
6870   (interactive
6871    (list
6872     (when current-prefix-arg
6873       (completing-read
6874        "FAQ dir: " (and (listp gnus-group-faq-directory)
6875                         (mapcar 'list
6876                                 gnus-group-faq-directory))))))
6877   (let (gnus-faq-buffer)
6878     (when (setq gnus-faq-buffer
6879                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6880       (gnus-configure-windows 'summary-faq))))
6881
6882 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6883 (defun gnus-summary-describe-group (&optional force)
6884   "Describe the current newsgroup."
6885   (interactive "P")
6886   (gnus-group-describe-group force gnus-newsgroup-name))
6887
6888 (defun gnus-summary-describe-briefly ()
6889   "Describe summary mode commands briefly."
6890   (interactive)
6891   (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")))
6892
6893 ;; Walking around group mode buffer from summary mode.
6894
6895 (defun gnus-summary-next-group (&optional no-article target-group backward)
6896   "Exit current newsgroup and then select next unread newsgroup.
6897 If prefix argument NO-ARTICLE is non-nil, no article is selected
6898 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6899 previous group instead."
6900   (interactive "P")
6901   ;; Stop pre-fetching.
6902   (gnus-async-halt-prefetch)
6903   (let ((current-group gnus-newsgroup-name)
6904         (current-buffer (current-buffer))
6905         entered)
6906     ;; First we semi-exit this group to update Xrefs and all variables.
6907     ;; We can't do a real exit, because the window conf must remain
6908     ;; the same in case the user is prompted for info, and we don't
6909     ;; want the window conf to change before that...
6910     (gnus-summary-exit t)
6911     (while (not entered)
6912       ;; Then we find what group we are supposed to enter.
6913       (set-buffer gnus-group-buffer)
6914       (gnus-group-jump-to-group current-group)
6915       (setq target-group
6916             (or target-group
6917                 (if (eq gnus-keep-same-level 'best)
6918                     (gnus-summary-best-group gnus-newsgroup-name)
6919                   (gnus-summary-search-group backward gnus-keep-same-level))))
6920       (if (not target-group)
6921           ;; There are no further groups, so we return to the group
6922           ;; buffer.
6923           (progn
6924             (gnus-message 5 "Returning to the group buffer")
6925             (setq entered t)
6926             (when (gnus-buffer-live-p current-buffer)
6927               (set-buffer current-buffer)
6928               (gnus-summary-exit))
6929             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6930         ;; We try to enter the target group.
6931         (gnus-group-jump-to-group target-group)
6932         (let ((unreads (gnus-group-group-unread)))
6933           (if (and (or (eq t unreads)
6934                        (and unreads (not (zerop unreads))))
6935                    (gnus-summary-read-group
6936                     target-group nil no-article
6937                     (and (buffer-name current-buffer) current-buffer)
6938                     nil backward))
6939               (setq entered t)
6940             (setq current-group target-group
6941                   target-group nil)))))))
6942
6943 (defun gnus-summary-prev-group (&optional no-article)
6944   "Exit current newsgroup and then select previous unread newsgroup.
6945 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6946   (interactive "P")
6947   (gnus-summary-next-group no-article nil t))
6948
6949 ;; Walking around summary lines.
6950
6951 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6952   "Go to the first subject satisfying any non-nil constraint.
6953 If UNREAD is non-nil, the article should be unread.
6954 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6955 If UNSEEN is non-nil, the article should be unseen.
6956 Returns the article selected or nil if there are no matching articles."
6957   (interactive "P")
6958   (cond
6959    ;; Empty summary.
6960    ((null gnus-newsgroup-data)
6961     (gnus-message 3 "No articles in the group")
6962     nil)
6963    ;; Pick the first article.
6964    ((not (or unread undownloaded unseen))
6965     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6966     (gnus-data-number (car gnus-newsgroup-data)))
6967    ;; Find the first unread article.
6968    (t
6969     (let ((data gnus-newsgroup-data))
6970       (while (and data
6971                   (let ((num (gnus-data-number (car data))))
6972                     (or (memq num gnus-newsgroup-unfetched)
6973                         (not (or (and unread
6974                                       (memq num gnus-newsgroup-unreads))
6975                                  (and undownloaded
6976                                       (memq num gnus-newsgroup-undownloaded))
6977                                  (and unseen
6978                                       (memq num gnus-newsgroup-unseen)))))))
6979         (setq data (cdr data)))
6980       (prog1
6981           (if data
6982               (progn
6983                 (goto-char (gnus-data-pos (car data)))
6984                 (gnus-data-number (car data)))
6985             (gnus-message 3 "No more%s articles"
6986                           (let* ((r (when unread " unread"))
6987                                  (d (when undownloaded " undownloaded"))
6988                                  (s (when unseen " unseen"))
6989                                  (l (delq nil (list r d s))))
6990                             (cond ((= 3 (length l))
6991                                    (concat r "," d ", or" s))
6992                                   ((= 2 (length l))
6993                                    (concat (car l) ", or" (cadr l)))
6994                                   ((= 1 (length l))
6995                                    (car l))
6996                                   (t
6997                                    ""))))
6998             nil
6999             )
7000         (gnus-summary-position-point))))))
7001
7002 (defun gnus-summary-next-subject (n &optional unread dont-display)
7003   "Go to next N'th summary line.
7004 If N is negative, go to the previous N'th subject line.
7005 If UNREAD is non-nil, only unread articles are selected.
7006 The difference between N and the actual number of steps taken is
7007 returned."
7008   (interactive "p")
7009   (let ((backward (< n 0))
7010         (n (abs n)))
7011     (while (and (> n 0)
7012                 (if backward
7013                     (gnus-summary-find-prev unread)
7014                   (gnus-summary-find-next unread)))
7015       (unless (zerop (setq n (1- n)))
7016         (gnus-summary-show-thread)))
7017     (when (/= 0 n)
7018       (gnus-message 7 "No more%s articles"
7019                     (if unread " unread" "")))
7020     (unless dont-display
7021       (gnus-summary-recenter)
7022       (gnus-summary-position-point))
7023     n))
7024
7025 (defun gnus-summary-next-unread-subject (n)
7026   "Go to next N'th unread summary line."
7027   (interactive "p")
7028   (gnus-summary-next-subject n t))
7029
7030 (defun gnus-summary-prev-subject (n &optional unread)
7031   "Go to previous N'th summary line.
7032 If optional argument UNREAD is non-nil, only unread article is selected."
7033   (interactive "p")
7034   (gnus-summary-next-subject (- n) unread))
7035
7036 (defun gnus-summary-prev-unread-subject (n)
7037   "Go to previous N'th unread summary line."
7038   (interactive "p")
7039   (gnus-summary-next-subject (- n) t))
7040
7041 (defun gnus-summary-goto-subjects (articles)
7042   "Insert the subject header for ARTICLES in the current buffer."
7043   (save-excursion
7044     (dolist (article articles)
7045       (gnus-summary-goto-subject article t)))
7046   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7047   (gnus-summary-position-point))
7048  
7049 (defun gnus-summary-goto-subject (article &optional force silent)
7050   "Go the subject line of ARTICLE.
7051 If FORCE, also allow jumping to articles not currently shown."
7052   (interactive "nArticle number: ")
7053   (unless (numberp article)
7054     (error "Article %s is not a number" article))
7055   (let ((b (point))
7056         (data (gnus-data-find article)))
7057     ;; We read in the article if we have to.
7058     (and (not data)
7059          force
7060          (gnus-summary-insert-subject
7061           article
7062           (if (or (numberp force) (vectorp force)) force)
7063           t)
7064          (setq data (gnus-data-find article)))
7065     (goto-char b)
7066     (if (not data)
7067         (progn
7068           (unless silent
7069             (gnus-message 3 "Can't find article %d" article))
7070           nil)
7071       (let ((pt (gnus-data-pos data)))
7072         (goto-char pt)
7073         (gnus-summary-set-article-display-arrow pt))
7074       (gnus-summary-position-point)
7075       article)))
7076
7077 ;; Walking around summary lines with displaying articles.
7078
7079 (defun gnus-summary-expand-window (&optional arg)
7080   "Make the summary buffer take up the entire Emacs frame.
7081 Given a prefix, will force an `article' buffer configuration."
7082   (interactive "P")
7083   (if arg
7084       (gnus-configure-windows 'article 'force)
7085     (gnus-configure-windows 'summary 'force)))
7086
7087 (defun gnus-summary-display-article (article &optional all-header)
7088   "Display ARTICLE in article buffer."
7089   (when (gnus-buffer-live-p gnus-article-buffer)
7090     (with-current-buffer gnus-article-buffer
7091       (set-buffer-multibyte t)))
7092   (gnus-set-global-variables)
7093   (when (gnus-buffer-live-p gnus-article-buffer)
7094     (with-current-buffer gnus-article-buffer
7095       (setq gnus-article-charset gnus-newsgroup-charset)
7096       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7097       (set-buffer-multibyte t)))
7098   (if (null article)
7099       nil
7100     (prog1
7101         (if gnus-summary-display-article-function
7102             (funcall gnus-summary-display-article-function article all-header)
7103           (gnus-article-prepare article all-header))
7104       (with-current-buffer gnus-article-buffer
7105         (set (make-local-variable 'gnus-summary-search-article-matched-data)
7106              nil))
7107       (gnus-run-hooks 'gnus-select-article-hook)
7108       (when (and gnus-current-article
7109                  (not (zerop gnus-current-article)))
7110         (gnus-summary-goto-subject gnus-current-article))
7111       (gnus-summary-recenter)
7112       (when (and gnus-use-trees gnus-show-threads)
7113         (gnus-possibly-generate-tree article)
7114         (gnus-highlight-selected-tree article))
7115       ;; Successfully display article.
7116       (gnus-article-set-window-start
7117        (cdr (assq article gnus-newsgroup-bookmarks))))))
7118
7119 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7120   "Select the current article.
7121 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7122 non-nil, the article will be re-fetched even if it already present in
7123 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7124 be displayed."
7125   ;; Make sure we are in the summary buffer to work around bbdb bug.
7126   (unless (eq major-mode 'gnus-summary-mode)
7127     (set-buffer gnus-summary-buffer))
7128   (let ((article (or article (gnus-summary-article-number)))
7129         (all-headers (not (not all-headers))) ;Must be T or NIL.
7130         gnus-summary-display-article-function)
7131     (and (not pseudo)
7132          (gnus-summary-article-pseudo-p article)
7133          (error "This is a pseudo-article"))
7134     (save-excursion
7135       (set-buffer gnus-summary-buffer)
7136       (if (or (and gnus-single-article-buffer
7137                    (or (null gnus-current-article)
7138                        (null gnus-article-current)
7139                        (null (get-buffer gnus-article-buffer))
7140                        (not (eq article (cdr gnus-article-current)))
7141                        (not (equal (car gnus-article-current)
7142                                    gnus-newsgroup-name))))
7143               (and (not gnus-single-article-buffer)
7144                    (or (null gnus-current-article)
7145                        (not (eq gnus-current-article article))))
7146               force)
7147           ;; The requested article is different from the current article.
7148           (progn
7149             (gnus-summary-display-article article all-headers)
7150             (gnus-article-set-window-start
7151              (cdr (assq article gnus-newsgroup-bookmarks)))
7152             article)
7153         'old))))
7154
7155 (defun gnus-summary-force-verify-and-decrypt ()
7156   "Display buttons for signed/encrypted parts and verify/decrypt them."
7157   (interactive)
7158   (let ((mm-verify-option 'known)
7159         (mm-decrypt-option 'known)
7160         (gnus-article-emulate-mime t)
7161         (mm-fill-flowed nil)
7162         (gnus-buttonized-mime-types (append (list "multipart/signed"
7163                                                   "multipart/encrypted")
7164                                             gnus-buttonized-mime-types)))
7165     (gnus-summary-select-article nil 'force)))
7166
7167 (defun gnus-summary-set-current-mark (&optional current-mark)
7168   "Obsolete function."
7169   nil)
7170
7171 (defun gnus-summary-next-article (&optional unread subject backward push)
7172   "Select the next article.
7173 If UNREAD, only unread articles are selected.
7174 If SUBJECT, only articles with SUBJECT are selected.
7175 If BACKWARD, the previous article is selected instead of the next."
7176   (interactive "P")
7177   (cond
7178    ;; Is there such an article?
7179    ((and (gnus-summary-search-forward unread subject backward)
7180          (or (gnus-summary-display-article (gnus-summary-article-number))
7181              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7182     (gnus-summary-position-point))
7183    ;; If not, we try the first unread, if that is wanted.
7184    ((and subject
7185          gnus-auto-select-same
7186          (gnus-summary-first-unread-article))
7187     (gnus-summary-position-point)
7188     (gnus-message 6 "Wrapped"))
7189    ;; Try to get next/previous article not displayed in this group.
7190    ((and gnus-auto-extend-newsgroup
7191          (not unread) (not subject))
7192     (gnus-summary-goto-article
7193      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7194      nil (count-lines (point-min) (point))))
7195    ;; Go to next/previous group.
7196    (t
7197     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7198       (gnus-summary-jump-to-group gnus-newsgroup-name))
7199     (let ((cmd last-command-char)
7200           (point
7201            (with-current-buffer gnus-group-buffer
7202              (point)))
7203           (group
7204            (if (eq gnus-keep-same-level 'best)
7205                (gnus-summary-best-group gnus-newsgroup-name)
7206              (gnus-summary-search-group backward gnus-keep-same-level))))
7207       ;; For some reason, the group window gets selected.  We change
7208       ;; it back.
7209       (select-window (get-buffer-window (current-buffer)))
7210       ;; Select next unread newsgroup automagically.
7211       (cond
7212        ((or (not gnus-auto-select-next)
7213             (not cmd))
7214         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7215        ((or (eq gnus-auto-select-next 'quietly)
7216             (and (eq gnus-auto-select-next 'slightly-quietly)
7217                  push)
7218             (and (eq gnus-auto-select-next 'almost-quietly)
7219                  (gnus-summary-last-article-p)))
7220         ;; Select quietly.
7221         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7222             (gnus-summary-exit)
7223           (gnus-message 7 "No more%s articles (%s)..."
7224                         (if unread " unread" "")
7225                         (if group (concat "selecting " group)
7226                           "exiting"))
7227           (gnus-summary-next-group nil group backward)))
7228        (t
7229         (when (gnus-key-press-event-p last-input-event)
7230           (gnus-summary-walk-group-buffer
7231            gnus-newsgroup-name cmd unread backward point))))))))
7232
7233 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7234   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7235                       (?\C-p (gnus-group-prev-unread-group 1))))
7236         (cursor-in-echo-area t)
7237         keve key group ended prompt)
7238     (save-excursion
7239       (set-buffer gnus-group-buffer)
7240       (goto-char start)
7241       (setq group
7242             (if (eq gnus-keep-same-level 'best)
7243                 (gnus-summary-best-group gnus-newsgroup-name)
7244               (gnus-summary-search-group backward gnus-keep-same-level))))
7245     (while (not ended)
7246       (setq prompt
7247             (format
7248              "No more%s articles%s " (if unread " unread" "")
7249              (if (and group
7250                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7251                  (format " (Type %s for %s [%s])"
7252                          (single-key-description cmd) group
7253                          (gnus-group-unread group))
7254                (format " (Type %s to exit %s)"
7255                        (single-key-description cmd)
7256                        gnus-newsgroup-name))))
7257       ;; Confirm auto selection.
7258       (setq key (car (setq keve (gnus-read-event-char prompt)))
7259             ended t)
7260       (cond
7261        ((assq key keystrokes)
7262         (let ((obuf (current-buffer)))
7263           (switch-to-buffer gnus-group-buffer)
7264           (when group
7265             (gnus-group-jump-to-group group))
7266           (eval (cadr (assq key keystrokes)))
7267           (setq group (gnus-group-group-name))
7268           (switch-to-buffer obuf))
7269         (setq ended nil))
7270        ((equal key cmd)
7271         (if (or (not group)
7272                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7273             (gnus-summary-exit)
7274           (gnus-summary-next-group nil group backward)))
7275        (t
7276         (push (cdr keve) unread-command-events))))))
7277
7278 (defun gnus-summary-next-unread-article ()
7279   "Select unread article after current one."
7280   (interactive)
7281   (gnus-summary-next-article
7282    (or (not (eq gnus-summary-goto-unread 'never))
7283        (gnus-summary-last-article-p (gnus-summary-article-number)))
7284    (and gnus-auto-select-same
7285         (gnus-summary-article-subject))))
7286
7287 (defun gnus-summary-prev-article (&optional unread subject)
7288   "Select the article after the current one.
7289 If UNREAD is non-nil, only unread articles are selected."
7290   (interactive "P")
7291   (gnus-summary-next-article unread subject t))
7292
7293 (defun gnus-summary-prev-unread-article ()
7294   "Select unread article before current one."
7295   (interactive)
7296   (gnus-summary-prev-article
7297    (or (not (eq gnus-summary-goto-unread 'never))
7298        (gnus-summary-first-article-p (gnus-summary-article-number)))
7299    (and gnus-auto-select-same
7300         (gnus-summary-article-subject))))
7301
7302 (defun gnus-summary-next-page (&optional lines circular stop)
7303   "Show next page of the selected article.
7304 If at the end of the current article, select the next article.
7305 LINES says how many lines should be scrolled up.
7306
7307 If CIRCULAR is non-nil, go to the start of the article instead of
7308 selecting the next article when reaching the end of the current
7309 article.
7310
7311 If STOP is non-nil, just stop when reaching the end of the message.
7312
7313 Also see the variable `gnus-article-skip-boring'."
7314   (interactive "P")
7315   (setq gnus-summary-buffer (current-buffer))
7316   (gnus-set-global-variables)
7317   (let ((article (gnus-summary-article-number))
7318         (article-window (get-buffer-window gnus-article-buffer t))
7319         endp)
7320     ;; If the buffer is empty, we have no article.
7321     (unless article
7322       (error "No article to select"))
7323     (gnus-configure-windows 'article)
7324     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7325         (if (and (eq gnus-summary-goto-unread 'never)
7326                  (not (gnus-summary-last-article-p article)))
7327             (gnus-summary-next-article)
7328           (gnus-summary-next-unread-article))
7329       (if (or (null gnus-current-article)
7330               (null gnus-article-current)
7331               (/= article (cdr gnus-article-current))
7332               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7333           ;; Selected subject is different from current article's.
7334           (gnus-summary-display-article article)
7335         (when article-window
7336           (gnus-eval-in-buffer-window gnus-article-buffer
7337             (setq endp (or (gnus-article-next-page lines)
7338                            (gnus-article-only-boring-p))))
7339           (when endp
7340             (cond (stop
7341                    (gnus-message 3 "End of message"))
7342                   (circular
7343                    (gnus-summary-beginning-of-article))
7344                   (lines
7345                    (gnus-message 3 "End of message"))
7346                   ((null lines)
7347                    (if (and (eq gnus-summary-goto-unread 'never)
7348                             (not (gnus-summary-last-article-p article)))
7349                        (gnus-summary-next-article)
7350                      (gnus-summary-next-unread-article))))))))
7351     (gnus-summary-recenter)
7352     (gnus-summary-position-point)))
7353
7354 (defun gnus-summary-prev-page (&optional lines move)
7355   "Show previous page of selected article.
7356 Argument LINES specifies lines to be scrolled down.
7357 If MOVE, move to the previous unread article if point is at
7358 the beginning of the buffer."
7359   (interactive "P")
7360   (let ((article (gnus-summary-article-number))
7361         (article-window (get-buffer-window gnus-article-buffer t))
7362         endp)
7363     (gnus-configure-windows 'article)
7364     (if (or (null gnus-current-article)
7365             (null gnus-article-current)
7366             (/= article (cdr gnus-article-current))
7367             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7368         ;; Selected subject is different from current article's.
7369         (gnus-summary-display-article article)
7370       (gnus-summary-recenter)
7371       (when article-window
7372         (gnus-eval-in-buffer-window gnus-article-buffer
7373           (setq endp (gnus-article-prev-page lines)))
7374         (when (and move endp)
7375           (cond (lines
7376                  (gnus-message 3 "Beginning of message"))
7377                 ((null lines)
7378                  (if (and (eq gnus-summary-goto-unread 'never)
7379                           (not (gnus-summary-first-article-p article)))
7380                      (gnus-summary-prev-article)
7381                    (gnus-summary-prev-unread-article))))))))
7382   (gnus-summary-position-point))
7383
7384 (defun gnus-summary-prev-page-or-article (&optional lines)
7385   "Show previous page of selected article.
7386 Argument LINES specifies lines to be scrolled down.
7387 If at the beginning of the article, go to the next article."
7388   (interactive "P")
7389   (gnus-summary-prev-page lines t))
7390
7391 (defun gnus-summary-scroll-up (lines)
7392   "Scroll up (or down) one line current article.
7393 Argument LINES specifies lines to be scrolled up (or down if negative)."
7394   (interactive "p")
7395   (gnus-configure-windows 'article)
7396   (gnus-summary-show-thread)
7397   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7398     (gnus-eval-in-buffer-window gnus-article-buffer
7399       (cond ((> lines 0)
7400              (when (gnus-article-next-page lines)
7401                (gnus-message 3 "End of message")))
7402             ((< lines 0)
7403              (gnus-article-prev-page (- lines))))))
7404   (gnus-summary-recenter)
7405   (gnus-summary-position-point))
7406
7407 (defun gnus-summary-scroll-down (lines)
7408   "Scroll down (or up) one line current article.
7409 Argument LINES specifies lines to be scrolled down (or up if negative)."
7410   (interactive "p")
7411   (gnus-summary-scroll-up (- lines)))
7412
7413 (defun gnus-summary-next-same-subject ()
7414   "Select next article which has the same subject as current one."
7415   (interactive)
7416   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7417
7418 (defun gnus-summary-prev-same-subject ()
7419   "Select previous article which has the same subject as current one."
7420   (interactive)
7421   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7422
7423 (defun gnus-summary-next-unread-same-subject ()
7424   "Select next unread article which has the same subject as current one."
7425   (interactive)
7426   (gnus-summary-next-article t (gnus-summary-article-subject)))
7427
7428 (defun gnus-summary-prev-unread-same-subject ()
7429   "Select previous unread article which has the same subject as current one."
7430   (interactive)
7431   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7432
7433 (defun gnus-summary-first-unread-article ()
7434   "Select the first unread article.
7435 Return nil if there are no unread articles."
7436   (interactive)
7437   (prog1
7438       (when (gnus-summary-first-subject t)
7439         (gnus-summary-show-thread)
7440         (gnus-summary-first-subject t)
7441         (gnus-summary-display-article (gnus-summary-article-number)))
7442     (gnus-summary-position-point)))
7443
7444 (defun gnus-summary-first-unread-subject ()
7445   "Place the point on the subject line of the first unread article.
7446 Return nil if there are no unread articles."
7447   (interactive)
7448   (prog1
7449       (when (gnus-summary-first-subject t)
7450         (gnus-summary-show-thread)
7451         (gnus-summary-first-subject t))
7452     (gnus-summary-position-point)))
7453
7454 (defun gnus-summary-first-unseen-subject ()
7455   "Place the point on the subject line of the first unseen article.
7456 Return nil if there are no unseen articles."
7457   (interactive)
7458   (prog1
7459       (when (gnus-summary-first-subject nil nil t)
7460         (gnus-summary-show-thread)
7461         (gnus-summary-first-subject nil nil t))
7462     (gnus-summary-position-point)))
7463
7464 (defun gnus-summary-first-unseen-or-unread-subject ()
7465   "Place the point on the subject line of the first unseen article or,
7466 if all article have been seen, on the subject line of the first unread
7467 article."
7468   (interactive)
7469   (prog1
7470       (unless (when (gnus-summary-first-subject nil nil t)
7471                 (gnus-summary-show-thread)
7472                 (gnus-summary-first-subject nil nil t))
7473         (when (gnus-summary-first-subject t)
7474           (gnus-summary-show-thread)
7475           (gnus-summary-first-subject t)))
7476     (gnus-summary-position-point)))
7477
7478 (defun gnus-summary-first-article ()
7479   "Select the first article.
7480 Return nil if there are no articles."
7481   (interactive)
7482   (prog1
7483       (when (gnus-summary-first-subject)
7484         (gnus-summary-show-thread)
7485         (gnus-summary-first-subject)
7486         (gnus-summary-display-article (gnus-summary-article-number)))
7487     (gnus-summary-position-point)))
7488
7489 (defun gnus-summary-best-unread-article (&optional arg)
7490   "Select the unread article with the highest score.
7491 If given a prefix argument, select the next unread article that has a
7492 score higher than the default score."
7493   (interactive "P")
7494   (let ((article (if arg
7495                      (gnus-summary-better-unread-subject)
7496                    (gnus-summary-best-unread-subject))))
7497     (if article
7498         (gnus-summary-goto-article article)
7499       (error "No unread articles"))))
7500
7501 (defun gnus-summary-best-unread-subject ()
7502   "Select the unread subject with the highest score."
7503   (interactive)
7504   (let ((best -1000000)
7505         (data gnus-newsgroup-data)
7506         article score)
7507     (while data
7508       (and (gnus-data-unread-p (car data))
7509            (> (setq score
7510                     (gnus-summary-article-score (gnus-data-number (car data))))
7511               best)
7512            (setq best score
7513                  article (gnus-data-number (car data))))
7514       (setq data (cdr data)))
7515     (when article
7516       (gnus-summary-goto-subject article))
7517     (gnus-summary-position-point)
7518     article))
7519
7520 (defun gnus-summary-better-unread-subject ()
7521   "Select the first unread subject that has a score over the default score."
7522   (interactive)
7523   (let ((data gnus-newsgroup-data)
7524         article score)
7525     (while (and (setq article (gnus-data-number (car data)))
7526                 (or (gnus-data-read-p (car data))
7527                     (not (> (gnus-summary-article-score article)
7528                             gnus-summary-default-score))))
7529       (setq data (cdr data)))
7530     (when article
7531       (gnus-summary-goto-subject article))
7532     (gnus-summary-position-point)
7533     article))
7534
7535 (defun gnus-summary-last-subject ()
7536   "Go to the last displayed subject line in the group."
7537   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7538     (when article
7539       (gnus-summary-goto-subject article))))
7540
7541 (defun gnus-summary-goto-article (article &optional all-headers force)
7542   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7543 If ALL-HEADERS is non-nil, no header lines are hidden.
7544 If FORCE, go to the article even if it isn't displayed.  If FORCE
7545 is a number, it is the line the article is to be displayed on."
7546   (interactive
7547    (list
7548     (completing-read
7549      "Article number or Message-ID: "
7550      (mapcar (lambda (number) (list (int-to-string number)))
7551              gnus-newsgroup-limit))
7552     current-prefix-arg
7553     t))
7554   (prog1
7555       (if (and (stringp article)
7556                (string-match "@\\|%40" article))
7557           (gnus-summary-refer-article article)
7558         (when (stringp article)
7559           (setq article (string-to-number article)))
7560         (if (gnus-summary-goto-subject article force)
7561             (gnus-summary-display-article article all-headers)
7562           (gnus-message 4 "Couldn't go to article %s" article) nil))
7563     (gnus-summary-position-point)))
7564
7565 (defun gnus-summary-goto-last-article ()
7566   "Go to the previously read article."
7567   (interactive)
7568   (prog1
7569       (when gnus-last-article
7570         (gnus-summary-goto-article gnus-last-article nil t))
7571     (gnus-summary-position-point)))
7572
7573 (defun gnus-summary-pop-article (number)
7574   "Pop one article off the history and go to the previous.
7575 NUMBER articles will be popped off."
7576   (interactive "p")
7577   (let (to)
7578     (setq gnus-newsgroup-history
7579           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7580     (if to
7581         (gnus-summary-goto-article (car to) nil t)
7582       (error "Article history empty")))
7583   (gnus-summary-position-point))
7584
7585 ;; Summary commands and functions for limiting the summary buffer.
7586
7587 (defun gnus-summary-limit-to-articles (n)
7588   "Limit the summary buffer to the next N articles.
7589 If not given a prefix, use the process marked articles instead."
7590   (interactive "P")
7591   (prog1
7592       (let ((articles (gnus-summary-work-articles n)))
7593         (setq gnus-newsgroup-processable nil)
7594         (gnus-summary-limit articles))
7595     (gnus-summary-position-point)))
7596
7597 (defun gnus-summary-pop-limit (&optional total)
7598   "Restore the previous limit.
7599 If given a prefix, remove all limits."
7600   (interactive "P")
7601   (when total
7602     (setq gnus-newsgroup-limits
7603           (list (mapcar (lambda (h) (mail-header-number h))
7604                         gnus-newsgroup-headers))))
7605   (unless gnus-newsgroup-limits
7606     (error "No limit to pop"))
7607   (prog1
7608       (gnus-summary-limit nil 'pop)
7609     (gnus-summary-position-point)))
7610
7611 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7612   "Limit the summary buffer to articles that have subjects that match a regexp.
7613 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7614   (interactive
7615    (list (read-string (if current-prefix-arg
7616                           "Exclude subject (regexp): "
7617                         "Limit to subject (regexp): "))
7618          nil current-prefix-arg))
7619   (unless header
7620     (setq header "subject"))
7621   (when (not (equal "" subject))
7622     (prog1
7623         (let ((articles (gnus-summary-find-matching
7624                          (or header "subject") subject 'all nil nil
7625                          not-matching)))
7626           (unless articles
7627             (error "Found no matches for \"%s\"" subject))
7628           (gnus-summary-limit articles))
7629       (gnus-summary-position-point))))
7630
7631 (defun gnus-summary-limit-to-author (from &optional not-matching)
7632   "Limit the summary buffer to articles that have authors that match a regexp.
7633 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7634   (interactive
7635    (list (read-string (if current-prefix-arg
7636                           "Exclude author (regexp): "
7637                         "Limit to author (regexp): "))
7638          current-prefix-arg))
7639   (gnus-summary-limit-to-subject from "from" not-matching))
7640
7641 (defun gnus-summary-limit-to-age (age &optional younger-p)
7642   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7643 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7644 articles that are younger than AGE days."
7645   (interactive
7646    (let ((younger current-prefix-arg)
7647          (days-got nil)
7648          days)
7649      (while (not days-got)
7650        (setq days (if younger
7651                       (read-string "Limit to articles younger than (in days, older when negative): ")
7652                     (read-string
7653                      "Limit to articles older than (in days, younger when negative): ")))
7654        (when (> (length days) 0)
7655          (setq days (read days)))
7656        (if (numberp days)
7657            (progn
7658              (setq days-got t)
7659              (when (< days 0)
7660                (setq younger (not younger))
7661                (setq days (* days -1))))
7662          (message "Please enter a number.")
7663          (sleep-for 1)))
7664      (list days younger)))
7665   (prog1
7666       (let ((data gnus-newsgroup-data)
7667             (cutoff (days-to-time age))
7668             articles d date is-younger)
7669         (while (setq d (pop data))
7670           (when (and (vectorp (gnus-data-header d))
7671                      (setq date (mail-header-date (gnus-data-header d))))
7672             (setq is-younger (time-less-p
7673                               (time-since (condition-case ()
7674                                               (date-to-time date)
7675                                             (error '(0 0))))
7676                               cutoff))
7677             (when (if younger-p
7678                       is-younger
7679                     (not is-younger))
7680               (push (gnus-data-number d) articles))))
7681         (gnus-summary-limit (nreverse articles)))
7682     (gnus-summary-position-point)))
7683
7684 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7685   "Limit the summary buffer to articles that match an 'extra' header."
7686   (interactive
7687    (let ((header
7688           (intern
7689            (gnus-completing-read-with-default
7690             (symbol-name (car gnus-extra-headers))
7691             (if current-prefix-arg
7692                 "Exclude extra header:"
7693               "Limit extra header:")
7694             (mapcar (lambda (x)
7695                       (cons (symbol-name x) x))
7696                     gnus-extra-headers)
7697             nil
7698             t))))
7699      (list header
7700            (read-string (format "%s header %s (regexp): "
7701                                 (if current-prefix-arg "Exclude" "Limit to")
7702                                 header))
7703            current-prefix-arg)))
7704   (when (not (equal "" regexp))
7705     (prog1
7706         (let ((articles (gnus-summary-find-matching
7707                          (cons 'extra header) regexp 'all nil nil
7708                          not-matching)))
7709           (unless articles
7710             (error "Found no matches for \"%s\"" regexp))
7711           (gnus-summary-limit articles))
7712       (gnus-summary-position-point))))
7713
7714 (defun gnus-summary-limit-to-display-predicate ()
7715   "Limit the summary buffer to the predicated in the `display' group parameter."
7716   (interactive)
7717   (unless gnus-newsgroup-display
7718     (error "There is no `display' group parameter"))
7719   (let (articles)
7720     (dolist (number gnus-newsgroup-articles)
7721       (when (funcall gnus-newsgroup-display)
7722         (push number articles)))
7723     (gnus-summary-limit articles))
7724   (gnus-summary-position-point))
7725
7726 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7727 (make-obsolete
7728  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7729
7730 (defun gnus-summary-limit-to-unread (&optional all)
7731   "Limit the summary buffer to articles that are not marked as read.
7732 If ALL is non-nil, limit strictly to unread articles."
7733   (interactive "P")
7734   (if all
7735       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7736     (gnus-summary-limit-to-marks
7737      ;; Concat all the marks that say that an article is read and have
7738      ;; those removed.
7739      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7740            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7741            gnus-low-score-mark gnus-expirable-mark
7742            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7743            gnus-duplicate-mark gnus-souped-mark)
7744      'reverse)))
7745
7746 (defun gnus-summary-limit-to-replied (&optional unreplied)
7747   "Limit the summary buffer to replied articles.
7748 If UNREPLIED (the prefix), limit to unreplied articles."
7749   (interactive "P")
7750   (if unreplied
7751       (gnus-summary-limit
7752        (gnus-set-difference gnus-newsgroup-articles
7753         gnus-newsgroup-replied))
7754     (gnus-summary-limit gnus-newsgroup-replied))
7755   (gnus-summary-position-point))
7756
7757 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7758 (make-obsolete 'gnus-summary-delete-marked-with
7759                'gnus-summary-limit-exclude-marks)
7760
7761 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7762   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7763 If REVERSE, limit the summary buffer to articles that are marked
7764 with MARKS.  MARKS can either be a string of marks or a list of marks.
7765 Returns how many articles were removed."
7766   (interactive "sMarks: ")
7767   (gnus-summary-limit-to-marks marks t))
7768
7769 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7770   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7771 If REVERSE (the prefix), limit the summary buffer to articles that are
7772 not marked with MARKS.  MARKS can either be a string of marks or a
7773 list of marks.
7774 Returns how many articles were removed."
7775   (interactive "sMarks: \nP")
7776   (prog1
7777       (let ((data gnus-newsgroup-data)
7778             (marks (if (listp marks) marks
7779                      (append marks nil))) ; Transform to list.
7780             articles)
7781         (while data
7782           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7783                   (memq (gnus-data-mark (car data)) marks))
7784             (push (gnus-data-number (car data)) articles))
7785           (setq data (cdr data)))
7786         (gnus-summary-limit articles))
7787     (gnus-summary-position-point)))
7788
7789 (defun gnus-summary-limit-to-score (score)
7790   "Limit to articles with score at or above SCORE."
7791   (interactive "NLimit to articles with score of at least: ")
7792   (let ((data gnus-newsgroup-data)
7793         articles)
7794     (while data
7795       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7796                 score)
7797         (push (gnus-data-number (car data)) articles))
7798       (setq data (cdr data)))
7799     (prog1
7800         (gnus-summary-limit articles)
7801       (gnus-summary-position-point))))
7802
7803 (defun gnus-summary-limit-to-unseen ()
7804   "Limit to unseen articles."
7805   (interactive)
7806   (prog1
7807       (gnus-summary-limit gnus-newsgroup-unseen)
7808     (gnus-summary-position-point)))
7809
7810 (defun gnus-summary-limit-include-thread (id)
7811   "Display all the hidden articles that is in the thread with ID in it.
7812 When called interactively, ID is the Message-ID of the current
7813 article."
7814   (interactive (list (mail-header-id (gnus-summary-article-header))))
7815   (let ((articles (gnus-articles-in-thread
7816                    (gnus-id-to-thread (gnus-root-id id)))))
7817     (prog1
7818         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7819       (gnus-summary-limit-include-matching-articles
7820        "subject"
7821        (regexp-quote (gnus-simplify-subject-re
7822                       (mail-header-subject (gnus-id-to-header id)))))
7823       (gnus-summary-position-point))))
7824
7825 (defun gnus-summary-limit-include-matching-articles (header regexp)
7826   "Display all the hidden articles that have HEADERs that match REGEXP."
7827   (interactive (list (read-string "Match on header: ")
7828                      (read-string "Regexp: ")))
7829   (let ((articles (gnus-find-matching-articles header regexp)))
7830     (prog1
7831         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7832       (gnus-summary-position-point))))
7833
7834 (defun gnus-summary-insert-dormant-articles ()
7835   "Insert all the dormant articles for this group into the current buffer."
7836   (interactive)
7837   (let ((gnus-verbose (max 6 gnus-verbose)))
7838     (if (not gnus-newsgroup-dormant)
7839         (gnus-message 3 "No cached articles for this group")
7840       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7841
7842 (defun gnus-summary-limit-include-dormant ()
7843   "Display all the hidden articles that are marked as dormant.
7844 Note that this command only works on a subset of the articles currently
7845 fetched for this group."
7846   (interactive)
7847   (unless gnus-newsgroup-dormant
7848     (error "There are no dormant articles in this group"))
7849   (prog1
7850       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7851     (gnus-summary-position-point)))
7852
7853 (defun gnus-summary-limit-exclude-dormant ()
7854   "Hide all dormant articles."
7855   (interactive)
7856   (prog1
7857       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7858     (gnus-summary-position-point)))
7859
7860 (defun gnus-summary-limit-exclude-childless-dormant ()
7861   "Hide all dormant articles that have no children."
7862   (interactive)
7863   (let ((data (gnus-data-list t))
7864         articles d children)
7865     ;; Find all articles that are either not dormant or have
7866     ;; children.
7867     (while (setq d (pop data))
7868       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7869                 (and (setq children
7870                            (gnus-article-children (gnus-data-number d)))
7871                      (let (found)
7872                        (while children
7873                          (when (memq (car children) articles)
7874                            (setq children nil
7875                                  found t))
7876                          (pop children))
7877                        found)))
7878         (push (gnus-data-number d) articles)))
7879     ;; Do the limiting.
7880     (prog1
7881         (gnus-summary-limit articles)
7882       (gnus-summary-position-point))))
7883
7884 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7885   "Mark all unread excluded articles as read.
7886 If ALL, mark even excluded ticked and dormants as read."
7887   (interactive "P")
7888   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7889   (let ((articles (gnus-sorted-ndifference
7890                    (sort
7891                     (mapcar (lambda (h) (mail-header-number h))
7892                             gnus-newsgroup-headers)
7893                     '<)
7894                    gnus-newsgroup-limit))
7895         article)
7896     (setq gnus-newsgroup-unreads
7897           (gnus-sorted-intersection gnus-newsgroup-unreads
7898                                     gnus-newsgroup-limit))
7899     (if all
7900         (setq gnus-newsgroup-dormant nil
7901               gnus-newsgroup-marked nil
7902               gnus-newsgroup-reads
7903               (nconc
7904                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7905                gnus-newsgroup-reads))
7906       (while (setq article (pop articles))
7907         (unless (or (memq article gnus-newsgroup-dormant)
7908                     (memq article gnus-newsgroup-marked))
7909           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7910
7911 (defun gnus-summary-limit (articles &optional pop)
7912   (if pop
7913       ;; We pop the previous limit off the stack and use that.
7914       (setq articles (car gnus-newsgroup-limits)
7915             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7916     ;; We use the new limit, so we push the old limit on the stack.
7917     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7918   ;; Set the limit.
7919   (setq gnus-newsgroup-limit articles)
7920   (let ((total (length gnus-newsgroup-data))
7921         (data (gnus-data-find-list (gnus-summary-article-number)))
7922         (gnus-summary-mark-below nil)   ; Inhibit this.
7923         found)
7924     ;; This will do all the work of generating the new summary buffer
7925     ;; according to the new limit.
7926     (gnus-summary-prepare)
7927     ;; Hide any threads, possibly.
7928     (gnus-summary-maybe-hide-threads)
7929     ;; Try to return to the article you were at, or one in the
7930     ;; neighborhood.
7931     (when data
7932       ;; We try to find some article after the current one.
7933       (while data
7934         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7935           (setq data nil
7936                 found t))
7937         (setq data (cdr data))))
7938     (unless found
7939       ;; If there is no data, that means that we were after the last
7940       ;; article.  The same goes when we can't find any articles
7941       ;; after the current one.
7942       (goto-char (point-max))
7943       (gnus-summary-find-prev))
7944     (gnus-set-mode-line 'summary)
7945     ;; We return how many articles were removed from the summary
7946     ;; buffer as a result of the new limit.
7947     (- total (length gnus-newsgroup-data))))
7948
7949 (defsubst gnus-invisible-cut-children (threads)
7950   (let ((num 0))
7951     (while threads
7952       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7953         (incf num))
7954       (pop threads))
7955     (< num 2)))
7956
7957 (defsubst gnus-cut-thread (thread)
7958   "Go forwards in the thread until we find an article that we want to display."
7959   (when (or (eq gnus-fetch-old-headers 'some)
7960             (eq gnus-fetch-old-headers 'invisible)
7961             (numberp gnus-fetch-old-headers)
7962             (eq gnus-build-sparse-threads 'some)
7963             (eq gnus-build-sparse-threads 'more))
7964     ;; Deal with old-fetched headers and sparse threads.
7965     (while (and
7966             thread
7967             (or
7968              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7969              (gnus-summary-article-ancient-p
7970               (mail-header-number (car thread))))
7971             (if (or (<= (length (cdr thread)) 1)
7972                     (eq gnus-fetch-old-headers 'invisible))
7973                 (setq gnus-newsgroup-limit
7974                       (delq (mail-header-number (car thread))
7975                             gnus-newsgroup-limit)
7976                       thread (cadr thread))
7977               (when (gnus-invisible-cut-children (cdr thread))
7978                 (let ((th (cdr thread)))
7979                   (while th
7980                     (if (memq (mail-header-number (caar th))
7981                               gnus-newsgroup-limit)
7982                         (setq thread (car th)
7983                               th nil)
7984                       (setq th (cdr th))))))))))
7985   thread)
7986
7987 (defun gnus-cut-threads (threads)
7988   "Cut off all uninteresting articles from the beginning of THREADS."
7989   (when (or (eq gnus-fetch-old-headers 'some)
7990             (eq gnus-fetch-old-headers 'invisible)
7991             (numberp gnus-fetch-old-headers)
7992             (eq gnus-build-sparse-threads 'some)
7993             (eq gnus-build-sparse-threads 'more))
7994     (let ((th threads))
7995       (while th
7996         (setcar th (gnus-cut-thread (car th)))
7997         (setq th (cdr th)))))
7998   ;; Remove nixed out threads.
7999   (delq nil threads))
8000
8001 (defun gnus-summary-initial-limit (&optional show-if-empty)
8002   "Figure out what the initial limit is supposed to be on group entry.
8003 This entails weeding out unwanted dormants, low-scored articles,
8004 fetch-old-headers verbiage, and so on."
8005   ;; Most groups have nothing to remove.
8006   (if (or gnus-inhibit-limiting
8007           (and (null gnus-newsgroup-dormant)
8008                (eq gnus-newsgroup-display 'gnus-not-ignore)
8009                (not (eq gnus-fetch-old-headers 'some))
8010                (not (numberp gnus-fetch-old-headers))
8011                (not (eq gnus-fetch-old-headers 'invisible))
8012                (null gnus-summary-expunge-below)
8013                (not (eq gnus-build-sparse-threads 'some))
8014                (not (eq gnus-build-sparse-threads 'more))
8015                (null gnus-thread-expunge-below)
8016                (not gnus-use-nocem)))
8017       ()                                ; Do nothing.
8018     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8019     (setq gnus-newsgroup-limit nil)
8020     (mapatoms
8021      (lambda (node)
8022        (unless (car (symbol-value node))
8023          ;; These threads have no parents -- they are roots.
8024          (let ((nodes (cdr (symbol-value node)))
8025                thread)
8026            (while nodes
8027              (if (and gnus-thread-expunge-below
8028                       (< (gnus-thread-total-score (car nodes))
8029                          gnus-thread-expunge-below))
8030                  (gnus-expunge-thread (pop nodes))
8031                (setq thread (pop nodes))
8032                (gnus-summary-limit-children thread))))))
8033      gnus-newsgroup-dependencies)
8034     ;; If this limitation resulted in an empty group, we might
8035     ;; pop the previous limit and use it instead.
8036     (when (and (not gnus-newsgroup-limit)
8037                show-if-empty)
8038       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8039     gnus-newsgroup-limit))
8040
8041 (defun gnus-summary-limit-children (thread)
8042   "Return 1 if this subthread is visible and 0 if it is not."
8043   ;; First we get the number of visible children to this thread.  This
8044   ;; is done by recursing down the thread using this function, so this
8045   ;; will really go down to a leaf article first, before slowly
8046   ;; working its way up towards the root.
8047   (when thread
8048     (let* ((max-lisp-eval-depth 5000)
8049            (children
8050            (if (cdr thread)
8051                (apply '+ (mapcar 'gnus-summary-limit-children
8052                                  (cdr thread)))
8053              0))
8054           (number (mail-header-number (car thread)))
8055           score)
8056       (if (and
8057            (not (memq number gnus-newsgroup-marked))
8058            (or
8059             ;; If this article is dormant and has absolutely no visible
8060             ;; children, then this article isn't visible.
8061             (and (memq number gnus-newsgroup-dormant)
8062                  (zerop children))
8063             ;; If this is "fetch-old-headered" and there is no
8064             ;; visible children, then we don't want this article.
8065             (and (or (eq gnus-fetch-old-headers 'some)
8066                      (numberp gnus-fetch-old-headers))
8067                  (gnus-summary-article-ancient-p number)
8068                  (zerop children))
8069             ;; If this is "fetch-old-headered" and `invisible', then
8070             ;; we don't want this article.
8071             (and (eq gnus-fetch-old-headers 'invisible)
8072                  (gnus-summary-article-ancient-p number))
8073             ;; If this is a sparsely inserted article with no children,
8074             ;; we don't want it.
8075             (and (eq gnus-build-sparse-threads 'some)
8076                  (gnus-summary-article-sparse-p number)
8077                  (zerop children))
8078             ;; If we use expunging, and this article is really
8079             ;; low-scored, then we don't want this article.
8080             (when (and gnus-summary-expunge-below
8081                        (< (setq score
8082                                 (or (cdr (assq number gnus-newsgroup-scored))
8083                                     gnus-summary-default-score))
8084                           gnus-summary-expunge-below))
8085               ;; We increase the expunge-tally here, but that has
8086               ;; nothing to do with the limits, really.
8087               (incf gnus-newsgroup-expunged-tally)
8088               ;; We also mark as read here, if that's wanted.
8089               (when (and gnus-summary-mark-below
8090                          (< score gnus-summary-mark-below))
8091                 (setq gnus-newsgroup-unreads
8092                       (delq number gnus-newsgroup-unreads))
8093                 (if gnus-newsgroup-auto-expire
8094                     (push number gnus-newsgroup-expirable)
8095                   (push (cons number gnus-low-score-mark)
8096                         gnus-newsgroup-reads)))
8097               t)
8098             ;; Do the `display' group parameter.
8099             (and gnus-newsgroup-display
8100                  (not (funcall gnus-newsgroup-display)))
8101             ;; Check NoCeM things.
8102             (when (and gnus-use-nocem
8103                        (gnus-nocem-unwanted-article-p
8104                         (mail-header-id (car thread))))
8105               (setq gnus-newsgroup-unreads
8106                     (delq number gnus-newsgroup-unreads))
8107               t)))
8108           ;; Nope, invisible article.
8109           0
8110         ;; Ok, this article is to be visible, so we add it to the limit
8111         ;; and return 1.
8112         (push number gnus-newsgroup-limit)
8113         1))))
8114
8115 (defun gnus-expunge-thread (thread)
8116   "Mark all articles in THREAD as read."
8117   (let* ((number (mail-header-number (car thread))))
8118     (incf gnus-newsgroup-expunged-tally)
8119     ;; We also mark as read here, if that's wanted.
8120     (setq gnus-newsgroup-unreads
8121           (delq number gnus-newsgroup-unreads))
8122     (if gnus-newsgroup-auto-expire
8123         (push number gnus-newsgroup-expirable)
8124       (push (cons number gnus-low-score-mark)
8125             gnus-newsgroup-reads)))
8126   ;; Go recursively through all subthreads.
8127   (mapcar 'gnus-expunge-thread (cdr thread)))
8128
8129 ;; Summary article oriented commands
8130
8131 (defun gnus-summary-refer-parent-article (n)
8132   "Refer parent article N times.
8133 If N is negative, go to ancestor -N instead.
8134 The difference between N and the number of articles fetched is returned."
8135   (interactive "p")
8136   (let ((skip 1)
8137         error header ref)
8138     (when (not (natnump n))
8139       (setq skip (abs n)
8140             n 1))
8141     (while (and (> n 0)
8142                 (not error))
8143       (setq header (gnus-summary-article-header))
8144       (if (and (eq (mail-header-number header)
8145                    (cdr gnus-article-current))
8146                (equal gnus-newsgroup-name
8147                       (car gnus-article-current)))
8148           ;; If we try to find the parent of the currently
8149           ;; displayed article, then we take a look at the actual
8150           ;; References header, since this is slightly more
8151           ;; reliable than the References field we got from the
8152           ;; server.
8153           (save-excursion
8154             (set-buffer gnus-original-article-buffer)
8155             (nnheader-narrow-to-headers)
8156             (unless (setq ref (message-fetch-field "references"))
8157               (when (setq ref (message-fetch-field "in-reply-to"))
8158                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8159             (widen))
8160         (setq ref
8161               ;; It's not the current article, so we take a bet on
8162               ;; the value we got from the server.
8163               (mail-header-references header)))
8164       (if (and ref
8165                (not (equal ref "")))
8166           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8167             (gnus-message 1 "Couldn't find parent"))
8168         (gnus-message 1 "No references in article %d"
8169                       (gnus-summary-article-number))
8170         (setq error t))
8171       (decf n))
8172     (gnus-summary-position-point)
8173     n))
8174
8175 (defun gnus-summary-refer-references ()
8176   "Fetch all articles mentioned in the References header.
8177 Return the number of articles fetched."
8178   (interactive)
8179   (let ((ref (mail-header-references (gnus-summary-article-header)))
8180         (current (gnus-summary-article-number))
8181         (n 0))
8182     (if (or (not ref)
8183             (equal ref ""))
8184         (error "No References in the current article")
8185       ;; For each Message-ID in the References header...
8186       (while (string-match "<[^>]*>" ref)
8187         (incf n)
8188         ;; ... fetch that article.
8189         (gnus-summary-refer-article
8190          (prog1 (match-string 0 ref)
8191            (setq ref (substring ref (match-end 0))))))
8192       (gnus-summary-goto-subject current)
8193       (gnus-summary-position-point)
8194       n)))
8195
8196 (defun gnus-summary-refer-thread (&optional limit)
8197   "Fetch all articles in the current thread.
8198 If LIMIT (the numerical prefix), fetch that many old headers instead
8199 of what's specified by the `gnus-refer-thread-limit' variable."
8200   (interactive "P")
8201   (let ((id (mail-header-id (gnus-summary-article-header)))
8202         (limit (if limit (prefix-numeric-value limit)
8203                  gnus-refer-thread-limit)))
8204     (unless (eq gnus-fetch-old-headers 'invisible)
8205       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8206       ;; Retrieve the headers and read them in.
8207       (if (eq (if (numberp limit)
8208                   (gnus-retrieve-headers
8209                    (list (min
8210                           (+ (mail-header-number
8211                               (gnus-summary-article-header))
8212                              limit)
8213                           gnus-newsgroup-end))
8214                    gnus-newsgroup-name (* limit 2))
8215                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8216                 ;; headers.
8217                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8218                                        gnus-newsgroup-name limit))
8219               'nov)
8220           (gnus-build-all-threads)
8221         (error "Can't fetch thread from back ends that don't support NOV"))
8222       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8223     (gnus-summary-limit-include-thread id)))
8224
8225 (defun gnus-summary-refer-article (message-id)
8226   "Fetch an article specified by MESSAGE-ID."
8227   (interactive "sMessage-ID: ")
8228   (when (and (stringp message-id)
8229              (not (zerop (length message-id))))
8230     (setq message-id (gnus-replace-in-string message-id " " ""))
8231     ;; Construct the correct Message-ID if necessary.
8232     ;; Suggested by tale@pawl.rpi.edu.
8233     (unless (string-match "^<" message-id)
8234       (setq message-id (concat "<" message-id)))
8235     (unless (string-match ">$" message-id)
8236       (setq message-id (concat message-id ">")))
8237     ;; People often post MIDs from URLs, so unhex it:
8238     (unless (string-match "@" message-id)
8239       (setq message-id (gnus-url-unhex-string message-id)))
8240     (let* ((header (gnus-id-to-header message-id))
8241            (sparse (and header
8242                         (gnus-summary-article-sparse-p
8243                          (mail-header-number header))
8244                         (memq (mail-header-number header)
8245                               gnus-newsgroup-limit)))
8246            number)
8247       (cond
8248        ;; If the article is present in the buffer we just go to it.
8249        ((and header
8250              (or (not (gnus-summary-article-sparse-p
8251                        (mail-header-number header)))
8252                  sparse))
8253         (prog1
8254             (gnus-summary-goto-article
8255              (mail-header-number header) nil t)
8256           (when sparse
8257             (gnus-summary-update-article (mail-header-number header)))))
8258        (t
8259         ;; We fetch the article.
8260         (catch 'found
8261           (dolist (gnus-override-method (gnus-refer-article-methods))
8262             (when (and (gnus-check-server gnus-override-method)
8263                        ;; Fetch the header,
8264                        (setq number (gnus-summary-insert-subject message-id)))
8265               ;; and display the article.
8266               (gnus-summary-select-article nil nil nil number)
8267               (throw 'found t)))
8268           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8269
8270 (defun gnus-refer-article-methods ()
8271   "Return a list of referable methods."
8272   (cond
8273    ;; No method, so we default to current and native.
8274    ((null gnus-refer-article-method)
8275     (list gnus-current-select-method gnus-select-method))
8276    ;; Current.
8277    ((eq 'current gnus-refer-article-method)
8278     (list gnus-current-select-method))
8279    ;; List of select methods.
8280    ((not (and (symbolp (car gnus-refer-article-method))
8281               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8282     (let (out)
8283       (dolist (method gnus-refer-article-method)
8284         (push (if (eq 'current method)
8285                   gnus-current-select-method
8286                 method)
8287               out))
8288       (nreverse out)))
8289    ;; One single select method.
8290    (t
8291     (list gnus-refer-article-method))))
8292
8293 (defun gnus-summary-edit-parameters ()
8294   "Edit the group parameters of the current group."
8295   (interactive)
8296   (gnus-group-edit-group gnus-newsgroup-name 'params))
8297
8298 (defun gnus-summary-customize-parameters ()
8299   "Customize the group parameters of the current group."
8300   (interactive)
8301   (gnus-group-customize gnus-newsgroup-name))
8302
8303 (defun gnus-summary-enter-digest-group (&optional force)
8304   "Enter an nndoc group based on the current article.
8305 If FORCE, force a digest interpretation.  If not, try
8306 to guess what the document format is."
8307   (interactive "P")
8308   (let ((conf gnus-current-window-configuration))
8309     (save-window-excursion
8310       (save-excursion
8311         (let (gnus-article-prepare-hook
8312               gnus-display-mime-function
8313               gnus-break-pages)
8314           (gnus-summary-select-article))))
8315     (setq gnus-current-window-configuration conf)
8316     (let* ((name (format "%s-%d"
8317                          (gnus-group-prefixed-name
8318                           gnus-newsgroup-name (list 'nndoc ""))
8319                          (with-current-buffer gnus-summary-buffer
8320                            gnus-current-article)))
8321            (ogroup gnus-newsgroup-name)
8322            (params (append (gnus-info-params (gnus-get-info ogroup))
8323                            (list (cons 'to-group ogroup))
8324                            (list (cons 'parent-group ogroup))
8325                            (list (cons 'save-article-group ogroup))))
8326            (case-fold-search t)
8327            (buf (current-buffer))
8328            dig to-address)
8329       (save-excursion
8330         (set-buffer gnus-original-article-buffer)
8331         ;; Have the digest group inherit the main mail address of
8332         ;; the parent article.
8333         (when (setq to-address (or (gnus-fetch-field "reply-to")
8334                                    (gnus-fetch-field "from")))
8335           (setq params (append
8336                         (list (cons 'to-address
8337                                     (funcall gnus-decode-encoded-word-function
8338                                              to-address))))))
8339         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8340         (insert-buffer-substring gnus-original-article-buffer)
8341         ;; Remove lines that may lead nndoc to misinterpret the
8342         ;; document type.
8343         (narrow-to-region
8344          (goto-char (point-min))
8345          (or (search-forward "\n\n" nil t) (point)))
8346         (goto-char (point-min))
8347         (delete-matching-lines "^Path:\\|^From ")
8348         (widen))
8349       (unwind-protect
8350           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8351                     (gnus-newsgroup-ephemeral-ignored-charsets
8352                      gnus-newsgroup-ignored-charsets))
8353                 (gnus-group-read-ephemeral-group
8354                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8355                               (nndoc-article-type
8356                                ,(if force 'mbox 'guess)))
8357                  t nil nil nil
8358                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8359                                                         "ADAPT")))))
8360               ;; Make all postings to this group go to the parent group.
8361               (nconc (gnus-info-params (gnus-get-info name))
8362                      params)
8363             ;; Couldn't select this doc group.
8364             (switch-to-buffer buf)
8365             (gnus-set-global-variables)
8366             (gnus-configure-windows 'summary)
8367             (gnus-message 3 "Article couldn't be entered?"))
8368         (kill-buffer dig)))))
8369
8370 (defun gnus-summary-read-document (n)
8371   "Open a new group based on the current article(s).
8372 This will allow you to read digests and other similar
8373 documents as newsgroups.
8374 Obeys the standard process/prefix convention."
8375   (interactive "P")
8376   (let* ((articles (gnus-summary-work-articles n))
8377          (ogroup gnus-newsgroup-name)
8378          (params (append (gnus-info-params (gnus-get-info ogroup))
8379                          (list (cons 'to-group ogroup))))
8380          article group egroup groups vgroup)
8381     (while (setq article (pop articles))
8382       (setq group (format "%s-%d" gnus-newsgroup-name article))
8383       (gnus-summary-remove-process-mark article)
8384       (when (gnus-summary-display-article article)
8385         (save-excursion
8386           (with-temp-buffer
8387             (insert-buffer-substring gnus-original-article-buffer)
8388             ;; Remove some headers that may lead nndoc to make
8389             ;; the wrong guess.
8390             (message-narrow-to-head)
8391             (goto-char (point-min))
8392             (delete-matching-lines "^Path:\\|^From ")
8393             (widen)
8394             (if (setq egroup
8395                       (gnus-group-read-ephemeral-group
8396                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8397                                      (nndoc-article-type guess))
8398                        t nil t))
8399                 (progn
8400                   ;; Make all postings to this group go to the parent group.
8401                   (nconc (gnus-info-params (gnus-get-info egroup))
8402                          params)
8403                   (push egroup groups))
8404               ;; Couldn't select this doc group.
8405               (gnus-error 3 "Article couldn't be entered"))))))
8406     ;; Now we have selected all the documents.
8407     (cond
8408      ((not groups)
8409       (error "None of the articles could be interpreted as documents"))
8410      ((gnus-group-read-ephemeral-group
8411        (setq vgroup (format
8412                      "nnvirtual:%s-%s" gnus-newsgroup-name
8413                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8414        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8415        t
8416        (cons (current-buffer) 'summary)))
8417      (t
8418       (error "Couldn't select virtual nndoc group")))))
8419
8420 (defun gnus-summary-isearch-article (&optional regexp-p)
8421   "Do incremental search forward on the current article.
8422 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8423   (interactive "P")
8424   (let* ((gnus-inhibit-treatment t)
8425          (old (gnus-summary-select-article)))
8426     (gnus-configure-windows 'article)
8427     (gnus-eval-in-buffer-window gnus-article-buffer
8428       (save-restriction
8429         (widen)
8430         (when (eq 'old old)
8431           (gnus-article-show-all-headers))
8432         (goto-char (point-min))
8433         (isearch-forward regexp-p)))))
8434
8435 (defun gnus-summary-search-article-forward (regexp &optional backward)
8436   "Search for an article containing REGEXP forward.
8437 If BACKWARD, search backward instead."
8438   (interactive
8439    (list (read-string
8440           (format "Search article %s (regexp%s): "
8441                   (if current-prefix-arg "backward" "forward")
8442                   (if gnus-last-search-regexp
8443                       (concat ", default " gnus-last-search-regexp)
8444                     "")))
8445          current-prefix-arg))
8446   (if (string-equal regexp "")
8447       (setq regexp (or gnus-last-search-regexp ""))
8448     (setq gnus-last-search-regexp regexp)
8449     (setq gnus-article-before-search gnus-current-article))
8450   ;; Intentionally set gnus-last-article.
8451   (setq gnus-last-article gnus-article-before-search)
8452   (let ((gnus-last-article gnus-last-article))
8453     (if (gnus-summary-search-article regexp backward)
8454         (gnus-summary-show-thread)
8455       (signal 'search-failed (list regexp)))))
8456
8457 (defun gnus-summary-search-article-backward (regexp)
8458   "Search for an article containing REGEXP backward."
8459   (interactive
8460    (list (read-string
8461           (format "Search article backward (regexp%s): "
8462                   (if gnus-last-search-regexp
8463                       (concat ", default " gnus-last-search-regexp)
8464                     "")))))
8465   (gnus-summary-search-article-forward regexp 'backward))
8466
8467 (eval-when-compile
8468   (defmacro gnus-summary-search-article-position-point (regexp backward)
8469     "Dehighlight the last matched text and goto the beginning position."
8470     (` (if (and gnus-summary-search-article-matched-data
8471                 (let ((text (caddr gnus-summary-search-article-matched-data))
8472                       (inhibit-read-only t)
8473                       buffer-read-only)
8474                   (delete-region
8475                    (goto-char (car gnus-summary-search-article-matched-data))
8476                    (cadr gnus-summary-search-article-matched-data))
8477                   (insert text)
8478                   (string-match (, regexp) text)))
8479            (if (, backward) (beginning-of-line) (end-of-line))
8480          (goto-char (if (, backward) (point-max) (point-min))))))
8481
8482   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8483     "Place point where X-Face image is displayed."
8484     (if (featurep 'xemacs)
8485         (` (let ((end (if (search-forward "\n\n" nil t)
8486                           (goto-char (1- (point)))
8487                         (point-min)))
8488                  extent)
8489              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8490              (unless (and (re-search-forward "^From:" end t)
8491                           (setq extent (extent-at (point)))
8492                           (extent-begin-glyph extent))
8493                (goto-char (, opoint)))))
8494       (` (let ((end (if (search-forward "\n\n" nil t)
8495                         (goto-char (1- (point)))
8496                       (point-min)))
8497                (start (or (search-backward "\n\n" nil t) (point-min))))
8498            (goto-char
8499             (or (text-property-any start end 'x-face-image t);; x-face-e21
8500                 (text-property-any start end 'x-face-mule-bitmap-image t)
8501                 (, opoint)))))))
8502
8503   (defmacro gnus-summary-search-article-highlight-matched-text
8504     (backward treated x-face)
8505     "Highlight matched text in the function `gnus-summary-search-article'."
8506     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8507              (end (set-marker (make-marker) (match-end 0)))
8508              (inhibit-read-only t)
8509              buffer-read-only)
8510          (unless treated
8511            (let ((,@
8512                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8513                     (mapcar
8514                      (lambda (item) (setq items (delq item items)))
8515                      '(gnus-treat-buttonize
8516                        gnus-treat-fill-article
8517                        gnus-treat-fill-long-lines
8518                        gnus-treat-emphasize
8519                        gnus-treat-highlight-headers
8520                        gnus-treat-highlight-citation
8521                        gnus-treat-highlight-signature
8522                        gnus-treat-overstrike
8523                        gnus-treat-display-x-face
8524                        gnus-treat-buttonize-head
8525                        gnus-treat-decode-article-as-default-mime-charset))
8526                     (static-if (featurep 'xemacs)
8527                         items
8528                       (cons '(x-face-mule-delete-x-face-field
8529                               (quote never))
8530                             items))))
8531                  (gnus-treat-display-x-face
8532                   (when (, x-face) gnus-treat-display-x-face)))
8533              (gnus-article-prepare-mime-display)))
8534          (goto-char (if (, backward) start end))
8535          (when (, x-face)
8536            (gnus-summary-search-article-highlight-goto-x-face (point)))
8537          (setq gnus-summary-search-article-matched-data
8538                (list start end (buffer-substring start end)))
8539          (unless (eq start end);; matched text has been deleted. :-<
8540            (put-text-property start end 'face
8541                               (or (find-face 'isearch)
8542                                   'secondary-selection))))))
8543   )
8544
8545 (defun gnus-summary-search-article (regexp &optional backward)
8546   "Search for an article containing REGEXP.
8547 Optional argument BACKWARD means do search for backward.
8548 `gnus-select-article-hook' is not called during the search."
8549   ;; We have to require this here to make sure that the following
8550   ;; dynamic binding isn't shadowed by autoloading.
8551   (require 'gnus-async)
8552   (require 'gnus-art)
8553   (let ((gnus-select-article-hook nil)  ;Disable hook.
8554         (gnus-article-prepare-hook nil)
8555         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8556         (gnus-use-article-prefetch nil)
8557         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8558         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8559         (gnus-visual nil)
8560         (gnus-keep-backlog nil)
8561         (gnus-break-pages nil)
8562         (gnus-summary-display-arrow nil)
8563         (gnus-updated-mode-lines nil)
8564         (gnus-auto-center-summary nil)
8565         (sum (current-buffer))
8566         (found nil)
8567         point treated)
8568     (gnus-save-hidden-threads
8569       (static-if (featurep 'xemacs)
8570           (let ((gnus-inhibit-treatment t))
8571             (setq treated (eq 'old (gnus-summary-select-article)))
8572             (when (and treated
8573                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8574                                  (window-live-p (get-buffer-window
8575                                                  gnus-article-buffer t)))))
8576               (gnus-summary-select-article nil t)
8577               (setq treated nil)))
8578         (let ((gnus-inhibit-treatment t)
8579               (x-face-mule-delete-x-face-field 'never))
8580           (setq treated (eq 'old (gnus-summary-select-article)))
8581           (when (and treated
8582                      (not
8583                       (and (gnus-buffer-live-p gnus-article-buffer)
8584                            (window-live-p (get-buffer-window
8585                                            gnus-article-buffer t))
8586                            (or (not (string-match "^\\^X-Face:" regexp))
8587                                (with-current-buffer gnus-article-buffer
8588                                  gnus-summary-search-article-matched-data)))))
8589             (gnus-summary-select-article nil t)
8590             (setq treated nil))))
8591       (set-buffer gnus-article-buffer)
8592       (widen)
8593       (if treated
8594           (progn
8595             (gnus-article-show-all-headers)
8596             (gnus-summary-search-article-position-point regexp backward))
8597         (goto-char (if backward (point-max) (point-min))))
8598       (while (not found)
8599         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8600         (if (if backward
8601                 (re-search-backward regexp nil t)
8602               (re-search-forward regexp nil t))
8603             ;; We found the regexp.
8604             (progn
8605               (gnus-summary-search-article-highlight-matched-text
8606                backward treated (string-match "^\\^X-Face:" regexp))
8607               (setq found 'found)
8608               (forward-line
8609                (/ (- 2 (window-height
8610                         (get-buffer-window gnus-article-buffer t)))
8611                   2))
8612               (set-window-start
8613                (get-buffer-window (current-buffer))
8614                (point))
8615               (set-buffer sum)
8616               (setq point (point)))
8617           ;; We didn't find it, so we go to the next article.
8618           (set-buffer sum)
8619           (setq found 'not)
8620           (while (eq found 'not)
8621             (if (not (if backward (gnus-summary-find-prev)
8622                        (gnus-summary-find-next)))
8623                 ;; No more articles.
8624                 (setq found t)
8625               ;; Select the next article and adjust point.
8626               (unless (gnus-summary-article-sparse-p
8627                        (gnus-summary-article-number))
8628                 (setq found nil)
8629                 (let ((gnus-inhibit-treatment t))
8630                   (gnus-summary-select-article))
8631                 (setq treated nil)
8632                 (set-buffer gnus-article-buffer)
8633                 (widen)
8634                 (goto-char (if backward (point-max) (point-min))))))))
8635       (gnus-message 7 ""))
8636     ;; Return whether we found the regexp.
8637     (when (eq found 'found)
8638       (goto-char point)
8639       (gnus-summary-show-thread)
8640       (gnus-summary-goto-subject gnus-current-article)
8641       (gnus-summary-position-point)
8642       t)))
8643
8644 (defun gnus-find-matching-articles (header regexp)
8645   "Return a list of all articles that match REGEXP on HEADER.
8646 This search includes all articles in the current group that Gnus has
8647 fetched headers for, whether they are displayed or not."
8648   (let ((articles nil)
8649         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8650         (case-fold-search t))
8651     (dolist (header gnus-newsgroup-headers)
8652       (when (string-match regexp (funcall func header))
8653         (push (mail-header-number header) articles)))
8654     (nreverse articles)))
8655
8656 (defun gnus-summary-find-matching (header regexp &optional backward unread
8657                                           not-case-fold not-matching)
8658   "Return a list of all articles that match REGEXP on HEADER.
8659 The search stars on the current article and goes forwards unless
8660 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8661 If UNREAD is non-nil, only unread articles will
8662 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8663 in the comparisons. If NOT-MATCHING, return a list of all articles that
8664 not match REGEXP on HEADER."
8665   (let ((case-fold-search (not not-case-fold))
8666         articles d func)
8667     (if (consp header)
8668         (if (eq (car header) 'extra)
8669             (setq func
8670                   `(lambda (h)
8671                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8672                          "")))
8673           (error "%s is an invalid header" header))
8674       (unless (fboundp (intern (concat "mail-header-" header)))
8675         (error "%s is not a valid header" header))
8676       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8677     (dolist (d (if (eq backward 'all)
8678                    gnus-newsgroup-data
8679                  (gnus-data-find-list
8680                   (gnus-summary-article-number)
8681                   (gnus-data-list backward))))
8682       (when (and (or (not unread)       ; We want all articles...
8683                      (gnus-data-unread-p d)) ; Or just unreads.
8684                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8685                  (if not-matching
8686                      (not (string-match
8687                            regexp
8688                            (funcall func (gnus-data-header d))))
8689                    (string-match regexp
8690                                  (funcall func (gnus-data-header d)))))
8691         (push (gnus-data-number d) articles))) ; Success!
8692     (nreverse articles)))
8693
8694 (defun gnus-summary-execute-command (header regexp command &optional backward)
8695   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8696 If HEADER is an empty string (or nil), the match is done on the entire
8697 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8698   (interactive
8699    (list (let ((completion-ignore-case t))
8700            (completing-read
8701             "Header name: "
8702             (mapcar (lambda (header) (list (format "%s" header)))
8703                     (append
8704                      '("Number" "Subject" "From" "Lines" "Date"
8705                        "Message-ID" "Xref" "References" "Body")
8706                      gnus-extra-headers))
8707             nil 'require-match))
8708          (read-string "Regexp: ")
8709          (read-key-sequence "Command: ")
8710          current-prefix-arg))
8711   (when (equal header "Body")
8712     (setq header ""))
8713   ;; Hidden thread subtrees must be searched as well.
8714   (gnus-summary-show-all-threads)
8715   ;; We don't want to change current point nor window configuration.
8716   (save-excursion
8717     (save-window-excursion
8718       (let (gnus-visual
8719             gnus-treat-strip-trailing-blank-lines
8720             gnus-treat-strip-leading-blank-lines
8721             gnus-treat-strip-multiple-blank-lines
8722             gnus-treat-hide-boring-headers
8723             gnus-treat-fold-newsgroups
8724             gnus-article-prepare-hook)
8725         (gnus-message 6 "Executing %s..." (key-description command))
8726         ;; We'd like to execute COMMAND interactively so as to give arguments.
8727         (gnus-execute header regexp
8728                       `(call-interactively ',(key-binding command))
8729                       backward)
8730         (gnus-message 6 "Executing %s...done" (key-description command))))))
8731
8732 (defun gnus-summary-beginning-of-article ()
8733   "Scroll the article back to the beginning."
8734   (interactive)
8735   (gnus-summary-select-article)
8736   (gnus-configure-windows 'article)
8737   (gnus-eval-in-buffer-window gnus-article-buffer
8738     (widen)
8739     (goto-char (point-min))
8740     (when gnus-break-pages
8741       (gnus-narrow-to-page))))
8742
8743 (defun gnus-summary-end-of-article ()
8744   "Scroll to the end of the article."
8745   (interactive)
8746   (gnus-summary-select-article)
8747   (gnus-configure-windows 'article)
8748   (gnus-eval-in-buffer-window gnus-article-buffer
8749     (widen)
8750     (goto-char (point-max))
8751     (recenter -3)
8752     (when gnus-break-pages
8753       (when (re-search-backward page-delimiter nil t)
8754         (narrow-to-region (match-end 0) (point-max)))
8755       (gnus-narrow-to-page))))
8756
8757 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8758   "Truncate to LEN and quote all \"(\"'s in STRING."
8759   (gnus-replace-in-string (if (and len (> (length string) len))
8760                               (substring string 0 len)
8761                             string)
8762                           "[()]" "\\\\\\&"))
8763
8764 (defun gnus-summary-print-article (&optional filename n)
8765   "Generate and print a PostScript image of the process-marked (mail) articles.
8766
8767 If used interactively, print the current article if none are
8768 process-marked.  With prefix arg, prompt the user for the name of the
8769 file to save in.
8770
8771 When used from Lisp, accept two optional args FILENAME and N.  N means
8772 to print the next N articles.  If N is negative, print the N previous
8773 articles.  If N is nil and articles have been marked with the process
8774 mark, print these instead.
8775
8776 If the optional first argument FILENAME is nil, send the image to the
8777 printer.  If FILENAME is a string, save the PostScript image in a file with
8778 that name.  If FILENAME is a number, prompt the user for the name of the file
8779 to save in."
8780   (interactive (list (ps-print-preprint current-prefix-arg)))
8781   (dolist (article (gnus-summary-work-articles n))
8782     (gnus-summary-select-article nil nil 'pseudo article)
8783     (gnus-eval-in-buffer-window gnus-article-buffer
8784       (gnus-print-buffer))
8785     (gnus-summary-remove-process-mark article))
8786   (ps-despool filename))
8787
8788 (defun gnus-print-buffer ()
8789   (let ((buffer (generate-new-buffer " *print*")))
8790     (unwind-protect
8791         (progn
8792           (copy-to-buffer buffer (point-min) (point-max))
8793           (set-buffer buffer)
8794           (gnus-remove-text-with-property 'gnus-decoration)
8795           (when (gnus-visual-p 'article-highlight 'highlight)
8796             ;; Copy-to-buffer doesn't copy overlay.  So redo
8797             ;; highlight.
8798             (let ((gnus-article-buffer buffer))
8799               (gnus-article-highlight-citation t)
8800               (gnus-article-highlight-signature)
8801               (gnus-article-emphasize)
8802               (gnus-article-delete-invisible-text)))
8803           (let ((ps-left-header
8804                  (list
8805                   (concat "("
8806                           (gnus-summary-print-truncate-and-quote
8807                            (mail-header-subject gnus-current-headers)
8808                            66) ")")
8809                   (concat "("
8810                           (gnus-summary-print-truncate-and-quote
8811                            (mail-header-from gnus-current-headers)
8812                            45) ")")))
8813                 (ps-right-header
8814                  (list
8815                   "/pagenumberstring load"
8816                   (concat "("
8817                           (mail-header-date gnus-current-headers) ")"))))
8818             (gnus-run-hooks 'gnus-ps-print-hook)
8819             (save-excursion
8820               (if window-system
8821                   (ps-spool-buffer-with-faces)
8822                 (ps-spool-buffer)))))
8823       (kill-buffer buffer))))
8824
8825 (defun gnus-summary-show-article (&optional arg)
8826   "Force redisplaying of the current article.
8827 If ARG (the prefix) is a number, show the article with the charset
8828 defined in `gnus-summary-show-article-charset-alist', or the charset
8829 input.
8830 If ARG (the prefix) is non-nil and not a number, show the raw article
8831 without any article massaging functions being run.  Normally, the key
8832 strokes are `C-u g'."
8833   (interactive "P")
8834   (cond
8835    ((numberp arg)
8836     (gnus-summary-show-article t)
8837     (let* ((gnus-newsgroup-charset
8838             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8839                 (mm-read-coding-system
8840                  "View as charset: " ;; actually it is coding system.
8841                  (save-excursion
8842                    (set-buffer gnus-article-buffer)
8843                    (mm-detect-coding-region (point) (point-max))))))
8844            (default-mime-charset gnus-newsgroup-charset)
8845            (gnus-newsgroup-ignored-charsets 'gnus-all))
8846       (gnus-summary-select-article nil 'force)
8847       (let ((deps gnus-newsgroup-dependencies)
8848             head header lines)
8849         (save-excursion
8850           (set-buffer gnus-original-article-buffer)
8851           (save-restriction
8852             (message-narrow-to-head)
8853             (setq head (buffer-string))
8854             (goto-char (point-min))
8855             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8856               (goto-char (point-max))
8857               (widen)
8858               (setq lines (1- (count-lines (point) (point-max))))))
8859           (with-temp-buffer
8860             (insert (format "211 %d Article retrieved.\n"
8861                             (cdr gnus-article-current)))
8862             (insert head)
8863             (if lines (insert (format "Lines: %d\n" lines)))
8864             (insert ".\n")
8865             (let ((nntp-server-buffer (current-buffer)))
8866               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8867         (gnus-data-set-header
8868          (gnus-data-find (cdr gnus-article-current))
8869          header)
8870         (gnus-summary-update-article-line
8871          (cdr gnus-article-current) header)
8872         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8873           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8874    ((not arg)
8875     ;; Select the article the normal way.
8876     (gnus-summary-select-article nil 'force))
8877    (t
8878     ;; We have to require this here to make sure that the following
8879     ;; dynamic binding isn't shadowed by autoloading.
8880     (require 'gnus-async)
8881     (require 'gnus-art)
8882     ;; Bind the article treatment functions to nil.
8883     (let ((gnus-have-all-headers t)
8884           gnus-article-prepare-hook
8885           gnus-article-decode-hook
8886           gnus-break-pages
8887           gnus-show-mime
8888           (gnus-inhibit-treatment t))
8889       (gnus-summary-select-article nil 'force))))
8890   (gnus-summary-goto-subject gnus-current-article)
8891   (gnus-summary-position-point))
8892
8893 (defun gnus-summary-show-raw-article ()
8894   "Show the raw article without any article massaging functions being run."
8895   (interactive)
8896   (gnus-summary-show-article t))
8897
8898 (defun gnus-summary-verbose-headers (&optional arg)
8899   "Toggle permanent full header display.
8900 If ARG is a positive number, turn header display on.
8901 If ARG is a negative number, turn header display off."
8902   (interactive "P")
8903   (setq gnus-show-all-headers
8904         (cond ((or (not (numberp arg))
8905                    (zerop arg))
8906                (not gnus-show-all-headers))
8907               ((natnump arg)
8908                t)))
8909   (gnus-summary-show-article))
8910
8911 (defun gnus-summary-toggle-header (&optional arg)
8912   "Show the headers if they are hidden, or hide them if they are shown.
8913 If ARG is a positive number, show the entire header.
8914 If ARG is a negative number, hide the unwanted header lines."
8915   (interactive "P")
8916   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8917                      (get-buffer-window gnus-article-buffer t))))
8918     (with-current-buffer gnus-article-buffer
8919       (widen)
8920       (article-narrow-to-head)
8921       (let* ((buffer-read-only nil)
8922              (inhibit-point-motion-hooks t)
8923              (hidden (if (numberp arg)
8924                          (>= arg 0)
8925                        (gnus-article-hidden-text-p 'headers)))
8926              s e)
8927         (delete-region (point-min) (point-max))
8928         (with-current-buffer gnus-original-article-buffer
8929           (goto-char (setq s (point-min)))
8930           (setq e (if (search-forward "\n\n" nil t)
8931                       (1- (point))
8932                     (point-max))))
8933         (insert-buffer-substring gnus-original-article-buffer s e)
8934         ;; In T-gnus, gnus-article-decode-hook doesn't contain
8935         ;; article-decode-encoded-words by default.
8936         (article-decode-encoded-words)
8937         (run-hooks 'gnus-article-decode-hook)
8938         (if hidden
8939             (let ((gnus-treat-hide-headers nil)
8940                   (gnus-treat-hide-boring-headers nil))
8941               (gnus-delete-wash-type 'headers)
8942               (gnus-treat-article 'head))
8943           (gnus-treat-article 'head))
8944         (widen)
8945         (if window
8946             (set-window-start window (goto-char (point-min))))
8947         (if gnus-break-pages
8948             (gnus-narrow-to-page)
8949           (when (gnus-visual-p 'page-marker)
8950             (let ((buffer-read-only nil))
8951               (gnus-remove-text-with-property 'gnus-prev)
8952               (gnus-remove-text-with-property 'gnus-next))))
8953         (gnus-set-mode-line 'article)))))
8954
8955 (defun gnus-summary-show-all-headers ()
8956   "Make all header lines visible."
8957   (interactive)
8958   (gnus-summary-toggle-header 1))
8959
8960 (defun gnus-summary-toggle-mime (&optional arg)
8961   "Toggle MIME processing.
8962 If ARG is a positive number, turn MIME processing on."
8963   (interactive "P")
8964   (setq gnus-show-mime
8965         (if (null arg)
8966             (not gnus-show-mime)
8967           (> (prefix-numeric-value arg) 0)))
8968   (gnus-summary-select-article t 'force))
8969
8970 (defun gnus-summary-caesar-message (&optional arg)
8971   "Caesar rotate the current article by 13.
8972 The numerical prefix specifies how many places to rotate each letter
8973 forward."
8974   (interactive "P")
8975   (gnus-summary-select-article)
8976   (let ((mail-header-separator ""))
8977     (gnus-eval-in-buffer-window gnus-article-buffer
8978       (save-restriction
8979         (widen)
8980         (let ((start (window-start))
8981               buffer-read-only)
8982           (message-caesar-buffer-body arg)
8983           (set-window-start (get-buffer-window (current-buffer)) start))))))
8984
8985 (autoload 'unmorse-region "morse"
8986   "Convert morse coded text in region to ordinary ASCII text."
8987   t)
8988
8989 (defun gnus-summary-morse-message (&optional arg)
8990   "Morse decode the current article."
8991   (interactive "P")
8992   (gnus-summary-select-article)
8993   (let ((mail-header-separator ""))
8994     (gnus-eval-in-buffer-window gnus-article-buffer
8995       (save-excursion
8996         (save-restriction
8997           (widen)
8998           (let ((pos (window-start))
8999                 buffer-read-only)
9000             (goto-char (point-min))
9001             (when (message-goto-body)
9002               (gnus-narrow-to-body))
9003             (goto-char (point-min))
9004             (while (search-forward "·" (point-max) t)
9005               (replace-match "."))
9006             (unmorse-region (point-min) (point-max))
9007             (widen)
9008             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9009
9010 (defun gnus-summary-stop-page-breaking ()
9011   "Stop page breaking in the current article."
9012   (interactive)
9013   (gnus-summary-select-article)
9014   (gnus-eval-in-buffer-window gnus-article-buffer
9015     (widen)
9016     (when (gnus-visual-p 'page-marker)
9017       (let ((buffer-read-only nil))
9018         (gnus-remove-text-with-property 'gnus-prev)
9019         (gnus-remove-text-with-property 'gnus-next))
9020       (setq gnus-page-broken nil))))
9021
9022 (defun gnus-summary-move-article (&optional n to-newsgroup
9023                                             select-method action)
9024   "Move the current article to a different newsgroup.
9025 If N is a positive number, move the N next articles.
9026 If N is a negative number, move the N previous articles.
9027 If N is nil and any articles have been marked with the process mark,
9028 move those articles instead.
9029 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9030 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9031 re-spool using this method.
9032
9033 When called interactively with TO-NEWSGROUP being nil, the value of
9034 the variable `gnus-move-split-methods' is used for finding a default
9035 for the target newsgroup.
9036
9037 For this function to work, both the current newsgroup and the
9038 newsgroup that you want to move to have to support the `request-move'
9039 and `request-accept' functions.
9040
9041 ACTION can be either `move' (the default), `crosspost' or `copy'."
9042   (interactive "P")
9043   (unless action
9044     (setq action 'move))
9045   ;; Check whether the source group supports the required functions.
9046   (cond ((and (eq action 'move)
9047               (not (gnus-check-backend-function
9048                     'request-move-article gnus-newsgroup-name)))
9049          (error "The current group does not support article moving"))
9050         ((and (eq action 'crosspost)
9051               (not (gnus-check-backend-function
9052                     'request-replace-article gnus-newsgroup-name)))
9053          (error "The current group does not support article editing")))
9054   (let ((articles (gnus-summary-work-articles n))
9055         (prefix (if (gnus-check-backend-function
9056                      'request-move-article gnus-newsgroup-name)
9057                     (funcall gnus-move-group-prefix-function
9058                              gnus-newsgroup-name)
9059                   ""))
9060         (names '((move "Move" "Moving")
9061                  (copy "Copy" "Copying")
9062                  (crosspost "Crosspost" "Crossposting")))
9063         (copy-buf (save-excursion
9064                     (nnheader-set-temp-buffer " *copy article*")))
9065         art-group to-method new-xref article to-groups)
9066     (unless (assq action names)
9067       (error "Unknown action %s" action))
9068     ;; Read the newsgroup name.
9069     (when (and (not to-newsgroup)
9070                (not select-method))
9071       (if (and gnus-move-split-methods
9072                (not
9073                 (and (memq gnus-current-article articles)
9074                      (gnus-buffer-live-p gnus-original-article-buffer))))
9075           ;; When `gnus-move-split-methods' is non-nil, we have to
9076           ;; select an article to give `gnus-read-move-group-name' an
9077           ;; opportunity to suggest an appropriate default.  However,
9078           ;; we needn't render or mark the article.
9079           (let ((gnus-display-mime-function nil)
9080                 (gnus-article-prepare-hook nil)
9081                 (gnus-mark-article-hook nil))
9082             (gnus-summary-select-article nil nil nil (car articles))))
9083       (setq to-newsgroup
9084             (gnus-read-move-group-name
9085              (cadr (assq action names))
9086              (symbol-value (intern (format "gnus-current-%s-group" action)))
9087              articles prefix))
9088       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9089     (setq to-method (or select-method
9090                         (gnus-server-to-method
9091                          (gnus-group-method to-newsgroup))))
9092     ;; Check the method we are to move this article to...
9093     (unless (gnus-check-backend-function
9094              'request-accept-article (car to-method))
9095       (error "%s does not support article copying" (car to-method)))
9096     (unless (gnus-check-server to-method)
9097       (error "Can't open server %s" (car to-method)))
9098     (gnus-message 6 "%s to %s: %s..."
9099                   (caddr (assq action names))
9100                   (or (car select-method) to-newsgroup) articles)
9101     (while articles
9102       (setq article (pop articles))
9103       (setq
9104        art-group
9105        (cond
9106         ;; Move the article.
9107         ((eq action 'move)
9108          ;; Remove this article from future suppression.
9109          (gnus-dup-unsuppress-article article)
9110          (gnus-request-move-article
9111           article                       ; Article to move
9112           gnus-newsgroup-name           ; From newsgroup
9113           (nth 1 (gnus-find-method-for-group
9114                   gnus-newsgroup-name)) ; Server
9115           (list 'gnus-request-accept-article
9116                 to-newsgroup (list 'quote select-method)
9117                 (not articles) t)       ; Accept form
9118           (not articles)))              ; Only save nov last time
9119         ;; Copy the article.
9120         ((eq action 'copy)
9121          (save-excursion
9122            (set-buffer copy-buf)
9123            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9124              (gnus-request-accept-article
9125               to-newsgroup select-method (not articles) t))))
9126         ;; Crosspost the article.
9127         ((eq action 'crosspost)
9128          (let ((xref (message-tokenize-header
9129                       (mail-header-xref (gnus-summary-article-header article))
9130                       " ")))
9131            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9132                                   ":" (number-to-string article)))
9133            (unless xref
9134              (setq xref (list (system-name))))
9135            (setq new-xref
9136                  (concat
9137                   (mapconcat 'identity
9138                              (delete "Xref:" (delete new-xref xref))
9139                              " ")
9140                   " " new-xref))
9141            (save-excursion
9142              (set-buffer copy-buf)
9143              ;; First put the article in the destination group.
9144              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9145              (when (consp (setq art-group
9146                                 (gnus-request-accept-article
9147                                  to-newsgroup select-method (not articles))))
9148                (setq new-xref (concat new-xref " " (car art-group)
9149                                       ":"
9150                                       (number-to-string (cdr art-group))))
9151                ;; Now we have the new Xrefs header, so we insert
9152                ;; it and replace the new article.
9153                (nnheader-replace-header "Xref" new-xref)
9154                (gnus-request-replace-article
9155                 (cdr art-group) to-newsgroup (current-buffer))
9156                art-group))))))
9157       (cond
9158        ((not art-group)
9159         (gnus-message 1 "Couldn't %s article %s: %s"
9160                       (cadr (assq action names)) article
9161                       (nnheader-get-report (car to-method))))
9162        ((eq art-group 'junk)
9163         (when (eq action 'move)
9164           (gnus-summary-mark-article article gnus-canceled-mark)
9165           (gnus-message 4 "Deleted article %s" article)
9166           ;; run the delete hook
9167           (run-hook-with-args 'gnus-summary-article-delete-hook
9168                               action
9169                               (gnus-data-header
9170                                (assoc article (gnus-data-list nil)))
9171                               gnus-newsgroup-name nil
9172                               select-method)))
9173        (t
9174         (let* ((pto-group (gnus-group-prefixed-name
9175                            (car art-group) to-method))
9176                (info (gnus-get-info pto-group))
9177                (to-group (gnus-info-group info))
9178                to-marks)
9179           ;; Update the group that has been moved to.
9180           (when (and info
9181                      (memq action '(move copy)))
9182             (unless (member to-group to-groups)
9183               (push to-group to-groups))
9184
9185             (unless (memq article gnus-newsgroup-unreads)
9186               (push 'read to-marks)
9187               (gnus-info-set-read
9188                info (gnus-add-to-range (gnus-info-read info)
9189                                        (list (cdr art-group)))))
9190
9191             ;; See whether the article is to be put in the cache.
9192             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9193                              gnus-article-mark-lists
9194                            (delete '(expirable . expire)
9195                                    (copy-sequence gnus-article-mark-lists))))
9196                   (to-article (cdr art-group)))
9197
9198               ;; Enter the article into the cache in the new group,
9199               ;; if that is required.
9200               (when gnus-use-cache
9201                 (gnus-cache-possibly-enter-article
9202                  to-group to-article
9203                  (let ((header (copy-sequence
9204                                 (gnus-summary-article-header article))))
9205                    (mail-header-set-number header to-article)
9206                    header)
9207                  (memq article gnus-newsgroup-marked)
9208                  (memq article gnus-newsgroup-dormant)
9209                  (memq article gnus-newsgroup-unreads)))
9210
9211               (when gnus-preserve-marks
9212                 ;; Copy any marks over to the new group.
9213                 (when (and (equal to-group gnus-newsgroup-name)
9214                            (not (memq article gnus-newsgroup-unreads)))
9215                   ;; Mark this article as read in this group.
9216                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9217                   (setcdr (gnus-active to-group) to-article)
9218                   (setcdr gnus-newsgroup-active to-article))
9219
9220                 (while marks
9221                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9222                     (when (memq article (symbol-value
9223                                          (intern (format "gnus-newsgroup-%s"
9224                                                          (caar marks)))))
9225                       (push (cdar marks) to-marks)
9226                       ;; If the other group is the same as this group,
9227                       ;; then we have to add the mark to the list.
9228                       (when (equal to-group gnus-newsgroup-name)
9229                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9230                              (cons to-article
9231                                    (symbol-value
9232                                     (intern (format "gnus-newsgroup-%s"
9233                                                     (caar marks)))))))
9234                       ;; Copy the marks to other group.
9235                       (gnus-add-marked-articles
9236                        to-group (cdar marks) (list to-article) info)))
9237                   (setq marks (cdr marks)))
9238
9239                 (gnus-request-set-mark
9240                  to-group (list (list (list to-article) 'add to-marks))))
9241
9242               (gnus-dribble-enter
9243                (concat "(gnus-group-set-info '"
9244                        (gnus-prin1-to-string (gnus-get-info to-group))
9245                        ")"))))
9246
9247           ;; Update the Xref header in this article to point to
9248           ;; the new crossposted article we have just created.
9249           (when (eq action 'crosspost)
9250             (save-excursion
9251               (set-buffer copy-buf)
9252               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9253               (nnheader-replace-header "Xref" new-xref)
9254               (gnus-request-replace-article
9255                article gnus-newsgroup-name (current-buffer))))
9256
9257           ;; run the move/copy/crosspost/respool hook
9258           (run-hook-with-args 'gnus-summary-article-move-hook
9259                               action
9260                               (gnus-data-header
9261                                (assoc article (gnus-data-list nil)))
9262                               gnus-newsgroup-name
9263                               to-newsgroup
9264                               select-method))
9265
9266         ;;;!!!Why is this necessary?
9267         (set-buffer gnus-summary-buffer)
9268         
9269         (gnus-summary-goto-subject article)
9270         (when (eq action 'move)
9271           (gnus-summary-mark-article article gnus-canceled-mark))))
9272       (gnus-summary-remove-process-mark article))
9273     ;; Re-activate all groups that have been moved to.
9274     (save-excursion
9275       (set-buffer gnus-group-buffer)
9276       (let ((gnus-group-marked to-groups))
9277         (gnus-group-get-new-news-this-group nil t)))
9278
9279     (gnus-kill-buffer copy-buf)
9280     (gnus-summary-position-point)
9281     (gnus-set-mode-line 'summary)))
9282
9283 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9284   "Copy the current article to some other group.
9285 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9286 When called interactively, if TO-NEWSGROUP is nil, use the value of
9287 the variable `gnus-move-split-methods' for finding a default target
9288 newsgroup.
9289 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9290 re-spool using this method."
9291   (interactive "P")
9292   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9293
9294 (defun gnus-summary-crosspost-article (&optional n)
9295   "Crosspost the current article to some other group."
9296   (interactive "P")
9297   (gnus-summary-move-article n nil nil 'crosspost))
9298
9299 (defcustom gnus-summary-respool-default-method nil
9300   "Default method type for respooling an article.
9301 If nil, use to the current newsgroup method."
9302   :type 'symbol
9303   :group 'gnus-summary-mail)
9304
9305 (defcustom gnus-summary-display-while-building nil
9306   "If non-nil, show and update the summary buffer as it's being built.
9307 If the value is t, update the buffer after every line is inserted.  If
9308 the value is an integer (N), update the display every N lines."
9309   :group 'gnus-thread
9310   :type '(choice (const :tag "off" nil)
9311                  number
9312                  (const :tag "frequently" t)))
9313
9314 (defun gnus-summary-respool-article (&optional n method)
9315   "Respool the current article.
9316 The article will be squeezed through the mail spooling process again,
9317 which means that it will be put in some mail newsgroup or other
9318 depending on `nnmail-split-methods'.
9319 If N is a positive number, respool the N next articles.
9320 If N is a negative number, respool the N previous articles.
9321 If N is nil and any articles have been marked with the process mark,
9322 respool those articles instead.
9323
9324 Respooling can be done both from mail groups and \"real\" newsgroups.
9325 In the former case, the articles in question will be moved from the
9326 current group into whatever groups they are destined to.  In the
9327 latter case, they will be copied into the relevant groups."
9328   (interactive
9329    (list current-prefix-arg
9330          (let* ((methods (gnus-methods-using 'respool))
9331                 (methname
9332                  (symbol-name (or gnus-summary-respool-default-method
9333                                   (car (gnus-find-method-for-group
9334                                         gnus-newsgroup-name)))))
9335                 (method
9336                  (gnus-completing-read-with-default
9337                   methname "What backend do you want to use when respooling?"
9338                   methods nil t nil 'gnus-mail-method-history))
9339                 ms)
9340            (cond
9341             ((zerop (length (setq ms (gnus-servers-using-backend
9342                                       (intern method)))))
9343              (list (intern method) ""))
9344             ((= 1 (length ms))
9345              (car ms))
9346             (t
9347              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9348                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9349                            ms-alist))))))))
9350   (unless method
9351     (error "No method given for respooling"))
9352   (if (assoc (symbol-name
9353               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9354              (gnus-methods-using 'respool))
9355       (gnus-summary-move-article n nil method)
9356     (gnus-summary-copy-article n nil method)))
9357
9358 (defun gnus-summary-import-article (file &optional edit)
9359   "Import an arbitrary file into a mail newsgroup."
9360   (interactive "fImport file: \nP")
9361   (let ((group gnus-newsgroup-name)
9362         (now (current-time))
9363         atts lines group-art)
9364     (unless (gnus-check-backend-function 'request-accept-article group)
9365       (error "%s does not support article importing" group))
9366     (or (file-readable-p file)
9367         (not (file-regular-p file))
9368         (error "Can't read %s" file))
9369     (save-excursion
9370       (set-buffer (gnus-get-buffer-create " *import file*"))
9371       (erase-buffer)
9372       (nnheader-insert-file-contents file)
9373       (goto-char (point-min))
9374       (if (nnheader-article-p)
9375           (save-restriction
9376             (goto-char (point-min))
9377             (search-forward "\n\n" nil t)
9378             (narrow-to-region (point-min) (1- (point)))
9379             (goto-char (point-min))
9380             (unless (re-search-forward "^date:" nil t)
9381               (goto-char (point-max))
9382               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9383         ;; This doesn't look like an article, so we fudge some headers.
9384         (setq atts (file-attributes file)
9385               lines (count-lines (point-min) (point-max)))
9386         (insert "From: " (read-string "From: ") "\n"
9387                 "Subject: " (read-string "Subject: ") "\n"
9388                 "Date: " (message-make-date (nth 5 atts)) "\n"
9389                 "Message-ID: " (message-make-message-id) "\n"
9390                 "Lines: " (int-to-string lines) "\n"
9391                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9392       (setq group-art (gnus-request-accept-article group nil t))
9393       (kill-buffer (current-buffer)))
9394     (setq gnus-newsgroup-active (gnus-activate-group group))
9395     (forward-line 1)
9396     (gnus-summary-goto-article (cdr group-art) nil t)
9397     (when edit
9398       (gnus-summary-edit-article))))
9399
9400 (defun gnus-summary-create-article ()
9401   "Create an article in a mail newsgroup."
9402   (interactive)
9403   (let ((group gnus-newsgroup-name)
9404         (now (current-time))
9405         group-art)
9406     (unless (gnus-check-backend-function 'request-accept-article group)
9407       (error "%s does not support article importing" group))
9408     (save-excursion
9409       (set-buffer (gnus-get-buffer-create " *import file*"))
9410       (erase-buffer)
9411       (goto-char (point-min))
9412       ;; This doesn't look like an article, so we fudge some headers.
9413       (insert "From: " (read-string "From: ") "\n"
9414               "Subject: " (read-string "Subject: ") "\n"
9415               "Date: " (message-make-date now) "\n"
9416               "Message-ID: " (message-make-message-id) "\n")
9417       (setq group-art (gnus-request-accept-article group nil t))
9418       (kill-buffer (current-buffer)))
9419     (setq gnus-newsgroup-active (gnus-activate-group group))
9420     (forward-line 1)
9421     (gnus-summary-goto-article (cdr group-art) nil t)
9422     (gnus-summary-edit-article)))
9423
9424 (defun gnus-summary-article-posted-p ()
9425   "Say whether the current (mail) article is available from news as well.
9426 This will be the case if the article has both been mailed and posted."
9427   (interactive)
9428   (let ((id (mail-header-references (gnus-summary-article-header)))
9429         (gnus-override-method (car (gnus-refer-article-methods))))
9430     (if (gnus-request-head id "")
9431         (gnus-message 2 "The current message was found on %s"
9432                       gnus-override-method)
9433       (gnus-message 2 "The current message couldn't be found on %s"
9434                     gnus-override-method)
9435       nil)))
9436
9437 (defun gnus-summary-expire-articles (&optional now)
9438   "Expire all articles that are marked as expirable in the current group."
9439   (interactive)
9440   (when (and (not gnus-group-is-exiting-without-update-p)
9441              (gnus-check-backend-function
9442               'request-expire-articles gnus-newsgroup-name))
9443     ;; This backend supports expiry.
9444     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9445            (expirable (if total
9446                           (progn
9447                             ;; We need to update the info for
9448                             ;; this group for `gnus-list-of-read-articles'
9449                             ;; to give us the right answer.
9450                             (gnus-run-hooks 'gnus-exit-group-hook)
9451                             (gnus-summary-update-info)
9452                             (gnus-list-of-read-articles gnus-newsgroup-name))
9453                         (setq gnus-newsgroup-expirable
9454                               (sort gnus-newsgroup-expirable '<))))
9455            (expiry-wait (if now 'immediate
9456                           (gnus-group-find-parameter
9457                            gnus-newsgroup-name 'expiry-wait)))
9458            (nnmail-expiry-target
9459             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9460                 nnmail-expiry-target))
9461            es)
9462       (when expirable
9463         ;; There are expirable articles in this group, so we run them
9464         ;; through the expiry process.
9465         (gnus-message 6 "Expiring articles...")
9466         (unless (gnus-check-group gnus-newsgroup-name)
9467           (error "Can't open server for %s" gnus-newsgroup-name))
9468         ;; The list of articles that weren't expired is returned.
9469         (save-excursion
9470           (if expiry-wait
9471               (let ((nnmail-expiry-wait-function nil)
9472                     (nnmail-expiry-wait expiry-wait))
9473                 (setq es (gnus-request-expire-articles
9474                           expirable gnus-newsgroup-name)))
9475             (setq es (gnus-request-expire-articles
9476                       expirable gnus-newsgroup-name)))
9477           (unless total
9478             (setq gnus-newsgroup-expirable es))
9479           ;; We go through the old list of expirable, and mark all
9480           ;; really expired articles as nonexistent.
9481           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9482             (let ((gnus-use-cache nil))
9483               (dolist (article expirable)
9484                 (when (and (not (memq article es))
9485                            (gnus-data-find article))
9486                   (gnus-summary-mark-article article gnus-canceled-mark)
9487                   (run-hook-with-args 'gnus-summary-article-expire-hook
9488                                       'delete
9489                                       (gnus-data-header
9490                                        (assoc article (gnus-data-list nil)))
9491                                       gnus-newsgroup-name
9492                                       nil
9493                                       nil))))))
9494         (gnus-message 6 "Expiring articles...done")))))
9495
9496 (defun gnus-summary-expire-articles-now ()
9497   "Expunge all expirable articles in the current group.
9498 This means that *all* articles that are marked as expirable will be
9499 deleted forever, right now."
9500   (interactive)
9501   (or gnus-expert-user
9502       (gnus-yes-or-no-p
9503        "Are you really, really, really sure you want to delete all these messages? ")
9504       (error "Phew!"))
9505   (gnus-summary-expire-articles t))
9506
9507 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9508 (defun gnus-summary-delete-article (&optional n)
9509   "Delete the N next (mail) articles.
9510 This command actually deletes articles.  This is not a marking
9511 command.  The article will disappear forever from your life, never to
9512 return.
9513
9514 If N is negative, delete backwards.
9515 If N is nil and articles have been marked with the process mark,
9516 delete these instead.
9517
9518 If `gnus-novice-user' is non-nil you will be asked for
9519 confirmation before the articles are deleted."
9520   (interactive "P")
9521   (unless (gnus-check-backend-function 'request-expire-articles
9522                                        gnus-newsgroup-name)
9523     (error "The current newsgroup does not support article deletion"))
9524   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9525     (error "Couldn't open server"))
9526   ;; Compute the list of articles to delete.
9527   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9528         (nnmail-expiry-target 'delete)
9529         not-deleted)
9530     (if (and gnus-novice-user
9531              (not (gnus-yes-or-no-p
9532                    (format "Do you really want to delete %s forever? "
9533                            (if (> (length articles) 1)
9534                                (format "these %s articles" (length articles))
9535                              "this article")))))
9536         ()
9537       ;; Delete the articles.
9538       (setq not-deleted (gnus-request-expire-articles
9539                          articles gnus-newsgroup-name 'force))
9540       (while articles
9541         (gnus-summary-remove-process-mark (car articles))
9542         ;; The backend might not have been able to delete the article
9543         ;; after all.
9544         (unless (memq (car articles) not-deleted)
9545           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9546         (let* ((article (car articles))
9547                (ghead  (gnus-data-header
9548                                     (assoc article (gnus-data-list nil)))))
9549           (run-hook-with-args 'gnus-summary-article-delete-hook
9550                               'delete ghead gnus-newsgroup-name nil
9551                               nil))
9552         (setq articles (cdr articles)))
9553       (when not-deleted
9554         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9555     (gnus-summary-position-point)
9556     (gnus-set-mode-line 'summary)
9557     not-deleted))
9558
9559 (defun gnus-summary-edit-article (&optional force)
9560   "Edit the current article.
9561 This will have permanent effect only in mail groups.
9562 If FORCE is non-nil, allow editing of articles even in read-only
9563 groups."
9564   (interactive "P")
9565   (save-excursion
9566     (set-buffer gnus-summary-buffer)
9567     (let ((mail-parse-charset gnus-newsgroup-charset)
9568           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9569       (gnus-set-global-variables)
9570       (when (and (not force)
9571                  (gnus-group-read-only-p))
9572         (error "The current newsgroup does not support article editing"))
9573       (gnus-summary-show-article t)
9574       (gnus-article-edit-article
9575        'ignore
9576        `(lambda (no-highlight)
9577           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9578                 (message-options message-options)
9579                 (message-options-set-recipient)
9580                 (mail-parse-ignored-charsets
9581                  ',gnus-newsgroup-ignored-charsets))
9582             (gnus-summary-edit-article-done
9583              ,(or (mail-header-references gnus-current-headers) "")
9584              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9585
9586 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9587
9588 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9589                                                  no-highlight)
9590   "Make edits to the current article permanent."
9591   (interactive)
9592   (save-excursion
9593     ;; The buffer restriction contains the entire article if it exists.
9594     (when (article-goto-body)
9595       (let ((lines (count-lines (point) (point-max)))
9596             (length (- (point-max) (point)))
9597             (case-fold-search t)
9598             (body (copy-marker (point))))
9599         (goto-char (point-min))
9600         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9601           (delete-region (match-beginning 1) (match-end 1))
9602           (insert (number-to-string length)))
9603         (goto-char (point-min))
9604         (when (re-search-forward
9605                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9606           (delete-region (match-beginning 1) (match-end 1))
9607           (insert (number-to-string length)))
9608         (goto-char (point-min))
9609         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9610           (delete-region (match-beginning 1) (match-end 1))
9611           (insert (number-to-string lines))))))
9612   ;; Replace the article.
9613   (let ((buf (current-buffer)))
9614     (with-temp-buffer
9615       (insert-buffer-substring buf)
9616
9617       (if (and (not read-only)
9618                (not (gnus-request-replace-article
9619                      (cdr gnus-article-current) (car gnus-article-current)
9620                      (current-buffer) t)))
9621           (error "Couldn't replace article")
9622         ;; Update the summary buffer.
9623         (if (and references
9624                  (equal (message-tokenize-header references " ")
9625                         (message-tokenize-header
9626                          (or (message-fetch-field "references") "") " ")))
9627             ;; We only have to update this line.
9628             (save-excursion
9629               (save-restriction
9630                 (message-narrow-to-head)
9631                 (let ((head (buffer-string))
9632                       header)
9633                   (with-temp-buffer
9634                     (insert (format "211 %d Article retrieved.\n"
9635                                     (cdr gnus-article-current)))
9636                     (insert head)
9637                     (insert ".\n")
9638                     (let ((nntp-server-buffer (current-buffer)))
9639                       (setq header (car (gnus-get-newsgroup-headers
9640                                          nil t))))
9641                     (save-excursion
9642                       (set-buffer gnus-summary-buffer)
9643                       (gnus-data-set-header
9644                        (gnus-data-find (cdr gnus-article-current))
9645                        header)
9646                       (gnus-summary-update-article-line
9647                        (cdr gnus-article-current) header)
9648                       (if (gnus-summary-goto-subject
9649                            (cdr gnus-article-current) nil t)
9650                           (gnus-summary-update-secondary-mark
9651                            (cdr gnus-article-current))))))))
9652           ;; Update threads.
9653           (set-buffer (or buffer gnus-summary-buffer))
9654           (gnus-summary-update-article (cdr gnus-article-current))
9655           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9656               (gnus-summary-update-secondary-mark
9657                (cdr gnus-article-current))))
9658         ;; Prettify the article buffer again.
9659         (unless no-highlight
9660           (save-excursion
9661             (set-buffer gnus-article-buffer)
9662             ;;;!!! Fix this -- article should be rehighlighted.
9663             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9664             (set-buffer gnus-original-article-buffer)
9665             (gnus-request-article
9666              (cdr gnus-article-current)
9667              (car gnus-article-current) (current-buffer))))
9668         ;; Prettify the summary buffer line.
9669         (when (gnus-visual-p 'summary-highlight 'highlight)
9670           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9671
9672 (defun gnus-summary-edit-wash (key)
9673   "Perform editing command KEY in the article buffer."
9674   (interactive
9675    (list
9676     (progn
9677       (message "%s" (concat (this-command-keys) "- "))
9678       (read-char))))
9679   (message "")
9680   (gnus-summary-edit-article)
9681   (execute-kbd-macro (concat (this-command-keys) key))
9682   (gnus-article-edit-done))
9683
9684 ;;; Respooling
9685
9686 (defun gnus-summary-respool-query (&optional silent trace)
9687   "Query where the respool algorithm would put this article."
9688   (interactive)
9689   (let (gnus-mark-article-hook)
9690     (gnus-summary-select-article)
9691     (save-excursion
9692       (set-buffer gnus-original-article-buffer)
9693       (let ((groups (nnmail-article-group 'identity trace)))
9694         (unless silent
9695           (if groups
9696               (message "This message would go to %s"
9697                        (mapconcat 'car groups ", "))
9698             (message "This message would go to no groups"))
9699           groups)))))
9700
9701 (defun gnus-summary-respool-trace ()
9702   "Trace where the respool algorithm would put this article.
9703 Display a buffer showing all fancy splitting patterns which matched."
9704   (interactive)
9705   (gnus-summary-respool-query nil t))
9706
9707 ;; Summary marking commands.
9708
9709 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9710   "Mark articles which has the same subject as read, and then select the next.
9711 If UNMARK is positive, remove any kind of mark.
9712 If UNMARK is negative, tick articles."
9713   (interactive "P")
9714   (when unmark
9715     (setq unmark (prefix-numeric-value unmark)))
9716   (let ((count
9717          (gnus-summary-mark-same-subject
9718           (gnus-summary-article-subject) unmark)))
9719     ;; Select next unread article.  If auto-select-same mode, should
9720     ;; select the first unread article.
9721     (gnus-summary-next-article t (and gnus-auto-select-same
9722                                       (gnus-summary-article-subject)))
9723     (gnus-message 7 "%d article%s marked as %s"
9724                   count (if (= count 1) " is" "s are")
9725                   (if unmark "unread" "read"))))
9726
9727 (defun gnus-summary-kill-same-subject (&optional unmark)
9728   "Mark articles which has the same subject as read.
9729 If UNMARK is positive, remove any kind of mark.
9730 If UNMARK is negative, tick articles."
9731   (interactive "P")
9732   (when unmark
9733     (setq unmark (prefix-numeric-value unmark)))
9734   (let ((count
9735          (gnus-summary-mark-same-subject
9736           (gnus-summary-article-subject) unmark)))
9737     ;; If marked as read, go to next unread subject.
9738     (when (null unmark)
9739       ;; Go to next unread subject.
9740       (gnus-summary-next-subject 1 t))
9741     (gnus-message 7 "%d articles are marked as %s"
9742                   count (if unmark "unread" "read"))))
9743
9744 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9745   "Mark articles with same SUBJECT as read, and return marked number.
9746 If optional argument UNMARK is positive, remove any kinds of marks.
9747 If optional argument UNMARK is negative, mark articles as unread instead."
9748   (let ((count 1))
9749     (save-excursion
9750       (cond
9751        ((null unmark)                   ; Mark as read.
9752         (while (and
9753                 (progn
9754                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9755                   (gnus-summary-show-thread) t)
9756                 (gnus-summary-find-subject subject))
9757           (setq count (1+ count))))
9758        ((> unmark 0)                    ; Tick.
9759         (while (and
9760                 (progn
9761                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9762                   (gnus-summary-show-thread) t)
9763                 (gnus-summary-find-subject subject))
9764           (setq count (1+ count))))
9765        (t                               ; Mark as unread.
9766         (while (and
9767                 (progn
9768                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9769                   (gnus-summary-show-thread) t)
9770                 (gnus-summary-find-subject subject))
9771           (setq count (1+ count)))))
9772       (gnus-set-mode-line 'summary)
9773       ;; Return the number of marked articles.
9774       count)))
9775
9776 (defun gnus-summary-mark-as-processable (n &optional unmark)
9777   "Set the process mark on the next N articles.
9778 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9779 the process mark instead.  The difference between N and the actual
9780 number of articles marked is returned."
9781   (interactive "P")
9782   (if (and (null n) (gnus-region-active-p))
9783       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9784     (setq n (prefix-numeric-value n))
9785     (let ((backward (< n 0))
9786           (n (abs n)))
9787       (while (and
9788               (> n 0)
9789               (if unmark
9790                   (gnus-summary-remove-process-mark
9791                    (gnus-summary-article-number))
9792                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9793               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9794         (setq n (1- n)))
9795       (when (/= 0 n)
9796         (gnus-message 7 "No more articles"))
9797       (gnus-summary-recenter)
9798       (gnus-summary-position-point)
9799       n)))
9800
9801 (defun gnus-summary-unmark-as-processable (n)
9802   "Remove the process mark from the next N articles.
9803 If N is negative, unmark backward instead.  The difference between N and
9804 the actual number of articles unmarked is returned."
9805   (interactive "P")
9806   (gnus-summary-mark-as-processable n t))
9807
9808 (defun gnus-summary-unmark-all-processable ()
9809   "Remove the process mark from all articles."
9810   (interactive)
9811   (save-excursion
9812     (while gnus-newsgroup-processable
9813       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9814   (gnus-summary-position-point))
9815
9816 (defun gnus-summary-add-mark (article type)
9817   "Mark ARTICLE with a mark of TYPE."
9818   (let ((vtype (car (assq type gnus-article-mark-lists)))
9819         var)
9820     (if (not vtype)
9821         (error "No such mark type: %s" type)
9822       (setq var (intern (format "gnus-newsgroup-%s" type)))
9823       (set var (cons article (symbol-value var)))
9824       (if (memq type '(processable cached replied forwarded recent saved))
9825           (gnus-summary-update-secondary-mark article)
9826         ;;; !!! This is bogus.  We should find out what primary
9827         ;;; !!! mark we want to set.
9828         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9829
9830 (defun gnus-summary-mark-as-expirable (n)
9831   "Mark N articles forward as expirable.
9832 If N is negative, mark backward instead.  The difference between N and
9833 the actual number of articles marked is returned."
9834   (interactive "p")
9835   (gnus-summary-mark-forward n gnus-expirable-mark))
9836
9837 (defun gnus-summary-mark-as-spam (n)
9838   "Mark N articles forward as spam.
9839 If N is negative, mark backward instead.  The difference between N and
9840 the actual number of articles marked is returned."
9841   (interactive "p")
9842   (gnus-summary-mark-forward n gnus-spam-mark))
9843
9844 (defun gnus-summary-mark-article-as-replied (article)
9845   "Mark ARTICLE as replied to and update the summary line.
9846 ARTICLE can also be a list of articles."
9847   (interactive (list (gnus-summary-article-number)))
9848   (let ((articles (if (listp article) article (list article))))
9849     (dolist (article articles)
9850       (unless (numberp article)
9851         (error "%s is not a number" article))
9852       (push article gnus-newsgroup-replied)
9853       (let ((buffer-read-only nil))
9854         (when (gnus-summary-goto-subject article nil t)
9855           (gnus-summary-update-secondary-mark article))))))
9856
9857 (defun gnus-summary-mark-article-as-forwarded (article)
9858   "Mark ARTICLE as forwarded and update the summary line.
9859 ARTICLE can also be a list of articles."
9860   (let ((articles (if (listp article) article (list article))))
9861     (dolist (article articles)
9862       (push article gnus-newsgroup-forwarded)
9863       (let ((buffer-read-only nil))
9864         (when (gnus-summary-goto-subject article nil t)
9865           (gnus-summary-update-secondary-mark article))))))
9866
9867 (defun gnus-summary-set-bookmark (article)
9868   "Set a bookmark in current article."
9869   (interactive (list (gnus-summary-article-number)))
9870   (when (or (not (get-buffer gnus-article-buffer))
9871             (not gnus-current-article)
9872             (not gnus-article-current)
9873             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9874     (error "No current article selected"))
9875   ;; Remove old bookmark, if one exists.
9876   (gnus-pull article gnus-newsgroup-bookmarks)
9877   ;; Set the new bookmark, which is on the form
9878   ;; (article-number . line-number-in-body).
9879   (push
9880    (cons article
9881          (with-current-buffer gnus-article-buffer
9882            (count-lines
9883             (min (point)
9884                  (save-excursion
9885                    (article-goto-body)
9886                    (point)))
9887             (point))))
9888    gnus-newsgroup-bookmarks)
9889   (gnus-message 6 "A bookmark has been added to the current article."))
9890
9891 (defun gnus-summary-remove-bookmark (article)
9892   "Remove the bookmark from the current article."
9893   (interactive (list (gnus-summary-article-number)))
9894   ;; Remove old bookmark, if one exists.
9895   (if (not (assq article gnus-newsgroup-bookmarks))
9896       (gnus-message 6 "No bookmark in current article.")
9897     (gnus-pull article gnus-newsgroup-bookmarks)
9898     (gnus-message 6 "Removed bookmark.")))
9899
9900 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9901 (defun gnus-summary-mark-as-dormant (n)
9902   "Mark N articles forward as dormant.
9903 If N is negative, mark backward instead.  The difference between N and
9904 the actual number of articles marked is returned."
9905   (interactive "p")
9906   (gnus-summary-mark-forward n gnus-dormant-mark))
9907
9908 (defun gnus-summary-set-process-mark (article)
9909   "Set the process mark on ARTICLE and update the summary line."
9910   (setq gnus-newsgroup-processable
9911         (cons article
9912               (delq article gnus-newsgroup-processable)))
9913   (when (gnus-summary-goto-subject article)
9914     (gnus-summary-show-thread)
9915     (gnus-summary-goto-subject article)
9916     (gnus-summary-update-secondary-mark article)))
9917
9918 (defun gnus-summary-remove-process-mark (article)
9919   "Remove the process mark from ARTICLE and update the summary line."
9920   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9921   (when (gnus-summary-goto-subject article)
9922     (gnus-summary-show-thread)
9923     (gnus-summary-goto-subject article)
9924     (gnus-summary-update-secondary-mark article)))
9925
9926 (defun gnus-summary-set-saved-mark (article)
9927   "Set the process mark on ARTICLE and update the summary line."
9928   (push article gnus-newsgroup-saved)
9929   (when (gnus-summary-goto-subject article)
9930     (gnus-summary-update-secondary-mark article)))
9931
9932 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9933   "Mark N articles as read forwards.
9934 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9935 The difference between N and the actual number of articles marked is
9936 returned.
9937 If NO-EXPIRE, auto-expiry will be inhibited."
9938   (interactive "p")
9939   (gnus-summary-show-thread)
9940   (let ((backward (< n 0))
9941         (gnus-summary-goto-unread
9942          (and gnus-summary-goto-unread
9943               (not (eq gnus-summary-goto-unread 'never))
9944               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9945                                     gnus-ticked-mark gnus-dormant-mark)))))
9946         (n (abs n))
9947         (mark (or mark gnus-del-mark)))
9948     (while (and (> n 0)
9949                 (gnus-summary-mark-article nil mark no-expire)
9950                 (zerop (gnus-summary-next-subject
9951                         (if backward -1 1)
9952                         (and gnus-summary-goto-unread
9953                              (not (eq gnus-summary-goto-unread 'never)))
9954                         t)))
9955       (setq n (1- n)))
9956     (when (/= 0 n)
9957       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9958     (gnus-summary-recenter)
9959     (gnus-summary-position-point)
9960     (gnus-set-mode-line 'summary)
9961     n))
9962
9963 (defun gnus-summary-mark-article-as-read (mark)
9964   "Mark the current article quickly as read with MARK."
9965   (let ((article (gnus-summary-article-number)))
9966     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9967     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9968     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9969     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9970     (push (cons article mark) gnus-newsgroup-reads)
9971     ;; Possibly remove from cache, if that is used.
9972     (when gnus-use-cache
9973       (gnus-cache-enter-remove-article article))
9974     ;; Allow the backend to change the mark.
9975     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9976     ;; Check for auto-expiry.
9977     (when (and gnus-newsgroup-auto-expire
9978                (memq mark gnus-auto-expirable-marks))
9979       (setq mark gnus-expirable-mark)
9980       ;; Let the backend know about the mark change.
9981       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9982       (push article gnus-newsgroup-expirable))
9983     ;; Set the mark in the buffer.
9984     (gnus-summary-update-mark mark 'unread)
9985     t))
9986
9987 (defun gnus-summary-mark-article-as-unread (mark)
9988   "Mark the current article quickly as unread with MARK."
9989   (let* ((article (gnus-summary-article-number))
9990          (old-mark (gnus-summary-article-mark article)))
9991     ;; Allow the backend to change the mark.
9992     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9993     (if (eq mark old-mark)
9994         t
9995       (if (<= article 0)
9996           (progn
9997             (gnus-error 1 "Can't mark negative article numbers")
9998             nil)
9999         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10000         (setq gnus-newsgroup-spam-marked
10001               (delq article gnus-newsgroup-spam-marked))
10002         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10003         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10004         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10005         (cond ((= mark gnus-ticked-mark)
10006                (setq gnus-newsgroup-marked
10007                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10008                                               article)))
10009               ((= mark gnus-spam-mark)
10010                (setq gnus-newsgroup-spam-marked
10011                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10012                                               article)))
10013               ((= mark gnus-dormant-mark)
10014                (setq gnus-newsgroup-dormant
10015                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10016                                               article)))
10017               (t
10018                (setq gnus-newsgroup-unreads
10019                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10020                                               article))))
10021         (gnus-pull article gnus-newsgroup-reads)
10022
10023         ;; See whether the article is to be put in the cache.
10024         (and gnus-use-cache
10025              (vectorp (gnus-summary-article-header article))
10026              (save-excursion
10027                (gnus-cache-possibly-enter-article
10028                 gnus-newsgroup-name article
10029                 (gnus-summary-article-header article)
10030                 (= mark gnus-ticked-mark)
10031                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10032
10033         ;; Fix the mark.
10034         (gnus-summary-update-mark mark 'unread)
10035         t))))
10036
10037 (defun gnus-summary-mark-article (&optional article mark no-expire)
10038   "Mark ARTICLE with MARK.  MARK can be any character.
10039 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10040 `??' (dormant) and `?E' (expirable).
10041 If MARK is nil, then the default character `?r' is used.
10042 If ARTICLE is nil, then the article on the current line will be
10043 marked.
10044 If NO-EXPIRE, auto-expiry will be inhibited."
10045   ;; The mark might be a string.
10046   (when (stringp mark)
10047     (setq mark (aref mark 0)))
10048   ;; If no mark is given, then we check auto-expiring.
10049   (when (null mark)
10050     (setq mark gnus-del-mark))
10051   (when (and (not no-expire)
10052              gnus-newsgroup-auto-expire
10053              (memq mark gnus-auto-expirable-marks))
10054     (setq mark gnus-expirable-mark))
10055   (let ((article (or article (gnus-summary-article-number)))
10056         (old-mark (gnus-summary-article-mark article)))
10057     ;; Allow the backend to change the mark.
10058     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10059     (if (eq mark old-mark)
10060         t
10061       (unless article
10062         (error "No article on current line"))
10063       (if (not (if (or (= mark gnus-unread-mark)
10064                        (= mark gnus-ticked-mark)
10065                        (= mark gnus-spam-mark)
10066                        (= mark gnus-dormant-mark))
10067                    (gnus-mark-article-as-unread article mark)
10068                  (gnus-mark-article-as-read article mark)))
10069           t
10070         ;; See whether the article is to be put in the cache.
10071         (and gnus-use-cache
10072              (not (= mark gnus-canceled-mark))
10073              (vectorp (gnus-summary-article-header article))
10074              (save-excursion
10075                (gnus-cache-possibly-enter-article
10076                 gnus-newsgroup-name article
10077                 (gnus-summary-article-header article)
10078                 (= mark gnus-ticked-mark)
10079                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10080
10081         (when (gnus-summary-goto-subject article nil t)
10082           (let ((buffer-read-only nil))
10083             (gnus-summary-show-thread)
10084             ;; Fix the mark.
10085             (gnus-summary-update-mark mark 'unread)
10086             t))))))
10087
10088 (defun gnus-summary-update-secondary-mark (article)
10089   "Update the secondary (read, process, cache) mark."
10090   (gnus-summary-update-mark
10091    (cond ((memq article gnus-newsgroup-processable)
10092           gnus-process-mark)
10093          ((memq article gnus-newsgroup-cached)
10094           gnus-cached-mark)
10095          ((memq article gnus-newsgroup-replied)
10096           gnus-replied-mark)
10097          ((memq article gnus-newsgroup-forwarded)
10098           gnus-forwarded-mark)
10099          ((memq article gnus-newsgroup-saved)
10100           gnus-saved-mark)
10101          ((memq article gnus-newsgroup-recent)
10102           gnus-recent-mark)
10103          ((memq article gnus-newsgroup-unseen)
10104           gnus-unseen-mark)
10105          (t gnus-no-mark))
10106    'replied)
10107   (when (gnus-visual-p 'summary-highlight 'highlight)
10108     (gnus-run-hooks 'gnus-summary-update-hook))
10109   t)
10110
10111 (defun gnus-summary-update-download-mark (article)
10112   "Update the download mark."
10113   (gnus-summary-update-mark
10114    (cond ((memq article gnus-newsgroup-undownloaded)
10115           gnus-undownloaded-mark)
10116          (gnus-newsgroup-agentized
10117           gnus-downloaded-mark)
10118          (t
10119           gnus-no-mark))
10120    'download)
10121   (gnus-summary-update-line t)
10122   t)
10123
10124 (defun gnus-summary-update-mark (mark type)
10125   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10126         (buffer-read-only nil))
10127     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10128     (when forward
10129       (when (looking-at "\r")
10130         (incf forward))
10131       (when (<= (+ forward (point)) (point-max))
10132         ;; Go to the right position on the line.
10133         (goto-char (+ forward (point)))
10134         ;; Replace the old mark with the new mark.
10135         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10136         ;; Optionally update the marks by some user rule.
10137         (when (eq type 'unread)
10138           (gnus-data-set-mark
10139            (gnus-data-find (gnus-summary-article-number)) mark)
10140           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10141
10142 (defun gnus-mark-article-as-read (article &optional mark)
10143   "Enter ARTICLE in the pertinent lists and remove it from others."
10144   ;; Make the article expirable.
10145   (let ((mark (or mark gnus-del-mark)))
10146     (setq gnus-newsgroup-expirable
10147           (if (= mark gnus-expirable-mark)
10148               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10149             (delq article gnus-newsgroup-expirable)))
10150     ;; Remove from unread and marked lists.
10151     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10152     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10153     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10154     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10155     (push (cons article mark) gnus-newsgroup-reads)
10156     ;; Possibly remove from cache, if that is used.
10157     (when gnus-use-cache
10158       (gnus-cache-enter-remove-article article))
10159     t))
10160
10161 (defun gnus-mark-article-as-unread (article &optional mark)
10162   "Enter ARTICLE in the pertinent lists and remove it from others."
10163   (let ((mark (or mark gnus-ticked-mark)))
10164     (if (<= article 0)
10165         (progn
10166           (gnus-error 1 "Can't mark negative article numbers")
10167           nil)
10168       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10169             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10170             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10171             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10172             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10173
10174       ;; Unsuppress duplicates?
10175       (when gnus-suppress-duplicates
10176         (gnus-dup-unsuppress-article article))
10177
10178       (cond ((= mark gnus-ticked-mark)
10179              (setq gnus-newsgroup-marked
10180                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10181             ((= mark gnus-spam-mark)
10182              (setq gnus-newsgroup-spam-marked
10183                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10184                                             article)))
10185             ((= mark gnus-dormant-mark)
10186              (setq gnus-newsgroup-dormant
10187                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10188             (t
10189              (setq gnus-newsgroup-unreads
10190                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10191       (gnus-pull article gnus-newsgroup-reads)
10192       t)))
10193
10194 (defalias 'gnus-summary-mark-as-unread-forward
10195   'gnus-summary-tick-article-forward)
10196 (make-obsolete 'gnus-summary-mark-as-unread-forward
10197                'gnus-summary-tick-article-forward)
10198 (defun gnus-summary-tick-article-forward (n)
10199   "Tick N articles forwards.
10200 If N is negative, tick backwards instead.
10201 The difference between N and the number of articles ticked is returned."
10202   (interactive "p")
10203   (gnus-summary-mark-forward n gnus-ticked-mark))
10204
10205 (defalias 'gnus-summary-mark-as-unread-backward
10206   'gnus-summary-tick-article-backward)
10207 (make-obsolete 'gnus-summary-mark-as-unread-backward
10208                'gnus-summary-tick-article-backward)
10209 (defun gnus-summary-tick-article-backward (n)
10210   "Tick N articles backwards.
10211 The difference between N and the number of articles ticked is returned."
10212   (interactive "p")
10213   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10214
10215 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10216 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10217 (defun gnus-summary-tick-article (&optional article clear-mark)
10218   "Mark current article as unread.
10219 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10220 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10221   (interactive)
10222   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10223                                        gnus-ticked-mark)))
10224
10225 (defun gnus-summary-mark-as-read-forward (n)
10226   "Mark N articles as read forwards.
10227 If N is negative, mark backwards instead.
10228 The difference between N and the actual number of articles marked is
10229 returned."
10230   (interactive "p")
10231   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10232
10233 (defun gnus-summary-mark-as-read-backward (n)
10234   "Mark the N articles as read backwards.
10235 The difference between N and the actual number of articles marked is
10236 returned."
10237   (interactive "p")
10238   (gnus-summary-mark-forward
10239    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10240
10241 (defun gnus-summary-mark-as-read (&optional article mark)
10242   "Mark current article as read.
10243 ARTICLE specifies the article to be marked as read.
10244 MARK specifies a string to be inserted at the beginning of the line."
10245   (gnus-summary-mark-article article mark))
10246
10247 (defun gnus-summary-clear-mark-forward (n)
10248   "Clear marks from N articles forward.
10249 If N is negative, clear backward instead.
10250 The difference between N and the number of marks cleared is returned."
10251   (interactive "p")
10252   (gnus-summary-mark-forward n gnus-unread-mark))
10253
10254 (defun gnus-summary-clear-mark-backward (n)
10255   "Clear marks from N articles backward.
10256 The difference between N and the number of marks cleared is returned."
10257   (interactive "p")
10258   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10259
10260 (defun gnus-summary-mark-unread-as-read ()
10261   "Intended to be used by `gnus-summary-mark-article-hook'."
10262   (when (memq gnus-current-article gnus-newsgroup-unreads)
10263     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10264
10265 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10266   "Intended to be used by `gnus-summary-mark-article-hook'."
10267   (let ((mark (gnus-summary-article-mark)))
10268     (when (or (gnus-unread-mark-p mark)
10269               (gnus-read-mark-p mark))
10270       (gnus-summary-mark-article gnus-current-article
10271                                  (or new-mark gnus-read-mark)))))
10272
10273 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10274   "Intended to be used by `gnus-summary-mark-article-hook'."
10275   (let ((mark (gnus-summary-article-mark)))
10276     (when (or (gnus-unread-mark-p mark)
10277               (gnus-read-mark-p mark))
10278       (gnus-summary-mark-article (gnus-summary-article-number)
10279                                  (or new-mark gnus-read-mark)))))
10280
10281 (defun gnus-summary-mark-unread-as-ticked ()
10282   "Intended to be used by `gnus-summary-mark-article-hook'."
10283   (when (memq gnus-current-article gnus-newsgroup-unreads)
10284     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10285
10286 (defun gnus-summary-mark-region-as-read (point mark all)
10287   "Mark all unread articles between point and mark as read.
10288 If given a prefix, mark all articles between point and mark as read,
10289 even ticked and dormant ones."
10290   (interactive "r\nP")
10291   (save-excursion
10292     (let (article)
10293       (goto-char point)
10294       (beginning-of-line)
10295       (while (and
10296               (< (point) mark)
10297               (progn
10298                 (when (or all
10299                           (memq (setq article (gnus-summary-article-number))
10300                                 gnus-newsgroup-unreads))
10301                   (gnus-summary-mark-article article gnus-del-mark))
10302                 t)
10303               (gnus-summary-find-next))))))
10304
10305 (defun gnus-summary-mark-below (score mark)
10306   "Mark articles with score less than SCORE with MARK."
10307   (interactive "P\ncMark: ")
10308   (setq score (if score
10309                   (prefix-numeric-value score)
10310                 (or gnus-summary-default-score 0)))
10311   (save-excursion
10312     (set-buffer gnus-summary-buffer)
10313     (goto-char (point-min))
10314     (while
10315         (progn
10316           (and (< (gnus-summary-article-score) score)
10317                (gnus-summary-mark-article nil mark))
10318           (gnus-summary-find-next)))))
10319
10320 (defun gnus-summary-kill-below (&optional score)
10321   "Mark articles with score below SCORE as read."
10322   (interactive "P")
10323   (gnus-summary-mark-below score gnus-killed-mark))
10324
10325 (defun gnus-summary-clear-above (&optional score)
10326   "Clear all marks from articles with score above SCORE."
10327   (interactive "P")
10328   (gnus-summary-mark-above score gnus-unread-mark))
10329
10330 (defun gnus-summary-tick-above (&optional score)
10331   "Tick all articles with score above SCORE."
10332   (interactive "P")
10333   (gnus-summary-mark-above score gnus-ticked-mark))
10334
10335 (defun gnus-summary-mark-above (score mark)
10336   "Mark articles with score over SCORE with MARK."
10337   (interactive "P\ncMark: ")
10338   (setq score (if score
10339                   (prefix-numeric-value score)
10340                 (or gnus-summary-default-score 0)))
10341   (save-excursion
10342     (set-buffer gnus-summary-buffer)
10343     (goto-char (point-min))
10344     (while (and (progn
10345                   (when (> (gnus-summary-article-score) score)
10346                     (gnus-summary-mark-article nil mark))
10347                   t)
10348                 (gnus-summary-find-next)))))
10349
10350 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10351 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10352 (defun gnus-summary-limit-include-expunged (&optional no-error)
10353   "Display all the hidden articles that were expunged for low scores."
10354   (interactive)
10355   (let ((buffer-read-only nil))
10356     (let ((scored gnus-newsgroup-scored)
10357           headers h)
10358       (while scored
10359         (unless (gnus-summary-article-header (caar scored))
10360           (and (setq h (gnus-number-to-header (caar scored)))
10361                (< (cdar scored) gnus-summary-expunge-below)
10362                (push h headers)))
10363         (setq scored (cdr scored)))
10364       (if (not headers)
10365           (when (not no-error)
10366             (error "No expunged articles hidden"))
10367         (goto-char (point-min))
10368         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10369         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10370         (mapcar (lambda (x) (push (mail-header-number x)
10371                                   gnus-newsgroup-limit))
10372                 headers)
10373         (gnus-summary-prepare-unthreaded (nreverse headers))
10374         (goto-char (point-min))
10375         (gnus-summary-position-point)
10376         t))))
10377
10378 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10379   "Mark all unread articles in this newsgroup as read.
10380 If prefix argument ALL is non-nil, ticked and dormant articles will
10381 also be marked as read.
10382 If QUIETLY is non-nil, no questions will be asked.
10383
10384 If TO-HERE is non-nil, it should be a point in the buffer.  All
10385 articles before (after, if REVERSE is set) this point will be marked
10386 as read.
10387
10388 Note that this function will only catch up the unread article
10389 in the current summary buffer limitation.
10390
10391 The number of articles marked as read is returned."
10392   (interactive "P")
10393   (prog1
10394       (save-excursion
10395         (when (or quietly
10396                   (not gnus-interactive-catchup) ;Without confirmation?
10397                   gnus-expert-user
10398                   (gnus-y-or-n-p
10399                    (if all
10400                        "Mark absolutely all articles as read? "
10401                      "Mark all unread articles as read? ")))
10402           (if (and not-mark
10403                    (not gnus-newsgroup-adaptive)
10404                    (not gnus-newsgroup-auto-expire)
10405                    (not gnus-suppress-duplicates)
10406                    (or (not gnus-use-cache)
10407                        (eq gnus-use-cache 'passive)))
10408               (progn
10409                 (when all
10410                   (setq gnus-newsgroup-marked nil
10411                         gnus-newsgroup-spam-marked nil
10412                         gnus-newsgroup-dormant nil))
10413                 (setq gnus-newsgroup-unreads
10414                       (gnus-sorted-nunion
10415                        (gnus-intersection gnus-newsgroup-unreads
10416                                           gnus-newsgroup-downloadable)
10417                        gnus-newsgroup-unfetched)))
10418             ;; We actually mark all articles as canceled, which we
10419             ;; have to do when using auto-expiry or adaptive scoring.
10420             (gnus-summary-show-all-threads)
10421             (if (and to-here reverse)
10422                 (progn
10423                   (goto-char to-here)
10424                   (gnus-summary-mark-current-read-and-unread-as-read
10425                    gnus-catchup-mark)
10426                   (while (gnus-summary-find-next (not all))
10427                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10428               (when (gnus-summary-first-subject (not all))
10429                 (while (and
10430                         (if to-here (< (point) to-here) t)
10431                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10432                         (gnus-summary-find-next (not all))))))
10433             (gnus-set-mode-line 'summary))
10434           t))
10435     (gnus-summary-position-point)))
10436
10437 (defun gnus-summary-catchup-to-here (&optional all)
10438   "Mark all unticked articles before the current one as read.
10439 If ALL is non-nil, also mark ticked and dormant articles as read."
10440   (interactive "P")
10441   (save-excursion
10442     (gnus-save-hidden-threads
10443       (let ((beg (point)))
10444         ;; We check that there are unread articles.
10445         (when (or all (gnus-summary-find-prev))
10446           (gnus-summary-catchup all t beg)))))
10447   (gnus-summary-position-point))
10448
10449 (defun gnus-summary-catchup-from-here (&optional all)
10450   "Mark all unticked articles after (and including) the current one as read.
10451 If ALL is non-nil, also mark ticked and dormant articles as read."
10452   (interactive "P")
10453   (save-excursion
10454     (gnus-save-hidden-threads
10455       (let ((beg (point)))
10456         ;; We check that there are unread articles.
10457         (when (or all (gnus-summary-find-next))
10458           (gnus-summary-catchup all t beg nil t)))))
10459   (gnus-summary-position-point))
10460
10461 (defun gnus-summary-catchup-all (&optional quietly)
10462   "Mark all articles in this newsgroup as read.
10463 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10464 instead, which marks only unread articles as read."
10465   (interactive "P")
10466   (gnus-summary-catchup t quietly))
10467
10468 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10469   "Mark all unread articles in this group as read, then exit.
10470 If prefix argument ALL is non-nil, all articles are marked as read.
10471 If QUIETLY is non-nil, no questions will be asked."
10472   (interactive "P")
10473   (when (gnus-summary-catchup all quietly nil 'fast)
10474     ;; Select next newsgroup or exit.
10475     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10476              (eq gnus-auto-select-next 'quietly))
10477         (gnus-summary-next-group nil)
10478       (gnus-summary-exit))))
10479
10480 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10481   "Mark all articles in this newsgroup as read, and then exit.
10482 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10483 instead, which marks only unread articles as read."
10484   (interactive "P")
10485   (gnus-summary-catchup-and-exit t quietly))
10486
10487 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10488   "Mark all articles in this group as read and select the next group.
10489 If given a prefix, mark all articles, unread as well as ticked, as
10490 read."
10491   (interactive "P")
10492   (save-excursion
10493     (gnus-summary-catchup all))
10494   (gnus-summary-next-group))
10495
10496 ;;;
10497 ;;; with article
10498 ;;;
10499
10500 (defmacro gnus-with-article (article &rest forms)
10501   "Select ARTICLE and perform FORMS in the original article buffer.
10502 Then replace the article with the result."
10503   `(progn
10504      ;; We don't want the article to be marked as read.
10505      (let (gnus-mark-article-hook)
10506        (gnus-summary-select-article t t nil ,article))
10507      (set-buffer gnus-original-article-buffer)
10508      ,@forms
10509      (if (not (gnus-check-backend-function
10510                'request-replace-article (car gnus-article-current)))
10511          (gnus-message 5 "Read-only group; not replacing")
10512        (unless (gnus-request-replace-article
10513                 ,article (car gnus-article-current)
10514                 (current-buffer) t)
10515          (error "Couldn't replace article")))
10516      ;; The cache and backlog have to be flushed somewhat.
10517      (when gnus-keep-backlog
10518        (gnus-backlog-remove-article
10519         (car gnus-article-current) (cdr gnus-article-current)))
10520      (when gnus-use-cache
10521        (gnus-cache-update-article
10522         (car gnus-article-current) (cdr gnus-article-current)))))
10523
10524 (put 'gnus-with-article 'lisp-indent-function 1)
10525 (put 'gnus-with-article 'edebug-form-spec '(form body))
10526
10527 ;; Thread-based commands.
10528
10529 (defun gnus-summary-articles-in-thread (&optional article)
10530   "Return a list of all articles in the current thread.
10531 If ARTICLE is non-nil, return all articles in the thread that starts
10532 with that article."
10533   (let* ((article (or article (gnus-summary-article-number)))
10534          (data (gnus-data-find-list article))
10535          (top-level (gnus-data-level (car data)))
10536          (top-subject
10537           (cond ((null gnus-thread-operation-ignore-subject)
10538                  (gnus-simplify-subject-re
10539                   (mail-header-subject (gnus-data-header (car data)))))
10540                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10541                  (gnus-simplify-subject-fuzzy
10542                   (mail-header-subject (gnus-data-header (car data)))))
10543                 (t nil)))
10544          (end-point (save-excursion
10545                       (if (gnus-summary-go-to-next-thread)
10546                           (point) (point-max))))
10547          articles)
10548     (while (and data
10549                 (< (gnus-data-pos (car data)) end-point))
10550       (when (or (not top-subject)
10551                 (string= top-subject
10552                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10553                              (gnus-simplify-subject-fuzzy
10554                               (mail-header-subject
10555                                (gnus-data-header (car data))))
10556                            (gnus-simplify-subject-re
10557                             (mail-header-subject
10558                              (gnus-data-header (car data)))))))
10559         (push (gnus-data-number (car data)) articles))
10560       (unless (and (setq data (cdr data))
10561                    (> (gnus-data-level (car data)) top-level))
10562         (setq data nil)))
10563     ;; Return the list of articles.
10564     (nreverse articles)))
10565
10566 (defun gnus-summary-rethread-current ()
10567   "Rethread the thread the current article is part of."
10568   (interactive)
10569   (let* ((gnus-show-threads t)
10570          (article (gnus-summary-article-number))
10571          (id (mail-header-id (gnus-summary-article-header)))
10572          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10573     (unless id
10574       (error "No article on the current line"))
10575     (gnus-rebuild-thread id)
10576     (gnus-summary-goto-subject article)))
10577
10578 (defun gnus-summary-reparent-thread ()
10579   "Make the current article child of the marked (or previous) article.
10580
10581 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10582 is non-nil or the Subject: of both articles are the same."
10583   (interactive)
10584   (unless (not (gnus-group-read-only-p))
10585     (error "The current newsgroup does not support article editing"))
10586   (unless (<= (length gnus-newsgroup-processable) 1)
10587     (error "No more than one article may be marked"))
10588   (save-window-excursion
10589     (let ((gnus-article-buffer " *reparent*")
10590           (current-article (gnus-summary-article-number))
10591           ;; First grab the marked article, otherwise one line up.
10592           (parent-article (if (not (null gnus-newsgroup-processable))
10593                               (car gnus-newsgroup-processable)
10594                             (save-excursion
10595                               (if (eq (forward-line -1) 0)
10596                                   (gnus-summary-article-number)
10597                                 (error "Beginning of summary buffer"))))))
10598       (unless (not (eq current-article parent-article))
10599         (error "An article may not be self-referential"))
10600       (let ((message-id (mail-header-id
10601                          (gnus-summary-article-header parent-article))))
10602         (unless (and message-id (not (equal message-id "")))
10603           (error "No message-id in desired parent"))
10604         (gnus-with-article current-article
10605           (save-restriction
10606             (goto-char (point-min))
10607             (message-narrow-to-head)
10608             (if (re-search-forward "^References: " nil t)
10609                 (progn
10610                   (re-search-forward "^[^ \t]" nil t)
10611                   (forward-line -1)
10612                   (end-of-line)
10613                   (insert " " message-id))
10614               (insert "References: " message-id "\n"))))
10615         (set-buffer gnus-summary-buffer)
10616         (gnus-summary-unmark-all-processable)
10617         (gnus-summary-update-article current-article)
10618         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10619             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10620         (gnus-summary-rethread-current)
10621         (gnus-message 3 "Article %d is now the child of article %d"
10622                       current-article parent-article)))))
10623
10624 (defun gnus-summary-toggle-threads (&optional arg)
10625   "Toggle showing conversation threads.
10626 If ARG is positive number, turn showing conversation threads on."
10627   (interactive "P")
10628   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10629     (setq gnus-show-threads
10630           (if (null arg) (not gnus-show-threads)
10631             (> (prefix-numeric-value arg) 0)))
10632     (gnus-summary-prepare)
10633     (gnus-summary-goto-subject current)
10634     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10635     (gnus-summary-position-point)))
10636
10637 (defun gnus-summary-show-all-threads ()
10638   "Show all threads."
10639   (interactive)
10640   (save-excursion
10641     (let ((buffer-read-only nil))
10642       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10643   (gnus-summary-position-point))
10644
10645 (defun gnus-summary-show-thread ()
10646   "Show thread subtrees.
10647 Returns nil if no thread was there to be shown."
10648   (interactive)
10649   (let ((buffer-read-only nil)
10650         (orig (point))
10651         (end (point-at-eol))
10652         ;; Leave point at bol
10653         (beg (progn (beginning-of-line) (point))))
10654     (prog1
10655         ;; Any hidden lines here?
10656         (search-forward "\r" end t)
10657       (subst-char-in-region beg end ?\^M ?\n t)
10658       (goto-char orig)
10659       (gnus-summary-position-point))))
10660
10661 (defun gnus-summary-maybe-hide-threads ()
10662   "If requested, hide the threads that should be hidden."
10663   (when (and gnus-show-threads
10664              gnus-thread-hide-subtree)
10665     (gnus-summary-hide-all-threads
10666      (if (or (consp gnus-thread-hide-subtree)
10667              (functionp gnus-thread-hide-subtree))
10668          (gnus-make-predicate gnus-thread-hide-subtree)
10669        nil))))
10670
10671 ;;; Hiding predicates.
10672
10673 (defun gnus-article-unread-p (header)
10674   (memq (mail-header-number header) gnus-newsgroup-unreads))
10675
10676 (defun gnus-article-unseen-p (header)
10677   (memq (mail-header-number header) gnus-newsgroup-unseen))
10678
10679 (defun gnus-map-articles (predicate articles)
10680   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10681   (apply 'gnus-or (mapcar predicate
10682                           (mapcar 'gnus-summary-article-header articles))))
10683
10684 (defun gnus-summary-hide-all-threads (&optional predicate)
10685   "Hide all thread subtrees.
10686 If PREDICATE is supplied, threads that satisfy this predicate
10687 will not be hidden."
10688   (interactive)
10689   (save-excursion
10690     (goto-char (point-min))
10691     (let ((end nil))
10692       (while (not end)
10693         (when (or (not predicate)
10694                   (gnus-map-articles
10695                    predicate (gnus-summary-article-children)))
10696             (gnus-summary-hide-thread))
10697         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10698   (gnus-summary-position-point))
10699
10700 (defun gnus-summary-hide-thread ()
10701   "Hide thread subtrees.
10702 If PREDICATE is supplied, threads that satisfy this predicate
10703 will not be hidden.
10704 Returns nil if no threads were there to be hidden."
10705   (interactive)
10706   (let ((buffer-read-only nil)
10707         (start (point))
10708         (article (gnus-summary-article-number)))
10709     (goto-char start)
10710     ;; Go forward until either the buffer ends or the subthread
10711     ;; ends.
10712     (when (and (not (eobp))
10713                (or (zerop (gnus-summary-next-thread 1 t))
10714                    (goto-char (point-max))))
10715       (prog1
10716           (if (and (> (point) start)
10717                    (search-backward "\n" start t))
10718               (progn
10719                 (subst-char-in-region start (point) ?\n ?\^M)
10720                 (gnus-summary-goto-subject article))
10721             (goto-char start)
10722             nil)))))
10723
10724 (defun gnus-summary-go-to-next-thread (&optional previous)
10725   "Go to the same level (or less) next thread.
10726 If PREVIOUS is non-nil, go to previous thread instead.
10727 Return the article number moved to, or nil if moving was impossible."
10728   (let ((level (gnus-summary-thread-level))
10729         (way (if previous -1 1))
10730         (beg (point)))
10731     (forward-line way)
10732     (while (and (not (eobp))
10733                 (< level (gnus-summary-thread-level)))
10734       (forward-line way))
10735     (if (eobp)
10736         (progn
10737           (goto-char beg)
10738           nil)
10739       (setq beg (point))
10740       (prog1
10741           (gnus-summary-article-number)
10742         (goto-char beg)))))
10743
10744 (defun gnus-summary-next-thread (n &optional silent)
10745   "Go to the same level next N'th thread.
10746 If N is negative, search backward instead.
10747 Returns the difference between N and the number of skips actually
10748 done.
10749
10750 If SILENT, don't output messages."
10751   (interactive "p")
10752   (let ((backward (< n 0))
10753         (n (abs n)))
10754     (while (and (> n 0)
10755                 (gnus-summary-go-to-next-thread backward))
10756       (decf n))
10757     (unless silent
10758       (gnus-summary-position-point))
10759     (when (and (not silent) (/= 0 n))
10760       (gnus-message 7 "No more threads"))
10761     n))
10762
10763 (defun gnus-summary-prev-thread (n)
10764   "Go to the same level previous N'th thread.
10765 Returns the difference between N and the number of skips actually
10766 done."
10767   (interactive "p")
10768   (gnus-summary-next-thread (- n)))
10769
10770 (defun gnus-summary-go-down-thread ()
10771   "Go down one level in the current thread."
10772   (let ((children (gnus-summary-article-children)))
10773     (when children
10774       (gnus-summary-goto-subject (car children)))))
10775
10776 (defun gnus-summary-go-up-thread ()
10777   "Go up one level in the current thread."
10778   (let ((parent (gnus-summary-article-parent)))
10779     (when parent
10780       (gnus-summary-goto-subject parent))))
10781
10782 (defun gnus-summary-down-thread (n)
10783   "Go down thread N steps.
10784 If N is negative, go up instead.
10785 Returns the difference between N and how many steps down that were
10786 taken."
10787   (interactive "p")
10788   (let ((up (< n 0))
10789         (n (abs n)))
10790     (while (and (> n 0)
10791                 (if up (gnus-summary-go-up-thread)
10792                   (gnus-summary-go-down-thread)))
10793       (setq n (1- n)))
10794     (gnus-summary-position-point)
10795     (when (/= 0 n)
10796       (gnus-message 7 "Can't go further"))
10797     n))
10798
10799 (defun gnus-summary-up-thread (n)
10800   "Go up thread N steps.
10801 If N is negative, go down instead.
10802 Returns the difference between N and how many steps down that were
10803 taken."
10804   (interactive "p")
10805   (gnus-summary-down-thread (- n)))
10806
10807 (defun gnus-summary-top-thread ()
10808   "Go to the top of the thread."
10809   (interactive)
10810   (while (gnus-summary-go-up-thread))
10811   (gnus-summary-article-number))
10812
10813 (defun gnus-summary-kill-thread (&optional unmark)
10814   "Mark articles under current thread as read.
10815 If the prefix argument is positive, remove any kinds of marks.
10816 If the prefix argument is negative, tick articles instead."
10817   (interactive "P")
10818   (when unmark
10819     (setq unmark (prefix-numeric-value unmark)))
10820   (let ((articles (gnus-summary-articles-in-thread)))
10821     (save-excursion
10822       ;; Expand the thread.
10823       (gnus-summary-show-thread)
10824       ;; Mark all the articles.
10825       (while articles
10826         (gnus-summary-goto-subject (car articles))
10827         (cond ((null unmark)
10828                (gnus-summary-mark-article-as-read gnus-killed-mark))
10829               ((> unmark 0)
10830                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10831               (t
10832                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10833         (setq articles (cdr articles))))
10834     ;; Hide killed subtrees.
10835     (and (null unmark)
10836          gnus-thread-hide-killed
10837          (gnus-summary-hide-thread))
10838     ;; If marked as read, go to next unread subject.
10839     (when (null unmark)
10840       ;; Go to next unread subject.
10841       (gnus-summary-next-subject 1 t)))
10842   (gnus-set-mode-line 'summary))
10843
10844 ;; Summary sorting commands
10845
10846 (defun gnus-summary-sort-by-number (&optional reverse)
10847   "Sort the summary buffer by article number.
10848 Argument REVERSE means reverse order."
10849   (interactive "P")
10850   (gnus-summary-sort 'number reverse))
10851
10852 (defun gnus-summary-sort-by-random (&optional reverse)
10853   "Randomize the order in the summary buffer.
10854 Argument REVERSE means to randomize in reverse order."
10855   (interactive "P")
10856   (gnus-summary-sort 'random reverse))
10857
10858 (defun gnus-summary-sort-by-author (&optional reverse)
10859   "Sort the summary buffer by author name alphabetically.
10860 If `case-fold-search' is non-nil, case of letters is ignored.
10861 Argument REVERSE means reverse order."
10862   (interactive "P")
10863   (gnus-summary-sort 'author reverse))
10864
10865 (defun gnus-summary-sort-by-subject (&optional reverse)
10866   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10867 If `case-fold-search' is non-nil, case of letters is ignored.
10868 Argument REVERSE means reverse order."
10869   (interactive "P")
10870   (gnus-summary-sort 'subject reverse))
10871
10872 (defun gnus-summary-sort-by-date (&optional reverse)
10873   "Sort the summary buffer by date.
10874 Argument REVERSE means reverse order."
10875   (interactive "P")
10876   (gnus-summary-sort 'date reverse))
10877
10878 (defun gnus-summary-sort-by-score (&optional reverse)
10879   "Sort the summary buffer by score.
10880 Argument REVERSE means reverse order."
10881   (interactive "P")
10882   (gnus-summary-sort 'score reverse))
10883
10884 (defun gnus-summary-sort-by-lines (&optional reverse)
10885   "Sort the summary buffer by the number of lines.
10886 Argument REVERSE means reverse order."
10887   (interactive "P")
10888   (gnus-summary-sort 'lines reverse))
10889
10890 (defun gnus-summary-sort-by-chars (&optional reverse)
10891   "Sort the summary buffer by article length.
10892 Argument REVERSE means reverse order."
10893   (interactive "P")
10894   (gnus-summary-sort 'chars reverse))
10895
10896 (defun gnus-summary-sort-by-original (&optional reverse)
10897   "Sort the summary buffer using the default sorting method.
10898 Argument REVERSE means reverse order."
10899   (interactive "P")
10900   (let* ((buffer-read-only)
10901          (gnus-summary-prepare-hook nil))
10902     ;; We do the sorting by regenerating the threads.
10903     (gnus-summary-prepare)
10904     ;; Hide subthreads if needed.
10905     (gnus-summary-maybe-hide-threads)))
10906
10907 (defun gnus-summary-sort (predicate reverse)
10908   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10909   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10910          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10911          (gnus-thread-sort-functions
10912           (if (not reverse)
10913               thread
10914             `(lambda (t1 t2)
10915                (,thread t2 t1))))
10916          (gnus-sort-gathered-threads-function
10917           gnus-thread-sort-functions)
10918          (gnus-article-sort-functions
10919           (if (not reverse)
10920               article
10921             `(lambda (t1 t2)
10922                (,article t2 t1))))
10923          (buffer-read-only)
10924          (gnus-summary-prepare-hook nil))
10925     ;; We do the sorting by regenerating the threads.
10926     (gnus-summary-prepare)
10927     ;; Hide subthreads if needed.
10928     (gnus-summary-maybe-hide-threads)))
10929
10930 ;; Summary saving commands.
10931
10932 (defun gnus-summary-save-article (&optional n not-saved)
10933   "Save the current article using the default saver function.
10934 If N is a positive number, save the N next articles.
10935 If N is a negative number, save the N previous articles.
10936 If N is nil and any articles have been marked with the process mark,
10937 save those articles instead.
10938 The variable `gnus-default-article-saver' specifies the saver function."
10939   (interactive "P")
10940   (let* ((articles (gnus-summary-work-articles n))
10941          (save-buffer (save-excursion
10942                         (nnheader-set-temp-buffer " *Gnus Save*")))
10943          (num (length articles))
10944          header file)
10945     (dolist (article articles)
10946       (setq header (gnus-summary-article-header article))
10947       (if (not (vectorp header))
10948           ;; This is a pseudo-article.
10949           (if (assq 'name header)
10950               (gnus-copy-file (cdr (assq 'name header)))
10951             (gnus-message 1 "Article %d is unsaveable" article))
10952         ;; This is a real article.
10953         (save-window-excursion
10954           (let ((gnus-display-mime-function nil)
10955                 (gnus-article-prepare-hook nil))
10956             (gnus-summary-select-article t nil nil article)))
10957         (save-excursion
10958           (set-buffer save-buffer)
10959           (erase-buffer)
10960           (insert-buffer-substring gnus-original-article-buffer))
10961         (setq file (gnus-article-save save-buffer file num))
10962         (gnus-summary-remove-process-mark article)
10963         (unless not-saved
10964           (gnus-summary-set-saved-mark article))))
10965     (gnus-kill-buffer save-buffer)
10966     (gnus-summary-position-point)
10967     (gnus-set-mode-line 'summary)
10968     n))
10969
10970 (defun gnus-summary-pipe-output (&optional arg headers)
10971   "Pipe the current article to a subprocess.
10972 If N is a positive number, pipe the N next articles.
10973 If N is a negative number, pipe the N previous articles.
10974 If N is nil and any articles have been marked with the process mark,
10975 pipe those articles instead.
10976 If HEADERS (the symbolic prefix), include the headers, too."
10977   (interactive (gnus-interactive "P\ny"))
10978   (require 'gnus-art)
10979   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10980         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10981     (gnus-summary-save-article arg t))
10982   (let ((buffer (get-buffer "*Shell Command Output*")))
10983     (when (and buffer
10984                (not (zerop (buffer-size buffer))))
10985       (gnus-configure-windows 'pipe))))
10986
10987 (defun gnus-summary-save-article-mail (&optional arg)
10988   "Append the current article to an mail file.
10989 If N is a positive number, save the N next articles.
10990 If N is a negative number, save the N previous articles.
10991 If N is nil and any articles have been marked with the process mark,
10992 save those articles instead."
10993   (interactive "P")
10994   (require 'gnus-art)
10995   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10996     (gnus-summary-save-article arg)))
10997
10998 (defun gnus-summary-save-article-rmail (&optional arg)
10999   "Append the current article to an rmail file.
11000 If N is a positive number, save the N next articles.
11001 If N is a negative number, save the N previous articles.
11002 If N is nil and any articles have been marked with the process mark,
11003 save those articles instead."
11004   (interactive "P")
11005   (require 'gnus-art)
11006   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11007     (gnus-summary-save-article arg)))
11008
11009 (defun gnus-summary-save-article-file (&optional arg)
11010   "Append the current article to a file.
11011 If N is a positive number, save the N next articles.
11012 If N is a negative number, save the N previous articles.
11013 If N is nil and any articles have been marked with the process mark,
11014 save those articles instead."
11015   (interactive "P")
11016   (require 'gnus-art)
11017   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11018     (gnus-summary-save-article arg)))
11019
11020 (defun gnus-summary-write-article-file (&optional arg)
11021   "Write the current article to a file, deleting the previous file.
11022 If N is a positive number, save the N next articles.
11023 If N is a negative number, save the N previous articles.
11024 If N is nil and any articles have been marked with the process mark,
11025 save those articles instead."
11026   (interactive "P")
11027   (require 'gnus-art)
11028   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11029     (gnus-summary-save-article arg)))
11030
11031 (defun gnus-summary-save-article-body-file (&optional arg)
11032   "Append the current article body to a file.
11033 If N is a positive number, save the N next articles.
11034 If N is a negative number, save the N previous articles.
11035 If N is nil and any articles have been marked with the process mark,
11036 save those articles instead."
11037   (interactive "P")
11038   (require 'gnus-art)
11039   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11040     (gnus-summary-save-article arg)))
11041
11042 (defun gnus-summary-muttprint (&optional arg)
11043   "Print the current article using Muttprint.
11044 If N is a positive number, save the N next articles.
11045 If N is a negative number, save the N previous articles.
11046 If N is nil and any articles have been marked with the process mark,
11047 save those articles instead."
11048   (interactive "P")
11049   (require 'gnus-art)
11050   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11051     (gnus-summary-save-article arg t)))
11052
11053 (defun gnus-summary-pipe-message (program)
11054   "Pipe the current article through PROGRAM."
11055   (interactive "sProgram: ")
11056   (gnus-summary-select-article)
11057   (let ((mail-header-separator ""))
11058     (gnus-eval-in-buffer-window gnus-article-buffer
11059       (save-restriction
11060         (widen)
11061         (let ((start (window-start))
11062               buffer-read-only)
11063           (message-pipe-buffer-body program)
11064           (set-window-start (get-buffer-window (current-buffer)) start))))))
11065
11066 (defun gnus-get-split-value (methods)
11067   "Return a value based on the split METHODS."
11068   (let (split-name method result match)
11069     (when methods
11070       (save-excursion
11071         (set-buffer gnus-original-article-buffer)
11072         (save-restriction
11073           (nnheader-narrow-to-headers)
11074           (while (and methods (not split-name))
11075             (goto-char (point-min))
11076             (setq method (pop methods))
11077             (setq match (car method))
11078             (when (cond
11079                    ((stringp match)
11080                     ;; Regular expression.
11081                     (ignore-errors
11082                       (re-search-forward match nil t)))
11083                    ((functionp match)
11084                     ;; Function.
11085                     (save-restriction
11086                       (widen)
11087                       (setq result (funcall match gnus-newsgroup-name))))
11088                    ((consp match)
11089                     ;; Form.
11090                     (save-restriction
11091                       (widen)
11092                       (setq result (eval match)))))
11093               (setq split-name (cdr method))
11094               (cond ((stringp result)
11095                      (push (expand-file-name
11096                             result gnus-article-save-directory)
11097                            split-name))
11098                     ((consp result)
11099                      (setq split-name (append result split-name)))))))))
11100     (nreverse split-name)))
11101
11102 (defun gnus-valid-move-group-p (group)
11103   (and (boundp group)
11104        (symbol-name group)
11105        (symbol-value group)
11106        (gnus-get-function (gnus-find-method-for-group
11107                            (symbol-name group)) 'request-accept-article t)))
11108
11109 (defun gnus-read-move-group-name (prompt default articles prefix)
11110   "Read a group name."
11111   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11112          (minibuffer-confirm-incomplete nil) ; XEmacs
11113          (prom
11114           (format "%s %s to:"
11115                   prompt
11116                   (if (> (length articles) 1)
11117                       (format "these %d articles" (length articles))
11118                     "this article")))
11119          (to-newsgroup
11120           (cond
11121            ((null split-name)
11122             (gnus-completing-read-with-default
11123              default prom
11124              gnus-active-hashtb
11125              'gnus-valid-move-group-p
11126              nil prefix
11127              'gnus-group-history))
11128            ((= 1 (length split-name))
11129             (gnus-completing-read-with-default
11130              (car split-name) prom
11131              gnus-active-hashtb
11132              'gnus-valid-move-group-p
11133              nil nil
11134              'gnus-group-history))
11135            (t
11136             (gnus-completing-read-with-default
11137              nil prom
11138              (mapcar 'list (nreverse split-name))
11139              nil nil nil
11140              'gnus-group-history))))
11141          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11142     (when to-newsgroup
11143       (if (or (string= to-newsgroup "")
11144               (string= to-newsgroup prefix))
11145           (setq to-newsgroup default))
11146       (unless to-newsgroup
11147         (error "No group name entered"))
11148       (or (gnus-active to-newsgroup)
11149           (gnus-activate-group to-newsgroup nil nil to-method)
11150           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11151                                      to-newsgroup))
11152               (or (and (gnus-request-create-group to-newsgroup to-method)
11153                        (gnus-activate-group
11154                         to-newsgroup nil nil to-method)
11155                        (gnus-subscribe-group to-newsgroup))
11156                   (error "Couldn't create group %s" to-newsgroup)))
11157           (error "No such group: %s" to-newsgroup)))
11158     to-newsgroup))
11159
11160 (defun gnus-summary-save-parts (type dir n &optional reverse)
11161   "Save parts matching TYPE to DIR.
11162 If REVERSE, save parts that do not match TYPE."
11163   (interactive
11164    (list (read-string "Save parts of type: "
11165                       (or (car gnus-summary-save-parts-type-history)
11166                           gnus-summary-save-parts-default-mime)
11167                       'gnus-summary-save-parts-type-history)
11168          (setq gnus-summary-save-parts-last-directory
11169                (read-file-name "Save to directory: "
11170                                gnus-summary-save-parts-last-directory
11171                                nil t))
11172          current-prefix-arg))
11173   (gnus-summary-iterate n
11174     (let ((gnus-display-mime-function nil)
11175           (gnus-inhibit-treatment t))
11176       (gnus-summary-select-article))
11177     (save-excursion
11178       (set-buffer gnus-article-buffer)
11179       (let ((handles (or gnus-article-mime-handles
11180                          (mm-dissect-buffer nil gnus-article-loose-mime)
11181                          (and gnus-article-emulate-mime
11182                               (mm-uu-dissect)))))
11183         (when handles
11184           (gnus-summary-save-parts-1 type dir handles reverse)
11185           (unless gnus-article-mime-handles ;; Don't destroy this case.
11186             (mm-destroy-parts handles)))))))
11187
11188 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11189   (if (stringp (car handle))
11190       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11191               (cdr handle))
11192     (when (if reverse
11193               (not (string-match type (mm-handle-media-type handle)))
11194             (string-match type (mm-handle-media-type handle)))
11195       (let ((file (expand-file-name
11196                    (gnus-map-function
11197                     mm-file-name-rewrite-functions
11198                     (file-name-nondirectory
11199                      (or
11200                       (mail-content-type-get
11201                        (mm-handle-disposition handle) 'filename)
11202                       (mail-content-type-get
11203                        (mm-handle-type handle) 'name)
11204                       (concat gnus-newsgroup-name
11205                               "." (number-to-string
11206                                    (cdr gnus-article-current))))))
11207                    dir)))
11208         (unless (file-exists-p file)
11209           (mm-save-part-to-file handle file))))))
11210
11211 ;; Summary extract commands
11212
11213 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11214   (let ((buffer-read-only nil)
11215         (article (gnus-summary-article-number))
11216         after-article b e)
11217     (unless (gnus-summary-goto-subject article)
11218       (error "No such article: %d" article))
11219     (gnus-summary-position-point)
11220     ;; If all commands are to be bunched up on one line, we collect
11221     ;; them here.
11222     (unless gnus-view-pseudos-separately
11223       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11224             files action)
11225         (while ps
11226           (setq action (cdr (assq 'action (car ps))))
11227           (setq files (list (cdr (assq 'name (car ps)))))
11228           (while (and ps (cdr ps)
11229                       (string= (or action "1")
11230                                (or (cdr (assq 'action (cadr ps))) "2")))
11231             (push (cdr (assq 'name (cadr ps))) files)
11232             (setcdr ps (cddr ps)))
11233           (when files
11234             (when (not (string-match "%s" action))
11235               (push " " files))
11236             (push " " files)
11237             (when (assq 'execute (car ps))
11238               (setcdr (assq 'execute (car ps))
11239                       (funcall (if (string-match "%s" action)
11240                                    'format 'concat)
11241                                action
11242                                (mapconcat
11243                                 (lambda (f)
11244                                   (if (equal f " ")
11245                                       f
11246                                     (shell-quote-argument f)))
11247                                 files " ")))))
11248           (setq ps (cdr ps)))))
11249     (if (and gnus-view-pseudos (not not-view))
11250         (while pslist
11251           (when (assq 'execute (car pslist))
11252             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11253                                   (eq gnus-view-pseudos 'not-confirm)))
11254           (setq pslist (cdr pslist)))
11255       (save-excursion
11256         (while pslist
11257           (setq after-article (or (cdr (assq 'article (car pslist)))
11258                                   (gnus-summary-article-number)))
11259           (gnus-summary-goto-subject after-article)
11260           (forward-line 1)
11261           (setq b (point))
11262           (insert "    " (file-name-nondirectory
11263                           (cdr (assq 'name (car pslist))))
11264                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11265           (setq e (point))
11266           (forward-line -1)             ; back to `b'
11267           (gnus-add-text-properties
11268            b (1- e) (list 'gnus-number gnus-reffed-article-number
11269                           gnus-mouse-face-prop gnus-mouse-face))
11270           (gnus-data-enter
11271            after-article gnus-reffed-article-number
11272            gnus-unread-mark b (car pslist) 0 (- e b))
11273           (setq gnus-newsgroup-unreads
11274                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11275                                          gnus-reffed-article-number))
11276           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11277           (setq pslist (cdr pslist)))))))
11278
11279 (defun gnus-pseudos< (p1 p2)
11280   (let ((c1 (cdr (assq 'action p1)))
11281         (c2 (cdr (assq 'action p2))))
11282     (and c1 c2 (string< c1 c2))))
11283
11284 (defun gnus-request-pseudo-article (props)
11285   (cond ((assq 'execute props)
11286          (gnus-execute-command (cdr (assq 'execute props)))))
11287   (let ((gnus-current-article (gnus-summary-article-number)))
11288     (gnus-run-hooks 'gnus-mark-article-hook)))
11289
11290 (defun gnus-execute-command (command &optional automatic)
11291   (save-excursion
11292     (gnus-article-setup-buffer)
11293     (set-buffer gnus-article-buffer)
11294     (setq buffer-read-only nil)
11295     (let ((command (if automatic command
11296                      (read-string "Command: " (cons command 0)))))
11297       (erase-buffer)
11298       (insert "$ " command "\n\n")
11299       (if gnus-view-pseudo-asynchronously
11300           (start-process "gnus-execute" (current-buffer) shell-file-name
11301                          shell-command-switch command)
11302         (call-process shell-file-name nil t nil
11303                       shell-command-switch command)))))
11304
11305 ;; Summary kill commands.
11306
11307 (defun gnus-summary-edit-global-kill (article)
11308   "Edit the \"global\" kill file."
11309   (interactive (list (gnus-summary-article-number)))
11310   (gnus-group-edit-global-kill article))
11311
11312 (defun gnus-summary-edit-local-kill ()
11313   "Edit a local kill file applied to the current newsgroup."
11314   (interactive)
11315   (setq gnus-current-headers (gnus-summary-article-header))
11316   (gnus-group-edit-local-kill
11317    (gnus-summary-article-number) gnus-newsgroup-name))
11318
11319 ;;; Header reading.
11320
11321 (defun gnus-read-header (id &optional header)
11322   "Read the headers of article ID and enter them into the Gnus system."
11323   (let ((group gnus-newsgroup-name)
11324         (gnus-override-method
11325          (or
11326           gnus-override-method
11327           (and (gnus-news-group-p gnus-newsgroup-name)
11328                (car (gnus-refer-article-methods)))))
11329         where)
11330     ;; First we check to see whether the header in question is already
11331     ;; fetched.
11332     (if (stringp id)
11333         ;; This is a Message-ID.
11334         (setq header (or header (gnus-id-to-header id)))
11335       ;; This is an article number.
11336       (setq header (or header (gnus-summary-article-header id))))
11337     (if (and header
11338              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11339         ;; We have found the header.
11340         header
11341       ;; We have to really fetch the header to this article.
11342       (save-excursion
11343         (set-buffer nntp-server-buffer)
11344         (when (setq where (gnus-request-head id group))
11345           (nnheader-fold-continuation-lines)
11346           (goto-char (point-max))
11347           (insert ".\n")
11348           (goto-char (point-min))
11349           (insert "211 ")
11350           (princ (cond
11351                   ((numberp id) id)
11352                   ((cdr where) (cdr where))
11353                   (header (mail-header-number header))
11354                   (t gnus-reffed-article-number))
11355                  (current-buffer))
11356           (insert " Article retrieved.\n"))
11357         (if (or (not where)
11358                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11359             ()                          ; Malformed head.
11360           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11361             (when (and (stringp id)
11362                        (not (string= (gnus-group-real-name group)
11363                                      (car where))))
11364               ;; If we fetched by Message-ID and the article came
11365               ;; from a different group, we fudge some bogus article
11366               ;; numbers for this article.
11367               (mail-header-set-number header gnus-reffed-article-number))
11368             (save-excursion
11369               (set-buffer gnus-summary-buffer)
11370               (decf gnus-reffed-article-number)
11371               (gnus-remove-header (mail-header-number header))
11372               (push header gnus-newsgroup-headers)
11373               (setq gnus-current-headers header)
11374               (push (mail-header-number header) gnus-newsgroup-limit)))
11375           header)))))
11376
11377 (defun gnus-remove-header (number)
11378   "Remove header NUMBER from `gnus-newsgroup-headers'."
11379   (if (and gnus-newsgroup-headers
11380            (= number (mail-header-number (car gnus-newsgroup-headers))))
11381       (pop gnus-newsgroup-headers)
11382     (let ((headers gnus-newsgroup-headers))
11383       (while (and (cdr headers)
11384                   (not (= number (mail-header-number (cadr headers)))))
11385         (pop headers))
11386       (when (cdr headers)
11387         (setcdr headers (cddr headers))))))
11388
11389 ;;;
11390 ;;; summary highlights
11391 ;;;
11392
11393 (defun gnus-highlight-selected-summary ()
11394   "Highlight selected article in summary buffer."
11395   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11396   (when gnus-summary-selected-face
11397     (save-excursion
11398       (let* ((beg (point-at-bol))
11399              (end (point-at-eol))
11400              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11401              (from (if (get-text-property beg gnus-mouse-face-prop)
11402                        beg
11403                      (or (next-single-property-change
11404                           beg gnus-mouse-face-prop nil end)
11405                          beg)))
11406              (to
11407               (if (= from end)
11408                   (- from 2)
11409                 (or (next-single-property-change
11410                      from gnus-mouse-face-prop nil end)
11411                     end))))
11412         ;; If no mouse-face prop on line we will have to = from = end,
11413         ;; so we highlight the entire line instead.
11414         (when (= (+ to 2) from)
11415           (setq from beg)
11416           (setq to end))
11417         (if gnus-newsgroup-selected-overlay
11418             ;; Move old overlay.
11419             (gnus-move-overlay
11420              gnus-newsgroup-selected-overlay from to (current-buffer))
11421           ;; Create new overlay.
11422           (gnus-overlay-put
11423            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11424            'face gnus-summary-selected-face))))))
11425
11426 (defvar gnus-summary-highlight-line-cached nil)
11427 (defvar gnus-summary-highlight-line-trigger nil)
11428
11429 (defun gnus-summary-highlight-line-0 ()
11430   (if (and (eq gnus-summary-highlight-line-trigger
11431                gnus-summary-highlight)
11432            gnus-summary-highlight-line-cached)
11433       gnus-summary-highlight-line-cached
11434     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11435           gnus-summary-highlight-line-cached
11436           (let* ((cond (list 'cond))
11437                  (c cond)
11438                  (list gnus-summary-highlight))
11439             (while list
11440               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11441                               nil))
11442               (setq c (cdr c)
11443                     list (cdr list)))
11444             (gnus-byte-compile (list 'lambda nil cond))))))
11445
11446 (defun gnus-summary-highlight-line ()
11447   "Highlight current line according to `gnus-summary-highlight'."
11448   (let* ((beg (point-at-bol))
11449          (article (or (gnus-summary-article-number) gnus-current-article))
11450          (score (or (cdr (assq article
11451                                gnus-newsgroup-scored))
11452                     gnus-summary-default-score 0))
11453          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11454          (inhibit-read-only t)
11455          (default gnus-summary-default-score)
11456          (default-high gnus-summary-default-high-score)
11457          (default-low gnus-summary-default-low-score)
11458          (uncached (and gnus-summary-use-undownloaded-faces
11459                         (memq article gnus-newsgroup-undownloaded)
11460                         (not (memq article gnus-newsgroup-cached)))))
11461     (let ((face (funcall (gnus-summary-highlight-line-0))))
11462       (unless (eq face (get-text-property beg 'face))
11463         (gnus-put-text-property-excluding-characters-with-faces
11464          beg (point-at-eol) 'face
11465          (setq face (if (boundp face) (symbol-value face) face)))
11466         (when gnus-summary-highlight-line-function
11467           (funcall gnus-summary-highlight-line-function article face))))))
11468
11469 (defun gnus-update-read-articles (group unread &optional compute)
11470   "Update the list of read articles in GROUP.
11471 UNREAD is a sorted list."
11472   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11473         (info (gnus-get-info group))
11474         (prev 1)
11475         read)
11476     (if (or (not info) (not active))
11477         ;; There is no info on this group if it was, in fact,
11478         ;; killed.  Gnus stores no information on killed groups, so
11479         ;; there's nothing to be done.
11480         ;; One could store the information somewhere temporarily,
11481         ;; perhaps...  Hmmm...
11482         ()
11483       ;; Remove any negative articles numbers.
11484       (while (and unread (< (car unread) 0))
11485         (setq unread (cdr unread)))
11486       ;; Remove any expired article numbers
11487       (while (and unread (< (car unread) (car active)))
11488         (setq unread (cdr unread)))
11489       ;; Compute the ranges of read articles by looking at the list of
11490       ;; unread articles.
11491       (while unread
11492         (when (/= (car unread) prev)
11493           (push (if (= prev (1- (car unread))) prev
11494                   (cons prev (1- (car unread))))
11495                 read))
11496         (setq prev (1+ (car unread)))
11497         (setq unread (cdr unread)))
11498       (when (<= prev (cdr active))
11499         (push (cons prev (cdr active)) read))
11500       (setq read (if (> (length read) 1) (nreverse read) read))
11501       (if compute
11502           read
11503         (save-excursion
11504           (let (setmarkundo)
11505             ;; Propagate the read marks to the backend.
11506             (when (gnus-check-backend-function 'request-set-mark group)
11507               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11508                     (add (gnus-remove-from-range read (gnus-info-read info))))
11509                 (when (or add del)
11510                   (unless (gnus-check-group group)
11511                     (error "Can't open server for %s" group))
11512                   (gnus-request-set-mark
11513                    group (delq nil (list (if add (list add 'add '(read)))
11514                                          (if del (list del 'del '(read))))))
11515                   (setq setmarkundo
11516                         `(gnus-request-set-mark
11517                           ,group
11518                           ',(delq nil (list
11519                                        (if del (list del 'add '(read)))
11520                                        (if add (list add 'del '(read))))))))))
11521             (set-buffer gnus-group-buffer)
11522             (gnus-undo-register
11523               `(progn
11524                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11525                  (gnus-info-set-read ',info ',(gnus-info-read info))
11526                  (gnus-get-unread-articles-in-group ',info
11527                                                     (gnus-active ,group))
11528                  (gnus-group-update-group ,group t)
11529                  ,setmarkundo))))
11530         ;; Enter this list into the group info.
11531         (gnus-info-set-read info read)
11532         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11533         (gnus-get-unread-articles-in-group info (gnus-active group))
11534         t))))
11535
11536 (defun gnus-offer-save-summaries ()
11537   "Offer to save all active summary buffers."
11538   (let (buffers)
11539     ;; Go through all buffers and find all summaries.
11540     (dolist (buffer (buffer-list))
11541       (when (and (setq buffer (buffer-name buffer))
11542                  (string-match "Summary" buffer)
11543                  (with-current-buffer buffer
11544                    ;; We check that this is, indeed, a summary buffer.
11545                    (and (eq major-mode 'gnus-summary-mode)
11546                         ;; Also make sure this isn't bogus.
11547                         gnus-newsgroup-prepared
11548                         ;; Also make sure that this isn't a
11549                         ;; dead summary buffer.
11550                         (not gnus-dead-summary-mode))))
11551         (push buffer buffers)))
11552     ;; Go through all these summary buffers and offer to save them.
11553     (when buffers
11554       (save-excursion
11555         (map-y-or-n-p
11556          "Update summary buffer %s? "
11557          (lambda (buf)
11558            (switch-to-buffer buf)
11559            (gnus-summary-exit))
11560          buffers)))))
11561
11562
11563 ;;; @ for mime-partial
11564 ;;;
11565
11566 (defun gnus-request-partial-message ()
11567   (save-excursion
11568     (let ((number (gnus-summary-article-number))
11569           (group gnus-newsgroup-name)
11570           (mother gnus-article-buffer))
11571       (set-buffer (get-buffer-create " *Partial Article*"))
11572       (erase-buffer)
11573       (setq mime-preview-buffer mother)
11574       (gnus-request-article-this-buffer number group)
11575       (mime-parse-buffer)
11576       )))
11577
11578 (autoload 'mime-combine-message/partial-pieces-automatically
11579   "mime-partial"
11580   "Internal method to combine message/partial messages automatically.")
11581
11582 (mime-add-condition
11583  'action '((type . message)(subtype . partial)
11584            (major-mode . gnus-original-article-mode)
11585            (method . mime-combine-message/partial-pieces-automatically)
11586            (summary-buffer-exp . gnus-summary-buffer)
11587            (request-partial-message-method . gnus-request-partial-message)
11588            ))
11589
11590
11591 ;;; @ for message/rfc822
11592 ;;;
11593
11594 (defun gnus-mime-extract-message/rfc822 (entity situation)
11595   "Burst a forwarded article."
11596   (save-excursion
11597     (set-buffer gnus-summary-buffer)
11598     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11599                                    gnus-newsgroup-name 'gnus-group-history))
11600            (gnus-group-marked (list group))
11601            article info)
11602       (with-temp-buffer
11603         (mime-insert-entity-content entity)
11604         (setq article (gnus-request-accept-article group)))
11605       (when (and (consp article)
11606                  (numberp (setq article (cdr article))))
11607         (setq info (gnus-get-info group))
11608         (gnus-info-set-read info
11609                             (gnus-remove-from-range (gnus-info-read info)
11610                                                     (list article)))
11611         (when (string-equal group gnus-newsgroup-name)
11612           (forward-line 1)
11613           (let (gnus-show-threads)
11614             (gnus-summary-goto-subject article t))
11615           (gnus-summary-clear-mark-forward 1))
11616         (set-buffer gnus-group-buffer)
11617         (gnus-group-get-new-news-this-group nil t)))))
11618
11619 (mime-add-condition
11620  'action '((type . message)(subtype . rfc822)
11621            (major-mode . gnus-original-article-mode)
11622            (method . gnus-mime-extract-message/rfc822)
11623            (mode . "extract")
11624            ))
11625
11626 (mime-add-condition
11627  'action '((type . message)(subtype . news)
11628            (major-mode . gnus-original-article-mode)
11629            (method . gnus-mime-extract-message/rfc822)
11630            (mode . "extract")
11631            ))
11632
11633 (defun gnus-mime-extract-multipart (entity situation)
11634   (let ((children (mime-entity-children entity))
11635         mime-acting-situation-to-override
11636         f)
11637     (while children
11638       (mime-play-entity (car children)
11639                         (cons (assq 'mode situation)
11640                               mime-acting-situation-to-override))
11641       (setq children (cdr children)))
11642     (if (setq f (cdr (assq 'after-method
11643                            mime-acting-situation-to-override)))
11644         (eval f)
11645       )))
11646
11647 (mime-add-condition
11648  'action '((type . multipart)
11649            (method . gnus-mime-extract-multipart)
11650            (mode . "extract")
11651            )
11652  'with-default)
11653
11654
11655 ;;; @ end
11656 ;;;
11657
11658 (defun gnus-summary-inherit-default-charset ()
11659   "Import `default-mime-charset' from summary buffer.
11660 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11661 of FLIM is used."
11662   (if (buffer-live-p gnus-summary-buffer)
11663       (let (d-m-c d-m-c-u)
11664         (with-current-buffer gnus-summary-buffer
11665           (setq d-m-c (if (local-variable-p 'default-mime-charset
11666                                             gnus-summary-buffer)
11667                           default-mime-charset
11668                         t)
11669                 ;; LIMIT
11670                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11671                                               gnus-summary-buffer)
11672                             (symbol-value 'default-mime-charset-unlimited)
11673                           t)))
11674         (if (eq t d-m-c)
11675             (kill-local-variable 'default-mime-charset)
11676           (set (make-local-variable 'default-mime-charset) d-m-c))
11677         (if (eq t d-m-c-u)
11678             (kill-local-variable 'default-mime-charset-unlimited)
11679           (set (make-local-variable 'default-mime-charset-unlimited)
11680                d-m-c-u)))))
11681
11682 (defun gnus-summary-setup-default-charset ()
11683   "Setup newsgroup default charset."
11684   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11685       (progn
11686         (setq gnus-newsgroup-charset nil)
11687         (set (make-local-variable 'default-mime-charset) nil)
11688         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11689           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11690     (let ((ignored-charsets
11691            (or gnus-newsgroup-ephemeral-ignored-charsets
11692                (append
11693                 (and gnus-newsgroup-name
11694                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11695                 gnus-newsgroup-ignored-charsets)))
11696           charset)
11697       (setq gnus-newsgroup-charset
11698             (or gnus-newsgroup-ephemeral-charset
11699                 (when (and gnus-newsgroup-name
11700                            (setq charset (gnus-parameter-charset
11701                                           gnus-newsgroup-name)))
11702                   (make-local-variable 'default-mime-charset)
11703                   (setq default-mime-charset charset))
11704                 gnus-default-charset))
11705       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11706            ignored-charsets))))
11707
11708 ;;;
11709 ;;; Mime Commands
11710 ;;;
11711
11712 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11713   "Display the current article buffer fully MIME-buttonized.
11714 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11715 treated as multipart/mixed."
11716   (interactive "P")
11717   (require 'gnus-art)
11718   (let ((gnus-unbuttonized-mime-types nil)
11719         (gnus-mime-display-multipart-as-mixed show-all-parts))
11720     (gnus-summary-show-article)))
11721
11722 (defun gnus-summary-repair-multipart (article)
11723   "Add a Content-Type header to a multipart article without one."
11724   (interactive (list (gnus-summary-article-number)))
11725   (gnus-with-article article
11726     (message-narrow-to-head)
11727     (message-remove-header "Mime-Version")
11728     (goto-char (point-max))
11729     (insert "Mime-Version: 1.0\n")
11730     (widen)
11731     (when (search-forward "\n--" nil t)
11732       (let ((separator (buffer-substring (point) (point-at-eol))))
11733         (message-narrow-to-head)
11734         (message-remove-header "Content-Type")
11735         (goto-char (point-max))
11736         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11737                         separator))
11738         (widen))))
11739   (let (gnus-mark-article-hook)
11740     (gnus-summary-select-article t t nil article)))
11741
11742 (defun gnus-summary-toggle-display-buttonized ()
11743   "Toggle the buttonizing of the article buffer."
11744   (interactive)
11745   (require 'gnus-art)
11746   (if (setq gnus-inhibit-mime-unbuttonizing
11747             (not gnus-inhibit-mime-unbuttonizing))
11748       (let ((gnus-unbuttonized-mime-types nil))
11749         (gnus-summary-show-article))
11750     (gnus-summary-show-article)))
11751
11752 ;;;
11753 ;;; Intelli-mouse commmands
11754 ;;;
11755
11756 (defun gnus-wheel-summary-scroll (event)
11757   (interactive "e")
11758   (let ((amount (if (memq 'shift (event-modifiers event))
11759                     (car gnus-wheel-scroll-amount)
11760                   (cdr gnus-wheel-scroll-amount)))
11761         (direction (- (* (static-if (featurep 'xemacs)
11762                              (event-button event)
11763                            (cond ((eq 'mouse-4 (event-basic-type event))
11764                                   4)
11765                                  ((eq 'mouse-5 (event-basic-type event))
11766                                   5)))
11767                          2) 9))
11768         edge)
11769     (gnus-summary-scroll-up (* amount direction))
11770     (when (gnus-eval-in-buffer-window gnus-article-buffer
11771             (save-restriction
11772               (widen)
11773               (and (if (< 0 direction)
11774                        (gnus-article-next-page 0)
11775                      (gnus-article-prev-page 0)
11776                      (bobp))
11777                    (if (setq edge (get-text-property
11778                                    (point-min) 'gnus-wheel-edge))
11779                        (setq edge (* edge direction))
11780                      (setq edge -1))
11781                    (or (plusp edge)
11782                        (let ((buffer-read-only nil)
11783                              (inhibit-read-only t))
11784                          (put-text-property (point-min) (point-max)
11785                                             'gnus-wheel-edge direction)
11786                          nil))
11787                    (or (> edge gnus-wheel-edge-resistance)
11788                        (let ((buffer-read-only nil)
11789                              (inhibit-read-only t))
11790                          (put-text-property (point-min) (point-max)
11791                                             'gnus-wheel-edge
11792                                             (* (1+ edge) direction))
11793                          nil))
11794                    (eq last-command 'gnus-wheel-summary-scroll))))
11795       (gnus-summary-next-article nil nil (minusp direction)))))
11796
11797 (defun gnus-wheel-install ()
11798   "Enable mouse wheel support on summary window."
11799   (when gnus-use-wheel
11800     (let ((keys
11801            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11802       (dolist (key keys)
11803         (define-key gnus-summary-mode-map key
11804           'gnus-wheel-summary-scroll)))))
11805
11806 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11807
11808 ;;;
11809 ;;; Traditional PGP commmands
11810 ;;;
11811
11812 (defun gnus-summary-decrypt-article (&optional force)
11813   "Decrypt the current article in traditional PGP way.
11814 This will have permanent effect only in mail groups.
11815 If FORCE is non-nil, allow editing of articles even in read-only
11816 groups."
11817   (interactive "P")
11818   (gnus-summary-select-article t)
11819   (gnus-eval-in-buffer-window gnus-article-buffer
11820     (save-excursion
11821       (save-restriction
11822         (widen)
11823         (goto-char (point-min))
11824         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11825           (error "Not a traditional PGP message!"))
11826         (let ((armor-start (match-beginning 0)))
11827           (if (and (pgg-decrypt-region armor-start (point-max))
11828                    (or force (not (gnus-group-read-only-p))))
11829               (let ((inhibit-read-only t)
11830                     buffer-read-only)
11831                 (delete-region armor-start
11832                                (progn
11833                                  (re-search-forward "^-+END PGP" nil t)
11834                                  (beginning-of-line 2)
11835                                  (point)))
11836                 (insert-buffer-substring pgg-output-buffer))))))))
11837
11838 (defun gnus-summary-verify-article ()
11839   "Verify the current article in traditional PGP way."
11840   (interactive)
11841   (save-excursion
11842     (set-buffer gnus-original-article-buffer)
11843     (goto-char (point-min))
11844     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11845       (error "Not a traditional PGP message!"))
11846     (re-search-forward "^-+END PGP" nil t)
11847     (beginning-of-line 2)
11848     (call-interactively (function pgg-verify-region))))
11849
11850 ;;;
11851 ;;; Generic summary marking commands
11852 ;;;
11853
11854 (defvar gnus-summary-marking-alist
11855   '((read gnus-del-mark "d")
11856     (unread gnus-unread-mark "u")
11857     (ticked gnus-ticked-mark "!")
11858     (dormant gnus-dormant-mark "?")
11859     (expirable gnus-expirable-mark "e"))
11860   "An alist of names/marks/keystrokes.")
11861
11862 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11863 (defvar gnus-summary-mark-map)
11864
11865 (defun gnus-summary-make-all-marking-commands ()
11866   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11867   (dolist (elem gnus-summary-marking-alist)
11868     (apply 'gnus-summary-make-marking-command elem)))
11869
11870 (defun gnus-summary-make-marking-command (name mark keystroke)
11871   (let ((map (make-sparse-keymap)))
11872     (define-key gnus-summary-generic-mark-map keystroke map)
11873     (dolist (lway `((next "next" next nil "n")
11874                     (next-unread "next unread" next t "N")
11875                     (prev "previous" prev nil "p")
11876                     (prev-unread "previous unread" prev t "P")
11877                     (nomove "" nil nil ,keystroke)))
11878       (let ((func (gnus-summary-make-marking-command-1
11879                    mark (car lway) lway name)))
11880         (setq func (eval func))
11881         (define-key map (nth 4 lway) func)))))
11882
11883 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11884   `(defun ,(intern
11885             (format "gnus-summary-put-mark-as-%s%s"
11886                     name (if (eq way 'nomove)
11887                              ""
11888                            (concat "-" (symbol-name way)))))
11889      (n)
11890      ,(format
11891        "Mark the current article as %s%s.
11892 If N, the prefix, then repeat N times.
11893 If N is negative, move in reverse order.
11894 The difference between N and the actual number of articles marked is
11895 returned."
11896        name (car (cdr lway)))
11897      (interactive "p")
11898      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11899
11900 (defun gnus-summary-generic-mark (n mark move unread)
11901   "Mark N articles with MARK."
11902   (unless (eq major-mode 'gnus-summary-mode)
11903     (error "This command can only be used in the summary buffer"))
11904   (gnus-summary-show-thread)
11905   (let ((nummove
11906          (cond
11907           ((eq move 'next) 1)
11908           ((eq move 'prev) -1)
11909           (t 0))))
11910     (if (zerop nummove)
11911         (setq n 1)
11912       (when (< n 0)
11913         (setq n (abs n)
11914               nummove (* -1 nummove))))
11915     (while (and (> n 0)
11916                 (gnus-summary-mark-article nil mark)
11917                 (zerop (gnus-summary-next-subject nummove unread t)))
11918       (setq n (1- n)))
11919     (when (/= 0 n)
11920       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11921     (gnus-summary-recenter)
11922     (gnus-summary-position-point)
11923     (gnus-set-mode-line 'summary)
11924     n))
11925
11926 (defun gnus-summary-insert-articles (articles)
11927   (when (setq articles
11928               (gnus-sorted-difference articles
11929                                       (mapcar (lambda (h)
11930                                                 (mail-header-number h))
11931                                               gnus-newsgroup-headers)))
11932     (setq gnus-newsgroup-headers
11933           (gnus-merge 'list
11934                       gnus-newsgroup-headers
11935                       (gnus-fetch-headers articles)
11936                       'gnus-article-sort-by-number))
11937     ;; Suppress duplicates?
11938     (when gnus-suppress-duplicates
11939       (gnus-dup-suppress-articles))
11940
11941     ;; We might want to build some more threads first.
11942     (when (and gnus-fetch-old-headers
11943                (eq gnus-headers-retrieved-by 'nov))
11944       (if (eq gnus-fetch-old-headers 'invisible)
11945           (gnus-build-all-threads)
11946         (gnus-build-old-threads)))
11947     ;; Let the Gnus agent mark articles as read.
11948     (when gnus-agent
11949       (gnus-agent-get-undownloaded-list))
11950     ;; Remove list identifiers from subject
11951     (when gnus-list-identifiers
11952       (gnus-summary-remove-list-identifiers))
11953     ;; First and last article in this newsgroup.
11954     (when gnus-newsgroup-headers
11955       (setq gnus-newsgroup-begin
11956             (mail-header-number (car gnus-newsgroup-headers))
11957             gnus-newsgroup-end
11958             (mail-header-number
11959              (gnus-last-element gnus-newsgroup-headers))))
11960     (when gnus-use-scoring
11961       (gnus-possibly-score-headers))))
11962
11963 (defun gnus-summary-insert-old-articles (&optional all)
11964   "Insert all old articles in this group.
11965 If ALL is non-nil, already read articles become readable.
11966 If ALL is a number, fetch this number of articles."
11967   (interactive "P")
11968   (prog1
11969       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11970             older len)
11971         (setq older
11972               ;; Some nntp servers lie about their active range.  When
11973               ;; this happens, the active range can be in the millions.
11974               ;; Use a compressed range to avoid creating a huge list.
11975               (gnus-range-difference (list gnus-newsgroup-active) old))
11976         (setq len (gnus-range-length older))
11977         (cond
11978          ((null older) nil)
11979          ((numberp all)
11980           (if (< all len)
11981               (let ((older-range (nreverse older)))
11982                 (setq older nil)
11983
11984                 (while (> all 0)
11985                   (let* ((r (pop older-range))
11986                          (min (if (numberp r) r (car r)))
11987                          (max (if (numberp r) r (cdr r))))
11988                     (while (and (<= min max)
11989                                 (> all 0))
11990                       (push max older)
11991                       (setq all (1- all)
11992                             max (1- max))))))
11993             (setq older (gnus-uncompress-range older))))
11994          (all
11995           (setq older (gnus-uncompress-range older)))
11996          (t
11997           (when (and (numberp gnus-large-newsgroup)
11998                    (> len gnus-large-newsgroup))
11999               (let* ((cursor-in-echo-area nil)
12000                      (initial (gnus-parameter-large-newsgroup-initial
12001                                gnus-newsgroup-name))
12002                      (input
12003                       (read-string
12004                        (format
12005                         "How many articles from %s (%s %d): "
12006                         (gnus-limit-string
12007                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12008                         (if initial "max" "default")
12009                         len)
12010                        (if initial
12011                            (cons (number-to-string initial)
12012                                  0)))))
12013                 (unless (string-match "^[ \t]*$" input)
12014                   (setq all (string-to-number input))
12015                   (if (< all len)
12016                       (let ((older-range (nreverse older)))
12017                         (setq older nil)
12018
12019                         (while (> all 0)
12020                           (let* ((r (pop older-range))
12021                                  (min (if (numberp r) r (car r)))
12022                                  (max (if (numberp r) r (cdr r))))
12023                             (while (and (<= min max)
12024                                         (> all 0))
12025                               (push max older)
12026                               (setq all (1- all)
12027                                     max (1- max))))))))))
12028           (setq older (gnus-uncompress-range older))))
12029         (if (not older)
12030             (message "No old news.")
12031           (gnus-summary-insert-articles older)
12032           (gnus-summary-limit (gnus-sorted-nunion old older))))
12033     (gnus-summary-position-point)))
12034
12035 (defun gnus-summary-insert-new-articles ()
12036   "Insert all new articles in this group."
12037   (interactive)
12038   (prog1
12039       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12040             (old-active gnus-newsgroup-active)
12041             (nnmail-fetched-sources (list t))
12042             i new)
12043         (setq gnus-newsgroup-active
12044               (gnus-activate-group gnus-newsgroup-name 'scan))
12045         (setq i (cdr gnus-newsgroup-active))
12046         (while (> i (cdr old-active))
12047           (push i new)
12048           (decf i))
12049         (if (not new)
12050             (message "No gnus is bad news")
12051           (gnus-summary-insert-articles new)
12052           (setq gnus-newsgroup-unreads
12053                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12054           (gnus-summary-limit (gnus-sorted-nunion old new))))
12055     (gnus-summary-position-point)))
12056
12057 (gnus-summary-make-all-marking-commands)
12058
12059 (gnus-ems-redefine)
12060
12061 (provide 'gnus-sum)
12062
12063 (run-hooks 'gnus-sum-load-hook)
12064
12065 ;; Local Variables:
12066 ;; coding: iso-8859-1
12067 ;; End:
12068
12069 ;;; gnus-sum.el ends here