(gnus-pick-line-number): Add autoload.
[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       ;; The agent may be storing articles that are no longer in the
5036       ;; server's active range.  If that is the case, the active range
5037       ;; needs to be expanded such that the agent's articles can be
5038       ;; included in the summary.
5039       (let* ((gnus-command-method (gnus-find-method-for-group group))
5040              (alist (gnus-agent-load-alist group))
5041              (active (gnus-active group)))
5042         (if (and (car alist)
5043                  (< (caar alist) (car active)))
5044             (gnus-set-active group (cons (caar alist) (cdr active)))))
5045
5046       (setq gnus-summary-use-undownloaded-faces
5047             (gnus-agent-find-parameter
5048              group
5049              'agent-enable-undownloaded-faces)))
5050
5051     (setq gnus-newsgroup-name group
5052           gnus-newsgroup-unselected nil
5053           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5054
5055     (let ((display (gnus-group-find-parameter group 'display)))
5056       (setq gnus-newsgroup-display
5057             (cond
5058              ((not (zerop (or (car-safe read-all) 0)))
5059               ;; The user entered the group with C-u SPC/RET, let's show
5060               ;; all articles.
5061               'gnus-not-ignore)
5062              ((eq display 'all)
5063               'gnus-not-ignore)
5064              ((arrayp display)
5065               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5066              ((numberp display)
5067               ;; The following is probably the "correct" solution, but
5068               ;; it makes Gnus fetch all headers and then limit the
5069               ;; articles (which is slow), so instead we hack the
5070               ;; select-articles parameter instead. -- Simon Josefsson
5071               ;; <jas@kth.se>
5072               ;;
5073               ;; (gnus-byte-compile
5074               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5075               ;;                         display)))))
5076               (setq select-articles
5077                     (gnus-uncompress-range
5078                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5079                              (if (> tmp 0)
5080                                  tmp
5081                                1))
5082                            (cdr (gnus-active group)))))
5083               nil)
5084              (t
5085               nil))))
5086
5087     (gnus-summary-setup-default-charset)
5088
5089     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5090     (when (gnus-virtual-group-p group)
5091       (setq cached gnus-newsgroup-cached))
5092
5093     (setq gnus-newsgroup-unreads
5094           (gnus-sorted-ndifference
5095            (gnus-sorted-ndifference gnus-newsgroup-unreads
5096                                     gnus-newsgroup-marked)
5097            gnus-newsgroup-dormant))
5098
5099     (setq gnus-newsgroup-processable nil)
5100
5101     (gnus-update-read-articles group gnus-newsgroup-unreads)
5102
5103     ;; Adjust and set lists of article marks.
5104     (when info
5105       (gnus-adjust-marked-articles info))
5106     (if (setq articles select-articles)
5107         (setq gnus-newsgroup-unselected
5108               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5109       (setq articles (gnus-articles-to-read group read-all)))
5110
5111     (cond
5112      ((null articles)
5113       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5114       'quit)
5115      ((eq articles 0) nil)
5116      (t
5117       ;; Init the dependencies hash table.
5118       (setq gnus-newsgroup-dependencies
5119             (gnus-make-hashtable (length articles)))
5120       (gnus-set-global-variables)
5121       ;; Retrieve the headers and read them in.
5122
5123       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5124
5125       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5126       (when cached
5127         (setq gnus-newsgroup-cached cached))
5128
5129       ;; Suppress duplicates?
5130       (when gnus-suppress-duplicates
5131         (gnus-dup-suppress-articles))
5132
5133       ;; Set the initial limit.
5134       (setq gnus-newsgroup-limit (copy-sequence articles))
5135       ;; Remove canceled articles from the list of unread articles.
5136       (setq fetched-articles
5137             (mapcar (lambda (headers) (mail-header-number headers))
5138                     gnus-newsgroup-headers))
5139       (setq gnus-newsgroup-articles fetched-articles)
5140       (setq gnus-newsgroup-unreads
5141             (gnus-sorted-nintersection
5142              gnus-newsgroup-unreads fetched-articles))
5143       (gnus-compute-unseen-list)
5144
5145       ;; Removed marked articles that do not exist.
5146       (gnus-update-missing-marks
5147        (gnus-sorted-difference articles fetched-articles))
5148       ;; We might want to build some more threads first.
5149       (when (and gnus-fetch-old-headers
5150                  (eq gnus-headers-retrieved-by 'nov))
5151         (if (eq gnus-fetch-old-headers 'invisible)
5152             (gnus-build-all-threads)
5153           (gnus-build-old-threads)))
5154       ;; Let the Gnus agent mark articles as read.
5155       (when gnus-agent
5156         (gnus-agent-get-undownloaded-list))
5157       ;; Remove list identifiers from subject
5158       (when gnus-list-identifiers
5159         (gnus-summary-remove-list-identifiers))
5160       ;; Check whether auto-expire is to be done in this group.
5161       (setq gnus-newsgroup-auto-expire
5162             (gnus-group-auto-expirable-p group))
5163       ;; Set up the article buffer now, if necessary.
5164       (unless gnus-single-article-buffer
5165         (gnus-article-setup-buffer))
5166       ;; First and last article in this newsgroup.
5167       (when gnus-newsgroup-headers
5168         (setq gnus-newsgroup-begin
5169               (mail-header-number (car gnus-newsgroup-headers))
5170               gnus-newsgroup-end
5171               (mail-header-number
5172                (gnus-last-element gnus-newsgroup-headers))))
5173       ;; GROUP is successfully selected.
5174       (or gnus-newsgroup-headers t)))))
5175
5176 (defun gnus-compute-unseen-list ()
5177   ;; The `seen' marks are treated specially.
5178   (if (not gnus-newsgroup-seen)
5179       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5180     (setq gnus-newsgroup-unseen
5181           (gnus-inverse-list-range-intersection
5182            gnus-newsgroup-articles gnus-newsgroup-seen))))
5183
5184 (defun gnus-summary-display-make-predicate (display)
5185   (require 'gnus-agent)
5186   (when (= (length display) 1)
5187     (setq display (car display)))
5188   (unless gnus-summary-display-cache
5189     (dolist (elem (append '((unread . unread)
5190                             (read . read)
5191                             (unseen . unseen))
5192                           gnus-article-mark-lists))
5193       (push (cons (cdr elem)
5194                   (gnus-byte-compile
5195                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5196             gnus-summary-display-cache)))
5197   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5198         (gnus-category-predicate-cache gnus-summary-display-cache))
5199     (gnus-get-predicate display)))
5200
5201 ;; Uses the dynamically bound `number' variable.
5202 (eval-when-compile
5203   (defvar number))
5204 (defun gnus-article-marked-p (type &optional article)
5205   (let ((article (or article number)))
5206     (cond
5207      ((eq type 'tick)
5208       (memq article gnus-newsgroup-marked))
5209      ((eq type 'spam)
5210       (memq article gnus-newsgroup-spam-marked))
5211      ((eq type 'unsend)
5212       (memq article gnus-newsgroup-unsendable))
5213      ((eq type 'undownload)
5214       (memq article gnus-newsgroup-undownloaded))
5215      ((eq type 'download)
5216       (memq article gnus-newsgroup-downloadable))
5217      ((eq type 'unread)
5218       (memq article gnus-newsgroup-unreads))
5219      ((eq type 'read)
5220       (memq article gnus-newsgroup-reads))
5221      ((eq type 'dormant)
5222       (memq article gnus-newsgroup-dormant) )
5223      ((eq type 'expire)
5224       (memq article gnus-newsgroup-expirable))
5225      ((eq type 'reply)
5226       (memq article gnus-newsgroup-replied))
5227      ((eq type 'killed)
5228       (memq article gnus-newsgroup-killed))
5229      ((eq type 'bookmark)
5230       (assq article gnus-newsgroup-bookmarks))
5231      ((eq type 'score)
5232       (assq article gnus-newsgroup-scored))
5233      ((eq type 'save)
5234       (memq article gnus-newsgroup-saved))
5235      ((eq type 'cache)
5236       (memq article gnus-newsgroup-cached))
5237      ((eq type 'forward)
5238       (memq article gnus-newsgroup-forwarded))
5239      ((eq type 'seen)
5240       (not (memq article gnus-newsgroup-unseen)))
5241      ((eq type 'recent)
5242       (memq article gnus-newsgroup-recent))
5243      (t t))))
5244
5245 (defun gnus-articles-to-read (group &optional read-all)
5246   "Find out what articles the user wants to read."
5247   (let* ((display (gnus-group-find-parameter group 'display))
5248          (articles
5249           ;; Select all articles if `read-all' is non-nil, or if there
5250           ;; are no unread articles.
5251           (if (or read-all
5252                   (and (zerop (length gnus-newsgroup-marked))
5253                        (zerop (length gnus-newsgroup-unreads)))
5254                   ;; Fetch all if the predicate is non-nil.
5255                   gnus-newsgroup-display)
5256               ;; We want to select the headers for all the articles in
5257               ;; the group, so we select either all the active
5258               ;; articles in the group, or (if that's nil), the
5259               ;; articles in the cache.
5260               (or
5261                (gnus-uncompress-range (gnus-active group))
5262                (gnus-cache-articles-in-group group))
5263             ;; Select only the "normal" subset of articles.
5264             (gnus-sorted-nunion
5265              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5266              gnus-newsgroup-unreads)))
5267          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5268          (scored (length scored-list))
5269          (number (length articles))
5270          (marked (+ (length gnus-newsgroup-marked)
5271                     (length gnus-newsgroup-dormant)))
5272          (select
5273           (cond
5274            ((numberp read-all)
5275             read-all)
5276            ((numberp gnus-newsgroup-display)
5277             gnus-newsgroup-display)
5278            (t
5279             (condition-case ()
5280                 (cond
5281                  ((and (or (<= scored marked) (= scored number))
5282                        (numberp gnus-large-newsgroup)
5283                        (> number gnus-large-newsgroup))
5284                   (let* ((cursor-in-echo-area nil)
5285                          (initial (gnus-parameter-large-newsgroup-initial
5286                                    gnus-newsgroup-name))
5287                          (input
5288                           (read-string
5289                            (format
5290                             "How many articles from %s (%s %d): "
5291                             (gnus-limit-string
5292                              (gnus-group-decoded-name gnus-newsgroup-name)
5293                              35)
5294                             (if initial "max" "default")
5295                             number)
5296                            (if initial
5297                                (cons (number-to-string initial)
5298                                      0)))))
5299                     (if (string-match "^[ \t]*$" input) number input)))
5300                  ((and (> scored marked) (< scored number)
5301                        (> (- scored number) 20))
5302                   (let ((input
5303                          (read-string
5304                           (format "%s %s (%d scored, %d total): "
5305                                   "How many articles from"
5306                                   (gnus-group-decoded-name group)
5307                                   scored number))))
5308                     (if (string-match "^[ \t]*$" input)
5309                         number input)))
5310                  (t number))
5311               (quit
5312                (message "Quit getting the articles to read")
5313                nil))))))
5314     (setq select (if (stringp select) (string-to-number select) select))
5315     (if (or (null select) (zerop select))
5316         select
5317       (if (and (not (zerop scored)) (<= (abs select) scored))
5318           (progn
5319             (setq articles (sort scored-list '<))
5320             (setq number (length articles)))
5321         (setq articles (copy-sequence articles)))
5322
5323       (when (< (abs select) number)
5324         (if (< select 0)
5325             ;; Select the N oldest articles.
5326             (setcdr (nthcdr (1- (abs select)) articles) nil)
5327           ;; Select the N most recent articles.
5328           (setq articles (nthcdr (- number select) articles))))
5329       (setq gnus-newsgroup-unselected
5330             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5331       (when gnus-alter-articles-to-read-function
5332         (setq articles
5333               (sort
5334                (funcall gnus-alter-articles-to-read-function
5335                         gnus-newsgroup-name articles)
5336                '<)))
5337       articles)))
5338
5339 (defun gnus-killed-articles (killed articles)
5340   (let (out)
5341     (while articles
5342       (when (inline (gnus-member-of-range (car articles) killed))
5343         (push (car articles) out))
5344       (setq articles (cdr articles)))
5345     out))
5346
5347 (defun gnus-uncompress-marks (marks)
5348   "Uncompress the mark ranges in MARKS."
5349   (let ((uncompressed '(score bookmark))
5350         out)
5351     (while marks
5352       (if (memq (caar marks) uncompressed)
5353           (push (car marks) out)
5354         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5355       (setq marks (cdr marks)))
5356     out))
5357
5358 (defun gnus-article-mark-to-type (mark)
5359   "Return the type of MARK."
5360   (or (cadr (assq mark gnus-article-special-mark-lists))
5361       'list))
5362
5363 (defun gnus-article-unpropagatable-p (mark)
5364   "Return whether MARK should be propagated to back end."
5365   (memq mark gnus-article-unpropagated-mark-lists))
5366
5367 (defun gnus-adjust-marked-articles (info)
5368   "Set all article lists and remove all marks that are no longer valid."
5369   (let* ((marked-lists (gnus-info-marks info))
5370          (active (gnus-active (gnus-info-group info)))
5371          (min (car active))
5372          (max (cdr active))
5373          (types gnus-article-mark-lists)
5374          marks var articles article mark mark-type)
5375
5376     (dolist (marks marked-lists)
5377       (setq mark (car marks)
5378             mark-type (gnus-article-mark-to-type mark)
5379             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5380
5381       ;; We set the variable according to the type of the marks list,
5382       ;; and then adjust the marks to a subset of the active articles.
5383       (cond
5384        ;; Adjust "simple" lists.
5385        ((eq mark-type 'list)
5386         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5387         (when (memq mark '(tick dormant expire reply save))
5388           (while articles
5389             (when (or (< (setq article (pop articles)) min) (> article max))
5390               (set var (delq article (symbol-value var)))))))
5391        ;; Adjust assocs.
5392        ((eq mark-type 'tuple)
5393         (set var (setq articles (cdr marks)))
5394         (when (not (listp (cdr (symbol-value var))))
5395           (set var (list (symbol-value var))))
5396         (when (not (listp (cdr articles)))
5397           (setq articles (list articles)))
5398         (while articles
5399           (when (or (not (consp (setq article (pop articles))))
5400                     (< (car article) min)
5401                     (> (car article) max))
5402             (set var (delq article (symbol-value var))))))
5403        ;; Adjust ranges (sloppily).
5404        ((eq mark-type 'range)
5405         (cond
5406          ((eq mark 'seen)
5407           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5408           ;; It should be (seen (NUM1 . NUM2)).
5409           (when (numberp (cddr marks))
5410             (setcdr marks (list (cdr marks))))
5411           (setq articles (cdr marks))
5412           (while (and articles
5413                       (or (and (consp (car articles))
5414                                (> min (cdar articles)))
5415                           (and (numberp (car articles))
5416                                (> min (car articles)))))
5417             (pop articles))
5418           (set var articles))))))))
5419
5420 (defun gnus-update-missing-marks (missing)
5421   "Go through the list of MISSING articles and remove them from the mark lists."
5422   (when missing
5423     (let (var m)
5424       ;; Go through all types.
5425       (dolist (elem gnus-article-mark-lists)
5426         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5427           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5428           (when (symbol-value var)
5429             ;; This list has articles.  So we delete all missing
5430             ;; articles from it.
5431             (setq m missing)
5432             (while m
5433               (set var (delq (pop m) (symbol-value var))))))))))
5434
5435 (defun gnus-update-marks ()
5436   "Enter the various lists of marked articles into the newsgroup info list."
5437   (let ((types gnus-article-mark-lists)
5438         (info (gnus-get-info gnus-newsgroup-name))
5439         type list newmarked symbol delta-marks)
5440     (when info
5441       ;; Add all marks lists to the list of marks lists.
5442       (while (setq type (pop types))
5443         (setq list (symbol-value
5444                     (setq symbol
5445                           (intern (format "gnus-newsgroup-%s" (car type))))))
5446
5447         (when list
5448           ;; Get rid of the entries of the articles that have the
5449           ;; default score.
5450           (when (and (eq (cdr type) 'score)
5451                      gnus-save-score
5452                      list)
5453             (let* ((arts list)
5454                    (prev (cons nil list))
5455                    (all prev))
5456               (while arts
5457                 (if (or (not (consp (car arts)))
5458                         (= (cdar arts) gnus-summary-default-score))
5459                     (setcdr prev (cdr arts))
5460                   (setq prev arts))
5461                 (setq arts (cdr arts)))
5462               (setq list (cdr all)))))
5463
5464         (when (eq (cdr type) 'seen)
5465           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5466
5467         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5468           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5469
5470         (when (and (gnus-check-backend-function
5471                     'request-set-mark gnus-newsgroup-name)
5472                    (not (gnus-article-unpropagatable-p (cdr type))))
5473           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5474                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5475                  (add (gnus-remove-from-range
5476                        (gnus-copy-sequence list) old)))
5477             (when add
5478               (push (list add 'add (list (cdr type))) delta-marks))
5479             (when del
5480               (push (list del 'del (list (cdr type))) delta-marks))))
5481
5482         (when list
5483           (push (cons (cdr type) list) newmarked)))
5484
5485       (when delta-marks
5486         (unless (gnus-check-group gnus-newsgroup-name)
5487           (error "Can't open server for %s" gnus-newsgroup-name))
5488         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5489
5490       ;; Enter these new marks into the info of the group.
5491       (if (nthcdr 3 info)
5492           (setcar (nthcdr 3 info) newmarked)
5493         ;; Add the marks lists to the end of the info.
5494         (when newmarked
5495           (setcdr (nthcdr 2 info) (list newmarked))))
5496
5497       ;; Cut off the end of the info if there's nothing else there.
5498       (let ((i 5))
5499         (while (and (> i 2)
5500                     (not (nth i info)))
5501           (when (nthcdr (decf i) info)
5502             (setcdr (nthcdr i info) nil)))))))
5503
5504 (defun gnus-set-mode-line (where)
5505   "Set the mode line of the article or summary buffers.
5506 If WHERE is `summary', the summary mode line format will be used."
5507   ;; Is this mode line one we keep updated?
5508   (when (and (memq where gnus-updated-mode-lines)
5509              (symbol-value
5510               (intern (format "gnus-%s-mode-line-format-spec" where))))
5511     (let (mode-string)
5512       (save-excursion
5513         ;; We evaluate this in the summary buffer since these
5514         ;; variables are buffer-local to that buffer.
5515         (set-buffer gnus-summary-buffer)
5516        ;; We bind all these variables that are used in the `eval' form
5517         ;; below.
5518         (let* ((mformat (symbol-value
5519                          (intern
5520                           (format "gnus-%s-mode-line-format-spec" where))))
5521                (gnus-tmp-group-name (gnus-group-decoded-name
5522                                      gnus-newsgroup-name))
5523                (gnus-tmp-article-number (or gnus-current-article 0))
5524                (gnus-tmp-unread gnus-newsgroup-unreads)
5525                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5526                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5527                (gnus-tmp-unread-and-unselected
5528                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5529                             (zerop gnus-tmp-unselected))
5530                        "")
5531                       ((zerop gnus-tmp-unselected)
5532                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5533                       (t (format "{%d(+%d) more}"
5534                                  gnus-tmp-unread-and-unticked
5535                                  gnus-tmp-unselected))))
5536                (gnus-tmp-subject
5537                 (if (and gnus-current-headers
5538                          (vectorp gnus-current-headers))
5539                     (gnus-mode-string-quote
5540                      (mail-header-subject gnus-current-headers))
5541                   ""))
5542                bufname-length max-len
5543                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5544           (setq mode-string (eval mformat))
5545           (setq bufname-length (if (string-match "%b" mode-string)
5546                                    (- (length
5547                                        (buffer-name
5548                                         (if (eq where 'summary)
5549                                             nil
5550                                           (get-buffer gnus-article-buffer))))
5551                                       2)
5552                                  0))
5553           (setq max-len (max 4 (if gnus-mode-non-string-length
5554                                    (- (window-width)
5555                                       gnus-mode-non-string-length
5556                                       bufname-length)
5557                                  (length mode-string))))
5558           ;; We might have to chop a bit of the string off...
5559           (when (> (length mode-string) max-len)
5560             (setq mode-string
5561                   (concat (truncate-string-to-width mode-string (- max-len 3))
5562                           "...")))
5563           ;; Pad the mode string a bit.
5564           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5565       ;; Update the mode line.
5566       (setq mode-line-buffer-identification
5567             (gnus-mode-line-buffer-identification (list mode-string)))
5568       (set-buffer-modified-p t))))
5569
5570 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5571   "Go through the HEADERS list and add all Xrefs to a hash table.
5572 The resulting hash table is returned, or nil if no Xrefs were found."
5573   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5574          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5575          (xref-hashtb (gnus-make-hashtable))
5576          start group entry number xrefs header)
5577     (while headers
5578       (setq header (pop headers))
5579       (when (and (setq xrefs (mail-header-xref header))
5580                  (not (memq (setq number (mail-header-number header))
5581                             unreads)))
5582         (setq start 0)
5583         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5584           (setq start (match-end 0))
5585           (setq group (if prefix
5586                           (concat prefix (substring xrefs (match-beginning 1)
5587                                                     (match-end 1)))
5588                         (substring xrefs (match-beginning 1) (match-end 1))))
5589           (setq number
5590                 (string-to-int (substring xrefs (match-beginning 2)
5591                                           (match-end 2))))
5592           (if (setq entry (gnus-gethash group xref-hashtb))
5593               (setcdr entry (cons number (cdr entry)))
5594             (gnus-sethash group (cons number nil) xref-hashtb)))))
5595     (and start xref-hashtb)))
5596
5597 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5598   "Look through all the headers and mark the Xrefs as read."
5599   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5600         name info xref-hashtb idlist method nth4)
5601     (save-excursion
5602       (set-buffer gnus-group-buffer)
5603       (when (setq xref-hashtb
5604                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5605         (mapatoms
5606          (lambda (group)
5607            (unless (string= from-newsgroup (setq name (symbol-name group)))
5608              (setq idlist (symbol-value group))
5609              ;; Dead groups are not updated.
5610              (and (prog1
5611                       (setq info (gnus-get-info name))
5612                     (when (stringp (setq nth4 (gnus-info-method info)))
5613                       (setq nth4 (gnus-server-to-method nth4))))
5614                   ;; Only do the xrefs if the group has the same
5615                   ;; select method as the group we have just read.
5616                   (or (gnus-methods-equal-p
5617                        nth4 (gnus-find-method-for-group from-newsgroup))
5618                       virtual
5619                       (equal nth4 (setq method (gnus-find-method-for-group
5620                                                 from-newsgroup)))
5621                       (and (equal (car nth4) (car method))
5622                            (equal (nth 1 nth4) (nth 1 method))))
5623                   gnus-use-cross-reference
5624                   (or (not (eq gnus-use-cross-reference t))
5625                       virtual
5626                       ;; Only do cross-references on subscribed
5627                       ;; groups, if that is what is wanted.
5628                       (<= (gnus-info-level info) gnus-level-subscribed))
5629                   (gnus-group-make-articles-read name idlist))))
5630          xref-hashtb)))))
5631
5632 (defun gnus-compute-read-articles (group articles)
5633   (let* ((entry (gnus-group-entry group))
5634          (info (nth 2 entry))
5635          (active (gnus-active group))
5636          ninfo)
5637     (when entry
5638       ;; First peel off all invalid article numbers.
5639       (when active
5640         (let ((ids articles)
5641               id first)
5642           (while (setq id (pop ids))
5643             (when (and first (> id (cdr active)))
5644               ;; We'll end up in this situation in one particular
5645               ;; obscure situation.  If you re-scan a group and get
5646               ;; a new article that is cross-posted to a different
5647               ;; group that has not been re-scanned, you might get
5648               ;; crossposted article that has a higher number than
5649               ;; Gnus believes possible.  So we re-activate this
5650               ;; group as well.  This might mean doing the
5651               ;; crossposting thingy will *increase* the number
5652               ;; of articles in some groups.  Tsk, tsk.
5653               (setq active (or (gnus-activate-group group) active)))
5654             (when (or (> id (cdr active))
5655                       (< id (car active)))
5656               (setq articles (delq id articles))))))
5657       ;; If the read list is nil, we init it.
5658       (if (and active
5659                (null (gnus-info-read info))
5660                (> (car active) 1))
5661           (setq ninfo (cons 1 (1- (car active))))
5662         (setq ninfo (gnus-info-read info)))
5663       ;; Then we add the read articles to the range.
5664       (gnus-add-to-range
5665        ninfo (setq articles (sort articles '<))))))
5666
5667 (defun gnus-group-make-articles-read (group articles)
5668   "Update the info of GROUP to say that ARTICLES are read."
5669   (let* ((num 0)
5670          (entry (gnus-group-entry group))
5671          (info (nth 2 entry))
5672          (active (gnus-active group))
5673          range)
5674     (when entry
5675       (setq range (gnus-compute-read-articles group articles))
5676       (with-current-buffer gnus-group-buffer
5677         (gnus-undo-register
5678           `(progn
5679              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5680              (gnus-info-set-read ',info ',(gnus-info-read info))
5681              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5682              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5683              (gnus-group-update-group ,group t))))
5684       ;; Add the read articles to the range.
5685       (gnus-info-set-read info range)
5686       (gnus-request-set-mark group (list (list range 'add '(read))))
5687       ;; Then we have to re-compute how many unread
5688       ;; articles there are in this group.
5689       (when active
5690         (cond
5691          ((not range)
5692           (setq num (- (1+ (cdr active)) (car active))))
5693          ((not (listp (cdr range)))
5694           (setq num (- (cdr active) (- (1+ (cdr range))
5695                                        (car range)))))
5696          (t
5697           (while range
5698             (if (numberp (car range))
5699                 (setq num (1+ num))
5700               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5701             (setq range (cdr range)))
5702           (setq num (- (cdr active) num))))
5703         ;; Update the number of unread articles.
5704         (setcar entry num)
5705         ;; Update the group buffer.
5706         (unless (gnus-ephemeral-group-p group)
5707           (gnus-group-update-group group t))))))
5708
5709 (defvar gnus-newsgroup-none-id 0)
5710
5711 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5712   (let ((cur nntp-server-buffer)
5713         (dependencies
5714          (or dependencies
5715              (save-excursion (set-buffer gnus-summary-buffer)
5716                              gnus-newsgroup-dependencies)))
5717         headers id end ref
5718         (mail-parse-charset gnus-newsgroup-charset)
5719         (mail-parse-ignored-charsets
5720          (save-excursion (condition-case nil
5721                              (set-buffer gnus-summary-buffer)
5722                            (error))
5723                          gnus-newsgroup-ignored-charsets)))
5724     (save-excursion
5725       (set-buffer nntp-server-buffer)
5726       ;; Translate all TAB characters into SPACE characters.
5727       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5728       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5729       (ietf-drums-unfold-fws)
5730       (gnus-run-hooks 'gnus-parse-headers-hook)
5731       (let ((case-fold-search t)
5732             in-reply-to header p lines chars)
5733         (goto-char (point-min))
5734         ;; Search to the beginning of the next header.  Error messages
5735         ;; do not begin with 2 or 3.
5736         (while (re-search-forward "^[23][0-9]+ " nil t)
5737           (setq id nil
5738                 ref nil)
5739           ;; This implementation of this function, with nine
5740           ;; search-forwards instead of the one re-search-forward and
5741           ;; a case (which basically was the old function) is actually
5742           ;; about twice as fast, even though it looks messier.  You
5743           ;; can't have everything, I guess.  Speed and elegance
5744           ;; doesn't always go hand in hand.
5745           (setq
5746            header
5747            (vector
5748             ;; Number.
5749             (prog1
5750                 (read cur)
5751               (end-of-line)
5752               (setq p (point))
5753               (narrow-to-region (point)
5754                                 (or (and (search-forward "\n.\n" nil t)
5755                                          (- (point) 2))
5756                                     (point))))
5757             ;; Subject.
5758             (progn
5759               (goto-char p)
5760               (if (search-forward "\nsubject:" nil t)
5761                   (funcall gnus-decode-encoded-word-function
5762                            (nnheader-header-value))
5763                 "(none)"))
5764             ;; From.
5765             (progn
5766               (goto-char p)
5767               (if (search-forward "\nfrom:" nil t)
5768                   (funcall gnus-decode-encoded-word-function
5769                            (nnheader-header-value))
5770                 "(nobody)"))
5771             ;; Date.
5772             (progn
5773               (goto-char p)
5774               (if (search-forward "\ndate:" nil t)
5775                   (nnheader-header-value) ""))
5776             ;; Message-ID.
5777             (progn
5778               (goto-char p)
5779               (setq id (if (re-search-forward
5780                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5781                            ;; We do it this way to make sure the Message-ID
5782                            ;; is (somewhat) syntactically valid.
5783                            (buffer-substring (match-beginning 1)
5784                                              (match-end 1))
5785                          ;; If there was no message-id, we just fake one
5786                          ;; to make subsequent routines simpler.
5787                          (nnheader-generate-fake-message-id))))
5788             ;; References.
5789             (progn
5790               (goto-char p)
5791               (if (search-forward "\nreferences:" nil t)
5792                   (progn
5793                     (setq end (point))
5794                     (prog1
5795                         (nnheader-header-value)
5796                       (setq ref
5797                             (buffer-substring
5798                              (progn
5799                                (end-of-line)
5800                                (search-backward ">" end t)
5801                                (1+ (point)))
5802                              (progn
5803                                (search-backward "<" end t)
5804                                (point))))))
5805                 ;; Get the references from the in-reply-to header if there
5806                 ;; were no references and the in-reply-to header looks
5807                 ;; promising.
5808                 (if (and (search-forward "\nin-reply-to:" nil t)
5809                          (setq in-reply-to (nnheader-header-value))
5810                          (string-match "<[^>]+>" in-reply-to))
5811                     (let (ref2)
5812                       (setq ref (substring in-reply-to (match-beginning 0)
5813                                            (match-end 0)))
5814                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5815                         (setq ref2 (substring in-reply-to (match-beginning 0)
5816                                               (match-end 0)))
5817                         (when (> (length ref2) (length ref))
5818                           (setq ref ref2)))
5819                       ref)
5820                   (setq ref nil))))
5821             ;; Chars.
5822             (progn
5823               (goto-char p)
5824               (if (search-forward "\nchars: " nil t)
5825                   (if (numberp (setq chars (ignore-errors (read cur))))
5826                       chars -1)
5827                 -1))
5828             ;; Lines.
5829             (progn
5830               (goto-char p)
5831               (if (search-forward "\nlines: " nil t)
5832                   (if (numberp (setq lines (ignore-errors (read cur))))
5833                       lines -1)
5834                 -1))
5835             ;; Xref.
5836             (progn
5837               (goto-char p)
5838               (and (search-forward "\nxref:" nil t)
5839                    (nnheader-header-value)))
5840             ;; Extra.
5841             (when gnus-extra-headers
5842               (let ((extra gnus-extra-headers)
5843                     out)
5844                 (while extra
5845                   (goto-char p)
5846                   (when (search-forward
5847                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5848                     (push (cons (car extra) (nnheader-header-value))
5849                           out))
5850                   (pop extra))
5851                 out))))
5852           (when (equal id ref)
5853             (setq ref nil))
5854
5855           (when gnus-alter-header-function
5856             (funcall gnus-alter-header-function header)
5857             (setq id (mail-header-id header)
5858                   ref (gnus-parent-id (mail-header-references header))))
5859
5860           (when (setq header
5861                       (gnus-dependencies-add-header
5862                        header dependencies force-new))
5863             (push header headers))
5864           (goto-char (point-max))
5865           (widen))
5866         (nreverse headers)))))
5867
5868 ;; Goes through the xover lines and returns a list of vectors
5869 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5870                                                   force-new dependencies
5871                                                   group also-fetch-heads)
5872   "Parse the news overview data in the server buffer.
5873 Return a list of headers that match SEQUENCE (see
5874 `nntp-retrieve-headers')."
5875   ;; Get the Xref when the users reads the articles since most/some
5876   ;; NNTP servers do not include Xrefs when using XOVER.
5877   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5878   (let ((mail-parse-charset gnus-newsgroup-charset)
5879         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5880         (cur nntp-server-buffer)
5881         (dependencies (or dependencies gnus-newsgroup-dependencies))
5882         (allp (cond
5883                ((eq gnus-read-all-available-headers t)
5884                 t)
5885                ((stringp gnus-read-all-available-headers)
5886                 (string-match gnus-read-all-available-headers group))
5887                (t
5888                 nil)))
5889         number headers header)
5890     (save-excursion
5891       (set-buffer nntp-server-buffer)
5892       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5893       ;; Allow the user to mangle the headers before parsing them.
5894       (gnus-run-hooks 'gnus-parse-headers-hook)
5895       (goto-char (point-min))
5896       (gnus-parse-without-error
5897         (while (and (or sequence allp)
5898                     (not (eobp)))
5899           (setq number (read cur))
5900           (when (not allp)
5901             (while (and sequence
5902                         (< (car sequence) number))
5903               (setq sequence (cdr sequence))))
5904           (when (and (or allp
5905                          (and sequence
5906                               (eq number (car sequence))))
5907                      (progn
5908                        (setq sequence (cdr sequence))
5909                        (setq header (inline
5910                                       (gnus-nov-parse-line
5911                                        number dependencies force-new)))))
5912             (push header headers))
5913           (forward-line 1)))
5914       ;; A common bug in inn is that if you have posted an article and
5915       ;; then retrieves the active file, it will answer correctly --
5916       ;; the new article is included.  However, a NOV entry for the
5917       ;; article may not have been generated yet, so this may fail.
5918       ;; We work around this problem by retrieving the last few
5919       ;; headers using HEAD.
5920       (if (or (not also-fetch-heads)
5921               (not sequence))
5922           ;; We (probably) got all the headers.
5923           (nreverse headers)
5924         (let ((gnus-nov-is-evil t))
5925           (nconc
5926            (nreverse headers)
5927            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5928              (gnus-get-newsgroup-headers))))))))
5929
5930 (defun gnus-article-get-xrefs ()
5931   "Fill in the Xref value in `gnus-current-headers', if necessary.
5932 This is meant to be called in `gnus-article-internal-prepare-hook'."
5933   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5934                                  gnus-current-headers)))
5935     (or (not gnus-use-cross-reference)
5936         (not headers)
5937         (and (mail-header-xref headers)
5938              (not (string= (mail-header-xref headers) "")))
5939         (let ((case-fold-search t)
5940               xref)
5941           (save-restriction
5942             (nnheader-narrow-to-headers)
5943             (goto-char (point-min))
5944             (when (or (and (not (eobp))
5945                            (eq (downcase (char-after)) ?x)
5946                            (looking-at "Xref:"))
5947                       (search-forward "\nXref:" nil t))
5948               (goto-char (1+ (match-end 0)))
5949               (setq xref (buffer-substring (point) (point-at-eol)))
5950               (mail-header-set-xref headers xref)))))))
5951
5952 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5953   "Find article ID and insert the summary line for that article.
5954 OLD-HEADER can either be a header or a line number to insert
5955 the subject line on."
5956   (let* ((line (and (numberp old-header) old-header))
5957          (old-header (and (vectorp old-header) old-header))
5958          (header (cond ((and old-header use-old-header)
5959                         old-header)
5960                        ((and (numberp id)
5961                              (gnus-number-to-header id))
5962                         (gnus-number-to-header id))
5963                        (t
5964                         (gnus-read-header id))))
5965          (number (and (numberp id) id))
5966          d)
5967     (when header
5968       ;; Rebuild the thread that this article is part of and go to the
5969       ;; article we have fetched.
5970       (when (and (not gnus-show-threads)
5971                  old-header)
5972         (when (and number
5973                    (setq d (gnus-data-find (mail-header-number old-header))))
5974           (goto-char (gnus-data-pos d))
5975           (gnus-data-remove
5976            number
5977            (- (point-at-bol)
5978               (prog1
5979                   (1+ (point-at-eol))
5980                 (gnus-delete-line))))))
5981       (when old-header
5982         (mail-header-set-number header (mail-header-number old-header)))
5983       (setq gnus-newsgroup-sparse
5984             (delq (setq number (mail-header-number header))
5985                   gnus-newsgroup-sparse))
5986       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5987       (push number gnus-newsgroup-limit)
5988       (gnus-rebuild-thread (mail-header-id header) line)
5989       (gnus-summary-goto-subject number nil t))
5990     (when (and (numberp number)
5991                (> number 0))
5992       ;; We have to update the boundaries even if we can't fetch the
5993       ;; article if ID is a number -- so that the next `P' or `N'
5994       ;; command will fetch the previous (or next) article even
5995       ;; if the one we tried to fetch this time has been canceled.
5996       (when (> number gnus-newsgroup-end)
5997         (setq gnus-newsgroup-end number))
5998       (when (< number gnus-newsgroup-begin)
5999         (setq gnus-newsgroup-begin number))
6000       (setq gnus-newsgroup-unselected
6001             (delq number gnus-newsgroup-unselected)))
6002     ;; Report back a success?
6003     (and header (mail-header-number header))))
6004
6005 ;;; Process/prefix in the summary buffer
6006
6007 (defun gnus-summary-work-articles (n)
6008   "Return a list of articles to be worked upon.
6009 The prefix argument, the list of process marked articles, and the
6010 current article will be taken into consideration."
6011   (save-excursion
6012     (set-buffer gnus-summary-buffer)
6013     (cond
6014      (n
6015       ;; A numerical prefix has been given.
6016       (setq n (prefix-numeric-value n))
6017       (let ((backward (< n 0))
6018             (n (abs (prefix-numeric-value n)))
6019             articles article)
6020         (save-excursion
6021           (while
6022               (and (> n 0)
6023                    (push (setq article (gnus-summary-article-number))
6024                          articles)
6025                    (if backward
6026                        (gnus-summary-find-prev nil article)
6027                      (gnus-summary-find-next nil article)))
6028             (decf n)))
6029         (nreverse articles)))
6030      ((and (gnus-region-active-p) (mark))
6031       (message "region active")
6032       ;; Work on the region between point and mark.
6033       (let ((max (max (point) (mark)))
6034             articles article)
6035         (save-excursion
6036           (goto-char (min (min (point) (mark))))
6037           (while
6038               (and
6039                (push (setq article (gnus-summary-article-number)) articles)
6040                (gnus-summary-find-next nil article)
6041                (< (point) max)))
6042           (nreverse articles))))
6043      (gnus-newsgroup-processable
6044       ;; There are process-marked articles present.
6045       ;; Save current state.
6046       (gnus-summary-save-process-mark)
6047       ;; Return the list.
6048       (reverse gnus-newsgroup-processable))
6049      (t
6050       ;; Just return the current article.
6051       (list (gnus-summary-article-number))))))
6052
6053 (defmacro gnus-summary-iterate (arg &rest forms)
6054   "Iterate over the process/prefixed articles and do FORMS.
6055 ARG is the interactive prefix given to the command.  FORMS will be
6056 executed with point over the summary line of the articles."
6057   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6058     `(let ((,articles (gnus-summary-work-articles ,arg)))
6059        (while ,articles
6060          (gnus-summary-goto-subject (car ,articles))
6061          ,@forms
6062          (pop ,articles)))))
6063
6064 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6065 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6066
6067 (defun gnus-summary-save-process-mark ()
6068   "Push the current set of process marked articles on the stack."
6069   (interactive)
6070   (push (copy-sequence gnus-newsgroup-processable)
6071         gnus-newsgroup-process-stack))
6072
6073 (defun gnus-summary-kill-process-mark ()
6074   "Push the current set of process marked articles on the stack and unmark."
6075   (interactive)
6076   (gnus-summary-save-process-mark)
6077   (gnus-summary-unmark-all-processable))
6078
6079 (defun gnus-summary-yank-process-mark ()
6080   "Pop the last process mark state off the stack and restore it."
6081   (interactive)
6082   (unless gnus-newsgroup-process-stack
6083     (error "Empty mark stack"))
6084   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6085
6086 (defun gnus-summary-process-mark-set (set)
6087   "Make SET into the current process marked articles."
6088   (gnus-summary-unmark-all-processable)
6089   (while set
6090     (gnus-summary-set-process-mark (pop set))))
6091
6092 ;;; Searching and stuff
6093
6094 (defun gnus-summary-search-group (&optional backward use-level)
6095   "Search for next unread newsgroup.
6096 If optional argument BACKWARD is non-nil, search backward instead."
6097   (save-excursion
6098     (set-buffer gnus-group-buffer)
6099     (when (gnus-group-search-forward
6100            backward nil (if use-level (gnus-group-group-level) nil))
6101       (gnus-group-group-name))))
6102
6103 (defun gnus-summary-best-group (&optional exclude-group)
6104   "Find the name of the best unread group.
6105 If EXCLUDE-GROUP, do not go to this group."
6106   (save-excursion
6107     (set-buffer gnus-group-buffer)
6108     (save-excursion
6109       (gnus-group-best-unread-group exclude-group))))
6110
6111 (defun gnus-summary-find-next (&optional unread article backward)
6112   (if backward
6113       (gnus-summary-find-prev unread article)
6114     (let* ((dummy (gnus-summary-article-intangible-p))
6115            (article (or article (gnus-summary-article-number)))
6116            (data (gnus-data-find-list article))
6117            result)
6118       (when (and (not dummy)
6119                  (or (not gnus-summary-check-current)
6120                      (not unread)
6121                      (not (gnus-data-unread-p (car data)))))
6122         (setq data (cdr data)))
6123       (when (setq result
6124                   (if unread
6125                       (progn
6126                         (while data
6127                           (unless (memq (gnus-data-number (car data))
6128                                         (cond
6129                                          ((eq gnus-auto-goto-ignores
6130                                               'always-undownloaded)
6131                                           gnus-newsgroup-undownloaded)
6132                                          (gnus-plugged
6133                                           nil)
6134                                          ((eq gnus-auto-goto-ignores
6135                                               'unfetched)
6136                                           gnus-newsgroup-unfetched)
6137                                          ((eq gnus-auto-goto-ignores
6138                                               'undownloaded)
6139                                           gnus-newsgroup-undownloaded)))
6140                             (when (gnus-data-unread-p (car data))
6141                               (setq result (car data)
6142                                     data nil)))
6143                           (setq data (cdr data)))
6144                         result)
6145                     (car data)))
6146         (goto-char (gnus-data-pos result))
6147         (gnus-data-number result)))))
6148
6149 (defun gnus-summary-find-prev (&optional unread article)
6150   (let* ((eobp (eobp))
6151          (article (or article (gnus-summary-article-number)))
6152          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6153          result)
6154     (when (and (not eobp)
6155                (or (not gnus-summary-check-current)
6156                    (not unread)
6157                    (not (gnus-data-unread-p (car data)))))
6158       (setq data (cdr data)))
6159     (when (setq result
6160                 (if unread
6161                     (progn
6162                       (while data
6163                         (unless (memq (gnus-data-number (car data))
6164                                       (cond
6165                                        ((eq gnus-auto-goto-ignores
6166                                             'always-undownloaded)
6167                                         gnus-newsgroup-undownloaded)
6168                                        (gnus-plugged
6169                                         nil)
6170                                        ((eq gnus-auto-goto-ignores
6171                                             'unfetched)
6172                                         gnus-newsgroup-unfetched)
6173                                        ((eq gnus-auto-goto-ignores
6174                                             'undownloaded)
6175                                         gnus-newsgroup-undownloaded)))
6176                           (when (gnus-data-unread-p (car data))
6177                             (setq result (car data)
6178                                   data nil)))
6179                         (setq data (cdr data)))
6180                       result)
6181                   (car data)))
6182       (goto-char (gnus-data-pos result))
6183       (gnus-data-number result))))
6184
6185 (defun gnus-summary-find-subject (subject &optional unread backward article)
6186   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6187          (article (or article (gnus-summary-article-number)))
6188          (articles (gnus-data-list backward))
6189          (arts (gnus-data-find-list article articles))
6190          result)
6191     (when (or (not gnus-summary-check-current)
6192               (not unread)
6193               (not (gnus-data-unread-p (car arts))))
6194       (setq arts (cdr arts)))
6195     (while arts
6196       (and (or (not unread)
6197                (gnus-data-unread-p (car arts)))
6198            (vectorp (gnus-data-header (car arts)))
6199            (gnus-subject-equal
6200             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6201            (setq result (car arts)
6202                  arts nil))
6203       (setq arts (cdr arts)))
6204     (and result
6205          (goto-char (gnus-data-pos result))
6206          (gnus-data-number result))))
6207
6208 (defun gnus-summary-search-forward (&optional unread subject backward)
6209   "Search forward for an article.
6210 If UNREAD, look for unread articles.  If SUBJECT, look for
6211 articles with that subject.  If BACKWARD, search backward instead."
6212   (cond (subject (gnus-summary-find-subject subject unread backward))
6213         (backward (gnus-summary-find-prev unread))
6214         (t (gnus-summary-find-next unread))))
6215
6216 (defun gnus-recenter (&optional n)
6217   "Center point in window and redisplay frame.
6218 Also do horizontal recentering."
6219   (interactive "P")
6220   (when (and gnus-auto-center-summary
6221              (not (eq gnus-auto-center-summary 'vertical)))
6222     (gnus-horizontal-recenter))
6223   (recenter n))
6224
6225 (defun gnus-summary-recenter ()
6226   "Center point in the summary window.
6227 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6228 displayed, no centering will be performed."
6229   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6230   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6231   (interactive)
6232   ;; The user has to want it.
6233   (when gnus-auto-center-summary
6234     (let* ((top (cond ((< (window-height) 4) 0)
6235                       ((< (window-height) 7) 1)
6236                       (t (if (numberp gnus-auto-center-summary)
6237                              gnus-auto-center-summary
6238                            2))))
6239            (height (1- (window-height)))
6240            (bottom (save-excursion (goto-char (point-max))
6241                                    (forward-line (- height))
6242                                    (point)))
6243            (window (get-buffer-window (current-buffer))))
6244       (when (get-buffer-window gnus-article-buffer)
6245         ;; Only do recentering when the article buffer is displayed,
6246         ;; Set the window start to either `bottom', which is the biggest
6247         ;; possible valid number, or the second line from the top,
6248         ;; whichever is the least.
6249         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6250           (if (> bottom top-pos)
6251               ;; Keep the second line from the top visible
6252               (set-window-start window top-pos t)
6253             ;; Try to keep the bottom line visible; if it's partially
6254             ;; obscured, either scroll one more line to make it fully
6255             ;; visible, or revert to using TOP-POS.
6256             (save-excursion
6257               (goto-char (point-max))
6258               (forward-line -1)
6259               (let ((last-line-start (point)))
6260                 (goto-char bottom)
6261                 (set-window-start window (point) t)
6262                 (when (not (pos-visible-in-window-p last-line-start window))
6263                   (forward-line 1)
6264                   (set-window-start window (min (point) top-pos) t)))))))
6265       ;; Do horizontal recentering while we're at it.
6266       (when (and (get-buffer-window (current-buffer) t)
6267                  (not (eq gnus-auto-center-summary 'vertical)))
6268         (let ((selected (selected-window)))
6269           (select-window (get-buffer-window (current-buffer) t))
6270           (gnus-summary-position-point)
6271           (gnus-horizontal-recenter)
6272           (select-window selected))))))
6273
6274 (defun gnus-summary-jump-to-group (newsgroup)
6275   "Move point to NEWSGROUP in group mode buffer."
6276   ;; Keep update point of group mode buffer if visible.
6277   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6278       (save-window-excursion
6279         ;; Take care of tree window mode.
6280         (when (get-buffer-window gnus-group-buffer)
6281           (pop-to-buffer gnus-group-buffer))
6282         (gnus-group-jump-to-group newsgroup))
6283     (save-excursion
6284       ;; Take care of tree window mode.
6285       (if (get-buffer-window gnus-group-buffer)
6286           (pop-to-buffer gnus-group-buffer)
6287         (set-buffer gnus-group-buffer))
6288       (gnus-group-jump-to-group newsgroup))))
6289
6290 ;; This function returns a list of article numbers based on the
6291 ;; difference between the ranges of read articles in this group and
6292 ;; the range of active articles.
6293 (defun gnus-list-of-unread-articles (group)
6294   (let* ((read (gnus-info-read (gnus-get-info group)))
6295          (active (or (gnus-active group) (gnus-activate-group group)))
6296          (last (cdr active))
6297          first nlast unread)
6298     ;; If none are read, then all are unread.
6299     (if (not read)
6300         (setq first (car active))
6301       ;; If the range of read articles is a single range, then the
6302       ;; first unread article is the article after the last read
6303       ;; article.  Sounds logical, doesn't it?
6304       (if (and (not (listp (cdr read)))
6305                (or (< (car read) (car active))
6306                    (progn (setq read (list read))
6307                           nil)))
6308           (setq first (max (car active) (1+ (cdr read))))
6309         ;; `read' is a list of ranges.
6310         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6311                                   (caar read)))
6312                   1)
6313           (setq first (car active)))
6314         (while read
6315           (when first
6316             (while (< first nlast)
6317               (push first unread)
6318               (setq first (1+ first))))
6319           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6320           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6321           (setq read (cdr read)))))
6322     ;; And add the last unread articles.
6323     (while (<= first last)
6324       (push first unread)
6325       (setq first (1+ first)))
6326     ;; Return the list of unread articles.
6327     (delq 0 (nreverse unread))))
6328
6329 (defun gnus-list-of-read-articles (group)
6330   "Return a list of unread, unticked and non-dormant articles."
6331   (let* ((info (gnus-get-info group))
6332          (marked (gnus-info-marks info))
6333          (active (gnus-active group)))
6334     (and info active
6335          (gnus-list-range-difference
6336           (gnus-list-range-difference
6337            (gnus-sorted-complement
6338             (gnus-uncompress-range active)
6339             (gnus-list-of-unread-articles group))
6340            (cdr (assq 'dormant marked)))
6341           (cdr (assq 'tick marked))))))
6342
6343 ;; Various summary commands
6344
6345 (defun gnus-summary-select-article-buffer ()
6346   "Reconfigure windows to show article buffer."
6347   (interactive)
6348   (if (not (gnus-buffer-live-p gnus-article-buffer))
6349       (error "There is no article buffer for this summary buffer")
6350     (gnus-configure-windows 'article)
6351     (select-window (get-buffer-window gnus-article-buffer))))
6352
6353 (defun gnus-summary-universal-argument (arg)
6354   "Perform any operation on all articles that are process/prefixed."
6355   (interactive "P")
6356   (let ((articles (gnus-summary-work-articles arg))
6357         func article)
6358     (if (eq
6359          (setq
6360           func
6361           (key-binding
6362            (read-key-sequence
6363             (substitute-command-keys
6364              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6365          'undefined)
6366         (gnus-error 1 "Undefined key")
6367       (save-excursion
6368         (while articles
6369           (gnus-summary-goto-subject (setq article (pop articles)))
6370           (let (gnus-newsgroup-processable)
6371             (command-execute func))
6372           (gnus-summary-remove-process-mark article)))))
6373   (gnus-summary-position-point))
6374
6375 (defun gnus-summary-toggle-truncation (&optional arg)
6376   "Toggle truncation of summary lines.
6377 With ARG, turn line truncation on if ARG is positive."
6378   (interactive "P")
6379   (setq truncate-lines
6380         (if (null arg) (not truncate-lines)
6381           (> (prefix-numeric-value arg) 0)))
6382   (redraw-display))
6383
6384 (defun gnus-summary-find-for-reselect ()
6385   "Return the number of an article to stay on across a reselect.
6386 The current article is considered, then following articles, then previous
6387 articles.  An article is sought which is not cancelled and isn't a temporary
6388 insertion from another group.  If there's no such then return a dummy 0."
6389   (let (found)
6390     (dolist (rev '(nil t))
6391       (unless found      ; don't demand the reverse list if we don't need it
6392         (let ((data (gnus-data-find-list
6393                      (gnus-summary-article-number) (gnus-data-list rev))))
6394           (while (and data (not found))
6395             (if (and (< 0 (gnus-data-number (car data)))
6396                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6397                 (setq found (gnus-data-number (car data))))
6398             (setq data (cdr data))))))
6399     (or found 0)))
6400
6401 (defun gnus-summary-reselect-current-group (&optional all rescan)
6402   "Exit and then reselect the current newsgroup.
6403 The prefix argument ALL means to select all articles."
6404   (interactive "P")
6405   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6406     (error "Ephemeral groups can't be reselected"))
6407   (let ((current-subject (gnus-summary-find-for-reselect))
6408         (group gnus-newsgroup-name))
6409     (setq gnus-newsgroup-begin nil)
6410     (gnus-summary-exit nil 'leave-hidden)
6411     ;; We have to adjust the point of group mode buffer because
6412     ;; point was moved to the next unread newsgroup by exiting.
6413     (gnus-summary-jump-to-group group)
6414     (when rescan
6415       (save-excursion
6416         (gnus-group-get-new-news-this-group 1)))
6417     (gnus-group-read-group all t)
6418     (gnus-summary-goto-subject current-subject nil t)))
6419
6420 (defun gnus-summary-rescan-group (&optional all)
6421   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6422   (interactive "P")
6423   (gnus-summary-reselect-current-group all t))
6424
6425 (defun gnus-summary-update-info (&optional non-destructive)
6426   (save-excursion
6427     (let ((group gnus-newsgroup-name))
6428       (when group
6429         (when gnus-newsgroup-kill-headers
6430           (setq gnus-newsgroup-killed
6431                 (gnus-compress-sequence
6432                  (gnus-sorted-union
6433                   (gnus-list-range-intersection
6434                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6435                   gnus-newsgroup-unreads)
6436                  t)))
6437         (unless (listp (cdr gnus-newsgroup-killed))
6438           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6439         (let ((headers gnus-newsgroup-headers))
6440           ;; Set the new ranges of read articles.
6441           (save-excursion
6442             (set-buffer gnus-group-buffer)
6443             (gnus-undo-force-boundary))
6444           (gnus-update-read-articles
6445            group (gnus-sorted-union
6446                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6447           ;; Set the current article marks.
6448           (let ((gnus-newsgroup-scored
6449                  (if (and (not gnus-save-score)
6450                           (not non-destructive))
6451                      nil
6452                    gnus-newsgroup-scored)))
6453             (save-excursion
6454               (gnus-update-marks)))
6455           ;; Do the cross-ref thing.
6456           (when gnus-use-cross-reference
6457             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6458           ;; Do not switch windows but change the buffer to work.
6459           (set-buffer gnus-group-buffer)
6460           (unless (gnus-ephemeral-group-p group)
6461             (gnus-group-update-group group)))))))
6462
6463 (defun gnus-summary-save-newsrc (&optional force)
6464   "Save the current number of read/marked articles in the dribble buffer.
6465 The dribble buffer will then be saved.
6466 If FORCE (the prefix), also save the .newsrc file(s)."
6467   (interactive "P")
6468   (gnus-summary-update-info t)
6469   (if force
6470       (gnus-save-newsrc-file)
6471     (gnus-dribble-save)))
6472
6473 (defun gnus-summary-exit (&optional temporary leave-hidden)
6474   "Exit reading current newsgroup, and then return to group selection mode.
6475 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6476   (interactive)
6477   (gnus-set-global-variables)
6478   (when (gnus-buffer-live-p gnus-article-buffer)
6479     (save-excursion
6480       (set-buffer gnus-article-buffer)
6481       (mm-destroy-parts gnus-article-mime-handles)
6482       ;; Set it to nil for safety reason.
6483       (setq gnus-article-mime-handle-alist nil)
6484       (setq gnus-article-mime-handles nil)))
6485   (gnus-kill-save-kill-buffer)
6486   (gnus-async-halt-prefetch)
6487   (let* ((group gnus-newsgroup-name)
6488          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6489          (gnus-group-is-exiting-p t)
6490          (mode major-mode)
6491          (group-point nil)
6492          (buf (current-buffer)))
6493     (unless quit-config
6494       ;; Do adaptive scoring, and possibly save score files.
6495       (when gnus-newsgroup-adaptive
6496         (gnus-score-adaptive))
6497       (when gnus-use-scoring
6498         (gnus-score-save)))
6499     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6500     ;; If we have several article buffers, we kill them at exit.
6501     (unless gnus-single-article-buffer
6502       (gnus-kill-buffer gnus-original-article-buffer)
6503       (setq gnus-article-current nil))
6504     (when gnus-use-cache
6505       (gnus-cache-possibly-remove-articles)
6506       (gnus-cache-save-buffers))
6507     (gnus-async-prefetch-remove-group group)
6508     (when gnus-suppress-duplicates
6509       (gnus-dup-enter-articles))
6510     (when gnus-use-trees
6511       (gnus-tree-close group))
6512     (when gnus-use-cache
6513       (gnus-cache-write-active))
6514     ;; Remove entries for this group.
6515     (nnmail-purge-split-history (gnus-group-real-name group))
6516     ;; Make all changes in this group permanent.
6517     (unless quit-config
6518       (gnus-run-hooks 'gnus-exit-group-hook)
6519       (gnus-summary-update-info))
6520     (gnus-close-group group)
6521     ;; Make sure where we were, and go to next newsgroup.
6522     (set-buffer gnus-group-buffer)
6523     (unless quit-config
6524       (gnus-group-jump-to-group group))
6525     (gnus-run-hooks 'gnus-summary-exit-hook)
6526     (unless (or quit-config
6527                 ;; If this group has disappeared from the summary
6528                 ;; buffer, don't skip forwards.
6529                 (not (string= group (gnus-group-group-name))))
6530       (gnus-group-next-unread-group 1))
6531     (setq group-point (point))
6532     (if temporary
6533         nil                             ;Nothing to do.
6534       ;; If we have several article buffers, we kill them at exit.
6535       (unless gnus-single-article-buffer
6536         (gnus-kill-buffer gnus-article-buffer)
6537         (gnus-kill-buffer gnus-original-article-buffer)
6538         (setq gnus-article-current nil))
6539       (set-buffer buf)
6540       (if (not gnus-kill-summary-on-exit)
6541           (progn
6542             (gnus-deaden-summary)
6543             (setq mode nil))
6544         ;; We set all buffer-local variables to nil.  It is unclear why
6545         ;; this is needed, but if we don't, buffer-local variables are
6546         ;; not garbage-collected, it seems.  This would the lead to en
6547         ;; ever-growing Emacs.
6548         (gnus-summary-clear-local-variables)
6549         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6550           (gnus-summary-clear-local-variables))
6551         (when (get-buffer gnus-article-buffer)
6552           (bury-buffer gnus-article-buffer))
6553         ;; We clear the global counterparts of the buffer-local
6554         ;; variables as well, just to be on the safe side.
6555         (set-buffer gnus-group-buffer)
6556         (gnus-summary-clear-local-variables)
6557         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6558           (gnus-summary-clear-local-variables))
6559         ;; Return to group mode buffer.
6560         (when (eq mode 'gnus-summary-mode)
6561           (gnus-kill-buffer buf)))
6562       (setq gnus-current-select-method gnus-select-method)
6563       (if leave-hidden
6564           (set-buffer gnus-group-buffer)
6565         (pop-to-buffer gnus-group-buffer))
6566       (if (not quit-config)
6567           (progn
6568             (goto-char group-point)
6569             (unless leave-hidden
6570               (gnus-configure-windows 'group 'force)))
6571         (gnus-handle-ephemeral-exit quit-config))
6572       ;; Clear the current group name.
6573       (unless quit-config
6574         (setq gnus-newsgroup-name nil)))))
6575
6576 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6577 (defun gnus-summary-exit-no-update (&optional no-questions)
6578   "Quit reading current newsgroup without updating read article info."
6579   (interactive)
6580   (let* ((group gnus-newsgroup-name)
6581          (gnus-group-is-exiting-p t)
6582          (gnus-group-is-exiting-without-update-p t)
6583          (quit-config (gnus-group-quit-config group)))
6584     (when (or no-questions
6585               gnus-expert-user
6586               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6587       (gnus-async-halt-prefetch)
6588       (run-hooks 'gnus-summary-prepare-exit-hook)
6589       (when (gnus-buffer-live-p gnus-article-buffer)
6590         (save-excursion
6591           (set-buffer gnus-article-buffer)
6592           (mm-destroy-parts gnus-article-mime-handles)
6593           ;; Set it to nil for safety reason.
6594           (setq gnus-article-mime-handle-alist nil)
6595           (setq gnus-article-mime-handles nil)))
6596       ;; If we have several article buffers, we kill them at exit.
6597       (unless gnus-single-article-buffer
6598         (gnus-kill-buffer gnus-article-buffer)
6599         (gnus-kill-buffer gnus-original-article-buffer)
6600         (setq gnus-article-current nil))
6601       (if (not gnus-kill-summary-on-exit)
6602           (gnus-deaden-summary)
6603         (gnus-close-group group)
6604         (gnus-summary-clear-local-variables)
6605         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6606           (gnus-summary-clear-local-variables))
6607         (set-buffer gnus-group-buffer)
6608         (gnus-summary-clear-local-variables)
6609         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6610           (gnus-summary-clear-local-variables))
6611         (gnus-kill-buffer gnus-summary-buffer))
6612       (unless gnus-single-article-buffer
6613         (setq gnus-article-current nil))
6614       (when gnus-use-trees
6615         (gnus-tree-close group))
6616       (gnus-async-prefetch-remove-group group)
6617       (when (get-buffer gnus-article-buffer)
6618         (bury-buffer gnus-article-buffer))
6619       ;; Return to the group buffer.
6620       (gnus-configure-windows 'group 'force)
6621       ;; Clear the current group name.
6622       (setq gnus-newsgroup-name nil)
6623       (unless (gnus-ephemeral-group-p group)
6624         (gnus-group-update-group group))
6625       (when (equal (gnus-group-group-name) group)
6626         (gnus-group-next-unread-group 1))
6627       (when quit-config
6628         (gnus-handle-ephemeral-exit quit-config)))))
6629
6630 (defun gnus-handle-ephemeral-exit (quit-config)
6631   "Handle movement when leaving an ephemeral group.
6632 The state which existed when entering the ephemeral is reset."
6633   (if (not (buffer-name (car quit-config)))
6634       (gnus-configure-windows 'group 'force)
6635     (set-buffer (car quit-config))
6636     (cond ((eq major-mode 'gnus-summary-mode)
6637            (gnus-set-global-variables))
6638           ((eq major-mode 'gnus-article-mode)
6639            (save-excursion
6640              ;; The `gnus-summary-buffer' variable may point
6641              ;; to the old summary buffer when using a single
6642              ;; article buffer.
6643              (unless (gnus-buffer-live-p gnus-summary-buffer)
6644                (set-buffer gnus-group-buffer))
6645              (set-buffer gnus-summary-buffer)
6646              (gnus-set-global-variables))))
6647     (if (or (eq (cdr quit-config) 'article)
6648             (eq (cdr quit-config) 'pick))
6649         (progn
6650           ;; The current article may be from the ephemeral group
6651           ;; thus it is best that we reload this article
6652           ;;
6653           ;; If we're exiting from a large digest, this can be
6654           ;; extremely slow.  So, it's better not to reload it. -- jh.
6655           ;;(gnus-summary-show-article)
6656           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6657               (gnus-configure-windows 'pick 'force)
6658             (gnus-configure-windows (cdr quit-config) 'force)))
6659       (gnus-configure-windows (cdr quit-config) 'force))
6660     (when (eq major-mode 'gnus-summary-mode)
6661       (gnus-summary-next-subject 1 nil t)
6662       (gnus-summary-recenter)
6663       (gnus-summary-position-point))))
6664
6665 ;;; Dead summaries.
6666
6667 (defvar gnus-dead-summary-mode-map nil)
6668
6669 (unless gnus-dead-summary-mode-map
6670   (setq gnus-dead-summary-mode-map (make-keymap))
6671   (suppress-keymap gnus-dead-summary-mode-map)
6672   (substitute-key-definition
6673    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6674   (dolist (key '("\C-d" "\r" "\177" [delete]))
6675     (define-key gnus-dead-summary-mode-map
6676       key 'gnus-summary-wake-up-the-dead))
6677   (dolist (key '("q" "Q"))
6678     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6679
6680 (defvar gnus-dead-summary-mode nil
6681   "Minor mode for Gnus summary buffers.")
6682
6683 (defun gnus-dead-summary-mode (&optional arg)
6684   "Minor mode for Gnus summary buffers."
6685   (interactive "P")
6686   (when (eq major-mode 'gnus-summary-mode)
6687     (make-local-variable 'gnus-dead-summary-mode)
6688     (setq gnus-dead-summary-mode
6689           (if (null arg) (not gnus-dead-summary-mode)
6690             (> (prefix-numeric-value arg) 0)))
6691     (when gnus-dead-summary-mode
6692       (add-minor-mode
6693        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6694
6695 (defun gnus-deaden-summary ()
6696   "Make the current summary buffer into a dead summary buffer."
6697   ;; Kill any previous dead summary buffer.
6698   (when (and gnus-dead-summary
6699              (buffer-name gnus-dead-summary))
6700     (save-excursion
6701       (set-buffer gnus-dead-summary)
6702       (when gnus-dead-summary-mode
6703         (kill-buffer (current-buffer)))))
6704   ;; Make this the current dead summary.
6705   (setq gnus-dead-summary (current-buffer))
6706   (gnus-dead-summary-mode 1)
6707   (let ((name (buffer-name)))
6708     (when (string-match "Summary" name)
6709       (rename-buffer
6710        (concat (substring name 0 (match-beginning 0)) "Dead "
6711                (substring name (match-beginning 0)))
6712        t)
6713       (bury-buffer))))
6714
6715 (defun gnus-kill-or-deaden-summary (buffer)
6716   "Kill or deaden the summary BUFFER."
6717   (save-excursion
6718     (when (and (buffer-name buffer)
6719                (not gnus-single-article-buffer))
6720       (save-excursion
6721         (set-buffer buffer)
6722         (gnus-kill-buffer gnus-article-buffer)
6723         (gnus-kill-buffer gnus-original-article-buffer)))
6724     (cond
6725      ;; Kill the buffer.
6726      (gnus-kill-summary-on-exit
6727       (when (and gnus-use-trees
6728                  (gnus-buffer-exists-p buffer))
6729         (save-excursion
6730           (set-buffer buffer)
6731           (gnus-tree-close gnus-newsgroup-name)))
6732       (gnus-kill-buffer buffer))
6733      ;; Deaden the buffer.
6734      ((gnus-buffer-exists-p buffer)
6735       (save-excursion
6736         (set-buffer buffer)
6737         (gnus-deaden-summary))))))
6738
6739 (defun gnus-summary-wake-up-the-dead (&rest args)
6740   "Wake up the dead summary buffer."
6741   (interactive)
6742   (gnus-dead-summary-mode -1)
6743   (let ((name (buffer-name)))
6744     (when (string-match "Dead " name)
6745       (rename-buffer
6746        (concat (substring name 0 (match-beginning 0))
6747                (substring name (match-end 0)))
6748        t)))
6749   (gnus-message 3 "This dead summary is now alive again"))
6750
6751 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6752 (defun gnus-summary-fetch-faq (&optional faq-dir)
6753   "Fetch the FAQ for the current group.
6754 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6755 in."
6756   (interactive
6757    (list
6758     (when current-prefix-arg
6759       (completing-read
6760        "FAQ dir: " (and (listp gnus-group-faq-directory)
6761                         (mapcar (lambda (file) (list file))
6762                                 gnus-group-faq-directory))))))
6763   (let (gnus-faq-buffer)
6764     (when (setq gnus-faq-buffer
6765                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6766       (gnus-configure-windows 'summary-faq))))
6767
6768 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6769 (defun gnus-summary-describe-group (&optional force)
6770   "Describe the current newsgroup."
6771   (interactive "P")
6772   (gnus-group-describe-group force gnus-newsgroup-name))
6773
6774 (defun gnus-summary-describe-briefly ()
6775   "Describe summary mode commands briefly."
6776   (interactive)
6777   (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")))
6778
6779 ;; Walking around group mode buffer from summary mode.
6780
6781 (defun gnus-summary-next-group (&optional no-article target-group backward)
6782   "Exit current newsgroup and then select next unread newsgroup.
6783 If prefix argument NO-ARTICLE is non-nil, no article is selected
6784 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6785 previous group instead."
6786   (interactive "P")
6787   ;; Stop pre-fetching.
6788   (gnus-async-halt-prefetch)
6789   (let ((current-group gnus-newsgroup-name)
6790         (current-buffer (current-buffer))
6791         entered)
6792     ;; First we semi-exit this group to update Xrefs and all variables.
6793     ;; We can't do a real exit, because the window conf must remain
6794     ;; the same in case the user is prompted for info, and we don't
6795     ;; want the window conf to change before that...
6796     (gnus-summary-exit t)
6797     (while (not entered)
6798       ;; Then we find what group we are supposed to enter.
6799       (set-buffer gnus-group-buffer)
6800       (gnus-group-jump-to-group current-group)
6801       (setq target-group
6802             (or target-group
6803                 (if (eq gnus-keep-same-level 'best)
6804                     (gnus-summary-best-group gnus-newsgroup-name)
6805                   (gnus-summary-search-group backward gnus-keep-same-level))))
6806       (if (not target-group)
6807           ;; There are no further groups, so we return to the group
6808           ;; buffer.
6809           (progn
6810             (gnus-message 5 "Returning to the group buffer")
6811             (setq entered t)
6812             (when (gnus-buffer-live-p current-buffer)
6813               (set-buffer current-buffer)
6814               (gnus-summary-exit))
6815             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6816         ;; We try to enter the target group.
6817         (gnus-group-jump-to-group target-group)
6818         (let ((unreads (gnus-group-group-unread)))
6819           (if (and (or (eq t unreads)
6820                        (and unreads (not (zerop unreads))))
6821                    (gnus-summary-read-group
6822                     target-group nil no-article
6823                     (and (buffer-name current-buffer) current-buffer)
6824                     nil backward))
6825               (setq entered t)
6826             (setq current-group target-group
6827                   target-group nil)))))))
6828
6829 (defun gnus-summary-prev-group (&optional no-article)
6830   "Exit current newsgroup and then select previous unread newsgroup.
6831 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6832   (interactive "P")
6833   (gnus-summary-next-group no-article nil t))
6834
6835 ;; Walking around summary lines.
6836
6837 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6838   "Go to the first subject satisfying any non-nil constraint.
6839 If UNREAD is non-nil, the article should be unread.
6840 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6841 If UNSEEN is non-nil, the article should be unseen.
6842 Returns the article selected or nil if there are no matching articles."
6843   (interactive "P")
6844   (cond
6845    ;; Empty summary.
6846    ((null gnus-newsgroup-data)
6847     (gnus-message 3 "No articles in the group")
6848     nil)
6849    ;; Pick the first article.
6850    ((not (or unread undownloaded unseen))
6851     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6852     (gnus-data-number (car gnus-newsgroup-data)))
6853    ;; Find the first unread article.
6854    (t
6855     (let ((data gnus-newsgroup-data))
6856       (while (and data
6857                   (let ((num (gnus-data-number (car data))))
6858                     (or (memq num gnus-newsgroup-unfetched)
6859                         (not (or (and unread
6860                                       (memq num gnus-newsgroup-unreads))
6861                                  (and undownloaded
6862                                       (memq num gnus-newsgroup-undownloaded))
6863                                  (and unseen
6864                                       (memq num gnus-newsgroup-unseen)))))))
6865         (setq data (cdr data)))
6866       (prog1
6867           (if data
6868               (progn
6869                 (goto-char (gnus-data-pos (car data)))
6870                 (gnus-data-number (car data)))
6871             (gnus-message 3 "No more%s articles"
6872                           (let* ((r (when unread " unread"))
6873                                  (d (when undownloaded " undownloaded"))
6874                                  (s (when unseen " unseen"))
6875                                  (l (delq nil (list r d s))))
6876                             (cond ((= 3 (length l))
6877                                    (concat r "," d ", or" s))
6878                                   ((= 2 (length l))
6879                                    (concat (car l) ", or" (cadr l)))
6880                                   ((= 1 (length l))
6881                                    (car l))
6882                                   (t
6883                                    ""))))
6884             nil
6885             )
6886         (gnus-summary-position-point))))))
6887
6888 (defun gnus-summary-next-subject (n &optional unread dont-display)
6889   "Go to next N'th summary line.
6890 If N is negative, go to the previous N'th subject line.
6891 If UNREAD is non-nil, only unread articles are selected.
6892 The difference between N and the actual number of steps taken is
6893 returned."
6894   (interactive "p")
6895   (let ((backward (< n 0))
6896         (n (abs n)))
6897     (while (and (> n 0)
6898                 (if backward
6899                     (gnus-summary-find-prev unread)
6900                   (gnus-summary-find-next unread)))
6901       (unless (zerop (setq n (1- n)))
6902         (gnus-summary-show-thread)))
6903     (when (/= 0 n)
6904       (gnus-message 7 "No more%s articles"
6905                     (if unread " unread" "")))
6906     (unless dont-display
6907       (gnus-summary-recenter)
6908       (gnus-summary-position-point))
6909     n))
6910
6911 (defun gnus-summary-next-unread-subject (n)
6912   "Go to next N'th unread summary line."
6913   (interactive "p")
6914   (gnus-summary-next-subject n t))
6915
6916 (defun gnus-summary-prev-subject (n &optional unread)
6917   "Go to previous N'th summary line.
6918 If optional argument UNREAD is non-nil, only unread article is selected."
6919   (interactive "p")
6920   (gnus-summary-next-subject (- n) unread))
6921
6922 (defun gnus-summary-prev-unread-subject (n)
6923   "Go to previous N'th unread summary line."
6924   (interactive "p")
6925   (gnus-summary-next-subject (- n) t))
6926
6927 (defun gnus-summary-goto-subjects (articles)
6928   "Insert the subject header for ARTICLES in the current buffer."
6929   (save-excursion
6930     (dolist (article articles)
6931       (gnus-summary-goto-subject article t)))
6932   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6933   (gnus-summary-position-point))
6934  
6935 (defun gnus-summary-goto-subject (article &optional force silent)
6936   "Go the subject line of ARTICLE.
6937 If FORCE, also allow jumping to articles not currently shown."
6938   (interactive "nArticle number: ")
6939   (unless (numberp article)
6940     (error "Article %s is not a number" article))
6941   (let ((b (point))
6942         (data (gnus-data-find article)))
6943     ;; We read in the article if we have to.
6944     (and (not data)
6945          force
6946          (gnus-summary-insert-subject
6947           article
6948           (if (or (numberp force) (vectorp force)) force)
6949           t)
6950          (setq data (gnus-data-find article)))
6951     (goto-char b)
6952     (if (not data)
6953         (progn
6954           (unless silent
6955             (gnus-message 3 "Can't find article %d" article))
6956           nil)
6957       (let ((pt (gnus-data-pos data)))
6958         (goto-char pt)
6959         (gnus-summary-set-article-display-arrow pt))
6960       (gnus-summary-position-point)
6961       article)))
6962
6963 ;; Walking around summary lines with displaying articles.
6964
6965 (defun gnus-summary-expand-window (&optional arg)
6966   "Make the summary buffer take up the entire Emacs frame.
6967 Given a prefix, will force an `article' buffer configuration."
6968   (interactive "P")
6969   (if arg
6970       (gnus-configure-windows 'article 'force)
6971     (gnus-configure-windows 'summary 'force)))
6972
6973 (defun gnus-summary-display-article (article &optional all-header)
6974   "Display ARTICLE in article buffer."
6975   (when (gnus-buffer-live-p gnus-article-buffer)
6976     (with-current-buffer gnus-article-buffer
6977       (mm-enable-multibyte)))
6978   (gnus-set-global-variables)
6979   (when (gnus-buffer-live-p gnus-article-buffer)
6980     (with-current-buffer gnus-article-buffer
6981       (setq gnus-article-charset gnus-newsgroup-charset)
6982       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6983       (mm-enable-multibyte)))
6984   (if (null article)
6985       nil
6986     (prog1
6987         (if gnus-summary-display-article-function
6988             (funcall gnus-summary-display-article-function article all-header)
6989           (gnus-article-prepare article all-header))
6990       (gnus-run-hooks 'gnus-select-article-hook)
6991       (when (and gnus-current-article
6992                  (not (zerop gnus-current-article)))
6993         (gnus-summary-goto-subject gnus-current-article))
6994       (gnus-summary-recenter)
6995       (when (and gnus-use-trees gnus-show-threads)
6996         (gnus-possibly-generate-tree article)
6997         (gnus-highlight-selected-tree article))
6998       ;; Successfully display article.
6999       (gnus-article-set-window-start
7000        (cdr (assq article gnus-newsgroup-bookmarks))))))
7001
7002 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7003   "Select the current article.
7004 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7005 non-nil, the article will be re-fetched even if it already present in
7006 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7007 be displayed."
7008   ;; Make sure we are in the summary buffer to work around bbdb bug.
7009   (unless (eq major-mode 'gnus-summary-mode)
7010     (set-buffer gnus-summary-buffer))
7011   (let ((article (or article (gnus-summary-article-number)))
7012         (all-headers (not (not all-headers))) ;Must be t or nil.
7013         gnus-summary-display-article-function)
7014     (and (not pseudo)
7015          (gnus-summary-article-pseudo-p article)
7016          (error "This is a pseudo-article"))
7017     (save-excursion
7018       (set-buffer gnus-summary-buffer)
7019       (if (or (and gnus-single-article-buffer
7020                    (or (null gnus-current-article)
7021                        (null gnus-article-current)
7022                        (null (get-buffer gnus-article-buffer))
7023                        (not (eq article (cdr gnus-article-current)))
7024                        (not (equal (car gnus-article-current)
7025                                    gnus-newsgroup-name))))
7026               (and (not gnus-single-article-buffer)
7027                    (or (null gnus-current-article)
7028                        (not (eq gnus-current-article article))))
7029               force)
7030           ;; The requested article is different from the current article.
7031           (progn
7032             (gnus-summary-display-article article all-headers)
7033             (when (gnus-buffer-live-p gnus-article-buffer)
7034               (with-current-buffer gnus-article-buffer
7035                 (if (not gnus-article-decoded-p) ;; a local variable
7036                     (mm-disable-multibyte))))
7037             (gnus-article-set-window-start
7038              (cdr (assq article gnus-newsgroup-bookmarks)))
7039             article)
7040         'old))))
7041
7042 (defun gnus-summary-force-verify-and-decrypt ()
7043   "Display buttons for signed/encrypted parts and verify/decrypt them."
7044   (interactive)
7045   (let ((mm-verify-option 'known)
7046         (mm-decrypt-option 'known)
7047         (gnus-article-emulate-mime t)
7048         (gnus-buttonized-mime-types (append (list "multipart/signed"
7049                                                   "multipart/encrypted")
7050                                             gnus-buttonized-mime-types)))
7051     (gnus-summary-select-article nil 'force)))
7052
7053 (defun gnus-summary-set-current-mark (&optional current-mark)
7054   "Obsolete function."
7055   nil)
7056
7057 (defun gnus-summary-next-article (&optional unread subject backward push)
7058   "Select the next article.
7059 If UNREAD, only unread articles are selected.
7060 If SUBJECT, only articles with SUBJECT are selected.
7061 If BACKWARD, the previous article is selected instead of the next."
7062   (interactive "P")
7063   (cond
7064    ;; Is there such an article?
7065    ((and (gnus-summary-search-forward unread subject backward)
7066          (or (gnus-summary-display-article (gnus-summary-article-number))
7067              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7068     (gnus-summary-position-point))
7069    ;; If not, we try the first unread, if that is wanted.
7070    ((and subject
7071          gnus-auto-select-same
7072          (gnus-summary-first-unread-article))
7073     (gnus-summary-position-point)
7074     (gnus-message 6 "Wrapped"))
7075    ;; Try to get next/previous article not displayed in this group.
7076    ((and gnus-auto-extend-newsgroup
7077          (not unread) (not subject))
7078     (gnus-summary-goto-article
7079      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7080      nil (count-lines (point-min) (point))))
7081    ;; Go to next/previous group.
7082    (t
7083     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7084       (gnus-summary-jump-to-group gnus-newsgroup-name))
7085     (let ((cmd last-command-char)
7086           (point
7087            (save-excursion
7088              (set-buffer gnus-group-buffer)
7089              (point)))
7090           (group
7091            (if (eq gnus-keep-same-level 'best)
7092                (gnus-summary-best-group gnus-newsgroup-name)
7093              (gnus-summary-search-group backward gnus-keep-same-level))))
7094       ;; For some reason, the group window gets selected.  We change
7095       ;; it back.
7096       (select-window (get-buffer-window (current-buffer)))
7097       ;; Select next unread newsgroup automagically.
7098       (cond
7099        ((or (not gnus-auto-select-next)
7100             (not cmd))
7101         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7102        ((or (eq gnus-auto-select-next 'quietly)
7103             (and (eq gnus-auto-select-next 'slightly-quietly)
7104                  push)
7105             (and (eq gnus-auto-select-next 'almost-quietly)
7106                  (gnus-summary-last-article-p)))
7107         ;; Select quietly.
7108         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7109             (gnus-summary-exit)
7110           (gnus-message 7 "No more%s articles (%s)..."
7111                         (if unread " unread" "")
7112                         (if group (concat "selecting " group)
7113                           "exiting"))
7114           (gnus-summary-next-group nil group backward)))
7115        (t
7116         (when (gnus-key-press-event-p last-input-event)
7117           (gnus-summary-walk-group-buffer
7118            gnus-newsgroup-name cmd unread backward point))))))))
7119
7120 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7121   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7122                       (?\C-p (gnus-group-prev-unread-group 1))))
7123         (cursor-in-echo-area t)
7124         keve key group ended prompt)
7125     (save-excursion
7126       (set-buffer gnus-group-buffer)
7127       (goto-char start)
7128       (setq group
7129             (if (eq gnus-keep-same-level 'best)
7130                 (gnus-summary-best-group gnus-newsgroup-name)
7131               (gnus-summary-search-group backward gnus-keep-same-level))))
7132     (while (not ended)
7133       (setq prompt
7134             (format
7135              "No more%s articles%s " (if unread " unread" "")
7136              (if (and group
7137                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7138                  (format " (Type %s for %s [%s])"
7139                          (single-key-description cmd) group
7140                          (gnus-group-unread group))
7141                (format " (Type %s to exit %s)"
7142                        (single-key-description cmd)
7143                        gnus-newsgroup-name))))
7144       ;; Confirm auto selection.
7145       (setq key (car (setq keve (gnus-read-event-char prompt)))
7146             ended t)
7147       (cond
7148        ((assq key keystrokes)
7149         (let ((obuf (current-buffer)))
7150           (switch-to-buffer gnus-group-buffer)
7151           (when group
7152             (gnus-group-jump-to-group group))
7153           (eval (cadr (assq key keystrokes)))
7154           (setq group (gnus-group-group-name))
7155           (switch-to-buffer obuf))
7156         (setq ended nil))
7157        ((equal key cmd)
7158         (if (or (not group)
7159                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7160             (gnus-summary-exit)
7161           (gnus-summary-next-group nil group backward)))
7162        (t
7163         (push (cdr keve) unread-command-events))))))
7164
7165 (defun gnus-summary-next-unread-article ()
7166   "Select unread article after current one."
7167   (interactive)
7168   (gnus-summary-next-article
7169    (or (not (eq gnus-summary-goto-unread 'never))
7170        (gnus-summary-last-article-p (gnus-summary-article-number)))
7171    (and gnus-auto-select-same
7172         (gnus-summary-article-subject))))
7173
7174 (defun gnus-summary-prev-article (&optional unread subject)
7175   "Select the article after the current one.
7176 If UNREAD is non-nil, only unread articles are selected."
7177   (interactive "P")
7178   (gnus-summary-next-article unread subject t))
7179
7180 (defun gnus-summary-prev-unread-article ()
7181   "Select unread article before current one."
7182   (interactive)
7183   (gnus-summary-prev-article
7184    (or (not (eq gnus-summary-goto-unread 'never))
7185        (gnus-summary-first-article-p (gnus-summary-article-number)))
7186    (and gnus-auto-select-same
7187         (gnus-summary-article-subject))))
7188
7189 (defun gnus-summary-next-page (&optional lines circular stop)
7190   "Show next page of the selected article.
7191 If at the end of the current article, select the next article.
7192 LINES says how many lines should be scrolled up.
7193
7194 If CIRCULAR is non-nil, go to the start of the article instead of
7195 selecting the next article when reaching the end of the current
7196 article.
7197
7198 If STOP is non-nil, just stop when reaching the end of the message.
7199
7200 Also see the variable `gnus-article-skip-boring'."
7201   (interactive "P")
7202   (setq gnus-summary-buffer (current-buffer))
7203   (gnus-set-global-variables)
7204   (let ((article (gnus-summary-article-number))
7205         (article-window (get-buffer-window gnus-article-buffer t))
7206         endp)
7207     ;; If the buffer is empty, we have no article.
7208     (unless article
7209       (error "No article to select"))
7210     (gnus-configure-windows 'article)
7211     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7212         (if (and (eq gnus-summary-goto-unread 'never)
7213                  (not (gnus-summary-last-article-p article)))
7214             (gnus-summary-next-article)
7215           (gnus-summary-next-unread-article))
7216       (if (or (null gnus-current-article)
7217               (null gnus-article-current)
7218               (/= article (cdr gnus-article-current))
7219               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7220           ;; Selected subject is different from current article's.
7221           (gnus-summary-display-article article)
7222         (when article-window
7223           (gnus-eval-in-buffer-window gnus-article-buffer
7224             (setq endp (or (gnus-article-next-page lines)
7225                            (gnus-article-only-boring-p))))
7226           (when endp
7227             (cond (stop
7228                    (gnus-message 3 "End of message"))
7229                   (circular
7230                    (gnus-summary-beginning-of-article))
7231                   (lines
7232                    (gnus-message 3 "End of message"))
7233                   ((null lines)
7234                    (if (and (eq gnus-summary-goto-unread 'never)
7235                             (not (gnus-summary-last-article-p article)))
7236                        (gnus-summary-next-article)
7237                      (gnus-summary-next-unread-article))))))))
7238     (gnus-summary-recenter)
7239     (gnus-summary-position-point)))
7240
7241 (defun gnus-summary-prev-page (&optional lines move)
7242   "Show previous page of selected article.
7243 Argument LINES specifies lines to be scrolled down.
7244 If MOVE, move to the previous unread article if point is at
7245 the beginning of the buffer."
7246   (interactive "P")
7247   (let ((article (gnus-summary-article-number))
7248         (article-window (get-buffer-window gnus-article-buffer t))
7249         endp)
7250     (gnus-configure-windows 'article)
7251     (if (or (null gnus-current-article)
7252             (null gnus-article-current)
7253             (/= article (cdr gnus-article-current))
7254             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7255         ;; Selected subject is different from current article's.
7256         (gnus-summary-display-article article)
7257       (gnus-summary-recenter)
7258       (when article-window
7259         (gnus-eval-in-buffer-window gnus-article-buffer
7260           (setq endp (gnus-article-prev-page lines)))
7261         (when (and move endp)
7262           (cond (lines
7263                  (gnus-message 3 "Beginning of message"))
7264                 ((null lines)
7265                  (if (and (eq gnus-summary-goto-unread 'never)
7266                           (not (gnus-summary-first-article-p article)))
7267                      (gnus-summary-prev-article)
7268                    (gnus-summary-prev-unread-article))))))))
7269   (gnus-summary-position-point))
7270
7271 (defun gnus-summary-prev-page-or-article (&optional lines)
7272   "Show previous page of selected article.
7273 Argument LINES specifies lines to be scrolled down.
7274 If at the beginning of the article, go to the next article."
7275   (interactive "P")
7276   (gnus-summary-prev-page lines t))
7277
7278 (defun gnus-summary-scroll-up (lines)
7279   "Scroll up (or down) one line current article.
7280 Argument LINES specifies lines to be scrolled up (or down if negative)."
7281   (interactive "p")
7282   (gnus-configure-windows 'article)
7283   (gnus-summary-show-thread)
7284   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7285     (gnus-eval-in-buffer-window gnus-article-buffer
7286       (cond ((> lines 0)
7287              (when (gnus-article-next-page lines)
7288                (gnus-message 3 "End of message")))
7289             ((< lines 0)
7290              (gnus-article-prev-page (- lines))))))
7291   (gnus-summary-recenter)
7292   (gnus-summary-position-point))
7293
7294 (defun gnus-summary-scroll-down (lines)
7295   "Scroll down (or up) one line current article.
7296 Argument LINES specifies lines to be scrolled down (or up if negative)."
7297   (interactive "p")
7298   (gnus-summary-scroll-up (- lines)))
7299
7300 (defun gnus-summary-next-same-subject ()
7301   "Select next article which has the same subject as current one."
7302   (interactive)
7303   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7304
7305 (defun gnus-summary-prev-same-subject ()
7306   "Select previous article which has the same subject as current one."
7307   (interactive)
7308   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7309
7310 (defun gnus-summary-next-unread-same-subject ()
7311   "Select next unread article which has the same subject as current one."
7312   (interactive)
7313   (gnus-summary-next-article t (gnus-summary-article-subject)))
7314
7315 (defun gnus-summary-prev-unread-same-subject ()
7316   "Select previous unread article which has the same subject as current one."
7317   (interactive)
7318   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7319
7320 (defun gnus-summary-first-unread-article ()
7321   "Select the first unread article.
7322 Return nil if there are no unread articles."
7323   (interactive)
7324   (prog1
7325       (when (gnus-summary-first-subject t)
7326         (gnus-summary-show-thread)
7327         (gnus-summary-first-subject t)
7328         (gnus-summary-display-article (gnus-summary-article-number)))
7329     (gnus-summary-position-point)))
7330
7331 (defun gnus-summary-first-unread-subject ()
7332   "Place the point on the subject line of the first unread article.
7333 Return nil if there are no unread articles."
7334   (interactive)
7335   (prog1
7336       (when (gnus-summary-first-subject t)
7337         (gnus-summary-show-thread)
7338         (gnus-summary-first-subject t))
7339     (gnus-summary-position-point)))
7340
7341 (defun gnus-summary-first-unseen-subject ()
7342   "Place the point on the subject line of the first unseen article.
7343 Return nil if there are no unseen articles."
7344   (interactive)
7345   (prog1
7346       (when (gnus-summary-first-subject nil nil t)
7347         (gnus-summary-show-thread)
7348         (gnus-summary-first-subject nil nil t))
7349     (gnus-summary-position-point)))
7350
7351 (defun gnus-summary-first-unseen-or-unread-subject ()
7352   "Place the point on the subject line of the first unseen article or,
7353 if all article have been seen, on the subject line of the first unread
7354 article."
7355   (interactive)
7356   (prog1
7357       (unless (when (gnus-summary-first-subject nil nil t)
7358                 (gnus-summary-show-thread)
7359                 (gnus-summary-first-subject nil nil t))
7360         (when (gnus-summary-first-subject t)
7361           (gnus-summary-show-thread)
7362           (gnus-summary-first-subject t)))
7363     (gnus-summary-position-point)))
7364
7365 (defun gnus-summary-first-article ()
7366   "Select the first article.
7367 Return nil if there are no articles."
7368   (interactive)
7369   (prog1
7370       (when (gnus-summary-first-subject)
7371         (gnus-summary-show-thread)
7372         (gnus-summary-first-subject)
7373         (gnus-summary-display-article (gnus-summary-article-number)))
7374     (gnus-summary-position-point)))
7375
7376 (defun gnus-summary-best-unread-article (&optional arg)
7377   "Select the unread article with the highest score.
7378 If given a prefix argument, select the next unread article that has a
7379 score higher than the default score."
7380   (interactive "P")
7381   (let ((article (if arg
7382                      (gnus-summary-better-unread-subject)
7383                    (gnus-summary-best-unread-subject))))
7384     (if article
7385         (gnus-summary-goto-article article)
7386       (error "No unread articles"))))
7387
7388 (defun gnus-summary-best-unread-subject ()
7389   "Select the unread subject with the highest score."
7390   (interactive)
7391   (let ((best -1000000)
7392         (data gnus-newsgroup-data)
7393         article score)
7394     (while data
7395       (and (gnus-data-unread-p (car data))
7396            (> (setq score
7397                     (gnus-summary-article-score (gnus-data-number (car data))))
7398               best)
7399            (setq best score
7400                  article (gnus-data-number (car data))))
7401       (setq data (cdr data)))
7402     (when article
7403       (gnus-summary-goto-subject article))
7404     (gnus-summary-position-point)
7405     article))
7406
7407 (defun gnus-summary-better-unread-subject ()
7408   "Select the first unread subject that has a score over the default score."
7409   (interactive)
7410   (let ((data gnus-newsgroup-data)
7411         article score)
7412     (while (and (setq article (gnus-data-number (car data)))
7413                 (or (gnus-data-read-p (car data))
7414                     (not (> (gnus-summary-article-score article)
7415                             gnus-summary-default-score))))
7416       (setq data (cdr data)))
7417     (when article
7418       (gnus-summary-goto-subject article))
7419     (gnus-summary-position-point)
7420     article))
7421
7422 (defun gnus-summary-last-subject ()
7423   "Go to the last displayed subject line in the group."
7424   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7425     (when article
7426       (gnus-summary-goto-subject article))))
7427
7428 (defun gnus-summary-goto-article (article &optional all-headers force)
7429   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7430 If ALL-HEADERS is non-nil, no header lines are hidden.
7431 If FORCE, go to the article even if it isn't displayed.  If FORCE
7432 is a number, it is the line the article is to be displayed on."
7433   (interactive
7434    (list
7435     (completing-read
7436      "Article number or Message-ID: "
7437      (mapcar (lambda (number) (list (int-to-string number)))
7438              gnus-newsgroup-limit))
7439     current-prefix-arg
7440     t))
7441   (prog1
7442       (if (and (stringp article)
7443                (string-match "@\\|%40" article))
7444           (gnus-summary-refer-article article)
7445         (when (stringp article)
7446           (setq article (string-to-number article)))
7447         (if (gnus-summary-goto-subject article force)
7448             (gnus-summary-display-article article all-headers)
7449           (gnus-message 4 "Couldn't go to article %s" article) nil))
7450     (gnus-summary-position-point)))
7451
7452 (defun gnus-summary-goto-last-article ()
7453   "Go to the previously read article."
7454   (interactive)
7455   (prog1
7456       (when gnus-last-article
7457         (gnus-summary-goto-article gnus-last-article nil t))
7458     (gnus-summary-position-point)))
7459
7460 (defun gnus-summary-pop-article (number)
7461   "Pop one article off the history and go to the previous.
7462 NUMBER articles will be popped off."
7463   (interactive "p")
7464   (let (to)
7465     (setq gnus-newsgroup-history
7466           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7467     (if to
7468         (gnus-summary-goto-article (car to) nil t)
7469       (error "Article history empty")))
7470   (gnus-summary-position-point))
7471
7472 ;; Summary commands and functions for limiting the summary buffer.
7473
7474 (defun gnus-summary-limit-to-articles (n)
7475   "Limit the summary buffer to the next N articles.
7476 If not given a prefix, use the process marked articles instead."
7477   (interactive "P")
7478   (prog1
7479       (let ((articles (gnus-summary-work-articles n)))
7480         (setq gnus-newsgroup-processable nil)
7481         (gnus-summary-limit articles))
7482     (gnus-summary-position-point)))
7483
7484 (defun gnus-summary-pop-limit (&optional total)
7485   "Restore the previous limit.
7486 If given a prefix, remove all limits."
7487   (interactive "P")
7488   (when total
7489     (setq gnus-newsgroup-limits
7490           (list (mapcar (lambda (h) (mail-header-number h))
7491                         gnus-newsgroup-headers))))
7492   (unless gnus-newsgroup-limits
7493     (error "No limit to pop"))
7494   (prog1
7495       (gnus-summary-limit nil 'pop)
7496     (gnus-summary-position-point)))
7497
7498 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7499   "Limit the summary buffer to articles that have subjects that match a regexp.
7500 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7501   (interactive
7502    (list (read-string (if current-prefix-arg
7503                           "Exclude subject (regexp): "
7504                         "Limit to subject (regexp): "))
7505          nil current-prefix-arg))
7506   (unless header
7507     (setq header "subject"))
7508   (when (not (equal "" subject))
7509     (prog1
7510         (let ((articles (gnus-summary-find-matching
7511                          (or header "subject") subject 'all nil nil
7512                          not-matching)))
7513           (unless articles
7514             (error "Found no matches for \"%s\"" subject))
7515           (gnus-summary-limit articles))
7516       (gnus-summary-position-point))))
7517
7518 (defun gnus-summary-limit-to-author (from &optional not-matching)
7519   "Limit the summary buffer to articles that have authors that match a regexp.
7520 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7521   (interactive
7522    (list (read-string (if current-prefix-arg
7523                           "Exclude author (regexp): "
7524                         "Limit to author (regexp): "))
7525          current-prefix-arg))
7526   (gnus-summary-limit-to-subject from "from" not-matching))
7527
7528 (defun gnus-summary-limit-to-age (age &optional younger-p)
7529   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7530 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7531 articles that are younger than AGE days."
7532   (interactive
7533    (let ((younger current-prefix-arg)
7534          (days-got nil)
7535          days)
7536      (while (not days-got)
7537        (setq days (if younger
7538                       (read-string "Limit to articles younger than (in days, older when negative): ")
7539                     (read-string
7540                      "Limit to articles older than (in days, younger when negative): ")))
7541        (when (> (length days) 0)
7542          (setq days (read days)))
7543        (if (numberp days)
7544            (progn
7545              (setq days-got t)
7546              (if (< days 0)
7547                  (progn
7548                    (setq younger (not younger))
7549                    (setq days (* days -1)))))
7550          (message "Please enter a number.")
7551          (sleep-for 1)))
7552      (list days younger)))
7553   (prog1
7554       (let ((data gnus-newsgroup-data)
7555             (cutoff (days-to-time age))
7556             articles d date is-younger)
7557         (while (setq d (pop data))
7558           (when (and (vectorp (gnus-data-header d))
7559                      (setq date (mail-header-date (gnus-data-header d))))
7560             (setq is-younger (time-less-p
7561                               (time-since (condition-case ()
7562                                               (date-to-time date)
7563                                             (error '(0 0))))
7564                               cutoff))
7565             (when (if younger-p
7566                       is-younger
7567                     (not is-younger))
7568               (push (gnus-data-number d) articles))))
7569         (gnus-summary-limit (nreverse articles)))
7570     (gnus-summary-position-point)))
7571
7572 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7573   "Limit the summary buffer to articles that match an 'extra' header."
7574   (interactive
7575    (let ((header
7576           (intern
7577            (gnus-completing-read-with-default
7578             (symbol-name (car gnus-extra-headers))
7579             (if current-prefix-arg
7580                 "Exclude extra header:"
7581               "Limit extra header:")
7582             (mapcar (lambda (x)
7583                       (cons (symbol-name x) x))
7584                     gnus-extra-headers)
7585             nil
7586             t))))
7587      (list header
7588            (read-string (format "%s header %s (regexp): "
7589                                 (if current-prefix-arg "Exclude" "Limit to")
7590                                 header))
7591            current-prefix-arg)))
7592   (when (not (equal "" regexp))
7593     (prog1
7594         (let ((articles (gnus-summary-find-matching
7595                          (cons 'extra header) regexp 'all nil nil
7596                          not-matching)))
7597           (unless articles
7598             (error "Found no matches for \"%s\"" regexp))
7599           (gnus-summary-limit articles))
7600       (gnus-summary-position-point))))
7601
7602 (defun gnus-summary-limit-to-display-predicate ()
7603   "Limit the summary buffer to the predicated in the `display' group parameter."
7604   (interactive)
7605   (unless gnus-newsgroup-display
7606     (error "There is no `display' group parameter"))
7607   (let (articles)
7608     (dolist (number gnus-newsgroup-articles)
7609       (when (funcall gnus-newsgroup-display)
7610         (push number articles)))
7611     (gnus-summary-limit articles))
7612   (gnus-summary-position-point))
7613
7614 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7615 (make-obsolete
7616  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7617
7618 (defun gnus-summary-limit-to-unread (&optional all)
7619   "Limit the summary buffer to articles that are not marked as read.
7620 If ALL is non-nil, limit strictly to unread articles."
7621   (interactive "P")
7622   (if all
7623       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7624     (gnus-summary-limit-to-marks
7625      ;; Concat all the marks that say that an article is read and have
7626      ;; those removed.
7627      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7628            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7629            gnus-low-score-mark gnus-expirable-mark
7630            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7631            gnus-duplicate-mark gnus-souped-mark)
7632      'reverse)))
7633
7634 (defun gnus-summary-limit-to-replied (&optional unreplied)
7635   "Limit the summary buffer to replied articles.
7636 If UNREPLIED (the prefix), limit to unreplied articles."
7637   (interactive "P")
7638   (if unreplied
7639       (gnus-summary-limit
7640        (gnus-set-difference gnus-newsgroup-articles
7641         gnus-newsgroup-replied))
7642     (gnus-summary-limit gnus-newsgroup-replied))
7643   (gnus-summary-position-point))
7644
7645 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7646 (make-obsolete 'gnus-summary-delete-marked-with
7647                'gnus-summary-limit-exclude-marks)
7648
7649 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7650   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7651 If REVERSE, limit the summary buffer to articles that are marked
7652 with MARKS.  MARKS can either be a string of marks or a list of marks.
7653 Returns how many articles were removed."
7654   (interactive "sMarks: ")
7655   (gnus-summary-limit-to-marks marks t))
7656
7657 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7658   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7659 If REVERSE (the prefix), limit the summary buffer to articles that are
7660 not marked with MARKS.  MARKS can either be a string of marks or a
7661 list of marks.
7662 Returns how many articles were removed."
7663   (interactive "sMarks: \nP")
7664   (prog1
7665       (let ((data gnus-newsgroup-data)
7666             (marks (if (listp marks) marks
7667                      (append marks nil))) ; Transform to list.
7668             articles)
7669         (while data
7670           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7671                   (memq (gnus-data-mark (car data)) marks))
7672             (push (gnus-data-number (car data)) articles))
7673           (setq data (cdr data)))
7674         (gnus-summary-limit articles))
7675     (gnus-summary-position-point)))
7676
7677 (defun gnus-summary-limit-to-score (score)
7678   "Limit to articles with score at or above SCORE."
7679   (interactive "NLimit to articles with score of at least: ")
7680   (let ((data gnus-newsgroup-data)
7681         articles)
7682     (while data
7683       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7684                 score)
7685         (push (gnus-data-number (car data)) articles))
7686       (setq data (cdr data)))
7687     (prog1
7688         (gnus-summary-limit articles)
7689       (gnus-summary-position-point))))
7690
7691 (defun gnus-summary-limit-to-unseen ()
7692   "Limit to unseen articles."
7693   (interactive)
7694   (prog1
7695       (gnus-summary-limit gnus-newsgroup-unseen)
7696     (gnus-summary-position-point)))
7697
7698 (defun gnus-summary-limit-include-thread (id)
7699   "Display all the hidden articles that is in the thread with ID in it.
7700 When called interactively, ID is the Message-ID of the current
7701 article."
7702   (interactive (list (mail-header-id (gnus-summary-article-header))))
7703   (let ((articles (gnus-articles-in-thread
7704                    (gnus-id-to-thread (gnus-root-id id)))))
7705     (prog1
7706         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7707       (gnus-summary-limit-include-matching-articles
7708        "subject"
7709        (regexp-quote (gnus-simplify-subject-re
7710                       (mail-header-subject (gnus-id-to-header id)))))
7711       (gnus-summary-position-point))))
7712
7713 (defun gnus-summary-limit-include-matching-articles (header regexp)
7714   "Display all the hidden articles that have HEADERs that match REGEXP."
7715   (interactive (list (read-string "Match on header: ")
7716                      (read-string "Regexp: ")))
7717   (let ((articles (gnus-find-matching-articles header regexp)))
7718     (prog1
7719         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7720       (gnus-summary-position-point))))
7721
7722 (defun gnus-summary-insert-dormant-articles ()
7723   "Insert all the dormant articles for this group into the current buffer."
7724   (interactive)
7725   (let ((gnus-verbose (max 6 gnus-verbose)))
7726     (if (not gnus-newsgroup-dormant)
7727         (gnus-message 3 "No cached articles for this group")
7728       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7729
7730 (defun gnus-summary-limit-include-dormant ()
7731   "Display all the hidden articles that are marked as dormant.
7732 Note that this command only works on a subset of the articles currently
7733 fetched for this group."
7734   (interactive)
7735   (unless gnus-newsgroup-dormant
7736     (error "There are no dormant articles in this group"))
7737   (prog1
7738       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7739     (gnus-summary-position-point)))
7740
7741 (defun gnus-summary-limit-exclude-dormant ()
7742   "Hide all dormant articles."
7743   (interactive)
7744   (prog1
7745       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7746     (gnus-summary-position-point)))
7747
7748 (defun gnus-summary-limit-exclude-childless-dormant ()
7749   "Hide all dormant articles that have no children."
7750   (interactive)
7751   (let ((data (gnus-data-list t))
7752         articles d children)
7753     ;; Find all articles that are either not dormant or have
7754     ;; children.
7755     (while (setq d (pop data))
7756       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7757                 (and (setq children
7758                            (gnus-article-children (gnus-data-number d)))
7759                      (let (found)
7760                        (while children
7761                          (when (memq (car children) articles)
7762                            (setq children nil
7763                                  found t))
7764                          (pop children))
7765                        found)))
7766         (push (gnus-data-number d) articles)))
7767     ;; Do the limiting.
7768     (prog1
7769         (gnus-summary-limit articles)
7770       (gnus-summary-position-point))))
7771
7772 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7773   "Mark all unread excluded articles as read.
7774 If ALL, mark even excluded ticked and dormants as read."
7775   (interactive "P")
7776   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7777   (let ((articles (gnus-sorted-ndifference
7778                    (sort
7779                     (mapcar (lambda (h) (mail-header-number h))
7780                             gnus-newsgroup-headers)
7781                     '<)
7782                    gnus-newsgroup-limit))
7783         article)
7784     (setq gnus-newsgroup-unreads
7785           (gnus-sorted-intersection gnus-newsgroup-unreads
7786                                     gnus-newsgroup-limit))
7787     (if all
7788         (setq gnus-newsgroup-dormant nil
7789               gnus-newsgroup-marked nil
7790               gnus-newsgroup-reads
7791               (nconc
7792                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7793                gnus-newsgroup-reads))
7794       (while (setq article (pop articles))
7795         (unless (or (memq article gnus-newsgroup-dormant)
7796                     (memq article gnus-newsgroup-marked))
7797           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7798
7799 (defun gnus-summary-limit (articles &optional pop)
7800   (if pop
7801       ;; We pop the previous limit off the stack and use that.
7802       (setq articles (car gnus-newsgroup-limits)
7803             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7804     ;; We use the new limit, so we push the old limit on the stack.
7805     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7806   ;; Set the limit.
7807   (setq gnus-newsgroup-limit articles)
7808   (let ((total (length gnus-newsgroup-data))
7809         (data (gnus-data-find-list (gnus-summary-article-number)))
7810         (gnus-summary-mark-below nil)   ; Inhibit this.
7811         found)
7812     ;; This will do all the work of generating the new summary buffer
7813     ;; according to the new limit.
7814     (gnus-summary-prepare)
7815     ;; Hide any threads, possibly.
7816     (gnus-summary-maybe-hide-threads)
7817     ;; Try to return to the article you were at, or one in the
7818     ;; neighborhood.
7819     (when data
7820       ;; We try to find some article after the current one.
7821       (while data
7822         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7823           (setq data nil
7824                 found t))
7825         (setq data (cdr data))))
7826     (unless found
7827       ;; If there is no data, that means that we were after the last
7828       ;; article.  The same goes when we can't find any articles
7829       ;; after the current one.
7830       (goto-char (point-max))
7831       (gnus-summary-find-prev))
7832     (gnus-set-mode-line 'summary)
7833     ;; We return how many articles were removed from the summary
7834     ;; buffer as a result of the new limit.
7835     (- total (length gnus-newsgroup-data))))
7836
7837 (defsubst gnus-invisible-cut-children (threads)
7838   (let ((num 0))
7839     (while threads
7840       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7841         (incf num))
7842       (pop threads))
7843     (< num 2)))
7844
7845 (defsubst gnus-cut-thread (thread)
7846   "Go forwards in the thread until we find an article that we want to display."
7847   (when (or (eq gnus-fetch-old-headers 'some)
7848             (eq gnus-fetch-old-headers 'invisible)
7849             (numberp gnus-fetch-old-headers)
7850             (eq gnus-build-sparse-threads 'some)
7851             (eq gnus-build-sparse-threads 'more))
7852     ;; Deal with old-fetched headers and sparse threads.
7853     (while (and
7854             thread
7855             (or
7856              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7857              (gnus-summary-article-ancient-p
7858               (mail-header-number (car thread))))
7859             (if (or (<= (length (cdr thread)) 1)
7860                     (eq gnus-fetch-old-headers 'invisible))
7861                 (setq gnus-newsgroup-limit
7862                       (delq (mail-header-number (car thread))
7863                             gnus-newsgroup-limit)
7864                       thread (cadr thread))
7865               (when (gnus-invisible-cut-children (cdr thread))
7866                 (let ((th (cdr thread)))
7867                   (while th
7868                     (if (memq (mail-header-number (caar th))
7869                               gnus-newsgroup-limit)
7870                         (setq thread (car th)
7871                               th nil)
7872                       (setq th (cdr th))))))))))
7873   thread)
7874
7875 (defun gnus-cut-threads (threads)
7876   "Cut off all uninteresting articles from the beginning of THREADS."
7877   (when (or (eq gnus-fetch-old-headers 'some)
7878             (eq gnus-fetch-old-headers 'invisible)
7879             (numberp gnus-fetch-old-headers)
7880             (eq gnus-build-sparse-threads 'some)
7881             (eq gnus-build-sparse-threads 'more))
7882     (let ((th threads))
7883       (while th
7884         (setcar th (gnus-cut-thread (car th)))
7885         (setq th (cdr th)))))
7886   ;; Remove nixed out threads.
7887   (delq nil threads))
7888
7889 (defun gnus-summary-initial-limit (&optional show-if-empty)
7890   "Figure out what the initial limit is supposed to be on group entry.
7891 This entails weeding out unwanted dormants, low-scored articles,
7892 fetch-old-headers verbiage, and so on."
7893   ;; Most groups have nothing to remove.
7894   (if (or gnus-inhibit-limiting
7895           (and (null gnus-newsgroup-dormant)
7896                (eq gnus-newsgroup-display 'gnus-not-ignore)
7897                (not (eq gnus-fetch-old-headers 'some))
7898                (not (numberp gnus-fetch-old-headers))
7899                (not (eq gnus-fetch-old-headers 'invisible))
7900                (null gnus-summary-expunge-below)
7901                (not (eq gnus-build-sparse-threads 'some))
7902                (not (eq gnus-build-sparse-threads 'more))
7903                (null gnus-thread-expunge-below)
7904                (not gnus-use-nocem)))
7905       ()                                ; Do nothing.
7906     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7907     (setq gnus-newsgroup-limit nil)
7908     (mapatoms
7909      (lambda (node)
7910        (unless (car (symbol-value node))
7911          ;; These threads have no parents -- they are roots.
7912          (let ((nodes (cdr (symbol-value node)))
7913                thread)
7914            (while nodes
7915              (if (and gnus-thread-expunge-below
7916                       (< (gnus-thread-total-score (car nodes))
7917                          gnus-thread-expunge-below))
7918                  (gnus-expunge-thread (pop nodes))
7919                (setq thread (pop nodes))
7920                (gnus-summary-limit-children thread))))))
7921      gnus-newsgroup-dependencies)
7922     ;; If this limitation resulted in an empty group, we might
7923     ;; pop the previous limit and use it instead.
7924     (when (and (not gnus-newsgroup-limit)
7925                show-if-empty)
7926       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7927     gnus-newsgroup-limit))
7928
7929 (defun gnus-summary-limit-children (thread)
7930   "Return 1 if this subthread is visible and 0 if it is not."
7931   ;; First we get the number of visible children to this thread.  This
7932   ;; is done by recursing down the thread using this function, so this
7933   ;; will really go down to a leaf article first, before slowly
7934   ;; working its way up towards the root.
7935   (when thread
7936     (let* ((max-lisp-eval-depth 5000)
7937            (children
7938            (if (cdr thread)
7939                (apply '+ (mapcar 'gnus-summary-limit-children
7940                                  (cdr thread)))
7941              0))
7942           (number (mail-header-number (car thread)))
7943           score)
7944       (if (and
7945            (not (memq number gnus-newsgroup-marked))
7946            (or
7947             ;; If this article is dormant and has absolutely no visible
7948             ;; children, then this article isn't visible.
7949             (and (memq number gnus-newsgroup-dormant)
7950                  (zerop children))
7951             ;; If this is "fetch-old-headered" and there is no
7952             ;; visible children, then we don't want this article.
7953             (and (or (eq gnus-fetch-old-headers 'some)
7954                      (numberp gnus-fetch-old-headers))
7955                  (gnus-summary-article-ancient-p number)
7956                  (zerop children))
7957             ;; If this is "fetch-old-headered" and `invisible', then
7958             ;; we don't want this article.
7959             (and (eq gnus-fetch-old-headers 'invisible)
7960                  (gnus-summary-article-ancient-p number))
7961             ;; If this is a sparsely inserted article with no children,
7962             ;; we don't want it.
7963             (and (eq gnus-build-sparse-threads 'some)
7964                  (gnus-summary-article-sparse-p number)
7965                  (zerop children))
7966             ;; If we use expunging, and this article is really
7967             ;; low-scored, then we don't want this article.
7968             (when (and gnus-summary-expunge-below
7969                        (< (setq score
7970                                 (or (cdr (assq number gnus-newsgroup-scored))
7971                                     gnus-summary-default-score))
7972                           gnus-summary-expunge-below))
7973               ;; We increase the expunge-tally here, but that has
7974               ;; nothing to do with the limits, really.
7975               (incf gnus-newsgroup-expunged-tally)
7976               ;; We also mark as read here, if that's wanted.
7977               (when (and gnus-summary-mark-below
7978                          (< score gnus-summary-mark-below))
7979                 (setq gnus-newsgroup-unreads
7980                       (delq number gnus-newsgroup-unreads))
7981                 (if gnus-newsgroup-auto-expire
7982                     (push number gnus-newsgroup-expirable)
7983                   (push (cons number gnus-low-score-mark)
7984                         gnus-newsgroup-reads)))
7985               t)
7986             ;; Do the `display' group parameter.
7987             (and gnus-newsgroup-display
7988                  (not (funcall gnus-newsgroup-display)))
7989             ;; Check NoCeM things.
7990             (if (and gnus-use-nocem
7991                      (gnus-nocem-unwanted-article-p
7992                       (mail-header-id (car thread))))
7993                 (progn
7994                   (setq gnus-newsgroup-unreads
7995                         (delq number gnus-newsgroup-unreads))
7996                   t))))
7997           ;; Nope, invisible article.
7998           0
7999         ;; Ok, this article is to be visible, so we add it to the limit
8000         ;; and return 1.
8001         (push number gnus-newsgroup-limit)
8002         1))))
8003
8004 (defun gnus-expunge-thread (thread)
8005   "Mark all articles in THREAD as read."
8006   (let* ((number (mail-header-number (car thread))))
8007     (incf gnus-newsgroup-expunged-tally)
8008     ;; We also mark as read here, if that's wanted.
8009     (setq gnus-newsgroup-unreads
8010           (delq number gnus-newsgroup-unreads))
8011     (if gnus-newsgroup-auto-expire
8012         (push number gnus-newsgroup-expirable)
8013       (push (cons number gnus-low-score-mark)
8014             gnus-newsgroup-reads)))
8015   ;; Go recursively through all subthreads.
8016   (mapcar 'gnus-expunge-thread (cdr thread)))
8017
8018 ;; Summary article oriented commands
8019
8020 (defun gnus-summary-refer-parent-article (n)
8021   "Refer parent article N times.
8022 If N is negative, go to ancestor -N instead.
8023 The difference between N and the number of articles fetched is returned."
8024   (interactive "p")
8025   (let ((skip 1)
8026         error header ref)
8027     (when (not (natnump n))
8028       (setq skip (abs n)
8029             n 1))
8030     (while (and (> n 0)
8031                 (not error))
8032       (setq header (gnus-summary-article-header))
8033       (if (and (eq (mail-header-number header)
8034                    (cdr gnus-article-current))
8035                (equal gnus-newsgroup-name
8036                       (car gnus-article-current)))
8037           ;; If we try to find the parent of the currently
8038           ;; displayed article, then we take a look at the actual
8039           ;; References header, since this is slightly more
8040           ;; reliable than the References field we got from the
8041           ;; server.
8042           (save-excursion
8043             (set-buffer gnus-original-article-buffer)
8044             (nnheader-narrow-to-headers)
8045             (unless (setq ref (message-fetch-field "references"))
8046               (when (setq ref (message-fetch-field "in-reply-to"))
8047                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8048             (widen))
8049         (setq ref
8050               ;; It's not the current article, so we take a bet on
8051               ;; the value we got from the server.
8052               (mail-header-references header)))
8053       (if (and ref
8054                (not (equal ref "")))
8055           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8056             (gnus-message 1 "Couldn't find parent"))
8057         (gnus-message 1 "No references in article %d"
8058                       (gnus-summary-article-number))
8059         (setq error t))
8060       (decf n))
8061     (gnus-summary-position-point)
8062     n))
8063
8064 (defun gnus-summary-refer-references ()
8065   "Fetch all articles mentioned in the References header.
8066 Return the number of articles fetched."
8067   (interactive)
8068   (let ((ref (mail-header-references (gnus-summary-article-header)))
8069         (current (gnus-summary-article-number))
8070         (n 0))
8071     (if (or (not ref)
8072             (equal ref ""))
8073         (error "No References in the current article")
8074       ;; For each Message-ID in the References header...
8075       (while (string-match "<[^>]*>" ref)
8076         (incf n)
8077         ;; ... fetch that article.
8078         (gnus-summary-refer-article
8079          (prog1 (match-string 0 ref)
8080            (setq ref (substring ref (match-end 0))))))
8081       (gnus-summary-goto-subject current)
8082       (gnus-summary-position-point)
8083       n)))
8084
8085 (defun gnus-summary-refer-thread (&optional limit)
8086   "Fetch all articles in the current thread.
8087 If LIMIT (the numerical prefix), fetch that many old headers instead
8088 of what's specified by the `gnus-refer-thread-limit' variable."
8089   (interactive "P")
8090   (let ((id (mail-header-id (gnus-summary-article-header)))
8091         (limit (if limit (prefix-numeric-value limit)
8092                  gnus-refer-thread-limit)))
8093     (unless (eq gnus-fetch-old-headers 'invisible)
8094       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8095       ;; Retrieve the headers and read them in.
8096       (if (eq (if (numberp limit)
8097                   (gnus-retrieve-headers
8098                    (list (min
8099                           (+ (mail-header-number
8100                               (gnus-summary-article-header))
8101                              limit)
8102                           gnus-newsgroup-end))
8103                    gnus-newsgroup-name (* limit 2))
8104                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8105                 ;; headers.
8106                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8107                                        gnus-newsgroup-name limit))
8108               'nov)
8109           (gnus-build-all-threads)
8110         (error "Can't fetch thread from back ends that don't support NOV"))
8111       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8112     (gnus-summary-limit-include-thread id)))
8113
8114 (defun gnus-summary-refer-article (message-id)
8115   "Fetch an article specified by MESSAGE-ID."
8116   (interactive "sMessage-ID: ")
8117   (when (and (stringp message-id)
8118              (not (zerop (length message-id))))
8119     (setq message-id (gnus-replace-in-string message-id " " ""))
8120     ;; Construct the correct Message-ID if necessary.
8121     ;; Suggested by tale@pawl.rpi.edu.
8122     (unless (string-match "^<" message-id)
8123       (setq message-id (concat "<" message-id)))
8124     (unless (string-match ">$" message-id)
8125       (setq message-id (concat message-id ">")))
8126     ;; People often post MIDs from URLs, so unhex it:
8127     (unless (string-match "@" message-id)
8128       (setq message-id (gnus-url-unhex-string message-id)))
8129     (let* ((header (gnus-id-to-header message-id))
8130            (sparse (and header
8131                         (gnus-summary-article-sparse-p
8132                          (mail-header-number header))
8133                         (memq (mail-header-number header)
8134                               gnus-newsgroup-limit)))
8135            number)
8136       (cond
8137        ;; If the article is present in the buffer we just go to it.
8138        ((and header
8139              (or (not (gnus-summary-article-sparse-p
8140                        (mail-header-number header)))
8141                  sparse))
8142         (prog1
8143             (gnus-summary-goto-article
8144              (mail-header-number header) nil t)
8145           (when sparse
8146             (gnus-summary-update-article (mail-header-number header)))))
8147        (t
8148         ;; We fetch the article.
8149         (catch 'found
8150           (dolist (gnus-override-method (gnus-refer-article-methods))
8151             (when (and (gnus-check-server gnus-override-method)
8152                        ;; Fetch the header,
8153                        (setq number (gnus-summary-insert-subject message-id)))
8154               ;; and display the article.
8155               (gnus-summary-select-article nil nil nil number)
8156               (throw 'found t)))
8157           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8158
8159 (defun gnus-refer-article-methods ()
8160   "Return a list of referable methods."
8161   (cond
8162    ;; No method, so we default to current and native.
8163    ((null gnus-refer-article-method)
8164     (list gnus-current-select-method gnus-select-method))
8165    ;; Current.
8166    ((eq 'current gnus-refer-article-method)
8167     (list gnus-current-select-method))
8168    ;; List of select methods.
8169    ((not (and (symbolp (car gnus-refer-article-method))
8170               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8171     (let (out)
8172       (dolist (method gnus-refer-article-method)
8173         (push (if (eq 'current method)
8174                   gnus-current-select-method
8175                 method)
8176               out))
8177       (nreverse out)))
8178    ;; One single select method.
8179    (t
8180     (list gnus-refer-article-method))))
8181
8182 (defun gnus-summary-edit-parameters ()
8183   "Edit the group parameters of the current group."
8184   (interactive)
8185   (gnus-group-edit-group gnus-newsgroup-name 'params))
8186
8187 (defun gnus-summary-customize-parameters ()
8188   "Customize the group parameters of the current group."
8189   (interactive)
8190   (gnus-group-customize gnus-newsgroup-name))
8191
8192 (defun gnus-summary-enter-digest-group (&optional force)
8193   "Enter an nndoc group based on the current article.
8194 If FORCE, force a digest interpretation.  If not, try
8195 to guess what the document format is."
8196   (interactive "P")
8197   (let ((conf gnus-current-window-configuration))
8198     (save-window-excursion
8199       (save-excursion
8200         (let (gnus-article-prepare-hook
8201               gnus-display-mime-function
8202               gnus-break-pages)
8203           (gnus-summary-select-article))))
8204     (setq gnus-current-window-configuration conf)
8205     (let* ((name (format "%s-%d"
8206                          (gnus-group-prefixed-name
8207                           gnus-newsgroup-name (list 'nndoc ""))
8208                          (save-excursion
8209                            (set-buffer gnus-summary-buffer)
8210                            gnus-current-article)))
8211            (ogroup gnus-newsgroup-name)
8212            (params (append (gnus-info-params (gnus-get-info ogroup))
8213                            (list (cons 'to-group ogroup))
8214                            (list (cons 'parent-group ogroup))
8215                            (list (cons 'save-article-group ogroup))))
8216            (case-fold-search t)
8217            (buf (current-buffer))
8218            dig to-address)
8219       (save-excursion
8220         (set-buffer gnus-original-article-buffer)
8221         ;; Have the digest group inherit the main mail address of
8222         ;; the parent article.
8223         (when (setq to-address (or (gnus-fetch-field "reply-to")
8224                                    (gnus-fetch-field "from")))
8225           (setq params (append
8226                         (list (cons 'to-address
8227                                     (funcall gnus-decode-encoded-word-function
8228                                              to-address))))))
8229         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8230         (insert-buffer-substring gnus-original-article-buffer)
8231         ;; Remove lines that may lead nndoc to misinterpret the
8232         ;; document type.
8233         (narrow-to-region
8234          (goto-char (point-min))
8235          (or (search-forward "\n\n" nil t) (point)))
8236         (goto-char (point-min))
8237         (delete-matching-lines "^Path:\\|^From ")
8238         (widen))
8239       (unwind-protect
8240           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8241                     (gnus-newsgroup-ephemeral-ignored-charsets
8242                      gnus-newsgroup-ignored-charsets))
8243                 (gnus-group-read-ephemeral-group
8244                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8245                               (nndoc-article-type
8246                                ,(if force 'mbox 'guess)))
8247                  t nil nil nil
8248                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8249                                                         "ADAPT")))))
8250               ;; Make all postings to this group go to the parent group.
8251               (nconc (gnus-info-params (gnus-get-info name))
8252                      params)
8253             ;; Couldn't select this doc group.
8254             (switch-to-buffer buf)
8255             (gnus-set-global-variables)
8256             (gnus-configure-windows 'summary)
8257             (gnus-message 3 "Article couldn't be entered?"))
8258         (kill-buffer dig)))))
8259
8260 (defun gnus-summary-read-document (n)
8261   "Open a new group based on the current article(s).
8262 This will allow you to read digests and other similar
8263 documents as newsgroups.
8264 Obeys the standard process/prefix convention."
8265   (interactive "P")
8266   (let* ((articles (gnus-summary-work-articles n))
8267          (ogroup gnus-newsgroup-name)
8268          (params (append (gnus-info-params (gnus-get-info ogroup))
8269                          (list (cons 'to-group ogroup))))
8270          article group egroup groups vgroup)
8271     (while (setq article (pop articles))
8272       (setq group (format "%s-%d" gnus-newsgroup-name article))
8273       (gnus-summary-remove-process-mark article)
8274       (when (gnus-summary-display-article article)
8275         (save-excursion
8276           (with-temp-buffer
8277             (insert-buffer-substring gnus-original-article-buffer)
8278             ;; Remove some headers that may lead nndoc to make
8279             ;; the wrong guess.
8280             (message-narrow-to-head)
8281             (goto-char (point-min))
8282             (delete-matching-lines "^Path:\\|^From ")
8283             (widen)
8284             (if (setq egroup
8285                       (gnus-group-read-ephemeral-group
8286                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8287                                      (nndoc-article-type guess))
8288                        t nil t))
8289                 (progn
8290             ;; Make all postings to this group go to the parent group.
8291                   (nconc (gnus-info-params (gnus-get-info egroup))
8292                          params)
8293                   (push egroup groups))
8294               ;; Couldn't select this doc group.
8295               (gnus-error 3 "Article couldn't be entered"))))))
8296     ;; Now we have selected all the documents.
8297     (cond
8298      ((not groups)
8299       (error "None of the articles could be interpreted as documents"))
8300      ((gnus-group-read-ephemeral-group
8301        (setq vgroup (format
8302                      "nnvirtual:%s-%s" gnus-newsgroup-name
8303                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8304        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8305        t
8306        (cons (current-buffer) 'summary)))
8307      (t
8308       (error "Couldn't select virtual nndoc group")))))
8309
8310 (defun gnus-summary-isearch-article (&optional regexp-p)
8311   "Do incremental search forward on the current article.
8312 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8313   (interactive "P")
8314   (gnus-summary-select-article)
8315   (gnus-configure-windows 'article)
8316   (gnus-eval-in-buffer-window gnus-article-buffer
8317     (save-restriction
8318       (widen)
8319       (isearch-forward regexp-p))))
8320
8321 (defun gnus-summary-search-article-forward (regexp &optional backward)
8322   "Search for an article containing REGEXP forward.
8323 If BACKWARD, search backward instead."
8324   (interactive
8325    (list (read-string
8326           (format "Search article %s (regexp%s): "
8327                   (if current-prefix-arg "backward" "forward")
8328                   (if gnus-last-search-regexp
8329                       (concat ", default " gnus-last-search-regexp)
8330                     "")))
8331          current-prefix-arg))
8332   (if (string-equal regexp "")
8333       (setq regexp (or gnus-last-search-regexp ""))
8334     (setq gnus-last-search-regexp regexp)
8335     (setq gnus-article-before-search gnus-current-article))
8336   ;; Intentionally set gnus-last-article.
8337   (setq gnus-last-article gnus-article-before-search)
8338   (let ((gnus-last-article gnus-last-article))
8339     (if (gnus-summary-search-article regexp backward)
8340         (gnus-summary-show-thread)
8341       (error "Search failed: \"%s\"" regexp))))
8342
8343 (defun gnus-summary-search-article-backward (regexp)
8344   "Search for an article containing REGEXP backward."
8345   (interactive
8346    (list (read-string
8347           (format "Search article backward (regexp%s): "
8348                   (if gnus-last-search-regexp
8349                       (concat ", default " gnus-last-search-regexp)
8350                     "")))))
8351   (gnus-summary-search-article-forward regexp 'backward))
8352
8353 (defun gnus-summary-search-article (regexp &optional backward)
8354   "Search for an article containing REGEXP.
8355 Optional argument BACKWARD means do search for backward.
8356 `gnus-select-article-hook' is not called during the search."
8357   ;; We have to require this here to make sure that the following
8358   ;; dynamic binding isn't shadowed by autoloading.
8359   (require 'gnus-async)
8360   (require 'gnus-art)
8361   (let ((gnus-select-article-hook nil)  ;Disable hook.
8362         (gnus-article-prepare-hook nil)
8363         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8364         (gnus-use-article-prefetch nil)
8365         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8366         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8367         (gnus-visual nil)
8368         (gnus-keep-backlog nil)
8369         (gnus-break-pages nil)
8370         (gnus-summary-display-arrow nil)
8371         (gnus-updated-mode-lines nil)
8372         (gnus-auto-center-summary nil)
8373         (sum (current-buffer))
8374         (gnus-display-mime-function nil)
8375         (found nil)
8376         point)
8377     (gnus-save-hidden-threads
8378       (gnus-summary-select-article)
8379       (set-buffer gnus-article-buffer)
8380       (goto-char (window-point (get-buffer-window (current-buffer))))
8381       (when backward
8382         (forward-line -1))
8383       (while (not found)
8384         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8385         (if (if backward
8386                 (re-search-backward regexp nil t)
8387               (re-search-forward regexp nil t))
8388             ;; We found the regexp.
8389             (progn
8390               (setq found 'found)
8391               (beginning-of-line)
8392               (set-window-start
8393                (get-buffer-window (current-buffer))
8394                (point))
8395               (forward-line 1)
8396               (set-window-point
8397                (get-buffer-window (current-buffer))
8398                (point))
8399               (set-buffer sum)
8400               (setq point (point)))
8401           ;; We didn't find it, so we go to the next article.
8402           (set-buffer sum)
8403           (setq found 'not)
8404           (while (eq found 'not)
8405             (if (not (if backward (gnus-summary-find-prev)
8406                        (gnus-summary-find-next)))
8407                 ;; No more articles.
8408                 (setq found t)
8409               ;; Select the next article and adjust point.
8410               (unless (gnus-summary-article-sparse-p
8411                        (gnus-summary-article-number))
8412                 (setq found nil)
8413                 (gnus-summary-select-article)
8414                 (set-buffer gnus-article-buffer)
8415                 (widen)
8416                 (goto-char (if backward (point-max) (point-min))))))))
8417       (gnus-message 7 ""))
8418     ;; Return whether we found the regexp.
8419     (when (eq found 'found)
8420       (goto-char point)
8421       (gnus-summary-show-thread)
8422       (gnus-summary-goto-subject gnus-current-article)
8423       (gnus-summary-position-point)
8424       t)))
8425
8426 (defun gnus-find-matching-articles (header regexp)
8427   "Return a list of all articles that match REGEXP on HEADER.
8428 This search includes all articles in the current group that Gnus has
8429 fetched headers for, whether they are displayed or not."
8430   (let ((articles nil)
8431         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8432         (case-fold-search t))
8433     (dolist (header gnus-newsgroup-headers)
8434       (when (string-match regexp (funcall func header))
8435         (push (mail-header-number header) articles)))
8436     (nreverse articles)))
8437
8438 (defun gnus-summary-find-matching (header regexp &optional backward unread
8439                                           not-case-fold not-matching)
8440   "Return a list of all articles that match REGEXP on HEADER.
8441 The search stars on the current article and goes forwards unless
8442 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8443 If UNREAD is non-nil, only unread articles will
8444 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8445 in the comparisons. If NOT-MATCHING, return a list of all articles that
8446 not match REGEXP on HEADER."
8447   (let ((case-fold-search (not not-case-fold))
8448         articles d func)
8449     (if (consp header)
8450         (if (eq (car header) 'extra)
8451             (setq func
8452                   `(lambda (h)
8453                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8454                          "")))
8455           (error "%s is an invalid header" header))
8456       (unless (fboundp (intern (concat "mail-header-" header)))
8457         (error "%s is not a valid header" header))
8458       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8459     (dolist (d (if (eq backward 'all)
8460                    gnus-newsgroup-data
8461                  (gnus-data-find-list
8462                   (gnus-summary-article-number)
8463                   (gnus-data-list backward))))
8464       (when (and (or (not unread)       ; We want all articles...
8465                      (gnus-data-unread-p d)) ; Or just unreads.
8466                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8467                  (if not-matching
8468                      (not (string-match
8469                            regexp
8470                            (funcall func (gnus-data-header d))))
8471                    (string-match regexp
8472                                  (funcall func (gnus-data-header d)))))
8473         (push (gnus-data-number d) articles))) ; Success!
8474     (nreverse articles)))
8475
8476 (defun gnus-summary-execute-command (header regexp command &optional backward)
8477   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8478 If HEADER is an empty string (or nil), the match is done on the entire
8479 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8480   (interactive
8481    (list (let ((completion-ignore-case t))
8482            (completing-read
8483             "Header name: "
8484             (mapcar (lambda (header) (list (format "%s" header)))
8485                     (append
8486                      '("Number" "Subject" "From" "Lines" "Date"
8487                        "Message-ID" "Xref" "References" "Body")
8488                      gnus-extra-headers))
8489             nil 'require-match))
8490          (read-string "Regexp: ")
8491          (read-key-sequence "Command: ")
8492          current-prefix-arg))
8493   (when (equal header "Body")
8494     (setq header ""))
8495   ;; Hidden thread subtrees must be searched as well.
8496   (gnus-summary-show-all-threads)
8497   ;; We don't want to change current point nor window configuration.
8498   (save-excursion
8499     (save-window-excursion
8500       (let (gnus-visual
8501             gnus-treat-strip-trailing-blank-lines
8502             gnus-treat-strip-leading-blank-lines
8503             gnus-treat-strip-multiple-blank-lines
8504             gnus-treat-hide-boring-headers
8505             gnus-treat-fold-newsgroups
8506             gnus-article-prepare-hook)
8507         (gnus-message 6 "Executing %s..." (key-description command))
8508         ;; We'd like to execute COMMAND interactively so as to give arguments.
8509         (gnus-execute header regexp
8510                       `(call-interactively ',(key-binding command))
8511                       backward)
8512         (gnus-message 6 "Executing %s...done" (key-description command))))))
8513
8514 (defun gnus-summary-beginning-of-article ()
8515   "Scroll the article back to the beginning."
8516   (interactive)
8517   (gnus-summary-select-article)
8518   (gnus-configure-windows 'article)
8519   (gnus-eval-in-buffer-window gnus-article-buffer
8520     (widen)
8521     (goto-char (point-min))
8522     (when gnus-break-pages
8523       (gnus-narrow-to-page))))
8524
8525 (defun gnus-summary-end-of-article ()
8526   "Scroll to the end of the article."
8527   (interactive)
8528   (gnus-summary-select-article)
8529   (gnus-configure-windows 'article)
8530   (gnus-eval-in-buffer-window gnus-article-buffer
8531     (widen)
8532     (goto-char (point-max))
8533     (recenter -3)
8534     (when gnus-break-pages
8535       (when (re-search-backward page-delimiter nil t)
8536         (narrow-to-region (match-end 0) (point-max)))
8537       (gnus-narrow-to-page))))
8538
8539 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8540   "Truncate to LEN and quote all \"(\"'s in STRING."
8541   (gnus-replace-in-string (if (and len (> (length string) len))
8542                               (substring string 0 len)
8543                             string)
8544                           "[()]" "\\\\\\&"))
8545
8546 (defun gnus-summary-print-article (&optional filename n)
8547   "Generate and print a PostScript image of the process-marked (mail) articles.
8548
8549 If used interactively, print the current article if none are
8550 process-marked.  With prefix arg, prompt the user for the name of the
8551 file to save in.
8552
8553 When used from Lisp, accept two optional args FILENAME and N.  N means
8554 to print the next N articles.  If N is negative, print the N previous
8555 articles.  If N is nil and articles have been marked with the process
8556 mark, print these instead.
8557
8558 If the optional first argument FILENAME is nil, send the image to the
8559 printer.  If FILENAME is a string, save the PostScript image in a file with
8560 that name.  If FILENAME is a number, prompt the user for the name of the file
8561 to save in."
8562   (interactive (list (ps-print-preprint current-prefix-arg)))
8563   (dolist (article (gnus-summary-work-articles n))
8564     (gnus-summary-select-article nil nil 'pseudo article)
8565     (gnus-eval-in-buffer-window gnus-article-buffer
8566       (gnus-print-buffer))
8567     (gnus-summary-remove-process-mark article))
8568   (ps-despool filename))
8569
8570 (defun gnus-print-buffer ()
8571   (let ((buffer (generate-new-buffer " *print*")))
8572     (unwind-protect
8573         (progn
8574           (copy-to-buffer buffer (point-min) (point-max))
8575           (set-buffer buffer)
8576           (gnus-remove-text-with-property 'gnus-decoration)
8577           (when (gnus-visual-p 'article-highlight 'highlight)
8578             ;; Copy-to-buffer doesn't copy overlay.  So redo
8579             ;; highlight.
8580             (let ((gnus-article-buffer buffer))
8581               (gnus-article-highlight-citation t)
8582               (gnus-article-highlight-signature)
8583               (gnus-article-emphasize)
8584               (gnus-article-delete-invisible-text)))
8585           (let ((ps-left-header
8586                  (list
8587                   (concat "("
8588                           (gnus-summary-print-truncate-and-quote
8589                            (mail-header-subject gnus-current-headers)
8590                            66) ")")
8591                   (concat "("
8592                           (gnus-summary-print-truncate-and-quote
8593                            (mail-header-from gnus-current-headers)
8594                            45) ")")))
8595                 (ps-right-header
8596                  (list
8597                   "/pagenumberstring load"
8598                   (concat "("
8599                           (mail-header-date gnus-current-headers) ")"))))
8600             (gnus-run-hooks 'gnus-ps-print-hook)
8601             (save-excursion
8602               (if window-system
8603                   (ps-spool-buffer-with-faces)
8604                 (ps-spool-buffer)))))
8605       (kill-buffer buffer))))
8606
8607 (defun gnus-summary-show-article (&optional arg)
8608   "Force redisplaying of the current article.
8609 If ARG (the prefix) is a number, show the article with the charset
8610 defined in `gnus-summary-show-article-charset-alist', or the charset
8611 input.
8612 If ARG (the prefix) is non-nil and not a number, show the raw article
8613 without any article massaging functions being run.  Normally, the key
8614 strokes are `C-u g'."
8615   (interactive "P")
8616   (cond
8617    ((numberp arg)
8618     (gnus-summary-show-article t)
8619     (let ((gnus-newsgroup-charset
8620            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8621                (mm-read-coding-system
8622                 "View as charset: " ;; actually it is coding system.
8623                 (save-excursion
8624                   (set-buffer gnus-article-buffer)
8625                   (mm-detect-coding-region (point) (point-max))))))
8626           (gnus-newsgroup-ignored-charsets 'gnus-all))
8627       (gnus-summary-select-article nil 'force)
8628       (let ((deps gnus-newsgroup-dependencies)
8629             head header lines)
8630         (save-excursion
8631           (set-buffer gnus-original-article-buffer)
8632           (save-restriction
8633             (message-narrow-to-head)
8634             (setq head (buffer-string))
8635             (goto-char (point-min))
8636             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8637               (goto-char (point-max))
8638               (widen)
8639               (setq lines (1- (count-lines (point) (point-max))))))
8640           (with-temp-buffer
8641             (insert (format "211 %d Article retrieved.\n"
8642                             (cdr gnus-article-current)))
8643             (insert head)
8644             (if lines (insert (format "Lines: %d\n" lines)))
8645             (insert ".\n")
8646             (let ((nntp-server-buffer (current-buffer)))
8647               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8648         (gnus-data-set-header
8649          (gnus-data-find (cdr gnus-article-current))
8650          header)
8651         (gnus-summary-update-article-line
8652          (cdr gnus-article-current) header)
8653         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8654           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8655    ((not arg)
8656     ;; Select the article the normal way.
8657     (gnus-summary-select-article nil 'force))
8658    (t
8659     ;; We have to require this here to make sure that the following
8660     ;; dynamic binding isn't shadowed by autoloading.
8661     (require 'gnus-async)
8662     (require 'gnus-art)
8663     ;; Bind the article treatment functions to nil.
8664     (let ((gnus-have-all-headers t)
8665           gnus-article-prepare-hook
8666           gnus-article-decode-hook
8667           gnus-display-mime-function
8668           gnus-break-pages)
8669       ;; Destroy any MIME parts.
8670       (when (gnus-buffer-live-p gnus-article-buffer)
8671         (save-excursion
8672           (set-buffer gnus-article-buffer)
8673           (mm-destroy-parts gnus-article-mime-handles)
8674           ;; Set it to nil for safety reason.
8675           (setq gnus-article-mime-handle-alist nil)
8676           (setq gnus-article-mime-handles nil)))
8677       (gnus-summary-select-article nil 'force))))
8678   (gnus-summary-goto-subject gnus-current-article)
8679   (gnus-summary-position-point))
8680
8681 (defun gnus-summary-show-raw-article ()
8682   "Show the raw article without any article massaging functions being run."
8683   (interactive)
8684   (gnus-summary-show-article t))
8685
8686 (defun gnus-summary-verbose-headers (&optional arg)
8687   "Toggle permanent full header display.
8688 If ARG is a positive number, turn header display on.
8689 If ARG is a negative number, turn header display off."
8690   (interactive "P")
8691   (setq gnus-show-all-headers
8692         (cond ((or (not (numberp arg))
8693                    (zerop arg))
8694                (not gnus-show-all-headers))
8695               ((natnump arg)
8696                t)))
8697   (gnus-summary-show-article))
8698
8699 (defun gnus-summary-toggle-header (&optional arg)
8700   "Show the headers if they are hidden, or hide them if they are shown.
8701 If ARG is a positive number, show the entire header.
8702 If ARG is a negative number, hide the unwanted header lines."
8703   (interactive "P")
8704   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8705                      (get-buffer-window gnus-article-buffer t))))
8706     (with-current-buffer gnus-article-buffer
8707       (widen)
8708       (article-narrow-to-head)
8709       (let* ((buffer-read-only nil)
8710              (inhibit-point-motion-hooks t)
8711              (hidden (if (numberp arg)
8712                          (>= arg 0)
8713                        (gnus-article-hidden-text-p 'headers)))
8714              s e)
8715         (delete-region (point-min) (point-max))
8716         (with-current-buffer gnus-original-article-buffer
8717           (goto-char (setq s (point-min)))
8718           (setq e (if (search-forward "\n\n" nil t)
8719                       (1- (point))
8720                     (point-max))))
8721         (insert-buffer-substring gnus-original-article-buffer s e)
8722         (run-hooks 'gnus-article-decode-hook)
8723         (if hidden
8724             (let ((gnus-treat-hide-headers nil)
8725                   (gnus-treat-hide-boring-headers nil))
8726               (gnus-delete-wash-type 'headers)
8727               (gnus-treat-article 'head))
8728           (gnus-treat-article 'head))
8729         (widen)
8730         (if window
8731             (set-window-start window (goto-char (point-min))))
8732         (if gnus-break-pages
8733             (gnus-narrow-to-page)
8734           (when (gnus-visual-p 'page-marker)
8735             (let ((buffer-read-only nil))
8736               (gnus-remove-text-with-property 'gnus-prev)
8737               (gnus-remove-text-with-property 'gnus-next))))
8738         (gnus-set-mode-line 'article)))))
8739
8740 (defun gnus-summary-show-all-headers ()
8741   "Make all header lines visible."
8742   (interactive)
8743   (gnus-summary-toggle-header 1))
8744
8745 (defun gnus-summary-caesar-message (&optional arg)
8746   "Caesar rotate the current article by 13.
8747 The numerical prefix specifies how many places to rotate each letter
8748 forward."
8749   (interactive "P")
8750   (gnus-summary-select-article)
8751   (let ((mail-header-separator ""))
8752     (gnus-eval-in-buffer-window gnus-article-buffer
8753       (save-restriction
8754         (widen)
8755         (let ((start (window-start))
8756               buffer-read-only)
8757           (message-caesar-buffer-body arg)
8758           (set-window-start (get-buffer-window (current-buffer)) start))))))
8759
8760 (autoload 'unmorse-region "morse"
8761   "Convert morse coded text in region to ordinary ASCII text."
8762   t)
8763
8764 (defun gnus-summary-morse-message (&optional arg)
8765   "Morse decode the current article."
8766   (interactive "P")
8767   (gnus-summary-select-article)
8768   (let ((mail-header-separator ""))
8769     (gnus-eval-in-buffer-window gnus-article-buffer
8770       (save-excursion
8771         (save-restriction
8772           (widen)
8773           (let ((pos (window-start))
8774                 buffer-read-only)
8775             (goto-char (point-min))
8776             (when (message-goto-body)
8777               (gnus-narrow-to-body))
8778             (goto-char (point-min))
8779             (while (re-search-forward "·" (point-max) t)
8780               (replace-match "."))
8781             (unmorse-region (point-min) (point-max))
8782             (widen)
8783             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8784
8785 (defun gnus-summary-stop-page-breaking ()
8786   "Stop page breaking in the current article."
8787   (interactive)
8788   (gnus-summary-select-article)
8789   (gnus-eval-in-buffer-window gnus-article-buffer
8790     (widen)
8791     (when (gnus-visual-p 'page-marker)
8792       (let ((buffer-read-only nil))
8793         (gnus-remove-text-with-property 'gnus-prev)
8794         (gnus-remove-text-with-property 'gnus-next))
8795       (setq gnus-page-broken nil))))
8796
8797 (defun gnus-summary-move-article (&optional n to-newsgroup
8798                                             select-method action)
8799   "Move the current article to a different newsgroup.
8800 If N is a positive number, move the N next articles.
8801 If N is a negative number, move the N previous articles.
8802 If N is nil and any articles have been marked with the process mark,
8803 move those articles instead.
8804 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8805 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8806 re-spool using this method.
8807
8808 When called interactively with TO-NEWSGROUP being nil, the value of
8809 the variable `gnus-move-split-methods' is used for finding a default
8810 for the target newsgroup.
8811
8812 For this function to work, both the current newsgroup and the
8813 newsgroup that you want to move to have to support the `request-move'
8814 and `request-accept' functions.
8815
8816 ACTION can be either `move' (the default), `crosspost' or `copy'."
8817   (interactive "P")
8818   (unless action
8819     (setq action 'move))
8820   ;; Check whether the source group supports the required functions.
8821   (cond ((and (eq action 'move)
8822               (not (gnus-check-backend-function
8823                     'request-move-article gnus-newsgroup-name)))
8824          (error "The current group does not support article moving"))
8825         ((and (eq action 'crosspost)
8826               (not (gnus-check-backend-function
8827                     'request-replace-article gnus-newsgroup-name)))
8828          (error "The current group does not support article editing")))
8829   (let ((articles (gnus-summary-work-articles n))
8830         (prefix (if (gnus-check-backend-function
8831                      'request-move-article gnus-newsgroup-name)
8832                     (gnus-group-real-prefix gnus-newsgroup-name)
8833                   ""))
8834         (names '((move "Move" "Moving")
8835                  (copy "Copy" "Copying")
8836                  (crosspost "Crosspost" "Crossposting")))
8837         (copy-buf (save-excursion
8838                     (nnheader-set-temp-buffer " *copy article*")))
8839         art-group to-method new-xref article to-groups)
8840     (unless (assq action names)
8841       (error "Unknown action %s" action))
8842     ;; Read the newsgroup name.
8843     (when (and (not to-newsgroup)
8844                (not select-method))
8845       (if (and gnus-move-split-methods
8846                (not
8847                 (and (memq gnus-current-article articles)
8848                      (gnus-buffer-live-p gnus-original-article-buffer))))
8849           ;; When `gnus-move-split-methods' is non-nil, we have to
8850           ;; select an article to give `gnus-read-move-group-name' an
8851           ;; opportunity to suggest an appropriate default.  However,
8852           ;; we needn't render or mark the article.
8853           (let ((gnus-display-mime-function nil)
8854                 (gnus-article-prepare-hook nil)
8855                 (gnus-mark-article-hook nil))
8856             (gnus-summary-select-article nil nil nil (car articles))))
8857       (setq to-newsgroup
8858             (gnus-read-move-group-name
8859              (cadr (assq action names))
8860              (symbol-value (intern (format "gnus-current-%s-group" action)))
8861              articles prefix))
8862       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8863     (setq to-method (or select-method
8864                         (gnus-server-to-method
8865                          (gnus-group-method to-newsgroup))))
8866     ;; Check the method we are to move this article to...
8867     (unless (gnus-check-backend-function
8868              'request-accept-article (car to-method))
8869       (error "%s does not support article copying" (car to-method)))
8870     (unless (gnus-check-server to-method)
8871       (error "Can't open server %s" (car to-method)))
8872     (gnus-message 6 "%s to %s: %s..."
8873                   (caddr (assq action names))
8874                   (or (car select-method) to-newsgroup) articles)
8875     (while articles
8876       (setq article (pop articles))
8877       (setq
8878        art-group
8879        (cond
8880         ;; Move the article.
8881         ((eq action 'move)
8882          ;; Remove this article from future suppression.
8883          (gnus-dup-unsuppress-article article)
8884          (gnus-request-move-article
8885           article                       ; Article to move
8886           gnus-newsgroup-name           ; From newsgroup
8887           (nth 1 (gnus-find-method-for-group
8888                   gnus-newsgroup-name)) ; Server
8889           (list 'gnus-request-accept-article
8890                 to-newsgroup (list 'quote select-method)
8891                 (not articles) t)       ; Accept form
8892           (not articles)))              ; Only save nov last time
8893         ;; Copy the article.
8894         ((eq action 'copy)
8895          (save-excursion
8896            (set-buffer copy-buf)
8897            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8898              (gnus-request-accept-article
8899               to-newsgroup select-method (not articles) t))))
8900         ;; Crosspost the article.
8901         ((eq action 'crosspost)
8902          (let ((xref (message-tokenize-header
8903                       (mail-header-xref (gnus-summary-article-header article))
8904                       " ")))
8905            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8906                                   ":" (number-to-string article)))
8907            (unless xref
8908              (setq xref (list (system-name))))
8909            (setq new-xref
8910                  (concat
8911                   (mapconcat 'identity
8912                              (delete "Xref:" (delete new-xref xref))
8913                              " ")
8914                   " " new-xref))
8915            (save-excursion
8916              (set-buffer copy-buf)
8917              ;; First put the article in the destination group.
8918              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8919              (when (consp (setq art-group
8920                                 (gnus-request-accept-article
8921                                  to-newsgroup select-method (not articles))))
8922                (setq new-xref (concat new-xref " " (car art-group)
8923                                       ":"
8924                                       (number-to-string (cdr art-group))))
8925                ;; Now we have the new Xrefs header, so we insert
8926                ;; it and replace the new article.
8927                (nnheader-replace-header "Xref" new-xref)
8928                (gnus-request-replace-article
8929                 (cdr art-group) to-newsgroup (current-buffer))
8930                art-group))))))
8931       (cond
8932        ((not art-group)
8933         (gnus-message 1 "Couldn't %s article %s: %s"
8934                       (cadr (assq action names)) article
8935                       (nnheader-get-report (car to-method))))
8936        ((eq art-group 'junk)
8937         (when (eq action 'move)
8938           (gnus-summary-mark-article article gnus-canceled-mark)
8939           (gnus-message 4 "Deleted article %s" article)
8940           ;; run the delete hook
8941           (run-hook-with-args 'gnus-summary-article-delete-hook
8942                               action
8943                               (gnus-data-header
8944                                (assoc article (gnus-data-list nil)))
8945                               gnus-newsgroup-name nil
8946                               select-method)))
8947        (t
8948         (let* ((pto-group (gnus-group-prefixed-name
8949                            (car art-group) to-method))
8950                (info (gnus-get-info pto-group))
8951                (to-group (gnus-info-group info))
8952                to-marks)
8953           ;; Update the group that has been moved to.
8954           (when (and info
8955                      (memq action '(move copy)))
8956             (unless (member to-group to-groups)
8957               (push to-group to-groups))
8958
8959             (unless (memq article gnus-newsgroup-unreads)
8960               (push 'read to-marks)
8961               (gnus-info-set-read
8962                info (gnus-add-to-range (gnus-info-read info)
8963                                        (list (cdr art-group)))))
8964
8965             ;; See whether the article is to be put in the cache.
8966             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8967                              gnus-article-mark-lists
8968                            (delete '(expirable . expire)
8969                                    (copy-sequence gnus-article-mark-lists))))
8970                   (to-article (cdr art-group)))
8971
8972               ;; Enter the article into the cache in the new group,
8973               ;; if that is required.
8974               (when gnus-use-cache
8975                 (gnus-cache-possibly-enter-article
8976                  to-group to-article
8977                  (memq article gnus-newsgroup-marked)
8978                  (memq article gnus-newsgroup-dormant)
8979                  (memq article gnus-newsgroup-unreads)))
8980
8981               (when gnus-preserve-marks
8982                 ;; Copy any marks over to the new group.
8983                 (when (and (equal to-group gnus-newsgroup-name)
8984                            (not (memq article gnus-newsgroup-unreads)))
8985                   ;; Mark this article as read in this group.
8986                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8987                   (setcdr (gnus-active to-group) to-article)
8988                   (setcdr gnus-newsgroup-active to-article))
8989
8990                 (while marks
8991                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8992                     (when (memq article (symbol-value
8993                                          (intern (format "gnus-newsgroup-%s"
8994                                                          (caar marks)))))
8995                       (push (cdar marks) to-marks)
8996                       ;; If the other group is the same as this group,
8997                       ;; then we have to add the mark to the list.
8998                       (when (equal to-group gnus-newsgroup-name)
8999                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9000                              (cons to-article
9001                                    (symbol-value
9002                                     (intern (format "gnus-newsgroup-%s"
9003                                                     (caar marks)))))))
9004                       ;; Copy the marks to other group.
9005                       (gnus-add-marked-articles
9006                        to-group (cdar marks) (list to-article) info)))
9007                   (setq marks (cdr marks)))
9008
9009                 (gnus-request-set-mark
9010                  to-group (list (list (list to-article) 'add to-marks))))
9011
9012               (gnus-dribble-enter
9013                (concat "(gnus-group-set-info '"
9014                        (gnus-prin1-to-string (gnus-get-info to-group))
9015                        ")"))))
9016
9017           ;; Update the Xref header in this article to point to
9018           ;; the new crossposted article we have just created.
9019           (when (eq action 'crosspost)
9020             (save-excursion
9021               (set-buffer copy-buf)
9022               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9023               (nnheader-replace-header "Xref" new-xref)
9024               (gnus-request-replace-article
9025                article gnus-newsgroup-name (current-buffer))))
9026
9027           ;; run the move/copy/crosspost/respool hook
9028           (run-hook-with-args 'gnus-summary-article-move-hook
9029                               action
9030                               (gnus-data-header
9031                                (assoc article (gnus-data-list nil)))
9032                               gnus-newsgroup-name
9033                               to-newsgroup
9034                               select-method))
9035
9036         ;;;!!!Why is this necessary?
9037         (set-buffer gnus-summary-buffer)
9038         
9039         (gnus-summary-goto-subject article)
9040         (when (eq action 'move)
9041           (gnus-summary-mark-article article gnus-canceled-mark))))
9042       (gnus-summary-remove-process-mark article))
9043     ;; Re-activate all groups that have been moved to.
9044     (save-excursion
9045       (set-buffer gnus-group-buffer)
9046       (let ((gnus-group-marked to-groups))
9047         (gnus-group-get-new-news-this-group nil t)))
9048
9049     (gnus-kill-buffer copy-buf)
9050     (gnus-summary-position-point)
9051     (gnus-set-mode-line 'summary)))
9052
9053 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9054   "Move the current article to a different newsgroup.
9055 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9056 When called interactively, if TO-NEWSGROUP is nil, use the value of
9057 the variable `gnus-move-split-methods' for finding a default target
9058 newsgroup.
9059 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9060 re-spool using this method."
9061   (interactive "P")
9062   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9063
9064 (defun gnus-summary-crosspost-article (&optional n)
9065   "Crosspost the current article to some other group."
9066   (interactive "P")
9067   (gnus-summary-move-article n nil nil 'crosspost))
9068
9069 (defcustom gnus-summary-respool-default-method nil
9070   "Default method type for respooling an article.
9071 If nil, use to the current newsgroup method."
9072   :type 'symbol
9073   :group 'gnus-summary-mail)
9074
9075 (defcustom gnus-summary-display-while-building nil
9076   "If non-nil, show and update the summary buffer as it's being built.
9077 If the value is t, update the buffer after every line is inserted.  If
9078 the value is an integer (N), update the display every N lines."
9079   :group 'gnus-thread
9080   :type '(choice (const :tag "off" nil)
9081                  number
9082                  (const :tag "frequently" t)))
9083
9084 (defun gnus-summary-respool-article (&optional n method)
9085   "Respool the current article.
9086 The article will be squeezed through the mail spooling process again,
9087 which means that it will be put in some mail newsgroup or other
9088 depending on `nnmail-split-methods'.
9089 If N is a positive number, respool the N next articles.
9090 If N is a negative number, respool the N previous articles.
9091 If N is nil and any articles have been marked with the process mark,
9092 respool those articles instead.
9093
9094 Respooling can be done both from mail groups and \"real\" newsgroups.
9095 In the former case, the articles in question will be moved from the
9096 current group into whatever groups they are destined to.  In the
9097 latter case, they will be copied into the relevant groups."
9098   (interactive
9099    (list current-prefix-arg
9100          (let* ((methods (gnus-methods-using 'respool))
9101                 (methname
9102                  (symbol-name (or gnus-summary-respool-default-method
9103                                   (car (gnus-find-method-for-group
9104                                         gnus-newsgroup-name)))))
9105                 (method
9106                  (gnus-completing-read-with-default
9107                   methname "What backend do you want to use when respooling?"
9108                   methods nil t nil 'gnus-mail-method-history))
9109                 ms)
9110            (cond
9111             ((zerop (length (setq ms (gnus-servers-using-backend
9112                                       (intern method)))))
9113              (list (intern method) ""))
9114             ((= 1 (length ms))
9115              (car ms))
9116             (t
9117              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9118                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9119                            ms-alist))))))))
9120   (unless method
9121     (error "No method given for respooling"))
9122   (if (assoc (symbol-name
9123               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9124              (gnus-methods-using 'respool))
9125       (gnus-summary-move-article n nil method)
9126     (gnus-summary-copy-article n nil method)))
9127
9128 (defun gnus-summary-import-article (file &optional edit)
9129   "Import an arbitrary file into a mail newsgroup."
9130   (interactive "fImport file: \nP")
9131   (let ((group gnus-newsgroup-name)
9132         (now (current-time))
9133         atts lines group-art)
9134     (unless (gnus-check-backend-function 'request-accept-article group)
9135       (error "%s does not support article importing" group))
9136     (or (file-readable-p file)
9137         (not (file-regular-p file))
9138         (error "Can't read %s" file))
9139     (save-excursion
9140       (set-buffer (gnus-get-buffer-create " *import file*"))
9141       (erase-buffer)
9142       (nnheader-insert-file-contents file)
9143       (goto-char (point-min))
9144       (if (nnheader-article-p)
9145           (save-restriction
9146             (goto-char (point-min))
9147             (search-forward "\n\n" nil t)
9148             (narrow-to-region (point-min) (1- (point)))
9149             (goto-char (point-min))
9150             (unless (re-search-forward "^date:" nil t)
9151               (goto-char (point-max))
9152               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9153        ;; This doesn't look like an article, so we fudge some headers.
9154         (setq atts (file-attributes file)
9155               lines (count-lines (point-min) (point-max)))
9156         (insert "From: " (read-string "From: ") "\n"
9157                 "Subject: " (read-string "Subject: ") "\n"
9158                 "Date: " (message-make-date (nth 5 atts)) "\n"
9159                 "Message-ID: " (message-make-message-id) "\n"
9160                 "Lines: " (int-to-string lines) "\n"
9161                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9162       (setq group-art (gnus-request-accept-article group nil t))
9163       (kill-buffer (current-buffer)))
9164     (setq gnus-newsgroup-active (gnus-activate-group group))
9165     (forward-line 1)
9166     (gnus-summary-goto-article (cdr group-art) nil t)
9167     (when edit
9168       (gnus-summary-edit-article))))
9169
9170 (defun gnus-summary-create-article ()
9171   "Create an article in a mail newsgroup."
9172   (interactive)
9173   (let ((group gnus-newsgroup-name)
9174         (now (current-time))
9175         group-art)
9176     (unless (gnus-check-backend-function 'request-accept-article group)
9177       (error "%s does not support article importing" group))
9178     (save-excursion
9179       (set-buffer (gnus-get-buffer-create " *import file*"))
9180       (erase-buffer)
9181       (goto-char (point-min))
9182       ;; This doesn't look like an article, so we fudge some headers.
9183       (insert "From: " (read-string "From: ") "\n"
9184               "Subject: " (read-string "Subject: ") "\n"
9185               "Date: " (message-make-date now) "\n"
9186               "Message-ID: " (message-make-message-id) "\n")
9187       (setq group-art (gnus-request-accept-article group nil t))
9188       (kill-buffer (current-buffer)))
9189     (setq gnus-newsgroup-active (gnus-activate-group group))
9190     (forward-line 1)
9191     (gnus-summary-goto-article (cdr group-art) nil t)
9192     (gnus-summary-edit-article)))
9193
9194 (defun gnus-summary-article-posted-p ()
9195   "Say whether the current (mail) article is available from news as well.
9196 This will be the case if the article has both been mailed and posted."
9197   (interactive)
9198   (let ((id (mail-header-references (gnus-summary-article-header)))
9199         (gnus-override-method (car (gnus-refer-article-methods))))
9200     (if (gnus-request-head id "")
9201         (gnus-message 2 "The current message was found on %s"
9202                       gnus-override-method)
9203       (gnus-message 2 "The current message couldn't be found on %s"
9204                     gnus-override-method)
9205       nil)))
9206
9207 (defun gnus-summary-expire-articles (&optional now)
9208   "Expire all articles that are marked as expirable in the current group."
9209   (interactive)
9210   (when (and (not gnus-group-is-exiting-without-update-p)
9211              (gnus-check-backend-function
9212               'request-expire-articles gnus-newsgroup-name))
9213     ;; This backend supports expiry.
9214     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9215            (expirable (if total
9216                           (progn
9217                             ;; We need to update the info for
9218                             ;; this group for `gnus-list-of-read-articles'
9219                             ;; to give us the right answer.
9220                             (gnus-run-hooks 'gnus-exit-group-hook)
9221                             (gnus-summary-update-info)
9222                             (gnus-list-of-read-articles gnus-newsgroup-name))
9223                         (setq gnus-newsgroup-expirable
9224                               (sort gnus-newsgroup-expirable '<))))
9225            (expiry-wait (if now 'immediate
9226                           (gnus-group-find-parameter
9227                            gnus-newsgroup-name 'expiry-wait)))
9228            (nnmail-expiry-target
9229             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9230                 nnmail-expiry-target))
9231            es)
9232       (when expirable
9233         ;; There are expirable articles in this group, so we run them
9234         ;; through the expiry process.
9235         (gnus-message 6 "Expiring articles...")
9236         (unless (gnus-check-group gnus-newsgroup-name)
9237           (error "Can't open server for %s" gnus-newsgroup-name))
9238         ;; The list of articles that weren't expired is returned.
9239         (save-excursion
9240           (if expiry-wait
9241               (let ((nnmail-expiry-wait-function nil)
9242                     (nnmail-expiry-wait expiry-wait))
9243                 (setq es (gnus-request-expire-articles
9244                           expirable gnus-newsgroup-name)))
9245             (setq es (gnus-request-expire-articles
9246                       expirable gnus-newsgroup-name)))
9247           (unless total
9248             (setq gnus-newsgroup-expirable es))
9249           ;; We go through the old list of expirable, and mark all
9250           ;; really expired articles as nonexistent.
9251           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9252             (let ((gnus-use-cache nil))
9253               (dolist (article expirable)
9254                 (when (and (not (memq article es))
9255                            (gnus-data-find article))
9256                   (gnus-summary-mark-article article gnus-canceled-mark)
9257                   (run-hook-with-args 'gnus-summary-article-expire-hook
9258                                       'delete
9259                                       (gnus-data-header
9260                                        (assoc article (gnus-data-list nil)))
9261                                       gnus-newsgroup-name
9262                                       nil
9263                                       nil))))))
9264         (gnus-message 6 "Expiring articles...done")))))
9265
9266 (defun gnus-summary-expire-articles-now ()
9267   "Expunge all expirable articles in the current group.
9268 This means that *all* articles that are marked as expirable will be
9269 deleted forever, right now."
9270   (interactive)
9271   (or gnus-expert-user
9272       (gnus-yes-or-no-p
9273        "Are you really, really, really sure you want to delete all these messages? ")
9274       (error "Phew!"))
9275   (gnus-summary-expire-articles t))
9276
9277 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9278 (defun gnus-summary-delete-article (&optional n)
9279   "Delete the N next (mail) articles.
9280 This command actually deletes articles.  This is not a marking
9281 command.  The article will disappear forever from your life, never to
9282 return.
9283
9284 If N is negative, delete backwards.
9285 If N is nil and articles have been marked with the process mark,
9286 delete these instead.
9287
9288 If `gnus-novice-user' is non-nil you will be asked for
9289 confirmation before the articles are deleted."
9290   (interactive "P")
9291   (unless (gnus-check-backend-function 'request-expire-articles
9292                                        gnus-newsgroup-name)
9293     (error "The current newsgroup does not support article deletion"))
9294   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9295     (error "Couldn't open server"))
9296   ;; Compute the list of articles to delete.
9297   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9298         (nnmail-expiry-target 'delete)
9299         not-deleted)
9300     (if (and gnus-novice-user
9301              (not (gnus-yes-or-no-p
9302                    (format "Do you really want to delete %s forever? "
9303                            (if (> (length articles) 1)
9304                                (format "these %s articles" (length articles))
9305                              "this article")))))
9306         ()
9307       ;; Delete the articles.
9308       (setq not-deleted (gnus-request-expire-articles
9309                          articles gnus-newsgroup-name 'force))
9310       (while articles
9311         (gnus-summary-remove-process-mark (car articles))
9312         ;; The backend might not have been able to delete the article
9313         ;; after all.
9314         (unless (memq (car articles) not-deleted)
9315           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9316         (let* ((article (car articles))
9317                (id (mail-header-id (gnus-data-header
9318                                     (assoc article (gnus-data-list nil))))))
9319           (run-hook-with-args 'gnus-summary-article-delete-hook
9320                               'delete id gnus-newsgroup-name nil
9321                               nil))
9322         (setq articles (cdr articles)))
9323       (when not-deleted
9324         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9325     (gnus-summary-position-point)
9326     (gnus-set-mode-line 'summary)
9327     not-deleted))
9328
9329 (defun gnus-summary-edit-article (&optional arg)
9330   "Edit the current article.
9331 This will have permanent effect only in mail groups.
9332 If ARG is nil, edit the decoded articles.
9333 If ARG is 1, edit the raw articles.
9334 If ARG is 2, edit the raw articles even in read-only groups.
9335 If ARG is 3, edit the articles with the current handles.
9336 Otherwise, allow editing of articles even in read-only
9337 groups."
9338   (interactive "P")
9339   (let (force raw current-handles)
9340     (cond
9341      ((null arg))
9342      ((eq arg 1)
9343       (setq raw t))
9344      ((eq arg 2)
9345       (setq raw t
9346             force t))
9347      ((eq arg 3)
9348       (setq current-handles
9349             (and (gnus-buffer-live-p gnus-article-buffer)
9350                  (with-current-buffer gnus-article-buffer
9351                    (prog1
9352                        gnus-article-mime-handles
9353                      (setq gnus-article-mime-handles nil))))))
9354      (t
9355       (setq force t)))
9356     (when (and raw (not force)
9357                (member gnus-newsgroup-name '("nndraft:delayed"
9358                                              "nndraft:drafts"
9359                                              "nndraft:queue")))
9360       (error "Can't edit the raw article in group %s"
9361              gnus-newsgroup-name))
9362     (save-excursion
9363       (set-buffer gnus-summary-buffer)
9364       (let ((mail-parse-charset gnus-newsgroup-charset)
9365             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9366         (gnus-set-global-variables)
9367         (when (and (not force)
9368                    (gnus-group-read-only-p))
9369           (error "The current newsgroup does not support article editing"))
9370         (gnus-summary-show-article t)
9371         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9372           (with-current-buffer gnus-article-buffer
9373             (mm-enable-multibyte)))
9374         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9375             (setq raw t))
9376         (gnus-article-edit-article
9377          (if raw 'ignore
9378            `(lambda ()
9379               (let ((mbl mml-buffer-list))
9380                 (setq mml-buffer-list nil)
9381                 (mime-to-mml ,'current-handles)
9382                 (let ((mbl1 mml-buffer-list))
9383                   (setq mml-buffer-list mbl)
9384                   (set (make-local-variable 'mml-buffer-list) mbl1))
9385                 (gnus-make-local-hook 'kill-buffer-hook)
9386                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9387          `(lambda (no-highlight)
9388             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9389                   (message-options message-options)
9390                   (message-options-set-recipient)
9391                   (mail-parse-ignored-charsets
9392                    ',gnus-newsgroup-ignored-charsets))
9393               ,(if (not raw) '(progn
9394                                 (mml-to-mime)
9395                                 (mml-destroy-buffers)
9396                                 (remove-hook 'kill-buffer-hook
9397                                              'mml-destroy-buffers t)
9398                                 (kill-local-variable 'mml-buffer-list)))
9399               (gnus-summary-edit-article-done
9400                ,(or (mail-header-references gnus-current-headers) "")
9401                ,(gnus-group-read-only-p)
9402                ,gnus-summary-buffer no-highlight))))))))
9403
9404 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9405
9406 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9407                                                  no-highlight)
9408   "Make edits to the current article permanent."
9409   (interactive)
9410   (save-excursion
9411    ;; The buffer restriction contains the entire article if it exists.
9412     (when (article-goto-body)
9413       (let ((lines (count-lines (point) (point-max)))
9414             (length (- (point-max) (point)))
9415             (case-fold-search t)
9416             (body (copy-marker (point))))
9417         (goto-char (point-min))
9418         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9419           (delete-region (match-beginning 1) (match-end 1))
9420           (insert (number-to-string length)))
9421         (goto-char (point-min))
9422         (when (re-search-forward
9423                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9424           (delete-region (match-beginning 1) (match-end 1))
9425           (insert (number-to-string length)))
9426         (goto-char (point-min))
9427         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9428           (delete-region (match-beginning 1) (match-end 1))
9429           (insert (number-to-string lines))))))
9430   ;; Replace the article.
9431   (let ((buf (current-buffer)))
9432     (with-temp-buffer
9433       (insert-buffer-substring buf)
9434
9435       (if (and (not read-only)
9436                (not (gnus-request-replace-article
9437                      (cdr gnus-article-current) (car gnus-article-current)
9438                      (current-buffer) t)))
9439           (error "Couldn't replace article")
9440         ;; Update the summary buffer.
9441         (if (and references
9442                  (equal (message-tokenize-header references " ")
9443                         (message-tokenize-header
9444                          (or (message-fetch-field "references") "") " ")))
9445             ;; We only have to update this line.
9446             (save-excursion
9447               (save-restriction
9448                 (message-narrow-to-head)
9449                 (let ((head (buffer-string))
9450                       header)
9451                   (with-temp-buffer
9452                     (insert (format "211 %d Article retrieved.\n"
9453                                     (cdr gnus-article-current)))
9454                     (insert head)
9455                     (insert ".\n")
9456                     (let ((nntp-server-buffer (current-buffer)))
9457                       (setq header (car (gnus-get-newsgroup-headers
9458                                          nil t))))
9459                     (save-excursion
9460                       (set-buffer gnus-summary-buffer)
9461                       (gnus-data-set-header
9462                        (gnus-data-find (cdr gnus-article-current))
9463                        header)
9464                       (gnus-summary-update-article-line
9465                        (cdr gnus-article-current) header)
9466                       (if (gnus-summary-goto-subject
9467                            (cdr gnus-article-current) nil t)
9468                           (gnus-summary-update-secondary-mark
9469                            (cdr gnus-article-current))))))))
9470           ;; Update threads.
9471           (set-buffer (or buffer gnus-summary-buffer))
9472           (gnus-summary-update-article (cdr gnus-article-current))
9473           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9474               (gnus-summary-update-secondary-mark
9475                (cdr gnus-article-current))))
9476         ;; Prettify the article buffer again.
9477         (unless no-highlight
9478           (save-excursion
9479             (set-buffer gnus-article-buffer)
9480             ;;;!!! Fix this -- article should be rehighlighted.
9481             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9482             (set-buffer gnus-original-article-buffer)
9483             (gnus-request-article
9484              (cdr gnus-article-current)
9485              (car gnus-article-current) (current-buffer))))
9486         ;; Prettify the summary buffer line.
9487         (when (gnus-visual-p 'summary-highlight 'highlight)
9488           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9489
9490 (defun gnus-summary-edit-wash (key)
9491   "Perform editing command KEY in the article buffer."
9492   (interactive
9493    (list
9494     (progn
9495       (message "%s" (concat (this-command-keys) "- "))
9496       (read-char))))
9497   (message "")
9498   (gnus-summary-edit-article)
9499   (execute-kbd-macro (concat (this-command-keys) key))
9500   (gnus-article-edit-done))
9501
9502 ;;; Respooling
9503
9504 (defun gnus-summary-respool-query (&optional silent trace)
9505   "Query where the respool algorithm would put this article."
9506   (interactive)
9507   (let (gnus-mark-article-hook)
9508     (gnus-summary-select-article)
9509     (save-excursion
9510       (set-buffer gnus-original-article-buffer)
9511       (let ((groups (nnmail-article-group 'identity trace)))
9512         (unless silent
9513           (if groups
9514               (message "This message would go to %s"
9515                        (mapconcat 'car groups ", "))
9516             (message "This message would go to no groups"))
9517           groups)))))
9518
9519 (defun gnus-summary-respool-trace ()
9520   "Trace where the respool algorithm would put this article.
9521 Display a buffer showing all fancy splitting patterns which matched."
9522   (interactive)
9523   (gnus-summary-respool-query nil t))
9524
9525 ;; Summary marking commands.
9526
9527 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9528   "Mark articles which has the same subject as read, and then select the next.
9529 If UNMARK is positive, remove any kind of mark.
9530 If UNMARK is negative, tick articles."
9531   (interactive "P")
9532   (when unmark
9533     (setq unmark (prefix-numeric-value unmark)))
9534   (let ((count
9535          (gnus-summary-mark-same-subject
9536           (gnus-summary-article-subject) unmark)))
9537     ;; Select next unread article.  If auto-select-same mode, should
9538     ;; select the first unread article.
9539     (gnus-summary-next-article t (and gnus-auto-select-same
9540                                       (gnus-summary-article-subject)))
9541     (gnus-message 7 "%d article%s marked as %s"
9542                   count (if (= count 1) " is" "s are")
9543                   (if unmark "unread" "read"))))
9544
9545 (defun gnus-summary-kill-same-subject (&optional unmark)
9546   "Mark articles which has the same subject as read.
9547 If UNMARK is positive, remove any kind of mark.
9548 If UNMARK is negative, tick articles."
9549   (interactive "P")
9550   (when unmark
9551     (setq unmark (prefix-numeric-value unmark)))
9552   (let ((count
9553          (gnus-summary-mark-same-subject
9554           (gnus-summary-article-subject) unmark)))
9555     ;; If marked as read, go to next unread subject.
9556     (when (null unmark)
9557       ;; Go to next unread subject.
9558       (gnus-summary-next-subject 1 t))
9559     (gnus-message 7 "%d articles are marked as %s"
9560                   count (if unmark "unread" "read"))))
9561
9562 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9563   "Mark articles with same SUBJECT as read, and return marked number.
9564 If optional argument UNMARK is positive, remove any kinds of marks.
9565 If optional argument UNMARK is negative, mark articles as unread instead."
9566   (let ((count 1))
9567     (save-excursion
9568       (cond
9569        ((null unmark)                   ; Mark as read.
9570         (while (and
9571                 (progn
9572                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9573                   (gnus-summary-show-thread) t)
9574                 (gnus-summary-find-subject subject))
9575           (setq count (1+ count))))
9576        ((> unmark 0)                    ; Tick.
9577         (while (and
9578                 (progn
9579                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9580                   (gnus-summary-show-thread) t)
9581                 (gnus-summary-find-subject subject))
9582           (setq count (1+ count))))
9583        (t                               ; Mark as unread.
9584         (while (and
9585                 (progn
9586                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9587                   (gnus-summary-show-thread) t)
9588                 (gnus-summary-find-subject subject))
9589           (setq count (1+ count)))))
9590       (gnus-set-mode-line 'summary)
9591       ;; Return the number of marked articles.
9592       count)))
9593
9594 (defun gnus-summary-mark-as-processable (n &optional unmark)
9595   "Set the process mark on the next N articles.
9596 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9597 the process mark instead.  The difference between N and the actual
9598 number of articles marked is returned."
9599   (interactive "P")
9600   (if (and (null n) (gnus-region-active-p))
9601       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9602     (setq n (prefix-numeric-value n))
9603     (let ((backward (< n 0))
9604           (n (abs n)))
9605       (while (and
9606               (> n 0)
9607               (if unmark
9608                   (gnus-summary-remove-process-mark
9609                    (gnus-summary-article-number))
9610                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9611               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9612         (setq n (1- n)))
9613       (when (/= 0 n)
9614         (gnus-message 7 "No more articles"))
9615       (gnus-summary-recenter)
9616       (gnus-summary-position-point)
9617       n)))
9618
9619 (defun gnus-summary-unmark-as-processable (n)
9620   "Remove the process mark from the next N articles.
9621 If N is negative, unmark backward instead.  The difference between N and
9622 the actual number of articles unmarked is returned."
9623   (interactive "P")
9624   (gnus-summary-mark-as-processable n t))
9625
9626 (defun gnus-summary-unmark-all-processable ()
9627   "Remove the process mark from all articles."
9628   (interactive)
9629   (save-excursion
9630     (while gnus-newsgroup-processable
9631       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9632   (gnus-summary-position-point))
9633
9634 (defun gnus-summary-add-mark (article type)
9635   "Mark ARTICLE with a mark of TYPE."
9636   (let ((vtype (car (assq type gnus-article-mark-lists)))
9637         var)
9638     (if (not vtype)
9639         (error "No such mark type: %s" type)
9640       (setq var (intern (format "gnus-newsgroup-%s" type)))
9641       (set var (cons article (symbol-value var)))
9642       (if (memq type '(processable cached replied forwarded recent saved))
9643           (gnus-summary-update-secondary-mark article)
9644         ;;; !!! This is bogus.  We should find out what primary
9645         ;;; !!! mark we want to set.
9646         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9647
9648 (defun gnus-summary-mark-as-expirable (n)
9649   "Mark N articles forward as expirable.
9650 If N is negative, mark backward instead.  The difference between N and
9651 the actual number of articles marked is returned."
9652   (interactive "p")
9653   (gnus-summary-mark-forward n gnus-expirable-mark))
9654
9655 (defun gnus-summary-mark-as-spam (n)
9656   "Mark N articles forward as spam.
9657 If N is negative, mark backward instead.  The difference between N and
9658 the actual number of articles marked is returned."
9659   (interactive "p")
9660   (gnus-summary-mark-forward n gnus-spam-mark))
9661
9662 (defun gnus-summary-mark-article-as-replied (article)
9663   "Mark ARTICLE as replied to and update the summary line.
9664 ARTICLE can also be a list of articles."
9665   (interactive (list (gnus-summary-article-number)))
9666   (let ((articles (if (listp article) article (list article))))
9667     (dolist (article articles)
9668       (unless (numberp article)
9669         (error "%s is not a number" article))
9670       (push article gnus-newsgroup-replied)
9671       (let ((buffer-read-only nil))
9672         (when (gnus-summary-goto-subject article nil t)
9673           (gnus-summary-update-secondary-mark article))))))
9674
9675 (defun gnus-summary-mark-article-as-forwarded (article)
9676   "Mark ARTICLE as forwarded and update the summary line.
9677 ARTICLE can also be a list of articles."
9678   (let ((articles (if (listp article) article (list article))))
9679     (dolist (article articles)
9680       (push article gnus-newsgroup-forwarded)
9681       (let ((buffer-read-only nil))
9682         (when (gnus-summary-goto-subject article nil t)
9683           (gnus-summary-update-secondary-mark article))))))
9684
9685 (defun gnus-summary-set-bookmark (article)
9686   "Set a bookmark in current article."
9687   (interactive (list (gnus-summary-article-number)))
9688   (when (or (not (get-buffer gnus-article-buffer))
9689             (not gnus-current-article)
9690             (not gnus-article-current)
9691             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9692     (error "No current article selected"))
9693   ;; Remove old bookmark, if one exists.
9694   (gnus-pull article gnus-newsgroup-bookmarks)
9695   ;; Set the new bookmark, which is on the form
9696   ;; (article-number . line-number-in-body).
9697   (push
9698    (cons article
9699          (save-excursion
9700            (set-buffer gnus-article-buffer)
9701            (count-lines
9702             (min (point)
9703                  (save-excursion
9704                    (article-goto-body)
9705                    (point)))
9706             (point))))
9707    gnus-newsgroup-bookmarks)
9708   (gnus-message 6 "A bookmark has been added to the current article."))
9709
9710 (defun gnus-summary-remove-bookmark (article)
9711   "Remove the bookmark from the current article."
9712   (interactive (list (gnus-summary-article-number)))
9713   ;; Remove old bookmark, if one exists.
9714   (if (not (assq article gnus-newsgroup-bookmarks))
9715       (gnus-message 6 "No bookmark in current article.")
9716     (gnus-pull article gnus-newsgroup-bookmarks)
9717     (gnus-message 6 "Removed bookmark.")))
9718
9719 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9720 (defun gnus-summary-mark-as-dormant (n)
9721   "Mark N articles forward as dormant.
9722 If N is negative, mark backward instead.  The difference between N and
9723 the actual number of articles marked is returned."
9724   (interactive "p")
9725   (gnus-summary-mark-forward n gnus-dormant-mark))
9726
9727 (defun gnus-summary-set-process-mark (article)
9728   "Set the process mark on ARTICLE and update the summary line."
9729   (setq gnus-newsgroup-processable
9730         (cons article
9731               (delq article gnus-newsgroup-processable)))
9732   (when (gnus-summary-goto-subject article)
9733     (gnus-summary-show-thread)
9734     (gnus-summary-goto-subject article)
9735     (gnus-summary-update-secondary-mark article)))
9736
9737 (defun gnus-summary-remove-process-mark (article)
9738   "Remove the process mark from ARTICLE and update the summary line."
9739   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9740   (when (gnus-summary-goto-subject article)
9741     (gnus-summary-show-thread)
9742     (gnus-summary-goto-subject article)
9743     (gnus-summary-update-secondary-mark article)))
9744
9745 (defun gnus-summary-set-saved-mark (article)
9746   "Set the process mark on ARTICLE and update the summary line."
9747   (push article gnus-newsgroup-saved)
9748   (when (gnus-summary-goto-subject article)
9749     (gnus-summary-update-secondary-mark article)))
9750
9751 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9752   "Mark N articles as read forwards.
9753 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9754 The difference between N and the actual number of articles marked is
9755 returned.
9756 If NO-EXPIRE, auto-expiry will be inhibited."
9757   (interactive "p")
9758   (gnus-summary-show-thread)
9759   (let ((backward (< n 0))
9760         (gnus-summary-goto-unread
9761          (and gnus-summary-goto-unread
9762               (not (eq gnus-summary-goto-unread 'never))
9763               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9764                                     gnus-ticked-mark gnus-dormant-mark)))))
9765         (n (abs n))
9766         (mark (or mark gnus-del-mark)))
9767     (while (and (> n 0)
9768                 (gnus-summary-mark-article nil mark no-expire)
9769                 (zerop (gnus-summary-next-subject
9770                         (if backward -1 1)
9771                         (and gnus-summary-goto-unread
9772                              (not (eq gnus-summary-goto-unread 'never)))
9773                         t)))
9774       (setq n (1- n)))
9775     (when (/= 0 n)
9776       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9777     (gnus-summary-recenter)
9778     (gnus-summary-position-point)
9779     (gnus-set-mode-line 'summary)
9780     n))
9781
9782 (defun gnus-summary-mark-article-as-read (mark)
9783   "Mark the current article quickly as read with MARK."
9784   (let ((article (gnus-summary-article-number)))
9785     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9786     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9787     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9788     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9789     (push (cons article mark) gnus-newsgroup-reads)
9790     ;; Possibly remove from cache, if that is used.
9791     (when gnus-use-cache
9792       (gnus-cache-enter-remove-article article))
9793     ;; Allow the backend to change the mark.
9794     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9795     ;; Check for auto-expiry.
9796     (when (and gnus-newsgroup-auto-expire
9797                (memq mark gnus-auto-expirable-marks))
9798       (setq mark gnus-expirable-mark)
9799       ;; Let the backend know about the mark change.
9800       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9801       (push article gnus-newsgroup-expirable))
9802     ;; Set the mark in the buffer.
9803     (gnus-summary-update-mark mark 'unread)
9804     t))
9805
9806 (defun gnus-summary-mark-article-as-unread (mark)
9807   "Mark the current article quickly as unread with MARK."
9808   (let* ((article (gnus-summary-article-number))
9809          (old-mark (gnus-summary-article-mark article)))
9810     ;; Allow the backend to change the mark.
9811     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9812     (if (eq mark old-mark)
9813         t
9814       (if (<= article 0)
9815           (progn
9816             (gnus-error 1 "Can't mark negative article numbers")
9817             nil)
9818         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9819         (setq gnus-newsgroup-spam-marked
9820               (delq article gnus-newsgroup-spam-marked))
9821         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9822         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9823         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9824         (cond ((= mark gnus-ticked-mark)
9825                (setq gnus-newsgroup-marked
9826                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9827                                               article)))
9828               ((= mark gnus-spam-mark)
9829                (setq gnus-newsgroup-spam-marked
9830                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9831                                               article)))
9832               ((= mark gnus-dormant-mark)
9833                (setq gnus-newsgroup-dormant
9834                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9835                                               article)))
9836               (t
9837                (setq gnus-newsgroup-unreads
9838                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9839                                               article))))
9840         (gnus-pull article gnus-newsgroup-reads)
9841
9842         ;; See whether the article is to be put in the cache.
9843         (and gnus-use-cache
9844              (vectorp (gnus-summary-article-header article))
9845              (save-excursion
9846                (gnus-cache-possibly-enter-article
9847                 gnus-newsgroup-name article
9848                 (= mark gnus-ticked-mark)
9849                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9850
9851         ;; Fix the mark.
9852         (gnus-summary-update-mark mark 'unread)
9853         t))))
9854
9855 (defun gnus-summary-mark-article (&optional article mark no-expire)
9856   "Mark ARTICLE with MARK.  MARK can be any character.
9857 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9858 `??' (dormant) and `?E' (expirable).
9859 If MARK is nil, then the default character `?r' is used.
9860 If ARTICLE is nil, then the article on the current line will be
9861 marked.
9862 If NO-EXPIRE, auto-expiry will be inhibited."
9863   ;; The mark might be a string.
9864   (when (stringp mark)
9865     (setq mark (aref mark 0)))
9866   ;; If no mark is given, then we check auto-expiring.
9867   (when (null mark)
9868     (setq mark gnus-del-mark))
9869   (when (and (not no-expire)
9870              gnus-newsgroup-auto-expire
9871              (memq mark gnus-auto-expirable-marks))
9872     (setq mark gnus-expirable-mark))
9873   (let ((article (or article (gnus-summary-article-number)))
9874         (old-mark (gnus-summary-article-mark article)))
9875     ;; Allow the backend to change the mark.
9876     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9877     (if (eq mark old-mark)
9878         t
9879       (unless article
9880         (error "No article on current line"))
9881       (if (not (if (or (= mark gnus-unread-mark)
9882                        (= mark gnus-ticked-mark)
9883                        (= mark gnus-spam-mark)
9884                        (= mark gnus-dormant-mark))
9885                    (gnus-mark-article-as-unread article mark)
9886                  (gnus-mark-article-as-read article mark)))
9887           t
9888         ;; See whether the article is to be put in the cache.
9889         (and gnus-use-cache
9890              (not (= mark gnus-canceled-mark))
9891              (vectorp (gnus-summary-article-header article))
9892              (save-excursion
9893                (gnus-cache-possibly-enter-article
9894                 gnus-newsgroup-name article
9895                 (= mark gnus-ticked-mark)
9896                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9897
9898         (when (gnus-summary-goto-subject article nil t)
9899           (let ((buffer-read-only nil))
9900             (gnus-summary-show-thread)
9901             ;; Fix the mark.
9902             (gnus-summary-update-mark mark 'unread)
9903             t))))))
9904
9905 (defun gnus-summary-update-secondary-mark (article)
9906   "Update the secondary (read, process, cache) mark."
9907   (gnus-summary-update-mark
9908    (cond ((memq article gnus-newsgroup-processable)
9909           gnus-process-mark)
9910          ((memq article gnus-newsgroup-cached)
9911           gnus-cached-mark)
9912          ((memq article gnus-newsgroup-replied)
9913           gnus-replied-mark)
9914          ((memq article gnus-newsgroup-forwarded)
9915           gnus-forwarded-mark)
9916          ((memq article gnus-newsgroup-saved)
9917           gnus-saved-mark)
9918          ((memq article gnus-newsgroup-recent)
9919           gnus-recent-mark)
9920          ((memq article gnus-newsgroup-unseen)
9921           gnus-unseen-mark)
9922          (t gnus-no-mark))
9923    'replied)
9924   (when (gnus-visual-p 'summary-highlight 'highlight)
9925     (gnus-run-hooks 'gnus-summary-update-hook))
9926   t)
9927
9928 (defun gnus-summary-update-download-mark (article)
9929   "Update the download mark."
9930   (gnus-summary-update-mark
9931    (cond ((memq article gnus-newsgroup-undownloaded)
9932           gnus-undownloaded-mark)
9933          (gnus-newsgroup-agentized
9934           gnus-downloaded-mark)
9935          (t
9936           gnus-no-mark))
9937    'download)
9938   (gnus-summary-update-line t)
9939   t)
9940
9941 (defun gnus-summary-update-mark (mark type)
9942   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9943         (buffer-read-only nil))
9944     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
9945     (when forward
9946       (when (looking-at "\r")
9947         (incf forward))
9948       (when (<= (+ forward (point)) (point-max))
9949         ;; Go to the right position on the line.
9950         (goto-char (+ forward (point)))
9951         ;; Replace the old mark with the new mark.
9952         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9953         ;; Optionally update the marks by some user rule.
9954         (when (eq type 'unread)
9955           (gnus-data-set-mark
9956            (gnus-data-find (gnus-summary-article-number)) mark)
9957           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9958
9959 (defun gnus-mark-article-as-read (article &optional mark)
9960   "Enter ARTICLE in the pertinent lists and remove it from others."
9961   ;; Make the article expirable.
9962   (let ((mark (or mark gnus-del-mark)))
9963     (setq gnus-newsgroup-expirable
9964           (if (= mark gnus-expirable-mark)
9965               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9966             (delq article gnus-newsgroup-expirable)))
9967     ;; Remove from unread and marked lists.
9968     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9969     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9970     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9971     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9972     (push (cons article mark) gnus-newsgroup-reads)
9973     ;; Possibly remove from cache, if that is used.
9974     (when gnus-use-cache
9975       (gnus-cache-enter-remove-article article))
9976     t))
9977
9978 (defun gnus-mark-article-as-unread (article &optional mark)
9979   "Enter ARTICLE in the pertinent lists and remove it from others."
9980   (let ((mark (or mark gnus-ticked-mark)))
9981     (if (<= article 0)
9982         (progn
9983           (gnus-error 1 "Can't mark negative article numbers")
9984           nil)
9985       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9986             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9987             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9988             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9989             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9990
9991       ;; Unsuppress duplicates?
9992       (when gnus-suppress-duplicates
9993         (gnus-dup-unsuppress-article article))
9994
9995       (cond ((= mark gnus-ticked-mark)
9996              (setq gnus-newsgroup-marked
9997                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9998             ((= mark gnus-spam-mark)
9999              (setq gnus-newsgroup-spam-marked
10000                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10001                                             article)))
10002             ((= mark gnus-dormant-mark)
10003              (setq gnus-newsgroup-dormant
10004                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10005             (t
10006              (setq gnus-newsgroup-unreads
10007                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10008       (gnus-pull article gnus-newsgroup-reads)
10009       t)))
10010
10011 (defalias 'gnus-summary-mark-as-unread-forward
10012   'gnus-summary-tick-article-forward)
10013 (make-obsolete 'gnus-summary-mark-as-unread-forward
10014                'gnus-summary-tick-article-forward)
10015 (defun gnus-summary-tick-article-forward (n)
10016   "Tick N articles forwards.
10017 If N is negative, tick backwards instead.
10018 The difference between N and the number of articles ticked is returned."
10019   (interactive "p")
10020   (gnus-summary-mark-forward n gnus-ticked-mark))
10021
10022 (defalias 'gnus-summary-mark-as-unread-backward
10023   'gnus-summary-tick-article-backward)
10024 (make-obsolete 'gnus-summary-mark-as-unread-backward
10025                'gnus-summary-tick-article-backward)
10026 (defun gnus-summary-tick-article-backward (n)
10027   "Tick N articles backwards.
10028 The difference between N and the number of articles ticked is returned."
10029   (interactive "p")
10030   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10031
10032 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10033 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10034 (defun gnus-summary-tick-article (&optional article clear-mark)
10035   "Mark current article as unread.
10036 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10037 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10038   (interactive)
10039   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10040                                        gnus-ticked-mark)))
10041
10042 (defun gnus-summary-mark-as-read-forward (n)
10043   "Mark N articles as read forwards.
10044 If N is negative, mark backwards instead.
10045 The difference between N and the actual number of articles marked is
10046 returned."
10047   (interactive "p")
10048   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10049
10050 (defun gnus-summary-mark-as-read-backward (n)
10051   "Mark the N articles as read backwards.
10052 The difference between N and the actual number of articles marked is
10053 returned."
10054   (interactive "p")
10055   (gnus-summary-mark-forward
10056    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10057
10058 (defun gnus-summary-mark-as-read (&optional article mark)
10059   "Mark current article as read.
10060 ARTICLE specifies the article to be marked as read.
10061 MARK specifies a string to be inserted at the beginning of the line."
10062   (gnus-summary-mark-article article mark))
10063
10064 (defun gnus-summary-clear-mark-forward (n)
10065   "Clear marks from N articles forward.
10066 If N is negative, clear backward instead.
10067 The difference between N and the number of marks cleared is returned."
10068   (interactive "p")
10069   (gnus-summary-mark-forward n gnus-unread-mark))
10070
10071 (defun gnus-summary-clear-mark-backward (n)
10072   "Clear marks from N articles backward.
10073 The difference between N and the number of marks cleared is returned."
10074   (interactive "p")
10075   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10076
10077 (defun gnus-summary-mark-unread-as-read ()
10078   "Intended to be used by `gnus-summary-mark-article-hook'."
10079   (when (memq gnus-current-article gnus-newsgroup-unreads)
10080     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10081
10082 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10083   "Intended to be used by `gnus-summary-mark-article-hook'."
10084   (let ((mark (gnus-summary-article-mark)))
10085     (when (or (gnus-unread-mark-p mark)
10086               (gnus-read-mark-p mark))
10087       (gnus-summary-mark-article gnus-current-article
10088                                  (or new-mark gnus-read-mark)))))
10089
10090 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10091   "Intended to be used by `gnus-summary-mark-article-hook'."
10092   (let ((mark (gnus-summary-article-mark)))
10093     (when (or (gnus-unread-mark-p mark)
10094               (gnus-read-mark-p mark))
10095       (gnus-summary-mark-article (gnus-summary-article-number)
10096                                  (or new-mark gnus-read-mark)))))
10097
10098 (defun gnus-summary-mark-unread-as-ticked ()
10099   "Intended to be used by `gnus-summary-mark-article-hook'."
10100   (when (memq gnus-current-article gnus-newsgroup-unreads)
10101     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10102
10103 (defun gnus-summary-mark-region-as-read (point mark all)
10104   "Mark all unread articles between point and mark as read.
10105 If given a prefix, mark all articles between point and mark as read,
10106 even ticked and dormant ones."
10107   (interactive "r\nP")
10108   (save-excursion
10109     (let (article)
10110       (goto-char point)
10111       (beginning-of-line)
10112       (while (and
10113               (< (point) mark)
10114               (progn
10115                 (when (or all
10116                           (memq (setq article (gnus-summary-article-number))
10117                                 gnus-newsgroup-unreads))
10118                   (gnus-summary-mark-article article gnus-del-mark))
10119                 t)
10120               (gnus-summary-find-next))))))
10121
10122 (defun gnus-summary-mark-below (score mark)
10123   "Mark articles with score less than SCORE with MARK."
10124   (interactive "P\ncMark: ")
10125   (setq score (if score
10126                   (prefix-numeric-value score)
10127                 (or gnus-summary-default-score 0)))
10128   (save-excursion
10129     (set-buffer gnus-summary-buffer)
10130     (goto-char (point-min))
10131     (while
10132         (progn
10133           (and (< (gnus-summary-article-score) score)
10134                (gnus-summary-mark-article nil mark))
10135           (gnus-summary-find-next)))))
10136
10137 (defun gnus-summary-kill-below (&optional score)
10138   "Mark articles with score below SCORE as read."
10139   (interactive "P")
10140   (gnus-summary-mark-below score gnus-killed-mark))
10141
10142 (defun gnus-summary-clear-above (&optional score)
10143   "Clear all marks from articles with score above SCORE."
10144   (interactive "P")
10145   (gnus-summary-mark-above score gnus-unread-mark))
10146
10147 (defun gnus-summary-tick-above (&optional score)
10148   "Tick all articles with score above SCORE."
10149   (interactive "P")
10150   (gnus-summary-mark-above score gnus-ticked-mark))
10151
10152 (defun gnus-summary-mark-above (score mark)
10153   "Mark articles with score over SCORE with MARK."
10154   (interactive "P\ncMark: ")
10155   (setq score (if score
10156                   (prefix-numeric-value score)
10157                 (or gnus-summary-default-score 0)))
10158   (save-excursion
10159     (set-buffer gnus-summary-buffer)
10160     (goto-char (point-min))
10161     (while (and (progn
10162                   (when (> (gnus-summary-article-score) score)
10163                     (gnus-summary-mark-article nil mark))
10164                   t)
10165                 (gnus-summary-find-next)))))
10166
10167 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10168 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10169 (defun gnus-summary-limit-include-expunged (&optional no-error)
10170   "Display all the hidden articles that were expunged for low scores."
10171   (interactive)
10172   (let ((buffer-read-only nil))
10173     (let ((scored gnus-newsgroup-scored)
10174           headers h)
10175       (while scored
10176         (unless (gnus-summary-article-header (caar scored))
10177           (and (setq h (gnus-number-to-header (caar scored)))
10178                (< (cdar scored) gnus-summary-expunge-below)
10179                (push h headers)))
10180         (setq scored (cdr scored)))
10181       (if (not headers)
10182           (when (not no-error)
10183             (error "No expunged articles hidden"))
10184         (goto-char (point-min))
10185         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10186         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10187         (mapcar (lambda (x) (push (mail-header-number x)
10188                                   gnus-newsgroup-limit))
10189                 headers)
10190         (gnus-summary-prepare-unthreaded (nreverse headers))
10191         (goto-char (point-min))
10192         (gnus-summary-position-point)
10193         t))))
10194
10195 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10196   "Mark all unread articles in this newsgroup as read.
10197 If prefix argument ALL is non-nil, ticked and dormant articles will
10198 also be marked as read.
10199 If QUIETLY is non-nil, no questions will be asked.
10200
10201 If TO-HERE is non-nil, it should be a point in the buffer.  All
10202 articles before (after, if REVERSE is set) this point will be marked
10203 as read.
10204
10205 Note that this function will only catch up the unread article
10206 in the current summary buffer limitation.
10207
10208 The number of articles marked as read is returned."
10209   (interactive "P")
10210   (prog1
10211       (save-excursion
10212         (when (or quietly
10213                   (not gnus-interactive-catchup) ;Without confirmation?
10214                   gnus-expert-user
10215                   (gnus-y-or-n-p
10216                    (if all
10217                        "Mark absolutely all articles as read? "
10218                      "Mark all unread articles as read? ")))
10219           (if (and not-mark
10220                    (not gnus-newsgroup-adaptive)
10221                    (not gnus-newsgroup-auto-expire)
10222                    (not gnus-suppress-duplicates)
10223                    (or (not gnus-use-cache)
10224                        (eq gnus-use-cache 'passive)))
10225               (progn
10226                 (when all
10227                   (setq gnus-newsgroup-marked nil
10228                         gnus-newsgroup-spam-marked nil
10229                         gnus-newsgroup-dormant nil))
10230                 (setq gnus-newsgroup-unreads
10231                       (gnus-sorted-nunion
10232                        (gnus-intersection gnus-newsgroup-unreads
10233                                           gnus-newsgroup-downloadable)
10234                        gnus-newsgroup-unfetched)))
10235             ;; We actually mark all articles as canceled, which we
10236             ;; have to do when using auto-expiry or adaptive scoring.
10237             (gnus-summary-show-all-threads)
10238             (if (and to-here reverse)
10239                 (progn
10240                   (goto-char to-here)
10241                   (gnus-summary-mark-current-read-and-unread-as-read
10242                    gnus-catchup-mark)
10243                   (while (gnus-summary-find-next (not all))
10244                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10245               (when (gnus-summary-first-subject (not all))
10246                 (while (and
10247                         (if to-here (< (point) to-here) t)
10248                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10249                         (gnus-summary-find-next (not all))))))
10250             (gnus-set-mode-line 'summary))
10251           t))
10252     (gnus-summary-position-point)))
10253
10254 (defun gnus-summary-catchup-to-here (&optional all)
10255   "Mark all unticked articles before the current one as read.
10256 If ALL is non-nil, also mark ticked and dormant articles as read."
10257   (interactive "P")
10258   (save-excursion
10259     (gnus-save-hidden-threads
10260       (let ((beg (point)))
10261         ;; We check that there are unread articles.
10262         (when (or all (gnus-summary-find-prev))
10263           (gnus-summary-catchup all t beg)))))
10264   (gnus-summary-position-point))
10265
10266 (defun gnus-summary-catchup-from-here (&optional all)
10267   "Mark all unticked articles after (and including) the current one as read.
10268 If ALL is non-nil, also mark ticked and dormant articles as read."
10269   (interactive "P")
10270   (save-excursion
10271     (gnus-save-hidden-threads
10272       (let ((beg (point)))
10273         ;; We check that there are unread articles.
10274         (when (or all (gnus-summary-find-next))
10275           (gnus-summary-catchup all t beg nil t)))))
10276   (gnus-summary-position-point))
10277
10278 (defun gnus-summary-catchup-all (&optional quietly)
10279   "Mark all articles in this newsgroup as read.
10280 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10281 instead, which marks only unread articles as read."
10282   (interactive "P")
10283   (gnus-summary-catchup t quietly))
10284
10285 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10286   "Mark all unread articles in this group as read, then exit.
10287 If prefix argument ALL is non-nil, all articles are marked as read.
10288 If QUIETLY is non-nil, no questions will be asked."
10289   (interactive "P")
10290   (when (gnus-summary-catchup all quietly nil 'fast)
10291     ;; Select next newsgroup or exit.
10292     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10293              (eq gnus-auto-select-next 'quietly))
10294         (gnus-summary-next-group nil)
10295       (gnus-summary-exit))))
10296
10297 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10298   "Mark all articles in this newsgroup as read, and then exit.
10299 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10300 instead, which marks only unread articles as read."
10301   (interactive "P")
10302   (gnus-summary-catchup-and-exit t quietly))
10303
10304 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10305   "Mark all articles in this group as read and select the next group.
10306 If given a prefix, mark all articles, unread as well as ticked, as
10307 read."
10308   (interactive "P")
10309   (save-excursion
10310     (gnus-summary-catchup all))
10311   (gnus-summary-next-group))
10312
10313 ;;;
10314 ;;; with article
10315 ;;;
10316
10317 (defmacro gnus-with-article (article &rest forms)
10318   "Select ARTICLE and perform FORMS in the original article buffer.
10319 Then replace the article with the result."
10320   `(progn
10321      ;; We don't want the article to be marked as read.
10322      (let (gnus-mark-article-hook)
10323        (gnus-summary-select-article t t nil ,article))
10324      (set-buffer gnus-original-article-buffer)
10325      ,@forms
10326      (if (not (gnus-check-backend-function
10327                'request-replace-article (car gnus-article-current)))
10328          (gnus-message 5 "Read-only group; not replacing")
10329        (unless (gnus-request-replace-article
10330                 ,article (car gnus-article-current)
10331                 (current-buffer) t)
10332          (error "Couldn't replace article")))
10333      ;; The cache and backlog have to be flushed somewhat.
10334      (when gnus-keep-backlog
10335        (gnus-backlog-remove-article
10336         (car gnus-article-current) (cdr gnus-article-current)))
10337      (when gnus-use-cache
10338        (gnus-cache-update-article
10339         (car gnus-article-current) (cdr gnus-article-current)))))
10340
10341 (put 'gnus-with-article 'lisp-indent-function 1)
10342 (put 'gnus-with-article 'edebug-form-spec '(form body))
10343
10344 ;; Thread-based commands.
10345
10346 (defun gnus-summary-articles-in-thread (&optional article)
10347   "Return a list of all articles in the current thread.
10348 If ARTICLE is non-nil, return all articles in the thread that starts
10349 with that article."
10350   (let* ((article (or article (gnus-summary-article-number)))
10351          (data (gnus-data-find-list article))
10352          (top-level (gnus-data-level (car data)))
10353          (top-subject
10354           (cond ((null gnus-thread-operation-ignore-subject)
10355                  (gnus-simplify-subject-re
10356                   (mail-header-subject (gnus-data-header (car data)))))
10357                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10358                  (gnus-simplify-subject-fuzzy
10359                   (mail-header-subject (gnus-data-header (car data)))))
10360                 (t nil)))
10361          (end-point (save-excursion
10362                       (if (gnus-summary-go-to-next-thread)
10363                           (point) (point-max))))
10364          articles)
10365     (while (and data
10366                 (< (gnus-data-pos (car data)) end-point))
10367       (when (or (not top-subject)
10368                 (string= top-subject
10369                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10370                              (gnus-simplify-subject-fuzzy
10371                               (mail-header-subject
10372                                (gnus-data-header (car data))))
10373                            (gnus-simplify-subject-re
10374                             (mail-header-subject
10375                              (gnus-data-header (car data)))))))
10376         (push (gnus-data-number (car data)) articles))
10377       (unless (and (setq data (cdr data))
10378                    (> (gnus-data-level (car data)) top-level))
10379         (setq data nil)))
10380     ;; Return the list of articles.
10381     (nreverse articles)))
10382
10383 (defun gnus-summary-rethread-current ()
10384   "Rethread the thread the current article is part of."
10385   (interactive)
10386   (let* ((gnus-show-threads t)
10387          (article (gnus-summary-article-number))
10388          (id (mail-header-id (gnus-summary-article-header)))
10389          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10390     (unless id
10391       (error "No article on the current line"))
10392     (gnus-rebuild-thread id)
10393     (gnus-summary-goto-subject article)))
10394
10395 (defun gnus-summary-reparent-thread ()
10396   "Make the current article child of the marked (or previous) article.
10397
10398 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10399 is non-nil or the Subject: of both articles are the same."
10400   (interactive)
10401   (unless (not (gnus-group-read-only-p))
10402     (error "The current newsgroup does not support article editing"))
10403   (unless (<= (length gnus-newsgroup-processable) 1)
10404     (error "No more than one article may be marked"))
10405   (save-window-excursion
10406     (let ((gnus-article-buffer " *reparent*")
10407           (current-article (gnus-summary-article-number))
10408           ;; First grab the marked article, otherwise one line up.
10409           (parent-article (if (not (null gnus-newsgroup-processable))
10410                               (car gnus-newsgroup-processable)
10411                             (save-excursion
10412                               (if (eq (forward-line -1) 0)
10413                                   (gnus-summary-article-number)
10414                                 (error "Beginning of summary buffer"))))))
10415       (unless (not (eq current-article parent-article))
10416         (error "An article may not be self-referential"))
10417       (let ((message-id (mail-header-id
10418                          (gnus-summary-article-header parent-article))))
10419         (unless (and message-id (not (equal message-id "")))
10420           (error "No message-id in desired parent"))
10421         (gnus-with-article current-article
10422           (save-restriction
10423             (goto-char (point-min))
10424             (message-narrow-to-head)
10425             (if (re-search-forward "^References: " nil t)
10426                 (progn
10427                   (re-search-forward "^[^ \t]" nil t)
10428                   (forward-line -1)
10429                   (end-of-line)
10430                   (insert " " message-id))
10431               (insert "References: " message-id "\n"))))
10432         (set-buffer gnus-summary-buffer)
10433         (gnus-summary-unmark-all-processable)
10434         (gnus-summary-update-article current-article)
10435         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10436             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10437         (gnus-summary-rethread-current)
10438         (gnus-message 3 "Article %d is now the child of article %d"
10439                       current-article parent-article)))))
10440
10441 (defun gnus-summary-toggle-threads (&optional arg)
10442   "Toggle showing conversation threads.
10443 If ARG is positive number, turn showing conversation threads on."
10444   (interactive "P")
10445   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10446     (setq gnus-show-threads
10447           (if (null arg) (not gnus-show-threads)
10448             (> (prefix-numeric-value arg) 0)))
10449     (gnus-summary-prepare)
10450     (gnus-summary-goto-subject current)
10451     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10452     (gnus-summary-position-point)))
10453
10454 (defun gnus-summary-show-all-threads ()
10455   "Show all threads."
10456   (interactive)
10457   (save-excursion
10458     (let ((buffer-read-only nil))
10459       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10460   (gnus-summary-position-point))
10461
10462 (defun gnus-summary-show-thread ()
10463   "Show thread subtrees.
10464 Returns nil if no thread was there to be shown."
10465   (interactive)
10466   (let ((buffer-read-only nil)
10467         (orig (point))
10468         (end (point-at-eol))
10469         ;; Leave point at bol
10470         (beg (progn (beginning-of-line) (point))))
10471     (prog1
10472         ;; Any hidden lines here?
10473         (search-forward "\r" end t)
10474       (subst-char-in-region beg end ?\^M ?\n t)
10475       (goto-char orig)
10476       (gnus-summary-position-point))))
10477
10478 (defun gnus-summary-maybe-hide-threads ()
10479   "If requested, hide the threads that should be hidden."
10480   (when (and gnus-show-threads
10481              gnus-thread-hide-subtree)
10482     (gnus-summary-hide-all-threads
10483      (if (or (consp gnus-thread-hide-subtree)
10484              (functionp gnus-thread-hide-subtree))
10485          (gnus-make-predicate gnus-thread-hide-subtree)
10486        nil))))
10487
10488 ;;; Hiding predicates.
10489
10490 (defun gnus-article-unread-p (header)
10491   (memq (mail-header-number header) gnus-newsgroup-unreads))
10492
10493 (defun gnus-article-unseen-p (header)
10494   (memq (mail-header-number header) gnus-newsgroup-unseen))
10495
10496 (defun gnus-map-articles (predicate articles)
10497   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10498   (apply 'gnus-or (mapcar predicate
10499                           (mapcar 'gnus-summary-article-header articles))))
10500
10501 (defun gnus-summary-hide-all-threads (&optional predicate)
10502   "Hide all thread subtrees.
10503 If PREDICATE is supplied, threads that satisfy this predicate
10504 will not be hidden."
10505   (interactive)
10506   (save-excursion
10507     (goto-char (point-min))
10508     (let ((end nil))
10509       (while (not end)
10510         (when (or (not predicate)
10511                   (gnus-map-articles
10512                    predicate (gnus-summary-article-children)))
10513             (gnus-summary-hide-thread))
10514         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10515   (gnus-summary-position-point))
10516
10517 (defun gnus-summary-hide-thread ()
10518   "Hide thread subtrees.
10519 If PREDICATE is supplied, threads that satisfy this predicate
10520 will not be hidden.
10521 Returns nil if no threads were there to be hidden."
10522   (interactive)
10523   (let ((buffer-read-only nil)
10524         (start (point))
10525         (article (gnus-summary-article-number)))
10526     (goto-char start)
10527     ;; Go forward until either the buffer ends or the subthread
10528     ;; ends.
10529     (when (and (not (eobp))
10530                (or (zerop (gnus-summary-next-thread 1 t))
10531                    (goto-char (point-max))))
10532       (prog1
10533           (if (and (> (point) start)
10534                    (search-backward "\n" start t))
10535               (progn
10536                 (subst-char-in-region start (point) ?\n ?\^M)
10537                 (gnus-summary-goto-subject article))
10538             (goto-char start)
10539             nil)))))
10540
10541 (defun gnus-summary-go-to-next-thread (&optional previous)
10542   "Go to the same level (or less) next thread.
10543 If PREVIOUS is non-nil, go to previous thread instead.
10544 Return the article number moved to, or nil if moving was impossible."
10545   (let ((level (gnus-summary-thread-level))
10546         (way (if previous -1 1))
10547         (beg (point)))
10548     (forward-line way)
10549     (while (and (not (eobp))
10550                 (< level (gnus-summary-thread-level)))
10551       (forward-line way))
10552     (if (eobp)
10553         (progn
10554           (goto-char beg)
10555           nil)
10556       (setq beg (point))
10557       (prog1
10558           (gnus-summary-article-number)
10559         (goto-char beg)))))
10560
10561 (defun gnus-summary-next-thread (n &optional silent)
10562   "Go to the same level next N'th thread.
10563 If N is negative, search backward instead.
10564 Returns the difference between N and the number of skips actually
10565 done.
10566
10567 If SILENT, don't output messages."
10568   (interactive "p")
10569   (let ((backward (< n 0))
10570         (n (abs n)))
10571     (while (and (> n 0)
10572                 (gnus-summary-go-to-next-thread backward))
10573       (decf n))
10574     (unless silent
10575       (gnus-summary-position-point))
10576     (when (and (not silent) (/= 0 n))
10577       (gnus-message 7 "No more threads"))
10578     n))
10579
10580 (defun gnus-summary-prev-thread (n)
10581   "Go to the same level previous N'th thread.
10582 Returns the difference between N and the number of skips actually
10583 done."
10584   (interactive "p")
10585   (gnus-summary-next-thread (- n)))
10586
10587 (defun gnus-summary-go-down-thread ()
10588   "Go down one level in the current thread."
10589   (let ((children (gnus-summary-article-children)))
10590     (when children
10591       (gnus-summary-goto-subject (car children)))))
10592
10593 (defun gnus-summary-go-up-thread ()
10594   "Go up one level in the current thread."
10595   (let ((parent (gnus-summary-article-parent)))
10596     (when parent
10597       (gnus-summary-goto-subject parent))))
10598
10599 (defun gnus-summary-down-thread (n)
10600   "Go down thread N steps.
10601 If N is negative, go up instead.
10602 Returns the difference between N and how many steps down that were
10603 taken."
10604   (interactive "p")
10605   (let ((up (< n 0))
10606         (n (abs n)))
10607     (while (and (> n 0)
10608                 (if up (gnus-summary-go-up-thread)
10609                   (gnus-summary-go-down-thread)))
10610       (setq n (1- n)))
10611     (gnus-summary-position-point)
10612     (when (/= 0 n)
10613       (gnus-message 7 "Can't go further"))
10614     n))
10615
10616 (defun gnus-summary-up-thread (n)
10617   "Go up thread N steps.
10618 If N is negative, go down instead.
10619 Returns the difference between N and how many steps down that were
10620 taken."
10621   (interactive "p")
10622   (gnus-summary-down-thread (- n)))
10623
10624 (defun gnus-summary-top-thread ()
10625   "Go to the top of the thread."
10626   (interactive)
10627   (while (gnus-summary-go-up-thread))
10628   (gnus-summary-article-number))
10629
10630 (defun gnus-summary-kill-thread (&optional unmark)
10631   "Mark articles under current thread as read.
10632 If the prefix argument is positive, remove any kinds of marks.
10633 If the prefix argument is negative, tick articles instead."
10634   (interactive "P")
10635   (when unmark
10636     (setq unmark (prefix-numeric-value unmark)))
10637   (let ((articles (gnus-summary-articles-in-thread)))
10638     (save-excursion
10639       ;; Expand the thread.
10640       (gnus-summary-show-thread)
10641       ;; Mark all the articles.
10642       (while articles
10643         (gnus-summary-goto-subject (car articles))
10644         (cond ((null unmark)
10645                (gnus-summary-mark-article-as-read gnus-killed-mark))
10646               ((> unmark 0)
10647                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10648               (t
10649                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10650         (setq articles (cdr articles))))
10651     ;; Hide killed subtrees.
10652     (and (null unmark)
10653          gnus-thread-hide-killed
10654          (gnus-summary-hide-thread))
10655     ;; If marked as read, go to next unread subject.
10656     (when (null unmark)
10657       ;; Go to next unread subject.
10658       (gnus-summary-next-subject 1 t)))
10659   (gnus-set-mode-line 'summary))
10660
10661 ;; Summary sorting commands
10662
10663 (defun gnus-summary-sort-by-number (&optional reverse)
10664   "Sort the summary buffer by article number.
10665 Argument REVERSE means reverse order."
10666   (interactive "P")
10667   (gnus-summary-sort 'number reverse))
10668
10669 (defun gnus-summary-sort-by-random (&optional reverse)
10670   "Randomize the order in the summary buffer.
10671 Argument REVERSE means to randomize in reverse order."
10672   (interactive "P")
10673   (gnus-summary-sort 'random reverse))
10674
10675 (defun gnus-summary-sort-by-author (&optional reverse)
10676   "Sort the summary buffer by author name alphabetically.
10677 If `case-fold-search' is non-nil, case of letters is ignored.
10678 Argument REVERSE means reverse order."
10679   (interactive "P")
10680   (gnus-summary-sort 'author reverse))
10681
10682 (defun gnus-summary-sort-by-subject (&optional reverse)
10683   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10684 If `case-fold-search' is non-nil, case of letters is ignored.
10685 Argument REVERSE means reverse order."
10686   (interactive "P")
10687   (gnus-summary-sort 'subject reverse))
10688
10689 (defun gnus-summary-sort-by-date (&optional reverse)
10690   "Sort the summary buffer by date.
10691 Argument REVERSE means reverse order."
10692   (interactive "P")
10693   (gnus-summary-sort 'date reverse))
10694
10695 (defun gnus-summary-sort-by-score (&optional reverse)
10696   "Sort the summary buffer by score.
10697 Argument REVERSE means reverse order."
10698   (interactive "P")
10699   (gnus-summary-sort 'score reverse))
10700
10701 (defun gnus-summary-sort-by-lines (&optional reverse)
10702   "Sort the summary buffer by the number of lines.
10703 Argument REVERSE means reverse order."
10704   (interactive "P")
10705   (gnus-summary-sort 'lines reverse))
10706
10707 (defun gnus-summary-sort-by-chars (&optional reverse)
10708   "Sort the summary buffer by article length.
10709 Argument REVERSE means reverse order."
10710   (interactive "P")
10711   (gnus-summary-sort 'chars reverse))
10712
10713 (defun gnus-summary-sort-by-original (&optional reverse)
10714   "Sort the summary buffer using the default sorting method.
10715 Argument REVERSE means reverse order."
10716   (interactive "P")
10717   (let* ((buffer-read-only)
10718          (gnus-summary-prepare-hook nil))
10719     ;; We do the sorting by regenerating the threads.
10720     (gnus-summary-prepare)
10721     ;; Hide subthreads if needed.
10722     (gnus-summary-maybe-hide-threads)))
10723
10724 (defun gnus-summary-sort (predicate reverse)
10725   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10726   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10727          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10728          (gnus-thread-sort-functions
10729           (if (not reverse)
10730               thread
10731             `(lambda (t1 t2)
10732                (,thread t2 t1))))
10733          (gnus-sort-gathered-threads-function
10734           gnus-thread-sort-functions)
10735          (gnus-article-sort-functions
10736           (if (not reverse)
10737               article
10738             `(lambda (t1 t2)
10739                (,article t2 t1))))
10740          (buffer-read-only)
10741          (gnus-summary-prepare-hook nil))
10742     ;; We do the sorting by regenerating the threads.
10743     (gnus-summary-prepare)
10744     ;; Hide subthreads if needed.
10745     (gnus-summary-maybe-hide-threads)))
10746
10747 ;; Summary saving commands.
10748
10749 (defun gnus-summary-save-article (&optional n not-saved)
10750   "Save the current article using the default saver function.
10751 If N is a positive number, save the N next articles.
10752 If N is a negative number, save the N previous articles.
10753 If N is nil and any articles have been marked with the process mark,
10754 save those articles instead.
10755 The variable `gnus-default-article-saver' specifies the saver function."
10756   (interactive "P")
10757   (let* ((articles (gnus-summary-work-articles n))
10758          (save-buffer (save-excursion
10759                         (nnheader-set-temp-buffer " *Gnus Save*")))
10760          (num (length articles))
10761          header file)
10762     (dolist (article articles)
10763       (setq header (gnus-summary-article-header article))
10764       (if (not (vectorp header))
10765           ;; This is a pseudo-article.
10766           (if (assq 'name header)
10767               (gnus-copy-file (cdr (assq 'name header)))
10768             (gnus-message 1 "Article %d is unsaveable" article))
10769         ;; This is a real article.
10770         (save-window-excursion
10771           (let ((gnus-display-mime-function nil)
10772                 (gnus-article-prepare-hook nil))
10773             (gnus-summary-select-article t nil nil article)))
10774         (save-excursion
10775           (set-buffer save-buffer)
10776           (erase-buffer)
10777           (insert-buffer-substring gnus-original-article-buffer))
10778         (setq file (gnus-article-save save-buffer file num))
10779         (gnus-summary-remove-process-mark article)
10780         (unless not-saved
10781           (gnus-summary-set-saved-mark article))))
10782     (gnus-kill-buffer save-buffer)
10783     (gnus-summary-position-point)
10784     (gnus-set-mode-line 'summary)
10785     n))
10786
10787 (defun gnus-summary-pipe-output (&optional arg headers)
10788   "Pipe the current article to a subprocess.
10789 If N is a positive number, pipe the N next articles.
10790 If N is a negative number, pipe the N previous articles.
10791 If N is nil and any articles have been marked with the process mark,
10792 pipe those articles instead.
10793 If HEADERS (the symbolic prefix), include the headers, too."
10794   (interactive (gnus-interactive "P\ny"))
10795   (require 'gnus-art)
10796   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10797         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10798     (gnus-summary-save-article arg t))
10799   (let ((buffer (get-buffer "*Shell Command Output*")))
10800     (when (and buffer
10801                (not (zerop (buffer-size buffer))))
10802       (gnus-configure-windows 'pipe))))
10803
10804 (defun gnus-summary-save-article-mail (&optional arg)
10805   "Append the current article to an mail file.
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   (interactive "P")
10811   (require 'gnus-art)
10812   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10813     (gnus-summary-save-article arg)))
10814
10815 (defun gnus-summary-save-article-rmail (&optional arg)
10816   "Append the current article to an rmail file.
10817 If N is a positive number, save the N next articles.
10818 If N is a negative number, save the N previous articles.
10819 If N is nil and any articles have been marked with the process mark,
10820 save those articles instead."
10821   (interactive "P")
10822   (require 'gnus-art)
10823   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10824     (gnus-summary-save-article arg)))
10825
10826 (defun gnus-summary-save-article-file (&optional arg)
10827   "Append the current article to a file.
10828 If N is a positive number, save the N next articles.
10829 If N is a negative number, save the N previous articles.
10830 If N is nil and any articles have been marked with the process mark,
10831 save those articles instead."
10832   (interactive "P")
10833   (require 'gnus-art)
10834   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10835     (gnus-summary-save-article arg)))
10836
10837 (defun gnus-summary-write-article-file (&optional arg)
10838   "Write the current article to a file, deleting the previous file.
10839 If N is a positive number, save the N next articles.
10840 If N is a negative number, save the N previous articles.
10841 If N is nil and any articles have been marked with the process mark,
10842 save those articles instead."
10843   (interactive "P")
10844   (require 'gnus-art)
10845   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10846     (gnus-summary-save-article arg)))
10847
10848 (defun gnus-summary-save-article-body-file (&optional arg)
10849   "Append the current article body to a file.
10850 If N is a positive number, save the N next articles.
10851 If N is a negative number, save the N previous articles.
10852 If N is nil and any articles have been marked with the process mark,
10853 save those articles instead."
10854   (interactive "P")
10855   (require 'gnus-art)
10856   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10857     (gnus-summary-save-article arg)))
10858
10859 (defun gnus-summary-muttprint (&optional arg)
10860   "Print the current article using Muttprint.
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-pipe-to-muttprint))
10868     (gnus-summary-save-article arg t)))
10869
10870 (defun gnus-summary-pipe-message (program)
10871   "Pipe the current article through PROGRAM."
10872   (interactive "sProgram: ")
10873   (gnus-summary-select-article)
10874   (let ((mail-header-separator ""))
10875     (gnus-eval-in-buffer-window gnus-article-buffer
10876       (save-restriction
10877         (widen)
10878         (let ((start (window-start))
10879               buffer-read-only)
10880           (message-pipe-buffer-body program)
10881           (set-window-start (get-buffer-window (current-buffer)) start))))))
10882
10883 (defun gnus-get-split-value (methods)
10884   "Return a value based on the split METHODS."
10885   (let (split-name method result match)
10886     (when methods
10887       (save-excursion
10888         (set-buffer gnus-original-article-buffer)
10889         (save-restriction
10890           (nnheader-narrow-to-headers)
10891           (while (and methods (not split-name))
10892             (goto-char (point-min))
10893             (setq method (pop methods))
10894             (setq match (car method))
10895             (when (cond
10896                    ((stringp match)
10897                     ;; Regular expression.
10898                     (ignore-errors
10899                       (re-search-forward match nil t)))
10900                    ((functionp match)
10901                     ;; Function.
10902                     (save-restriction
10903                       (widen)
10904                       (setq result (funcall match gnus-newsgroup-name))))
10905                    ((consp match)
10906                     ;; Form.
10907                     (save-restriction
10908                       (widen)
10909                       (setq result (eval match)))))
10910               (setq split-name (cdr method))
10911               (cond ((stringp result)
10912                      (push (expand-file-name
10913                             result gnus-article-save-directory)
10914                            split-name))
10915                     ((consp result)
10916                      (setq split-name (append result split-name)))))))))
10917     (nreverse split-name)))
10918
10919 (defun gnus-valid-move-group-p (group)
10920   (and (boundp group)
10921        (symbol-name group)
10922        (symbol-value group)
10923        (gnus-get-function (gnus-find-method-for-group
10924                            (symbol-name group)) 'request-accept-article t)))
10925
10926 (defun gnus-read-move-group-name (prompt default articles prefix)
10927   "Read a group name."
10928   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10929          (minibuffer-confirm-incomplete nil) ; XEmacs
10930          (prom
10931           (format "%s %s to:"
10932                   prompt
10933                   (if (> (length articles) 1)
10934                       (format "these %d articles" (length articles))
10935                     "this article")))
10936          (to-newsgroup
10937           (cond
10938            ((null split-name)
10939             (gnus-completing-read-with-default
10940              default prom
10941              gnus-active-hashtb
10942              'gnus-valid-move-group-p
10943              nil prefix
10944              'gnus-group-history))
10945            ((= 1 (length split-name))
10946             (gnus-completing-read-with-default
10947              (car split-name) prom
10948              gnus-active-hashtb
10949              'gnus-valid-move-group-p
10950              nil nil
10951              'gnus-group-history))
10952            (t
10953             (gnus-completing-read-with-default
10954              nil prom
10955              (mapcar (lambda (el) (list el))
10956                      (nreverse split-name))
10957              nil nil nil
10958              'gnus-group-history))))
10959          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10960     (when to-newsgroup
10961       (if (or (string= to-newsgroup "")
10962               (string= to-newsgroup prefix))
10963           (setq to-newsgroup default))
10964       (unless to-newsgroup
10965         (error "No group name entered"))
10966       (or (gnus-active to-newsgroup)
10967           (gnus-activate-group to-newsgroup nil nil to-method)
10968           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10969                                      to-newsgroup))
10970               (or (and (gnus-request-create-group to-newsgroup to-method)
10971                        (gnus-activate-group
10972                         to-newsgroup nil nil to-method)
10973                        (gnus-subscribe-group to-newsgroup))
10974                   (error "Couldn't create group %s" to-newsgroup)))
10975           (error "No such group: %s" to-newsgroup)))
10976     to-newsgroup))
10977
10978 (defun gnus-summary-save-parts (type dir n &optional reverse)
10979   "Save parts matching TYPE to DIR.
10980 If REVERSE, save parts that do not match TYPE."
10981   (interactive
10982    (list (read-string "Save parts of type: "
10983                       (or (car gnus-summary-save-parts-type-history)
10984                           gnus-summary-save-parts-default-mime)
10985                       'gnus-summary-save-parts-type-history)
10986          (setq gnus-summary-save-parts-last-directory
10987                (read-file-name "Save to directory: "
10988                                gnus-summary-save-parts-last-directory
10989                                nil t))
10990          current-prefix-arg))
10991   (gnus-summary-iterate n
10992     (let ((gnus-display-mime-function nil)
10993           (gnus-inhibit-treatment t))
10994       (gnus-summary-select-article))
10995     (save-excursion
10996       (set-buffer gnus-article-buffer)
10997       (let ((handles (or gnus-article-mime-handles
10998                          (mm-dissect-buffer nil gnus-article-loose-mime)
10999                          (and gnus-article-emulate-mime
11000                               (mm-uu-dissect)))))
11001         (when handles
11002           (gnus-summary-save-parts-1 type dir handles reverse)
11003           (unless gnus-article-mime-handles ;; Don't destroy this case.
11004             (mm-destroy-parts handles)))))))
11005
11006 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11007   (if (stringp (car handle))
11008       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11009               (cdr handle))
11010     (when (if reverse
11011               (not (string-match type (mm-handle-media-type handle)))
11012             (string-match type (mm-handle-media-type handle)))
11013       (let ((file (expand-file-name
11014                    (gnus-map-function
11015                     mm-file-name-rewrite-functions
11016                     (file-name-nondirectory
11017                      (or
11018                       (mail-content-type-get
11019                        (mm-handle-disposition handle) 'filename)
11020                       (mail-content-type-get
11021                        (mm-handle-type handle) 'name)
11022                       (concat gnus-newsgroup-name
11023                               "." (number-to-string
11024                                    (cdr gnus-article-current))))))
11025                    dir)))
11026         (unless (file-exists-p file)
11027           (mm-save-part-to-file handle file))))))
11028
11029 ;; Summary extract commands
11030
11031 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11032   (let ((buffer-read-only nil)
11033         (article (gnus-summary-article-number))
11034         after-article b e)
11035     (unless (gnus-summary-goto-subject article)
11036       (error "No such article: %d" article))
11037     (gnus-summary-position-point)
11038     ;; If all commands are to be bunched up on one line, we collect
11039     ;; them here.
11040     (unless gnus-view-pseudos-separately
11041       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11042             files action)
11043         (while ps
11044           (setq action (cdr (assq 'action (car ps))))
11045           (setq files (list (cdr (assq 'name (car ps)))))
11046           (while (and ps (cdr ps)
11047                       (string= (or action "1")
11048                                (or (cdr (assq 'action (cadr ps))) "2")))
11049             (push (cdr (assq 'name (cadr ps))) files)
11050             (setcdr ps (cddr ps)))
11051           (when files
11052             (when (not (string-match "%s" action))
11053               (push " " files))
11054             (push " " files)
11055             (when (assq 'execute (car ps))
11056               (setcdr (assq 'execute (car ps))
11057                       (funcall (if (string-match "%s" action)
11058                                    'format 'concat)
11059                                action
11060                                (mapconcat
11061                                 (lambda (f)
11062                                   (if (equal f " ")
11063                                       f
11064                                     (shell-quote-argument f)))
11065                                 files " ")))))
11066           (setq ps (cdr ps)))))
11067     (if (and gnus-view-pseudos (not not-view))
11068         (while pslist
11069           (when (assq 'execute (car pslist))
11070             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11071                                   (eq gnus-view-pseudos 'not-confirm)))
11072           (setq pslist (cdr pslist)))
11073       (save-excursion
11074         (while pslist
11075           (setq after-article (or (cdr (assq 'article (car pslist)))
11076                                   (gnus-summary-article-number)))
11077           (gnus-summary-goto-subject after-article)
11078           (forward-line 1)
11079           (setq b (point))
11080           (insert "    " (file-name-nondirectory
11081                           (cdr (assq 'name (car pslist))))
11082                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11083           (setq e (point))
11084           (forward-line -1)             ; back to `b'
11085           (gnus-add-text-properties
11086            b (1- e) (list 'gnus-number gnus-reffed-article-number
11087                           gnus-mouse-face-prop gnus-mouse-face))
11088           (gnus-data-enter
11089            after-article gnus-reffed-article-number
11090            gnus-unread-mark b (car pslist) 0 (- e b))
11091           (setq gnus-newsgroup-unreads
11092                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11093                                          gnus-reffed-article-number))
11094           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11095           (setq pslist (cdr pslist)))))))
11096
11097 (defun gnus-pseudos< (p1 p2)
11098   (let ((c1 (cdr (assq 'action p1)))
11099         (c2 (cdr (assq 'action p2))))
11100     (and c1 c2 (string< c1 c2))))
11101
11102 (defun gnus-request-pseudo-article (props)
11103   (cond ((assq 'execute props)
11104          (gnus-execute-command (cdr (assq 'execute props)))))
11105   (let ((gnus-current-article (gnus-summary-article-number)))
11106     (gnus-run-hooks 'gnus-mark-article-hook)))
11107
11108 (defun gnus-execute-command (command &optional automatic)
11109   (save-excursion
11110     (gnus-article-setup-buffer)
11111     (set-buffer gnus-article-buffer)
11112     (setq buffer-read-only nil)
11113     (let ((command (if automatic command
11114                      (read-string "Command: " (cons command 0)))))
11115       (erase-buffer)
11116       (insert "$ " command "\n\n")
11117       (if gnus-view-pseudo-asynchronously
11118           (start-process "gnus-execute" (current-buffer) shell-file-name
11119                          shell-command-switch command)
11120         (call-process shell-file-name nil t nil
11121                       shell-command-switch command)))))
11122
11123 ;; Summary kill commands.
11124
11125 (defun gnus-summary-edit-global-kill (article)
11126   "Edit the \"global\" kill file."
11127   (interactive (list (gnus-summary-article-number)))
11128   (gnus-group-edit-global-kill article))
11129
11130 (defun gnus-summary-edit-local-kill ()
11131   "Edit a local kill file applied to the current newsgroup."
11132   (interactive)
11133   (setq gnus-current-headers (gnus-summary-article-header))
11134   (gnus-group-edit-local-kill
11135    (gnus-summary-article-number) gnus-newsgroup-name))
11136
11137 ;;; Header reading.
11138
11139 (defun gnus-read-header (id &optional header)
11140   "Read the headers of article ID and enter them into the Gnus system."
11141   (let ((group gnus-newsgroup-name)
11142         (gnus-override-method
11143          (or
11144           gnus-override-method
11145           (and (gnus-news-group-p gnus-newsgroup-name)
11146                (car (gnus-refer-article-methods)))))
11147         where)
11148     ;; First we check to see whether the header in question is already
11149     ;; fetched.
11150     (if (stringp id)
11151         ;; This is a Message-ID.
11152         (setq header (or header (gnus-id-to-header id)))
11153       ;; This is an article number.
11154       (setq header (or header (gnus-summary-article-header id))))
11155     (if (and header
11156              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11157         ;; We have found the header.
11158         header
11159       ;; If this is a sparse article, we have to nix out its
11160       ;; previous entry in the thread hashtb.
11161       (when (and header
11162                  (gnus-summary-article-sparse-p (mail-header-number header)))
11163         (let* ((parent (gnus-parent-id (mail-header-references header)))
11164                (thread (and parent (gnus-id-to-thread parent))))
11165           (when thread
11166             (delq (assq header thread) thread))))
11167       ;; We have to really fetch the header to this article.
11168       (save-excursion
11169         (set-buffer nntp-server-buffer)
11170         (when (setq where (gnus-request-head id group))
11171           (nnheader-fold-continuation-lines)
11172           (goto-char (point-max))
11173           (insert ".\n")
11174           (goto-char (point-min))
11175           (insert "211 ")
11176           (princ (cond
11177                   ((numberp id) id)
11178                   ((cdr where) (cdr where))
11179                   (header (mail-header-number header))
11180                   (t gnus-reffed-article-number))
11181                  (current-buffer))
11182           (insert " Article retrieved.\n"))
11183         (if (or (not where)
11184                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11185             ()                          ; Malformed head.
11186           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11187             (when (and (stringp id)
11188                        (not (string= (gnus-group-real-name group)
11189                                      (car where))))
11190               ;; If we fetched by Message-ID and the article came
11191               ;; from a different group, we fudge some bogus article
11192               ;; numbers for this article.
11193               (mail-header-set-number header gnus-reffed-article-number))
11194             (save-excursion
11195               (set-buffer gnus-summary-buffer)
11196               (decf gnus-reffed-article-number)
11197               (gnus-remove-header (mail-header-number header))
11198               (push header gnus-newsgroup-headers)
11199               (setq gnus-current-headers header)
11200               (push (mail-header-number header) gnus-newsgroup-limit)))
11201           header)))))
11202
11203 (defun gnus-remove-header (number)
11204   "Remove header NUMBER from `gnus-newsgroup-headers'."
11205   (if (and gnus-newsgroup-headers
11206            (= number (mail-header-number (car gnus-newsgroup-headers))))
11207       (pop gnus-newsgroup-headers)
11208     (let ((headers gnus-newsgroup-headers))
11209       (while (and (cdr headers)
11210                   (not (= number (mail-header-number (cadr headers)))))
11211         (pop headers))
11212       (when (cdr headers)
11213         (setcdr headers (cddr headers))))))
11214
11215 ;;;
11216 ;;; summary highlights
11217 ;;;
11218
11219 (defun gnus-highlight-selected-summary ()
11220   "Highlight selected article in summary buffer."
11221   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11222   (when gnus-summary-selected-face
11223     (save-excursion
11224       (let* ((beg (point-at-bol))
11225              (end (point-at-eol))
11226              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11227              (from (if (get-text-property beg gnus-mouse-face-prop)
11228                        beg
11229                      (or (next-single-property-change
11230                           beg gnus-mouse-face-prop nil end)
11231                          beg)))
11232              (to
11233               (if (= from end)
11234                   (- from 2)
11235                 (or (next-single-property-change
11236                      from gnus-mouse-face-prop nil end)
11237                     end))))
11238         ;; If no mouse-face prop on line we will have to = from = end,
11239         ;; so we highlight the entire line instead.
11240         (when (= (+ to 2) from)
11241           (setq from beg)
11242           (setq to end))
11243         (if gnus-newsgroup-selected-overlay
11244             ;; Move old overlay.
11245             (gnus-move-overlay
11246              gnus-newsgroup-selected-overlay from to (current-buffer))
11247           ;; Create new overlay.
11248           (gnus-overlay-put
11249            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11250            'face gnus-summary-selected-face))))))
11251
11252 (defvar gnus-summary-highlight-line-cached nil)
11253 (defvar gnus-summary-highlight-line-trigger nil)
11254
11255 (defun gnus-summary-highlight-line-0 ()
11256   (if (and (eq gnus-summary-highlight-line-trigger
11257                gnus-summary-highlight)
11258            gnus-summary-highlight-line-cached)
11259       gnus-summary-highlight-line-cached
11260     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11261           gnus-summary-highlight-line-cached
11262           (let* ((cond (list 'cond))
11263                  (c cond)
11264                  (list gnus-summary-highlight))
11265             (while list
11266               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11267                               nil))
11268               (setq c (cdr c)
11269                     list (cdr list)))
11270             (gnus-byte-compile (list 'lambda nil cond))))))
11271
11272 (defun gnus-summary-highlight-line ()
11273   "Highlight current line according to `gnus-summary-highlight'."
11274   (let* ((beg (point-at-bol))
11275          (article (or (gnus-summary-article-number) gnus-current-article))
11276          (score (or (cdr (assq article
11277                                gnus-newsgroup-scored))
11278                     gnus-summary-default-score 0))
11279          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11280          (inhibit-read-only t)
11281          (default gnus-summary-default-score)
11282          (default-high gnus-summary-default-high-score)
11283          (default-low gnus-summary-default-low-score)
11284          (uncached (and gnus-summary-use-undownloaded-faces
11285                         (memq article gnus-newsgroup-undownloaded))))
11286     (let ((face (funcall (gnus-summary-highlight-line-0))))
11287       (unless (eq face (get-text-property beg 'face))
11288         (gnus-put-text-property-excluding-characters-with-faces
11289          beg (point-at-eol) 'face
11290          (setq face (if (boundp face) (symbol-value face) face)))
11291         (when gnus-summary-highlight-line-function
11292           (funcall gnus-summary-highlight-line-function article face))))))
11293
11294 (defun gnus-update-read-articles (group unread &optional compute)
11295   "Update the list of read articles in GROUP.
11296 UNREAD is a sorted list."
11297   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11298         (info (gnus-get-info group))
11299         (prev 1)
11300         read)
11301     (if (or (not info) (not active))
11302         ;; There is no info on this group if it was, in fact,
11303         ;; killed.  Gnus stores no information on killed groups, so
11304         ;; there's nothing to be done.
11305         ;; One could store the information somewhere temporarily,
11306         ;; perhaps...  Hmmm...
11307         ()
11308       ;; Remove any negative articles numbers.
11309       (while (and unread (< (car unread) 0))
11310         (setq unread (cdr unread)))
11311       ;; Remove any expired article numbers
11312       (while (and unread (< (car unread) (car active)))
11313         (setq unread (cdr unread)))
11314       ;; Compute the ranges of read articles by looking at the list of
11315       ;; unread articles.
11316       (while unread
11317         (when (/= (car unread) prev)
11318           (push (if (= prev (1- (car unread))) prev
11319                   (cons prev (1- (car unread))))
11320                 read))
11321         (setq prev (1+ (car unread)))
11322         (setq unread (cdr unread)))
11323       (when (<= prev (cdr active))
11324         (push (cons prev (cdr active)) read))
11325       (setq read (if (> (length read) 1) (nreverse read) read))
11326       (if compute
11327           read
11328         (save-excursion
11329           (let (setmarkundo)
11330             ;; Propagate the read marks to the backend.
11331             (when (gnus-check-backend-function 'request-set-mark group)
11332               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11333                     (add (gnus-remove-from-range read (gnus-info-read info))))
11334                 (when (or add del)
11335                   (unless (gnus-check-group group)
11336                     (error "Can't open server for %s" group))
11337                   (gnus-request-set-mark
11338                    group (delq nil (list (if add (list add 'add '(read)))
11339                                          (if del (list del 'del '(read))))))
11340                   (setq setmarkundo
11341                         `(gnus-request-set-mark
11342                           ,group
11343                           ',(delq nil (list
11344                                        (if del (list del 'add '(read)))
11345                                        (if add (list add 'del '(read))))))))))
11346             (set-buffer gnus-group-buffer)
11347             (gnus-undo-register
11348               `(progn
11349                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11350                  (gnus-info-set-read ',info ',(gnus-info-read info))
11351                  (gnus-get-unread-articles-in-group ',info
11352                                                     (gnus-active ,group))
11353                  (gnus-group-update-group ,group t)
11354                  ,setmarkundo))))
11355         ;; Enter this list into the group info.
11356         (gnus-info-set-read info read)
11357         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11358         (gnus-get-unread-articles-in-group info (gnus-active group))
11359         t))))
11360
11361 (defun gnus-offer-save-summaries ()
11362   "Offer to save all active summary buffers."
11363   (let (buffers)
11364     ;; Go through all buffers and find all summaries.
11365     (dolist (buffer (buffer-list))
11366       (when (and (setq buffer (buffer-name buffer))
11367                  (string-match "Summary" buffer)
11368                  (save-excursion
11369                    (set-buffer buffer)
11370                    ;; We check that this is, indeed, a summary buffer.
11371                    (and (eq major-mode 'gnus-summary-mode)
11372                         ;; Also make sure this isn't bogus.
11373                         gnus-newsgroup-prepared
11374                         ;; Also make sure that this isn't a
11375                         ;; dead summary buffer.
11376                         (not gnus-dead-summary-mode))))
11377         (push buffer buffers)))
11378     ;; Go through all these summary buffers and offer to save them.
11379     (when buffers
11380       (save-excursion
11381         (map-y-or-n-p
11382          "Update summary buffer %s? "
11383          (lambda (buf)
11384            (switch-to-buffer buf)
11385            (gnus-summary-exit))
11386          buffers)))))
11387
11388 (defun gnus-summary-setup-default-charset ()
11389   "Setup newsgroup default charset."
11390   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11391       (setq gnus-newsgroup-charset nil)
11392     (let* ((ignored-charsets
11393             (or gnus-newsgroup-ephemeral-ignored-charsets
11394                 (append
11395                  (and gnus-newsgroup-name
11396                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11397                  gnus-newsgroup-ignored-charsets))))
11398       (setq gnus-newsgroup-charset
11399             (or gnus-newsgroup-ephemeral-charset
11400                 (and gnus-newsgroup-name
11401                      (gnus-parameter-charset gnus-newsgroup-name))
11402                 gnus-default-charset))
11403       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11404            ignored-charsets))))
11405
11406 ;;;
11407 ;;; Mime Commands
11408 ;;;
11409
11410 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11411   "Display the current article buffer fully MIME-buttonized.
11412 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11413 treated as multipart/mixed."
11414   (interactive "P")
11415   (require 'gnus-art)
11416   (let ((gnus-unbuttonized-mime-types nil)
11417         (gnus-mime-display-multipart-as-mixed show-all-parts))
11418     (gnus-summary-show-article)))
11419
11420 (defun gnus-summary-repair-multipart (article)
11421   "Add a Content-Type header to a multipart article without one."
11422   (interactive (list (gnus-summary-article-number)))
11423   (gnus-with-article article
11424     (message-narrow-to-head)
11425     (message-remove-header "Mime-Version")
11426     (goto-char (point-max))
11427     (insert "Mime-Version: 1.0\n")
11428     (widen)
11429     (when (search-forward "\n--" nil t)
11430       (let ((separator (buffer-substring (point) (point-at-eol))))
11431         (message-narrow-to-head)
11432         (message-remove-header "Content-Type")
11433         (goto-char (point-max))
11434         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11435                         separator))
11436         (widen))))
11437   (let (gnus-mark-article-hook)
11438     (gnus-summary-select-article t t nil article)))
11439
11440 (defun gnus-summary-toggle-display-buttonized ()
11441   "Toggle the buttonizing of the article buffer."
11442   (interactive)
11443   (require 'gnus-art)
11444   (if (setq gnus-inhibit-mime-unbuttonizing
11445             (not gnus-inhibit-mime-unbuttonizing))
11446       (let ((gnus-unbuttonized-mime-types nil))
11447         (gnus-summary-show-article))
11448     (gnus-summary-show-article)))
11449
11450 ;;;
11451 ;;; Generic summary marking commands
11452 ;;;
11453
11454 (defvar gnus-summary-marking-alist
11455   '((read gnus-del-mark "d")
11456     (unread gnus-unread-mark "u")
11457     (ticked gnus-ticked-mark "!")
11458     (dormant gnus-dormant-mark "?")
11459     (expirable gnus-expirable-mark "e"))
11460   "An alist of names/marks/keystrokes.")
11461
11462 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11463 (defvar gnus-summary-mark-map)
11464
11465 (defun gnus-summary-make-all-marking-commands ()
11466   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11467   (dolist (elem gnus-summary-marking-alist)
11468     (apply 'gnus-summary-make-marking-command elem)))
11469
11470 (defun gnus-summary-make-marking-command (name mark keystroke)
11471   (let ((map (make-sparse-keymap)))
11472     (define-key gnus-summary-generic-mark-map keystroke map)
11473     (dolist (lway `((next "next" next nil "n")
11474                     (next-unread "next unread" next t "N")
11475                     (prev "previous" prev nil "p")
11476                     (prev-unread "previous unread" prev t "P")
11477                     (nomove "" nil nil ,keystroke)))
11478       (let ((func (gnus-summary-make-marking-command-1
11479                    mark (car lway) lway name)))
11480         (setq func (eval func))
11481         (define-key map (nth 4 lway) func)))))
11482
11483 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11484   `(defun ,(intern
11485             (format "gnus-summary-put-mark-as-%s%s"
11486                     name (if (eq way 'nomove)
11487                              ""
11488                            (concat "-" (symbol-name way)))))
11489      (n)
11490      ,(format
11491        "Mark the current article as %s%s.
11492 If N, the prefix, then repeat N times.
11493 If N is negative, move in reverse order.
11494 The difference between N and the actual number of articles marked is
11495 returned."
11496        name (cadr lway))
11497      (interactive "p")
11498      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11499
11500 (defun gnus-summary-generic-mark (n mark move unread)
11501   "Mark N articles with MARK."
11502   (unless (eq major-mode 'gnus-summary-mode)
11503     (error "This command can only be used in the summary buffer"))
11504   (gnus-summary-show-thread)
11505   (let ((nummove
11506          (cond
11507           ((eq move 'next) 1)
11508           ((eq move 'prev) -1)
11509           (t 0))))
11510     (if (zerop nummove)
11511         (setq n 1)
11512       (when (< n 0)
11513         (setq n (abs n)
11514               nummove (* -1 nummove))))
11515     (while (and (> n 0)
11516                 (gnus-summary-mark-article nil mark)
11517                 (zerop (gnus-summary-next-subject nummove unread t)))
11518       (setq n (1- n)))
11519     (when (/= 0 n)
11520       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11521     (gnus-summary-recenter)
11522     (gnus-summary-position-point)
11523     (gnus-set-mode-line 'summary)
11524     n))
11525
11526 (defun gnus-summary-insert-articles (articles)
11527   (when (setq articles
11528               (gnus-sorted-difference articles
11529                                       (mapcar (lambda (h)
11530                                                 (mail-header-number h))
11531                                               gnus-newsgroup-headers)))
11532     (setq gnus-newsgroup-headers
11533           (gnus-merge 'list
11534                       gnus-newsgroup-headers
11535                       (gnus-fetch-headers articles)
11536                       'gnus-article-sort-by-number))
11537     ;; Suppress duplicates?
11538     (when gnus-suppress-duplicates
11539       (gnus-dup-suppress-articles))
11540
11541     ;; We might want to build some more threads first.
11542     (when (and gnus-fetch-old-headers
11543                (eq gnus-headers-retrieved-by 'nov))
11544       (if (eq gnus-fetch-old-headers 'invisible)
11545           (gnus-build-all-threads)
11546         (gnus-build-old-threads)))
11547     ;; Let the Gnus agent mark articles as read.
11548     (when gnus-agent
11549       (gnus-agent-get-undownloaded-list))
11550     ;; Remove list identifiers from subject
11551     (when gnus-list-identifiers
11552       (gnus-summary-remove-list-identifiers))
11553     ;; First and last article in this newsgroup.
11554     (when gnus-newsgroup-headers
11555       (setq gnus-newsgroup-begin
11556             (mail-header-number (car gnus-newsgroup-headers))
11557             gnus-newsgroup-end
11558             (mail-header-number
11559              (gnus-last-element gnus-newsgroup-headers))))
11560     (when gnus-use-scoring
11561       (gnus-possibly-score-headers))))
11562
11563 (defun gnus-summary-insert-old-articles (&optional all)
11564   "Insert all old articles in this group.
11565 If ALL is non-nil, already read articles become readable.
11566 If ALL is a number, fetch this number of articles."
11567   (interactive "P")
11568   (prog1
11569       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11570             older len)
11571         (setq older
11572               ;; Some nntp servers lie about their active range.  When
11573               ;; this happens, the active range can be in the millions.
11574               ;; Use a compressed range to avoid creating a huge list.
11575               (gnus-range-difference (list gnus-newsgroup-active) old))
11576         (setq len (gnus-range-length older))
11577         (cond
11578          ((null older) nil)
11579          ((numberp all)
11580           (if (< all len)
11581               (let ((older-range (nreverse older)))
11582                 (setq older nil)
11583
11584                 (while (> all 0)
11585                   (let* ((r (pop older-range))
11586                          (min (if (numberp r) r (car r)))
11587                          (max (if (numberp r) r (cdr r))))
11588                     (while (and (<= min max)
11589                                 (> all 0))
11590                       (push max older)
11591                       (setq all (1- all)
11592                             max (1- max))))))
11593             (setq older (gnus-uncompress-range older))))
11594          (all
11595           (setq older (gnus-uncompress-range older)))
11596          (t
11597           (when (and (numberp gnus-large-newsgroup)
11598                    (> len gnus-large-newsgroup))
11599               (let* ((cursor-in-echo-area nil)
11600                      (initial (gnus-parameter-large-newsgroup-initial
11601                                gnus-newsgroup-name))
11602                      (input
11603                       (read-string
11604                        (format
11605                         "How many articles from %s (%s %d): "
11606                         (gnus-limit-string
11607                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11608                         (if initial "max" "default")
11609                         len)
11610                        (if initial
11611                            (cons (number-to-string initial)
11612                                  0)))))
11613                 (unless (string-match "^[ \t]*$" input)
11614                   (setq all (string-to-number input))
11615                   (if (< all len)
11616                       (let ((older-range (nreverse older)))
11617                         (setq older nil)
11618
11619                         (while (> all 0)
11620                           (let* ((r (pop older-range))
11621                                  (min (if (numberp r) r (car r)))
11622                                  (max (if (numberp r) r (cdr r))))
11623                             (while (and (<= min max)
11624                                         (> all 0))
11625                               (push max older)
11626                               (setq all (1- all)
11627                                     max (1- max))))))))))
11628           (setq older (gnus-uncompress-range older))))
11629         (if (not older)
11630             (message "No old news.")
11631           (gnus-summary-insert-articles older)
11632           (gnus-summary-limit (gnus-sorted-nunion old older))))
11633     (gnus-summary-position-point)))
11634
11635 (defun gnus-summary-insert-new-articles ()
11636   "Insert all new articles in this group."
11637   (interactive)
11638   (prog1
11639       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11640             (old-active gnus-newsgroup-active)
11641             (nnmail-fetched-sources (list t))
11642             i new)
11643         (setq gnus-newsgroup-active
11644               (gnus-activate-group gnus-newsgroup-name 'scan))
11645         (setq i (cdr gnus-newsgroup-active))
11646         (while (> i (cdr old-active))
11647           (push i new)
11648           (decf i))
11649         (if (not new)
11650             (message "No gnus is bad news")
11651           (gnus-summary-insert-articles new)
11652           (setq gnus-newsgroup-unreads
11653                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11654           (gnus-summary-limit (gnus-sorted-nunion old new))))
11655     (gnus-summary-position-point)))
11656
11657 (gnus-summary-make-all-marking-commands)
11658
11659 (gnus-ems-redefine)
11660
11661 (provide 'gnus-sum)
11662
11663 (run-hooks 'gnus-sum-load-hook)
11664
11665 ;; Local Variables:
11666 ;; coding: iso-8859-1
11667 ;; End:
11668
11669 ;;; gnus-sum.el ends here