* gnus-sum.el (gnus-adjust-marks): Avoid splicing null INTO the
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
48 (autoload 'mm-uu-dissect "mm-uu")
49 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
50   "Deuglify broken Outlook (Express) articles and redisplay."
51   t)
52 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
53 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
54 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
55
56 (defcustom gnus-kill-summary-on-exit t
57   "*If non-nil, kill the summary buffer when you exit from it.
58 If nil, the summary will become a \"*Dead Summary*\" buffer, and
59 it will be killed sometime later."
60   :group 'gnus-summary-exit
61   :type 'boolean)
62
63 (defcustom gnus-fetch-old-headers nil
64   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
65 If an unread article in the group refers to an older, already read (or
66 just marked as read) article, the old article will not normally be
67 displayed in the Summary buffer.  If this variable is non-nil, Gnus
68 will attempt to grab the headers to the old articles, and thereby
69 build complete threads.  If it has the value `some', only enough
70 headers to connect otherwise loose threads will be displayed.  This
71 variable can also be a number.  In that case, no more than that number
72 of old headers will be fetched.  If it has the value `invisible', all
73 old headers will be fetched, but none will be displayed.
74
75 The server has to support NOV for any of this to work."
76   :group 'gnus-thread
77   :type '(choice (const :tag "off" nil)
78                  (const some)
79                  number
80                  (sexp :menu-tag "other" t)))
81
82 (defcustom gnus-refer-thread-limit 200
83   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
84 If t, fetch all the available old headers."
85   :group 'gnus-thread
86   :type '(choice number
87                  (sexp :menu-tag "other" t)))
88
89 (defcustom gnus-summary-make-false-root 'adopt
90   "*nil means that Gnus won't gather loose threads.
91 If the root of a thread has expired or been read in a previous
92 session, the information necessary to build a complete thread has been
93 lost.  Instead of having many small sub-threads from this original thread
94 scattered all over the summary buffer, Gnus can gather them.
95
96 If non-nil, Gnus will try to gather all loose sub-threads from an
97 original thread into one large thread.
98
99 If this variable is non-nil, it should be one of `none', `adopt',
100 `dummy' or `empty'.
101
102 If this variable is `none', Gnus will not make a false root, but just
103 present the sub-threads after another.
104 If this variable is `dummy', Gnus will create a dummy root that will
105 have all the sub-threads as children.
106 If this variable is `adopt', Gnus will make one of the \"children\"
107 the parent and mark all the step-children as such.
108 If this variable is `empty', the \"children\" are printed with empty
109 subject fields.  (Or rather, they will be printed with a string
110 given by the `gnus-summary-same-subject' variable.)"
111   :group 'gnus-thread
112   :type '(choice (const :tag "off" nil)
113                  (const none)
114                  (const dummy)
115                  (const adopt)
116                  (const empty)))
117
118 (defcustom gnus-summary-make-false-root-always nil
119   "Always make a false dummy root."
120   :group 'gnus-thread
121   :type 'boolean)
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include:
151 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
152 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 \(they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-default-high-score 0
217   "*Default threshold for a high scored article.
218 An article will be highlighted as high scored if its score is greater
219 than this score."
220   :group 'gnus-score-default
221   :type 'integer)
222
223 (defcustom gnus-summary-default-low-score 0
224   "*Default threshold for a low scored article.
225 An article will be highlighted as low scored if its score is smaller
226 than this score."
227   :group 'gnus-score-default
228   :type 'integer)
229
230 (defcustom gnus-summary-zcore-fuzz 0
231   "*Fuzziness factor for the zcore in the summary buffer.
232 Articles with scores closer than this to `gnus-summary-default-score'
233 will not be marked."
234   :group 'gnus-summary-format
235   :type 'integer)
236
237 (defcustom gnus-simplify-subject-fuzzy-regexp nil
238   "*Strings to be removed when doing fuzzy matches.
239 This can either be a regular expression or list of regular expressions
240 that will be removed from subject strings if fuzzy subject
241 simplification is selected."
242   :group 'gnus-thread
243   :type '(repeat regexp))
244
245 (defcustom gnus-show-threads t
246   "*If non-nil, display threads in summary mode."
247   :group 'gnus-thread
248   :type 'boolean)
249
250 (defcustom gnus-thread-hide-subtree nil
251   "*If non-nil, hide all threads initially.
252 This can be a predicate specifier which says which threads to hide.
253 If threads are hidden, you have to run the command
254 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
255 to expose hidden threads."
256   :group 'gnus-thread
257   :type '(radio (sexp :format "Non-nil\n"
258                       :match (lambda (widget value)
259                                (not (or (consp value) (functionp value))))
260                       :value t)
261                 (const nil)
262                 (sexp :tag "Predicate specifier" :size 0)))
263
264 (defcustom gnus-thread-hide-killed t
265   "*If non-nil, hide killed threads automatically."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-ignore-subject t
270   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
271 If nil, articles that have different subjects from their parents will
272 start separate threads."
273   :group 'gnus-thread
274   :type 'boolean)
275
276 (defcustom gnus-thread-operation-ignore-subject t
277   "*If non-nil, subjects will be ignored when doing thread commands.
278 This affects commands like `gnus-summary-kill-thread' and
279 `gnus-summary-lower-thread'.
280
281 If this variable is nil, articles in the same thread with different
282 subjects will not be included in the operation in question.  If this
283 variable is `fuzzy', only articles that have subjects that are fuzzily
284 equal will be included."
285   :group 'gnus-thread
286   :type '(choice (const :tag "off" nil)
287                  (const fuzzy)
288                  (sexp :tag "on" t)))
289
290 (defcustom gnus-thread-indent-level 4
291   "*Number that says how much each sub-thread should be indented."
292   :group 'gnus-thread
293   :type 'integer)
294
295 (defcustom gnus-auto-extend-newsgroup t
296   "*If non-nil, extend newsgroup forward and backward when requested."
297   :group 'gnus-summary-choose
298   :type 'boolean)
299
300 (defcustom gnus-auto-select-first t
301   "*If non-nil, select the article under point.
302 Which article this is is controlled by the `gnus-auto-select-subject'
303 variable.
304
305 If you want to prevent automatic selection of articles in some
306 newsgroups, set the variable to nil in `gnus-select-group-hook'."
307   :group 'gnus-group-select
308   :type '(choice (const :tag "none" nil)
309                  (sexp :menu-tag "first" t)))
310
311 (defcustom gnus-auto-select-subject 'unread
312   "*Says what subject to place under point when entering a group.
313
314 This variable can either be the symbols `first' (place point on the
315 first subject), `unread' (place point on the subject line of the first
316 unread article), `best' (place point on the subject line of the
317 higest-scored article), `unseen' (place point on the subject line of
318 the first unseen article), 'unseen-or-unread' (place point on the subject
319 line of the first unseen article or, if all article have been seen, on the
320 subject line of the first unread article), or a function to be called to
321 place point on some subject line."
322   :group 'gnus-group-select
323   :type '(choice (const best)
324                  (const unread)
325                  (const first)
326                  (const unseen)
327                  (const unseen-or-unread)))
328
329 (defcustom gnus-auto-select-next t
330   "*If non-nil, offer to go to the next group from the end of the previous.
331 If the value is t and the next newsgroup is empty, Gnus will exit
332 summary mode and go back to group mode.  If the value is neither nil
333 nor t, Gnus will select the following unread newsgroup.  In
334 particular, if the value is the symbol `quietly', the next unread
335 newsgroup will be selected without any confirmation, and if it is
336 `almost-quietly', the next group will be selected without any
337 confirmation if you are located on the last article in the group.
338 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
339 will go to the next group without confirmation."
340   :group 'gnus-summary-maneuvering
341   :type '(choice (const :tag "off" nil)
342                  (const quietly)
343                  (const almost-quietly)
344                  (const slightly-quietly)
345                  (sexp :menu-tag "on" t)))
346
347 (defcustom gnus-auto-select-same nil
348   "*If non-nil, select the next article with the same subject.
349 If there are no more articles with the same subject, go to
350 the first unread article."
351   :group 'gnus-summary-maneuvering
352   :type 'boolean)
353
354 (defcustom gnus-auto-goto-ignores 'unfetched
355   "*Says how to handle unfetched articles when maneuvering.
356
357 This variable can either be the symbols nil (maneuver to any
358 article), `undownloaded' (maneuvering while unplugged ignores articles
359 that have not been fetched), `always-undownloaded' (maneuvering always
360 ignores articles that have not been fetched), `unfetched' (maneuvering
361 ignores articles whose headers have not been fetched).
362
363 NOTE: The list of unfetched articles will always be nil when plugged
364 and, when unplugged, a subset of the undownloaded article list."
365   :group 'gnus-summary-maneuvering
366   :type '(choice (const :tag "None" nil)
367                  (const :tag "Undownloaded when unplugged" undownloaded)
368                  (const :tag "Undownloaded" always-undownloaded)
369                  (const :tag "Unfetched" unfetched)))
370
371 (defcustom gnus-summary-check-current nil
372   "*If non-nil, consider the current article when moving.
373 The \"unread\" movement commands will stay on the same line if the
374 current article is unread."
375   :group 'gnus-summary-maneuvering
376   :type 'boolean)
377
378 (defcustom gnus-auto-center-summary t
379   "*If non-nil, always center the current summary buffer.
380 In particular, if `vertical' do only vertical recentering.  If non-nil
381 and non-`vertical', do both horizontal and vertical recentering."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "none" nil)
384                  (const vertical)
385                  (integer :tag "height")
386                  (sexp :menu-tag "both" t)))
387
388 (defvar gnus-auto-center-group t
389   "*If non-nil, always center the group buffer.")
390
391 (defcustom gnus-show-all-headers nil
392   "*If non-nil, don't hide any headers."
393   :group 'gnus-article-hiding
394   :group 'gnus-article-headers
395   :type 'boolean)
396
397 (defcustom gnus-summary-ignore-duplicates nil
398   "*If non-nil, ignore articles with identical Message-ID headers."
399   :group 'gnus-summary
400   :type 'boolean)
401
402 (defcustom gnus-single-article-buffer t
403   "*If non-nil, display all articles in the same buffer.
404 If nil, each group will get its own article buffer."
405   :group 'gnus-article-various
406   :type 'boolean)
407
408 (defcustom gnus-break-pages t
409   "*If non-nil, do page breaking on articles.
410 The page delimiter is specified by the `gnus-page-delimiter'
411 variable."
412   :group 'gnus-article-various
413   :type 'boolean)
414
415 (defcustom gnus-move-split-methods nil
416   "*Variable used to suggest where articles are to be moved to.
417 It uses the same syntax as the `gnus-split-methods' variable.
418 However, whereas `gnus-split-methods' specifies file names as targets,
419 this variable specifies group names."
420   :group 'gnus-summary-mail
421   :type '(repeat (choice (list :value (fun) function)
422                          (cons :value ("" "") regexp (repeat string))
423                          (sexp :value nil))))
424
425 (defcustom gnus-unread-mark ?           ;Whitespace
426   "*Mark used for unread articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-ticked-mark ?!
431   "*Mark used for ticked articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-dormant-mark ??
436   "*Mark used for dormant articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-del-mark ?r
441   "*Mark used for del'd articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-read-mark ?R
446   "*Mark used for read articles."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-expirable-mark ?E
451   "*Mark used for expirable articles."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-killed-mark ?K
456   "*Mark used for killed articles."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-spam-mark ?$
461   "*Mark used for spam articles."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-souped-mark ?F
466   "*Mark used for souped articles."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-kill-file-mark ?X
471   "*Mark used for articles killed by kill files."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-low-score-mark ?Y
476   "*Mark used for articles with a low score."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-catchup-mark ?C
481   "*Mark used for articles that are caught up."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-replied-mark ?A
486   "*Mark used for articles that have been replied to."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-forwarded-mark ?F
491   "*Mark used for articles that have been forwarded."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-recent-mark ?N
496   "*Mark used for articles that are recent."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-cached-mark ?*
501   "*Mark used for articles that are in the cache."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-saved-mark ?S
506   "*Mark used for articles that have been saved."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-unseen-mark ?.
511   "*Mark used for articles that haven't been seen."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-no-mark ?               ;Whitespace
516   "*Mark used for articles that have no other secondary mark."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-ancient-mark ?O
521   "*Mark used for ancient articles."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-sparse-mark ?Q
526   "*Mark used for sparsely reffed articles."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-canceled-mark ?G
531   "*Mark used for canceled articles."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-duplicate-mark ?M
536   "*Mark used for duplicate articles."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-undownloaded-mark ?-
541   "*Mark used for articles that weren't downloaded."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-downloaded-mark ?+
546   "*Mark used for articles that were downloaded."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-downloadable-mark ?%
551   "*Mark used for articles that are to be downloaded."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-unsendable-mark ?=
556   "*Mark used for articles that won't be sent."
557   :group 'gnus-summary-marks
558   :type 'character)
559
560 (defcustom gnus-score-over-mark ?+
561   "*Score mark used for articles with high scores."
562   :group 'gnus-summary-marks
563   :type 'character)
564
565 (defcustom gnus-score-below-mark ?-
566   "*Score mark used for articles with low scores."
567   :group 'gnus-summary-marks
568   :type 'character)
569
570 (defcustom gnus-empty-thread-mark ?     ;Whitespace
571   "*There is no thread under the article."
572   :group 'gnus-summary-marks
573   :type 'character)
574
575 (defcustom gnus-not-empty-thread-mark ?=
576   "*There is a thread under the article."
577   :group 'gnus-summary-marks
578   :type 'character)
579
580 (defcustom gnus-view-pseudo-asynchronously nil
581   "*If non-nil, Gnus will view pseudo-articles asynchronously."
582   :group 'gnus-extract-view
583   :type 'boolean)
584
585 (defcustom gnus-auto-expirable-marks
586   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
587         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
588         gnus-souped-mark gnus-duplicate-mark)
589   "*The list of marks converted into expiration if a group is auto-expirable."
590   :version "21.1"
591   :group 'gnus-summary
592   :type '(repeat character))
593
594 (defcustom gnus-inhibit-user-auto-expire t
595   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
596   :version "21.1"
597   :group 'gnus-summary
598   :type 'boolean)
599
600 (defcustom gnus-view-pseudos nil
601   "*If `automatic', pseudo-articles will be viewed automatically.
602 If `not-confirm', pseudos will be viewed automatically, and the user
603 will not be asked to confirm the command."
604   :group 'gnus-extract-view
605   :type '(choice (const :tag "off" nil)
606                  (const automatic)
607                  (const not-confirm)))
608
609 (defcustom gnus-view-pseudos-separately t
610   "*If non-nil, one pseudo-article will be created for each file to be viewed.
611 If nil, all files that use the same viewing command will be given as a
612 list of parameters to that command."
613   :group 'gnus-extract-view
614   :type 'boolean)
615
616 (defcustom gnus-insert-pseudo-articles t
617   "*If non-nil, insert pseudo-articles when decoding articles."
618   :group 'gnus-extract-view
619   :type 'boolean)
620
621 (defcustom gnus-summary-dummy-line-format
622   "   %(:                             :%) %S\n"
623   "*The format specification for the dummy roots in the summary buffer.
624 It works along the same lines as a normal formatting string,
625 with some simple extensions.
626
627 %S  The subject
628
629 General format specifiers can also be used.
630 See `(gnus)Formatting Variables'."
631   :link '(custom-manual "(gnus)Formatting Variables")
632   :group 'gnus-threading
633   :type 'string)
634
635 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
636   "*The format specification for the summary mode line.
637 It works along the same lines as a normal formatting string,
638 with some simple extensions:
639
640 %G  Group name
641 %p  Unprefixed group name
642 %A  Current article number
643 %z  Current article score
644 %V  Gnus version
645 %U  Number of unread articles in the group
646 %e  Number of unselected articles in the group
647 %Z  A string with unread/unselected article counts
648 %g  Shortish group name
649 %S  Subject of the current article
650 %u  User-defined spec
651 %s  Current score file name
652 %d  Number of dormant articles
653 %r  Number of articles that have been marked as read in this session
654 %E  Number of articles expunged by the score files"
655   :group 'gnus-summary-format
656   :type 'string)
657
658 (defcustom gnus-list-identifiers nil
659   "Regexp that matches list identifiers to be removed from subject.
660 This can also be a list of regexps."
661   :version "21.1"
662   :group 'gnus-summary-format
663   :group 'gnus-article-hiding
664   :type '(choice (const :tag "none" nil)
665                  (regexp :value ".*")
666                  (repeat :value (".*") regexp)))
667
668 (defcustom gnus-summary-mark-below 0
669   "*Mark all articles with a score below this variable as read.
670 This variable is local to each summary buffer and usually set by the
671 score file."
672   :group 'gnus-score-default
673   :type 'integer)
674
675 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
676   "*List of functions used for sorting articles in the summary buffer.
677
678 Each function takes two articles and returns non-nil if the first
679 article should be sorted before the other.  If you use more than one
680 function, the primary sort function should be the last.  You should
681 probably always include `gnus-article-sort-by-number' in the list of
682 sorting functions -- preferably first.  Also note that sorting by date
683 is often much slower than sorting by number, and the sorting order is
684 very similar.  (Sorting by date means sorting by the time the message
685 was sent, sorting by number means sorting by arrival time.)
686
687 Ready-made functions include `gnus-article-sort-by-number',
688 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
689 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
690 and `gnus-article-sort-by-score'.
691
692 When threading is turned on, the variable `gnus-thread-sort-functions'
693 controls how articles are sorted."
694   :group 'gnus-summary-sort
695   :type '(repeat (choice (function-item gnus-article-sort-by-number)
696                          (function-item gnus-article-sort-by-author)
697                          (function-item gnus-article-sort-by-subject)
698                          (function-item gnus-article-sort-by-date)
699                          (function-item gnus-article-sort-by-score)
700                          (function-item gnus-article-sort-by-random)
701                          (function :tag "other"))))
702
703 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
704   "*List of functions used for sorting threads in the summary buffer.
705 By default, threads are sorted by article number.
706
707 Each function takes two threads and returns non-nil if the first
708 thread should be sorted before the other.  If you use more than one
709 function, the primary sort function should be the last.  You should
710 probably always include `gnus-thread-sort-by-number' in the list of
711 sorting functions -- preferably first.  Also note that sorting by date
712 is often much slower than sorting by number, and the sorting order is
713 very similar.  (Sorting by date means sorting by the time the message
714 was sent, sorting by number means sorting by arrival time.)
715
716 Ready-made functions include `gnus-thread-sort-by-number',
717 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
718 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
719 `gnus-thread-sort-by-most-recent-number',
720 `gnus-thread-sort-by-most-recent-date',
721 `gnus-thread-sort-by-random', and
722 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
723
724 When threading is turned off, the variable
725 `gnus-article-sort-functions' controls how articles are sorted."
726   :group 'gnus-summary-sort
727   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
728                          (function-item gnus-thread-sort-by-author)
729                          (function-item gnus-thread-sort-by-subject)
730                          (function-item gnus-thread-sort-by-date)
731                          (function-item gnus-thread-sort-by-score)
732                          (function-item gnus-thread-sort-by-total-score)
733                          (function-item gnus-thread-sort-by-random)
734                          (function :tag "other"))))
735
736 (defcustom gnus-thread-score-function '+
737   "*Function used for calculating the total score of a thread.
738
739 The function is called with the scores of the article and each
740 subthread and should then return the score of the thread.
741
742 Some functions you can use are `+', `max', or `min'."
743   :group 'gnus-summary-sort
744   :type 'function)
745
746 (defcustom gnus-summary-expunge-below nil
747   "All articles that have a score less than this variable will be expunged.
748 This variable is local to the summary buffers."
749   :group 'gnus-score-default
750   :type '(choice (const :tag "off" nil)
751                  integer))
752
753 (defcustom gnus-thread-expunge-below nil
754   "All threads that have a total score less than this variable will be expunged.
755 See `gnus-thread-score-function' for en explanation of what a
756 \"thread score\" is.
757
758 This variable is local to the summary buffers."
759   :group 'gnus-threading
760   :group 'gnus-score-default
761   :type '(choice (const :tag "off" nil)
762                  integer))
763
764 (defcustom gnus-summary-mode-hook nil
765   "*A hook for Gnus summary mode.
766 This hook is run before any variables are set in the summary buffer."
767   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
768   :group 'gnus-summary-various
769   :type 'hook)
770
771 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
772 (when (featurep 'xemacs)
773   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
774   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
775   (add-hook 'gnus-summary-mode-hook
776             'gnus-xmas-switch-horizontal-scrollbar-off))
777
778 (defcustom gnus-summary-menu-hook nil
779   "*Hook run after the creation of the summary mode menu."
780   :group 'gnus-summary-visual
781   :type 'hook)
782
783 (defcustom gnus-summary-exit-hook nil
784   "*A hook called on exit from the summary buffer.
785 It will be called with point in the group buffer."
786   :group 'gnus-summary-exit
787   :type 'hook)
788
789 (defcustom gnus-summary-prepare-hook nil
790   "*A hook called after the summary buffer has been generated.
791 If you want to modify the summary buffer, you can use this hook."
792   :group 'gnus-summary-various
793   :type 'hook)
794
795 (defcustom gnus-summary-prepared-hook nil
796   "*A hook called as the last thing after the summary buffer has been generated."
797   :group 'gnus-summary-various
798   :type 'hook)
799
800 (defcustom gnus-summary-generate-hook nil
801   "*A hook run just before generating the summary buffer.
802 This hook is commonly used to customize threading variables and the
803 like."
804   :group 'gnus-summary-various
805   :type 'hook)
806
807 (defcustom gnus-select-group-hook nil
808   "*A hook called when a newsgroup is selected.
809
810 If you'd like to simplify subjects like the
811 `gnus-summary-next-same-subject' command does, you can use the
812 following hook:
813
814  (add-hook gnus-select-group-hook
815            (lambda ()
816              (mapcar (lambda (header)
817                        (mail-header-set-subject
818                         header
819                         (gnus-simplify-subject
820                          (mail-header-subject header) 're-only)))
821                      gnus-newsgroup-headers)))"
822   :group 'gnus-group-select
823   :type 'hook)
824
825 (defcustom gnus-select-article-hook nil
826   "*A hook called when an article is selected."
827   :group 'gnus-summary-choose
828   :options '(gnus-agent-fetch-selected-article)
829   :type 'hook)
830
831 (defcustom gnus-visual-mark-article-hook
832   (list 'gnus-highlight-selected-summary)
833   "*Hook run after selecting an article in the summary buffer.
834 It is meant to be used for highlighting the article in some way.  It
835 is not run if `gnus-visual' is nil."
836   :group 'gnus-summary-visual
837   :type 'hook)
838
839 (defcustom gnus-parse-headers-hook nil
840   "*A hook called before parsing the headers."
841   :group 'gnus-various
842   :type 'hook)
843
844 (defcustom gnus-exit-group-hook nil
845   "*A hook called when exiting summary mode.
846 This hook is not called from the non-updating exit commands like `Q'."
847   :group 'gnus-various
848   :type 'hook)
849
850 (defcustom gnus-summary-update-hook
851   (list 'gnus-summary-highlight-line)
852   "*A hook called when a summary line is changed.
853 The hook will not be called if `gnus-visual' is nil.
854
855 The default function `gnus-summary-highlight-line' will
856 highlight the line according to the `gnus-summary-highlight'
857 variable."
858   :group 'gnus-summary-visual
859   :type 'hook)
860
861 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
862   "*A hook called when an article is selected for the first time.
863 The hook is intended to mark an article as read (or unread)
864 automatically when it is selected."
865   :group 'gnus-summary-choose
866   :type 'hook)
867
868 (defcustom gnus-group-no-more-groups-hook nil
869   "*A hook run when returning to group mode having no more (unread) groups."
870   :group 'gnus-group-select
871   :type 'hook)
872
873 (defcustom gnus-ps-print-hook nil
874   "*A hook run before ps-printing something from Gnus."
875   :group 'gnus-summary
876   :type 'hook)
877
878 (defcustom gnus-summary-article-move-hook nil
879   "*A hook called after an article is moved, copied, respooled, or crossposted."
880   :group 'gnus-summary
881   :type 'hook)
882
883 (defcustom gnus-summary-article-delete-hook nil
884   "*A hook called after an article is deleted."
885   :group 'gnus-summary
886   :type 'hook)
887
888 (defcustom gnus-summary-article-expire-hook nil
889   "*A hook called after an article is expired."
890   :group 'gnus-summary
891   :type 'hook)
892
893 (defcustom gnus-summary-display-arrow
894   (and (fboundp 'display-graphic-p)
895        (display-graphic-p))
896   "*If non-nil, display an arrow highlighting the current article."
897   :version "21.1"
898   :group 'gnus-summary
899   :type 'boolean)
900
901 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
902   "Face used for highlighting the current article in the summary buffer."
903   :group 'gnus-summary-visual
904   :type 'face)
905
906 (defvar gnus-tmp-downloaded nil)
907
908 (defcustom gnus-summary-highlight
909   '(((eq mark gnus-canceled-mark)
910      . gnus-summary-cancelled-face)
911     ((and uncached (> score default-high))
912      . gnus-summary-high-undownloaded-face)
913     ((and uncached (< score default-low))
914      . gnus-summary-low-undownloaded-face)
915     (uncached
916      . gnus-summary-normal-undownloaded-face)
917     ((and (> score default-high)
918           (or (eq mark gnus-dormant-mark)
919               (eq mark gnus-ticked-mark)))
920      . gnus-summary-high-ticked-face)
921     ((and (< score default-low)
922           (or (eq mark gnus-dormant-mark)
923               (eq mark gnus-ticked-mark)))
924      . gnus-summary-low-ticked-face)
925     ((or (eq mark gnus-dormant-mark)
926          (eq mark gnus-ticked-mark))
927      . gnus-summary-normal-ticked-face)
928     ((and (> score default-high) (eq mark gnus-ancient-mark))
929      . gnus-summary-high-ancient-face)
930     ((and (< score default-low) (eq mark gnus-ancient-mark))
931      . gnus-summary-low-ancient-face)
932     ((eq mark gnus-ancient-mark)
933      . gnus-summary-normal-ancient-face)
934     ((and (> score default-high) (eq mark gnus-unread-mark))
935      . gnus-summary-high-unread-face)
936     ((and (< score default-low) (eq mark gnus-unread-mark))
937      . gnus-summary-low-unread-face)
938     ((eq mark gnus-unread-mark)
939      . gnus-summary-normal-unread-face)
940     ((> score default-high)
941      . gnus-summary-high-read-face)
942     ((< score default-low)
943      . gnus-summary-low-read-face)
944     (t
945      . gnus-summary-normal-read-face))
946   "*Controls the highlighting of summary buffer lines.
947
948 A list of (FORM . FACE) pairs.  When deciding how a a particular
949 summary line should be displayed, each form is evaluated.  The content
950 of the face field after the first true form is used.  You can change
951 how those summary lines are displayed, by editing the face field.
952
953 You can use the following variables in the FORM field.
954
955 score:        The article's score
956 default:      The default article score.
957 default-high: The default score for high scored articles.
958 default-low:  The default score for low scored articles.
959 below:        The score below which articles are automatically marked as read.
960 mark:         The article's mark.
961 uncached:     Non-nil if the article is uncached."
962   :group 'gnus-summary-visual
963   :type '(repeat (cons (sexp :tag "Form" nil)
964                        face)))
965
966 (defcustom gnus-alter-header-function nil
967   "Function called to allow alteration of article header structures.
968 The function is called with one parameter, the article header vector,
969 which it may alter in any way."
970   :type '(choice (const :tag "None" nil)
971                  function)
972   :group 'gnus-summary)
973
974 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
975   "Variable that says which function should be used to decode a string with encoded words.")
976
977 (defcustom gnus-extra-headers '(To Newsgroups)
978   "*Extra headers to parse."
979   :version "21.1"
980   :group 'gnus-summary
981   :type '(repeat symbol))
982
983 (defcustom gnus-ignored-from-addresses
984   (and user-mail-address (regexp-quote user-mail-address))
985   "*Regexp of From headers that may be suppressed in favor of To headers."
986   :version "21.1"
987   :group 'gnus-summary
988   :type 'regexp)
989
990 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
991   "List of charsets that should be ignored.
992 When these charsets are used in the \"charset\" parameter, the
993 default charset will be used instead."
994   :version "21.1"
995   :type '(repeat symbol)
996   :group 'gnus-charset)
997
998 (gnus-define-group-parameter
999  ignored-charsets
1000  :type list
1001  :function-document
1002  "Return the ignored charsets of GROUP."
1003  :variable gnus-group-ignored-charsets-alist
1004  :variable-default
1005  '(("alt\\.chinese\\.text" iso-8859-1))
1006  :variable-document
1007  "Alist of regexps (to match group names) and charsets that should be ignored.
1008 When these charsets are used in the \"charset\" parameter, the
1009 default charset will be used instead."
1010  :variable-group gnus-charset
1011  :variable-type '(repeat (cons (regexp :tag "Group")
1012                                (repeat symbol)))
1013  :parameter-type '(choice :tag "Ignored charsets"
1014                           :value nil
1015                           (repeat (symbol)))
1016  :parameter-document       "\
1017 List of charsets that should be ignored.
1018
1019 When these charsets are used in the \"charset\" parameter, the
1020 default charset will be used instead.")
1021
1022 (defcustom gnus-group-highlight-words-alist nil
1023   "Alist of group regexps and highlight regexps.
1024 This variable uses the same syntax as `gnus-emphasis-alist'."
1025   :version "21.1"
1026   :type '(repeat (cons (regexp :tag "Group")
1027                        (repeat (list (regexp :tag "Highlight regexp")
1028                                      (number :tag "Group for entire word" 0)
1029                                      (number :tag "Group for displayed part" 0)
1030                                      (symbol :tag "Face"
1031                                              gnus-emphasis-highlight-words)))))
1032   :group 'gnus-summary-visual)
1033
1034 (defcustom gnus-summary-show-article-charset-alist
1035   nil
1036   "Alist of number and charset.
1037 The article will be shown with the charset corresponding to the
1038 numbered argument.
1039 For example: ((1 . cn-gb-2312) (2 . big5))."
1040   :version "21.1"
1041   :type '(repeat (cons (number :tag "Argument" 1)
1042                        (symbol :tag "Charset")))
1043   :group 'gnus-charset)
1044
1045 (defcustom gnus-preserve-marks t
1046   "Whether marks are preserved when moving, copying and respooling messages."
1047   :version "21.1"
1048   :type 'boolean
1049   :group 'gnus-summary-marks)
1050
1051 (defcustom gnus-alter-articles-to-read-function nil
1052   "Function to be called to alter the list of articles to be selected."
1053   :type '(choice (const nil) function)
1054   :group 'gnus-summary)
1055
1056 (defcustom gnus-orphan-score nil
1057   "*All orphans get this score added.  Set in the score file."
1058   :group 'gnus-score-default
1059   :type '(choice (const nil)
1060                  integer))
1061
1062 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1063   "*A regexp to match MIME parts when saving multiple parts of a
1064 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1065 This regexp will be used by default when prompting the user for which
1066 type of files to save."
1067   :group 'gnus-summary
1068   :type 'regexp)
1069
1070 (defcustom gnus-read-all-available-headers nil
1071   "Whether Gnus should parse all headers made available to it.
1072 This is mostly relevant for slow back ends where the user may
1073 wish to widen the summary buffer to include all headers
1074 that were fetched.  Say, for nnultimate groups."
1075   :group 'gnus-summary
1076   :type '(choice boolean regexp))
1077
1078 (defcustom gnus-summary-muttprint-program "muttprint"
1079   "Command (and optional arguments) used to run Muttprint."
1080   :version "21.3"
1081   :group 'gnus-summary
1082   :type 'string)
1083
1084 (defcustom gnus-article-loose-mime nil
1085   "If non-nil, don't require MIME-Version header.
1086 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1087 supply the MIME-Version header or deliberately strip it From the mail.
1088 Set it to non-nil, Gnus will treat some articles as MIME even if
1089 the MIME-Version header is missed."
1090   :version "21.3"
1091   :type 'boolean
1092   :group 'gnus-article-mime)
1093
1094 (defcustom gnus-article-emulate-mime t
1095   "If non-nil, use MIME emulation for uuencode and the like.
1096 This means that Gnus will search message bodies for text that look
1097 like uuencoded bits, yEncoded bits, and so on, and present that using
1098 the normal Gnus MIME machinery."
1099   :type 'boolean
1100   :group 'gnus-article-mime)
1101
1102 ;;; Internal variables
1103
1104 (defvar gnus-summary-display-cache nil)
1105 (defvar gnus-article-mime-handles nil)
1106 (defvar gnus-article-decoded-p nil)
1107 (defvar gnus-article-charset nil)
1108 (defvar gnus-article-ignored-charsets nil)
1109 (defvar gnus-scores-exclude-files nil)
1110 (defvar gnus-page-broken nil)
1111
1112 (defvar gnus-original-article nil)
1113 (defvar gnus-article-internal-prepare-hook nil)
1114 (defvar gnus-newsgroup-process-stack nil)
1115
1116 (defvar gnus-thread-indent-array nil)
1117 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1118 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1119   "Function called to sort the articles within a thread after it has been gathered together.")
1120
1121 (defvar gnus-summary-save-parts-type-history nil)
1122 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1123
1124 ;; Avoid highlighting in kill files.
1125 (defvar gnus-summary-inhibit-highlight nil)
1126 (defvar gnus-newsgroup-selected-overlay nil)
1127 (defvar gnus-inhibit-limiting nil)
1128 (defvar gnus-newsgroup-adaptive-score-file nil)
1129 (defvar gnus-current-score-file nil)
1130 (defvar gnus-current-move-group nil)
1131 (defvar gnus-current-copy-group nil)
1132 (defvar gnus-current-crosspost-group nil)
1133 (defvar gnus-newsgroup-display nil)
1134
1135 (defvar gnus-newsgroup-dependencies nil)
1136 (defvar gnus-newsgroup-adaptive nil)
1137 (defvar gnus-summary-display-article-function nil)
1138 (defvar gnus-summary-highlight-line-function nil
1139   "Function called after highlighting a summary line.")
1140
1141 (defvar gnus-summary-line-format-alist
1142   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1143     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1144     (?s gnus-tmp-subject-or-nil ?s)
1145     (?n gnus-tmp-name ?s)
1146     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1147         ?s)
1148     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1149             gnus-tmp-from) ?s)
1150     (?F gnus-tmp-from ?s)
1151     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1152     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1153     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1154     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1155     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1156     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1157     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1158     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1159     (?L gnus-tmp-lines ?s)
1160     (?O gnus-tmp-downloaded ?c)
1161     (?I gnus-tmp-indentation ?s)
1162     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1163     (?R gnus-tmp-replied ?c)
1164     (?\[ gnus-tmp-opening-bracket ?c)
1165     (?\] gnus-tmp-closing-bracket ?c)
1166     (?\> (make-string gnus-tmp-level ? ) ?s)
1167     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1168     (?i gnus-tmp-score ?d)
1169     (?z gnus-tmp-score-char ?c)
1170     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1171     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1172     (?U gnus-tmp-unread ?c)
1173     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1174         ?s)
1175     (?t (gnus-summary-number-of-articles-in-thread
1176          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1177         ?d)
1178     (?e (gnus-summary-number-of-articles-in-thread
1179          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1180         ?c)
1181     (?u gnus-tmp-user-defined ?s)
1182     (?P (gnus-pick-line-number) ?d)
1183     (?B gnus-tmp-thread-tree-header-string ?s)
1184     (user-date (gnus-user-date
1185                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1186   "An alist of format specifications that can appear in summary lines.
1187 These are paired with what variables they correspond with, along with
1188 the type of the variable (string, integer, character, etc).")
1189
1190 (defvar gnus-summary-dummy-line-format-alist
1191   `((?S gnus-tmp-subject ?s)
1192     (?N gnus-tmp-number ?d)
1193     (?u gnus-tmp-user-defined ?s)))
1194
1195 (defvar gnus-summary-mode-line-format-alist
1196   `((?G gnus-tmp-group-name ?s)
1197     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1198     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1199     (?A gnus-tmp-article-number ?d)
1200     (?Z gnus-tmp-unread-and-unselected ?s)
1201     (?V gnus-version ?s)
1202     (?U gnus-tmp-unread-and-unticked ?d)
1203     (?S gnus-tmp-subject ?s)
1204     (?e gnus-tmp-unselected ?d)
1205     (?u gnus-tmp-user-defined ?s)
1206     (?d (length gnus-newsgroup-dormant) ?d)
1207     (?t (length gnus-newsgroup-marked) ?d)
1208     (?h (length gnus-newsgroup-spam-marked) ?d)
1209     (?r (length gnus-newsgroup-reads) ?d)
1210     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1211     (?E gnus-newsgroup-expunged-tally ?d)
1212     (?s (gnus-current-score-file-nondirectory) ?s)))
1213
1214 (defvar gnus-last-search-regexp nil
1215   "Default regexp for article search command.")
1216
1217 (defvar gnus-last-shell-command nil
1218   "Default shell command on article.")
1219
1220 (defvar gnus-newsgroup-agentized nil
1221   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1222 (defvar gnus-newsgroup-begin nil)
1223 (defvar gnus-newsgroup-end nil)
1224 (defvar gnus-newsgroup-last-rmail nil)
1225 (defvar gnus-newsgroup-last-mail nil)
1226 (defvar gnus-newsgroup-last-folder nil)
1227 (defvar gnus-newsgroup-last-file nil)
1228 (defvar gnus-newsgroup-auto-expire nil)
1229 (defvar gnus-newsgroup-active nil)
1230
1231 (defvar gnus-newsgroup-data nil)
1232 (defvar gnus-newsgroup-data-reverse nil)
1233 (defvar gnus-newsgroup-limit nil)
1234 (defvar gnus-newsgroup-limits nil)
1235 (defvar gnus-summary-use-undownloaded-faces nil)
1236
1237 (defvar gnus-newsgroup-unreads nil
1238   "Sorted list of unread articles in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-unselected nil
1241   "Sorted list of unselected unread articles in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-reads nil
1244   "Alist of read articles and article marks in the current newsgroup.")
1245
1246 (defvar gnus-newsgroup-expunged-tally nil)
1247
1248 (defvar gnus-newsgroup-marked nil
1249   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1250
1251 (defvar gnus-newsgroup-spam-marked nil
1252   "List of ranges of articles that have been marked as spam.")
1253
1254 (defvar gnus-newsgroup-killed nil
1255   "List of ranges of articles that have been through the scoring process.")
1256
1257 (defvar gnus-newsgroup-cached nil
1258   "Sorted list of articles that come from the article cache.")
1259
1260 (defvar gnus-newsgroup-saved nil
1261   "List of articles that have been saved.")
1262
1263 (defvar gnus-newsgroup-kill-headers nil)
1264
1265 (defvar gnus-newsgroup-replied nil
1266   "List of articles that have been replied to in the current newsgroup.")
1267
1268 (defvar gnus-newsgroup-forwarded nil
1269   "List of articles that have been forwarded in the current newsgroup.")
1270
1271 (defvar gnus-newsgroup-recent nil
1272   "List of articles that have are recent in the current newsgroup.")
1273
1274 (defvar gnus-newsgroup-expirable nil
1275   "Sorted list of articles in the current newsgroup that can be expired.")
1276
1277 (defvar gnus-newsgroup-processable nil
1278   "List of articles in the current newsgroup that can be processed.")
1279
1280 (defvar gnus-newsgroup-downloadable nil
1281   "Sorted list of articles in the current newsgroup that can be processed.")
1282
1283 (defvar gnus-newsgroup-unfetched nil
1284   "Sorted list of articles in the current newsgroup whose headers have
1285 not been fetched into the agent.
1286
1287 This list will always be a subset of gnus-newsgroup-undownloaded.")
1288
1289 (defvar gnus-newsgroup-undownloaded nil
1290   "List of articles in the current newsgroup that haven't been downloaded.")
1291
1292 (defvar gnus-newsgroup-unsendable nil
1293   "List of articles in the current newsgroup that won't be sent.")
1294
1295 (defvar gnus-newsgroup-bookmarks nil
1296   "List of articles in the current newsgroup that have bookmarks.")
1297
1298 (defvar gnus-newsgroup-dormant nil
1299   "Sorted list of dormant articles in the current newsgroup.")
1300
1301 (defvar gnus-newsgroup-unseen nil
1302   "List of unseen articles in the current newsgroup.")
1303
1304 (defvar gnus-newsgroup-seen nil
1305   "Range of seen articles in the current newsgroup.")
1306
1307 (defvar gnus-newsgroup-articles nil
1308   "List of articles in the current newsgroup.")
1309
1310 (defvar gnus-newsgroup-scored nil
1311   "List of scored articles in the current newsgroup.")
1312
1313 (defvar gnus-newsgroup-headers nil
1314   "List of article headers in the current newsgroup.")
1315
1316 (defvar gnus-newsgroup-threads nil)
1317
1318 (defvar gnus-newsgroup-prepared nil
1319   "Whether the current group has been prepared properly.")
1320
1321 (defvar gnus-newsgroup-ancient nil
1322   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1323
1324 (defvar gnus-newsgroup-sparse nil)
1325
1326 (defvar gnus-current-article nil)
1327 (defvar gnus-article-current nil)
1328 (defvar gnus-current-headers nil)
1329 (defvar gnus-have-all-headers nil)
1330 (defvar gnus-last-article nil)
1331 (defvar gnus-newsgroup-history nil)
1332 (defvar gnus-newsgroup-charset nil)
1333 (defvar gnus-newsgroup-ephemeral-charset nil)
1334 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1335
1336 (defvar gnus-article-before-search nil)
1337
1338 (defvar gnus-summary-local-variables
1339   '(gnus-newsgroup-name
1340     gnus-newsgroup-begin gnus-newsgroup-end
1341     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1342     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1343     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1344     gnus-newsgroup-unselected gnus-newsgroup-marked
1345     gnus-newsgroup-spam-marked
1346     gnus-newsgroup-reads gnus-newsgroup-saved
1347     gnus-newsgroup-replied gnus-newsgroup-forwarded
1348     gnus-newsgroup-recent
1349     gnus-newsgroup-expirable
1350     gnus-newsgroup-processable gnus-newsgroup-killed
1351     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1352     gnus-newsgroup-unfetched
1353     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1354     gnus-newsgroup-seen gnus-newsgroup-articles
1355     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1356     gnus-newsgroup-headers gnus-newsgroup-threads
1357     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1358     gnus-current-article gnus-current-headers gnus-have-all-headers
1359     gnus-last-article gnus-article-internal-prepare-hook
1360     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1361     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1362     gnus-thread-expunge-below
1363     gnus-score-alist gnus-current-score-file
1364     (gnus-summary-expunge-below . global)
1365     (gnus-summary-mark-below . global)
1366     (gnus-orphan-score . global)
1367     gnus-newsgroup-active gnus-scores-exclude-files
1368     gnus-newsgroup-history gnus-newsgroup-ancient
1369     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1370     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1371     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1372     (gnus-newsgroup-expunged-tally . 0)
1373     gnus-cache-removable-articles gnus-newsgroup-cached
1374     gnus-newsgroup-data gnus-newsgroup-data-reverse
1375     gnus-newsgroup-limit gnus-newsgroup-limits
1376     gnus-newsgroup-charset gnus-newsgroup-display
1377     gnus-summary-use-undownloaded-faces)
1378   "Variables that are buffer-local to the summary buffers.")
1379
1380 (defvar gnus-newsgroup-variables nil
1381   "A list of variables that have separate values in different newsgroups.
1382 A list of newsgroup (summary buffer) local variables, or cons of
1383 variables and their default values (when the default values are not
1384 nil), that should be made global while the summary buffer is active.
1385 These variables can be used to set variables in the group parameters
1386 while still allowing them to affect operations done in other
1387 buffers. For example:
1388
1389 \(setq gnus-newsgroup-variables
1390      '(message-use-followup-to
1391        (gnus-visible-headers .
1392          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1393 ")
1394
1395 ;; Byte-compiler warning.
1396 (eval-when-compile
1397   ;; Bind features so that require will believe that gnus-sum has
1398   ;; already been loaded (avoids infinite recursion)
1399   (let ((features (cons 'gnus-sum features)))
1400     ;; Several of the declarations in gnus-sum are needed to load the
1401     ;; following files. Right now, these definitions have been
1402     ;; compiled but not defined (evaluated).  We could either do a
1403     ;; eval-and-compile about all of the declarations or evaluate the
1404     ;; source file.
1405     (if (boundp 'gnus-newsgroup-variables)
1406         nil
1407       (load "gnus-sum.el" t t t))
1408     (require 'gnus)
1409     (require 'gnus-agent)
1410     (require 'gnus-art)))
1411
1412 ;; MIME stuff.
1413
1414 (defvar gnus-decode-encoded-word-methods
1415   '(mail-decode-encoded-word-string)
1416   "List of methods used to decode encoded words.
1417
1418 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1419 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1420 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1421 whose names match REGEXP.
1422
1423 For example:
1424 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1425  mail-decode-encoded-word-string
1426  (\"chinese\" . rfc1843-decode-string))")
1427
1428 (defvar gnus-decode-encoded-word-methods-cache nil)
1429
1430 (defun gnus-multi-decode-encoded-word-string (string)
1431   "Apply the functions from `gnus-encoded-word-methods' that match."
1432   (unless (and gnus-decode-encoded-word-methods-cache
1433                (eq gnus-newsgroup-name
1434                    (car gnus-decode-encoded-word-methods-cache)))
1435     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1436     (mapcar (lambda (x)
1437               (if (symbolp x)
1438                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1439                 (if (and gnus-newsgroup-name
1440                          (string-match (car x) gnus-newsgroup-name))
1441                     (nconc gnus-decode-encoded-word-methods-cache
1442                            (list (cdr x))))))
1443             gnus-decode-encoded-word-methods))
1444   (let ((xlist gnus-decode-encoded-word-methods-cache))
1445     (pop xlist)
1446     (while xlist
1447       (setq string (funcall (pop xlist) string))))
1448   string)
1449
1450 ;; Subject simplification.
1451
1452 (defun gnus-simplify-whitespace (str)
1453   "Remove excessive whitespace from STR."
1454   ;; Multiple spaces.
1455   (while (string-match "[ \t][ \t]+" str)
1456     (setq str (concat (substring str 0 (match-beginning 0))
1457                         " "
1458                         (substring str (match-end 0)))))
1459   ;; Leading spaces.
1460   (when (string-match "^[ \t]+" str)
1461     (setq str (substring str (match-end 0))))
1462   ;; Trailing spaces.
1463   (when (string-match "[ \t]+$" str)
1464     (setq str (substring str 0 (match-beginning 0))))
1465   str)
1466
1467 (defun gnus-simplify-all-whitespace (str)
1468   "Remove all whitespace from STR."
1469   (while (string-match "[ \t\n]+" str)
1470     (setq str (replace-match "" nil nil str)))
1471   str)
1472
1473 (defsubst gnus-simplify-subject-re (subject)
1474   "Remove \"Re:\" from subject lines."
1475   (if (string-match message-subject-re-regexp subject)
1476       (substring subject (match-end 0))
1477     subject))
1478
1479 (defun gnus-simplify-subject (subject &optional re-only)
1480   "Remove `Re:' and words in parentheses.
1481 If RE-ONLY is non-nil, strip leading `Re:'s only."
1482   (let ((case-fold-search t))           ;Ignore case.
1483     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1484     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1485       (setq subject (substring subject (match-end 0))))
1486     ;; Remove uninteresting prefixes.
1487     (when (and (not re-only)
1488                gnus-simplify-ignored-prefixes
1489                (string-match gnus-simplify-ignored-prefixes subject))
1490       (setq subject (substring subject (match-end 0))))
1491     ;; Remove words in parentheses from end.
1492     (unless re-only
1493       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1494         (setq subject (substring subject 0 (match-beginning 0)))))
1495     ;; Return subject string.
1496     subject))
1497
1498 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1499 ;; all whitespace.
1500 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1501   (goto-char (point-min))
1502   (while (re-search-forward regexp nil t)
1503     (replace-match (or newtext ""))))
1504
1505 (defun gnus-simplify-buffer-fuzzy ()
1506   "Simplify string in the buffer fuzzily.
1507 The string in the accessible portion of the current buffer is simplified.
1508 It is assumed to be a single-line subject.
1509 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1510 matter is removed.  Additional things can be deleted by setting
1511 `gnus-simplify-subject-fuzzy-regexp'."
1512   (let ((case-fold-search t)
1513         (modified-tick))
1514     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1515
1516     (while (not (eq modified-tick (buffer-modified-tick)))
1517       (setq modified-tick (buffer-modified-tick))
1518       (cond
1519        ((listp gnus-simplify-subject-fuzzy-regexp)
1520         (mapcar 'gnus-simplify-buffer-fuzzy-step
1521                 gnus-simplify-subject-fuzzy-regexp))
1522        (gnus-simplify-subject-fuzzy-regexp
1523         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1524       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1525       (gnus-simplify-buffer-fuzzy-step
1526        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1527       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1528
1529     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1530     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1531     (gnus-simplify-buffer-fuzzy-step " $")
1532     (gnus-simplify-buffer-fuzzy-step "^ +")))
1533
1534 (defun gnus-simplify-subject-fuzzy (subject)
1535   "Simplify a subject string fuzzily.
1536 See `gnus-simplify-buffer-fuzzy' for details."
1537   (save-excursion
1538     (gnus-set-work-buffer)
1539     (let ((case-fold-search t))
1540       ;; Remove uninteresting prefixes.
1541       (when (and gnus-simplify-ignored-prefixes
1542                  (string-match gnus-simplify-ignored-prefixes subject))
1543         (setq subject (substring subject (match-end 0))))
1544       (insert subject)
1545       (inline (gnus-simplify-buffer-fuzzy))
1546       (buffer-string))))
1547
1548 (defsubst gnus-simplify-subject-fully (subject)
1549   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1550   (cond
1551    (gnus-simplify-subject-functions
1552     (gnus-map-function gnus-simplify-subject-functions subject))
1553    ((null gnus-summary-gather-subject-limit)
1554     (gnus-simplify-subject-re subject))
1555    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1556     (gnus-simplify-subject-fuzzy subject))
1557    ((numberp gnus-summary-gather-subject-limit)
1558     (gnus-limit-string (gnus-simplify-subject-re subject)
1559                        gnus-summary-gather-subject-limit))
1560    (t
1561     subject)))
1562
1563 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1564   "Check whether two subjects are equal.
1565 If optional argument SIMPLE-FIRST is t, first argument is already
1566 simplified."
1567   (cond
1568    ((null simple-first)
1569     (equal (gnus-simplify-subject-fully s1)
1570            (gnus-simplify-subject-fully s2)))
1571    (t
1572     (equal s1
1573            (gnus-simplify-subject-fully s2)))))
1574
1575 (defun gnus-summary-bubble-group ()
1576   "Increase the score of the current group.
1577 This is a handy function to add to `gnus-summary-exit-hook' to
1578 increase the score of each group you read."
1579   (gnus-group-add-score gnus-newsgroup-name))
1580
1581 \f
1582 ;;;
1583 ;;; Gnus summary mode
1584 ;;;
1585
1586 (put 'gnus-summary-mode 'mode-class 'special)
1587
1588 (defvar gnus-article-commands-menu)
1589
1590 ;; Non-orthogonal keys
1591
1592 (gnus-define-keys gnus-summary-mode-map
1593   " " gnus-summary-next-page
1594   "\177" gnus-summary-prev-page
1595   [delete] gnus-summary-prev-page
1596   [backspace] gnus-summary-prev-page
1597   "\r" gnus-summary-scroll-up
1598   "\M-\r" gnus-summary-scroll-down
1599   "n" gnus-summary-next-unread-article
1600   "p" gnus-summary-prev-unread-article
1601   "N" gnus-summary-next-article
1602   "P" gnus-summary-prev-article
1603   "\M-\C-n" gnus-summary-next-same-subject
1604   "\M-\C-p" gnus-summary-prev-same-subject
1605   "\M-n" gnus-summary-next-unread-subject
1606   "\M-p" gnus-summary-prev-unread-subject
1607   "." gnus-summary-first-unread-article
1608   "," gnus-summary-best-unread-article
1609   "\M-s" gnus-summary-search-article-forward
1610   "\M-r" gnus-summary-search-article-backward
1611   "<" gnus-summary-beginning-of-article
1612   ">" gnus-summary-end-of-article
1613   "j" gnus-summary-goto-article
1614   "^" gnus-summary-refer-parent-article
1615   "\M-^" gnus-summary-refer-article
1616   "u" gnus-summary-tick-article-forward
1617   "!" gnus-summary-tick-article-forward
1618   "U" gnus-summary-tick-article-backward
1619   "d" gnus-summary-mark-as-read-forward
1620   "D" gnus-summary-mark-as-read-backward
1621   "E" gnus-summary-mark-as-expirable
1622   "\M-u" gnus-summary-clear-mark-forward
1623   "\M-U" gnus-summary-clear-mark-backward
1624   "k" gnus-summary-kill-same-subject-and-select
1625   "\C-k" gnus-summary-kill-same-subject
1626   "\M-\C-k" gnus-summary-kill-thread
1627   "\M-\C-l" gnus-summary-lower-thread
1628   "e" gnus-summary-edit-article
1629   "#" gnus-summary-mark-as-processable
1630   "\M-#" gnus-summary-unmark-as-processable
1631   "\M-\C-t" gnus-summary-toggle-threads
1632   "\M-\C-s" gnus-summary-show-thread
1633   "\M-\C-h" gnus-summary-hide-thread
1634   "\M-\C-f" gnus-summary-next-thread
1635   "\M-\C-b" gnus-summary-prev-thread
1636   [(meta down)] gnus-summary-next-thread
1637   [(meta up)] gnus-summary-prev-thread
1638   "\M-\C-u" gnus-summary-up-thread
1639   "\M-\C-d" gnus-summary-down-thread
1640   "&" gnus-summary-execute-command
1641   "c" gnus-summary-catchup-and-exit
1642   "\C-w" gnus-summary-mark-region-as-read
1643   "\C-t" gnus-summary-toggle-truncation
1644   "?" gnus-summary-mark-as-dormant
1645   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1646   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1647   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1648   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1649   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1650   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1651   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1652   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1653   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1654   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1655   "=" gnus-summary-expand-window
1656   "\C-x\C-s" gnus-summary-reselect-current-group
1657   "\M-g" gnus-summary-rescan-group
1658   "w" gnus-summary-stop-page-breaking
1659   "\C-c\C-r" gnus-summary-caesar-message
1660   "f" gnus-summary-followup
1661   "F" gnus-summary-followup-with-original
1662   "C" gnus-summary-cancel-article
1663   "r" gnus-summary-reply
1664   "R" gnus-summary-reply-with-original
1665   "\C-c\C-f" gnus-summary-mail-forward
1666   "o" gnus-summary-save-article
1667   "\C-o" gnus-summary-save-article-mail
1668   "|" gnus-summary-pipe-output
1669   "\M-k" gnus-summary-edit-local-kill
1670   "\M-K" gnus-summary-edit-global-kill
1671   ;; "V" gnus-version
1672   "\C-c\C-d" gnus-summary-describe-group
1673   "q" gnus-summary-exit
1674   "Q" gnus-summary-exit-no-update
1675   "\C-c\C-i" gnus-info-find-node
1676   gnus-mouse-2 gnus-mouse-pick-article
1677   "m" gnus-summary-mail-other-window
1678   "a" gnus-summary-post-news
1679   "i" gnus-summary-news-other-window
1680   "x" gnus-summary-limit-to-unread
1681   "s" gnus-summary-isearch-article
1682   "t" gnus-summary-toggle-header
1683   "g" gnus-summary-show-article
1684   "l" gnus-summary-goto-last-article
1685   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1686   "\C-d" gnus-summary-enter-digest-group
1687   "\M-\C-d" gnus-summary-read-document
1688   "\M-\C-e" gnus-summary-edit-parameters
1689   "\M-\C-a" gnus-summary-customize-parameters
1690   "\C-c\C-b" gnus-bug
1691   "*" gnus-cache-enter-article
1692   "\M-*" gnus-cache-remove-article
1693   "\M-&" gnus-summary-universal-argument
1694   "\C-l" gnus-recenter
1695   "I" gnus-summary-increase-score
1696   "L" gnus-summary-lower-score
1697   "\M-i" gnus-symbolic-argument
1698   "h" gnus-summary-select-article-buffer
1699
1700   "b" gnus-article-view-part
1701   "\M-t" gnus-summary-toggle-display-buttonized
1702
1703   "V" gnus-summary-score-map
1704   "X" gnus-uu-extract-map
1705   "S" gnus-summary-send-map)
1706
1707 ;; Sort of orthogonal keymap
1708 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1709   "t" gnus-summary-tick-article-forward
1710   "!" gnus-summary-tick-article-forward
1711   "d" gnus-summary-mark-as-read-forward
1712   "r" gnus-summary-mark-as-read-forward
1713   "c" gnus-summary-clear-mark-forward
1714   " " gnus-summary-clear-mark-forward
1715   "e" gnus-summary-mark-as-expirable
1716   "x" gnus-summary-mark-as-expirable
1717   "?" gnus-summary-mark-as-dormant
1718   "b" gnus-summary-set-bookmark
1719   "B" gnus-summary-remove-bookmark
1720   "#" gnus-summary-mark-as-processable
1721   "\M-#" gnus-summary-unmark-as-processable
1722   "S" gnus-summary-limit-include-expunged
1723   "C" gnus-summary-catchup
1724   "H" gnus-summary-catchup-to-here
1725   "h" gnus-summary-catchup-from-here
1726   "\C-c" gnus-summary-catchup-all
1727   "k" gnus-summary-kill-same-subject-and-select
1728   "K" gnus-summary-kill-same-subject
1729   "P" gnus-uu-mark-map)
1730
1731 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1732   "c" gnus-summary-clear-above
1733   "u" gnus-summary-tick-above
1734   "m" gnus-summary-mark-above
1735   "k" gnus-summary-kill-below)
1736
1737 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1738   "/" gnus-summary-limit-to-subject
1739   "n" gnus-summary-limit-to-articles
1740   "w" gnus-summary-pop-limit
1741   "s" gnus-summary-limit-to-subject
1742   "a" gnus-summary-limit-to-author
1743   "u" gnus-summary-limit-to-unread
1744   "m" gnus-summary-limit-to-marks
1745   "M" gnus-summary-limit-exclude-marks
1746   "v" gnus-summary-limit-to-score
1747   "*" gnus-summary-limit-include-cached
1748   "D" gnus-summary-limit-include-dormant
1749   "T" gnus-summary-limit-include-thread
1750   "d" gnus-summary-limit-exclude-dormant
1751   "t" gnus-summary-limit-to-age
1752   "." gnus-summary-limit-to-unseen
1753   "x" gnus-summary-limit-to-extra
1754   "p" gnus-summary-limit-to-display-predicate
1755   "E" gnus-summary-limit-include-expunged
1756   "c" gnus-summary-limit-exclude-childless-dormant
1757   "C" gnus-summary-limit-mark-excluded-as-read
1758   "o" gnus-summary-insert-old-articles
1759   "N" gnus-summary-insert-new-articles
1760   "r" gnus-summary-limit-to-replied)
1761
1762 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1763   "n" gnus-summary-next-unread-article
1764   "p" gnus-summary-prev-unread-article
1765   "N" gnus-summary-next-article
1766   "P" gnus-summary-prev-article
1767   "\C-n" gnus-summary-next-same-subject
1768   "\C-p" gnus-summary-prev-same-subject
1769   "\M-n" gnus-summary-next-unread-subject
1770   "\M-p" gnus-summary-prev-unread-subject
1771   "f" gnus-summary-first-unread-article
1772   "b" gnus-summary-best-unread-article
1773   "j" gnus-summary-goto-article
1774   "g" gnus-summary-goto-subject
1775   "l" gnus-summary-goto-last-article
1776   "o" gnus-summary-pop-article)
1777
1778 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1779   "k" gnus-summary-kill-thread
1780   "l" gnus-summary-lower-thread
1781   "i" gnus-summary-raise-thread
1782   "T" gnus-summary-toggle-threads
1783   "t" gnus-summary-rethread-current
1784   "^" gnus-summary-reparent-thread
1785   "s" gnus-summary-show-thread
1786   "S" gnus-summary-show-all-threads
1787   "h" gnus-summary-hide-thread
1788   "H" gnus-summary-hide-all-threads
1789   "n" gnus-summary-next-thread
1790   "p" gnus-summary-prev-thread
1791   "u" gnus-summary-up-thread
1792   "o" gnus-summary-top-thread
1793   "d" gnus-summary-down-thread
1794   "#" gnus-uu-mark-thread
1795   "\M-#" gnus-uu-unmark-thread)
1796
1797 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1798   "g" gnus-summary-prepare
1799   "c" gnus-summary-insert-cached-articles
1800   "d" gnus-summary-insert-dormant-articles)
1801
1802 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1803   "c" gnus-summary-catchup-and-exit
1804   "C" gnus-summary-catchup-all-and-exit
1805   "E" gnus-summary-exit-no-update
1806   "Q" gnus-summary-exit
1807   "Z" gnus-summary-exit
1808   "n" gnus-summary-catchup-and-goto-next-group
1809   "R" gnus-summary-reselect-current-group
1810   "G" gnus-summary-rescan-group
1811   "N" gnus-summary-next-group
1812   "s" gnus-summary-save-newsrc
1813   "P" gnus-summary-prev-group)
1814
1815 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1816   " " gnus-summary-next-page
1817   "n" gnus-summary-next-page
1818   "\177" gnus-summary-prev-page
1819   [delete] gnus-summary-prev-page
1820   "p" gnus-summary-prev-page
1821   "\r" gnus-summary-scroll-up
1822   "\M-\r" gnus-summary-scroll-down
1823   "<" gnus-summary-beginning-of-article
1824   ">" gnus-summary-end-of-article
1825   "b" gnus-summary-beginning-of-article
1826   "e" gnus-summary-end-of-article
1827   "^" gnus-summary-refer-parent-article
1828   "r" gnus-summary-refer-parent-article
1829   "D" gnus-summary-enter-digest-group
1830   "R" gnus-summary-refer-references
1831   "T" gnus-summary-refer-thread
1832   "g" gnus-summary-show-article
1833   "s" gnus-summary-isearch-article
1834   "P" gnus-summary-print-article
1835   "M" gnus-mailing-list-insinuate
1836   "t" gnus-article-babel)
1837
1838 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1839   "b" gnus-article-add-buttons
1840   "B" gnus-article-add-buttons-to-head
1841   "o" gnus-article-treat-overstrike
1842   "e" gnus-article-emphasize
1843   "w" gnus-article-fill-cited-article
1844   "Q" gnus-article-fill-long-lines
1845   "C" gnus-article-capitalize-sentences
1846   "c" gnus-article-remove-cr
1847   "q" gnus-article-de-quoted-unreadable
1848   "6" gnus-article-de-base64-unreadable
1849   "Z" gnus-article-decode-HZ
1850   "A" gnus-article-treat-ansi-sequences
1851   "h" gnus-article-wash-html
1852   "u" gnus-article-unsplit-urls
1853   "s" gnus-summary-force-verify-and-decrypt
1854   "f" gnus-article-display-x-face
1855   "l" gnus-summary-stop-page-breaking
1856   "r" gnus-summary-caesar-message
1857   "m" gnus-summary-morse-message
1858   "t" gnus-summary-toggle-header
1859   "g" gnus-treat-smiley
1860   "v" gnus-summary-verbose-headers
1861   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1862   "p" gnus-article-verify-x-pgp-sig
1863   "d" gnus-article-treat-dumbquotes)
1864
1865 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1866   ;; mnemonic: deuglif*Y*
1867   "u" gnus-article-outlook-unwrap-lines
1868   "a" gnus-article-outlook-repair-attribution
1869   "c" gnus-article-outlook-rearrange-citation
1870   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1871
1872 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1873   "a" gnus-article-hide
1874   "h" gnus-article-hide-headers
1875   "b" gnus-article-hide-boring-headers
1876   "s" gnus-article-hide-signature
1877   "c" gnus-article-hide-citation
1878   "C" gnus-article-hide-citation-in-followups
1879   "l" gnus-article-hide-list-identifiers
1880   "B" gnus-article-strip-banner
1881   "P" gnus-article-hide-pem
1882   "\C-c" gnus-article-hide-citation-maybe)
1883
1884 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1885   "a" gnus-article-highlight
1886   "h" gnus-article-highlight-headers
1887   "c" gnus-article-highlight-citation
1888   "s" gnus-article-highlight-signature)
1889
1890 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1891   "f" gnus-article-treat-fold-headers
1892   "u" gnus-article-treat-unfold-headers
1893   "n" gnus-article-treat-fold-newsgroups)
1894
1895 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1896   "x" gnus-article-display-x-face
1897   "d" gnus-article-display-face
1898   "s" gnus-treat-smiley
1899   "D" gnus-article-remove-images
1900   "f" gnus-treat-from-picon
1901   "m" gnus-treat-mail-picon
1902   "n" gnus-treat-newsgroups-picon)
1903
1904 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1905   "w" gnus-article-decode-mime-words
1906   "c" gnus-article-decode-charset
1907   "v" gnus-mime-view-all-parts
1908   "b" gnus-article-view-part)
1909
1910 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1911   "z" gnus-article-date-ut
1912   "u" gnus-article-date-ut
1913   "l" gnus-article-date-local
1914   "p" gnus-article-date-english
1915   "e" gnus-article-date-lapsed
1916   "o" gnus-article-date-original
1917   "i" gnus-article-date-iso8601
1918   "s" gnus-article-date-user)
1919
1920 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1921   "t" gnus-article-remove-trailing-blank-lines
1922   "l" gnus-article-strip-leading-blank-lines
1923   "m" gnus-article-strip-multiple-blank-lines
1924   "a" gnus-article-strip-blank-lines
1925   "A" gnus-article-strip-all-blank-lines
1926   "s" gnus-article-strip-leading-space
1927   "e" gnus-article-strip-trailing-space
1928   "w" gnus-article-remove-leading-whitespace)
1929
1930 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1931   "v" gnus-version
1932   "f" gnus-summary-fetch-faq
1933   "d" gnus-summary-describe-group
1934   "h" gnus-summary-describe-briefly
1935   "i" gnus-info-find-node
1936   "c" gnus-group-fetch-charter
1937   "C" gnus-group-fetch-control)
1938
1939 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1940   "e" gnus-summary-expire-articles
1941   "\M-\C-e" gnus-summary-expire-articles-now
1942   "\177" gnus-summary-delete-article
1943   [delete] gnus-summary-delete-article
1944   [backspace] gnus-summary-delete-article
1945   "m" gnus-summary-move-article
1946   "r" gnus-summary-respool-article
1947   "w" gnus-summary-edit-article
1948   "c" gnus-summary-copy-article
1949   "B" gnus-summary-crosspost-article
1950   "q" gnus-summary-respool-query
1951   "t" gnus-summary-respool-trace
1952   "i" gnus-summary-import-article
1953   "I" gnus-summary-create-article
1954   "p" gnus-summary-article-posted-p)
1955
1956 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1957   "o" gnus-summary-save-article
1958   "m" gnus-summary-save-article-mail
1959   "F" gnus-summary-write-article-file
1960   "r" gnus-summary-save-article-rmail
1961   "f" gnus-summary-save-article-file
1962   "b" gnus-summary-save-article-body-file
1963   "h" gnus-summary-save-article-folder
1964   "v" gnus-summary-save-article-vm
1965   "p" gnus-summary-pipe-output
1966   "P" gnus-summary-muttprint
1967   "s" gnus-soup-add-article)
1968
1969 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1970   "b" gnus-summary-display-buttonized
1971   "m" gnus-summary-repair-multipart
1972   "v" gnus-article-view-part
1973   "o" gnus-article-save-part
1974   "c" gnus-article-copy-part
1975   "C" gnus-article-view-part-as-charset
1976   "e" gnus-article-view-part-externally
1977   "E" gnus-article-encrypt-body
1978   "i" gnus-article-inline-part
1979   "|" gnus-article-pipe-part)
1980
1981 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1982   "p" gnus-summary-mark-as-processable
1983   "u" gnus-summary-unmark-as-processable
1984   "U" gnus-summary-unmark-all-processable
1985   "v" gnus-uu-mark-over
1986   "s" gnus-uu-mark-series
1987   "r" gnus-uu-mark-region
1988   "g" gnus-uu-unmark-region
1989   "R" gnus-uu-mark-by-regexp
1990   "G" gnus-uu-unmark-by-regexp
1991   "t" gnus-uu-mark-thread
1992   "T" gnus-uu-unmark-thread
1993   "a" gnus-uu-mark-all
1994   "b" gnus-uu-mark-buffer
1995   "S" gnus-uu-mark-sparse
1996   "k" gnus-summary-kill-process-mark
1997   "y" gnus-summary-yank-process-mark
1998   "w" gnus-summary-save-process-mark
1999   "i" gnus-uu-invert-processable)
2000
2001 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2002   ;;"x" gnus-uu-extract-any
2003   "m" gnus-summary-save-parts
2004   "u" gnus-uu-decode-uu
2005   "U" gnus-uu-decode-uu-and-save
2006   "s" gnus-uu-decode-unshar
2007   "S" gnus-uu-decode-unshar-and-save
2008   "o" gnus-uu-decode-save
2009   "O" gnus-uu-decode-save
2010   "b" gnus-uu-decode-binhex
2011   "B" gnus-uu-decode-binhex
2012   "p" gnus-uu-decode-postscript
2013   "P" gnus-uu-decode-postscript-and-save)
2014
2015 (gnus-define-keys
2016     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2017   "u" gnus-uu-decode-uu-view
2018   "U" gnus-uu-decode-uu-and-save-view
2019   "s" gnus-uu-decode-unshar-view
2020   "S" gnus-uu-decode-unshar-and-save-view
2021   "o" gnus-uu-decode-save-view
2022   "O" gnus-uu-decode-save-view
2023   "b" gnus-uu-decode-binhex-view
2024   "B" gnus-uu-decode-binhex-view
2025   "p" gnus-uu-decode-postscript-view
2026   "P" gnus-uu-decode-postscript-and-save-view)
2027
2028 (defvar gnus-article-post-menu nil)
2029
2030 (defconst gnus-summary-menu-maxlen 20)
2031
2032 (defun gnus-summary-menu-split (menu)
2033   ;; If we have lots of elements, divide them into groups of 20
2034   ;; and make a pane (or submenu) for each one.
2035   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2036       (let ((menu menu) sublists next
2037             (i 1))
2038         (while menu
2039           ;; Pull off the next gnus-summary-menu-maxlen elements
2040           ;; and make them the next element of sublist.
2041           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2042           (if next
2043               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2044                       nil))
2045           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2046                                              (aref (car (last menu)) 0)) menu)
2047                                sublists))
2048           (setq i (1+ i))
2049           (setq menu next))
2050         (nreverse sublists))
2051     ;; Few elements--put them all in one pane.
2052     menu))
2053
2054 (defun gnus-summary-make-menu-bar ()
2055   (gnus-turn-off-edit-menu 'summary)
2056
2057   (unless (boundp 'gnus-summary-misc-menu)
2058
2059     (easy-menu-define
2060       gnus-summary-kill-menu gnus-summary-mode-map ""
2061       (cons
2062        "Score"
2063        (nconc
2064         (list
2065          ["Customize" gnus-score-customize t])
2066         (gnus-make-score-map 'increase)
2067         (gnus-make-score-map 'lower)
2068         '(("Mark"
2069            ["Kill below" gnus-summary-kill-below t]
2070            ["Mark above" gnus-summary-mark-above t]
2071            ["Tick above" gnus-summary-tick-above t]
2072            ["Clear above" gnus-summary-clear-above t])
2073           ["Current score" gnus-summary-current-score t]
2074           ["Set score" gnus-summary-set-score t]
2075           ["Switch current score file..." gnus-score-change-score-file t]
2076           ["Set mark below..." gnus-score-set-mark-below t]
2077           ["Set expunge below..." gnus-score-set-expunge-below t]
2078           ["Edit current score file" gnus-score-edit-current-scores t]
2079           ["Edit score file" gnus-score-edit-file t]
2080           ["Trace score" gnus-score-find-trace t]
2081           ["Find words" gnus-score-find-favourite-words t]
2082           ["Rescore buffer" gnus-summary-rescore t]
2083           ["Increase score..." gnus-summary-increase-score t]
2084           ["Lower score..." gnus-summary-lower-score t]))))
2085
2086     ;; Define both the Article menu in the summary buffer and the
2087     ;; equivalent Commands menu in the article buffer here for
2088     ;; consistency.
2089     (let ((innards
2090            `(("Hide"
2091               ["All" gnus-article-hide t]
2092               ["Headers" gnus-article-hide-headers t]
2093               ["Signature" gnus-article-hide-signature t]
2094               ["Citation" gnus-article-hide-citation t]
2095               ["List identifiers" gnus-article-hide-list-identifiers t]
2096               ["Banner" gnus-article-strip-banner t]
2097               ["Boring headers" gnus-article-hide-boring-headers t])
2098              ("Highlight"
2099               ["All" gnus-article-highlight t]
2100               ["Headers" gnus-article-highlight-headers t]
2101               ["Signature" gnus-article-highlight-signature t]
2102               ["Citation" gnus-article-highlight-citation t])
2103              ("MIME"
2104               ["Words" gnus-article-decode-mime-words t]
2105               ["Charset" gnus-article-decode-charset t]
2106               ["QP" gnus-article-de-quoted-unreadable t]
2107               ["Base64" gnus-article-de-base64-unreadable t]
2108               ["View MIME buttons" gnus-summary-display-buttonized t]
2109               ["View all" gnus-mime-view-all-parts t]
2110               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2111               ["Encrypt body" gnus-article-encrypt-body
2112                :active (not (gnus-group-read-only-p))
2113                ,@(if (featurep 'xemacs) nil
2114                    '(:help "Encrypt the message body on disk"))]
2115               ["Extract all parts..." gnus-summary-save-parts t]
2116               ("Multipart"
2117                ["Repair multipart" gnus-summary-repair-multipart t]
2118                ["Pipe part..." gnus-article-pipe-part t]
2119                ["Inline part" gnus-article-inline-part t]
2120                ["Encrypt body" gnus-article-encrypt-body
2121                 :active (not (gnus-group-read-only-p))
2122                ,@(if (featurep 'xemacs) nil
2123                    '(:help "Encrypt the message body on disk"))]
2124                ["View part externally" gnus-article-view-part-externally t]
2125                ["View part with charset..." gnus-article-view-part-as-charset t]
2126                ["Copy part" gnus-article-copy-part t]
2127                ["Save part..." gnus-article-save-part t]
2128                ["View part" gnus-article-view-part t]))
2129              ("Date"
2130               ["Local" gnus-article-date-local t]
2131               ["ISO8601" gnus-article-date-iso8601 t]
2132               ["UT" gnus-article-date-ut t]
2133               ["Original" gnus-article-date-original t]
2134               ["Lapsed" gnus-article-date-lapsed t]
2135               ["User-defined" gnus-article-date-user t])
2136              ("Display"
2137               ["Remove images" gnus-article-remove-images t]
2138               ["Toggle smiley" gnus-treat-smiley t]
2139               ["Show X-Face" gnus-article-display-x-face t]
2140               ["Show picons in From" gnus-treat-from-picon t]
2141               ["Show picons in mail headers" gnus-treat-mail-picon t]
2142               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2143               ("View as different encoding"
2144                ,@(gnus-summary-menu-split
2145                   (mapcar
2146                    (lambda (cs)
2147                      ;; Since easymenu under Emacs doesn't allow
2148                      ;; lambda forms for menu commands, we should
2149                      ;; provide intern'ed function symbols.
2150                      (let ((command (intern (format "\
2151 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2152                        (fset command
2153                              `(lambda ()
2154                                 (interactive)
2155                                 (let ((gnus-summary-show-article-charset-alist
2156                                        '((1 . ,cs))))
2157                                   (gnus-summary-show-article 1))))
2158                        `[,(symbol-name cs) ,command t]))
2159                    (sort (if (fboundp 'coding-system-list)
2160                              (coding-system-list)
2161                            (mapcar 'car mm-mime-mule-charset-alist))
2162                          'string<)))))
2163              ("Washing"
2164               ("Remove Blanks"
2165                ["Leading" gnus-article-strip-leading-blank-lines t]
2166                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2167                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2168                ["All of the above" gnus-article-strip-blank-lines t]
2169                ["All" gnus-article-strip-all-blank-lines t]
2170                ["Leading space" gnus-article-strip-leading-space t]
2171                ["Trailing space" gnus-article-strip-trailing-space t]
2172                ["Leading space in headers"
2173                 gnus-article-remove-leading-whitespace t])
2174               ["Overstrike" gnus-article-treat-overstrike t]
2175               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2176               ["Emphasis" gnus-article-emphasize t]
2177               ["Word wrap" gnus-article-fill-cited-article t]
2178               ["Fill long lines" gnus-article-fill-long-lines t]
2179               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2180               ["Remove CR" gnus-article-remove-cr t]
2181               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2182               ["Base64" gnus-article-de-base64-unreadable t]
2183               ["Rot 13" gnus-summary-caesar-message
2184                ,@(if (featurep 'xemacs) '(t)
2185                    '(:help "\"Caesar rotate\" article by 13"))]
2186               ["Morse decode" gnus-summary-morse-message t]
2187               ["Unix pipe..." gnus-summary-pipe-message t]
2188               ["Add buttons" gnus-article-add-buttons t]
2189               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2190               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2191               ["Verbose header" gnus-summary-verbose-headers t]
2192               ["Toggle header" gnus-summary-toggle-header t]
2193               ["Unfold headers" gnus-article-treat-unfold-headers t]
2194               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2195               ["Html" gnus-article-wash-html t]
2196               ["Unsplit URLs" gnus-article-unsplit-urls t]
2197               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2198               ["Decode HZ" gnus-article-decode-HZ t]
2199               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2200               ("(Outlook) Deuglify"
2201                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2202                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2203                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2204                ["Full (Outlook) deuglify"
2205                 gnus-article-outlook-deuglify-article t])
2206               )
2207              ("Output"
2208               ["Save in default format..." gnus-summary-save-article
2209                ,@(if (featurep 'xemacs) '(t)
2210                    '(:help "Save article using default method"))]
2211               ["Save in file..." gnus-summary-save-article-file
2212                ,@(if (featurep 'xemacs) '(t)
2213                    '(:help "Save article in file"))]
2214               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2215               ["Save in MH folder..." gnus-summary-save-article-folder t]
2216               ["Save in VM folder..." gnus-summary-save-article-vm t]
2217               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2218               ["Save body in file..." gnus-summary-save-article-body-file t]
2219               ["Pipe through a filter..." gnus-summary-pipe-output t]
2220               ["Add to SOUP packet" gnus-soup-add-article t]
2221               ["Print with Muttprint..." gnus-summary-muttprint t]
2222               ["Print" gnus-summary-print-article t])
2223              ("Backend"
2224               ["Respool article..." gnus-summary-respool-article t]
2225               ["Move article..." gnus-summary-move-article
2226                (gnus-check-backend-function
2227                 'request-move-article gnus-newsgroup-name)]
2228               ["Copy article..." gnus-summary-copy-article t]
2229               ["Crosspost article..." gnus-summary-crosspost-article
2230                (gnus-check-backend-function
2231                 'request-replace-article gnus-newsgroup-name)]
2232               ["Import file..." gnus-summary-import-article
2233                (gnus-check-backend-function
2234                 'request-accept-article gnus-newsgroup-name)]
2235               ["Create article..." gnus-summary-create-article
2236                (gnus-check-backend-function
2237                 'request-accept-article gnus-newsgroup-name)]
2238               ["Check if posted" gnus-summary-article-posted-p t]
2239               ["Edit article" gnus-summary-edit-article
2240                (not (gnus-group-read-only-p))]
2241               ["Delete article" gnus-summary-delete-article
2242                (gnus-check-backend-function
2243                 'request-expire-articles gnus-newsgroup-name)]
2244               ["Query respool" gnus-summary-respool-query t]
2245               ["Trace respool" gnus-summary-respool-trace t]
2246               ["Delete expirable articles" gnus-summary-expire-articles-now
2247                (gnus-check-backend-function
2248                 'request-expire-articles gnus-newsgroup-name)])
2249              ("Extract"
2250               ["Uudecode" gnus-uu-decode-uu
2251                ,@(if (featurep 'xemacs) '(t)
2252                    '(:help "Decode uuencoded article(s)"))]
2253               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2254               ["Unshar" gnus-uu-decode-unshar t]
2255               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2256               ["Save" gnus-uu-decode-save t]
2257               ["Binhex" gnus-uu-decode-binhex t]
2258               ["Postscript" gnus-uu-decode-postscript t]
2259               ["All MIME parts" gnus-summary-save-parts t])
2260              ("Cache"
2261               ["Enter article" gnus-cache-enter-article t]
2262               ["Remove article" gnus-cache-remove-article t])
2263              ["Translate" gnus-article-babel t]
2264              ["Select article buffer" gnus-summary-select-article-buffer t]
2265              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2266              ["Isearch article..." gnus-summary-isearch-article t]
2267              ["Beginning of the article" gnus-summary-beginning-of-article t]
2268              ["End of the article" gnus-summary-end-of-article t]
2269              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2270              ["Fetch referenced articles" gnus-summary-refer-references t]
2271              ["Fetch current thread" gnus-summary-refer-thread t]
2272              ["Fetch article with id..." gnus-summary-refer-article t]
2273              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2274              ["Redisplay" gnus-summary-show-article t]
2275              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2276       (easy-menu-define
2277         gnus-summary-article-menu gnus-summary-mode-map ""
2278         (cons "Article" innards))
2279
2280       (if (not (keymapp gnus-summary-article-menu))
2281           (easy-menu-define
2282             gnus-article-commands-menu gnus-article-mode-map ""
2283             (cons "Commands" innards))
2284         ;; in Emacs, don't share menu.
2285         (setq gnus-article-commands-menu
2286               (copy-keymap gnus-summary-article-menu))
2287         (define-key gnus-article-mode-map [menu-bar commands]
2288           (cons "Commands" gnus-article-commands-menu))))
2289
2290     (easy-menu-define
2291       gnus-summary-thread-menu gnus-summary-mode-map ""
2292       '("Threads"
2293         ["Find all messages in thread" gnus-summary-refer-thread t]
2294         ["Toggle threading" gnus-summary-toggle-threads t]
2295         ["Hide threads" gnus-summary-hide-all-threads t]
2296         ["Show threads" gnus-summary-show-all-threads t]
2297         ["Hide thread" gnus-summary-hide-thread t]
2298         ["Show thread" gnus-summary-show-thread t]
2299         ["Go to next thread" gnus-summary-next-thread t]
2300         ["Go to previous thread" gnus-summary-prev-thread t]
2301         ["Go down thread" gnus-summary-down-thread t]
2302         ["Go up thread" gnus-summary-up-thread t]
2303         ["Top of thread" gnus-summary-top-thread t]
2304         ["Mark thread as read" gnus-summary-kill-thread t]
2305         ["Lower thread score" gnus-summary-lower-thread t]
2306         ["Raise thread score" gnus-summary-raise-thread t]
2307         ["Rethread current" gnus-summary-rethread-current t]))
2308
2309     (easy-menu-define
2310       gnus-summary-post-menu gnus-summary-mode-map ""
2311       `("Post"
2312         ["Send a message (mail or news)" gnus-summary-post-news
2313          ,@(if (featurep 'xemacs) '(t)
2314              '(:help "Post an article"))]
2315         ["Followup" gnus-summary-followup
2316          ,@(if (featurep 'xemacs) '(t)
2317              '(:help "Post followup to this article"))]
2318         ["Followup and yank" gnus-summary-followup-with-original
2319          ,@(if (featurep 'xemacs) '(t)
2320              '(:help "Post followup to this article, quoting its contents"))]
2321         ["Supersede article" gnus-summary-supersede-article t]
2322         ["Cancel article" gnus-summary-cancel-article
2323          ,@(if (featurep 'xemacs) '(t)
2324              '(:help "Cancel an article you posted"))]
2325         ["Reply" gnus-summary-reply t]
2326         ["Reply and yank" gnus-summary-reply-with-original t]
2327         ["Wide reply" gnus-summary-wide-reply t]
2328         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2329          ,@(if (featurep 'xemacs) '(t)
2330              '(:help "Mail a reply, quoting this article"))]
2331         ["Very wide reply" gnus-summary-very-wide-reply t]
2332         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2333          ,@(if (featurep 'xemacs) '(t)
2334              '(:help "Mail a very wide reply, quoting this article"))]
2335         ["Mail forward" gnus-summary-mail-forward t]
2336         ["Post forward" gnus-summary-post-forward t]
2337         ["Digest and mail" gnus-uu-digest-mail-forward t]
2338         ["Digest and post" gnus-uu-digest-post-forward t]
2339         ["Resend message" gnus-summary-resend-message t]
2340         ["Resend message edit" gnus-summary-resend-message-edit t]
2341         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2342         ["Send a mail" gnus-summary-mail-other-window t]
2343         ["Create a local message" gnus-summary-news-other-window t]
2344         ["Uuencode and post" gnus-uu-post-news
2345          ,@(if (featurep 'xemacs) '(t)
2346              '(:help "Post a uuencoded article"))]
2347         ["Followup via news" gnus-summary-followup-to-mail t]
2348         ["Followup via news and yank"
2349          gnus-summary-followup-to-mail-with-original t]
2350         ;;("Draft"
2351         ;;["Send" gnus-summary-send-draft t]
2352         ;;["Send bounced" gnus-resend-bounced-mail t])
2353         ))
2354
2355     (cond
2356      ((not (keymapp gnus-summary-post-menu))
2357       (setq gnus-article-post-menu gnus-summary-post-menu))
2358      ((not gnus-article-post-menu)
2359       ;; Don't share post menu.
2360       (setq gnus-article-post-menu
2361             (copy-keymap gnus-summary-post-menu))))
2362     (define-key gnus-article-mode-map [menu-bar post]
2363       (cons "Post" gnus-article-post-menu))
2364
2365     (easy-menu-define
2366       gnus-summary-misc-menu gnus-summary-mode-map ""
2367       `("Gnus"
2368         ("Mark Read"
2369          ["Mark as read" gnus-summary-mark-as-read-forward t]
2370          ["Mark same subject and select"
2371           gnus-summary-kill-same-subject-and-select t]
2372          ["Mark same subject" gnus-summary-kill-same-subject t]
2373          ["Catchup" gnus-summary-catchup
2374           ,@(if (featurep 'xemacs) '(t)
2375               '(:help "Mark unread articles in this group as read"))]
2376          ["Catchup all" gnus-summary-catchup-all t]
2377          ["Catchup to here" gnus-summary-catchup-to-here t]
2378          ["Catchup from here" gnus-summary-catchup-from-here t]
2379          ["Catchup region" gnus-summary-mark-region-as-read
2380           (gnus-mark-active-p)]
2381          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2382         ("Mark Various"
2383          ["Tick" gnus-summary-tick-article-forward t]
2384          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2385          ["Remove marks" gnus-summary-clear-mark-forward t]
2386          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2387          ["Set bookmark" gnus-summary-set-bookmark t]
2388          ["Remove bookmark" gnus-summary-remove-bookmark t])
2389         ("Limit to"
2390          ["Marks..." gnus-summary-limit-to-marks t]
2391          ["Subject..." gnus-summary-limit-to-subject t]
2392          ["Author..." gnus-summary-limit-to-author t]
2393          ["Age..." gnus-summary-limit-to-age t]
2394          ["Extra..." gnus-summary-limit-to-extra t]
2395          ["Score..." gnus-summary-limit-to-score t]
2396          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2397          ["Unread" gnus-summary-limit-to-unread t]
2398          ["Unseen" gnus-summary-limit-to-unseen t]
2399          ["Replied" gnus-summary-limit-to-replied t]
2400          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2401          ["Next articles" gnus-summary-limit-to-articles t]
2402          ["Pop limit" gnus-summary-pop-limit t]
2403          ["Show dormant" gnus-summary-limit-include-dormant t]
2404          ["Hide childless dormant"
2405           gnus-summary-limit-exclude-childless-dormant t]
2406          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2407          ["Hide marked" gnus-summary-limit-exclude-marks t]
2408          ["Show expunged" gnus-summary-limit-include-expunged t])
2409         ("Process Mark"
2410          ["Set mark" gnus-summary-mark-as-processable t]
2411          ["Remove mark" gnus-summary-unmark-as-processable t]
2412          ["Remove all marks" gnus-summary-unmark-all-processable t]
2413          ["Mark above" gnus-uu-mark-over t]
2414          ["Mark series" gnus-uu-mark-series t]
2415          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2416          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2417          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2418          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2419          ["Mark all" gnus-uu-mark-all t]
2420          ["Mark buffer" gnus-uu-mark-buffer t]
2421          ["Mark sparse" gnus-uu-mark-sparse t]
2422          ["Mark thread" gnus-uu-mark-thread t]
2423          ["Unmark thread" gnus-uu-unmark-thread t]
2424          ("Process Mark Sets"
2425           ["Kill" gnus-summary-kill-process-mark t]
2426           ["Yank" gnus-summary-yank-process-mark
2427            gnus-newsgroup-process-stack]
2428           ["Save" gnus-summary-save-process-mark t]
2429           ["Run command on marked..." gnus-summary-universal-argument t]))
2430         ("Scroll article"
2431          ["Page forward" gnus-summary-next-page
2432           ,@(if (featurep 'xemacs) '(t)
2433               '(:help "Show next page of article"))]
2434          ["Page backward" gnus-summary-prev-page
2435           ,@(if (featurep 'xemacs) '(t)
2436               '(:help "Show previous page of article"))]
2437          ["Line forward" gnus-summary-scroll-up t])
2438         ("Move"
2439          ["Next unread article" gnus-summary-next-unread-article t]
2440          ["Previous unread article" gnus-summary-prev-unread-article t]
2441          ["Next article" gnus-summary-next-article t]
2442          ["Previous article" gnus-summary-prev-article t]
2443          ["Next unread subject" gnus-summary-next-unread-subject t]
2444          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2445          ["Next article same subject" gnus-summary-next-same-subject t]
2446          ["Previous article same subject" gnus-summary-prev-same-subject t]
2447          ["First unread article" gnus-summary-first-unread-article t]
2448          ["Best unread article" gnus-summary-best-unread-article t]
2449          ["Go to subject number..." gnus-summary-goto-subject t]
2450          ["Go to article number..." gnus-summary-goto-article t]
2451          ["Go to the last article" gnus-summary-goto-last-article t]
2452          ["Pop article off history" gnus-summary-pop-article t])
2453         ("Sort"
2454          ["Sort by number" gnus-summary-sort-by-number t]
2455          ["Sort by author" gnus-summary-sort-by-author t]
2456          ["Sort by subject" gnus-summary-sort-by-subject t]
2457          ["Sort by date" gnus-summary-sort-by-date t]
2458          ["Sort by score" gnus-summary-sort-by-score t]
2459          ["Sort by lines" gnus-summary-sort-by-lines t]
2460          ["Sort by characters" gnus-summary-sort-by-chars t]
2461          ["Randomize" gnus-summary-sort-by-random t]
2462          ["Original sort" gnus-summary-sort-by-original t])
2463         ("Help"
2464          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2465          ["Describe group" gnus-summary-describe-group t]
2466          ["Fetch charter" gnus-group-fetch-charter
2467           ,@(if (featurep 'xemacs) nil
2468               '(:help "Display the charter of the current group"))]
2469          ["Fetch control message" gnus-group-fetch-control
2470           ,@(if (featurep 'xemacs) nil
2471               '(:help "Display the archived control message for the current group"))]
2472          ["Read manual" gnus-info-find-node t])
2473         ("Modes"
2474          ["Pick and read" gnus-pick-mode t]
2475          ["Binary" gnus-binary-mode t])
2476         ("Regeneration"
2477          ["Regenerate" gnus-summary-prepare t]
2478          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2479          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2480          ["Toggle threading" gnus-summary-toggle-threads t])
2481         ["See old articles" gnus-summary-insert-old-articles t]
2482         ["See new articles" gnus-summary-insert-new-articles t]
2483         ["Filter articles..." gnus-summary-execute-command t]
2484         ["Run command on articles..." gnus-summary-universal-argument t]
2485         ["Search articles forward..." gnus-summary-search-article-forward t]
2486         ["Search articles backward..." gnus-summary-search-article-backward t]
2487         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2488         ["Expand window" gnus-summary-expand-window t]
2489         ["Expire expirable articles" gnus-summary-expire-articles
2490          (gnus-check-backend-function
2491           'request-expire-articles gnus-newsgroup-name)]
2492         ["Edit local kill file" gnus-summary-edit-local-kill t]
2493         ["Edit main kill file" gnus-summary-edit-global-kill t]
2494         ["Edit group parameters" gnus-summary-edit-parameters t]
2495         ["Customize group parameters" gnus-summary-customize-parameters t]
2496         ["Send a bug report" gnus-bug t]
2497         ("Exit"
2498          ["Catchup and exit" gnus-summary-catchup-and-exit
2499           ,@(if (featurep 'xemacs) '(t)
2500               '(:help "Mark unread articles in this group as read, then exit"))]
2501          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2502          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2503          ["Exit group" gnus-summary-exit
2504           ,@(if (featurep 'xemacs) '(t)
2505               '(:help "Exit current group, return to group selection mode"))]
2506          ["Exit group without updating" gnus-summary-exit-no-update t]
2507          ["Exit and goto next group" gnus-summary-next-group t]
2508          ["Exit and goto prev group" gnus-summary-prev-group t]
2509          ["Reselect group" gnus-summary-reselect-current-group t]
2510          ["Rescan group" gnus-summary-rescan-group t]
2511          ["Update dribble" gnus-summary-save-newsrc t])))
2512
2513     (gnus-run-hooks 'gnus-summary-menu-hook)))
2514
2515 (defvar gnus-summary-tool-bar-map nil)
2516
2517 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2518 (defun gnus-summary-make-tool-bar ()
2519   (if (and (fboundp 'tool-bar-add-item-from-menu)
2520            (default-value 'tool-bar-mode)
2521            (not gnus-summary-tool-bar-map))
2522       (setq gnus-summary-tool-bar-map
2523             (let ((tool-bar-map (make-sparse-keymap))
2524                   (load-path (mm-image-load-path)))
2525               (tool-bar-add-item-from-menu
2526                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2527               (tool-bar-add-item-from-menu
2528                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2529               (tool-bar-add-item-from-menu
2530                'gnus-summary-post-news "post" gnus-summary-mode-map)
2531               (tool-bar-add-item-from-menu
2532                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2533               (tool-bar-add-item-from-menu
2534                'gnus-summary-followup "followup" gnus-summary-mode-map)
2535               (tool-bar-add-item-from-menu
2536                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2537               (tool-bar-add-item-from-menu
2538                'gnus-summary-reply "reply" gnus-summary-mode-map)
2539               (tool-bar-add-item-from-menu
2540                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2541               (tool-bar-add-item-from-menu
2542                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2543               (tool-bar-add-item-from-menu
2544                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2545               (tool-bar-add-item-from-menu
2546                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2547               (tool-bar-add-item-from-menu
2548                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2549               (tool-bar-add-item-from-menu
2550                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2551               (tool-bar-add-item-from-menu
2552                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2553               (tool-bar-add-item-from-menu
2554                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2555               tool-bar-map)))
2556   (if gnus-summary-tool-bar-map
2557       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2558
2559 (defun gnus-score-set-default (var value)
2560   "A version of set that updates the GNU Emacs menu-bar."
2561   (set var value)
2562   ;; It is the message that forces the active status to be updated.
2563   (message ""))
2564
2565 (defun gnus-make-score-map (type)
2566   "Make a summary score map of type TYPE."
2567   (if t
2568       nil
2569     (let ((headers '(("author" "from" string)
2570                      ("subject" "subject" string)
2571                      ("article body" "body" string)
2572                      ("article head" "head" string)
2573                      ("xref" "xref" string)
2574                      ("extra header" "extra" string)
2575                      ("lines" "lines" number)
2576                      ("followups to author" "followup" string)))
2577           (types '((number ("less than" <)
2578                            ("greater than" >)
2579                            ("equal" =))
2580                    (string ("substring" s)
2581                            ("exact string" e)
2582                            ("fuzzy string" f)
2583                            ("regexp" r))))
2584           (perms '(("temporary" (current-time-string))
2585                    ("permanent" nil)
2586                    ("immediate" now)))
2587           header)
2588       (list
2589        (apply
2590         'nconc
2591         (list
2592          (if (eq type 'lower)
2593              "Lower score"
2594            "Increase score"))
2595         (let (outh)
2596           (while headers
2597             (setq header (car headers))
2598             (setq outh
2599                   (cons
2600                    (apply
2601                     'nconc
2602                     (list (car header))
2603                     (let ((ts (cdr (assoc (nth 2 header) types)))
2604                           outt)
2605                       (while ts
2606                         (setq outt
2607                               (cons
2608                                (apply
2609                                 'nconc
2610                                 (list (caar ts))
2611                                 (let ((ps perms)
2612                                       outp)
2613                                   (while ps
2614                                     (setq outp
2615                                           (cons
2616                                            (vector
2617                                             (caar ps)
2618                                             (list
2619                                              'gnus-summary-score-entry
2620                                              (nth 1 header)
2621                                              (if (or (string= (nth 1 header)
2622                                                               "head")
2623                                                      (string= (nth 1 header)
2624                                                               "body"))
2625                                                  ""
2626                                                (list 'gnus-summary-header
2627                                                      (nth 1 header)))
2628                                              (list 'quote (nth 1 (car ts)))
2629                                              (list 'gnus-score-delta-default
2630                                                    nil)
2631                                              (nth 1 (car ps))
2632                                              t)
2633                                             t)
2634                                            outp))
2635                                     (setq ps (cdr ps)))
2636                                   (list (nreverse outp))))
2637                                outt))
2638                         (setq ts (cdr ts)))
2639                       (list (nreverse outt))))
2640                    outh))
2641             (setq headers (cdr headers)))
2642           (list (nreverse outh))))))))
2643
2644 \f
2645
2646 (defun gnus-summary-mode (&optional group)
2647   "Major mode for reading articles.
2648
2649 All normal editing commands are switched off.
2650 \\<gnus-summary-mode-map>
2651 Each line in this buffer represents one article.  To read an
2652 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2653 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2654 respectively.
2655
2656 You can also post articles and send mail from this buffer.  To
2657 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2658 of an article, type `\\[gnus-summary-reply]'.
2659
2660 There are approx. one gazillion commands you can execute in this
2661 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2662
2663 The following commands are available:
2664
2665 \\{gnus-summary-mode-map}"
2666   (interactive)
2667   (kill-all-local-variables)
2668   (when (gnus-visual-p 'summary-menu 'menu)
2669     (gnus-summary-make-menu-bar)
2670     (gnus-summary-make-tool-bar))
2671   (gnus-summary-make-local-variables)
2672   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2673     (gnus-summary-make-local-variables))
2674   (gnus-make-thread-indent-array)
2675   (gnus-simplify-mode-line)
2676   (setq major-mode 'gnus-summary-mode)
2677   (setq mode-name "Summary")
2678   (make-local-variable 'minor-mode-alist)
2679   (use-local-map gnus-summary-mode-map)
2680   (buffer-disable-undo)
2681   (setq buffer-read-only t)             ;Disable modification
2682   (setq truncate-lines t)
2683   (setq selective-display t)
2684   (setq selective-display-ellipses t)   ;Display `...'
2685   (gnus-summary-set-display-table)
2686   (gnus-set-default-directory)
2687   (setq gnus-newsgroup-name group)
2688   (make-local-variable 'gnus-summary-line-format)
2689   (make-local-variable 'gnus-summary-line-format-spec)
2690   (make-local-variable 'gnus-summary-dummy-line-format)
2691   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2692   (make-local-variable 'gnus-summary-mark-positions)
2693   (gnus-make-local-hook 'pre-command-hook)
2694   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2695   (gnus-run-hooks 'gnus-summary-mode-hook)
2696   (turn-on-gnus-mailing-list-mode)
2697   (mm-enable-multibyte)
2698   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2699   (gnus-update-summary-mark-positions))
2700
2701 (defun gnus-summary-make-local-variables ()
2702   "Make all the local summary buffer variables."
2703   (let (global)
2704     (dolist (local gnus-summary-local-variables)
2705       (if (consp local)
2706           (progn
2707             (if (eq (cdr local) 'global)
2708                 ;; Copy the global value of the variable.
2709                 (setq global (symbol-value (car local)))
2710               ;; Use the value from the list.
2711               (setq global (eval (cdr local))))
2712             (set (make-local-variable (car local)) global))
2713         ;; Simple nil-valued local variable.
2714         (set (make-local-variable local) nil)))))
2715
2716 (defun gnus-summary-clear-local-variables ()
2717   (let ((locals gnus-summary-local-variables))
2718     (while locals
2719       (if (consp (car locals))
2720           (and (vectorp (caar locals))
2721                (set (caar locals) nil))
2722         (and (vectorp (car locals))
2723              (set (car locals) nil)))
2724       (setq locals (cdr locals)))))
2725
2726 ;; Summary data functions.
2727
2728 (defmacro gnus-data-number (data)
2729   `(car ,data))
2730
2731 (defmacro gnus-data-set-number (data number)
2732   `(setcar ,data ,number))
2733
2734 (defmacro gnus-data-mark (data)
2735   `(nth 1 ,data))
2736
2737 (defmacro gnus-data-set-mark (data mark)
2738   `(setcar (nthcdr 1 ,data) ,mark))
2739
2740 (defmacro gnus-data-pos (data)
2741   `(nth 2 ,data))
2742
2743 (defmacro gnus-data-set-pos (data pos)
2744   `(setcar (nthcdr 2 ,data) ,pos))
2745
2746 (defmacro gnus-data-header (data)
2747   `(nth 3 ,data))
2748
2749 (defmacro gnus-data-set-header (data header)
2750   `(setf (nth 3 ,data) ,header))
2751
2752 (defmacro gnus-data-level (data)
2753   `(nth 4 ,data))
2754
2755 (defmacro gnus-data-unread-p (data)
2756   `(= (nth 1 ,data) gnus-unread-mark))
2757
2758 (defmacro gnus-data-read-p (data)
2759   `(/= (nth 1 ,data) gnus-unread-mark))
2760
2761 (defmacro gnus-data-pseudo-p (data)
2762   `(consp (nth 3 ,data)))
2763
2764 (defmacro gnus-data-find (number)
2765   `(assq ,number gnus-newsgroup-data))
2766
2767 (defmacro gnus-data-find-list (number &optional data)
2768   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2769      (memq (assq ,number bdata)
2770            bdata)))
2771
2772 (defmacro gnus-data-make (number mark pos header level)
2773   `(list ,number ,mark ,pos ,header ,level))
2774
2775 (defun gnus-data-enter (after-article number mark pos header level offset)
2776   (let ((data (gnus-data-find-list after-article)))
2777     (unless data
2778       (error "No such article: %d" after-article))
2779     (setcdr data (cons (gnus-data-make number mark pos header level)
2780                        (cdr data)))
2781     (setq gnus-newsgroup-data-reverse nil)
2782     (gnus-data-update-list (cddr data) offset)))
2783
2784 (defun gnus-data-enter-list (after-article list &optional offset)
2785   (when list
2786     (let ((data (and after-article (gnus-data-find-list after-article)))
2787           (ilist list))
2788       (if (not (or data
2789                    after-article))
2790           (let ((odata gnus-newsgroup-data))
2791             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2792             (when offset
2793               (gnus-data-update-list odata offset)))
2794       ;; Find the last element in the list to be spliced into the main
2795         ;; list.
2796         (while (cdr list)
2797           (setq list (cdr list)))
2798         (if (not data)
2799             (progn
2800               (setcdr list gnus-newsgroup-data)
2801               (setq gnus-newsgroup-data ilist)
2802               (when offset
2803                 (gnus-data-update-list (cdr list) offset)))
2804           (setcdr list (cdr data))
2805           (setcdr data ilist)
2806           (when offset
2807             (gnus-data-update-list (cdr list) offset))))
2808       (setq gnus-newsgroup-data-reverse nil))))
2809
2810 (defun gnus-data-remove (article &optional offset)
2811   (let ((data gnus-newsgroup-data))
2812     (if (= (gnus-data-number (car data)) article)
2813         (progn
2814           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2815                 gnus-newsgroup-data-reverse nil)
2816           (when offset
2817             (gnus-data-update-list gnus-newsgroup-data offset)))
2818       (while (cdr data)
2819         (when (= (gnus-data-number (cadr data)) article)
2820           (setcdr data (cddr data))
2821           (when offset
2822             (gnus-data-update-list (cdr data) offset))
2823           (setq data nil
2824                 gnus-newsgroup-data-reverse nil))
2825         (setq data (cdr data))))))
2826
2827 (defmacro gnus-data-list (backward)
2828   `(if ,backward
2829        (or gnus-newsgroup-data-reverse
2830            (setq gnus-newsgroup-data-reverse
2831                  (reverse gnus-newsgroup-data)))
2832      gnus-newsgroup-data))
2833
2834 (defun gnus-data-update-list (data offset)
2835   "Add OFFSET to the POS of all data entries in DATA."
2836   (setq gnus-newsgroup-data-reverse nil)
2837   (while data
2838     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2839     (setq data (cdr data))))
2840
2841 (defun gnus-summary-article-pseudo-p (article)
2842   "Say whether this article is a pseudo article or not."
2843   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2844
2845 (defmacro gnus-summary-article-sparse-p (article)
2846   "Say whether this article is a sparse article or not."
2847   `(memq ,article gnus-newsgroup-sparse))
2848
2849 (defmacro gnus-summary-article-ancient-p (article)
2850   "Say whether this article is a sparse article or not."
2851   `(memq ,article gnus-newsgroup-ancient))
2852
2853 (defun gnus-article-parent-p (number)
2854   "Say whether this article is a parent or not."
2855   (let ((data (gnus-data-find-list number)))
2856     (and (cdr data)              ; There has to be an article after...
2857          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2858             (gnus-data-level (nth 1 data))))))
2859
2860 (defun gnus-article-children (number)
2861   "Return a list of all children to NUMBER."
2862   (let* ((data (gnus-data-find-list number))
2863          (level (gnus-data-level (car data)))
2864          children)
2865     (setq data (cdr data))
2866     (while (and data
2867                 (= (gnus-data-level (car data)) (1+ level)))
2868       (push (gnus-data-number (car data)) children)
2869       (setq data (cdr data)))
2870     children))
2871
2872 (defmacro gnus-summary-skip-intangible ()
2873   "If the current article is intangible, then jump to a different article."
2874   '(let ((to (get-text-property (point) 'gnus-intangible)))
2875      (and to (gnus-summary-goto-subject to))))
2876
2877 (defmacro gnus-summary-article-intangible-p ()
2878   "Say whether this article is intangible or not."
2879   '(get-text-property (point) 'gnus-intangible))
2880
2881 (defun gnus-article-read-p (article)
2882   "Say whether ARTICLE is read or not."
2883   (not (or (memq article gnus-newsgroup-marked)
2884            (memq article gnus-newsgroup-spam-marked)
2885            (memq article gnus-newsgroup-unreads)
2886            (memq article gnus-newsgroup-unselected)
2887            (memq article gnus-newsgroup-dormant))))
2888
2889 ;; Some summary mode macros.
2890
2891 (defmacro gnus-summary-article-number ()
2892   "The article number of the article on the current line.
2893 If there isn't an article number here, then we return the current
2894 article number."
2895   '(progn
2896      (gnus-summary-skip-intangible)
2897      (or (get-text-property (point) 'gnus-number)
2898          (gnus-summary-last-subject))))
2899
2900 (defmacro gnus-summary-article-header (&optional number)
2901   "Return the header of article NUMBER."
2902   `(gnus-data-header (gnus-data-find
2903                       ,(or number '(gnus-summary-article-number)))))
2904
2905 (defmacro gnus-summary-thread-level (&optional number)
2906   "Return the level of thread that starts with article NUMBER."
2907   `(if (and (eq gnus-summary-make-false-root 'dummy)
2908             (get-text-property (point) 'gnus-intangible))
2909        0
2910      (gnus-data-level (gnus-data-find
2911                        ,(or number '(gnus-summary-article-number))))))
2912
2913 (defmacro gnus-summary-article-mark (&optional number)
2914   "Return the mark of article NUMBER."
2915   `(gnus-data-mark (gnus-data-find
2916                     ,(or number '(gnus-summary-article-number)))))
2917
2918 (defmacro gnus-summary-article-pos (&optional number)
2919   "Return the position of the line of article NUMBER."
2920   `(gnus-data-pos (gnus-data-find
2921                    ,(or number '(gnus-summary-article-number)))))
2922
2923 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2924 (defmacro gnus-summary-article-subject (&optional number)
2925   "Return current subject string or nil if nothing."
2926   `(let ((headers
2927           ,(if number
2928                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2929              '(gnus-data-header (assq (gnus-summary-article-number)
2930                                       gnus-newsgroup-data)))))
2931      (and headers
2932           (vectorp headers)
2933           (mail-header-subject headers))))
2934
2935 (defmacro gnus-summary-article-score (&optional number)
2936   "Return current article score."
2937   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2938                   gnus-newsgroup-scored))
2939        gnus-summary-default-score 0))
2940
2941 (defun gnus-summary-article-children (&optional number)
2942   "Return a list of article numbers that are children of article NUMBER."
2943   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2944          (level (gnus-data-level (car data)))
2945          l children)
2946     (while (and (setq data (cdr data))
2947                 (> (setq l (gnus-data-level (car data))) level))
2948       (and (= (1+ level) l)
2949            (push (gnus-data-number (car data))
2950                  children)))
2951     (nreverse children)))
2952
2953 (defun gnus-summary-article-parent (&optional number)
2954   "Return the article number of the parent of article NUMBER."
2955   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2956                                     (gnus-data-list t)))
2957          (level (gnus-data-level (car data))))
2958     (if (zerop level)
2959         ()                              ; This is a root.
2960       ;; We search until we find an article with a level less than
2961       ;; this one.  That function has to be the parent.
2962       (while (and (setq data (cdr data))
2963                   (not (< (gnus-data-level (car data)) level))))
2964       (and data (gnus-data-number (car data))))))
2965
2966 (defun gnus-unread-mark-p (mark)
2967   "Say whether MARK is the unread mark."
2968   (= mark gnus-unread-mark))
2969
2970 (defun gnus-read-mark-p (mark)
2971   "Say whether MARK is one of the marks that mark as read.
2972 This is all marks except unread, ticked, dormant, and expirable."
2973   (not (or (= mark gnus-unread-mark)
2974            (= mark gnus-ticked-mark)
2975            (= mark gnus-spam-mark)
2976            (= mark gnus-dormant-mark)
2977            (= mark gnus-expirable-mark))))
2978
2979 (defmacro gnus-article-mark (number)
2980   "Return the MARK of article NUMBER.
2981 This macro should only be used when computing the mark the \"first\"
2982 time; i.e., when generating the summary lines.  After that,
2983 `gnus-summary-article-mark' should be used to examine the
2984 marks of articles."
2985   `(cond
2986     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2987     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2988     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2989     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2990     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2991     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2992     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2993     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2994            gnus-ancient-mark))))
2995
2996 ;; Saving hidden threads.
2997
2998 (defmacro gnus-save-hidden-threads (&rest forms)
2999   "Save hidden threads, eval FORMS, and restore the hidden threads."
3000   (let ((config (make-symbol "config")))
3001     `(let ((,config (gnus-hidden-threads-configuration)))
3002        (unwind-protect
3003            (save-excursion
3004              ,@forms)
3005          (gnus-restore-hidden-threads-configuration ,config)))))
3006 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3007 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3008
3009 (defun gnus-data-compute-positions ()
3010   "Compute the positions of all articles."
3011   (setq gnus-newsgroup-data-reverse nil)
3012   (let ((data gnus-newsgroup-data))
3013     (save-excursion
3014       (gnus-save-hidden-threads
3015         (gnus-summary-show-all-threads)
3016         (goto-char (point-min))
3017         (while data
3018           (while (get-text-property (point) 'gnus-intangible)
3019             (forward-line 1))
3020           (gnus-data-set-pos (car data) (+ (point) 3))
3021           (setq data (cdr data))
3022           (forward-line 1))))))
3023
3024 (defun gnus-hidden-threads-configuration ()
3025   "Return the current hidden threads configuration."
3026   (save-excursion
3027     (let (config)
3028       (goto-char (point-min))
3029       (while (search-forward "\r" nil t)
3030         (push (1- (point)) config))
3031       config)))
3032
3033 (defun gnus-restore-hidden-threads-configuration (config)
3034   "Restore hidden threads configuration from CONFIG."
3035   (save-excursion
3036     (let (point buffer-read-only)
3037       (while (setq point (pop config))
3038         (when (and (< point (point-max))
3039                    (goto-char point)
3040                    (eq (char-after) ?\n))
3041           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3042
3043 ;; Various summary mode internalish functions.
3044
3045 (defun gnus-mouse-pick-article (e)
3046   (interactive "e")
3047   (mouse-set-point e)
3048   (gnus-summary-next-page nil t))
3049
3050 (defun gnus-summary-set-display-table ()
3051   "Change the display table.
3052 Odd characters have a tendency to mess
3053 up nicely formatted displays - we make all possible glyphs
3054 display only a single character."
3055
3056   ;; We start from the standard display table, if any.
3057   (let ((table (or (copy-sequence standard-display-table)
3058                    (make-display-table)))
3059         (i 32))
3060     ;; Nix out all the control chars...
3061     (while (>= (setq i (1- i)) 0)
3062       (aset table i [??]))
3063    ;; ... but not newline and cr, of course.  (cr is necessary for the
3064     ;; selective display).
3065     (aset table ?\n nil)
3066     (aset table ?\r nil)
3067     ;; We keep TAB as well.
3068     (aset table ?\t nil)
3069     ;; We nix out any glyphs over 126 that are not set already.
3070     (let ((i 256))
3071       (while (>= (setq i (1- i)) 127)
3072         ;; Only modify if the entry is nil.
3073         (unless (aref table i)
3074           (aset table i [??]))))
3075     (setq buffer-display-table table)))
3076
3077 (defun gnus-summary-set-article-display-arrow (pos)
3078   "Update the overlay arrow to point to line at position POS."
3079   (when (and gnus-summary-display-arrow
3080              (boundp 'overlay-arrow-position)
3081              (boundp 'overlay-arrow-string))
3082     (save-excursion
3083       (goto-char pos)
3084       (beginning-of-line)
3085       (unless overlay-arrow-position
3086         (setq overlay-arrow-position (make-marker)))
3087       (setq overlay-arrow-string "=>"
3088             overlay-arrow-position (set-marker overlay-arrow-position
3089                                                (point)
3090                                                (current-buffer))))))
3091
3092 (defun gnus-summary-setup-buffer (group)
3093   "Initialize summary buffer."
3094   (let ((buffer (gnus-summary-buffer-name group))
3095         (dead-name (concat "*Dead Summary "
3096                            (gnus-group-decoded-name group) "*")))
3097     ;; If a dead summary buffer exists, we kill it.
3098     (when (gnus-buffer-live-p dead-name)
3099       (gnus-kill-buffer dead-name))
3100     (if (get-buffer buffer)
3101         (progn
3102           (set-buffer buffer)
3103           (setq gnus-summary-buffer (current-buffer))
3104           (not gnus-newsgroup-prepared))
3105       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3106       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3107       (gnus-summary-mode group)
3108       (when gnus-carpal
3109         (gnus-carpal-setup-buffer 'summary))
3110       (unless gnus-single-article-buffer
3111         (make-local-variable 'gnus-article-buffer)
3112         (make-local-variable 'gnus-article-current)
3113         (make-local-variable 'gnus-original-article-buffer))
3114       (setq gnus-newsgroup-name group)
3115       ;; Set any local variables in the group parameters.
3116       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3117       t)))
3118
3119 (defun gnus-set-global-variables ()
3120   "Set the global equivalents of the buffer-local variables.
3121 They are set to the latest values they had.  These reflect the summary
3122 buffer that was in action when the last article was fetched."
3123   (when (eq major-mode 'gnus-summary-mode)
3124     (setq gnus-summary-buffer (current-buffer))
3125     (let ((name gnus-newsgroup-name)
3126           (marked gnus-newsgroup-marked)
3127           (spam gnus-newsgroup-spam-marked)
3128           (unread gnus-newsgroup-unreads)
3129           (headers gnus-current-headers)
3130           (data gnus-newsgroup-data)
3131           (summary gnus-summary-buffer)
3132           (article-buffer gnus-article-buffer)
3133           (original gnus-original-article-buffer)
3134           (gac gnus-article-current)
3135           (reffed gnus-reffed-article-number)
3136           (score-file gnus-current-score-file)
3137           (default-charset gnus-newsgroup-charset)
3138           vlist)
3139       (let ((locals gnus-newsgroup-variables))
3140         (while locals
3141           (if (consp (car locals))
3142               (push (eval (caar locals)) vlist)
3143             (push (eval (car locals)) vlist))
3144           (setq locals (cdr locals)))
3145         (setq vlist (nreverse vlist)))
3146       (save-excursion
3147         (set-buffer gnus-group-buffer)
3148         (setq gnus-newsgroup-name name
3149               gnus-newsgroup-marked marked
3150               gnus-newsgroup-spam-marked spam
3151               gnus-newsgroup-unreads unread
3152               gnus-current-headers headers
3153               gnus-newsgroup-data data
3154               gnus-article-current gac
3155               gnus-summary-buffer summary
3156               gnus-article-buffer article-buffer
3157               gnus-original-article-buffer original
3158               gnus-reffed-article-number reffed
3159               gnus-current-score-file score-file
3160               gnus-newsgroup-charset default-charset)
3161         (let ((locals gnus-newsgroup-variables))
3162           (while locals
3163             (if (consp (car locals))
3164                 (set (caar locals) (pop vlist))
3165               (set (car locals) (pop vlist)))
3166             (setq locals (cdr locals))))
3167         ;; The article buffer also has local variables.
3168         (when (gnus-buffer-live-p gnus-article-buffer)
3169           (set-buffer gnus-article-buffer)
3170           (setq gnus-summary-buffer summary))))))
3171
3172 (defun gnus-summary-article-unread-p (article)
3173   "Say whether ARTICLE is unread or not."
3174   (memq article gnus-newsgroup-unreads))
3175
3176 (defun gnus-summary-first-article-p (&optional article)
3177   "Return whether ARTICLE is the first article in the buffer."
3178   (if (not (setq article (or article (gnus-summary-article-number))))
3179       nil
3180     (eq article (caar gnus-newsgroup-data))))
3181
3182 (defun gnus-summary-last-article-p (&optional article)
3183   "Return whether ARTICLE is the last article in the buffer."
3184   (if (not (setq article (or article (gnus-summary-article-number))))
3185       ;; All non-existent numbers are the last article.  :-)
3186       t
3187     (not (cdr (gnus-data-find-list article)))))
3188
3189 (defun gnus-make-thread-indent-array ()
3190   (let ((n 200))
3191     (unless (and gnus-thread-indent-array
3192                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3193       (setq gnus-thread-indent-array (make-vector 201 "")
3194             gnus-thread-indent-array-level gnus-thread-indent-level)
3195       (while (>= n 0)
3196         (aset gnus-thread-indent-array n
3197               (make-string (* n gnus-thread-indent-level) ? ))
3198         (setq n (1- n))))))
3199
3200 (defun gnus-update-summary-mark-positions ()
3201   "Compute where the summary marks are to go."
3202   (save-excursion
3203     (when (gnus-buffer-exists-p gnus-summary-buffer)
3204       (set-buffer gnus-summary-buffer))
3205     (let ((gnus-replied-mark 129)
3206           (gnus-score-below-mark 130)
3207           (gnus-score-over-mark 130)
3208           (gnus-undownloaded-mark 131)
3209           (spec gnus-summary-line-format-spec)
3210           gnus-visual pos)
3211       (save-excursion
3212         (gnus-set-work-buffer)
3213         (let ((gnus-summary-line-format-spec spec)
3214               (gnus-newsgroup-downloadable '(0)))
3215           (gnus-summary-insert-line
3216            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3217            0 nil t 128 t nil "" nil 1)
3218           (goto-char (point-min))
3219           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3220                                              (- (point) (point-min) 1)))))
3221           (goto-char (point-min))
3222           (push (cons 'replied (and (search-forward "\201" nil t)
3223                                     (- (point) (point-min) 1)))
3224                 pos)
3225           (goto-char (point-min))
3226           (push (cons 'score (and (search-forward "\202" nil t)
3227                                   (- (point) (point-min) 1)))
3228                 pos)
3229           (goto-char (point-min))
3230           (push (cons 'download
3231                       (and (search-forward "\203" nil t)
3232                            (- (point) (point-min) 1)))
3233                 pos)))
3234       (setq gnus-summary-mark-positions pos))))
3235
3236 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3237   "Insert a dummy root in the summary buffer."
3238   (beginning-of-line)
3239   (gnus-add-text-properties
3240    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3241    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3242
3243 (defun gnus-summary-extract-address-component (from)
3244   (or (car (funcall gnus-extract-address-components from))
3245       from))
3246
3247 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3248   (let ((mail-parse-charset gnus-newsgroup-charset)
3249         ; Is it really necessary to do this next part for each summary line?
3250         ; Luckily, doesn't seem to slow things down much.
3251         (mail-parse-ignored-charsets
3252          (with-current-buffer gnus-summary-buffer
3253            gnus-newsgroup-ignored-charsets)))
3254     (or
3255      (and gnus-ignored-from-addresses
3256           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3257           (let ((extra-headers (mail-header-extra header))
3258                 to
3259                 newsgroups)
3260             (cond
3261              ((setq to (cdr (assq 'To extra-headers)))
3262               (concat "-> "
3263                       (inline
3264                         (gnus-summary-extract-address-component
3265                          (funcall gnus-decode-encoded-word-function to)))))
3266              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3267               (concat "=> " newsgroups)))))
3268      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3269
3270 (defun gnus-summary-insert-line (gnus-tmp-header
3271                                  gnus-tmp-level gnus-tmp-current
3272                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3273                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3274                                  &optional gnus-tmp-dummy gnus-tmp-score
3275                                  gnus-tmp-process)
3276   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3277          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3278          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3279          (gnus-tmp-score-char
3280           (if (or (null gnus-summary-default-score)
3281                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3282                       gnus-summary-zcore-fuzz))
3283               ?                         ;Whitespace
3284             (if (< gnus-tmp-score gnus-summary-default-score)
3285                 gnus-score-below-mark gnus-score-over-mark)))
3286          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3287          (gnus-tmp-replied
3288           (cond (gnus-tmp-process gnus-process-mark)
3289                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3290                  gnus-cached-mark)
3291                 (gnus-tmp-replied gnus-replied-mark)
3292                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3293                  gnus-forwarded-mark)
3294                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3295                  gnus-saved-mark)
3296                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3297                  gnus-recent-mark)
3298                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3299                  gnus-unseen-mark)
3300                 (t gnus-no-mark)))
3301          (gnus-tmp-downloaded
3302           (cond (undownloaded
3303                  gnus-undownloaded-mark)
3304                 (gnus-newsgroup-agentized
3305                  gnus-downloaded-mark)
3306                 (t
3307                  gnus-no-mark)))
3308          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3309          (gnus-tmp-name
3310           (cond
3311            ((string-match "<[^>]+> *$" gnus-tmp-from)
3312             (let ((beg (match-beginning 0)))
3313               (or (and (string-match "^\".+\"" gnus-tmp-from)
3314                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3315                   (substring gnus-tmp-from 0 beg))))
3316            ((string-match "(.+)" gnus-tmp-from)
3317             (substring gnus-tmp-from
3318                        (1+ (match-beginning 0)) (1- (match-end 0))))
3319            (t gnus-tmp-from)))
3320          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3321          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3322          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3323          (buffer-read-only nil))
3324     (when (string= gnus-tmp-name "")
3325       (setq gnus-tmp-name gnus-tmp-from))
3326     (unless (numberp gnus-tmp-lines)
3327       (setq gnus-tmp-lines -1))
3328     (if (= gnus-tmp-lines -1)
3329         (setq gnus-tmp-lines "?")
3330       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3331       (gnus-put-text-property
3332      (point)
3333      (progn (eval gnus-summary-line-format-spec) (point))
3334        'gnus-number gnus-tmp-number)
3335     (when (gnus-visual-p 'summary-highlight 'highlight)
3336       (forward-line -1)
3337       (gnus-run-hooks 'gnus-summary-update-hook)
3338       (forward-line 1))))
3339
3340 (defun gnus-summary-update-line (&optional dont-update)
3341   "Update summary line after change."
3342   (when (and gnus-summary-default-score
3343              (not gnus-summary-inhibit-highlight))
3344     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3345            (article (gnus-summary-article-number))
3346            (score (gnus-summary-article-score article)))
3347       (unless dont-update
3348         (if (and gnus-summary-mark-below
3349                  (< (gnus-summary-article-score)
3350                     gnus-summary-mark-below))
3351             ;; This article has a low score, so we mark it as read.
3352             (when (memq article gnus-newsgroup-unreads)
3353               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3354           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3355             ;; This article was previously marked as read on account
3356             ;; of a low score, but now it has risen, so we mark it as
3357             ;; unread.
3358             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3359         (gnus-summary-update-mark
3360          (if (or (null gnus-summary-default-score)
3361                  (<= (abs (- score gnus-summary-default-score))
3362                      gnus-summary-zcore-fuzz))
3363              ?                          ;Whitespace
3364            (if (< score gnus-summary-default-score)
3365                gnus-score-below-mark gnus-score-over-mark))
3366          'score))
3367       ;; Do visual highlighting.
3368       (when (gnus-visual-p 'summary-highlight 'highlight)
3369         (gnus-run-hooks 'gnus-summary-update-hook)))))
3370
3371 (defvar gnus-tmp-new-adopts nil)
3372
3373 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3374   "Return the number of articles in THREAD.
3375 This may be 0 in some cases -- if none of the articles in
3376 the thread are to be displayed."
3377   (let* ((number
3378          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3379           (cond
3380            ((not (listp thread))
3381             1)
3382            ((and (consp thread) (cdr thread))
3383             (apply
3384              '+ 1 (mapcar
3385                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3386            ((null thread)
3387             1)
3388            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3389             1)
3390            (t 0))))
3391     (when (and level (zerop level) gnus-tmp-new-adopts)
3392       (incf number
3393             (apply '+ (mapcar
3394                        'gnus-summary-number-of-articles-in-thread
3395                        gnus-tmp-new-adopts))))
3396     (if char
3397         (if (> number 1) gnus-not-empty-thread-mark
3398           gnus-empty-thread-mark)
3399       number)))
3400
3401 (defsubst gnus-summary-line-message-size (head)
3402   "Return pretty-printed version of message size.
3403 This function is intended to be used in
3404 `gnus-summary-line-format-alist'."
3405   (let ((c (or (mail-header-chars head) -1)))
3406     (cond ((< c 0) "n/a")               ; chars not available
3407           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3408           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3409           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3410           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3411
3412
3413 (defun gnus-summary-set-local-parameters (group)
3414   "Go through the local params of GROUP and set all variable specs in that list."
3415   (let ((params (gnus-group-find-parameter group))
3416         (vars '(quit-config))           ; Ignore quit-config.
3417         elem)
3418     (while params
3419       (setq elem (car params)
3420             params (cdr params))
3421       (and (consp elem)                 ; Has to be a cons.
3422            (consp (cdr elem))           ; The cdr has to be a list.
3423            (symbolp (car elem))         ; Has to be a symbol in there.
3424            (not (memq (car elem) vars))
3425            (ignore-errors               ; So we set it.
3426              (push (car elem) vars)
3427              (make-local-variable (car elem))
3428              (set (car elem) (eval (nth 1 elem))))))))
3429
3430 (defun gnus-summary-read-group (group &optional show-all no-article
3431                                       kill-buffer no-display backward
3432                                       select-articles)
3433   "Start reading news in newsgroup GROUP.
3434 If SHOW-ALL is non-nil, already read articles are also listed.
3435 If NO-ARTICLE is non-nil, no article is selected initially.
3436 If NO-DISPLAY, don't generate a summary buffer."
3437   (let (result)
3438     (while (and group
3439                 (null (setq result
3440                             (let ((gnus-auto-select-next nil))
3441                               (or (gnus-summary-read-group-1
3442                                    group show-all no-article
3443                                    kill-buffer no-display
3444                                    select-articles)
3445                                   (setq show-all nil
3446                                         select-articles nil)))))
3447                 (eq gnus-auto-select-next 'quietly))
3448       (set-buffer gnus-group-buffer)
3449       ;; The entry function called above goes to the next
3450       ;; group automatically, so we go two groups back
3451       ;; if we are searching for the previous group.
3452       (when backward
3453         (gnus-group-prev-unread-group 2))
3454       (if (not (equal group (gnus-group-group-name)))
3455           (setq group (gnus-group-group-name))
3456         (setq group nil)))
3457     result))
3458
3459 (defun gnus-summary-read-group-1 (group show-all no-article
3460                                         kill-buffer no-display
3461                                         &optional select-articles)
3462   ;; Killed foreign groups can't be entered.
3463   ;;  (when (and (not (gnus-group-native-p group))
3464   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3465   ;;    (error "Dead non-native groups can't be entered"))
3466   (gnus-message 5 "Retrieving newsgroup: %s..."
3467                 (gnus-group-decoded-name group))
3468   (let* ((new-group (gnus-summary-setup-buffer group))
3469          (quit-config (gnus-group-quit-config group))
3470          (did-select (and new-group (gnus-select-newsgroup
3471                                      group show-all select-articles))))
3472     (cond
3473      ;; This summary buffer exists already, so we just select it.
3474      ((not new-group)
3475       (gnus-set-global-variables)
3476       (when kill-buffer
3477         (gnus-kill-or-deaden-summary kill-buffer))
3478       (gnus-configure-windows 'summary 'force)
3479       (gnus-set-mode-line 'summary)
3480       (gnus-summary-position-point)
3481       (message "")
3482       t)
3483      ;; We couldn't select this group.
3484      ((null did-select)
3485       (when (and (eq major-mode 'gnus-summary-mode)
3486                  (not (equal (current-buffer) kill-buffer)))
3487         (kill-buffer (current-buffer))
3488         (if (not quit-config)
3489             (progn
3490               ;; Update the info -- marks might need to be removed,
3491               ;; for instance.
3492               (gnus-summary-update-info)
3493               (set-buffer gnus-group-buffer)
3494               (gnus-group-jump-to-group group)
3495               (gnus-group-next-unread-group 1))
3496           (gnus-handle-ephemeral-exit quit-config)))
3497       (let ((grpinfo (gnus-get-info group)))
3498         (if (null (gnus-info-read grpinfo))
3499             (gnus-message 3 "Group %s contains no messages"
3500                           (gnus-group-decoded-name group))
3501           (gnus-message 3 "Can't select group")))
3502       nil)
3503      ;; The user did a `C-g' while prompting for number of articles,
3504      ;; so we exit this group.
3505      ((eq did-select 'quit)
3506       (and (eq major-mode 'gnus-summary-mode)
3507            (not (equal (current-buffer) kill-buffer))
3508            (kill-buffer (current-buffer)))
3509       (when kill-buffer
3510         (gnus-kill-or-deaden-summary kill-buffer))
3511       (if (not quit-config)
3512           (progn
3513             (set-buffer gnus-group-buffer)
3514             (gnus-group-jump-to-group group)
3515             (gnus-group-next-unread-group 1)
3516             (gnus-configure-windows 'group 'force))
3517         (gnus-handle-ephemeral-exit quit-config))
3518       ;; Finally signal the quit.
3519       (signal 'quit nil))
3520      ;; The group was successfully selected.
3521      (t
3522       (gnus-set-global-variables)
3523       ;; Save the active value in effect when the group was entered.
3524       (setq gnus-newsgroup-active
3525             (gnus-copy-sequence
3526              (gnus-active gnus-newsgroup-name)))
3527       ;; You can change the summary buffer in some way with this hook.
3528       (gnus-run-hooks 'gnus-select-group-hook)
3529       (gnus-update-format-specifications
3530        nil 'summary 'summary-mode 'summary-dummy)
3531       (gnus-update-summary-mark-positions)
3532       ;; Do score processing.
3533       (when gnus-use-scoring
3534         (gnus-possibly-score-headers))
3535       ;; Check whether to fill in the gaps in the threads.
3536       (when gnus-build-sparse-threads
3537         (gnus-build-sparse-threads))
3538       ;; Find the initial limit.
3539       (if gnus-show-threads
3540           (if show-all
3541               (let ((gnus-newsgroup-dormant nil))
3542                 (gnus-summary-initial-limit show-all))
3543             (gnus-summary-initial-limit show-all))
3544         ;; When unthreaded, all articles are always shown.
3545         (setq gnus-newsgroup-limit
3546               (mapcar
3547                (lambda (header) (mail-header-number header))
3548                gnus-newsgroup-headers)))
3549       ;; Generate the summary buffer.
3550       (unless no-display
3551         (gnus-summary-prepare))
3552       (when gnus-use-trees
3553         (gnus-tree-open group)
3554         (setq gnus-summary-highlight-line-function
3555               'gnus-tree-highlight-article))
3556       ;; If the summary buffer is empty, but there are some low-scored
3557       ;; articles or some excluded dormants, we include these in the
3558       ;; buffer.
3559       (when (and (zerop (buffer-size))
3560                  (not no-display))
3561         (cond (gnus-newsgroup-dormant
3562                (gnus-summary-limit-include-dormant))
3563               ((and gnus-newsgroup-scored show-all)
3564                (gnus-summary-limit-include-expunged t))))
3565       ;; Function `gnus-apply-kill-file' must be called in this hook.
3566       (gnus-run-hooks 'gnus-apply-kill-hook)
3567       (if (and (zerop (buffer-size))
3568                (not no-display))
3569           (progn
3570             ;; This newsgroup is empty.
3571             (gnus-summary-catchup-and-exit nil t)
3572             (gnus-message 6 "No unread news")
3573             (when kill-buffer
3574               (gnus-kill-or-deaden-summary kill-buffer))
3575             ;; Return nil from this function.
3576             nil)
3577         ;; Hide conversation thread subtrees.  We cannot do this in
3578         ;; gnus-summary-prepare-hook since kill processing may not
3579         ;; work with hidden articles.
3580         (gnus-summary-maybe-hide-threads)
3581         (when kill-buffer
3582           (gnus-kill-or-deaden-summary kill-buffer))
3583         (gnus-summary-auto-select-subject)
3584         ;; Show first unread article if requested.
3585         (if (and (not no-article)
3586                  (not no-display)
3587                  gnus-newsgroup-unreads
3588                  gnus-auto-select-first)
3589             (progn
3590               (gnus-configure-windows 'summary)
3591               (let ((art (gnus-summary-article-number)))
3592                 (unless (and (not gnus-plugged)
3593                              (or (memq art gnus-newsgroup-undownloaded)
3594                                  (memq art gnus-newsgroup-downloadable)))
3595                   (gnus-summary-goto-article art))))
3596           ;; Don't select any articles.
3597           (gnus-summary-position-point)
3598           (gnus-configure-windows 'summary 'force)
3599           (gnus-set-mode-line 'summary))
3600         (when (and gnus-auto-center-group
3601                    (get-buffer-window gnus-group-buffer t))
3602           ;; Gotta use windows, because recenter does weird stuff if
3603           ;; the current buffer ain't the displayed window.
3604           (let ((owin (selected-window)))
3605             (select-window (get-buffer-window gnus-group-buffer t))
3606             (when (gnus-group-goto-group group)
3607               (recenter))
3608             (select-window owin)))
3609         ;; Mark this buffer as "prepared".
3610         (setq gnus-newsgroup-prepared t)
3611         (gnus-run-hooks 'gnus-summary-prepared-hook)
3612         (unless (gnus-ephemeral-group-p group)
3613           (gnus-group-update-group group))
3614         t)))))
3615
3616 (defun gnus-summary-auto-select-subject ()
3617   "Select the subject line on initial group entry."
3618   (goto-char (point-min))
3619   (cond
3620    ((eq gnus-auto-select-subject 'best)
3621     (gnus-summary-best-unread-subject))
3622    ((eq gnus-auto-select-subject 'unread)
3623     (gnus-summary-first-unread-subject))
3624    ((eq gnus-auto-select-subject 'unseen)
3625     (gnus-summary-first-unseen-subject))
3626    ((eq gnus-auto-select-subject 'unseen-or-unread)
3627     (gnus-summary-first-unseen-or-unread-subject))
3628    ((eq gnus-auto-select-subject 'first)
3629     ;; Do nothing.
3630     )
3631    ((functionp gnus-auto-select-subject)
3632     (funcall gnus-auto-select-subject))))
3633
3634 (defun gnus-summary-prepare ()
3635   "Generate the summary buffer."
3636   (interactive)
3637   (let ((buffer-read-only nil))
3638     (erase-buffer)
3639     (setq gnus-newsgroup-data nil
3640           gnus-newsgroup-data-reverse nil)
3641     (gnus-run-hooks 'gnus-summary-generate-hook)
3642     ;; Generate the buffer, either with threads or without.
3643     (when gnus-newsgroup-headers
3644       (gnus-summary-prepare-threads
3645        (if gnus-show-threads
3646            (gnus-sort-gathered-threads
3647             (funcall gnus-summary-thread-gathering-function
3648                      (gnus-sort-threads
3649                       (gnus-cut-threads (gnus-make-threads)))))
3650          ;; Unthreaded display.
3651          (gnus-sort-articles gnus-newsgroup-headers))))
3652     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3653     ;; Call hooks for modifying summary buffer.
3654     (goto-char (point-min))
3655     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3656
3657 (defsubst gnus-general-simplify-subject (subject)
3658   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3659   (setq subject
3660         (cond
3661          ;; Truncate the subject.
3662          (gnus-simplify-subject-functions
3663           (gnus-map-function gnus-simplify-subject-functions subject))
3664          ((numberp gnus-summary-gather-subject-limit)
3665           (setq subject (gnus-simplify-subject-re subject))
3666           (if (> (length subject) gnus-summary-gather-subject-limit)
3667               (substring subject 0 gnus-summary-gather-subject-limit)
3668             subject))
3669          ;; Fuzzily simplify it.
3670          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3671           (gnus-simplify-subject-fuzzy subject))
3672          ;; Just remove the leading "Re:".
3673          (t
3674           (gnus-simplify-subject-re subject))))
3675
3676   (if (and gnus-summary-gather-exclude-subject
3677            (string-match gnus-summary-gather-exclude-subject subject))
3678       nil                         ; This article shouldn't be gathered
3679     subject))
3680
3681 (defun gnus-summary-simplify-subject-query ()
3682   "Query where the respool algorithm would put this article."
3683   (interactive)
3684   (gnus-summary-select-article)
3685   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3686
3687 (defun gnus-gather-threads-by-subject (threads)
3688   "Gather threads by looking at Subject headers."
3689   (if (not gnus-summary-make-false-root)
3690       threads
3691     (let ((hashtb (gnus-make-hashtable 1024))
3692           (prev threads)
3693           (result threads)
3694           subject hthread whole-subject)
3695       (while threads
3696         (setq subject (gnus-general-simplify-subject
3697                        (setq whole-subject (mail-header-subject
3698                                             (caar threads)))))
3699         (when subject
3700           (if (setq hthread (gnus-gethash subject hashtb))
3701               (progn
3702                 ;; We enter a dummy root into the thread, if we
3703                 ;; haven't done that already.
3704                 (unless (stringp (caar hthread))
3705                   (setcar hthread (list whole-subject (car hthread))))
3706                 ;; We add this new gathered thread to this gathered
3707                 ;; thread.
3708                 (setcdr (car hthread)
3709                         (nconc (cdar hthread) (list (car threads))))
3710                 ;; Remove it from the list of threads.
3711                 (setcdr prev (cdr threads))
3712                 (setq threads prev))
3713             ;; Enter this thread into the hash table.
3714             (gnus-sethash subject
3715                           (if gnus-summary-make-false-root-always
3716                               (progn
3717                                 ;; If you want a dummy root above all
3718                                 ;; threads...
3719                                 (setcar threads (list whole-subject
3720                                                       (car threads)))
3721                                 threads)
3722                             threads)
3723                           hashtb)))
3724         (setq prev threads)
3725         (setq threads (cdr threads)))
3726       result)))
3727
3728 (defun gnus-gather-threads-by-references (threads)
3729   "Gather threads by looking at References headers."
3730   (let ((idhashtb (gnus-make-hashtable 1024))
3731         (thhashtb (gnus-make-hashtable 1024))
3732         (prev threads)
3733         (result threads)
3734         ids references id gthread gid entered ref)
3735     (while threads
3736       (when (setq references (mail-header-references (caar threads)))
3737         (setq id (mail-header-id (caar threads))
3738               ids (inline (gnus-split-references references))
3739               entered nil)
3740         (while (setq ref (pop ids))
3741           (setq ids (delete ref ids))
3742           (if (not (setq gid (gnus-gethash ref idhashtb)))
3743               (progn
3744                 (gnus-sethash ref id idhashtb)
3745                 (gnus-sethash id threads thhashtb))
3746             (setq gthread (gnus-gethash gid thhashtb))
3747             (unless entered
3748               ;; We enter a dummy root into the thread, if we
3749               ;; haven't done that already.
3750               (unless (stringp (caar gthread))
3751                 (setcar gthread (list (mail-header-subject (caar gthread))
3752                                       (car gthread))))
3753               ;; We add this new gathered thread to this gathered
3754               ;; thread.
3755               (setcdr (car gthread)
3756                       (nconc (cdar gthread) (list (car threads)))))
3757             ;; Add it into the thread hash table.
3758             (gnus-sethash id gthread thhashtb)
3759             (setq entered t)
3760             ;; Remove it from the list of threads.
3761             (setcdr prev (cdr threads))
3762             (setq threads prev))))
3763       (setq prev threads)
3764       (setq threads (cdr threads)))
3765     result))
3766
3767 (defun gnus-sort-gathered-threads (threads)
3768   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3769   (let ((result threads))
3770     (while threads
3771       (when (stringp (caar threads))
3772         (setcdr (car threads)
3773                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3774       (setq threads (cdr threads)))
3775     result))
3776
3777 (defun gnus-thread-loop-p (root thread)
3778   "Say whether ROOT is in THREAD."
3779   (let ((stack (list thread))
3780         (infloop 0)
3781         th)
3782     (while (setq thread (pop stack))
3783       (setq th (cdr thread))
3784       (while (and th
3785                   (not (eq (caar th) root)))
3786         (pop th))
3787       (if th
3788           ;; We have found a loop.
3789           (let (ref-dep)
3790             (setcdr thread (delq (car th) (cdr thread)))
3791             (if (boundp (setq ref-dep (intern "none"
3792                                               gnus-newsgroup-dependencies)))
3793                 (setcdr (symbol-value ref-dep)
3794                         (nconc (cdr (symbol-value ref-dep))
3795                                (list (car th))))
3796               (set ref-dep (list nil (car th))))
3797             (setq infloop 1
3798                   stack nil))
3799         ;; Push all the subthreads onto the stack.
3800         (push (cdr thread) stack)))
3801     infloop))
3802
3803 (defun gnus-make-threads ()
3804   "Go through the dependency hashtb and find the roots.  Return all threads."
3805   (let (threads)
3806     (while (catch 'infloop
3807              (mapatoms
3808               (lambda (refs)
3809                 ;; Deal with self-referencing References loops.
3810                 (when (and (car (symbol-value refs))
3811                            (not (zerop
3812                                  (apply
3813                                   '+
3814                                   (mapcar
3815                                    (lambda (thread)
3816                                      (gnus-thread-loop-p
3817                                       (car (symbol-value refs)) thread))
3818                                    (cdr (symbol-value refs)))))))
3819                   (setq threads nil)
3820                   (throw 'infloop t))
3821                 (unless (car (symbol-value refs))
3822                   ;; These threads do not refer back to any other
3823                   ;; articles, so they're roots.
3824                   (setq threads (append (cdr (symbol-value refs)) threads))))
3825               gnus-newsgroup-dependencies)))
3826     threads))
3827
3828 ;; Build the thread tree.
3829 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3830   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3831
3832 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3833 if it was already present.
3834
3835 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3836 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3837 Message-IDs will be renamed to a unique Message-ID before being
3838 entered.
3839
3840 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3841   (let* ((id (mail-header-id header))
3842          (id-dep (and id (intern id dependencies)))
3843          parent-id ref ref-dep ref-header replaced)
3844     ;; Enter this `header' in the `dependencies' table.
3845     (cond
3846      ((not id-dep)
3847       (setq header nil))
3848      ;; The first two cases do the normal part: enter a new `header'
3849      ;; in the `dependencies' table.
3850      ((not (boundp id-dep))
3851       (set id-dep (list header)))
3852      ((null (car (symbol-value id-dep)))
3853       (setcar (symbol-value id-dep) header))
3854
3855      ;; From here the `header' was already present in the
3856      ;; `dependencies' table.
3857      (force-new
3858       ;; Overrides an existing entry;
3859       ;; just set the header part of the entry.
3860       (setcar (symbol-value id-dep) header)
3861       (setq replaced t))
3862
3863      ;; Renames the existing `header' to a unique Message-ID.
3864      ((not gnus-summary-ignore-duplicates)
3865       ;; An article with this Message-ID has already been seen.
3866       ;; We rename the Message-ID.
3867       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3868            (list header))
3869       (mail-header-set-id header id))
3870
3871      ;; The last case ignores an existing entry, except it adds any
3872      ;; additional Xrefs (in case the two articles came from different
3873      ;; servers.
3874      ;; Also sets `header' to `nil' meaning that the `dependencies'
3875      ;; table was *not* modified.
3876      (t
3877       (mail-header-set-xref
3878        (car (symbol-value id-dep))
3879        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3880                    "")
3881                (or (mail-header-xref header) "")))
3882       (setq header nil)))
3883
3884     (when (and header (not replaced))
3885       ;; First check that we are not creating a References loop.
3886       (setq parent-id (gnus-parent-id (mail-header-references header)))
3887       (setq ref parent-id)
3888       (while (and ref
3889                   (setq ref-dep (intern-soft ref dependencies))
3890                   (boundp ref-dep)
3891                   (setq ref-header (car (symbol-value ref-dep))))
3892         (if (string= id ref)
3893             ;; Yuk!  This is a reference loop.  Make the article be a
3894             ;; root article.
3895             (progn
3896               (mail-header-set-references (car (symbol-value id-dep)) "none")
3897               (setq ref nil)
3898               (setq parent-id nil))
3899           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3900       (setq ref-dep (intern (or parent-id "none") dependencies))
3901       (if (boundp ref-dep)
3902           (setcdr (symbol-value ref-dep)
3903                   (nconc (cdr (symbol-value ref-dep))
3904                          (list (symbol-value id-dep))))
3905         (set ref-dep (list nil (symbol-value id-dep)))))
3906     header))
3907
3908 (defun gnus-extract-message-id-from-in-reply-to (string)
3909   (if (string-match "<[^>]+>" string)
3910       (substring string (match-beginning 0) (match-end 0))
3911     nil))
3912
3913 (defun gnus-build-sparse-threads ()
3914   (let ((headers gnus-newsgroup-headers)
3915         (mail-parse-charset gnus-newsgroup-charset)
3916         (gnus-summary-ignore-duplicates t)
3917         header references generation relations
3918         subject child end new-child date)
3919     ;; First we create an alist of generations/relations, where
3920     ;; generations is how much we trust the relation, and the relation
3921     ;; is parent/child.
3922     (gnus-message 7 "Making sparse threads...")
3923     (save-excursion
3924       (nnheader-set-temp-buffer " *gnus sparse threads*")
3925       (while (setq header (pop headers))
3926         (when (and (setq references (mail-header-references header))
3927                    (not (string= references "")))
3928           (insert references)
3929           (setq child (mail-header-id header)
3930                 subject (mail-header-subject header)
3931                 date (mail-header-date header)
3932                 generation 0)
3933           (while (search-backward ">" nil t)
3934             (setq end (1+ (point)))
3935             (when (search-backward "<" nil t)
3936               (setq new-child (buffer-substring (point) end))
3937               (push (list (incf generation)
3938                           child (setq child new-child)
3939                           subject date)
3940                     relations)))
3941           (when child
3942             (push (list (1+ generation) child nil subject) relations))
3943           (erase-buffer)))
3944       (kill-buffer (current-buffer)))
3945     ;; Sort over trustworthiness.
3946     (mapcar
3947      (lambda (relation)
3948        (when (gnus-dependencies-add-header
3949               (make-full-mail-header
3950                gnus-reffed-article-number
3951                (nth 3 relation) "" (or (nth 4 relation) "")
3952                (nth 1 relation)
3953                (or (nth 2 relation) "") 0 0 "")
3954               gnus-newsgroup-dependencies nil)
3955          (push gnus-reffed-article-number gnus-newsgroup-limit)
3956          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3957          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3958                gnus-newsgroup-reads)
3959          (decf gnus-reffed-article-number)))
3960      (sort relations 'car-less-than-car))
3961     (gnus-message 7 "Making sparse threads...done")))
3962
3963 (defun gnus-build-old-threads ()
3964   ;; Look at all the articles that refer back to old articles, and
3965   ;; fetch the headers for the articles that aren't there.  This will
3966   ;; build complete threads - if the roots haven't been expired by the
3967   ;; server, that is.
3968   (let ((mail-parse-charset gnus-newsgroup-charset)
3969         id heads)
3970     (mapatoms
3971      (lambda (refs)
3972        (when (not (car (symbol-value refs)))
3973          (setq heads (cdr (symbol-value refs)))
3974          (while heads
3975            (if (memq (mail-header-number (caar heads))
3976                      gnus-newsgroup-dormant)
3977                (setq heads (cdr heads))
3978              (setq id (symbol-name refs))
3979              (while (and (setq id (gnus-build-get-header id))
3980                          (not (car (gnus-id-to-thread id)))))
3981              (setq heads nil)))))
3982      gnus-newsgroup-dependencies)))
3983
3984 (defsubst gnus-remove-odd-characters (string)
3985   "Translate STRING into something that doesn't contain weird characters."
3986   (mm-subst-char-in-string
3987    ?\r ?\-
3988    (mm-subst-char-in-string ?\n ?\- string t) t))
3989
3990 ;; This function has to be called with point after the article number
3991 ;; on the beginning of the line.
3992 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3993   (let ((eol (point-at-eol))
3994         (buffer (current-buffer))
3995         header references in-reply-to)
3996
3997     ;; overview: [num subject from date id refs chars lines misc]
3998     (unwind-protect
3999         (let (x)
4000           (narrow-to-region (point) eol)
4001           (unless (eobp)
4002             (forward-char))
4003
4004           (setq header
4005                 (make-full-mail-header
4006                  number                 ; number
4007                  (condition-case ()     ; subject
4008                      (gnus-remove-odd-characters
4009                       (funcall gnus-decode-encoded-word-function
4010                                (setq x (nnheader-nov-field))))
4011                    (error x))
4012                  (condition-case ()     ; from
4013                      (gnus-remove-odd-characters
4014                       (funcall gnus-decode-encoded-word-function
4015                                (setq x (nnheader-nov-field))))
4016                    (error x))
4017                  (nnheader-nov-field)   ; date
4018                  (nnheader-nov-read-message-id) ; id
4019                  (setq references (nnheader-nov-field)) ; refs
4020                  (nnheader-nov-read-integer) ; chars
4021                  (nnheader-nov-read-integer) ; lines
4022                  (unless (eobp)
4023                    (if (looking-at "Xref: ")
4024                        (goto-char (match-end 0)))
4025                    (nnheader-nov-field)) ; Xref
4026                  (nnheader-nov-parse-extra)))) ; extra
4027
4028       (widen))
4029
4030     (when (and (string= references "")
4031                (setq in-reply-to (mail-header-extra header))
4032                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4033       (mail-header-set-references
4034        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4035
4036     (when gnus-alter-header-function
4037       (funcall gnus-alter-header-function header))
4038     (gnus-dependencies-add-header header dependencies force-new)))
4039
4040 (defun gnus-build-get-header (id)
4041   "Look through the buffer of NOV lines and find the header to ID.
4042 Enter this line into the dependencies hash table, and return
4043 the id of the parent article (if any)."
4044   (let ((deps gnus-newsgroup-dependencies)
4045         found header)
4046     (prog1
4047         (save-excursion
4048           (set-buffer nntp-server-buffer)
4049           (let ((case-fold-search nil))
4050             (goto-char (point-min))
4051             (while (and (not found)
4052                         (search-forward id nil t))
4053               (beginning-of-line)
4054               (setq found (looking-at
4055                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4056                                    (regexp-quote id))))
4057               (or found (beginning-of-line 2)))
4058             (when found
4059               (beginning-of-line)
4060               (and
4061                (setq header (gnus-nov-parse-line
4062                              (read (current-buffer)) deps))
4063                (gnus-parent-id (mail-header-references header))))))
4064       (when header
4065         (let ((number (mail-header-number header)))
4066           (push number gnus-newsgroup-limit)
4067           (push header gnus-newsgroup-headers)
4068           (if (memq number gnus-newsgroup-unselected)
4069               (progn
4070                 (setq gnus-newsgroup-unreads
4071                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4072                                                number))
4073                 (setq gnus-newsgroup-unselected
4074                       (delq number gnus-newsgroup-unselected)))
4075             (push number gnus-newsgroup-ancient)))))))
4076
4077 (defun gnus-build-all-threads ()
4078   "Read all the headers."
4079   (let ((gnus-summary-ignore-duplicates t)
4080         (mail-parse-charset gnus-newsgroup-charset)
4081         (dependencies gnus-newsgroup-dependencies)
4082         header article)
4083     (save-excursion
4084       (set-buffer nntp-server-buffer)
4085       (let ((case-fold-search nil))
4086         (goto-char (point-min))
4087         (while (not (eobp))
4088           (ignore-errors
4089             (setq article (read (current-buffer))
4090                   header (gnus-nov-parse-line article dependencies)))
4091           (when header
4092             (save-excursion
4093               (set-buffer gnus-summary-buffer)
4094               (push header gnus-newsgroup-headers)
4095               (if (memq (setq article (mail-header-number header))
4096                         gnus-newsgroup-unselected)
4097                   (progn
4098                     (setq gnus-newsgroup-unreads
4099                           (gnus-add-to-sorted-list
4100                            gnus-newsgroup-unreads article))
4101                     (setq gnus-newsgroup-unselected
4102                           (delq article gnus-newsgroup-unselected)))
4103                 (push article gnus-newsgroup-ancient)))
4104             (forward-line 1)))))))
4105
4106 (defun gnus-summary-update-article-line (article header)
4107   "Update the line for ARTICLE using HEADER."
4108   (let* ((id (mail-header-id header))
4109          (thread (gnus-id-to-thread id)))
4110     (unless thread
4111       (error "Article in no thread"))
4112     ;; Update the thread.
4113     (setcar thread header)
4114     (gnus-summary-goto-subject article)
4115     (let* ((datal (gnus-data-find-list article))
4116            (data (car datal))
4117            (buffer-read-only nil)
4118            (level (gnus-summary-thread-level)))
4119       (gnus-delete-line)
4120       (let ((inserted (- (point)
4121                          (progn
4122                            (gnus-summary-insert-line
4123                             header level nil
4124                             (memq article gnus-newsgroup-undownloaded)
4125                             (gnus-article-mark article)
4126                             (memq article gnus-newsgroup-replied)
4127                             (memq article gnus-newsgroup-expirable)
4128                             ;; Only insert the Subject string when it's different
4129                             ;; from the previous Subject string.
4130                             (if (and
4131                                  gnus-show-threads
4132                                  (gnus-subject-equal
4133                                   (condition-case ()
4134                                       (mail-header-subject
4135                                        (gnus-data-header
4136                                         (cadr
4137                                          (gnus-data-find-list
4138                                           article
4139                                           (gnus-data-list t)))))
4140                                     ;; Error on the side of excessive subjects.
4141                                     (error ""))
4142                                   (mail-header-subject header)))
4143                                 ""
4144                               (mail-header-subject header))
4145                             nil (cdr (assq article gnus-newsgroup-scored))
4146                             (memq article gnus-newsgroup-processable))
4147                            (point)))))
4148         (when (cdr datal)
4149           (gnus-data-update-list
4150            (cdr datal)
4151            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4152
4153 (defun gnus-summary-update-article (article &optional iheader)
4154   "Update ARTICLE in the summary buffer."
4155   (set-buffer gnus-summary-buffer)
4156   (let* ((header (gnus-summary-article-header article))
4157          (id (mail-header-id header))
4158          (data (gnus-data-find article))
4159          (thread (gnus-id-to-thread id))
4160          (references (mail-header-references header))
4161          (parent
4162           (gnus-id-to-thread
4163            (or (gnus-parent-id
4164                 (when (and references
4165                            (not (equal "" references)))
4166                   references))
4167                "none")))
4168          (buffer-read-only nil)
4169          (old (car thread)))
4170     (when thread
4171       (unless iheader
4172         (setcar thread nil)
4173         (when parent
4174           (delq thread parent)))
4175       (if (gnus-summary-insert-subject id header)
4176           ;; Set the (possibly) new article number in the data structure.
4177           (gnus-data-set-number data (gnus-id-to-article id))
4178         (setcar thread old)
4179         nil))))
4180
4181 (defun gnus-rebuild-thread (id &optional line)
4182   "Rebuild the thread containing ID.
4183 If LINE, insert the rebuilt thread starting on line LINE."
4184   (let ((buffer-read-only nil)
4185         old-pos current thread data)
4186     (if (not gnus-show-threads)
4187         (setq thread (list (car (gnus-id-to-thread id))))
4188       ;; Get the thread this article is part of.
4189       (setq thread (gnus-remove-thread id)))
4190     (setq old-pos (point-at-bol))
4191     (setq current (save-excursion
4192                     (and (re-search-backward "[\r\n]" nil t)
4193                          (gnus-summary-article-number))))
4194     ;; If this is a gathered thread, we have to go some re-gathering.
4195     (when (stringp (car thread))
4196       (let ((subject (car thread))
4197             roots thr)
4198         (setq thread (cdr thread))
4199         (while thread
4200           (unless (memq (setq thr (gnus-id-to-thread
4201                                    (gnus-root-id
4202                                     (mail-header-id (caar thread)))))
4203                         roots)
4204             (push thr roots))
4205           (setq thread (cdr thread)))
4206         ;; We now have all (unique) roots.
4207         (if (= (length roots) 1)
4208             ;; All the loose roots are now one solid root.
4209             (setq thread (car roots))
4210           (setq thread (cons subject (gnus-sort-threads roots))))))
4211     (let (threads)
4212       ;; We then insert this thread into the summary buffer.
4213       (when line
4214         (goto-char (point-min))
4215         (forward-line (1- line)))
4216       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4217         (if gnus-show-threads
4218             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4219           (gnus-summary-prepare-unthreaded thread))
4220         (setq data (nreverse gnus-newsgroup-data))
4221         (setq threads gnus-newsgroup-threads))
4222       ;; We splice the new data into the data structure.
4223       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4224       ;;!!! then we want to insert at the beginning of the buffer.
4225       ;;!!! That happens to be true with Gnus now, but that may
4226       ;;!!! change in the future.  Perhaps.
4227       (gnus-data-enter-list
4228        (if line nil current) data (- (point) old-pos))
4229       (setq gnus-newsgroup-threads
4230             (nconc threads gnus-newsgroup-threads))
4231       (gnus-data-compute-positions))))
4232
4233 (defun gnus-number-to-header (number)
4234   "Return the header for article NUMBER."
4235   (let ((headers gnus-newsgroup-headers))
4236     (while (and headers
4237                 (not (= number (mail-header-number (car headers)))))
4238       (pop headers))
4239     (when headers
4240       (car headers))))
4241
4242 (defun gnus-parent-headers (in-headers &optional generation)
4243   "Return the headers of the GENERATIONeth parent of HEADERS."
4244   (unless generation
4245     (setq generation 1))
4246   (let ((parent t)
4247         (headers in-headers)
4248         references)
4249     (while (and parent
4250                 (not (zerop generation))
4251                 (setq references (mail-header-references headers)))
4252       (setq headers (if (and references
4253                              (setq parent (gnus-parent-id references)))
4254                         (car (gnus-id-to-thread parent))
4255                       nil))
4256       (decf generation))
4257     (and (not (eq headers in-headers))
4258          headers)))
4259
4260 (defun gnus-id-to-thread (id)
4261   "Return the (sub-)thread where ID appears."
4262   (gnus-gethash id gnus-newsgroup-dependencies))
4263
4264 (defun gnus-id-to-article (id)
4265   "Return the article number of ID."
4266   (let ((thread (gnus-id-to-thread id)))
4267     (when (and thread
4268                (car thread))
4269       (mail-header-number (car thread)))))
4270
4271 (defun gnus-id-to-header (id)
4272   "Return the article headers of ID."
4273   (car (gnus-id-to-thread id)))
4274
4275 (defun gnus-article-displayed-root-p (article)
4276   "Say whether ARTICLE is a root(ish) article."
4277   (let ((level (gnus-summary-thread-level article))
4278         (refs (mail-header-references  (gnus-summary-article-header article)))
4279         particle)
4280     (cond
4281      ((null level) nil)
4282      ((zerop level) t)
4283      ((null refs) t)
4284      ((null (gnus-parent-id refs)) t)
4285      ((and (= 1 level)
4286            (null (setq particle (gnus-id-to-article
4287                                  (gnus-parent-id refs))))
4288            (null (gnus-summary-thread-level particle)))))))
4289
4290 (defun gnus-root-id (id)
4291   "Return the id of the root of the thread where ID appears."
4292   (let (last-id prev)
4293     (while (and id (setq prev (car (gnus-id-to-thread id))))
4294       (setq last-id id
4295             id (gnus-parent-id (mail-header-references prev))))
4296     last-id))
4297
4298 (defun gnus-articles-in-thread (thread)
4299   "Return the list of articles in THREAD."
4300   (cons (mail-header-number (car thread))
4301         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4302
4303 (defun gnus-remove-thread (id &optional dont-remove)
4304   "Remove the thread that has ID in it."
4305   (let (headers thread last-id)
4306     ;; First go up in this thread until we find the root.
4307     (setq last-id (gnus-root-id id)
4308           headers (message-flatten-list (gnus-id-to-thread last-id)))
4309     ;; We have now found the real root of this thread.  It might have
4310     ;; been gathered into some loose thread, so we have to search
4311     ;; through the threads to find the thread we wanted.
4312     (let ((threads gnus-newsgroup-threads)
4313           sub)
4314       (while threads
4315         (setq sub (car threads))
4316         (if (stringp (car sub))
4317             ;; This is a gathered thread, so we look at the roots
4318             ;; below it to find whether this article is in this
4319             ;; gathered root.
4320             (progn
4321               (setq sub (cdr sub))
4322               (while sub
4323                 (when (member (caar sub) headers)
4324                   (setq thread (car threads)
4325                         threads nil
4326                         sub nil))
4327                 (setq sub (cdr sub))))
4328           ;; It's an ordinary thread, so we check it.
4329           (when (eq (car sub) (car headers))
4330             (setq thread sub
4331                   threads nil)))
4332         (setq threads (cdr threads)))
4333       ;; If this article is in no thread, then it's a root.
4334       (if thread
4335           (unless dont-remove
4336             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4337         (setq thread (gnus-id-to-thread last-id)))
4338       (when thread
4339         (prog1
4340             thread                      ; We return this thread.
4341           (unless dont-remove
4342             (if (stringp (car thread))
4343                 (progn
4344                   ;; If we use dummy roots, then we have to remove the
4345                   ;; dummy root as well.
4346                   (when (eq gnus-summary-make-false-root 'dummy)
4347                     ;; We go to the dummy root by going to
4348                     ;; the first sub-"thread", and then one line up.
4349                     (gnus-summary-goto-article
4350                      (mail-header-number (caadr thread)))
4351                     (forward-line -1)
4352                     (gnus-delete-line)
4353                     (gnus-data-compute-positions))
4354                   (setq thread (cdr thread))
4355                   (while thread
4356                     (gnus-remove-thread-1 (car thread))
4357                     (setq thread (cdr thread))))
4358               (gnus-remove-thread-1 thread))))))))
4359
4360 (defun gnus-remove-thread-1 (thread)
4361   "Remove the thread THREAD recursively."
4362   (let ((number (mail-header-number (pop thread)))
4363         d)
4364     (setq thread (reverse thread))
4365     (while thread
4366       (gnus-remove-thread-1 (pop thread)))
4367     (when (setq d (gnus-data-find number))
4368       (goto-char (gnus-data-pos d))
4369       (gnus-summary-show-thread)
4370       (gnus-data-remove
4371        number
4372        (- (point-at-bol)
4373           (prog1
4374               (1+ (point-at-eol))
4375             (gnus-delete-line)))))))
4376
4377 (defun gnus-sort-threads-1 (threads func)
4378   (sort (mapcar (lambda (thread)
4379                   (cons (car thread)
4380                         (and (cdr thread)
4381                              (gnus-sort-threads-1 (cdr thread) func))))
4382                 threads) func))
4383
4384 (defun gnus-sort-threads (threads)
4385   "Sort THREADS."
4386   (if (not gnus-thread-sort-functions)
4387       threads
4388     (gnus-message 8 "Sorting threads...")
4389     (let ((max-lisp-eval-depth 5000))
4390       (prog1 (gnus-sort-threads-1
4391          threads
4392          (gnus-make-sort-function gnus-thread-sort-functions))
4393         (gnus-message 8 "Sorting threads...done")))))
4394
4395 (defun gnus-sort-articles (articles)
4396   "Sort ARTICLES."
4397   (when gnus-article-sort-functions
4398     (gnus-message 7 "Sorting articles...")
4399     (prog1
4400         (setq gnus-newsgroup-headers
4401               (sort articles (gnus-make-sort-function
4402                               gnus-article-sort-functions)))
4403       (gnus-message 7 "Sorting articles...done"))))
4404
4405 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4406 (defmacro gnus-thread-header (thread)
4407   "Return header of first article in THREAD.
4408 Note that THREAD must never, ever be anything else than a variable -
4409 using some other form will lead to serious barfage."
4410   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4411   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4412   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4413         (vector thread) 2))
4414
4415 (defsubst gnus-article-sort-by-number (h1 h2)
4416   "Sort articles by article number."
4417   (< (mail-header-number h1)
4418      (mail-header-number h2)))
4419
4420 (defun gnus-thread-sort-by-number (h1 h2)
4421   "Sort threads by root article number."
4422   (gnus-article-sort-by-number
4423    (gnus-thread-header h1) (gnus-thread-header h2)))
4424
4425 (defsubst gnus-article-sort-by-random (h1 h2)
4426   "Sort articles by article number."
4427   (zerop (random 2)))
4428
4429 (defun gnus-thread-sort-by-random (h1 h2)
4430   "Sort threads by root article number."
4431   (gnus-article-sort-by-random
4432    (gnus-thread-header h1) (gnus-thread-header h2)))
4433
4434 (defsubst gnus-article-sort-by-lines (h1 h2)
4435   "Sort articles by article Lines header."
4436   (< (mail-header-lines h1)
4437      (mail-header-lines h2)))
4438
4439 (defun gnus-thread-sort-by-lines (h1 h2)
4440   "Sort threads by root article Lines header."
4441   (gnus-article-sort-by-lines
4442    (gnus-thread-header h1) (gnus-thread-header h2)))
4443
4444 (defsubst gnus-article-sort-by-chars (h1 h2)
4445   "Sort articles by octet length."
4446   (< (mail-header-chars h1)
4447      (mail-header-chars h2)))
4448
4449 (defun gnus-thread-sort-by-chars (h1 h2)
4450   "Sort threads by root article octet length."
4451   (gnus-article-sort-by-chars
4452    (gnus-thread-header h1) (gnus-thread-header h2)))
4453
4454 (defsubst gnus-article-sort-by-author (h1 h2)
4455   "Sort articles by root author."
4456   (string-lessp
4457    (let ((extract (funcall
4458                    gnus-extract-address-components
4459                    (mail-header-from h1))))
4460      (or (car extract) (cadr extract) ""))
4461    (let ((extract (funcall
4462                    gnus-extract-address-components
4463                    (mail-header-from h2))))
4464      (or (car extract) (cadr extract) ""))))
4465
4466 (defun gnus-thread-sort-by-author (h1 h2)
4467   "Sort threads by root author."
4468   (gnus-article-sort-by-author
4469    (gnus-thread-header h1)  (gnus-thread-header h2)))
4470
4471 (defsubst gnus-article-sort-by-subject (h1 h2)
4472   "Sort articles by root subject."
4473   (string-lessp
4474    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4475    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4476
4477 (defun gnus-thread-sort-by-subject (h1 h2)
4478   "Sort threads by root subject."
4479   (gnus-article-sort-by-subject
4480    (gnus-thread-header h1) (gnus-thread-header h2)))
4481
4482 (defsubst gnus-article-sort-by-date (h1 h2)
4483   "Sort articles by root article date."
4484   (time-less-p
4485    (gnus-date-get-time (mail-header-date h1))
4486    (gnus-date-get-time (mail-header-date h2))))
4487
4488 (defun gnus-thread-sort-by-date (h1 h2)
4489   "Sort threads by root article date."
4490   (gnus-article-sort-by-date
4491    (gnus-thread-header h1) (gnus-thread-header h2)))
4492
4493 (defsubst gnus-article-sort-by-score (h1 h2)
4494   "Sort articles by root article score.
4495 Unscored articles will be counted as having a score of zero."
4496   (> (or (cdr (assq (mail-header-number h1)
4497                     gnus-newsgroup-scored))
4498          gnus-summary-default-score 0)
4499      (or (cdr (assq (mail-header-number h2)
4500                     gnus-newsgroup-scored))
4501          gnus-summary-default-score 0)))
4502
4503 (defun gnus-thread-sort-by-score (h1 h2)
4504   "Sort threads by root article score."
4505   (gnus-article-sort-by-score
4506    (gnus-thread-header h1) (gnus-thread-header h2)))
4507
4508 (defun gnus-thread-sort-by-total-score (h1 h2)
4509   "Sort threads by the sum of all scores in the thread.
4510 Unscored articles will be counted as having a score of zero."
4511   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4512
4513 (defun gnus-thread-total-score (thread)
4514   ;; This function find the total score of THREAD.
4515   (cond
4516    ((null thread)
4517     0)
4518    ((consp thread)
4519     (if (stringp (car thread))
4520         (apply gnus-thread-score-function 0
4521                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4522       (gnus-thread-total-score-1 thread)))
4523    (t
4524     (gnus-thread-total-score-1 (list thread)))))
4525
4526 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4527   "Sort threads such that the thread with the most recently arrived article comes first."
4528   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4529
4530 (defun gnus-thread-highest-number (thread)
4531   "Return the highest article number in THREAD."
4532   (apply 'max (mapcar (lambda (header)
4533                         (mail-header-number header))
4534                       (message-flatten-list thread))))
4535
4536 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4537   "Sort threads such that the thread with the most recently dated article comes first."
4538   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4539
4540 (defun gnus-thread-latest-date (thread)
4541   "Return the highest article date in THREAD."
4542   (let ((previous-time 0))
4543     (apply 'max
4544            (mapcar
4545             (lambda (header)
4546               (setq previous-time
4547                     (condition-case ()
4548                         (time-to-seconds (mail-header-parse-date
4549                                           (mail-header-date header)))
4550                       (error previous-time))))
4551             (sort
4552              (message-flatten-list thread)
4553              (lambda (h1 h2)
4554                (< (mail-header-number h1)
4555                   (mail-header-number h2))))))))
4556
4557 (defun gnus-thread-total-score-1 (root)
4558   ;; This function find the total score of the thread below ROOT.
4559   (setq root (car root))
4560   (apply gnus-thread-score-function
4561          (or (append
4562               (mapcar 'gnus-thread-total-score
4563                       (cdr (gnus-id-to-thread (mail-header-id root))))
4564               (when (> (mail-header-number root) 0)
4565                 (list (or (cdr (assq (mail-header-number root)
4566                                      gnus-newsgroup-scored))
4567                           gnus-summary-default-score 0))))
4568              (list gnus-summary-default-score)
4569              '(0))))
4570
4571 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4572 (defvar gnus-tmp-prev-subject nil)
4573 (defvar gnus-tmp-false-parent nil)
4574 (defvar gnus-tmp-root-expunged nil)
4575 (defvar gnus-tmp-dummy-line nil)
4576
4577 (eval-when-compile (defvar gnus-tmp-header))
4578 (defun gnus-extra-header (type &optional header)
4579   "Return the extra header of TYPE."
4580   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4581       ""))
4582
4583 (defvar gnus-tmp-thread-tree-header-string "")
4584
4585 (defcustom gnus-sum-thread-tree-root "> "
4586   "With %B spec, used for the root of a thread.
4587 If nil, use subject instead."
4588   :type '(radio (const :format "%v  " nil) (string :size 0))
4589   :group 'gnus-thread)
4590 (defcustom gnus-sum-thread-tree-false-root "> "
4591   "With %B spec, used for a false root of a thread.
4592 If nil, use subject instead."
4593   :type '(radio (const :format "%v  " nil) (string :size 0))
4594   :group 'gnus-thread)
4595 (defcustom gnus-sum-thread-tree-single-indent ""
4596   "With %B spec, used for a thread with just one message.
4597 If nil, use subject instead."
4598   :type '(radio (const :format "%v  " nil) (string :size 0))
4599   :group 'gnus-thread)
4600 (defcustom gnus-sum-thread-tree-vertical "| "
4601   "With %B spec, used for drawing a vertical line."
4602   :type 'string
4603   :group 'gnus-thread)
4604 (defcustom gnus-sum-thread-tree-indent "  "
4605   "With %B spec, used for indenting."
4606   :type 'string
4607   :group 'gnus-thread)
4608 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4609   "With %B spec, used for a leaf with brothers."
4610   :type 'string
4611   :group 'gnus-thread)
4612 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4613   "With %B spec, used for a leaf without brothers."
4614   :type 'string
4615   :group 'gnus-thread)
4616
4617 (defun gnus-summary-prepare-threads (threads)
4618   "Prepare summary buffer from THREADS and indentation LEVEL.
4619 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4620 or a straight list of headers."
4621   (gnus-message 7 "Generating summary...")
4622
4623   (setq gnus-newsgroup-threads threads)
4624   (beginning-of-line)
4625
4626   (let ((gnus-tmp-level 0)
4627         (default-score (or gnus-summary-default-score 0))
4628         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4629         (building-line-count gnus-summary-display-while-building)
4630         (building-count (integerp gnus-summary-display-while-building))
4631         thread number subject stack state gnus-tmp-gathered beg-match
4632         new-roots gnus-tmp-new-adopts thread-end simp-subject
4633         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4634         gnus-tmp-replied gnus-tmp-subject-or-nil
4635         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4636         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4637         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4638         tree-stack)
4639
4640     (setq gnus-tmp-prev-subject nil
4641           gnus-tmp-thread-tree-header-string "")
4642
4643     (if (vectorp (car threads))
4644         ;; If this is a straight (sic) list of headers, then a
4645         ;; threaded summary display isn't required, so we just create
4646         ;; an unthreaded one.
4647         (gnus-summary-prepare-unthreaded threads)
4648
4649       ;; Do the threaded display.
4650
4651       (if gnus-summary-display-while-building
4652           (switch-to-buffer (buffer-name)))
4653       (while (or threads stack gnus-tmp-new-adopts new-roots)
4654
4655         (if (and (= gnus-tmp-level 0)
4656                  (or (not stack)
4657                      (= (caar stack) 0))
4658                  (not gnus-tmp-false-parent)
4659                  (or gnus-tmp-new-adopts new-roots))
4660             (if gnus-tmp-new-adopts
4661                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4662                       thread (list (car gnus-tmp-new-adopts))
4663                       gnus-tmp-header (caar thread)
4664                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4665               (when new-roots
4666                 (setq thread (list (car new-roots))
4667                       gnus-tmp-header (caar thread)
4668                       new-roots (cdr new-roots))))
4669
4670           (if threads
4671               ;; If there are some threads, we do them before the
4672               ;; threads on the stack.
4673               (setq thread threads
4674                     gnus-tmp-header (caar thread))
4675             ;; There were no current threads, so we pop something off
4676             ;; the stack.
4677             (setq state (car stack)
4678                   gnus-tmp-level (car state)
4679                   tree-stack (cadr state)
4680                   thread (caddr state)
4681                   stack (cdr stack)
4682                   gnus-tmp-header (caar thread))))
4683
4684         (setq gnus-tmp-false-parent nil)
4685         (setq gnus-tmp-root-expunged nil)
4686         (setq thread-end nil)
4687
4688         (if (stringp gnus-tmp-header)
4689             ;; The header is a dummy root.
4690             (cond
4691              ((eq gnus-summary-make-false-root 'adopt)
4692               ;; We let the first article adopt the rest.
4693               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4694                                                (cddar thread)))
4695               (setq gnus-tmp-gathered
4696                     (nconc (mapcar
4697                             (lambda (h) (mail-header-number (car h)))
4698                             (cddar thread))
4699                            gnus-tmp-gathered))
4700               (setq thread (cons (list (caar thread)
4701                                        (cadar thread))
4702                                  (cdr thread)))
4703               (setq gnus-tmp-level -1
4704                     gnus-tmp-false-parent t))
4705              ((eq gnus-summary-make-false-root 'empty)
4706               ;; We print adopted articles with empty subject fields.
4707               (setq gnus-tmp-gathered
4708                     (nconc (mapcar
4709                             (lambda (h) (mail-header-number (car h)))
4710                             (cddar thread))
4711                            gnus-tmp-gathered))
4712               (setq gnus-tmp-level -1))
4713              ((eq gnus-summary-make-false-root 'dummy)
4714               ;; We remember that we probably want to output a dummy
4715               ;; root.
4716               (setq gnus-tmp-dummy-line gnus-tmp-header)
4717               (setq gnus-tmp-prev-subject gnus-tmp-header))
4718              (t
4719               ;; We do not make a root for the gathered
4720               ;; sub-threads at all.
4721               (setq gnus-tmp-level -1)))
4722
4723           (setq number (mail-header-number gnus-tmp-header)
4724                 subject (mail-header-subject gnus-tmp-header)
4725                 simp-subject (gnus-simplify-subject-fully subject))
4726
4727           (cond
4728            ;; If the thread has changed subject, we might want to make
4729            ;; this subthread into a root.
4730            ((and (null gnus-thread-ignore-subject)
4731                  (not (zerop gnus-tmp-level))
4732                  gnus-tmp-prev-subject
4733                  (not (string= gnus-tmp-prev-subject simp-subject)))
4734             (setq new-roots (nconc new-roots (list (car thread)))
4735                   thread-end t
4736                   gnus-tmp-header nil))
4737            ;; If the article lies outside the current limit,
4738            ;; then we do not display it.
4739            ((not (memq number gnus-newsgroup-limit))
4740             (setq gnus-tmp-gathered
4741                   (nconc (mapcar
4742                           (lambda (h) (mail-header-number (car h)))
4743                           (cdar thread))
4744                          gnus-tmp-gathered))
4745             (setq gnus-tmp-new-adopts (if (cdar thread)
4746                                           (append gnus-tmp-new-adopts
4747                                                   (cdar thread))
4748                                         gnus-tmp-new-adopts)
4749                   thread-end t
4750                   gnus-tmp-header nil)
4751             (when (zerop gnus-tmp-level)
4752               (setq gnus-tmp-root-expunged t)))
4753            ;; Perhaps this article is to be marked as read?
4754            ((and gnus-summary-mark-below
4755                  (< (or (cdr (assq number gnus-newsgroup-scored))
4756                         default-score)
4757                     gnus-summary-mark-below)
4758                  ;; Don't touch sparse articles.
4759                  (not (gnus-summary-article-sparse-p number))
4760                  (not (gnus-summary-article-ancient-p number)))
4761             (setq gnus-newsgroup-unreads
4762                   (delq number gnus-newsgroup-unreads))
4763             (if gnus-newsgroup-auto-expire
4764                 (setq gnus-newsgroup-expirable
4765                       (gnus-add-to-sorted-list
4766                        gnus-newsgroup-expirable number))
4767               (push (cons number gnus-low-score-mark)
4768                     gnus-newsgroup-reads))))
4769
4770           (when gnus-tmp-header
4771             ;; We may have an old dummy line to output before this
4772             ;; article.
4773             (when (and gnus-tmp-dummy-line
4774                        (gnus-subject-equal
4775                         gnus-tmp-dummy-line
4776                         (mail-header-subject gnus-tmp-header)))
4777               (gnus-summary-insert-dummy-line
4778                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4779               (setq gnus-tmp-dummy-line nil))
4780
4781             ;; Compute the mark.
4782             (setq gnus-tmp-unread (gnus-article-mark number))
4783
4784             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4785                                   gnus-tmp-header gnus-tmp-level)
4786                   gnus-newsgroup-data)
4787
4788             ;; Actually insert the line.
4789             (setq
4790              gnus-tmp-subject-or-nil
4791              (cond
4792               ((and gnus-thread-ignore-subject
4793                     gnus-tmp-prev-subject
4794                     (not (string= gnus-tmp-prev-subject simp-subject)))
4795                subject)
4796               ((zerop gnus-tmp-level)
4797                (if (and (eq gnus-summary-make-false-root 'empty)
4798                         (memq number gnus-tmp-gathered)
4799                         gnus-tmp-prev-subject
4800                         (string= gnus-tmp-prev-subject simp-subject))
4801                    gnus-summary-same-subject
4802                  subject))
4803               (t gnus-summary-same-subject)))
4804             (if (and (eq gnus-summary-make-false-root 'adopt)
4805                      (= gnus-tmp-level 1)
4806                      (memq number gnus-tmp-gathered))
4807                 (setq gnus-tmp-opening-bracket ?\<
4808                       gnus-tmp-closing-bracket ?\>)
4809               (setq gnus-tmp-opening-bracket ?\[
4810                     gnus-tmp-closing-bracket ?\]))
4811             (setq
4812              gnus-tmp-indentation
4813              (aref gnus-thread-indent-array gnus-tmp-level)
4814              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4815              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4816                                 gnus-summary-default-score 0)
4817              gnus-tmp-score-char
4818              (if (or (null gnus-summary-default-score)
4819                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4820                          gnus-summary-zcore-fuzz))
4821                  ?                      ;Whitespace
4822                (if (< gnus-tmp-score gnus-summary-default-score)
4823                    gnus-score-below-mark gnus-score-over-mark))
4824              gnus-tmp-replied
4825              (cond ((memq number gnus-newsgroup-processable)
4826                     gnus-process-mark)
4827                    ((memq number gnus-newsgroup-cached)
4828                     gnus-cached-mark)
4829                    ((memq number gnus-newsgroup-replied)
4830                     gnus-replied-mark)
4831                    ((memq number gnus-newsgroup-forwarded)
4832                     gnus-forwarded-mark)
4833                    ((memq number gnus-newsgroup-saved)
4834                     gnus-saved-mark)
4835                    ((memq number gnus-newsgroup-recent)
4836                     gnus-recent-mark)
4837                    ((memq number gnus-newsgroup-unseen)
4838                     gnus-unseen-mark)
4839                    (t gnus-no-mark))
4840              gnus-tmp-downloaded
4841              (cond ((memq number gnus-newsgroup-undownloaded)
4842                     gnus-undownloaded-mark)
4843                    (gnus-newsgroup-agentized
4844                     gnus-downloaded-mark)
4845                    (t
4846                     gnus-no-mark))
4847              gnus-tmp-from (mail-header-from gnus-tmp-header)
4848              gnus-tmp-name
4849              (cond
4850               ((string-match "<[^>]+> *$" gnus-tmp-from)
4851                (setq beg-match (match-beginning 0))
4852                (or (and (string-match "^\".+\"" gnus-tmp-from)
4853                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4854                    (substring gnus-tmp-from 0 beg-match)))
4855               ((string-match "(.+)" gnus-tmp-from)
4856                (substring gnus-tmp-from
4857                           (1+ (match-beginning 0)) (1- (match-end 0))))
4858               (t gnus-tmp-from))
4859
4860              ;; Do the %B string
4861              gnus-tmp-thread-tree-header-string
4862              (cond
4863               ((not gnus-show-threads) "")
4864               ((zerop gnus-tmp-level)
4865                (cond ((cdar thread)
4866                       (or gnus-sum-thread-tree-root subject))
4867                      (gnus-tmp-new-adopts
4868                       (or gnus-sum-thread-tree-false-root subject))
4869                      (t
4870                       (or gnus-sum-thread-tree-single-indent subject))))
4871               (t
4872                (concat (apply 'concat
4873                               (mapcar (lambda (item)
4874                                         (if (= item 1)
4875                                             gnus-sum-thread-tree-vertical
4876                                           gnus-sum-thread-tree-indent))
4877                                       (cdr (reverse tree-stack))))
4878                        (if (nth 1 thread)
4879                            gnus-sum-thread-tree-leaf-with-other
4880                          gnus-sum-thread-tree-single-leaf)))))
4881             (when (string= gnus-tmp-name "")
4882               (setq gnus-tmp-name gnus-tmp-from))
4883             (unless (numberp gnus-tmp-lines)
4884               (setq gnus-tmp-lines -1))
4885             (if (= gnus-tmp-lines -1)
4886                 (setq gnus-tmp-lines "?")
4887               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4888               (gnus-put-text-property
4889              (point)
4890              (progn (eval gnus-summary-line-format-spec) (point))
4891                'gnus-number number)
4892             (when gnus-visual-p
4893               (forward-line -1)
4894               (gnus-run-hooks 'gnus-summary-update-hook)
4895               (forward-line 1))
4896
4897             (setq gnus-tmp-prev-subject simp-subject)))
4898
4899         (when (nth 1 thread)
4900           (push (list (max 0 gnus-tmp-level)
4901                       (copy-sequence tree-stack)
4902                       (nthcdr 1 thread))
4903                 stack))
4904         (push (if (nth 1 thread) 1 0) tree-stack)
4905         (incf gnus-tmp-level)
4906         (setq threads (if thread-end nil (cdar thread)))
4907         (if gnus-summary-display-while-building
4908             (if building-count
4909                 (progn
4910                   ;; use a set frequency
4911                   (setq building-line-count (1- building-line-count))
4912                   (when (= building-line-count 0)
4913                     (sit-for 0)
4914                     (setq building-line-count
4915                           gnus-summary-display-while-building)))
4916               ;; always
4917               (sit-for 0)))
4918         (unless threads
4919           (setq gnus-tmp-level 0)))))
4920   (gnus-message 7 "Generating summary...done"))
4921
4922 (defun gnus-summary-prepare-unthreaded (headers)
4923   "Generate an unthreaded summary buffer based on HEADERS."
4924   (let (header number mark)
4925
4926     (beginning-of-line)
4927
4928     (while headers
4929       ;; We may have to root out some bad articles...
4930       (when (memq (setq number (mail-header-number
4931                                 (setq header (pop headers))))
4932                   gnus-newsgroup-limit)
4933         ;; Mark article as read when it has a low score.
4934         (when (and gnus-summary-mark-below
4935                    (< (or (cdr (assq number gnus-newsgroup-scored))
4936                           gnus-summary-default-score 0)
4937                       gnus-summary-mark-below)
4938                    (not (gnus-summary-article-ancient-p number)))
4939           (setq gnus-newsgroup-unreads
4940                 (delq number gnus-newsgroup-unreads))
4941           (if gnus-newsgroup-auto-expire
4942               (push number gnus-newsgroup-expirable)
4943             (push (cons number gnus-low-score-mark)
4944                   gnus-newsgroup-reads)))
4945
4946         (setq mark (gnus-article-mark number))
4947         (push (gnus-data-make number mark (1+ (point)) header 0)
4948               gnus-newsgroup-data)
4949         (gnus-summary-insert-line
4950          header 0 number
4951          (memq number gnus-newsgroup-undownloaded)
4952          mark (memq number gnus-newsgroup-replied)
4953          (memq number gnus-newsgroup-expirable)
4954          (mail-header-subject header) nil
4955          (cdr (assq number gnus-newsgroup-scored))
4956          (memq number gnus-newsgroup-processable))))))
4957
4958 (defun gnus-summary-remove-list-identifiers ()
4959   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4960   (let ((regexp (if (consp gnus-list-identifiers)
4961                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4962                   gnus-list-identifiers))
4963         changed subject)
4964     (when regexp
4965       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
4966       (dolist (header gnus-newsgroup-headers)
4967         (setq subject (mail-header-subject header)
4968               changed nil)
4969         (while (string-match regexp subject)
4970           (setq subject
4971                 (concat (substring subject 0 (match-beginning 1))
4972                         (substring subject (match-end 0)))
4973                 changed t))
4974         (when changed
4975           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
4976             (setq subject
4977                   (concat (substring subject 0 (match-beginning 1))
4978                           (substring subject (match-end 1)))))
4979           (mail-header-set-subject header subject))))))
4980
4981 (defun gnus-fetch-headers (articles)
4982   "Fetch headers of ARTICLES."
4983   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4984     (gnus-message 5 "Fetching headers for %s..." name)
4985     (prog1
4986         (if (eq 'nov
4987                 (setq gnus-headers-retrieved-by
4988                       (gnus-retrieve-headers
4989                        articles gnus-newsgroup-name
4990                        ;; We might want to fetch old headers, but
4991                        ;; not if there is only 1 article.
4992                        (and (or (and
4993                                  (not (eq gnus-fetch-old-headers 'some))
4994                                  (not (numberp gnus-fetch-old-headers)))
4995                                 (> (length articles) 1))
4996                             gnus-fetch-old-headers))))
4997             (gnus-get-newsgroup-headers-xover
4998              articles nil nil gnus-newsgroup-name t)
4999           (gnus-get-newsgroup-headers))
5000       (gnus-message 5 "Fetching headers for %s...done" name))))
5001
5002 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5003   "Select newsgroup GROUP.
5004 If READ-ALL is non-nil, all articles in the group are selected.
5005 If SELECT-ARTICLES, only select those articles from GROUP."
5006   (let* ((entry (gnus-group-entry group))
5007          ;;!!! Dirty hack; should be removed.
5008          (gnus-summary-ignore-duplicates
5009           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5010               t
5011             gnus-summary-ignore-duplicates))
5012          (info (nth 2 entry))
5013          articles fetched-articles cached)
5014
5015     (unless (gnus-check-server
5016              (set (make-local-variable 'gnus-current-select-method)
5017                   (gnus-find-method-for-group group)))
5018       (error "Couldn't open server"))
5019
5020     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5021         (gnus-activate-group group)     ; Or we can activate it...
5022         (progn                          ; Or we bug out.
5023           (when (equal major-mode 'gnus-summary-mode)
5024             (gnus-kill-buffer (current-buffer)))
5025           (error "Couldn't activate group %s: %s"
5026                  group (gnus-status-message group))))
5027
5028     (unless (gnus-request-group group t)
5029       (when (equal major-mode 'gnus-summary-mode)
5030         (gnus-kill-buffer (current-buffer)))
5031       (error "Couldn't request group %s: %s"
5032              group (gnus-status-message group)))
5033
5034     (when gnus-agent
5035       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5036       
5037       (setq gnus-summary-use-undownloaded-faces
5038             (gnus-agent-find-parameter
5039              group
5040              'agent-enable-undownloaded-faces)))
5041
5042     (setq gnus-newsgroup-name group
5043           gnus-newsgroup-unselected nil
5044           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5045
5046     (let ((display (gnus-group-find-parameter group 'display)))
5047       (setq gnus-newsgroup-display
5048             (cond
5049              ((not (zerop (or (car-safe read-all) 0)))
5050               ;; The user entered the group with C-u SPC/RET, let's show
5051               ;; all articles.
5052               'gnus-not-ignore)
5053              ((eq display 'all)
5054               'gnus-not-ignore)
5055              ((arrayp display)
5056               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5057              ((numberp display)
5058               ;; The following is probably the "correct" solution, but
5059               ;; it makes Gnus fetch all headers and then limit the
5060               ;; articles (which is slow), so instead we hack the
5061               ;; select-articles parameter instead. -- Simon Josefsson
5062               ;; <jas@kth.se>
5063               ;;
5064               ;; (gnus-byte-compile
5065               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5066               ;;                         display)))))
5067               (setq select-articles
5068                     (gnus-uncompress-range
5069                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5070                              (if (> tmp 0)
5071                                  tmp
5072                                1))
5073                            (cdr (gnus-active group)))))
5074               nil)
5075              (t
5076               nil))))
5077
5078     (gnus-summary-setup-default-charset)
5079
5080     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5081     (when (gnus-virtual-group-p group)
5082       (setq cached gnus-newsgroup-cached))
5083
5084     (setq gnus-newsgroup-unreads
5085           (gnus-sorted-ndifference
5086            (gnus-sorted-ndifference gnus-newsgroup-unreads
5087                                     gnus-newsgroup-marked)
5088            gnus-newsgroup-dormant))
5089
5090     (setq gnus-newsgroup-processable nil)
5091
5092     (gnus-update-read-articles group gnus-newsgroup-unreads)
5093
5094     ;; Adjust and set lists of article marks.
5095     (when info
5096       (gnus-adjust-marked-articles info))
5097     (if (setq articles select-articles)
5098         (setq gnus-newsgroup-unselected
5099               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5100       (setq articles (gnus-articles-to-read group read-all)))
5101
5102     (cond
5103      ((null articles)
5104       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5105       'quit)
5106      ((eq articles 0) nil)
5107      (t
5108       ;; Init the dependencies hash table.
5109       (setq gnus-newsgroup-dependencies
5110             (gnus-make-hashtable (length articles)))
5111       (gnus-set-global-variables)
5112       ;; Retrieve the headers and read them in.
5113
5114       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5115
5116       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5117       (when cached
5118         (setq gnus-newsgroup-cached cached))
5119
5120       ;; Suppress duplicates?
5121       (when gnus-suppress-duplicates
5122         (gnus-dup-suppress-articles))
5123
5124       ;; Set the initial limit.
5125       (setq gnus-newsgroup-limit (copy-sequence articles))
5126       ;; Remove canceled articles from the list of unread articles.
5127       (setq fetched-articles
5128             (mapcar (lambda (headers) (mail-header-number headers))
5129                     gnus-newsgroup-headers))
5130       (setq gnus-newsgroup-articles fetched-articles)
5131       (setq gnus-newsgroup-unreads
5132             (gnus-sorted-nintersection
5133              gnus-newsgroup-unreads fetched-articles))
5134       (gnus-compute-unseen-list)
5135
5136       ;; Removed marked articles that do not exist.
5137       (gnus-update-missing-marks
5138        (gnus-sorted-difference articles fetched-articles))
5139       ;; We might want to build some more threads first.
5140       (when (and gnus-fetch-old-headers
5141                  (eq gnus-headers-retrieved-by 'nov))
5142         (if (eq gnus-fetch-old-headers 'invisible)
5143             (gnus-build-all-threads)
5144           (gnus-build-old-threads)))
5145       ;; Let the Gnus agent mark articles as read.
5146       (when gnus-agent
5147         (gnus-agent-get-undownloaded-list))
5148       ;; Remove list identifiers from subject
5149       (when gnus-list-identifiers
5150         (gnus-summary-remove-list-identifiers))
5151       ;; Check whether auto-expire is to be done in this group.
5152       (setq gnus-newsgroup-auto-expire
5153             (gnus-group-auto-expirable-p group))
5154       ;; Set up the article buffer now, if necessary.
5155       (unless gnus-single-article-buffer
5156         (gnus-article-setup-buffer))
5157       ;; First and last article in this newsgroup.
5158       (when gnus-newsgroup-headers
5159         (setq gnus-newsgroup-begin
5160               (mail-header-number (car gnus-newsgroup-headers))
5161               gnus-newsgroup-end
5162               (mail-header-number
5163                (gnus-last-element gnus-newsgroup-headers))))
5164       ;; GROUP is successfully selected.
5165       (or gnus-newsgroup-headers t)))))
5166
5167 (defun gnus-compute-unseen-list ()
5168   ;; The `seen' marks are treated specially.
5169   (if (not gnus-newsgroup-seen)
5170       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5171     (setq gnus-newsgroup-unseen
5172           (gnus-inverse-list-range-intersection
5173            gnus-newsgroup-articles gnus-newsgroup-seen))))
5174
5175 (defun gnus-summary-display-make-predicate (display)
5176   (require 'gnus-agent)
5177   (when (= (length display) 1)
5178     (setq display (car display)))
5179   (unless gnus-summary-display-cache
5180     (dolist (elem (append '((unread . unread)
5181                             (read . read)
5182                             (unseen . unseen))
5183                           gnus-article-mark-lists))
5184       (push (cons (cdr elem)
5185                   (gnus-byte-compile
5186                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5187             gnus-summary-display-cache)))
5188   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5189         (gnus-category-predicate-cache gnus-summary-display-cache))
5190     (gnus-get-predicate display)))
5191
5192 ;; Uses the dynamically bound `number' variable.
5193 (eval-when-compile
5194   (defvar number))
5195 (defun gnus-article-marked-p (type &optional article)
5196   (let ((article (or article number)))
5197     (cond
5198      ((eq type 'tick)
5199       (memq article gnus-newsgroup-marked))
5200      ((eq type 'spam)
5201       (memq article gnus-newsgroup-spam-marked))
5202      ((eq type 'unsend)
5203       (memq article gnus-newsgroup-unsendable))
5204      ((eq type 'undownload)
5205       (memq article gnus-newsgroup-undownloaded))
5206      ((eq type 'download)
5207       (memq article gnus-newsgroup-downloadable))
5208      ((eq type 'unread)
5209       (memq article gnus-newsgroup-unreads))
5210      ((eq type 'read)
5211       (memq article gnus-newsgroup-reads))
5212      ((eq type 'dormant)
5213       (memq article gnus-newsgroup-dormant) )
5214      ((eq type 'expire)
5215       (memq article gnus-newsgroup-expirable))
5216      ((eq type 'reply)
5217       (memq article gnus-newsgroup-replied))
5218      ((eq type 'killed)
5219       (memq article gnus-newsgroup-killed))
5220      ((eq type 'bookmark)
5221       (assq article gnus-newsgroup-bookmarks))
5222      ((eq type 'score)
5223       (assq article gnus-newsgroup-scored))
5224      ((eq type 'save)
5225       (memq article gnus-newsgroup-saved))
5226      ((eq type 'cache)
5227       (memq article gnus-newsgroup-cached))
5228      ((eq type 'forward)
5229       (memq article gnus-newsgroup-forwarded))
5230      ((eq type 'seen)
5231       (not (memq article gnus-newsgroup-unseen)))
5232      ((eq type 'recent)
5233       (memq article gnus-newsgroup-recent))
5234      (t t))))
5235
5236 (defun gnus-articles-to-read (group &optional read-all)
5237   "Find out what articles the user wants to read."
5238   (let* ((display (gnus-group-find-parameter group 'display))
5239          (articles
5240           ;; Select all articles if `read-all' is non-nil, or if there
5241           ;; are no unread articles.
5242           (if (or read-all
5243                   (and (zerop (length gnus-newsgroup-marked))
5244                        (zerop (length gnus-newsgroup-unreads)))
5245                   ;; Fetch all if the predicate is non-nil.
5246                   gnus-newsgroup-display)
5247               ;; We want to select the headers for all the articles in
5248               ;; the group, so we select either all the active
5249               ;; articles in the group, or (if that's nil), the
5250               ;; articles in the cache.
5251               (or
5252                (gnus-uncompress-range (gnus-active group))
5253                (gnus-cache-articles-in-group group))
5254             ;; Select only the "normal" subset of articles.
5255             (gnus-sorted-nunion
5256              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5257              gnus-newsgroup-unreads)))
5258          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5259          (scored (length scored-list))
5260          (number (length articles))
5261          (marked (+ (length gnus-newsgroup-marked)
5262                     (length gnus-newsgroup-dormant)))
5263          (select
5264           (cond
5265            ((numberp read-all)
5266             read-all)
5267            ((numberp gnus-newsgroup-display)
5268             gnus-newsgroup-display)
5269            (t
5270             (condition-case ()
5271                 (cond
5272                  ((and (or (<= scored marked) (= scored number))
5273                        (numberp gnus-large-newsgroup)
5274                        (> number gnus-large-newsgroup))
5275                   (let* ((cursor-in-echo-area nil)
5276                          (initial (gnus-parameter-large-newsgroup-initial
5277                                    gnus-newsgroup-name))
5278                          (input
5279                           (read-string
5280                            (format
5281                             "How many articles from %s (%s %d): "
5282                             (gnus-limit-string
5283                              (gnus-group-decoded-name gnus-newsgroup-name)
5284                              35)
5285                             (if initial "max" "default")
5286                             number)
5287                            (if initial
5288                                (cons (number-to-string initial)
5289                                      0)))))
5290                     (if (string-match "^[ \t]*$" input) number input)))
5291                  ((and (> scored marked) (< scored number)
5292                        (> (- scored number) 20))
5293                   (let ((input
5294                          (read-string
5295                           (format "%s %s (%d scored, %d total): "
5296                                   "How many articles from"
5297                                   (gnus-group-decoded-name group)
5298                                   scored number))))
5299                     (if (string-match "^[ \t]*$" input)
5300                         number input)))
5301                  (t number))
5302               (quit
5303                (message "Quit getting the articles to read")
5304                nil))))))
5305     (setq select (if (stringp select) (string-to-number select) select))
5306     (if (or (null select) (zerop select))
5307         select
5308       (if (and (not (zerop scored)) (<= (abs select) scored))
5309           (progn
5310             (setq articles (sort scored-list '<))
5311             (setq number (length articles)))
5312         (setq articles (copy-sequence articles)))
5313
5314       (when (< (abs select) number)
5315         (if (< select 0)
5316             ;; Select the N oldest articles.
5317             (setcdr (nthcdr (1- (abs select)) articles) nil)
5318           ;; Select the N most recent articles.
5319           (setq articles (nthcdr (- number select) articles))))
5320       (setq gnus-newsgroup-unselected
5321             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5322       (when gnus-alter-articles-to-read-function
5323         (setq articles
5324               (sort
5325                (funcall gnus-alter-articles-to-read-function
5326                         gnus-newsgroup-name articles)
5327                '<)))
5328       articles)))
5329
5330 (defun gnus-killed-articles (killed articles)
5331   (let (out)
5332     (while articles
5333       (when (inline (gnus-member-of-range (car articles) killed))
5334         (push (car articles) out))
5335       (setq articles (cdr articles)))
5336     out))
5337
5338 (defun gnus-uncompress-marks (marks)
5339   "Uncompress the mark ranges in MARKS."
5340   (let ((uncompressed '(score bookmark))
5341         out)
5342     (while marks
5343       (if (memq (caar marks) uncompressed)
5344           (push (car marks) out)
5345         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5346       (setq marks (cdr marks)))
5347     out))
5348
5349 (defun gnus-article-mark-to-type (mark)
5350   "Return the type of MARK."
5351   (or (cadr (assq mark gnus-article-special-mark-lists))
5352       'list))
5353
5354 (defun gnus-article-unpropagatable-p (mark)
5355   "Return whether MARK should be propagated to back end."
5356   (memq mark gnus-article-unpropagated-mark-lists))
5357
5358 (defun gnus-adjust-marked-articles (info)
5359   "Set all article lists and remove all marks that are no longer valid."
5360   (let* ((marked-lists (gnus-info-marks info))
5361          (active (gnus-active (gnus-info-group info)))
5362          (min (car active))
5363          (max (cdr active))
5364          (types gnus-article-mark-lists)
5365          marks var articles article mark mark-type p-articles
5366          bgn end)
5367
5368     (dolist (marks marked-lists)
5369       (setq mark (car marks)
5370             mark-type (gnus-article-mark-to-type mark)
5371             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5372
5373       ;; We set the variable according to the type of the marks list,
5374       ;; and then adjust the marks to a subset of the active articles.
5375       (cond
5376        ;; Adjust "simple" lists - compressed yet unsorted
5377        ((eq mark-type 'list)
5378         ;; Simultaneously uncompress and clip to active range
5379         (setq p-articles marks
5380               articles (cdr p-articles))
5381         (while (setq article (car articles))
5382           (when (cond ((consp article)
5383                        (setq bgn (max (car article) min)
5384                              end (min (cdr article) max))
5385                        (if (> bgn end)
5386                            t    ; range excluded - splice out of marks
5387                          (setcar articles bgn) ; First value replaces range.
5388                          (setq bgn (1+ bgn))
5389                          (while (<= bgn end)
5390                            (setq articles (setcdr articles (cons bgn (cdr articles)))
5391                                  bgn (1+ bgn)))
5392                          (setq p-articles articles
5393                                articles (cdr articles))
5394                          nil))
5395                       ((or (< article min)
5396                            (> article max))
5397                        t        ; value excluded - splice out of marks
5398                        )
5399                       (t
5400                        (setq p-articles articles
5401                              articles (cdr articles))
5402                        nil))
5403             ;; perform slice to remove article
5404             (if (setcar articles (cadr articles))
5405                 (setcdr articles (cddr articles))
5406               (setcdr p-articles nil))))
5407         (set var (cdr marks)))
5408        ;; Adjust assocs.
5409        ((eq mark-type 'tuple)
5410         (set var (setq articles (cdr marks)))
5411         (when (not (listp (cdr (symbol-value var))))
5412           (set var (list (symbol-value var))))
5413         (when (not (listp (cdr articles)))
5414           (setq articles (list articles)))
5415         (while articles
5416           (when (or (not (consp (setq article (pop articles))))
5417                     (< (car article) min)
5418                     (> (car article) max))
5419             (set var (delq article (symbol-value var))))))
5420        ;; Adjust ranges (sloppily).
5421        ((eq mark-type 'range)
5422         (cond
5423          ((eq mark 'seen)
5424           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5425           ;; It should be (seen (NUM1 . NUM2)).
5426           (when (numberp (cddr marks))
5427             (setcdr marks (list (cdr marks))))
5428           (setq articles (cdr marks))
5429           (while (and articles
5430                       (or (and (consp (car articles))
5431                                (> min (cdar articles)))
5432                           (and (numberp (car articles))
5433                                (> min (car articles)))))
5434             (pop articles))
5435           (set var articles))))))))
5436
5437 (defun gnus-update-missing-marks (missing)
5438   "Go through the list of MISSING articles and remove them from the mark lists."
5439   (when missing
5440     (let (var m)
5441       ;; Go through all types.
5442       (dolist (elem gnus-article-mark-lists)
5443         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5444           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5445           (when (symbol-value var)
5446             ;; This list has articles.  So we delete all missing
5447             ;; articles from it.
5448             (setq m missing)
5449             (while m
5450               (set var (delq (pop m) (symbol-value var))))))))))
5451
5452 (defun gnus-update-marks ()
5453   "Enter the various lists of marked articles into the newsgroup info list."
5454   (let ((types gnus-article-mark-lists)
5455         (info (gnus-get-info gnus-newsgroup-name))
5456         type list newmarked symbol delta-marks)
5457     (when info
5458       ;; Add all marks lists to the list of marks lists.
5459       (while (setq type (pop types))
5460         (setq list (symbol-value
5461                     (setq symbol
5462                           (intern (format "gnus-newsgroup-%s" (car type))))))
5463
5464         (when list
5465           ;; Get rid of the entries of the articles that have the
5466           ;; default score.
5467           (when (and (eq (cdr type) 'score)
5468                      gnus-save-score
5469                      list)
5470             (let* ((arts list)
5471                    (prev (cons nil list))
5472                    (all prev))
5473               (while arts
5474                 (if (or (not (consp (car arts)))
5475                         (= (cdar arts) gnus-summary-default-score))
5476                     (setcdr prev (cdr arts))
5477                   (setq prev arts))
5478                 (setq arts (cdr arts)))
5479               (setq list (cdr all)))))
5480
5481         (when (eq (cdr type) 'seen)
5482           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5483
5484         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5485           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5486
5487         (when (and (gnus-check-backend-function
5488                     'request-set-mark gnus-newsgroup-name)
5489                    (not (gnus-article-unpropagatable-p (cdr type))))
5490           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5491                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5492                  (add (gnus-remove-from-range
5493                        (gnus-copy-sequence list) old)))
5494             (when add
5495               (push (list add 'add (list (cdr type))) delta-marks))
5496             (when del
5497               (push (list del 'del (list (cdr type))) delta-marks))))
5498
5499         (when list
5500           (push (cons (cdr type) list) newmarked)))
5501
5502       (when delta-marks
5503         (unless (gnus-check-group gnus-newsgroup-name)
5504           (error "Can't open server for %s" gnus-newsgroup-name))
5505         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5506
5507       ;; Enter these new marks into the info of the group.
5508       (if (nthcdr 3 info)
5509           (setcar (nthcdr 3 info) newmarked)
5510         ;; Add the marks lists to the end of the info.
5511         (when newmarked
5512           (setcdr (nthcdr 2 info) (list newmarked))))
5513
5514       ;; Cut off the end of the info if there's nothing else there.
5515       (let ((i 5))
5516         (while (and (> i 2)
5517                     (not (nth i info)))
5518           (when (nthcdr (decf i) info)
5519             (setcdr (nthcdr i info) nil)))))))
5520
5521 (defun gnus-set-mode-line (where)
5522   "Set the mode line of the article or summary buffers.
5523 If WHERE is `summary', the summary mode line format will be used."
5524   ;; Is this mode line one we keep updated?
5525   (when (and (memq where gnus-updated-mode-lines)
5526              (symbol-value
5527               (intern (format "gnus-%s-mode-line-format-spec" where))))
5528     (let (mode-string)
5529       (save-excursion
5530         ;; We evaluate this in the summary buffer since these
5531         ;; variables are buffer-local to that buffer.
5532         (set-buffer gnus-summary-buffer)
5533        ;; We bind all these variables that are used in the `eval' form
5534         ;; below.
5535         (let* ((mformat (symbol-value
5536                          (intern
5537                           (format "gnus-%s-mode-line-format-spec" where))))
5538                (gnus-tmp-group-name (gnus-group-decoded-name
5539                                      gnus-newsgroup-name))
5540                (gnus-tmp-article-number (or gnus-current-article 0))
5541                (gnus-tmp-unread gnus-newsgroup-unreads)
5542                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5543                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5544                (gnus-tmp-unread-and-unselected
5545                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5546                             (zerop gnus-tmp-unselected))
5547                        "")
5548                       ((zerop gnus-tmp-unselected)
5549                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5550                       (t (format "{%d(+%d) more}"
5551                                  gnus-tmp-unread-and-unticked
5552                                  gnus-tmp-unselected))))
5553                (gnus-tmp-subject
5554                 (if (and gnus-current-headers
5555                          (vectorp gnus-current-headers))
5556                     (gnus-mode-string-quote
5557                      (mail-header-subject gnus-current-headers))
5558                   ""))
5559                bufname-length max-len
5560                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5561           (setq mode-string (eval mformat))
5562           (setq bufname-length (if (string-match "%b" mode-string)
5563                                    (- (length
5564                                        (buffer-name
5565                                         (if (eq where 'summary)
5566                                             nil
5567                                           (get-buffer gnus-article-buffer))))
5568                                       2)
5569                                  0))
5570           (setq max-len (max 4 (if gnus-mode-non-string-length
5571                                    (- (window-width)
5572                                       gnus-mode-non-string-length
5573                                       bufname-length)
5574                                  (length mode-string))))
5575           ;; We might have to chop a bit of the string off...
5576           (when (> (length mode-string) max-len)
5577             (setq mode-string
5578                   (concat (truncate-string-to-width mode-string (- max-len 3))
5579                           "...")))
5580           ;; Pad the mode string a bit.
5581           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5582       ;; Update the mode line.
5583       (setq mode-line-buffer-identification
5584             (gnus-mode-line-buffer-identification (list mode-string)))
5585       (set-buffer-modified-p t))))
5586
5587 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5588   "Go through the HEADERS list and add all Xrefs to a hash table.
5589 The resulting hash table is returned, or nil if no Xrefs were found."
5590   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5591          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5592          (xref-hashtb (gnus-make-hashtable))
5593          start group entry number xrefs header)
5594     (while headers
5595       (setq header (pop headers))
5596       (when (and (setq xrefs (mail-header-xref header))
5597                  (not (memq (setq number (mail-header-number header))
5598                             unreads)))
5599         (setq start 0)
5600         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5601           (setq start (match-end 0))
5602           (setq group (if prefix
5603                           (concat prefix (substring xrefs (match-beginning 1)
5604                                                     (match-end 1)))
5605                         (substring xrefs (match-beginning 1) (match-end 1))))
5606           (setq number
5607                 (string-to-int (substring xrefs (match-beginning 2)
5608                                           (match-end 2))))
5609           (if (setq entry (gnus-gethash group xref-hashtb))
5610               (setcdr entry (cons number (cdr entry)))
5611             (gnus-sethash group (cons number nil) xref-hashtb)))))
5612     (and start xref-hashtb)))
5613
5614 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5615   "Look through all the headers and mark the Xrefs as read."
5616   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5617         name info xref-hashtb idlist method nth4)
5618     (save-excursion
5619       (set-buffer gnus-group-buffer)
5620       (when (setq xref-hashtb
5621                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5622         (mapatoms
5623          (lambda (group)
5624            (unless (string= from-newsgroup (setq name (symbol-name group)))
5625              (setq idlist (symbol-value group))
5626              ;; Dead groups are not updated.
5627              (and (prog1
5628                       (setq info (gnus-get-info name))
5629                     (when (stringp (setq nth4 (gnus-info-method info)))
5630                       (setq nth4 (gnus-server-to-method nth4))))
5631                   ;; Only do the xrefs if the group has the same
5632                   ;; select method as the group we have just read.
5633                   (or (gnus-methods-equal-p
5634                        nth4 (gnus-find-method-for-group from-newsgroup))
5635                       virtual
5636                       (equal nth4 (setq method (gnus-find-method-for-group
5637                                                 from-newsgroup)))
5638                       (and (equal (car nth4) (car method))
5639                            (equal (nth 1 nth4) (nth 1 method))))
5640                   gnus-use-cross-reference
5641                   (or (not (eq gnus-use-cross-reference t))
5642                       virtual
5643                       ;; Only do cross-references on subscribed
5644                       ;; groups, if that is what is wanted.
5645                       (<= (gnus-info-level info) gnus-level-subscribed))
5646                   (gnus-group-make-articles-read name idlist))))
5647          xref-hashtb)))))
5648
5649 (defun gnus-compute-read-articles (group articles)
5650   (let* ((entry (gnus-group-entry group))
5651          (info (nth 2 entry))
5652          (active (gnus-active group))
5653          ninfo)
5654     (when entry
5655       ;; First peel off all invalid article numbers.
5656       (when active
5657         (let ((ids articles)
5658               id first)
5659           (while (setq id (pop ids))
5660             (when (and first (> id (cdr active)))
5661               ;; We'll end up in this situation in one particular
5662               ;; obscure situation.  If you re-scan a group and get
5663               ;; a new article that is cross-posted to a different
5664               ;; group that has not been re-scanned, you might get
5665               ;; crossposted article that has a higher number than
5666               ;; Gnus believes possible.  So we re-activate this
5667               ;; group as well.  This might mean doing the
5668               ;; crossposting thingy will *increase* the number
5669               ;; of articles in some groups.  Tsk, tsk.
5670               (setq active (or (gnus-activate-group group) active)))
5671             (when (or (> id (cdr active))
5672                       (< id (car active)))
5673               (setq articles (delq id articles))))))
5674       ;; If the read list is nil, we init it.
5675       (if (and active
5676                (null (gnus-info-read info))
5677                (> (car active) 1))
5678           (setq ninfo (cons 1 (1- (car active))))
5679         (setq ninfo (gnus-info-read info)))
5680       ;; Then we add the read articles to the range.
5681       (gnus-add-to-range
5682        ninfo (setq articles (sort articles '<))))))
5683
5684 (defun gnus-group-make-articles-read (group articles)
5685   "Update the info of GROUP to say that ARTICLES are read."
5686   (let* ((num 0)
5687          (entry (gnus-group-entry group))
5688          (info (nth 2 entry))
5689          (active (gnus-active group))
5690          range)
5691     (when entry
5692       (setq range (gnus-compute-read-articles group articles))
5693       (with-current-buffer gnus-group-buffer
5694         (gnus-undo-register
5695           `(progn
5696              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5697              (gnus-info-set-read ',info ',(gnus-info-read info))
5698              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5699              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5700              (gnus-group-update-group ,group t))))
5701       ;; Add the read articles to the range.
5702       (gnus-info-set-read info range)
5703       (gnus-request-set-mark group (list (list range 'add '(read))))
5704       ;; Then we have to re-compute how many unread
5705       ;; articles there are in this group.
5706       (when active
5707         (cond
5708          ((not range)
5709           (setq num (- (1+ (cdr active)) (car active))))
5710          ((not (listp (cdr range)))
5711           (setq num (- (cdr active) (- (1+ (cdr range))
5712                                        (car range)))))
5713          (t
5714           (while range
5715             (if (numberp (car range))
5716                 (setq num (1+ num))
5717               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5718             (setq range (cdr range)))
5719           (setq num (- (cdr active) num))))
5720         ;; Update the number of unread articles.
5721         (setcar entry num)
5722         ;; Update the group buffer.
5723         (unless (gnus-ephemeral-group-p group)
5724           (gnus-group-update-group group t))))))
5725
5726 (defvar gnus-newsgroup-none-id 0)
5727
5728 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5729   (let ((cur nntp-server-buffer)
5730         (dependencies
5731          (or dependencies
5732              (save-excursion (set-buffer gnus-summary-buffer)
5733                              gnus-newsgroup-dependencies)))
5734         headers id end ref
5735         (mail-parse-charset gnus-newsgroup-charset)
5736         (mail-parse-ignored-charsets
5737          (save-excursion (condition-case nil
5738                              (set-buffer gnus-summary-buffer)
5739                            (error))
5740                          gnus-newsgroup-ignored-charsets)))
5741     (save-excursion
5742       (set-buffer nntp-server-buffer)
5743       ;; Translate all TAB characters into SPACE characters.
5744       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5745       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5746       (ietf-drums-unfold-fws)
5747       (gnus-run-hooks 'gnus-parse-headers-hook)
5748       (let ((case-fold-search t)
5749             in-reply-to header p lines chars)
5750         (goto-char (point-min))
5751         ;; Search to the beginning of the next header.  Error messages
5752         ;; do not begin with 2 or 3.
5753         (while (re-search-forward "^[23][0-9]+ " nil t)
5754           (setq id nil
5755                 ref nil)
5756           ;; This implementation of this function, with nine
5757           ;; search-forwards instead of the one re-search-forward and
5758           ;; a case (which basically was the old function) is actually
5759           ;; about twice as fast, even though it looks messier.  You
5760           ;; can't have everything, I guess.  Speed and elegance
5761           ;; doesn't always go hand in hand.
5762           (setq
5763            header
5764            (vector
5765             ;; Number.
5766             (prog1
5767                 (read cur)
5768               (end-of-line)
5769               (setq p (point))
5770               (narrow-to-region (point)
5771                                 (or (and (search-forward "\n.\n" nil t)
5772                                          (- (point) 2))
5773                                     (point))))
5774             ;; Subject.
5775             (progn
5776               (goto-char p)
5777               (if (search-forward "\nsubject:" nil t)
5778                   (funcall gnus-decode-encoded-word-function
5779                            (nnheader-header-value))
5780                 "(none)"))
5781             ;; From.
5782             (progn
5783               (goto-char p)
5784               (if (search-forward "\nfrom:" nil t)
5785                   (funcall gnus-decode-encoded-word-function
5786                            (nnheader-header-value))
5787                 "(nobody)"))
5788             ;; Date.
5789             (progn
5790               (goto-char p)
5791               (if (search-forward "\ndate:" nil t)
5792                   (nnheader-header-value) ""))
5793             ;; Message-ID.
5794             (progn
5795               (goto-char p)
5796               (setq id (if (re-search-forward
5797                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5798                            ;; We do it this way to make sure the Message-ID
5799                            ;; is (somewhat) syntactically valid.
5800                            (buffer-substring (match-beginning 1)
5801                                              (match-end 1))
5802                          ;; If there was no message-id, we just fake one
5803                          ;; to make subsequent routines simpler.
5804                          (nnheader-generate-fake-message-id))))
5805             ;; References.
5806             (progn
5807               (goto-char p)
5808               (if (search-forward "\nreferences:" nil t)
5809                   (progn
5810                     (setq end (point))
5811                     (prog1
5812                         (nnheader-header-value)
5813                       (setq ref
5814                             (buffer-substring
5815                              (progn
5816                                (end-of-line)
5817                                (search-backward ">" end t)
5818                                (1+ (point)))
5819                              (progn
5820                                (search-backward "<" end t)
5821                                (point))))))
5822                 ;; Get the references from the in-reply-to header if there
5823                 ;; were no references and the in-reply-to header looks
5824                 ;; promising.
5825                 (if (and (search-forward "\nin-reply-to:" nil t)
5826                          (setq in-reply-to (nnheader-header-value))
5827                          (string-match "<[^>]+>" in-reply-to))
5828                     (let (ref2)
5829                       (setq ref (substring in-reply-to (match-beginning 0)
5830                                            (match-end 0)))
5831                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5832                         (setq ref2 (substring in-reply-to (match-beginning 0)
5833                                               (match-end 0)))
5834                         (when (> (length ref2) (length ref))
5835                           (setq ref ref2)))
5836                       ref)
5837                   (setq ref nil))))
5838             ;; Chars.
5839             (progn
5840               (goto-char p)
5841               (if (search-forward "\nchars: " nil t)
5842                   (if (numberp (setq chars (ignore-errors (read cur))))
5843                       chars -1)
5844                 -1))
5845             ;; Lines.
5846             (progn
5847               (goto-char p)
5848               (if (search-forward "\nlines: " nil t)
5849                   (if (numberp (setq lines (ignore-errors (read cur))))
5850                       lines -1)
5851                 -1))
5852             ;; Xref.
5853             (progn
5854               (goto-char p)
5855               (and (search-forward "\nxref:" nil t)
5856                    (nnheader-header-value)))
5857             ;; Extra.
5858             (when gnus-extra-headers
5859               (let ((extra gnus-extra-headers)
5860                     out)
5861                 (while extra
5862                   (goto-char p)
5863                   (when (search-forward
5864                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5865                     (push (cons (car extra) (nnheader-header-value))
5866                           out))
5867                   (pop extra))
5868                 out))))
5869           (when (equal id ref)
5870             (setq ref nil))
5871
5872           (when gnus-alter-header-function
5873             (funcall gnus-alter-header-function header)
5874             (setq id (mail-header-id header)
5875                   ref (gnus-parent-id (mail-header-references header))))
5876
5877           (when (setq header
5878                       (gnus-dependencies-add-header
5879                        header dependencies force-new))
5880             (push header headers))
5881           (goto-char (point-max))
5882           (widen))
5883         (nreverse headers)))))
5884
5885 ;; Goes through the xover lines and returns a list of vectors
5886 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5887                                                   force-new dependencies
5888                                                   group also-fetch-heads)
5889   "Parse the news overview data in the server buffer.
5890 Return a list of headers that match SEQUENCE (see
5891 `nntp-retrieve-headers')."
5892   ;; Get the Xref when the users reads the articles since most/some
5893   ;; NNTP servers do not include Xrefs when using XOVER.
5894   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5895   (let ((mail-parse-charset gnus-newsgroup-charset)
5896         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5897         (cur nntp-server-buffer)
5898         (dependencies (or dependencies gnus-newsgroup-dependencies))
5899         (allp (cond
5900                ((eq gnus-read-all-available-headers t)
5901                 t)
5902                ((stringp gnus-read-all-available-headers)
5903                 (string-match gnus-read-all-available-headers group))
5904                (t
5905                 nil)))
5906         number headers header)
5907     (save-excursion
5908       (set-buffer nntp-server-buffer)
5909       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5910       ;; Allow the user to mangle the headers before parsing them.
5911       (gnus-run-hooks 'gnus-parse-headers-hook)
5912       (goto-char (point-min))
5913       (gnus-parse-without-error
5914         (while (and (or sequence allp)
5915                     (not (eobp)))
5916           (setq number (read cur))
5917           (when (not allp)
5918             (while (and sequence
5919                         (< (car sequence) number))
5920               (setq sequence (cdr sequence))))
5921           (when (and (or allp
5922                          (and sequence
5923                               (eq number (car sequence))))
5924                      (progn
5925                        (setq sequence (cdr sequence))
5926                        (setq header (inline
5927                                       (gnus-nov-parse-line
5928                                        number dependencies force-new)))))
5929             (push header headers))
5930           (forward-line 1)))
5931       ;; A common bug in inn is that if you have posted an article and
5932       ;; then retrieves the active file, it will answer correctly --
5933       ;; the new article is included.  However, a NOV entry for the
5934       ;; article may not have been generated yet, so this may fail.
5935       ;; We work around this problem by retrieving the last few
5936       ;; headers using HEAD.
5937       (if (or (not also-fetch-heads)
5938               (not sequence))
5939           ;; We (probably) got all the headers.
5940           (nreverse headers)
5941         (let ((gnus-nov-is-evil t))
5942           (nconc
5943            (nreverse headers)
5944            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5945              (gnus-get-newsgroup-headers))))))))
5946
5947 (defun gnus-article-get-xrefs ()
5948   "Fill in the Xref value in `gnus-current-headers', if necessary.
5949 This is meant to be called in `gnus-article-internal-prepare-hook'."
5950   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5951                                  gnus-current-headers)))
5952     (or (not gnus-use-cross-reference)
5953         (not headers)
5954         (and (mail-header-xref headers)
5955              (not (string= (mail-header-xref headers) "")))
5956         (let ((case-fold-search t)
5957               xref)
5958           (save-restriction
5959             (nnheader-narrow-to-headers)
5960             (goto-char (point-min))
5961             (when (or (and (not (eobp))
5962                            (eq (downcase (char-after)) ?x)
5963                            (looking-at "Xref:"))
5964                       (search-forward "\nXref:" nil t))
5965               (goto-char (1+ (match-end 0)))
5966               (setq xref (buffer-substring (point) (point-at-eol)))
5967               (mail-header-set-xref headers xref)))))))
5968
5969 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5970   "Find article ID and insert the summary line for that article.
5971 OLD-HEADER can either be a header or a line number to insert
5972 the subject line on."
5973   (let* ((line (and (numberp old-header) old-header))
5974          (old-header (and (vectorp old-header) old-header))
5975          (header (cond ((and old-header use-old-header)
5976                         old-header)
5977                        ((and (numberp id)
5978                              (gnus-number-to-header id))
5979                         (gnus-number-to-header id))
5980                        (t
5981                         (gnus-read-header id))))
5982          (number (and (numberp id) id))
5983          d)
5984     (when header
5985       ;; Rebuild the thread that this article is part of and go to the
5986       ;; article we have fetched.
5987       (when (and (not gnus-show-threads)
5988                  old-header)
5989         (when (and number
5990                    (setq d (gnus-data-find (mail-header-number old-header))))
5991           (goto-char (gnus-data-pos d))
5992           (gnus-data-remove
5993            number
5994            (- (point-at-bol)
5995               (prog1
5996                   (1+ (point-at-eol))
5997                 (gnus-delete-line))))))
5998       (when old-header
5999         (mail-header-set-number header (mail-header-number old-header)))
6000       (setq gnus-newsgroup-sparse
6001             (delq (setq number (mail-header-number header))
6002                   gnus-newsgroup-sparse))
6003       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6004       (push number gnus-newsgroup-limit)
6005       (gnus-rebuild-thread (mail-header-id header) line)
6006       (gnus-summary-goto-subject number nil t))
6007     (when (and (numberp number)
6008                (> number 0))
6009       ;; We have to update the boundaries even if we can't fetch the
6010       ;; article if ID is a number -- so that the next `P' or `N'
6011       ;; command will fetch the previous (or next) article even
6012       ;; if the one we tried to fetch this time has been canceled.
6013       (when (> number gnus-newsgroup-end)
6014         (setq gnus-newsgroup-end number))
6015       (when (< number gnus-newsgroup-begin)
6016         (setq gnus-newsgroup-begin number))
6017       (setq gnus-newsgroup-unselected
6018             (delq number gnus-newsgroup-unselected)))
6019     ;; Report back a success?
6020     (and header (mail-header-number header))))
6021
6022 ;;; Process/prefix in the summary buffer
6023
6024 (defun gnus-summary-work-articles (n)
6025   "Return a list of articles to be worked upon.
6026 The prefix argument, the list of process marked articles, and the
6027 current article will be taken into consideration."
6028   (save-excursion
6029     (set-buffer gnus-summary-buffer)
6030     (cond
6031      (n
6032       ;; A numerical prefix has been given.
6033       (setq n (prefix-numeric-value n))
6034       (let ((backward (< n 0))
6035             (n (abs (prefix-numeric-value n)))
6036             articles article)
6037         (save-excursion
6038           (while
6039               (and (> n 0)
6040                    (push (setq article (gnus-summary-article-number))
6041                          articles)
6042                    (if backward
6043                        (gnus-summary-find-prev nil article)
6044                      (gnus-summary-find-next nil article)))
6045             (decf n)))
6046         (nreverse articles)))
6047      ((and (gnus-region-active-p) (mark))
6048       (message "region active")
6049       ;; Work on the region between point and mark.
6050       (let ((max (max (point) (mark)))
6051             articles article)
6052         (save-excursion
6053           (goto-char (min (min (point) (mark))))
6054           (while
6055               (and
6056                (push (setq article (gnus-summary-article-number)) articles)
6057                (gnus-summary-find-next nil article)
6058                (< (point) max)))
6059           (nreverse articles))))
6060      (gnus-newsgroup-processable
6061       ;; There are process-marked articles present.
6062       ;; Save current state.
6063       (gnus-summary-save-process-mark)
6064       ;; Return the list.
6065       (reverse gnus-newsgroup-processable))
6066      (t
6067       ;; Just return the current article.
6068       (list (gnus-summary-article-number))))))
6069
6070 (defmacro gnus-summary-iterate (arg &rest forms)
6071   "Iterate over the process/prefixed articles and do FORMS.
6072 ARG is the interactive prefix given to the command.  FORMS will be
6073 executed with point over the summary line of the articles."
6074   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6075     `(let ((,articles (gnus-summary-work-articles ,arg)))
6076        (while ,articles
6077          (gnus-summary-goto-subject (car ,articles))
6078          ,@forms
6079          (pop ,articles)))))
6080
6081 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6082 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6083
6084 (defun gnus-summary-save-process-mark ()
6085   "Push the current set of process marked articles on the stack."
6086   (interactive)
6087   (push (copy-sequence gnus-newsgroup-processable)
6088         gnus-newsgroup-process-stack))
6089
6090 (defun gnus-summary-kill-process-mark ()
6091   "Push the current set of process marked articles on the stack and unmark."
6092   (interactive)
6093   (gnus-summary-save-process-mark)
6094   (gnus-summary-unmark-all-processable))
6095
6096 (defun gnus-summary-yank-process-mark ()
6097   "Pop the last process mark state off the stack and restore it."
6098   (interactive)
6099   (unless gnus-newsgroup-process-stack
6100     (error "Empty mark stack"))
6101   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6102
6103 (defun gnus-summary-process-mark-set (set)
6104   "Make SET into the current process marked articles."
6105   (gnus-summary-unmark-all-processable)
6106   (while set
6107     (gnus-summary-set-process-mark (pop set))))
6108
6109 ;;; Searching and stuff
6110
6111 (defun gnus-summary-search-group (&optional backward use-level)
6112   "Search for next unread newsgroup.
6113 If optional argument BACKWARD is non-nil, search backward instead."
6114   (save-excursion
6115     (set-buffer gnus-group-buffer)
6116     (when (gnus-group-search-forward
6117            backward nil (if use-level (gnus-group-group-level) nil))
6118       (gnus-group-group-name))))
6119
6120 (defun gnus-summary-best-group (&optional exclude-group)
6121   "Find the name of the best unread group.
6122 If EXCLUDE-GROUP, do not go to this group."
6123   (save-excursion
6124     (set-buffer gnus-group-buffer)
6125     (save-excursion
6126       (gnus-group-best-unread-group exclude-group))))
6127
6128 (defun gnus-summary-find-next (&optional unread article backward)
6129   (if backward
6130       (gnus-summary-find-prev unread article)
6131     (let* ((dummy (gnus-summary-article-intangible-p))
6132            (article (or article (gnus-summary-article-number)))
6133            (data (gnus-data-find-list article))
6134            result)
6135       (when (and (not dummy)
6136                  (or (not gnus-summary-check-current)
6137                      (not unread)
6138                      (not (gnus-data-unread-p (car data)))))
6139         (setq data (cdr data)))
6140       (when (setq result
6141                   (if unread
6142                       (progn
6143                         (while data
6144                           (unless (memq (gnus-data-number (car data))
6145                                         (cond
6146                                          ((eq gnus-auto-goto-ignores
6147                                               'always-undownloaded)
6148                                           gnus-newsgroup-undownloaded)
6149                                          (gnus-plugged
6150                                           nil)
6151                                          ((eq gnus-auto-goto-ignores
6152                                               'unfetched)
6153                                           gnus-newsgroup-unfetched)
6154                                          ((eq gnus-auto-goto-ignores
6155                                               'undownloaded)
6156                                           gnus-newsgroup-undownloaded)))
6157                             (when (gnus-data-unread-p (car data))
6158                               (setq result (car data)
6159                                     data nil)))
6160                           (setq data (cdr data)))
6161                         result)
6162                     (car data)))
6163         (goto-char (gnus-data-pos result))
6164         (gnus-data-number result)))))
6165
6166 (defun gnus-summary-find-prev (&optional unread article)
6167   (let* ((eobp (eobp))
6168          (article (or article (gnus-summary-article-number)))
6169          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6170          result)
6171     (when (and (not eobp)
6172                (or (not gnus-summary-check-current)
6173                    (not unread)
6174                    (not (gnus-data-unread-p (car data)))))
6175       (setq data (cdr data)))
6176     (when (setq result
6177                 (if unread
6178                     (progn
6179                       (while data
6180                         (unless (memq (gnus-data-number (car data))
6181                                       (cond
6182                                        ((eq gnus-auto-goto-ignores
6183                                             'always-undownloaded)
6184                                         gnus-newsgroup-undownloaded)
6185                                        (gnus-plugged
6186                                         nil)
6187                                        ((eq gnus-auto-goto-ignores
6188                                             'unfetched)
6189                                         gnus-newsgroup-unfetched)
6190                                        ((eq gnus-auto-goto-ignores
6191                                             'undownloaded)
6192                                         gnus-newsgroup-undownloaded)))
6193                           (when (gnus-data-unread-p (car data))
6194                             (setq result (car data)
6195                                   data nil)))
6196                         (setq data (cdr data)))
6197                       result)
6198                   (car data)))
6199       (goto-char (gnus-data-pos result))
6200       (gnus-data-number result))))
6201
6202 (defun gnus-summary-find-subject (subject &optional unread backward article)
6203   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6204          (article (or article (gnus-summary-article-number)))
6205          (articles (gnus-data-list backward))
6206          (arts (gnus-data-find-list article articles))
6207          result)
6208     (when (or (not gnus-summary-check-current)
6209               (not unread)
6210               (not (gnus-data-unread-p (car arts))))
6211       (setq arts (cdr arts)))
6212     (while arts
6213       (and (or (not unread)
6214                (gnus-data-unread-p (car arts)))
6215            (vectorp (gnus-data-header (car arts)))
6216            (gnus-subject-equal
6217             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6218            (setq result (car arts)
6219                  arts nil))
6220       (setq arts (cdr arts)))
6221     (and result
6222          (goto-char (gnus-data-pos result))
6223          (gnus-data-number result))))
6224
6225 (defun gnus-summary-search-forward (&optional unread subject backward)
6226   "Search forward for an article.
6227 If UNREAD, look for unread articles.  If SUBJECT, look for
6228 articles with that subject.  If BACKWARD, search backward instead."
6229   (cond (subject (gnus-summary-find-subject subject unread backward))
6230         (backward (gnus-summary-find-prev unread))
6231         (t (gnus-summary-find-next unread))))
6232
6233 (defun gnus-recenter (&optional n)
6234   "Center point in window and redisplay frame.
6235 Also do horizontal recentering."
6236   (interactive "P")
6237   (when (and gnus-auto-center-summary
6238              (not (eq gnus-auto-center-summary 'vertical)))
6239     (gnus-horizontal-recenter))
6240   (recenter n))
6241
6242 (defun gnus-summary-recenter ()
6243   "Center point in the summary window.
6244 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6245 displayed, no centering will be performed."
6246   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6247   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6248   (interactive)
6249   ;; The user has to want it.
6250   (when gnus-auto-center-summary
6251     (let* ((top (cond ((< (window-height) 4) 0)
6252                       ((< (window-height) 7) 1)
6253                       (t (if (numberp gnus-auto-center-summary)
6254                              gnus-auto-center-summary
6255                            2))))
6256            (height (1- (window-height)))
6257            (bottom (save-excursion (goto-char (point-max))
6258                                    (forward-line (- height))
6259                                    (point)))
6260            (window (get-buffer-window (current-buffer))))
6261       (when (get-buffer-window gnus-article-buffer)
6262         ;; Only do recentering when the article buffer is displayed,
6263         ;; Set the window start to either `bottom', which is the biggest
6264         ;; possible valid number, or the second line from the top,
6265         ;; whichever is the least.
6266         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6267           (if (> bottom top-pos)
6268               ;; Keep the second line from the top visible
6269               (set-window-start window top-pos t)
6270             ;; Try to keep the bottom line visible; if it's partially
6271             ;; obscured, either scroll one more line to make it fully
6272             ;; visible, or revert to using TOP-POS.
6273             (save-excursion
6274               (goto-char (point-max))
6275               (forward-line -1)
6276               (let ((last-line-start (point)))
6277                 (goto-char bottom)
6278                 (set-window-start window (point) t)
6279                 (when (not (pos-visible-in-window-p last-line-start window))
6280                   (forward-line 1)
6281                   (set-window-start window (min (point) top-pos) t)))))))
6282       ;; Do horizontal recentering while we're at it.
6283       (when (and (get-buffer-window (current-buffer) t)
6284                  (not (eq gnus-auto-center-summary 'vertical)))
6285         (let ((selected (selected-window)))
6286           (select-window (get-buffer-window (current-buffer) t))
6287           (gnus-summary-position-point)
6288           (gnus-horizontal-recenter)
6289           (select-window selected))))))
6290
6291 (defun gnus-summary-jump-to-group (newsgroup)
6292   "Move point to NEWSGROUP in group mode buffer."
6293   ;; Keep update point of group mode buffer if visible.
6294   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6295       (save-window-excursion
6296         ;; Take care of tree window mode.
6297         (when (get-buffer-window gnus-group-buffer)
6298           (pop-to-buffer gnus-group-buffer))
6299         (gnus-group-jump-to-group newsgroup))
6300     (save-excursion
6301       ;; Take care of tree window mode.
6302       (if (get-buffer-window gnus-group-buffer)
6303           (pop-to-buffer gnus-group-buffer)
6304         (set-buffer gnus-group-buffer))
6305       (gnus-group-jump-to-group newsgroup))))
6306
6307 ;; This function returns a list of article numbers based on the
6308 ;; difference between the ranges of read articles in this group and
6309 ;; the range of active articles.
6310 (defun gnus-list-of-unread-articles (group)
6311   (let* ((read (gnus-info-read (gnus-get-info group)))
6312          (active (or (gnus-active group) (gnus-activate-group group)))
6313          (last (cdr active))
6314          first nlast unread)
6315     ;; If none are read, then all are unread.
6316     (if (not read)
6317         (setq first (car active))
6318       ;; If the range of read articles is a single range, then the
6319       ;; first unread article is the article after the last read
6320       ;; article.  Sounds logical, doesn't it?
6321       (if (and (not (listp (cdr read)))
6322                (or (< (car read) (car active))
6323                    (progn (setq read (list read))
6324                           nil)))
6325           (setq first (max (car active) (1+ (cdr read))))
6326         ;; `read' is a list of ranges.
6327         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6328                                   (caar read)))
6329                   1)
6330           (setq first (car active)))
6331         (while read
6332           (when first
6333             (while (< first nlast)
6334               (setq unread (cons first unread)
6335                     first (1+ first))))
6336           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6337           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6338           (setq read (cdr read)))))
6339     ;; And add the last unread articles.
6340     (while (<= first last)
6341       (setq unread (cons first unread)
6342             first (1+ first)))
6343     ;; Return the list of unread articles.
6344     (delq 0 (nreverse unread))))
6345
6346 (defun gnus-list-of-read-articles (group)
6347   "Return a list of unread, unticked and non-dormant articles."
6348   (let* ((info (gnus-get-info group))
6349          (marked (gnus-info-marks info))
6350          (active (gnus-active group)))
6351     (and info active
6352          (gnus-list-range-difference
6353           (gnus-list-range-difference
6354            (gnus-sorted-complement
6355             (gnus-uncompress-range active)
6356             (gnus-list-of-unread-articles group))
6357            (cdr (assq 'dormant marked)))
6358           (cdr (assq 'tick marked))))))
6359
6360 ;; This function returns a sequence of article numbers based on the
6361 ;; difference between the ranges of read articles in this group and
6362 ;; the range of active articles.
6363 (defun gnus-sequence-of-unread-articles (group)
6364   (let* ((read (gnus-info-read (gnus-get-info group)))
6365          (active (or (gnus-active group) (gnus-activate-group group)))
6366          (last (cdr active))
6367          first nlast unread)
6368     ;; If none are read, then all are unread.
6369     (if (not read)
6370         (setq first (car active))
6371       ;; If the range of read articles is a single range, then the
6372       ;; first unread article is the article after the last read
6373       ;; article.  Sounds logical, doesn't it?
6374       (if (and (not (listp (cdr read)))
6375                (or (< (car read) (car active))
6376                    (progn (setq read (list read))
6377                           nil)))
6378           (setq first (max (car active) (1+ (cdr read))))
6379         ;; `read' is a list of ranges.
6380         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6381                                   (caar read)))
6382                   1)
6383           (setq first (car active)))
6384         (while read
6385           (when first
6386             (push (cons first nlast) unread))
6387           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6388           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6389           (setq read (cdr read)))))
6390     ;; And add the last unread articles.
6391     (cond ((< first last)
6392            (push (cons first last) unread))
6393           ((= first last)
6394            (push first unread)))
6395     ;; Return the sequence of unread articles.
6396     (delq 0 (nreverse unread))))
6397
6398 ;; Various summary commands
6399
6400 (defun gnus-summary-select-article-buffer ()
6401   "Reconfigure windows to show article buffer."
6402   (interactive)
6403   (if (not (gnus-buffer-live-p gnus-article-buffer))
6404       (error "There is no article buffer for this summary buffer")
6405     (gnus-configure-windows 'article)
6406     (select-window (get-buffer-window gnus-article-buffer))))
6407
6408 (defun gnus-summary-universal-argument (arg)
6409   "Perform any operation on all articles that are process/prefixed."
6410   (interactive "P")
6411   (let ((articles (gnus-summary-work-articles arg))
6412         func article)
6413     (if (eq
6414          (setq
6415           func
6416           (key-binding
6417            (read-key-sequence
6418             (substitute-command-keys
6419              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6420          'undefined)
6421         (gnus-error 1 "Undefined key")
6422       (save-excursion
6423         (while articles
6424           (gnus-summary-goto-subject (setq article (pop articles)))
6425           (let (gnus-newsgroup-processable)
6426             (command-execute func))
6427           (gnus-summary-remove-process-mark article)))))
6428   (gnus-summary-position-point))
6429
6430 (defun gnus-summary-toggle-truncation (&optional arg)
6431   "Toggle truncation of summary lines.
6432 With ARG, turn line truncation on if ARG is positive."
6433   (interactive "P")
6434   (setq truncate-lines
6435         (if (null arg) (not truncate-lines)
6436           (> (prefix-numeric-value arg) 0)))
6437   (redraw-display))
6438
6439 (defun gnus-summary-find-for-reselect ()
6440   "Return the number of an article to stay on across a reselect.
6441 The current article is considered, then following articles, then previous
6442 articles.  An article is sought which is not cancelled and isn't a temporary
6443 insertion from another group.  If there's no such then return a dummy 0."
6444   (let (found)
6445     (dolist (rev '(nil t))
6446       (unless found      ; don't demand the reverse list if we don't need it
6447         (let ((data (gnus-data-find-list
6448                      (gnus-summary-article-number) (gnus-data-list rev))))
6449           (while (and data (not found))
6450             (if (and (< 0 (gnus-data-number (car data)))
6451                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6452                 (setq found (gnus-data-number (car data))))
6453             (setq data (cdr data))))))
6454     (or found 0)))
6455
6456 (defun gnus-summary-reselect-current-group (&optional all rescan)
6457   "Exit and then reselect the current newsgroup.
6458 The prefix argument ALL means to select all articles."
6459   (interactive "P")
6460   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6461     (error "Ephemeral groups can't be reselected"))
6462   (let ((current-subject (gnus-summary-find-for-reselect))
6463         (group gnus-newsgroup-name))
6464     (setq gnus-newsgroup-begin nil)
6465     (gnus-summary-exit nil 'leave-hidden)
6466     ;; We have to adjust the point of group mode buffer because
6467     ;; point was moved to the next unread newsgroup by exiting.
6468     (gnus-summary-jump-to-group group)
6469     (when rescan
6470       (save-excursion
6471         (gnus-group-get-new-news-this-group 1)))
6472     (gnus-group-read-group all t)
6473     (gnus-summary-goto-subject current-subject nil t)))
6474
6475 (defun gnus-summary-rescan-group (&optional all)
6476   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6477   (interactive "P")
6478   (gnus-summary-reselect-current-group all t))
6479
6480 (defun gnus-summary-update-info (&optional non-destructive)
6481   (save-excursion
6482     (let ((group gnus-newsgroup-name))
6483       (when group
6484         (when gnus-newsgroup-kill-headers
6485           (setq gnus-newsgroup-killed
6486                 (gnus-compress-sequence
6487                  (gnus-sorted-union
6488                   (gnus-list-range-intersection
6489                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6490                   gnus-newsgroup-unreads)
6491                  t)))
6492         (unless (listp (cdr gnus-newsgroup-killed))
6493           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6494         (let ((headers gnus-newsgroup-headers))
6495           ;; Set the new ranges of read articles.
6496           (save-excursion
6497             (set-buffer gnus-group-buffer)
6498             (gnus-undo-force-boundary))
6499           (gnus-update-read-articles
6500            group (gnus-sorted-union
6501                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6502           ;; Set the current article marks.
6503           (let ((gnus-newsgroup-scored
6504                  (if (and (not gnus-save-score)
6505                           (not non-destructive))
6506                      nil
6507                    gnus-newsgroup-scored)))
6508             (save-excursion
6509               (gnus-update-marks)))
6510           ;; Do the cross-ref thing.
6511           (when gnus-use-cross-reference
6512             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6513           ;; Do not switch windows but change the buffer to work.
6514           (set-buffer gnus-group-buffer)
6515           (unless (gnus-ephemeral-group-p group)
6516             (gnus-group-update-group group)))))))
6517
6518 (defun gnus-summary-save-newsrc (&optional force)
6519   "Save the current number of read/marked articles in the dribble buffer.
6520 The dribble buffer will then be saved.
6521 If FORCE (the prefix), also save the .newsrc file(s)."
6522   (interactive "P")
6523   (gnus-summary-update-info t)
6524   (if force
6525       (gnus-save-newsrc-file)
6526     (gnus-dribble-save)))
6527
6528 (defun gnus-summary-exit (&optional temporary leave-hidden)
6529   "Exit reading current newsgroup, and then return to group selection mode.
6530 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6531   (interactive)
6532   (gnus-set-global-variables)
6533   (when (gnus-buffer-live-p gnus-article-buffer)
6534     (save-excursion
6535       (set-buffer gnus-article-buffer)
6536       (mm-destroy-parts gnus-article-mime-handles)
6537       ;; Set it to nil for safety reason.
6538       (setq gnus-article-mime-handle-alist nil)
6539       (setq gnus-article-mime-handles nil)))
6540   (gnus-kill-save-kill-buffer)
6541   (gnus-async-halt-prefetch)
6542   (let* ((group gnus-newsgroup-name)
6543          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6544          (gnus-group-is-exiting-p t)
6545          (mode major-mode)
6546          (group-point nil)
6547          (buf (current-buffer)))
6548     (unless quit-config
6549       ;; Do adaptive scoring, and possibly save score files.
6550       (when gnus-newsgroup-adaptive
6551         (gnus-score-adaptive))
6552       (when gnus-use-scoring
6553         (gnus-score-save)))
6554     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6555     ;; If we have several article buffers, we kill them at exit.
6556     (unless gnus-single-article-buffer
6557       (gnus-kill-buffer gnus-original-article-buffer)
6558       (setq gnus-article-current nil))
6559     (when gnus-use-cache
6560       (gnus-cache-possibly-remove-articles)
6561       (gnus-cache-save-buffers))
6562     (gnus-async-prefetch-remove-group group)
6563     (when gnus-suppress-duplicates
6564       (gnus-dup-enter-articles))
6565     (when gnus-use-trees
6566       (gnus-tree-close group))
6567     (when gnus-use-cache
6568       (gnus-cache-write-active))
6569     ;; Remove entries for this group.
6570     (nnmail-purge-split-history (gnus-group-real-name group))
6571     ;; Make all changes in this group permanent.
6572     (unless quit-config
6573       (gnus-run-hooks 'gnus-exit-group-hook)
6574       (gnus-summary-update-info))
6575     (gnus-close-group group)
6576     ;; Make sure where we were, and go to next newsgroup.
6577     (set-buffer gnus-group-buffer)
6578     (unless quit-config
6579       (gnus-group-jump-to-group group))
6580     (gnus-run-hooks 'gnus-summary-exit-hook)
6581     (unless (or quit-config
6582                 ;; If this group has disappeared from the summary
6583                 ;; buffer, don't skip forwards.
6584                 (not (string= group (gnus-group-group-name))))
6585       (gnus-group-next-unread-group 1))
6586     (setq group-point (point))
6587     (if temporary
6588         nil                             ;Nothing to do.
6589       ;; If we have several article buffers, we kill them at exit.
6590       (unless gnus-single-article-buffer
6591         (gnus-kill-buffer gnus-article-buffer)
6592         (gnus-kill-buffer gnus-original-article-buffer)
6593         (setq gnus-article-current nil))
6594       (set-buffer buf)
6595       (if (not gnus-kill-summary-on-exit)
6596           (progn
6597             (gnus-deaden-summary)
6598             (setq mode nil))
6599         ;; We set all buffer-local variables to nil.  It is unclear why
6600         ;; this is needed, but if we don't, buffer-local variables are
6601         ;; not garbage-collected, it seems.  This would the lead to en
6602         ;; ever-growing Emacs.
6603         (gnus-summary-clear-local-variables)
6604         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6605           (gnus-summary-clear-local-variables))
6606         (when (get-buffer gnus-article-buffer)
6607           (bury-buffer gnus-article-buffer))
6608         ;; We clear the global counterparts of the buffer-local
6609         ;; variables as well, just to be on the safe side.
6610         (set-buffer gnus-group-buffer)
6611         (gnus-summary-clear-local-variables)
6612         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6613           (gnus-summary-clear-local-variables))
6614         ;; Return to group mode buffer.
6615         (when (eq mode 'gnus-summary-mode)
6616           (gnus-kill-buffer buf)))
6617       (setq gnus-current-select-method gnus-select-method)
6618       (if leave-hidden
6619           (set-buffer gnus-group-buffer)
6620         (pop-to-buffer gnus-group-buffer))
6621       (if (not quit-config)
6622           (progn
6623             (goto-char group-point)
6624             (unless leave-hidden
6625               (gnus-configure-windows 'group 'force)))
6626         (gnus-handle-ephemeral-exit quit-config))
6627       ;; Clear the current group name.
6628       (unless quit-config
6629         (setq gnus-newsgroup-name nil)))))
6630
6631 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6632 (defun gnus-summary-exit-no-update (&optional no-questions)
6633   "Quit reading current newsgroup without updating read article info."
6634   (interactive)
6635   (let* ((group gnus-newsgroup-name)
6636          (gnus-group-is-exiting-p t)
6637          (gnus-group-is-exiting-without-update-p t)
6638          (quit-config (gnus-group-quit-config group)))
6639     (when (or no-questions
6640               gnus-expert-user
6641               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6642       (gnus-async-halt-prefetch)
6643       (run-hooks 'gnus-summary-prepare-exit-hook)
6644       (when (gnus-buffer-live-p gnus-article-buffer)
6645         (save-excursion
6646           (set-buffer gnus-article-buffer)
6647           (mm-destroy-parts gnus-article-mime-handles)
6648           ;; Set it to nil for safety reason.
6649           (setq gnus-article-mime-handle-alist nil)
6650           (setq gnus-article-mime-handles nil)))
6651       ;; If we have several article buffers, we kill them at exit.
6652       (unless gnus-single-article-buffer
6653         (gnus-kill-buffer gnus-article-buffer)
6654         (gnus-kill-buffer gnus-original-article-buffer)
6655         (setq gnus-article-current nil))
6656       (if (not gnus-kill-summary-on-exit)
6657           (gnus-deaden-summary)
6658         (gnus-close-group group)
6659         (gnus-summary-clear-local-variables)
6660         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6661           (gnus-summary-clear-local-variables))
6662         (set-buffer gnus-group-buffer)
6663         (gnus-summary-clear-local-variables)
6664         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6665           (gnus-summary-clear-local-variables))
6666         (gnus-kill-buffer gnus-summary-buffer))
6667       (unless gnus-single-article-buffer
6668         (setq gnus-article-current nil))
6669       (when gnus-use-trees
6670         (gnus-tree-close group))
6671       (gnus-async-prefetch-remove-group group)
6672       (when (get-buffer gnus-article-buffer)
6673         (bury-buffer gnus-article-buffer))
6674       ;; Return to the group buffer.
6675       (gnus-configure-windows 'group 'force)
6676       ;; Clear the current group name.
6677       (setq gnus-newsgroup-name nil)
6678       (unless (gnus-ephemeral-group-p group)
6679         (gnus-group-update-group group))
6680       (when (equal (gnus-group-group-name) group)
6681         (gnus-group-next-unread-group 1))
6682       (when quit-config
6683         (gnus-handle-ephemeral-exit quit-config)))))
6684
6685 (defun gnus-handle-ephemeral-exit (quit-config)
6686   "Handle movement when leaving an ephemeral group.
6687 The state which existed when entering the ephemeral is reset."
6688   (if (not (buffer-name (car quit-config)))
6689       (gnus-configure-windows 'group 'force)
6690     (set-buffer (car quit-config))
6691     (cond ((eq major-mode 'gnus-summary-mode)
6692            (gnus-set-global-variables))
6693           ((eq major-mode 'gnus-article-mode)
6694            (save-excursion
6695              ;; The `gnus-summary-buffer' variable may point
6696              ;; to the old summary buffer when using a single
6697              ;; article buffer.
6698              (unless (gnus-buffer-live-p gnus-summary-buffer)
6699                (set-buffer gnus-group-buffer))
6700              (set-buffer gnus-summary-buffer)
6701              (gnus-set-global-variables))))
6702     (if (or (eq (cdr quit-config) 'article)
6703             (eq (cdr quit-config) 'pick))
6704         (progn
6705           ;; The current article may be from the ephemeral group
6706           ;; thus it is best that we reload this article
6707           ;;
6708           ;; If we're exiting from a large digest, this can be
6709           ;; extremely slow.  So, it's better not to reload it. -- jh.
6710           ;;(gnus-summary-show-article)
6711           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6712               (gnus-configure-windows 'pick 'force)
6713             (gnus-configure-windows (cdr quit-config) 'force)))
6714       (gnus-configure-windows (cdr quit-config) 'force))
6715     (when (eq major-mode 'gnus-summary-mode)
6716       (gnus-summary-next-subject 1 nil t)
6717       (gnus-summary-recenter)
6718       (gnus-summary-position-point))))
6719
6720 ;;; Dead summaries.
6721
6722 (defvar gnus-dead-summary-mode-map nil)
6723
6724 (unless gnus-dead-summary-mode-map
6725   (setq gnus-dead-summary-mode-map (make-keymap))
6726   (suppress-keymap gnus-dead-summary-mode-map)
6727   (substitute-key-definition
6728    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6729   (dolist (key '("\C-d" "\r" "\177" [delete]))
6730     (define-key gnus-dead-summary-mode-map
6731       key 'gnus-summary-wake-up-the-dead))
6732   (dolist (key '("q" "Q"))
6733     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6734
6735 (defvar gnus-dead-summary-mode nil
6736   "Minor mode for Gnus summary buffers.")
6737
6738 (defun gnus-dead-summary-mode (&optional arg)
6739   "Minor mode for Gnus summary buffers."
6740   (interactive "P")
6741   (when (eq major-mode 'gnus-summary-mode)
6742     (make-local-variable 'gnus-dead-summary-mode)
6743     (setq gnus-dead-summary-mode
6744           (if (null arg) (not gnus-dead-summary-mode)
6745             (> (prefix-numeric-value arg) 0)))
6746     (when gnus-dead-summary-mode
6747       (add-minor-mode
6748        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6749
6750 (defun gnus-deaden-summary ()
6751   "Make the current summary buffer into a dead summary buffer."
6752   ;; Kill any previous dead summary buffer.
6753   (when (and gnus-dead-summary
6754              (buffer-name gnus-dead-summary))
6755     (save-excursion
6756       (set-buffer gnus-dead-summary)
6757       (when gnus-dead-summary-mode
6758         (kill-buffer (current-buffer)))))
6759   ;; Make this the current dead summary.
6760   (setq gnus-dead-summary (current-buffer))
6761   (gnus-dead-summary-mode 1)
6762   (let ((name (buffer-name)))
6763     (when (string-match "Summary" name)
6764       (rename-buffer
6765        (concat (substring name 0 (match-beginning 0)) "Dead "
6766                (substring name (match-beginning 0)))
6767        t)
6768       (bury-buffer))))
6769
6770 (defun gnus-kill-or-deaden-summary (buffer)
6771   "Kill or deaden the summary BUFFER."
6772   (save-excursion
6773     (when (and (buffer-name buffer)
6774                (not gnus-single-article-buffer))
6775       (save-excursion
6776         (set-buffer buffer)
6777         (gnus-kill-buffer gnus-article-buffer)
6778         (gnus-kill-buffer gnus-original-article-buffer)))
6779     (cond
6780      ;; Kill the buffer.
6781      (gnus-kill-summary-on-exit
6782       (when (and gnus-use-trees
6783                  (gnus-buffer-exists-p buffer))
6784         (save-excursion
6785           (set-buffer buffer)
6786           (gnus-tree-close gnus-newsgroup-name)))
6787       (gnus-kill-buffer buffer))
6788      ;; Deaden the buffer.
6789      ((gnus-buffer-exists-p buffer)
6790       (save-excursion
6791         (set-buffer buffer)
6792         (gnus-deaden-summary))))))
6793
6794 (defun gnus-summary-wake-up-the-dead (&rest args)
6795   "Wake up the dead summary buffer."
6796   (interactive)
6797   (gnus-dead-summary-mode -1)
6798   (let ((name (buffer-name)))
6799     (when (string-match "Dead " name)
6800       (rename-buffer
6801        (concat (substring name 0 (match-beginning 0))
6802                (substring name (match-end 0)))
6803        t)))
6804   (gnus-message 3 "This dead summary is now alive again"))
6805
6806 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6807 (defun gnus-summary-fetch-faq (&optional faq-dir)
6808   "Fetch the FAQ for the current group.
6809 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6810 in."
6811   (interactive
6812    (list
6813     (when current-prefix-arg
6814       (completing-read
6815        "FAQ dir: " (and (listp gnus-group-faq-directory)
6816                         (mapcar (lambda (file) (list file))
6817                                 gnus-group-faq-directory))))))
6818   (let (gnus-faq-buffer)
6819     (when (setq gnus-faq-buffer
6820                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6821       (gnus-configure-windows 'summary-faq))))
6822
6823 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6824 (defun gnus-summary-describe-group (&optional force)
6825   "Describe the current newsgroup."
6826   (interactive "P")
6827   (gnus-group-describe-group force gnus-newsgroup-name))
6828
6829 (defun gnus-summary-describe-briefly ()
6830   "Describe summary mode commands briefly."
6831   (interactive)
6832   (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")))
6833
6834 ;; Walking around group mode buffer from summary mode.
6835
6836 (defun gnus-summary-next-group (&optional no-article target-group backward)
6837   "Exit current newsgroup and then select next unread newsgroup.
6838 If prefix argument NO-ARTICLE is non-nil, no article is selected
6839 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6840 previous group instead."
6841   (interactive "P")
6842   ;; Stop pre-fetching.
6843   (gnus-async-halt-prefetch)
6844   (let ((current-group gnus-newsgroup-name)
6845         (current-buffer (current-buffer))
6846         entered)
6847     ;; First we semi-exit this group to update Xrefs and all variables.
6848     ;; We can't do a real exit, because the window conf must remain
6849     ;; the same in case the user is prompted for info, and we don't
6850     ;; want the window conf to change before that...
6851     (gnus-summary-exit t)
6852     (while (not entered)
6853       ;; Then we find what group we are supposed to enter.
6854       (set-buffer gnus-group-buffer)
6855       (gnus-group-jump-to-group current-group)
6856       (setq target-group
6857             (or target-group
6858                 (if (eq gnus-keep-same-level 'best)
6859                     (gnus-summary-best-group gnus-newsgroup-name)
6860                   (gnus-summary-search-group backward gnus-keep-same-level))))
6861       (if (not target-group)
6862           ;; There are no further groups, so we return to the group
6863           ;; buffer.
6864           (progn
6865             (gnus-message 5 "Returning to the group buffer")
6866             (setq entered t)
6867             (when (gnus-buffer-live-p current-buffer)
6868               (set-buffer current-buffer)
6869               (gnus-summary-exit))
6870             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6871         ;; We try to enter the target group.
6872         (gnus-group-jump-to-group target-group)
6873         (let ((unreads (gnus-group-group-unread)))
6874           (if (and (or (eq t unreads)
6875                        (and unreads (not (zerop unreads))))
6876                    (gnus-summary-read-group
6877                     target-group nil no-article
6878                     (and (buffer-name current-buffer) current-buffer)
6879                     nil backward))
6880               (setq entered t)
6881             (setq current-group target-group
6882                   target-group nil)))))))
6883
6884 (defun gnus-summary-prev-group (&optional no-article)
6885   "Exit current newsgroup and then select previous unread newsgroup.
6886 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6887   (interactive "P")
6888   (gnus-summary-next-group no-article nil t))
6889
6890 ;; Walking around summary lines.
6891
6892 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6893   "Go to the first subject satisfying any non-nil constraint.
6894 If UNREAD is non-nil, the article should be unread.
6895 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6896 If UNSEEN is non-nil, the article should be unseen.
6897 Returns the article selected or nil if there are no matching articles."
6898   (interactive "P")
6899   (cond
6900    ;; Empty summary.
6901    ((null gnus-newsgroup-data)
6902     (gnus-message 3 "No articles in the group")
6903     nil)
6904    ;; Pick the first article.
6905    ((not (or unread undownloaded unseen))
6906     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6907     (gnus-data-number (car gnus-newsgroup-data)))
6908    ;; Find the first unread article.
6909    (t
6910     (let ((data gnus-newsgroup-data))
6911       (while (and data
6912                   (let ((num (gnus-data-number (car data))))
6913                     (or (memq num gnus-newsgroup-unfetched)
6914                         (not (or (and unread
6915                                       (memq num gnus-newsgroup-unreads))
6916                                  (and undownloaded
6917                                       (memq num gnus-newsgroup-undownloaded))
6918                                  (and unseen
6919                                       (memq num gnus-newsgroup-unseen)))))))
6920         (setq data (cdr data)))
6921       (prog1
6922           (if data
6923               (progn
6924                 (goto-char (gnus-data-pos (car data)))
6925                 (gnus-data-number (car data)))
6926             (gnus-message 3 "No more%s articles"
6927                           (let* ((r (when unread " unread"))
6928                                  (d (when undownloaded " undownloaded"))
6929                                  (s (when unseen " unseen"))
6930                                  (l (delq nil (list r d s))))
6931                             (cond ((= 3 (length l))
6932                                    (concat r "," d ", or" s))
6933                                   ((= 2 (length l))
6934                                    (concat (car l) ", or" (cadr l)))
6935                                   ((= 1 (length l))
6936                                    (car l))
6937                                   (t
6938                                    ""))))
6939             nil
6940             )
6941         (gnus-summary-position-point))))))
6942
6943 (defun gnus-summary-next-subject (n &optional unread dont-display)
6944   "Go to next N'th summary line.
6945 If N is negative, go to the previous N'th subject line.
6946 If UNREAD is non-nil, only unread articles are selected.
6947 The difference between N and the actual number of steps taken is
6948 returned."
6949   (interactive "p")
6950   (let ((backward (< n 0))
6951         (n (abs n)))
6952     (while (and (> n 0)
6953                 (if backward
6954                     (gnus-summary-find-prev unread)
6955                   (gnus-summary-find-next unread)))
6956       (unless (zerop (setq n (1- n)))
6957         (gnus-summary-show-thread)))
6958     (when (/= 0 n)
6959       (gnus-message 7 "No more%s articles"
6960                     (if unread " unread" "")))
6961     (unless dont-display
6962       (gnus-summary-recenter)
6963       (gnus-summary-position-point))
6964     n))
6965
6966 (defun gnus-summary-next-unread-subject (n)
6967   "Go to next N'th unread summary line."
6968   (interactive "p")
6969   (gnus-summary-next-subject n t))
6970
6971 (defun gnus-summary-prev-subject (n &optional unread)
6972   "Go to previous N'th summary line.
6973 If optional argument UNREAD is non-nil, only unread article is selected."
6974   (interactive "p")
6975   (gnus-summary-next-subject (- n) unread))
6976
6977 (defun gnus-summary-prev-unread-subject (n)
6978   "Go to previous N'th unread summary line."
6979   (interactive "p")
6980   (gnus-summary-next-subject (- n) t))
6981
6982 (defun gnus-summary-goto-subjects (articles)
6983   "Insert the subject header for ARTICLES in the current buffer."
6984   (save-excursion
6985     (dolist (article articles)
6986       (gnus-summary-goto-subject article t)))
6987   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6988   (gnus-summary-position-point))
6989  
6990 (defun gnus-summary-goto-subject (article &optional force silent)
6991   "Go the subject line of ARTICLE.
6992 If FORCE, also allow jumping to articles not currently shown."
6993   (interactive "nArticle number: ")
6994   (unless (numberp article)
6995     (error "Article %s is not a number" article))
6996   (let ((b (point))
6997         (data (gnus-data-find article)))
6998     ;; We read in the article if we have to.
6999     (and (not data)
7000          force
7001          (gnus-summary-insert-subject
7002           article
7003           (if (or (numberp force) (vectorp force)) force)
7004           t)
7005          (setq data (gnus-data-find article)))
7006     (goto-char b)
7007     (if (not data)
7008         (progn
7009           (unless silent
7010             (gnus-message 3 "Can't find article %d" article))
7011           nil)
7012       (let ((pt (gnus-data-pos data)))
7013         (goto-char pt)
7014         (gnus-summary-set-article-display-arrow pt))
7015       (gnus-summary-position-point)
7016       article)))
7017
7018 ;; Walking around summary lines with displaying articles.
7019
7020 (defun gnus-summary-expand-window (&optional arg)
7021   "Make the summary buffer take up the entire Emacs frame.
7022 Given a prefix, will force an `article' buffer configuration."
7023   (interactive "P")
7024   (if arg
7025       (gnus-configure-windows 'article 'force)
7026     (gnus-configure-windows 'summary 'force)))
7027
7028 (defun gnus-summary-display-article (article &optional all-header)
7029   "Display ARTICLE in article buffer."
7030   (when (gnus-buffer-live-p gnus-article-buffer)
7031     (with-current-buffer gnus-article-buffer
7032       (mm-enable-multibyte)))
7033   (gnus-set-global-variables)
7034   (when (gnus-buffer-live-p gnus-article-buffer)
7035     (with-current-buffer gnus-article-buffer
7036       (setq gnus-article-charset gnus-newsgroup-charset)
7037       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7038       (mm-enable-multibyte)))
7039   (if (null article)
7040       nil
7041     (prog1
7042         (if gnus-summary-display-article-function
7043             (funcall gnus-summary-display-article-function article all-header)
7044           (gnus-article-prepare article all-header))
7045       (gnus-run-hooks 'gnus-select-article-hook)
7046       (when (and gnus-current-article
7047                  (not (zerop gnus-current-article)))
7048         (gnus-summary-goto-subject gnus-current-article))
7049       (gnus-summary-recenter)
7050       (when (and gnus-use-trees gnus-show-threads)
7051         (gnus-possibly-generate-tree article)
7052         (gnus-highlight-selected-tree article))
7053       ;; Successfully display article.
7054       (gnus-article-set-window-start
7055        (cdr (assq article gnus-newsgroup-bookmarks))))))
7056
7057 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7058   "Select the current article.
7059 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7060 non-nil, the article will be re-fetched even if it already present in
7061 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7062 be displayed."
7063   ;; Make sure we are in the summary buffer to work around bbdb bug.
7064   (unless (eq major-mode 'gnus-summary-mode)
7065     (set-buffer gnus-summary-buffer))
7066   (let ((article (or article (gnus-summary-article-number)))
7067         (all-headers (not (not all-headers))) ;Must be t or nil.
7068         gnus-summary-display-article-function)
7069     (and (not pseudo)
7070          (gnus-summary-article-pseudo-p article)
7071          (error "This is a pseudo-article"))
7072     (save-excursion
7073       (set-buffer gnus-summary-buffer)
7074       (if (or (and gnus-single-article-buffer
7075                    (or (null gnus-current-article)
7076                        (null gnus-article-current)
7077                        (null (get-buffer gnus-article-buffer))
7078                        (not (eq article (cdr gnus-article-current)))
7079                        (not (equal (car gnus-article-current)
7080                                    gnus-newsgroup-name))))
7081               (and (not gnus-single-article-buffer)
7082                    (or (null gnus-current-article)
7083                        (not (eq gnus-current-article article))))
7084               force)
7085           ;; The requested article is different from the current article.
7086           (progn
7087             (gnus-summary-display-article article all-headers)
7088             (when (gnus-buffer-live-p gnus-article-buffer)
7089               (with-current-buffer gnus-article-buffer
7090                 (if (not gnus-article-decoded-p) ;; a local variable
7091                     (mm-disable-multibyte))))
7092             (gnus-article-set-window-start
7093              (cdr (assq article gnus-newsgroup-bookmarks)))
7094             article)
7095         'old))))
7096
7097 (defun gnus-summary-force-verify-and-decrypt ()
7098   "Display buttons for signed/encrypted parts and verify/decrypt them."
7099   (interactive)
7100   (let ((mm-verify-option 'known)
7101         (mm-decrypt-option 'known)
7102         (gnus-article-emulate-mime t)
7103         (gnus-buttonized-mime-types (append (list "multipart/signed"
7104                                                   "multipart/encrypted")
7105                                             gnus-buttonized-mime-types)))
7106     (gnus-summary-select-article nil 'force)))
7107
7108 (defun gnus-summary-set-current-mark (&optional current-mark)
7109   "Obsolete function."
7110   nil)
7111
7112 (defun gnus-summary-next-article (&optional unread subject backward push)
7113   "Select the next article.
7114 If UNREAD, only unread articles are selected.
7115 If SUBJECT, only articles with SUBJECT are selected.
7116 If BACKWARD, the previous article is selected instead of the next."
7117   (interactive "P")
7118   (cond
7119    ;; Is there such an article?
7120    ((and (gnus-summary-search-forward unread subject backward)
7121          (or (gnus-summary-display-article (gnus-summary-article-number))
7122              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7123     (gnus-summary-position-point))
7124    ;; If not, we try the first unread, if that is wanted.
7125    ((and subject
7126          gnus-auto-select-same
7127          (gnus-summary-first-unread-article))
7128     (gnus-summary-position-point)
7129     (gnus-message 6 "Wrapped"))
7130    ;; Try to get next/previous article not displayed in this group.
7131    ((and gnus-auto-extend-newsgroup
7132          (not unread) (not subject))
7133     (gnus-summary-goto-article
7134      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7135      nil (count-lines (point-min) (point))))
7136    ;; Go to next/previous group.
7137    (t
7138     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7139       (gnus-summary-jump-to-group gnus-newsgroup-name))
7140     (let ((cmd last-command-char)
7141           (point
7142            (save-excursion
7143              (set-buffer gnus-group-buffer)
7144              (point)))
7145           (group
7146            (if (eq gnus-keep-same-level 'best)
7147                (gnus-summary-best-group gnus-newsgroup-name)
7148              (gnus-summary-search-group backward gnus-keep-same-level))))
7149       ;; For some reason, the group window gets selected.  We change
7150       ;; it back.
7151       (select-window (get-buffer-window (current-buffer)))
7152       ;; Select next unread newsgroup automagically.
7153       (cond
7154        ((or (not gnus-auto-select-next)
7155             (not cmd))
7156         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7157        ((or (eq gnus-auto-select-next 'quietly)
7158             (and (eq gnus-auto-select-next 'slightly-quietly)
7159                  push)
7160             (and (eq gnus-auto-select-next 'almost-quietly)
7161                  (gnus-summary-last-article-p)))
7162         ;; Select quietly.
7163         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7164             (gnus-summary-exit)
7165           (gnus-message 7 "No more%s articles (%s)..."
7166                         (if unread " unread" "")
7167                         (if group (concat "selecting " group)
7168                           "exiting"))
7169           (gnus-summary-next-group nil group backward)))
7170        (t
7171         (when (gnus-key-press-event-p last-input-event)
7172           (gnus-summary-walk-group-buffer
7173            gnus-newsgroup-name cmd unread backward point))))))))
7174
7175 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7176   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7177                       (?\C-p (gnus-group-prev-unread-group 1))))
7178         (cursor-in-echo-area t)
7179         keve key group ended prompt)
7180     (save-excursion
7181       (set-buffer gnus-group-buffer)
7182       (goto-char start)
7183       (setq group
7184             (if (eq gnus-keep-same-level 'best)
7185                 (gnus-summary-best-group gnus-newsgroup-name)
7186               (gnus-summary-search-group backward gnus-keep-same-level))))
7187     (while (not ended)
7188       (setq prompt
7189             (format
7190              "No more%s articles%s " (if unread " unread" "")
7191              (if (and group
7192                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7193                  (format " (Type %s for %s [%s])"
7194                          (single-key-description cmd) group
7195                          (gnus-group-unread group))
7196                (format " (Type %s to exit %s)"
7197                        (single-key-description cmd)
7198                        gnus-newsgroup-name))))
7199       ;; Confirm auto selection.
7200       (setq key (car (setq keve (gnus-read-event-char prompt)))
7201             ended t)
7202       (cond
7203        ((assq key keystrokes)
7204         (let ((obuf (current-buffer)))
7205           (switch-to-buffer gnus-group-buffer)
7206           (when group
7207             (gnus-group-jump-to-group group))
7208           (eval (cadr (assq key keystrokes)))
7209           (setq group (gnus-group-group-name))
7210           (switch-to-buffer obuf))
7211         (setq ended nil))
7212        ((equal key cmd)
7213         (if (or (not group)
7214                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7215             (gnus-summary-exit)
7216           (gnus-summary-next-group nil group backward)))
7217        (t
7218         (push (cdr keve) unread-command-events))))))
7219
7220 (defun gnus-summary-next-unread-article ()
7221   "Select unread article after current one."
7222   (interactive)
7223   (gnus-summary-next-article
7224    (or (not (eq gnus-summary-goto-unread 'never))
7225        (gnus-summary-last-article-p (gnus-summary-article-number)))
7226    (and gnus-auto-select-same
7227         (gnus-summary-article-subject))))
7228
7229 (defun gnus-summary-prev-article (&optional unread subject)
7230   "Select the article after the current one.
7231 If UNREAD is non-nil, only unread articles are selected."
7232   (interactive "P")
7233   (gnus-summary-next-article unread subject t))
7234
7235 (defun gnus-summary-prev-unread-article ()
7236   "Select unread article before current one."
7237   (interactive)
7238   (gnus-summary-prev-article
7239    (or (not (eq gnus-summary-goto-unread 'never))
7240        (gnus-summary-first-article-p (gnus-summary-article-number)))
7241    (and gnus-auto-select-same
7242         (gnus-summary-article-subject))))
7243
7244 (defun gnus-summary-next-page (&optional lines circular stop)
7245   "Show next page of the selected article.
7246 If at the end of the current article, select the next article.
7247 LINES says how many lines should be scrolled up.
7248
7249 If CIRCULAR is non-nil, go to the start of the article instead of
7250 selecting the next article when reaching the end of the current
7251 article.
7252
7253 If STOP is non-nil, just stop when reaching the end of the message.
7254
7255 Also see the variable `gnus-article-skip-boring'."
7256   (interactive "P")
7257   (setq gnus-summary-buffer (current-buffer))
7258   (gnus-set-global-variables)
7259   (let ((article (gnus-summary-article-number))
7260         (article-window (get-buffer-window gnus-article-buffer t))
7261         endp)
7262     ;; If the buffer is empty, we have no article.
7263     (unless article
7264       (error "No article to select"))
7265     (gnus-configure-windows 'article)
7266     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7267         (if (and (eq gnus-summary-goto-unread 'never)
7268                  (not (gnus-summary-last-article-p article)))
7269             (gnus-summary-next-article)
7270           (gnus-summary-next-unread-article))
7271       (if (or (null gnus-current-article)
7272               (null gnus-article-current)
7273               (/= article (cdr gnus-article-current))
7274               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7275           ;; Selected subject is different from current article's.
7276           (gnus-summary-display-article article)
7277         (when article-window
7278           (gnus-eval-in-buffer-window gnus-article-buffer
7279             (setq endp (or (gnus-article-next-page lines)
7280                            (gnus-article-only-boring-p))))
7281           (when endp
7282             (cond (stop
7283                    (gnus-message 3 "End of message"))
7284                   (circular
7285                    (gnus-summary-beginning-of-article))
7286                   (lines
7287                    (gnus-message 3 "End of message"))
7288                   ((null lines)
7289                    (if (and (eq gnus-summary-goto-unread 'never)
7290                             (not (gnus-summary-last-article-p article)))
7291                        (gnus-summary-next-article)
7292                      (gnus-summary-next-unread-article))))))))
7293     (gnus-summary-recenter)
7294     (gnus-summary-position-point)))
7295
7296 (defun gnus-summary-prev-page (&optional lines move)
7297   "Show previous page of selected article.
7298 Argument LINES specifies lines to be scrolled down.
7299 If MOVE, move to the previous unread article if point is at
7300 the beginning of the buffer."
7301   (interactive "P")
7302   (let ((article (gnus-summary-article-number))
7303         (article-window (get-buffer-window gnus-article-buffer t))
7304         endp)
7305     (gnus-configure-windows 'article)
7306     (if (or (null gnus-current-article)
7307             (null gnus-article-current)
7308             (/= article (cdr gnus-article-current))
7309             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7310         ;; Selected subject is different from current article's.
7311         (gnus-summary-display-article article)
7312       (gnus-summary-recenter)
7313       (when article-window
7314         (gnus-eval-in-buffer-window gnus-article-buffer
7315           (setq endp (gnus-article-prev-page lines)))
7316         (when (and move endp)
7317           (cond (lines
7318                  (gnus-message 3 "Beginning of message"))
7319                 ((null lines)
7320                  (if (and (eq gnus-summary-goto-unread 'never)
7321                           (not (gnus-summary-first-article-p article)))
7322                      (gnus-summary-prev-article)
7323                    (gnus-summary-prev-unread-article))))))))
7324   (gnus-summary-position-point))
7325
7326 (defun gnus-summary-prev-page-or-article (&optional lines)
7327   "Show previous page of selected article.
7328 Argument LINES specifies lines to be scrolled down.
7329 If at the beginning of the article, go to the next article."
7330   (interactive "P")
7331   (gnus-summary-prev-page lines t))
7332
7333 (defun gnus-summary-scroll-up (lines)
7334   "Scroll up (or down) one line current article.
7335 Argument LINES specifies lines to be scrolled up (or down if negative)."
7336   (interactive "p")
7337   (gnus-configure-windows 'article)
7338   (gnus-summary-show-thread)
7339   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7340     (gnus-eval-in-buffer-window gnus-article-buffer
7341       (cond ((> lines 0)
7342              (when (gnus-article-next-page lines)
7343                (gnus-message 3 "End of message")))
7344             ((< lines 0)
7345              (gnus-article-prev-page (- lines))))))
7346   (gnus-summary-recenter)
7347   (gnus-summary-position-point))
7348
7349 (defun gnus-summary-scroll-down (lines)
7350   "Scroll down (or up) one line current article.
7351 Argument LINES specifies lines to be scrolled down (or up if negative)."
7352   (interactive "p")
7353   (gnus-summary-scroll-up (- lines)))
7354
7355 (defun gnus-summary-next-same-subject ()
7356   "Select next article which has the same subject as current one."
7357   (interactive)
7358   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7359
7360 (defun gnus-summary-prev-same-subject ()
7361   "Select previous article which has the same subject as current one."
7362   (interactive)
7363   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7364
7365 (defun gnus-summary-next-unread-same-subject ()
7366   "Select next unread article which has the same subject as current one."
7367   (interactive)
7368   (gnus-summary-next-article t (gnus-summary-article-subject)))
7369
7370 (defun gnus-summary-prev-unread-same-subject ()
7371   "Select previous unread article which has the same subject as current one."
7372   (interactive)
7373   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7374
7375 (defun gnus-summary-first-unread-article ()
7376   "Select the first unread article.
7377 Return nil if there are no unread articles."
7378   (interactive)
7379   (prog1
7380       (when (gnus-summary-first-subject t)
7381         (gnus-summary-show-thread)
7382         (gnus-summary-first-subject t)
7383         (gnus-summary-display-article (gnus-summary-article-number)))
7384     (gnus-summary-position-point)))
7385
7386 (defun gnus-summary-first-unread-subject ()
7387   "Place the point on the subject line of the first unread article.
7388 Return nil if there are no unread articles."
7389   (interactive)
7390   (prog1
7391       (when (gnus-summary-first-subject t)
7392         (gnus-summary-show-thread)
7393         (gnus-summary-first-subject t))
7394     (gnus-summary-position-point)))
7395
7396 (defun gnus-summary-first-unseen-subject ()
7397   "Place the point on the subject line of the first unseen article.
7398 Return nil if there are no unseen articles."
7399   (interactive)
7400   (prog1
7401       (when (gnus-summary-first-subject nil nil t)
7402         (gnus-summary-show-thread)
7403         (gnus-summary-first-subject nil nil t))
7404     (gnus-summary-position-point)))
7405
7406 (defun gnus-summary-first-unseen-or-unread-subject ()
7407   "Place the point on the subject line of the first unseen article or,
7408 if all article have been seen, on the subject line of the first unread
7409 article."
7410   (interactive)
7411   (prog1
7412       (unless (when (gnus-summary-first-subject nil nil t)
7413                 (gnus-summary-show-thread)
7414                 (gnus-summary-first-subject nil nil t))
7415         (when (gnus-summary-first-subject t)
7416           (gnus-summary-show-thread)
7417           (gnus-summary-first-subject t)))
7418     (gnus-summary-position-point)))
7419
7420 (defun gnus-summary-first-article ()
7421   "Select the first article.
7422 Return nil if there are no articles."
7423   (interactive)
7424   (prog1
7425       (when (gnus-summary-first-subject)
7426         (gnus-summary-show-thread)
7427         (gnus-summary-first-subject)
7428         (gnus-summary-display-article (gnus-summary-article-number)))
7429     (gnus-summary-position-point)))
7430
7431 (defun gnus-summary-best-unread-article (&optional arg)
7432   "Select the unread article with the highest score.
7433 If given a prefix argument, select the next unread article that has a
7434 score higher than the default score."
7435   (interactive "P")
7436   (let ((article (if arg
7437                      (gnus-summary-better-unread-subject)
7438                    (gnus-summary-best-unread-subject))))
7439     (if article
7440         (gnus-summary-goto-article article)
7441       (error "No unread articles"))))
7442
7443 (defun gnus-summary-best-unread-subject ()
7444   "Select the unread subject with the highest score."
7445   (interactive)
7446   (let ((best -1000000)
7447         (data gnus-newsgroup-data)
7448         article score)
7449     (while data
7450       (and (gnus-data-unread-p (car data))
7451            (> (setq score
7452                     (gnus-summary-article-score (gnus-data-number (car data))))
7453               best)
7454            (setq best score
7455                  article (gnus-data-number (car data))))
7456       (setq data (cdr data)))
7457     (when article
7458       (gnus-summary-goto-subject article))
7459     (gnus-summary-position-point)
7460     article))
7461
7462 (defun gnus-summary-better-unread-subject ()
7463   "Select the first unread subject that has a score over the default score."
7464   (interactive)
7465   (let ((data gnus-newsgroup-data)
7466         article score)
7467     (while (and (setq article (gnus-data-number (car data)))
7468                 (or (gnus-data-read-p (car data))
7469                     (not (> (gnus-summary-article-score article)
7470                             gnus-summary-default-score))))
7471       (setq data (cdr data)))
7472     (when article
7473       (gnus-summary-goto-subject article))
7474     (gnus-summary-position-point)
7475     article))
7476
7477 (defun gnus-summary-last-subject ()
7478   "Go to the last displayed subject line in the group."
7479   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7480     (when article
7481       (gnus-summary-goto-subject article))))
7482
7483 (defun gnus-summary-goto-article (article &optional all-headers force)
7484   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7485 If ALL-HEADERS is non-nil, no header lines are hidden.
7486 If FORCE, go to the article even if it isn't displayed.  If FORCE
7487 is a number, it is the line the article is to be displayed on."
7488   (interactive
7489    (list
7490     (completing-read
7491      "Article number or Message-ID: "
7492      (mapcar (lambda (number) (list (int-to-string number)))
7493              gnus-newsgroup-limit))
7494     current-prefix-arg
7495     t))
7496   (prog1
7497       (if (and (stringp article)
7498                (string-match "@\\|%40" article))
7499           (gnus-summary-refer-article article)
7500         (when (stringp article)
7501           (setq article (string-to-number article)))
7502         (if (gnus-summary-goto-subject article force)
7503             (gnus-summary-display-article article all-headers)
7504           (gnus-message 4 "Couldn't go to article %s" article) nil))
7505     (gnus-summary-position-point)))
7506
7507 (defun gnus-summary-goto-last-article ()
7508   "Go to the previously read article."
7509   (interactive)
7510   (prog1
7511       (when gnus-last-article
7512         (gnus-summary-goto-article gnus-last-article nil t))
7513     (gnus-summary-position-point)))
7514
7515 (defun gnus-summary-pop-article (number)
7516   "Pop one article off the history and go to the previous.
7517 NUMBER articles will be popped off."
7518   (interactive "p")
7519   (let (to)
7520     (setq gnus-newsgroup-history
7521           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7522     (if to
7523         (gnus-summary-goto-article (car to) nil t)
7524       (error "Article history empty")))
7525   (gnus-summary-position-point))
7526
7527 ;; Summary commands and functions for limiting the summary buffer.
7528
7529 (defun gnus-summary-limit-to-articles (n)
7530   "Limit the summary buffer to the next N articles.
7531 If not given a prefix, use the process marked articles instead."
7532   (interactive "P")
7533   (prog1
7534       (let ((articles (gnus-summary-work-articles n)))
7535         (setq gnus-newsgroup-processable nil)
7536         (gnus-summary-limit articles))
7537     (gnus-summary-position-point)))
7538
7539 (defun gnus-summary-pop-limit (&optional total)
7540   "Restore the previous limit.
7541 If given a prefix, remove all limits."
7542   (interactive "P")
7543   (when total
7544     (setq gnus-newsgroup-limits
7545           (list (mapcar (lambda (h) (mail-header-number h))
7546                         gnus-newsgroup-headers))))
7547   (unless gnus-newsgroup-limits
7548     (error "No limit to pop"))
7549   (prog1
7550       (gnus-summary-limit nil 'pop)
7551     (gnus-summary-position-point)))
7552
7553 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7554   "Limit the summary buffer to articles that have subjects that match a regexp.
7555 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7556   (interactive
7557    (list (read-string (if current-prefix-arg
7558                           "Exclude subject (regexp): "
7559                         "Limit to subject (regexp): "))
7560          nil current-prefix-arg))
7561   (unless header
7562     (setq header "subject"))
7563   (when (not (equal "" subject))
7564     (prog1
7565         (let ((articles (gnus-summary-find-matching
7566                          (or header "subject") subject 'all nil nil
7567                          not-matching)))
7568           (unless articles
7569             (error "Found no matches for \"%s\"" subject))
7570           (gnus-summary-limit articles))
7571       (gnus-summary-position-point))))
7572
7573 (defun gnus-summary-limit-to-author (from &optional not-matching)
7574   "Limit the summary buffer to articles that have authors that match a regexp.
7575 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7576   (interactive
7577    (list (read-string (if current-prefix-arg
7578                           "Exclude author (regexp): "
7579                         "Limit to author (regexp): "))
7580          current-prefix-arg))
7581   (gnus-summary-limit-to-subject from "from" not-matching))
7582
7583 (defun gnus-summary-limit-to-age (age &optional younger-p)
7584   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7585 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7586 articles that are younger than AGE days."
7587   (interactive
7588    (let ((younger current-prefix-arg)
7589          (days-got nil)
7590          days)
7591      (while (not days-got)
7592        (setq days (if younger
7593                       (read-string "Limit to articles younger than (in days, older when negative): ")
7594                     (read-string
7595                      "Limit to articles older than (in days, younger when negative): ")))
7596        (when (> (length days) 0)
7597          (setq days (read days)))
7598        (if (numberp days)
7599            (progn
7600              (setq days-got t)
7601              (if (< days 0)
7602                  (progn
7603                    (setq younger (not younger))
7604                    (setq days (* days -1)))))
7605          (message "Please enter a number.")
7606          (sleep-for 1)))
7607      (list days younger)))
7608   (prog1
7609       (let ((data gnus-newsgroup-data)
7610             (cutoff (days-to-time age))
7611             articles d date is-younger)
7612         (while (setq d (pop data))
7613           (when (and (vectorp (gnus-data-header d))
7614                      (setq date (mail-header-date (gnus-data-header d))))
7615             (setq is-younger (time-less-p
7616                               (time-since (condition-case ()
7617                                               (date-to-time date)
7618                                             (error '(0 0))))
7619                               cutoff))
7620             (when (if younger-p
7621                       is-younger
7622                     (not is-younger))
7623               (push (gnus-data-number d) articles))))
7624         (gnus-summary-limit (nreverse articles)))
7625     (gnus-summary-position-point)))
7626
7627 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7628   "Limit the summary buffer to articles that match an 'extra' header."
7629   (interactive
7630    (let ((header
7631           (intern
7632            (gnus-completing-read-with-default
7633             (symbol-name (car gnus-extra-headers))
7634             (if current-prefix-arg
7635                 "Exclude extra header:"
7636               "Limit extra header:")
7637             (mapcar (lambda (x)
7638                       (cons (symbol-name x) x))
7639                     gnus-extra-headers)
7640             nil
7641             t))))
7642      (list header
7643            (read-string (format "%s header %s (regexp): "
7644                                 (if current-prefix-arg "Exclude" "Limit to")
7645                                 header))
7646            current-prefix-arg)))
7647   (when (not (equal "" regexp))
7648     (prog1
7649         (let ((articles (gnus-summary-find-matching
7650                          (cons 'extra header) regexp 'all nil nil
7651                          not-matching)))
7652           (unless articles
7653             (error "Found no matches for \"%s\"" regexp))
7654           (gnus-summary-limit articles))
7655       (gnus-summary-position-point))))
7656
7657 (defun gnus-summary-limit-to-display-predicate ()
7658   "Limit the summary buffer to the predicated in the `display' group parameter."
7659   (interactive)
7660   (unless gnus-newsgroup-display
7661     (error "There is no `display' group parameter"))
7662   (let (articles)
7663     (dolist (number gnus-newsgroup-articles)
7664       (when (funcall gnus-newsgroup-display)
7665         (push number articles)))
7666     (gnus-summary-limit articles))
7667   (gnus-summary-position-point))
7668
7669 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7670 (make-obsolete
7671  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7672
7673 (defun gnus-summary-limit-to-unread (&optional all)
7674   "Limit the summary buffer to articles that are not marked as read.
7675 If ALL is non-nil, limit strictly to unread articles."
7676   (interactive "P")
7677   (if all
7678       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7679     (gnus-summary-limit-to-marks
7680      ;; Concat all the marks that say that an article is read and have
7681      ;; those removed.
7682      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7683            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7684            gnus-low-score-mark gnus-expirable-mark
7685            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7686            gnus-duplicate-mark gnus-souped-mark)
7687      'reverse)))
7688
7689 (defun gnus-summary-limit-to-replied (&optional unreplied)
7690   "Limit the summary buffer to replied articles.
7691 If UNREPLIED (the prefix), limit to unreplied articles."
7692   (interactive "P")
7693   (if unreplied
7694       (gnus-summary-limit
7695        (gnus-set-difference gnus-newsgroup-articles
7696         gnus-newsgroup-replied))
7697     (gnus-summary-limit gnus-newsgroup-replied))
7698   (gnus-summary-position-point))
7699
7700 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7701 (make-obsolete 'gnus-summary-delete-marked-with
7702                'gnus-summary-limit-exclude-marks)
7703
7704 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7705   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7706 If REVERSE, limit the summary buffer to articles that are marked
7707 with MARKS.  MARKS can either be a string of marks or a list of marks.
7708 Returns how many articles were removed."
7709   (interactive "sMarks: ")
7710   (gnus-summary-limit-to-marks marks t))
7711
7712 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7713   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7714 If REVERSE (the prefix), limit the summary buffer to articles that are
7715 not marked with MARKS.  MARKS can either be a string of marks or a
7716 list of marks.
7717 Returns how many articles were removed."
7718   (interactive "sMarks: \nP")
7719   (prog1
7720       (let ((data gnus-newsgroup-data)
7721             (marks (if (listp marks) marks
7722                      (append marks nil))) ; Transform to list.
7723             articles)
7724         (while data
7725           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7726                   (memq (gnus-data-mark (car data)) marks))
7727             (push (gnus-data-number (car data)) articles))
7728           (setq data (cdr data)))
7729         (gnus-summary-limit articles))
7730     (gnus-summary-position-point)))
7731
7732 (defun gnus-summary-limit-to-score (score)
7733   "Limit to articles with score at or above SCORE."
7734   (interactive "NLimit to articles with score of at least: ")
7735   (let ((data gnus-newsgroup-data)
7736         articles)
7737     (while data
7738       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7739                 score)
7740         (push (gnus-data-number (car data)) articles))
7741       (setq data (cdr data)))
7742     (prog1
7743         (gnus-summary-limit articles)
7744       (gnus-summary-position-point))))
7745
7746 (defun gnus-summary-limit-to-unseen ()
7747   "Limit to unseen articles."
7748   (interactive)
7749   (prog1
7750       (gnus-summary-limit gnus-newsgroup-unseen)
7751     (gnus-summary-position-point)))
7752
7753 (defun gnus-summary-limit-include-thread (id)
7754   "Display all the hidden articles that is in the thread with ID in it.
7755 When called interactively, ID is the Message-ID of the current
7756 article."
7757   (interactive (list (mail-header-id (gnus-summary-article-header))))
7758   (let ((articles (gnus-articles-in-thread
7759                    (gnus-id-to-thread (gnus-root-id id)))))
7760     (prog1
7761         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7762       (gnus-summary-limit-include-matching-articles
7763        "subject"
7764        (regexp-quote (gnus-simplify-subject-re
7765                       (mail-header-subject (gnus-id-to-header id)))))
7766       (gnus-summary-position-point))))
7767
7768 (defun gnus-summary-limit-include-matching-articles (header regexp)
7769   "Display all the hidden articles that have HEADERs that match REGEXP."
7770   (interactive (list (read-string "Match on header: ")
7771                      (read-string "Regexp: ")))
7772   (let ((articles (gnus-find-matching-articles header regexp)))
7773     (prog1
7774         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7775       (gnus-summary-position-point))))
7776
7777 (defun gnus-summary-insert-dormant-articles ()
7778   "Insert all the dormant articles for this group into the current buffer."
7779   (interactive)
7780   (let ((gnus-verbose (max 6 gnus-verbose)))
7781     (if (not gnus-newsgroup-dormant)
7782         (gnus-message 3 "No cached articles for this group")
7783       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7784
7785 (defun gnus-summary-limit-include-dormant ()
7786   "Display all the hidden articles that are marked as dormant.
7787 Note that this command only works on a subset of the articles currently
7788 fetched for this group."
7789   (interactive)
7790   (unless gnus-newsgroup-dormant
7791     (error "There are no dormant articles in this group"))
7792   (prog1
7793       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7794     (gnus-summary-position-point)))
7795
7796 (defun gnus-summary-limit-exclude-dormant ()
7797   "Hide all dormant articles."
7798   (interactive)
7799   (prog1
7800       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7801     (gnus-summary-position-point)))
7802
7803 (defun gnus-summary-limit-exclude-childless-dormant ()
7804   "Hide all dormant articles that have no children."
7805   (interactive)
7806   (let ((data (gnus-data-list t))
7807         articles d children)
7808     ;; Find all articles that are either not dormant or have
7809     ;; children.
7810     (while (setq d (pop data))
7811       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7812                 (and (setq children
7813                            (gnus-article-children (gnus-data-number d)))
7814                      (let (found)
7815                        (while children
7816                          (when (memq (car children) articles)
7817                            (setq children nil
7818                                  found t))
7819                          (pop children))
7820                        found)))
7821         (push (gnus-data-number d) articles)))
7822     ;; Do the limiting.
7823     (prog1
7824         (gnus-summary-limit articles)
7825       (gnus-summary-position-point))))
7826
7827 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7828   "Mark all unread excluded articles as read.
7829 If ALL, mark even excluded ticked and dormants as read."
7830   (interactive "P")
7831   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7832   (let ((articles (gnus-sorted-ndifference
7833                    (sort
7834                     (mapcar (lambda (h) (mail-header-number h))
7835                             gnus-newsgroup-headers)
7836                     '<)
7837                    gnus-newsgroup-limit))
7838         article)
7839     (setq gnus-newsgroup-unreads
7840           (gnus-sorted-intersection gnus-newsgroup-unreads
7841                                     gnus-newsgroup-limit))
7842     (if all
7843         (setq gnus-newsgroup-dormant nil
7844               gnus-newsgroup-marked nil
7845               gnus-newsgroup-reads
7846               (nconc
7847                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7848                gnus-newsgroup-reads))
7849       (while (setq article (pop articles))
7850         (unless (or (memq article gnus-newsgroup-dormant)
7851                     (memq article gnus-newsgroup-marked))
7852           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7853
7854 (defun gnus-summary-limit (articles &optional pop)
7855   (if pop
7856       ;; We pop the previous limit off the stack and use that.
7857       (setq articles (car gnus-newsgroup-limits)
7858             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7859     ;; We use the new limit, so we push the old limit on the stack.
7860     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7861   ;; Set the limit.
7862   (setq gnus-newsgroup-limit articles)
7863   (let ((total (length gnus-newsgroup-data))
7864         (data (gnus-data-find-list (gnus-summary-article-number)))
7865         (gnus-summary-mark-below nil)   ; Inhibit this.
7866         found)
7867     ;; This will do all the work of generating the new summary buffer
7868     ;; according to the new limit.
7869     (gnus-summary-prepare)
7870     ;; Hide any threads, possibly.
7871     (gnus-summary-maybe-hide-threads)
7872     ;; Try to return to the article you were at, or one in the
7873     ;; neighborhood.
7874     (when data
7875       ;; We try to find some article after the current one.
7876       (while data
7877         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7878           (setq data nil
7879                 found t))
7880         (setq data (cdr data))))
7881     (unless found
7882       ;; If there is no data, that means that we were after the last
7883       ;; article.  The same goes when we can't find any articles
7884       ;; after the current one.
7885       (goto-char (point-max))
7886       (gnus-summary-find-prev))
7887     (gnus-set-mode-line 'summary)
7888     ;; We return how many articles were removed from the summary
7889     ;; buffer as a result of the new limit.
7890     (- total (length gnus-newsgroup-data))))
7891
7892 (defsubst gnus-invisible-cut-children (threads)
7893   (let ((num 0))
7894     (while threads
7895       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7896         (incf num))
7897       (pop threads))
7898     (< num 2)))
7899
7900 (defsubst gnus-cut-thread (thread)
7901   "Go forwards in the thread until we find an article that we want to display."
7902   (when (or (eq gnus-fetch-old-headers 'some)
7903             (eq gnus-fetch-old-headers 'invisible)
7904             (numberp gnus-fetch-old-headers)
7905             (eq gnus-build-sparse-threads 'some)
7906             (eq gnus-build-sparse-threads 'more))
7907     ;; Deal with old-fetched headers and sparse threads.
7908     (while (and
7909             thread
7910             (or
7911              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7912              (gnus-summary-article-ancient-p
7913               (mail-header-number (car thread))))
7914             (if (or (<= (length (cdr thread)) 1)
7915                     (eq gnus-fetch-old-headers 'invisible))
7916                 (setq gnus-newsgroup-limit
7917                       (delq (mail-header-number (car thread))
7918                             gnus-newsgroup-limit)
7919                       thread (cadr thread))
7920               (when (gnus-invisible-cut-children (cdr thread))
7921                 (let ((th (cdr thread)))
7922                   (while th
7923                     (if (memq (mail-header-number (caar th))
7924                               gnus-newsgroup-limit)
7925                         (setq thread (car th)
7926                               th nil)
7927                       (setq th (cdr th))))))))))
7928   thread)
7929
7930 (defun gnus-cut-threads (threads)
7931   "Cut off all uninteresting articles from the beginning of THREADS."
7932   (when (or (eq gnus-fetch-old-headers 'some)
7933             (eq gnus-fetch-old-headers 'invisible)
7934             (numberp gnus-fetch-old-headers)
7935             (eq gnus-build-sparse-threads 'some)
7936             (eq gnus-build-sparse-threads 'more))
7937     (let ((th threads))
7938       (while th
7939         (setcar th (gnus-cut-thread (car th)))
7940         (setq th (cdr th)))))
7941   ;; Remove nixed out threads.
7942   (delq nil threads))
7943
7944 (defun gnus-summary-initial-limit (&optional show-if-empty)
7945   "Figure out what the initial limit is supposed to be on group entry.
7946 This entails weeding out unwanted dormants, low-scored articles,
7947 fetch-old-headers verbiage, and so on."
7948   ;; Most groups have nothing to remove.
7949   (if (or gnus-inhibit-limiting
7950           (and (null gnus-newsgroup-dormant)
7951                (eq gnus-newsgroup-display 'gnus-not-ignore)
7952                (not (eq gnus-fetch-old-headers 'some))
7953                (not (numberp gnus-fetch-old-headers))
7954                (not (eq gnus-fetch-old-headers 'invisible))
7955                (null gnus-summary-expunge-below)
7956                (not (eq gnus-build-sparse-threads 'some))
7957                (not (eq gnus-build-sparse-threads 'more))
7958                (null gnus-thread-expunge-below)
7959                (not gnus-use-nocem)))
7960       ()                                ; Do nothing.
7961     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7962     (setq gnus-newsgroup-limit nil)
7963     (mapatoms
7964      (lambda (node)
7965        (unless (car (symbol-value node))
7966          ;; These threads have no parents -- they are roots.
7967          (let ((nodes (cdr (symbol-value node)))
7968                thread)
7969            (while nodes
7970              (if (and gnus-thread-expunge-below
7971                       (< (gnus-thread-total-score (car nodes))
7972                          gnus-thread-expunge-below))
7973                  (gnus-expunge-thread (pop nodes))
7974                (setq thread (pop nodes))
7975                (gnus-summary-limit-children thread))))))
7976      gnus-newsgroup-dependencies)
7977     ;; If this limitation resulted in an empty group, we might
7978     ;; pop the previous limit and use it instead.
7979     (when (and (not gnus-newsgroup-limit)
7980                show-if-empty)
7981       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7982     gnus-newsgroup-limit))
7983
7984 (defun gnus-summary-limit-children (thread)
7985   "Return 1 if this subthread is visible and 0 if it is not."
7986   ;; First we get the number of visible children to this thread.  This
7987   ;; is done by recursing down the thread using this function, so this
7988   ;; will really go down to a leaf article first, before slowly
7989   ;; working its way up towards the root.
7990   (when thread
7991     (let* ((max-lisp-eval-depth 5000)
7992            (children
7993            (if (cdr thread)
7994                (apply '+ (mapcar 'gnus-summary-limit-children
7995                                  (cdr thread)))
7996              0))
7997           (number (mail-header-number (car thread)))
7998           score)
7999       (if (and
8000            (not (memq number gnus-newsgroup-marked))
8001            (or
8002             ;; If this article is dormant and has absolutely no visible
8003             ;; children, then this article isn't visible.
8004             (and (memq number gnus-newsgroup-dormant)
8005                  (zerop children))
8006             ;; If this is "fetch-old-headered" and there is no
8007             ;; visible children, then we don't want this article.
8008             (and (or (eq gnus-fetch-old-headers 'some)
8009                      (numberp gnus-fetch-old-headers))
8010                  (gnus-summary-article-ancient-p number)
8011                  (zerop children))
8012             ;; If this is "fetch-old-headered" and `invisible', then
8013             ;; we don't want this article.
8014             (and (eq gnus-fetch-old-headers 'invisible)
8015                  (gnus-summary-article-ancient-p number))
8016             ;; If this is a sparsely inserted article with no children,
8017             ;; we don't want it.
8018             (and (eq gnus-build-sparse-threads 'some)
8019                  (gnus-summary-article-sparse-p number)
8020                  (zerop children))
8021             ;; If we use expunging, and this article is really
8022             ;; low-scored, then we don't want this article.
8023             (when (and gnus-summary-expunge-below
8024                        (< (setq score
8025                                 (or (cdr (assq number gnus-newsgroup-scored))
8026                                     gnus-summary-default-score))
8027                           gnus-summary-expunge-below))
8028               ;; We increase the expunge-tally here, but that has
8029               ;; nothing to do with the limits, really.
8030               (incf gnus-newsgroup-expunged-tally)
8031               ;; We also mark as read here, if that's wanted.
8032               (when (and gnus-summary-mark-below
8033                          (< score gnus-summary-mark-below))
8034                 (setq gnus-newsgroup-unreads
8035                       (delq number gnus-newsgroup-unreads))
8036                 (if gnus-newsgroup-auto-expire
8037                     (push number gnus-newsgroup-expirable)
8038                   (push (cons number gnus-low-score-mark)
8039                         gnus-newsgroup-reads)))
8040               t)
8041             ;; Do the `display' group parameter.
8042             (and gnus-newsgroup-display
8043                  (not (funcall gnus-newsgroup-display)))
8044             ;; Check NoCeM things.
8045             (if (and gnus-use-nocem
8046                      (gnus-nocem-unwanted-article-p
8047                       (mail-header-id (car thread))))
8048                 (progn
8049                   (setq gnus-newsgroup-unreads
8050                         (delq number gnus-newsgroup-unreads))
8051                   t))))
8052           ;; Nope, invisible article.
8053           0
8054         ;; Ok, this article is to be visible, so we add it to the limit
8055         ;; and return 1.
8056         (push number gnus-newsgroup-limit)
8057         1))))
8058
8059 (defun gnus-expunge-thread (thread)
8060   "Mark all articles in THREAD as read."
8061   (let* ((number (mail-header-number (car thread))))
8062     (incf gnus-newsgroup-expunged-tally)
8063     ;; We also mark as read here, if that's wanted.
8064     (setq gnus-newsgroup-unreads
8065           (delq number gnus-newsgroup-unreads))
8066     (if gnus-newsgroup-auto-expire
8067         (push number gnus-newsgroup-expirable)
8068       (push (cons number gnus-low-score-mark)
8069             gnus-newsgroup-reads)))
8070   ;; Go recursively through all subthreads.
8071   (mapcar 'gnus-expunge-thread (cdr thread)))
8072
8073 ;; Summary article oriented commands
8074
8075 (defun gnus-summary-refer-parent-article (n)
8076   "Refer parent article N times.
8077 If N is negative, go to ancestor -N instead.
8078 The difference between N and the number of articles fetched is returned."
8079   (interactive "p")
8080   (let ((skip 1)
8081         error header ref)
8082     (when (not (natnump n))
8083       (setq skip (abs n)
8084             n 1))
8085     (while (and (> n 0)
8086                 (not error))
8087       (setq header (gnus-summary-article-header))
8088       (if (and (eq (mail-header-number header)
8089                    (cdr gnus-article-current))
8090                (equal gnus-newsgroup-name
8091                       (car gnus-article-current)))
8092           ;; If we try to find the parent of the currently
8093           ;; displayed article, then we take a look at the actual
8094           ;; References header, since this is slightly more
8095           ;; reliable than the References field we got from the
8096           ;; server.
8097           (save-excursion
8098             (set-buffer gnus-original-article-buffer)
8099             (nnheader-narrow-to-headers)
8100             (unless (setq ref (message-fetch-field "references"))
8101               (when (setq ref (message-fetch-field "in-reply-to"))
8102                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8103             (widen))
8104         (setq ref
8105               ;; It's not the current article, so we take a bet on
8106               ;; the value we got from the server.
8107               (mail-header-references header)))
8108       (if (and ref
8109                (not (equal ref "")))
8110           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8111             (gnus-message 1 "Couldn't find parent"))
8112         (gnus-message 1 "No references in article %d"
8113                       (gnus-summary-article-number))
8114         (setq error t))
8115       (decf n))
8116     (gnus-summary-position-point)
8117     n))
8118
8119 (defun gnus-summary-refer-references ()
8120   "Fetch all articles mentioned in the References header.
8121 Return the number of articles fetched."
8122   (interactive)
8123   (let ((ref (mail-header-references (gnus-summary-article-header)))
8124         (current (gnus-summary-article-number))
8125         (n 0))
8126     (if (or (not ref)
8127             (equal ref ""))
8128         (error "No References in the current article")
8129       ;; For each Message-ID in the References header...
8130       (while (string-match "<[^>]*>" ref)
8131         (incf n)
8132         ;; ... fetch that article.
8133         (gnus-summary-refer-article
8134          (prog1 (match-string 0 ref)
8135            (setq ref (substring ref (match-end 0))))))
8136       (gnus-summary-goto-subject current)
8137       (gnus-summary-position-point)
8138       n)))
8139
8140 (defun gnus-summary-refer-thread (&optional limit)
8141   "Fetch all articles in the current thread.
8142 If LIMIT (the numerical prefix), fetch that many old headers instead
8143 of what's specified by the `gnus-refer-thread-limit' variable."
8144   (interactive "P")
8145   (let ((id (mail-header-id (gnus-summary-article-header)))
8146         (limit (if limit (prefix-numeric-value limit)
8147                  gnus-refer-thread-limit)))
8148     (unless (eq gnus-fetch-old-headers 'invisible)
8149       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8150       ;; Retrieve the headers and read them in.
8151       (if (eq (if (numberp limit)
8152                   (gnus-retrieve-headers
8153                    (list (min
8154                           (+ (mail-header-number
8155                               (gnus-summary-article-header))
8156                              limit)
8157                           gnus-newsgroup-end))
8158                    gnus-newsgroup-name (* limit 2))
8159                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8160                 ;; headers.
8161                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8162                                        gnus-newsgroup-name limit))
8163               'nov)
8164           (gnus-build-all-threads)
8165         (error "Can't fetch thread from back ends that don't support NOV"))
8166       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8167     (gnus-summary-limit-include-thread id)))
8168
8169 (defun gnus-summary-refer-article (message-id)
8170   "Fetch an article specified by MESSAGE-ID."
8171   (interactive "sMessage-ID: ")
8172   (when (and (stringp message-id)
8173              (not (zerop (length message-id))))
8174     (setq message-id (gnus-replace-in-string message-id " " ""))
8175     ;; Construct the correct Message-ID if necessary.
8176     ;; Suggested by tale@pawl.rpi.edu.
8177     (unless (string-match "^<" message-id)
8178       (setq message-id (concat "<" message-id)))
8179     (unless (string-match ">$" message-id)
8180       (setq message-id (concat message-id ">")))
8181     ;; People often post MIDs from URLs, so unhex it:
8182     (unless (string-match "@" message-id)
8183       (setq message-id (gnus-url-unhex-string message-id)))
8184     (let* ((header (gnus-id-to-header message-id))
8185            (sparse (and header
8186                         (gnus-summary-article-sparse-p
8187                          (mail-header-number header))
8188                         (memq (mail-header-number header)
8189                               gnus-newsgroup-limit)))
8190            number)
8191       (cond
8192        ;; If the article is present in the buffer we just go to it.
8193        ((and header
8194              (or (not (gnus-summary-article-sparse-p
8195                        (mail-header-number header)))
8196                  sparse))
8197         (prog1
8198             (gnus-summary-goto-article
8199              (mail-header-number header) nil t)
8200           (when sparse
8201             (gnus-summary-update-article (mail-header-number header)))))
8202        (t
8203         ;; We fetch the article.
8204         (catch 'found
8205           (dolist (gnus-override-method (gnus-refer-article-methods))
8206             (when (and (gnus-check-server gnus-override-method)
8207                        ;; Fetch the header,
8208                        (setq number (gnus-summary-insert-subject message-id)))
8209               ;; and display the article.
8210               (gnus-summary-select-article nil nil nil number)
8211               (throw 'found t)))
8212           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8213
8214 (defun gnus-refer-article-methods ()
8215   "Return a list of referable methods."
8216   (cond
8217    ;; No method, so we default to current and native.
8218    ((null gnus-refer-article-method)
8219     (list gnus-current-select-method gnus-select-method))
8220    ;; Current.
8221    ((eq 'current gnus-refer-article-method)
8222     (list gnus-current-select-method))
8223    ;; List of select methods.
8224    ((not (and (symbolp (car gnus-refer-article-method))
8225               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8226     (let (out)
8227       (dolist (method gnus-refer-article-method)
8228         (push (if (eq 'current method)
8229                   gnus-current-select-method
8230                 method)
8231               out))
8232       (nreverse out)))
8233    ;; One single select method.
8234    (t
8235     (list gnus-refer-article-method))))
8236
8237 (defun gnus-summary-edit-parameters ()
8238   "Edit the group parameters of the current group."
8239   (interactive)
8240   (gnus-group-edit-group gnus-newsgroup-name 'params))
8241
8242 (defun gnus-summary-customize-parameters ()
8243   "Customize the group parameters of the current group."
8244   (interactive)
8245   (gnus-group-customize gnus-newsgroup-name))
8246
8247 (defun gnus-summary-enter-digest-group (&optional force)
8248   "Enter an nndoc group based on the current article.
8249 If FORCE, force a digest interpretation.  If not, try
8250 to guess what the document format is."
8251   (interactive "P")
8252   (let ((conf gnus-current-window-configuration))
8253     (save-window-excursion
8254       (save-excursion
8255         (let (gnus-article-prepare-hook
8256               gnus-display-mime-function
8257               gnus-break-pages)
8258           (gnus-summary-select-article))))
8259     (setq gnus-current-window-configuration conf)
8260     (let* ((name (format "%s-%d"
8261                          (gnus-group-prefixed-name
8262                           gnus-newsgroup-name (list 'nndoc ""))
8263                          (save-excursion
8264                            (set-buffer gnus-summary-buffer)
8265                            gnus-current-article)))
8266            (ogroup gnus-newsgroup-name)
8267            (params (append (gnus-info-params (gnus-get-info ogroup))
8268                            (list (cons 'to-group ogroup))
8269                            (list (cons 'parent-group ogroup))
8270                            (list (cons 'save-article-group ogroup))))
8271            (case-fold-search t)
8272            (buf (current-buffer))
8273            dig to-address)
8274       (save-excursion
8275         (set-buffer gnus-original-article-buffer)
8276         ;; Have the digest group inherit the main mail address of
8277         ;; the parent article.
8278         (when (setq to-address (or (gnus-fetch-field "reply-to")
8279                                    (gnus-fetch-field "from")))
8280           (setq params (append
8281                         (list (cons 'to-address
8282                                     (funcall gnus-decode-encoded-word-function
8283                                              to-address))))))
8284         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8285         (insert-buffer-substring gnus-original-article-buffer)
8286         ;; Remove lines that may lead nndoc to misinterpret the
8287         ;; document type.
8288         (narrow-to-region
8289          (goto-char (point-min))
8290          (or (search-forward "\n\n" nil t) (point)))
8291         (goto-char (point-min))
8292         (delete-matching-lines "^Path:\\|^From ")
8293         (widen))
8294       (unwind-protect
8295           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8296                     (gnus-newsgroup-ephemeral-ignored-charsets
8297                      gnus-newsgroup-ignored-charsets))
8298                 (gnus-group-read-ephemeral-group
8299                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8300                               (nndoc-article-type
8301                                ,(if force 'mbox 'guess)))
8302                  t nil nil nil
8303                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8304                                                         "ADAPT")))))
8305               ;; Make all postings to this group go to the parent group.
8306               (nconc (gnus-info-params (gnus-get-info name))
8307                      params)
8308             ;; Couldn't select this doc group.
8309             (switch-to-buffer buf)
8310             (gnus-set-global-variables)
8311             (gnus-configure-windows 'summary)
8312             (gnus-message 3 "Article couldn't be entered?"))
8313         (kill-buffer dig)))))
8314
8315 (defun gnus-summary-read-document (n)
8316   "Open a new group based on the current article(s).
8317 This will allow you to read digests and other similar
8318 documents as newsgroups.
8319 Obeys the standard process/prefix convention."
8320   (interactive "P")
8321   (let* ((articles (gnus-summary-work-articles n))
8322          (ogroup gnus-newsgroup-name)
8323          (params (append (gnus-info-params (gnus-get-info ogroup))
8324                          (list (cons 'to-group ogroup))))
8325          article group egroup groups vgroup)
8326     (while (setq article (pop articles))
8327       (setq group (format "%s-%d" gnus-newsgroup-name article))
8328       (gnus-summary-remove-process-mark article)
8329       (when (gnus-summary-display-article article)
8330         (save-excursion
8331           (with-temp-buffer
8332             (insert-buffer-substring gnus-original-article-buffer)
8333             ;; Remove some headers that may lead nndoc to make
8334             ;; the wrong guess.
8335             (message-narrow-to-head)
8336             (goto-char (point-min))
8337             (delete-matching-lines "^Path:\\|^From ")
8338             (widen)
8339             (if (setq egroup
8340                       (gnus-group-read-ephemeral-group
8341                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8342                                      (nndoc-article-type guess))
8343                        t nil t))
8344                 (progn
8345             ;; Make all postings to this group go to the parent group.
8346                   (nconc (gnus-info-params (gnus-get-info egroup))
8347                          params)
8348                   (push egroup groups))
8349               ;; Couldn't select this doc group.
8350               (gnus-error 3 "Article couldn't be entered"))))))
8351     ;; Now we have selected all the documents.
8352     (cond
8353      ((not groups)
8354       (error "None of the articles could be interpreted as documents"))
8355      ((gnus-group-read-ephemeral-group
8356        (setq vgroup (format
8357                      "nnvirtual:%s-%s" gnus-newsgroup-name
8358                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8359        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8360        t
8361        (cons (current-buffer) 'summary)))
8362      (t
8363       (error "Couldn't select virtual nndoc group")))))
8364
8365 (defun gnus-summary-isearch-article (&optional regexp-p)
8366   "Do incremental search forward on the current article.
8367 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8368   (interactive "P")
8369   (gnus-summary-select-article)
8370   (gnus-configure-windows 'article)
8371   (gnus-eval-in-buffer-window gnus-article-buffer
8372     (save-restriction
8373       (widen)
8374       (isearch-forward regexp-p))))
8375
8376 (defun gnus-summary-search-article-forward (regexp &optional backward)
8377   "Search for an article containing REGEXP forward.
8378 If BACKWARD, search backward instead."
8379   (interactive
8380    (list (read-string
8381           (format "Search article %s (regexp%s): "
8382                   (if current-prefix-arg "backward" "forward")
8383                   (if gnus-last-search-regexp
8384                       (concat ", default " gnus-last-search-regexp)
8385                     "")))
8386          current-prefix-arg))
8387   (if (string-equal regexp "")
8388       (setq regexp (or gnus-last-search-regexp ""))
8389     (setq gnus-last-search-regexp regexp)
8390     (setq gnus-article-before-search gnus-current-article))
8391   ;; Intentionally set gnus-last-article.
8392   (setq gnus-last-article gnus-article-before-search)
8393   (let ((gnus-last-article gnus-last-article))
8394     (if (gnus-summary-search-article regexp backward)
8395         (gnus-summary-show-thread)
8396       (error "Search failed: \"%s\"" regexp))))
8397
8398 (defun gnus-summary-search-article-backward (regexp)
8399   "Search for an article containing REGEXP backward."
8400   (interactive
8401    (list (read-string
8402           (format "Search article backward (regexp%s): "
8403                   (if gnus-last-search-regexp
8404                       (concat ", default " gnus-last-search-regexp)
8405                     "")))))
8406   (gnus-summary-search-article-forward regexp 'backward))
8407
8408 (defun gnus-summary-search-article (regexp &optional backward)
8409   "Search for an article containing REGEXP.
8410 Optional argument BACKWARD means do search for backward.
8411 `gnus-select-article-hook' is not called during the search."
8412   ;; We have to require this here to make sure that the following
8413   ;; dynamic binding isn't shadowed by autoloading.
8414   (require 'gnus-async)
8415   (require 'gnus-art)
8416   (let ((gnus-select-article-hook nil)  ;Disable hook.
8417         (gnus-article-prepare-hook nil)
8418         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8419         (gnus-use-article-prefetch nil)
8420         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8421         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8422         (gnus-visual nil)
8423         (gnus-keep-backlog nil)
8424         (gnus-break-pages nil)
8425         (gnus-summary-display-arrow nil)
8426         (gnus-updated-mode-lines nil)
8427         (gnus-auto-center-summary nil)
8428         (sum (current-buffer))
8429         (gnus-display-mime-function nil)
8430         (found nil)
8431         point)
8432     (gnus-save-hidden-threads
8433       (gnus-summary-select-article)
8434       (set-buffer gnus-article-buffer)
8435       (goto-char (window-point (get-buffer-window (current-buffer))))
8436       (when backward
8437         (forward-line -1))
8438       (while (not found)
8439         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8440         (if (if backward
8441                 (re-search-backward regexp nil t)
8442               (re-search-forward regexp nil t))
8443             ;; We found the regexp.
8444             (progn
8445               (setq found 'found)
8446               (beginning-of-line)
8447               (set-window-start
8448                (get-buffer-window (current-buffer))
8449                (point))
8450               (forward-line 1)
8451               (set-window-point
8452                (get-buffer-window (current-buffer))
8453                (point))
8454               (set-buffer sum)
8455               (setq point (point)))
8456           ;; We didn't find it, so we go to the next article.
8457           (set-buffer sum)
8458           (setq found 'not)
8459           (while (eq found 'not)
8460             (if (not (if backward (gnus-summary-find-prev)
8461                        (gnus-summary-find-next)))
8462                 ;; No more articles.
8463                 (setq found t)
8464               ;; Select the next article and adjust point.
8465               (unless (gnus-summary-article-sparse-p
8466                        (gnus-summary-article-number))
8467                 (setq found nil)
8468                 (gnus-summary-select-article)
8469                 (set-buffer gnus-article-buffer)
8470                 (widen)
8471                 (goto-char (if backward (point-max) (point-min))))))))
8472       (gnus-message 7 ""))
8473     ;; Return whether we found the regexp.
8474     (when (eq found 'found)
8475       (goto-char point)
8476       (gnus-summary-show-thread)
8477       (gnus-summary-goto-subject gnus-current-article)
8478       (gnus-summary-position-point)
8479       t)))
8480
8481 (defun gnus-find-matching-articles (header regexp)
8482   "Return a list of all articles that match REGEXP on HEADER.
8483 This search includes all articles in the current group that Gnus has
8484 fetched headers for, whether they are displayed or not."
8485   (let ((articles nil)
8486         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8487         (case-fold-search t))
8488     (dolist (header gnus-newsgroup-headers)
8489       (when (string-match regexp (funcall func header))
8490         (push (mail-header-number header) articles)))
8491     (nreverse articles)))
8492
8493 (defun gnus-summary-find-matching (header regexp &optional backward unread
8494                                           not-case-fold not-matching)
8495   "Return a list of all articles that match REGEXP on HEADER.
8496 The search stars on the current article and goes forwards unless
8497 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8498 If UNREAD is non-nil, only unread articles will
8499 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8500 in the comparisons. If NOT-MATCHING, return a list of all articles that
8501 not match REGEXP on HEADER."
8502   (let ((case-fold-search (not not-case-fold))
8503         articles d func)
8504     (if (consp header)
8505         (if (eq (car header) 'extra)
8506             (setq func
8507                   `(lambda (h)
8508                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8509                          "")))
8510           (error "%s is an invalid header" header))
8511       (unless (fboundp (intern (concat "mail-header-" header)))
8512         (error "%s is not a valid header" header))
8513       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8514     (dolist (d (if (eq backward 'all)
8515                    gnus-newsgroup-data
8516                  (gnus-data-find-list
8517                   (gnus-summary-article-number)
8518                   (gnus-data-list backward))))
8519       (when (and (or (not unread)       ; We want all articles...
8520                      (gnus-data-unread-p d)) ; Or just unreads.
8521                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8522                  (if not-matching
8523                      (not (string-match
8524                            regexp
8525                            (funcall func (gnus-data-header d))))
8526                    (string-match regexp
8527                                  (funcall func (gnus-data-header d)))))
8528         (push (gnus-data-number d) articles))) ; Success!
8529     (nreverse articles)))
8530
8531 (defun gnus-summary-execute-command (header regexp command &optional backward)
8532   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8533 If HEADER is an empty string (or nil), the match is done on the entire
8534 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8535   (interactive
8536    (list (let ((completion-ignore-case t))
8537            (completing-read
8538             "Header name: "
8539             (mapcar (lambda (header) (list (format "%s" header)))
8540                     (append
8541                      '("Number" "Subject" "From" "Lines" "Date"
8542                        "Message-ID" "Xref" "References" "Body")
8543                      gnus-extra-headers))
8544             nil 'require-match))
8545          (read-string "Regexp: ")
8546          (read-key-sequence "Command: ")
8547          current-prefix-arg))
8548   (when (equal header "Body")
8549     (setq header ""))
8550   ;; Hidden thread subtrees must be searched as well.
8551   (gnus-summary-show-all-threads)
8552   ;; We don't want to change current point nor window configuration.
8553   (save-excursion
8554     (save-window-excursion
8555       (let (gnus-visual
8556             gnus-treat-strip-trailing-blank-lines
8557             gnus-treat-strip-leading-blank-lines
8558             gnus-treat-strip-multiple-blank-lines
8559             gnus-treat-hide-boring-headers
8560             gnus-treat-fold-newsgroups
8561             gnus-article-prepare-hook)
8562         (gnus-message 6 "Executing %s..." (key-description command))
8563         ;; We'd like to execute COMMAND interactively so as to give arguments.
8564         (gnus-execute header regexp
8565                       `(call-interactively ',(key-binding command))
8566                       backward)
8567         (gnus-message 6 "Executing %s...done" (key-description command))))))
8568
8569 (defun gnus-summary-beginning-of-article ()
8570   "Scroll the article back to the beginning."
8571   (interactive)
8572   (gnus-summary-select-article)
8573   (gnus-configure-windows 'article)
8574   (gnus-eval-in-buffer-window gnus-article-buffer
8575     (widen)
8576     (goto-char (point-min))
8577     (when gnus-break-pages
8578       (gnus-narrow-to-page))))
8579
8580 (defun gnus-summary-end-of-article ()
8581   "Scroll to the end of the article."
8582   (interactive)
8583   (gnus-summary-select-article)
8584   (gnus-configure-windows 'article)
8585   (gnus-eval-in-buffer-window gnus-article-buffer
8586     (widen)
8587     (goto-char (point-max))
8588     (recenter -3)
8589     (when gnus-break-pages
8590       (when (re-search-backward page-delimiter nil t)
8591         (narrow-to-region (match-end 0) (point-max)))
8592       (gnus-narrow-to-page))))
8593
8594 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8595   "Truncate to LEN and quote all \"(\"'s in STRING."
8596   (gnus-replace-in-string (if (and len (> (length string) len))
8597                               (substring string 0 len)
8598                             string)
8599                           "[()]" "\\\\\\&"))
8600
8601 (defun gnus-summary-print-article (&optional filename n)
8602   "Generate and print a PostScript image of the process-marked (mail) articles.
8603
8604 If used interactively, print the current article if none are
8605 process-marked.  With prefix arg, prompt the user for the name of the
8606 file to save in.
8607
8608 When used from Lisp, accept two optional args FILENAME and N.  N means
8609 to print the next N articles.  If N is negative, print the N previous
8610 articles.  If N is nil and articles have been marked with the process
8611 mark, print these instead.
8612
8613 If the optional first argument FILENAME is nil, send the image to the
8614 printer.  If FILENAME is a string, save the PostScript image in a file with
8615 that name.  If FILENAME is a number, prompt the user for the name of the file
8616 to save in."
8617   (interactive (list (ps-print-preprint current-prefix-arg)))
8618   (dolist (article (gnus-summary-work-articles n))
8619     (gnus-summary-select-article nil nil 'pseudo article)
8620     (gnus-eval-in-buffer-window gnus-article-buffer
8621       (gnus-print-buffer))
8622     (gnus-summary-remove-process-mark article))
8623   (ps-despool filename))
8624
8625 (defun gnus-print-buffer ()
8626   (let ((buffer (generate-new-buffer " *print*")))
8627     (unwind-protect
8628         (progn
8629           (copy-to-buffer buffer (point-min) (point-max))
8630           (set-buffer buffer)
8631           (gnus-remove-text-with-property 'gnus-decoration)
8632           (when (gnus-visual-p 'article-highlight 'highlight)
8633             ;; Copy-to-buffer doesn't copy overlay.  So redo
8634             ;; highlight.
8635             (let ((gnus-article-buffer buffer))
8636               (gnus-article-highlight-citation t)
8637               (gnus-article-highlight-signature)
8638               (gnus-article-emphasize)
8639               (gnus-article-delete-invisible-text)))
8640           (let ((ps-left-header
8641                  (list
8642                   (concat "("
8643                           (gnus-summary-print-truncate-and-quote
8644                            (mail-header-subject gnus-current-headers)
8645                            66) ")")
8646                   (concat "("
8647                           (gnus-summary-print-truncate-and-quote
8648                            (mail-header-from gnus-current-headers)
8649                            45) ")")))
8650                 (ps-right-header
8651                  (list
8652                   "/pagenumberstring load"
8653                   (concat "("
8654                           (mail-header-date gnus-current-headers) ")"))))
8655             (gnus-run-hooks 'gnus-ps-print-hook)
8656             (save-excursion
8657               (if window-system
8658                   (ps-spool-buffer-with-faces)
8659                 (ps-spool-buffer)))))
8660       (kill-buffer buffer))))
8661
8662 (defun gnus-summary-show-article (&optional arg)
8663   "Force redisplaying of the current article.
8664 If ARG (the prefix) is a number, show the article with the charset
8665 defined in `gnus-summary-show-article-charset-alist', or the charset
8666 input.
8667 If ARG (the prefix) is non-nil and not a number, show the raw article
8668 without any article massaging functions being run.  Normally, the key
8669 strokes are `C-u g'."
8670   (interactive "P")
8671   (cond
8672    ((numberp arg)
8673     (gnus-summary-show-article t)
8674     (let ((gnus-newsgroup-charset
8675            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8676                (mm-read-coding-system
8677                 "View as charset: " ;; actually it is coding system.
8678                 (save-excursion
8679                   (set-buffer gnus-article-buffer)
8680                   (mm-detect-coding-region (point) (point-max))))))
8681           (gnus-newsgroup-ignored-charsets 'gnus-all))
8682       (gnus-summary-select-article nil 'force)
8683       (let ((deps gnus-newsgroup-dependencies)
8684             head header lines)
8685         (save-excursion
8686           (set-buffer gnus-original-article-buffer)
8687           (save-restriction
8688             (message-narrow-to-head)
8689             (setq head (buffer-string))
8690             (goto-char (point-min))
8691             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8692               (goto-char (point-max))
8693               (widen)
8694               (setq lines (1- (count-lines (point) (point-max))))))
8695           (with-temp-buffer
8696             (insert (format "211 %d Article retrieved.\n"
8697                             (cdr gnus-article-current)))
8698             (insert head)
8699             (if lines (insert (format "Lines: %d\n" lines)))
8700             (insert ".\n")
8701             (let ((nntp-server-buffer (current-buffer)))
8702               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8703         (gnus-data-set-header
8704          (gnus-data-find (cdr gnus-article-current))
8705          header)
8706         (gnus-summary-update-article-line
8707          (cdr gnus-article-current) header)
8708         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8709           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8710    ((not arg)
8711     ;; Select the article the normal way.
8712     (gnus-summary-select-article nil 'force))
8713    (t
8714     ;; We have to require this here to make sure that the following
8715     ;; dynamic binding isn't shadowed by autoloading.
8716     (require 'gnus-async)
8717     (require 'gnus-art)
8718     ;; Bind the article treatment functions to nil.
8719     (let ((gnus-have-all-headers t)
8720           gnus-article-prepare-hook
8721           gnus-article-decode-hook
8722           gnus-display-mime-function
8723           gnus-break-pages)
8724       ;; Destroy any MIME parts.
8725       (when (gnus-buffer-live-p gnus-article-buffer)
8726         (save-excursion
8727           (set-buffer gnus-article-buffer)
8728           (mm-destroy-parts gnus-article-mime-handles)
8729           ;; Set it to nil for safety reason.
8730           (setq gnus-article-mime-handle-alist nil)
8731           (setq gnus-article-mime-handles nil)))
8732       (gnus-summary-select-article nil 'force))))
8733   (gnus-summary-goto-subject gnus-current-article)
8734   (gnus-summary-position-point))
8735
8736 (defun gnus-summary-show-raw-article ()
8737   "Show the raw article without any article massaging functions being run."
8738   (interactive)
8739   (gnus-summary-show-article t))
8740
8741 (defun gnus-summary-verbose-headers (&optional arg)
8742   "Toggle permanent full header display.
8743 If ARG is a positive number, turn header display on.
8744 If ARG is a negative number, turn header display off."
8745   (interactive "P")
8746   (setq gnus-show-all-headers
8747         (cond ((or (not (numberp arg))
8748                    (zerop arg))
8749                (not gnus-show-all-headers))
8750               ((natnump arg)
8751                t)))
8752   (gnus-summary-show-article))
8753
8754 (defun gnus-summary-toggle-header (&optional arg)
8755   "Show the headers if they are hidden, or hide them if they are shown.
8756 If ARG is a positive number, show the entire header.
8757 If ARG is a negative number, hide the unwanted header lines."
8758   (interactive "P")
8759   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8760                      (get-buffer-window gnus-article-buffer t))))
8761     (with-current-buffer gnus-article-buffer
8762       (widen)
8763       (article-narrow-to-head)
8764       (let* ((buffer-read-only nil)
8765              (inhibit-point-motion-hooks t)
8766              (hidden (if (numberp arg)
8767                          (>= arg 0)
8768                        (gnus-article-hidden-text-p 'headers)))
8769              s e)
8770         (delete-region (point-min) (point-max))
8771         (with-current-buffer gnus-original-article-buffer
8772           (goto-char (setq s (point-min)))
8773           (setq e (if (search-forward "\n\n" nil t)
8774                       (1- (point))
8775                     (point-max))))
8776         (insert-buffer-substring gnus-original-article-buffer s e)
8777         (run-hooks 'gnus-article-decode-hook)
8778         (if hidden
8779             (let ((gnus-treat-hide-headers nil)
8780                   (gnus-treat-hide-boring-headers nil))
8781               (gnus-delete-wash-type 'headers)
8782               (gnus-treat-article 'head))
8783           (gnus-treat-article 'head))
8784         (widen)
8785         (if window
8786             (set-window-start window (goto-char (point-min))))
8787         (if gnus-break-pages
8788             (gnus-narrow-to-page)
8789           (when (gnus-visual-p 'page-marker)
8790             (let ((buffer-read-only nil))
8791               (gnus-remove-text-with-property 'gnus-prev)
8792               (gnus-remove-text-with-property 'gnus-next))))
8793         (gnus-set-mode-line 'article)))))
8794
8795 (defun gnus-summary-show-all-headers ()
8796   "Make all header lines visible."
8797   (interactive)
8798   (gnus-summary-toggle-header 1))
8799
8800 (defun gnus-summary-caesar-message (&optional arg)
8801   "Caesar rotate the current article by 13.
8802 The numerical prefix specifies how many places to rotate each letter
8803 forward."
8804   (interactive "P")
8805   (gnus-summary-select-article)
8806   (let ((mail-header-separator ""))
8807     (gnus-eval-in-buffer-window gnus-article-buffer
8808       (save-restriction
8809         (widen)
8810         (let ((start (window-start))
8811               buffer-read-only)
8812           (message-caesar-buffer-body arg)
8813           (set-window-start (get-buffer-window (current-buffer)) start))))))
8814
8815 (autoload 'unmorse-region "morse"
8816   "Convert morse coded text in region to ordinary ASCII text."
8817   t)
8818
8819 (defun gnus-summary-morse-message (&optional arg)
8820   "Morse decode the current article."
8821   (interactive "P")
8822   (gnus-summary-select-article)
8823   (let ((mail-header-separator ""))
8824     (gnus-eval-in-buffer-window gnus-article-buffer
8825       (save-excursion
8826         (save-restriction
8827           (widen)
8828           (let ((pos (window-start))
8829                 buffer-read-only)
8830             (goto-char (point-min))
8831             (when (message-goto-body)
8832               (gnus-narrow-to-body))
8833             (goto-char (point-min))
8834             (while (re-search-forward "·" (point-max) t)
8835               (replace-match "."))
8836             (unmorse-region (point-min) (point-max))
8837             (widen)
8838             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8839
8840 (defun gnus-summary-stop-page-breaking ()
8841   "Stop page breaking in the current article."
8842   (interactive)
8843   (gnus-summary-select-article)
8844   (gnus-eval-in-buffer-window gnus-article-buffer
8845     (widen)
8846     (when (gnus-visual-p 'page-marker)
8847       (let ((buffer-read-only nil))
8848         (gnus-remove-text-with-property 'gnus-prev)
8849         (gnus-remove-text-with-property 'gnus-next))
8850       (setq gnus-page-broken nil))))
8851
8852 (defun gnus-summary-move-article (&optional n to-newsgroup
8853                                             select-method action)
8854   "Move the current article to a different newsgroup.
8855 If N is a positive number, move the N next articles.
8856 If N is a negative number, move the N previous articles.
8857 If N is nil and any articles have been marked with the process mark,
8858 move those articles instead.
8859 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8860 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8861 re-spool using this method.
8862
8863 When called interactively with TO-NEWSGROUP being nil, the value of
8864 the variable `gnus-move-split-methods' is used for finding a default
8865 for the target newsgroup.
8866
8867 For this function to work, both the current newsgroup and the
8868 newsgroup that you want to move to have to support the `request-move'
8869 and `request-accept' functions.
8870
8871 ACTION can be either `move' (the default), `crosspost' or `copy'."
8872   (interactive "P")
8873   (unless action
8874     (setq action 'move))
8875   ;; Check whether the source group supports the required functions.
8876   (cond ((and (eq action 'move)
8877               (not (gnus-check-backend-function
8878                     'request-move-article gnus-newsgroup-name)))
8879          (error "The current group does not support article moving"))
8880         ((and (eq action 'crosspost)
8881               (not (gnus-check-backend-function
8882                     'request-replace-article gnus-newsgroup-name)))
8883          (error "The current group does not support article editing")))
8884   (let ((articles (gnus-summary-work-articles n))
8885         (prefix (if (gnus-check-backend-function
8886                      'request-move-article gnus-newsgroup-name)
8887                     (gnus-group-real-prefix gnus-newsgroup-name)
8888                   ""))
8889         (names '((move "Move" "Moving")
8890                  (copy "Copy" "Copying")
8891                  (crosspost "Crosspost" "Crossposting")))
8892         (copy-buf (save-excursion
8893                     (nnheader-set-temp-buffer " *copy article*")))
8894         art-group to-method new-xref article to-groups)
8895     (unless (assq action names)
8896       (error "Unknown action %s" action))
8897     ;; Read the newsgroup name.
8898     (when (and (not to-newsgroup)
8899                (not select-method))
8900       (if (and gnus-move-split-methods
8901                (not
8902                 (and (memq gnus-current-article articles)
8903                      (gnus-buffer-live-p gnus-original-article-buffer))))
8904           ;; When `gnus-move-split-methods' is non-nil, we have to
8905           ;; select an article to give `gnus-read-move-group-name' an
8906           ;; opportunity to suggest an appropriate default.  However,
8907           ;; we needn't render or mark the article.
8908           (let ((gnus-display-mime-function nil)
8909                 (gnus-article-prepare-hook nil)
8910                 (gnus-mark-article-hook nil))
8911             (gnus-summary-select-article nil nil nil (car articles))))
8912       (setq to-newsgroup
8913             (gnus-read-move-group-name
8914              (cadr (assq action names))
8915              (symbol-value (intern (format "gnus-current-%s-group" action)))
8916              articles prefix))
8917       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8918     (setq to-method (or select-method
8919                         (gnus-server-to-method
8920                          (gnus-group-method to-newsgroup))))
8921     ;; Check the method we are to move this article to...
8922     (unless (gnus-check-backend-function
8923              'request-accept-article (car to-method))
8924       (error "%s does not support article copying" (car to-method)))
8925     (unless (gnus-check-server to-method)
8926       (error "Can't open server %s" (car to-method)))
8927     (gnus-message 6 "%s to %s: %s..."
8928                   (caddr (assq action names))
8929                   (or (car select-method) to-newsgroup) articles)
8930     (while articles
8931       (setq article (pop articles))
8932       (setq
8933        art-group
8934        (cond
8935         ;; Move the article.
8936         ((eq action 'move)
8937          ;; Remove this article from future suppression.
8938          (gnus-dup-unsuppress-article article)
8939          (gnus-request-move-article
8940           article                       ; Article to move
8941           gnus-newsgroup-name           ; From newsgroup
8942           (nth 1 (gnus-find-method-for-group
8943                   gnus-newsgroup-name)) ; Server
8944           (list 'gnus-request-accept-article
8945                 to-newsgroup (list 'quote select-method)
8946                 (not articles) t)       ; Accept form
8947           (not articles)))              ; Only save nov last time
8948         ;; Copy the article.
8949         ((eq action 'copy)
8950          (save-excursion
8951            (set-buffer copy-buf)
8952            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8953              (gnus-request-accept-article
8954               to-newsgroup select-method (not articles) t))))
8955         ;; Crosspost the article.
8956         ((eq action 'crosspost)
8957          (let ((xref (message-tokenize-header
8958                       (mail-header-xref (gnus-summary-article-header article))
8959                       " ")))
8960            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8961                                   ":" (number-to-string article)))
8962            (unless xref
8963              (setq xref (list (system-name))))
8964            (setq new-xref
8965                  (concat
8966                   (mapconcat 'identity
8967                              (delete "Xref:" (delete new-xref xref))
8968                              " ")
8969                   " " new-xref))
8970            (save-excursion
8971              (set-buffer copy-buf)
8972              ;; First put the article in the destination group.
8973              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8974              (when (consp (setq art-group
8975                                 (gnus-request-accept-article
8976                                  to-newsgroup select-method (not articles))))
8977                (setq new-xref (concat new-xref " " (car art-group)
8978                                       ":"
8979                                       (number-to-string (cdr art-group))))
8980                ;; Now we have the new Xrefs header, so we insert
8981                ;; it and replace the new article.
8982                (nnheader-replace-header "Xref" new-xref)
8983                (gnus-request-replace-article
8984                 (cdr art-group) to-newsgroup (current-buffer))
8985                art-group))))))
8986       (cond
8987        ((not art-group)
8988         (gnus-message 1 "Couldn't %s article %s: %s"
8989                       (cadr (assq action names)) article
8990                       (nnheader-get-report (car to-method))))
8991        ((eq art-group 'junk)
8992         (when (eq action 'move)
8993           (gnus-summary-mark-article article gnus-canceled-mark)
8994           (gnus-message 4 "Deleted article %s" article)
8995           ;; run the delete hook
8996           (run-hook-with-args 'gnus-summary-article-delete-hook
8997                               action
8998                               (gnus-data-header
8999                                (assoc article (gnus-data-list nil)))
9000                               gnus-newsgroup-name nil
9001                               select-method)))
9002        (t
9003         (let* ((pto-group (gnus-group-prefixed-name
9004                            (car art-group) to-method))
9005                (info (gnus-get-info pto-group))
9006                (to-group (gnus-info-group info))
9007                to-marks)
9008           ;; Update the group that has been moved to.
9009           (when (and info
9010                      (memq action '(move copy)))
9011             (unless (member to-group to-groups)
9012               (push to-group to-groups))
9013
9014             (unless (memq article gnus-newsgroup-unreads)
9015               (push 'read to-marks)
9016               (gnus-info-set-read
9017                info (gnus-add-to-range (gnus-info-read info)
9018                                        (list (cdr art-group)))))
9019
9020             ;; See whether the article is to be put in the cache.
9021             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9022                              gnus-article-mark-lists
9023                            (delete '(expirable . expire)
9024                                    (copy-sequence gnus-article-mark-lists))))
9025                   (to-article (cdr art-group)))
9026
9027               ;; Enter the article into the cache in the new group,
9028               ;; if that is required.
9029               (when gnus-use-cache
9030                 (gnus-cache-possibly-enter-article
9031                  to-group to-article
9032                  (memq article gnus-newsgroup-marked)
9033                  (memq article gnus-newsgroup-dormant)
9034                  (memq article gnus-newsgroup-unreads)))
9035
9036               (when gnus-preserve-marks
9037                 ;; Copy any marks over to the new group.
9038                 (when (and (equal to-group gnus-newsgroup-name)
9039                            (not (memq article gnus-newsgroup-unreads)))
9040                   ;; Mark this article as read in this group.
9041                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9042                   (setcdr (gnus-active to-group) to-article)
9043                   (setcdr gnus-newsgroup-active to-article))
9044
9045                 (while marks
9046                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9047                     (when (memq article (symbol-value
9048                                          (intern (format "gnus-newsgroup-%s"
9049                                                          (caar marks)))))
9050                       (push (cdar marks) to-marks)
9051                       ;; If the other group is the same as this group,
9052                       ;; then we have to add the mark to the list.
9053                       (when (equal to-group gnus-newsgroup-name)
9054                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9055                              (cons to-article
9056                                    (symbol-value
9057                                     (intern (format "gnus-newsgroup-%s"
9058                                                     (caar marks)))))))
9059                       ;; Copy the marks to other group.
9060                       (gnus-add-marked-articles
9061                        to-group (cdar marks) (list to-article) info)))
9062                   (setq marks (cdr marks)))
9063
9064                 (gnus-request-set-mark
9065                  to-group (list (list (list to-article) 'add to-marks))))
9066
9067               (gnus-dribble-enter
9068                (concat "(gnus-group-set-info '"
9069                        (gnus-prin1-to-string (gnus-get-info to-group))
9070                        ")"))))
9071
9072           ;; Update the Xref header in this article to point to
9073           ;; the new crossposted article we have just created.
9074           (when (eq action 'crosspost)
9075             (save-excursion
9076               (set-buffer copy-buf)
9077               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9078               (nnheader-replace-header "Xref" new-xref)
9079               (gnus-request-replace-article
9080                article gnus-newsgroup-name (current-buffer))))
9081
9082           ;; run the move/copy/crosspost/respool hook
9083           (run-hook-with-args 'gnus-summary-article-move-hook
9084                               action
9085                               (gnus-data-header
9086                                (assoc article (gnus-data-list nil)))
9087                               gnus-newsgroup-name
9088                               to-newsgroup
9089                               select-method))
9090
9091         ;;;!!!Why is this necessary?
9092         (set-buffer gnus-summary-buffer)
9093         
9094         (gnus-summary-goto-subject article)
9095         (when (eq action 'move)
9096           (gnus-summary-mark-article article gnus-canceled-mark))))
9097       (gnus-summary-remove-process-mark article))
9098     ;; Re-activate all groups that have been moved to.
9099     (save-excursion
9100       (set-buffer gnus-group-buffer)
9101       (let ((gnus-group-marked to-groups))
9102         (gnus-group-get-new-news-this-group nil t)))
9103
9104     (gnus-kill-buffer copy-buf)
9105     (gnus-summary-position-point)
9106     (gnus-set-mode-line 'summary)))
9107
9108 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9109   "Move the current article to a different newsgroup.
9110 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9111 When called interactively, if TO-NEWSGROUP is nil, use the value of
9112 the variable `gnus-move-split-methods' for finding a default target
9113 newsgroup.
9114 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9115 re-spool using this method."
9116   (interactive "P")
9117   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9118
9119 (defun gnus-summary-crosspost-article (&optional n)
9120   "Crosspost the current article to some other group."
9121   (interactive "P")
9122   (gnus-summary-move-article n nil nil 'crosspost))
9123
9124 (defcustom gnus-summary-respool-default-method nil
9125   "Default method type for respooling an article.
9126 If nil, use to the current newsgroup method."
9127   :type 'symbol
9128   :group 'gnus-summary-mail)
9129
9130 (defcustom gnus-summary-display-while-building nil
9131   "If non-nil, show and update the summary buffer as it's being built.
9132 If the value is t, update the buffer after every line is inserted.  If
9133 the value is an integer (N), update the display every N lines."
9134   :group 'gnus-thread
9135   :type '(choice (const :tag "off" nil)
9136                  number
9137                  (const :tag "frequently" t)))
9138
9139 (defun gnus-summary-respool-article (&optional n method)
9140   "Respool the current article.
9141 The article will be squeezed through the mail spooling process again,
9142 which means that it will be put in some mail newsgroup or other
9143 depending on `nnmail-split-methods'.
9144 If N is a positive number, respool the N next articles.
9145 If N is a negative number, respool the N previous articles.
9146 If N is nil and any articles have been marked with the process mark,
9147 respool those articles instead.
9148
9149 Respooling can be done both from mail groups and \"real\" newsgroups.
9150 In the former case, the articles in question will be moved from the
9151 current group into whatever groups they are destined to.  In the
9152 latter case, they will be copied into the relevant groups."
9153   (interactive
9154    (list current-prefix-arg
9155          (let* ((methods (gnus-methods-using 'respool))
9156                 (methname
9157                  (symbol-name (or gnus-summary-respool-default-method
9158                                   (car (gnus-find-method-for-group
9159                                         gnus-newsgroup-name)))))
9160                 (method
9161                  (gnus-completing-read-with-default
9162                   methname "What backend do you want to use when respooling?"
9163                   methods nil t nil 'gnus-mail-method-history))
9164                 ms)
9165            (cond
9166             ((zerop (length (setq ms (gnus-servers-using-backend
9167                                       (intern method)))))
9168              (list (intern method) ""))
9169             ((= 1 (length ms))
9170              (car ms))
9171             (t
9172              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9173                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9174                            ms-alist))))))))
9175   (unless method
9176     (error "No method given for respooling"))
9177   (if (assoc (symbol-name
9178               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9179              (gnus-methods-using 'respool))
9180       (gnus-summary-move-article n nil method)
9181     (gnus-summary-copy-article n nil method)))
9182
9183 (defun gnus-summary-import-article (file &optional edit)
9184   "Import an arbitrary file into a mail newsgroup."
9185   (interactive "fImport file: \nP")
9186   (let ((group gnus-newsgroup-name)
9187         (now (current-time))
9188         atts lines group-art)
9189     (unless (gnus-check-backend-function 'request-accept-article group)
9190       (error "%s does not support article importing" group))
9191     (or (file-readable-p file)
9192         (not (file-regular-p file))
9193         (error "Can't read %s" file))
9194     (save-excursion
9195       (set-buffer (gnus-get-buffer-create " *import file*"))
9196       (erase-buffer)
9197       (nnheader-insert-file-contents file)
9198       (goto-char (point-min))
9199       (if (nnheader-article-p)
9200           (save-restriction
9201             (goto-char (point-min))
9202             (search-forward "\n\n" nil t)
9203             (narrow-to-region (point-min) (1- (point)))
9204             (goto-char (point-min))
9205             (unless (re-search-forward "^date:" nil t)
9206               (goto-char (point-max))
9207               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9208        ;; This doesn't look like an article, so we fudge some headers.
9209         (setq atts (file-attributes file)
9210               lines (count-lines (point-min) (point-max)))
9211         (insert "From: " (read-string "From: ") "\n"
9212                 "Subject: " (read-string "Subject: ") "\n"
9213                 "Date: " (message-make-date (nth 5 atts)) "\n"
9214                 "Message-ID: " (message-make-message-id) "\n"
9215                 "Lines: " (int-to-string lines) "\n"
9216                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9217       (setq group-art (gnus-request-accept-article group nil t))
9218       (kill-buffer (current-buffer)))
9219     (setq gnus-newsgroup-active (gnus-activate-group group))
9220     (forward-line 1)
9221     (gnus-summary-goto-article (cdr group-art) nil t)
9222     (when edit
9223       (gnus-summary-edit-article))))
9224
9225 (defun gnus-summary-create-article ()
9226   "Create an article in a mail newsgroup."
9227   (interactive)
9228   (let ((group gnus-newsgroup-name)
9229         (now (current-time))
9230         group-art)
9231     (unless (gnus-check-backend-function 'request-accept-article group)
9232       (error "%s does not support article importing" group))
9233     (save-excursion
9234       (set-buffer (gnus-get-buffer-create " *import file*"))
9235       (erase-buffer)
9236       (goto-char (point-min))
9237       ;; This doesn't look like an article, so we fudge some headers.
9238       (insert "From: " (read-string "From: ") "\n"
9239               "Subject: " (read-string "Subject: ") "\n"
9240               "Date: " (message-make-date now) "\n"
9241               "Message-ID: " (message-make-message-id) "\n")
9242       (setq group-art (gnus-request-accept-article group nil t))
9243       (kill-buffer (current-buffer)))
9244     (setq gnus-newsgroup-active (gnus-activate-group group))
9245     (forward-line 1)
9246     (gnus-summary-goto-article (cdr group-art) nil t)
9247     (gnus-summary-edit-article)))
9248
9249 (defun gnus-summary-article-posted-p ()
9250   "Say whether the current (mail) article is available from news as well.
9251 This will be the case if the article has both been mailed and posted."
9252   (interactive)
9253   (let ((id (mail-header-references (gnus-summary-article-header)))
9254         (gnus-override-method (car (gnus-refer-article-methods))))
9255     (if (gnus-request-head id "")
9256         (gnus-message 2 "The current message was found on %s"
9257                       gnus-override-method)
9258       (gnus-message 2 "The current message couldn't be found on %s"
9259                     gnus-override-method)
9260       nil)))
9261
9262 (defun gnus-summary-expire-articles (&optional now)
9263   "Expire all articles that are marked as expirable in the current group."
9264   (interactive)
9265   (when (and (not gnus-group-is-exiting-without-update-p)
9266              (gnus-check-backend-function
9267               'request-expire-articles gnus-newsgroup-name))
9268     ;; This backend supports expiry.
9269     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9270            (expirable (if total
9271                           (progn
9272                             ;; We need to update the info for
9273                             ;; this group for `gnus-list-of-read-articles'
9274                             ;; to give us the right answer.
9275                             (gnus-run-hooks 'gnus-exit-group-hook)
9276                             (gnus-summary-update-info)
9277                             (gnus-list-of-read-articles gnus-newsgroup-name))
9278                         (setq gnus-newsgroup-expirable
9279                               (sort gnus-newsgroup-expirable '<))))
9280            (expiry-wait (if now 'immediate
9281                           (gnus-group-find-parameter
9282                            gnus-newsgroup-name 'expiry-wait)))
9283            (nnmail-expiry-target
9284             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9285                 nnmail-expiry-target))
9286            es)
9287       (when expirable
9288         ;; There are expirable articles in this group, so we run them
9289         ;; through the expiry process.
9290         (gnus-message 6 "Expiring articles...")
9291         (unless (gnus-check-group gnus-newsgroup-name)
9292           (error "Can't open server for %s" gnus-newsgroup-name))
9293         ;; The list of articles that weren't expired is returned.
9294         (save-excursion
9295           (if expiry-wait
9296               (let ((nnmail-expiry-wait-function nil)
9297                     (nnmail-expiry-wait expiry-wait))
9298                 (setq es (gnus-request-expire-articles
9299                           expirable gnus-newsgroup-name)))
9300             (setq es (gnus-request-expire-articles
9301                       expirable gnus-newsgroup-name)))
9302           (unless total
9303             (setq gnus-newsgroup-expirable es))
9304           ;; We go through the old list of expirable, and mark all
9305           ;; really expired articles as nonexistent.
9306           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9307             (let ((gnus-use-cache nil))
9308               (dolist (article expirable)
9309                 (when (and (not (memq article es))
9310                            (gnus-data-find article))
9311                   (gnus-summary-mark-article article gnus-canceled-mark)
9312                   (run-hook-with-args 'gnus-summary-article-expire-hook
9313                                       'delete
9314                                       (gnus-data-header
9315                                        (assoc article (gnus-data-list nil)))
9316                                       gnus-newsgroup-name
9317                                       nil
9318                                       nil))))))
9319         (gnus-message 6 "Expiring articles...done")))))
9320
9321 (defun gnus-summary-expire-articles-now ()
9322   "Expunge all expirable articles in the current group.
9323 This means that *all* articles that are marked as expirable will be
9324 deleted forever, right now."
9325   (interactive)
9326   (or gnus-expert-user
9327       (gnus-yes-or-no-p
9328        "Are you really, really, really sure you want to delete all these messages? ")
9329       (error "Phew!"))
9330   (gnus-summary-expire-articles t))
9331
9332 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9333 (defun gnus-summary-delete-article (&optional n)
9334   "Delete the N next (mail) articles.
9335 This command actually deletes articles.  This is not a marking
9336 command.  The article will disappear forever from your life, never to
9337 return.
9338
9339 If N is negative, delete backwards.
9340 If N is nil and articles have been marked with the process mark,
9341 delete these instead.
9342
9343 If `gnus-novice-user' is non-nil you will be asked for
9344 confirmation before the articles are deleted."
9345   (interactive "P")
9346   (unless (gnus-check-backend-function 'request-expire-articles
9347                                        gnus-newsgroup-name)
9348     (error "The current newsgroup does not support article deletion"))
9349   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9350     (error "Couldn't open server"))
9351   ;; Compute the list of articles to delete.
9352   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9353         (nnmail-expiry-target 'delete)
9354         not-deleted)
9355     (if (and gnus-novice-user
9356              (not (gnus-yes-or-no-p
9357                    (format "Do you really want to delete %s forever? "
9358                            (if (> (length articles) 1)
9359                                (format "these %s articles" (length articles))
9360                              "this article")))))
9361         ()
9362       ;; Delete the articles.
9363       (setq not-deleted (gnus-request-expire-articles
9364                          articles gnus-newsgroup-name 'force))
9365       (while articles
9366         (gnus-summary-remove-process-mark (car articles))
9367         ;; The backend might not have been able to delete the article
9368         ;; after all.
9369         (unless (memq (car articles) not-deleted)
9370           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9371         (let* ((article (car articles))
9372                (id (mail-header-id (gnus-data-header
9373                                     (assoc article (gnus-data-list nil))))))
9374           (run-hook-with-args 'gnus-summary-article-delete-hook
9375                               'delete id gnus-newsgroup-name nil
9376                               nil))
9377         (setq articles (cdr articles)))
9378       (when not-deleted
9379         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9380     (gnus-summary-position-point)
9381     (gnus-set-mode-line 'summary)
9382     not-deleted))
9383
9384 (defun gnus-summary-edit-article (&optional arg)
9385   "Edit the current article.
9386 This will have permanent effect only in mail groups.
9387 If ARG is nil, edit the decoded articles.
9388 If ARG is 1, edit the raw articles.
9389 If ARG is 2, edit the raw articles even in read-only groups.
9390 If ARG is 3, edit the articles with the current handles.
9391 Otherwise, allow editing of articles even in read-only
9392 groups."
9393   (interactive "P")
9394   (let (force raw current-handles)
9395     (cond
9396      ((null arg))
9397      ((eq arg 1)
9398       (setq raw t))
9399      ((eq arg 2)
9400       (setq raw t
9401             force t))
9402      ((eq arg 3)
9403       (setq current-handles
9404             (and (gnus-buffer-live-p gnus-article-buffer)
9405                  (with-current-buffer gnus-article-buffer
9406                    (prog1
9407                        gnus-article-mime-handles
9408                      (setq gnus-article-mime-handles nil))))))
9409      (t
9410       (setq force t)))
9411     (when (and raw (not force)
9412                (member gnus-newsgroup-name '("nndraft:delayed"
9413                                              "nndraft:drafts"
9414                                              "nndraft:queue")))
9415       (error "Can't edit the raw article in group %s"
9416              gnus-newsgroup-name))
9417     (save-excursion
9418       (set-buffer gnus-summary-buffer)
9419       (let ((mail-parse-charset gnus-newsgroup-charset)
9420             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9421         (gnus-set-global-variables)
9422         (when (and (not force)
9423                    (gnus-group-read-only-p))
9424           (error "The current newsgroup does not support article editing"))
9425         (gnus-summary-show-article t)
9426         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9427           (with-current-buffer gnus-article-buffer
9428             (mm-enable-multibyte)))
9429         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9430             (setq raw t))
9431         (gnus-article-edit-article
9432          (if raw 'ignore
9433            `(lambda ()
9434               (let ((mbl mml-buffer-list))
9435                 (setq mml-buffer-list nil)
9436                 (mime-to-mml ,'current-handles)
9437                 (let ((mbl1 mml-buffer-list))
9438                   (setq mml-buffer-list mbl)
9439                   (set (make-local-variable 'mml-buffer-list) mbl1))
9440                 (gnus-make-local-hook 'kill-buffer-hook)
9441                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9442          `(lambda (no-highlight)
9443             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9444                   (message-options message-options)
9445                   (message-options-set-recipient)
9446                   (mail-parse-ignored-charsets
9447                    ',gnus-newsgroup-ignored-charsets))
9448               ,(if (not raw) '(progn
9449                                 (mml-to-mime)
9450                                 (mml-destroy-buffers)
9451                                 (remove-hook 'kill-buffer-hook
9452                                              'mml-destroy-buffers t)
9453                                 (kill-local-variable 'mml-buffer-list)))
9454               (gnus-summary-edit-article-done
9455                ,(or (mail-header-references gnus-current-headers) "")
9456                ,(gnus-group-read-only-p)
9457                ,gnus-summary-buffer no-highlight))))))))
9458
9459 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9460
9461 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9462                                                  no-highlight)
9463   "Make edits to the current article permanent."
9464   (interactive)
9465   (save-excursion
9466    ;; The buffer restriction contains the entire article if it exists.
9467     (when (article-goto-body)
9468       (let ((lines (count-lines (point) (point-max)))
9469             (length (- (point-max) (point)))
9470             (case-fold-search t)
9471             (body (copy-marker (point))))
9472         (goto-char (point-min))
9473         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9474           (delete-region (match-beginning 1) (match-end 1))
9475           (insert (number-to-string length)))
9476         (goto-char (point-min))
9477         (when (re-search-forward
9478                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9479           (delete-region (match-beginning 1) (match-end 1))
9480           (insert (number-to-string length)))
9481         (goto-char (point-min))
9482         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9483           (delete-region (match-beginning 1) (match-end 1))
9484           (insert (number-to-string lines))))))
9485   ;; Replace the article.
9486   (let ((buf (current-buffer)))
9487     (with-temp-buffer
9488       (insert-buffer-substring buf)
9489
9490       (if (and (not read-only)
9491                (not (gnus-request-replace-article
9492                      (cdr gnus-article-current) (car gnus-article-current)
9493                      (current-buffer) t)))
9494           (error "Couldn't replace article")
9495         ;; Update the summary buffer.
9496         (if (and references
9497                  (equal (message-tokenize-header references " ")
9498                         (message-tokenize-header
9499                          (or (message-fetch-field "references") "") " ")))
9500             ;; We only have to update this line.
9501             (save-excursion
9502               (save-restriction
9503                 (message-narrow-to-head)
9504                 (let ((head (buffer-string))
9505                       header)
9506                   (with-temp-buffer
9507                     (insert (format "211 %d Article retrieved.\n"
9508                                     (cdr gnus-article-current)))
9509                     (insert head)
9510                     (insert ".\n")
9511                     (let ((nntp-server-buffer (current-buffer)))
9512                       (setq header (car (gnus-get-newsgroup-headers
9513                                          nil t))))
9514                     (save-excursion
9515                       (set-buffer gnus-summary-buffer)
9516                       (gnus-data-set-header
9517                        (gnus-data-find (cdr gnus-article-current))
9518                        header)
9519                       (gnus-summary-update-article-line
9520                        (cdr gnus-article-current) header)
9521                       (if (gnus-summary-goto-subject
9522                            (cdr gnus-article-current) nil t)
9523                           (gnus-summary-update-secondary-mark
9524                            (cdr gnus-article-current))))))))
9525           ;; Update threads.
9526           (set-buffer (or buffer gnus-summary-buffer))
9527           (gnus-summary-update-article (cdr gnus-article-current))
9528           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9529               (gnus-summary-update-secondary-mark
9530                (cdr gnus-article-current))))
9531         ;; Prettify the article buffer again.
9532         (unless no-highlight
9533           (save-excursion
9534             (set-buffer gnus-article-buffer)
9535             ;;;!!! Fix this -- article should be rehighlighted.
9536             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9537             (set-buffer gnus-original-article-buffer)
9538             (gnus-request-article
9539              (cdr gnus-article-current)
9540              (car gnus-article-current) (current-buffer))))
9541         ;; Prettify the summary buffer line.
9542         (when (gnus-visual-p 'summary-highlight 'highlight)
9543           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9544
9545 (defun gnus-summary-edit-wash (key)
9546   "Perform editing command KEY in the article buffer."
9547   (interactive
9548    (list
9549     (progn
9550       (message "%s" (concat (this-command-keys) "- "))
9551       (read-char))))
9552   (message "")
9553   (gnus-summary-edit-article)
9554   (execute-kbd-macro (concat (this-command-keys) key))
9555   (gnus-article-edit-done))
9556
9557 ;;; Respooling
9558
9559 (defun gnus-summary-respool-query (&optional silent trace)
9560   "Query where the respool algorithm would put this article."
9561   (interactive)
9562   (let (gnus-mark-article-hook)
9563     (gnus-summary-select-article)
9564     (save-excursion
9565       (set-buffer gnus-original-article-buffer)
9566       (let ((groups (nnmail-article-group 'identity trace)))
9567         (unless silent
9568           (if groups
9569               (message "This message would go to %s"
9570                        (mapconcat 'car groups ", "))
9571             (message "This message would go to no groups"))
9572           groups)))))
9573
9574 (defun gnus-summary-respool-trace ()
9575   "Trace where the respool algorithm would put this article.
9576 Display a buffer showing all fancy splitting patterns which matched."
9577   (interactive)
9578   (gnus-summary-respool-query nil t))
9579
9580 ;; Summary marking commands.
9581
9582 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9583   "Mark articles which has the same subject as read, and then select the next.
9584 If UNMARK is positive, remove any kind of mark.
9585 If UNMARK is negative, tick articles."
9586   (interactive "P")
9587   (when unmark
9588     (setq unmark (prefix-numeric-value unmark)))
9589   (let ((count
9590          (gnus-summary-mark-same-subject
9591           (gnus-summary-article-subject) unmark)))
9592     ;; Select next unread article.  If auto-select-same mode, should
9593     ;; select the first unread article.
9594     (gnus-summary-next-article t (and gnus-auto-select-same
9595                                       (gnus-summary-article-subject)))
9596     (gnus-message 7 "%d article%s marked as %s"
9597                   count (if (= count 1) " is" "s are")
9598                   (if unmark "unread" "read"))))
9599
9600 (defun gnus-summary-kill-same-subject (&optional unmark)
9601   "Mark articles which has the same subject as read.
9602 If UNMARK is positive, remove any kind of mark.
9603 If UNMARK is negative, tick articles."
9604   (interactive "P")
9605   (when unmark
9606     (setq unmark (prefix-numeric-value unmark)))
9607   (let ((count
9608          (gnus-summary-mark-same-subject
9609           (gnus-summary-article-subject) unmark)))
9610     ;; If marked as read, go to next unread subject.
9611     (when (null unmark)
9612       ;; Go to next unread subject.
9613       (gnus-summary-next-subject 1 t))
9614     (gnus-message 7 "%d articles are marked as %s"
9615                   count (if unmark "unread" "read"))))
9616
9617 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9618   "Mark articles with same SUBJECT as read, and return marked number.
9619 If optional argument UNMARK is positive, remove any kinds of marks.
9620 If optional argument UNMARK is negative, mark articles as unread instead."
9621   (let ((count 1))
9622     (save-excursion
9623       (cond
9624        ((null unmark)                   ; Mark as read.
9625         (while (and
9626                 (progn
9627                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9628                   (gnus-summary-show-thread) t)
9629                 (gnus-summary-find-subject subject))
9630           (setq count (1+ count))))
9631        ((> unmark 0)                    ; Tick.
9632         (while (and
9633                 (progn
9634                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9635                   (gnus-summary-show-thread) t)
9636                 (gnus-summary-find-subject subject))
9637           (setq count (1+ count))))
9638        (t                               ; Mark as unread.
9639         (while (and
9640                 (progn
9641                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9642                   (gnus-summary-show-thread) t)
9643                 (gnus-summary-find-subject subject))
9644           (setq count (1+ count)))))
9645       (gnus-set-mode-line 'summary)
9646       ;; Return the number of marked articles.
9647       count)))
9648
9649 (defun gnus-summary-mark-as-processable (n &optional unmark)
9650   "Set the process mark on the next N articles.
9651 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9652 the process mark instead.  The difference between N and the actual
9653 number of articles marked is returned."
9654   (interactive "P")
9655   (if (and (null n) (gnus-region-active-p))
9656       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9657     (setq n (prefix-numeric-value n))
9658     (let ((backward (< n 0))
9659           (n (abs n)))
9660       (while (and
9661               (> n 0)
9662               (if unmark
9663                   (gnus-summary-remove-process-mark
9664                    (gnus-summary-article-number))
9665                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9666               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9667         (setq n (1- n)))
9668       (when (/= 0 n)
9669         (gnus-message 7 "No more articles"))
9670       (gnus-summary-recenter)
9671       (gnus-summary-position-point)
9672       n)))
9673
9674 (defun gnus-summary-unmark-as-processable (n)
9675   "Remove the process mark from the next N articles.
9676 If N is negative, unmark backward instead.  The difference between N and
9677 the actual number of articles unmarked is returned."
9678   (interactive "P")
9679   (gnus-summary-mark-as-processable n t))
9680
9681 (defun gnus-summary-unmark-all-processable ()
9682   "Remove the process mark from all articles."
9683   (interactive)
9684   (save-excursion
9685     (while gnus-newsgroup-processable
9686       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9687   (gnus-summary-position-point))
9688
9689 (defun gnus-summary-add-mark (article type)
9690   "Mark ARTICLE with a mark of TYPE."
9691   (let ((vtype (car (assq type gnus-article-mark-lists)))
9692         var)
9693     (if (not vtype)
9694         (error "No such mark type: %s" type)
9695       (setq var (intern (format "gnus-newsgroup-%s" type)))
9696       (set var (cons article (symbol-value var)))
9697       (if (memq type '(processable cached replied forwarded recent saved))
9698           (gnus-summary-update-secondary-mark article)
9699         ;;; !!! This is bogus.  We should find out what primary
9700         ;;; !!! mark we want to set.
9701         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9702
9703 (defun gnus-summary-mark-as-expirable (n)
9704   "Mark N articles forward as expirable.
9705 If N is negative, mark backward instead.  The difference between N and
9706 the actual number of articles marked is returned."
9707   (interactive "p")
9708   (gnus-summary-mark-forward n gnus-expirable-mark))
9709
9710 (defun gnus-summary-mark-as-spam (n)
9711   "Mark N articles forward as spam.
9712 If N is negative, mark backward instead.  The difference between N and
9713 the actual number of articles marked is returned."
9714   (interactive "p")
9715   (gnus-summary-mark-forward n gnus-spam-mark))
9716
9717 (defun gnus-summary-mark-article-as-replied (article)
9718   "Mark ARTICLE as replied to and update the summary line.
9719 ARTICLE can also be a list of articles."
9720   (interactive (list (gnus-summary-article-number)))
9721   (let ((articles (if (listp article) article (list article))))
9722     (dolist (article articles)
9723       (unless (numberp article)
9724         (error "%s is not a number" article))
9725       (push article gnus-newsgroup-replied)
9726       (let ((buffer-read-only nil))
9727         (when (gnus-summary-goto-subject article nil t)
9728           (gnus-summary-update-secondary-mark article))))))
9729
9730 (defun gnus-summary-mark-article-as-forwarded (article)
9731   "Mark ARTICLE as forwarded and update the summary line.
9732 ARTICLE can also be a list of articles."
9733   (let ((articles (if (listp article) article (list article))))
9734     (dolist (article articles)
9735       (push article gnus-newsgroup-forwarded)
9736       (let ((buffer-read-only nil))
9737         (when (gnus-summary-goto-subject article nil t)
9738           (gnus-summary-update-secondary-mark article))))))
9739
9740 (defun gnus-summary-set-bookmark (article)
9741   "Set a bookmark in current article."
9742   (interactive (list (gnus-summary-article-number)))
9743   (when (or (not (get-buffer gnus-article-buffer))
9744             (not gnus-current-article)
9745             (not gnus-article-current)
9746             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9747     (error "No current article selected"))
9748   ;; Remove old bookmark, if one exists.
9749   (gnus-pull article gnus-newsgroup-bookmarks)
9750   ;; Set the new bookmark, which is on the form
9751   ;; (article-number . line-number-in-body).
9752   (push
9753    (cons article
9754          (save-excursion
9755            (set-buffer gnus-article-buffer)
9756            (count-lines
9757             (min (point)
9758                  (save-excursion
9759                    (article-goto-body)
9760                    (point)))
9761             (point))))
9762    gnus-newsgroup-bookmarks)
9763   (gnus-message 6 "A bookmark has been added to the current article."))
9764
9765 (defun gnus-summary-remove-bookmark (article)
9766   "Remove the bookmark from the current article."
9767   (interactive (list (gnus-summary-article-number)))
9768   ;; Remove old bookmark, if one exists.
9769   (if (not (assq article gnus-newsgroup-bookmarks))
9770       (gnus-message 6 "No bookmark in current article.")
9771     (gnus-pull article gnus-newsgroup-bookmarks)
9772     (gnus-message 6 "Removed bookmark.")))
9773
9774 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9775 (defun gnus-summary-mark-as-dormant (n)
9776   "Mark N articles forward as dormant.
9777 If N is negative, mark backward instead.  The difference between N and
9778 the actual number of articles marked is returned."
9779   (interactive "p")
9780   (gnus-summary-mark-forward n gnus-dormant-mark))
9781
9782 (defun gnus-summary-set-process-mark (article)
9783   "Set the process mark on ARTICLE and update the summary line."
9784   (setq gnus-newsgroup-processable
9785         (cons article
9786               (delq article gnus-newsgroup-processable)))
9787   (when (gnus-summary-goto-subject article)
9788     (gnus-summary-show-thread)
9789     (gnus-summary-goto-subject article)
9790     (gnus-summary-update-secondary-mark article)))
9791
9792 (defun gnus-summary-remove-process-mark (article)
9793   "Remove the process mark from ARTICLE and update the summary line."
9794   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9795   (when (gnus-summary-goto-subject article)
9796     (gnus-summary-show-thread)
9797     (gnus-summary-goto-subject article)
9798     (gnus-summary-update-secondary-mark article)))
9799
9800 (defun gnus-summary-set-saved-mark (article)
9801   "Set the process mark on ARTICLE and update the summary line."
9802   (push article gnus-newsgroup-saved)
9803   (when (gnus-summary-goto-subject article)
9804     (gnus-summary-update-secondary-mark article)))
9805
9806 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9807   "Mark N articles as read forwards.
9808 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9809 The difference between N and the actual number of articles marked is
9810 returned.
9811 If NO-EXPIRE, auto-expiry will be inhibited."
9812   (interactive "p")
9813   (gnus-summary-show-thread)
9814   (let ((backward (< n 0))
9815         (gnus-summary-goto-unread
9816          (and gnus-summary-goto-unread
9817               (not (eq gnus-summary-goto-unread 'never))
9818               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9819                                     gnus-ticked-mark gnus-dormant-mark)))))
9820         (n (abs n))
9821         (mark (or mark gnus-del-mark)))
9822     (while (and (> n 0)
9823                 (gnus-summary-mark-article nil mark no-expire)
9824                 (zerop (gnus-summary-next-subject
9825                         (if backward -1 1)
9826                         (and gnus-summary-goto-unread
9827                              (not (eq gnus-summary-goto-unread 'never)))
9828                         t)))
9829       (setq n (1- n)))
9830     (when (/= 0 n)
9831       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9832     (gnus-summary-recenter)
9833     (gnus-summary-position-point)
9834     (gnus-set-mode-line 'summary)
9835     n))
9836
9837 (defun gnus-summary-mark-article-as-read (mark)
9838   "Mark the current article quickly as read with MARK."
9839   (let ((article (gnus-summary-article-number)))
9840     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9841     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9842     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9843     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9844     (push (cons article mark) gnus-newsgroup-reads)
9845     ;; Possibly remove from cache, if that is used.
9846     (when gnus-use-cache
9847       (gnus-cache-enter-remove-article article))
9848     ;; Allow the backend to change the mark.
9849     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9850     ;; Check for auto-expiry.
9851     (when (and gnus-newsgroup-auto-expire
9852                (memq mark gnus-auto-expirable-marks))
9853       (setq mark gnus-expirable-mark)
9854       ;; Let the backend know about the mark change.
9855       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9856       (push article gnus-newsgroup-expirable))
9857     ;; Set the mark in the buffer.
9858     (gnus-summary-update-mark mark 'unread)
9859     t))
9860
9861 (defun gnus-summary-mark-article-as-unread (mark)
9862   "Mark the current article quickly as unread with MARK."
9863   (let* ((article (gnus-summary-article-number))
9864          (old-mark (gnus-summary-article-mark article)))
9865     ;; Allow the backend to change the mark.
9866     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9867     (if (eq mark old-mark)
9868         t
9869       (if (<= article 0)
9870           (progn
9871             (gnus-error 1 "Can't mark negative article numbers")
9872             nil)
9873         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9874         (setq gnus-newsgroup-spam-marked
9875               (delq article gnus-newsgroup-spam-marked))
9876         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9877         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9878         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9879         (cond ((= mark gnus-ticked-mark)
9880                (setq gnus-newsgroup-marked
9881                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9882                                               article)))
9883               ((= mark gnus-spam-mark)
9884                (setq gnus-newsgroup-spam-marked
9885                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9886                                               article)))
9887               ((= mark gnus-dormant-mark)
9888                (setq gnus-newsgroup-dormant
9889                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9890                                               article)))
9891               (t
9892                (setq gnus-newsgroup-unreads
9893                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9894                                               article))))
9895         (gnus-pull article gnus-newsgroup-reads)
9896
9897         ;; See whether the article is to be put in the cache.
9898         (and gnus-use-cache
9899              (vectorp (gnus-summary-article-header article))
9900              (save-excursion
9901                (gnus-cache-possibly-enter-article
9902                 gnus-newsgroup-name article
9903                 (= mark gnus-ticked-mark)
9904                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9905
9906         ;; Fix the mark.
9907         (gnus-summary-update-mark mark 'unread)
9908         t))))
9909
9910 (defun gnus-summary-mark-article (&optional article mark no-expire)
9911   "Mark ARTICLE with MARK.  MARK can be any character.
9912 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9913 `??' (dormant) and `?E' (expirable).
9914 If MARK is nil, then the default character `?r' is used.
9915 If ARTICLE is nil, then the article on the current line will be
9916 marked.
9917 If NO-EXPIRE, auto-expiry will be inhibited."
9918   ;; The mark might be a string.
9919   (when (stringp mark)
9920     (setq mark (aref mark 0)))
9921   ;; If no mark is given, then we check auto-expiring.
9922   (when (null mark)
9923     (setq mark gnus-del-mark))
9924   (when (and (not no-expire)
9925              gnus-newsgroup-auto-expire
9926              (memq mark gnus-auto-expirable-marks))
9927     (setq mark gnus-expirable-mark))
9928   (let ((article (or article (gnus-summary-article-number)))
9929         (old-mark (gnus-summary-article-mark article)))
9930     ;; Allow the backend to change the mark.
9931     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9932     (if (eq mark old-mark)
9933         t
9934       (unless article
9935         (error "No article on current line"))
9936       (if (not (if (or (= mark gnus-unread-mark)
9937                        (= mark gnus-ticked-mark)
9938                        (= mark gnus-spam-mark)
9939                        (= mark gnus-dormant-mark))
9940                    (gnus-mark-article-as-unread article mark)
9941                  (gnus-mark-article-as-read article mark)))
9942           t
9943         ;; See whether the article is to be put in the cache.
9944         (and gnus-use-cache
9945              (not (= mark gnus-canceled-mark))
9946              (vectorp (gnus-summary-article-header article))
9947              (save-excursion
9948                (gnus-cache-possibly-enter-article
9949                 gnus-newsgroup-name article
9950                 (= mark gnus-ticked-mark)
9951                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9952
9953         (when (gnus-summary-goto-subject article nil t)
9954           (let ((buffer-read-only nil))
9955             (gnus-summary-show-thread)
9956             ;; Fix the mark.
9957             (gnus-summary-update-mark mark 'unread)
9958             t))))))
9959
9960 (defun gnus-summary-update-secondary-mark (article)
9961   "Update the secondary (read, process, cache) mark."
9962   (gnus-summary-update-mark
9963    (cond ((memq article gnus-newsgroup-processable)
9964           gnus-process-mark)
9965          ((memq article gnus-newsgroup-cached)
9966           gnus-cached-mark)
9967          ((memq article gnus-newsgroup-replied)
9968           gnus-replied-mark)
9969          ((memq article gnus-newsgroup-forwarded)
9970           gnus-forwarded-mark)
9971          ((memq article gnus-newsgroup-saved)
9972           gnus-saved-mark)
9973          ((memq article gnus-newsgroup-recent)
9974           gnus-recent-mark)
9975          ((memq article gnus-newsgroup-unseen)
9976           gnus-unseen-mark)
9977          (t gnus-no-mark))
9978    'replied)
9979   (when (gnus-visual-p 'summary-highlight 'highlight)
9980     (gnus-run-hooks 'gnus-summary-update-hook))
9981   t)
9982
9983 (defun gnus-summary-update-download-mark (article)
9984   "Update the download mark."
9985   (gnus-summary-update-mark
9986    (cond ((memq article gnus-newsgroup-undownloaded)
9987           gnus-undownloaded-mark)
9988          (gnus-newsgroup-agentized
9989           gnus-downloaded-mark)
9990          (t
9991           gnus-no-mark))
9992    'download)
9993   (gnus-summary-update-line t)
9994   t)
9995
9996 (defun gnus-summary-update-mark (mark type)
9997   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9998         (buffer-read-only nil))
9999     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10000     (when forward
10001       (when (looking-at "\r")
10002         (incf forward))
10003       (when (<= (+ forward (point)) (point-max))
10004         ;; Go to the right position on the line.
10005         (goto-char (+ forward (point)))
10006         ;; Replace the old mark with the new mark.
10007         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10008         ;; Optionally update the marks by some user rule.
10009         (when (eq type 'unread)
10010           (gnus-data-set-mark
10011            (gnus-data-find (gnus-summary-article-number)) mark)
10012           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10013
10014 (defun gnus-mark-article-as-read (article &optional mark)
10015   "Enter ARTICLE in the pertinent lists and remove it from others."
10016   ;; Make the article expirable.
10017   (let ((mark (or mark gnus-del-mark)))
10018     (setq gnus-newsgroup-expirable
10019           (if (= mark gnus-expirable-mark)
10020               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10021             (delq article gnus-newsgroup-expirable)))
10022     ;; Remove from unread and marked lists.
10023     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10024     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10025     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10026     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10027     (push (cons article mark) gnus-newsgroup-reads)
10028     ;; Possibly remove from cache, if that is used.
10029     (when gnus-use-cache
10030       (gnus-cache-enter-remove-article article))
10031     t))
10032
10033 (defun gnus-mark-article-as-unread (article &optional mark)
10034   "Enter ARTICLE in the pertinent lists and remove it from others."
10035   (let ((mark (or mark gnus-ticked-mark)))
10036     (if (<= article 0)
10037         (progn
10038           (gnus-error 1 "Can't mark negative article numbers")
10039           nil)
10040       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10041             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10042             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10043             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10044             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10045
10046       ;; Unsuppress duplicates?
10047       (when gnus-suppress-duplicates
10048         (gnus-dup-unsuppress-article article))
10049
10050       (cond ((= mark gnus-ticked-mark)
10051              (setq gnus-newsgroup-marked
10052                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10053             ((= mark gnus-spam-mark)
10054              (setq gnus-newsgroup-spam-marked
10055                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10056                                             article)))
10057             ((= mark gnus-dormant-mark)
10058              (setq gnus-newsgroup-dormant
10059                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10060             (t
10061              (setq gnus-newsgroup-unreads
10062                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10063       (gnus-pull article gnus-newsgroup-reads)
10064       t)))
10065
10066 (defalias 'gnus-summary-mark-as-unread-forward
10067   'gnus-summary-tick-article-forward)
10068 (make-obsolete 'gnus-summary-mark-as-unread-forward
10069                'gnus-summary-tick-article-forward)
10070 (defun gnus-summary-tick-article-forward (n)
10071   "Tick N articles forwards.
10072 If N is negative, tick backwards instead.
10073 The difference between N and the number of articles ticked is returned."
10074   (interactive "p")
10075   (gnus-summary-mark-forward n gnus-ticked-mark))
10076
10077 (defalias 'gnus-summary-mark-as-unread-backward
10078   'gnus-summary-tick-article-backward)
10079 (make-obsolete 'gnus-summary-mark-as-unread-backward
10080                'gnus-summary-tick-article-backward)
10081 (defun gnus-summary-tick-article-backward (n)
10082   "Tick N articles backwards.
10083 The difference between N and the number of articles ticked is returned."
10084   (interactive "p")
10085   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10086
10087 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10088 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10089 (defun gnus-summary-tick-article (&optional article clear-mark)
10090   "Mark current article as unread.
10091 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10092 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10093   (interactive)
10094   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10095                                        gnus-ticked-mark)))
10096
10097 (defun gnus-summary-mark-as-read-forward (n)
10098   "Mark N articles as read forwards.
10099 If N is negative, mark backwards instead.
10100 The difference between N and the actual number of articles marked is
10101 returned."
10102   (interactive "p")
10103   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10104
10105 (defun gnus-summary-mark-as-read-backward (n)
10106   "Mark the N articles as read backwards.
10107 The difference between N and the actual number of articles marked is
10108 returned."
10109   (interactive "p")
10110   (gnus-summary-mark-forward
10111    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10112
10113 (defun gnus-summary-mark-as-read (&optional article mark)
10114   "Mark current article as read.
10115 ARTICLE specifies the article to be marked as read.
10116 MARK specifies a string to be inserted at the beginning of the line."
10117   (gnus-summary-mark-article article mark))
10118
10119 (defun gnus-summary-clear-mark-forward (n)
10120   "Clear marks from N articles forward.
10121 If N is negative, clear backward instead.
10122 The difference between N and the number of marks cleared is returned."
10123   (interactive "p")
10124   (gnus-summary-mark-forward n gnus-unread-mark))
10125
10126 (defun gnus-summary-clear-mark-backward (n)
10127   "Clear marks from N articles backward.
10128 The difference between N and the number of marks cleared is returned."
10129   (interactive "p")
10130   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10131
10132 (defun gnus-summary-mark-unread-as-read ()
10133   "Intended to be used by `gnus-summary-mark-article-hook'."
10134   (when (memq gnus-current-article gnus-newsgroup-unreads)
10135     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10136
10137 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10138   "Intended to be used by `gnus-summary-mark-article-hook'."
10139   (let ((mark (gnus-summary-article-mark)))
10140     (when (or (gnus-unread-mark-p mark)
10141               (gnus-read-mark-p mark))
10142       (gnus-summary-mark-article gnus-current-article
10143                                  (or new-mark gnus-read-mark)))))
10144
10145 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10146   "Intended to be used by `gnus-summary-mark-article-hook'."
10147   (let ((mark (gnus-summary-article-mark)))
10148     (when (or (gnus-unread-mark-p mark)
10149               (gnus-read-mark-p mark))
10150       (gnus-summary-mark-article (gnus-summary-article-number)
10151                                  (or new-mark gnus-read-mark)))))
10152
10153 (defun gnus-summary-mark-unread-as-ticked ()
10154   "Intended to be used by `gnus-summary-mark-article-hook'."
10155   (when (memq gnus-current-article gnus-newsgroup-unreads)
10156     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10157
10158 (defun gnus-summary-mark-region-as-read (point mark all)
10159   "Mark all unread articles between point and mark as read.
10160 If given a prefix, mark all articles between point and mark as read,
10161 even ticked and dormant ones."
10162   (interactive "r\nP")
10163   (save-excursion
10164     (let (article)
10165       (goto-char point)
10166       (beginning-of-line)
10167       (while (and
10168               (< (point) mark)
10169               (progn
10170                 (when (or all
10171                           (memq (setq article (gnus-summary-article-number))
10172                                 gnus-newsgroup-unreads))
10173                   (gnus-summary-mark-article article gnus-del-mark))
10174                 t)
10175               (gnus-summary-find-next))))))
10176
10177 (defun gnus-summary-mark-below (score mark)
10178   "Mark articles with score less than SCORE with MARK."
10179   (interactive "P\ncMark: ")
10180   (setq score (if score
10181                   (prefix-numeric-value score)
10182                 (or gnus-summary-default-score 0)))
10183   (save-excursion
10184     (set-buffer gnus-summary-buffer)
10185     (goto-char (point-min))
10186     (while
10187         (progn
10188           (and (< (gnus-summary-article-score) score)
10189                (gnus-summary-mark-article nil mark))
10190           (gnus-summary-find-next)))))
10191
10192 (defun gnus-summary-kill-below (&optional score)
10193   "Mark articles with score below SCORE as read."
10194   (interactive "P")
10195   (gnus-summary-mark-below score gnus-killed-mark))
10196
10197 (defun gnus-summary-clear-above (&optional score)
10198   "Clear all marks from articles with score above SCORE."
10199   (interactive "P")
10200   (gnus-summary-mark-above score gnus-unread-mark))
10201
10202 (defun gnus-summary-tick-above (&optional score)
10203   "Tick all articles with score above SCORE."
10204   (interactive "P")
10205   (gnus-summary-mark-above score gnus-ticked-mark))
10206
10207 (defun gnus-summary-mark-above (score mark)
10208   "Mark articles with score over SCORE with MARK."
10209   (interactive "P\ncMark: ")
10210   (setq score (if score
10211                   (prefix-numeric-value score)
10212                 (or gnus-summary-default-score 0)))
10213   (save-excursion
10214     (set-buffer gnus-summary-buffer)
10215     (goto-char (point-min))
10216     (while (and (progn
10217                   (when (> (gnus-summary-article-score) score)
10218                     (gnus-summary-mark-article nil mark))
10219                   t)
10220                 (gnus-summary-find-next)))))
10221
10222 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10223 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10224 (defun gnus-summary-limit-include-expunged (&optional no-error)
10225   "Display all the hidden articles that were expunged for low scores."
10226   (interactive)
10227   (let ((buffer-read-only nil))
10228     (let ((scored gnus-newsgroup-scored)
10229           headers h)
10230       (while scored
10231         (unless (gnus-summary-article-header (caar scored))
10232           (and (setq h (gnus-number-to-header (caar scored)))
10233                (< (cdar scored) gnus-summary-expunge-below)
10234                (push h headers)))
10235         (setq scored (cdr scored)))
10236       (if (not headers)
10237           (when (not no-error)
10238             (error "No expunged articles hidden"))
10239         (goto-char (point-min))
10240         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10241         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10242         (mapcar (lambda (x) (push (mail-header-number x)
10243                                   gnus-newsgroup-limit))
10244                 headers)
10245         (gnus-summary-prepare-unthreaded (nreverse headers))
10246         (goto-char (point-min))
10247         (gnus-summary-position-point)
10248         t))))
10249
10250 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10251   "Mark all unread articles in this newsgroup as read.
10252 If prefix argument ALL is non-nil, ticked and dormant articles will
10253 also be marked as read.
10254 If QUIETLY is non-nil, no questions will be asked.
10255
10256 If TO-HERE is non-nil, it should be a point in the buffer.  All
10257 articles before (after, if REVERSE is set) this point will be marked
10258 as read.
10259
10260 Note that this function will only catch up the unread article
10261 in the current summary buffer limitation.
10262
10263 The number of articles marked as read is returned."
10264   (interactive "P")
10265   (prog1
10266       (save-excursion
10267         (when (or quietly
10268                   (not gnus-interactive-catchup) ;Without confirmation?
10269                   gnus-expert-user
10270                   (gnus-y-or-n-p
10271                    (if all
10272                        "Mark absolutely all articles as read? "
10273                      "Mark all unread articles as read? ")))
10274           (if (and not-mark
10275                    (not gnus-newsgroup-adaptive)
10276                    (not gnus-newsgroup-auto-expire)
10277                    (not gnus-suppress-duplicates)
10278                    (or (not gnus-use-cache)
10279                        (eq gnus-use-cache 'passive)))
10280               (progn
10281                 (when all
10282                   (setq gnus-newsgroup-marked nil
10283                         gnus-newsgroup-spam-marked nil
10284                         gnus-newsgroup-dormant nil))
10285                 (setq gnus-newsgroup-unreads
10286                       (gnus-sorted-nunion
10287                        (gnus-intersection gnus-newsgroup-unreads
10288                                           gnus-newsgroup-downloadable)
10289                        gnus-newsgroup-unfetched)))
10290             ;; We actually mark all articles as canceled, which we
10291             ;; have to do when using auto-expiry or adaptive scoring.
10292             (gnus-summary-show-all-threads)
10293             (if (and to-here reverse)
10294                 (progn
10295                   (goto-char to-here)
10296                   (gnus-summary-mark-current-read-and-unread-as-read
10297                    gnus-catchup-mark)
10298                   (while (gnus-summary-find-next (not all))
10299                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10300               (when (gnus-summary-first-subject (not all))
10301                 (while (and
10302                         (if to-here (< (point) to-here) t)
10303                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10304                         (gnus-summary-find-next (not all))))))
10305             (gnus-set-mode-line 'summary))
10306           t))
10307     (gnus-summary-position-point)))
10308
10309 (defun gnus-summary-catchup-to-here (&optional all)
10310   "Mark all unticked articles before the current one as read.
10311 If ALL is non-nil, also mark ticked and dormant articles as read."
10312   (interactive "P")
10313   (save-excursion
10314     (gnus-save-hidden-threads
10315       (let ((beg (point)))
10316         ;; We check that there are unread articles.
10317         (when (or all (gnus-summary-find-prev))
10318           (gnus-summary-catchup all t beg)))))
10319   (gnus-summary-position-point))
10320
10321 (defun gnus-summary-catchup-from-here (&optional all)
10322   "Mark all unticked articles after (and including) the current one as read.
10323 If ALL is non-nil, also mark ticked and dormant articles as read."
10324   (interactive "P")
10325   (save-excursion
10326     (gnus-save-hidden-threads
10327       (let ((beg (point)))
10328         ;; We check that there are unread articles.
10329         (when (or all (gnus-summary-find-next))
10330           (gnus-summary-catchup all t beg nil t)))))
10331   (gnus-summary-position-point))
10332
10333 (defun gnus-summary-catchup-all (&optional quietly)
10334   "Mark all articles in this newsgroup as read.
10335 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10336 instead, which marks only unread articles as read."
10337   (interactive "P")
10338   (gnus-summary-catchup t quietly))
10339
10340 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10341   "Mark all unread articles in this group as read, then exit.
10342 If prefix argument ALL is non-nil, all articles are marked as read.
10343 If QUIETLY is non-nil, no questions will be asked."
10344   (interactive "P")
10345   (when (gnus-summary-catchup all quietly nil 'fast)
10346     ;; Select next newsgroup or exit.
10347     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10348              (eq gnus-auto-select-next 'quietly))
10349         (gnus-summary-next-group nil)
10350       (gnus-summary-exit))))
10351
10352 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10353   "Mark all articles in this newsgroup as read, and then exit.
10354 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10355 instead, which marks only unread articles as read."
10356   (interactive "P")
10357   (gnus-summary-catchup-and-exit t quietly))
10358
10359 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10360   "Mark all articles in this group as read and select the next group.
10361 If given a prefix, mark all articles, unread as well as ticked, as
10362 read."
10363   (interactive "P")
10364   (save-excursion
10365     (gnus-summary-catchup all))
10366   (gnus-summary-next-group))
10367
10368 ;;;
10369 ;;; with article
10370 ;;;
10371
10372 (defmacro gnus-with-article (article &rest forms)
10373   "Select ARTICLE and perform FORMS in the original article buffer.
10374 Then replace the article with the result."
10375   `(progn
10376      ;; We don't want the article to be marked as read.
10377      (let (gnus-mark-article-hook)
10378        (gnus-summary-select-article t t nil ,article))
10379      (set-buffer gnus-original-article-buffer)
10380      ,@forms
10381      (if (not (gnus-check-backend-function
10382                'request-replace-article (car gnus-article-current)))
10383          (gnus-message 5 "Read-only group; not replacing")
10384        (unless (gnus-request-replace-article
10385                 ,article (car gnus-article-current)
10386                 (current-buffer) t)
10387          (error "Couldn't replace article")))
10388      ;; The cache and backlog have to be flushed somewhat.
10389      (when gnus-keep-backlog
10390        (gnus-backlog-remove-article
10391         (car gnus-article-current) (cdr gnus-article-current)))
10392      (when gnus-use-cache
10393        (gnus-cache-update-article
10394         (car gnus-article-current) (cdr gnus-article-current)))))
10395
10396 (put 'gnus-with-article 'lisp-indent-function 1)
10397 (put 'gnus-with-article 'edebug-form-spec '(form body))
10398
10399 ;; Thread-based commands.
10400
10401 (defun gnus-summary-articles-in-thread (&optional article)
10402   "Return a list of all articles in the current thread.
10403 If ARTICLE is non-nil, return all articles in the thread that starts
10404 with that article."
10405   (let* ((article (or article (gnus-summary-article-number)))
10406          (data (gnus-data-find-list article))
10407          (top-level (gnus-data-level (car data)))
10408          (top-subject
10409           (cond ((null gnus-thread-operation-ignore-subject)
10410                  (gnus-simplify-subject-re
10411                   (mail-header-subject (gnus-data-header (car data)))))
10412                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10413                  (gnus-simplify-subject-fuzzy
10414                   (mail-header-subject (gnus-data-header (car data)))))
10415                 (t nil)))
10416          (end-point (save-excursion
10417                       (if (gnus-summary-go-to-next-thread)
10418                           (point) (point-max))))
10419          articles)
10420     (while (and data
10421                 (< (gnus-data-pos (car data)) end-point))
10422       (when (or (not top-subject)
10423                 (string= top-subject
10424                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10425                              (gnus-simplify-subject-fuzzy
10426                               (mail-header-subject
10427                                (gnus-data-header (car data))))
10428                            (gnus-simplify-subject-re
10429                             (mail-header-subject
10430                              (gnus-data-header (car data)))))))
10431         (push (gnus-data-number (car data)) articles))
10432       (unless (and (setq data (cdr data))
10433                    (> (gnus-data-level (car data)) top-level))
10434         (setq data nil)))
10435     ;; Return the list of articles.
10436     (nreverse articles)))
10437
10438 (defun gnus-summary-rethread-current ()
10439   "Rethread the thread the current article is part of."
10440   (interactive)
10441   (let* ((gnus-show-threads t)
10442          (article (gnus-summary-article-number))
10443          (id (mail-header-id (gnus-summary-article-header)))
10444          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10445     (unless id
10446       (error "No article on the current line"))
10447     (gnus-rebuild-thread id)
10448     (gnus-summary-goto-subject article)))
10449
10450 (defun gnus-summary-reparent-thread ()
10451   "Make the current article child of the marked (or previous) article.
10452
10453 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10454 is non-nil or the Subject: of both articles are the same."
10455   (interactive)
10456   (unless (not (gnus-group-read-only-p))
10457     (error "The current newsgroup does not support article editing"))
10458   (unless (<= (length gnus-newsgroup-processable) 1)
10459     (error "No more than one article may be marked"))
10460   (save-window-excursion
10461     (let ((gnus-article-buffer " *reparent*")
10462           (current-article (gnus-summary-article-number))
10463           ;; First grab the marked article, otherwise one line up.
10464           (parent-article (if (not (null gnus-newsgroup-processable))
10465                               (car gnus-newsgroup-processable)
10466                             (save-excursion
10467                               (if (eq (forward-line -1) 0)
10468                                   (gnus-summary-article-number)
10469                                 (error "Beginning of summary buffer"))))))
10470       (unless (not (eq current-article parent-article))
10471         (error "An article may not be self-referential"))
10472       (let ((message-id (mail-header-id
10473                          (gnus-summary-article-header parent-article))))
10474         (unless (and message-id (not (equal message-id "")))
10475           (error "No message-id in desired parent"))
10476         (gnus-with-article current-article
10477           (save-restriction
10478             (goto-char (point-min))
10479             (message-narrow-to-head)
10480             (if (re-search-forward "^References: " nil t)
10481                 (progn
10482                   (re-search-forward "^[^ \t]" nil t)
10483                   (forward-line -1)
10484                   (end-of-line)
10485                   (insert " " message-id))
10486               (insert "References: " message-id "\n"))))
10487         (set-buffer gnus-summary-buffer)
10488         (gnus-summary-unmark-all-processable)
10489         (gnus-summary-update-article current-article)
10490         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10491             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10492         (gnus-summary-rethread-current)
10493         (gnus-message 3 "Article %d is now the child of article %d"
10494                       current-article parent-article)))))
10495
10496 (defun gnus-summary-toggle-threads (&optional arg)
10497   "Toggle showing conversation threads.
10498 If ARG is positive number, turn showing conversation threads on."
10499   (interactive "P")
10500   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10501     (setq gnus-show-threads
10502           (if (null arg) (not gnus-show-threads)
10503             (> (prefix-numeric-value arg) 0)))
10504     (gnus-summary-prepare)
10505     (gnus-summary-goto-subject current)
10506     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10507     (gnus-summary-position-point)))
10508
10509 (defun gnus-summary-show-all-threads ()
10510   "Show all threads."
10511   (interactive)
10512   (save-excursion
10513     (let ((buffer-read-only nil))
10514       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10515   (gnus-summary-position-point))
10516
10517 (defun gnus-summary-show-thread ()
10518   "Show thread subtrees.
10519 Returns nil if no thread was there to be shown."
10520   (interactive)
10521   (let ((buffer-read-only nil)
10522         (orig (point))
10523         (end (point-at-eol))
10524         ;; Leave point at bol
10525         (beg (progn (beginning-of-line) (point))))
10526     (prog1
10527         ;; Any hidden lines here?
10528         (search-forward "\r" end t)
10529       (subst-char-in-region beg end ?\^M ?\n t)
10530       (goto-char orig)
10531       (gnus-summary-position-point))))
10532
10533 (defun gnus-summary-maybe-hide-threads ()
10534   "If requested, hide the threads that should be hidden."
10535   (when (and gnus-show-threads
10536              gnus-thread-hide-subtree)
10537     (gnus-summary-hide-all-threads
10538      (if (or (consp gnus-thread-hide-subtree)
10539              (functionp gnus-thread-hide-subtree))
10540          (gnus-make-predicate gnus-thread-hide-subtree)
10541        nil))))
10542
10543 ;;; Hiding predicates.
10544
10545 (defun gnus-article-unread-p (header)
10546   (memq (mail-header-number header) gnus-newsgroup-unreads))
10547
10548 (defun gnus-article-unseen-p (header)
10549   (memq (mail-header-number header) gnus-newsgroup-unseen))
10550
10551 (defun gnus-map-articles (predicate articles)
10552   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10553   (apply 'gnus-or (mapcar predicate
10554                           (mapcar 'gnus-summary-article-header articles))))
10555
10556 (defun gnus-summary-hide-all-threads (&optional predicate)
10557   "Hide all thread subtrees.
10558 If PREDICATE is supplied, threads that satisfy this predicate
10559 will not be hidden."
10560   (interactive)
10561   (save-excursion
10562     (goto-char (point-min))
10563     (let ((end nil))
10564       (while (not end)
10565         (when (or (not predicate)
10566                   (gnus-map-articles
10567                    predicate (gnus-summary-article-children)))
10568             (gnus-summary-hide-thread))
10569         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10570   (gnus-summary-position-point))
10571
10572 (defun gnus-summary-hide-thread ()
10573   "Hide thread subtrees.
10574 If PREDICATE is supplied, threads that satisfy this predicate
10575 will not be hidden.
10576 Returns nil if no threads were there to be hidden."
10577   (interactive)
10578   (let ((buffer-read-only nil)
10579         (start (point))
10580         (article (gnus-summary-article-number)))
10581     (goto-char start)
10582     ;; Go forward until either the buffer ends or the subthread
10583     ;; ends.
10584     (when (and (not (eobp))
10585                (or (zerop (gnus-summary-next-thread 1 t))
10586                    (goto-char (point-max))))
10587       (prog1
10588           (if (and (> (point) start)
10589                    (search-backward "\n" start t))
10590               (progn
10591                 (subst-char-in-region start (point) ?\n ?\^M)
10592                 (gnus-summary-goto-subject article))
10593             (goto-char start)
10594             nil)))))
10595
10596 (defun gnus-summary-go-to-next-thread (&optional previous)
10597   "Go to the same level (or less) next thread.
10598 If PREVIOUS is non-nil, go to previous thread instead.
10599 Return the article number moved to, or nil if moving was impossible."
10600   (let ((level (gnus-summary-thread-level))
10601         (way (if previous -1 1))
10602         (beg (point)))
10603     (forward-line way)
10604     (while (and (not (eobp))
10605                 (< level (gnus-summary-thread-level)))
10606       (forward-line way))
10607     (if (eobp)
10608         (progn
10609           (goto-char beg)
10610           nil)
10611       (setq beg (point))
10612       (prog1
10613           (gnus-summary-article-number)
10614         (goto-char beg)))))
10615
10616 (defun gnus-summary-next-thread (n &optional silent)
10617   "Go to the same level next N'th thread.
10618 If N is negative, search backward instead.
10619 Returns the difference between N and the number of skips actually
10620 done.
10621
10622 If SILENT, don't output messages."
10623   (interactive "p")
10624   (let ((backward (< n 0))
10625         (n (abs n)))
10626     (while (and (> n 0)
10627                 (gnus-summary-go-to-next-thread backward))
10628       (decf n))
10629     (unless silent
10630       (gnus-summary-position-point))
10631     (when (and (not silent) (/= 0 n))
10632       (gnus-message 7 "No more threads"))
10633     n))
10634
10635 (defun gnus-summary-prev-thread (n)
10636   "Go to the same level previous N'th thread.
10637 Returns the difference between N and the number of skips actually
10638 done."
10639   (interactive "p")
10640   (gnus-summary-next-thread (- n)))
10641
10642 (defun gnus-summary-go-down-thread ()
10643   "Go down one level in the current thread."
10644   (let ((children (gnus-summary-article-children)))
10645     (when children
10646       (gnus-summary-goto-subject (car children)))))
10647
10648 (defun gnus-summary-go-up-thread ()
10649   "Go up one level in the current thread."
10650   (let ((parent (gnus-summary-article-parent)))
10651     (when parent
10652       (gnus-summary-goto-subject parent))))
10653
10654 (defun gnus-summary-down-thread (n)
10655   "Go down thread N steps.
10656 If N is negative, go up instead.
10657 Returns the difference between N and how many steps down that were
10658 taken."
10659   (interactive "p")
10660   (let ((up (< n 0))
10661         (n (abs n)))
10662     (while (and (> n 0)
10663                 (if up (gnus-summary-go-up-thread)
10664                   (gnus-summary-go-down-thread)))
10665       (setq n (1- n)))
10666     (gnus-summary-position-point)
10667     (when (/= 0 n)
10668       (gnus-message 7 "Can't go further"))
10669     n))
10670
10671 (defun gnus-summary-up-thread (n)
10672   "Go up thread N steps.
10673 If N is negative, go down instead.
10674 Returns the difference between N and how many steps down that were
10675 taken."
10676   (interactive "p")
10677   (gnus-summary-down-thread (- n)))
10678
10679 (defun gnus-summary-top-thread ()
10680   "Go to the top of the thread."
10681   (interactive)
10682   (while (gnus-summary-go-up-thread))
10683   (gnus-summary-article-number))
10684
10685 (defun gnus-summary-kill-thread (&optional unmark)
10686   "Mark articles under current thread as read.
10687 If the prefix argument is positive, remove any kinds of marks.
10688 If the prefix argument is negative, tick articles instead."
10689   (interactive "P")
10690   (when unmark
10691     (setq unmark (prefix-numeric-value unmark)))
10692   (let ((articles (gnus-summary-articles-in-thread)))
10693     (save-excursion
10694       ;; Expand the thread.
10695       (gnus-summary-show-thread)
10696       ;; Mark all the articles.
10697       (while articles
10698         (gnus-summary-goto-subject (car articles))
10699         (cond ((null unmark)
10700                (gnus-summary-mark-article-as-read gnus-killed-mark))
10701               ((> unmark 0)
10702                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10703               (t
10704                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10705         (setq articles (cdr articles))))
10706     ;; Hide killed subtrees.
10707     (and (null unmark)
10708          gnus-thread-hide-killed
10709          (gnus-summary-hide-thread))
10710     ;; If marked as read, go to next unread subject.
10711     (when (null unmark)
10712       ;; Go to next unread subject.
10713       (gnus-summary-next-subject 1 t)))
10714   (gnus-set-mode-line 'summary))
10715
10716 ;; Summary sorting commands
10717
10718 (defun gnus-summary-sort-by-number (&optional reverse)
10719   "Sort the summary buffer by article number.
10720 Argument REVERSE means reverse order."
10721   (interactive "P")
10722   (gnus-summary-sort 'number reverse))
10723
10724 (defun gnus-summary-sort-by-random (&optional reverse)
10725   "Randomize the order in the summary buffer.
10726 Argument REVERSE means to randomize in reverse order."
10727   (interactive "P")
10728   (gnus-summary-sort 'random reverse))
10729
10730 (defun gnus-summary-sort-by-author (&optional reverse)
10731   "Sort the summary buffer by author name alphabetically.
10732 If `case-fold-search' is non-nil, case of letters is ignored.
10733 Argument REVERSE means reverse order."
10734   (interactive "P")
10735   (gnus-summary-sort 'author reverse))
10736
10737 (defun gnus-summary-sort-by-subject (&optional reverse)
10738   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10739 If `case-fold-search' is non-nil, case of letters is ignored.
10740 Argument REVERSE means reverse order."
10741   (interactive "P")
10742   (gnus-summary-sort 'subject reverse))
10743
10744 (defun gnus-summary-sort-by-date (&optional reverse)
10745   "Sort the summary buffer by date.
10746 Argument REVERSE means reverse order."
10747   (interactive "P")
10748   (gnus-summary-sort 'date reverse))
10749
10750 (defun gnus-summary-sort-by-score (&optional reverse)
10751   "Sort the summary buffer by score.
10752 Argument REVERSE means reverse order."
10753   (interactive "P")
10754   (gnus-summary-sort 'score reverse))
10755
10756 (defun gnus-summary-sort-by-lines (&optional reverse)
10757   "Sort the summary buffer by the number of lines.
10758 Argument REVERSE means reverse order."
10759   (interactive "P")
10760   (gnus-summary-sort 'lines reverse))
10761
10762 (defun gnus-summary-sort-by-chars (&optional reverse)
10763   "Sort the summary buffer by article length.
10764 Argument REVERSE means reverse order."
10765   (interactive "P")
10766   (gnus-summary-sort 'chars reverse))
10767
10768 (defun gnus-summary-sort-by-original (&optional reverse)
10769   "Sort the summary buffer using the default sorting method.
10770 Argument REVERSE means reverse order."
10771   (interactive "P")
10772   (let* ((buffer-read-only)
10773          (gnus-summary-prepare-hook nil))
10774     ;; We do the sorting by regenerating the threads.
10775     (gnus-summary-prepare)
10776     ;; Hide subthreads if needed.
10777     (gnus-summary-maybe-hide-threads)))
10778
10779 (defun gnus-summary-sort (predicate reverse)
10780   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10781   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10782          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10783          (gnus-thread-sort-functions
10784           (if (not reverse)
10785               thread
10786             `(lambda (t1 t2)
10787                (,thread t2 t1))))
10788          (gnus-sort-gathered-threads-function
10789           gnus-thread-sort-functions)
10790          (gnus-article-sort-functions
10791           (if (not reverse)
10792               article
10793             `(lambda (t1 t2)
10794                (,article t2 t1))))
10795          (buffer-read-only)
10796          (gnus-summary-prepare-hook nil))
10797     ;; We do the sorting by regenerating the threads.
10798     (gnus-summary-prepare)
10799     ;; Hide subthreads if needed.
10800     (gnus-summary-maybe-hide-threads)))
10801
10802 ;; Summary saving commands.
10803
10804 (defun gnus-summary-save-article (&optional n not-saved)
10805   "Save the current article using the default saver function.
10806 If N is a positive number, save the N next articles.
10807 If N is a negative number, save the N previous articles.
10808 If N is nil and any articles have been marked with the process mark,
10809 save those articles instead.
10810 The variable `gnus-default-article-saver' specifies the saver function."
10811   (interactive "P")
10812   (let* ((articles (gnus-summary-work-articles n))
10813          (save-buffer (save-excursion
10814                         (nnheader-set-temp-buffer " *Gnus Save*")))
10815          (num (length articles))
10816          header file)
10817     (dolist (article articles)
10818       (setq header (gnus-summary-article-header article))
10819       (if (not (vectorp header))
10820           ;; This is a pseudo-article.
10821           (if (assq 'name header)
10822               (gnus-copy-file (cdr (assq 'name header)))
10823             (gnus-message 1 "Article %d is unsaveable" article))
10824         ;; This is a real article.
10825         (save-window-excursion
10826           (let ((gnus-display-mime-function nil)
10827                 (gnus-article-prepare-hook nil))
10828             (gnus-summary-select-article t nil nil article)))
10829         (save-excursion
10830           (set-buffer save-buffer)
10831           (erase-buffer)
10832           (insert-buffer-substring gnus-original-article-buffer))
10833         (setq file (gnus-article-save save-buffer file num))
10834         (gnus-summary-remove-process-mark article)
10835         (unless not-saved
10836           (gnus-summary-set-saved-mark article))))
10837     (gnus-kill-buffer save-buffer)
10838     (gnus-summary-position-point)
10839     (gnus-set-mode-line 'summary)
10840     n))
10841
10842 (defun gnus-summary-pipe-output (&optional arg headers)
10843   "Pipe the current article to a subprocess.
10844 If N is a positive number, pipe the N next articles.
10845 If N is a negative number, pipe the N previous articles.
10846 If N is nil and any articles have been marked with the process mark,
10847 pipe those articles instead.
10848 If HEADERS (the symbolic prefix), include the headers, too."
10849   (interactive (gnus-interactive "P\ny"))
10850   (require 'gnus-art)
10851   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10852         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10853     (gnus-summary-save-article arg t))
10854   (let ((buffer (get-buffer "*Shell Command Output*")))
10855     (when (and buffer
10856                (not (zerop (buffer-size buffer))))
10857       (gnus-configure-windows 'pipe))))
10858
10859 (defun gnus-summary-save-article-mail (&optional arg)
10860   "Append the current article to an mail file.
10861 If N is a positive number, save the N next articles.
10862 If N is a negative number, save the N previous articles.
10863 If N is nil and any articles have been marked with the process mark,
10864 save those articles instead."
10865   (interactive "P")
10866   (require 'gnus-art)
10867   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10868     (gnus-summary-save-article arg)))
10869
10870 (defun gnus-summary-save-article-rmail (&optional arg)
10871   "Append the current article to an rmail file.
10872 If N is a positive number, save the N next articles.
10873 If N is a negative number, save the N previous articles.
10874 If N is nil and any articles have been marked with the process mark,
10875 save those articles instead."
10876   (interactive "P")
10877   (require 'gnus-art)
10878   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10879     (gnus-summary-save-article arg)))
10880
10881 (defun gnus-summary-save-article-file (&optional arg)
10882   "Append the current article to a file.
10883 If N is a positive number, save the N next articles.
10884 If N is a negative number, save the N previous articles.
10885 If N is nil and any articles have been marked with the process mark,
10886 save those articles instead."
10887   (interactive "P")
10888   (require 'gnus-art)
10889   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10890     (gnus-summary-save-article arg)))
10891
10892 (defun gnus-summary-write-article-file (&optional arg)
10893   "Write the current article to a file, deleting the previous file.
10894 If N is a positive number, save the N next articles.
10895 If N is a negative number, save the N previous articles.
10896 If N is nil and any articles have been marked with the process mark,
10897 save those articles instead."
10898   (interactive "P")
10899   (require 'gnus-art)
10900   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10901     (gnus-summary-save-article arg)))
10902
10903 (defun gnus-summary-save-article-body-file (&optional arg)
10904   "Append the current article body to a file.
10905 If N is a positive number, save the N next articles.
10906 If N is a negative number, save the N previous articles.
10907 If N is nil and any articles have been marked with the process mark,
10908 save those articles instead."
10909   (interactive "P")
10910   (require 'gnus-art)
10911   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10912     (gnus-summary-save-article arg)))
10913
10914 (defun gnus-summary-muttprint (&optional arg)
10915   "Print the current article using Muttprint.
10916 If N is a positive number, save the N next articles.
10917 If N is a negative number, save the N previous articles.
10918 If N is nil and any articles have been marked with the process mark,
10919 save those articles instead."
10920   (interactive "P")
10921   (require 'gnus-art)
10922   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10923     (gnus-summary-save-article arg t)))
10924
10925 (defun gnus-summary-pipe-message (program)
10926   "Pipe the current article through PROGRAM."
10927   (interactive "sProgram: ")
10928   (gnus-summary-select-article)
10929   (let ((mail-header-separator ""))
10930     (gnus-eval-in-buffer-window gnus-article-buffer
10931       (save-restriction
10932         (widen)
10933         (let ((start (window-start))
10934               buffer-read-only)
10935           (message-pipe-buffer-body program)
10936           (set-window-start (get-buffer-window (current-buffer)) start))))))
10937
10938 (defun gnus-get-split-value (methods)
10939   "Return a value based on the split METHODS."
10940   (let (split-name method result match)
10941     (when methods
10942       (save-excursion
10943         (set-buffer gnus-original-article-buffer)
10944         (save-restriction
10945           (nnheader-narrow-to-headers)
10946           (while (and methods (not split-name))
10947             (goto-char (point-min))
10948             (setq method (pop methods))
10949             (setq match (car method))
10950             (when (cond
10951                    ((stringp match)
10952                     ;; Regular expression.
10953                     (ignore-errors
10954                       (re-search-forward match nil t)))
10955                    ((functionp match)
10956                     ;; Function.
10957                     (save-restriction
10958                       (widen)
10959                       (setq result (funcall match gnus-newsgroup-name))))
10960                    ((consp match)
10961                     ;; Form.
10962                     (save-restriction
10963                       (widen)
10964                       (setq result (eval match)))))
10965               (setq split-name (cdr method))
10966               (cond ((stringp result)
10967                      (push (expand-file-name
10968                             result gnus-article-save-directory)
10969                            split-name))
10970                     ((consp result)
10971                      (setq split-name (append result split-name)))))))))
10972     (nreverse split-name)))
10973
10974 (defun gnus-valid-move-group-p (group)
10975   (and (boundp group)
10976        (symbol-name group)
10977        (symbol-value group)
10978        (gnus-get-function (gnus-find-method-for-group
10979                            (symbol-name group)) 'request-accept-article t)))
10980
10981 (defun gnus-read-move-group-name (prompt default articles prefix)
10982   "Read a group name."
10983   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10984          (minibuffer-confirm-incomplete nil) ; XEmacs
10985          (prom
10986           (format "%s %s to:"
10987                   prompt
10988                   (if (> (length articles) 1)
10989                       (format "these %d articles" (length articles))
10990                     "this article")))
10991          (to-newsgroup
10992           (cond
10993            ((null split-name)
10994             (gnus-completing-read-with-default
10995              default prom
10996              gnus-active-hashtb
10997              'gnus-valid-move-group-p
10998              nil prefix
10999              'gnus-group-history))
11000            ((= 1 (length split-name))
11001             (gnus-completing-read-with-default
11002              (car split-name) prom
11003              gnus-active-hashtb
11004              'gnus-valid-move-group-p
11005              nil nil
11006              'gnus-group-history))
11007            (t
11008             (gnus-completing-read-with-default
11009              nil prom
11010              (mapcar (lambda (el) (list el))
11011                      (nreverse split-name))
11012              nil nil nil
11013              'gnus-group-history))))
11014          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11015     (when to-newsgroup
11016       (if (or (string= to-newsgroup "")
11017               (string= to-newsgroup prefix))
11018           (setq to-newsgroup default))
11019       (unless to-newsgroup
11020         (error "No group name entered"))
11021       (or (gnus-active to-newsgroup)
11022           (gnus-activate-group to-newsgroup nil nil to-method)
11023           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11024                                      to-newsgroup))
11025               (or (and (gnus-request-create-group to-newsgroup to-method)
11026                        (gnus-activate-group
11027                         to-newsgroup nil nil to-method)
11028                        (gnus-subscribe-group to-newsgroup))
11029                   (error "Couldn't create group %s" to-newsgroup)))
11030           (error "No such group: %s" to-newsgroup)))
11031     to-newsgroup))
11032
11033 (defun gnus-summary-save-parts (type dir n &optional reverse)
11034   "Save parts matching TYPE to DIR.
11035 If REVERSE, save parts that do not match TYPE."
11036   (interactive
11037    (list (read-string "Save parts of type: "
11038                       (or (car gnus-summary-save-parts-type-history)
11039                           gnus-summary-save-parts-default-mime)
11040                       'gnus-summary-save-parts-type-history)
11041          (setq gnus-summary-save-parts-last-directory
11042                (read-file-name "Save to directory: "
11043                                gnus-summary-save-parts-last-directory
11044                                nil t))
11045          current-prefix-arg))
11046   (gnus-summary-iterate n
11047     (let ((gnus-display-mime-function nil)
11048           (gnus-inhibit-treatment t))
11049       (gnus-summary-select-article))
11050     (save-excursion
11051       (set-buffer gnus-article-buffer)
11052       (let ((handles (or gnus-article-mime-handles
11053                          (mm-dissect-buffer nil gnus-article-loose-mime)
11054                          (and gnus-article-emulate-mime
11055                               (mm-uu-dissect)))))
11056         (when handles
11057           (gnus-summary-save-parts-1 type dir handles reverse)
11058           (unless gnus-article-mime-handles ;; Don't destroy this case.
11059             (mm-destroy-parts handles)))))))
11060
11061 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11062   (if (stringp (car handle))
11063       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11064               (cdr handle))
11065     (when (if reverse
11066               (not (string-match type (mm-handle-media-type handle)))
11067             (string-match type (mm-handle-media-type handle)))
11068       (let ((file (expand-file-name
11069                    (gnus-map-function
11070                     mm-file-name-rewrite-functions
11071                     (file-name-nondirectory
11072                      (or
11073                       (mail-content-type-get
11074                        (mm-handle-disposition handle) 'filename)
11075                       (mail-content-type-get
11076                        (mm-handle-type handle) 'name)
11077                       (concat gnus-newsgroup-name
11078                               "." (number-to-string
11079                                    (cdr gnus-article-current))))))
11080                    dir)))
11081         (unless (file-exists-p file)
11082           (mm-save-part-to-file handle file))))))
11083
11084 ;; Summary extract commands
11085
11086 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11087   (let ((buffer-read-only nil)
11088         (article (gnus-summary-article-number))
11089         after-article b e)
11090     (unless (gnus-summary-goto-subject article)
11091       (error "No such article: %d" article))
11092     (gnus-summary-position-point)
11093     ;; If all commands are to be bunched up on one line, we collect
11094     ;; them here.
11095     (unless gnus-view-pseudos-separately
11096       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11097             files action)
11098         (while ps
11099           (setq action (cdr (assq 'action (car ps))))
11100           (setq files (list (cdr (assq 'name (car ps)))))
11101           (while (and ps (cdr ps)
11102                       (string= (or action "1")
11103                                (or (cdr (assq 'action (cadr ps))) "2")))
11104             (push (cdr (assq 'name (cadr ps))) files)
11105             (setcdr ps (cddr ps)))
11106           (when files
11107             (when (not (string-match "%s" action))
11108               (push " " files))
11109             (push " " files)
11110             (when (assq 'execute (car ps))
11111               (setcdr (assq 'execute (car ps))
11112                       (funcall (if (string-match "%s" action)
11113                                    'format 'concat)
11114                                action
11115                                (mapconcat
11116                                 (lambda (f)
11117                                   (if (equal f " ")
11118                                       f
11119                                     (shell-quote-argument f)))
11120                                 files " ")))))
11121           (setq ps (cdr ps)))))
11122     (if (and gnus-view-pseudos (not not-view))
11123         (while pslist
11124           (when (assq 'execute (car pslist))
11125             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11126                                   (eq gnus-view-pseudos 'not-confirm)))
11127           (setq pslist (cdr pslist)))
11128       (save-excursion
11129         (while pslist
11130           (setq after-article (or (cdr (assq 'article (car pslist)))
11131                                   (gnus-summary-article-number)))
11132           (gnus-summary-goto-subject after-article)
11133           (forward-line 1)
11134           (setq b (point))
11135           (insert "    " (file-name-nondirectory
11136                           (cdr (assq 'name (car pslist))))
11137                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11138           (setq e (point))
11139           (forward-line -1)             ; back to `b'
11140           (gnus-add-text-properties
11141            b (1- e) (list 'gnus-number gnus-reffed-article-number
11142                           gnus-mouse-face-prop gnus-mouse-face))
11143           (gnus-data-enter
11144            after-article gnus-reffed-article-number
11145            gnus-unread-mark b (car pslist) 0 (- e b))
11146           (setq gnus-newsgroup-unreads
11147                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11148                                          gnus-reffed-article-number))
11149           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11150           (setq pslist (cdr pslist)))))))
11151
11152 (defun gnus-pseudos< (p1 p2)
11153   (let ((c1 (cdr (assq 'action p1)))
11154         (c2 (cdr (assq 'action p2))))
11155     (and c1 c2 (string< c1 c2))))
11156
11157 (defun gnus-request-pseudo-article (props)
11158   (cond ((assq 'execute props)
11159          (gnus-execute-command (cdr (assq 'execute props)))))
11160   (let ((gnus-current-article (gnus-summary-article-number)))
11161     (gnus-run-hooks 'gnus-mark-article-hook)))
11162
11163 (defun gnus-execute-command (command &optional automatic)
11164   (save-excursion
11165     (gnus-article-setup-buffer)
11166     (set-buffer gnus-article-buffer)
11167     (setq buffer-read-only nil)
11168     (let ((command (if automatic command
11169                      (read-string "Command: " (cons command 0)))))
11170       (erase-buffer)
11171       (insert "$ " command "\n\n")
11172       (if gnus-view-pseudo-asynchronously
11173           (start-process "gnus-execute" (current-buffer) shell-file-name
11174                          shell-command-switch command)
11175         (call-process shell-file-name nil t nil
11176                       shell-command-switch command)))))
11177
11178 ;; Summary kill commands.
11179
11180 (defun gnus-summary-edit-global-kill (article)
11181   "Edit the \"global\" kill file."
11182   (interactive (list (gnus-summary-article-number)))
11183   (gnus-group-edit-global-kill article))
11184
11185 (defun gnus-summary-edit-local-kill ()
11186   "Edit a local kill file applied to the current newsgroup."
11187   (interactive)
11188   (setq gnus-current-headers (gnus-summary-article-header))
11189   (gnus-group-edit-local-kill
11190    (gnus-summary-article-number) gnus-newsgroup-name))
11191
11192 ;;; Header reading.
11193
11194 (defun gnus-read-header (id &optional header)
11195   "Read the headers of article ID and enter them into the Gnus system."
11196   (let ((group gnus-newsgroup-name)
11197         (gnus-override-method
11198          (or
11199           gnus-override-method
11200           (and (gnus-news-group-p gnus-newsgroup-name)
11201                (car (gnus-refer-article-methods)))))
11202         where)
11203     ;; First we check to see whether the header in question is already
11204     ;; fetched.
11205     (if (stringp id)
11206         ;; This is a Message-ID.
11207         (setq header (or header (gnus-id-to-header id)))
11208       ;; This is an article number.
11209       (setq header (or header (gnus-summary-article-header id))))
11210     (if (and header
11211              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11212         ;; We have found the header.
11213         header
11214       ;; If this is a sparse article, we have to nix out its
11215       ;; previous entry in the thread hashtb.
11216       (when (and header
11217                  (gnus-summary-article-sparse-p (mail-header-number header)))
11218         (let* ((parent (gnus-parent-id (mail-header-references header)))
11219                (thread (and parent (gnus-id-to-thread parent))))
11220           (when thread
11221             (delq (assq header thread) thread))))
11222       ;; We have to really fetch the header to this article.
11223       (save-excursion
11224         (set-buffer nntp-server-buffer)
11225         (when (setq where (gnus-request-head id group))
11226           (nnheader-fold-continuation-lines)
11227           (goto-char (point-max))
11228           (insert ".\n")
11229           (goto-char (point-min))
11230           (insert "211 ")
11231           (princ (cond
11232                   ((numberp id) id)
11233                   ((cdr where) (cdr where))
11234                   (header (mail-header-number header))
11235                   (t gnus-reffed-article-number))
11236                  (current-buffer))
11237           (insert " Article retrieved.\n"))
11238         (if (or (not where)
11239                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11240             ()                          ; Malformed head.
11241           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11242             (when (and (stringp id)
11243                        (not (string= (gnus-group-real-name group)
11244                                      (car where))))
11245               ;; If we fetched by Message-ID and the article came
11246               ;; from a different group, we fudge some bogus article
11247               ;; numbers for this article.
11248               (mail-header-set-number header gnus-reffed-article-number))
11249             (save-excursion
11250               (set-buffer gnus-summary-buffer)
11251               (decf gnus-reffed-article-number)
11252               (gnus-remove-header (mail-header-number header))
11253               (push header gnus-newsgroup-headers)
11254               (setq gnus-current-headers header)
11255               (push (mail-header-number header) gnus-newsgroup-limit)))
11256           header)))))
11257
11258 (defun gnus-remove-header (number)
11259   "Remove header NUMBER from `gnus-newsgroup-headers'."
11260   (if (and gnus-newsgroup-headers
11261            (= number (mail-header-number (car gnus-newsgroup-headers))))
11262       (pop gnus-newsgroup-headers)
11263     (let ((headers gnus-newsgroup-headers))
11264       (while (and (cdr headers)
11265                   (not (= number (mail-header-number (cadr headers)))))
11266         (pop headers))
11267       (when (cdr headers)
11268         (setcdr headers (cddr headers))))))
11269
11270 ;;;
11271 ;;; summary highlights
11272 ;;;
11273
11274 (defun gnus-highlight-selected-summary ()
11275   "Highlight selected article in summary buffer."
11276   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11277   (when gnus-summary-selected-face
11278     (save-excursion
11279       (let* ((beg (point-at-bol))
11280              (end (point-at-eol))
11281              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11282              (from (if (get-text-property beg gnus-mouse-face-prop)
11283                        beg
11284                      (or (next-single-property-change
11285                           beg gnus-mouse-face-prop nil end)
11286                          beg)))
11287              (to
11288               (if (= from end)
11289                   (- from 2)
11290                 (or (next-single-property-change
11291                      from gnus-mouse-face-prop nil end)
11292                     end))))
11293         ;; If no mouse-face prop on line we will have to = from = end,
11294         ;; so we highlight the entire line instead.
11295         (when (= (+ to 2) from)
11296           (setq from beg)
11297           (setq to end))
11298         (if gnus-newsgroup-selected-overlay
11299             ;; Move old overlay.
11300             (gnus-move-overlay
11301              gnus-newsgroup-selected-overlay from to (current-buffer))
11302           ;; Create new overlay.
11303           (gnus-overlay-put
11304            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11305            'face gnus-summary-selected-face))))))
11306
11307 (defvar gnus-summary-highlight-line-cached nil)
11308 (defvar gnus-summary-highlight-line-trigger nil)
11309
11310 (defun gnus-summary-highlight-line-0 ()
11311   (if (and (eq gnus-summary-highlight-line-trigger
11312                gnus-summary-highlight)
11313            gnus-summary-highlight-line-cached)
11314       gnus-summary-highlight-line-cached
11315     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11316           gnus-summary-highlight-line-cached
11317           (let* ((cond (list 'cond))
11318                  (c cond)
11319                  (list gnus-summary-highlight))
11320             (while list
11321               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11322                               nil))
11323               (setq c (cdr c)
11324                     list (cdr list)))
11325             (gnus-byte-compile (list 'lambda nil cond))))))
11326
11327 (defun gnus-summary-highlight-line ()
11328   "Highlight current line according to `gnus-summary-highlight'."
11329   (let* ((beg (point-at-bol))
11330          (article (or (gnus-summary-article-number) gnus-current-article))
11331          (score (or (cdr (assq article
11332                                gnus-newsgroup-scored))
11333                     gnus-summary-default-score 0))
11334          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11335          (inhibit-read-only t)
11336          (default gnus-summary-default-score)
11337          (default-high gnus-summary-default-high-score)
11338          (default-low gnus-summary-default-low-score)
11339          (uncached (and gnus-summary-use-undownloaded-faces
11340                         (memq article gnus-newsgroup-undownloaded))))
11341     (let ((face (funcall (gnus-summary-highlight-line-0))))
11342       (unless (eq face (get-text-property beg 'face))
11343         (gnus-put-text-property-excluding-characters-with-faces
11344          beg (point-at-eol) 'face
11345          (setq face (if (boundp face) (symbol-value face) face)))
11346         (when gnus-summary-highlight-line-function
11347           (funcall gnus-summary-highlight-line-function article face))))))
11348
11349 (defun gnus-update-read-articles (group unread &optional compute)
11350   "Update the list of read articles in GROUP.
11351 UNREAD is a sorted list."
11352   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11353         (info (gnus-get-info group))
11354         (prev 1)
11355         read)
11356     (if (or (not info) (not active))
11357         ;; There is no info on this group if it was, in fact,
11358         ;; killed.  Gnus stores no information on killed groups, so
11359         ;; there's nothing to be done.
11360         ;; One could store the information somewhere temporarily,
11361         ;; perhaps...  Hmmm...
11362         ()
11363       ;; Remove any negative articles numbers.
11364       (while (and unread (< (car unread) 0))
11365         (setq unread (cdr unread)))
11366       ;; Remove any expired article numbers
11367       (while (and unread (< (car unread) (car active)))
11368         (setq unread (cdr unread)))
11369       ;; Compute the ranges of read articles by looking at the list of
11370       ;; unread articles.
11371       (while unread
11372         (when (/= (car unread) prev)
11373           (push (if (= prev (1- (car unread))) prev
11374                   (cons prev (1- (car unread))))
11375                 read))
11376         (setq prev (1+ (car unread)))
11377         (setq unread (cdr unread)))
11378       (when (<= prev (cdr active))
11379         (push (cons prev (cdr active)) read))
11380       (setq read (if (> (length read) 1) (nreverse read) read))
11381       (if compute
11382           read
11383         (save-excursion
11384           (let (setmarkundo)
11385             ;; Propagate the read marks to the backend.
11386             (when (gnus-check-backend-function 'request-set-mark group)
11387               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11388                     (add (gnus-remove-from-range read (gnus-info-read info))))
11389                 (when (or add del)
11390                   (unless (gnus-check-group group)
11391                     (error "Can't open server for %s" group))
11392                   (gnus-request-set-mark
11393                    group (delq nil (list (if add (list add 'add '(read)))
11394                                          (if del (list del 'del '(read))))))
11395                   (setq setmarkundo
11396                         `(gnus-request-set-mark
11397                           ,group
11398                           ',(delq nil (list
11399                                        (if del (list del 'add '(read)))
11400                                        (if add (list add 'del '(read))))))))))
11401             (set-buffer gnus-group-buffer)
11402             (gnus-undo-register
11403               `(progn
11404                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11405                  (gnus-info-set-read ',info ',(gnus-info-read info))
11406                  (gnus-get-unread-articles-in-group ',info
11407                                                     (gnus-active ,group))
11408                  (gnus-group-update-group ,group t)
11409                  ,setmarkundo))))
11410         ;; Enter this list into the group info.
11411         (gnus-info-set-read info read)
11412         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11413         (gnus-get-unread-articles-in-group info (gnus-active group))
11414         t))))
11415
11416 (defun gnus-offer-save-summaries ()
11417   "Offer to save all active summary buffers."
11418   (let (buffers)
11419     ;; Go through all buffers and find all summaries.
11420     (dolist (buffer (buffer-list))
11421       (when (and (setq buffer (buffer-name buffer))
11422                  (string-match "Summary" buffer)
11423                  (save-excursion
11424                    (set-buffer buffer)
11425                    ;; We check that this is, indeed, a summary buffer.
11426                    (and (eq major-mode 'gnus-summary-mode)
11427                         ;; Also make sure this isn't bogus.
11428                         gnus-newsgroup-prepared
11429                         ;; Also make sure that this isn't a
11430                         ;; dead summary buffer.
11431                         (not gnus-dead-summary-mode))))
11432         (push buffer buffers)))
11433     ;; Go through all these summary buffers and offer to save them.
11434     (when buffers
11435       (save-excursion
11436         (map-y-or-n-p
11437          "Update summary buffer %s? "
11438          (lambda (buf)
11439            (switch-to-buffer buf)
11440            (gnus-summary-exit))
11441          buffers)))))
11442
11443 (defun gnus-summary-setup-default-charset ()
11444   "Setup newsgroup default charset."
11445   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11446       (setq gnus-newsgroup-charset nil)
11447     (let* ((ignored-charsets
11448             (or gnus-newsgroup-ephemeral-ignored-charsets
11449                 (append
11450                  (and gnus-newsgroup-name
11451                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11452                  gnus-newsgroup-ignored-charsets))))
11453       (setq gnus-newsgroup-charset
11454             (or gnus-newsgroup-ephemeral-charset
11455                 (and gnus-newsgroup-name
11456                      (gnus-parameter-charset gnus-newsgroup-name))
11457                 gnus-default-charset))
11458       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11459            ignored-charsets))))
11460
11461 ;;;
11462 ;;; Mime Commands
11463 ;;;
11464
11465 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11466   "Display the current article buffer fully MIME-buttonized.
11467 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11468 treated as multipart/mixed."
11469   (interactive "P")
11470   (require 'gnus-art)
11471   (let ((gnus-unbuttonized-mime-types nil)
11472         (gnus-mime-display-multipart-as-mixed show-all-parts))
11473     (gnus-summary-show-article)))
11474
11475 (defun gnus-summary-repair-multipart (article)
11476   "Add a Content-Type header to a multipart article without one."
11477   (interactive (list (gnus-summary-article-number)))
11478   (gnus-with-article article
11479     (message-narrow-to-head)
11480     (message-remove-header "Mime-Version")
11481     (goto-char (point-max))
11482     (insert "Mime-Version: 1.0\n")
11483     (widen)
11484     (when (search-forward "\n--" nil t)
11485       (let ((separator (buffer-substring (point) (point-at-eol))))
11486         (message-narrow-to-head)
11487         (message-remove-header "Content-Type")
11488         (goto-char (point-max))
11489         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11490                         separator))
11491         (widen))))
11492   (let (gnus-mark-article-hook)
11493     (gnus-summary-select-article t t nil article)))
11494
11495 (defun gnus-summary-toggle-display-buttonized ()
11496   "Toggle the buttonizing of the article buffer."
11497   (interactive)
11498   (require 'gnus-art)
11499   (if (setq gnus-inhibit-mime-unbuttonizing
11500             (not gnus-inhibit-mime-unbuttonizing))
11501       (let ((gnus-unbuttonized-mime-types nil))
11502         (gnus-summary-show-article))
11503     (gnus-summary-show-article)))
11504
11505 ;;;
11506 ;;; Generic summary marking commands
11507 ;;;
11508
11509 (defvar gnus-summary-marking-alist
11510   '((read gnus-del-mark "d")
11511     (unread gnus-unread-mark "u")
11512     (ticked gnus-ticked-mark "!")
11513     (dormant gnus-dormant-mark "?")
11514     (expirable gnus-expirable-mark "e"))
11515   "An alist of names/marks/keystrokes.")
11516
11517 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11518 (defvar gnus-summary-mark-map)
11519
11520 (defun gnus-summary-make-all-marking-commands ()
11521   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11522   (dolist (elem gnus-summary-marking-alist)
11523     (apply 'gnus-summary-make-marking-command elem)))
11524
11525 (defun gnus-summary-make-marking-command (name mark keystroke)
11526   (let ((map (make-sparse-keymap)))
11527     (define-key gnus-summary-generic-mark-map keystroke map)
11528     (dolist (lway `((next "next" next nil "n")
11529                     (next-unread "next unread" next t "N")
11530                     (prev "previous" prev nil "p")
11531                     (prev-unread "previous unread" prev t "P")
11532                     (nomove "" nil nil ,keystroke)))
11533       (let ((func (gnus-summary-make-marking-command-1
11534                    mark (car lway) lway name)))
11535         (setq func (eval func))
11536         (define-key map (nth 4 lway) func)))))
11537
11538 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11539   `(defun ,(intern
11540             (format "gnus-summary-put-mark-as-%s%s"
11541                     name (if (eq way 'nomove)
11542                              ""
11543                            (concat "-" (symbol-name way)))))
11544      (n)
11545      ,(format
11546        "Mark the current article as %s%s.
11547 If N, the prefix, then repeat N times.
11548 If N is negative, move in reverse order.
11549 The difference between N and the actual number of articles marked is
11550 returned."
11551        name (cadr lway))
11552      (interactive "p")
11553      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11554
11555 (defun gnus-summary-generic-mark (n mark move unread)
11556   "Mark N articles with MARK."
11557   (unless (eq major-mode 'gnus-summary-mode)
11558     (error "This command can only be used in the summary buffer"))
11559   (gnus-summary-show-thread)
11560   (let ((nummove
11561          (cond
11562           ((eq move 'next) 1)
11563           ((eq move 'prev) -1)
11564           (t 0))))
11565     (if (zerop nummove)
11566         (setq n 1)
11567       (when (< n 0)
11568         (setq n (abs n)
11569               nummove (* -1 nummove))))
11570     (while (and (> n 0)
11571                 (gnus-summary-mark-article nil mark)
11572                 (zerop (gnus-summary-next-subject nummove unread t)))
11573       (setq n (1- n)))
11574     (when (/= 0 n)
11575       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11576     (gnus-summary-recenter)
11577     (gnus-summary-position-point)
11578     (gnus-set-mode-line 'summary)
11579     n))
11580
11581 (defun gnus-summary-insert-articles (articles)
11582   (when (setq articles
11583               (gnus-sorted-difference articles
11584                                       (mapcar (lambda (h)
11585                                                 (mail-header-number h))
11586                                               gnus-newsgroup-headers)))
11587     (setq gnus-newsgroup-headers
11588           (gnus-merge 'list
11589                       gnus-newsgroup-headers
11590                       (gnus-fetch-headers articles)
11591                       'gnus-article-sort-by-number))
11592     ;; Suppress duplicates?
11593     (when gnus-suppress-duplicates
11594       (gnus-dup-suppress-articles))
11595
11596     ;; We might want to build some more threads first.
11597     (when (and gnus-fetch-old-headers
11598                (eq gnus-headers-retrieved-by 'nov))
11599       (if (eq gnus-fetch-old-headers 'invisible)
11600           (gnus-build-all-threads)
11601         (gnus-build-old-threads)))
11602     ;; Let the Gnus agent mark articles as read.
11603     (when gnus-agent
11604       (gnus-agent-get-undownloaded-list))
11605     ;; Remove list identifiers from subject
11606     (when gnus-list-identifiers
11607       (gnus-summary-remove-list-identifiers))
11608     ;; First and last article in this newsgroup.
11609     (when gnus-newsgroup-headers
11610       (setq gnus-newsgroup-begin
11611             (mail-header-number (car gnus-newsgroup-headers))
11612             gnus-newsgroup-end
11613             (mail-header-number
11614              (gnus-last-element gnus-newsgroup-headers))))
11615     (when gnus-use-scoring
11616       (gnus-possibly-score-headers))))
11617
11618 (defun gnus-summary-insert-old-articles (&optional all)
11619   "Insert all old articles in this group.
11620 If ALL is non-nil, already read articles become readable.
11621 If ALL is a number, fetch this number of articles."
11622   (interactive "P")
11623   (prog1
11624       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11625             older len)
11626         (setq older
11627               ;; Some nntp servers lie about their active range.  When
11628               ;; this happens, the active range can be in the millions.
11629               ;; Use a compressed range to avoid creating a huge list.
11630               (gnus-range-difference (list gnus-newsgroup-active) old))
11631         (setq len (gnus-range-length older))
11632         (cond
11633          ((null older) nil)
11634          ((numberp all)
11635           (if (< all len)
11636               (let ((older-range (nreverse older)))
11637                 (setq older nil)
11638
11639                 (while (> all 0)
11640                   (let* ((r (pop older-range))
11641                          (min (if (numberp r) r (car r)))
11642                          (max (if (numberp r) r (cdr r))))
11643                     (while (and (<= min max)
11644                                 (> all 0))
11645                       (push max older)
11646                       (setq all (1- all)
11647                             max (1- max))))))
11648             (setq older (gnus-uncompress-range older))))
11649          (all
11650           (setq older (gnus-uncompress-range older)))
11651          (t
11652           (when (and (numberp gnus-large-newsgroup)
11653                    (> len gnus-large-newsgroup))
11654               (let* ((cursor-in-echo-area nil)
11655                      (initial (gnus-parameter-large-newsgroup-initial
11656                                gnus-newsgroup-name))
11657                      (input
11658                       (read-string
11659                        (format
11660                         "How many articles from %s (%s %d): "
11661                         (gnus-limit-string
11662                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11663                         (if initial "max" "default")
11664                         len)
11665                        (if initial
11666                            (cons (number-to-string initial)
11667                                  0)))))
11668                 (unless (string-match "^[ \t]*$" input)
11669                   (setq all (string-to-number input))
11670                   (if (< all len)
11671                       (let ((older-range (nreverse older)))
11672                         (setq older nil)
11673
11674                         (while (> all 0)
11675                           (let* ((r (pop older-range))
11676                                  (min (if (numberp r) r (car r)))
11677                                  (max (if (numberp r) r (cdr r))))
11678                             (while (and (<= min max)
11679                                         (> all 0))
11680                               (push max older)
11681                               (setq all (1- all)
11682                                     max (1- max))))))))))
11683           (setq older (gnus-uncompress-range older))))
11684         (if (not older)
11685             (message "No old news.")
11686           (gnus-summary-insert-articles older)
11687           (gnus-summary-limit (gnus-sorted-nunion old older))))
11688     (gnus-summary-position-point)))
11689
11690 (defun gnus-summary-insert-new-articles ()
11691   "Insert all new articles in this group."
11692   (interactive)
11693   (prog1
11694       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11695             (old-active gnus-newsgroup-active)
11696             (nnmail-fetched-sources (list t))
11697             i new)
11698         (setq gnus-newsgroup-active
11699               (gnus-activate-group gnus-newsgroup-name 'scan))
11700         (setq i (cdr gnus-newsgroup-active))
11701         (while (> i (cdr old-active))
11702           (push i new)
11703           (decf i))
11704         (if (not new)
11705             (message "No gnus is bad news")
11706           (gnus-summary-insert-articles new)
11707           (setq gnus-newsgroup-unreads
11708                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11709           (gnus-summary-limit (gnus-sorted-nunion old new))))
11710     (gnus-summary-position-point)))
11711
11712 (gnus-summary-make-all-marking-commands)
11713
11714 (gnus-ems-redefine)
11715
11716 (provide 'gnus-sum)
11717
11718 (run-hooks 'gnus-sum-load-hook)
11719
11720 ;; Local Variables:
11721 ;; coding: iso-8859-1
11722 ;; End:
11723
11724 ;;; gnus-sum.el ends here