d16da42e6fb7638af701ce5589c1f72358bfbd8e
[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-agent)
1526     (require 'gnus-art)))
1527
1528 ;; Subject simplification.
1529
1530 (defun gnus-simplify-whitespace (str)
1531   "Remove excessive whitespace from STR."
1532   ;; Multiple spaces.
1533   (while (string-match "[ \t][ \t]+" str)
1534     (setq str (concat (substring str 0 (match-beginning 0))
1535                         " "
1536                         (substring str (match-end 0)))))
1537   ;; Leading spaces.
1538   (when (string-match "^[ \t]+" str)
1539     (setq str (substring str (match-end 0))))
1540   ;; Trailing spaces.
1541   (when (string-match "[ \t]+$" str)
1542     (setq str (substring str 0 (match-beginning 0))))
1543   str)
1544
1545 (defun gnus-simplify-all-whitespace (str)
1546   "Remove all whitespace from STR."
1547   (while (string-match "[ \t\n]+" str)
1548     (setq str (replace-match "" nil nil str)))
1549   str)
1550
1551 (defsubst gnus-simplify-subject-re (subject)
1552   "Remove \"Re:\" from subject lines."
1553   (if (string-match message-subject-re-regexp subject)
1554       (substring subject (match-end 0))
1555     subject))
1556
1557 (defun gnus-simplify-subject (subject &optional re-only)
1558   "Remove `Re:' and words in parentheses.
1559 If RE-ONLY is non-nil, strip leading `Re:'s only."
1560   (let ((case-fold-search t))           ;Ignore case.
1561     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1562     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1563       (setq subject (substring subject (match-end 0))))
1564     ;; Remove uninteresting prefixes.
1565     (when (and (not re-only)
1566                gnus-simplify-ignored-prefixes
1567                (string-match gnus-simplify-ignored-prefixes subject))
1568       (setq subject (substring subject (match-end 0))))
1569     ;; Remove words in parentheses from end.
1570     (unless re-only
1571       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1572         (setq subject (substring subject 0 (match-beginning 0)))))
1573     ;; Return subject string.
1574     subject))
1575
1576 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1577 ;; all whitespace.
1578 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1579   (goto-char (point-min))
1580   (while (re-search-forward regexp nil t)
1581     (replace-match (or newtext ""))))
1582
1583 (defun gnus-simplify-buffer-fuzzy ()
1584   "Simplify string in the buffer fuzzily.
1585 The string in the accessible portion of the current buffer is simplified.
1586 It is assumed to be a single-line subject.
1587 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1588 matter is removed.  Additional things can be deleted by setting
1589 `gnus-simplify-subject-fuzzy-regexp'."
1590   (let ((case-fold-search t)
1591         (modified-tick))
1592     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1593
1594     (while (not (eq modified-tick (buffer-modified-tick)))
1595       (setq modified-tick (buffer-modified-tick))
1596       (cond
1597        ((listp gnus-simplify-subject-fuzzy-regexp)
1598         (mapcar 'gnus-simplify-buffer-fuzzy-step
1599                 gnus-simplify-subject-fuzzy-regexp))
1600        (gnus-simplify-subject-fuzzy-regexp
1601         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1602       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1603       (gnus-simplify-buffer-fuzzy-step
1604        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1605       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1606
1607     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1608     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1609     (gnus-simplify-buffer-fuzzy-step " $")
1610     (gnus-simplify-buffer-fuzzy-step "^ +")))
1611
1612 (defun gnus-simplify-subject-fuzzy (subject)
1613   "Simplify a subject string fuzzily.
1614 See `gnus-simplify-buffer-fuzzy' for details."
1615   (save-excursion
1616     (gnus-set-work-buffer)
1617     (let ((case-fold-search t))
1618       ;; Remove uninteresting prefixes.
1619       (when (and gnus-simplify-ignored-prefixes
1620                  (string-match gnus-simplify-ignored-prefixes subject))
1621         (setq subject (substring subject (match-end 0))))
1622       (insert subject)
1623       (inline (gnus-simplify-buffer-fuzzy))
1624       (buffer-string))))
1625
1626 (defsubst gnus-simplify-subject-fully (subject)
1627   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1628   (cond
1629    (gnus-simplify-subject-functions
1630     (gnus-map-function gnus-simplify-subject-functions subject))
1631    ((null gnus-summary-gather-subject-limit)
1632     (gnus-simplify-subject-re subject))
1633    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1634     (gnus-simplify-subject-fuzzy subject))
1635    ((numberp gnus-summary-gather-subject-limit)
1636     (gnus-limit-string (gnus-simplify-subject-re subject)
1637                        gnus-summary-gather-subject-limit))
1638    (t
1639     subject)))
1640
1641 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1642   "Check whether two subjects are equal.
1643 If optional argument SIMPLE-FIRST is t, first argument is already
1644 simplified."
1645   (cond
1646    ((null simple-first)
1647     (equal (gnus-simplify-subject-fully s1)
1648            (gnus-simplify-subject-fully s2)))
1649    (t
1650     (equal s1
1651            (gnus-simplify-subject-fully s2)))))
1652
1653 (defun gnus-summary-bubble-group ()
1654   "Increase the score of the current group.
1655 This is a handy function to add to `gnus-summary-exit-hook' to
1656 increase the score of each group you read."
1657   (gnus-group-add-score gnus-newsgroup-name))
1658
1659 \f
1660 ;;;
1661 ;;; Gnus summary mode
1662 ;;;
1663
1664 (put 'gnus-summary-mode 'mode-class 'special)
1665
1666 (defvar gnus-article-commands-menu)
1667
1668 ;; Non-orthogonal keys
1669
1670 (gnus-define-keys gnus-summary-mode-map
1671   " " gnus-summary-next-page
1672   "\177" gnus-summary-prev-page
1673   [delete] gnus-summary-prev-page
1674   [backspace] gnus-summary-prev-page
1675   "\r" gnus-summary-scroll-up
1676   "\M-\r" gnus-summary-scroll-down
1677   "n" gnus-summary-next-unread-article
1678   "p" gnus-summary-prev-unread-article
1679   "N" gnus-summary-next-article
1680   "P" gnus-summary-prev-article
1681   "\M-\C-n" gnus-summary-next-same-subject
1682   "\M-\C-p" gnus-summary-prev-same-subject
1683   "\M-n" gnus-summary-next-unread-subject
1684   "\M-p" gnus-summary-prev-unread-subject
1685   "." gnus-summary-first-unread-article
1686   "," gnus-summary-best-unread-article
1687   "\M-s" gnus-summary-search-article-forward
1688   "\M-r" gnus-summary-search-article-backward
1689   "<" gnus-summary-beginning-of-article
1690   ">" gnus-summary-end-of-article
1691   "j" gnus-summary-goto-article
1692   "^" gnus-summary-refer-parent-article
1693   "\M-^" gnus-summary-refer-article
1694   "u" gnus-summary-tick-article-forward
1695   "!" gnus-summary-tick-article-forward
1696   "U" gnus-summary-tick-article-backward
1697   "d" gnus-summary-mark-as-read-forward
1698   "D" gnus-summary-mark-as-read-backward
1699   "E" gnus-summary-mark-as-expirable
1700   "\M-u" gnus-summary-clear-mark-forward
1701   "\M-U" gnus-summary-clear-mark-backward
1702   "k" gnus-summary-kill-same-subject-and-select
1703   "\C-k" gnus-summary-kill-same-subject
1704   "\M-\C-k" gnus-summary-kill-thread
1705   "\M-\C-l" gnus-summary-lower-thread
1706   "e" gnus-summary-edit-article
1707   "#" gnus-summary-mark-as-processable
1708   "\M-#" gnus-summary-unmark-as-processable
1709   "\M-\C-t" gnus-summary-toggle-threads
1710   "\M-\C-s" gnus-summary-show-thread
1711   "\M-\C-h" gnus-summary-hide-thread
1712   "\M-\C-f" gnus-summary-next-thread
1713   "\M-\C-b" gnus-summary-prev-thread
1714   [(meta down)] gnus-summary-next-thread
1715   [(meta up)] gnus-summary-prev-thread
1716   "\M-\C-u" gnus-summary-up-thread
1717   "\M-\C-d" gnus-summary-down-thread
1718   "&" gnus-summary-execute-command
1719   "c" gnus-summary-catchup-and-exit
1720   "\C-w" gnus-summary-mark-region-as-read
1721   "\C-t" gnus-summary-toggle-truncation
1722   "?" gnus-summary-mark-as-dormant
1723   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1724   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1725   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1726   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1727   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1728   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1729   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1730   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1731   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1732   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1733   "=" gnus-summary-expand-window
1734   "\C-x\C-s" gnus-summary-reselect-current-group
1735   "\M-g" gnus-summary-rescan-group
1736   "w" gnus-summary-stop-page-breaking
1737   "\C-c\C-r" gnus-summary-caesar-message
1738   "\M-t" gnus-summary-toggle-mime
1739   "f" gnus-summary-followup
1740   "F" gnus-summary-followup-with-original
1741   "C" gnus-summary-cancel-article
1742   "r" gnus-summary-reply
1743   "R" gnus-summary-reply-with-original
1744   "\C-c\C-f" gnus-summary-mail-forward
1745   "o" gnus-summary-save-article
1746   "\C-o" gnus-summary-save-article-mail
1747   "|" gnus-summary-pipe-output
1748   "\M-k" gnus-summary-edit-local-kill
1749   "\M-K" gnus-summary-edit-global-kill
1750   ;; "V" gnus-version
1751   "\C-c\C-d" gnus-summary-describe-group
1752   "q" gnus-summary-exit
1753   "Q" gnus-summary-exit-no-update
1754   "\C-c\C-i" gnus-info-find-node
1755   gnus-mouse-2 gnus-mouse-pick-article
1756   "m" gnus-summary-mail-other-window
1757   "a" gnus-summary-post-news
1758   "i" gnus-summary-news-other-window
1759   "x" gnus-summary-limit-to-unread
1760   "s" gnus-summary-isearch-article
1761   "t" gnus-summary-toggle-header
1762   "g" gnus-summary-show-article
1763   "l" gnus-summary-goto-last-article
1764   "v" gnus-summary-preview-mime-message
1765   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1766   "\C-d" gnus-summary-enter-digest-group
1767   "\M-\C-d" gnus-summary-read-document
1768   "\M-\C-e" gnus-summary-edit-parameters
1769   "\M-\C-a" gnus-summary-customize-parameters
1770   "\C-c\C-b" gnus-bug
1771   "\C-c\C-n" gnus-namazu-search
1772   "*" gnus-cache-enter-article
1773   "\M-*" gnus-cache-remove-article
1774   "\M-&" gnus-summary-universal-argument
1775   "\C-l" gnus-recenter
1776   "I" gnus-summary-increase-score
1777   "L" gnus-summary-lower-score
1778   "\M-i" gnus-symbolic-argument
1779   "h" gnus-summary-select-article-buffer
1780
1781   "V" gnus-summary-score-map
1782   "X" gnus-uu-extract-map
1783   "S" gnus-summary-send-map)
1784
1785   ;; Sort of orthogonal keymap
1786 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1787   "t" gnus-summary-tick-article-forward
1788   "!" gnus-summary-tick-article-forward
1789   "d" gnus-summary-mark-as-read-forward
1790   "r" gnus-summary-mark-as-read-forward
1791   "c" gnus-summary-clear-mark-forward
1792   " " gnus-summary-clear-mark-forward
1793   "e" gnus-summary-mark-as-expirable
1794   "x" gnus-summary-mark-as-expirable
1795   "?" gnus-summary-mark-as-dormant
1796   "b" gnus-summary-set-bookmark
1797   "B" gnus-summary-remove-bookmark
1798   "#" gnus-summary-mark-as-processable
1799   "\M-#" gnus-summary-unmark-as-processable
1800   "S" gnus-summary-limit-include-expunged
1801   "C" gnus-summary-catchup
1802   "H" gnus-summary-catchup-to-here
1803   "h" gnus-summary-catchup-from-here
1804   "\C-c" gnus-summary-catchup-all
1805   "k" gnus-summary-kill-same-subject-and-select
1806   "K" gnus-summary-kill-same-subject
1807   "P" gnus-uu-mark-map)
1808
1809 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1810   "c" gnus-summary-clear-above
1811   "u" gnus-summary-tick-above
1812   "m" gnus-summary-mark-above
1813   "k" gnus-summary-kill-below)
1814
1815 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1816   "/" gnus-summary-limit-to-subject
1817   "n" gnus-summary-limit-to-articles
1818   "w" gnus-summary-pop-limit
1819   "s" gnus-summary-limit-to-subject
1820   "a" gnus-summary-limit-to-author
1821   "u" gnus-summary-limit-to-unread
1822   "m" gnus-summary-limit-to-marks
1823   "M" gnus-summary-limit-exclude-marks
1824   "v" gnus-summary-limit-to-score
1825   "*" gnus-summary-limit-include-cached
1826   "D" gnus-summary-limit-include-dormant
1827   "T" gnus-summary-limit-include-thread
1828   "d" gnus-summary-limit-exclude-dormant
1829   "t" gnus-summary-limit-to-age
1830   "." gnus-summary-limit-to-unseen
1831   "x" gnus-summary-limit-to-extra
1832   "p" gnus-summary-limit-to-display-predicate
1833   "E" gnus-summary-limit-include-expunged
1834   "c" gnus-summary-limit-exclude-childless-dormant
1835   "C" gnus-summary-limit-mark-excluded-as-read
1836   "o" gnus-summary-insert-old-articles
1837   "N" gnus-summary-insert-new-articles
1838   "r" gnus-summary-limit-to-replied)
1839
1840 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1841   "n" gnus-summary-next-unread-article
1842   "p" gnus-summary-prev-unread-article
1843   "N" gnus-summary-next-article
1844   "P" gnus-summary-prev-article
1845   "\C-n" gnus-summary-next-same-subject
1846   "\C-p" gnus-summary-prev-same-subject
1847   "\M-n" gnus-summary-next-unread-subject
1848   "\M-p" gnus-summary-prev-unread-subject
1849   "f" gnus-summary-first-unread-article
1850   "b" gnus-summary-best-unread-article
1851   "j" gnus-summary-goto-article
1852   "g" gnus-summary-goto-subject
1853   "l" gnus-summary-goto-last-article
1854   "o" gnus-summary-pop-article)
1855
1856 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1857   "k" gnus-summary-kill-thread
1858   "l" gnus-summary-lower-thread
1859   "i" gnus-summary-raise-thread
1860   "T" gnus-summary-toggle-threads
1861   "t" gnus-summary-rethread-current
1862   "^" gnus-summary-reparent-thread
1863   "s" gnus-summary-show-thread
1864   "S" gnus-summary-show-all-threads
1865   "h" gnus-summary-hide-thread
1866   "H" gnus-summary-hide-all-threads
1867   "n" gnus-summary-next-thread
1868   "p" gnus-summary-prev-thread
1869   "u" gnus-summary-up-thread
1870   "o" gnus-summary-top-thread
1871   "d" gnus-summary-down-thread
1872   "#" gnus-uu-mark-thread
1873   "\M-#" gnus-uu-unmark-thread)
1874
1875 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1876   "g" gnus-summary-prepare
1877   "c" gnus-summary-insert-cached-articles
1878   "d" gnus-summary-insert-dormant-articles)
1879
1880 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1881   "c" gnus-summary-catchup-and-exit
1882   "C" gnus-summary-catchup-all-and-exit
1883   "E" gnus-summary-exit-no-update
1884   "J" gnus-summary-jump-to-other-group
1885   "Q" gnus-summary-exit
1886   "Z" gnus-summary-exit
1887   "n" gnus-summary-catchup-and-goto-next-group
1888   "R" gnus-summary-reselect-current-group
1889   "G" gnus-summary-rescan-group
1890   "N" gnus-summary-next-group
1891   "s" gnus-summary-save-newsrc
1892   "P" gnus-summary-prev-group)
1893
1894 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1895   " " gnus-summary-next-page
1896   "n" gnus-summary-next-page
1897   "\177" gnus-summary-prev-page
1898   [delete] gnus-summary-prev-page
1899   "p" gnus-summary-prev-page
1900   "\r" gnus-summary-scroll-up
1901   "\M-\r" gnus-summary-scroll-down
1902   "<" gnus-summary-beginning-of-article
1903   ">" gnus-summary-end-of-article
1904   "b" gnus-summary-beginning-of-article
1905   "e" gnus-summary-end-of-article
1906   "^" gnus-summary-refer-parent-article
1907   "r" gnus-summary-refer-parent-article
1908   "D" gnus-summary-enter-digest-group
1909   "R" gnus-summary-refer-references
1910   "T" gnus-summary-refer-thread
1911   "g" gnus-summary-show-article
1912   "s" gnus-summary-isearch-article
1913   "P" gnus-summary-print-article
1914   "M" gnus-mailing-list-insinuate
1915   "t" gnus-article-babel)
1916
1917 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1918   "b" gnus-article-add-buttons
1919   "B" gnus-article-add-buttons-to-head
1920   "o" gnus-article-treat-overstrike
1921   "e" gnus-article-emphasize
1922   "w" gnus-article-fill-cited-article
1923   "Q" gnus-article-fill-long-lines
1924   "C" gnus-article-capitalize-sentences
1925   "c" gnus-article-remove-cr
1926   "Z" gnus-article-decode-HZ
1927   "A" gnus-article-treat-ansi-sequences
1928   "h" gnus-article-wash-html
1929   "u" gnus-article-unsplit-urls
1930   "f" gnus-article-display-x-face
1931   "l" gnus-summary-stop-page-breaking
1932   "r" gnus-summary-caesar-message
1933   "m" gnus-summary-morse-message
1934   "t" gnus-summary-toggle-header
1935   "g" gnus-treat-smiley
1936   "v" gnus-summary-verbose-headers
1937   "m" gnus-summary-toggle-mime
1938   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1939   "p" gnus-article-verify-x-pgp-sig
1940   "d" gnus-article-treat-dumbquotes)
1941
1942 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1943   ;; mnemonic: deuglif*Y*
1944   "u" gnus-article-outlook-unwrap-lines
1945   "a" gnus-article-outlook-repair-attribution
1946   "c" gnus-article-outlook-rearrange-citation
1947   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1948
1949 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1950   "a" gnus-article-hide
1951   "h" gnus-article-hide-headers
1952   "b" gnus-article-hide-boring-headers
1953   "s" gnus-article-hide-signature
1954   "c" gnus-article-hide-citation
1955   "C" gnus-article-hide-citation-in-followups
1956   "l" gnus-article-hide-list-identifiers
1957   "B" gnus-article-strip-banner
1958   "P" gnus-article-hide-pem
1959   "\C-c" gnus-article-hide-citation-maybe)
1960
1961 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1962   "a" gnus-article-highlight
1963   "h" gnus-article-highlight-headers
1964   "c" gnus-article-highlight-citation
1965   "s" gnus-article-highlight-signature)
1966
1967 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1968   "f" gnus-article-treat-fold-headers
1969   "u" gnus-article-treat-unfold-headers
1970   "n" gnus-article-treat-fold-newsgroups)
1971
1972 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1973   "x" gnus-article-display-x-face
1974   "d" gnus-article-display-face
1975   "s" gnus-treat-smiley
1976   "D" gnus-article-remove-images
1977   "f" gnus-treat-from-picon
1978   "m" gnus-treat-mail-picon
1979   "n" gnus-treat-newsgroups-picon)
1980
1981 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1982   "z" gnus-article-date-ut
1983   "u" gnus-article-date-ut
1984   "l" gnus-article-date-local
1985   "p" gnus-article-date-english
1986   "e" gnus-article-date-lapsed
1987   "o" gnus-article-date-original
1988   "i" gnus-article-date-iso8601
1989   "s" gnus-article-date-user)
1990
1991 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1992   "t" gnus-article-remove-trailing-blank-lines
1993   "l" gnus-article-strip-leading-blank-lines
1994   "m" gnus-article-strip-multiple-blank-lines
1995   "a" gnus-article-strip-blank-lines
1996   "A" gnus-article-strip-all-blank-lines
1997   "s" gnus-article-strip-leading-space
1998   "e" gnus-article-strip-trailing-space
1999   "w" gnus-article-remove-leading-whitespace)
2000
2001 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2002   "v" gnus-version
2003   "f" gnus-summary-fetch-faq
2004   "d" gnus-summary-describe-group
2005   "h" gnus-summary-describe-briefly
2006   "i" gnus-info-find-node
2007   "c" gnus-group-fetch-charter
2008   "C" gnus-group-fetch-control)
2009
2010 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2011   "e" gnus-summary-expire-articles
2012   "\M-\C-e" gnus-summary-expire-articles-now
2013   "\177" gnus-summary-delete-article
2014   [delete] gnus-summary-delete-article
2015   [backspace] gnus-summary-delete-article
2016   "m" gnus-summary-move-article
2017   "r" gnus-summary-respool-article
2018   "w" gnus-summary-edit-article
2019   "c" gnus-summary-copy-article
2020   "B" gnus-summary-crosspost-article
2021   "q" gnus-summary-respool-query
2022   "t" gnus-summary-respool-trace
2023   "i" gnus-summary-import-article
2024   "I" gnus-summary-create-article
2025   "p" gnus-summary-article-posted-p)
2026
2027 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2028   "o" gnus-summary-save-article
2029   "m" gnus-summary-save-article-mail
2030   "F" gnus-summary-write-article-file
2031   "r" gnus-summary-save-article-rmail
2032   "f" gnus-summary-save-article-file
2033   "b" gnus-summary-save-article-body-file
2034   "h" gnus-summary-save-article-folder
2035   "v" gnus-summary-save-article-vm
2036   "p" gnus-summary-pipe-output
2037   "P" gnus-summary-muttprint
2038   "s" gnus-soup-add-article)
2039
2040 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2041   "b" gnus-summary-display-buttonized
2042   "m" gnus-summary-repair-multipart
2043   "v" gnus-article-view-part
2044   "o" gnus-article-save-part
2045   "c" gnus-article-copy-part
2046   "C" gnus-article-view-part-as-charset
2047   "e" gnus-article-view-part-externally
2048   "E" gnus-article-encrypt-body
2049   "i" gnus-article-inline-part
2050   "|" gnus-article-pipe-part)
2051
2052 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2053   "p" gnus-summary-mark-as-processable
2054   "u" gnus-summary-unmark-as-processable
2055   "U" gnus-summary-unmark-all-processable
2056   "v" gnus-uu-mark-over
2057   "s" gnus-uu-mark-series
2058   "r" gnus-uu-mark-region
2059   "g" gnus-uu-unmark-region
2060   "R" gnus-uu-mark-by-regexp
2061   "G" gnus-uu-unmark-by-regexp
2062   "t" gnus-uu-mark-thread
2063   "T" gnus-uu-unmark-thread
2064   "a" gnus-uu-mark-all
2065   "b" gnus-uu-mark-buffer
2066   "S" gnus-uu-mark-sparse
2067   "k" gnus-summary-kill-process-mark
2068   "y" gnus-summary-yank-process-mark
2069   "w" gnus-summary-save-process-mark
2070   "i" gnus-uu-invert-processable)
2071
2072 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2073   ;;"x" gnus-uu-extract-any
2074   "m" gnus-summary-save-parts
2075   "u" gnus-uu-decode-uu
2076   "U" gnus-uu-decode-uu-and-save
2077   "s" gnus-uu-decode-unshar
2078   "S" gnus-uu-decode-unshar-and-save
2079   "o" gnus-uu-decode-save
2080   "O" gnus-uu-decode-save
2081   "b" gnus-uu-decode-binhex
2082   "B" gnus-uu-decode-binhex
2083   "p" gnus-uu-decode-postscript
2084   "P" gnus-uu-decode-postscript-and-save)
2085
2086 (gnus-define-keys
2087     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2088   "u" gnus-uu-decode-uu-view
2089   "U" gnus-uu-decode-uu-and-save-view
2090   "s" gnus-uu-decode-unshar-view
2091   "S" gnus-uu-decode-unshar-and-save-view
2092   "o" gnus-uu-decode-save-view
2093   "O" gnus-uu-decode-save-view
2094   "b" gnus-uu-decode-binhex-view
2095   "B" gnus-uu-decode-binhex-view
2096   "p" gnus-uu-decode-postscript-view
2097   "P" gnus-uu-decode-postscript-and-save-view)
2098
2099 (defvar gnus-article-post-menu nil)
2100
2101 (defconst gnus-summary-menu-maxlen 20)
2102
2103 (defun gnus-summary-menu-split (menu)
2104   ;; If we have lots of elements, divide them into groups of 20
2105   ;; and make a pane (or submenu) for each one.
2106   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2107       (let ((menu menu) sublists next
2108             (i 1))
2109         (while menu
2110           ;; Pull off the next gnus-summary-menu-maxlen elements
2111           ;; and make them the next element of sublist.
2112           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2113           (if next
2114               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2115                       nil))
2116           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2117                                              (aref (car (last menu)) 0)) menu)
2118                                sublists))
2119           (setq i (1+ i))
2120           (setq menu next))
2121         (nreverse sublists))
2122     ;; Few elements--put them all in one pane.
2123     menu))
2124
2125 (defun gnus-summary-make-menu-bar ()
2126   (gnus-turn-off-edit-menu 'summary)
2127
2128   (unless (boundp 'gnus-summary-misc-menu)
2129
2130     (easy-menu-define
2131       gnus-summary-kill-menu gnus-summary-mode-map ""
2132       (cons
2133        "Score"
2134        (nconc
2135         (list
2136          ["Customize" gnus-score-customize t])
2137         (gnus-make-score-map 'increase)
2138         (gnus-make-score-map 'lower)
2139         '(("Mark"
2140            ["Kill below" gnus-summary-kill-below t]
2141            ["Mark above" gnus-summary-mark-above t]
2142            ["Tick above" gnus-summary-tick-above t]
2143            ["Clear above" gnus-summary-clear-above t])
2144           ["Current score" gnus-summary-current-score t]
2145           ["Set score" gnus-summary-set-score t]
2146           ["Switch current score file..." gnus-score-change-score-file t]
2147           ["Set mark below..." gnus-score-set-mark-below t]
2148           ["Set expunge below..." gnus-score-set-expunge-below t]
2149           ["Edit current score file" gnus-score-edit-current-scores t]
2150           ["Edit score file" gnus-score-edit-file t]
2151           ["Trace score" gnus-score-find-trace t]
2152           ["Find words" gnus-score-find-favourite-words t]
2153           ["Rescore buffer" gnus-summary-rescore t]
2154           ["Increase score..." gnus-summary-increase-score t]
2155           ["Lower score..." gnus-summary-lower-score t]))))
2156
2157     ;; Define both the Article menu in the summary buffer and the
2158     ;; equivalent Commands menu in the article buffer here for
2159     ;; consistency.
2160     (let ((innards
2161            `(("Hide"
2162               ["All" gnus-article-hide t]
2163               ["Headers" gnus-article-hide-headers t]
2164               ["Signature" gnus-article-hide-signature t]
2165               ["Citation" gnus-article-hide-citation t]
2166               ["List identifiers" gnus-article-hide-list-identifiers t]
2167               ["Banner" gnus-article-strip-banner t]
2168               ["Boring headers" gnus-article-hide-boring-headers t])
2169              ("Highlight"
2170               ["All" gnus-article-highlight t]
2171               ["Headers" gnus-article-highlight-headers t]
2172               ["Signature" gnus-article-highlight-signature t]
2173               ["Citation" gnus-article-highlight-citation t])
2174              ("Date"
2175               ["Local" gnus-article-date-local t]
2176               ["ISO8601" gnus-article-date-iso8601 t]
2177               ["UT" gnus-article-date-ut t]
2178               ["Original" gnus-article-date-original t]
2179               ["Lapsed" gnus-article-date-lapsed t]
2180               ["User-defined" gnus-article-date-user t])
2181              ("Display"
2182               ["Remove images" gnus-article-remove-images t]
2183               ["Toggle smiley" gnus-treat-smiley t]
2184               ["Show X-Face" gnus-article-display-x-face t]
2185               ["Show picons in From" gnus-treat-from-picon t]
2186               ["Show picons in mail headers" gnus-treat-mail-picon t]
2187               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2188               ("View as different encoding"
2189                ,@(gnus-summary-menu-split
2190                   (mapcar
2191                    (lambda (cs)
2192                      ;; Since easymenu under Emacs doesn't allow
2193                      ;; lambda forms for menu commands, we should
2194                      ;; provide intern'ed function symbols.
2195                      (let ((command (intern (format "\
2196 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2197                        (fset command
2198                              `(lambda ()
2199                                 (interactive)
2200                                 (let ((gnus-summary-show-article-charset-alist
2201                                        '((1 . ,cs))))
2202                                   (gnus-summary-show-article 1))))
2203                        `[,(symbol-name cs) ,command t]))
2204                    (sort (if (fboundp 'coding-system-list)
2205                              (coding-system-list)
2206                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2207                            )
2208                          'string<)))))
2209              ("Washing"
2210               ("Remove Blanks"
2211                ["Leading" gnus-article-strip-leading-blank-lines t]
2212                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2213                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2214                ["All of the above" gnus-article-strip-blank-lines t]
2215                ["All" gnus-article-strip-all-blank-lines t]
2216                ["Leading space" gnus-article-strip-leading-space t]
2217                ["Trailing space" gnus-article-strip-trailing-space t]
2218                ["Leading space in headers"
2219                 gnus-article-remove-leading-whitespace t])
2220               ["Overstrike" gnus-article-treat-overstrike t]
2221               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2222               ["Emphasis" gnus-article-emphasize t]
2223               ["Word wrap" gnus-article-fill-cited-article t]
2224               ["Fill long lines" gnus-article-fill-long-lines t]
2225               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2226               ["Remove CR" gnus-article-remove-cr t]
2227               ["Rot 13" gnus-summary-caesar-message
2228                ,@(if (featurep 'xemacs) '(t)
2229                    '(:help "\"Caesar rotate\" article by 13"))]
2230               ["Morse decode" gnus-summary-morse-message t]
2231               ["Unix pipe..." gnus-summary-pipe-message t]
2232               ["Add buttons" gnus-article-add-buttons t]
2233               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2234               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2235               ["Toggle MIME" gnus-summary-toggle-mime t]
2236               ["Verbose header" gnus-summary-verbose-headers t]
2237               ["Toggle header" gnus-summary-toggle-header t]
2238               ["Unfold headers" gnus-article-treat-unfold-headers t]
2239               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2240               ["Html" gnus-article-wash-html t]
2241               ["Unsplit URLs" gnus-article-unsplit-urls t]
2242               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2243               ["Decode HZ" gnus-article-decode-HZ t]
2244               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2245               ("(Outlook) Deuglify"
2246                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2247                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2248                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2249                ["Full (Outlook) deuglify"
2250                 gnus-article-outlook-deuglify-article t])
2251               )
2252              ("Output"
2253               ["Save in default format..." gnus-summary-save-article
2254                ,@(if (featurep 'xemacs) '(t)
2255                    '(:help "Save article using default method"))]
2256               ["Save in file..." gnus-summary-save-article-file
2257                ,@(if (featurep 'xemacs) '(t)
2258                    '(:help "Save article in file"))]
2259               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2260               ["Save in MH folder..." gnus-summary-save-article-folder t]
2261               ["Save in VM folder..." gnus-summary-save-article-vm t]
2262               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2263               ["Save body in file..." gnus-summary-save-article-body-file t]
2264               ["Pipe through a filter..." gnus-summary-pipe-output t]
2265               ["Add to SOUP packet" gnus-soup-add-article t]
2266               ["Print with Muttprint..." gnus-summary-muttprint t]
2267               ["Print" gnus-summary-print-article
2268                ,@(if (featurep 'xemacs) '(t)
2269                    '(:help "Generate and print a PostScript image"))])
2270              ("Copy, move,... (Backend)"
2271               ,@(if (featurep 'xemacs) '(t)
2272                   '(:help "Copying, moving, expiring articles..."))
2273               ["Respool article..." gnus-summary-respool-article t]
2274               ["Move article..." gnus-summary-move-article
2275                (gnus-check-backend-function
2276                 'request-move-article gnus-newsgroup-name)]
2277               ["Copy article..." gnus-summary-copy-article t]
2278               ["Crosspost article..." gnus-summary-crosspost-article
2279                (gnus-check-backend-function
2280                 'request-replace-article gnus-newsgroup-name)]
2281               ["Import file..." gnus-summary-import-article
2282                (gnus-check-backend-function
2283                 'request-accept-article gnus-newsgroup-name)]
2284               ["Create article..." gnus-summary-create-article
2285                (gnus-check-backend-function
2286                 'request-accept-article gnus-newsgroup-name)]
2287               ["Check if posted" gnus-summary-article-posted-p t]
2288               ["Edit article" gnus-summary-edit-article
2289                (not (gnus-group-read-only-p))]
2290               ["Delete article" gnus-summary-delete-article
2291                (gnus-check-backend-function
2292                 'request-expire-articles gnus-newsgroup-name)]
2293               ["Query respool" gnus-summary-respool-query t]
2294               ["Trace respool" gnus-summary-respool-trace t]
2295               ["Delete expirable articles" gnus-summary-expire-articles-now
2296                (gnus-check-backend-function
2297                 'request-expire-articles gnus-newsgroup-name)])
2298              ("Extract"
2299               ["Uudecode" gnus-uu-decode-uu
2300                ,@(if (featurep 'xemacs) '(t)
2301                    '(:help "Decode uuencoded article(s)"))]
2302               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2303               ["Unshar" gnus-uu-decode-unshar t]
2304               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2305               ["Save" gnus-uu-decode-save t]
2306               ["Binhex" gnus-uu-decode-binhex t]
2307               ["Postscript" gnus-uu-decode-postscript t])
2308              ("Cache"
2309               ["Enter article" gnus-cache-enter-article t]
2310               ["Remove article" gnus-cache-remove-article t])
2311              ["Translate" gnus-article-babel t]
2312              ["Select article buffer" gnus-summary-select-article-buffer t]
2313              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2314              ["Isearch article..." gnus-summary-isearch-article t]
2315              ["Beginning of the article" gnus-summary-beginning-of-article t]
2316              ["End of the article" gnus-summary-end-of-article t]
2317              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2318              ["Fetch referenced articles" gnus-summary-refer-references t]
2319              ["Fetch current thread" gnus-summary-refer-thread t]
2320              ["Fetch article with id..." gnus-summary-refer-article t]
2321              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2322              ["Redisplay" gnus-summary-show-article t]
2323              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2324       (easy-menu-define
2325         gnus-summary-article-menu gnus-summary-mode-map ""
2326         (cons "Article" innards))
2327
2328       (if (not (keymapp gnus-summary-article-menu))
2329           (easy-menu-define
2330             gnus-article-commands-menu gnus-article-mode-map ""
2331             (cons "Commands" innards))
2332         ;; in Emacs, don't share menu.
2333         (setq gnus-article-commands-menu
2334               (copy-keymap gnus-summary-article-menu))
2335         (define-key gnus-article-mode-map [menu-bar commands]
2336           (cons "Commands" gnus-article-commands-menu))))
2337
2338     (easy-menu-define
2339       gnus-summary-thread-menu gnus-summary-mode-map ""
2340       '("Threads"
2341         ["Find all messages in thread" gnus-summary-refer-thread t]
2342         ["Toggle threading" gnus-summary-toggle-threads t]
2343         ["Hide threads" gnus-summary-hide-all-threads t]
2344         ["Show threads" gnus-summary-show-all-threads t]
2345         ["Hide thread" gnus-summary-hide-thread t]
2346         ["Show thread" gnus-summary-show-thread t]
2347         ["Go to next thread" gnus-summary-next-thread t]
2348         ["Go to previous thread" gnus-summary-prev-thread t]
2349         ["Go down thread" gnus-summary-down-thread t]
2350         ["Go up thread" gnus-summary-up-thread t]
2351         ["Top of thread" gnus-summary-top-thread t]
2352         ["Mark thread as read" gnus-summary-kill-thread t]
2353         ["Lower thread score" gnus-summary-lower-thread t]
2354         ["Raise thread score" gnus-summary-raise-thread t]
2355         ["Rethread current" gnus-summary-rethread-current t]))
2356
2357     (easy-menu-define
2358       gnus-summary-post-menu gnus-summary-mode-map ""
2359       `("Post"
2360         ["Send a message (mail or news)" gnus-summary-post-news
2361          ,@(if (featurep 'xemacs) '(t)
2362              '(:help "Compose a new message (mail or news)"))]
2363         ["Followup" gnus-summary-followup
2364          ,@(if (featurep 'xemacs) '(t)
2365              '(:help "Post followup to this article"))]
2366         ["Followup and yank" gnus-summary-followup-with-original
2367          ,@(if (featurep 'xemacs) '(t)
2368              '(:help "Post followup to this article, quoting its contents"))]
2369         ["Supersede article" gnus-summary-supersede-article t]
2370         ["Cancel article" gnus-summary-cancel-article
2371          ,@(if (featurep 'xemacs) '(t)
2372              '(:help "Cancel an article you posted"))]
2373         ["Reply" gnus-summary-reply t]
2374         ["Reply and yank" gnus-summary-reply-with-original t]
2375         ["Wide reply" gnus-summary-wide-reply t]
2376         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2377          ,@(if (featurep 'xemacs) '(t)
2378              '(:help "Mail a reply, quoting this article"))]
2379         ["Very wide reply" gnus-summary-very-wide-reply t]
2380         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2381          ,@(if (featurep 'xemacs) '(t)
2382              '(:help "Mail a very wide reply, quoting this article"))]
2383         ["Mail forward" gnus-summary-mail-forward t]
2384         ["Post forward" gnus-summary-post-forward t]
2385         ["Digest and mail" gnus-summary-digest-mail-forward t]
2386         ["Digest and post" gnus-summary-digest-post-forward t]
2387         ["Resend message" gnus-summary-resend-message t]
2388         ["Resend message edit" gnus-summary-resend-message-edit t]
2389         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2390         ["Send a mail" gnus-summary-mail-other-window t]
2391         ["Create a local message" gnus-summary-news-other-window t]
2392         ["Uuencode and post" gnus-uu-post-news
2393          ,@(if (featurep 'xemacs) '(t)
2394              '(:help "Post a uuencoded article"))]
2395         ["Followup via news" gnus-summary-followup-to-mail t]
2396         ["Followup via news and yank"
2397          gnus-summary-followup-to-mail-with-original t]
2398         ;;("Draft"
2399         ;;["Send" gnus-summary-send-draft t]
2400         ;;["Send bounced" gnus-resend-bounced-mail t])
2401         ))
2402
2403     (cond
2404      ((not (keymapp gnus-summary-post-menu))
2405       (setq gnus-article-post-menu gnus-summary-post-menu))
2406      ((not gnus-article-post-menu)
2407       ;; Don't share post menu.
2408       (setq gnus-article-post-menu
2409             (copy-keymap gnus-summary-post-menu))))
2410     (define-key gnus-article-mode-map [menu-bar post]
2411       (cons "Post" gnus-article-post-menu))
2412
2413     (easy-menu-define
2414       gnus-summary-misc-menu gnus-summary-mode-map ""
2415       `("Gnus"
2416         ("Mark Read"
2417          ["Mark as read" gnus-summary-mark-as-read-forward t]
2418          ["Mark same subject and select"
2419           gnus-summary-kill-same-subject-and-select t]
2420          ["Mark same subject" gnus-summary-kill-same-subject t]
2421          ["Catchup" gnus-summary-catchup
2422           ,@(if (featurep 'xemacs) '(t)
2423               '(:help "Mark unread articles in this group as read"))]
2424          ["Catchup all" gnus-summary-catchup-all t]
2425          ["Catchup to here" gnus-summary-catchup-to-here t]
2426          ["Catchup from here" gnus-summary-catchup-from-here t]
2427          ["Catchup region" gnus-summary-mark-region-as-read
2428           (gnus-mark-active-p)]
2429          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2430         ("Mark Various"
2431          ["Tick" gnus-summary-tick-article-forward t]
2432          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2433          ["Remove marks" gnus-summary-clear-mark-forward t]
2434          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2435          ["Set bookmark" gnus-summary-set-bookmark t]
2436          ["Remove bookmark" gnus-summary-remove-bookmark t])
2437         ("Limit to"
2438          ["Marks..." gnus-summary-limit-to-marks t]
2439          ["Subject..." gnus-summary-limit-to-subject t]
2440          ["Author..." gnus-summary-limit-to-author t]
2441          ["Age..." gnus-summary-limit-to-age t]
2442          ["Extra..." gnus-summary-limit-to-extra t]
2443          ["Score..." gnus-summary-limit-to-score t]
2444          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2445          ["Unread" gnus-summary-limit-to-unread t]
2446          ["Unseen" gnus-summary-limit-to-unseen t]
2447          ["Replied" gnus-summary-limit-to-replied t]
2448          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2449          ["Next articles" gnus-summary-limit-to-articles t]
2450          ["Pop limit" gnus-summary-pop-limit t]
2451          ["Show dormant" gnus-summary-limit-include-dormant t]
2452          ["Hide childless dormant"
2453           gnus-summary-limit-exclude-childless-dormant t]
2454          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2455          ["Hide marked" gnus-summary-limit-exclude-marks t]
2456          ["Show expunged" gnus-summary-limit-include-expunged t])
2457         ("Process Mark"
2458          ["Set mark" gnus-summary-mark-as-processable t]
2459          ["Remove mark" gnus-summary-unmark-as-processable t]
2460          ["Remove all marks" gnus-summary-unmark-all-processable t]
2461          ["Invert marks" gnus-uu-invert-processable t]
2462          ["Mark above" gnus-uu-mark-over t]
2463          ["Mark series" gnus-uu-mark-series t]
2464          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2465          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2466          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2467          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2468          ["Mark all" gnus-uu-mark-all t]
2469          ["Mark buffer" gnus-uu-mark-buffer t]
2470          ["Mark sparse" gnus-uu-mark-sparse t]
2471          ["Mark thread" gnus-uu-mark-thread t]
2472          ["Unmark thread" gnus-uu-unmark-thread t]
2473          ("Process Mark Sets"
2474           ["Kill" gnus-summary-kill-process-mark t]
2475           ["Yank" gnus-summary-yank-process-mark
2476            gnus-newsgroup-process-stack]
2477           ["Save" gnus-summary-save-process-mark t]
2478           ["Run command on marked..." gnus-summary-universal-argument t]))
2479         ("Scroll article"
2480          ["Page forward" gnus-summary-next-page
2481           ,@(if (featurep 'xemacs) '(t)
2482               '(:help "Show next page of article"))]
2483          ["Page backward" gnus-summary-prev-page
2484           ,@(if (featurep 'xemacs) '(t)
2485               '(:help "Show previous page of article"))]
2486          ["Line forward" gnus-summary-scroll-up t])
2487         ("Move"
2488          ["Next unread article" gnus-summary-next-unread-article t]
2489          ["Previous unread article" gnus-summary-prev-unread-article t]
2490          ["Next article" gnus-summary-next-article t]
2491          ["Previous article" gnus-summary-prev-article t]
2492          ["Next unread subject" gnus-summary-next-unread-subject t]
2493          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2494          ["Next article same subject" gnus-summary-next-same-subject t]
2495          ["Previous article same subject" gnus-summary-prev-same-subject t]
2496          ["First unread article" gnus-summary-first-unread-article t]
2497          ["Best unread article" gnus-summary-best-unread-article t]
2498          ["Go to subject number..." gnus-summary-goto-subject t]
2499          ["Go to article number..." gnus-summary-goto-article t]
2500          ["Go to the last article" gnus-summary-goto-last-article t]
2501          ["Pop article off history" gnus-summary-pop-article t])
2502         ("Sort"
2503          ["Sort by number" gnus-summary-sort-by-number t]
2504          ["Sort by author" gnus-summary-sort-by-author t]
2505          ["Sort by subject" gnus-summary-sort-by-subject t]
2506          ["Sort by date" gnus-summary-sort-by-date t]
2507          ["Sort by score" gnus-summary-sort-by-score t]
2508          ["Sort by lines" gnus-summary-sort-by-lines t]
2509          ["Sort by characters" gnus-summary-sort-by-chars t]
2510          ["Randomize" gnus-summary-sort-by-random t]
2511          ["Original sort" gnus-summary-sort-by-original t])
2512         ("Help"
2513          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2514          ["Describe group" gnus-summary-describe-group t]
2515          ["Fetch charter" gnus-group-fetch-charter
2516           ,@(if (featurep 'xemacs) nil
2517               '(:help "Display the charter of the current group"))]
2518          ["Fetch control message" gnus-group-fetch-control
2519           ,@(if (featurep 'xemacs) nil
2520               '(:help "Display the archived control message for the current group"))]
2521          ["Read manual" gnus-info-find-node t])
2522         ("Modes"
2523          ["Pick and read" gnus-pick-mode t]
2524          ["Binary" gnus-binary-mode t])
2525         ("Regeneration"
2526          ["Regenerate" gnus-summary-prepare t]
2527          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2528          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2529          ["Toggle threading" gnus-summary-toggle-threads t])
2530         ["See old articles" gnus-summary-insert-old-articles t]
2531         ["See new articles" gnus-summary-insert-new-articles t]
2532         ["Filter articles..." gnus-summary-execute-command t]
2533         ["Run command on articles..." gnus-summary-universal-argument t]
2534         ["Search articles forward..." gnus-summary-search-article-forward t]
2535         ["Search articles backward..." gnus-summary-search-article-backward t]
2536         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2537         ["Expand window" gnus-summary-expand-window t]
2538         ["Expire expirable articles" gnus-summary-expire-articles
2539          (gnus-check-backend-function
2540           'request-expire-articles gnus-newsgroup-name)]
2541         ["Edit local kill file" gnus-summary-edit-local-kill t]
2542         ["Edit main kill file" gnus-summary-edit-global-kill t]
2543         ["Edit group parameters" gnus-summary-edit-parameters t]
2544         ["Customize group parameters" gnus-summary-customize-parameters t]
2545         ["Send a bug report" gnus-bug t]
2546         ("Exit"
2547          ["Catchup and exit" gnus-summary-catchup-and-exit
2548           ,@(if (featurep 'xemacs) '(t)
2549               '(:help "Mark unread articles in this group as read, then exit"))]
2550          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2551          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2552          ["Exit group" gnus-summary-exit
2553           ,@(if (featurep 'xemacs) '(t)
2554               '(:help "Exit current group, return to group selection mode"))]
2555          ["Exit group without updating" gnus-summary-exit-no-update t]
2556          ["Exit and goto next group" gnus-summary-next-group t]
2557          ["Exit and goto prev group" gnus-summary-prev-group t]
2558          ["Reselect group" gnus-summary-reselect-current-group t]
2559          ["Rescan group" gnus-summary-rescan-group t]
2560          ["Update dribble" gnus-summary-save-newsrc t])))
2561
2562     (gnus-run-hooks 'gnus-summary-menu-hook)))
2563
2564 (defvar gnus-summary-tool-bar-map nil)
2565
2566 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2567 (defun gnus-summary-make-tool-bar ()
2568   (if (and (fboundp 'tool-bar-add-item-from-menu)
2569            (default-value 'tool-bar-mode)
2570            (not gnus-summary-tool-bar-map))
2571       (setq gnus-summary-tool-bar-map
2572             (let ((tool-bar-map (make-sparse-keymap))
2573                   (load-path (mm-image-load-path)))
2574               (tool-bar-add-item-from-menu
2575                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2576               (tool-bar-add-item-from-menu
2577                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2578               (tool-bar-add-item-from-menu
2579                'gnus-summary-post-news "post" gnus-summary-mode-map)
2580               (tool-bar-add-item-from-menu
2581                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2582               (tool-bar-add-item-from-menu
2583                'gnus-summary-followup "followup" gnus-summary-mode-map)
2584               (tool-bar-add-item-from-menu
2585                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2586               (tool-bar-add-item-from-menu
2587                'gnus-summary-reply "reply" gnus-summary-mode-map)
2588               (tool-bar-add-item-from-menu
2589                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2590               (tool-bar-add-item-from-menu
2591                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2592               (tool-bar-add-item-from-menu
2593                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2594               (tool-bar-add-item-from-menu
2595                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2596               (tool-bar-add-item-from-menu
2597                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2598               (tool-bar-add-item-from-menu
2599                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2600               (tool-bar-add-item-from-menu
2601                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2602               (tool-bar-add-item-from-menu
2603                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2604               (tool-bar-add-item-from-menu
2605                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2606               tool-bar-map)))
2607   (if gnus-summary-tool-bar-map
2608       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2609
2610 (defun gnus-score-set-default (var value)
2611   "A version of set that updates the GNU Emacs menu-bar."
2612   (set var value)
2613   ;; It is the message that forces the active status to be updated.
2614   (message ""))
2615
2616 (defun gnus-make-score-map (type)
2617   "Make a summary score map of type TYPE."
2618   (if t
2619       nil
2620     (let ((headers '(("author" "from" string)
2621                      ("subject" "subject" string)
2622                      ("article body" "body" string)
2623                      ("article head" "head" string)
2624                      ("xref" "xref" string)
2625                      ("extra header" "extra" string)
2626                      ("lines" "lines" number)
2627                      ("followups to author" "followup" string)))
2628           (types '((number ("less than" <)
2629                            ("greater than" >)
2630                            ("equal" =))
2631                    (string ("substring" s)
2632                            ("exact string" e)
2633                            ("fuzzy string" f)
2634                            ("regexp" r))))
2635           (perms '(("temporary" (current-time-string))
2636                    ("permanent" nil)
2637                    ("immediate" now)))
2638           header)
2639       (list
2640        (apply
2641         'nconc
2642         (list
2643          (if (eq type 'lower)
2644              "Lower score"
2645            "Increase score"))
2646         (let (outh)
2647           (while headers
2648             (setq header (car headers))
2649             (setq outh
2650                   (cons
2651                    (apply
2652                     'nconc
2653                     (list (car header))
2654                     (let ((ts (cdr (assoc (nth 2 header) types)))
2655                           outt)
2656                       (while ts
2657                         (setq outt
2658                               (cons
2659                                (apply
2660                                 'nconc
2661                                 (list (caar ts))
2662                                 (let ((ps perms)
2663                                       outp)
2664                                   (while ps
2665                                     (setq outp
2666                                           (cons
2667                                            (vector
2668                                             (caar ps)
2669                                             (list
2670                                              'gnus-summary-score-entry
2671                                              (nth 1 header)
2672                                              (if (or (string= (nth 1 header)
2673                                                               "head")
2674                                                      (string= (nth 1 header)
2675                                                               "body"))
2676                                                  ""
2677                                                (list 'gnus-summary-header
2678                                                      (nth 1 header)))
2679                                              (list 'quote (nth 1 (car ts)))
2680                                              (list 'gnus-score-delta-default
2681                                                    nil)
2682                                              (nth 1 (car ps))
2683                                              t)
2684                                             t)
2685                                            outp))
2686                                     (setq ps (cdr ps)))
2687                                   (list (nreverse outp))))
2688                                outt))
2689                         (setq ts (cdr ts)))
2690                       (list (nreverse outt))))
2691                    outh))
2692             (setq headers (cdr headers)))
2693           (list (nreverse outh))))))))
2694
2695 \f
2696
2697 (defun gnus-summary-mode (&optional group)
2698   "Major mode for reading articles.
2699
2700 All normal editing commands are switched off.
2701 \\<gnus-summary-mode-map>
2702 Each line in this buffer represents one article.  To read an
2703 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2704 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2705 respectively.
2706
2707 You can also post articles and send mail from this buffer.  To
2708 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2709 of an article, type `\\[gnus-summary-reply]'.
2710
2711 There are approx. one gazillion commands you can execute in this
2712 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2713
2714 The following commands are available:
2715
2716 \\{gnus-summary-mode-map}"
2717   (interactive)
2718   (kill-all-local-variables)
2719   (when (gnus-visual-p 'summary-menu 'menu)
2720     (gnus-summary-make-menu-bar)
2721     (gnus-summary-make-tool-bar))
2722   (gnus-summary-make-local-variables)
2723   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2724     (gnus-summary-make-local-variables))
2725   (gnus-make-thread-indent-array)
2726   (gnus-simplify-mode-line)
2727   (setq major-mode 'gnus-summary-mode)
2728   (setq mode-name "Summary")
2729   (make-local-variable 'minor-mode-alist)
2730   (use-local-map gnus-summary-mode-map)
2731   (buffer-disable-undo)
2732   (setq buffer-read-only t              ;Disable modification
2733         show-trailing-whitespace nil)
2734   (setq truncate-lines t)
2735   (setq selective-display t)
2736   (setq selective-display-ellipses t)   ;Display `...'
2737   (gnus-summary-set-display-table)
2738   (gnus-set-default-directory)
2739   (setq gnus-newsgroup-name group)
2740   (unless (gnus-news-group-p group)
2741     (setq gnus-newsgroup-incorporated
2742           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2743   (make-local-variable 'gnus-summary-line-format)
2744   (make-local-variable 'gnus-summary-line-format-spec)
2745   (make-local-variable 'gnus-summary-dummy-line-format)
2746   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2747   (make-local-variable 'gnus-summary-mark-positions)
2748   (gnus-make-local-hook 'pre-command-hook)
2749   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2750   (gnus-run-hooks 'gnus-summary-mode-hook)
2751   (turn-on-gnus-mailing-list-mode)
2752   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2753   (gnus-update-summary-mark-positions))
2754
2755 (defun gnus-summary-make-local-variables ()
2756   "Make all the local summary buffer variables."
2757   (let (global)
2758     (dolist (local gnus-summary-local-variables)
2759       (if (consp local)
2760           (progn
2761             (if (eq (cdr local) 'global)
2762                 ;; Copy the global value of the variable.
2763                 (setq global (symbol-value (car local)))
2764               ;; Use the value from the list.
2765               (setq global (eval (cdr local))))
2766             (set (make-local-variable (car local)) global))
2767         ;; Simple nil-valued local variable.
2768         (set (make-local-variable local) nil)))))
2769
2770 (defun gnus-summary-clear-local-variables ()
2771   (let ((locals gnus-summary-local-variables))
2772     (while locals
2773       (if (consp (car locals))
2774           (and (vectorp (caar locals))
2775                (set (caar locals) nil))
2776         (and (vectorp (car locals))
2777              (set (car locals) nil)))
2778       (setq locals (cdr locals)))))
2779
2780 ;; Summary data functions.
2781
2782 (defmacro gnus-data-number (data)
2783   `(car ,data))
2784
2785 (defmacro gnus-data-set-number (data number)
2786   `(setcar ,data ,number))
2787
2788 (defmacro gnus-data-mark (data)
2789   `(nth 1 ,data))
2790
2791 (defmacro gnus-data-set-mark (data mark)
2792   `(setcar (nthcdr 1 ,data) ,mark))
2793
2794 (defmacro gnus-data-pos (data)
2795   `(nth 2 ,data))
2796
2797 (defmacro gnus-data-set-pos (data pos)
2798   `(setcar (nthcdr 2 ,data) ,pos))
2799
2800 (defmacro gnus-data-header (data)
2801   `(nth 3 ,data))
2802
2803 (defmacro gnus-data-set-header (data header)
2804   `(setcar (nthcdr 3 ,data) ,header))
2805
2806 (defmacro gnus-data-level (data)
2807   `(nth 4 ,data))
2808
2809 (defmacro gnus-data-unread-p (data)
2810   `(= (nth 1 ,data) gnus-unread-mark))
2811
2812 (defmacro gnus-data-read-p (data)
2813   `(/= (nth 1 ,data) gnus-unread-mark))
2814
2815 (defmacro gnus-data-pseudo-p (data)
2816   `(consp (nth 3 ,data)))
2817
2818 (defmacro gnus-data-find (number)
2819   `(assq ,number gnus-newsgroup-data))
2820
2821 (defmacro gnus-data-find-list (number &optional data)
2822   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2823      (memq (assq ,number bdata)
2824            bdata)))
2825
2826 (defmacro gnus-data-make (number mark pos header level)
2827   `(list ,number ,mark ,pos ,header ,level))
2828
2829 (defun gnus-data-enter (after-article number mark pos header level offset)
2830   (let ((data (gnus-data-find-list after-article)))
2831     (unless data
2832       (error "No such article: %d" after-article))
2833     (setcdr data (cons (gnus-data-make number mark pos header level)
2834                        (cdr data)))
2835     (setq gnus-newsgroup-data-reverse nil)
2836     (gnus-data-update-list (cddr data) offset)))
2837
2838 (defun gnus-data-enter-list (after-article list &optional offset)
2839   (when list
2840     (let ((data (and after-article (gnus-data-find-list after-article)))
2841           (ilist list))
2842       (if (not (or data
2843                    after-article))
2844           (let ((odata gnus-newsgroup-data))
2845             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2846             (when offset
2847               (gnus-data-update-list odata offset)))
2848         ;; Find the last element in the list to be spliced into the main
2849         ;; list.
2850         (while (cdr list)
2851           (setq list (cdr list)))
2852         (if (not data)
2853             (progn
2854               (setcdr list gnus-newsgroup-data)
2855               (setq gnus-newsgroup-data ilist)
2856               (when offset
2857                 (gnus-data-update-list (cdr list) offset)))
2858           (setcdr list (cdr data))
2859           (setcdr data ilist)
2860           (when offset
2861             (gnus-data-update-list (cdr list) offset))))
2862       (setq gnus-newsgroup-data-reverse nil))))
2863
2864 (defun gnus-data-remove (article &optional offset)
2865   (let ((data gnus-newsgroup-data))
2866     (if (= (gnus-data-number (car data)) article)
2867         (progn
2868           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2869                 gnus-newsgroup-data-reverse nil)
2870           (when offset
2871             (gnus-data-update-list gnus-newsgroup-data offset)))
2872       (while (cdr data)
2873         (when (= (gnus-data-number (cadr data)) article)
2874           (setcdr data (cddr data))
2875           (when offset
2876             (gnus-data-update-list (cdr data) offset))
2877           (setq data nil
2878                 gnus-newsgroup-data-reverse nil))
2879         (setq data (cdr data))))))
2880
2881 (defmacro gnus-data-list (backward)
2882   `(if ,backward
2883        (or gnus-newsgroup-data-reverse
2884            (setq gnus-newsgroup-data-reverse
2885                  (reverse gnus-newsgroup-data)))
2886      gnus-newsgroup-data))
2887
2888 (defun gnus-data-update-list (data offset)
2889   "Add OFFSET to the POS of all data entries in DATA."
2890   (setq gnus-newsgroup-data-reverse nil)
2891   (while data
2892     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2893     (setq data (cdr data))))
2894
2895 (defun gnus-summary-article-pseudo-p (article)
2896   "Say whether this article is a pseudo article or not."
2897   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2898
2899 (defmacro gnus-summary-article-sparse-p (article)
2900   "Say whether this article is a sparse article or not."
2901   `(memq ,article gnus-newsgroup-sparse))
2902
2903 (defmacro gnus-summary-article-ancient-p (article)
2904   "Say whether this article is a sparse article or not."
2905   `(memq ,article gnus-newsgroup-ancient))
2906
2907 (defun gnus-article-parent-p (number)
2908   "Say whether this article is a parent or not."
2909   (let ((data (gnus-data-find-list number)))
2910     (and (cdr data)                     ; There has to be an article after...
2911          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2912             (gnus-data-level (nth 1 data))))))
2913
2914 (defun gnus-article-children (number)
2915   "Return a list of all children to NUMBER."
2916   (let* ((data (gnus-data-find-list number))
2917          (level (gnus-data-level (car data)))
2918          children)
2919     (setq data (cdr data))
2920     (while (and data
2921                 (= (gnus-data-level (car data)) (1+ level)))
2922       (push (gnus-data-number (car data)) children)
2923       (setq data (cdr data)))
2924     children))
2925
2926 (defmacro gnus-summary-skip-intangible ()
2927   "If the current article is intangible, then jump to a different article."
2928   '(let ((to (get-text-property (point) 'gnus-intangible)))
2929      (and to (gnus-summary-goto-subject to))))
2930
2931 (defmacro gnus-summary-article-intangible-p ()
2932   "Say whether this article is intangible or not."
2933   '(get-text-property (point) 'gnus-intangible))
2934
2935 (defun gnus-article-read-p (article)
2936   "Say whether ARTICLE is read or not."
2937   (not (or (memq article gnus-newsgroup-marked)
2938            (memq article gnus-newsgroup-spam-marked)
2939            (memq article gnus-newsgroup-unreads)
2940            (memq article gnus-newsgroup-unselected)
2941            (memq article gnus-newsgroup-dormant))))
2942
2943 ;; Some summary mode macros.
2944
2945 (defmacro gnus-summary-article-number ()
2946   "The article number of the article on the current line.
2947 If there isn't an article number here, then we return the current
2948 article number."
2949   '(progn
2950      (gnus-summary-skip-intangible)
2951      (or (get-text-property (point) 'gnus-number)
2952          (gnus-summary-last-subject))))
2953
2954 (defmacro gnus-summary-article-header (&optional number)
2955   "Return the header of article NUMBER."
2956   `(gnus-data-header (gnus-data-find
2957                       ,(or number '(gnus-summary-article-number)))))
2958
2959 (defmacro gnus-summary-thread-level (&optional number)
2960   "Return the level of thread that starts with article NUMBER."
2961   `(if (and (eq gnus-summary-make-false-root 'dummy)
2962             (get-text-property (point) 'gnus-intangible))
2963        0
2964      (gnus-data-level (gnus-data-find
2965                        ,(or number '(gnus-summary-article-number))))))
2966
2967 (defmacro gnus-summary-article-mark (&optional number)
2968   "Return the mark of article NUMBER."
2969   `(gnus-data-mark (gnus-data-find
2970                     ,(or number '(gnus-summary-article-number)))))
2971
2972 (defmacro gnus-summary-article-pos (&optional number)
2973   "Return the position of the line of article NUMBER."
2974   `(gnus-data-pos (gnus-data-find
2975                    ,(or number '(gnus-summary-article-number)))))
2976
2977 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2978 (defmacro gnus-summary-article-subject (&optional number)
2979   "Return current subject string or nil if nothing."
2980   `(let ((headers
2981           ,(if number
2982                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2983              '(gnus-data-header (assq (gnus-summary-article-number)
2984                                       gnus-newsgroup-data)))))
2985      (and headers
2986           (vectorp headers)
2987           (mail-header-subject headers))))
2988
2989 (defmacro gnus-summary-article-score (&optional number)
2990   "Return current article score."
2991   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2992                   gnus-newsgroup-scored))
2993        gnus-summary-default-score 0))
2994
2995 (defun gnus-summary-article-children (&optional number)
2996   "Return a list of article numbers that are children of article NUMBER."
2997   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2998          (level (gnus-data-level (car data)))
2999          l children)
3000     (while (and (setq data (cdr data))
3001                 (> (setq l (gnus-data-level (car data))) level))
3002       (and (= (1+ level) l)
3003            (push (gnus-data-number (car data))
3004                  children)))
3005     (nreverse children)))
3006
3007 (defun gnus-summary-article-parent (&optional number)
3008   "Return the article number of the parent of article NUMBER."
3009   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3010                                     (gnus-data-list t)))
3011          (level (gnus-data-level (car data))))
3012     (if (zerop level)
3013         ()                              ; This is a root.
3014       ;; We search until we find an article with a level less than
3015       ;; this one.  That function has to be the parent.
3016       (while (and (setq data (cdr data))
3017                   (not (< (gnus-data-level (car data)) level))))
3018       (and data (gnus-data-number (car data))))))
3019
3020 (defun gnus-unread-mark-p (mark)
3021   "Say whether MARK is the unread mark."
3022   (= mark gnus-unread-mark))
3023
3024 (defun gnus-read-mark-p (mark)
3025   "Say whether MARK is one of the marks that mark as read.
3026 This is all marks except unread, ticked, dormant, and expirable."
3027   (not (or (= mark gnus-unread-mark)
3028            (= mark gnus-ticked-mark)
3029            (= mark gnus-spam-mark)
3030            (= mark gnus-dormant-mark)
3031            (= mark gnus-expirable-mark))))
3032
3033 (defmacro gnus-article-mark (number)
3034   "Return the MARK of article NUMBER.
3035 This macro should only be used when computing the mark the \"first\"
3036 time; i.e., when generating the summary lines.  After that,
3037 `gnus-summary-article-mark' should be used to examine the
3038 marks of articles."
3039   `(cond
3040     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3041     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3042     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3043     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3044     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3045     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3046     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3047     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3048            gnus-ancient-mark))))
3049
3050 ;; Saving hidden threads.
3051
3052 (defmacro gnus-save-hidden-threads (&rest forms)
3053   "Save hidden threads, eval FORMS, and restore the hidden threads."
3054   (let ((config (make-symbol "config")))
3055     `(let ((,config (gnus-hidden-threads-configuration)))
3056        (unwind-protect
3057            (save-excursion
3058              ,@forms)
3059          (gnus-restore-hidden-threads-configuration ,config)))))
3060 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3061 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3062
3063 (defun gnus-data-compute-positions ()
3064   "Compute the positions of all articles."
3065   (setq gnus-newsgroup-data-reverse nil)
3066   (let ((data gnus-newsgroup-data))
3067     (save-excursion
3068       (gnus-save-hidden-threads
3069         (gnus-summary-show-all-threads)
3070         (goto-char (point-min))
3071         (while data
3072           (while (get-text-property (point) 'gnus-intangible)
3073             (forward-line 1))
3074           (gnus-data-set-pos (car data) (+ (point) 3))
3075           (setq data (cdr data))
3076           (forward-line 1))))))
3077
3078 (defun gnus-hidden-threads-configuration ()
3079   "Return the current hidden threads configuration."
3080   (save-excursion
3081     (let (config)
3082       (goto-char (point-min))
3083       (while (search-forward "\r" nil t)
3084         (push (1- (point)) config))
3085       config)))
3086
3087 (defun gnus-restore-hidden-threads-configuration (config)
3088   "Restore hidden threads configuration from CONFIG."
3089   (save-excursion
3090     (let (point buffer-read-only)
3091       (while (setq point (pop config))
3092         (when (and (< point (point-max))
3093                    (goto-char point)
3094                    (eq (char-after) ?\n))
3095           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3096
3097 ;; Various summary mode internalish functions.
3098
3099 (defun gnus-mouse-pick-article (e)
3100   (interactive "e")
3101   (mouse-set-point e)
3102   (gnus-summary-next-page nil t))
3103
3104 (defun gnus-summary-set-display-table ()
3105   "Change the display table.
3106 Odd characters have a tendency to mess
3107 up nicely formatted displays - we make all possible glyphs
3108 display only a single character."
3109
3110   ;; We start from the standard display table, if any.
3111   (let ((table (or (copy-sequence standard-display-table)
3112                    (make-display-table)))
3113         (i 32))
3114     ;; Nix out all the control chars...
3115     (while (>= (setq i (1- i)) 0)
3116       (aset table i [??]))
3117     ;; ... but not newline and cr, of course.  (cr is necessary for the
3118     ;; selective display).
3119     (aset table ?\n nil)
3120     (aset table ?\r nil)
3121     ;; We keep TAB as well.
3122     (aset table ?\t nil)
3123     ;; We nix out any glyphs over 126 that are not set already.
3124     (let ((i 256))
3125       (while (>= (setq i (1- i)) 127)
3126         ;; Only modify if the entry is nil.
3127         (unless (aref table i)
3128           (aset table i [??]))))
3129     (setq buffer-display-table table)))
3130
3131 (defun gnus-summary-set-article-display-arrow (pos)
3132   "Update the overlay arrow to point to line at position POS."
3133   (when (and gnus-summary-display-arrow
3134              (boundp 'overlay-arrow-position)
3135              (boundp 'overlay-arrow-string))
3136     (save-excursion
3137       (goto-char pos)
3138       (beginning-of-line)
3139       (unless overlay-arrow-position
3140         (setq overlay-arrow-position (make-marker)))
3141       (setq overlay-arrow-string "=>"
3142             overlay-arrow-position (set-marker overlay-arrow-position
3143                                                (point)
3144                                                (current-buffer))))))
3145
3146 (defun gnus-summary-setup-buffer (group)
3147   "Initialize summary buffer."
3148   (let ((buffer (gnus-summary-buffer-name group))
3149         (dead-name (concat "*Dead Summary "
3150                            (gnus-group-decoded-name group) "*")))
3151     ;; If a dead summary buffer exists, we kill it.
3152     (when (gnus-buffer-live-p dead-name)
3153       (gnus-kill-buffer dead-name))
3154     (if (get-buffer buffer)
3155         (progn
3156           (set-buffer buffer)
3157           (setq gnus-summary-buffer (current-buffer))
3158           (not gnus-newsgroup-prepared))
3159       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3160       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3161       (gnus-summary-mode group)
3162       (when gnus-carpal
3163         (gnus-carpal-setup-buffer 'summary))
3164       (unless gnus-single-article-buffer
3165         (make-local-variable 'gnus-article-buffer)
3166         (make-local-variable 'gnus-article-current)
3167         (make-local-variable 'gnus-original-article-buffer))
3168       (setq gnus-newsgroup-name group)
3169       ;; Set any local variables in the group parameters.
3170       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3171       t)))
3172
3173 (defun gnus-set-global-variables ()
3174   "Set the global equivalents of the buffer-local variables.
3175 They are set to the latest values they had.  These reflect the summary
3176 buffer that was in action when the last article was fetched."
3177   (when (eq major-mode 'gnus-summary-mode)
3178     (setq gnus-summary-buffer (current-buffer))
3179     (let ((name gnus-newsgroup-name)
3180           (marked gnus-newsgroup-marked)
3181           (spam gnus-newsgroup-spam-marked)
3182           (unread gnus-newsgroup-unreads)
3183           (headers gnus-current-headers)
3184           (data gnus-newsgroup-data)
3185           (summary gnus-summary-buffer)
3186           (article-buffer gnus-article-buffer)
3187           (original gnus-original-article-buffer)
3188           (gac gnus-article-current)
3189           (reffed gnus-reffed-article-number)
3190           (score-file gnus-current-score-file)
3191           (default-charset gnus-newsgroup-charset)
3192           vlist)
3193       (let ((locals gnus-newsgroup-variables))
3194         (while locals
3195           (if (consp (car locals))
3196               (push (eval (caar locals)) vlist)
3197             (push (eval (car locals)) vlist))
3198           (setq locals (cdr locals)))
3199         (setq vlist (nreverse vlist)))
3200       (with-current-buffer gnus-group-buffer
3201         (setq gnus-newsgroup-name name
3202               gnus-newsgroup-marked marked
3203               gnus-newsgroup-spam-marked spam
3204               gnus-newsgroup-unreads unread
3205               gnus-current-headers headers
3206               gnus-newsgroup-data data
3207               gnus-article-current gac
3208               gnus-summary-buffer summary
3209               gnus-article-buffer article-buffer
3210               gnus-original-article-buffer original
3211               gnus-reffed-article-number reffed
3212               gnus-current-score-file score-file
3213               gnus-newsgroup-charset default-charset)
3214         (let ((locals gnus-newsgroup-variables))
3215           (while locals
3216             (if (consp (car locals))
3217                 (set (caar locals) (pop vlist))
3218               (set (car locals) (pop vlist)))
3219             (setq locals (cdr locals))))
3220         ;; The article buffer also has local variables.
3221         (when (gnus-buffer-live-p gnus-article-buffer)
3222           (set-buffer gnus-article-buffer)
3223           (setq gnus-summary-buffer summary))))))
3224
3225 (defun gnus-summary-article-unread-p (article)
3226   "Say whether ARTICLE is unread or not."
3227   (memq article gnus-newsgroup-unreads))
3228
3229 (defun gnus-summary-first-article-p (&optional article)
3230   "Return whether ARTICLE is the first article in the buffer."
3231   (if (not (setq article (or article (gnus-summary-article-number))))
3232       nil
3233     (eq article (caar gnus-newsgroup-data))))
3234
3235 (defun gnus-summary-last-article-p (&optional article)
3236   "Return whether ARTICLE is the last article in the buffer."
3237   (if (not (setq article (or article (gnus-summary-article-number))))
3238       ;; All non-existent numbers are the last article.  :-)
3239       t
3240     (not (cdr (gnus-data-find-list article)))))
3241
3242 (defun gnus-make-thread-indent-array ()
3243   (let ((n 200))
3244     (unless (and gnus-thread-indent-array
3245                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3246       (setq gnus-thread-indent-array (make-vector 201 "")
3247             gnus-thread-indent-array-level gnus-thread-indent-level)
3248       (while (>= n 0)
3249         (aset gnus-thread-indent-array n
3250               (make-string (* n gnus-thread-indent-level) ? ))
3251         (setq n (1- n))))))
3252
3253 (defun gnus-update-summary-mark-positions ()
3254   "Compute where the summary marks are to go."
3255   (save-excursion
3256     (when (gnus-buffer-exists-p gnus-summary-buffer)
3257       (set-buffer gnus-summary-buffer))
3258     (let ((gnus-replied-mark 129)
3259           (gnus-score-below-mark 130)
3260           (gnus-score-over-mark 130)
3261           (gnus-undownloaded-mark 131)
3262           (spec gnus-summary-line-format-spec)
3263           gnus-visual pos)
3264       (save-excursion
3265         (gnus-set-work-buffer)
3266         (let ((gnus-summary-line-format-spec spec)
3267               (gnus-newsgroup-downloadable '(0)))
3268           (gnus-summary-insert-line
3269            (make-full-mail-header 0 "" "nobody"
3270                                   "05 Apr 2001 23:33:09 +0400"
3271                                   "" "" 0 0 "" nil)
3272            0 nil t 128 t nil "" nil 1)
3273           (goto-char (point-min))
3274           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3275                                              (- (point) (point-min) 1)))))
3276           (goto-char (point-min))
3277           (push (cons 'replied (and (search-forward "\201" nil t)
3278                                     (- (point) (point-min) 1)))
3279                 pos)
3280           (goto-char (point-min))
3281           (push (cons 'score (and (search-forward "\202" nil t)
3282                                   (- (point) (point-min) 1)))
3283                 pos)
3284           (goto-char (point-min))
3285           (push (cons 'download
3286                       (and (search-forward "\203" nil t)
3287                            (- (point) (point-min) 1)))
3288                 pos)))
3289       (setq gnus-summary-mark-positions pos))))
3290
3291 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3292   "Insert a dummy root in the summary buffer."
3293   (beginning-of-line)
3294   (gnus-add-text-properties
3295    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3296    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3297
3298 (defun gnus-summary-extract-address-component (from)
3299   (or (car (funcall gnus-extract-address-components from))
3300       from))
3301
3302 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3303   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3304                                 default-mime-charset)))
3305     ;; Is it really necessary to do this next part for each summary line?
3306     ;; Luckily, doesn't seem to slow things down much.
3307     (or
3308      (and gnus-ignored-from-addresses
3309           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3310           (let ((extra-headers (mail-header-extra header))
3311                 to
3312                 newsgroups)
3313             (cond
3314              ((setq to (cdr (assq 'To extra-headers)))
3315               (concat "-> "
3316                       (inline
3317                         (gnus-summary-extract-address-component
3318                          (funcall gnus-decode-encoded-word-function to)))))
3319              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3320               (concat "=> " newsgroups)))))
3321      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3322
3323 (defun gnus-summary-insert-line (gnus-tmp-header
3324                                  gnus-tmp-level gnus-tmp-current
3325                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3326                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3327                                  &optional gnus-tmp-dummy gnus-tmp-score
3328                                  gnus-tmp-process)
3329   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3330          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3331          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3332          (gnus-tmp-score-char
3333           (if (or (null gnus-summary-default-score)
3334                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3335                       gnus-summary-zcore-fuzz))
3336               ?\ ;;;Whitespace
3337             (if (< gnus-tmp-score gnus-summary-default-score)
3338                 gnus-score-below-mark gnus-score-over-mark)))
3339          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3340          (gnus-tmp-replied
3341           (cond (gnus-tmp-process gnus-process-mark)
3342                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3343                  gnus-cached-mark)
3344                 (gnus-tmp-replied gnus-replied-mark)
3345                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3346                  gnus-forwarded-mark)
3347                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3348                  gnus-saved-mark)
3349                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3350                  gnus-recent-mark)
3351                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3352                  gnus-unseen-mark)
3353                 (t gnus-no-mark)))
3354          (gnus-tmp-downloaded
3355           (cond (undownloaded
3356                  gnus-undownloaded-mark)
3357                 (gnus-newsgroup-agentized
3358                  gnus-downloaded-mark)
3359                 (t
3360                  gnus-no-mark)))
3361          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3362          (gnus-tmp-name
3363           (cond
3364            ((string-match "<[^>]+> *$" gnus-tmp-from)
3365             (let ((beg (match-beginning 0)))
3366               (or (and (string-match "^\".+\"" gnus-tmp-from)
3367                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3368                   (substring gnus-tmp-from 0 beg))))
3369            ((string-match "(.+)" gnus-tmp-from)
3370             (substring gnus-tmp-from
3371                        (1+ (match-beginning 0)) (1- (match-end 0))))
3372            (t gnus-tmp-from)))
3373          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3374          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3375          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3376          (buffer-read-only nil))
3377     (when (string= gnus-tmp-name "")
3378       (setq gnus-tmp-name gnus-tmp-from))
3379     (unless (numberp gnus-tmp-lines)
3380       (setq gnus-tmp-lines -1))
3381     (if (= gnus-tmp-lines -1)
3382         (setq gnus-tmp-lines "?")
3383       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3384     (gnus-put-text-property-excluding-characters-with-faces
3385      (point)
3386      (progn (eval gnus-summary-line-format-spec) (point))
3387      'gnus-number gnus-tmp-number)
3388     (when (gnus-visual-p 'summary-highlight 'highlight)
3389       (forward-line -1)
3390       (gnus-run-hooks 'gnus-summary-update-hook)
3391       (forward-line 1))))
3392
3393 (defun gnus-summary-update-line (&optional dont-update)
3394   "Update summary line after change."
3395   (when (and gnus-summary-default-score
3396              (not gnus-summary-inhibit-highlight))
3397     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3398            (article (gnus-summary-article-number))
3399            (score (gnus-summary-article-score article)))
3400       (unless dont-update
3401         (if (and gnus-summary-mark-below
3402                  (< (gnus-summary-article-score)
3403                     gnus-summary-mark-below))
3404             ;; This article has a low score, so we mark it as read.
3405             (when (memq article gnus-newsgroup-unreads)
3406               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3407           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3408             ;; This article was previously marked as read on account
3409             ;; of a low score, but now it has risen, so we mark it as
3410             ;; unread.
3411             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3412         (gnus-summary-update-mark
3413          (if (or (null gnus-summary-default-score)
3414                  (<= (abs (- score gnus-summary-default-score))
3415                      gnus-summary-zcore-fuzz))
3416              ?\ ;;;Whitespace
3417            (if (< score gnus-summary-default-score)
3418                gnus-score-below-mark gnus-score-over-mark))
3419          'score))
3420       ;; Do visual highlighting.
3421       (when (gnus-visual-p 'summary-highlight 'highlight)
3422         (gnus-run-hooks 'gnus-summary-update-hook)))))
3423
3424 (defvar gnus-tmp-new-adopts nil)
3425
3426 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3427   "Return the number of articles in THREAD.
3428 This may be 0 in some cases -- if none of the articles in
3429 the thread are to be displayed."
3430   (let* ((number
3431           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3432           (cond
3433            ((not (listp thread))
3434             1)
3435            ((and (consp thread) (cdr thread))
3436             (apply
3437              '+ 1 (mapcar
3438                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3439            ((null thread)
3440             1)
3441            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3442             1)
3443            (t 0))))
3444     (when (and level (zerop level) gnus-tmp-new-adopts)
3445       (incf number
3446             (apply '+ (mapcar
3447                        'gnus-summary-number-of-articles-in-thread
3448                        gnus-tmp-new-adopts))))
3449     (if char
3450         (if (> number 1) gnus-not-empty-thread-mark
3451           gnus-empty-thread-mark)
3452       number)))
3453
3454 (defsubst gnus-summary-line-message-size (head)
3455   "Return pretty-printed version of message size.
3456 This function is intended to be used in
3457 `gnus-summary-line-format-alist'."
3458   (let ((c (or (mail-header-chars head) -1)))
3459     (cond ((< c 0) "n/a")               ; chars not available
3460           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3461           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3462           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3463           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3464
3465
3466 (defun gnus-summary-set-local-parameters (group)
3467   "Go through the local params of GROUP and set all variable specs in that list."
3468   (let ((params (gnus-group-find-parameter group))
3469         (vars '(quit-config))           ; Ignore quit-config.
3470         elem)
3471     (while params
3472       (setq elem (car params)
3473             params (cdr params))
3474       (and (consp elem)                 ; Has to be a cons.
3475            (consp (cdr elem))           ; The cdr has to be a list.
3476            (symbolp (car elem))         ; Has to be a symbol in there.
3477            (not (memq (car elem) vars))
3478            (ignore-errors               ; So we set it.
3479              (push (car elem) vars)
3480              (make-local-variable (car elem))
3481              (set (car elem) (eval (nth 1 elem))))))))
3482
3483 (defun gnus-summary-read-group (group &optional show-all no-article
3484                                       kill-buffer no-display backward
3485                                       select-articles)
3486   "Start reading news in newsgroup GROUP.
3487 If SHOW-ALL is non-nil, already read articles are also listed.
3488 If NO-ARTICLE is non-nil, no article is selected initially.
3489 If NO-DISPLAY, don't generate a summary buffer."
3490   (let (result)
3491     (while (and group
3492                 (null (setq result
3493                             (let ((gnus-auto-select-next nil))
3494                               (or (gnus-summary-read-group-1
3495                                    group show-all no-article
3496                                    kill-buffer no-display
3497                                    select-articles)
3498                                   (setq show-all nil
3499                                         select-articles nil)))))
3500                 (eq gnus-auto-select-next 'quietly))
3501       (set-buffer gnus-group-buffer)
3502       ;; The entry function called above goes to the next
3503       ;; group automatically, so we go two groups back
3504       ;; if we are searching for the previous group.
3505       (when backward
3506         (gnus-group-prev-unread-group 2))
3507       (if (not (equal group (gnus-group-group-name)))
3508           (setq group (gnus-group-group-name))
3509         (setq group nil)))
3510     result))
3511
3512 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3513   "Directly jump to the other GROUP from summary buffer.
3514 If SHOW-ALL is non-nil, already read articles are also listed."
3515   (interactive
3516    (if (eq gnus-summary-buffer (current-buffer))
3517        (list (completing-read
3518               "Group: " gnus-active-hashtb nil t
3519               (when (and gnus-newsgroup-name
3520                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3521                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3522               'gnus-group-history)
3523              current-prefix-arg)
3524      (error "%s must be invoked from a gnus summary buffer." this-command)))
3525   (unless (or (zerop (length group))
3526               (and gnus-newsgroup-name
3527                    (string-equal gnus-newsgroup-name group)))
3528     (gnus-summary-exit)
3529     (gnus-summary-read-group group show-all
3530                              gnus-dont-select-after-jump-to-other-group)))
3531
3532 (defun gnus-summary-read-group-1 (group show-all no-article
3533                                         kill-buffer no-display
3534                                         &optional select-articles)
3535   ;; Killed foreign groups can't be entered.
3536   ;;  (when (and (not (gnus-group-native-p group))
3537   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3538   ;;    (error "Dead non-native groups can't be entered"))
3539   (gnus-message 5 "Retrieving newsgroup: %s..."
3540                 (gnus-group-decoded-name group))
3541   (let* ((new-group (gnus-summary-setup-buffer group))
3542          (quit-config (gnus-group-quit-config group))
3543          (did-select (and new-group (gnus-select-newsgroup
3544                                      group show-all select-articles))))
3545     (cond
3546      ;; This summary buffer exists already, so we just select it.
3547      ((not new-group)
3548       (gnus-set-global-variables)
3549       (when kill-buffer
3550         (gnus-kill-or-deaden-summary kill-buffer))
3551       (gnus-configure-windows 'summary 'force)
3552       (gnus-set-mode-line 'summary)
3553       (gnus-summary-position-point)
3554       (message "")
3555       t)
3556      ;; We couldn't select this group.
3557      ((null did-select)
3558       (when (and (eq major-mode 'gnus-summary-mode)
3559                  (not (equal (current-buffer) kill-buffer)))
3560         (kill-buffer (current-buffer))
3561         (if (not quit-config)
3562             (progn
3563               ;; Update the info -- marks might need to be removed,
3564               ;; for instance.
3565               (gnus-summary-update-info)
3566               (set-buffer gnus-group-buffer)
3567               (gnus-group-jump-to-group group)
3568               (gnus-group-next-unread-group 1))
3569           (gnus-handle-ephemeral-exit quit-config)))
3570       (let ((grpinfo (gnus-get-info group)))
3571         (if (null (gnus-info-read grpinfo))
3572             (gnus-message 3 "Group %s contains no messages"
3573                           (gnus-group-decoded-name group))
3574           (gnus-message 3 "Can't select group")))
3575       nil)
3576      ;; The user did a `C-g' while prompting for number of articles,
3577      ;; so we exit this group.
3578      ((eq did-select 'quit)
3579       (and (eq major-mode 'gnus-summary-mode)
3580            (not (equal (current-buffer) kill-buffer))
3581            (kill-buffer (current-buffer)))
3582       (when kill-buffer
3583         (gnus-kill-or-deaden-summary kill-buffer))
3584       (if (not quit-config)
3585           (progn
3586             (set-buffer gnus-group-buffer)
3587             (gnus-group-jump-to-group group)
3588             (gnus-group-next-unread-group 1)
3589             (gnus-configure-windows 'group 'force))
3590         (gnus-handle-ephemeral-exit quit-config))
3591       ;; Finally signal the quit.
3592       (signal 'quit nil))
3593      ;; The group was successfully selected.
3594      (t
3595       (gnus-set-global-variables)
3596       ;; Save the active value in effect when the group was entered.
3597       (setq gnus-newsgroup-active
3598             (gnus-copy-sequence
3599              (gnus-active gnus-newsgroup-name)))
3600       ;; You can change the summary buffer in some way with this hook.
3601       (gnus-run-hooks 'gnus-select-group-hook)
3602       (gnus-update-format-specifications
3603        nil 'summary 'summary-mode 'summary-dummy)
3604       (gnus-update-summary-mark-positions)
3605       ;; Do score processing.
3606       (when gnus-use-scoring
3607         (gnus-possibly-score-headers))
3608       ;; Check whether to fill in the gaps in the threads.
3609       (when gnus-build-sparse-threads
3610         (gnus-build-sparse-threads))
3611       ;; Find the initial limit.
3612       (if show-all
3613           (let ((gnus-newsgroup-dormant nil))
3614             (gnus-summary-initial-limit show-all))
3615         (gnus-summary-initial-limit show-all))
3616       ;; Generate the summary buffer.
3617       (unless no-display
3618         (gnus-summary-prepare))
3619       (when gnus-use-trees
3620         (gnus-tree-open group)
3621         (setq gnus-summary-highlight-line-function
3622               'gnus-tree-highlight-article))
3623       ;; If the summary buffer is empty, but there are some low-scored
3624       ;; articles or some excluded dormants, we include these in the
3625       ;; buffer.
3626       (when (and (zerop (buffer-size))
3627                  (not no-display))
3628         (cond (gnus-newsgroup-dormant
3629                (gnus-summary-limit-include-dormant))
3630               ((and gnus-newsgroup-scored show-all)
3631                (gnus-summary-limit-include-expunged t))))
3632       ;; Function `gnus-apply-kill-file' must be called in this hook.
3633       (gnus-run-hooks 'gnus-apply-kill-hook)
3634       (if (and (zerop (buffer-size))
3635                (not no-display))
3636           (progn
3637             ;; This newsgroup is empty.
3638             (gnus-summary-catchup-and-exit nil t)
3639             (gnus-message 6 "No unread news")
3640             (when kill-buffer
3641               (gnus-kill-or-deaden-summary kill-buffer))
3642             ;; Return nil from this function.
3643             nil)
3644         ;; Hide conversation thread subtrees.  We cannot do this in
3645         ;; gnus-summary-prepare-hook since kill processing may not
3646         ;; work with hidden articles.
3647         (gnus-summary-maybe-hide-threads)
3648         (when kill-buffer
3649           (gnus-kill-or-deaden-summary kill-buffer))
3650         (gnus-summary-auto-select-subject)
3651         ;; Show first unread article if requested.
3652         (if (and (not no-article)
3653                  (not no-display)
3654                  gnus-newsgroup-unreads
3655                  gnus-auto-select-first)
3656             (progn
3657               (gnus-configure-windows 'summary)
3658               (let ((art (gnus-summary-article-number)))
3659                 (unless (and (not gnus-plugged)
3660                              (or (memq art gnus-newsgroup-undownloaded)
3661                                  (memq art gnus-newsgroup-downloadable)))
3662                   (gnus-summary-goto-article art))))
3663           ;; Don't select any articles.
3664           (gnus-summary-position-point)
3665           (gnus-configure-windows 'summary 'force)
3666           (gnus-set-mode-line 'summary))
3667         (when (and gnus-auto-center-group
3668                    (get-buffer-window gnus-group-buffer t))
3669           ;; Gotta use windows, because recenter does weird stuff if
3670           ;; the current buffer ain't the displayed window.
3671           (let ((owin (selected-window)))
3672             (select-window (get-buffer-window gnus-group-buffer t))
3673             (when (gnus-group-goto-group group)
3674               (recenter))
3675             (select-window owin)))
3676         ;; Mark this buffer as "prepared".
3677         (setq gnus-newsgroup-prepared t)
3678         (gnus-run-hooks 'gnus-summary-prepared-hook)
3679         (unless (gnus-ephemeral-group-p group)
3680           (gnus-group-update-group group))
3681         t)))))
3682
3683 (defun gnus-summary-auto-select-subject ()
3684   "Select the subject line on initial group entry."
3685   (goto-char (point-min))
3686   (cond
3687    ((eq gnus-auto-select-subject 'best)
3688     (gnus-summary-best-unread-subject))
3689    ((eq gnus-auto-select-subject 'unread)
3690     (gnus-summary-first-unread-subject))
3691    ((eq gnus-auto-select-subject 'unseen)
3692     (gnus-summary-first-unseen-subject))
3693    ((eq gnus-auto-select-subject 'unseen-or-unread)
3694     (gnus-summary-first-unseen-or-unread-subject))
3695    ((eq gnus-auto-select-subject 'first)
3696     ;; Do nothing.
3697     )
3698    ((functionp gnus-auto-select-subject)
3699     (funcall gnus-auto-select-subject))))
3700
3701 (defun gnus-summary-prepare ()
3702   "Generate the summary buffer."
3703   (interactive)
3704   (let ((buffer-read-only nil))
3705     (erase-buffer)
3706     (setq gnus-newsgroup-data nil
3707           gnus-newsgroup-data-reverse nil)
3708     (gnus-run-hooks 'gnus-summary-generate-hook)
3709     ;; Generate the buffer, either with threads or without.
3710     (when gnus-newsgroup-headers
3711       (gnus-summary-prepare-threads
3712        (if gnus-show-threads
3713            (gnus-sort-gathered-threads
3714             (funcall gnus-summary-thread-gathering-function
3715                      (gnus-sort-threads
3716                       (gnus-cut-threads (gnus-make-threads)))))
3717          ;; Unthreaded display.
3718          (gnus-sort-articles gnus-newsgroup-headers))))
3719     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3720     ;; Call hooks for modifying summary buffer.
3721     (goto-char (point-min))
3722     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3723
3724 (defsubst gnus-general-simplify-subject (subject)
3725   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3726   (setq subject
3727         (cond
3728          ;; Truncate the subject.
3729          (gnus-simplify-subject-functions
3730           (gnus-map-function gnus-simplify-subject-functions subject))
3731          ((numberp gnus-summary-gather-subject-limit)
3732           (setq subject (gnus-simplify-subject-re subject))
3733           (if (> (length subject) gnus-summary-gather-subject-limit)
3734               (substring subject 0 gnus-summary-gather-subject-limit)
3735             subject))
3736          ;; Fuzzily simplify it.
3737          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3738           (gnus-simplify-subject-fuzzy subject))
3739          ;; Just remove the leading "Re:".
3740          (t
3741           (gnus-simplify-subject-re subject))))
3742
3743   (if (and gnus-summary-gather-exclude-subject
3744            (string-match gnus-summary-gather-exclude-subject subject))
3745       nil                               ; This article shouldn't be gathered
3746     subject))
3747
3748 (defun gnus-summary-simplify-subject-query ()
3749   "Query where the respool algorithm would put this article."
3750   (interactive)
3751   (gnus-summary-select-article)
3752   (message "%s"
3753            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3754
3755 (defun gnus-gather-threads-by-subject (threads)
3756   "Gather threads by looking at Subject headers."
3757   (if (not gnus-summary-make-false-root)
3758       threads
3759     (let ((hashtb (gnus-make-hashtable 1024))
3760           (prev threads)
3761           (result threads)
3762           subject hthread whole-subject)
3763       (while threads
3764         (setq subject (gnus-general-simplify-subject
3765                        (setq whole-subject (mail-header-subject
3766                                             (caar threads)))))
3767         (when subject
3768           (if (setq hthread (gnus-gethash subject hashtb))
3769               (progn
3770                 ;; We enter a dummy root into the thread, if we
3771                 ;; haven't done that already.
3772                 (unless (stringp (caar hthread))
3773                   (setcar hthread (list whole-subject (car hthread))))
3774                 ;; We add this new gathered thread to this gathered
3775                 ;; thread.
3776                 (setcdr (car hthread)
3777                         (nconc (cdar hthread) (list (car threads))))
3778                 ;; Remove it from the list of threads.
3779                 (setcdr prev (cdr threads))
3780                 (setq threads prev))
3781             ;; Enter this thread into the hash table.
3782             (gnus-sethash subject
3783                           (if gnus-summary-make-false-root-always
3784                               (progn
3785                                 ;; If you want a dummy root above all
3786                                 ;; threads...
3787                                 (setcar threads (list whole-subject
3788                                                       (car threads)))
3789                                 threads)
3790                             threads)
3791                           hashtb)))
3792         (setq prev threads)
3793         (setq threads (cdr threads)))
3794       result)))
3795
3796 (defun gnus-gather-threads-by-references (threads)
3797   "Gather threads by looking at References headers."
3798   (let ((idhashtb (gnus-make-hashtable 1024))
3799         (thhashtb (gnus-make-hashtable 1024))
3800         (prev threads)
3801         (result threads)
3802         ids references id gthread gid entered ref)
3803     (while threads
3804       (when (setq references (mail-header-references (caar threads)))
3805         (setq id (mail-header-id (caar threads))
3806               ids (inline (gnus-split-references references))
3807               entered nil)
3808         (while (setq ref (pop ids))
3809           (setq ids (delete ref ids))
3810           (if (not (setq gid (gnus-gethash ref idhashtb)))
3811               (progn
3812                 (gnus-sethash ref id idhashtb)
3813                 (gnus-sethash id threads thhashtb))
3814             (setq gthread (gnus-gethash gid thhashtb))
3815             (unless entered
3816               ;; We enter a dummy root into the thread, if we
3817               ;; haven't done that already.
3818               (unless (stringp (caar gthread))
3819                 (setcar gthread (list (mail-header-subject (caar gthread))
3820                                       (car gthread))))
3821               ;; We add this new gathered thread to this gathered
3822               ;; thread.
3823               (setcdr (car gthread)
3824                       (nconc (cdar gthread) (list (car threads)))))
3825             ;; Add it into the thread hash table.
3826             (gnus-sethash id gthread thhashtb)
3827             (setq entered t)
3828             ;; Remove it from the list of threads.
3829             (setcdr prev (cdr threads))
3830             (setq threads prev))))
3831       (setq prev threads)
3832       (setq threads (cdr threads)))
3833     result))
3834
3835 (defun gnus-sort-gathered-threads (threads)
3836   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3837   (let ((result threads))
3838     (while threads
3839       (when (stringp (caar threads))
3840         (setcdr (car threads)
3841                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3842       (setq threads (cdr threads)))
3843     result))
3844
3845 (defun gnus-thread-loop-p (root thread)
3846   "Say whether ROOT is in THREAD."
3847   (let ((stack (list thread))
3848         (infloop 0)
3849         th)
3850     (while (setq thread (pop stack))
3851       (setq th (cdr thread))
3852       (while (and th
3853                   (not (eq (caar th) root)))
3854         (pop th))
3855       (if th
3856           ;; We have found a loop.
3857           (let (ref-dep)
3858             (setcdr thread (delq (car th) (cdr thread)))
3859             (if (boundp (setq ref-dep (intern "none"
3860                                               gnus-newsgroup-dependencies)))
3861                 (setcdr (symbol-value ref-dep)
3862                         (nconc (cdr (symbol-value ref-dep))
3863                                (list (car th))))
3864               (set ref-dep (list nil (car th))))
3865             (setq infloop 1
3866                   stack nil))
3867         ;; Push all the subthreads onto the stack.
3868         (push (cdr thread) stack)))
3869     infloop))
3870
3871 (defun gnus-make-threads ()
3872   "Go through the dependency hashtb and find the roots.  Return all threads."
3873   (let (threads)
3874     (while (catch 'infloop
3875              (mapatoms
3876               (lambda (refs)
3877                 ;; Deal with self-referencing References loops.
3878                 (when (and (car (symbol-value refs))
3879                            (not (zerop
3880                                  (apply
3881                                   '+
3882                                   (mapcar
3883                                    (lambda (thread)
3884                                      (gnus-thread-loop-p
3885                                       (car (symbol-value refs)) thread))
3886                                    (cdr (symbol-value refs)))))))
3887                   (setq threads nil)
3888                   (throw 'infloop t))
3889                 (unless (car (symbol-value refs))
3890                   ;; These threads do not refer back to any other articles,
3891                   ;; so they're roots.
3892                   (setq threads (append (cdr (symbol-value refs)) threads))))
3893               gnus-newsgroup-dependencies)))
3894     threads))
3895
3896 ;; Build the thread tree.
3897 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3898   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3899
3900 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3901 if it was already present.
3902
3903 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3904 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3905 Message-IDs will be renamed to a unique Message-ID before being
3906 entered.
3907
3908 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3909   (let* ((id (mail-header-id header))
3910          (id-dep (and id (intern id dependencies)))
3911          parent-id ref ref-dep ref-header replaced)
3912     ;; Enter this `header' in the `dependencies' table.
3913     (cond
3914      ((not id-dep)
3915       (setq header nil))
3916      ;; The first two cases do the normal part: enter a new `header'
3917      ;; in the `dependencies' table.
3918      ((not (boundp id-dep))
3919       (set id-dep (list header)))
3920      ((null (car (symbol-value id-dep)))
3921       (setcar (symbol-value id-dep) header))
3922
3923      ;; From here the `header' was already present in the
3924      ;; `dependencies' table.
3925      (force-new
3926       ;; Overrides an existing entry;
3927       ;; just set the header part of the entry.
3928       (setcar (symbol-value id-dep) header)
3929       (setq replaced t))
3930
3931      ;; Renames the existing `header' to a unique Message-ID.
3932      ((not gnus-summary-ignore-duplicates)
3933       ;; An article with this Message-ID has already been seen.
3934       ;; We rename the Message-ID.
3935       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3936            (list header))
3937       (mail-header-set-id header id))
3938
3939      ;; The last case ignores an existing entry, except it adds any
3940      ;; additional Xrefs (in case the two articles came from different
3941      ;; servers.
3942      ;; Also sets `header' to `nil' meaning that the `dependencies'
3943      ;; table was *not* modified.
3944      (t
3945       (mail-header-set-xref
3946        (car (symbol-value id-dep))
3947        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3948                    "")
3949                (or (mail-header-xref header) "")))
3950       (setq header nil)))
3951
3952     (when (and header (not replaced))
3953       ;; First check that we are not creating a References loop.
3954       (setq parent-id (gnus-parent-id (mail-header-references header)))
3955       (setq ref parent-id)
3956       (while (and ref
3957                   (setq ref-dep (intern-soft ref dependencies))
3958                   (boundp ref-dep)
3959                   (setq ref-header (car (symbol-value ref-dep))))
3960         (if (string= id ref)
3961             ;; Yuk!  This is a reference loop.  Make the article be a
3962             ;; root article.
3963             (progn
3964               (mail-header-set-references (car (symbol-value id-dep)) "none")
3965               (setq ref nil)
3966               (setq parent-id nil))
3967           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3968       (setq ref-dep (intern (or parent-id "none") dependencies))
3969       (if (boundp ref-dep)
3970           (setcdr (symbol-value ref-dep)
3971                   (nconc (cdr (symbol-value ref-dep))
3972                          (list (symbol-value id-dep))))
3973         (set ref-dep (list nil (symbol-value id-dep)))))
3974     header))
3975
3976 (defun gnus-extract-message-id-from-in-reply-to (string)
3977   (if (string-match "<[^>]+>" string)
3978       (substring string (match-beginning 0) (match-end 0))
3979     nil))
3980
3981 (defun gnus-build-sparse-threads ()
3982   (let ((headers gnus-newsgroup-headers)
3983         (mail-parse-charset gnus-newsgroup-charset)
3984         (gnus-summary-ignore-duplicates t)
3985         header references generation relations
3986         subject child end new-child date)
3987     ;; First we create an alist of generations/relations, where
3988     ;; generations is how much we trust the relation, and the relation
3989     ;; is parent/child.
3990     (gnus-message 7 "Making sparse threads...")
3991     (save-excursion
3992       (nnheader-set-temp-buffer " *gnus sparse threads*")
3993       (while (setq header (pop headers))
3994         (when (and (setq references (mail-header-references header))
3995                    (not (string= references "")))
3996           (insert references)
3997           (setq child (mail-header-id header)
3998                 subject (mail-header-subject header)
3999                 date (mail-header-date header)
4000                 generation 0)
4001           (while (search-backward ">" nil t)
4002             (setq end (1+ (point)))
4003             (when (search-backward "<" nil t)
4004               (setq new-child (buffer-substring (point) end))
4005               (push (list (incf generation)
4006                           child (setq child new-child)
4007                           subject date)
4008                     relations)))
4009           (when child
4010             (push (list (1+ generation) child nil subject) relations))
4011           (erase-buffer)))
4012       (kill-buffer (current-buffer)))
4013     ;; Sort over trustworthiness.
4014     (mapcar
4015      (lambda (relation)
4016        (when (gnus-dependencies-add-header
4017               (make-full-mail-header-from-decoded-header
4018                gnus-reffed-article-number
4019                (nth 3 relation) "" (or (nth 4 relation) "")
4020                (nth 1 relation)
4021                (or (nth 2 relation) "") 0 0 "")
4022               gnus-newsgroup-dependencies nil)
4023          (push gnus-reffed-article-number gnus-newsgroup-limit)
4024          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4025          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4026                gnus-newsgroup-reads)
4027          (decf gnus-reffed-article-number)))
4028      (sort relations 'car-less-than-car))
4029     (gnus-message 7 "Making sparse threads...done")))
4030
4031 (defun gnus-build-old-threads ()
4032   ;; Look at all the articles that refer back to old articles, and
4033   ;; fetch the headers for the articles that aren't there.  This will
4034   ;; build complete threads - if the roots haven't been expired by the
4035   ;; server, that is.
4036   (let ((mail-parse-charset gnus-newsgroup-charset)
4037         id heads)
4038     (mapatoms
4039      (lambda (refs)
4040        (when (not (car (symbol-value refs)))
4041          (setq heads (cdr (symbol-value refs)))
4042          (while heads
4043            (if (memq (mail-header-number (caar heads))
4044                      gnus-newsgroup-dormant)
4045                (setq heads (cdr heads))
4046              (setq id (symbol-name refs))
4047              (while (and (setq id (gnus-build-get-header id))
4048                          (not (car (gnus-id-to-thread id)))))
4049              (setq heads nil)))))
4050      gnus-newsgroup-dependencies)))
4051
4052 (defsubst gnus-remove-odd-characters (string)
4053   "Translate STRING into something that doesn't contain weird characters."
4054   (mm-subst-char-in-string
4055    ?\r ?\-
4056    (mm-subst-char-in-string ?\n ?\- string t) t))
4057
4058 ;; This function has to be called with point after the article number
4059 ;; on the beginning of the line.
4060 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4061   (let ((eol (point-at-eol))
4062         (buffer (current-buffer))
4063         header references in-reply-to)
4064
4065     ;; overview: [num subject from date id refs chars lines misc]
4066     (unwind-protect
4067         (progn
4068           (narrow-to-region (point) eol)
4069           (unless (eobp)
4070             (forward-char))
4071
4072           (setq header
4073                 (make-full-mail-header
4074                  number                         ; number
4075                  (nnheader-nov-field)           ; subject
4076                  (nnheader-nov-field)           ; from
4077                  (nnheader-nov-field)           ; date
4078                  (nnheader-nov-read-message-id number)  ; id
4079                  (setq references (nnheader-nov-field)) ; refs
4080                  (nnheader-nov-read-integer)    ; chars
4081                  (nnheader-nov-read-integer)    ; lines
4082                  (unless (eobp)
4083                    (if (looking-at "Xref: ")
4084                        (goto-char (match-end 0)))
4085                    (nnheader-nov-field))        ; Xref
4086                  (nnheader-nov-parse-extra))))  ; extra
4087
4088       (widen))
4089
4090     (when (and (string= references "")
4091                (setq in-reply-to (mail-header-extra header))
4092                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4093       (mail-header-set-references
4094        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4095
4096     (when gnus-alter-header-function
4097       (funcall gnus-alter-header-function header))
4098     (gnus-dependencies-add-header header dependencies force-new)))
4099
4100 (defun gnus-build-get-header (id)
4101   "Look through the buffer of NOV lines and find the header to ID.
4102 Enter this line into the dependencies hash table, and return
4103 the id of the parent article (if any)."
4104   (let ((deps gnus-newsgroup-dependencies)
4105         found header)
4106     (prog1
4107         (save-excursion
4108           (set-buffer nntp-server-buffer)
4109           (let ((case-fold-search nil))
4110             (goto-char (point-min))
4111             (while (and (not found)
4112                         (search-forward id nil t))
4113               (beginning-of-line)
4114               (setq found (looking-at
4115                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4116                                    (regexp-quote id))))
4117               (or found (beginning-of-line 2)))
4118             (when found
4119               (beginning-of-line)
4120               (and
4121                (setq header (gnus-nov-parse-line
4122                              (read (current-buffer)) deps))
4123                (gnus-parent-id (mail-header-references header))))))
4124       (when header
4125         (let ((number (mail-header-number header)))
4126           (push number gnus-newsgroup-limit)
4127           (push header gnus-newsgroup-headers)
4128           (if (memq number gnus-newsgroup-unselected)
4129               (progn
4130                 (setq gnus-newsgroup-unreads
4131                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4132                                                number))
4133                 (setq gnus-newsgroup-unselected
4134                       (delq number gnus-newsgroup-unselected)))
4135             (push number gnus-newsgroup-ancient)))))))
4136
4137 (defun gnus-build-all-threads ()
4138   "Read all the headers."
4139   (let ((gnus-summary-ignore-duplicates t)
4140         (mail-parse-charset gnus-newsgroup-charset)
4141         (dependencies gnus-newsgroup-dependencies)
4142         header article)
4143     (save-excursion
4144       (set-buffer nntp-server-buffer)
4145       (let ((case-fold-search nil))
4146         (goto-char (point-min))
4147         (while (not (eobp))
4148           (ignore-errors
4149             (setq article (read (current-buffer))
4150                   header (gnus-nov-parse-line article dependencies)))
4151           (when header
4152             (with-current-buffer gnus-summary-buffer
4153               (push header gnus-newsgroup-headers)
4154               (if (memq (setq article (mail-header-number header))
4155                         gnus-newsgroup-unselected)
4156                   (progn
4157                     (setq gnus-newsgroup-unreads
4158                           (gnus-add-to-sorted-list
4159                            gnus-newsgroup-unreads article))
4160                     (setq gnus-newsgroup-unselected
4161                           (delq article gnus-newsgroup-unselected)))
4162                 (push article gnus-newsgroup-ancient)))
4163             (forward-line 1)))))))
4164
4165 (defun gnus-summary-update-article-line (article header)
4166   "Update the line for ARTICLE using HEADER."
4167   (let* ((id (mail-header-id header))
4168          (thread (gnus-id-to-thread id)))
4169     (unless thread
4170       (error "Article in no thread"))
4171     ;; Update the thread.
4172     (setcar thread header)
4173     (gnus-summary-goto-subject article)
4174     (let* ((datal (gnus-data-find-list article))
4175            (data (car datal))
4176            (buffer-read-only nil)
4177            (level (gnus-summary-thread-level)))
4178       (gnus-delete-line)
4179       (let ((inserted (- (point)
4180                          (progn
4181                            (gnus-summary-insert-line
4182                             header level nil
4183                             (memq article gnus-newsgroup-undownloaded)
4184                             (gnus-article-mark article)
4185                             (memq article gnus-newsgroup-replied)
4186                             (memq article gnus-newsgroup-expirable)
4187                             ;; Only insert the Subject string when it's different
4188                             ;; from the previous Subject string.
4189                             (if (and
4190                                  gnus-show-threads
4191                                  (gnus-subject-equal
4192                                   (condition-case ()
4193                                       (mail-header-subject
4194                                        (gnus-data-header
4195                                         (cadr
4196                                          (gnus-data-find-list
4197                                           article
4198                                           (gnus-data-list t)))))
4199                                     ;; Error on the side of excessive subjects.
4200                                     (error ""))
4201                                   (mail-header-subject header)))
4202                                 ""
4203                               (mail-header-subject header))
4204                             nil (cdr (assq article gnus-newsgroup-scored))
4205                             (memq article gnus-newsgroup-processable))
4206                            (point)))))
4207         (when (cdr datal)
4208           (gnus-data-update-list
4209            (cdr datal)
4210            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4211
4212 (defun gnus-summary-update-article (article &optional iheader)
4213   "Update ARTICLE in the summary buffer."
4214   (set-buffer gnus-summary-buffer)
4215   (let* ((header (gnus-summary-article-header article))
4216          (id (mail-header-id header))
4217          (data (gnus-data-find article))
4218          (thread (gnus-id-to-thread id))
4219          (references (mail-header-references header))
4220          (parent
4221           (gnus-id-to-thread
4222            (or (gnus-parent-id
4223                 (when (and references
4224                            (not (equal "" references)))
4225                   references))
4226                "none")))
4227          (buffer-read-only nil)
4228          (old (car thread)))
4229     (when thread
4230       (unless iheader
4231         (setcar thread nil)
4232         (when parent
4233           (delq thread parent)))
4234       (if (gnus-summary-insert-subject id header)
4235           ;; Set the (possibly) new article number in the data structure.
4236           (gnus-data-set-number data (gnus-id-to-article id))
4237         (setcar thread old)
4238         nil))))
4239
4240 (defun gnus-rebuild-thread (id &optional line)
4241   "Rebuild the thread containing ID.
4242 If LINE, insert the rebuilt thread starting on line LINE."
4243   (let ((buffer-read-only nil)
4244         old-pos current thread data)
4245     (if (not gnus-show-threads)
4246         (setq thread (list (car (gnus-id-to-thread id))))
4247       ;; Get the thread this article is part of.
4248       (setq thread (gnus-remove-thread id)))
4249     (setq old-pos (point-at-bol))
4250     (setq current (save-excursion
4251                     (and (re-search-backward "[\r\n]" nil t)
4252                          (gnus-summary-article-number))))
4253     ;; If this is a gathered thread, we have to go some re-gathering.
4254     (when (stringp (car thread))
4255       (let ((subject (car thread))
4256             roots thr)
4257         (setq thread (cdr thread))
4258         (while thread
4259           (unless (memq (setq thr (gnus-id-to-thread
4260                                    (gnus-root-id
4261                                     (mail-header-id (caar thread)))))
4262                         roots)
4263             (push thr roots))
4264           (setq thread (cdr thread)))
4265         ;; We now have all (unique) roots.
4266         (if (= (length roots) 1)
4267             ;; All the loose roots are now one solid root.
4268             (setq thread (car roots))
4269           (setq thread (cons subject (gnus-sort-threads roots))))))
4270     (let (threads)
4271       ;; We then insert this thread into the summary buffer.
4272       (when line
4273         (goto-char (point-min))
4274         (forward-line (1- line)))
4275       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4276         (if gnus-show-threads
4277             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4278           (gnus-summary-prepare-unthreaded thread))
4279         (setq data (nreverse gnus-newsgroup-data))
4280         (setq threads gnus-newsgroup-threads))
4281       ;; We splice the new data into the data structure.
4282       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4283       ;;!!! then we want to insert at the beginning of the buffer.
4284       ;;!!! That happens to be true with Gnus now, but that may
4285       ;;!!! change in the future.  Perhaps.
4286       (gnus-data-enter-list
4287        (if line nil current) data (- (point) old-pos))
4288       (setq gnus-newsgroup-threads
4289             (nconc threads gnus-newsgroup-threads))
4290       (gnus-data-compute-positions))))
4291
4292 (defun gnus-number-to-header (number)
4293   "Return the header for article NUMBER."
4294   (let ((headers gnus-newsgroup-headers))
4295     (while (and headers
4296                 (not (= number (mail-header-number (car headers)))))
4297       (pop headers))
4298     (when headers
4299       (car headers))))
4300
4301 (defun gnus-parent-headers (in-headers &optional generation)
4302   "Return the headers of the GENERATIONeth parent of HEADERS."
4303   (unless generation
4304     (setq generation 1))
4305   (let ((parent t)
4306         (headers in-headers)
4307         references)
4308     (while (and parent
4309                 (not (zerop generation))
4310                 (setq references (mail-header-references headers)))
4311       (setq headers (if (and references
4312                              (setq parent (gnus-parent-id references)))
4313                         (car (gnus-id-to-thread parent))
4314                       nil))
4315       (decf generation))
4316     (and (not (eq headers in-headers))
4317          headers)))
4318
4319 (defun gnus-id-to-thread (id)
4320   "Return the (sub-)thread where ID appears."
4321   (gnus-gethash id gnus-newsgroup-dependencies))
4322
4323 (defun gnus-id-to-article (id)
4324   "Return the article number of ID."
4325   (let ((thread (gnus-id-to-thread id)))
4326     (when (and thread
4327                (car thread))
4328       (mail-header-number (car thread)))))
4329
4330 (defun gnus-id-to-header (id)
4331   "Return the article headers of ID."
4332   (car (gnus-id-to-thread id)))
4333
4334 (defun gnus-article-displayed-root-p (article)
4335   "Say whether ARTICLE is a root(ish) article."
4336   (let ((level (gnus-summary-thread-level article))
4337         (refs (mail-header-references  (gnus-summary-article-header article)))
4338         particle)
4339     (cond
4340      ((null level) nil)
4341      ((zerop level) t)
4342      ((null refs) t)
4343      ((null (gnus-parent-id refs)) t)
4344      ((and (= 1 level)
4345            (null (setq particle (gnus-id-to-article
4346                                  (gnus-parent-id refs))))
4347            (null (gnus-summary-thread-level particle)))))))
4348
4349 (defun gnus-root-id (id)
4350   "Return the id of the root of the thread where ID appears."
4351   (let (last-id prev)
4352     (while (and id (setq prev (car (gnus-id-to-thread id))))
4353       (setq last-id id
4354             id (gnus-parent-id (mail-header-references prev))))
4355     last-id))
4356
4357 (defun gnus-articles-in-thread (thread)
4358   "Return the list of articles in THREAD."
4359   (cons (mail-header-number (car thread))
4360         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4361
4362 (defun gnus-remove-thread (id &optional dont-remove)
4363   "Remove the thread that has ID in it."
4364   (let (headers thread last-id)
4365     ;; First go up in this thread until we find the root.
4366     (setq last-id (gnus-root-id id)
4367           headers (message-flatten-list (gnus-id-to-thread last-id)))
4368     ;; We have now found the real root of this thread.  It might have
4369     ;; been gathered into some loose thread, so we have to search
4370     ;; through the threads to find the thread we wanted.
4371     (let ((threads gnus-newsgroup-threads)
4372           sub)
4373       (while threads
4374         (setq sub (car threads))
4375         (if (stringp (car sub))
4376             ;; This is a gathered thread, so we look at the roots
4377             ;; below it to find whether this article is in this
4378             ;; gathered root.
4379             (progn
4380               (setq sub (cdr sub))
4381               (while sub
4382                 (when (member (caar sub) headers)
4383                   (setq thread (car threads)
4384                         threads nil
4385                         sub nil))
4386                 (setq sub (cdr sub))))
4387           ;; It's an ordinary thread, so we check it.
4388           (when (eq (car sub) (car headers))
4389             (setq thread sub
4390                   threads nil)))
4391         (setq threads (cdr threads)))
4392       ;; If this article is in no thread, then it's a root.
4393       (if thread
4394           (unless dont-remove
4395             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4396         (setq thread (gnus-id-to-thread last-id)))
4397       (when thread
4398         (prog1
4399             thread                      ; We return this thread.
4400           (unless dont-remove
4401             (if (stringp (car thread))
4402                 (progn
4403                   ;; If we use dummy roots, then we have to remove the
4404                   ;; dummy root as well.
4405                   (when (eq gnus-summary-make-false-root 'dummy)
4406                     ;; We go to the dummy root by going to
4407                     ;; the first sub-"thread", and then one line up.
4408                     (gnus-summary-goto-article
4409                      (mail-header-number (caadr thread)))
4410                     (forward-line -1)
4411                     (gnus-delete-line)
4412                     (gnus-data-compute-positions))
4413                   (setq thread (cdr thread))
4414                   (while thread
4415                     (gnus-remove-thread-1 (car thread))
4416                     (setq thread (cdr thread))))
4417               (gnus-remove-thread-1 thread))))))))
4418
4419 (defun gnus-remove-thread-1 (thread)
4420   "Remove the thread THREAD recursively."
4421   (let ((number (mail-header-number (pop thread)))
4422         d)
4423     (setq thread (reverse thread))
4424     (while thread
4425       (gnus-remove-thread-1 (pop thread)))
4426     (when (setq d (gnus-data-find number))
4427       (goto-char (gnus-data-pos d))
4428       (gnus-summary-show-thread)
4429       (gnus-data-remove
4430        number
4431        (- (point-at-bol)
4432           (prog1
4433               (1+ (point-at-eol))
4434             (gnus-delete-line)))))))
4435
4436 (defun gnus-sort-threads-1 (threads func)
4437   (sort (mapcar (lambda (thread)
4438                   (cons (car thread)
4439                         (and (cdr thread)
4440                              (gnus-sort-threads-1 (cdr thread) func))))
4441                 threads) func))
4442
4443 (defun gnus-sort-threads (threads)
4444   "Sort THREADS."
4445   (if (not gnus-thread-sort-functions)
4446       threads
4447     (gnus-message 8 "Sorting threads...")
4448     (let ((max-lisp-eval-depth 5000))
4449       (prog1 (gnus-sort-threads-1
4450          threads
4451          (gnus-make-sort-function gnus-thread-sort-functions))
4452         (gnus-message 8 "Sorting threads...done")))))
4453
4454 (defun gnus-sort-articles (articles)
4455   "Sort ARTICLES."
4456   (when gnus-article-sort-functions
4457     (gnus-message 7 "Sorting articles...")
4458     (prog1
4459         (setq gnus-newsgroup-headers
4460               (sort articles (gnus-make-sort-function
4461                               gnus-article-sort-functions)))
4462       (gnus-message 7 "Sorting articles...done"))))
4463
4464 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4465 (defmacro gnus-thread-header (thread)
4466   "Return header of first article in THREAD.
4467 Note that THREAD must never, ever be anything else than a variable -
4468 using some other form will lead to serious barfage."
4469   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4470   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4471   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4472         (vector thread) 2))
4473
4474 (defsubst gnus-article-sort-by-number (h1 h2)
4475   "Sort articles by article number."
4476   (< (mail-header-number h1)
4477      (mail-header-number h2)))
4478
4479 (defun gnus-thread-sort-by-number (h1 h2)
4480   "Sort threads by root article number."
4481   (gnus-article-sort-by-number
4482    (gnus-thread-header h1) (gnus-thread-header h2)))
4483
4484 (defsubst gnus-article-sort-by-random (h1 h2)
4485   "Sort articles by article number."
4486   (zerop (random 2)))
4487
4488 (defun gnus-thread-sort-by-random (h1 h2)
4489   "Sort threads by root article number."
4490   (gnus-article-sort-by-random
4491    (gnus-thread-header h1) (gnus-thread-header h2)))
4492
4493 (defsubst gnus-article-sort-by-lines (h1 h2)
4494   "Sort articles by article Lines header."
4495   (< (mail-header-lines h1)
4496      (mail-header-lines h2)))
4497
4498 (defun gnus-thread-sort-by-lines (h1 h2)
4499   "Sort threads by root article Lines header."
4500   (gnus-article-sort-by-lines
4501    (gnus-thread-header h1) (gnus-thread-header h2)))
4502
4503 (defsubst gnus-article-sort-by-chars (h1 h2)
4504   "Sort articles by octet length."
4505   (< (mail-header-chars h1)
4506      (mail-header-chars h2)))
4507
4508 (defun gnus-thread-sort-by-chars (h1 h2)
4509   "Sort threads by root article octet length."
4510   (gnus-article-sort-by-chars
4511    (gnus-thread-header h1) (gnus-thread-header h2)))
4512
4513 (defsubst gnus-article-sort-by-author (h1 h2)
4514   "Sort articles by root author."
4515   (string-lessp
4516    (let ((addr (car (mime-entity-read-field h1 'From))))
4517      (or (std11-full-name-string addr)
4518          (std11-address-string addr)
4519          ""))
4520    (let ((addr (car (mime-entity-read-field h2 'From))))
4521      (or (std11-full-name-string addr)
4522          (std11-address-string addr)
4523          ""))
4524    ))
4525
4526 (defun gnus-thread-sort-by-author (h1 h2)
4527   "Sort threads by root author."
4528   (gnus-article-sort-by-author
4529    (gnus-thread-header h1)  (gnus-thread-header h2)))
4530
4531 (defsubst gnus-article-sort-by-subject (h1 h2)
4532   "Sort articles by root subject."
4533   (string-lessp
4534    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4535    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4536
4537 (defun gnus-thread-sort-by-subject (h1 h2)
4538   "Sort threads by root subject."
4539   (gnus-article-sort-by-subject
4540    (gnus-thread-header h1) (gnus-thread-header h2)))
4541
4542 (defsubst gnus-article-sort-by-date (h1 h2)
4543   "Sort articles by root article date."
4544   (time-less-p
4545    (gnus-date-get-time (mail-header-date h1))
4546    (gnus-date-get-time (mail-header-date h2))))
4547
4548 (defun gnus-thread-sort-by-date (h1 h2)
4549   "Sort threads by root article date."
4550   (gnus-article-sort-by-date
4551    (gnus-thread-header h1) (gnus-thread-header h2)))
4552
4553 (defsubst gnus-article-sort-by-score (h1 h2)
4554   "Sort articles by root article score.
4555 Unscored articles will be counted as having a score of zero."
4556   (> (or (cdr (assq (mail-header-number h1)
4557                     gnus-newsgroup-scored))
4558          gnus-summary-default-score 0)
4559      (or (cdr (assq (mail-header-number h2)
4560                     gnus-newsgroup-scored))
4561          gnus-summary-default-score 0)))
4562
4563 (defun gnus-thread-sort-by-score (h1 h2)
4564   "Sort threads by root article score."
4565   (gnus-article-sort-by-score
4566    (gnus-thread-header h1) (gnus-thread-header h2)))
4567
4568 (defun gnus-thread-sort-by-total-score (h1 h2)
4569   "Sort threads by the sum of all scores in the thread.
4570 Unscored articles will be counted as having a score of zero."
4571   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4572
4573 (defun gnus-thread-total-score (thread)
4574   ;; This function find the total score of THREAD.
4575   (cond
4576    ((null thread)
4577     0)
4578    ((consp thread)
4579     (if (stringp (car thread))
4580         (apply gnus-thread-score-function 0
4581                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4582       (gnus-thread-total-score-1 thread)))
4583    (t
4584     (gnus-thread-total-score-1 (list thread)))))
4585
4586 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4587   "Sort threads such that the thread with the most recently arrived article comes first."
4588   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4589
4590 (defun gnus-thread-highest-number (thread)
4591   "Return the highest article number in THREAD."
4592   (apply 'max (mapcar (lambda (header)
4593                         (mail-header-number header))
4594                       (message-flatten-list thread))))
4595
4596 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4597   "Sort threads such that the thread with the most recently dated article comes first."
4598   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4599
4600 (defun gnus-thread-latest-date (thread)
4601   "Return the highest article date in THREAD."
4602   (let ((previous-time 0))
4603     (apply 'max
4604            (mapcar
4605             (lambda (header)
4606               (setq previous-time
4607                     (condition-case ()
4608                         (time-to-seconds (mail-header-parse-date
4609                                           (mail-header-date header)))
4610                       (error previous-time))))
4611             (sort
4612              (message-flatten-list thread)
4613              (lambda (h1 h2)
4614                (< (mail-header-number h1)
4615                   (mail-header-number h2))))))))
4616
4617 (defun gnus-thread-total-score-1 (root)
4618   ;; This function find the total score of the thread below ROOT.
4619   (setq root (car root))
4620   (apply gnus-thread-score-function
4621          (or (append
4622               (mapcar 'gnus-thread-total-score
4623                       (cdr (gnus-id-to-thread (mail-header-id root))))
4624               (when (> (mail-header-number root) 0)
4625                 (list (or (cdr (assq (mail-header-number root)
4626                                      gnus-newsgroup-scored))
4627                           gnus-summary-default-score 0))))
4628              (list gnus-summary-default-score)
4629              '(0))))
4630
4631 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4632 (defvar gnus-tmp-prev-subject nil)
4633 (defvar gnus-tmp-false-parent nil)
4634 (defvar gnus-tmp-root-expunged nil)
4635 (defvar gnus-tmp-dummy-line nil)
4636
4637 (eval-when-compile (defvar gnus-tmp-header))
4638 (defun gnus-extra-header (type &optional header)
4639   "Return the extra header of TYPE."
4640   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4641       ""))
4642
4643 (defvar gnus-tmp-thread-tree-header-string "")
4644
4645 (defcustom gnus-sum-thread-tree-root "> "
4646   "With %B spec, used for the root of a thread.
4647 If nil, use subject instead."
4648   :type '(radio (const :format "%v  " nil) (string :size 0))
4649   :group 'gnus-thread)
4650 (defcustom gnus-sum-thread-tree-false-root "> "
4651   "With %B spec, used for a false root of a thread.
4652 If nil, use subject instead."
4653   :type '(radio (const :format "%v  " nil) (string :size 0))
4654   :group 'gnus-thread)
4655 (defcustom gnus-sum-thread-tree-single-indent ""
4656   "With %B spec, used for a thread with just one message.
4657 If nil, use subject instead."
4658   :type '(radio (const :format "%v  " nil) (string :size 0))
4659   :group 'gnus-thread)
4660 (defcustom gnus-sum-thread-tree-vertical "| "
4661   "With %B spec, used for drawing a vertical line."
4662   :type 'string
4663   :group 'gnus-thread)
4664 (defcustom gnus-sum-thread-tree-indent "  "
4665   "With %B spec, used for indenting."
4666   :type 'string
4667   :group 'gnus-thread)
4668 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4669   "With %B spec, used for a leaf with brothers."
4670   :type 'string
4671   :group 'gnus-thread)
4672 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4673   "With %B spec, used for a leaf without brothers."
4674   :type 'string
4675   :group 'gnus-thread)
4676
4677 (defun gnus-summary-prepare-threads (threads)
4678   "Prepare summary buffer from THREADS and indentation LEVEL.
4679 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4680 or a straight list of headers."
4681   (gnus-message 7 "Generating summary...")
4682
4683   (setq gnus-newsgroup-threads threads)
4684   (beginning-of-line)
4685
4686   (let ((gnus-tmp-level 0)
4687         (default-score (or gnus-summary-default-score 0))
4688         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4689         (building-line-count gnus-summary-display-while-building)
4690         (building-count (integerp gnus-summary-display-while-building))
4691         thread number subject stack state gnus-tmp-gathered beg-match
4692         new-roots gnus-tmp-new-adopts thread-end simp-subject
4693         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4694         gnus-tmp-replied gnus-tmp-subject-or-nil
4695         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4696         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4697         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4698         tree-stack)
4699
4700     (setq gnus-tmp-prev-subject nil
4701           gnus-tmp-thread-tree-header-string "")
4702
4703     (if (vectorp (car threads))
4704         ;; If this is a straight (sic) list of headers, then a
4705         ;; threaded summary display isn't required, so we just create
4706         ;; an unthreaded one.
4707         (gnus-summary-prepare-unthreaded threads)
4708
4709       ;; Do the threaded display.
4710
4711       (if gnus-summary-display-while-building
4712           (switch-to-buffer (buffer-name)))
4713       (while (or threads stack gnus-tmp-new-adopts new-roots)
4714
4715         (if (and (= gnus-tmp-level 0)
4716                  (or (not stack)
4717                      (= (caar stack) 0))
4718                  (not gnus-tmp-false-parent)
4719                  (or gnus-tmp-new-adopts new-roots))
4720             (if gnus-tmp-new-adopts
4721                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4722                       thread (list (car gnus-tmp-new-adopts))
4723                       gnus-tmp-header (caar thread)
4724                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4725               (when new-roots
4726                 (setq thread (list (car new-roots))
4727                       gnus-tmp-header (caar thread)
4728                       new-roots (cdr new-roots))))
4729
4730           (if threads
4731               ;; If there are some threads, we do them before the
4732               ;; threads on the stack.
4733               (setq thread threads
4734                     gnus-tmp-header (caar thread))
4735             ;; There were no current threads, so we pop something off
4736             ;; the stack.
4737             (setq state (car stack)
4738                   gnus-tmp-level (car state)
4739                   tree-stack (cadr state)
4740                   thread (caddr state)
4741                   stack (cdr stack)
4742                   gnus-tmp-header (caar thread))))
4743
4744         (setq gnus-tmp-false-parent nil)
4745         (setq gnus-tmp-root-expunged nil)
4746         (setq thread-end nil)
4747
4748         (if (stringp gnus-tmp-header)
4749             ;; The header is a dummy root.
4750             (cond
4751              ((eq gnus-summary-make-false-root 'adopt)
4752               ;; We let the first article adopt the rest.
4753               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4754                                                (cddar thread)))
4755               (setq gnus-tmp-gathered
4756                     (nconc (mapcar
4757                             (lambda (h) (mail-header-number (car h)))
4758                             (cddar thread))
4759                            gnus-tmp-gathered))
4760               (setq thread (cons (list (caar thread)
4761                                        (cadar thread))
4762                                  (cdr thread)))
4763               (setq gnus-tmp-level -1
4764                     gnus-tmp-false-parent t))
4765              ((eq gnus-summary-make-false-root 'empty)
4766               ;; We print adopted articles with empty subject fields.
4767               (setq gnus-tmp-gathered
4768                     (nconc (mapcar
4769                             (lambda (h) (mail-header-number (car h)))
4770                             (cddar thread))
4771                            gnus-tmp-gathered))
4772               (setq gnus-tmp-level -1))
4773              ((eq gnus-summary-make-false-root 'dummy)
4774               ;; We remember that we probably want to output a dummy
4775               ;; root.
4776               (setq gnus-tmp-dummy-line gnus-tmp-header)
4777               (setq gnus-tmp-prev-subject gnus-tmp-header))
4778              (t
4779               ;; We do not make a root for the gathered
4780               ;; sub-threads at all.
4781               (setq gnus-tmp-level -1)))
4782
4783           (setq number (mail-header-number gnus-tmp-header)
4784                 subject (mail-header-subject gnus-tmp-header)
4785                 simp-subject (gnus-simplify-subject-fully subject))
4786
4787           (cond
4788            ;; If the thread has changed subject, we might want to make
4789            ;; this subthread into a root.
4790            ((and (null gnus-thread-ignore-subject)
4791                  (not (zerop gnus-tmp-level))
4792                  gnus-tmp-prev-subject
4793                  (not (string= gnus-tmp-prev-subject simp-subject)))
4794             (setq new-roots (nconc new-roots (list (car thread)))
4795                   thread-end t
4796                   gnus-tmp-header nil))
4797            ;; If the article lies outside the current limit,
4798            ;; then we do not display it.
4799            ((not (memq number gnus-newsgroup-limit))
4800             (setq gnus-tmp-gathered
4801                   (nconc (mapcar
4802                           (lambda (h) (mail-header-number (car h)))
4803                           (cdar thread))
4804                          gnus-tmp-gathered))
4805             (setq gnus-tmp-new-adopts (if (cdar thread)
4806                                           (append gnus-tmp-new-adopts
4807                                                   (cdar thread))
4808                                         gnus-tmp-new-adopts)
4809                   thread-end t
4810                   gnus-tmp-header nil)
4811             (when (zerop gnus-tmp-level)
4812               (setq gnus-tmp-root-expunged t)))
4813            ;; Perhaps this article is to be marked as read?
4814            ((and gnus-summary-mark-below
4815                  (< (or (cdr (assq number gnus-newsgroup-scored))
4816                         default-score)
4817                     gnus-summary-mark-below)
4818                  ;; Don't touch sparse articles.
4819                  (not (gnus-summary-article-sparse-p number))
4820                  (not (gnus-summary-article-ancient-p number)))
4821             (setq gnus-newsgroup-unreads
4822                   (delq number gnus-newsgroup-unreads))
4823             (if gnus-newsgroup-auto-expire
4824                 (setq gnus-newsgroup-expirable
4825                       (gnus-add-to-sorted-list
4826                        gnus-newsgroup-expirable number))
4827               (push (cons number gnus-low-score-mark)
4828                     gnus-newsgroup-reads))))
4829
4830           (when gnus-tmp-header
4831             ;; We may have an old dummy line to output before this
4832             ;; article.
4833             (when (and gnus-tmp-dummy-line
4834                        (gnus-subject-equal
4835                         gnus-tmp-dummy-line
4836                         (mail-header-subject gnus-tmp-header)))
4837               (gnus-summary-insert-dummy-line
4838                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4839               (setq gnus-tmp-dummy-line nil))
4840
4841             ;; Compute the mark.
4842             (setq gnus-tmp-unread (gnus-article-mark number))
4843
4844             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4845                                   gnus-tmp-header gnus-tmp-level)
4846                   gnus-newsgroup-data)
4847
4848             ;; Actually insert the line.
4849             (setq
4850              gnus-tmp-subject-or-nil
4851              (cond
4852               ((and gnus-thread-ignore-subject
4853                     gnus-tmp-prev-subject
4854                     (not (string= gnus-tmp-prev-subject simp-subject)))
4855                subject)
4856               ((zerop gnus-tmp-level)
4857                (if (and (eq gnus-summary-make-false-root 'empty)
4858                         (memq number gnus-tmp-gathered)
4859                         gnus-tmp-prev-subject
4860                         (string= gnus-tmp-prev-subject simp-subject))
4861                    gnus-summary-same-subject
4862                  subject))
4863               (t gnus-summary-same-subject)))
4864             (if (and (eq gnus-summary-make-false-root 'adopt)
4865                      (= gnus-tmp-level 1)
4866                      (memq number gnus-tmp-gathered))
4867                 (setq gnus-tmp-opening-bracket ?\<
4868                       gnus-tmp-closing-bracket ?\>)
4869               (setq gnus-tmp-opening-bracket ?\[
4870                     gnus-tmp-closing-bracket ?\]))
4871             (setq
4872              gnus-tmp-indentation
4873              (aref gnus-thread-indent-array gnus-tmp-level)
4874              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4875              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4876                                 gnus-summary-default-score 0)
4877              gnus-tmp-score-char
4878              (if (or (null gnus-summary-default-score)
4879                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4880                          gnus-summary-zcore-fuzz))
4881                  ?\ ;;;Whitespace
4882                (if (< gnus-tmp-score gnus-summary-default-score)
4883                    gnus-score-below-mark gnus-score-over-mark))
4884              gnus-tmp-replied
4885              (cond ((memq number gnus-newsgroup-processable)
4886                     gnus-process-mark)
4887                    ((memq number gnus-newsgroup-cached)
4888                     gnus-cached-mark)
4889                    ((memq number gnus-newsgroup-replied)
4890                     gnus-replied-mark)
4891                    ((memq number gnus-newsgroup-forwarded)
4892                     gnus-forwarded-mark)
4893                    ((memq number gnus-newsgroup-saved)
4894                     gnus-saved-mark)
4895                    ((memq number gnus-newsgroup-recent)
4896                     gnus-recent-mark)
4897                    ((memq number gnus-newsgroup-unseen)
4898                     gnus-unseen-mark)
4899                    (t gnus-no-mark))
4900              gnus-tmp-downloaded
4901              (cond ((memq number gnus-newsgroup-undownloaded)
4902                     gnus-undownloaded-mark)
4903                    (gnus-newsgroup-agentized
4904                     gnus-downloaded-mark)
4905                    (t
4906                     gnus-no-mark))
4907              gnus-tmp-from (mail-header-from gnus-tmp-header)
4908              gnus-tmp-name
4909              (cond
4910               ((string-match "<[^>]+> *$" gnus-tmp-from)
4911                (setq beg-match (match-beginning 0))
4912                (or (and (string-match "^\".+\"" gnus-tmp-from)
4913                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4914                    (substring gnus-tmp-from 0 beg-match)))
4915               ((string-match "(.+)" gnus-tmp-from)
4916                (substring gnus-tmp-from
4917                           (1+ (match-beginning 0)) (1- (match-end 0))))
4918               (t gnus-tmp-from))
4919
4920              ;; Do the %B string
4921              gnus-tmp-thread-tree-header-string
4922              (cond
4923               ((not gnus-show-threads) "")
4924               ((zerop gnus-tmp-level)
4925                (cond ((cdar thread)
4926                       (or gnus-sum-thread-tree-root subject))
4927                      (gnus-tmp-new-adopts
4928                       (or gnus-sum-thread-tree-false-root subject))
4929                      (t
4930                       (or gnus-sum-thread-tree-single-indent subject))))
4931               (t
4932                (concat (apply 'concat
4933                               (mapcar (lambda (item)
4934                                         (if (= item 1)
4935                                             gnus-sum-thread-tree-vertical
4936                                           gnus-sum-thread-tree-indent))
4937                                       (cdr (reverse tree-stack))))
4938                        (if (nth 1 thread)
4939                            gnus-sum-thread-tree-leaf-with-other
4940                          gnus-sum-thread-tree-single-leaf)))))
4941             (when (string= gnus-tmp-name "")
4942               (setq gnus-tmp-name gnus-tmp-from))
4943             (unless (numberp gnus-tmp-lines)
4944               (setq gnus-tmp-lines -1))
4945             (if (= gnus-tmp-lines -1)
4946                 (setq gnus-tmp-lines "?")
4947               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4948               (gnus-put-text-property
4949              (point)
4950              (progn (eval gnus-summary-line-format-spec) (point))
4951                'gnus-number number)
4952             (when gnus-visual-p
4953               (forward-line -1)
4954               (gnus-run-hooks 'gnus-summary-update-hook)
4955               (forward-line 1))
4956
4957             (setq gnus-tmp-prev-subject simp-subject)))
4958
4959         (when (nth 1 thread)
4960           (push (list (max 0 gnus-tmp-level)
4961                       (copy-sequence tree-stack)
4962                       (nthcdr 1 thread))
4963                 stack))
4964         (push (if (nth 1 thread) 1 0) tree-stack)
4965         (incf gnus-tmp-level)
4966         (setq threads (if thread-end nil (cdar thread)))
4967         (if gnus-summary-display-while-building
4968             (if building-count
4969                 (progn
4970                   ;; use a set frequency
4971                   (setq building-line-count (1- building-line-count))
4972                   (when (= building-line-count 0)
4973                     (sit-for 0)
4974                     (setq building-line-count
4975                           gnus-summary-display-while-building)))
4976               ;; always
4977               (sit-for 0)))
4978         (unless threads
4979           (setq gnus-tmp-level 0)))))
4980   (gnus-message 7 "Generating summary...done"))
4981
4982 (defun gnus-summary-prepare-unthreaded (headers)
4983   "Generate an unthreaded summary buffer based on HEADERS."
4984   (let (header number mark)
4985
4986     (beginning-of-line)
4987
4988     (while headers
4989       ;; We may have to root out some bad articles...
4990       (when (memq (setq number (mail-header-number
4991                                 (setq header (pop headers))))
4992                   gnus-newsgroup-limit)
4993         ;; Mark article as read when it has a low score.
4994         (when (and gnus-summary-mark-below
4995                    (< (or (cdr (assq number gnus-newsgroup-scored))
4996                           gnus-summary-default-score 0)
4997                       gnus-summary-mark-below)
4998                    (not (gnus-summary-article-ancient-p number)))
4999           (setq gnus-newsgroup-unreads
5000                 (delq number gnus-newsgroup-unreads))
5001           (if gnus-newsgroup-auto-expire
5002               (push number gnus-newsgroup-expirable)
5003             (push (cons number gnus-low-score-mark)
5004                   gnus-newsgroup-reads)))
5005
5006         (setq mark (gnus-article-mark number))
5007         (push (gnus-data-make number mark (1+ (point)) header 0)
5008               gnus-newsgroup-data)
5009         (gnus-summary-insert-line
5010          header 0 number
5011          (memq number gnus-newsgroup-undownloaded)
5012          mark (memq number gnus-newsgroup-replied)
5013          (memq number gnus-newsgroup-expirable)
5014          (mail-header-subject header) nil
5015          (cdr (assq number gnus-newsgroup-scored))
5016          (memq number gnus-newsgroup-processable))))))
5017
5018 (defun gnus-summary-remove-list-identifiers ()
5019   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5020   (let ((regexp (if (consp gnus-list-identifiers)
5021                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5022                   gnus-list-identifiers))
5023         changed subject)
5024     (when regexp
5025       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5026       (dolist (header gnus-newsgroup-headers)
5027         (setq subject (mail-header-subject header)
5028               changed nil)
5029         (while (string-match regexp subject)
5030           (setq subject
5031                 (concat (substring subject 0 (match-beginning 1))
5032                         (substring subject (match-end 0)))
5033                 changed t))
5034         (when changed
5035           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5036             (setq subject
5037                   (concat (substring subject 0 (match-beginning 1))
5038                           (substring subject (match-end 1)))))
5039           (mail-header-set-subject header subject))))))
5040
5041 (defun gnus-fetch-headers (articles)
5042   "Fetch headers of ARTICLES."
5043   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5044     (gnus-message 5 "Fetching headers for %s..." name)
5045     (prog1
5046         (if (eq 'nov
5047                 (setq gnus-headers-retrieved-by
5048                       (gnus-retrieve-headers
5049                        articles gnus-newsgroup-name
5050                        ;; We might want to fetch old headers, but
5051                        ;; not if there is only 1 article.
5052                        (and (or (and
5053                                  (not (eq gnus-fetch-old-headers 'some))
5054                                  (not (numberp gnus-fetch-old-headers)))
5055                                 (> (length articles) 1))
5056                             gnus-fetch-old-headers))))
5057             (gnus-get-newsgroup-headers-xover
5058              articles nil nil gnus-newsgroup-name t)
5059           (gnus-get-newsgroup-headers))
5060       (gnus-message 5 "Fetching headers for %s...done" name))))
5061
5062 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5063   "Select newsgroup GROUP.
5064 If READ-ALL is non-nil, all articles in the group are selected.
5065 If SELECT-ARTICLES, only select those articles from GROUP."
5066   (let* ((entry (gnus-group-entry group))
5067          ;;!!! Dirty hack; should be removed.
5068          (gnus-summary-ignore-duplicates
5069           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5070               t
5071             gnus-summary-ignore-duplicates))
5072          (info (nth 2 entry))
5073          articles fetched-articles cached)
5074
5075     (unless (gnus-check-server
5076              (set (make-local-variable 'gnus-current-select-method)
5077                   (gnus-find-method-for-group group)))
5078       (error "Couldn't open server"))
5079
5080     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5081         (gnus-activate-group group)     ; Or we can activate it...
5082         (progn                          ; Or we bug out.
5083           (when (equal major-mode 'gnus-summary-mode)
5084             (gnus-kill-buffer (current-buffer)))
5085           (error "Couldn't activate group %s: %s"
5086                  group (gnus-status-message group))))
5087
5088     (unless (gnus-request-group group t)
5089       (when (equal major-mode 'gnus-summary-mode)
5090         (gnus-kill-buffer (current-buffer)))
5091       (error "Couldn't request group %s: %s"
5092              group (gnus-status-message group)))
5093
5094     (when gnus-agent
5095       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5096       
5097       (setq gnus-summary-use-undownloaded-faces
5098             (gnus-agent-find-parameter
5099              group
5100              'agent-enable-undownloaded-faces)))
5101
5102     (setq gnus-newsgroup-name group
5103           gnus-newsgroup-unselected nil
5104           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5105
5106     (let ((display (gnus-group-find-parameter group 'display)))
5107       (setq gnus-newsgroup-display
5108             (cond
5109              ((not (zerop (or (car-safe read-all) 0)))
5110               ;; The user entered the group with C-u SPC/RET, let's show
5111               ;; all articles.
5112               'gnus-not-ignore)
5113              ((eq display 'all)
5114               'gnus-not-ignore)
5115              ((arrayp display)
5116               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5117              ((numberp display)
5118               ;; The following is probably the "correct" solution, but
5119               ;; it makes Gnus fetch all headers and then limit the
5120               ;; articles (which is slow), so instead we hack the
5121               ;; select-articles parameter instead. -- Simon Josefsson
5122               ;; <jas@kth.se>
5123               ;;
5124               ;; (gnus-byte-compile
5125               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5126               ;;                         display)))))
5127               (setq select-articles
5128                     (gnus-uncompress-range
5129                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5130                              (if (> tmp 0)
5131                                  tmp
5132                                1))
5133                            (cdr (gnus-active group)))))
5134               nil)
5135              (t
5136               nil))))
5137
5138     (gnus-summary-setup-default-charset)
5139
5140     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5141     (when (gnus-virtual-group-p group)
5142       (setq cached gnus-newsgroup-cached))
5143
5144     (setq gnus-newsgroup-unreads
5145           (gnus-sorted-ndifference
5146            (gnus-sorted-ndifference gnus-newsgroup-unreads
5147                                     gnus-newsgroup-marked)
5148            gnus-newsgroup-dormant))
5149
5150     (setq gnus-newsgroup-processable nil)
5151
5152     (gnus-update-read-articles group gnus-newsgroup-unreads)
5153
5154     ;; Adjust and set lists of article marks.
5155     (when info
5156       (gnus-adjust-marked-articles info))
5157     (if (setq articles select-articles)
5158         (setq gnus-newsgroup-unselected
5159               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5160       (setq articles (gnus-articles-to-read group read-all)))
5161
5162     (cond
5163      ((null articles)
5164       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5165       'quit)
5166      ((eq articles 0) nil)
5167      (t
5168       ;; Init the dependencies hash table.
5169       (setq gnus-newsgroup-dependencies
5170             (gnus-make-hashtable (length articles)))
5171       (gnus-set-global-variables)
5172       ;; Retrieve the headers and read them in.
5173
5174       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5175
5176       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5177       (when cached
5178         (setq gnus-newsgroup-cached cached))
5179
5180       ;; Suppress duplicates?
5181       (when gnus-suppress-duplicates
5182         (gnus-dup-suppress-articles))
5183
5184       ;; Set the initial limit.
5185       (setq gnus-newsgroup-limit (copy-sequence articles))
5186       ;; Remove canceled articles from the list of unread articles.
5187       (setq fetched-articles
5188             (mapcar (lambda (headers) (mail-header-number headers))
5189                     gnus-newsgroup-headers))
5190       (setq gnus-newsgroup-articles fetched-articles)
5191       (setq gnus-newsgroup-unreads
5192             (gnus-sorted-nintersection
5193              gnus-newsgroup-unreads fetched-articles))
5194       (gnus-compute-unseen-list)
5195
5196       ;; Removed marked articles that do not exist.
5197       (gnus-update-missing-marks
5198        (gnus-sorted-difference articles fetched-articles))
5199       ;; We might want to build some more threads first.
5200       (when (and gnus-fetch-old-headers
5201                  (eq gnus-headers-retrieved-by 'nov))
5202         (if (eq gnus-fetch-old-headers 'invisible)
5203             (gnus-build-all-threads)
5204           (gnus-build-old-threads)))
5205       ;; Let the Gnus agent mark articles as read.
5206       (when gnus-agent
5207         (gnus-agent-get-undownloaded-list))
5208       ;; Remove list identifiers from subject
5209       (when gnus-list-identifiers
5210         (gnus-summary-remove-list-identifiers))
5211       ;; Check whether auto-expire is to be done in this group.
5212       (setq gnus-newsgroup-auto-expire
5213             (gnus-group-auto-expirable-p group))
5214       ;; Set up the article buffer now, if necessary.
5215       (unless gnus-single-article-buffer
5216         (gnus-article-setup-buffer))
5217       ;; First and last article in this newsgroup.
5218       (when gnus-newsgroup-headers
5219         (setq gnus-newsgroup-begin
5220               (mail-header-number (car gnus-newsgroup-headers))
5221               gnus-newsgroup-end
5222               (mail-header-number
5223                (gnus-last-element gnus-newsgroup-headers))))
5224       ;; GROUP is successfully selected.
5225       (or gnus-newsgroup-headers t)))))
5226
5227 (defun gnus-compute-unseen-list ()
5228   ;; The `seen' marks are treated specially.
5229   (if (not gnus-newsgroup-seen)
5230       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5231     (setq gnus-newsgroup-unseen
5232           (gnus-inverse-list-range-intersection
5233            gnus-newsgroup-articles gnus-newsgroup-seen))))
5234
5235 (defun gnus-summary-display-make-predicate (display)
5236   (require 'gnus-agent)
5237   (when (= (length display) 1)
5238     (setq display (car display)))
5239   (unless gnus-summary-display-cache
5240     (dolist (elem (append '((unread . unread)
5241                             (read . read)
5242                             (unseen . unseen))
5243                           gnus-article-mark-lists))
5244       (push (cons (cdr elem)
5245                   (gnus-byte-compile
5246                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5247             gnus-summary-display-cache)))
5248   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5249         (gnus-category-predicate-cache gnus-summary-display-cache))
5250     (gnus-get-predicate display)))
5251
5252 ;; Uses the dynamically bound `number' variable.
5253 (eval-when-compile
5254   (defvar number))
5255 (defun gnus-article-marked-p (type &optional article)
5256   (let ((article (or article number)))
5257     (cond
5258      ((eq type 'tick)
5259       (memq article gnus-newsgroup-marked))
5260      ((eq type 'spam)
5261       (memq article gnus-newsgroup-spam-marked))
5262      ((eq type 'unsend)
5263       (memq article gnus-newsgroup-unsendable))
5264      ((eq type 'undownload)
5265       (memq article gnus-newsgroup-undownloaded))
5266      ((eq type 'download)
5267       (memq article gnus-newsgroup-downloadable))
5268      ((eq type 'unread)
5269       (memq article gnus-newsgroup-unreads))
5270      ((eq type 'read)
5271       (memq article gnus-newsgroup-reads))
5272      ((eq type 'dormant)
5273       (memq article gnus-newsgroup-dormant) )
5274      ((eq type 'expire)
5275       (memq article gnus-newsgroup-expirable))
5276      ((eq type 'reply)
5277       (memq article gnus-newsgroup-replied))
5278      ((eq type 'killed)
5279       (memq article gnus-newsgroup-killed))
5280      ((eq type 'bookmark)
5281       (assq article gnus-newsgroup-bookmarks))
5282      ((eq type 'score)
5283       (assq article gnus-newsgroup-scored))
5284      ((eq type 'save)
5285       (memq article gnus-newsgroup-saved))
5286      ((eq type 'cache)
5287       (memq article gnus-newsgroup-cached))
5288      ((eq type 'forward)
5289       (memq article gnus-newsgroup-forwarded))
5290      ((eq type 'seen)
5291       (not (memq article gnus-newsgroup-unseen)))
5292      ((eq type 'recent)
5293       (memq article gnus-newsgroup-recent))
5294      (t t))))
5295
5296 (defun gnus-articles-to-read (group &optional read-all)
5297   "Find out what articles the user wants to read."
5298   (let* ((articles
5299           ;; Select all articles if `read-all' is non-nil, or if there
5300           ;; are no unread articles.
5301           (if (or read-all
5302                   (and (zerop (length gnus-newsgroup-marked))
5303                        (zerop (length gnus-newsgroup-unreads)))
5304                   ;; Fetch all if the predicate is non-nil.
5305                   gnus-newsgroup-display)
5306               ;; We want to select the headers for all the articles in
5307               ;; the group, so we select either all the active
5308               ;; articles in the group, or (if that's nil), the
5309               ;; articles in the cache.
5310               (or
5311                (gnus-uncompress-range (gnus-active group))
5312                (gnus-cache-articles-in-group group))
5313             ;; Select only the "normal" subset of articles.
5314             (gnus-sorted-nunion
5315              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5316              gnus-newsgroup-unreads)))
5317          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5318          (scored (length scored-list))
5319          (number (length articles))
5320          (marked (+ (length gnus-newsgroup-marked)
5321                     (length gnus-newsgroup-dormant)))
5322          (select
5323           (cond
5324            ((numberp read-all)
5325             read-all)
5326            ((numberp gnus-newsgroup-display)
5327             gnus-newsgroup-display)
5328            (t
5329             (condition-case ()
5330                 (cond
5331                  ((and (or (<= scored marked) (= scored number))
5332                        (numberp gnus-large-newsgroup)
5333                        (> number gnus-large-newsgroup))
5334                   (let* ((cursor-in-echo-area nil)
5335                          (initial (gnus-parameter-large-newsgroup-initial
5336                                    gnus-newsgroup-name))
5337                          (input
5338                           (read-string
5339                            (format
5340                             "How many articles from %s (%s %d): "
5341                             (gnus-limit-string
5342                              (gnus-group-decoded-name gnus-newsgroup-name)
5343                              35)
5344                             (if initial "max" "default")
5345                             number)
5346                            (if initial
5347                                (cons (number-to-string initial)
5348                                      0)))))
5349                     (if (string-match "^[ \t]*$" input) number input)))
5350                  ((and (> scored marked) (< scored number)
5351                        (> (- scored number) 20))
5352                   (let ((input
5353                          (read-string
5354                           (format "%s %s (%d scored, %d total): "
5355                                   "How many articles from"
5356                                   (gnus-group-decoded-name group)
5357                                   scored number))))
5358                     (if (string-match "^[ \t]*$" input)
5359                         number input)))
5360                  (t number))
5361               (quit
5362                (message "Quit getting the articles to read")
5363                nil))))))
5364     (setq select (if (stringp select) (string-to-number select) select))
5365     (if (or (null select) (zerop select))
5366         select
5367       (if (and (not (zerop scored)) (<= (abs select) scored))
5368           (progn
5369             (setq articles (sort scored-list '<))
5370             (setq number (length articles)))
5371         (setq articles (copy-sequence articles)))
5372
5373       (when (< (abs select) number)
5374         (if (< select 0)
5375             ;; Select the N oldest articles.
5376             (setcdr (nthcdr (1- (abs select)) articles) nil)
5377           ;; Select the N most recent articles.
5378           (setq articles (nthcdr (- number select) articles))))
5379       (setq gnus-newsgroup-unselected
5380             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5381       (when gnus-alter-articles-to-read-function
5382         (setq articles
5383               (sort
5384                (funcall gnus-alter-articles-to-read-function
5385                         gnus-newsgroup-name articles)
5386                '<)))
5387       articles)))
5388
5389 (defun gnus-killed-articles (killed articles)
5390   (let (out)
5391     (while articles
5392       (when (inline (gnus-member-of-range (car articles) killed))
5393         (push (car articles) out))
5394       (setq articles (cdr articles)))
5395     out))
5396
5397 (defun gnus-uncompress-marks (marks)
5398   "Uncompress the mark ranges in MARKS."
5399   (let ((uncompressed '(score bookmark))
5400         out)
5401     (while marks
5402       (if (memq (caar marks) uncompressed)
5403           (push (car marks) out)
5404         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5405       (setq marks (cdr marks)))
5406     out))
5407
5408 (defun gnus-article-mark-to-type (mark)
5409   "Return the type of MARK."
5410   (or (cadr (assq mark gnus-article-special-mark-lists))
5411       'list))
5412
5413 (defun gnus-article-unpropagatable-p (mark)
5414   "Return whether MARK should be propagated to back end."
5415   (memq mark gnus-article-unpropagated-mark-lists))
5416
5417 (defun gnus-adjust-marked-articles (info)
5418   "Set all article lists and remove all marks that are no longer valid."
5419   (let* ((marked-lists (gnus-info-marks info))
5420          (active (gnus-active (gnus-info-group info)))
5421          (min (car active))
5422          (max (cdr active))
5423          (types gnus-article-mark-lists)
5424          marks var articles article mark mark-type
5425          bgn end)
5426
5427     (dolist (marks marked-lists)
5428       (setq mark (car marks)
5429             mark-type (gnus-article-mark-to-type mark)
5430             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5431
5432       ;; We set the variable according to the type of the marks list,
5433       ;; and then adjust the marks to a subset of the active articles.
5434       (cond
5435        ;; Adjust "simple" lists - compressed yet unsorted
5436        ((eq mark-type 'list)
5437         ;; Simultaneously uncompress and clip to active range
5438         ;; See gnus-uncompress-range for a description of possible marks
5439         (let (l lh)
5440           (if (not (cadr marks))
5441               (set var nil)
5442             (setq articles (if (numberp (cddr marks))
5443                                (list (cdr marks))
5444                              (cdr marks))
5445                   lh (cons nil nil)
5446                   l lh)
5447
5448             (while (setq article (pop articles))
5449               (cond ((consp article)
5450                      (setq bgn (max (car article) min)
5451                            end (min (cdr article) max))
5452                      (while (<= bgn end)
5453                        (setq l (setcdr l (cons bgn nil))
5454                              bgn (1+ bgn))))
5455                     ((and (<= min article)
5456                           (>= max article))
5457                      (setq l (setcdr l (cons article nil))))))
5458             (set var (cdr lh)))))
5459        ;; Adjust assocs.
5460        ((eq mark-type 'tuple)
5461         (set var (setq articles (cdr marks)))
5462         (when (not (listp (cdr (symbol-value var))))
5463           (set var (list (symbol-value var))))
5464         (when (not (listp (cdr articles)))
5465           (setq articles (list articles)))
5466         (while articles
5467           (when (or (not (consp (setq article (pop articles))))
5468                     (< (car article) min)
5469                     (> (car article) max))
5470             (set var (delq article (symbol-value var))))))
5471        ;; Adjust ranges (sloppily).
5472        ((eq mark-type 'range)
5473         (cond
5474          ((eq mark 'seen)
5475           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5476           ;; It should be (seen (NUM1 . NUM2)).
5477           (when (numberp (cddr marks))
5478             (setcdr marks (list (cdr marks))))
5479           (setq articles (cdr marks))
5480           (while (and articles
5481                       (or (and (consp (car articles))
5482                                (> min (cdar articles)))
5483                           (and (numberp (car articles))
5484                                (> min (car articles)))))
5485             (pop articles))
5486           (set var articles))))))))
5487
5488 (defun gnus-update-missing-marks (missing)
5489   "Go through the list of MISSING articles and remove them from the mark lists."
5490   (when missing
5491     (let (var m)
5492       ;; Go through all types.
5493       (dolist (elem gnus-article-mark-lists)
5494         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5495           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5496           (when (symbol-value var)
5497             ;; This list has articles.  So we delete all missing
5498             ;; articles from it.
5499             (setq m missing)
5500             (while m
5501               (set var (delq (pop m) (symbol-value var))))))))))
5502
5503 (defun gnus-update-marks ()
5504   "Enter the various lists of marked articles into the newsgroup info list."
5505   (let ((types gnus-article-mark-lists)
5506         (info (gnus-get-info gnus-newsgroup-name))
5507         type list newmarked symbol delta-marks)
5508     (when info
5509       ;; Add all marks lists to the list of marks lists.
5510       (while (setq type (pop types))
5511         (setq list (symbol-value
5512                     (setq symbol
5513                           (intern (format "gnus-newsgroup-%s" (car type))))))
5514
5515         (when list
5516           ;; Get rid of the entries of the articles that have the
5517           ;; default score.
5518           (when (and (eq (cdr type) 'score)
5519                      gnus-save-score
5520                      list)
5521             (let* ((arts list)
5522                    (prev (cons nil list))
5523                    (all prev))
5524               (while arts
5525                 (if (or (not (consp (car arts)))
5526                         (= (cdar arts) gnus-summary-default-score))
5527                     (setcdr prev (cdr arts))
5528                   (setq prev arts))
5529                 (setq arts (cdr arts)))
5530               (setq list (cdr all)))))
5531
5532         (when (eq (cdr type) 'seen)
5533           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5534
5535         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5536           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5537
5538         (when (and (gnus-check-backend-function
5539                     'request-set-mark gnus-newsgroup-name)
5540                    (not (gnus-article-unpropagatable-p (cdr type))))
5541           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5542                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5543                  (add (gnus-remove-from-range
5544                        (gnus-copy-sequence list) old)))
5545             (when add
5546               (push (list add 'add (list (cdr type))) delta-marks))
5547             (when del
5548               (push (list del 'del (list (cdr type))) delta-marks))))
5549
5550         (when list
5551           (push (cons (cdr type) list) newmarked)))
5552
5553       (when delta-marks
5554         (unless (gnus-check-group gnus-newsgroup-name)
5555           (error "Can't open server for %s" gnus-newsgroup-name))
5556         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5557
5558       ;; Enter these new marks into the info of the group.
5559       (if (nthcdr 3 info)
5560           (setcar (nthcdr 3 info) newmarked)
5561         ;; Add the marks lists to the end of the info.
5562         (when newmarked
5563           (setcdr (nthcdr 2 info) (list newmarked))))
5564
5565       ;; Cut off the end of the info if there's nothing else there.
5566       (let ((i 5))
5567         (while (and (> i 2)
5568                     (not (nth i info)))
5569           (when (nthcdr (decf i) info)
5570             (setcdr (nthcdr i info) nil)))))))
5571
5572 (defun gnus-set-mode-line (where)
5573   "Set the mode line of the article or summary buffers.
5574 If WHERE is `summary', the summary mode line format will be used."
5575   ;; Is this mode line one we keep updated?
5576   (when (and (memq where gnus-updated-mode-lines)
5577              (symbol-value
5578               (intern (format "gnus-%s-mode-line-format-spec" where))))
5579     (let (mode-string)
5580       (save-excursion
5581         ;; We evaluate this in the summary buffer since these
5582         ;; variables are buffer-local to that buffer.
5583         (set-buffer gnus-summary-buffer)
5584         ;; We bind all these variables that are used in the `eval' form
5585         ;; below.
5586         (let* ((mformat (symbol-value
5587                          (intern
5588                           (format "gnus-%s-mode-line-format-spec" where))))
5589                (gnus-tmp-group-name (gnus-group-decoded-name
5590                                      gnus-newsgroup-name))
5591                (gnus-tmp-article-number (or gnus-current-article 0))
5592                (gnus-tmp-unread gnus-newsgroup-unreads)
5593                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5594                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5595                (gnus-tmp-unread-and-unselected
5596                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5597                             (zerop gnus-tmp-unselected))
5598                        "")
5599                       ((zerop gnus-tmp-unselected)
5600                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5601                       (t (format "{%d(+%d) more}"
5602                                  gnus-tmp-unread-and-unticked
5603                                  gnus-tmp-unselected))))
5604                (gnus-tmp-subject
5605                 (if (and gnus-current-headers
5606                          (vectorp gnus-current-headers))
5607                     (gnus-mode-string-quote
5608                      (mail-header-subject gnus-current-headers))
5609                   ""))
5610                bufname-length max-len
5611                gnus-tmp-header);; passed as argument to any user-format-funcs
5612           (setq mode-string (eval mformat))
5613           (setq bufname-length (if (string-match "%b" mode-string)
5614                                    (- (length
5615                                        (buffer-name
5616                                         (if (eq where 'summary)
5617                                             nil
5618                                           (get-buffer gnus-article-buffer))))
5619                                       2)
5620                                  0))
5621           (setq max-len (max 4 (if gnus-mode-non-string-length
5622                                    (- (window-width)
5623                                       gnus-mode-non-string-length
5624                                       bufname-length)
5625                                  (length mode-string))))
5626           ;; We might have to chop a bit of the string off...
5627           (when (> (length mode-string) max-len)
5628             (setq mode-string
5629                   (concat (gnus-truncate-string mode-string (- max-len 3))
5630                           "...")))
5631           ;; Pad the mode string a bit.
5632           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5633       ;; Update the mode line.
5634       (setq mode-line-buffer-identification
5635             (gnus-mode-line-buffer-identification (list mode-string)))
5636       (set-buffer-modified-p t))))
5637
5638 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5639   "Go through the HEADERS list and add all Xrefs to a hash table.
5640 The resulting hash table is returned, or nil if no Xrefs were found."
5641   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5642          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5643          (xref-hashtb (gnus-make-hashtable))
5644          start group entry number xrefs header)
5645     (while headers
5646       (setq header (pop headers))
5647       (when (and (setq xrefs (mail-header-xref header))
5648                  (not (memq (setq number (mail-header-number header))
5649                             unreads)))
5650         (setq start 0)
5651         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5652           (setq start (match-end 0))
5653           (setq group (if prefix
5654                           (concat prefix (substring xrefs (match-beginning 1)
5655                                                     (match-end 1)))
5656                         (substring xrefs (match-beginning 1) (match-end 1))))
5657           (setq number
5658                 (string-to-int (substring xrefs (match-beginning 2)
5659                                           (match-end 2))))
5660           (if (setq entry (gnus-gethash group xref-hashtb))
5661               (setcdr entry (cons number (cdr entry)))
5662             (gnus-sethash group (cons number nil) xref-hashtb)))))
5663     (and start xref-hashtb)))
5664
5665 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5666   "Look through all the headers and mark the Xrefs as read."
5667   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5668         name info xref-hashtb idlist method nth4)
5669     (save-excursion
5670       (set-buffer gnus-group-buffer)
5671       (when (setq xref-hashtb
5672                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5673         (mapatoms
5674          (lambda (group)
5675            (unless (string= from-newsgroup (setq name (symbol-name group)))
5676              (setq idlist (symbol-value group))
5677              ;; Dead groups are not updated.
5678              (and (prog1
5679                       (setq info (gnus-get-info name))
5680                     (when (stringp (setq nth4 (gnus-info-method info)))
5681                       (setq nth4 (gnus-server-to-method nth4))))
5682                   ;; Only do the xrefs if the group has the same
5683                   ;; select method as the group we have just read.
5684                   (or (gnus-methods-equal-p
5685                        nth4 (gnus-find-method-for-group from-newsgroup))
5686                       virtual
5687                       (equal nth4 (setq method (gnus-find-method-for-group
5688                                                 from-newsgroup)))
5689                       (and (equal (car nth4) (car method))
5690                            (equal (nth 1 nth4) (nth 1 method))))
5691                   gnus-use-cross-reference
5692                   (or (not (eq gnus-use-cross-reference t))
5693                       virtual
5694                       ;; Only do cross-references on subscribed
5695                       ;; groups, if that is what is wanted.
5696                       (<= (gnus-info-level info) gnus-level-subscribed))
5697                   (gnus-group-make-articles-read name idlist))))
5698          xref-hashtb)))))
5699
5700 (defun gnus-compute-read-articles (group articles)
5701   (let* ((entry (gnus-group-entry group))
5702          (info (nth 2 entry))
5703          (active (gnus-active group))
5704          ninfo)
5705     (when entry
5706       ;; First peel off all invalid article numbers.
5707       (when active
5708         (let ((ids articles)
5709               id first)
5710           (while (setq id (pop ids))
5711             (when (and first (> id (cdr active)))
5712               ;; We'll end up in this situation in one particular
5713               ;; obscure situation.  If you re-scan a group and get
5714               ;; a new article that is cross-posted to a different
5715               ;; group that has not been re-scanned, you might get
5716               ;; crossposted article that has a higher number than
5717               ;; Gnus believes possible.  So we re-activate this
5718               ;; group as well.  This might mean doing the
5719               ;; crossposting thingy will *increase* the number
5720               ;; of articles in some groups.  Tsk, tsk.
5721               (setq active (or (gnus-activate-group group) active)))
5722             (when (or (> id (cdr active))
5723                       (< id (car active)))
5724               (setq articles (delq id articles))))))
5725       ;; If the read list is nil, we init it.
5726       (if (and active
5727                (null (gnus-info-read info))
5728                (> (car active) 1))
5729           (setq ninfo (cons 1 (1- (car active))))
5730         (setq ninfo (gnus-info-read info)))
5731       ;; Then we add the read articles to the range.
5732       (gnus-add-to-range
5733        ninfo (setq articles (sort articles '<))))))
5734
5735 (defun gnus-group-make-articles-read (group articles)
5736   "Update the info of GROUP to say that ARTICLES are read."
5737   (let* ((num 0)
5738          (entry (gnus-group-entry group))
5739          (info (nth 2 entry))
5740          (active (gnus-active group))
5741          range)
5742     (when entry
5743       (setq range (gnus-compute-read-articles group articles))
5744       (with-current-buffer gnus-group-buffer
5745         (gnus-undo-register
5746           `(progn
5747              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5748              (gnus-info-set-read ',info ',(gnus-info-read info))
5749              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5750              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5751              (gnus-group-update-group ,group t))))
5752       ;; Add the read articles to the range.
5753       (gnus-info-set-read info range)
5754       (gnus-request-set-mark group (list (list range 'add '(read))))
5755       ;; Then we have to re-compute how many unread
5756       ;; articles there are in this group.
5757       (when active
5758         (cond
5759          ((not range)
5760           (setq num (- (1+ (cdr active)) (car active))))
5761          ((not (listp (cdr range)))
5762           (setq num (- (cdr active) (- (1+ (cdr range))
5763                                        (car range)))))
5764          (t
5765           (while range
5766             (if (numberp (car range))
5767                 (setq num (1+ num))
5768               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5769             (setq range (cdr range)))
5770           (setq num (- (cdr active) num))))
5771         ;; Update the number of unread articles.
5772         (setcar entry num)
5773         ;; Update the group buffer.
5774         (unless (gnus-ephemeral-group-p group)
5775           (gnus-group-update-group group t))))))
5776
5777 (defvar gnus-newsgroup-none-id 0)
5778
5779 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5780   (let ((cur nntp-server-buffer)
5781         (dependencies
5782          (or dependencies
5783              (with-current-buffer gnus-summary-buffer
5784                gnus-newsgroup-dependencies)))
5785         headers id end ref number
5786         (mail-parse-charset gnus-newsgroup-charset)
5787         (mail-parse-ignored-charsets
5788          (save-excursion (condition-case nil
5789                              (set-buffer gnus-summary-buffer)
5790                            (error))
5791                          gnus-newsgroup-ignored-charsets)))
5792     (save-excursion
5793       (set-buffer nntp-server-buffer)
5794       ;; Translate all TAB characters into SPACE characters.
5795       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5796       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5797       (ietf-drums-unfold-fws)
5798       (gnus-run-hooks 'gnus-parse-headers-hook)
5799       (let ((case-fold-search t)
5800             in-reply-to header p lines chars ctype)
5801         (goto-char (point-min))
5802         ;; Search to the beginning of the next header.  Error messages
5803         ;; do not begin with 2 or 3.
5804         (while (re-search-forward "^[23][0-9]+ " nil t)
5805           (setq id nil
5806                 ref nil)
5807           ;; This implementation of this function, with nine
5808           ;; search-forwards instead of the one re-search-forward and
5809           ;; a case (which basically was the old function) is actually
5810           ;; about twice as fast, even though it looks messier.  You
5811           ;; can't have everything, I guess.  Speed and elegance
5812           ;; doesn't always go hand in hand.
5813           (setq
5814            header
5815            (make-full-mail-header
5816             ;; Number.
5817             (prog1
5818                 (setq number (read cur))
5819               (end-of-line)
5820               (setq p (point))
5821               (narrow-to-region (point)
5822                                 (or (and (search-forward "\n.\n" nil t)
5823                                          (- (point) 2))
5824                                     (point))))
5825             ;; Subject.
5826             (progn
5827               (goto-char p)
5828               (if (search-forward "\nsubject:" nil t)
5829                   (nnheader-header-value)
5830                 "(none)"))
5831             ;; From.
5832             (progn
5833               (goto-char p)
5834               (if (search-forward "\nfrom:" nil t)
5835                   (nnheader-header-value)
5836                 "(nobody)"))
5837             ;; Date.
5838             (progn
5839               (goto-char p)
5840               (if (search-forward "\ndate:" nil t)
5841                   (nnheader-header-value) ""))
5842             ;; Message-ID.
5843             (progn
5844               (goto-char p)
5845               (setq id (if (re-search-forward
5846                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5847                            ;; We do it this way to make sure the Message-ID
5848                            ;; is (somewhat) syntactically valid.
5849                            (buffer-substring (match-beginning 1)
5850                                              (match-end 1))
5851                          ;; If there was no message-id, we just fake one
5852                          ;; to make subsequent routines simpler.
5853                          (nnheader-generate-fake-message-id number))))
5854             ;; References.
5855             (progn
5856               (goto-char p)
5857               (if (search-forward "\nreferences:" nil t)
5858                   (progn
5859                     (setq end (point))
5860                     (prog1
5861                         (nnheader-header-value)
5862                       (setq ref
5863                             (buffer-substring
5864                              (progn
5865                                ;; (end-of-line)
5866                                (search-backward ">" end t)
5867                                (1+ (point)))
5868                              (progn
5869                                (search-backward "<" end t)
5870                                (point))))))
5871                 ;; Get the references from the in-reply-to header if there
5872                 ;; were no references and the in-reply-to header looks
5873                 ;; promising.
5874                 (if (and (search-forward "\nin-reply-to:" nil t)
5875                          (setq in-reply-to (nnheader-header-value))
5876                          (string-match "<[^>]+>" in-reply-to))
5877                     (let (ref2)
5878                       (setq ref (substring in-reply-to (match-beginning 0)
5879                                            (match-end 0)))
5880                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5881                         (setq ref2 (substring in-reply-to (match-beginning 0)
5882                                               (match-end 0)))
5883                         (when (> (length ref2) (length ref))
5884                           (setq ref ref2)))
5885                       ref)
5886                   (setq ref nil))))
5887             ;; Chars.
5888             (progn
5889               (goto-char p)
5890               (if (search-forward "\nchars: " nil t)
5891                   (if (numberp (setq chars (ignore-errors (read cur))))
5892                       chars -1)
5893                 -1))
5894             ;; Lines.
5895             (progn
5896               (goto-char p)
5897               (if (search-forward "\nlines: " nil t)
5898                   (if (numberp (setq lines (ignore-errors (read cur))))
5899                       lines -1)
5900                 -1))
5901             ;; Xref.
5902             (progn
5903               (goto-char p)
5904               (and (search-forward "\nxref:" nil t)
5905                    (nnheader-header-value)))
5906             ;; Extra.
5907             (when gnus-extra-headers
5908               (let ((extra gnus-extra-headers)
5909                     out)
5910                 (while extra
5911                   (goto-char p)
5912                   (when (search-forward
5913                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5914                     (push (cons (car extra) (nnheader-header-value)) out))
5915                   (pop extra))
5916                 out))))
5917           (goto-char p)
5918           (if (and (search-forward "\ncontent-type: " nil t)
5919                    (setq ctype (nnheader-header-value)))
5920               (mime-entity-set-content-type-internal
5921                header (mime-parse-Content-Type ctype)))
5922           (when (equal id ref)
5923             (setq ref nil))
5924
5925           (when gnus-alter-header-function
5926             (funcall gnus-alter-header-function header)
5927             (setq id (mail-header-id header)
5928                   ref (gnus-parent-id (mail-header-references header))))
5929
5930           (when (setq header
5931                       (gnus-dependencies-add-header
5932                        header dependencies force-new))
5933             (push header headers))
5934           (goto-char (point-max))
5935           (widen))
5936         (nreverse headers)))))
5937
5938 ;; Goes through the xover lines and returns a list of vectors
5939 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5940                                                   force-new dependencies
5941                                                   group also-fetch-heads)
5942   "Parse the news overview data in the server buffer.
5943 Return a list of headers that match SEQUENCE (see
5944 `nntp-retrieve-headers')."
5945   ;; Get the Xref when the users reads the articles since most/some
5946   ;; NNTP servers do not include Xrefs when using XOVER.
5947   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5948   (let ((mail-parse-charset gnus-newsgroup-charset)
5949         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5950         (cur nntp-server-buffer)
5951         (dependencies (or dependencies gnus-newsgroup-dependencies))
5952         (allp (cond
5953                ((eq gnus-read-all-available-headers t)
5954                 t)
5955                ((stringp gnus-read-all-available-headers)
5956                 (string-match gnus-read-all-available-headers group))
5957                (t
5958                 nil)))
5959         number headers header)
5960     (save-excursion
5961       (set-buffer nntp-server-buffer)
5962       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5963       ;; Allow the user to mangle the headers before parsing them.
5964       (gnus-run-hooks 'gnus-parse-headers-hook)
5965       (goto-char (point-min))
5966       (gnus-parse-without-error
5967         (while (and (or sequence allp)
5968                     (not (eobp)))
5969           (setq number (read cur))
5970           (when (not allp)
5971             (while (and sequence
5972                         (< (car sequence) number))
5973               (setq sequence (cdr sequence))))
5974           (when (and (or allp
5975                          (and sequence
5976                               (eq number (car sequence))))
5977                      (progn
5978                        (setq sequence (cdr sequence))
5979                        (setq header (inline
5980                                       (gnus-nov-parse-line
5981                                        number dependencies force-new)))))
5982             (push header headers))
5983           (forward-line 1)))
5984       ;; A common bug in inn is that if you have posted an article and
5985       ;; then retrieves the active file, it will answer correctly --
5986       ;; the new article is included.  However, a NOV entry for the
5987       ;; article may not have been generated yet, so this may fail.
5988       ;; We work around this problem by retrieving the last few
5989       ;; headers using HEAD.
5990       (if (or (not also-fetch-heads)
5991               (not sequence))
5992           ;; We (probably) got all the headers.
5993           (nreverse headers)
5994         (let ((gnus-nov-is-evil t))
5995           (nconc
5996            (nreverse headers)
5997            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5998              (gnus-get-newsgroup-headers))))))))
5999
6000 (defun gnus-article-get-xrefs ()
6001   "Fill in the Xref value in `gnus-current-headers', if necessary.
6002 This is meant to be called in `gnus-article-internal-prepare-hook'."
6003   (let ((headers (with-current-buffer gnus-summary-buffer
6004                    gnus-current-headers)))
6005     (or (not gnus-use-cross-reference)
6006         (not headers)
6007         (and (mail-header-xref headers)
6008              (not (string= (mail-header-xref headers) "")))
6009         (let ((case-fold-search t)
6010               xref)
6011           (save-restriction
6012             (nnheader-narrow-to-headers)
6013             (goto-char (point-min))
6014             (when (or (and (not (eobp))
6015                            (eq (downcase (char-after)) ?x)
6016                            (looking-at "Xref:"))
6017                       (search-forward "\nXref:" nil t))
6018               (goto-char (1+ (match-end 0)))
6019               (setq xref (buffer-substring (point) (point-at-eol)))
6020               (mail-header-set-xref headers xref)))))))
6021
6022 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6023   "Find article ID and insert the summary line for that article.
6024 OLD-HEADER can either be a header or a line number to insert
6025 the subject line on."
6026   (let* ((line (and (numberp old-header) old-header))
6027          (old-header (and (vectorp old-header) old-header))
6028          (header (cond ((and old-header use-old-header)
6029                         old-header)
6030                        ((and (numberp id)
6031                              (gnus-number-to-header id))
6032                         (gnus-number-to-header id))
6033                        (t
6034                         (gnus-read-header id))))
6035          (number (and (numberp id) id))
6036          d)
6037     (when header
6038       ;; Rebuild the thread that this article is part of and go to the
6039       ;; article we have fetched.
6040       (when (and (not gnus-show-threads)
6041                  old-header)
6042         (when (and number
6043                    (setq d (gnus-data-find (mail-header-number old-header))))
6044           (goto-char (gnus-data-pos d))
6045           (gnus-data-remove
6046            number
6047            (- (point-at-bol)
6048               (prog1
6049                   (1+ (point-at-eol))
6050                 (gnus-delete-line))))))
6051       (when old-header
6052         (mail-header-set-number header (mail-header-number old-header)))
6053       (setq gnus-newsgroup-sparse
6054             (delq (setq number (mail-header-number header))
6055                   gnus-newsgroup-sparse))
6056       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6057       (push number gnus-newsgroup-limit)
6058       (gnus-rebuild-thread (mail-header-id header) line)
6059       (gnus-summary-goto-subject number nil t))
6060     (when (and (numberp number)
6061                (> number 0))
6062       ;; We have to update the boundaries even if we can't fetch the
6063       ;; article if ID is a number -- so that the next `P' or `N'
6064       ;; command will fetch the previous (or next) article even
6065       ;; if the one we tried to fetch this time has been canceled.
6066       (when (> number gnus-newsgroup-end)
6067         (setq gnus-newsgroup-end number))
6068       (when (< number gnus-newsgroup-begin)
6069         (setq gnus-newsgroup-begin number))
6070       (setq gnus-newsgroup-unselected
6071             (delq number gnus-newsgroup-unselected)))
6072     ;; Report back a success?
6073     (and header (mail-header-number header))))
6074
6075 ;;; Process/prefix in the summary buffer
6076
6077 (defun gnus-summary-work-articles (n)
6078   "Return a list of articles to be worked upon.
6079 The prefix argument, the list of process marked articles, and the
6080 current article will be taken into consideration."
6081   (save-excursion
6082     (set-buffer gnus-summary-buffer)
6083     (cond
6084      (n
6085       ;; A numerical prefix has been given.
6086       (setq n (prefix-numeric-value n))
6087       (let ((backward (< n 0))
6088             (n (abs (prefix-numeric-value n)))
6089             articles article)
6090         (save-excursion
6091           (while
6092               (and (> n 0)
6093                    (push (setq article (gnus-summary-article-number))
6094                          articles)
6095                    (if backward
6096                        (gnus-summary-find-prev nil article)
6097                      (gnus-summary-find-next nil article)))
6098             (decf n)))
6099         (nreverse articles)))
6100      ((and (gnus-region-active-p) (mark))
6101       (message "region active")
6102       ;; Work on the region between point and mark.
6103       (let ((max (max (point) (mark)))
6104             articles article)
6105         (save-excursion
6106           (goto-char (min (point) (mark)))
6107           (while
6108               (and
6109                (push (setq article (gnus-summary-article-number)) articles)
6110                (gnus-summary-find-next nil article)
6111                (< (point) max)))
6112           (nreverse articles))))
6113      (gnus-newsgroup-processable
6114       ;; There are process-marked articles present.
6115       ;; Save current state.
6116       (gnus-summary-save-process-mark)
6117       ;; Return the list.
6118       (reverse gnus-newsgroup-processable))
6119      (t
6120       ;; Just return the current article.
6121       (list (gnus-summary-article-number))))))
6122
6123 (defmacro gnus-summary-iterate (arg &rest forms)
6124   "Iterate over the process/prefixed articles and do FORMS.
6125 ARG is the interactive prefix given to the command.  FORMS will be
6126 executed with point over the summary line of the articles."
6127   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6128     `(let ((,articles (gnus-summary-work-articles ,arg)))
6129        (while ,articles
6130          (gnus-summary-goto-subject (car ,articles))
6131          ,@forms
6132          (pop ,articles)))))
6133
6134 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6135 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6136
6137 (defun gnus-summary-save-process-mark ()
6138   "Push the current set of process marked articles on the stack."
6139   (interactive)
6140   (push (copy-sequence gnus-newsgroup-processable)
6141         gnus-newsgroup-process-stack))
6142
6143 (defun gnus-summary-kill-process-mark ()
6144   "Push the current set of process marked articles on the stack and unmark."
6145   (interactive)
6146   (gnus-summary-save-process-mark)
6147   (gnus-summary-unmark-all-processable))
6148
6149 (defun gnus-summary-yank-process-mark ()
6150   "Pop the last process mark state off the stack and restore it."
6151   (interactive)
6152   (unless gnus-newsgroup-process-stack
6153     (error "Empty mark stack"))
6154   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6155
6156 (defun gnus-summary-process-mark-set (set)
6157   "Make SET into the current process marked articles."
6158   (gnus-summary-unmark-all-processable)
6159   (while set
6160     (gnus-summary-set-process-mark (pop set))))
6161
6162 ;;; Searching and stuff
6163
6164 (defun gnus-summary-search-group (&optional backward use-level)
6165   "Search for next unread newsgroup.
6166 If optional argument BACKWARD is non-nil, search backward instead."
6167   (save-excursion
6168     (set-buffer gnus-group-buffer)
6169     (when (gnus-group-search-forward
6170            backward nil (if use-level (gnus-group-group-level) nil))
6171       (gnus-group-group-name))))
6172
6173 (defun gnus-summary-best-group (&optional exclude-group)
6174   "Find the name of the best unread group.
6175 If EXCLUDE-GROUP, do not go to this group."
6176   (with-current-buffer gnus-group-buffer
6177     (save-excursion
6178       (gnus-group-best-unread-group exclude-group))))
6179
6180 (defun gnus-summary-find-next (&optional unread article backward)
6181   (if backward
6182       (gnus-summary-find-prev unread article)
6183     (let* ((dummy (gnus-summary-article-intangible-p))
6184            (article (or article (gnus-summary-article-number)))
6185            (data (gnus-data-find-list article))
6186            result)
6187       (when (and (not dummy)
6188                  (or (not gnus-summary-check-current)
6189                      (not unread)
6190                      (not (gnus-data-unread-p (car data)))))
6191         (setq data (cdr data)))
6192       (when (setq result
6193                   (if unread
6194                       (progn
6195                         (while data
6196                           (unless (memq (gnus-data-number (car data))
6197                                         (cond
6198                                          ((eq gnus-auto-goto-ignores
6199                                               'always-undownloaded)
6200                                           gnus-newsgroup-undownloaded)
6201                                          (gnus-plugged
6202                                           nil)
6203                                          ((eq gnus-auto-goto-ignores
6204                                               'unfetched)
6205                                           gnus-newsgroup-unfetched)
6206                                          ((eq gnus-auto-goto-ignores
6207                                               'undownloaded)
6208                                           gnus-newsgroup-undownloaded)))
6209                             (when (gnus-data-unread-p (car data))
6210                               (setq result (car data)
6211                                     data nil)))
6212                           (setq data (cdr data)))
6213                         result)
6214                     (car data)))
6215         (goto-char (gnus-data-pos result))
6216         (gnus-data-number result)))))
6217
6218 (defun gnus-summary-find-prev (&optional unread article)
6219   (let* ((eobp (eobp))
6220          (article (or article (gnus-summary-article-number)))
6221          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6222          result)
6223     (when (and (not eobp)
6224                (or (not gnus-summary-check-current)
6225                    (not unread)
6226                    (not (gnus-data-unread-p (car data)))))
6227       (setq data (cdr data)))
6228     (when (setq result
6229                 (if unread
6230                     (progn
6231                       (while data
6232                         (unless (memq (gnus-data-number (car data))
6233                                       (cond
6234                                        ((eq gnus-auto-goto-ignores
6235                                             'always-undownloaded)
6236                                         gnus-newsgroup-undownloaded)
6237                                        (gnus-plugged
6238                                         nil)
6239                                        ((eq gnus-auto-goto-ignores
6240                                             'unfetched)
6241                                         gnus-newsgroup-unfetched)
6242                                        ((eq gnus-auto-goto-ignores
6243                                             'undownloaded)
6244                                         gnus-newsgroup-undownloaded)))
6245                           (when (gnus-data-unread-p (car data))
6246                             (setq result (car data)
6247                                   data nil)))
6248                         (setq data (cdr data)))
6249                       result)
6250                   (car data)))
6251       (goto-char (gnus-data-pos result))
6252       (gnus-data-number result))))
6253
6254 (defun gnus-summary-find-subject (subject &optional unread backward article)
6255   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6256          (article (or article (gnus-summary-article-number)))
6257          (articles (gnus-data-list backward))
6258          (arts (gnus-data-find-list article articles))
6259          result)
6260     (when (or (not gnus-summary-check-current)
6261               (not unread)
6262               (not (gnus-data-unread-p (car arts))))
6263       (setq arts (cdr arts)))
6264     (while arts
6265       (and (or (not unread)
6266                (gnus-data-unread-p (car arts)))
6267            (vectorp (gnus-data-header (car arts)))
6268            (gnus-subject-equal
6269             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6270            (setq result (car arts)
6271                  arts nil))
6272       (setq arts (cdr arts)))
6273     (and result
6274          (goto-char (gnus-data-pos result))
6275          (gnus-data-number result))))
6276
6277 (defun gnus-summary-search-forward (&optional unread subject backward)
6278   "Search forward for an article.
6279 If UNREAD, look for unread articles.  If SUBJECT, look for
6280 articles with that subject.  If BACKWARD, search backward instead."
6281   (cond (subject (gnus-summary-find-subject subject unread backward))
6282         (backward (gnus-summary-find-prev unread))
6283         (t (gnus-summary-find-next unread))))
6284
6285 (defun gnus-recenter (&optional n)
6286   "Center point in window and redisplay frame.
6287 Also do horizontal recentering."
6288   (interactive "P")
6289   (when (and gnus-auto-center-summary
6290              (not (eq gnus-auto-center-summary 'vertical)))
6291     (gnus-horizontal-recenter))
6292   (recenter n))
6293
6294 (defun gnus-summary-recenter ()
6295   "Center point in the summary window.
6296 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6297 displayed, no centering will be performed."
6298   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6299   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6300   (interactive)
6301   ;; The user has to want it.
6302   (when gnus-auto-center-summary
6303     (let* ((top (cond ((< (window-height) 4) 0)
6304                       ((< (window-height) 7) 1)
6305                       (t (if (numberp gnus-auto-center-summary)
6306                              gnus-auto-center-summary
6307                            (/ (1- (window-height)) 2)))))
6308            (height (1- (window-height)))
6309            (bottom (save-excursion (goto-char (point-max))
6310                                    (forward-line (- height))
6311                                    (point)))
6312            (window (get-buffer-window (current-buffer))))
6313       (when (get-buffer-window gnus-article-buffer)
6314         ;; Only do recentering when the article buffer is displayed,
6315         ;; Set the window start to either `bottom', which is the biggest
6316         ;; possible valid number, or the second line from the top,
6317         ;; whichever is the least.
6318         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6319           (if (> bottom top-pos)
6320               ;; Keep the second line from the top visible
6321               (set-window-start window top-pos t)
6322             ;; Try to keep the bottom line visible; if it's partially
6323             ;; obscured, either scroll one more line to make it fully
6324             ;; visible, or revert to using TOP-POS.
6325             (save-excursion
6326               (goto-char (point-max))
6327               (forward-line -1)
6328               (let ((last-line-start (point)))
6329                 (goto-char bottom)
6330                 (set-window-start window (point) t)
6331                 (when (not (pos-visible-in-window-p last-line-start window))
6332                   (forward-line 1)
6333                   (set-window-start window (min (point) top-pos) t)))))))
6334       ;; Do horizontal recentering while we're at it.
6335       (when (and (get-buffer-window (current-buffer) t)
6336                  (not (eq gnus-auto-center-summary 'vertical)))
6337         (let ((selected (selected-window)))
6338           (select-window (get-buffer-window (current-buffer) t))
6339           (gnus-summary-position-point)
6340           (gnus-horizontal-recenter)
6341           (select-window selected))))))
6342
6343 (defun gnus-summary-jump-to-group (newsgroup)
6344   "Move point to NEWSGROUP in group mode buffer."
6345   ;; Keep update point of group mode buffer if visible.
6346   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6347       (save-window-excursion
6348         ;; Take care of tree window mode.
6349         (when (get-buffer-window gnus-group-buffer)
6350           (pop-to-buffer gnus-group-buffer))
6351         (gnus-group-jump-to-group newsgroup))
6352     (save-excursion
6353       ;; Take care of tree window mode.
6354       (if (get-buffer-window gnus-group-buffer)
6355           (pop-to-buffer gnus-group-buffer)
6356         (set-buffer gnus-group-buffer))
6357       (gnus-group-jump-to-group newsgroup))))
6358
6359 ;; This function returns a list of article numbers based on the
6360 ;; difference between the ranges of read articles in this group and
6361 ;; the range of active articles.
6362 (defun gnus-list-of-unread-articles (group)
6363   (let* ((read (gnus-info-read (gnus-get-info group)))
6364          (active (or (gnus-active group) (gnus-activate-group group)))
6365          (last (cdr active))
6366          first nlast unread)
6367     ;; If none are read, then all are unread.
6368     (if (not read)
6369         (setq first (car active))
6370       ;; If the range of read articles is a single range, then the
6371       ;; first unread article is the article after the last read
6372       ;; article.  Sounds logical, doesn't it?
6373       (if (and (not (listp (cdr read)))
6374                (or (< (car read) (car active))
6375                    (progn (setq read (list read))
6376                           nil)))
6377           (setq first (max (car active) (1+ (cdr read))))
6378         ;; `read' is a list of ranges.
6379         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6380                                   (caar read)))
6381                   1)
6382           (setq first (car active)))
6383         (while read
6384           (when first
6385             (while (< first nlast)
6386               (setq unread (cons first unread)
6387                     first (1+ first))))
6388           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6389           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6390           (setq read (cdr read)))))
6391     ;; And add the last unread articles.
6392     (while (<= first last)
6393       (setq unread (cons first unread)
6394             first (1+ first)))
6395     ;; Return the list of unread articles.
6396     (delq 0 (nreverse unread))))
6397
6398 (defun gnus-list-of-read-articles (group)
6399   "Return a list of unread, unticked and non-dormant articles."
6400   (let* ((info (gnus-get-info group))
6401          (marked (gnus-info-marks info))
6402          (active (gnus-active group)))
6403     (and info active
6404          (gnus-list-range-difference
6405           (gnus-list-range-difference
6406            (gnus-sorted-complement
6407             (gnus-uncompress-range active)
6408             (gnus-list-of-unread-articles group))
6409            (cdr (assq 'dormant marked)))
6410           (cdr (assq 'tick marked))))))
6411
6412 ;; This function returns a sequence of article numbers based on the
6413 ;; difference between the ranges of read articles in this group and
6414 ;; the range of active articles.
6415 (defun gnus-sequence-of-unread-articles (group)
6416   (let* ((read (gnus-info-read (gnus-get-info group)))
6417          (active (or (gnus-active group) (gnus-activate-group group)))
6418          (last (cdr active))
6419          first nlast unread)
6420     ;; If none are read, then all are unread.
6421     (if (not read)
6422         (setq first (car active))
6423       ;; If the range of read articles is a single range, then the
6424       ;; first unread article is the article after the last read
6425       ;; article.  Sounds logical, doesn't it?
6426       (if (and (not (listp (cdr read)))
6427                (or (< (car read) (car active))
6428                    (progn (setq read (list read))
6429                           nil)))
6430           (setq first (max (car active) (1+ (cdr read))))
6431         ;; `read' is a list of ranges.
6432         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6433                                   (caar read)))
6434                   1)
6435           (setq first (car active)))
6436         (while read
6437           (when first
6438             (push (cons first nlast) unread))
6439           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6440           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6441           (setq read (cdr read)))))
6442     ;; And add the last unread articles.
6443     (cond ((< first last)
6444            (push (cons first last) unread))
6445           ((= first last)
6446            (push first unread)))
6447     ;; Return the sequence of unread articles.
6448     (delq 0 (nreverse unread))))
6449
6450 ;; Various summary commands
6451
6452 (defun gnus-summary-select-article-buffer ()
6453   "Reconfigure windows to show article buffer."
6454   (interactive)
6455   (if (not (gnus-buffer-live-p gnus-article-buffer))
6456       (error "There is no article buffer for this summary buffer")
6457     (gnus-configure-windows 'article)
6458     (select-window (get-buffer-window gnus-article-buffer))))
6459
6460 (defun gnus-summary-universal-argument (arg)
6461   "Perform any operation on all articles that are process/prefixed."
6462   (interactive "P")
6463   (let ((articles (gnus-summary-work-articles arg))
6464         func article)
6465     (if (eq
6466          (setq
6467           func
6468           (key-binding
6469            (read-key-sequence
6470             (substitute-command-keys
6471              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6472          'undefined)
6473         (gnus-error 1 "Undefined key")
6474       (save-excursion
6475         (while articles
6476           (gnus-summary-goto-subject (setq article (pop articles)))
6477           (let (gnus-newsgroup-processable)
6478             (command-execute func))
6479           (gnus-summary-remove-process-mark article)))))
6480   (gnus-summary-position-point))
6481
6482 (defun gnus-summary-toggle-truncation (&optional arg)
6483   "Toggle truncation of summary lines.
6484 With ARG, turn line truncation on if ARG is positive."
6485   (interactive "P")
6486   (setq truncate-lines
6487         (if (null arg) (not truncate-lines)
6488           (> (prefix-numeric-value arg) 0)))
6489   (redraw-display))
6490
6491 (defun gnus-summary-find-for-reselect ()
6492   "Return the number of an article to stay on across a reselect.
6493 The current article is considered, then following articles, then previous
6494 articles.  An article is sought which is not cancelled and isn't a temporary
6495 insertion from another group.  If there's no such then return a dummy 0."
6496   (let (found)
6497     (dolist (rev '(nil t))
6498       (unless found      ; don't demand the reverse list if we don't need it
6499         (let ((data (gnus-data-find-list
6500                      (gnus-summary-article-number) (gnus-data-list rev))))
6501           (while (and data (not found))
6502             (if (and (< 0 (gnus-data-number (car data)))
6503                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6504                 (setq found (gnus-data-number (car data))))
6505             (setq data (cdr data))))))
6506     (or found 0)))
6507
6508 (defun gnus-summary-reselect-current-group (&optional all rescan)
6509   "Exit and then reselect the current newsgroup.
6510 The prefix argument ALL means to select all articles."
6511   (interactive "P")
6512   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6513     (error "Ephemeral groups can't be reselected"))
6514   (let ((current-subject (gnus-summary-find-for-reselect))
6515         (group gnus-newsgroup-name))
6516     (setq gnus-newsgroup-begin nil)
6517     (gnus-summary-exit nil 'leave-hidden)
6518     ;; We have to adjust the point of group mode buffer because
6519     ;; point was moved to the next unread newsgroup by exiting.
6520     (gnus-summary-jump-to-group group)
6521     (when rescan
6522       (save-excursion
6523         (save-window-excursion
6524           ;; Don't show group contents.
6525           (set-window-start (selected-window) (point-max))
6526           (gnus-group-get-new-news-this-group 1))))
6527     (gnus-group-read-group all t)
6528     (gnus-summary-goto-subject current-subject nil t)))
6529
6530 (defun gnus-summary-rescan-group (&optional all)
6531   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6532   (interactive "P")
6533   (gnus-summary-reselect-current-group all t))
6534
6535 (defun gnus-summary-update-info (&optional non-destructive)
6536   (save-excursion
6537     (let ((group gnus-newsgroup-name))
6538       (when group
6539         (when gnus-newsgroup-kill-headers
6540           (setq gnus-newsgroup-killed
6541                 (gnus-compress-sequence
6542                  (gnus-sorted-union
6543                   (gnus-list-range-intersection
6544                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6545                   gnus-newsgroup-unreads)
6546                  t)))
6547         (unless (listp (cdr gnus-newsgroup-killed))
6548           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6549         (let ((headers gnus-newsgroup-headers))
6550           ;; Set the new ranges of read articles.
6551           (with-current-buffer gnus-group-buffer
6552             (gnus-undo-force-boundary))
6553           (gnus-update-read-articles
6554            group (gnus-sorted-union
6555                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6556           ;; Set the current article marks.
6557           (let ((gnus-newsgroup-scored
6558                  (if (and (not gnus-save-score)
6559                           (not non-destructive))
6560                      nil
6561                    gnus-newsgroup-scored)))
6562             (save-excursion
6563               (gnus-update-marks)))
6564           ;; Do the cross-ref thing.
6565           (when gnus-use-cross-reference
6566             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6567           ;; Do not switch windows but change the buffer to work.
6568           (set-buffer gnus-group-buffer)
6569           (unless (gnus-ephemeral-group-p group)
6570             (gnus-group-update-group group)))))))
6571
6572 (defun gnus-summary-save-newsrc (&optional force)
6573   "Save the current number of read/marked articles in the dribble buffer.
6574 The dribble buffer will then be saved.
6575 If FORCE (the prefix), also save the .newsrc file(s)."
6576   (interactive "P")
6577   (gnus-summary-update-info t)
6578   (if force
6579       (gnus-save-newsrc-file)
6580     (gnus-dribble-save)))
6581
6582 (defun gnus-summary-exit (&optional temporary leave-hidden)
6583   "Exit reading current newsgroup, and then return to group selection mode.
6584 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6585   (interactive)
6586   (gnus-set-global-variables)
6587   (gnus-kill-save-kill-buffer)
6588   (gnus-async-halt-prefetch)
6589   (let* ((group gnus-newsgroup-name)
6590          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6591          (gnus-group-is-exiting-p t)
6592          (mode major-mode)
6593          (group-point nil)
6594          (buf (current-buffer)))
6595     (unless quit-config
6596       ;; Do adaptive scoring, and possibly save score files.
6597       (when gnus-newsgroup-adaptive
6598         (gnus-score-adaptive))
6599       (when gnus-use-scoring
6600         (gnus-score-save)))
6601     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6602     ;; If we have several article buffers, we kill them at exit.
6603     (unless gnus-single-article-buffer
6604       (gnus-kill-buffer gnus-original-article-buffer)
6605       (setq gnus-article-current nil))
6606     (when gnus-use-cache
6607       (gnus-cache-possibly-remove-articles)
6608       (gnus-cache-save-buffers))
6609     (gnus-async-prefetch-remove-group group)
6610     (when gnus-suppress-duplicates
6611       (gnus-dup-enter-articles))
6612     (when gnus-use-trees
6613       (gnus-tree-close group))
6614     (when gnus-use-cache
6615       (gnus-cache-write-active))
6616     ;; Remove entries for this group.
6617     (nnmail-purge-split-history (gnus-group-real-name group))
6618     ;; Make all changes in this group permanent.
6619     (unless quit-config
6620       (gnus-run-hooks 'gnus-exit-group-hook)
6621       (gnus-summary-update-info))
6622     (gnus-close-group group)
6623     ;; Make sure where we were, and go to next newsgroup.
6624     (set-buffer gnus-group-buffer)
6625     (unless quit-config
6626       (gnus-group-jump-to-group group))
6627     (gnus-run-hooks 'gnus-summary-exit-hook)
6628     (unless (or quit-config
6629                 ;; If this group has disappeared from the summary
6630                 ;; buffer, don't skip forwards.
6631                 (not (string= group (gnus-group-group-name))))
6632       (gnus-group-next-unread-group 1))
6633     (setq group-point (point))
6634     (if temporary
6635         nil                             ;Nothing to do.
6636       ;; If we have several article buffers, we kill them at exit.
6637       (unless gnus-single-article-buffer
6638         (gnus-kill-buffer gnus-article-buffer)
6639         (gnus-kill-buffer gnus-original-article-buffer)
6640         (setq gnus-article-current nil))
6641       (set-buffer buf)
6642       (if (not gnus-kill-summary-on-exit)
6643           (progn
6644             (gnus-deaden-summary)
6645             (setq mode nil))
6646         ;; We set all buffer-local variables to nil.  It is unclear why
6647         ;; this is needed, but if we don't, buffer-local variables are
6648         ;; not garbage-collected, it seems.  This would the lead to en
6649         ;; ever-growing Emacs.
6650         (gnus-summary-clear-local-variables)
6651         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6652           (gnus-summary-clear-local-variables))
6653         (when (get-buffer gnus-article-buffer)
6654           (bury-buffer gnus-article-buffer))
6655         ;; We clear the global counterparts of the buffer-local
6656         ;; variables as well, just to be on the safe side.
6657         (set-buffer gnus-group-buffer)
6658         (gnus-summary-clear-local-variables)
6659         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6660           (gnus-summary-clear-local-variables))
6661         ;; Return to group mode buffer.
6662         (when (eq mode 'gnus-summary-mode)
6663           (gnus-kill-buffer buf)))
6664       (setq gnus-current-select-method gnus-select-method)
6665       (if leave-hidden
6666           (set-buffer gnus-group-buffer)
6667         (pop-to-buffer gnus-group-buffer))
6668       (if (not quit-config)
6669           (progn
6670             (goto-char group-point)
6671             (unless leave-hidden
6672               (gnus-configure-windows 'group 'force))
6673             (unless (pos-visible-in-window-p)
6674               (forward-line (/ (static-if (featurep 'xemacs)
6675                                    (window-displayed-height)
6676                                  (1- (window-height)))
6677                                -2))
6678               (set-window-start (selected-window) (point))
6679               (goto-char group-point)))
6680         (gnus-handle-ephemeral-exit quit-config))
6681       ;; Clear the current group name.
6682       (unless quit-config
6683         (setq gnus-newsgroup-name nil)))))
6684
6685 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6686 (defun gnus-summary-exit-no-update (&optional no-questions)
6687   "Quit reading current newsgroup without updating read article info."
6688   (interactive)
6689   (let* ((group gnus-newsgroup-name)
6690          (gnus-group-is-exiting-p t)
6691          (gnus-group-is-exiting-without-update-p t)
6692          (quit-config (gnus-group-quit-config group)))
6693     (when (or no-questions
6694               gnus-expert-user
6695               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6696       (gnus-async-halt-prefetch)
6697       (run-hooks 'gnus-summary-prepare-exit-hook)
6698       ;; If we have several article buffers, we kill them at exit.
6699       (unless gnus-single-article-buffer
6700         (gnus-kill-buffer gnus-article-buffer)
6701         (gnus-kill-buffer gnus-original-article-buffer)
6702         (setq gnus-article-current nil))
6703       (if (not gnus-kill-summary-on-exit)
6704           (gnus-deaden-summary)
6705         (gnus-close-group group)
6706         (gnus-summary-clear-local-variables)
6707         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6708           (gnus-summary-clear-local-variables))
6709         (set-buffer gnus-group-buffer)
6710         (gnus-summary-clear-local-variables)
6711         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6712           (gnus-summary-clear-local-variables))
6713         (gnus-kill-buffer gnus-summary-buffer))
6714       (unless gnus-single-article-buffer
6715         (setq gnus-article-current nil))
6716       (when gnus-use-trees
6717         (gnus-tree-close group))
6718       (gnus-async-prefetch-remove-group group)
6719       (when (get-buffer gnus-article-buffer)
6720         (bury-buffer gnus-article-buffer))
6721       ;; Return to the group buffer.
6722       (gnus-configure-windows 'group 'force)
6723       ;; Clear the current group name.
6724       (setq gnus-newsgroup-name nil)
6725       (unless (gnus-ephemeral-group-p group)
6726         (gnus-group-update-group group))
6727       (when (equal (gnus-group-group-name) group)
6728         (gnus-group-next-unread-group 1))
6729       (when quit-config
6730         (gnus-handle-ephemeral-exit quit-config)))))
6731
6732 (defun gnus-handle-ephemeral-exit (quit-config)
6733   "Handle movement when leaving an ephemeral group.
6734 The state which existed when entering the ephemeral is reset."
6735   (if (not (buffer-name (car quit-config)))
6736       (gnus-configure-windows 'group 'force)
6737     (set-buffer (car quit-config))
6738     (cond ((eq major-mode 'gnus-summary-mode)
6739            (gnus-set-global-variables))
6740           ((eq major-mode 'gnus-article-mode)
6741            (save-excursion
6742              ;; The `gnus-summary-buffer' variable may point
6743              ;; to the old summary buffer when using a single
6744              ;; article buffer.
6745              (unless (gnus-buffer-live-p gnus-summary-buffer)
6746                (set-buffer gnus-group-buffer))
6747              (set-buffer gnus-summary-buffer)
6748              (gnus-set-global-variables))))
6749     (if (or (eq (cdr quit-config) 'article)
6750             (eq (cdr quit-config) 'pick))
6751         (progn
6752           ;; The current article may be from the ephemeral group
6753           ;; thus it is best that we reload this article
6754           ;;
6755           ;; If we're exiting from a large digest, this can be
6756           ;; extremely slow.  So, it's better not to reload it. -- jh.
6757           ;;(gnus-summary-show-article)
6758           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6759               (gnus-configure-windows 'pick 'force)
6760             (gnus-configure-windows (cdr quit-config) 'force)))
6761       (gnus-configure-windows (cdr quit-config) 'force))
6762     (when (eq major-mode 'gnus-summary-mode)
6763       (gnus-summary-next-subject 1 nil t)
6764       (gnus-summary-recenter)
6765       (gnus-summary-position-point))))
6766
6767 (defun gnus-summary-preview-mime-message ()
6768   "MIME decode and play this message."
6769   (interactive)
6770   (let ((gnus-break-pages nil)
6771         (gnus-show-mime t))
6772     (gnus-summary-select-article gnus-show-all-headers t))
6773   (let ((w (get-buffer-window gnus-article-buffer)))
6774     (when w
6775       (select-window (get-buffer-window gnus-article-buffer)))))
6776
6777 ;;; Dead summaries.
6778
6779 (defvar gnus-dead-summary-mode-map nil)
6780
6781 (unless gnus-dead-summary-mode-map
6782   (setq gnus-dead-summary-mode-map (make-keymap))
6783   (suppress-keymap gnus-dead-summary-mode-map)
6784   (substitute-key-definition
6785    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6786   (dolist (key '("\C-d" "\r" "\177" [delete]))
6787     (define-key gnus-dead-summary-mode-map
6788       key 'gnus-summary-wake-up-the-dead))
6789   (dolist (key '("q" "Q"))
6790     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6791
6792 (defvar gnus-dead-summary-mode nil
6793   "Minor mode for Gnus summary buffers.")
6794
6795 (defun gnus-dead-summary-mode (&optional arg)
6796   "Minor mode for Gnus summary buffers."
6797   (interactive "P")
6798   (when (eq major-mode 'gnus-summary-mode)
6799     (make-local-variable 'gnus-dead-summary-mode)
6800     (setq gnus-dead-summary-mode
6801           (if (null arg) (not gnus-dead-summary-mode)
6802             (> (prefix-numeric-value arg) 0)))
6803     (when gnus-dead-summary-mode
6804       (add-minor-mode
6805        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6806
6807 (defun gnus-deaden-summary ()
6808   "Make the current summary buffer into a dead summary buffer."
6809   ;; Kill any previous dead summary buffer.
6810   (when (and gnus-dead-summary
6811              (buffer-name gnus-dead-summary))
6812     (with-current-buffer gnus-dead-summary
6813       (when gnus-dead-summary-mode
6814         (kill-buffer (current-buffer)))))
6815   ;; Make this the current dead summary.
6816   (setq gnus-dead-summary (current-buffer))
6817   (gnus-dead-summary-mode 1)
6818   (let ((name (buffer-name)))
6819     (when (string-match "Summary" name)
6820       (rename-buffer
6821        (concat (substring name 0 (match-beginning 0)) "Dead "
6822                (substring name (match-beginning 0)))
6823        t)
6824       (bury-buffer))))
6825
6826 (defun gnus-kill-or-deaden-summary (buffer)
6827   "Kill or deaden the summary BUFFER."
6828   (save-excursion
6829     (when (and (buffer-name buffer)
6830                (not gnus-single-article-buffer))
6831       (with-current-buffer buffer
6832         (gnus-kill-buffer gnus-article-buffer)
6833         (gnus-kill-buffer gnus-original-article-buffer)))
6834     (cond
6835      ;; Kill the buffer.
6836      (gnus-kill-summary-on-exit
6837       (when (and gnus-use-trees
6838                  (gnus-buffer-exists-p buffer))
6839         (save-excursion
6840           (set-buffer buffer)
6841           (gnus-tree-close gnus-newsgroup-name)))
6842       (gnus-kill-buffer buffer))
6843      ;; Deaden the buffer.
6844      ((gnus-buffer-exists-p buffer)
6845       (save-excursion
6846         (set-buffer buffer)
6847         (gnus-deaden-summary))))))
6848
6849 (defun gnus-summary-wake-up-the-dead (&rest args)
6850   "Wake up the dead summary buffer."
6851   (interactive)
6852   (gnus-dead-summary-mode -1)
6853   (let ((name (buffer-name)))
6854     (when (string-match "Dead " name)
6855       (rename-buffer
6856        (concat (substring name 0 (match-beginning 0))
6857                (substring name (match-end 0)))
6858        t)))
6859   (gnus-message 3 "This dead summary is now alive again"))
6860
6861 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6862 (defun gnus-summary-fetch-faq (&optional faq-dir)
6863   "Fetch the FAQ for the current group.
6864 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6865 in."
6866   (interactive
6867    (list
6868     (when current-prefix-arg
6869       (completing-read
6870        "FAQ dir: " (and (listp gnus-group-faq-directory)
6871                         (mapcar 'list
6872                                 gnus-group-faq-directory))))))
6873   (let (gnus-faq-buffer)
6874     (when (setq gnus-faq-buffer
6875                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6876       (gnus-configure-windows 'summary-faq))))
6877
6878 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6879 (defun gnus-summary-describe-group (&optional force)
6880   "Describe the current newsgroup."
6881   (interactive "P")
6882   (gnus-group-describe-group force gnus-newsgroup-name))
6883
6884 (defun gnus-summary-describe-briefly ()
6885   "Describe summary mode commands briefly."
6886   (interactive)
6887   (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")))
6888
6889 ;; Walking around group mode buffer from summary mode.
6890
6891 (defun gnus-summary-next-group (&optional no-article target-group backward)
6892   "Exit current newsgroup and then select next unread newsgroup.
6893 If prefix argument NO-ARTICLE is non-nil, no article is selected
6894 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6895 previous group instead."
6896   (interactive "P")
6897   ;; Stop pre-fetching.
6898   (gnus-async-halt-prefetch)
6899   (let ((current-group gnus-newsgroup-name)
6900         (current-buffer (current-buffer))
6901         entered)
6902     ;; First we semi-exit this group to update Xrefs and all variables.
6903     ;; We can't do a real exit, because the window conf must remain
6904     ;; the same in case the user is prompted for info, and we don't
6905     ;; want the window conf to change before that...
6906     (gnus-summary-exit t)
6907     (while (not entered)
6908       ;; Then we find what group we are supposed to enter.
6909       (set-buffer gnus-group-buffer)
6910       (gnus-group-jump-to-group current-group)
6911       (setq target-group
6912             (or target-group
6913                 (if (eq gnus-keep-same-level 'best)
6914                     (gnus-summary-best-group gnus-newsgroup-name)
6915                   (gnus-summary-search-group backward gnus-keep-same-level))))
6916       (if (not target-group)
6917           ;; There are no further groups, so we return to the group
6918           ;; buffer.
6919           (progn
6920             (gnus-message 5 "Returning to the group buffer")
6921             (setq entered t)
6922             (when (gnus-buffer-live-p current-buffer)
6923               (set-buffer current-buffer)
6924               (gnus-summary-exit))
6925             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6926         ;; We try to enter the target group.
6927         (gnus-group-jump-to-group target-group)
6928         (let ((unreads (gnus-group-group-unread)))
6929           (if (and (or (eq t unreads)
6930                        (and unreads (not (zerop unreads))))
6931                    (gnus-summary-read-group
6932                     target-group nil no-article
6933                     (and (buffer-name current-buffer) current-buffer)
6934                     nil backward))
6935               (setq entered t)
6936             (setq current-group target-group
6937                   target-group nil)))))))
6938
6939 (defun gnus-summary-prev-group (&optional no-article)
6940   "Exit current newsgroup and then select previous unread newsgroup.
6941 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6942   (interactive "P")
6943   (gnus-summary-next-group no-article nil t))
6944
6945 ;; Walking around summary lines.
6946
6947 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6948   "Go to the first subject satisfying any non-nil constraint.
6949 If UNREAD is non-nil, the article should be unread.
6950 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6951 If UNSEEN is non-nil, the article should be unseen.
6952 Returns the article selected or nil if there are no matching articles."
6953   (interactive "P")
6954   (cond
6955    ;; Empty summary.
6956    ((null gnus-newsgroup-data)
6957     (gnus-message 3 "No articles in the group")
6958     nil)
6959    ;; Pick the first article.
6960    ((not (or unread undownloaded unseen))
6961     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6962     (gnus-data-number (car gnus-newsgroup-data)))
6963    ;; Find the first unread article.
6964    (t
6965     (let ((data gnus-newsgroup-data))
6966       (while (and data
6967                   (let ((num (gnus-data-number (car data))))
6968                     (or (memq num gnus-newsgroup-unfetched)
6969                         (not (or (and unread
6970                                       (memq num gnus-newsgroup-unreads))
6971                                  (and undownloaded
6972                                       (memq num gnus-newsgroup-undownloaded))
6973                                  (and unseen
6974                                       (memq num gnus-newsgroup-unseen)))))))
6975         (setq data (cdr data)))
6976       (prog1
6977           (if data
6978               (progn
6979                 (goto-char (gnus-data-pos (car data)))
6980                 (gnus-data-number (car data)))
6981             (gnus-message 3 "No more%s articles"
6982                           (let* ((r (when unread " unread"))
6983                                  (d (when undownloaded " undownloaded"))
6984                                  (s (when unseen " unseen"))
6985                                  (l (delq nil (list r d s))))
6986                             (cond ((= 3 (length l))
6987                                    (concat r "," d ", or" s))
6988                                   ((= 2 (length l))
6989                                    (concat (car l) ", or" (cadr l)))
6990                                   ((= 1 (length l))
6991                                    (car l))
6992                                   (t
6993                                    ""))))
6994             nil
6995             )
6996         (gnus-summary-position-point))))))
6997
6998 (defun gnus-summary-next-subject (n &optional unread dont-display)
6999   "Go to next N'th summary line.
7000 If N is negative, go to the previous N'th subject line.
7001 If UNREAD is non-nil, only unread articles are selected.
7002 The difference between N and the actual number of steps taken is
7003 returned."
7004   (interactive "p")
7005   (let ((backward (< n 0))
7006         (n (abs n)))
7007     (while (and (> n 0)
7008                 (if backward
7009                     (gnus-summary-find-prev unread)
7010                   (gnus-summary-find-next unread)))
7011       (unless (zerop (setq n (1- n)))
7012         (gnus-summary-show-thread)))
7013     (when (/= 0 n)
7014       (gnus-message 7 "No more%s articles"
7015                     (if unread " unread" "")))
7016     (unless dont-display
7017       (gnus-summary-recenter)
7018       (gnus-summary-position-point))
7019     n))
7020
7021 (defun gnus-summary-next-unread-subject (n)
7022   "Go to next N'th unread summary line."
7023   (interactive "p")
7024   (gnus-summary-next-subject n t))
7025
7026 (defun gnus-summary-prev-subject (n &optional unread)
7027   "Go to previous N'th summary line.
7028 If optional argument UNREAD is non-nil, only unread article is selected."
7029   (interactive "p")
7030   (gnus-summary-next-subject (- n) unread))
7031
7032 (defun gnus-summary-prev-unread-subject (n)
7033   "Go to previous N'th unread summary line."
7034   (interactive "p")
7035   (gnus-summary-next-subject (- n) t))
7036
7037 (defun gnus-summary-goto-subjects (articles)
7038   "Insert the subject header for ARTICLES in the current buffer."
7039   (save-excursion
7040     (dolist (article articles)
7041       (gnus-summary-goto-subject article t)))
7042   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7043   (gnus-summary-position-point))
7044  
7045 (defun gnus-summary-goto-subject (article &optional force silent)
7046   "Go the subject line of ARTICLE.
7047 If FORCE, also allow jumping to articles not currently shown."
7048   (interactive "nArticle number: ")
7049   (unless (numberp article)
7050     (error "Article %s is not a number" article))
7051   (let ((b (point))
7052         (data (gnus-data-find article)))
7053     ;; We read in the article if we have to.
7054     (and (not data)
7055          force
7056          (gnus-summary-insert-subject
7057           article
7058           (if (or (numberp force) (vectorp force)) force)
7059           t)
7060          (setq data (gnus-data-find article)))
7061     (goto-char b)
7062     (if (not data)
7063         (progn
7064           (unless silent
7065             (gnus-message 3 "Can't find article %d" article))
7066           nil)
7067       (let ((pt (gnus-data-pos data)))
7068         (goto-char pt)
7069         (gnus-summary-set-article-display-arrow pt))
7070       (gnus-summary-position-point)
7071       article)))
7072
7073 ;; Walking around summary lines with displaying articles.
7074
7075 (defun gnus-summary-expand-window (&optional arg)
7076   "Make the summary buffer take up the entire Emacs frame.
7077 Given a prefix, will force an `article' buffer configuration."
7078   (interactive "P")
7079   (if arg
7080       (gnus-configure-windows 'article 'force)
7081     (gnus-configure-windows 'summary 'force)))
7082
7083 (defun gnus-summary-display-article (article &optional all-header)
7084   "Display ARTICLE in article buffer."
7085   (when (gnus-buffer-live-p gnus-article-buffer)
7086     (with-current-buffer gnus-article-buffer
7087       (set-buffer-multibyte t)))
7088   (gnus-set-global-variables)
7089   (when (gnus-buffer-live-p gnus-article-buffer)
7090     (with-current-buffer gnus-article-buffer
7091       (setq gnus-article-charset gnus-newsgroup-charset)
7092       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7093       (set-buffer-multibyte t)))
7094   (if (null article)
7095       nil
7096     (prog1
7097         (if gnus-summary-display-article-function
7098             (funcall gnus-summary-display-article-function article all-header)
7099           (gnus-article-prepare article all-header))
7100       (with-current-buffer gnus-article-buffer
7101         (set (make-local-variable 'gnus-summary-search-article-matched-data)
7102              nil))
7103       (gnus-run-hooks 'gnus-select-article-hook)
7104       (when (and gnus-current-article
7105                  (not (zerop gnus-current-article)))
7106         (gnus-summary-goto-subject gnus-current-article))
7107       (gnus-summary-recenter)
7108       (when (and gnus-use-trees gnus-show-threads)
7109         (gnus-possibly-generate-tree article)
7110         (gnus-highlight-selected-tree article))
7111       ;; Successfully display article.
7112       (gnus-article-set-window-start
7113        (cdr (assq article gnus-newsgroup-bookmarks))))))
7114
7115 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7116   "Select the current article.
7117 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7118 non-nil, the article will be re-fetched even if it already present in
7119 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7120 be displayed."
7121   ;; Make sure we are in the summary buffer to work around bbdb bug.
7122   (unless (eq major-mode 'gnus-summary-mode)
7123     (set-buffer gnus-summary-buffer))
7124   (let ((article (or article (gnus-summary-article-number)))
7125         (all-headers (not (not all-headers))) ;Must be T or NIL.
7126         gnus-summary-display-article-function)
7127     (and (not pseudo)
7128          (gnus-summary-article-pseudo-p article)
7129          (error "This is a pseudo-article"))
7130     (save-excursion
7131       (set-buffer gnus-summary-buffer)
7132       (if (or (and gnus-single-article-buffer
7133                    (or (null gnus-current-article)
7134                        (null gnus-article-current)
7135                        (null (get-buffer gnus-article-buffer))
7136                        (not (eq article (cdr gnus-article-current)))
7137                        (not (equal (car gnus-article-current)
7138                                    gnus-newsgroup-name))))
7139               (and (not gnus-single-article-buffer)
7140                    (or (null gnus-current-article)
7141                        (not (eq gnus-current-article article))))
7142               force)
7143           ;; The requested article is different from the current article.
7144           (progn
7145             (gnus-summary-display-article article all-headers)
7146             (gnus-article-set-window-start
7147              (cdr (assq article gnus-newsgroup-bookmarks)))
7148             article)
7149         'old))))
7150
7151 (defun gnus-summary-force-verify-and-decrypt ()
7152   "Display buttons for signed/encrypted parts and verify/decrypt them."
7153   (interactive)
7154   (let ((mm-verify-option 'known)
7155         (mm-decrypt-option 'known)
7156         (gnus-article-emulate-mime t)
7157         (mm-fill-flowed nil)
7158         (gnus-buttonized-mime-types (append (list "multipart/signed"
7159                                                   "multipart/encrypted")
7160                                             gnus-buttonized-mime-types)))
7161     (gnus-summary-select-article nil 'force)))
7162
7163 (defun gnus-summary-set-current-mark (&optional current-mark)
7164   "Obsolete function."
7165   nil)
7166
7167 (defun gnus-summary-next-article (&optional unread subject backward push)
7168   "Select the next article.
7169 If UNREAD, only unread articles are selected.
7170 If SUBJECT, only articles with SUBJECT are selected.
7171 If BACKWARD, the previous article is selected instead of the next."
7172   (interactive "P")
7173   (cond
7174    ;; Is there such an article?
7175    ((and (gnus-summary-search-forward unread subject backward)
7176          (or (gnus-summary-display-article (gnus-summary-article-number))
7177              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7178     (gnus-summary-position-point))
7179    ;; If not, we try the first unread, if that is wanted.
7180    ((and subject
7181          gnus-auto-select-same
7182          (gnus-summary-first-unread-article))
7183     (gnus-summary-position-point)
7184     (gnus-message 6 "Wrapped"))
7185    ;; Try to get next/previous article not displayed in this group.
7186    ((and gnus-auto-extend-newsgroup
7187          (not unread) (not subject))
7188     (gnus-summary-goto-article
7189      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7190      nil (count-lines (point-min) (point))))
7191    ;; Go to next/previous group.
7192    (t
7193     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7194       (gnus-summary-jump-to-group gnus-newsgroup-name))
7195     (let ((cmd last-command-char)
7196           (point
7197            (with-current-buffer gnus-group-buffer
7198              (point)))
7199           (group
7200            (if (eq gnus-keep-same-level 'best)
7201                (gnus-summary-best-group gnus-newsgroup-name)
7202              (gnus-summary-search-group backward gnus-keep-same-level))))
7203       ;; For some reason, the group window gets selected.  We change
7204       ;; it back.
7205       (select-window (get-buffer-window (current-buffer)))
7206       ;; Select next unread newsgroup automagically.
7207       (cond
7208        ((or (not gnus-auto-select-next)
7209             (not cmd))
7210         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7211        ((or (eq gnus-auto-select-next 'quietly)
7212             (and (eq gnus-auto-select-next 'slightly-quietly)
7213                  push)
7214             (and (eq gnus-auto-select-next 'almost-quietly)
7215                  (gnus-summary-last-article-p)))
7216         ;; Select quietly.
7217         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7218             (gnus-summary-exit)
7219           (gnus-message 7 "No more%s articles (%s)..."
7220                         (if unread " unread" "")
7221                         (if group (concat "selecting " group)
7222                           "exiting"))
7223           (gnus-summary-next-group nil group backward)))
7224        (t
7225         (when (gnus-key-press-event-p last-input-event)
7226           (gnus-summary-walk-group-buffer
7227            gnus-newsgroup-name cmd unread backward point))))))))
7228
7229 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7230   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7231                       (?\C-p (gnus-group-prev-unread-group 1))))
7232         (cursor-in-echo-area t)
7233         keve key group ended prompt)
7234     (save-excursion
7235       (set-buffer gnus-group-buffer)
7236       (goto-char start)
7237       (setq group
7238             (if (eq gnus-keep-same-level 'best)
7239                 (gnus-summary-best-group gnus-newsgroup-name)
7240               (gnus-summary-search-group backward gnus-keep-same-level))))
7241     (while (not ended)
7242       (setq prompt
7243             (format
7244              "No more%s articles%s " (if unread " unread" "")
7245              (if (and group
7246                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7247                  (format " (Type %s for %s [%s])"
7248                          (single-key-description cmd) group
7249                          (gnus-group-unread group))
7250                (format " (Type %s to exit %s)"
7251                        (single-key-description cmd)
7252                        gnus-newsgroup-name))))
7253       ;; Confirm auto selection.
7254       (setq key (car (setq keve (gnus-read-event-char prompt)))
7255             ended t)
7256       (cond
7257        ((assq key keystrokes)
7258         (let ((obuf (current-buffer)))
7259           (switch-to-buffer gnus-group-buffer)
7260           (when group
7261             (gnus-group-jump-to-group group))
7262           (eval (cadr (assq key keystrokes)))
7263           (setq group (gnus-group-group-name))
7264           (switch-to-buffer obuf))
7265         (setq ended nil))
7266        ((equal key cmd)
7267         (if (or (not group)
7268                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7269             (gnus-summary-exit)
7270           (gnus-summary-next-group nil group backward)))
7271        (t
7272         (push (cdr keve) unread-command-events))))))
7273
7274 (defun gnus-summary-next-unread-article ()
7275   "Select unread article after current one."
7276   (interactive)
7277   (gnus-summary-next-article
7278    (or (not (eq gnus-summary-goto-unread 'never))
7279        (gnus-summary-last-article-p (gnus-summary-article-number)))
7280    (and gnus-auto-select-same
7281         (gnus-summary-article-subject))))
7282
7283 (defun gnus-summary-prev-article (&optional unread subject)
7284   "Select the article after the current one.
7285 If UNREAD is non-nil, only unread articles are selected."
7286   (interactive "P")
7287   (gnus-summary-next-article unread subject t))
7288
7289 (defun gnus-summary-prev-unread-article ()
7290   "Select unread article before current one."
7291   (interactive)
7292   (gnus-summary-prev-article
7293    (or (not (eq gnus-summary-goto-unread 'never))
7294        (gnus-summary-first-article-p (gnus-summary-article-number)))
7295    (and gnus-auto-select-same
7296         (gnus-summary-article-subject))))
7297
7298 (defun gnus-summary-next-page (&optional lines circular stop)
7299   "Show next page of the selected article.
7300 If at the end of the current article, select the next article.
7301 LINES says how many lines should be scrolled up.
7302
7303 If CIRCULAR is non-nil, go to the start of the article instead of
7304 selecting the next article when reaching the end of the current
7305 article.
7306
7307 If STOP is non-nil, just stop when reaching the end of the message.
7308
7309 Also see the variable `gnus-article-skip-boring'."
7310   (interactive "P")
7311   (setq gnus-summary-buffer (current-buffer))
7312   (gnus-set-global-variables)
7313   (let ((article (gnus-summary-article-number))
7314         (article-window (get-buffer-window gnus-article-buffer t))
7315         endp)
7316     ;; If the buffer is empty, we have no article.
7317     (unless article
7318       (error "No article to select"))
7319     (gnus-configure-windows 'article)
7320     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7321         (if (and (eq gnus-summary-goto-unread 'never)
7322                  (not (gnus-summary-last-article-p article)))
7323             (gnus-summary-next-article)
7324           (gnus-summary-next-unread-article))
7325       (if (or (null gnus-current-article)
7326               (null gnus-article-current)
7327               (/= article (cdr gnus-article-current))
7328               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7329           ;; Selected subject is different from current article's.
7330           (gnus-summary-display-article article)
7331         (when article-window
7332           (gnus-eval-in-buffer-window gnus-article-buffer
7333             (setq endp (or (gnus-article-next-page lines)
7334                            (gnus-article-only-boring-p))))
7335           (when endp
7336             (cond (stop
7337                    (gnus-message 3 "End of message"))
7338                   (circular
7339                    (gnus-summary-beginning-of-article))
7340                   (lines
7341                    (gnus-message 3 "End of message"))
7342                   ((null lines)
7343                    (if (and (eq gnus-summary-goto-unread 'never)
7344                             (not (gnus-summary-last-article-p article)))
7345                        (gnus-summary-next-article)
7346                      (gnus-summary-next-unread-article))))))))
7347     (gnus-summary-recenter)
7348     (gnus-summary-position-point)))
7349
7350 (defun gnus-summary-prev-page (&optional lines move)
7351   "Show previous page of selected article.
7352 Argument LINES specifies lines to be scrolled down.
7353 If MOVE, move to the previous unread article if point is at
7354 the beginning of the buffer."
7355   (interactive "P")
7356   (let ((article (gnus-summary-article-number))
7357         (article-window (get-buffer-window gnus-article-buffer t))
7358         endp)
7359     (gnus-configure-windows 'article)
7360     (if (or (null gnus-current-article)
7361             (null gnus-article-current)
7362             (/= article (cdr gnus-article-current))
7363             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7364         ;; Selected subject is different from current article's.
7365         (gnus-summary-display-article article)
7366       (gnus-summary-recenter)
7367       (when article-window
7368         (gnus-eval-in-buffer-window gnus-article-buffer
7369           (setq endp (gnus-article-prev-page lines)))
7370         (when (and move endp)
7371           (cond (lines
7372                  (gnus-message 3 "Beginning of message"))
7373                 ((null lines)
7374                  (if (and (eq gnus-summary-goto-unread 'never)
7375                           (not (gnus-summary-first-article-p article)))
7376                      (gnus-summary-prev-article)
7377                    (gnus-summary-prev-unread-article))))))))
7378   (gnus-summary-position-point))
7379
7380 (defun gnus-summary-prev-page-or-article (&optional lines)
7381   "Show previous page of selected article.
7382 Argument LINES specifies lines to be scrolled down.
7383 If at the beginning of the article, go to the next article."
7384   (interactive "P")
7385   (gnus-summary-prev-page lines t))
7386
7387 (defun gnus-summary-scroll-up (lines)
7388   "Scroll up (or down) one line current article.
7389 Argument LINES specifies lines to be scrolled up (or down if negative)."
7390   (interactive "p")
7391   (gnus-configure-windows 'article)
7392   (gnus-summary-show-thread)
7393   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7394     (gnus-eval-in-buffer-window gnus-article-buffer
7395       (cond ((> lines 0)
7396              (when (gnus-article-next-page lines)
7397                (gnus-message 3 "End of message")))
7398             ((< lines 0)
7399              (gnus-article-prev-page (- lines))))))
7400   (gnus-summary-recenter)
7401   (gnus-summary-position-point))
7402
7403 (defun gnus-summary-scroll-down (lines)
7404   "Scroll down (or up) one line current article.
7405 Argument LINES specifies lines to be scrolled down (or up if negative)."
7406   (interactive "p")
7407   (gnus-summary-scroll-up (- lines)))
7408
7409 (defun gnus-summary-next-same-subject ()
7410   "Select next article which has the same subject as current one."
7411   (interactive)
7412   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7413
7414 (defun gnus-summary-prev-same-subject ()
7415   "Select previous article which has the same subject as current one."
7416   (interactive)
7417   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7418
7419 (defun gnus-summary-next-unread-same-subject ()
7420   "Select next unread article which has the same subject as current one."
7421   (interactive)
7422   (gnus-summary-next-article t (gnus-summary-article-subject)))
7423
7424 (defun gnus-summary-prev-unread-same-subject ()
7425   "Select previous unread article which has the same subject as current one."
7426   (interactive)
7427   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7428
7429 (defun gnus-summary-first-unread-article ()
7430   "Select the first unread article.
7431 Return nil if there are no unread articles."
7432   (interactive)
7433   (prog1
7434       (when (gnus-summary-first-subject t)
7435         (gnus-summary-show-thread)
7436         (gnus-summary-first-subject t)
7437         (gnus-summary-display-article (gnus-summary-article-number)))
7438     (gnus-summary-position-point)))
7439
7440 (defun gnus-summary-first-unread-subject ()
7441   "Place the point on the subject line of the first unread article.
7442 Return nil if there are no unread articles."
7443   (interactive)
7444   (prog1
7445       (when (gnus-summary-first-subject t)
7446         (gnus-summary-show-thread)
7447         (gnus-summary-first-subject t))
7448     (gnus-summary-position-point)))
7449
7450 (defun gnus-summary-first-unseen-subject ()
7451   "Place the point on the subject line of the first unseen article.
7452 Return nil if there are no unseen articles."
7453   (interactive)
7454   (prog1
7455       (when (gnus-summary-first-subject nil nil t)
7456         (gnus-summary-show-thread)
7457         (gnus-summary-first-subject nil nil t))
7458     (gnus-summary-position-point)))
7459
7460 (defun gnus-summary-first-unseen-or-unread-subject ()
7461   "Place the point on the subject line of the first unseen article or,
7462 if all article have been seen, on the subject line of the first unread
7463 article."
7464   (interactive)
7465   (prog1
7466       (unless (when (gnus-summary-first-subject nil nil t)
7467                 (gnus-summary-show-thread)
7468                 (gnus-summary-first-subject nil nil t))
7469         (when (gnus-summary-first-subject t)
7470           (gnus-summary-show-thread)
7471           (gnus-summary-first-subject t)))
7472     (gnus-summary-position-point)))
7473
7474 (defun gnus-summary-first-article ()
7475   "Select the first article.
7476 Return nil if there are no articles."
7477   (interactive)
7478   (prog1
7479       (when (gnus-summary-first-subject)
7480         (gnus-summary-show-thread)
7481         (gnus-summary-first-subject)
7482         (gnus-summary-display-article (gnus-summary-article-number)))
7483     (gnus-summary-position-point)))
7484
7485 (defun gnus-summary-best-unread-article (&optional arg)
7486   "Select the unread article with the highest score.
7487 If given a prefix argument, select the next unread article that has a
7488 score higher than the default score."
7489   (interactive "P")
7490   (let ((article (if arg
7491                      (gnus-summary-better-unread-subject)
7492                    (gnus-summary-best-unread-subject))))
7493     (if article
7494         (gnus-summary-goto-article article)
7495       (error "No unread articles"))))
7496
7497 (defun gnus-summary-best-unread-subject ()
7498   "Select the unread subject with the highest score."
7499   (interactive)
7500   (let ((best -1000000)
7501         (data gnus-newsgroup-data)
7502         article score)
7503     (while data
7504       (and (gnus-data-unread-p (car data))
7505            (> (setq score
7506                     (gnus-summary-article-score (gnus-data-number (car data))))
7507               best)
7508            (setq best score
7509                  article (gnus-data-number (car data))))
7510       (setq data (cdr data)))
7511     (when article
7512       (gnus-summary-goto-subject article))
7513     (gnus-summary-position-point)
7514     article))
7515
7516 (defun gnus-summary-better-unread-subject ()
7517   "Select the first unread subject that has a score over the default score."
7518   (interactive)
7519   (let ((data gnus-newsgroup-data)
7520         article score)
7521     (while (and (setq article (gnus-data-number (car data)))
7522                 (or (gnus-data-read-p (car data))
7523                     (not (> (gnus-summary-article-score article)
7524                             gnus-summary-default-score))))
7525       (setq data (cdr data)))
7526     (when article
7527       (gnus-summary-goto-subject article))
7528     (gnus-summary-position-point)
7529     article))
7530
7531 (defun gnus-summary-last-subject ()
7532   "Go to the last displayed subject line in the group."
7533   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7534     (when article
7535       (gnus-summary-goto-subject article))))
7536
7537 (defun gnus-summary-goto-article (article &optional all-headers force)
7538   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7539 If ALL-HEADERS is non-nil, no header lines are hidden.
7540 If FORCE, go to the article even if it isn't displayed.  If FORCE
7541 is a number, it is the line the article is to be displayed on."
7542   (interactive
7543    (list
7544     (completing-read
7545      "Article number or Message-ID: "
7546      (mapcar (lambda (number) (list (int-to-string number)))
7547              gnus-newsgroup-limit))
7548     current-prefix-arg
7549     t))
7550   (prog1
7551       (if (and (stringp article)
7552                (string-match "@\\|%40" article))
7553           (gnus-summary-refer-article article)
7554         (when (stringp article)
7555           (setq article (string-to-number article)))
7556         (if (gnus-summary-goto-subject article force)
7557             (gnus-summary-display-article article all-headers)
7558           (gnus-message 4 "Couldn't go to article %s" article) nil))
7559     (gnus-summary-position-point)))
7560
7561 (defun gnus-summary-goto-last-article ()
7562   "Go to the previously read article."
7563   (interactive)
7564   (prog1
7565       (when gnus-last-article
7566         (gnus-summary-goto-article gnus-last-article nil t))
7567     (gnus-summary-position-point)))
7568
7569 (defun gnus-summary-pop-article (number)
7570   "Pop one article off the history and go to the previous.
7571 NUMBER articles will be popped off."
7572   (interactive "p")
7573   (let (to)
7574     (setq gnus-newsgroup-history
7575           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7576     (if to
7577         (gnus-summary-goto-article (car to) nil t)
7578       (error "Article history empty")))
7579   (gnus-summary-position-point))
7580
7581 ;; Summary commands and functions for limiting the summary buffer.
7582
7583 (defun gnus-summary-limit-to-articles (n)
7584   "Limit the summary buffer to the next N articles.
7585 If not given a prefix, use the process marked articles instead."
7586   (interactive "P")
7587   (prog1
7588       (let ((articles (gnus-summary-work-articles n)))
7589         (setq gnus-newsgroup-processable nil)
7590         (gnus-summary-limit articles))
7591     (gnus-summary-position-point)))
7592
7593 (defun gnus-summary-pop-limit (&optional total)
7594   "Restore the previous limit.
7595 If given a prefix, remove all limits."
7596   (interactive "P")
7597   (when total
7598     (setq gnus-newsgroup-limits
7599           (list (mapcar (lambda (h) (mail-header-number h))
7600                         gnus-newsgroup-headers))))
7601   (unless gnus-newsgroup-limits
7602     (error "No limit to pop"))
7603   (prog1
7604       (gnus-summary-limit nil 'pop)
7605     (gnus-summary-position-point)))
7606
7607 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7608   "Limit the summary buffer to articles that have subjects that match a regexp.
7609 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7610   (interactive
7611    (list (read-string (if current-prefix-arg
7612                           "Exclude subject (regexp): "
7613                         "Limit to subject (regexp): "))
7614          nil current-prefix-arg))
7615   (unless header
7616     (setq header "subject"))
7617   (when (not (equal "" subject))
7618     (prog1
7619         (let ((articles (gnus-summary-find-matching
7620                          (or header "subject") subject 'all nil nil
7621                          not-matching)))
7622           (unless articles
7623             (error "Found no matches for \"%s\"" subject))
7624           (gnus-summary-limit articles))
7625       (gnus-summary-position-point))))
7626
7627 (defun gnus-summary-limit-to-author (from &optional not-matching)
7628   "Limit the summary buffer to articles that have authors that match a regexp.
7629 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7630   (interactive
7631    (list (read-string (if current-prefix-arg
7632                           "Exclude author (regexp): "
7633                         "Limit to author (regexp): "))
7634          current-prefix-arg))
7635   (gnus-summary-limit-to-subject from "from" not-matching))
7636
7637 (defun gnus-summary-limit-to-age (age &optional younger-p)
7638   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7639 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7640 articles that are younger than AGE days."
7641   (interactive
7642    (let ((younger current-prefix-arg)
7643          (days-got nil)
7644          days)
7645      (while (not days-got)
7646        (setq days (if younger
7647                       (read-string "Limit to articles younger than (in days, older when negative): ")
7648                     (read-string
7649                      "Limit to articles older than (in days, younger when negative): ")))
7650        (when (> (length days) 0)
7651          (setq days (read days)))
7652        (if (numberp days)
7653            (progn
7654              (setq days-got t)
7655              (when (< days 0)
7656                (setq younger (not younger))
7657                (setq days (* days -1))))
7658          (message "Please enter a number.")
7659          (sleep-for 1)))
7660      (list days younger)))
7661   (prog1
7662       (let ((data gnus-newsgroup-data)
7663             (cutoff (days-to-time age))
7664             articles d date is-younger)
7665         (while (setq d (pop data))
7666           (when (and (vectorp (gnus-data-header d))
7667                      (setq date (mail-header-date (gnus-data-header d))))
7668             (setq is-younger (time-less-p
7669                               (time-since (condition-case ()
7670                                               (date-to-time date)
7671                                             (error '(0 0))))
7672                               cutoff))
7673             (when (if younger-p
7674                       is-younger
7675                     (not is-younger))
7676               (push (gnus-data-number d) articles))))
7677         (gnus-summary-limit (nreverse articles)))
7678     (gnus-summary-position-point)))
7679
7680 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7681   "Limit the summary buffer to articles that match an 'extra' header."
7682   (interactive
7683    (let ((header
7684           (intern
7685            (gnus-completing-read-with-default
7686             (symbol-name (car gnus-extra-headers))
7687             (if current-prefix-arg
7688                 "Exclude extra header:"
7689               "Limit extra header:")
7690             (mapcar (lambda (x)
7691                       (cons (symbol-name x) x))
7692                     gnus-extra-headers)
7693             nil
7694             t))))
7695      (list header
7696            (read-string (format "%s header %s (regexp): "
7697                                 (if current-prefix-arg "Exclude" "Limit to")
7698                                 header))
7699            current-prefix-arg)))
7700   (when (not (equal "" regexp))
7701     (prog1
7702         (let ((articles (gnus-summary-find-matching
7703                          (cons 'extra header) regexp 'all nil nil
7704                          not-matching)))
7705           (unless articles
7706             (error "Found no matches for \"%s\"" regexp))
7707           (gnus-summary-limit articles))
7708       (gnus-summary-position-point))))
7709
7710 (defun gnus-summary-limit-to-display-predicate ()
7711   "Limit the summary buffer to the predicated in the `display' group parameter."
7712   (interactive)
7713   (unless gnus-newsgroup-display
7714     (error "There is no `display' group parameter"))
7715   (let (articles)
7716     (dolist (number gnus-newsgroup-articles)
7717       (when (funcall gnus-newsgroup-display)
7718         (push number articles)))
7719     (gnus-summary-limit articles))
7720   (gnus-summary-position-point))
7721
7722 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7723 (make-obsolete
7724  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7725
7726 (defun gnus-summary-limit-to-unread (&optional all)
7727   "Limit the summary buffer to articles that are not marked as read.
7728 If ALL is non-nil, limit strictly to unread articles."
7729   (interactive "P")
7730   (if all
7731       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7732     (gnus-summary-limit-to-marks
7733      ;; Concat all the marks that say that an article is read and have
7734      ;; those removed.
7735      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7736            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7737            gnus-low-score-mark gnus-expirable-mark
7738            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7739            gnus-duplicate-mark gnus-souped-mark)
7740      'reverse)))
7741
7742 (defun gnus-summary-limit-to-replied (&optional unreplied)
7743   "Limit the summary buffer to replied articles.
7744 If UNREPLIED (the prefix), limit to unreplied articles."
7745   (interactive "P")
7746   (if unreplied
7747       (gnus-summary-limit
7748        (gnus-set-difference gnus-newsgroup-articles
7749         gnus-newsgroup-replied))
7750     (gnus-summary-limit gnus-newsgroup-replied))
7751   (gnus-summary-position-point))
7752
7753 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7754 (make-obsolete 'gnus-summary-delete-marked-with
7755                'gnus-summary-limit-exclude-marks)
7756
7757 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7758   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7759 If REVERSE, limit the summary buffer to articles that are marked
7760 with MARKS.  MARKS can either be a string of marks or a list of marks.
7761 Returns how many articles were removed."
7762   (interactive "sMarks: ")
7763   (gnus-summary-limit-to-marks marks t))
7764
7765 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7766   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7767 If REVERSE (the prefix), limit the summary buffer to articles that are
7768 not marked with MARKS.  MARKS can either be a string of marks or a
7769 list of marks.
7770 Returns how many articles were removed."
7771   (interactive "sMarks: \nP")
7772   (prog1
7773       (let ((data gnus-newsgroup-data)
7774             (marks (if (listp marks) marks
7775                      (append marks nil))) ; Transform to list.
7776             articles)
7777         (while data
7778           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7779                   (memq (gnus-data-mark (car data)) marks))
7780             (push (gnus-data-number (car data)) articles))
7781           (setq data (cdr data)))
7782         (gnus-summary-limit articles))
7783     (gnus-summary-position-point)))
7784
7785 (defun gnus-summary-limit-to-score (score)
7786   "Limit to articles with score at or above SCORE."
7787   (interactive "NLimit to articles with score of at least: ")
7788   (let ((data gnus-newsgroup-data)
7789         articles)
7790     (while data
7791       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7792                 score)
7793         (push (gnus-data-number (car data)) articles))
7794       (setq data (cdr data)))
7795     (prog1
7796         (gnus-summary-limit articles)
7797       (gnus-summary-position-point))))
7798
7799 (defun gnus-summary-limit-to-unseen ()
7800   "Limit to unseen articles."
7801   (interactive)
7802   (prog1
7803       (gnus-summary-limit gnus-newsgroup-unseen)
7804     (gnus-summary-position-point)))
7805
7806 (defun gnus-summary-limit-include-thread (id)
7807   "Display all the hidden articles that is in the thread with ID in it.
7808 When called interactively, ID is the Message-ID of the current
7809 article."
7810   (interactive (list (mail-header-id (gnus-summary-article-header))))
7811   (let ((articles (gnus-articles-in-thread
7812                    (gnus-id-to-thread (gnus-root-id id)))))
7813     (prog1
7814         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7815       (gnus-summary-limit-include-matching-articles
7816        "subject"
7817        (regexp-quote (gnus-simplify-subject-re
7818                       (mail-header-subject (gnus-id-to-header id)))))
7819       (gnus-summary-position-point))))
7820
7821 (defun gnus-summary-limit-include-matching-articles (header regexp)
7822   "Display all the hidden articles that have HEADERs that match REGEXP."
7823   (interactive (list (read-string "Match on header: ")
7824                      (read-string "Regexp: ")))
7825   (let ((articles (gnus-find-matching-articles header regexp)))
7826     (prog1
7827         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7828       (gnus-summary-position-point))))
7829
7830 (defun gnus-summary-insert-dormant-articles ()
7831   "Insert all the dormant articles for this group into the current buffer."
7832   (interactive)
7833   (let ((gnus-verbose (max 6 gnus-verbose)))
7834     (if (not gnus-newsgroup-dormant)
7835         (gnus-message 3 "No cached articles for this group")
7836       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7837
7838 (defun gnus-summary-limit-include-dormant ()
7839   "Display all the hidden articles that are marked as dormant.
7840 Note that this command only works on a subset of the articles currently
7841 fetched for this group."
7842   (interactive)
7843   (unless gnus-newsgroup-dormant
7844     (error "There are no dormant articles in this group"))
7845   (prog1
7846       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7847     (gnus-summary-position-point)))
7848
7849 (defun gnus-summary-limit-exclude-dormant ()
7850   "Hide all dormant articles."
7851   (interactive)
7852   (prog1
7853       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7854     (gnus-summary-position-point)))
7855
7856 (defun gnus-summary-limit-exclude-childless-dormant ()
7857   "Hide all dormant articles that have no children."
7858   (interactive)
7859   (let ((data (gnus-data-list t))
7860         articles d children)
7861     ;; Find all articles that are either not dormant or have
7862     ;; children.
7863     (while (setq d (pop data))
7864       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7865                 (and (setq children
7866                            (gnus-article-children (gnus-data-number d)))
7867                      (let (found)
7868                        (while children
7869                          (when (memq (car children) articles)
7870                            (setq children nil
7871                                  found t))
7872                          (pop children))
7873                        found)))
7874         (push (gnus-data-number d) articles)))
7875     ;; Do the limiting.
7876     (prog1
7877         (gnus-summary-limit articles)
7878       (gnus-summary-position-point))))
7879
7880 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7881   "Mark all unread excluded articles as read.
7882 If ALL, mark even excluded ticked and dormants as read."
7883   (interactive "P")
7884   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7885   (let ((articles (gnus-sorted-ndifference
7886                    (sort
7887                     (mapcar (lambda (h) (mail-header-number h))
7888                             gnus-newsgroup-headers)
7889                     '<)
7890                    gnus-newsgroup-limit))
7891         article)
7892     (setq gnus-newsgroup-unreads
7893           (gnus-sorted-intersection gnus-newsgroup-unreads
7894                                     gnus-newsgroup-limit))
7895     (if all
7896         (setq gnus-newsgroup-dormant nil
7897               gnus-newsgroup-marked nil
7898               gnus-newsgroup-reads
7899               (nconc
7900                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7901                gnus-newsgroup-reads))
7902       (while (setq article (pop articles))
7903         (unless (or (memq article gnus-newsgroup-dormant)
7904                     (memq article gnus-newsgroup-marked))
7905           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7906
7907 (defun gnus-summary-limit (articles &optional pop)
7908   (if pop
7909       ;; We pop the previous limit off the stack and use that.
7910       (setq articles (car gnus-newsgroup-limits)
7911             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7912     ;; We use the new limit, so we push the old limit on the stack.
7913     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7914   ;; Set the limit.
7915   (setq gnus-newsgroup-limit articles)
7916   (let ((total (length gnus-newsgroup-data))
7917         (data (gnus-data-find-list (gnus-summary-article-number)))
7918         (gnus-summary-mark-below nil)   ; Inhibit this.
7919         found)
7920     ;; This will do all the work of generating the new summary buffer
7921     ;; according to the new limit.
7922     (gnus-summary-prepare)
7923     ;; Hide any threads, possibly.
7924     (gnus-summary-maybe-hide-threads)
7925     ;; Try to return to the article you were at, or one in the
7926     ;; neighborhood.
7927     (when data
7928       ;; We try to find some article after the current one.
7929       (while data
7930         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7931           (setq data nil
7932                 found t))
7933         (setq data (cdr data))))
7934     (unless found
7935       ;; If there is no data, that means that we were after the last
7936       ;; article.  The same goes when we can't find any articles
7937       ;; after the current one.
7938       (goto-char (point-max))
7939       (gnus-summary-find-prev))
7940     (gnus-set-mode-line 'summary)
7941     ;; We return how many articles were removed from the summary
7942     ;; buffer as a result of the new limit.
7943     (- total (length gnus-newsgroup-data))))
7944
7945 (defsubst gnus-invisible-cut-children (threads)
7946   (let ((num 0))
7947     (while threads
7948       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7949         (incf num))
7950       (pop threads))
7951     (< num 2)))
7952
7953 (defsubst gnus-cut-thread (thread)
7954   "Go forwards in the thread until we find an article that we want to display."
7955   (when (or (eq gnus-fetch-old-headers 'some)
7956             (eq gnus-fetch-old-headers 'invisible)
7957             (numberp gnus-fetch-old-headers)
7958             (eq gnus-build-sparse-threads 'some)
7959             (eq gnus-build-sparse-threads 'more))
7960     ;; Deal with old-fetched headers and sparse threads.
7961     (while (and
7962             thread
7963             (or
7964              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7965              (gnus-summary-article-ancient-p
7966               (mail-header-number (car thread))))
7967             (if (or (<= (length (cdr thread)) 1)
7968                     (eq gnus-fetch-old-headers 'invisible))
7969                 (setq gnus-newsgroup-limit
7970                       (delq (mail-header-number (car thread))
7971                             gnus-newsgroup-limit)
7972                       thread (cadr thread))
7973               (when (gnus-invisible-cut-children (cdr thread))
7974                 (let ((th (cdr thread)))
7975                   (while th
7976                     (if (memq (mail-header-number (caar th))
7977                               gnus-newsgroup-limit)
7978                         (setq thread (car th)
7979                               th nil)
7980                       (setq th (cdr th))))))))))
7981   thread)
7982
7983 (defun gnus-cut-threads (threads)
7984   "Cut off all uninteresting articles from the beginning of THREADS."
7985   (when (or (eq gnus-fetch-old-headers 'some)
7986             (eq gnus-fetch-old-headers 'invisible)
7987             (numberp gnus-fetch-old-headers)
7988             (eq gnus-build-sparse-threads 'some)
7989             (eq gnus-build-sparse-threads 'more))
7990     (let ((th threads))
7991       (while th
7992         (setcar th (gnus-cut-thread (car th)))
7993         (setq th (cdr th)))))
7994   ;; Remove nixed out threads.
7995   (delq nil threads))
7996
7997 (defun gnus-summary-initial-limit (&optional show-if-empty)
7998   "Figure out what the initial limit is supposed to be on group entry.
7999 This entails weeding out unwanted dormants, low-scored articles,
8000 fetch-old-headers verbiage, and so on."
8001   ;; Most groups have nothing to remove.
8002   (if (or gnus-inhibit-limiting
8003           (and (null gnus-newsgroup-dormant)
8004                (eq gnus-newsgroup-display 'gnus-not-ignore)
8005                (not (eq gnus-fetch-old-headers 'some))
8006                (not (numberp gnus-fetch-old-headers))
8007                (not (eq gnus-fetch-old-headers 'invisible))
8008                (null gnus-summary-expunge-below)
8009                (not (eq gnus-build-sparse-threads 'some))
8010                (not (eq gnus-build-sparse-threads 'more))
8011                (null gnus-thread-expunge-below)
8012                (not gnus-use-nocem)))
8013       ()                                ; Do nothing.
8014     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8015     (setq gnus-newsgroup-limit nil)
8016     (mapatoms
8017      (lambda (node)
8018        (unless (car (symbol-value node))
8019          ;; These threads have no parents -- they are roots.
8020          (let ((nodes (cdr (symbol-value node)))
8021                thread)
8022            (while nodes
8023              (if (and gnus-thread-expunge-below
8024                       (< (gnus-thread-total-score (car nodes))
8025                          gnus-thread-expunge-below))
8026                  (gnus-expunge-thread (pop nodes))
8027                (setq thread (pop nodes))
8028                (gnus-summary-limit-children thread))))))
8029      gnus-newsgroup-dependencies)
8030     ;; If this limitation resulted in an empty group, we might
8031     ;; pop the previous limit and use it instead.
8032     (when (and (not gnus-newsgroup-limit)
8033                show-if-empty)
8034       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8035     gnus-newsgroup-limit))
8036
8037 (defun gnus-summary-limit-children (thread)
8038   "Return 1 if this subthread is visible and 0 if it is not."
8039   ;; First we get the number of visible children to this thread.  This
8040   ;; is done by recursing down the thread using this function, so this
8041   ;; will really go down to a leaf article first, before slowly
8042   ;; working its way up towards the root.
8043   (when thread
8044     (let* ((max-lisp-eval-depth 5000)
8045            (children
8046            (if (cdr thread)
8047                (apply '+ (mapcar 'gnus-summary-limit-children
8048                                  (cdr thread)))
8049              0))
8050           (number (mail-header-number (car thread)))
8051           score)
8052       (if (and
8053            (not (memq number gnus-newsgroup-marked))
8054            (or
8055             ;; If this article is dormant and has absolutely no visible
8056             ;; children, then this article isn't visible.
8057             (and (memq number gnus-newsgroup-dormant)
8058                  (zerop children))
8059             ;; If this is "fetch-old-headered" and there is no
8060             ;; visible children, then we don't want this article.
8061             (and (or (eq gnus-fetch-old-headers 'some)
8062                      (numberp gnus-fetch-old-headers))
8063                  (gnus-summary-article-ancient-p number)
8064                  (zerop children))
8065             ;; If this is "fetch-old-headered" and `invisible', then
8066             ;; we don't want this article.
8067             (and (eq gnus-fetch-old-headers 'invisible)
8068                  (gnus-summary-article-ancient-p number))
8069             ;; If this is a sparsely inserted article with no children,
8070             ;; we don't want it.
8071             (and (eq gnus-build-sparse-threads 'some)
8072                  (gnus-summary-article-sparse-p number)
8073                  (zerop children))
8074             ;; If we use expunging, and this article is really
8075             ;; low-scored, then we don't want this article.
8076             (when (and gnus-summary-expunge-below
8077                        (< (setq score
8078                                 (or (cdr (assq number gnus-newsgroup-scored))
8079                                     gnus-summary-default-score))
8080                           gnus-summary-expunge-below))
8081               ;; We increase the expunge-tally here, but that has
8082               ;; nothing to do with the limits, really.
8083               (incf gnus-newsgroup-expunged-tally)
8084               ;; We also mark as read here, if that's wanted.
8085               (when (and gnus-summary-mark-below
8086                          (< score gnus-summary-mark-below))
8087                 (setq gnus-newsgroup-unreads
8088                       (delq number gnus-newsgroup-unreads))
8089                 (if gnus-newsgroup-auto-expire
8090                     (push number gnus-newsgroup-expirable)
8091                   (push (cons number gnus-low-score-mark)
8092                         gnus-newsgroup-reads)))
8093               t)
8094             ;; Do the `display' group parameter.
8095             (and gnus-newsgroup-display
8096                  (not (funcall gnus-newsgroup-display)))
8097             ;; Check NoCeM things.
8098             (when (and gnus-use-nocem
8099                        (gnus-nocem-unwanted-article-p
8100                         (mail-header-id (car thread))))
8101               (setq gnus-newsgroup-unreads
8102                     (delq number gnus-newsgroup-unreads))
8103               t)))
8104           ;; Nope, invisible article.
8105           0
8106         ;; Ok, this article is to be visible, so we add it to the limit
8107         ;; and return 1.
8108         (push number gnus-newsgroup-limit)
8109         1))))
8110
8111 (defun gnus-expunge-thread (thread)
8112   "Mark all articles in THREAD as read."
8113   (let* ((number (mail-header-number (car thread))))
8114     (incf gnus-newsgroup-expunged-tally)
8115     ;; We also mark as read here, if that's wanted.
8116     (setq gnus-newsgroup-unreads
8117           (delq number gnus-newsgroup-unreads))
8118     (if gnus-newsgroup-auto-expire
8119         (push number gnus-newsgroup-expirable)
8120       (push (cons number gnus-low-score-mark)
8121             gnus-newsgroup-reads)))
8122   ;; Go recursively through all subthreads.
8123   (mapcar 'gnus-expunge-thread (cdr thread)))
8124
8125 ;; Summary article oriented commands
8126
8127 (defun gnus-summary-refer-parent-article (n)
8128   "Refer parent article N times.
8129 If N is negative, go to ancestor -N instead.
8130 The difference between N and the number of articles fetched is returned."
8131   (interactive "p")
8132   (let ((skip 1)
8133         error header ref)
8134     (when (not (natnump n))
8135       (setq skip (abs n)
8136             n 1))
8137     (while (and (> n 0)
8138                 (not error))
8139       (setq header (gnus-summary-article-header))
8140       (if (and (eq (mail-header-number header)
8141                    (cdr gnus-article-current))
8142                (equal gnus-newsgroup-name
8143                       (car gnus-article-current)))
8144           ;; If we try to find the parent of the currently
8145           ;; displayed article, then we take a look at the actual
8146           ;; References header, since this is slightly more
8147           ;; reliable than the References field we got from the
8148           ;; server.
8149           (save-excursion
8150             (set-buffer gnus-original-article-buffer)
8151             (nnheader-narrow-to-headers)
8152             (unless (setq ref (message-fetch-field "references"))
8153               (when (setq ref (message-fetch-field "in-reply-to"))
8154                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8155             (widen))
8156         (setq ref
8157               ;; It's not the current article, so we take a bet on
8158               ;; the value we got from the server.
8159               (mail-header-references header)))
8160       (if (and ref
8161                (not (equal ref "")))
8162           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8163             (gnus-message 1 "Couldn't find parent"))
8164         (gnus-message 1 "No references in article %d"
8165                       (gnus-summary-article-number))
8166         (setq error t))
8167       (decf n))
8168     (gnus-summary-position-point)
8169     n))
8170
8171 (defun gnus-summary-refer-references ()
8172   "Fetch all articles mentioned in the References header.
8173 Return the number of articles fetched."
8174   (interactive)
8175   (let ((ref (mail-header-references (gnus-summary-article-header)))
8176         (current (gnus-summary-article-number))
8177         (n 0))
8178     (if (or (not ref)
8179             (equal ref ""))
8180         (error "No References in the current article")
8181       ;; For each Message-ID in the References header...
8182       (while (string-match "<[^>]*>" ref)
8183         (incf n)
8184         ;; ... fetch that article.
8185         (gnus-summary-refer-article
8186          (prog1 (match-string 0 ref)
8187            (setq ref (substring ref (match-end 0))))))
8188       (gnus-summary-goto-subject current)
8189       (gnus-summary-position-point)
8190       n)))
8191
8192 (defun gnus-summary-refer-thread (&optional limit)
8193   "Fetch all articles in the current thread.
8194 If LIMIT (the numerical prefix), fetch that many old headers instead
8195 of what's specified by the `gnus-refer-thread-limit' variable."
8196   (interactive "P")
8197   (let ((id (mail-header-id (gnus-summary-article-header)))
8198         (limit (if limit (prefix-numeric-value limit)
8199                  gnus-refer-thread-limit)))
8200     (unless (eq gnus-fetch-old-headers 'invisible)
8201       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8202       ;; Retrieve the headers and read them in.
8203       (if (eq (if (numberp limit)
8204                   (gnus-retrieve-headers
8205                    (list (min
8206                           (+ (mail-header-number
8207                               (gnus-summary-article-header))
8208                              limit)
8209                           gnus-newsgroup-end))
8210                    gnus-newsgroup-name (* limit 2))
8211                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8212                 ;; headers.
8213                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8214                                        gnus-newsgroup-name limit))
8215               'nov)
8216           (gnus-build-all-threads)
8217         (error "Can't fetch thread from back ends that don't support NOV"))
8218       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8219     (gnus-summary-limit-include-thread id)))
8220
8221 (defun gnus-summary-refer-article (message-id)
8222   "Fetch an article specified by MESSAGE-ID."
8223   (interactive "sMessage-ID: ")
8224   (when (and (stringp message-id)
8225              (not (zerop (length message-id))))
8226     (setq message-id (gnus-replace-in-string message-id " " ""))
8227     ;; Construct the correct Message-ID if necessary.
8228     ;; Suggested by tale@pawl.rpi.edu.
8229     (unless (string-match "^<" message-id)
8230       (setq message-id (concat "<" message-id)))
8231     (unless (string-match ">$" message-id)
8232       (setq message-id (concat message-id ">")))
8233     ;; People often post MIDs from URLs, so unhex it:
8234     (unless (string-match "@" message-id)
8235       (setq message-id (gnus-url-unhex-string message-id)))
8236     (let* ((header (gnus-id-to-header message-id))
8237            (sparse (and header
8238                         (gnus-summary-article-sparse-p
8239                          (mail-header-number header))
8240                         (memq (mail-header-number header)
8241                               gnus-newsgroup-limit)))
8242            number)
8243       (cond
8244        ;; If the article is present in the buffer we just go to it.
8245        ((and header
8246              (or (not (gnus-summary-article-sparse-p
8247                        (mail-header-number header)))
8248                  sparse))
8249         (prog1
8250             (gnus-summary-goto-article
8251              (mail-header-number header) nil t)
8252           (when sparse
8253             (gnus-summary-update-article (mail-header-number header)))))
8254        (t
8255         ;; We fetch the article.
8256         (catch 'found
8257           (dolist (gnus-override-method (gnus-refer-article-methods))
8258             (when (and (gnus-check-server gnus-override-method)
8259                        ;; Fetch the header,
8260                        (setq number (gnus-summary-insert-subject message-id)))
8261               ;; and display the article.
8262               (gnus-summary-select-article nil nil nil number)
8263               (throw 'found t)))
8264           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8265
8266 (defun gnus-refer-article-methods ()
8267   "Return a list of referable methods."
8268   (cond
8269    ;; No method, so we default to current and native.
8270    ((null gnus-refer-article-method)
8271     (list gnus-current-select-method gnus-select-method))
8272    ;; Current.
8273    ((eq 'current gnus-refer-article-method)
8274     (list gnus-current-select-method))
8275    ;; List of select methods.
8276    ((not (and (symbolp (car gnus-refer-article-method))
8277               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8278     (let (out)
8279       (dolist (method gnus-refer-article-method)
8280         (push (if (eq 'current method)
8281                   gnus-current-select-method
8282                 method)
8283               out))
8284       (nreverse out)))
8285    ;; One single select method.
8286    (t
8287     (list gnus-refer-article-method))))
8288
8289 (defun gnus-summary-edit-parameters ()
8290   "Edit the group parameters of the current group."
8291   (interactive)
8292   (gnus-group-edit-group gnus-newsgroup-name 'params))
8293
8294 (defun gnus-summary-customize-parameters ()
8295   "Customize the group parameters of the current group."
8296   (interactive)
8297   (gnus-group-customize gnus-newsgroup-name))
8298
8299 (defun gnus-summary-enter-digest-group (&optional force)
8300   "Enter an nndoc group based on the current article.
8301 If FORCE, force a digest interpretation.  If not, try
8302 to guess what the document format is."
8303   (interactive "P")
8304   (let ((conf gnus-current-window-configuration))
8305     (save-window-excursion
8306       (save-excursion
8307         (let (gnus-article-prepare-hook
8308               gnus-display-mime-function
8309               gnus-break-pages)
8310           (gnus-summary-select-article))))
8311     (setq gnus-current-window-configuration conf)
8312     (let* ((name (format "%s-%d"
8313                          (gnus-group-prefixed-name
8314                           gnus-newsgroup-name (list 'nndoc ""))
8315                          (with-current-buffer gnus-summary-buffer
8316                            gnus-current-article)))
8317            (ogroup gnus-newsgroup-name)
8318            (params (append (gnus-info-params (gnus-get-info ogroup))
8319                            (list (cons 'to-group ogroup))
8320                            (list (cons 'parent-group ogroup))
8321                            (list (cons 'save-article-group ogroup))))
8322            (case-fold-search t)
8323            (buf (current-buffer))
8324            dig to-address)
8325       (save-excursion
8326         (set-buffer gnus-original-article-buffer)
8327         ;; Have the digest group inherit the main mail address of
8328         ;; the parent article.
8329         (when (setq to-address (or (gnus-fetch-field "reply-to")
8330                                    (gnus-fetch-field "from")))
8331           (setq params (append
8332                         (list (cons 'to-address
8333                                     (funcall gnus-decode-encoded-word-function
8334                                              to-address))))))
8335         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8336         (insert-buffer-substring gnus-original-article-buffer)
8337         ;; Remove lines that may lead nndoc to misinterpret the
8338         ;; document type.
8339         (narrow-to-region
8340          (goto-char (point-min))
8341          (or (search-forward "\n\n" nil t) (point)))
8342         (goto-char (point-min))
8343         (delete-matching-lines "^Path:\\|^From ")
8344         (widen))
8345       (unwind-protect
8346           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8347                     (gnus-newsgroup-ephemeral-ignored-charsets
8348                      gnus-newsgroup-ignored-charsets))
8349                 (gnus-group-read-ephemeral-group
8350                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8351                               (nndoc-article-type
8352                                ,(if force 'mbox 'guess)))
8353                  t nil nil nil
8354                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8355                                                         "ADAPT")))))
8356               ;; Make all postings to this group go to the parent group.
8357               (nconc (gnus-info-params (gnus-get-info name))
8358                      params)
8359             ;; Couldn't select this doc group.
8360             (switch-to-buffer buf)
8361             (gnus-set-global-variables)
8362             (gnus-configure-windows 'summary)
8363             (gnus-message 3 "Article couldn't be entered?"))
8364         (kill-buffer dig)))))
8365
8366 (defun gnus-summary-read-document (n)
8367   "Open a new group based on the current article(s).
8368 This will allow you to read digests and other similar
8369 documents as newsgroups.
8370 Obeys the standard process/prefix convention."
8371   (interactive "P")
8372   (let* ((articles (gnus-summary-work-articles n))
8373          (ogroup gnus-newsgroup-name)
8374          (params (append (gnus-info-params (gnus-get-info ogroup))
8375                          (list (cons 'to-group ogroup))))
8376          article group egroup groups vgroup)
8377     (while (setq article (pop articles))
8378       (setq group (format "%s-%d" gnus-newsgroup-name article))
8379       (gnus-summary-remove-process-mark article)
8380       (when (gnus-summary-display-article article)
8381         (save-excursion
8382           (with-temp-buffer
8383             (insert-buffer-substring gnus-original-article-buffer)
8384             ;; Remove some headers that may lead nndoc to make
8385             ;; the wrong guess.
8386             (message-narrow-to-head)
8387             (goto-char (point-min))
8388             (delete-matching-lines "^Path:\\|^From ")
8389             (widen)
8390             (if (setq egroup
8391                       (gnus-group-read-ephemeral-group
8392                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8393                                      (nndoc-article-type guess))
8394                        t nil t))
8395                 (progn
8396                   ;; Make all postings to this group go to the parent group.
8397                   (nconc (gnus-info-params (gnus-get-info egroup))
8398                          params)
8399                   (push egroup groups))
8400               ;; Couldn't select this doc group.
8401               (gnus-error 3 "Article couldn't be entered"))))))
8402     ;; Now we have selected all the documents.
8403     (cond
8404      ((not groups)
8405       (error "None of the articles could be interpreted as documents"))
8406      ((gnus-group-read-ephemeral-group
8407        (setq vgroup (format
8408                      "nnvirtual:%s-%s" gnus-newsgroup-name
8409                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8410        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8411        t
8412        (cons (current-buffer) 'summary)))
8413      (t
8414       (error "Couldn't select virtual nndoc group")))))
8415
8416 (defun gnus-summary-isearch-article (&optional regexp-p)
8417   "Do incremental search forward on the current article.
8418 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8419   (interactive "P")
8420   (let* ((gnus-inhibit-treatment t)
8421          (old (gnus-summary-select-article)))
8422     (gnus-configure-windows 'article)
8423     (gnus-eval-in-buffer-window gnus-article-buffer
8424       (save-restriction
8425         (widen)
8426         (when (eq 'old old)
8427           (gnus-article-show-all-headers))
8428         (goto-char (point-min))
8429         (isearch-forward regexp-p)))))
8430
8431 (defun gnus-summary-search-article-forward (regexp &optional backward)
8432   "Search for an article containing REGEXP forward.
8433 If BACKWARD, search backward instead."
8434   (interactive
8435    (list (read-string
8436           (format "Search article %s (regexp%s): "
8437                   (if current-prefix-arg "backward" "forward")
8438                   (if gnus-last-search-regexp
8439                       (concat ", default " gnus-last-search-regexp)
8440                     "")))
8441          current-prefix-arg))
8442   (if (string-equal regexp "")
8443       (setq regexp (or gnus-last-search-regexp ""))
8444     (setq gnus-last-search-regexp regexp)
8445     (setq gnus-article-before-search gnus-current-article))
8446   ;; Intentionally set gnus-last-article.
8447   (setq gnus-last-article gnus-article-before-search)
8448   (let ((gnus-last-article gnus-last-article))
8449     (if (gnus-summary-search-article regexp backward)
8450         (gnus-summary-show-thread)
8451       (error "Search failed: \"%s\"" regexp))))
8452
8453 (defun gnus-summary-search-article-backward (regexp)
8454   "Search for an article containing REGEXP backward."
8455   (interactive
8456    (list (read-string
8457           (format "Search article backward (regexp%s): "
8458                   (if gnus-last-search-regexp
8459                       (concat ", default " gnus-last-search-regexp)
8460                     "")))))
8461   (gnus-summary-search-article-forward regexp 'backward))
8462
8463 (eval-when-compile
8464   (defmacro gnus-summary-search-article-position-point (regexp backward)
8465     "Dehighlight the last matched text and goto the beginning position."
8466     (` (if (and gnus-summary-search-article-matched-data
8467                 (let ((text (caddr gnus-summary-search-article-matched-data))
8468                       (inhibit-read-only t)
8469                       buffer-read-only)
8470                   (delete-region
8471                    (goto-char (car gnus-summary-search-article-matched-data))
8472                    (cadr gnus-summary-search-article-matched-data))
8473                   (insert text)
8474                   (string-match (, regexp) text)))
8475            (if (, backward) (beginning-of-line) (end-of-line))
8476          (goto-char (if (, backward) (point-max) (point-min))))))
8477
8478   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8479     "Place point where X-Face image is displayed."
8480     (if (featurep 'xemacs)
8481         (` (let ((end (if (search-forward "\n\n" nil t)
8482                           (goto-char (1- (point)))
8483                         (point-min)))
8484                  extent)
8485              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8486              (unless (and (re-search-forward "^From:" end t)
8487                           (setq extent (extent-at (point)))
8488                           (extent-begin-glyph extent))
8489                (goto-char (, opoint)))))
8490       (` (let ((end (if (search-forward "\n\n" nil t)
8491                         (goto-char (1- (point)))
8492                       (point-min)))
8493                (start (or (search-backward "\n\n" nil t) (point-min))))
8494            (goto-char
8495             (or (text-property-any start end 'x-face-image t);; x-face-e21
8496                 (text-property-any start end 'x-face-mule-bitmap-image t)
8497                 (, opoint)))))))
8498
8499   (defmacro gnus-summary-search-article-highlight-matched-text
8500     (backward treated x-face)
8501     "Highlight matched text in the function `gnus-summary-search-article'."
8502     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8503              (end (set-marker (make-marker) (match-end 0)))
8504              (inhibit-read-only t)
8505              buffer-read-only)
8506          (unless treated
8507            (let ((,@
8508                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8509                     (mapcar
8510                      (lambda (item) (setq items (delq item items)))
8511                      '(gnus-treat-buttonize
8512                        gnus-treat-fill-article
8513                        gnus-treat-fill-long-lines
8514                        gnus-treat-emphasize
8515                        gnus-treat-highlight-headers
8516                        gnus-treat-highlight-citation
8517                        gnus-treat-highlight-signature
8518                        gnus-treat-overstrike
8519                        gnus-treat-display-x-face
8520                        gnus-treat-buttonize-head
8521                        gnus-treat-decode-article-as-default-mime-charset))
8522                     (static-if (featurep 'xemacs)
8523                         items
8524                       (cons '(x-face-mule-delete-x-face-field
8525                               (quote never))
8526                             items))))
8527                  (gnus-treat-display-x-face
8528                   (when (, x-face) gnus-treat-display-x-face)))
8529              (gnus-article-prepare-mime-display)))
8530          (goto-char (if (, backward) start end))
8531          (when (, x-face)
8532            (gnus-summary-search-article-highlight-goto-x-face (point)))
8533          (setq gnus-summary-search-article-matched-data
8534                (list start end (buffer-substring start end)))
8535          (unless (eq start end);; matched text has been deleted. :-<
8536            (put-text-property start end 'face
8537                               (or (find-face 'isearch)
8538                                   'secondary-selection))))))
8539   )
8540
8541 (defun gnus-summary-search-article (regexp &optional backward)
8542   "Search for an article containing REGEXP.
8543 Optional argument BACKWARD means do search for backward.
8544 `gnus-select-article-hook' is not called during the search."
8545   ;; We have to require this here to make sure that the following
8546   ;; dynamic binding isn't shadowed by autoloading.
8547   (require 'gnus-async)
8548   (require 'gnus-art)
8549   (let ((gnus-select-article-hook nil)  ;Disable hook.
8550         (gnus-article-prepare-hook nil)
8551         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8552         (gnus-use-article-prefetch nil)
8553         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8554         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8555         (gnus-visual nil)
8556         (gnus-keep-backlog nil)
8557         (gnus-break-pages nil)
8558         (gnus-summary-display-arrow nil)
8559         (gnus-updated-mode-lines nil)
8560         (gnus-auto-center-summary nil)
8561         (sum (current-buffer))
8562         (found nil)
8563         point treated)
8564     (gnus-save-hidden-threads
8565       (static-if (featurep 'xemacs)
8566           (let ((gnus-inhibit-treatment t))
8567             (setq treated (eq 'old (gnus-summary-select-article)))
8568             (when (and treated
8569                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8570                                  (window-live-p (get-buffer-window
8571                                                  gnus-article-buffer t)))))
8572               (gnus-summary-select-article nil t)
8573               (setq treated nil)))
8574         (let ((gnus-inhibit-treatment t)
8575               (x-face-mule-delete-x-face-field 'never))
8576           (setq treated (eq 'old (gnus-summary-select-article)))
8577           (when (and treated
8578                      (not
8579                       (and (gnus-buffer-live-p gnus-article-buffer)
8580                            (window-live-p (get-buffer-window
8581                                            gnus-article-buffer t))
8582                            (or (not (string-match "^\\^X-Face:" regexp))
8583                                (with-current-buffer gnus-article-buffer
8584                                  gnus-summary-search-article-matched-data)))))
8585             (gnus-summary-select-article nil t)
8586             (setq treated nil))))
8587       (set-buffer gnus-article-buffer)
8588       (widen)
8589       (if treated
8590           (progn
8591             (gnus-article-show-all-headers)
8592             (gnus-summary-search-article-position-point regexp backward))
8593         (goto-char (if backward (point-max) (point-min))))
8594       (while (not found)
8595         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8596         (if (if backward
8597                 (re-search-backward regexp nil t)
8598               (re-search-forward regexp nil t))
8599             ;; We found the regexp.
8600             (progn
8601               (gnus-summary-search-article-highlight-matched-text
8602                backward treated (string-match "^\\^X-Face:" regexp))
8603               (setq found 'found)
8604               (forward-line
8605                (/ (- 2 (window-height
8606                         (get-buffer-window gnus-article-buffer t)))
8607                   2))
8608               (set-window-start
8609                (get-buffer-window (current-buffer))
8610                (point))
8611               (set-buffer sum)
8612               (setq point (point)))
8613           ;; We didn't find it, so we go to the next article.
8614           (set-buffer sum)
8615           (setq found 'not)
8616           (while (eq found 'not)
8617             (if (not (if backward (gnus-summary-find-prev)
8618                        (gnus-summary-find-next)))
8619                 ;; No more articles.
8620                 (setq found t)
8621               ;; Select the next article and adjust point.
8622               (unless (gnus-summary-article-sparse-p
8623                        (gnus-summary-article-number))
8624                 (setq found nil)
8625                 (let ((gnus-inhibit-treatment t))
8626                   (gnus-summary-select-article))
8627                 (setq treated nil)
8628                 (set-buffer gnus-article-buffer)
8629                 (widen)
8630                 (goto-char (if backward (point-max) (point-min))))))))
8631       (gnus-message 7 ""))
8632     ;; Return whether we found the regexp.
8633     (when (eq found 'found)
8634       (goto-char point)
8635       (gnus-summary-show-thread)
8636       (gnus-summary-goto-subject gnus-current-article)
8637       (gnus-summary-position-point)
8638       t)))
8639
8640 (defun gnus-find-matching-articles (header regexp)
8641   "Return a list of all articles that match REGEXP on HEADER.
8642 This search includes all articles in the current group that Gnus has
8643 fetched headers for, whether they are displayed or not."
8644   (let ((articles nil)
8645         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8646         (case-fold-search t))
8647     (dolist (header gnus-newsgroup-headers)
8648       (when (string-match regexp (funcall func header))
8649         (push (mail-header-number header) articles)))
8650     (nreverse articles)))
8651
8652 (defun gnus-summary-find-matching (header regexp &optional backward unread
8653                                           not-case-fold not-matching)
8654   "Return a list of all articles that match REGEXP on HEADER.
8655 The search stars on the current article and goes forwards unless
8656 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8657 If UNREAD is non-nil, only unread articles will
8658 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8659 in the comparisons. If NOT-MATCHING, return a list of all articles that
8660 not match REGEXP on HEADER."
8661   (let ((case-fold-search (not not-case-fold))
8662         articles d func)
8663     (if (consp header)
8664         (if (eq (car header) 'extra)
8665             (setq func
8666                   `(lambda (h)
8667                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8668                          "")))
8669           (error "%s is an invalid header" header))
8670       (unless (fboundp (intern (concat "mail-header-" header)))
8671         (error "%s is not a valid header" header))
8672       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8673     (dolist (d (if (eq backward 'all)
8674                    gnus-newsgroup-data
8675                  (gnus-data-find-list
8676                   (gnus-summary-article-number)
8677                   (gnus-data-list backward))))
8678       (when (and (or (not unread)       ; We want all articles...
8679                      (gnus-data-unread-p d)) ; Or just unreads.
8680                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8681                  (if not-matching
8682                      (not (string-match
8683                            regexp
8684                            (funcall func (gnus-data-header d))))
8685                    (string-match regexp
8686                                  (funcall func (gnus-data-header d)))))
8687         (push (gnus-data-number d) articles))) ; Success!
8688     (nreverse articles)))
8689
8690 (defun gnus-summary-execute-command (header regexp command &optional backward)
8691   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8692 If HEADER is an empty string (or nil), the match is done on the entire
8693 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8694   (interactive
8695    (list (let ((completion-ignore-case t))
8696            (completing-read
8697             "Header name: "
8698             (mapcar (lambda (header) (list (format "%s" header)))
8699                     (append
8700                      '("Number" "Subject" "From" "Lines" "Date"
8701                        "Message-ID" "Xref" "References" "Body")
8702                      gnus-extra-headers))
8703             nil 'require-match))
8704          (read-string "Regexp: ")
8705          (read-key-sequence "Command: ")
8706          current-prefix-arg))
8707   (when (equal header "Body")
8708     (setq header ""))
8709   ;; Hidden thread subtrees must be searched as well.
8710   (gnus-summary-show-all-threads)
8711   ;; We don't want to change current point nor window configuration.
8712   (save-excursion
8713     (save-window-excursion
8714       (let (gnus-visual
8715             gnus-treat-strip-trailing-blank-lines
8716             gnus-treat-strip-leading-blank-lines
8717             gnus-treat-strip-multiple-blank-lines
8718             gnus-treat-hide-boring-headers
8719             gnus-treat-fold-newsgroups
8720             gnus-article-prepare-hook)
8721         (gnus-message 6 "Executing %s..." (key-description command))
8722         ;; We'd like to execute COMMAND interactively so as to give arguments.
8723         (gnus-execute header regexp
8724                       `(call-interactively ',(key-binding command))
8725                       backward)
8726         (gnus-message 6 "Executing %s...done" (key-description command))))))
8727
8728 (defun gnus-summary-beginning-of-article ()
8729   "Scroll the article back to the beginning."
8730   (interactive)
8731   (gnus-summary-select-article)
8732   (gnus-configure-windows 'article)
8733   (gnus-eval-in-buffer-window gnus-article-buffer
8734     (widen)
8735     (goto-char (point-min))
8736     (when gnus-break-pages
8737       (gnus-narrow-to-page))))
8738
8739 (defun gnus-summary-end-of-article ()
8740   "Scroll to the end of the article."
8741   (interactive)
8742   (gnus-summary-select-article)
8743   (gnus-configure-windows 'article)
8744   (gnus-eval-in-buffer-window gnus-article-buffer
8745     (widen)
8746     (goto-char (point-max))
8747     (recenter -3)
8748     (when gnus-break-pages
8749       (when (re-search-backward page-delimiter nil t)
8750         (narrow-to-region (match-end 0) (point-max)))
8751       (gnus-narrow-to-page))))
8752
8753 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8754   "Truncate to LEN and quote all \"(\"'s in STRING."
8755   (gnus-replace-in-string (if (and len (> (length string) len))
8756                               (substring string 0 len)
8757                             string)
8758                           "[()]" "\\\\\\&"))
8759
8760 (defun gnus-summary-print-article (&optional filename n)
8761   "Generate and print a PostScript image of the process-marked (mail) articles.
8762
8763 If used interactively, print the current article if none are
8764 process-marked.  With prefix arg, prompt the user for the name of the
8765 file to save in.
8766
8767 When used from Lisp, accept two optional args FILENAME and N.  N means
8768 to print the next N articles.  If N is negative, print the N previous
8769 articles.  If N is nil and articles have been marked with the process
8770 mark, print these instead.
8771
8772 If the optional first argument FILENAME is nil, send the image to the
8773 printer.  If FILENAME is a string, save the PostScript image in a file with
8774 that name.  If FILENAME is a number, prompt the user for the name of the file
8775 to save in."
8776   (interactive (list (ps-print-preprint current-prefix-arg)))
8777   (dolist (article (gnus-summary-work-articles n))
8778     (gnus-summary-select-article nil nil 'pseudo article)
8779     (gnus-eval-in-buffer-window gnus-article-buffer
8780       (gnus-print-buffer))
8781     (gnus-summary-remove-process-mark article))
8782   (ps-despool filename))
8783
8784 (defun gnus-print-buffer ()
8785   (let ((buffer (generate-new-buffer " *print*")))
8786     (unwind-protect
8787         (progn
8788           (copy-to-buffer buffer (point-min) (point-max))
8789           (set-buffer buffer)
8790           (gnus-remove-text-with-property 'gnus-decoration)
8791           (when (gnus-visual-p 'article-highlight 'highlight)
8792             ;; Copy-to-buffer doesn't copy overlay.  So redo
8793             ;; highlight.
8794             (let ((gnus-article-buffer buffer))
8795               (gnus-article-highlight-citation t)
8796               (gnus-article-highlight-signature)
8797               (gnus-article-emphasize)
8798               (gnus-article-delete-invisible-text)))
8799           (let ((ps-left-header
8800                  (list
8801                   (concat "("
8802                           (gnus-summary-print-truncate-and-quote
8803                            (mail-header-subject gnus-current-headers)
8804                            66) ")")
8805                   (concat "("
8806                           (gnus-summary-print-truncate-and-quote
8807                            (mail-header-from gnus-current-headers)
8808                            45) ")")))
8809                 (ps-right-header
8810                  (list
8811                   "/pagenumberstring load"
8812                   (concat "("
8813                           (mail-header-date gnus-current-headers) ")"))))
8814             (gnus-run-hooks 'gnus-ps-print-hook)
8815             (save-excursion
8816               (if window-system
8817                   (ps-spool-buffer-with-faces)
8818                 (ps-spool-buffer)))))
8819       (kill-buffer buffer))))
8820
8821 (defun gnus-summary-show-article (&optional arg)
8822   "Force redisplaying of the current article.
8823 If ARG (the prefix) is a number, show the article with the charset
8824 defined in `gnus-summary-show-article-charset-alist', or the charset
8825 input.
8826 If ARG (the prefix) is non-nil and not a number, show the raw article
8827 without any article massaging functions being run.  Normally, the key
8828 strokes are `C-u g'."
8829   (interactive "P")
8830   (cond
8831    ((numberp arg)
8832     (gnus-summary-show-article t)
8833     (let* ((gnus-newsgroup-charset
8834             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8835                 (mm-read-coding-system
8836                  "View as charset: " ;; actually it is coding system.
8837                  (save-excursion
8838                    (set-buffer gnus-article-buffer)
8839                    (mm-detect-coding-region (point) (point-max))))))
8840            (default-mime-charset gnus-newsgroup-charset)
8841            (gnus-newsgroup-ignored-charsets 'gnus-all))
8842       (gnus-summary-select-article nil 'force)
8843       (let ((deps gnus-newsgroup-dependencies)
8844             head header lines)
8845         (save-excursion
8846           (set-buffer gnus-original-article-buffer)
8847           (save-restriction
8848             (message-narrow-to-head)
8849             (setq head (buffer-string))
8850             (goto-char (point-min))
8851             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8852               (goto-char (point-max))
8853               (widen)
8854               (setq lines (1- (count-lines (point) (point-max))))))
8855           (with-temp-buffer
8856             (insert (format "211 %d Article retrieved.\n"
8857                             (cdr gnus-article-current)))
8858             (insert head)
8859             (if lines (insert (format "Lines: %d\n" lines)))
8860             (insert ".\n")
8861             (let ((nntp-server-buffer (current-buffer)))
8862               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8863         (gnus-data-set-header
8864          (gnus-data-find (cdr gnus-article-current))
8865          header)
8866         (gnus-summary-update-article-line
8867          (cdr gnus-article-current) header)
8868         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8869           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8870    ((not arg)
8871     ;; Select the article the normal way.
8872     (gnus-summary-select-article nil 'force))
8873    (t
8874     ;; We have to require this here to make sure that the following
8875     ;; dynamic binding isn't shadowed by autoloading.
8876     (require 'gnus-async)
8877     (require 'gnus-art)
8878     ;; Bind the article treatment functions to nil.
8879     (let ((gnus-have-all-headers t)
8880           gnus-article-prepare-hook
8881           gnus-article-decode-hook
8882           gnus-break-pages
8883           gnus-show-mime
8884           (gnus-inhibit-treatment t))
8885       (gnus-summary-select-article nil 'force))))
8886   (gnus-summary-goto-subject gnus-current-article)
8887   (gnus-summary-position-point))
8888
8889 (defun gnus-summary-show-raw-article ()
8890   "Show the raw article without any article massaging functions being run."
8891   (interactive)
8892   (gnus-summary-show-article t))
8893
8894 (defun gnus-summary-verbose-headers (&optional arg)
8895   "Toggle permanent full header display.
8896 If ARG is a positive number, turn header display on.
8897 If ARG is a negative number, turn header display off."
8898   (interactive "P")
8899   (setq gnus-show-all-headers
8900         (cond ((or (not (numberp arg))
8901                    (zerop arg))
8902                (not gnus-show-all-headers))
8903               ((natnump arg)
8904                t)))
8905   (gnus-summary-show-article))
8906
8907 (defun gnus-summary-toggle-header (&optional arg)
8908   "Show the headers if they are hidden, or hide them if they are shown.
8909 If ARG is a positive number, show the entire header.
8910 If ARG is a negative number, hide the unwanted header lines."
8911   (interactive "P")
8912   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8913                      (get-buffer-window gnus-article-buffer t))))
8914     (with-current-buffer gnus-article-buffer
8915       (widen)
8916       (article-narrow-to-head)
8917       (let* ((buffer-read-only nil)
8918              (inhibit-point-motion-hooks t)
8919              (hidden (if (numberp arg)
8920                          (>= arg 0)
8921                        (gnus-article-hidden-text-p 'headers)))
8922              s e)
8923         (delete-region (point-min) (point-max))
8924         (with-current-buffer gnus-original-article-buffer
8925           (goto-char (setq s (point-min)))
8926           (setq e (if (search-forward "\n\n" nil t)
8927                       (1- (point))
8928                     (point-max))))
8929         (insert-buffer-substring gnus-original-article-buffer s e)
8930         ;; In T-gnus, gnus-article-decode-hook doesn't contain
8931         ;; article-decode-encoded-words by default.
8932         (article-decode-encoded-words)
8933         (run-hooks 'gnus-article-decode-hook)
8934         (if hidden
8935             (let ((gnus-treat-hide-headers nil)
8936                   (gnus-treat-hide-boring-headers nil))
8937               (gnus-delete-wash-type 'headers)
8938               (gnus-treat-article 'head))
8939           (gnus-treat-article 'head))
8940         (widen)
8941         (if window
8942             (set-window-start window (goto-char (point-min))))
8943         (if gnus-break-pages
8944             (gnus-narrow-to-page)
8945           (when (gnus-visual-p 'page-marker)
8946             (let ((buffer-read-only nil))
8947               (gnus-remove-text-with-property 'gnus-prev)
8948               (gnus-remove-text-with-property 'gnus-next))))
8949         (gnus-set-mode-line 'article)))))
8950
8951 (defun gnus-summary-show-all-headers ()
8952   "Make all header lines visible."
8953   (interactive)
8954   (gnus-summary-toggle-header 1))
8955
8956 (defun gnus-summary-toggle-mime (&optional arg)
8957   "Toggle MIME processing.
8958 If ARG is a positive number, turn MIME processing on."
8959   (interactive "P")
8960   (setq gnus-show-mime
8961         (if (null arg)
8962             (not gnus-show-mime)
8963           (> (prefix-numeric-value arg) 0)))
8964   (gnus-summary-select-article t 'force))
8965
8966 (defun gnus-summary-caesar-message (&optional arg)
8967   "Caesar rotate the current article by 13.
8968 The numerical prefix specifies how many places to rotate each letter
8969 forward."
8970   (interactive "P")
8971   (gnus-summary-select-article)
8972   (let ((mail-header-separator ""))
8973     (gnus-eval-in-buffer-window gnus-article-buffer
8974       (save-restriction
8975         (widen)
8976         (let ((start (window-start))
8977               buffer-read-only)
8978           (message-caesar-buffer-body arg)
8979           (set-window-start (get-buffer-window (current-buffer)) start))))))
8980
8981 (autoload 'unmorse-region "morse"
8982   "Convert morse coded text in region to ordinary ASCII text."
8983   t)
8984
8985 (defun gnus-summary-morse-message (&optional arg)
8986   "Morse decode the current article."
8987   (interactive "P")
8988   (gnus-summary-select-article)
8989   (let ((mail-header-separator ""))
8990     (gnus-eval-in-buffer-window gnus-article-buffer
8991       (save-excursion
8992         (save-restriction
8993           (widen)
8994           (let ((pos (window-start))
8995                 buffer-read-only)
8996             (goto-char (point-min))
8997             (when (message-goto-body)
8998               (gnus-narrow-to-body))
8999             (goto-char (point-min))
9000             (while (re-search-forward "·" (point-max) t)
9001               (replace-match "."))
9002             (unmorse-region (point-min) (point-max))
9003             (widen)
9004             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9005
9006 (defun gnus-summary-stop-page-breaking ()
9007   "Stop page breaking in the current article."
9008   (interactive)
9009   (gnus-summary-select-article)
9010   (gnus-eval-in-buffer-window gnus-article-buffer
9011     (widen)
9012     (when (gnus-visual-p 'page-marker)
9013       (let ((buffer-read-only nil))
9014         (gnus-remove-text-with-property 'gnus-prev)
9015         (gnus-remove-text-with-property 'gnus-next))
9016       (setq gnus-page-broken nil))))
9017
9018 (defun gnus-summary-move-article (&optional n to-newsgroup
9019                                             select-method action)
9020   "Move the current article to a different newsgroup.
9021 If N is a positive number, move the N next articles.
9022 If N is a negative number, move the N previous articles.
9023 If N is nil and any articles have been marked with the process mark,
9024 move those articles instead.
9025 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9026 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9027 re-spool using this method.
9028
9029 When called interactively with TO-NEWSGROUP being nil, the value of
9030 the variable `gnus-move-split-methods' is used for finding a default
9031 for the target newsgroup.
9032
9033 For this function to work, both the current newsgroup and the
9034 newsgroup that you want to move to have to support the `request-move'
9035 and `request-accept' functions.
9036
9037 ACTION can be either `move' (the default), `crosspost' or `copy'."
9038   (interactive "P")
9039   (unless action
9040     (setq action 'move))
9041   ;; Check whether the source group supports the required functions.
9042   (cond ((and (eq action 'move)
9043               (not (gnus-check-backend-function
9044                     'request-move-article gnus-newsgroup-name)))
9045          (error "The current group does not support article moving"))
9046         ((and (eq action 'crosspost)
9047               (not (gnus-check-backend-function
9048                     'request-replace-article gnus-newsgroup-name)))
9049          (error "The current group does not support article editing")))
9050   (let ((articles (gnus-summary-work-articles n))
9051         (prefix (if (gnus-check-backend-function
9052                      'request-move-article gnus-newsgroup-name)
9053                     (funcall gnus-move-group-prefix-function
9054                              gnus-newsgroup-name)
9055                   ""))
9056         (names '((move "Move" "Moving")
9057                  (copy "Copy" "Copying")
9058                  (crosspost "Crosspost" "Crossposting")))
9059         (copy-buf (save-excursion
9060                     (nnheader-set-temp-buffer " *copy article*")))
9061         art-group to-method new-xref article to-groups)
9062     (unless (assq action names)
9063       (error "Unknown action %s" action))
9064     ;; Read the newsgroup name.
9065     (when (and (not to-newsgroup)
9066                (not select-method))
9067       (if (and gnus-move-split-methods
9068                (not
9069                 (and (memq gnus-current-article articles)
9070                      (gnus-buffer-live-p gnus-original-article-buffer))))
9071           ;; When `gnus-move-split-methods' is non-nil, we have to
9072           ;; select an article to give `gnus-read-move-group-name' an
9073           ;; opportunity to suggest an appropriate default.  However,
9074           ;; we needn't render or mark the article.
9075           (let ((gnus-display-mime-function nil)
9076                 (gnus-article-prepare-hook nil)
9077                 (gnus-mark-article-hook nil))
9078             (gnus-summary-select-article nil nil nil (car articles))))
9079       (setq to-newsgroup
9080             (gnus-read-move-group-name
9081              (cadr (assq action names))
9082              (symbol-value (intern (format "gnus-current-%s-group" action)))
9083              articles prefix))
9084       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9085     (setq to-method (or select-method
9086                         (gnus-server-to-method
9087                          (gnus-group-method to-newsgroup))))
9088     ;; Check the method we are to move this article to...
9089     (unless (gnus-check-backend-function
9090              'request-accept-article (car to-method))
9091       (error "%s does not support article copying" (car to-method)))
9092     (unless (gnus-check-server to-method)
9093       (error "Can't open server %s" (car to-method)))
9094     (gnus-message 6 "%s to %s: %s..."
9095                   (caddr (assq action names))
9096                   (or (car select-method) to-newsgroup) articles)
9097     (while articles
9098       (setq article (pop articles))
9099       (setq
9100        art-group
9101        (cond
9102         ;; Move the article.
9103         ((eq action 'move)
9104          ;; Remove this article from future suppression.
9105          (gnus-dup-unsuppress-article article)
9106          (gnus-request-move-article
9107           article                       ; Article to move
9108           gnus-newsgroup-name           ; From newsgroup
9109           (nth 1 (gnus-find-method-for-group
9110                   gnus-newsgroup-name)) ; Server
9111           (list 'gnus-request-accept-article
9112                 to-newsgroup (list 'quote select-method)
9113                 (not articles) t)       ; Accept form
9114           (not articles)))              ; Only save nov last time
9115         ;; Copy the article.
9116         ((eq action 'copy)
9117          (save-excursion
9118            (set-buffer copy-buf)
9119            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9120              (gnus-request-accept-article
9121               to-newsgroup select-method (not articles) t))))
9122         ;; Crosspost the article.
9123         ((eq action 'crosspost)
9124          (let ((xref (message-tokenize-header
9125                       (mail-header-xref (gnus-summary-article-header article))
9126                       " ")))
9127            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9128                                   ":" (number-to-string article)))
9129            (unless xref
9130              (setq xref (list (system-name))))
9131            (setq new-xref
9132                  (concat
9133                   (mapconcat 'identity
9134                              (delete "Xref:" (delete new-xref xref))
9135                              " ")
9136                   " " new-xref))
9137            (save-excursion
9138              (set-buffer copy-buf)
9139              ;; First put the article in the destination group.
9140              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9141              (when (consp (setq art-group
9142                                 (gnus-request-accept-article
9143                                  to-newsgroup select-method (not articles))))
9144                (setq new-xref (concat new-xref " " (car art-group)
9145                                       ":"
9146                                       (number-to-string (cdr art-group))))
9147                ;; Now we have the new Xrefs header, so we insert
9148                ;; it and replace the new article.
9149                (nnheader-replace-header "Xref" new-xref)
9150                (gnus-request-replace-article
9151                 (cdr art-group) to-newsgroup (current-buffer))
9152                art-group))))))
9153       (cond
9154        ((not art-group)
9155         (gnus-message 1 "Couldn't %s article %s: %s"
9156                       (cadr (assq action names)) article
9157                       (nnheader-get-report (car to-method))))
9158        ((eq art-group 'junk)
9159         (when (eq action 'move)
9160           (gnus-summary-mark-article article gnus-canceled-mark)
9161           (gnus-message 4 "Deleted article %s" article)
9162           ;; run the delete hook
9163           (run-hook-with-args 'gnus-summary-article-delete-hook
9164                               action
9165                               (gnus-data-header
9166                                (assoc article (gnus-data-list nil)))
9167                               gnus-newsgroup-name nil
9168                               select-method)))
9169        (t
9170         (let* ((pto-group (gnus-group-prefixed-name
9171                            (car art-group) to-method))
9172                (info (gnus-get-info pto-group))
9173                (to-group (gnus-info-group info))
9174                to-marks)
9175           ;; Update the group that has been moved to.
9176           (when (and info
9177                      (memq action '(move copy)))
9178             (unless (member to-group to-groups)
9179               (push to-group to-groups))
9180
9181             (unless (memq article gnus-newsgroup-unreads)
9182               (push 'read to-marks)
9183               (gnus-info-set-read
9184                info (gnus-add-to-range (gnus-info-read info)
9185                                        (list (cdr art-group)))))
9186
9187             ;; See whether the article is to be put in the cache.
9188             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9189                              gnus-article-mark-lists
9190                            (delete '(expirable . expire)
9191                                    (copy-sequence gnus-article-mark-lists))))
9192                   (to-article (cdr art-group)))
9193
9194               ;; Enter the article into the cache in the new group,
9195               ;; if that is required.
9196               (when gnus-use-cache
9197                 (gnus-cache-possibly-enter-article
9198                  to-group to-article
9199                  (let ((header (copy-sequence
9200                                 (gnus-summary-article-header article))))
9201                    (mail-header-set-number header to-article)
9202                    header)
9203                  (memq article gnus-newsgroup-marked)
9204                  (memq article gnus-newsgroup-dormant)
9205                  (memq article gnus-newsgroup-unreads)))
9206
9207               (when gnus-preserve-marks
9208                 ;; Copy any marks over to the new group.
9209                 (when (and (equal to-group gnus-newsgroup-name)
9210                            (not (memq article gnus-newsgroup-unreads)))
9211                   ;; Mark this article as read in this group.
9212                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9213                   (setcdr (gnus-active to-group) to-article)
9214                   (setcdr gnus-newsgroup-active to-article))
9215
9216                 (while marks
9217                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9218                     (when (memq article (symbol-value
9219                                          (intern (format "gnus-newsgroup-%s"
9220                                                          (caar marks)))))
9221                       (push (cdar marks) to-marks)
9222                       ;; If the other group is the same as this group,
9223                       ;; then we have to add the mark to the list.
9224                       (when (equal to-group gnus-newsgroup-name)
9225                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9226                              (cons to-article
9227                                    (symbol-value
9228                                     (intern (format "gnus-newsgroup-%s"
9229                                                     (caar marks)))))))
9230                       ;; Copy the marks to other group.
9231                       (gnus-add-marked-articles
9232                        to-group (cdar marks) (list to-article) info)))
9233                   (setq marks (cdr marks)))
9234
9235                 (gnus-request-set-mark
9236                  to-group (list (list (list to-article) 'add to-marks))))
9237
9238               (gnus-dribble-enter
9239                (concat "(gnus-group-set-info '"
9240                        (gnus-prin1-to-string (gnus-get-info to-group))
9241                        ")"))))
9242
9243           ;; Update the Xref header in this article to point to
9244           ;; the new crossposted article we have just created.
9245           (when (eq action 'crosspost)
9246             (save-excursion
9247               (set-buffer copy-buf)
9248               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9249               (nnheader-replace-header "Xref" new-xref)
9250               (gnus-request-replace-article
9251                article gnus-newsgroup-name (current-buffer))))
9252
9253           ;; run the move/copy/crosspost/respool hook
9254           (run-hook-with-args 'gnus-summary-article-move-hook
9255                               action
9256                               (gnus-data-header
9257                                (assoc article (gnus-data-list nil)))
9258                               gnus-newsgroup-name
9259                               to-newsgroup
9260                               select-method))
9261
9262         ;;;!!!Why is this necessary?
9263         (set-buffer gnus-summary-buffer)
9264         
9265         (gnus-summary-goto-subject article)
9266         (when (eq action 'move)
9267           (gnus-summary-mark-article article gnus-canceled-mark))))
9268       (gnus-summary-remove-process-mark article))
9269     ;; Re-activate all groups that have been moved to.
9270     (save-excursion
9271       (set-buffer gnus-group-buffer)
9272       (let ((gnus-group-marked to-groups))
9273         (gnus-group-get-new-news-this-group nil t)))
9274
9275     (gnus-kill-buffer copy-buf)
9276     (gnus-summary-position-point)
9277     (gnus-set-mode-line 'summary)))
9278
9279 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9280   "Move the current article to a different newsgroup.
9281 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9282 When called interactively, if TO-NEWSGROUP is nil, use the value of
9283 the variable `gnus-move-split-methods' for finding a default target
9284 newsgroup.
9285 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9286 re-spool using this method."
9287   (interactive "P")
9288   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9289
9290 (defun gnus-summary-crosspost-article (&optional n)
9291   "Crosspost the current article to some other group."
9292   (interactive "P")
9293   (gnus-summary-move-article n nil nil 'crosspost))
9294
9295 (defcustom gnus-summary-respool-default-method nil
9296   "Default method type for respooling an article.
9297 If nil, use to the current newsgroup method."
9298   :type 'symbol
9299   :group 'gnus-summary-mail)
9300
9301 (defcustom gnus-summary-display-while-building nil
9302   "If non-nil, show and update the summary buffer as it's being built.
9303 If the value is t, update the buffer after every line is inserted.  If
9304 the value is an integer (N), update the display every N lines."
9305   :group 'gnus-thread
9306   :type '(choice (const :tag "off" nil)
9307                  number
9308                  (const :tag "frequently" t)))
9309
9310 (defun gnus-summary-respool-article (&optional n method)
9311   "Respool the current article.
9312 The article will be squeezed through the mail spooling process again,
9313 which means that it will be put in some mail newsgroup or other
9314 depending on `nnmail-split-methods'.
9315 If N is a positive number, respool the N next articles.
9316 If N is a negative number, respool the N previous articles.
9317 If N is nil and any articles have been marked with the process mark,
9318 respool those articles instead.
9319
9320 Respooling can be done both from mail groups and \"real\" newsgroups.
9321 In the former case, the articles in question will be moved from the
9322 current group into whatever groups they are destined to.  In the
9323 latter case, they will be copied into the relevant groups."
9324   (interactive
9325    (list current-prefix-arg
9326          (let* ((methods (gnus-methods-using 'respool))
9327                 (methname
9328                  (symbol-name (or gnus-summary-respool-default-method
9329                                   (car (gnus-find-method-for-group
9330                                         gnus-newsgroup-name)))))
9331                 (method
9332                  (gnus-completing-read-with-default
9333                   methname "What backend do you want to use when respooling?"
9334                   methods nil t nil 'gnus-mail-method-history))
9335                 ms)
9336            (cond
9337             ((zerop (length (setq ms (gnus-servers-using-backend
9338                                       (intern method)))))
9339              (list (intern method) ""))
9340             ((= 1 (length ms))
9341              (car ms))
9342             (t
9343              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9344                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9345                            ms-alist))))))))
9346   (unless method
9347     (error "No method given for respooling"))
9348   (if (assoc (symbol-name
9349               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9350              (gnus-methods-using 'respool))
9351       (gnus-summary-move-article n nil method)
9352     (gnus-summary-copy-article n nil method)))
9353
9354 (defun gnus-summary-import-article (file &optional edit)
9355   "Import an arbitrary file into a mail newsgroup."
9356   (interactive "fImport file: \nP")
9357   (let ((group gnus-newsgroup-name)
9358         (now (current-time))
9359         atts lines group-art)
9360     (unless (gnus-check-backend-function 'request-accept-article group)
9361       (error "%s does not support article importing" group))
9362     (or (file-readable-p file)
9363         (not (file-regular-p file))
9364         (error "Can't read %s" file))
9365     (save-excursion
9366       (set-buffer (gnus-get-buffer-create " *import file*"))
9367       (erase-buffer)
9368       (nnheader-insert-file-contents file)
9369       (goto-char (point-min))
9370       (if (nnheader-article-p)
9371           (save-restriction
9372             (goto-char (point-min))
9373             (search-forward "\n\n" nil t)
9374             (narrow-to-region (point-min) (1- (point)))
9375             (goto-char (point-min))
9376             (unless (re-search-forward "^date:" nil t)
9377               (goto-char (point-max))
9378               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9379         ;; This doesn't look like an article, so we fudge some headers.
9380         (setq atts (file-attributes file)
9381               lines (count-lines (point-min) (point-max)))
9382         (insert "From: " (read-string "From: ") "\n"
9383                 "Subject: " (read-string "Subject: ") "\n"
9384                 "Date: " (message-make-date (nth 5 atts)) "\n"
9385                 "Message-ID: " (message-make-message-id) "\n"
9386                 "Lines: " (int-to-string lines) "\n"
9387                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9388       (setq group-art (gnus-request-accept-article group nil t))
9389       (kill-buffer (current-buffer)))
9390     (setq gnus-newsgroup-active (gnus-activate-group group))
9391     (forward-line 1)
9392     (gnus-summary-goto-article (cdr group-art) nil t)
9393     (when edit
9394       (gnus-summary-edit-article))))
9395
9396 (defun gnus-summary-create-article ()
9397   "Create an article in a mail newsgroup."
9398   (interactive)
9399   (let ((group gnus-newsgroup-name)
9400         (now (current-time))
9401         group-art)
9402     (unless (gnus-check-backend-function 'request-accept-article group)
9403       (error "%s does not support article importing" group))
9404     (save-excursion
9405       (set-buffer (gnus-get-buffer-create " *import file*"))
9406       (erase-buffer)
9407       (goto-char (point-min))
9408       ;; This doesn't look like an article, so we fudge some headers.
9409       (insert "From: " (read-string "From: ") "\n"
9410               "Subject: " (read-string "Subject: ") "\n"
9411               "Date: " (message-make-date now) "\n"
9412               "Message-ID: " (message-make-message-id) "\n")
9413       (setq group-art (gnus-request-accept-article group nil t))
9414       (kill-buffer (current-buffer)))
9415     (setq gnus-newsgroup-active (gnus-activate-group group))
9416     (forward-line 1)
9417     (gnus-summary-goto-article (cdr group-art) nil t)
9418     (gnus-summary-edit-article)))
9419
9420 (defun gnus-summary-article-posted-p ()
9421   "Say whether the current (mail) article is available from news as well.
9422 This will be the case if the article has both been mailed and posted."
9423   (interactive)
9424   (let ((id (mail-header-references (gnus-summary-article-header)))
9425         (gnus-override-method (car (gnus-refer-article-methods))))
9426     (if (gnus-request-head id "")
9427         (gnus-message 2 "The current message was found on %s"
9428                       gnus-override-method)
9429       (gnus-message 2 "The current message couldn't be found on %s"
9430                     gnus-override-method)
9431       nil)))
9432
9433 (defun gnus-summary-expire-articles (&optional now)
9434   "Expire all articles that are marked as expirable in the current group."
9435   (interactive)
9436   (when (and (not gnus-group-is-exiting-without-update-p)
9437              (gnus-check-backend-function
9438               'request-expire-articles gnus-newsgroup-name))
9439     ;; This backend supports expiry.
9440     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9441            (expirable (if total
9442                           (progn
9443                             ;; We need to update the info for
9444                             ;; this group for `gnus-list-of-read-articles'
9445                             ;; to give us the right answer.
9446                             (gnus-run-hooks 'gnus-exit-group-hook)
9447                             (gnus-summary-update-info)
9448                             (gnus-list-of-read-articles gnus-newsgroup-name))
9449                         (setq gnus-newsgroup-expirable
9450                               (sort gnus-newsgroup-expirable '<))))
9451            (expiry-wait (if now 'immediate
9452                           (gnus-group-find-parameter
9453                            gnus-newsgroup-name 'expiry-wait)))
9454            (nnmail-expiry-target
9455             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9456                 nnmail-expiry-target))
9457            es)
9458       (when expirable
9459         ;; There are expirable articles in this group, so we run them
9460         ;; through the expiry process.
9461         (gnus-message 6 "Expiring articles...")
9462         (unless (gnus-check-group gnus-newsgroup-name)
9463           (error "Can't open server for %s" gnus-newsgroup-name))
9464         ;; The list of articles that weren't expired is returned.
9465         (save-excursion
9466           (if expiry-wait
9467               (let ((nnmail-expiry-wait-function nil)
9468                     (nnmail-expiry-wait expiry-wait))
9469                 (setq es (gnus-request-expire-articles
9470                           expirable gnus-newsgroup-name)))
9471             (setq es (gnus-request-expire-articles
9472                       expirable gnus-newsgroup-name)))
9473           (unless total
9474             (setq gnus-newsgroup-expirable es))
9475           ;; We go through the old list of expirable, and mark all
9476           ;; really expired articles as nonexistent.
9477           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9478             (let ((gnus-use-cache nil))
9479               (dolist (article expirable)
9480                 (when (and (not (memq article es))
9481                            (gnus-data-find article))
9482                   (gnus-summary-mark-article article gnus-canceled-mark)
9483                   (run-hook-with-args 'gnus-summary-article-expire-hook
9484                                       'delete
9485                                       (gnus-data-header
9486                                        (assoc article (gnus-data-list nil)))
9487                                       gnus-newsgroup-name
9488                                       nil
9489                                       nil))))))
9490         (gnus-message 6 "Expiring articles...done")))))
9491
9492 (defun gnus-summary-expire-articles-now ()
9493   "Expunge all expirable articles in the current group.
9494 This means that *all* articles that are marked as expirable will be
9495 deleted forever, right now."
9496   (interactive)
9497   (or gnus-expert-user
9498       (gnus-yes-or-no-p
9499        "Are you really, really, really sure you want to delete all these messages? ")
9500       (error "Phew!"))
9501   (gnus-summary-expire-articles t))
9502
9503 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9504 (defun gnus-summary-delete-article (&optional n)
9505   "Delete the N next (mail) articles.
9506 This command actually deletes articles.  This is not a marking
9507 command.  The article will disappear forever from your life, never to
9508 return.
9509
9510 If N is negative, delete backwards.
9511 If N is nil and articles have been marked with the process mark,
9512 delete these instead.
9513
9514 If `gnus-novice-user' is non-nil you will be asked for
9515 confirmation before the articles are deleted."
9516   (interactive "P")
9517   (unless (gnus-check-backend-function 'request-expire-articles
9518                                        gnus-newsgroup-name)
9519     (error "The current newsgroup does not support article deletion"))
9520   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9521     (error "Couldn't open server"))
9522   ;; Compute the list of articles to delete.
9523   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9524         (nnmail-expiry-target 'delete)
9525         not-deleted)
9526     (if (and gnus-novice-user
9527              (not (gnus-yes-or-no-p
9528                    (format "Do you really want to delete %s forever? "
9529                            (if (> (length articles) 1)
9530                                (format "these %s articles" (length articles))
9531                              "this article")))))
9532         ()
9533       ;; Delete the articles.
9534       (setq not-deleted (gnus-request-expire-articles
9535                          articles gnus-newsgroup-name 'force))
9536       (while articles
9537         (gnus-summary-remove-process-mark (car articles))
9538         ;; The backend might not have been able to delete the article
9539         ;; after all.
9540         (unless (memq (car articles) not-deleted)
9541           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9542         (let* ((article (car articles))
9543                (ghead  (gnus-data-header
9544                                     (assoc article (gnus-data-list nil)))))
9545           (run-hook-with-args 'gnus-summary-article-delete-hook
9546                               'delete ghead gnus-newsgroup-name nil
9547                               nil))
9548         (setq articles (cdr articles)))
9549       (when not-deleted
9550         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9551     (gnus-summary-position-point)
9552     (gnus-set-mode-line 'summary)
9553     not-deleted))
9554
9555 (defun gnus-summary-edit-article (&optional force)
9556   "Edit the current article.
9557 This will have permanent effect only in mail groups.
9558 If FORCE is non-nil, allow editing of articles even in read-only
9559 groups."
9560   (interactive "P")
9561   (save-excursion
9562     (set-buffer gnus-summary-buffer)
9563     (let ((mail-parse-charset gnus-newsgroup-charset)
9564           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9565       (gnus-set-global-variables)
9566       (when (and (not force)
9567                  (gnus-group-read-only-p))
9568         (error "The current newsgroup does not support article editing"))
9569       (gnus-summary-show-article t)
9570       (gnus-article-edit-article
9571        'ignore
9572        `(lambda (no-highlight)
9573           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9574                 (message-options message-options)
9575                 (message-options-set-recipient)
9576                 (mail-parse-ignored-charsets
9577                  ',gnus-newsgroup-ignored-charsets))
9578             (gnus-summary-edit-article-done
9579              ,(or (mail-header-references gnus-current-headers) "")
9580              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9581
9582 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9583
9584 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9585                                                  no-highlight)
9586   "Make edits to the current article permanent."
9587   (interactive)
9588   (save-excursion
9589     ;; The buffer restriction contains the entire article if it exists.
9590     (when (article-goto-body)
9591       (let ((lines (count-lines (point) (point-max)))
9592             (length (- (point-max) (point)))
9593             (case-fold-search t)
9594             (body (copy-marker (point))))
9595         (goto-char (point-min))
9596         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9597           (delete-region (match-beginning 1) (match-end 1))
9598           (insert (number-to-string length)))
9599         (goto-char (point-min))
9600         (when (re-search-forward
9601                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9602           (delete-region (match-beginning 1) (match-end 1))
9603           (insert (number-to-string length)))
9604         (goto-char (point-min))
9605         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9606           (delete-region (match-beginning 1) (match-end 1))
9607           (insert (number-to-string lines))))))
9608   ;; Replace the article.
9609   (let ((buf (current-buffer)))
9610     (with-temp-buffer
9611       (insert-buffer-substring buf)
9612
9613       (if (and (not read-only)
9614                (not (gnus-request-replace-article
9615                      (cdr gnus-article-current) (car gnus-article-current)
9616                      (current-buffer) t)))
9617           (error "Couldn't replace article")
9618         ;; Update the summary buffer.
9619         (if (and references
9620                  (equal (message-tokenize-header references " ")
9621                         (message-tokenize-header
9622                          (or (message-fetch-field "references") "") " ")))
9623             ;; We only have to update this line.
9624             (save-excursion
9625               (save-restriction
9626                 (message-narrow-to-head)
9627                 (let ((head (buffer-string))
9628                       header)
9629                   (with-temp-buffer
9630                     (insert (format "211 %d Article retrieved.\n"
9631                                     (cdr gnus-article-current)))
9632                     (insert head)
9633                     (insert ".\n")
9634                     (let ((nntp-server-buffer (current-buffer)))
9635                       (setq header (car (gnus-get-newsgroup-headers
9636                                          nil t))))
9637                     (save-excursion
9638                       (set-buffer gnus-summary-buffer)
9639                       (gnus-data-set-header
9640                        (gnus-data-find (cdr gnus-article-current))
9641                        header)
9642                       (gnus-summary-update-article-line
9643                        (cdr gnus-article-current) header)
9644                       (if (gnus-summary-goto-subject
9645                            (cdr gnus-article-current) nil t)
9646                           (gnus-summary-update-secondary-mark
9647                            (cdr gnus-article-current))))))))
9648           ;; Update threads.
9649           (set-buffer (or buffer gnus-summary-buffer))
9650           (gnus-summary-update-article (cdr gnus-article-current))
9651           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9652               (gnus-summary-update-secondary-mark
9653                (cdr gnus-article-current))))
9654         ;; Prettify the article buffer again.
9655         (unless no-highlight
9656           (save-excursion
9657             (set-buffer gnus-article-buffer)
9658             ;;;!!! Fix this -- article should be rehighlighted.
9659             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9660             (set-buffer gnus-original-article-buffer)
9661             (gnus-request-article
9662              (cdr gnus-article-current)
9663              (car gnus-article-current) (current-buffer))))
9664         ;; Prettify the summary buffer line.
9665         (when (gnus-visual-p 'summary-highlight 'highlight)
9666           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9667
9668 (defun gnus-summary-edit-wash (key)
9669   "Perform editing command KEY in the article buffer."
9670   (interactive
9671    (list
9672     (progn
9673       (message "%s" (concat (this-command-keys) "- "))
9674       (read-char))))
9675   (message "")
9676   (gnus-summary-edit-article)
9677   (execute-kbd-macro (concat (this-command-keys) key))
9678   (gnus-article-edit-done))
9679
9680 ;;; Respooling
9681
9682 (defun gnus-summary-respool-query (&optional silent trace)
9683   "Query where the respool algorithm would put this article."
9684   (interactive)
9685   (let (gnus-mark-article-hook)
9686     (gnus-summary-select-article)
9687     (save-excursion
9688       (set-buffer gnus-original-article-buffer)
9689       (let ((groups (nnmail-article-group 'identity trace)))
9690         (unless silent
9691           (if groups
9692               (message "This message would go to %s"
9693                        (mapconcat 'car groups ", "))
9694             (message "This message would go to no groups"))
9695           groups)))))
9696
9697 (defun gnus-summary-respool-trace ()
9698   "Trace where the respool algorithm would put this article.
9699 Display a buffer showing all fancy splitting patterns which matched."
9700   (interactive)
9701   (gnus-summary-respool-query nil t))
9702
9703 ;; Summary marking commands.
9704
9705 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9706   "Mark articles which has the same subject as read, and then select the next.
9707 If UNMARK is positive, remove any kind of mark.
9708 If UNMARK is negative, tick articles."
9709   (interactive "P")
9710   (when unmark
9711     (setq unmark (prefix-numeric-value unmark)))
9712   (let ((count
9713          (gnus-summary-mark-same-subject
9714           (gnus-summary-article-subject) unmark)))
9715     ;; Select next unread article.  If auto-select-same mode, should
9716     ;; select the first unread article.
9717     (gnus-summary-next-article t (and gnus-auto-select-same
9718                                       (gnus-summary-article-subject)))
9719     (gnus-message 7 "%d article%s marked as %s"
9720                   count (if (= count 1) " is" "s are")
9721                   (if unmark "unread" "read"))))
9722
9723 (defun gnus-summary-kill-same-subject (&optional unmark)
9724   "Mark articles which has the same subject as read.
9725 If UNMARK is positive, remove any kind of mark.
9726 If UNMARK is negative, tick articles."
9727   (interactive "P")
9728   (when unmark
9729     (setq unmark (prefix-numeric-value unmark)))
9730   (let ((count
9731          (gnus-summary-mark-same-subject
9732           (gnus-summary-article-subject) unmark)))
9733     ;; If marked as read, go to next unread subject.
9734     (when (null unmark)
9735       ;; Go to next unread subject.
9736       (gnus-summary-next-subject 1 t))
9737     (gnus-message 7 "%d articles are marked as %s"
9738                   count (if unmark "unread" "read"))))
9739
9740 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9741   "Mark articles with same SUBJECT as read, and return marked number.
9742 If optional argument UNMARK is positive, remove any kinds of marks.
9743 If optional argument UNMARK is negative, mark articles as unread instead."
9744   (let ((count 1))
9745     (save-excursion
9746       (cond
9747        ((null unmark)                   ; Mark as read.
9748         (while (and
9749                 (progn
9750                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9751                   (gnus-summary-show-thread) t)
9752                 (gnus-summary-find-subject subject))
9753           (setq count (1+ count))))
9754        ((> unmark 0)                    ; Tick.
9755         (while (and
9756                 (progn
9757                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9758                   (gnus-summary-show-thread) t)
9759                 (gnus-summary-find-subject subject))
9760           (setq count (1+ count))))
9761        (t                               ; Mark as unread.
9762         (while (and
9763                 (progn
9764                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9765                   (gnus-summary-show-thread) t)
9766                 (gnus-summary-find-subject subject))
9767           (setq count (1+ count)))))
9768       (gnus-set-mode-line 'summary)
9769       ;; Return the number of marked articles.
9770       count)))
9771
9772 (defun gnus-summary-mark-as-processable (n &optional unmark)
9773   "Set the process mark on the next N articles.
9774 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9775 the process mark instead.  The difference between N and the actual
9776 number of articles marked is returned."
9777   (interactive "P")
9778   (if (and (null n) (gnus-region-active-p))
9779       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9780     (setq n (prefix-numeric-value n))
9781     (let ((backward (< n 0))
9782           (n (abs n)))
9783       (while (and
9784               (> n 0)
9785               (if unmark
9786                   (gnus-summary-remove-process-mark
9787                    (gnus-summary-article-number))
9788                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9789               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9790         (setq n (1- n)))
9791       (when (/= 0 n)
9792         (gnus-message 7 "No more articles"))
9793       (gnus-summary-recenter)
9794       (gnus-summary-position-point)
9795       n)))
9796
9797 (defun gnus-summary-unmark-as-processable (n)
9798   "Remove the process mark from the next N articles.
9799 If N is negative, unmark backward instead.  The difference between N and
9800 the actual number of articles unmarked is returned."
9801   (interactive "P")
9802   (gnus-summary-mark-as-processable n t))
9803
9804 (defun gnus-summary-unmark-all-processable ()
9805   "Remove the process mark from all articles."
9806   (interactive)
9807   (save-excursion
9808     (while gnus-newsgroup-processable
9809       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9810   (gnus-summary-position-point))
9811
9812 (defun gnus-summary-add-mark (article type)
9813   "Mark ARTICLE with a mark of TYPE."
9814   (let ((vtype (car (assq type gnus-article-mark-lists)))
9815         var)
9816     (if (not vtype)
9817         (error "No such mark type: %s" type)
9818       (setq var (intern (format "gnus-newsgroup-%s" type)))
9819       (set var (cons article (symbol-value var)))
9820       (if (memq type '(processable cached replied forwarded recent saved))
9821           (gnus-summary-update-secondary-mark article)
9822         ;;; !!! This is bogus.  We should find out what primary
9823         ;;; !!! mark we want to set.
9824         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9825
9826 (defun gnus-summary-mark-as-expirable (n)
9827   "Mark N articles forward as expirable.
9828 If N is negative, mark backward instead.  The difference between N and
9829 the actual number of articles marked is returned."
9830   (interactive "p")
9831   (gnus-summary-mark-forward n gnus-expirable-mark))
9832
9833 (defun gnus-summary-mark-as-spam (n)
9834   "Mark N articles forward as spam.
9835 If N is negative, mark backward instead.  The difference between N and
9836 the actual number of articles marked is returned."
9837   (interactive "p")
9838   (gnus-summary-mark-forward n gnus-spam-mark))
9839
9840 (defun gnus-summary-mark-article-as-replied (article)
9841   "Mark ARTICLE as replied to and update the summary line.
9842 ARTICLE can also be a list of articles."
9843   (interactive (list (gnus-summary-article-number)))
9844   (let ((articles (if (listp article) article (list article))))
9845     (dolist (article articles)
9846       (unless (numberp article)
9847         (error "%s is not a number" article))
9848       (push article gnus-newsgroup-replied)
9849       (let ((buffer-read-only nil))
9850         (when (gnus-summary-goto-subject article nil t)
9851           (gnus-summary-update-secondary-mark article))))))
9852
9853 (defun gnus-summary-mark-article-as-forwarded (article)
9854   "Mark ARTICLE as forwarded and update the summary line.
9855 ARTICLE can also be a list of articles."
9856   (let ((articles (if (listp article) article (list article))))
9857     (dolist (article articles)
9858       (push article gnus-newsgroup-forwarded)
9859       (let ((buffer-read-only nil))
9860         (when (gnus-summary-goto-subject article nil t)
9861           (gnus-summary-update-secondary-mark article))))))
9862
9863 (defun gnus-summary-set-bookmark (article)
9864   "Set a bookmark in current article."
9865   (interactive (list (gnus-summary-article-number)))
9866   (when (or (not (get-buffer gnus-article-buffer))
9867             (not gnus-current-article)
9868             (not gnus-article-current)
9869             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9870     (error "No current article selected"))
9871   ;; Remove old bookmark, if one exists.
9872   (gnus-pull article gnus-newsgroup-bookmarks)
9873   ;; Set the new bookmark, which is on the form
9874   ;; (article-number . line-number-in-body).
9875   (push
9876    (cons article
9877          (with-current-buffer gnus-article-buffer
9878            (count-lines
9879             (min (point)
9880                  (save-excursion
9881                    (article-goto-body)
9882                    (point)))
9883             (point))))
9884    gnus-newsgroup-bookmarks)
9885   (gnus-message 6 "A bookmark has been added to the current article."))
9886
9887 (defun gnus-summary-remove-bookmark (article)
9888   "Remove the bookmark from the current article."
9889   (interactive (list (gnus-summary-article-number)))
9890   ;; Remove old bookmark, if one exists.
9891   (if (not (assq article gnus-newsgroup-bookmarks))
9892       (gnus-message 6 "No bookmark in current article.")
9893     (gnus-pull article gnus-newsgroup-bookmarks)
9894     (gnus-message 6 "Removed bookmark.")))
9895
9896 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9897 (defun gnus-summary-mark-as-dormant (n)
9898   "Mark N articles forward as dormant.
9899 If N is negative, mark backward instead.  The difference between N and
9900 the actual number of articles marked is returned."
9901   (interactive "p")
9902   (gnus-summary-mark-forward n gnus-dormant-mark))
9903
9904 (defun gnus-summary-set-process-mark (article)
9905   "Set the process mark on ARTICLE and update the summary line."
9906   (setq gnus-newsgroup-processable
9907         (cons article
9908               (delq article gnus-newsgroup-processable)))
9909   (when (gnus-summary-goto-subject article)
9910     (gnus-summary-show-thread)
9911     (gnus-summary-goto-subject article)
9912     (gnus-summary-update-secondary-mark article)))
9913
9914 (defun gnus-summary-remove-process-mark (article)
9915   "Remove the process mark from ARTICLE and update the summary line."
9916   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9917   (when (gnus-summary-goto-subject article)
9918     (gnus-summary-show-thread)
9919     (gnus-summary-goto-subject article)
9920     (gnus-summary-update-secondary-mark article)))
9921
9922 (defun gnus-summary-set-saved-mark (article)
9923   "Set the process mark on ARTICLE and update the summary line."
9924   (push article gnus-newsgroup-saved)
9925   (when (gnus-summary-goto-subject article)
9926     (gnus-summary-update-secondary-mark article)))
9927
9928 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9929   "Mark N articles as read forwards.
9930 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9931 The difference between N and the actual number of articles marked is
9932 returned.
9933 If NO-EXPIRE, auto-expiry will be inhibited."
9934   (interactive "p")
9935   (gnus-summary-show-thread)
9936   (let ((backward (< n 0))
9937         (gnus-summary-goto-unread
9938          (and gnus-summary-goto-unread
9939               (not (eq gnus-summary-goto-unread 'never))
9940               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9941                                     gnus-ticked-mark gnus-dormant-mark)))))
9942         (n (abs n))
9943         (mark (or mark gnus-del-mark)))
9944     (while (and (> n 0)
9945                 (gnus-summary-mark-article nil mark no-expire)
9946                 (zerop (gnus-summary-next-subject
9947                         (if backward -1 1)
9948                         (and gnus-summary-goto-unread
9949                              (not (eq gnus-summary-goto-unread 'never)))
9950                         t)))
9951       (setq n (1- n)))
9952     (when (/= 0 n)
9953       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9954     (gnus-summary-recenter)
9955     (gnus-summary-position-point)
9956     (gnus-set-mode-line 'summary)
9957     n))
9958
9959 (defun gnus-summary-mark-article-as-read (mark)
9960   "Mark the current article quickly as read with MARK."
9961   (let ((article (gnus-summary-article-number)))
9962     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9963     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9964     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9965     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9966     (push (cons article mark) gnus-newsgroup-reads)
9967     ;; Possibly remove from cache, if that is used.
9968     (when gnus-use-cache
9969       (gnus-cache-enter-remove-article article))
9970     ;; Allow the backend to change the mark.
9971     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9972     ;; Check for auto-expiry.
9973     (when (and gnus-newsgroup-auto-expire
9974                (memq mark gnus-auto-expirable-marks))
9975       (setq mark gnus-expirable-mark)
9976       ;; Let the backend know about the mark change.
9977       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9978       (push article gnus-newsgroup-expirable))
9979     ;; Set the mark in the buffer.
9980     (gnus-summary-update-mark mark 'unread)
9981     t))
9982
9983 (defun gnus-summary-mark-article-as-unread (mark)
9984   "Mark the current article quickly as unread with MARK."
9985   (let* ((article (gnus-summary-article-number))
9986          (old-mark (gnus-summary-article-mark article)))
9987     ;; Allow the backend to change the mark.
9988     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9989     (if (eq mark old-mark)
9990         t
9991       (if (<= article 0)
9992           (progn
9993             (gnus-error 1 "Can't mark negative article numbers")
9994             nil)
9995         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9996         (setq gnus-newsgroup-spam-marked
9997               (delq article gnus-newsgroup-spam-marked))
9998         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9999         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10000         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10001         (cond ((= mark gnus-ticked-mark)
10002                (setq gnus-newsgroup-marked
10003                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10004                                               article)))
10005               ((= mark gnus-spam-mark)
10006                (setq gnus-newsgroup-spam-marked
10007                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10008                                               article)))
10009               ((= mark gnus-dormant-mark)
10010                (setq gnus-newsgroup-dormant
10011                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10012                                               article)))
10013               (t
10014                (setq gnus-newsgroup-unreads
10015                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10016                                               article))))
10017         (gnus-pull article gnus-newsgroup-reads)
10018
10019         ;; See whether the article is to be put in the cache.
10020         (and gnus-use-cache
10021              (vectorp (gnus-summary-article-header article))
10022              (save-excursion
10023                (gnus-cache-possibly-enter-article
10024                 gnus-newsgroup-name article
10025                 (gnus-summary-article-header article)
10026                 (= mark gnus-ticked-mark)
10027                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10028
10029         ;; Fix the mark.
10030         (gnus-summary-update-mark mark 'unread)
10031         t))))
10032
10033 (defun gnus-summary-mark-article (&optional article mark no-expire)
10034   "Mark ARTICLE with MARK.  MARK can be any character.
10035 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10036 `??' (dormant) and `?E' (expirable).
10037 If MARK is nil, then the default character `?r' is used.
10038 If ARTICLE is nil, then the article on the current line will be
10039 marked.
10040 If NO-EXPIRE, auto-expiry will be inhibited."
10041   ;; The mark might be a string.
10042   (when (stringp mark)
10043     (setq mark (aref mark 0)))
10044   ;; If no mark is given, then we check auto-expiring.
10045   (when (null mark)
10046     (setq mark gnus-del-mark))
10047   (when (and (not no-expire)
10048              gnus-newsgroup-auto-expire
10049              (memq mark gnus-auto-expirable-marks))
10050     (setq mark gnus-expirable-mark))
10051   (let ((article (or article (gnus-summary-article-number)))
10052         (old-mark (gnus-summary-article-mark article)))
10053     ;; Allow the backend to change the mark.
10054     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10055     (if (eq mark old-mark)
10056         t
10057       (unless article
10058         (error "No article on current line"))
10059       (if (not (if (or (= mark gnus-unread-mark)
10060                        (= mark gnus-ticked-mark)
10061                        (= mark gnus-spam-mark)
10062                        (= mark gnus-dormant-mark))
10063                    (gnus-mark-article-as-unread article mark)
10064                  (gnus-mark-article-as-read article mark)))
10065           t
10066         ;; See whether the article is to be put in the cache.
10067         (and gnus-use-cache
10068              (not (= mark gnus-canceled-mark))
10069              (vectorp (gnus-summary-article-header article))
10070              (save-excursion
10071                (gnus-cache-possibly-enter-article
10072                 gnus-newsgroup-name article
10073                 (gnus-summary-article-header article)
10074                 (= mark gnus-ticked-mark)
10075                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10076
10077         (when (gnus-summary-goto-subject article nil t)
10078           (let ((buffer-read-only nil))
10079             (gnus-summary-show-thread)
10080             ;; Fix the mark.
10081             (gnus-summary-update-mark mark 'unread)
10082             t))))))
10083
10084 (defun gnus-summary-update-secondary-mark (article)
10085   "Update the secondary (read, process, cache) mark."
10086   (gnus-summary-update-mark
10087    (cond ((memq article gnus-newsgroup-processable)
10088           gnus-process-mark)
10089          ((memq article gnus-newsgroup-cached)
10090           gnus-cached-mark)
10091          ((memq article gnus-newsgroup-replied)
10092           gnus-replied-mark)
10093          ((memq article gnus-newsgroup-forwarded)
10094           gnus-forwarded-mark)
10095          ((memq article gnus-newsgroup-saved)
10096           gnus-saved-mark)
10097          ((memq article gnus-newsgroup-recent)
10098           gnus-recent-mark)
10099          ((memq article gnus-newsgroup-unseen)
10100           gnus-unseen-mark)
10101          (t gnus-no-mark))
10102    'replied)
10103   (when (gnus-visual-p 'summary-highlight 'highlight)
10104     (gnus-run-hooks 'gnus-summary-update-hook))
10105   t)
10106
10107 (defun gnus-summary-update-download-mark (article)
10108   "Update the download mark."
10109   (gnus-summary-update-mark
10110    (cond ((memq article gnus-newsgroup-undownloaded)
10111           gnus-undownloaded-mark)
10112          (gnus-newsgroup-agentized
10113           gnus-downloaded-mark)
10114          (t
10115           gnus-no-mark))
10116    'download)
10117   (gnus-summary-update-line t)
10118   t)
10119
10120 (defun gnus-summary-update-mark (mark type)
10121   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10122         (buffer-read-only nil))
10123     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10124     (when forward
10125       (when (looking-at "\r")
10126         (incf forward))
10127       (when (<= (+ forward (point)) (point-max))
10128         ;; Go to the right position on the line.
10129         (goto-char (+ forward (point)))
10130         ;; Replace the old mark with the new mark.
10131         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10132         ;; Optionally update the marks by some user rule.
10133         (when (eq type 'unread)
10134           (gnus-data-set-mark
10135            (gnus-data-find (gnus-summary-article-number)) mark)
10136           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10137
10138 (defun gnus-mark-article-as-read (article &optional mark)
10139   "Enter ARTICLE in the pertinent lists and remove it from others."
10140   ;; Make the article expirable.
10141   (let ((mark (or mark gnus-del-mark)))
10142     (setq gnus-newsgroup-expirable
10143           (if (= mark gnus-expirable-mark)
10144               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10145             (delq article gnus-newsgroup-expirable)))
10146     ;; Remove from unread and marked lists.
10147     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10148     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10149     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10150     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10151     (push (cons article mark) gnus-newsgroup-reads)
10152     ;; Possibly remove from cache, if that is used.
10153     (when gnus-use-cache
10154       (gnus-cache-enter-remove-article article))
10155     t))
10156
10157 (defun gnus-mark-article-as-unread (article &optional mark)
10158   "Enter ARTICLE in the pertinent lists and remove it from others."
10159   (let ((mark (or mark gnus-ticked-mark)))
10160     (if (<= article 0)
10161         (progn
10162           (gnus-error 1 "Can't mark negative article numbers")
10163           nil)
10164       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10165             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10166             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10167             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10168             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10169
10170       ;; Unsuppress duplicates?
10171       (when gnus-suppress-duplicates
10172         (gnus-dup-unsuppress-article article))
10173
10174       (cond ((= mark gnus-ticked-mark)
10175              (setq gnus-newsgroup-marked
10176                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10177             ((= mark gnus-spam-mark)
10178              (setq gnus-newsgroup-spam-marked
10179                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10180                                             article)))
10181             ((= mark gnus-dormant-mark)
10182              (setq gnus-newsgroup-dormant
10183                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10184             (t
10185              (setq gnus-newsgroup-unreads
10186                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10187       (gnus-pull article gnus-newsgroup-reads)
10188       t)))
10189
10190 (defalias 'gnus-summary-mark-as-unread-forward
10191   'gnus-summary-tick-article-forward)
10192 (make-obsolete 'gnus-summary-mark-as-unread-forward
10193                'gnus-summary-tick-article-forward)
10194 (defun gnus-summary-tick-article-forward (n)
10195   "Tick N articles forwards.
10196 If N is negative, tick backwards instead.
10197 The difference between N and the number of articles ticked is returned."
10198   (interactive "p")
10199   (gnus-summary-mark-forward n gnus-ticked-mark))
10200
10201 (defalias 'gnus-summary-mark-as-unread-backward
10202   'gnus-summary-tick-article-backward)
10203 (make-obsolete 'gnus-summary-mark-as-unread-backward
10204                'gnus-summary-tick-article-backward)
10205 (defun gnus-summary-tick-article-backward (n)
10206   "Tick N articles backwards.
10207 The difference between N and the number of articles ticked is returned."
10208   (interactive "p")
10209   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10210
10211 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10212 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10213 (defun gnus-summary-tick-article (&optional article clear-mark)
10214   "Mark current article as unread.
10215 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10216 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10217   (interactive)
10218   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10219                                        gnus-ticked-mark)))
10220
10221 (defun gnus-summary-mark-as-read-forward (n)
10222   "Mark N articles as read forwards.
10223 If N is negative, mark backwards instead.
10224 The difference between N and the actual number of articles marked is
10225 returned."
10226   (interactive "p")
10227   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10228
10229 (defun gnus-summary-mark-as-read-backward (n)
10230   "Mark the N articles as read backwards.
10231 The difference between N and the actual number of articles marked is
10232 returned."
10233   (interactive "p")
10234   (gnus-summary-mark-forward
10235    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10236
10237 (defun gnus-summary-mark-as-read (&optional article mark)
10238   "Mark current article as read.
10239 ARTICLE specifies the article to be marked as read.
10240 MARK specifies a string to be inserted at the beginning of the line."
10241   (gnus-summary-mark-article article mark))
10242
10243 (defun gnus-summary-clear-mark-forward (n)
10244   "Clear marks from N articles forward.
10245 If N is negative, clear backward instead.
10246 The difference between N and the number of marks cleared is returned."
10247   (interactive "p")
10248   (gnus-summary-mark-forward n gnus-unread-mark))
10249
10250 (defun gnus-summary-clear-mark-backward (n)
10251   "Clear marks from N articles backward.
10252 The difference between N and the number of marks cleared is returned."
10253   (interactive "p")
10254   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10255
10256 (defun gnus-summary-mark-unread-as-read ()
10257   "Intended to be used by `gnus-summary-mark-article-hook'."
10258   (when (memq gnus-current-article gnus-newsgroup-unreads)
10259     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10260
10261 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10262   "Intended to be used by `gnus-summary-mark-article-hook'."
10263   (let ((mark (gnus-summary-article-mark)))
10264     (when (or (gnus-unread-mark-p mark)
10265               (gnus-read-mark-p mark))
10266       (gnus-summary-mark-article gnus-current-article
10267                                  (or new-mark gnus-read-mark)))))
10268
10269 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10270   "Intended to be used by `gnus-summary-mark-article-hook'."
10271   (let ((mark (gnus-summary-article-mark)))
10272     (when (or (gnus-unread-mark-p mark)
10273               (gnus-read-mark-p mark))
10274       (gnus-summary-mark-article (gnus-summary-article-number)
10275                                  (or new-mark gnus-read-mark)))))
10276
10277 (defun gnus-summary-mark-unread-as-ticked ()
10278   "Intended to be used by `gnus-summary-mark-article-hook'."
10279   (when (memq gnus-current-article gnus-newsgroup-unreads)
10280     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10281
10282 (defun gnus-summary-mark-region-as-read (point mark all)
10283   "Mark all unread articles between point and mark as read.
10284 If given a prefix, mark all articles between point and mark as read,
10285 even ticked and dormant ones."
10286   (interactive "r\nP")
10287   (save-excursion
10288     (let (article)
10289       (goto-char point)
10290       (beginning-of-line)
10291       (while (and
10292               (< (point) mark)
10293               (progn
10294                 (when (or all
10295                           (memq (setq article (gnus-summary-article-number))
10296                                 gnus-newsgroup-unreads))
10297                   (gnus-summary-mark-article article gnus-del-mark))
10298                 t)
10299               (gnus-summary-find-next))))))
10300
10301 (defun gnus-summary-mark-below (score mark)
10302   "Mark articles with score less than SCORE with MARK."
10303   (interactive "P\ncMark: ")
10304   (setq score (if score
10305                   (prefix-numeric-value score)
10306                 (or gnus-summary-default-score 0)))
10307   (save-excursion
10308     (set-buffer gnus-summary-buffer)
10309     (goto-char (point-min))
10310     (while
10311         (progn
10312           (and (< (gnus-summary-article-score) score)
10313                (gnus-summary-mark-article nil mark))
10314           (gnus-summary-find-next)))))
10315
10316 (defun gnus-summary-kill-below (&optional score)
10317   "Mark articles with score below SCORE as read."
10318   (interactive "P")
10319   (gnus-summary-mark-below score gnus-killed-mark))
10320
10321 (defun gnus-summary-clear-above (&optional score)
10322   "Clear all marks from articles with score above SCORE."
10323   (interactive "P")
10324   (gnus-summary-mark-above score gnus-unread-mark))
10325
10326 (defun gnus-summary-tick-above (&optional score)
10327   "Tick all articles with score above SCORE."
10328   (interactive "P")
10329   (gnus-summary-mark-above score gnus-ticked-mark))
10330
10331 (defun gnus-summary-mark-above (score mark)
10332   "Mark articles with score over SCORE with MARK."
10333   (interactive "P\ncMark: ")
10334   (setq score (if score
10335                   (prefix-numeric-value score)
10336                 (or gnus-summary-default-score 0)))
10337   (save-excursion
10338     (set-buffer gnus-summary-buffer)
10339     (goto-char (point-min))
10340     (while (and (progn
10341                   (when (> (gnus-summary-article-score) score)
10342                     (gnus-summary-mark-article nil mark))
10343                   t)
10344                 (gnus-summary-find-next)))))
10345
10346 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10347 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10348 (defun gnus-summary-limit-include-expunged (&optional no-error)
10349   "Display all the hidden articles that were expunged for low scores."
10350   (interactive)
10351   (let ((buffer-read-only nil))
10352     (let ((scored gnus-newsgroup-scored)
10353           headers h)
10354       (while scored
10355         (unless (gnus-summary-article-header (caar scored))
10356           (and (setq h (gnus-number-to-header (caar scored)))
10357                (< (cdar scored) gnus-summary-expunge-below)
10358                (push h headers)))
10359         (setq scored (cdr scored)))
10360       (if (not headers)
10361           (when (not no-error)
10362             (error "No expunged articles hidden"))
10363         (goto-char (point-min))
10364         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10365         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10366         (mapcar (lambda (x) (push (mail-header-number x)
10367                                   gnus-newsgroup-limit))
10368                 headers)
10369         (gnus-summary-prepare-unthreaded (nreverse headers))
10370         (goto-char (point-min))
10371         (gnus-summary-position-point)
10372         t))))
10373
10374 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10375   "Mark all unread articles in this newsgroup as read.
10376 If prefix argument ALL is non-nil, ticked and dormant articles will
10377 also be marked as read.
10378 If QUIETLY is non-nil, no questions will be asked.
10379
10380 If TO-HERE is non-nil, it should be a point in the buffer.  All
10381 articles before (after, if REVERSE is set) this point will be marked
10382 as read.
10383
10384 Note that this function will only catch up the unread article
10385 in the current summary buffer limitation.
10386
10387 The number of articles marked as read is returned."
10388   (interactive "P")
10389   (prog1
10390       (save-excursion
10391         (when (or quietly
10392                   (not gnus-interactive-catchup) ;Without confirmation?
10393                   gnus-expert-user
10394                   (gnus-y-or-n-p
10395                    (if all
10396                        "Mark absolutely all articles as read? "
10397                      "Mark all unread articles as read? ")))
10398           (if (and not-mark
10399                    (not gnus-newsgroup-adaptive)
10400                    (not gnus-newsgroup-auto-expire)
10401                    (not gnus-suppress-duplicates)
10402                    (or (not gnus-use-cache)
10403                        (eq gnus-use-cache 'passive)))
10404               (progn
10405                 (when all
10406                   (setq gnus-newsgroup-marked nil
10407                         gnus-newsgroup-spam-marked nil
10408                         gnus-newsgroup-dormant nil))
10409                 (setq gnus-newsgroup-unreads
10410                       (gnus-sorted-nunion
10411                        (gnus-intersection gnus-newsgroup-unreads
10412                                           gnus-newsgroup-downloadable)
10413                        gnus-newsgroup-unfetched)))
10414             ;; We actually mark all articles as canceled, which we
10415             ;; have to do when using auto-expiry or adaptive scoring.
10416             (gnus-summary-show-all-threads)
10417             (if (and to-here reverse)
10418                 (progn
10419                   (goto-char to-here)
10420                   (gnus-summary-mark-current-read-and-unread-as-read
10421                    gnus-catchup-mark)
10422                   (while (gnus-summary-find-next (not all))
10423                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10424               (when (gnus-summary-first-subject (not all))
10425                 (while (and
10426                         (if to-here (< (point) to-here) t)
10427                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10428                         (gnus-summary-find-next (not all))))))
10429             (gnus-set-mode-line 'summary))
10430           t))
10431     (gnus-summary-position-point)))
10432
10433 (defun gnus-summary-catchup-to-here (&optional all)
10434   "Mark all unticked articles before the current one as read.
10435 If ALL is non-nil, also mark ticked and dormant articles as read."
10436   (interactive "P")
10437   (save-excursion
10438     (gnus-save-hidden-threads
10439       (let ((beg (point)))
10440         ;; We check that there are unread articles.
10441         (when (or all (gnus-summary-find-prev))
10442           (gnus-summary-catchup all t beg)))))
10443   (gnus-summary-position-point))
10444
10445 (defun gnus-summary-catchup-from-here (&optional all)
10446   "Mark all unticked articles after (and including) the current one as read.
10447 If ALL is non-nil, also mark ticked and dormant articles as read."
10448   (interactive "P")
10449   (save-excursion
10450     (gnus-save-hidden-threads
10451       (let ((beg (point)))
10452         ;; We check that there are unread articles.
10453         (when (or all (gnus-summary-find-next))
10454           (gnus-summary-catchup all t beg nil t)))))
10455   (gnus-summary-position-point))
10456
10457 (defun gnus-summary-catchup-all (&optional quietly)
10458   "Mark all articles in this newsgroup as read.
10459 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10460 instead, which marks only unread articles as read."
10461   (interactive "P")
10462   (gnus-summary-catchup t quietly))
10463
10464 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10465   "Mark all unread articles in this group as read, then exit.
10466 If prefix argument ALL is non-nil, all articles are marked as read.
10467 If QUIETLY is non-nil, no questions will be asked."
10468   (interactive "P")
10469   (when (gnus-summary-catchup all quietly nil 'fast)
10470     ;; Select next newsgroup or exit.
10471     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10472              (eq gnus-auto-select-next 'quietly))
10473         (gnus-summary-next-group nil)
10474       (gnus-summary-exit))))
10475
10476 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10477   "Mark all articles in this newsgroup as read, and then exit.
10478 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10479 instead, which marks only unread articles as read."
10480   (interactive "P")
10481   (gnus-summary-catchup-and-exit t quietly))
10482
10483 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10484   "Mark all articles in this group as read and select the next group.
10485 If given a prefix, mark all articles, unread as well as ticked, as
10486 read."
10487   (interactive "P")
10488   (save-excursion
10489     (gnus-summary-catchup all))
10490   (gnus-summary-next-group))
10491
10492 ;;;
10493 ;;; with article
10494 ;;;
10495
10496 (defmacro gnus-with-article (article &rest forms)
10497   "Select ARTICLE and perform FORMS in the original article buffer.
10498 Then replace the article with the result."
10499   `(progn
10500      ;; We don't want the article to be marked as read.
10501      (let (gnus-mark-article-hook)
10502        (gnus-summary-select-article t t nil ,article))
10503      (set-buffer gnus-original-article-buffer)
10504      ,@forms
10505      (if (not (gnus-check-backend-function
10506                'request-replace-article (car gnus-article-current)))
10507          (gnus-message 5 "Read-only group; not replacing")
10508        (unless (gnus-request-replace-article
10509                 ,article (car gnus-article-current)
10510                 (current-buffer) t)
10511          (error "Couldn't replace article")))
10512      ;; The cache and backlog have to be flushed somewhat.
10513      (when gnus-keep-backlog
10514        (gnus-backlog-remove-article
10515         (car gnus-article-current) (cdr gnus-article-current)))
10516      (when gnus-use-cache
10517        (gnus-cache-update-article
10518         (car gnus-article-current) (cdr gnus-article-current)))))
10519
10520 (put 'gnus-with-article 'lisp-indent-function 1)
10521 (put 'gnus-with-article 'edebug-form-spec '(form body))
10522
10523 ;; Thread-based commands.
10524
10525 (defun gnus-summary-articles-in-thread (&optional article)
10526   "Return a list of all articles in the current thread.
10527 If ARTICLE is non-nil, return all articles in the thread that starts
10528 with that article."
10529   (let* ((article (or article (gnus-summary-article-number)))
10530          (data (gnus-data-find-list article))
10531          (top-level (gnus-data-level (car data)))
10532          (top-subject
10533           (cond ((null gnus-thread-operation-ignore-subject)
10534                  (gnus-simplify-subject-re
10535                   (mail-header-subject (gnus-data-header (car data)))))
10536                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10537                  (gnus-simplify-subject-fuzzy
10538                   (mail-header-subject (gnus-data-header (car data)))))
10539                 (t nil)))
10540          (end-point (save-excursion
10541                       (if (gnus-summary-go-to-next-thread)
10542                           (point) (point-max))))
10543          articles)
10544     (while (and data
10545                 (< (gnus-data-pos (car data)) end-point))
10546       (when (or (not top-subject)
10547                 (string= top-subject
10548                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10549                              (gnus-simplify-subject-fuzzy
10550                               (mail-header-subject
10551                                (gnus-data-header (car data))))
10552                            (gnus-simplify-subject-re
10553                             (mail-header-subject
10554                              (gnus-data-header (car data)))))))
10555         (push (gnus-data-number (car data)) articles))
10556       (unless (and (setq data (cdr data))
10557                    (> (gnus-data-level (car data)) top-level))
10558         (setq data nil)))
10559     ;; Return the list of articles.
10560     (nreverse articles)))
10561
10562 (defun gnus-summary-rethread-current ()
10563   "Rethread the thread the current article is part of."
10564   (interactive)
10565   (let* ((gnus-show-threads t)
10566          (article (gnus-summary-article-number))
10567          (id (mail-header-id (gnus-summary-article-header)))
10568          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10569     (unless id
10570       (error "No article on the current line"))
10571     (gnus-rebuild-thread id)
10572     (gnus-summary-goto-subject article)))
10573
10574 (defun gnus-summary-reparent-thread ()
10575   "Make the current article child of the marked (or previous) article.
10576
10577 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10578 is non-nil or the Subject: of both articles are the same."
10579   (interactive)
10580   (unless (not (gnus-group-read-only-p))
10581     (error "The current newsgroup does not support article editing"))
10582   (unless (<= (length gnus-newsgroup-processable) 1)
10583     (error "No more than one article may be marked"))
10584   (save-window-excursion
10585     (let ((gnus-article-buffer " *reparent*")
10586           (current-article (gnus-summary-article-number))
10587           ;; First grab the marked article, otherwise one line up.
10588           (parent-article (if (not (null gnus-newsgroup-processable))
10589                               (car gnus-newsgroup-processable)
10590                             (save-excursion
10591                               (if (eq (forward-line -1) 0)
10592                                   (gnus-summary-article-number)
10593                                 (error "Beginning of summary buffer"))))))
10594       (unless (not (eq current-article parent-article))
10595         (error "An article may not be self-referential"))
10596       (let ((message-id (mail-header-id
10597                          (gnus-summary-article-header parent-article))))
10598         (unless (and message-id (not (equal message-id "")))
10599           (error "No message-id in desired parent"))
10600         (gnus-with-article current-article
10601           (save-restriction
10602             (goto-char (point-min))
10603             (message-narrow-to-head)
10604             (if (re-search-forward "^References: " nil t)
10605                 (progn
10606                   (re-search-forward "^[^ \t]" nil t)
10607                   (forward-line -1)
10608                   (end-of-line)
10609                   (insert " " message-id))
10610               (insert "References: " message-id "\n"))))
10611         (set-buffer gnus-summary-buffer)
10612         (gnus-summary-unmark-all-processable)
10613         (gnus-summary-update-article current-article)
10614         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10615             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10616         (gnus-summary-rethread-current)
10617         (gnus-message 3 "Article %d is now the child of article %d"
10618                       current-article parent-article)))))
10619
10620 (defun gnus-summary-toggle-threads (&optional arg)
10621   "Toggle showing conversation threads.
10622 If ARG is positive number, turn showing conversation threads on."
10623   (interactive "P")
10624   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10625     (setq gnus-show-threads
10626           (if (null arg) (not gnus-show-threads)
10627             (> (prefix-numeric-value arg) 0)))
10628     (gnus-summary-prepare)
10629     (gnus-summary-goto-subject current)
10630     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10631     (gnus-summary-position-point)))
10632
10633 (defun gnus-summary-show-all-threads ()
10634   "Show all threads."
10635   (interactive)
10636   (save-excursion
10637     (let ((buffer-read-only nil))
10638       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10639   (gnus-summary-position-point))
10640
10641 (defun gnus-summary-show-thread ()
10642   "Show thread subtrees.
10643 Returns nil if no thread was there to be shown."
10644   (interactive)
10645   (let ((buffer-read-only nil)
10646         (orig (point))
10647         (end (point-at-eol))
10648         ;; Leave point at bol
10649         (beg (progn (beginning-of-line) (point))))
10650     (prog1
10651         ;; Any hidden lines here?
10652         (search-forward "\r" end t)
10653       (subst-char-in-region beg end ?\^M ?\n t)
10654       (goto-char orig)
10655       (gnus-summary-position-point))))
10656
10657 (defun gnus-summary-maybe-hide-threads ()
10658   "If requested, hide the threads that should be hidden."
10659   (when (and gnus-show-threads
10660              gnus-thread-hide-subtree)
10661     (gnus-summary-hide-all-threads
10662      (if (or (consp gnus-thread-hide-subtree)
10663              (functionp gnus-thread-hide-subtree))
10664          (gnus-make-predicate gnus-thread-hide-subtree)
10665        nil))))
10666
10667 ;;; Hiding predicates.
10668
10669 (defun gnus-article-unread-p (header)
10670   (memq (mail-header-number header) gnus-newsgroup-unreads))
10671
10672 (defun gnus-article-unseen-p (header)
10673   (memq (mail-header-number header) gnus-newsgroup-unseen))
10674
10675 (defun gnus-map-articles (predicate articles)
10676   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10677   (apply 'gnus-or (mapcar predicate
10678                           (mapcar 'gnus-summary-article-header articles))))
10679
10680 (defun gnus-summary-hide-all-threads (&optional predicate)
10681   "Hide all thread subtrees.
10682 If PREDICATE is supplied, threads that satisfy this predicate
10683 will not be hidden."
10684   (interactive)
10685   (save-excursion
10686     (goto-char (point-min))
10687     (let ((end nil))
10688       (while (not end)
10689         (when (or (not predicate)
10690                   (gnus-map-articles
10691                    predicate (gnus-summary-article-children)))
10692             (gnus-summary-hide-thread))
10693         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10694   (gnus-summary-position-point))
10695
10696 (defun gnus-summary-hide-thread ()
10697   "Hide thread subtrees.
10698 If PREDICATE is supplied, threads that satisfy this predicate
10699 will not be hidden.
10700 Returns nil if no threads were there to be hidden."
10701   (interactive)
10702   (let ((buffer-read-only nil)
10703         (start (point))
10704         (article (gnus-summary-article-number)))
10705     (goto-char start)
10706     ;; Go forward until either the buffer ends or the subthread
10707     ;; ends.
10708     (when (and (not (eobp))
10709                (or (zerop (gnus-summary-next-thread 1 t))
10710                    (goto-char (point-max))))
10711       (prog1
10712           (if (and (> (point) start)
10713                    (search-backward "\n" start t))
10714               (progn
10715                 (subst-char-in-region start (point) ?\n ?\^M)
10716                 (gnus-summary-goto-subject article))
10717             (goto-char start)
10718             nil)))))
10719
10720 (defun gnus-summary-go-to-next-thread (&optional previous)
10721   "Go to the same level (or less) next thread.
10722 If PREVIOUS is non-nil, go to previous thread instead.
10723 Return the article number moved to, or nil if moving was impossible."
10724   (let ((level (gnus-summary-thread-level))
10725         (way (if previous -1 1))
10726         (beg (point)))
10727     (forward-line way)
10728     (while (and (not (eobp))
10729                 (< level (gnus-summary-thread-level)))
10730       (forward-line way))
10731     (if (eobp)
10732         (progn
10733           (goto-char beg)
10734           nil)
10735       (setq beg (point))
10736       (prog1
10737           (gnus-summary-article-number)
10738         (goto-char beg)))))
10739
10740 (defun gnus-summary-next-thread (n &optional silent)
10741   "Go to the same level next N'th thread.
10742 If N is negative, search backward instead.
10743 Returns the difference between N and the number of skips actually
10744 done.
10745
10746 If SILENT, don't output messages."
10747   (interactive "p")
10748   (let ((backward (< n 0))
10749         (n (abs n)))
10750     (while (and (> n 0)
10751                 (gnus-summary-go-to-next-thread backward))
10752       (decf n))
10753     (unless silent
10754       (gnus-summary-position-point))
10755     (when (and (not silent) (/= 0 n))
10756       (gnus-message 7 "No more threads"))
10757     n))
10758
10759 (defun gnus-summary-prev-thread (n)
10760   "Go to the same level previous N'th thread.
10761 Returns the difference between N and the number of skips actually
10762 done."
10763   (interactive "p")
10764   (gnus-summary-next-thread (- n)))
10765
10766 (defun gnus-summary-go-down-thread ()
10767   "Go down one level in the current thread."
10768   (let ((children (gnus-summary-article-children)))
10769     (when children
10770       (gnus-summary-goto-subject (car children)))))
10771
10772 (defun gnus-summary-go-up-thread ()
10773   "Go up one level in the current thread."
10774   (let ((parent (gnus-summary-article-parent)))
10775     (when parent
10776       (gnus-summary-goto-subject parent))))
10777
10778 (defun gnus-summary-down-thread (n)
10779   "Go down thread N steps.
10780 If N is negative, go up instead.
10781 Returns the difference between N and how many steps down that were
10782 taken."
10783   (interactive "p")
10784   (let ((up (< n 0))
10785         (n (abs n)))
10786     (while (and (> n 0)
10787                 (if up (gnus-summary-go-up-thread)
10788                   (gnus-summary-go-down-thread)))
10789       (setq n (1- n)))
10790     (gnus-summary-position-point)
10791     (when (/= 0 n)
10792       (gnus-message 7 "Can't go further"))
10793     n))
10794
10795 (defun gnus-summary-up-thread (n)
10796   "Go up thread N steps.
10797 If N is negative, go down instead.
10798 Returns the difference between N and how many steps down that were
10799 taken."
10800   (interactive "p")
10801   (gnus-summary-down-thread (- n)))
10802
10803 (defun gnus-summary-top-thread ()
10804   "Go to the top of the thread."
10805   (interactive)
10806   (while (gnus-summary-go-up-thread))
10807   (gnus-summary-article-number))
10808
10809 (defun gnus-summary-kill-thread (&optional unmark)
10810   "Mark articles under current thread as read.
10811 If the prefix argument is positive, remove any kinds of marks.
10812 If the prefix argument is negative, tick articles instead."
10813   (interactive "P")
10814   (when unmark
10815     (setq unmark (prefix-numeric-value unmark)))
10816   (let ((articles (gnus-summary-articles-in-thread)))
10817     (save-excursion
10818       ;; Expand the thread.
10819       (gnus-summary-show-thread)
10820       ;; Mark all the articles.
10821       (while articles
10822         (gnus-summary-goto-subject (car articles))
10823         (cond ((null unmark)
10824                (gnus-summary-mark-article-as-read gnus-killed-mark))
10825               ((> unmark 0)
10826                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10827               (t
10828                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10829         (setq articles (cdr articles))))
10830     ;; Hide killed subtrees.
10831     (and (null unmark)
10832          gnus-thread-hide-killed
10833          (gnus-summary-hide-thread))
10834     ;; If marked as read, go to next unread subject.
10835     (when (null unmark)
10836       ;; Go to next unread subject.
10837       (gnus-summary-next-subject 1 t)))
10838   (gnus-set-mode-line 'summary))
10839
10840 ;; Summary sorting commands
10841
10842 (defun gnus-summary-sort-by-number (&optional reverse)
10843   "Sort the summary buffer by article number.
10844 Argument REVERSE means reverse order."
10845   (interactive "P")
10846   (gnus-summary-sort 'number reverse))
10847
10848 (defun gnus-summary-sort-by-random (&optional reverse)
10849   "Randomize the order in the summary buffer.
10850 Argument REVERSE means to randomize in reverse order."
10851   (interactive "P")
10852   (gnus-summary-sort 'random reverse))
10853
10854 (defun gnus-summary-sort-by-author (&optional reverse)
10855   "Sort the summary buffer by author name alphabetically.
10856 If `case-fold-search' is non-nil, case of letters is ignored.
10857 Argument REVERSE means reverse order."
10858   (interactive "P")
10859   (gnus-summary-sort 'author reverse))
10860
10861 (defun gnus-summary-sort-by-subject (&optional reverse)
10862   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10863 If `case-fold-search' is non-nil, case of letters is ignored.
10864 Argument REVERSE means reverse order."
10865   (interactive "P")
10866   (gnus-summary-sort 'subject reverse))
10867
10868 (defun gnus-summary-sort-by-date (&optional reverse)
10869   "Sort the summary buffer by date.
10870 Argument REVERSE means reverse order."
10871   (interactive "P")
10872   (gnus-summary-sort 'date reverse))
10873
10874 (defun gnus-summary-sort-by-score (&optional reverse)
10875   "Sort the summary buffer by score.
10876 Argument REVERSE means reverse order."
10877   (interactive "P")
10878   (gnus-summary-sort 'score reverse))
10879
10880 (defun gnus-summary-sort-by-lines (&optional reverse)
10881   "Sort the summary buffer by the number of lines.
10882 Argument REVERSE means reverse order."
10883   (interactive "P")
10884   (gnus-summary-sort 'lines reverse))
10885
10886 (defun gnus-summary-sort-by-chars (&optional reverse)
10887   "Sort the summary buffer by article length.
10888 Argument REVERSE means reverse order."
10889   (interactive "P")
10890   (gnus-summary-sort 'chars reverse))
10891
10892 (defun gnus-summary-sort-by-original (&optional reverse)
10893   "Sort the summary buffer using the default sorting method.
10894 Argument REVERSE means reverse order."
10895   (interactive "P")
10896   (let* ((buffer-read-only)
10897          (gnus-summary-prepare-hook nil))
10898     ;; We do the sorting by regenerating the threads.
10899     (gnus-summary-prepare)
10900     ;; Hide subthreads if needed.
10901     (gnus-summary-maybe-hide-threads)))
10902
10903 (defun gnus-summary-sort (predicate reverse)
10904   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10905   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10906          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10907          (gnus-thread-sort-functions
10908           (if (not reverse)
10909               thread
10910             `(lambda (t1 t2)
10911                (,thread t2 t1))))
10912          (gnus-sort-gathered-threads-function
10913           gnus-thread-sort-functions)
10914          (gnus-article-sort-functions
10915           (if (not reverse)
10916               article
10917             `(lambda (t1 t2)
10918                (,article t2 t1))))
10919          (buffer-read-only)
10920          (gnus-summary-prepare-hook nil))
10921     ;; We do the sorting by regenerating the threads.
10922     (gnus-summary-prepare)
10923     ;; Hide subthreads if needed.
10924     (gnus-summary-maybe-hide-threads)))
10925
10926 ;; Summary saving commands.
10927
10928 (defun gnus-summary-save-article (&optional n not-saved)
10929   "Save the current article using the default saver function.
10930 If N is a positive number, save the N next articles.
10931 If N is a negative number, save the N previous articles.
10932 If N is nil and any articles have been marked with the process mark,
10933 save those articles instead.
10934 The variable `gnus-default-article-saver' specifies the saver function."
10935   (interactive "P")
10936   (let* ((articles (gnus-summary-work-articles n))
10937          (save-buffer (save-excursion
10938                         (nnheader-set-temp-buffer " *Gnus Save*")))
10939          (num (length articles))
10940          header file)
10941     (dolist (article articles)
10942       (setq header (gnus-summary-article-header article))
10943       (if (not (vectorp header))
10944           ;; This is a pseudo-article.
10945           (if (assq 'name header)
10946               (gnus-copy-file (cdr (assq 'name header)))
10947             (gnus-message 1 "Article %d is unsaveable" article))
10948         ;; This is a real article.
10949         (save-window-excursion
10950           (let ((gnus-display-mime-function nil)
10951                 (gnus-article-prepare-hook nil))
10952             (gnus-summary-select-article t nil nil article)))
10953         (save-excursion
10954           (set-buffer save-buffer)
10955           (erase-buffer)
10956           (insert-buffer-substring gnus-original-article-buffer))
10957         (setq file (gnus-article-save save-buffer file num))
10958         (gnus-summary-remove-process-mark article)
10959         (unless not-saved
10960           (gnus-summary-set-saved-mark article))))
10961     (gnus-kill-buffer save-buffer)
10962     (gnus-summary-position-point)
10963     (gnus-set-mode-line 'summary)
10964     n))
10965
10966 (defun gnus-summary-pipe-output (&optional arg headers)
10967   "Pipe the current article to a subprocess.
10968 If N is a positive number, pipe the N next articles.
10969 If N is a negative number, pipe the N previous articles.
10970 If N is nil and any articles have been marked with the process mark,
10971 pipe those articles instead.
10972 If HEADERS (the symbolic prefix), include the headers, too."
10973   (interactive (gnus-interactive "P\ny"))
10974   (require 'gnus-art)
10975   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10976         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10977     (gnus-summary-save-article arg t))
10978   (let ((buffer (get-buffer "*Shell Command Output*")))
10979     (when (and buffer
10980                (not (zerop (buffer-size buffer))))
10981       (gnus-configure-windows 'pipe))))
10982
10983 (defun gnus-summary-save-article-mail (&optional arg)
10984   "Append the current article to an mail file.
10985 If N is a positive number, save the N next articles.
10986 If N is a negative number, save the N previous articles.
10987 If N is nil and any articles have been marked with the process mark,
10988 save those articles instead."
10989   (interactive "P")
10990   (require 'gnus-art)
10991   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10992     (gnus-summary-save-article arg)))
10993
10994 (defun gnus-summary-save-article-rmail (&optional arg)
10995   "Append the current article to an rmail file.
10996 If N is a positive number, save the N next articles.
10997 If N is a negative number, save the N previous articles.
10998 If N is nil and any articles have been marked with the process mark,
10999 save those articles instead."
11000   (interactive "P")
11001   (require 'gnus-art)
11002   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11003     (gnus-summary-save-article arg)))
11004
11005 (defun gnus-summary-save-article-file (&optional arg)
11006   "Append the current article to a file.
11007 If N is a positive number, save the N next articles.
11008 If N is a negative number, save the N previous articles.
11009 If N is nil and any articles have been marked with the process mark,
11010 save those articles instead."
11011   (interactive "P")
11012   (require 'gnus-art)
11013   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11014     (gnus-summary-save-article arg)))
11015
11016 (defun gnus-summary-write-article-file (&optional arg)
11017   "Write the current article to a file, deleting the previous file.
11018 If N is a positive number, save the N next articles.
11019 If N is a negative number, save the N previous articles.
11020 If N is nil and any articles have been marked with the process mark,
11021 save those articles instead."
11022   (interactive "P")
11023   (require 'gnus-art)
11024   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11025     (gnus-summary-save-article arg)))
11026
11027 (defun gnus-summary-save-article-body-file (&optional arg)
11028   "Append the current article body to a file.
11029 If N is a positive number, save the N next articles.
11030 If N is a negative number, save the N previous articles.
11031 If N is nil and any articles have been marked with the process mark,
11032 save those articles instead."
11033   (interactive "P")
11034   (require 'gnus-art)
11035   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11036     (gnus-summary-save-article arg)))
11037
11038 (defun gnus-summary-muttprint (&optional arg)
11039   "Print the current article using Muttprint.
11040 If N is a positive number, save the N next articles.
11041 If N is a negative number, save the N previous articles.
11042 If N is nil and any articles have been marked with the process mark,
11043 save those articles instead."
11044   (interactive "P")
11045   (require 'gnus-art)
11046   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11047     (gnus-summary-save-article arg t)))
11048
11049 (defun gnus-summary-pipe-message (program)
11050   "Pipe the current article through PROGRAM."
11051   (interactive "sProgram: ")
11052   (gnus-summary-select-article)
11053   (let ((mail-header-separator ""))
11054     (gnus-eval-in-buffer-window gnus-article-buffer
11055       (save-restriction
11056         (widen)
11057         (let ((start (window-start))
11058               buffer-read-only)
11059           (message-pipe-buffer-body program)
11060           (set-window-start (get-buffer-window (current-buffer)) start))))))
11061
11062 (defun gnus-get-split-value (methods)
11063   "Return a value based on the split METHODS."
11064   (let (split-name method result match)
11065     (when methods
11066       (save-excursion
11067         (set-buffer gnus-original-article-buffer)
11068         (save-restriction
11069           (nnheader-narrow-to-headers)
11070           (while (and methods (not split-name))
11071             (goto-char (point-min))
11072             (setq method (pop methods))
11073             (setq match (car method))
11074             (when (cond
11075                    ((stringp match)
11076                     ;; Regular expression.
11077                     (ignore-errors
11078                       (re-search-forward match nil t)))
11079                    ((functionp match)
11080                     ;; Function.
11081                     (save-restriction
11082                       (widen)
11083                       (setq result (funcall match gnus-newsgroup-name))))
11084                    ((consp match)
11085                     ;; Form.
11086                     (save-restriction
11087                       (widen)
11088                       (setq result (eval match)))))
11089               (setq split-name (cdr method))
11090               (cond ((stringp result)
11091                      (push (expand-file-name
11092                             result gnus-article-save-directory)
11093                            split-name))
11094                     ((consp result)
11095                      (setq split-name (append result split-name)))))))))
11096     (nreverse split-name)))
11097
11098 (defun gnus-valid-move-group-p (group)
11099   (and (boundp group)
11100        (symbol-name group)
11101        (symbol-value group)
11102        (gnus-get-function (gnus-find-method-for-group
11103                            (symbol-name group)) 'request-accept-article t)))
11104
11105 (defun gnus-read-move-group-name (prompt default articles prefix)
11106   "Read a group name."
11107   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11108          (minibuffer-confirm-incomplete nil) ; XEmacs
11109          (prom
11110           (format "%s %s to:"
11111                   prompt
11112                   (if (> (length articles) 1)
11113                       (format "these %d articles" (length articles))
11114                     "this article")))
11115          (to-newsgroup
11116           (cond
11117            ((null split-name)
11118             (gnus-completing-read-with-default
11119              default prom
11120              gnus-active-hashtb
11121              'gnus-valid-move-group-p
11122              nil prefix
11123              'gnus-group-history))
11124            ((= 1 (length split-name))
11125             (gnus-completing-read-with-default
11126              (car split-name) prom
11127              gnus-active-hashtb
11128              'gnus-valid-move-group-p
11129              nil nil
11130              'gnus-group-history))
11131            (t
11132             (gnus-completing-read-with-default
11133              nil prom
11134              (mapcar 'list (nreverse split-name))
11135              nil nil nil
11136              'gnus-group-history))))
11137          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11138     (when to-newsgroup
11139       (if (or (string= to-newsgroup "")
11140               (string= to-newsgroup prefix))
11141           (setq to-newsgroup default))
11142       (unless to-newsgroup
11143         (error "No group name entered"))
11144       (or (gnus-active to-newsgroup)
11145           (gnus-activate-group to-newsgroup nil nil to-method)
11146           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11147                                      to-newsgroup))
11148               (or (and (gnus-request-create-group to-newsgroup to-method)
11149                        (gnus-activate-group
11150                         to-newsgroup nil nil to-method)
11151                        (gnus-subscribe-group to-newsgroup))
11152                   (error "Couldn't create group %s" to-newsgroup)))
11153           (error "No such group: %s" to-newsgroup)))
11154     to-newsgroup))
11155
11156 (defun gnus-summary-save-parts (type dir n &optional reverse)
11157   "Save parts matching TYPE to DIR.
11158 If REVERSE, save parts that do not match TYPE."
11159   (interactive
11160    (list (read-string "Save parts of type: "
11161                       (or (car gnus-summary-save-parts-type-history)
11162                           gnus-summary-save-parts-default-mime)
11163                       'gnus-summary-save-parts-type-history)
11164          (setq gnus-summary-save-parts-last-directory
11165                (read-file-name "Save to directory: "
11166                                gnus-summary-save-parts-last-directory
11167                                nil t))
11168          current-prefix-arg))
11169   (gnus-summary-iterate n
11170     (let ((gnus-display-mime-function nil)
11171           (gnus-inhibit-treatment t))
11172       (gnus-summary-select-article))
11173     (save-excursion
11174       (set-buffer gnus-article-buffer)
11175       (let ((handles (or gnus-article-mime-handles
11176                          (mm-dissect-buffer nil gnus-article-loose-mime)
11177                          (and gnus-article-emulate-mime
11178                               (mm-uu-dissect)))))
11179         (when handles
11180           (gnus-summary-save-parts-1 type dir handles reverse)
11181           (unless gnus-article-mime-handles ;; Don't destroy this case.
11182             (mm-destroy-parts handles)))))))
11183
11184 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11185   (if (stringp (car handle))
11186       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11187               (cdr handle))
11188     (when (if reverse
11189               (not (string-match type (mm-handle-media-type handle)))
11190             (string-match type (mm-handle-media-type handle)))
11191       (let ((file (expand-file-name
11192                    (gnus-map-function
11193                     mm-file-name-rewrite-functions
11194                     (file-name-nondirectory
11195                      (or
11196                       (mail-content-type-get
11197                        (mm-handle-disposition handle) 'filename)
11198                       (mail-content-type-get
11199                        (mm-handle-type handle) 'name)
11200                       (concat gnus-newsgroup-name
11201                               "." (number-to-string
11202                                    (cdr gnus-article-current))))))
11203                    dir)))
11204         (unless (file-exists-p file)
11205           (mm-save-part-to-file handle file))))))
11206
11207 ;; Summary extract commands
11208
11209 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11210   (let ((buffer-read-only nil)
11211         (article (gnus-summary-article-number))
11212         after-article b e)
11213     (unless (gnus-summary-goto-subject article)
11214       (error "No such article: %d" article))
11215     (gnus-summary-position-point)
11216     ;; If all commands are to be bunched up on one line, we collect
11217     ;; them here.
11218     (unless gnus-view-pseudos-separately
11219       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11220             files action)
11221         (while ps
11222           (setq action (cdr (assq 'action (car ps))))
11223           (setq files (list (cdr (assq 'name (car ps)))))
11224           (while (and ps (cdr ps)
11225                       (string= (or action "1")
11226                                (or (cdr (assq 'action (cadr ps))) "2")))
11227             (push (cdr (assq 'name (cadr ps))) files)
11228             (setcdr ps (cddr ps)))
11229           (when files
11230             (when (not (string-match "%s" action))
11231               (push " " files))
11232             (push " " files)
11233             (when (assq 'execute (car ps))
11234               (setcdr (assq 'execute (car ps))
11235                       (funcall (if (string-match "%s" action)
11236                                    'format 'concat)
11237                                action
11238                                (mapconcat
11239                                 (lambda (f)
11240                                   (if (equal f " ")
11241                                       f
11242                                     (shell-quote-argument f)))
11243                                 files " ")))))
11244           (setq ps (cdr ps)))))
11245     (if (and gnus-view-pseudos (not not-view))
11246         (while pslist
11247           (when (assq 'execute (car pslist))
11248             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11249                                   (eq gnus-view-pseudos 'not-confirm)))
11250           (setq pslist (cdr pslist)))
11251       (save-excursion
11252         (while pslist
11253           (setq after-article (or (cdr (assq 'article (car pslist)))
11254                                   (gnus-summary-article-number)))
11255           (gnus-summary-goto-subject after-article)
11256           (forward-line 1)
11257           (setq b (point))
11258           (insert "    " (file-name-nondirectory
11259                           (cdr (assq 'name (car pslist))))
11260                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11261           (setq e (point))
11262           (forward-line -1)             ; back to `b'
11263           (gnus-add-text-properties
11264            b (1- e) (list 'gnus-number gnus-reffed-article-number
11265                           gnus-mouse-face-prop gnus-mouse-face))
11266           (gnus-data-enter
11267            after-article gnus-reffed-article-number
11268            gnus-unread-mark b (car pslist) 0 (- e b))
11269           (setq gnus-newsgroup-unreads
11270                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11271                                          gnus-reffed-article-number))
11272           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11273           (setq pslist (cdr pslist)))))))
11274
11275 (defun gnus-pseudos< (p1 p2)
11276   (let ((c1 (cdr (assq 'action p1)))
11277         (c2 (cdr (assq 'action p2))))
11278     (and c1 c2 (string< c1 c2))))
11279
11280 (defun gnus-request-pseudo-article (props)
11281   (cond ((assq 'execute props)
11282          (gnus-execute-command (cdr (assq 'execute props)))))
11283   (let ((gnus-current-article (gnus-summary-article-number)))
11284     (gnus-run-hooks 'gnus-mark-article-hook)))
11285
11286 (defun gnus-execute-command (command &optional automatic)
11287   (save-excursion
11288     (gnus-article-setup-buffer)
11289     (set-buffer gnus-article-buffer)
11290     (setq buffer-read-only nil)
11291     (let ((command (if automatic command
11292                      (read-string "Command: " (cons command 0)))))
11293       (erase-buffer)
11294       (insert "$ " command "\n\n")
11295       (if gnus-view-pseudo-asynchronously
11296           (start-process "gnus-execute" (current-buffer) shell-file-name
11297                          shell-command-switch command)
11298         (call-process shell-file-name nil t nil
11299                       shell-command-switch command)))))
11300
11301 ;; Summary kill commands.
11302
11303 (defun gnus-summary-edit-global-kill (article)
11304   "Edit the \"global\" kill file."
11305   (interactive (list (gnus-summary-article-number)))
11306   (gnus-group-edit-global-kill article))
11307
11308 (defun gnus-summary-edit-local-kill ()
11309   "Edit a local kill file applied to the current newsgroup."
11310   (interactive)
11311   (setq gnus-current-headers (gnus-summary-article-header))
11312   (gnus-group-edit-local-kill
11313    (gnus-summary-article-number) gnus-newsgroup-name))
11314
11315 ;;; Header reading.
11316
11317 (defun gnus-read-header (id &optional header)
11318   "Read the headers of article ID and enter them into the Gnus system."
11319   (let ((group gnus-newsgroup-name)
11320         (gnus-override-method
11321          (or
11322           gnus-override-method
11323           (and (gnus-news-group-p gnus-newsgroup-name)
11324                (car (gnus-refer-article-methods)))))
11325         where)
11326     ;; First we check to see whether the header in question is already
11327     ;; fetched.
11328     (if (stringp id)
11329         ;; This is a Message-ID.
11330         (setq header (or header (gnus-id-to-header id)))
11331       ;; This is an article number.
11332       (setq header (or header (gnus-summary-article-header id))))
11333     (if (and header
11334              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11335         ;; We have found the header.
11336         header
11337       ;; We have to really fetch the header to this article.
11338       (save-excursion
11339         (set-buffer nntp-server-buffer)
11340         (when (setq where (gnus-request-head id group))
11341           (nnheader-fold-continuation-lines)
11342           (goto-char (point-max))
11343           (insert ".\n")
11344           (goto-char (point-min))
11345           (insert "211 ")
11346           (princ (cond
11347                   ((numberp id) id)
11348                   ((cdr where) (cdr where))
11349                   (header (mail-header-number header))
11350                   (t gnus-reffed-article-number))
11351                  (current-buffer))
11352           (insert " Article retrieved.\n"))
11353         (if (or (not where)
11354                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11355             ()                          ; Malformed head.
11356           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11357             (when (and (stringp id)
11358                        (not (string= (gnus-group-real-name group)
11359                                      (car where))))
11360               ;; If we fetched by Message-ID and the article came
11361               ;; from a different group, we fudge some bogus article
11362               ;; numbers for this article.
11363               (mail-header-set-number header gnus-reffed-article-number))
11364             (save-excursion
11365               (set-buffer gnus-summary-buffer)
11366               (decf gnus-reffed-article-number)
11367               (gnus-remove-header (mail-header-number header))
11368               (push header gnus-newsgroup-headers)
11369               (setq gnus-current-headers header)
11370               (push (mail-header-number header) gnus-newsgroup-limit)))
11371           header)))))
11372
11373 (defun gnus-remove-header (number)
11374   "Remove header NUMBER from `gnus-newsgroup-headers'."
11375   (if (and gnus-newsgroup-headers
11376            (= number (mail-header-number (car gnus-newsgroup-headers))))
11377       (pop gnus-newsgroup-headers)
11378     (let ((headers gnus-newsgroup-headers))
11379       (while (and (cdr headers)
11380                   (not (= number (mail-header-number (cadr headers)))))
11381         (pop headers))
11382       (when (cdr headers)
11383         (setcdr headers (cddr headers))))))
11384
11385 ;;;
11386 ;;; summary highlights
11387 ;;;
11388
11389 (defun gnus-highlight-selected-summary ()
11390   "Highlight selected article in summary buffer."
11391   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11392   (when gnus-summary-selected-face
11393     (save-excursion
11394       (let* ((beg (point-at-bol))
11395              (end (point-at-eol))
11396              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11397              (from (if (get-text-property beg gnus-mouse-face-prop)
11398                        beg
11399                      (or (next-single-property-change
11400                           beg gnus-mouse-face-prop nil end)
11401                          beg)))
11402              (to
11403               (if (= from end)
11404                   (- from 2)
11405                 (or (next-single-property-change
11406                      from gnus-mouse-face-prop nil end)
11407                     end))))
11408         ;; If no mouse-face prop on line we will have to = from = end,
11409         ;; so we highlight the entire line instead.
11410         (when (= (+ to 2) from)
11411           (setq from beg)
11412           (setq to end))
11413         (if gnus-newsgroup-selected-overlay
11414             ;; Move old overlay.
11415             (gnus-move-overlay
11416              gnus-newsgroup-selected-overlay from to (current-buffer))
11417           ;; Create new overlay.
11418           (gnus-overlay-put
11419            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11420            'face gnus-summary-selected-face))))))
11421
11422 (defvar gnus-summary-highlight-line-cached nil)
11423 (defvar gnus-summary-highlight-line-trigger nil)
11424
11425 (defun gnus-summary-highlight-line-0 ()
11426   (if (and (eq gnus-summary-highlight-line-trigger
11427                gnus-summary-highlight)
11428            gnus-summary-highlight-line-cached)
11429       gnus-summary-highlight-line-cached
11430     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11431           gnus-summary-highlight-line-cached
11432           (let* ((cond (list 'cond))
11433                  (c cond)
11434                  (list gnus-summary-highlight))
11435             (while list
11436               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11437                               nil))
11438               (setq c (cdr c)
11439                     list (cdr list)))
11440             (gnus-byte-compile (list 'lambda nil cond))))))
11441
11442 (defun gnus-summary-highlight-line ()
11443   "Highlight current line according to `gnus-summary-highlight'."
11444   (let* ((beg (point-at-bol))
11445          (article (or (gnus-summary-article-number) gnus-current-article))
11446          (score (or (cdr (assq article
11447                                gnus-newsgroup-scored))
11448                     gnus-summary-default-score 0))
11449          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11450          (inhibit-read-only t)
11451          (default gnus-summary-default-score)
11452          (default-high gnus-summary-default-high-score)
11453          (default-low gnus-summary-default-low-score)
11454          (uncached (and gnus-summary-use-undownloaded-faces
11455                         (memq article gnus-newsgroup-undownloaded)
11456                         (not (memq article gnus-newsgroup-cached)))))
11457     (let ((face (funcall (gnus-summary-highlight-line-0))))
11458       (unless (eq face (get-text-property beg 'face))
11459         (gnus-put-text-property-excluding-characters-with-faces
11460          beg (point-at-eol) 'face
11461          (setq face (if (boundp face) (symbol-value face) face)))
11462         (when gnus-summary-highlight-line-function
11463           (funcall gnus-summary-highlight-line-function article face))))))
11464
11465 (defun gnus-update-read-articles (group unread &optional compute)
11466   "Update the list of read articles in GROUP.
11467 UNREAD is a sorted list."
11468   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11469         (info (gnus-get-info group))
11470         (prev 1)
11471         read)
11472     (if (or (not info) (not active))
11473         ;; There is no info on this group if it was, in fact,
11474         ;; killed.  Gnus stores no information on killed groups, so
11475         ;; there's nothing to be done.
11476         ;; One could store the information somewhere temporarily,
11477         ;; perhaps...  Hmmm...
11478         ()
11479       ;; Remove any negative articles numbers.
11480       (while (and unread (< (car unread) 0))
11481         (setq unread (cdr unread)))
11482       ;; Remove any expired article numbers
11483       (while (and unread (< (car unread) (car active)))
11484         (setq unread (cdr unread)))
11485       ;; Compute the ranges of read articles by looking at the list of
11486       ;; unread articles.
11487       (while unread
11488         (when (/= (car unread) prev)
11489           (push (if (= prev (1- (car unread))) prev
11490                   (cons prev (1- (car unread))))
11491                 read))
11492         (setq prev (1+ (car unread)))
11493         (setq unread (cdr unread)))
11494       (when (<= prev (cdr active))
11495         (push (cons prev (cdr active)) read))
11496       (setq read (if (> (length read) 1) (nreverse read) read))
11497       (if compute
11498           read
11499         (save-excursion
11500           (let (setmarkundo)
11501             ;; Propagate the read marks to the backend.
11502             (when (gnus-check-backend-function 'request-set-mark group)
11503               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11504                     (add (gnus-remove-from-range read (gnus-info-read info))))
11505                 (when (or add del)
11506                   (unless (gnus-check-group group)
11507                     (error "Can't open server for %s" group))
11508                   (gnus-request-set-mark
11509                    group (delq nil (list (if add (list add 'add '(read)))
11510                                          (if del (list del 'del '(read))))))
11511                   (setq setmarkundo
11512                         `(gnus-request-set-mark
11513                           ,group
11514                           ',(delq nil (list
11515                                        (if del (list del 'add '(read)))
11516                                        (if add (list add 'del '(read))))))))))
11517             (set-buffer gnus-group-buffer)
11518             (gnus-undo-register
11519               `(progn
11520                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11521                  (gnus-info-set-read ',info ',(gnus-info-read info))
11522                  (gnus-get-unread-articles-in-group ',info
11523                                                     (gnus-active ,group))
11524                  (gnus-group-update-group ,group t)
11525                  ,setmarkundo))))
11526         ;; Enter this list into the group info.
11527         (gnus-info-set-read info read)
11528         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11529         (gnus-get-unread-articles-in-group info (gnus-active group))
11530         t))))
11531
11532 (defun gnus-offer-save-summaries ()
11533   "Offer to save all active summary buffers."
11534   (let (buffers)
11535     ;; Go through all buffers and find all summaries.
11536     (dolist (buffer (buffer-list))
11537       (when (and (setq buffer (buffer-name buffer))
11538                  (string-match "Summary" buffer)
11539                  (with-current-buffer buffer
11540                    ;; We check that this is, indeed, a summary buffer.
11541                    (and (eq major-mode 'gnus-summary-mode)
11542                         ;; Also make sure this isn't bogus.
11543                         gnus-newsgroup-prepared
11544                         ;; Also make sure that this isn't a
11545                         ;; dead summary buffer.
11546                         (not gnus-dead-summary-mode))))
11547         (push buffer buffers)))
11548     ;; Go through all these summary buffers and offer to save them.
11549     (when buffers
11550       (save-excursion
11551         (map-y-or-n-p
11552          "Update summary buffer %s? "
11553          (lambda (buf)
11554            (switch-to-buffer buf)
11555            (gnus-summary-exit))
11556          buffers)))))
11557
11558
11559 ;;; @ for mime-partial
11560 ;;;
11561
11562 (defun gnus-request-partial-message ()
11563   (save-excursion
11564     (let ((number (gnus-summary-article-number))
11565           (group gnus-newsgroup-name)
11566           (mother gnus-article-buffer))
11567       (set-buffer (get-buffer-create " *Partial Article*"))
11568       (erase-buffer)
11569       (setq mime-preview-buffer mother)
11570       (gnus-request-article-this-buffer number group)
11571       (mime-parse-buffer)
11572       )))
11573
11574 (autoload 'mime-combine-message/partial-pieces-automatically
11575   "mime-partial"
11576   "Internal method to combine message/partial messages automatically.")
11577
11578 (mime-add-condition
11579  'action '((type . message)(subtype . partial)
11580            (major-mode . gnus-original-article-mode)
11581            (method . mime-combine-message/partial-pieces-automatically)
11582            (summary-buffer-exp . gnus-summary-buffer)
11583            (request-partial-message-method . gnus-request-partial-message)
11584            ))
11585
11586
11587 ;;; @ for message/rfc822
11588 ;;;
11589
11590 (defun gnus-mime-extract-message/rfc822 (entity situation)
11591   "Burst a forwarded article."
11592   (save-excursion
11593     (set-buffer gnus-summary-buffer)
11594     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11595                                    gnus-newsgroup-name 'gnus-group-history))
11596            (gnus-group-marked (list group))
11597            article info)
11598       (with-temp-buffer
11599         (mime-insert-entity-content entity)
11600         (setq article (gnus-request-accept-article group)))
11601       (when (and (consp article)
11602                  (numberp (setq article (cdr article))))
11603         (setq info (gnus-get-info group))
11604         (gnus-info-set-read info
11605                             (gnus-remove-from-range (gnus-info-read info)
11606                                                     (list article)))
11607         (when (string-equal group gnus-newsgroup-name)
11608           (forward-line 1)
11609           (let (gnus-show-threads)
11610             (gnus-summary-goto-subject article t))
11611           (gnus-summary-clear-mark-forward 1))
11612         (set-buffer gnus-group-buffer)
11613         (gnus-group-get-new-news-this-group nil t)))))
11614
11615 (mime-add-condition
11616  'action '((type . message)(subtype . rfc822)
11617            (major-mode . gnus-original-article-mode)
11618            (method . gnus-mime-extract-message/rfc822)
11619            (mode . "extract")
11620            ))
11621
11622 (mime-add-condition
11623  'action '((type . message)(subtype . news)
11624            (major-mode . gnus-original-article-mode)
11625            (method . gnus-mime-extract-message/rfc822)
11626            (mode . "extract")
11627            ))
11628
11629 (defun gnus-mime-extract-multipart (entity situation)
11630   (let ((children (mime-entity-children entity))
11631         mime-acting-situation-to-override
11632         f)
11633     (while children
11634       (mime-play-entity (car children)
11635                         (cons (assq 'mode situation)
11636                               mime-acting-situation-to-override))
11637       (setq children (cdr children)))
11638     (if (setq f (cdr (assq 'after-method
11639                            mime-acting-situation-to-override)))
11640         (eval f)
11641       )))
11642
11643 (mime-add-condition
11644  'action '((type . multipart)
11645            (method . gnus-mime-extract-multipart)
11646            (mode . "extract")
11647            )
11648  'with-default)
11649
11650
11651 ;;; @ end
11652 ;;;
11653
11654 (defun gnus-summary-inherit-default-charset ()
11655   "Import `default-mime-charset' from summary buffer.
11656 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11657 of FLIM is used."
11658   (if (buffer-live-p gnus-summary-buffer)
11659       (let (d-m-c d-m-c-u)
11660         (with-current-buffer gnus-summary-buffer
11661           (setq d-m-c (if (local-variable-p 'default-mime-charset
11662                                             gnus-summary-buffer)
11663                           default-mime-charset
11664                         t)
11665                 ;; LIMIT
11666                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11667                                               gnus-summary-buffer)
11668                             (symbol-value 'default-mime-charset-unlimited)
11669                           t)))
11670         (if (eq t d-m-c)
11671             (kill-local-variable 'default-mime-charset)
11672           (set (make-local-variable 'default-mime-charset) d-m-c))
11673         (if (eq t d-m-c-u)
11674             (kill-local-variable 'default-mime-charset-unlimited)
11675           (set (make-local-variable 'default-mime-charset-unlimited)
11676                d-m-c-u)))))
11677
11678 (defun gnus-summary-setup-default-charset ()
11679   "Setup newsgroup default charset."
11680   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11681       (progn
11682         (setq gnus-newsgroup-charset nil)
11683         (set (make-local-variable 'default-mime-charset) nil)
11684         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11685           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11686     (let ((ignored-charsets
11687            (or gnus-newsgroup-ephemeral-ignored-charsets
11688                (append
11689                 (and gnus-newsgroup-name
11690                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11691                 gnus-newsgroup-ignored-charsets)))
11692           charset)
11693       (setq gnus-newsgroup-charset
11694             (or gnus-newsgroup-ephemeral-charset
11695                 (when (and gnus-newsgroup-name
11696                            (setq charset (gnus-parameter-charset
11697                                           gnus-newsgroup-name)))
11698                   (make-local-variable 'default-mime-charset)
11699                   (setq default-mime-charset charset))
11700                 gnus-default-charset))
11701       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11702            ignored-charsets))))
11703
11704 ;;;
11705 ;;; Mime Commands
11706 ;;;
11707
11708 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11709   "Display the current article buffer fully MIME-buttonized.
11710 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11711 treated as multipart/mixed."
11712   (interactive "P")
11713   (require 'gnus-art)
11714   (let ((gnus-unbuttonized-mime-types nil)
11715         (gnus-mime-display-multipart-as-mixed show-all-parts))
11716     (gnus-summary-show-article)))
11717
11718 (defun gnus-summary-repair-multipart (article)
11719   "Add a Content-Type header to a multipart article without one."
11720   (interactive (list (gnus-summary-article-number)))
11721   (gnus-with-article article
11722     (message-narrow-to-head)
11723     (message-remove-header "Mime-Version")
11724     (goto-char (point-max))
11725     (insert "Mime-Version: 1.0\n")
11726     (widen)
11727     (when (search-forward "\n--" nil t)
11728       (let ((separator (buffer-substring (point) (point-at-eol))))
11729         (message-narrow-to-head)
11730         (message-remove-header "Content-Type")
11731         (goto-char (point-max))
11732         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11733                         separator))
11734         (widen))))
11735   (let (gnus-mark-article-hook)
11736     (gnus-summary-select-article t t nil article)))
11737
11738 (defun gnus-summary-toggle-display-buttonized ()
11739   "Toggle the buttonizing of the article buffer."
11740   (interactive)
11741   (require 'gnus-art)
11742   (if (setq gnus-inhibit-mime-unbuttonizing
11743             (not gnus-inhibit-mime-unbuttonizing))
11744       (let ((gnus-unbuttonized-mime-types nil))
11745         (gnus-summary-show-article))
11746     (gnus-summary-show-article)))
11747
11748 ;;;
11749 ;;; Intelli-mouse commmands
11750 ;;;
11751
11752 (defun gnus-wheel-summary-scroll (event)
11753   (interactive "e")
11754   (let ((amount (if (memq 'shift (event-modifiers event))
11755                     (car gnus-wheel-scroll-amount)
11756                   (cdr gnus-wheel-scroll-amount)))
11757         (direction (- (* (static-if (featurep 'xemacs)
11758                              (event-button event)
11759                            (cond ((eq 'mouse-4 (event-basic-type event))
11760                                   4)
11761                                  ((eq 'mouse-5 (event-basic-type event))
11762                                   5)))
11763                          2) 9))
11764         edge)
11765     (gnus-summary-scroll-up (* amount direction))
11766     (when (gnus-eval-in-buffer-window gnus-article-buffer
11767             (save-restriction
11768               (widen)
11769               (and (if (< 0 direction)
11770                        (gnus-article-next-page 0)
11771                      (gnus-article-prev-page 0)
11772                      (bobp))
11773                    (if (setq edge (get-text-property
11774                                    (point-min) 'gnus-wheel-edge))
11775                        (setq edge (* edge direction))
11776                      (setq edge -1))
11777                    (or (plusp edge)
11778                        (let ((buffer-read-only nil)
11779                              (inhibit-read-only t))
11780                          (put-text-property (point-min) (point-max)
11781                                             'gnus-wheel-edge direction)
11782                          nil))
11783                    (or (> edge gnus-wheel-edge-resistance)
11784                        (let ((buffer-read-only nil)
11785                              (inhibit-read-only t))
11786                          (put-text-property (point-min) (point-max)
11787                                             'gnus-wheel-edge
11788                                             (* (1+ edge) direction))
11789                          nil))
11790                    (eq last-command 'gnus-wheel-summary-scroll))))
11791       (gnus-summary-next-article nil nil (minusp direction)))))
11792
11793 (defun gnus-wheel-install ()
11794   "Enable mouse wheel support on summary window."
11795   (when gnus-use-wheel
11796     (let ((keys
11797            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11798       (dolist (key keys)
11799         (define-key gnus-summary-mode-map key
11800           'gnus-wheel-summary-scroll)))))
11801
11802 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11803
11804 ;;;
11805 ;;; Traditional PGP commmands
11806 ;;;
11807
11808 (defun gnus-summary-decrypt-article (&optional force)
11809   "Decrypt the current article in traditional PGP way.
11810 This will have permanent effect only in mail groups.
11811 If FORCE is non-nil, allow editing of articles even in read-only
11812 groups."
11813   (interactive "P")
11814   (gnus-summary-select-article t)
11815   (gnus-eval-in-buffer-window gnus-article-buffer
11816     (save-excursion
11817       (save-restriction
11818         (widen)
11819         (goto-char (point-min))
11820         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11821           (error "Not a traditional PGP message!"))
11822         (let ((armor-start (match-beginning 0)))
11823           (if (and (pgg-decrypt-region armor-start (point-max))
11824                    (or force (not (gnus-group-read-only-p))))
11825               (let ((inhibit-read-only t)
11826                     buffer-read-only)
11827                 (delete-region armor-start
11828                                (progn
11829                                  (re-search-forward "^-+END PGP" nil t)
11830                                  (beginning-of-line 2)
11831                                  (point)))
11832                 (insert-buffer-substring pgg-output-buffer))))))))
11833
11834 (defun gnus-summary-verify-article ()
11835   "Verify the current article in traditional PGP way."
11836   (interactive)
11837   (save-excursion
11838     (set-buffer gnus-original-article-buffer)
11839     (goto-char (point-min))
11840     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11841       (error "Not a traditional PGP message!"))
11842     (re-search-forward "^-+END PGP" nil t)
11843     (beginning-of-line 2)
11844     (call-interactively (function pgg-verify-region))))
11845
11846 ;;;
11847 ;;; Generic summary marking commands
11848 ;;;
11849
11850 (defvar gnus-summary-marking-alist
11851   '((read gnus-del-mark "d")
11852     (unread gnus-unread-mark "u")
11853     (ticked gnus-ticked-mark "!")
11854     (dormant gnus-dormant-mark "?")
11855     (expirable gnus-expirable-mark "e"))
11856   "An alist of names/marks/keystrokes.")
11857
11858 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11859 (defvar gnus-summary-mark-map)
11860
11861 (defun gnus-summary-make-all-marking-commands ()
11862   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11863   (dolist (elem gnus-summary-marking-alist)
11864     (apply 'gnus-summary-make-marking-command elem)))
11865
11866 (defun gnus-summary-make-marking-command (name mark keystroke)
11867   (let ((map (make-sparse-keymap)))
11868     (define-key gnus-summary-generic-mark-map keystroke map)
11869     (dolist (lway `((next "next" next nil "n")
11870                     (next-unread "next unread" next t "N")
11871                     (prev "previous" prev nil "p")
11872                     (prev-unread "previous unread" prev t "P")
11873                     (nomove "" nil nil ,keystroke)))
11874       (let ((func (gnus-summary-make-marking-command-1
11875                    mark (car lway) lway name)))
11876         (setq func (eval func))
11877         (define-key map (nth 4 lway) func)))))
11878
11879 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11880   `(defun ,(intern
11881             (format "gnus-summary-put-mark-as-%s%s"
11882                     name (if (eq way 'nomove)
11883                              ""
11884                            (concat "-" (symbol-name way)))))
11885      (n)
11886      ,(format
11887        "Mark the current article as %s%s.
11888 If N, the prefix, then repeat N times.
11889 If N is negative, move in reverse order.
11890 The difference between N and the actual number of articles marked is
11891 returned."
11892        name (car (cdr lway)))
11893      (interactive "p")
11894      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11895
11896 (defun gnus-summary-generic-mark (n mark move unread)
11897   "Mark N articles with MARK."
11898   (unless (eq major-mode 'gnus-summary-mode)
11899     (error "This command can only be used in the summary buffer"))
11900   (gnus-summary-show-thread)
11901   (let ((nummove
11902          (cond
11903           ((eq move 'next) 1)
11904           ((eq move 'prev) -1)
11905           (t 0))))
11906     (if (zerop nummove)
11907         (setq n 1)
11908       (when (< n 0)
11909         (setq n (abs n)
11910               nummove (* -1 nummove))))
11911     (while (and (> n 0)
11912                 (gnus-summary-mark-article nil mark)
11913                 (zerop (gnus-summary-next-subject nummove unread t)))
11914       (setq n (1- n)))
11915     (when (/= 0 n)
11916       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11917     (gnus-summary-recenter)
11918     (gnus-summary-position-point)
11919     (gnus-set-mode-line 'summary)
11920     n))
11921
11922 (defun gnus-summary-insert-articles (articles)
11923   (when (setq articles
11924               (gnus-sorted-difference articles
11925                                       (mapcar (lambda (h)
11926                                                 (mail-header-number h))
11927                                               gnus-newsgroup-headers)))
11928     (setq gnus-newsgroup-headers
11929           (gnus-merge 'list
11930                       gnus-newsgroup-headers
11931                       (gnus-fetch-headers articles)
11932                       'gnus-article-sort-by-number))
11933     ;; Suppress duplicates?
11934     (when gnus-suppress-duplicates
11935       (gnus-dup-suppress-articles))
11936
11937     ;; We might want to build some more threads first.
11938     (when (and gnus-fetch-old-headers
11939                (eq gnus-headers-retrieved-by 'nov))
11940       (if (eq gnus-fetch-old-headers 'invisible)
11941           (gnus-build-all-threads)
11942         (gnus-build-old-threads)))
11943     ;; Let the Gnus agent mark articles as read.
11944     (when gnus-agent
11945       (gnus-agent-get-undownloaded-list))
11946     ;; Remove list identifiers from subject
11947     (when gnus-list-identifiers
11948       (gnus-summary-remove-list-identifiers))
11949     ;; First and last article in this newsgroup.
11950     (when gnus-newsgroup-headers
11951       (setq gnus-newsgroup-begin
11952             (mail-header-number (car gnus-newsgroup-headers))
11953             gnus-newsgroup-end
11954             (mail-header-number
11955              (gnus-last-element gnus-newsgroup-headers))))
11956     (when gnus-use-scoring
11957       (gnus-possibly-score-headers))))
11958
11959 (defun gnus-summary-insert-old-articles (&optional all)
11960   "Insert all old articles in this group.
11961 If ALL is non-nil, already read articles become readable.
11962 If ALL is a number, fetch this number of articles."
11963   (interactive "P")
11964   (prog1
11965       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11966             older len)
11967         (setq older
11968               ;; Some nntp servers lie about their active range.  When
11969               ;; this happens, the active range can be in the millions.
11970               ;; Use a compressed range to avoid creating a huge list.
11971               (gnus-range-difference (list gnus-newsgroup-active) old))
11972         (setq len (gnus-range-length older))
11973         (cond
11974          ((null older) nil)
11975          ((numberp all)
11976           (if (< all len)
11977               (let ((older-range (nreverse older)))
11978                 (setq older nil)
11979
11980                 (while (> all 0)
11981                   (let* ((r (pop older-range))
11982                          (min (if (numberp r) r (car r)))
11983                          (max (if (numberp r) r (cdr r))))
11984                     (while (and (<= min max)
11985                                 (> all 0))
11986                       (push max older)
11987                       (setq all (1- all)
11988                             max (1- max))))))
11989             (setq older (gnus-uncompress-range older))))
11990          (all
11991           (setq older (gnus-uncompress-range older)))
11992          (t
11993           (when (and (numberp gnus-large-newsgroup)
11994                    (> len gnus-large-newsgroup))
11995               (let* ((cursor-in-echo-area nil)
11996                      (initial (gnus-parameter-large-newsgroup-initial
11997                                gnus-newsgroup-name))
11998                      (input
11999                       (read-string
12000                        (format
12001                         "How many articles from %s (%s %d): "
12002                         (gnus-limit-string
12003                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12004                         (if initial "max" "default")
12005                         len)
12006                        (if initial
12007                            (cons (number-to-string initial)
12008                                  0)))))
12009                 (unless (string-match "^[ \t]*$" input)
12010                   (setq all (string-to-number input))
12011                   (if (< all len)
12012                       (let ((older-range (nreverse older)))
12013                         (setq older nil)
12014
12015                         (while (> all 0)
12016                           (let* ((r (pop older-range))
12017                                  (min (if (numberp r) r (car r)))
12018                                  (max (if (numberp r) r (cdr r))))
12019                             (while (and (<= min max)
12020                                         (> all 0))
12021                               (push max older)
12022                               (setq all (1- all)
12023                                     max (1- max))))))))))
12024           (setq older (gnus-uncompress-range older))))
12025         (if (not older)
12026             (message "No old news.")
12027           (gnus-summary-insert-articles older)
12028           (gnus-summary-limit (gnus-sorted-nunion old older))))
12029     (gnus-summary-position-point)))
12030
12031 (defun gnus-summary-insert-new-articles ()
12032   "Insert all new articles in this group."
12033   (interactive)
12034   (prog1
12035       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12036             (old-active gnus-newsgroup-active)
12037             (nnmail-fetched-sources (list t))
12038             i new)
12039         (setq gnus-newsgroup-active
12040               (gnus-activate-group gnus-newsgroup-name 'scan))
12041         (setq i (cdr gnus-newsgroup-active))
12042         (while (> i (cdr old-active))
12043           (push i new)
12044           (decf i))
12045         (if (not new)
12046             (message "No gnus is bad news")
12047           (gnus-summary-insert-articles new)
12048           (setq gnus-newsgroup-unreads
12049                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12050           (gnus-summary-limit (gnus-sorted-nunion old new))))
12051     (gnus-summary-position-point)))
12052
12053 (gnus-summary-make-all-marking-commands)
12054
12055 (gnus-ems-redefine)
12056
12057 (provide 'gnus-sum)
12058
12059 (run-hooks 'gnus-sum-load-hook)
12060
12061 ;; Local Variables:
12062 ;; coding: iso-8859-1
12063 ;; End:
12064
12065 ;;; gnus-sum.el ends here