12cc4d8d2c7dca8e7f4bd107a0a334e2ee24da55
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 'mm-uu-dissect "mm-uu")
48 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
49   "Deuglify broken Outlook (Express) articles and redisplay."
50   t)
51 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
52 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
53 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
54
55 (defcustom gnus-kill-summary-on-exit t
56   "*If non-nil, kill the summary buffer when you exit from it.
57 If nil, the summary will become a \"*Dead Summary*\" buffer, and
58 it will be killed sometime later."
59   :group 'gnus-summary-exit
60   :type 'boolean)
61
62 (defcustom gnus-fetch-old-headers nil
63   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
64 If an unread article in the group refers to an older, already read (or
65 just marked as read) article, the old article will not normally be
66 displayed in the Summary buffer.  If this variable is non-nil, Gnus
67 will attempt to grab the headers to the old articles, and thereby
68 build complete threads.  If it has the value `some', only enough
69 headers to connect otherwise loose threads will be displayed.  This
70 variable can also be a number.  In that case, no more than that number
71 of old headers will be fetched.  If it has the value `invisible', all
72 old headers will be fetched, but none will be displayed.
73
74 The server has to support NOV for any of this to work."
75   :group 'gnus-thread
76   :type '(choice (const :tag "off" nil)
77                  (const some)
78                  number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-refer-thread-limit 200
82   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
83 If t, fetch all the available old headers."
84   :group 'gnus-thread
85   :type '(choice number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-summary-make-false-root 'adopt
89   "*nil means that Gnus won't gather loose threads.
90 If the root of a thread has expired or been read in a previous
91 session, the information necessary to build a complete thread has been
92 lost.  Instead of having many small sub-threads from this original thread
93 scattered all over the summary buffer, Gnus can gather them.
94
95 If non-nil, Gnus will try to gather all loose sub-threads from an
96 original thread into one large thread.
97
98 If this variable is non-nil, it should be one of `none', `adopt',
99 `dummy' or `empty'.
100
101 If this variable is `none', Gnus will not make a false root, but just
102 present the sub-threads after another.
103 If this variable is `dummy', Gnus will create a dummy root that will
104 have all the sub-threads as children.
105 If this variable is `adopt', Gnus will make one of the \"children\"
106 the parent and mark all the step-children as such.
107 If this variable is `empty', the \"children\" are printed with empty
108 subject fields.  (Or rather, they will be printed with a string
109 given by the `gnus-summary-same-subject' variable.)"
110   :group 'gnus-thread
111   :type '(choice (const :tag "off" nil)
112                  (const none)
113                  (const dummy)
114                  (const adopt)
115                  (const empty)))
116
117 (defcustom gnus-summary-make-false-root-always nil
118   "Always make a false dummy root."
119   :group 'gnus-thread
120   :type 'boolean)
121
122 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
123   "*A regexp to match subjects to be excluded from loose thread gathering.
124 As loose thread gathering is done on subjects only, that means that
125 there can be many false gatherings performed.  By rooting out certain
126 common subjects, gathering might become saner."
127   :group 'gnus-thread
128   :type 'regexp)
129
130 (defcustom gnus-summary-gather-subject-limit nil
131   "*Maximum length of subject comparisons when gathering loose threads.
132 Use nil to compare full subjects.  Setting this variable to a low
133 number will help gather threads that have been corrupted by
134 newsreaders chopping off subject lines, but it might also mean that
135 unrelated articles that have subject that happen to begin with the
136 same few characters will be incorrectly gathered.
137
138 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
139 comparing subjects."
140   :group 'gnus-thread
141   :type '(choice (const :tag "off" nil)
142                  (const fuzzy)
143                  (sexp :menu-tag "on" t)))
144
145 (defcustom gnus-simplify-subject-functions nil
146   "List of functions taking a string argument that simplify subjects.
147 The functions are applied recursively.
148
149 Useful functions to put in this list include:
150 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
151 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 \(they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-default-high-score 0
216   "*Default threshold for a high scored article.
217 An article will be highlighted as high scored if its score is greater
218 than this score."
219   :group 'gnus-score-default
220   :type 'integer)
221
222 (defcustom gnus-summary-default-low-score 0
223   "*Default threshold for a low scored article.
224 An article will be highlighted as low scored if its score is smaller
225 than this score."
226   :group 'gnus-score-default
227   :type 'integer)
228
229 (defcustom gnus-summary-zcore-fuzz 0
230   "*Fuzziness factor for the zcore in the summary buffer.
231 Articles with scores closer than this to `gnus-summary-default-score'
232 will not be marked."
233   :group 'gnus-summary-format
234   :type 'integer)
235
236 (defcustom gnus-simplify-subject-fuzzy-regexp nil
237   "*Strings to be removed when doing fuzzy matches.
238 This can either be a regular expression or list of regular expressions
239 that will be removed from subject strings if fuzzy subject
240 simplification is selected."
241   :group 'gnus-thread
242   :type '(repeat regexp))
243
244 (defcustom gnus-show-threads t
245   "*If non-nil, display threads in summary mode."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-hide-subtree nil
250   "*If non-nil, hide all threads initially.
251 This can be a predicate specifier which says which threads to hide.
252 If threads are hidden, you have to run the command
253 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
254 to expose hidden threads."
255   :group 'gnus-thread
256   :type '(radio (sexp :format "Non-nil\n"
257                       :match (lambda (widget value)
258                                (not (or (consp value) (functionp value))))
259                       :value t)
260                 (const nil)
261                 (sexp :tag "Predicate specifier" :size 0)))
262
263 (defcustom gnus-thread-hide-killed t
264   "*If non-nil, hide killed threads automatically."
265   :group 'gnus-thread
266   :type 'boolean)
267
268 (defcustom gnus-thread-ignore-subject t
269   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
270 If nil, articles that have different subjects from their parents will
271 start separate threads."
272   :group 'gnus-thread
273   :type 'boolean)
274
275 (defcustom gnus-thread-operation-ignore-subject t
276   "*If non-nil, subjects will be ignored when doing thread commands.
277 This affects commands like `gnus-summary-kill-thread' and
278 `gnus-summary-lower-thread'.
279
280 If this variable is nil, articles in the same thread with different
281 subjects will not be included in the operation in question.  If this
282 variable is `fuzzy', only articles that have subjects that are fuzzily
283 equal will be included."
284   :group 'gnus-thread
285   :type '(choice (const :tag "off" nil)
286                  (const fuzzy)
287                  (sexp :tag "on" t)))
288
289 (defcustom gnus-thread-indent-level 4
290   "*Number that says how much each sub-thread should be indented."
291   :group 'gnus-thread
292   :type 'integer)
293
294 (defcustom gnus-auto-extend-newsgroup t
295   "*If non-nil, extend newsgroup forward and backward when requested."
296   :group 'gnus-summary-choose
297   :type 'boolean)
298
299 (defcustom gnus-auto-select-first t
300   "*If non-nil, select the article under point.
301 Which article this is is controlled by the `gnus-auto-select-subject'
302 variable.
303
304 If you want to prevent automatic selection of articles in some
305 newsgroups, set the variable to nil in `gnus-select-group-hook'."
306   :group 'gnus-group-select
307   :type '(choice (const :tag "none" nil)
308                  (sexp :menu-tag "first" t)))
309
310 (defcustom gnus-auto-select-subject 'unread
311   "*Says what subject to place under point when entering a group.
312
313 This variable can either be the symbols `first' (place point on the
314 first subject), `unread' (place point on the subject line of the first
315 unread article), `best' (place point on the subject line of the
316 higest-scored article), `unseen' (place point on the subject line of
317 the first unseen article), 'unseen-or-unread' (place point on the subject
318 line of the first unseen article or, if all article have been seen, on the
319 subject line of the first unread article), or a function to be called to
320 place point on some subject line."
321   :group 'gnus-group-select
322   :type '(choice (const best)
323                  (const unread)
324                  (const first)
325                  (const unseen)
326                  (const unseen-or-unread)))
327
328 (defcustom gnus-auto-select-next t
329   "*If non-nil, offer to go to the next group from the end of the previous.
330 If the value is t and the next newsgroup is empty, Gnus will exit
331 summary mode and go back to group mode.  If the value is neither nil
332 nor t, Gnus will select the following unread newsgroup.  In
333 particular, if the value is the symbol `quietly', the next unread
334 newsgroup will be selected without any confirmation, and if it is
335 `almost-quietly', the next group will be selected without any
336 confirmation if you are located on the last article in the group.
337 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
338 will go to the next group without confirmation."
339   :group 'gnus-summary-maneuvering
340   :type '(choice (const :tag "off" nil)
341                  (const quietly)
342                  (const almost-quietly)
343                  (const slightly-quietly)
344                  (sexp :menu-tag "on" t)))
345
346 (defcustom gnus-auto-select-same nil
347   "*If non-nil, select the next article with the same subject.
348 If there are no more articles with the same subject, go to
349 the first unread article."
350   :group 'gnus-summary-maneuvering
351   :type 'boolean)
352
353 (defcustom gnus-auto-goto-ignores 'unfetched
354   "*Says how to handle unfetched articles when maneuvering.
355
356 This variable can either be the symbols nil (maneuver to any
357 article), `undownloaded' (maneuvering while unplugged ignores articles
358 that have not been fetched), `always-undownloaded' (maneuvering always
359 ignores articles that have not been fetched), `unfetched' (maneuvering
360 ignores articles whose headers have not been fetched).
361
362 NOTE: The list of unfetched articles will always be nil when plugged
363 and, when unplugged, a subset of the undownloaded article list."
364   :group 'gnus-summary-maneuvering
365   :type '(choice (const :tag "None" nil)
366                  (const :tag "Undownloaded when unplugged" undownloaded)
367                  (const :tag "Undownloaded" always-undownloaded)
368                  (const :tag "Unfetched" unfetched)))
369
370 (defcustom gnus-summary-check-current nil
371   "*If non-nil, consider the current article when moving.
372 The \"unread\" movement commands will stay on the same line if the
373 current article is unread."
374   :group 'gnus-summary-maneuvering
375   :type 'boolean)
376
377 (defcustom gnus-auto-center-summary t
378   "*If non-nil, always center the current summary buffer.
379 In particular, if `vertical' do only vertical recentering.  If non-nil
380 and non-`vertical', do both horizontal and vertical recentering."
381   :group 'gnus-summary-maneuvering
382   :type '(choice (const :tag "none" nil)
383                  (const vertical)
384                  (integer :tag "height")
385                  (sexp :menu-tag "both" t)))
386
387 (defvar gnus-auto-center-group t
388   "*If non-nil, always center the group buffer.")
389
390 (defcustom gnus-show-all-headers nil
391   "*If non-nil, don't hide any headers."
392   :group 'gnus-article-hiding
393   :group 'gnus-article-headers
394   :type 'boolean)
395
396 (defcustom gnus-summary-ignore-duplicates nil
397   "*If non-nil, ignore articles with identical Message-ID headers."
398   :group 'gnus-summary
399   :type 'boolean)
400
401 (defcustom gnus-single-article-buffer t
402   "*If non-nil, display all articles in the same buffer.
403 If nil, each group will get its own article buffer."
404   :group 'gnus-article-various
405   :type 'boolean)
406
407 (defcustom gnus-break-pages t
408   "*If non-nil, do page breaking on articles.
409 The page delimiter is specified by the `gnus-page-delimiter'
410 variable."
411   :group 'gnus-article-various
412   :type 'boolean)
413
414 (defcustom gnus-move-split-methods nil
415   "*Variable used to suggest where articles are to be moved to.
416 It uses the same syntax as the `gnus-split-methods' variable.
417 However, whereas `gnus-split-methods' specifies file names as targets,
418 this variable specifies group names."
419   :group 'gnus-summary-mail
420   :type '(repeat (choice (list :value (fun) function)
421                          (cons :value ("" "") regexp (repeat string))
422                          (sexp :value nil))))
423
424 (defcustom gnus-unread-mark ?           ;Whitespace
425   "*Mark used for unread articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-ticked-mark ?!
430   "*Mark used for ticked articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-dormant-mark ??
435   "*Mark used for dormant articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-del-mark ?r
440   "*Mark used for del'd articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-read-mark ?R
445   "*Mark used for read articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-expirable-mark ?E
450   "*Mark used for expirable articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-killed-mark ?K
455   "*Mark used for killed articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-spam-mark ?$
460   "*Mark used for spam articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-souped-mark ?F
465   "*Mark used for souped articles."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-kill-file-mark ?X
470   "*Mark used for articles killed by kill files."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-low-score-mark ?Y
475   "*Mark used for articles with a low score."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-catchup-mark ?C
480   "*Mark used for articles that are caught up."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-replied-mark ?A
485   "*Mark used for articles that have been replied to."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-forwarded-mark ?F
490   "*Mark used for articles that have been forwarded."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-recent-mark ?N
495   "*Mark used for articles that are recent."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-cached-mark ?*
500   "*Mark used for articles that are in the cache."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-saved-mark ?S
505   "*Mark used for articles that have been saved."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-unseen-mark ?.
510   "*Mark used for articles that haven't been seen."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-no-mark ?               ;Whitespace
515   "*Mark used for articles that have no other secondary mark."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-ancient-mark ?O
520   "*Mark used for ancient articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-sparse-mark ?Q
525   "*Mark used for sparsely reffed articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-canceled-mark ?G
530   "*Mark used for canceled articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-duplicate-mark ?M
535   "*Mark used for duplicate articles."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-undownloaded-mark ?-
540   "*Mark used for articles that weren't downloaded."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-downloaded-mark ?+
545   "*Mark used for articles that were downloaded."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-downloadable-mark ?%
550   "*Mark used for articles that are to be downloaded."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-unsendable-mark ?=
555   "*Mark used for articles that won't be sent."
556   :group 'gnus-summary-marks
557   :type 'character)
558
559 (defcustom gnus-score-over-mark ?+
560   "*Score mark used for articles with high scores."
561   :group 'gnus-summary-marks
562   :type 'character)
563
564 (defcustom gnus-score-below-mark ?-
565   "*Score mark used for articles with low scores."
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-empty-thread-mark ?     ;Whitespace
570   "*There is no thread under the article."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-not-empty-thread-mark ?=
575   "*There is a thread under the article."
576   :group 'gnus-summary-marks
577   :type 'character)
578
579 (defcustom gnus-view-pseudo-asynchronously nil
580   "*If non-nil, Gnus will view pseudo-articles asynchronously."
581   :group 'gnus-extract-view
582   :type 'boolean)
583
584 (defcustom gnus-auto-expirable-marks
585   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
586         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
587         gnus-souped-mark gnus-duplicate-mark)
588   "*The list of marks converted into expiration if a group is auto-expirable."
589   :version "21.1"
590   :group 'gnus-summary
591   :type '(repeat character))
592
593 (defcustom gnus-inhibit-user-auto-expire t
594   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
595   :version "21.1"
596   :group 'gnus-summary
597   :type 'boolean)
598
599 (defcustom gnus-view-pseudos nil
600   "*If `automatic', pseudo-articles will be viewed automatically.
601 If `not-confirm', pseudos will be viewed automatically, and the user
602 will not be asked to confirm the command."
603   :group 'gnus-extract-view
604   :type '(choice (const :tag "off" nil)
605                  (const automatic)
606                  (const not-confirm)))
607
608 (defcustom gnus-view-pseudos-separately t
609   "*If non-nil, one pseudo-article will be created for each file to be viewed.
610 If nil, all files that use the same viewing command will be given as a
611 list of parameters to that command."
612   :group 'gnus-extract-view
613   :type 'boolean)
614
615 (defcustom gnus-insert-pseudo-articles t
616   "*If non-nil, insert pseudo-articles when decoding articles."
617   :group 'gnus-extract-view
618   :type 'boolean)
619
620 (defcustom gnus-summary-dummy-line-format
621   "   %(:                             :%) %S\n"
622   "*The format specification for the dummy roots in the summary buffer.
623 It works along the same lines as a normal formatting string,
624 with some simple extensions.
625
626 %S  The subject
627
628 General format specifiers can also be used.
629 See `(gnus)Formatting Variables'."
630   :link '(custom-manual "(gnus)Formatting Variables")
631   :group 'gnus-threading
632   :type 'string)
633
634 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
635   "*The format specification for the summary mode line.
636 It works along the same lines as a normal formatting string,
637 with some simple extensions:
638
639 %G  Group name
640 %p  Unprefixed group name
641 %A  Current article number
642 %z  Current article score
643 %V  Gnus version
644 %U  Number of unread articles in the group
645 %e  Number of unselected articles in the group
646 %Z  A string with unread/unselected article counts
647 %g  Shortish group name
648 %S  Subject of the current article
649 %u  User-defined spec
650 %s  Current score file name
651 %d  Number of dormant articles
652 %r  Number of articles that have been marked as read in this session
653 %E  Number of articles expunged by the score files"
654   :group 'gnus-summary-format
655   :type 'string)
656
657 (defcustom gnus-list-identifiers nil
658   "Regexp that matches list identifiers to be removed from subject.
659 This can also be a list of regexps."
660   :version "21.1"
661   :group 'gnus-summary-format
662   :group 'gnus-article-hiding
663   :type '(choice (const :tag "none" nil)
664                  (regexp :value ".*")
665                  (repeat :value (".*") regexp)))
666
667 (defcustom gnus-summary-mark-below 0
668   "*Mark all articles with a score below this variable as read.
669 This variable is local to each summary buffer and usually set by the
670 score file."
671   :group 'gnus-score-default
672   :type 'integer)
673
674 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
675   "*List of functions used for sorting articles in the summary buffer.
676
677 Each function takes two articles and returns non-nil if the first
678 article should be sorted before the other.  If you use more than one
679 function, the primary sort function should be the last.  You should
680 probably always include `gnus-article-sort-by-number' in the list of
681 sorting functions -- preferably first.  Also note that sorting by date
682 is often much slower than sorting by number, and the sorting order is
683 very similar.  (Sorting by date means sorting by the time the message
684 was sent, sorting by number means sorting by arrival time.)
685
686 Ready-made functions include `gnus-article-sort-by-number',
687 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
688 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
689 and `gnus-article-sort-by-score'.
690
691 When threading is turned on, the variable `gnus-thread-sort-functions'
692 controls how articles are sorted."
693   :group 'gnus-summary-sort
694   :type '(repeat (choice (function-item gnus-article-sort-by-number)
695                          (function-item gnus-article-sort-by-author)
696                          (function-item gnus-article-sort-by-subject)
697                          (function-item gnus-article-sort-by-date)
698                          (function-item gnus-article-sort-by-score)
699                          (function-item gnus-article-sort-by-random)
700                          (function :tag "other"))))
701
702 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
703   "*List of functions used for sorting threads in the summary buffer.
704 By default, threads are sorted by article number.
705
706 Each function takes two threads and returns non-nil if the first
707 thread should be sorted before the other.  If you use more than one
708 function, the primary sort function should be the last.  You should
709 probably always include `gnus-thread-sort-by-number' in the list of
710 sorting functions -- preferably first.  Also note that sorting by date
711 is often much slower than sorting by number, and the sorting order is
712 very similar.  (Sorting by date means sorting by the time the message
713 was sent, sorting by number means sorting by arrival time.)
714
715 Ready-made functions include `gnus-thread-sort-by-number',
716 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
717 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
718 `gnus-thread-sort-by-most-recent-number',
719 `gnus-thread-sort-by-most-recent-date',
720 `gnus-thread-sort-by-random', and
721 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
722
723 When threading is turned off, the variable
724 `gnus-article-sort-functions' controls how articles are sorted."
725   :group 'gnus-summary-sort
726   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
727                          (function-item gnus-thread-sort-by-author)
728                          (function-item gnus-thread-sort-by-subject)
729                          (function-item gnus-thread-sort-by-date)
730                          (function-item gnus-thread-sort-by-score)
731                          (function-item gnus-thread-sort-by-total-score)
732                          (function-item gnus-thread-sort-by-random)
733                          (function :tag "other"))))
734
735 (defcustom gnus-thread-score-function '+
736   "*Function used for calculating the total score of a thread.
737
738 The function is called with the scores of the article and each
739 subthread and should then return the score of the thread.
740
741 Some functions you can use are `+', `max', or `min'."
742   :group 'gnus-summary-sort
743   :type 'function)
744
745 (defcustom gnus-summary-expunge-below nil
746   "All articles that have a score less than this variable will be expunged.
747 This variable is local to the summary buffers."
748   :group 'gnus-score-default
749   :type '(choice (const :tag "off" nil)
750                  integer))
751
752 (defcustom gnus-thread-expunge-below nil
753   "All threads that have a total score less than this variable will be expunged.
754 See `gnus-thread-score-function' for en explanation of what a
755 \"thread score\" is.
756
757 This variable is local to the summary buffers."
758   :group 'gnus-threading
759   :group 'gnus-score-default
760   :type '(choice (const :tag "off" nil)
761                  integer))
762
763 (defcustom gnus-summary-mode-hook nil
764   "*A hook for Gnus summary mode.
765 This hook is run before any variables are set in the summary buffer."
766   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
767   :group 'gnus-summary-various
768   :type 'hook)
769
770 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
771 (when (featurep 'xemacs)
772   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
773   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
774   (add-hook 'gnus-summary-mode-hook
775             'gnus-xmas-switch-horizontal-scrollbar-off))
776
777 (defcustom gnus-summary-menu-hook nil
778   "*Hook run after the creation of the summary mode menu."
779   :group 'gnus-summary-visual
780   :type 'hook)
781
782 (defcustom gnus-summary-exit-hook nil
783   "*A hook called on exit from the summary buffer.
784 It will be called with point in the group buffer."
785   :group 'gnus-summary-exit
786   :type 'hook)
787
788 (defcustom gnus-summary-prepare-hook nil
789   "*A hook called after the summary buffer has been generated.
790 If you want to modify the summary buffer, you can use this hook."
791   :group 'gnus-summary-various
792   :type 'hook)
793
794 (defcustom gnus-summary-prepared-hook nil
795   "*A hook called as the last thing after the summary buffer has been generated."
796   :group 'gnus-summary-various
797   :type 'hook)
798
799 (defcustom gnus-summary-generate-hook nil
800   "*A hook run just before generating the summary buffer.
801 This hook is commonly used to customize threading variables and the
802 like."
803   :group 'gnus-summary-various
804   :type 'hook)
805
806 (defcustom gnus-select-group-hook nil
807   "*A hook called when a newsgroup is selected.
808
809 If you'd like to simplify subjects like the
810 `gnus-summary-next-same-subject' command does, you can use the
811 following hook:
812
813  (add-hook gnus-select-group-hook
814            (lambda ()
815              (mapcar (lambda (header)
816                        (mail-header-set-subject
817                         header
818                         (gnus-simplify-subject
819                          (mail-header-subject header) 're-only)))
820                      gnus-newsgroup-headers)))"
821   :group 'gnus-group-select
822   :type 'hook)
823
824 (defcustom gnus-select-article-hook nil
825   "*A hook called when an article is selected."
826   :group 'gnus-summary-choose
827   :options '(gnus-agent-fetch-selected-article)
828   :type 'hook)
829
830 (defcustom gnus-visual-mark-article-hook
831   (list 'gnus-highlight-selected-summary)
832   "*Hook run after selecting an article in the summary buffer.
833 It is meant to be used for highlighting the article in some way.  It
834 is not run if `gnus-visual' is nil."
835   :group 'gnus-summary-visual
836   :type 'hook)
837
838 (defcustom gnus-parse-headers-hook nil
839   "*A hook called before parsing the headers."
840   :group 'gnus-various
841   :type 'hook)
842
843 (defcustom gnus-exit-group-hook nil
844   "*A hook called when exiting summary mode.
845 This hook is not called from the non-updating exit commands like `Q'."
846   :group 'gnus-various
847   :type 'hook)
848
849 (defcustom gnus-summary-update-hook
850   (list 'gnus-summary-highlight-line)
851   "*A hook called when a summary line is changed.
852 The hook will not be called if `gnus-visual' is nil.
853
854 The default function `gnus-summary-highlight-line' will
855 highlight the line according to the `gnus-summary-highlight'
856 variable."
857   :group 'gnus-summary-visual
858   :type 'hook)
859
860 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
861   "*A hook called when an article is selected for the first time.
862 The hook is intended to mark an article as read (or unread)
863 automatically when it is selected."
864   :group 'gnus-summary-choose
865   :type 'hook)
866
867 (defcustom gnus-group-no-more-groups-hook nil
868   "*A hook run when returning to group mode having no more (unread) groups."
869   :group 'gnus-group-select
870   :type 'hook)
871
872 (defcustom gnus-ps-print-hook nil
873   "*A hook run before ps-printing something from Gnus."
874   :group 'gnus-summary
875   :type 'hook)
876
877 (defcustom gnus-summary-article-move-hook nil
878   "*A hook called after an article is moved, copied, respooled, or crossposted."
879   :group 'gnus-summary
880   :type 'hook)
881
882 (defcustom gnus-summary-article-delete-hook nil
883   "*A hook called after an article is deleted."
884   :group 'gnus-summary
885   :type 'hook)
886
887 (defcustom gnus-summary-article-expire-hook nil
888   "*A hook called after an article is expired."
889   :group 'gnus-summary
890   :type 'hook)
891
892 (defcustom gnus-summary-display-arrow
893   (and (fboundp 'display-graphic-p)
894        (display-graphic-p))
895   "*If non-nil, display an arrow highlighting the current article."
896   :version "21.1"
897   :group 'gnus-summary
898   :type 'boolean)
899
900 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
901   "Face used for highlighting the current article in the summary buffer."
902   :group 'gnus-summary-visual
903   :type 'face)
904
905 (defvar gnus-tmp-downloaded nil)
906
907 (defcustom gnus-summary-highlight
908   '(((eq mark gnus-canceled-mark)
909      . gnus-summary-cancelled-face)
910     ((and uncached (> score default-high))
911      . gnus-summary-high-undownloaded-face)
912     ((and uncached (< score default-low))
913      . gnus-summary-low-undownloaded-face)
914     (uncached
915      . gnus-summary-normal-undownloaded-face)
916     ((and (> score default-high)
917           (or (eq mark gnus-dormant-mark)
918               (eq mark gnus-ticked-mark)))
919      . gnus-summary-high-ticked-face)
920     ((and (< score default-low)
921           (or (eq mark gnus-dormant-mark)
922               (eq mark gnus-ticked-mark)))
923      . gnus-summary-low-ticked-face)
924     ((or (eq mark gnus-dormant-mark)
925          (eq mark gnus-ticked-mark))
926      . gnus-summary-normal-ticked-face)
927     ((and (> score default-high) (eq mark gnus-ancient-mark))
928      . gnus-summary-high-ancient-face)
929     ((and (< score default-low) (eq mark gnus-ancient-mark))
930      . gnus-summary-low-ancient-face)
931     ((eq mark gnus-ancient-mark)
932      . gnus-summary-normal-ancient-face)
933     ((and (> score default-high) (eq mark gnus-unread-mark))
934      . gnus-summary-high-unread-face)
935     ((and (< score default-low) (eq mark gnus-unread-mark))
936      . gnus-summary-low-unread-face)
937     ((eq mark gnus-unread-mark)
938      . gnus-summary-normal-unread-face)
939     ((> score default-high)
940      . gnus-summary-high-read-face)
941     ((< score default-low)
942      . gnus-summary-low-read-face)
943     (t
944      . gnus-summary-normal-read-face))
945   "*Controls the highlighting of summary buffer lines.
946
947 A list of (FORM . FACE) pairs.  When deciding how a a particular
948 summary line should be displayed, each form is evaluated.  The content
949 of the face field after the first true form is used.  You can change
950 how those summary lines are displayed, by editing the face field.
951
952 You can use the following variables in the FORM field.
953
954 score:        The article's score
955 default:      The default article score.
956 default-high: The default score for high scored articles.
957 default-low:  The default score for low scored articles.
958 below:        The score below which articles are automatically marked as read.
959 mark:         The article's mark.
960 uncached:     Non-nil if the article is uncached."
961   :group 'gnus-summary-visual
962   :type '(repeat (cons (sexp :tag "Form" nil)
963                        face)))
964
965 (defcustom gnus-alter-header-function nil
966   "Function called to allow alteration of article header structures.
967 The function is called with one parameter, the article header vector,
968 which it may alter in any way."
969   :type '(choice (const :tag "None" nil)
970                  function)
971   :group 'gnus-summary)
972
973 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
974   "Variable that says which function should be used to decode a string with encoded words.")
975
976 (defcustom gnus-extra-headers '(To Newsgroups)
977   "*Extra headers to parse."
978   :version "21.1"
979   :group 'gnus-summary
980   :type '(repeat symbol))
981
982 (defcustom gnus-ignored-from-addresses
983   (and user-mail-address (regexp-quote user-mail-address))
984   "*Regexp of From headers that may be suppressed in favor of To headers."
985   :version "21.1"
986   :group 'gnus-summary
987   :type 'regexp)
988
989 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
990   "List of charsets that should be ignored.
991 When these charsets are used in the \"charset\" parameter, the
992 default charset will be used instead."
993   :version "21.1"
994   :type '(repeat symbol)
995   :group 'gnus-charset)
996
997 (gnus-define-group-parameter
998  ignored-charsets
999  :type list
1000  :function-document
1001  "Return the ignored charsets of GROUP."
1002  :variable gnus-group-ignored-charsets-alist
1003  :variable-default
1004  '(("alt\\.chinese\\.text" iso-8859-1))
1005  :variable-document
1006  "Alist of regexps (to match group names) and charsets that should be ignored.
1007 When these charsets are used in the \"charset\" parameter, the
1008 default charset will be used instead."
1009  :variable-group gnus-charset
1010  :variable-type '(repeat (cons (regexp :tag "Group")
1011                                (repeat symbol)))
1012  :parameter-type '(choice :tag "Ignored charsets"
1013                           :value nil
1014                           (repeat (symbol)))
1015  :parameter-document       "\
1016 List of charsets that should be ignored.
1017
1018 When these charsets are used in the \"charset\" parameter, the
1019 default charset will be used instead.")
1020
1021 (defcustom gnus-group-highlight-words-alist nil
1022   "Alist of group regexps and highlight regexps.
1023 This variable uses the same syntax as `gnus-emphasis-alist'."
1024   :version "21.1"
1025   :type '(repeat (cons (regexp :tag "Group")
1026                        (repeat (list (regexp :tag "Highlight regexp")
1027                                      (number :tag "Group for entire word" 0)
1028                                      (number :tag "Group for displayed part" 0)
1029                                      (symbol :tag "Face"
1030                                              gnus-emphasis-highlight-words)))))
1031   :group 'gnus-summary-visual)
1032
1033 (defcustom gnus-summary-show-article-charset-alist
1034   nil
1035   "Alist of number and charset.
1036 The article will be shown with the charset corresponding to the
1037 numbered argument.
1038 For example: ((1 . cn-gb-2312) (2 . big5))."
1039   :version "21.1"
1040   :type '(repeat (cons (number :tag "Argument" 1)
1041                        (symbol :tag "Charset")))
1042   :group 'gnus-charset)
1043
1044 (defcustom gnus-preserve-marks t
1045   "Whether marks are preserved when moving, copying and respooling messages."
1046   :version "21.1"
1047   :type 'boolean
1048   :group 'gnus-summary-marks)
1049
1050 (defcustom gnus-alter-articles-to-read-function nil
1051   "Function to be called to alter the list of articles to be selected."
1052   :type '(choice (const nil) function)
1053   :group 'gnus-summary)
1054
1055 (defcustom gnus-orphan-score nil
1056   "*All orphans get this score added.  Set in the score file."
1057   :group 'gnus-score-default
1058   :type '(choice (const nil)
1059                  integer))
1060
1061 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1062   "*A regexp to match MIME parts when saving multiple parts of a
1063 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1064 This regexp will be used by default when prompting the user for which
1065 type of files to save."
1066   :group 'gnus-summary
1067   :type 'regexp)
1068
1069 (defcustom gnus-read-all-available-headers nil
1070   "Whether Gnus should parse all headers made available to it.
1071 This is mostly relevant for slow back ends where the user may
1072 wish to widen the summary buffer to include all headers
1073 that were fetched.  Say, for nnultimate groups."
1074   :group 'gnus-summary
1075   :type '(choice boolean regexp))
1076
1077 (defcustom gnus-summary-muttprint-program "muttprint"
1078   "Command (and optional arguments) used to run Muttprint."
1079   :version "21.3"
1080   :group 'gnus-summary
1081   :type 'string)
1082
1083 (defcustom gnus-article-loose-mime nil
1084   "If non-nil, don't require MIME-Version header.
1085 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1086 supply the MIME-Version header or deliberately strip it From the mail.
1087 Set it to non-nil, Gnus will treat some articles as MIME even if
1088 the MIME-Version header is missed."
1089   :version "21.3"
1090   :type 'boolean
1091   :group 'gnus-article-mime)
1092
1093 (defcustom gnus-article-emulate-mime t
1094   "If non-nil, use MIME emulation for uuencode and the like.
1095 This means that Gnus will search message bodies for text that look
1096 like uuencoded bits, yEncoded bits, and so on, and present that using
1097 the normal Gnus MIME machinery."
1098   :type 'boolean
1099   :group 'gnus-article-mime)
1100
1101 ;;; Internal variables
1102
1103 (defvar gnus-summary-display-cache nil)
1104 (defvar gnus-article-mime-handles nil)
1105 (defvar gnus-article-decoded-p nil)
1106 (defvar gnus-article-charset nil)
1107 (defvar gnus-article-ignored-charsets nil)
1108 (defvar gnus-scores-exclude-files nil)
1109 (defvar gnus-page-broken nil)
1110
1111 (defvar gnus-original-article nil)
1112 (defvar gnus-article-internal-prepare-hook nil)
1113 (defvar gnus-newsgroup-process-stack nil)
1114
1115 (defvar gnus-thread-indent-array nil)
1116 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1117 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1118   "Function called to sort the articles within a thread after it has been gathered together.")
1119
1120 (defvar gnus-summary-save-parts-type-history nil)
1121 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1122
1123 ;; Avoid highlighting in kill files.
1124 (defvar gnus-summary-inhibit-highlight nil)
1125 (defvar gnus-newsgroup-selected-overlay nil)
1126 (defvar gnus-inhibit-limiting nil)
1127 (defvar gnus-newsgroup-adaptive-score-file nil)
1128 (defvar gnus-current-score-file nil)
1129 (defvar gnus-current-move-group nil)
1130 (defvar gnus-current-copy-group nil)
1131 (defvar gnus-current-crosspost-group nil)
1132 (defvar gnus-newsgroup-display nil)
1133
1134 (defvar gnus-newsgroup-dependencies nil)
1135 (defvar gnus-newsgroup-adaptive nil)
1136 (defvar gnus-summary-display-article-function nil)
1137 (defvar gnus-summary-highlight-line-function nil
1138   "Function called after highlighting a summary line.")
1139
1140 (defvar gnus-summary-line-format-alist
1141   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1142     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1143     (?s gnus-tmp-subject-or-nil ?s)
1144     (?n gnus-tmp-name ?s)
1145     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1146         ?s)
1147     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1148             gnus-tmp-from) ?s)
1149     (?F gnus-tmp-from ?s)
1150     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1151     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1152     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1153     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1154     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1155     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1156     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1157     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1158     (?L gnus-tmp-lines ?s)
1159     (?O gnus-tmp-downloaded ?c)
1160     (?I gnus-tmp-indentation ?s)
1161     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1162     (?R gnus-tmp-replied ?c)
1163     (?\[ gnus-tmp-opening-bracket ?c)
1164     (?\] gnus-tmp-closing-bracket ?c)
1165     (?\> (make-string gnus-tmp-level ? ) ?s)
1166     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1167     (?i gnus-tmp-score ?d)
1168     (?z gnus-tmp-score-char ?c)
1169     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1170     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1171     (?U gnus-tmp-unread ?c)
1172     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1173         ?s)
1174     (?t (gnus-summary-number-of-articles-in-thread
1175          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1176         ?d)
1177     (?e (gnus-summary-number-of-articles-in-thread
1178          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1179         ?c)
1180     (?u gnus-tmp-user-defined ?s)
1181     (?P (gnus-pick-line-number) ?d)
1182     (?B gnus-tmp-thread-tree-header-string ?s)
1183     (user-date (gnus-user-date
1184                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1185   "An alist of format specifications that can appear in summary lines.
1186 These are paired with what variables they correspond with, along with
1187 the type of the variable (string, integer, character, etc).")
1188
1189 (defvar gnus-summary-dummy-line-format-alist
1190   `((?S gnus-tmp-subject ?s)
1191     (?N gnus-tmp-number ?d)
1192     (?u gnus-tmp-user-defined ?s)))
1193
1194 (defvar gnus-summary-mode-line-format-alist
1195   `((?G gnus-tmp-group-name ?s)
1196     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1197     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1198     (?A gnus-tmp-article-number ?d)
1199     (?Z gnus-tmp-unread-and-unselected ?s)
1200     (?V gnus-version ?s)
1201     (?U gnus-tmp-unread-and-unticked ?d)
1202     (?S gnus-tmp-subject ?s)
1203     (?e gnus-tmp-unselected ?d)
1204     (?u gnus-tmp-user-defined ?s)
1205     (?d (length gnus-newsgroup-dormant) ?d)
1206     (?t (length gnus-newsgroup-marked) ?d)
1207     (?h (length gnus-newsgroup-spam-marked) ?d)
1208     (?r (length gnus-newsgroup-reads) ?d)
1209     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1210     (?E gnus-newsgroup-expunged-tally ?d)
1211     (?s (gnus-current-score-file-nondirectory) ?s)))
1212
1213 (defvar gnus-last-search-regexp nil
1214   "Default regexp for article search command.")
1215
1216 (defvar gnus-last-shell-command nil
1217   "Default shell command on article.")
1218
1219 (defvar gnus-newsgroup-agentized nil
1220   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1221 (defvar gnus-newsgroup-begin nil)
1222 (defvar gnus-newsgroup-end nil)
1223 (defvar gnus-newsgroup-last-rmail nil)
1224 (defvar gnus-newsgroup-last-mail nil)
1225 (defvar gnus-newsgroup-last-folder nil)
1226 (defvar gnus-newsgroup-last-file nil)
1227 (defvar gnus-newsgroup-auto-expire nil)
1228 (defvar gnus-newsgroup-active nil)
1229
1230 (defvar gnus-newsgroup-data nil)
1231 (defvar gnus-newsgroup-data-reverse nil)
1232 (defvar gnus-newsgroup-limit nil)
1233 (defvar gnus-newsgroup-limits nil)
1234 (defvar gnus-summary-use-undownloaded-faces nil)
1235
1236 (defvar gnus-newsgroup-unreads nil
1237   "Sorted list of unread articles in the current newsgroup.")
1238
1239 (defvar gnus-newsgroup-unselected nil
1240   "Sorted list of unselected unread articles in the current newsgroup.")
1241
1242 (defvar gnus-newsgroup-reads nil
1243   "Alist of read articles and article marks in the current newsgroup.")
1244
1245 (defvar gnus-newsgroup-expunged-tally nil)
1246
1247 (defvar gnus-newsgroup-marked nil
1248   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1249
1250 (defvar gnus-newsgroup-spam-marked nil
1251   "List of ranges of articles that have been marked as spam.")
1252
1253 (defvar gnus-newsgroup-killed nil
1254   "List of ranges of articles that have been through the scoring process.")
1255
1256 (defvar gnus-newsgroup-cached nil
1257   "Sorted list of articles that come from the article cache.")
1258
1259 (defvar gnus-newsgroup-saved nil
1260   "List of articles that have been saved.")
1261
1262 (defvar gnus-newsgroup-kill-headers nil)
1263
1264 (defvar gnus-newsgroup-replied nil
1265   "List of articles that have been replied to in the current newsgroup.")
1266
1267 (defvar gnus-newsgroup-forwarded nil
1268   "List of articles that have been forwarded in the current newsgroup.")
1269
1270 (defvar gnus-newsgroup-recent nil
1271   "List of articles that have are recent in the current newsgroup.")
1272
1273 (defvar gnus-newsgroup-expirable nil
1274   "Sorted list of articles in the current newsgroup that can be expired.")
1275
1276 (defvar gnus-newsgroup-processable nil
1277   "List of articles in the current newsgroup that can be processed.")
1278
1279 (defvar gnus-newsgroup-downloadable nil
1280   "Sorted list of articles in the current newsgroup that can be processed.")
1281
1282 (defvar gnus-newsgroup-unfetched nil
1283   "Sorted list of articles in the current newsgroup whose headers have
1284 not been fetched into the agent.
1285
1286 This list will always be a subset of gnus-newsgroup-undownloaded.")
1287
1288 (defvar gnus-newsgroup-undownloaded nil
1289   "List of articles in the current newsgroup that haven't been downloaded.")
1290
1291 (defvar gnus-newsgroup-unsendable nil
1292   "List of articles in the current newsgroup that won't be sent.")
1293
1294 (defvar gnus-newsgroup-bookmarks nil
1295   "List of articles in the current newsgroup that have bookmarks.")
1296
1297 (defvar gnus-newsgroup-dormant nil
1298   "Sorted list of dormant articles in the current newsgroup.")
1299
1300 (defvar gnus-newsgroup-unseen nil
1301   "List of unseen articles in the current newsgroup.")
1302
1303 (defvar gnus-newsgroup-seen nil
1304   "Range of seen articles in the current newsgroup.")
1305
1306 (defvar gnus-newsgroup-articles nil
1307   "List of articles in the current newsgroup.")
1308
1309 (defvar gnus-newsgroup-scored nil
1310   "List of scored articles in the current newsgroup.")
1311
1312 (defvar gnus-newsgroup-headers nil
1313   "List of article headers in the current newsgroup.")
1314
1315 (defvar gnus-newsgroup-threads nil)
1316
1317 (defvar gnus-newsgroup-prepared nil
1318   "Whether the current group has been prepared properly.")
1319
1320 (defvar gnus-newsgroup-ancient nil
1321   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1322
1323 (defvar gnus-newsgroup-sparse nil)
1324
1325 (defvar gnus-current-article nil)
1326 (defvar gnus-article-current nil)
1327 (defvar gnus-current-headers nil)
1328 (defvar gnus-have-all-headers nil)
1329 (defvar gnus-last-article nil)
1330 (defvar gnus-newsgroup-history nil)
1331 (defvar gnus-newsgroup-charset nil)
1332 (defvar gnus-newsgroup-ephemeral-charset nil)
1333 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1334
1335 (defvar gnus-article-before-search nil)
1336
1337 (defvar gnus-summary-local-variables
1338   '(gnus-newsgroup-name
1339     gnus-newsgroup-begin gnus-newsgroup-end
1340     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1341     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1342     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1343     gnus-newsgroup-unselected gnus-newsgroup-marked
1344     gnus-newsgroup-spam-marked
1345     gnus-newsgroup-reads gnus-newsgroup-saved
1346     gnus-newsgroup-replied gnus-newsgroup-forwarded
1347     gnus-newsgroup-recent
1348     gnus-newsgroup-expirable
1349     gnus-newsgroup-processable gnus-newsgroup-killed
1350     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1351     gnus-newsgroup-unfetched
1352     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1353     gnus-newsgroup-seen gnus-newsgroup-articles
1354     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1355     gnus-newsgroup-headers gnus-newsgroup-threads
1356     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1357     gnus-current-article gnus-current-headers gnus-have-all-headers
1358     gnus-last-article gnus-article-internal-prepare-hook
1359     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1360     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1361     gnus-thread-expunge-below
1362     gnus-score-alist gnus-current-score-file
1363     (gnus-summary-expunge-below . global)
1364     (gnus-summary-mark-below . global)
1365     (gnus-orphan-score . global)
1366     gnus-newsgroup-active gnus-scores-exclude-files
1367     gnus-newsgroup-history gnus-newsgroup-ancient
1368     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1369     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1370     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1371     (gnus-newsgroup-expunged-tally . 0)
1372     gnus-cache-removable-articles gnus-newsgroup-cached
1373     gnus-newsgroup-data gnus-newsgroup-data-reverse
1374     gnus-newsgroup-limit gnus-newsgroup-limits
1375     gnus-newsgroup-charset gnus-newsgroup-display
1376     gnus-summary-use-undownloaded-faces)
1377   "Variables that are buffer-local to the summary buffers.")
1378
1379 (defvar gnus-newsgroup-variables nil
1380   "A list of variables that have separate values in different newsgroups.
1381 A list of newsgroup (summary buffer) local variables, or cons of
1382 variables and their default values (when the default values are not
1383 nil), that should be made global while the summary buffer is active.
1384 These variables can be used to set variables in the group parameters
1385 while still allowing them to affect operations done in other
1386 buffers. For example:
1387
1388 \(setq gnus-newsgroup-variables
1389      '(message-use-followup-to
1390        (gnus-visible-headers .
1391          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1392 ")
1393
1394 ;; Byte-compiler warning.
1395 (eval-when-compile
1396   ;; Bind features so that require will believe that gnus-sum has
1397   ;; already been loaded (avoids infinite recursion)
1398   (let ((features (cons 'gnus-sum features)))
1399     ;; Several of the declarations in gnus-sum are needed to load the
1400     ;; following files. Right now, these definitions have been
1401     ;; compiled but not defined (evaluated).  We could either do a
1402     ;; eval-and-compile about all of the declarations or evaluate the
1403     ;; source file.
1404     (if (boundp 'gnus-newsgroup-variables)
1405         nil
1406       (load "gnus-sum.el" t t t))
1407     (require 'gnus)
1408     (require 'gnus-agent)
1409     (require 'gnus-art)))
1410
1411 ;; MIME stuff.
1412
1413 (defvar gnus-decode-encoded-word-methods
1414   '(mail-decode-encoded-word-string)
1415   "List of methods used to decode encoded words.
1416
1417 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1418 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1419 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1420 whose names match REGEXP.
1421
1422 For example:
1423 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1424  mail-decode-encoded-word-string
1425  (\"chinese\" . rfc1843-decode-string))")
1426
1427 (defvar gnus-decode-encoded-word-methods-cache nil)
1428
1429 (defun gnus-multi-decode-encoded-word-string (string)
1430   "Apply the functions from `gnus-encoded-word-methods' that match."
1431   (unless (and gnus-decode-encoded-word-methods-cache
1432                (eq gnus-newsgroup-name
1433                    (car gnus-decode-encoded-word-methods-cache)))
1434     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1435     (mapcar (lambda (x)
1436               (if (symbolp x)
1437                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1438                 (if (and gnus-newsgroup-name
1439                          (string-match (car x) gnus-newsgroup-name))
1440                     (nconc gnus-decode-encoded-word-methods-cache
1441                            (list (cdr x))))))
1442             gnus-decode-encoded-word-methods))
1443   (let ((xlist gnus-decode-encoded-word-methods-cache))
1444     (pop xlist)
1445     (while xlist
1446       (setq string (funcall (pop xlist) string))))
1447   string)
1448
1449 ;; Subject simplification.
1450
1451 (defun gnus-simplify-whitespace (str)
1452   "Remove excessive whitespace from STR."
1453   ;; Multiple spaces.
1454   (while (string-match "[ \t][ \t]+" str)
1455     (setq str (concat (substring str 0 (match-beginning 0))
1456                         " "
1457                         (substring str (match-end 0)))))
1458   ;; Leading spaces.
1459   (when (string-match "^[ \t]+" str)
1460     (setq str (substring str (match-end 0))))
1461   ;; Trailing spaces.
1462   (when (string-match "[ \t]+$" str)
1463     (setq str (substring str 0 (match-beginning 0))))
1464   str)
1465
1466 (defun gnus-simplify-all-whitespace (str)
1467   "Remove all whitespace from STR."
1468   (while (string-match "[ \t\n]+" str)
1469     (setq str (replace-match "" nil nil str)))
1470   str)
1471
1472 (defsubst gnus-simplify-subject-re (subject)
1473   "Remove \"Re:\" from subject lines."
1474   (if (string-match message-subject-re-regexp subject)
1475       (substring subject (match-end 0))
1476     subject))
1477
1478 (defun gnus-simplify-subject (subject &optional re-only)
1479   "Remove `Re:' and words in parentheses.
1480 If RE-ONLY is non-nil, strip leading `Re:'s only."
1481   (let ((case-fold-search t))           ;Ignore case.
1482     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1483     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1484       (setq subject (substring subject (match-end 0))))
1485     ;; Remove uninteresting prefixes.
1486     (when (and (not re-only)
1487                gnus-simplify-ignored-prefixes
1488                (string-match gnus-simplify-ignored-prefixes subject))
1489       (setq subject (substring subject (match-end 0))))
1490     ;; Remove words in parentheses from end.
1491     (unless re-only
1492       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1493         (setq subject (substring subject 0 (match-beginning 0)))))
1494     ;; Return subject string.
1495     subject))
1496
1497 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1498 ;; all whitespace.
1499 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1500   (goto-char (point-min))
1501   (while (re-search-forward regexp nil t)
1502     (replace-match (or newtext ""))))
1503
1504 (defun gnus-simplify-buffer-fuzzy ()
1505   "Simplify string in the buffer fuzzily.
1506 The string in the accessible portion of the current buffer is simplified.
1507 It is assumed to be a single-line subject.
1508 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1509 matter is removed.  Additional things can be deleted by setting
1510 `gnus-simplify-subject-fuzzy-regexp'."
1511   (let ((case-fold-search t)
1512         (modified-tick))
1513     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1514
1515     (while (not (eq modified-tick (buffer-modified-tick)))
1516       (setq modified-tick (buffer-modified-tick))
1517       (cond
1518        ((listp gnus-simplify-subject-fuzzy-regexp)
1519         (mapcar 'gnus-simplify-buffer-fuzzy-step
1520                 gnus-simplify-subject-fuzzy-regexp))
1521        (gnus-simplify-subject-fuzzy-regexp
1522         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1523       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1524       (gnus-simplify-buffer-fuzzy-step
1525        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1526       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1527
1528     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1529     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1530     (gnus-simplify-buffer-fuzzy-step " $")
1531     (gnus-simplify-buffer-fuzzy-step "^ +")))
1532
1533 (defun gnus-simplify-subject-fuzzy (subject)
1534   "Simplify a subject string fuzzily.
1535 See `gnus-simplify-buffer-fuzzy' for details."
1536   (save-excursion
1537     (gnus-set-work-buffer)
1538     (let ((case-fold-search t))
1539       ;; Remove uninteresting prefixes.
1540       (when (and gnus-simplify-ignored-prefixes
1541                  (string-match gnus-simplify-ignored-prefixes subject))
1542         (setq subject (substring subject (match-end 0))))
1543       (insert subject)
1544       (inline (gnus-simplify-buffer-fuzzy))
1545       (buffer-string))))
1546
1547 (defsubst gnus-simplify-subject-fully (subject)
1548   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1549   (cond
1550    (gnus-simplify-subject-functions
1551     (gnus-map-function gnus-simplify-subject-functions subject))
1552    ((null gnus-summary-gather-subject-limit)
1553     (gnus-simplify-subject-re subject))
1554    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1555     (gnus-simplify-subject-fuzzy subject))
1556    ((numberp gnus-summary-gather-subject-limit)
1557     (gnus-limit-string (gnus-simplify-subject-re subject)
1558                        gnus-summary-gather-subject-limit))
1559    (t
1560     subject)))
1561
1562 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1563   "Check whether two subjects are equal.
1564 If optional argument SIMPLE-FIRST is t, first argument is already
1565 simplified."
1566   (cond
1567    ((null simple-first)
1568     (equal (gnus-simplify-subject-fully s1)
1569            (gnus-simplify-subject-fully s2)))
1570    (t
1571     (equal s1
1572            (gnus-simplify-subject-fully s2)))))
1573
1574 (defun gnus-summary-bubble-group ()
1575   "Increase the score of the current group.
1576 This is a handy function to add to `gnus-summary-exit-hook' to
1577 increase the score of each group you read."
1578   (gnus-group-add-score gnus-newsgroup-name))
1579
1580 \f
1581 ;;;
1582 ;;; Gnus summary mode
1583 ;;;
1584
1585 (put 'gnus-summary-mode 'mode-class 'special)
1586
1587 (defvar gnus-article-commands-menu)
1588
1589 ;; Non-orthogonal keys
1590
1591 (gnus-define-keys gnus-summary-mode-map
1592   " " gnus-summary-next-page
1593   "\177" gnus-summary-prev-page
1594   [delete] gnus-summary-prev-page
1595   [backspace] gnus-summary-prev-page
1596   "\r" gnus-summary-scroll-up
1597   "\M-\r" gnus-summary-scroll-down
1598   "n" gnus-summary-next-unread-article
1599   "p" gnus-summary-prev-unread-article
1600   "N" gnus-summary-next-article
1601   "P" gnus-summary-prev-article
1602   "\M-\C-n" gnus-summary-next-same-subject
1603   "\M-\C-p" gnus-summary-prev-same-subject
1604   "\M-n" gnus-summary-next-unread-subject
1605   "\M-p" gnus-summary-prev-unread-subject
1606   "." gnus-summary-first-unread-article
1607   "," gnus-summary-best-unread-article
1608   "\M-s" gnus-summary-search-article-forward
1609   "\M-r" gnus-summary-search-article-backward
1610   "<" gnus-summary-beginning-of-article
1611   ">" gnus-summary-end-of-article
1612   "j" gnus-summary-goto-article
1613   "^" gnus-summary-refer-parent-article
1614   "\M-^" gnus-summary-refer-article
1615   "u" gnus-summary-tick-article-forward
1616   "!" gnus-summary-tick-article-forward
1617   "U" gnus-summary-tick-article-backward
1618   "d" gnus-summary-mark-as-read-forward
1619   "D" gnus-summary-mark-as-read-backward
1620   "E" gnus-summary-mark-as-expirable
1621   "\M-u" gnus-summary-clear-mark-forward
1622   "\M-U" gnus-summary-clear-mark-backward
1623   "k" gnus-summary-kill-same-subject-and-select
1624   "\C-k" gnus-summary-kill-same-subject
1625   "\M-\C-k" gnus-summary-kill-thread
1626   "\M-\C-l" gnus-summary-lower-thread
1627   "e" gnus-summary-edit-article
1628   "#" gnus-summary-mark-as-processable
1629   "\M-#" gnus-summary-unmark-as-processable
1630   "\M-\C-t" gnus-summary-toggle-threads
1631   "\M-\C-s" gnus-summary-show-thread
1632   "\M-\C-h" gnus-summary-hide-thread
1633   "\M-\C-f" gnus-summary-next-thread
1634   "\M-\C-b" gnus-summary-prev-thread
1635   [(meta down)] gnus-summary-next-thread
1636   [(meta up)] gnus-summary-prev-thread
1637   "\M-\C-u" gnus-summary-up-thread
1638   "\M-\C-d" gnus-summary-down-thread
1639   "&" gnus-summary-execute-command
1640   "c" gnus-summary-catchup-and-exit
1641   "\C-w" gnus-summary-mark-region-as-read
1642   "\C-t" gnus-summary-toggle-truncation
1643   "?" gnus-summary-mark-as-dormant
1644   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1645   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1646   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1647   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1648   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1649   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1650   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1651   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1652   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1653   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1654   "=" gnus-summary-expand-window
1655   "\C-x\C-s" gnus-summary-reselect-current-group
1656   "\M-g" gnus-summary-rescan-group
1657   "w" gnus-summary-stop-page-breaking
1658   "\C-c\C-r" gnus-summary-caesar-message
1659   "f" gnus-summary-followup
1660   "F" gnus-summary-followup-with-original
1661   "C" gnus-summary-cancel-article
1662   "r" gnus-summary-reply
1663   "R" gnus-summary-reply-with-original
1664   "\C-c\C-f" gnus-summary-mail-forward
1665   "o" gnus-summary-save-article
1666   "\C-o" gnus-summary-save-article-mail
1667   "|" gnus-summary-pipe-output
1668   "\M-k" gnus-summary-edit-local-kill
1669   "\M-K" gnus-summary-edit-global-kill
1670   ;; "V" gnus-version
1671   "\C-c\C-d" gnus-summary-describe-group
1672   "q" gnus-summary-exit
1673   "Q" gnus-summary-exit-no-update
1674   "\C-c\C-i" gnus-info-find-node
1675   gnus-mouse-2 gnus-mouse-pick-article
1676   "m" gnus-summary-mail-other-window
1677   "a" gnus-summary-post-news
1678   "i" gnus-summary-news-other-window
1679   "x" gnus-summary-limit-to-unread
1680   "s" gnus-summary-isearch-article
1681   "t" gnus-summary-toggle-header
1682   "g" gnus-summary-show-article
1683   "l" gnus-summary-goto-last-article
1684   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1685   "\C-d" gnus-summary-enter-digest-group
1686   "\M-\C-d" gnus-summary-read-document
1687   "\M-\C-e" gnus-summary-edit-parameters
1688   "\M-\C-a" gnus-summary-customize-parameters
1689   "\C-c\C-b" gnus-bug
1690   "*" gnus-cache-enter-article
1691   "\M-*" gnus-cache-remove-article
1692   "\M-&" gnus-summary-universal-argument
1693   "\C-l" gnus-recenter
1694   "I" gnus-summary-increase-score
1695   "L" gnus-summary-lower-score
1696   "\M-i" gnus-symbolic-argument
1697   "h" gnus-summary-select-article-buffer
1698
1699   "b" gnus-article-view-part
1700   "\M-t" gnus-summary-toggle-display-buttonized
1701
1702   "V" gnus-summary-score-map
1703   "X" gnus-uu-extract-map
1704   "S" gnus-summary-send-map)
1705
1706 ;; Sort of orthogonal keymap
1707 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1708   "t" gnus-summary-tick-article-forward
1709   "!" gnus-summary-tick-article-forward
1710   "d" gnus-summary-mark-as-read-forward
1711   "r" gnus-summary-mark-as-read-forward
1712   "c" gnus-summary-clear-mark-forward
1713   " " gnus-summary-clear-mark-forward
1714   "e" gnus-summary-mark-as-expirable
1715   "x" gnus-summary-mark-as-expirable
1716   "?" gnus-summary-mark-as-dormant
1717   "b" gnus-summary-set-bookmark
1718   "B" gnus-summary-remove-bookmark
1719   "#" gnus-summary-mark-as-processable
1720   "\M-#" gnus-summary-unmark-as-processable
1721   "S" gnus-summary-limit-include-expunged
1722   "C" gnus-summary-catchup
1723   "H" gnus-summary-catchup-to-here
1724   "h" gnus-summary-catchup-from-here
1725   "\C-c" gnus-summary-catchup-all
1726   "k" gnus-summary-kill-same-subject-and-select
1727   "K" gnus-summary-kill-same-subject
1728   "P" gnus-uu-mark-map)
1729
1730 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1731   "c" gnus-summary-clear-above
1732   "u" gnus-summary-tick-above
1733   "m" gnus-summary-mark-above
1734   "k" gnus-summary-kill-below)
1735
1736 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1737   "/" gnus-summary-limit-to-subject
1738   "n" gnus-summary-limit-to-articles
1739   "w" gnus-summary-pop-limit
1740   "s" gnus-summary-limit-to-subject
1741   "a" gnus-summary-limit-to-author
1742   "u" gnus-summary-limit-to-unread
1743   "m" gnus-summary-limit-to-marks
1744   "M" gnus-summary-limit-exclude-marks
1745   "v" gnus-summary-limit-to-score
1746   "*" gnus-summary-limit-include-cached
1747   "D" gnus-summary-limit-include-dormant
1748   "T" gnus-summary-limit-include-thread
1749   "d" gnus-summary-limit-exclude-dormant
1750   "t" gnus-summary-limit-to-age
1751   "." gnus-summary-limit-to-unseen
1752   "x" gnus-summary-limit-to-extra
1753   "p" gnus-summary-limit-to-display-predicate
1754   "E" gnus-summary-limit-include-expunged
1755   "c" gnus-summary-limit-exclude-childless-dormant
1756   "C" gnus-summary-limit-mark-excluded-as-read
1757   "o" gnus-summary-insert-old-articles
1758   "N" gnus-summary-insert-new-articles)
1759
1760 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1761   "n" gnus-summary-next-unread-article
1762   "p" gnus-summary-prev-unread-article
1763   "N" gnus-summary-next-article
1764   "P" gnus-summary-prev-article
1765   "\C-n" gnus-summary-next-same-subject
1766   "\C-p" gnus-summary-prev-same-subject
1767   "\M-n" gnus-summary-next-unread-subject
1768   "\M-p" gnus-summary-prev-unread-subject
1769   "f" gnus-summary-first-unread-article
1770   "b" gnus-summary-best-unread-article
1771   "j" gnus-summary-goto-article
1772   "g" gnus-summary-goto-subject
1773   "l" gnus-summary-goto-last-article
1774   "o" gnus-summary-pop-article)
1775
1776 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1777   "k" gnus-summary-kill-thread
1778   "l" gnus-summary-lower-thread
1779   "i" gnus-summary-raise-thread
1780   "T" gnus-summary-toggle-threads
1781   "t" gnus-summary-rethread-current
1782   "^" gnus-summary-reparent-thread
1783   "s" gnus-summary-show-thread
1784   "S" gnus-summary-show-all-threads
1785   "h" gnus-summary-hide-thread
1786   "H" gnus-summary-hide-all-threads
1787   "n" gnus-summary-next-thread
1788   "p" gnus-summary-prev-thread
1789   "u" gnus-summary-up-thread
1790   "o" gnus-summary-top-thread
1791   "d" gnus-summary-down-thread
1792   "#" gnus-uu-mark-thread
1793   "\M-#" gnus-uu-unmark-thread)
1794
1795 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1796   "g" gnus-summary-prepare
1797   "c" gnus-summary-insert-cached-articles
1798   "d" gnus-summary-insert-dormant-articles)
1799
1800 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1801   "c" gnus-summary-catchup-and-exit
1802   "C" gnus-summary-catchup-all-and-exit
1803   "E" gnus-summary-exit-no-update
1804   "Q" gnus-summary-exit
1805   "Z" gnus-summary-exit
1806   "n" gnus-summary-catchup-and-goto-next-group
1807   "R" gnus-summary-reselect-current-group
1808   "G" gnus-summary-rescan-group
1809   "N" gnus-summary-next-group
1810   "s" gnus-summary-save-newsrc
1811   "P" gnus-summary-prev-group)
1812
1813 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1814   " " gnus-summary-next-page
1815   "n" gnus-summary-next-page
1816   "\177" gnus-summary-prev-page
1817   [delete] gnus-summary-prev-page
1818   "p" gnus-summary-prev-page
1819   "\r" gnus-summary-scroll-up
1820   "\M-\r" gnus-summary-scroll-down
1821   "<" gnus-summary-beginning-of-article
1822   ">" gnus-summary-end-of-article
1823   "b" gnus-summary-beginning-of-article
1824   "e" gnus-summary-end-of-article
1825   "^" gnus-summary-refer-parent-article
1826   "r" gnus-summary-refer-parent-article
1827   "D" gnus-summary-enter-digest-group
1828   "R" gnus-summary-refer-references
1829   "T" gnus-summary-refer-thread
1830   "g" gnus-summary-show-article
1831   "s" gnus-summary-isearch-article
1832   "P" gnus-summary-print-article
1833   "M" gnus-mailing-list-insinuate
1834   "t" gnus-article-babel)
1835
1836 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1837   "b" gnus-article-add-buttons
1838   "B" gnus-article-add-buttons-to-head
1839   "o" gnus-article-treat-overstrike
1840   "e" gnus-article-emphasize
1841   "w" gnus-article-fill-cited-article
1842   "Q" gnus-article-fill-long-lines
1843   "C" gnus-article-capitalize-sentences
1844   "c" gnus-article-remove-cr
1845   "q" gnus-article-de-quoted-unreadable
1846   "6" gnus-article-de-base64-unreadable
1847   "Z" gnus-article-decode-HZ
1848   "h" gnus-article-wash-html
1849   "u" gnus-article-unsplit-urls
1850   "s" gnus-summary-force-verify-and-decrypt
1851   "f" gnus-article-display-x-face
1852   "l" gnus-summary-stop-page-breaking
1853   "r" gnus-summary-caesar-message
1854   "m" gnus-summary-morse-message
1855   "t" gnus-summary-toggle-header
1856   "g" gnus-treat-smiley
1857   "v" gnus-summary-verbose-headers
1858   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1859   "p" gnus-article-verify-x-pgp-sig
1860   "d" gnus-article-treat-dumbquotes)
1861
1862 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1863   ;; mnemonic: deuglif*Y*
1864   "u" gnus-article-outlook-unwrap-lines
1865   "a" gnus-article-outlook-repair-attribution
1866   "c" gnus-article-outlook-rearrange-citation
1867   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1868
1869 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1870   "a" gnus-article-hide
1871   "h" gnus-article-hide-headers
1872   "b" gnus-article-hide-boring-headers
1873   "s" gnus-article-hide-signature
1874   "c" gnus-article-hide-citation
1875   "C" gnus-article-hide-citation-in-followups
1876   "l" gnus-article-hide-list-identifiers
1877   "B" gnus-article-strip-banner
1878   "P" gnus-article-hide-pem
1879   "\C-c" gnus-article-hide-citation-maybe)
1880
1881 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1882   "a" gnus-article-highlight
1883   "h" gnus-article-highlight-headers
1884   "c" gnus-article-highlight-citation
1885   "s" gnus-article-highlight-signature)
1886
1887 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1888   "f" gnus-article-treat-fold-headers
1889   "u" gnus-article-treat-unfold-headers
1890   "n" gnus-article-treat-fold-newsgroups)
1891
1892 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1893   "x" gnus-article-display-x-face
1894   "d" gnus-article-display-face
1895   "s" gnus-treat-smiley
1896   "D" gnus-article-remove-images
1897   "f" gnus-treat-from-picon
1898   "m" gnus-treat-mail-picon
1899   "n" gnus-treat-newsgroups-picon)
1900
1901 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1902   "w" gnus-article-decode-mime-words
1903   "c" gnus-article-decode-charset
1904   "v" gnus-mime-view-all-parts
1905   "b" gnus-article-view-part)
1906
1907 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1908   "z" gnus-article-date-ut
1909   "u" gnus-article-date-ut
1910   "l" gnus-article-date-local
1911   "p" gnus-article-date-english
1912   "e" gnus-article-date-lapsed
1913   "o" gnus-article-date-original
1914   "i" gnus-article-date-iso8601
1915   "s" gnus-article-date-user)
1916
1917 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1918   "t" gnus-article-remove-trailing-blank-lines
1919   "l" gnus-article-strip-leading-blank-lines
1920   "m" gnus-article-strip-multiple-blank-lines
1921   "a" gnus-article-strip-blank-lines
1922   "A" gnus-article-strip-all-blank-lines
1923   "s" gnus-article-strip-leading-space
1924   "e" gnus-article-strip-trailing-space
1925   "w" gnus-article-remove-leading-whitespace)
1926
1927 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1928   "v" gnus-version
1929   "f" gnus-summary-fetch-faq
1930   "d" gnus-summary-describe-group
1931   "h" gnus-summary-describe-briefly
1932   "i" gnus-info-find-node
1933   "c" gnus-group-fetch-charter
1934   "C" gnus-group-fetch-control)
1935
1936 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1937   "e" gnus-summary-expire-articles
1938   "\M-\C-e" gnus-summary-expire-articles-now
1939   "\177" gnus-summary-delete-article
1940   [delete] gnus-summary-delete-article
1941   [backspace] gnus-summary-delete-article
1942   "m" gnus-summary-move-article
1943   "r" gnus-summary-respool-article
1944   "w" gnus-summary-edit-article
1945   "c" gnus-summary-copy-article
1946   "B" gnus-summary-crosspost-article
1947   "q" gnus-summary-respool-query
1948   "t" gnus-summary-respool-trace
1949   "i" gnus-summary-import-article
1950   "I" gnus-summary-create-article
1951   "p" gnus-summary-article-posted-p)
1952
1953 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1954   "o" gnus-summary-save-article
1955   "m" gnus-summary-save-article-mail
1956   "F" gnus-summary-write-article-file
1957   "r" gnus-summary-save-article-rmail
1958   "f" gnus-summary-save-article-file
1959   "b" gnus-summary-save-article-body-file
1960   "h" gnus-summary-save-article-folder
1961   "v" gnus-summary-save-article-vm
1962   "p" gnus-summary-pipe-output
1963   "P" gnus-summary-muttprint
1964   "s" gnus-soup-add-article)
1965
1966 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1967   "b" gnus-summary-display-buttonized
1968   "m" gnus-summary-repair-multipart
1969   "v" gnus-article-view-part
1970   "o" gnus-article-save-part
1971   "c" gnus-article-copy-part
1972   "C" gnus-article-view-part-as-charset
1973   "e" gnus-article-view-part-externally
1974   "E" gnus-article-encrypt-body
1975   "i" gnus-article-inline-part
1976   "|" gnus-article-pipe-part)
1977
1978 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1979   "p" gnus-summary-mark-as-processable
1980   "u" gnus-summary-unmark-as-processable
1981   "U" gnus-summary-unmark-all-processable
1982   "v" gnus-uu-mark-over
1983   "s" gnus-uu-mark-series
1984   "r" gnus-uu-mark-region
1985   "g" gnus-uu-unmark-region
1986   "R" gnus-uu-mark-by-regexp
1987   "G" gnus-uu-unmark-by-regexp
1988   "t" gnus-uu-mark-thread
1989   "T" gnus-uu-unmark-thread
1990   "a" gnus-uu-mark-all
1991   "b" gnus-uu-mark-buffer
1992   "S" gnus-uu-mark-sparse
1993   "k" gnus-summary-kill-process-mark
1994   "y" gnus-summary-yank-process-mark
1995   "w" gnus-summary-save-process-mark
1996   "i" gnus-uu-invert-processable)
1997
1998 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1999   ;;"x" gnus-uu-extract-any
2000   "m" gnus-summary-save-parts
2001   "u" gnus-uu-decode-uu
2002   "U" gnus-uu-decode-uu-and-save
2003   "s" gnus-uu-decode-unshar
2004   "S" gnus-uu-decode-unshar-and-save
2005   "o" gnus-uu-decode-save
2006   "O" gnus-uu-decode-save
2007   "b" gnus-uu-decode-binhex
2008   "B" gnus-uu-decode-binhex
2009   "p" gnus-uu-decode-postscript
2010   "P" gnus-uu-decode-postscript-and-save)
2011
2012 (gnus-define-keys
2013     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2014   "u" gnus-uu-decode-uu-view
2015   "U" gnus-uu-decode-uu-and-save-view
2016   "s" gnus-uu-decode-unshar-view
2017   "S" gnus-uu-decode-unshar-and-save-view
2018   "o" gnus-uu-decode-save-view
2019   "O" gnus-uu-decode-save-view
2020   "b" gnus-uu-decode-binhex-view
2021   "B" gnus-uu-decode-binhex-view
2022   "p" gnus-uu-decode-postscript-view
2023   "P" gnus-uu-decode-postscript-and-save-view)
2024
2025 (defvar gnus-article-post-menu nil)
2026
2027 (defconst gnus-summary-menu-maxlen 20)
2028
2029 (defun gnus-summary-menu-split (menu)
2030   ;; If we have lots of elements, divide them into groups of 20
2031   ;; and make a pane (or submenu) for each one.
2032   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2033       (let ((menu menu) sublists next
2034             (i 1))
2035         (while menu
2036           ;; Pull off the next gnus-summary-menu-maxlen elements
2037           ;; and make them the next element of sublist.
2038           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2039           (if next
2040               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2041                       nil))
2042           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2043                                              (aref (car (last menu)) 0)) menu)
2044                                sublists))
2045           (setq i (1+ i))
2046           (setq menu next))
2047         (nreverse sublists))
2048     ;; Few elements--put them all in one pane.
2049     menu))
2050
2051 (defun gnus-summary-make-menu-bar ()
2052   (gnus-turn-off-edit-menu 'summary)
2053
2054   (unless (boundp 'gnus-summary-misc-menu)
2055
2056     (easy-menu-define
2057       gnus-summary-kill-menu gnus-summary-mode-map ""
2058       (cons
2059        "Score"
2060        (nconc
2061         (list
2062          ["Customize" gnus-score-customize t])
2063         (gnus-make-score-map 'increase)
2064         (gnus-make-score-map 'lower)
2065         '(("Mark"
2066            ["Kill below" gnus-summary-kill-below t]
2067            ["Mark above" gnus-summary-mark-above t]
2068            ["Tick above" gnus-summary-tick-above t]
2069            ["Clear above" gnus-summary-clear-above t])
2070           ["Current score" gnus-summary-current-score t]
2071           ["Set score" gnus-summary-set-score t]
2072           ["Switch current score file..." gnus-score-change-score-file t]
2073           ["Set mark below..." gnus-score-set-mark-below t]
2074           ["Set expunge below..." gnus-score-set-expunge-below t]
2075           ["Edit current score file" gnus-score-edit-current-scores t]
2076           ["Edit score file" gnus-score-edit-file t]
2077           ["Trace score" gnus-score-find-trace t]
2078           ["Find words" gnus-score-find-favourite-words t]
2079           ["Rescore buffer" gnus-summary-rescore t]
2080           ["Increase score..." gnus-summary-increase-score t]
2081           ["Lower score..." gnus-summary-lower-score t]))))
2082
2083     ;; Define both the Article menu in the summary buffer and the
2084     ;; equivalent Commands menu in the article buffer here for
2085     ;; consistency.
2086     (let ((innards
2087            `(("Hide"
2088               ["All" gnus-article-hide t]
2089               ["Headers" gnus-article-hide-headers t]
2090               ["Signature" gnus-article-hide-signature t]
2091               ["Citation" gnus-article-hide-citation t]
2092               ["List identifiers" gnus-article-hide-list-identifiers t]
2093               ["Banner" gnus-article-strip-banner t]
2094               ["Boring headers" gnus-article-hide-boring-headers t])
2095              ("Highlight"
2096               ["All" gnus-article-highlight t]
2097               ["Headers" gnus-article-highlight-headers t]
2098               ["Signature" gnus-article-highlight-signature t]
2099               ["Citation" gnus-article-highlight-citation t])
2100              ("MIME"
2101               ["Words" gnus-article-decode-mime-words t]
2102               ["Charset" gnus-article-decode-charset t]
2103               ["QP" gnus-article-de-quoted-unreadable t]
2104               ["Base64" gnus-article-de-base64-unreadable t]
2105               ["View MIME buttons" gnus-summary-display-buttonized t]
2106               ["View all" gnus-mime-view-all-parts t]
2107               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2108               ["Encrypt body" gnus-article-encrypt-body
2109                :active (not (gnus-group-read-only-p))
2110                ,@(if (featurep 'xemacs) nil
2111                    '(:help "Encrypt the message body on disk"))]
2112               ["Extract all parts..." gnus-summary-save-parts t]
2113               ("Multipart"
2114                ["Repair multipart" gnus-summary-repair-multipart t]
2115                ["Pipe part..." gnus-article-pipe-part t]
2116                ["Inline part" gnus-article-inline-part t]
2117                ["Encrypt body" gnus-article-encrypt-body
2118                 :active (not (gnus-group-read-only-p))
2119                ,@(if (featurep 'xemacs) nil
2120                    '(:help "Encrypt the message body on disk"))]
2121                ["View part externally" gnus-article-view-part-externally t]
2122                ["View part with charset..." gnus-article-view-part-as-charset t]
2123                ["Copy part" gnus-article-copy-part t]
2124                ["Save part..." gnus-article-save-part t]
2125                ["View part" gnus-article-view-part t]))
2126              ("Date"
2127               ["Local" gnus-article-date-local t]
2128               ["ISO8601" gnus-article-date-iso8601 t]
2129               ["UT" gnus-article-date-ut t]
2130               ["Original" gnus-article-date-original t]
2131               ["Lapsed" gnus-article-date-lapsed t]
2132               ["User-defined" gnus-article-date-user t])
2133              ("Display"
2134               ["Remove images" gnus-article-remove-images t]
2135               ["Toggle smiley" gnus-treat-smiley t]
2136               ["Show X-Face" gnus-article-display-x-face t]
2137               ["Show picons in From" gnus-treat-from-picon t]
2138               ["Show picons in mail headers" gnus-treat-mail-picon t]
2139               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2140               ("View as different encoding"
2141                ,@(gnus-summary-menu-split
2142                   (mapcar
2143                    (lambda (cs)
2144                      ;; Since easymenu under Emacs doesn't allow
2145                      ;; lambda forms for menu commands, we should
2146                      ;; provide intern'ed function symbols.
2147                      (let ((command (intern (format "\
2148 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2149                        (fset command
2150                              `(lambda ()
2151                                 (interactive)
2152                                 (let ((gnus-summary-show-article-charset-alist
2153                                        '((1 . ,cs))))
2154                                   (gnus-summary-show-article 1))))
2155                        `[,(symbol-name cs) ,command t]))
2156                    (sort (if (fboundp 'coding-system-list)
2157                              (coding-system-list)
2158                            (mapcar 'car mm-mime-mule-charset-alist))
2159                          'string<)))))
2160              ("Washing"
2161               ("Remove Blanks"
2162                ["Leading" gnus-article-strip-leading-blank-lines t]
2163                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2164                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2165                ["All of the above" gnus-article-strip-blank-lines t]
2166                ["All" gnus-article-strip-all-blank-lines t]
2167                ["Leading space" gnus-article-strip-leading-space t]
2168                ["Trailing space" gnus-article-strip-trailing-space t]
2169                ["Leading space in headers"
2170                 gnus-article-remove-leading-whitespace t])
2171               ["Overstrike" gnus-article-treat-overstrike t]
2172               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2173               ["Emphasis" gnus-article-emphasize t]
2174               ["Word wrap" gnus-article-fill-cited-article t]
2175               ["Fill long lines" gnus-article-fill-long-lines t]
2176               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2177               ["CR" gnus-article-remove-cr t]
2178               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2179               ["Base64" gnus-article-de-base64-unreadable t]
2180               ["Rot 13" gnus-summary-caesar-message
2181                ,@(if (featurep 'xemacs) '(t)
2182                    '(:help "\"Caesar rotate\" article by 13"))]
2183               ["Morse decode" gnus-summary-morse-message t]
2184               ["Unix pipe..." gnus-summary-pipe-message t]
2185               ["Add buttons" gnus-article-add-buttons t]
2186               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2187               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2188               ["Verbose header" gnus-summary-verbose-headers t]
2189               ["Toggle header" gnus-summary-toggle-header t]
2190               ["Unfold headers" gnus-article-treat-unfold-headers t]
2191               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2192               ["Html" gnus-article-wash-html t]
2193               ["URLs" gnus-article-unsplit-urls t]
2194               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2195               ["HZ" gnus-article-decode-HZ t]
2196               ("(Outlook) Deuglify"
2197                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2198                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2199                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2200                ["Full (Outlook) deuglify"
2201                 gnus-article-outlook-deuglify-article t])
2202               )
2203              ("Output"
2204               ["Save in default format..." gnus-summary-save-article
2205                ,@(if (featurep 'xemacs) '(t)
2206                    '(:help "Save article using default method"))]
2207               ["Save in file..." gnus-summary-save-article-file
2208                ,@(if (featurep 'xemacs) '(t)
2209                    '(:help "Save article in file"))]
2210               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2211               ["Save in MH folder..." gnus-summary-save-article-folder t]
2212               ["Save in VM folder..." gnus-summary-save-article-vm t]
2213               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2214               ["Save body in file..." gnus-summary-save-article-body-file t]
2215               ["Pipe through a filter..." gnus-summary-pipe-output t]
2216               ["Add to SOUP packet" gnus-soup-add-article t]
2217               ["Print with Muttprint..." gnus-summary-muttprint t]
2218               ["Print" gnus-summary-print-article t])
2219              ("Backend"
2220               ["Respool article..." gnus-summary-respool-article t]
2221               ["Move article..." gnus-summary-move-article
2222                (gnus-check-backend-function
2223                 'request-move-article gnus-newsgroup-name)]
2224               ["Copy article..." gnus-summary-copy-article t]
2225               ["Crosspost article..." gnus-summary-crosspost-article
2226                (gnus-check-backend-function
2227                 'request-replace-article gnus-newsgroup-name)]
2228               ["Import file..." gnus-summary-import-article
2229                (gnus-check-backend-function
2230                 'request-accept-article gnus-newsgroup-name)]
2231               ["Create article..." gnus-summary-create-article
2232                (gnus-check-backend-function
2233                 'request-accept-article gnus-newsgroup-name)]
2234               ["Check if posted" gnus-summary-article-posted-p t]
2235               ["Edit article" gnus-summary-edit-article
2236                (not (gnus-group-read-only-p))]
2237               ["Delete article" gnus-summary-delete-article
2238                (gnus-check-backend-function
2239                 'request-expire-articles gnus-newsgroup-name)]
2240               ["Query respool" gnus-summary-respool-query t]
2241               ["Trace respool" gnus-summary-respool-trace t]
2242               ["Delete expirable articles" gnus-summary-expire-articles-now
2243                (gnus-check-backend-function
2244                 'request-expire-articles gnus-newsgroup-name)])
2245              ("Extract"
2246               ["Uudecode" gnus-uu-decode-uu
2247                ,@(if (featurep 'xemacs) '(t)
2248                    '(:help "Decode uuencoded article(s)"))]
2249               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2250               ["Unshar" gnus-uu-decode-unshar t]
2251               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2252               ["Save" gnus-uu-decode-save t]
2253               ["Binhex" gnus-uu-decode-binhex t]
2254               ["Postscript" gnus-uu-decode-postscript t]
2255               ["All MIME parts" gnus-summary-save-parts t])
2256              ("Cache"
2257               ["Enter article" gnus-cache-enter-article t]
2258               ["Remove article" gnus-cache-remove-article t])
2259              ["Translate" gnus-article-babel t]
2260              ["Select article buffer" gnus-summary-select-article-buffer t]
2261              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2262              ["Isearch article..." gnus-summary-isearch-article t]
2263              ["Beginning of the article" gnus-summary-beginning-of-article t]
2264              ["End of the article" gnus-summary-end-of-article t]
2265              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2266              ["Fetch referenced articles" gnus-summary-refer-references t]
2267              ["Fetch current thread" gnus-summary-refer-thread t]
2268              ["Fetch article with id..." gnus-summary-refer-article t]
2269              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2270              ["Redisplay" gnus-summary-show-article t]
2271              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2272       (easy-menu-define
2273         gnus-summary-article-menu gnus-summary-mode-map ""
2274         (cons "Article" innards))
2275
2276       (if (not (keymapp gnus-summary-article-menu))
2277           (easy-menu-define
2278             gnus-article-commands-menu gnus-article-mode-map ""
2279             (cons "Commands" innards))
2280         ;; in Emacs, don't share menu.
2281         (setq gnus-article-commands-menu
2282               (copy-keymap gnus-summary-article-menu))
2283         (define-key gnus-article-mode-map [menu-bar commands]
2284           (cons "Commands" gnus-article-commands-menu))))
2285
2286     (easy-menu-define
2287       gnus-summary-thread-menu gnus-summary-mode-map ""
2288       '("Threads"
2289         ["Find all messages in thread" gnus-summary-refer-thread t]
2290         ["Toggle threading" gnus-summary-toggle-threads t]
2291         ["Hide threads" gnus-summary-hide-all-threads t]
2292         ["Show threads" gnus-summary-show-all-threads t]
2293         ["Hide thread" gnus-summary-hide-thread t]
2294         ["Show thread" gnus-summary-show-thread t]
2295         ["Go to next thread" gnus-summary-next-thread t]
2296         ["Go to previous thread" gnus-summary-prev-thread t]
2297         ["Go down thread" gnus-summary-down-thread t]
2298         ["Go up thread" gnus-summary-up-thread t]
2299         ["Top of thread" gnus-summary-top-thread t]
2300         ["Mark thread as read" gnus-summary-kill-thread t]
2301         ["Lower thread score" gnus-summary-lower-thread t]
2302         ["Raise thread score" gnus-summary-raise-thread t]
2303         ["Rethread current" gnus-summary-rethread-current t]))
2304
2305     (easy-menu-define
2306       gnus-summary-post-menu gnus-summary-mode-map ""
2307       `("Post"
2308         ["Send a message (mail or news)" gnus-summary-post-news
2309          ,@(if (featurep 'xemacs) '(t)
2310              '(:help "Post an article"))]
2311         ["Followup" gnus-summary-followup
2312          ,@(if (featurep 'xemacs) '(t)
2313              '(:help "Post followup to this article"))]
2314         ["Followup and yank" gnus-summary-followup-with-original
2315          ,@(if (featurep 'xemacs) '(t)
2316              '(:help "Post followup to this article, quoting its contents"))]
2317         ["Supersede article" gnus-summary-supersede-article t]
2318         ["Cancel article" gnus-summary-cancel-article
2319          ,@(if (featurep 'xemacs) '(t)
2320              '(:help "Cancel an article you posted"))]
2321         ["Reply" gnus-summary-reply t]
2322         ["Reply and yank" gnus-summary-reply-with-original t]
2323         ["Wide reply" gnus-summary-wide-reply t]
2324         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2325          ,@(if (featurep 'xemacs) '(t)
2326              '(:help "Mail a reply, quoting this article"))]
2327         ["Very wide reply" gnus-summary-very-wide-reply t]
2328         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2329          ,@(if (featurep 'xemacs) '(t)
2330              '(:help "Mail a very wide reply, quoting this article"))]
2331         ["Mail forward" gnus-summary-mail-forward t]
2332         ["Post forward" gnus-summary-post-forward t]
2333         ["Digest and mail" gnus-uu-digest-mail-forward t]
2334         ["Digest and post" gnus-uu-digest-post-forward t]
2335         ["Resend message" gnus-summary-resend-message t]
2336         ["Resend message edit" gnus-summary-resend-message-edit t]
2337         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2338         ["Send a mail" gnus-summary-mail-other-window t]
2339         ["Create a local message" gnus-summary-news-other-window t]
2340         ["Uuencode and post" gnus-uu-post-news
2341          ,@(if (featurep 'xemacs) '(t)
2342              '(:help "Post a uuencoded article"))]
2343         ["Followup via news" gnus-summary-followup-to-mail t]
2344         ["Followup via news and yank"
2345          gnus-summary-followup-to-mail-with-original t]
2346         ;;("Draft"
2347         ;;["Send" gnus-summary-send-draft t]
2348         ;;["Send bounced" gnus-resend-bounced-mail t])
2349         ))
2350
2351     (cond
2352      ((not (keymapp gnus-summary-post-menu))
2353       (setq gnus-article-post-menu gnus-summary-post-menu))
2354      ((not gnus-article-post-menu)
2355       ;; Don't share post menu.
2356       (setq gnus-article-post-menu
2357             (copy-keymap gnus-summary-post-menu))))
2358     (define-key gnus-article-mode-map [menu-bar post]
2359       (cons "Post" gnus-article-post-menu))
2360
2361     (easy-menu-define
2362       gnus-summary-misc-menu gnus-summary-mode-map ""
2363       `("Gnus"
2364         ("Mark Read"
2365          ["Mark as read" gnus-summary-mark-as-read-forward t]
2366          ["Mark same subject and select"
2367           gnus-summary-kill-same-subject-and-select t]
2368          ["Mark same subject" gnus-summary-kill-same-subject t]
2369          ["Catchup" gnus-summary-catchup
2370           ,@(if (featurep 'xemacs) '(t)
2371               '(:help "Mark unread articles in this group as read"))]
2372          ["Catchup all" gnus-summary-catchup-all t]
2373          ["Catchup to here" gnus-summary-catchup-to-here t]
2374          ["Catchup from here" gnus-summary-catchup-from-here t]
2375          ["Catchup region" gnus-summary-mark-region-as-read
2376           (gnus-mark-active-p)]
2377          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2378         ("Mark Various"
2379          ["Tick" gnus-summary-tick-article-forward t]
2380          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2381          ["Remove marks" gnus-summary-clear-mark-forward t]
2382          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2383          ["Set bookmark" gnus-summary-set-bookmark t]
2384          ["Remove bookmark" gnus-summary-remove-bookmark t])
2385         ("Limit to"
2386          ["Marks..." gnus-summary-limit-to-marks t]
2387          ["Subject..." gnus-summary-limit-to-subject t]
2388          ["Author..." gnus-summary-limit-to-author t]
2389          ["Age..." gnus-summary-limit-to-age t]
2390          ["Extra..." gnus-summary-limit-to-extra t]
2391          ["Score..." gnus-summary-limit-to-score t]
2392          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2393          ["Unread" gnus-summary-limit-to-unread t]
2394          ["Unseen" gnus-summary-limit-to-unseen t]
2395          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2396          ["Next articles" gnus-summary-limit-to-articles t]
2397          ["Pop limit" gnus-summary-pop-limit t]
2398          ["Show dormant" gnus-summary-limit-include-dormant t]
2399          ["Hide childless dormant"
2400           gnus-summary-limit-exclude-childless-dormant t]
2401          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2402          ["Hide marked" gnus-summary-limit-exclude-marks t]
2403          ["Show expunged" gnus-summary-limit-include-expunged t])
2404         ("Process Mark"
2405          ["Set mark" gnus-summary-mark-as-processable t]
2406          ["Remove mark" gnus-summary-unmark-as-processable t]
2407          ["Remove all marks" gnus-summary-unmark-all-processable t]
2408          ["Mark above" gnus-uu-mark-over t]
2409          ["Mark series" gnus-uu-mark-series t]
2410          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2411          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2412          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2413          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2414          ["Mark all" gnus-uu-mark-all t]
2415          ["Mark buffer" gnus-uu-mark-buffer t]
2416          ["Mark sparse" gnus-uu-mark-sparse t]
2417          ["Mark thread" gnus-uu-mark-thread t]
2418          ["Unmark thread" gnus-uu-unmark-thread t]
2419          ("Process Mark Sets"
2420           ["Kill" gnus-summary-kill-process-mark t]
2421           ["Yank" gnus-summary-yank-process-mark
2422            gnus-newsgroup-process-stack]
2423           ["Save" gnus-summary-save-process-mark t]
2424           ["Run command on marked..." gnus-summary-universal-argument t]))
2425         ("Scroll article"
2426          ["Page forward" gnus-summary-next-page
2427           ,@(if (featurep 'xemacs) '(t)
2428               '(:help "Show next page of article"))]
2429          ["Page backward" gnus-summary-prev-page
2430           ,@(if (featurep 'xemacs) '(t)
2431               '(:help "Show previous page of article"))]
2432          ["Line forward" gnus-summary-scroll-up t])
2433         ("Move"
2434          ["Next unread article" gnus-summary-next-unread-article t]
2435          ["Previous unread article" gnus-summary-prev-unread-article t]
2436          ["Next article" gnus-summary-next-article t]
2437          ["Previous article" gnus-summary-prev-article t]
2438          ["Next unread subject" gnus-summary-next-unread-subject t]
2439          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2440          ["Next article same subject" gnus-summary-next-same-subject t]
2441          ["Previous article same subject" gnus-summary-prev-same-subject t]
2442          ["First unread article" gnus-summary-first-unread-article t]
2443          ["Best unread article" gnus-summary-best-unread-article t]
2444          ["Go to subject number..." gnus-summary-goto-subject t]
2445          ["Go to article number..." gnus-summary-goto-article t]
2446          ["Go to the last article" gnus-summary-goto-last-article t]
2447          ["Pop article off history" gnus-summary-pop-article t])
2448         ("Sort"
2449          ["Sort by number" gnus-summary-sort-by-number t]
2450          ["Sort by author" gnus-summary-sort-by-author t]
2451          ["Sort by subject" gnus-summary-sort-by-subject t]
2452          ["Sort by date" gnus-summary-sort-by-date t]
2453          ["Sort by score" gnus-summary-sort-by-score t]
2454          ["Sort by lines" gnus-summary-sort-by-lines t]
2455          ["Sort by characters" gnus-summary-sort-by-chars t]
2456          ["Randomize" gnus-summary-sort-by-random t]
2457          ["Original sort" gnus-summary-sort-by-original t])
2458         ("Help"
2459          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2460          ["Describe group" gnus-summary-describe-group t]
2461          ["Fetch charter" gnus-group-fetch-charter
2462           ,@(if (featurep 'xemacs) nil
2463               '(:help "Display the charter of the current group"))]
2464          ["Fetch control message" gnus-group-fetch-control
2465           ,@(if (featurep 'xemacs) nil
2466               '(:help "Display the archived control message for the current group"))]
2467          ["Read manual" gnus-info-find-node t])
2468         ("Modes"
2469          ["Pick and read" gnus-pick-mode t]
2470          ["Binary" gnus-binary-mode t])
2471         ("Regeneration"
2472          ["Regenerate" gnus-summary-prepare t]
2473          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2474          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2475          ["Toggle threading" gnus-summary-toggle-threads t])
2476         ["See old articles" gnus-summary-insert-old-articles t]
2477         ["See new articles" gnus-summary-insert-new-articles t]
2478         ["Filter articles..." gnus-summary-execute-command t]
2479         ["Run command on articles..." gnus-summary-universal-argument t]
2480         ["Search articles forward..." gnus-summary-search-article-forward t]
2481         ["Search articles backward..." gnus-summary-search-article-backward t]
2482         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2483         ["Expand window" gnus-summary-expand-window t]
2484         ["Expire expirable articles" gnus-summary-expire-articles
2485          (gnus-check-backend-function
2486           'request-expire-articles gnus-newsgroup-name)]
2487         ["Edit local kill file" gnus-summary-edit-local-kill t]
2488         ["Edit main kill file" gnus-summary-edit-global-kill t]
2489         ["Edit group parameters" gnus-summary-edit-parameters t]
2490         ["Customize group parameters" gnus-summary-customize-parameters t]
2491         ["Send a bug report" gnus-bug t]
2492         ("Exit"
2493          ["Catchup and exit" gnus-summary-catchup-and-exit
2494           ,@(if (featurep 'xemacs) '(t)
2495               '(:help "Mark unread articles in this group as read, then exit"))]
2496          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2497          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2498          ["Exit group" gnus-summary-exit
2499           ,@(if (featurep 'xemacs) '(t)
2500               '(:help "Exit current group, return to group selection mode"))]
2501          ["Exit group without updating" gnus-summary-exit-no-update t]
2502          ["Exit and goto next group" gnus-summary-next-group t]
2503          ["Exit and goto prev group" gnus-summary-prev-group t]
2504          ["Reselect group" gnus-summary-reselect-current-group t]
2505          ["Rescan group" gnus-summary-rescan-group t]
2506          ["Update dribble" gnus-summary-save-newsrc t])))
2507
2508     (gnus-run-hooks 'gnus-summary-menu-hook)))
2509
2510 (defvar gnus-summary-tool-bar-map nil)
2511
2512 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2513 (defun gnus-summary-make-tool-bar ()
2514   (if (and (fboundp 'tool-bar-add-item-from-menu)
2515            (default-value 'tool-bar-mode)
2516            (not gnus-summary-tool-bar-map))
2517       (setq gnus-summary-tool-bar-map
2518             (let ((tool-bar-map (make-sparse-keymap))
2519                   (load-path (mm-image-load-path)))
2520               (tool-bar-add-item-from-menu
2521                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2522               (tool-bar-add-item-from-menu
2523                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2524               (tool-bar-add-item-from-menu
2525                'gnus-summary-post-news "post" gnus-summary-mode-map)
2526               (tool-bar-add-item-from-menu
2527                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2528               (tool-bar-add-item-from-menu
2529                'gnus-summary-followup "followup" gnus-summary-mode-map)
2530               (tool-bar-add-item-from-menu
2531                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2532               (tool-bar-add-item-from-menu
2533                'gnus-summary-reply "reply" gnus-summary-mode-map)
2534               (tool-bar-add-item-from-menu
2535                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2536               (tool-bar-add-item-from-menu
2537                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2538               (tool-bar-add-item-from-menu
2539                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2540               (tool-bar-add-item-from-menu
2541                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2542               (tool-bar-add-item-from-menu
2543                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2544               (tool-bar-add-item-from-menu
2545                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2546               (tool-bar-add-item-from-menu
2547                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2548               (tool-bar-add-item-from-menu
2549                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2550               tool-bar-map)))
2551   (if gnus-summary-tool-bar-map
2552       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2553
2554 (defun gnus-score-set-default (var value)
2555   "A version of set that updates the GNU Emacs menu-bar."
2556   (set var value)
2557   ;; It is the message that forces the active status to be updated.
2558   (message ""))
2559
2560 (defun gnus-make-score-map (type)
2561   "Make a summary score map of type TYPE."
2562   (if t
2563       nil
2564     (let ((headers '(("author" "from" string)
2565                      ("subject" "subject" string)
2566                      ("article body" "body" string)
2567                      ("article head" "head" string)
2568                      ("xref" "xref" string)
2569                      ("extra header" "extra" string)
2570                      ("lines" "lines" number)
2571                      ("followups to author" "followup" string)))
2572           (types '((number ("less than" <)
2573                            ("greater than" >)
2574                            ("equal" =))
2575                    (string ("substring" s)
2576                            ("exact string" e)
2577                            ("fuzzy string" f)
2578                            ("regexp" r))))
2579           (perms '(("temporary" (current-time-string))
2580                    ("permanent" nil)
2581                    ("immediate" now)))
2582           header)
2583       (list
2584        (apply
2585         'nconc
2586         (list
2587          (if (eq type 'lower)
2588              "Lower score"
2589            "Increase score"))
2590         (let (outh)
2591           (while headers
2592             (setq header (car headers))
2593             (setq outh
2594                   (cons
2595                    (apply
2596                     'nconc
2597                     (list (car header))
2598                     (let ((ts (cdr (assoc (nth 2 header) types)))
2599                           outt)
2600                       (while ts
2601                         (setq outt
2602                               (cons
2603                                (apply
2604                                 'nconc
2605                                 (list (caar ts))
2606                                 (let ((ps perms)
2607                                       outp)
2608                                   (while ps
2609                                     (setq outp
2610                                           (cons
2611                                            (vector
2612                                             (caar ps)
2613                                             (list
2614                                              'gnus-summary-score-entry
2615                                              (nth 1 header)
2616                                              (if (or (string= (nth 1 header)
2617                                                               "head")
2618                                                      (string= (nth 1 header)
2619                                                               "body"))
2620                                                  ""
2621                                                (list 'gnus-summary-header
2622                                                      (nth 1 header)))
2623                                              (list 'quote (nth 1 (car ts)))
2624                                              (list 'gnus-score-delta-default
2625                                                    nil)
2626                                              (nth 1 (car ps))
2627                                              t)
2628                                             t)
2629                                            outp))
2630                                     (setq ps (cdr ps)))
2631                                   (list (nreverse outp))))
2632                                outt))
2633                         (setq ts (cdr ts)))
2634                       (list (nreverse outt))))
2635                    outh))
2636             (setq headers (cdr headers)))
2637           (list (nreverse outh))))))))
2638
2639 \f
2640
2641 (defun gnus-summary-mode (&optional group)
2642   "Major mode for reading articles.
2643
2644 All normal editing commands are switched off.
2645 \\<gnus-summary-mode-map>
2646 Each line in this buffer represents one article.  To read an
2647 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2648 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2649 respectively.
2650
2651 You can also post articles and send mail from this buffer.  To
2652 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2653 of an article, type `\\[gnus-summary-reply]'.
2654
2655 There are approx. one gazillion commands you can execute in this
2656 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2657
2658 The following commands are available:
2659
2660 \\{gnus-summary-mode-map}"
2661   (interactive)
2662   (kill-all-local-variables)
2663   (when (gnus-visual-p 'summary-menu 'menu)
2664     (gnus-summary-make-menu-bar)
2665     (gnus-summary-make-tool-bar))
2666   (gnus-summary-make-local-variables)
2667   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2668     (gnus-summary-make-local-variables))
2669   (gnus-make-thread-indent-array)
2670   (gnus-simplify-mode-line)
2671   (setq major-mode 'gnus-summary-mode)
2672   (setq mode-name "Summary")
2673   (make-local-variable 'minor-mode-alist)
2674   (use-local-map gnus-summary-mode-map)
2675   (buffer-disable-undo)
2676   (setq buffer-read-only t)             ;Disable modification
2677   (setq truncate-lines t)
2678   (setq selective-display t)
2679   (setq selective-display-ellipses t)   ;Display `...'
2680   (gnus-summary-set-display-table)
2681   (gnus-set-default-directory)
2682   (setq gnus-newsgroup-name group)
2683   (make-local-variable 'gnus-summary-line-format)
2684   (make-local-variable 'gnus-summary-line-format-spec)
2685   (make-local-variable 'gnus-summary-dummy-line-format)
2686   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2687   (make-local-variable 'gnus-summary-mark-positions)
2688   (gnus-make-local-hook 'pre-command-hook)
2689   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2690   (gnus-run-hooks 'gnus-summary-mode-hook)
2691   (turn-on-gnus-mailing-list-mode)
2692   (mm-enable-multibyte)
2693   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2694   (gnus-update-summary-mark-positions))
2695
2696 (defun gnus-summary-make-local-variables ()
2697   "Make all the local summary buffer variables."
2698   (let (global)
2699     (dolist (local gnus-summary-local-variables)
2700       (if (consp local)
2701           (progn
2702             (if (eq (cdr local) 'global)
2703                 ;; Copy the global value of the variable.
2704                 (setq global (symbol-value (car local)))
2705               ;; Use the value from the list.
2706               (setq global (eval (cdr local))))
2707             (set (make-local-variable (car local)) global))
2708         ;; Simple nil-valued local variable.
2709         (set (make-local-variable local) nil)))))
2710
2711 (defun gnus-summary-clear-local-variables ()
2712   (let ((locals gnus-summary-local-variables))
2713     (while locals
2714       (if (consp (car locals))
2715           (and (vectorp (caar locals))
2716                (set (caar locals) nil))
2717         (and (vectorp (car locals))
2718              (set (car locals) nil)))
2719       (setq locals (cdr locals)))))
2720
2721 ;; Summary data functions.
2722
2723 (defmacro gnus-data-number (data)
2724   `(car ,data))
2725
2726 (defmacro gnus-data-set-number (data number)
2727   `(setcar ,data ,number))
2728
2729 (defmacro gnus-data-mark (data)
2730   `(nth 1 ,data))
2731
2732 (defmacro gnus-data-set-mark (data mark)
2733   `(setcar (nthcdr 1 ,data) ,mark))
2734
2735 (defmacro gnus-data-pos (data)
2736   `(nth 2 ,data))
2737
2738 (defmacro gnus-data-set-pos (data pos)
2739   `(setcar (nthcdr 2 ,data) ,pos))
2740
2741 (defmacro gnus-data-header (data)
2742   `(nth 3 ,data))
2743
2744 (defmacro gnus-data-set-header (data header)
2745   `(setf (nth 3 ,data) ,header))
2746
2747 (defmacro gnus-data-level (data)
2748   `(nth 4 ,data))
2749
2750 (defmacro gnus-data-unread-p (data)
2751   `(= (nth 1 ,data) gnus-unread-mark))
2752
2753 (defmacro gnus-data-read-p (data)
2754   `(/= (nth 1 ,data) gnus-unread-mark))
2755
2756 (defmacro gnus-data-pseudo-p (data)
2757   `(consp (nth 3 ,data)))
2758
2759 (defmacro gnus-data-find (number)
2760   `(assq ,number gnus-newsgroup-data))
2761
2762 (defmacro gnus-data-find-list (number &optional data)
2763   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2764      (memq (assq ,number bdata)
2765            bdata)))
2766
2767 (defmacro gnus-data-make (number mark pos header level)
2768   `(list ,number ,mark ,pos ,header ,level))
2769
2770 (defun gnus-data-enter (after-article number mark pos header level offset)
2771   (let ((data (gnus-data-find-list after-article)))
2772     (unless data
2773       (error "No such article: %d" after-article))
2774     (setcdr data (cons (gnus-data-make number mark pos header level)
2775                        (cdr data)))
2776     (setq gnus-newsgroup-data-reverse nil)
2777     (gnus-data-update-list (cddr data) offset)))
2778
2779 (defun gnus-data-enter-list (after-article list &optional offset)
2780   (when list
2781     (let ((data (and after-article (gnus-data-find-list after-article)))
2782           (ilist list))
2783       (if (not (or data
2784                    after-article))
2785           (let ((odata gnus-newsgroup-data))
2786             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2787             (when offset
2788               (gnus-data-update-list odata offset)))
2789       ;; Find the last element in the list to be spliced into the main
2790         ;; list.
2791         (while (cdr list)
2792           (setq list (cdr list)))
2793         (if (not data)
2794             (progn
2795               (setcdr list gnus-newsgroup-data)
2796               (setq gnus-newsgroup-data ilist)
2797               (when offset
2798                 (gnus-data-update-list (cdr list) offset)))
2799           (setcdr list (cdr data))
2800           (setcdr data ilist)
2801           (when offset
2802             (gnus-data-update-list (cdr list) offset))))
2803       (setq gnus-newsgroup-data-reverse nil))))
2804
2805 (defun gnus-data-remove (article &optional offset)
2806   (let ((data gnus-newsgroup-data))
2807     (if (= (gnus-data-number (car data)) article)
2808         (progn
2809           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2810                 gnus-newsgroup-data-reverse nil)
2811           (when offset
2812             (gnus-data-update-list gnus-newsgroup-data offset)))
2813       (while (cdr data)
2814         (when (= (gnus-data-number (cadr data)) article)
2815           (setcdr data (cddr data))
2816           (when offset
2817             (gnus-data-update-list (cdr data) offset))
2818           (setq data nil
2819                 gnus-newsgroup-data-reverse nil))
2820         (setq data (cdr data))))))
2821
2822 (defmacro gnus-data-list (backward)
2823   `(if ,backward
2824        (or gnus-newsgroup-data-reverse
2825            (setq gnus-newsgroup-data-reverse
2826                  (reverse gnus-newsgroup-data)))
2827      gnus-newsgroup-data))
2828
2829 (defun gnus-data-update-list (data offset)
2830   "Add OFFSET to the POS of all data entries in DATA."
2831   (setq gnus-newsgroup-data-reverse nil)
2832   (while data
2833     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2834     (setq data (cdr data))))
2835
2836 (defun gnus-summary-article-pseudo-p (article)
2837   "Say whether this article is a pseudo article or not."
2838   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2839
2840 (defmacro gnus-summary-article-sparse-p (article)
2841   "Say whether this article is a sparse article or not."
2842   `(memq ,article gnus-newsgroup-sparse))
2843
2844 (defmacro gnus-summary-article-ancient-p (article)
2845   "Say whether this article is a sparse article or not."
2846   `(memq ,article gnus-newsgroup-ancient))
2847
2848 (defun gnus-article-parent-p (number)
2849   "Say whether this article is a parent or not."
2850   (let ((data (gnus-data-find-list number)))
2851     (and (cdr data)              ; There has to be an article after...
2852          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2853             (gnus-data-level (nth 1 data))))))
2854
2855 (defun gnus-article-children (number)
2856   "Return a list of all children to NUMBER."
2857   (let* ((data (gnus-data-find-list number))
2858          (level (gnus-data-level (car data)))
2859          children)
2860     (setq data (cdr data))
2861     (while (and data
2862                 (= (gnus-data-level (car data)) (1+ level)))
2863       (push (gnus-data-number (car data)) children)
2864       (setq data (cdr data)))
2865     children))
2866
2867 (defmacro gnus-summary-skip-intangible ()
2868   "If the current article is intangible, then jump to a different article."
2869   '(let ((to (get-text-property (point) 'gnus-intangible)))
2870      (and to (gnus-summary-goto-subject to))))
2871
2872 (defmacro gnus-summary-article-intangible-p ()
2873   "Say whether this article is intangible or not."
2874   '(get-text-property (point) 'gnus-intangible))
2875
2876 (defun gnus-article-read-p (article)
2877   "Say whether ARTICLE is read or not."
2878   (not (or (memq article gnus-newsgroup-marked)
2879            (memq article gnus-newsgroup-spam-marked)
2880            (memq article gnus-newsgroup-unreads)
2881            (memq article gnus-newsgroup-unselected)
2882            (memq article gnus-newsgroup-dormant))))
2883
2884 ;; Some summary mode macros.
2885
2886 (defmacro gnus-summary-article-number ()
2887   "The article number of the article on the current line.
2888 If there isn't an article number here, then we return the current
2889 article number."
2890   '(progn
2891      (gnus-summary-skip-intangible)
2892      (or (get-text-property (point) 'gnus-number)
2893          (gnus-summary-last-subject))))
2894
2895 (defmacro gnus-summary-article-header (&optional number)
2896   "Return the header of article NUMBER."
2897   `(gnus-data-header (gnus-data-find
2898                       ,(or number '(gnus-summary-article-number)))))
2899
2900 (defmacro gnus-summary-thread-level (&optional number)
2901   "Return the level of thread that starts with article NUMBER."
2902   `(if (and (eq gnus-summary-make-false-root 'dummy)
2903             (get-text-property (point) 'gnus-intangible))
2904        0
2905      (gnus-data-level (gnus-data-find
2906                        ,(or number '(gnus-summary-article-number))))))
2907
2908 (defmacro gnus-summary-article-mark (&optional number)
2909   "Return the mark of article NUMBER."
2910   `(gnus-data-mark (gnus-data-find
2911                     ,(or number '(gnus-summary-article-number)))))
2912
2913 (defmacro gnus-summary-article-pos (&optional number)
2914   "Return the position of the line of article NUMBER."
2915   `(gnus-data-pos (gnus-data-find
2916                    ,(or number '(gnus-summary-article-number)))))
2917
2918 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2919 (defmacro gnus-summary-article-subject (&optional number)
2920   "Return current subject string or nil if nothing."
2921   `(let ((headers
2922           ,(if number
2923                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2924              '(gnus-data-header (assq (gnus-summary-article-number)
2925                                       gnus-newsgroup-data)))))
2926      (and headers
2927           (vectorp headers)
2928           (mail-header-subject headers))))
2929
2930 (defmacro gnus-summary-article-score (&optional number)
2931   "Return current article score."
2932   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2933                   gnus-newsgroup-scored))
2934        gnus-summary-default-score 0))
2935
2936 (defun gnus-summary-article-children (&optional number)
2937   "Return a list of article numbers that are children of article NUMBER."
2938   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2939          (level (gnus-data-level (car data)))
2940          l children)
2941     (while (and (setq data (cdr data))
2942                 (> (setq l (gnus-data-level (car data))) level))
2943       (and (= (1+ level) l)
2944            (push (gnus-data-number (car data))
2945                  children)))
2946     (nreverse children)))
2947
2948 (defun gnus-summary-article-parent (&optional number)
2949   "Return the article number of the parent of article NUMBER."
2950   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2951                                     (gnus-data-list t)))
2952          (level (gnus-data-level (car data))))
2953     (if (zerop level)
2954         ()                              ; This is a root.
2955       ;; We search until we find an article with a level less than
2956       ;; this one.  That function has to be the parent.
2957       (while (and (setq data (cdr data))
2958                   (not (< (gnus-data-level (car data)) level))))
2959       (and data (gnus-data-number (car data))))))
2960
2961 (defun gnus-unread-mark-p (mark)
2962   "Say whether MARK is the unread mark."
2963   (= mark gnus-unread-mark))
2964
2965 (defun gnus-read-mark-p (mark)
2966   "Say whether MARK is one of the marks that mark as read.
2967 This is all marks except unread, ticked, dormant, and expirable."
2968   (not (or (= mark gnus-unread-mark)
2969            (= mark gnus-ticked-mark)
2970            (= mark gnus-spam-mark)
2971            (= mark gnus-dormant-mark)
2972            (= mark gnus-expirable-mark))))
2973
2974 (defmacro gnus-article-mark (number)
2975   "Return the MARK of article NUMBER.
2976 This macro should only be used when computing the mark the \"first\"
2977 time; i.e., when generating the summary lines.  After that,
2978 `gnus-summary-article-mark' should be used to examine the
2979 marks of articles."
2980   `(cond
2981     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2982     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2983     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2984     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2985     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2986     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2987     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2988     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2989            gnus-ancient-mark))))
2990
2991 ;; Saving hidden threads.
2992
2993 (defmacro gnus-save-hidden-threads (&rest forms)
2994   "Save hidden threads, eval FORMS, and restore the hidden threads."
2995   (let ((config (make-symbol "config")))
2996     `(let ((,config (gnus-hidden-threads-configuration)))
2997        (unwind-protect
2998            (save-excursion
2999              ,@forms)
3000          (gnus-restore-hidden-threads-configuration ,config)))))
3001 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3002 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3003
3004 (defun gnus-data-compute-positions ()
3005   "Compute the positions of all articles."
3006   (setq gnus-newsgroup-data-reverse nil)
3007   (let ((data gnus-newsgroup-data))
3008     (save-excursion
3009       (gnus-save-hidden-threads
3010         (gnus-summary-show-all-threads)
3011         (goto-char (point-min))
3012         (while data
3013           (while (get-text-property (point) 'gnus-intangible)
3014             (forward-line 1))
3015           (gnus-data-set-pos (car data) (+ (point) 3))
3016           (setq data (cdr data))
3017           (forward-line 1))))))
3018
3019 (defun gnus-hidden-threads-configuration ()
3020   "Return the current hidden threads configuration."
3021   (save-excursion
3022     (let (config)
3023       (goto-char (point-min))
3024       (while (search-forward "\r" nil t)
3025         (push (1- (point)) config))
3026       config)))
3027
3028 (defun gnus-restore-hidden-threads-configuration (config)
3029   "Restore hidden threads configuration from CONFIG."
3030   (save-excursion
3031     (let (point buffer-read-only)
3032       (while (setq point (pop config))
3033         (when (and (< point (point-max))
3034                    (goto-char point)
3035                    (eq (char-after) ?\n))
3036           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3037
3038 ;; Various summary mode internalish functions.
3039
3040 (defun gnus-mouse-pick-article (e)
3041   (interactive "e")
3042   (mouse-set-point e)
3043   (gnus-summary-next-page nil t))
3044
3045 (defun gnus-summary-set-display-table ()
3046   "Change the display table.
3047 Odd characters have a tendency to mess
3048 up nicely formatted displays - we make all possible glyphs
3049 display only a single character."
3050
3051   ;; We start from the standard display table, if any.
3052   (let ((table (or (copy-sequence standard-display-table)
3053                    (make-display-table)))
3054         (i 32))
3055     ;; Nix out all the control chars...
3056     (while (>= (setq i (1- i)) 0)
3057       (aset table i [??]))
3058    ;; ... but not newline and cr, of course.  (cr is necessary for the
3059     ;; selective display).
3060     (aset table ?\n nil)
3061     (aset table ?\r nil)
3062     ;; We keep TAB as well.
3063     (aset table ?\t nil)
3064     ;; We nix out any glyphs over 126 that are not set already.
3065     (let ((i 256))
3066       (while (>= (setq i (1- i)) 127)
3067         ;; Only modify if the entry is nil.
3068         (unless (aref table i)
3069           (aset table i [??]))))
3070     (setq buffer-display-table table)))
3071
3072 (defun gnus-summary-set-article-display-arrow (pos)
3073   "Update the overlay arrow to point to line at position POS."
3074   (when (and gnus-summary-display-arrow
3075              (boundp 'overlay-arrow-position)
3076              (boundp 'overlay-arrow-string))
3077     (save-excursion
3078       (goto-char pos)
3079       (beginning-of-line)
3080       (unless overlay-arrow-position
3081         (setq overlay-arrow-position (make-marker)))
3082       (setq overlay-arrow-string "=>"
3083             overlay-arrow-position (set-marker overlay-arrow-position
3084                                                (point)
3085                                                (current-buffer))))))
3086
3087 (defun gnus-summary-setup-buffer (group)
3088   "Initialize summary buffer."
3089   (let ((buffer (gnus-summary-buffer-name group))
3090         (dead-name (concat "*Dead Summary "
3091                            (gnus-group-decoded-name group) "*")))
3092     ;; If a dead summary buffer exists, we kill it.
3093     (when (gnus-buffer-live-p dead-name)
3094       (gnus-kill-buffer dead-name))
3095     (if (get-buffer buffer)
3096         (progn
3097           (set-buffer buffer)
3098           (setq gnus-summary-buffer (current-buffer))
3099           (not gnus-newsgroup-prepared))
3100       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3101       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3102       (gnus-summary-mode group)
3103       (when gnus-carpal
3104         (gnus-carpal-setup-buffer 'summary))
3105       (unless gnus-single-article-buffer
3106         (make-local-variable 'gnus-article-buffer)
3107         (make-local-variable 'gnus-article-current)
3108         (make-local-variable 'gnus-original-article-buffer))
3109       (setq gnus-newsgroup-name group)
3110       ;; Set any local variables in the group parameters.
3111       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3112       t)))
3113
3114 (defun gnus-set-global-variables ()
3115   "Set the global equivalents of the buffer-local variables.
3116 They are set to the latest values they had.  These reflect the summary
3117 buffer that was in action when the last article was fetched."
3118   (when (eq major-mode 'gnus-summary-mode)
3119     (setq gnus-summary-buffer (current-buffer))
3120     (let ((name gnus-newsgroup-name)
3121           (marked gnus-newsgroup-marked)
3122           (spam gnus-newsgroup-spam-marked)
3123           (unread gnus-newsgroup-unreads)
3124           (headers gnus-current-headers)
3125           (data gnus-newsgroup-data)
3126           (summary gnus-summary-buffer)
3127           (article-buffer gnus-article-buffer)
3128           (original gnus-original-article-buffer)
3129           (gac gnus-article-current)
3130           (reffed gnus-reffed-article-number)
3131           (score-file gnus-current-score-file)
3132           (default-charset gnus-newsgroup-charset)
3133           vlist)
3134       (let ((locals gnus-newsgroup-variables))
3135         (while locals
3136           (if (consp (car locals))
3137               (push (eval (caar locals)) vlist)
3138             (push (eval (car locals)) vlist))
3139           (setq locals (cdr locals)))
3140         (setq vlist (nreverse vlist)))
3141       (save-excursion
3142         (set-buffer gnus-group-buffer)
3143         (setq gnus-newsgroup-name name
3144               gnus-newsgroup-marked marked
3145               gnus-newsgroup-spam-marked spam
3146               gnus-newsgroup-unreads unread
3147               gnus-current-headers headers
3148               gnus-newsgroup-data data
3149               gnus-article-current gac
3150               gnus-summary-buffer summary
3151               gnus-article-buffer article-buffer
3152               gnus-original-article-buffer original
3153               gnus-reffed-article-number reffed
3154               gnus-current-score-file score-file
3155               gnus-newsgroup-charset default-charset)
3156         (let ((locals gnus-newsgroup-variables))
3157           (while locals
3158             (if (consp (car locals))
3159                 (set (caar locals) (pop vlist))
3160               (set (car locals) (pop vlist)))
3161             (setq locals (cdr locals))))
3162         ;; The article buffer also has local variables.
3163         (when (gnus-buffer-live-p gnus-article-buffer)
3164           (set-buffer gnus-article-buffer)
3165           (setq gnus-summary-buffer summary))))))
3166
3167 (defun gnus-summary-article-unread-p (article)
3168   "Say whether ARTICLE is unread or not."
3169   (memq article gnus-newsgroup-unreads))
3170
3171 (defun gnus-summary-first-article-p (&optional article)
3172   "Return whether ARTICLE is the first article in the buffer."
3173   (if (not (setq article (or article (gnus-summary-article-number))))
3174       nil
3175     (eq article (caar gnus-newsgroup-data))))
3176
3177 (defun gnus-summary-last-article-p (&optional article)
3178   "Return whether ARTICLE is the last article in the buffer."
3179   (if (not (setq article (or article (gnus-summary-article-number))))
3180       ;; All non-existent numbers are the last article.  :-)
3181       t
3182     (not (cdr (gnus-data-find-list article)))))
3183
3184 (defun gnus-make-thread-indent-array ()
3185   (let ((n 200))
3186     (unless (and gnus-thread-indent-array
3187                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3188       (setq gnus-thread-indent-array (make-vector 201 "")
3189             gnus-thread-indent-array-level gnus-thread-indent-level)
3190       (while (>= n 0)
3191         (aset gnus-thread-indent-array n
3192               (make-string (* n gnus-thread-indent-level) ? ))
3193         (setq n (1- n))))))
3194
3195 (defun gnus-update-summary-mark-positions ()
3196   "Compute where the summary marks are to go."
3197   (save-excursion
3198     (when (gnus-buffer-exists-p gnus-summary-buffer)
3199       (set-buffer gnus-summary-buffer))
3200     (let ((gnus-replied-mark 129)
3201           (gnus-score-below-mark 130)
3202           (gnus-score-over-mark 130)
3203           (gnus-undownloaded-mark 131)
3204           (spec gnus-summary-line-format-spec)
3205           gnus-visual pos)
3206       (save-excursion
3207         (gnus-set-work-buffer)
3208         (let ((gnus-summary-line-format-spec spec)
3209               (gnus-newsgroup-downloadable '(0)))
3210           (gnus-summary-insert-line
3211            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3212            0 nil t 128 t nil "" nil 1)
3213           (goto-char (point-min))
3214           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3215                                              (- (point) (point-min) 1)))))
3216           (goto-char (point-min))
3217           (push (cons 'replied (and (search-forward "\201" nil t)
3218                                     (- (point) (point-min) 1)))
3219                 pos)
3220           (goto-char (point-min))
3221           (push (cons 'score (and (search-forward "\202" nil t)
3222                                   (- (point) (point-min) 1)))
3223                 pos)
3224           (goto-char (point-min))
3225           (push (cons 'download
3226                       (and (search-forward "\203" nil t)
3227                            (- (point) (point-min) 1)))
3228                 pos)))
3229       (setq gnus-summary-mark-positions pos))))
3230
3231 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3232   "Insert a dummy root in the summary buffer."
3233   (beginning-of-line)
3234   (gnus-add-text-properties
3235    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3236    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3237
3238 (defun gnus-summary-extract-address-component (from)
3239   (or (car (funcall gnus-extract-address-components from))
3240       from))
3241
3242 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3243   (let ((mail-parse-charset gnus-newsgroup-charset)
3244         ; Is it really necessary to do this next part for each summary line?
3245         ; Luckily, doesn't seem to slow things down much.
3246         (mail-parse-ignored-charsets
3247          (save-excursion (set-buffer gnus-summary-buffer)
3248                          gnus-newsgroup-ignored-charsets)))
3249     (or
3250      (and gnus-ignored-from-addresses
3251           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3252           (let ((extra-headers (mail-header-extra header))
3253                 to
3254                 newsgroups)
3255             (cond
3256              ((setq to (cdr (assq 'To extra-headers)))
3257               (concat "-> "
3258                       (inline
3259                         (gnus-summary-extract-address-component
3260                          (funcall gnus-decode-encoded-word-function to)))))
3261              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3262               (concat "=> " newsgroups)))))
3263      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3264
3265 (defun gnus-summary-insert-line (gnus-tmp-header
3266                                  gnus-tmp-level gnus-tmp-current
3267                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3268                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3269                                  &optional gnus-tmp-dummy gnus-tmp-score
3270                                  gnus-tmp-process)
3271   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3272          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3273          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3274          (gnus-tmp-score-char
3275           (if (or (null gnus-summary-default-score)
3276                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3277                       gnus-summary-zcore-fuzz))
3278               ?                         ;Whitespace
3279             (if (< gnus-tmp-score gnus-summary-default-score)
3280                 gnus-score-below-mark gnus-score-over-mark)))
3281          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3282          (gnus-tmp-replied
3283           (cond (gnus-tmp-process gnus-process-mark)
3284                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3285                  gnus-cached-mark)
3286                 (gnus-tmp-replied gnus-replied-mark)
3287                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3288                  gnus-forwarded-mark)
3289                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3290                  gnus-saved-mark)
3291                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3292                  gnus-recent-mark)
3293                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3294                  gnus-unseen-mark)
3295                 (t gnus-no-mark)))
3296          (gnus-tmp-downloaded
3297           (cond (undownloaded
3298                  gnus-undownloaded-mark)
3299                 (gnus-newsgroup-agentized
3300                  gnus-downloaded-mark)
3301                 (t
3302                  gnus-no-mark)))
3303          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3304          (gnus-tmp-name
3305           (cond
3306            ((string-match "<[^>]+> *$" gnus-tmp-from)
3307             (let ((beg (match-beginning 0)))
3308               (or (and (string-match "^\".+\"" gnus-tmp-from)
3309                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3310                   (substring gnus-tmp-from 0 beg))))
3311            ((string-match "(.+)" gnus-tmp-from)
3312             (substring gnus-tmp-from
3313                        (1+ (match-beginning 0)) (1- (match-end 0))))
3314            (t gnus-tmp-from)))
3315          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3316          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3317          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3318          (buffer-read-only nil))
3319     (when (string= gnus-tmp-name "")
3320       (setq gnus-tmp-name gnus-tmp-from))
3321     (unless (numberp gnus-tmp-lines)
3322       (setq gnus-tmp-lines -1))
3323     (if (= gnus-tmp-lines -1)
3324         (setq gnus-tmp-lines "?")
3325       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3326       (gnus-put-text-property
3327      (point)
3328      (progn (eval gnus-summary-line-format-spec) (point))
3329        'gnus-number gnus-tmp-number)
3330     (when (gnus-visual-p 'summary-highlight 'highlight)
3331       (forward-line -1)
3332       (gnus-run-hooks 'gnus-summary-update-hook)
3333       (forward-line 1))))
3334
3335 (defun gnus-summary-update-line (&optional dont-update)
3336   "Update summary line after change."
3337   (when (and gnus-summary-default-score
3338              (not gnus-summary-inhibit-highlight))
3339     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3340            (article (gnus-summary-article-number))
3341            (score (gnus-summary-article-score article)))
3342       (unless dont-update
3343         (if (and gnus-summary-mark-below
3344                  (< (gnus-summary-article-score)
3345                     gnus-summary-mark-below))
3346             ;; This article has a low score, so we mark it as read.
3347             (when (memq article gnus-newsgroup-unreads)
3348               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3349           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3350             ;; This article was previously marked as read on account
3351             ;; of a low score, but now it has risen, so we mark it as
3352             ;; unread.
3353             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3354         (gnus-summary-update-mark
3355          (if (or (null gnus-summary-default-score)
3356                  (<= (abs (- score gnus-summary-default-score))
3357                      gnus-summary-zcore-fuzz))
3358              ?                          ;Whitespace
3359            (if (< score gnus-summary-default-score)
3360                gnus-score-below-mark gnus-score-over-mark))
3361          'score))
3362       ;; Do visual highlighting.
3363       (when (gnus-visual-p 'summary-highlight 'highlight)
3364         (gnus-run-hooks 'gnus-summary-update-hook)))))
3365
3366 (defvar gnus-tmp-new-adopts nil)
3367
3368 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3369   "Return the number of articles in THREAD.
3370 This may be 0 in some cases -- if none of the articles in
3371 the thread are to be displayed."
3372   (let* ((number
3373          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3374           (cond
3375            ((not (listp thread))
3376             1)
3377            ((and (consp thread) (cdr thread))
3378             (apply
3379              '+ 1 (mapcar
3380                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3381            ((null thread)
3382             1)
3383            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3384             1)
3385            (t 0))))
3386     (when (and level (zerop level) gnus-tmp-new-adopts)
3387       (incf number
3388             (apply '+ (mapcar
3389                        'gnus-summary-number-of-articles-in-thread
3390                        gnus-tmp-new-adopts))))
3391     (if char
3392         (if (> number 1) gnus-not-empty-thread-mark
3393           gnus-empty-thread-mark)
3394       number)))
3395
3396 (defsubst gnus-summary-line-message-size (head)
3397   "Return pretty-printed version of message size.
3398 This function is intended to be used in
3399 `gnus-summary-line-format-alist'."
3400   (let ((c (or (mail-header-chars head) -1)))
3401     (cond ((< c 0) "n/a")               ; chars not available
3402           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3403           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3404           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3405           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3406
3407
3408 (defun gnus-summary-set-local-parameters (group)
3409   "Go through the local params of GROUP and set all variable specs in that list."
3410   (let ((params (gnus-group-find-parameter group))
3411         (vars '(quit-config))           ; Ignore quit-config.
3412         elem)
3413     (while params
3414       (setq elem (car params)
3415             params (cdr params))
3416       (and (consp elem)                 ; Has to be a cons.
3417            (consp (cdr elem))           ; The cdr has to be a list.
3418            (symbolp (car elem))         ; Has to be a symbol in there.
3419            (not (memq (car elem) vars))
3420            (ignore-errors               ; So we set it.
3421              (push (car elem) vars)
3422              (make-local-variable (car elem))
3423              (set (car elem) (eval (nth 1 elem))))))))
3424
3425 (defun gnus-summary-read-group (group &optional show-all no-article
3426                                       kill-buffer no-display backward
3427                                       select-articles)
3428   "Start reading news in newsgroup GROUP.
3429 If SHOW-ALL is non-nil, already read articles are also listed.
3430 If NO-ARTICLE is non-nil, no article is selected initially.
3431 If NO-DISPLAY, don't generate a summary buffer."
3432   (let (result)
3433     (while (and group
3434                 (null (setq result
3435                             (let ((gnus-auto-select-next nil))
3436                               (or (gnus-summary-read-group-1
3437                                    group show-all no-article
3438                                    kill-buffer no-display
3439                                    select-articles)
3440                                   (setq show-all nil
3441                                         select-articles nil)))))
3442                 (eq gnus-auto-select-next 'quietly))
3443       (set-buffer gnus-group-buffer)
3444       ;; The entry function called above goes to the next
3445       ;; group automatically, so we go two groups back
3446       ;; if we are searching for the previous group.
3447       (when backward
3448         (gnus-group-prev-unread-group 2))
3449       (if (not (equal group (gnus-group-group-name)))
3450           (setq group (gnus-group-group-name))
3451         (setq group nil)))
3452     result))
3453
3454 (defun gnus-summary-read-group-1 (group show-all no-article
3455                                         kill-buffer no-display
3456                                         &optional select-articles)
3457   ;; Killed foreign groups can't be entered.
3458   ;;  (when (and (not (gnus-group-native-p group))
3459   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3460   ;;    (error "Dead non-native groups can't be entered"))
3461   (gnus-message 5 "Retrieving newsgroup: %s..."
3462                 (gnus-group-decoded-name group))
3463   (let* ((new-group (gnus-summary-setup-buffer group))
3464          (quit-config (gnus-group-quit-config group))
3465          (did-select (and new-group (gnus-select-newsgroup
3466                                      group show-all select-articles))))
3467     (cond
3468      ;; This summary buffer exists already, so we just select it.
3469      ((not new-group)
3470       (gnus-set-global-variables)
3471       (when kill-buffer
3472         (gnus-kill-or-deaden-summary kill-buffer))
3473       (gnus-configure-windows 'summary 'force)
3474       (gnus-set-mode-line 'summary)
3475       (gnus-summary-position-point)
3476       (message "")
3477       t)
3478      ;; We couldn't select this group.
3479      ((null did-select)
3480       (when (and (eq major-mode 'gnus-summary-mode)
3481                  (not (equal (current-buffer) kill-buffer)))
3482         (kill-buffer (current-buffer))
3483         (if (not quit-config)
3484             (progn
3485               ;; Update the info -- marks might need to be removed,
3486               ;; for instance.
3487               (gnus-summary-update-info)
3488               (set-buffer gnus-group-buffer)
3489               (gnus-group-jump-to-group group)
3490               (gnus-group-next-unread-group 1))
3491           (gnus-handle-ephemeral-exit quit-config)))
3492       (let ((grpinfo (gnus-get-info group)))
3493         (if (null (gnus-info-read grpinfo))
3494             (gnus-message 3 "Group %s contains no messages"
3495                           (gnus-group-decoded-name group))
3496           (gnus-message 3 "Can't select group")))
3497       nil)
3498      ;; The user did a `C-g' while prompting for number of articles,
3499      ;; so we exit this group.
3500      ((eq did-select 'quit)
3501       (and (eq major-mode 'gnus-summary-mode)
3502            (not (equal (current-buffer) kill-buffer))
3503            (kill-buffer (current-buffer)))
3504       (when kill-buffer
3505         (gnus-kill-or-deaden-summary kill-buffer))
3506       (if (not quit-config)
3507           (progn
3508             (set-buffer gnus-group-buffer)
3509             (gnus-group-jump-to-group group)
3510             (gnus-group-next-unread-group 1)
3511             (gnus-configure-windows 'group 'force))
3512         (gnus-handle-ephemeral-exit quit-config))
3513       ;; Finally signal the quit.
3514       (signal 'quit nil))
3515      ;; The group was successfully selected.
3516      (t
3517       (gnus-set-global-variables)
3518       ;; Save the active value in effect when the group was entered.
3519       (setq gnus-newsgroup-active
3520             (gnus-copy-sequence
3521              (gnus-active gnus-newsgroup-name)))
3522       ;; You can change the summary buffer in some way with this hook.
3523       (gnus-run-hooks 'gnus-select-group-hook)
3524       (gnus-update-format-specifications
3525        nil 'summary 'summary-mode 'summary-dummy)
3526       (gnus-update-summary-mark-positions)
3527       ;; Do score processing.
3528       (when gnus-use-scoring
3529         (gnus-possibly-score-headers))
3530       ;; Check whether to fill in the gaps in the threads.
3531       (when gnus-build-sparse-threads
3532         (gnus-build-sparse-threads))
3533       ;; Find the initial limit.
3534       (if gnus-show-threads
3535           (if show-all
3536               (let ((gnus-newsgroup-dormant nil))
3537                 (gnus-summary-initial-limit show-all))
3538             (gnus-summary-initial-limit show-all))
3539         ;; When unthreaded, all articles are always shown.
3540         (setq gnus-newsgroup-limit
3541               (mapcar
3542                (lambda (header) (mail-header-number header))
3543                gnus-newsgroup-headers)))
3544       ;; Generate the summary buffer.
3545       (unless no-display
3546         (gnus-summary-prepare))
3547       (when gnus-use-trees
3548         (gnus-tree-open group)
3549         (setq gnus-summary-highlight-line-function
3550               'gnus-tree-highlight-article))
3551       ;; If the summary buffer is empty, but there are some low-scored
3552       ;; articles or some excluded dormants, we include these in the
3553       ;; buffer.
3554       (when (and (zerop (buffer-size))
3555                  (not no-display))
3556         (cond (gnus-newsgroup-dormant
3557                (gnus-summary-limit-include-dormant))
3558               ((and gnus-newsgroup-scored show-all)
3559                (gnus-summary-limit-include-expunged t))))
3560       ;; Function `gnus-apply-kill-file' must be called in this hook.
3561       (gnus-run-hooks 'gnus-apply-kill-hook)
3562       (if (and (zerop (buffer-size))
3563                (not no-display))
3564           (progn
3565             ;; This newsgroup is empty.
3566             (gnus-summary-catchup-and-exit nil t)
3567             (gnus-message 6 "No unread news")
3568             (when kill-buffer
3569               (gnus-kill-or-deaden-summary kill-buffer))
3570             ;; Return nil from this function.
3571             nil)
3572         ;; Hide conversation thread subtrees.  We cannot do this in
3573         ;; gnus-summary-prepare-hook since kill processing may not
3574         ;; work with hidden articles.
3575         (gnus-summary-maybe-hide-threads)
3576         (when kill-buffer
3577           (gnus-kill-or-deaden-summary kill-buffer))
3578         (gnus-summary-auto-select-subject)
3579         ;; Show first unread article if requested.
3580         (if (and (not no-article)
3581                  (not no-display)
3582                  gnus-newsgroup-unreads
3583                  gnus-auto-select-first)
3584             (progn
3585               (gnus-configure-windows 'summary)
3586               (let ((art (gnus-summary-article-number)))
3587                 (unless (and (not gnus-plugged)
3588                              (or (memq art gnus-newsgroup-undownloaded)
3589                                  (memq art gnus-newsgroup-downloadable)))
3590                   (gnus-summary-goto-article art))))
3591           ;; Don't select any articles.
3592           (gnus-summary-position-point)
3593           (gnus-configure-windows 'summary 'force)
3594           (gnus-set-mode-line 'summary))
3595         (when (and gnus-auto-center-group
3596                    (get-buffer-window gnus-group-buffer t))
3597           ;; Gotta use windows, because recenter does weird stuff if
3598           ;; the current buffer ain't the displayed window.
3599           (let ((owin (selected-window)))
3600             (select-window (get-buffer-window gnus-group-buffer t))
3601             (when (gnus-group-goto-group group)
3602               (recenter))
3603             (select-window owin)))
3604         ;; Mark this buffer as "prepared".
3605         (setq gnus-newsgroup-prepared t)
3606         (gnus-run-hooks 'gnus-summary-prepared-hook)
3607         (unless (gnus-ephemeral-group-p group)
3608           (gnus-group-update-group group))
3609         t)))))
3610
3611 (defun gnus-summary-auto-select-subject ()
3612   "Select the subject line on initial group entry."
3613   (goto-char (point-min))
3614   (cond
3615    ((eq gnus-auto-select-subject 'best)
3616     (gnus-summary-best-unread-subject))
3617    ((eq gnus-auto-select-subject 'unread)
3618     (gnus-summary-first-unread-subject))
3619    ((eq gnus-auto-select-subject 'unseen)
3620     (gnus-summary-first-unseen-subject))
3621    ((eq gnus-auto-select-subject 'unseen-or-unread)
3622     (gnus-summary-first-unseen-or-unread-subject))
3623    ((eq gnus-auto-select-subject 'first)
3624     ;; Do nothing.
3625     )
3626    ((functionp gnus-auto-select-subject)
3627     (funcall gnus-auto-select-subject))))
3628
3629 (defun gnus-summary-prepare ()
3630   "Generate the summary buffer."
3631   (interactive)
3632   (let ((buffer-read-only nil))
3633     (erase-buffer)
3634     (setq gnus-newsgroup-data nil
3635           gnus-newsgroup-data-reverse nil)
3636     (gnus-run-hooks 'gnus-summary-generate-hook)
3637     ;; Generate the buffer, either with threads or without.
3638     (when gnus-newsgroup-headers
3639       (gnus-summary-prepare-threads
3640        (if gnus-show-threads
3641            (gnus-sort-gathered-threads
3642             (funcall gnus-summary-thread-gathering-function
3643                      (gnus-sort-threads
3644                       (gnus-cut-threads (gnus-make-threads)))))
3645          ;; Unthreaded display.
3646          (gnus-sort-articles gnus-newsgroup-headers))))
3647     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3648     ;; Call hooks for modifying summary buffer.
3649     (goto-char (point-min))
3650     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3651
3652 (defsubst gnus-general-simplify-subject (subject)
3653   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3654   (setq subject
3655         (cond
3656          ;; Truncate the subject.
3657          (gnus-simplify-subject-functions
3658           (gnus-map-function gnus-simplify-subject-functions subject))
3659          ((numberp gnus-summary-gather-subject-limit)
3660           (setq subject (gnus-simplify-subject-re subject))
3661           (if (> (length subject) gnus-summary-gather-subject-limit)
3662               (substring subject 0 gnus-summary-gather-subject-limit)
3663             subject))
3664          ;; Fuzzily simplify it.
3665          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3666           (gnus-simplify-subject-fuzzy subject))
3667          ;; Just remove the leading "Re:".
3668          (t
3669           (gnus-simplify-subject-re subject))))
3670
3671   (if (and gnus-summary-gather-exclude-subject
3672            (string-match gnus-summary-gather-exclude-subject subject))
3673       nil                         ; This article shouldn't be gathered
3674     subject))
3675
3676 (defun gnus-summary-simplify-subject-query ()
3677   "Query where the respool algorithm would put this article."
3678   (interactive)
3679   (gnus-summary-select-article)
3680   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3681
3682 (defun gnus-gather-threads-by-subject (threads)
3683   "Gather threads by looking at Subject headers."
3684   (if (not gnus-summary-make-false-root)
3685       threads
3686     (let ((hashtb (gnus-make-hashtable 1024))
3687           (prev threads)
3688           (result threads)
3689           subject hthread whole-subject)
3690       (while threads
3691         (setq subject (gnus-general-simplify-subject
3692                        (setq whole-subject (mail-header-subject
3693                                             (caar threads)))))
3694         (when subject
3695           (if (setq hthread (gnus-gethash subject hashtb))
3696               (progn
3697                 ;; We enter a dummy root into the thread, if we
3698                 ;; haven't done that already.
3699                 (unless (stringp (caar hthread))
3700                   (setcar hthread (list whole-subject (car hthread))))
3701                 ;; We add this new gathered thread to this gathered
3702                 ;; thread.
3703                 (setcdr (car hthread)
3704                         (nconc (cdar hthread) (list (car threads))))
3705                 ;; Remove it from the list of threads.
3706                 (setcdr prev (cdr threads))
3707                 (setq threads prev))
3708             ;; Enter this thread into the hash table.
3709             (gnus-sethash subject
3710                           (if gnus-summary-make-false-root-always
3711                               (progn
3712                                 ;; If you want a dummy root above all
3713                                 ;; threads...
3714                                 (setcar threads (list whole-subject
3715                                                       (car threads)))
3716                                 threads)
3717                             threads)
3718                           hashtb)))
3719         (setq prev threads)
3720         (setq threads (cdr threads)))
3721       result)))
3722
3723 (defun gnus-gather-threads-by-references (threads)
3724   "Gather threads by looking at References headers."
3725   (let ((idhashtb (gnus-make-hashtable 1024))
3726         (thhashtb (gnus-make-hashtable 1024))
3727         (prev threads)
3728         (result threads)
3729         ids references id gthread gid entered ref)
3730     (while threads
3731       (when (setq references (mail-header-references (caar threads)))
3732         (setq id (mail-header-id (caar threads))
3733               ids (inline (gnus-split-references references))
3734               entered nil)
3735         (while (setq ref (pop ids))
3736           (setq ids (delete ref ids))
3737           (if (not (setq gid (gnus-gethash ref idhashtb)))
3738               (progn
3739                 (gnus-sethash ref id idhashtb)
3740                 (gnus-sethash id threads thhashtb))
3741             (setq gthread (gnus-gethash gid thhashtb))
3742             (unless entered
3743               ;; We enter a dummy root into the thread, if we
3744               ;; haven't done that already.
3745               (unless (stringp (caar gthread))
3746                 (setcar gthread (list (mail-header-subject (caar gthread))
3747                                       (car gthread))))
3748               ;; We add this new gathered thread to this gathered
3749               ;; thread.
3750               (setcdr (car gthread)
3751                       (nconc (cdar gthread) (list (car threads)))))
3752             ;; Add it into the thread hash table.
3753             (gnus-sethash id gthread thhashtb)
3754             (setq entered t)
3755             ;; Remove it from the list of threads.
3756             (setcdr prev (cdr threads))
3757             (setq threads prev))))
3758       (setq prev threads)
3759       (setq threads (cdr threads)))
3760     result))
3761
3762 (defun gnus-sort-gathered-threads (threads)
3763   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3764   (let ((result threads))
3765     (while threads
3766       (when (stringp (caar threads))
3767         (setcdr (car threads)
3768                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3769       (setq threads (cdr threads)))
3770     result))
3771
3772 (defun gnus-thread-loop-p (root thread)
3773   "Say whether ROOT is in THREAD."
3774   (let ((stack (list thread))
3775         (infloop 0)
3776         th)
3777     (while (setq thread (pop stack))
3778       (setq th (cdr thread))
3779       (while (and th
3780                   (not (eq (caar th) root)))
3781         (pop th))
3782       (if th
3783           ;; We have found a loop.
3784           (let (ref-dep)
3785             (setcdr thread (delq (car th) (cdr thread)))
3786             (if (boundp (setq ref-dep (intern "none"
3787                                               gnus-newsgroup-dependencies)))
3788                 (setcdr (symbol-value ref-dep)
3789                         (nconc (cdr (symbol-value ref-dep))
3790                                (list (car th))))
3791               (set ref-dep (list nil (car th))))
3792             (setq infloop 1
3793                   stack nil))
3794         ;; Push all the subthreads onto the stack.
3795         (push (cdr thread) stack)))
3796     infloop))
3797
3798 (defun gnus-make-threads ()
3799   "Go through the dependency hashtb and find the roots.  Return all threads."
3800   (let (threads)
3801     (while (catch 'infloop
3802              (mapatoms
3803               (lambda (refs)
3804                 ;; Deal with self-referencing References loops.
3805                 (when (and (car (symbol-value refs))
3806                            (not (zerop
3807                                  (apply
3808                                   '+
3809                                   (mapcar
3810                                    (lambda (thread)
3811                                      (gnus-thread-loop-p
3812                                       (car (symbol-value refs)) thread))
3813                                    (cdr (symbol-value refs)))))))
3814                   (setq threads nil)
3815                   (throw 'infloop t))
3816                 (unless (car (symbol-value refs))
3817                   ;; These threads do not refer back to any other
3818                   ;; articles, so they're roots.
3819                   (setq threads (append (cdr (symbol-value refs)) threads))))
3820               gnus-newsgroup-dependencies)))
3821     threads))
3822
3823 ;; Build the thread tree.
3824 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3825   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3826
3827 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3828 if it was already present.
3829
3830 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3831 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3832 Message-IDs will be renamed to a unique Message-ID before being
3833 entered.
3834
3835 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3836   (let* ((id (mail-header-id header))
3837          (id-dep (and id (intern id dependencies)))
3838          parent-id ref ref-dep ref-header replaced)
3839     ;; Enter this `header' in the `dependencies' table.
3840     (cond
3841      ((not id-dep)
3842       (setq header nil))
3843      ;; The first two cases do the normal part: enter a new `header'
3844      ;; in the `dependencies' table.
3845      ((not (boundp id-dep))
3846       (set id-dep (list header)))
3847      ((null (car (symbol-value id-dep)))
3848       (setcar (symbol-value id-dep) header))
3849
3850      ;; From here the `header' was already present in the
3851      ;; `dependencies' table.
3852      (force-new
3853       ;; Overrides an existing entry;
3854       ;; just set the header part of the entry.
3855       (setcar (symbol-value id-dep) header)
3856       (setq replaced t))
3857
3858      ;; Renames the existing `header' to a unique Message-ID.
3859      ((not gnus-summary-ignore-duplicates)
3860       ;; An article with this Message-ID has already been seen.
3861       ;; We rename the Message-ID.
3862       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3863            (list header))
3864       (mail-header-set-id header id))
3865
3866      ;; The last case ignores an existing entry, except it adds any
3867      ;; additional Xrefs (in case the two articles came from different
3868      ;; servers.
3869      ;; Also sets `header' to `nil' meaning that the `dependencies'
3870      ;; table was *not* modified.
3871      (t
3872       (mail-header-set-xref
3873        (car (symbol-value id-dep))
3874        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3875                    "")
3876                (or (mail-header-xref header) "")))
3877       (setq header nil)))
3878
3879     (when (and header (not replaced))
3880       ;; First check that we are not creating a References loop.
3881       (setq parent-id (gnus-parent-id (mail-header-references header)))
3882       (setq ref parent-id)
3883       (while (and ref
3884                   (setq ref-dep (intern-soft ref dependencies))
3885                   (boundp ref-dep)
3886                   (setq ref-header (car (symbol-value ref-dep))))
3887         (if (string= id ref)
3888             ;; Yuk!  This is a reference loop.  Make the article be a
3889             ;; root article.
3890             (progn
3891               (mail-header-set-references (car (symbol-value id-dep)) "none")
3892               (setq ref nil)
3893               (setq parent-id nil))
3894           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3895       (setq ref-dep (intern (or parent-id "none") dependencies))
3896       (if (boundp ref-dep)
3897           (setcdr (symbol-value ref-dep)
3898                   (nconc (cdr (symbol-value ref-dep))
3899                          (list (symbol-value id-dep))))
3900         (set ref-dep (list nil (symbol-value id-dep)))))
3901     header))
3902
3903 (defun gnus-extract-message-id-from-in-reply-to (string)
3904   (if (string-match "<[^>]+>" string)
3905       (substring string (match-beginning 0) (match-end 0))
3906     nil))
3907
3908 (defun gnus-build-sparse-threads ()
3909   (let ((headers gnus-newsgroup-headers)
3910         (mail-parse-charset gnus-newsgroup-charset)
3911         (gnus-summary-ignore-duplicates t)
3912         header references generation relations
3913         subject child end new-child date)
3914     ;; First we create an alist of generations/relations, where
3915     ;; generations is how much we trust the relation, and the relation
3916     ;; is parent/child.
3917     (gnus-message 7 "Making sparse threads...")
3918     (save-excursion
3919       (nnheader-set-temp-buffer " *gnus sparse threads*")
3920       (while (setq header (pop headers))
3921         (when (and (setq references (mail-header-references header))
3922                    (not (string= references "")))
3923           (insert references)
3924           (setq child (mail-header-id header)
3925                 subject (mail-header-subject header)
3926                 date (mail-header-date header)
3927                 generation 0)
3928           (while (search-backward ">" nil t)
3929             (setq end (1+ (point)))
3930             (when (search-backward "<" nil t)
3931               (setq new-child (buffer-substring (point) end))
3932               (push (list (incf generation)
3933                           child (setq child new-child)
3934                           subject date)
3935                     relations)))
3936           (when child
3937             (push (list (1+ generation) child nil subject) relations))
3938           (erase-buffer)))
3939       (kill-buffer (current-buffer)))
3940     ;; Sort over trustworthiness.
3941     (mapcar
3942      (lambda (relation)
3943        (when (gnus-dependencies-add-header
3944               (make-full-mail-header
3945                gnus-reffed-article-number
3946                (nth 3 relation) "" (or (nth 4 relation) "")
3947                (nth 1 relation)
3948                (or (nth 2 relation) "") 0 0 "")
3949               gnus-newsgroup-dependencies nil)
3950          (push gnus-reffed-article-number gnus-newsgroup-limit)
3951          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3952          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3953                gnus-newsgroup-reads)
3954          (decf gnus-reffed-article-number)))
3955      (sort relations 'car-less-than-car))
3956     (gnus-message 7 "Making sparse threads...done")))
3957
3958 (defun gnus-build-old-threads ()
3959   ;; Look at all the articles that refer back to old articles, and
3960   ;; fetch the headers for the articles that aren't there.  This will
3961   ;; build complete threads - if the roots haven't been expired by the
3962   ;; server, that is.
3963   (let ((mail-parse-charset gnus-newsgroup-charset)
3964         id heads)
3965     (mapatoms
3966      (lambda (refs)
3967        (when (not (car (symbol-value refs)))
3968          (setq heads (cdr (symbol-value refs)))
3969          (while heads
3970            (if (memq (mail-header-number (caar heads))
3971                      gnus-newsgroup-dormant)
3972                (setq heads (cdr heads))
3973              (setq id (symbol-name refs))
3974              (while (and (setq id (gnus-build-get-header id))
3975                          (not (car (gnus-id-to-thread id)))))
3976              (setq heads nil)))))
3977      gnus-newsgroup-dependencies)))
3978
3979 (defsubst gnus-remove-odd-characters (string)
3980   "Translate STRING into something that doesn't contain weird characters."
3981   (mm-subst-char-in-string
3982    ?\r ?\-
3983    (mm-subst-char-in-string
3984     ?\n ?\- string)))
3985
3986 ;; This function has to be called with point after the article number
3987 ;; on the beginning of the line.
3988 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3989   (let ((eol (gnus-point-at-eol))
3990         (buffer (current-buffer))
3991         header references in-reply-to)
3992
3993     ;; overview: [num subject from date id refs chars lines misc]
3994     (unwind-protect
3995         (let (x)
3996           (narrow-to-region (point) eol)
3997           (unless (eobp)
3998             (forward-char))
3999
4000           (setq header
4001                 (make-full-mail-header
4002                  number                 ; number
4003                  (condition-case ()     ; subject
4004                      (gnus-remove-odd-characters
4005                       (funcall gnus-decode-encoded-word-function
4006                                (setq x (nnheader-nov-field))))
4007                    (error x))
4008                  (condition-case ()     ; from
4009                      (gnus-remove-odd-characters
4010                       (funcall gnus-decode-encoded-word-function
4011                                (setq x (nnheader-nov-field))))
4012                    (error x))
4013                  (nnheader-nov-field)   ; date
4014                  (nnheader-nov-read-message-id) ; id
4015                  (setq references (nnheader-nov-field)) ; refs
4016                  (nnheader-nov-read-integer) ; chars
4017                  (nnheader-nov-read-integer) ; lines
4018                  (unless (eobp)
4019                    (if (looking-at "Xref: ")
4020                        (goto-char (match-end 0)))
4021                    (nnheader-nov-field)) ; Xref
4022                  (nnheader-nov-parse-extra)))) ; extra
4023
4024       (widen))
4025
4026     (when (and (string= references "")
4027                (setq in-reply-to (mail-header-extra header))
4028                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4029       (mail-header-set-references
4030        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4031
4032     (when gnus-alter-header-function
4033       (funcall gnus-alter-header-function header))
4034     (gnus-dependencies-add-header header dependencies force-new)))
4035
4036 (defun gnus-build-get-header (id)
4037   "Look through the buffer of NOV lines and find the header to ID.
4038 Enter this line into the dependencies hash table, and return
4039 the id of the parent article (if any)."
4040   (let ((deps gnus-newsgroup-dependencies)
4041         found header)
4042     (prog1
4043         (save-excursion
4044           (set-buffer nntp-server-buffer)
4045           (let ((case-fold-search nil))
4046             (goto-char (point-min))
4047             (while (and (not found)
4048                         (search-forward id nil t))
4049               (beginning-of-line)
4050               (setq found (looking-at
4051                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4052                                    (regexp-quote id))))
4053               (or found (beginning-of-line 2)))
4054             (when found
4055               (beginning-of-line)
4056               (and
4057                (setq header (gnus-nov-parse-line
4058                              (read (current-buffer)) deps))
4059                (gnus-parent-id (mail-header-references header))))))
4060       (when header
4061         (let ((number (mail-header-number header)))
4062           (push number gnus-newsgroup-limit)
4063           (push header gnus-newsgroup-headers)
4064           (if (memq number gnus-newsgroup-unselected)
4065               (progn
4066                 (setq gnus-newsgroup-unreads
4067                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4068                                                number))
4069                 (setq gnus-newsgroup-unselected
4070                       (delq number gnus-newsgroup-unselected)))
4071             (push number gnus-newsgroup-ancient)))))))
4072
4073 (defun gnus-build-all-threads ()
4074   "Read all the headers."
4075   (let ((gnus-summary-ignore-duplicates t)
4076         (mail-parse-charset gnus-newsgroup-charset)
4077         (dependencies gnus-newsgroup-dependencies)
4078         header article)
4079     (save-excursion
4080       (set-buffer nntp-server-buffer)
4081       (let ((case-fold-search nil))
4082         (goto-char (point-min))
4083         (while (not (eobp))
4084           (ignore-errors
4085             (setq article (read (current-buffer))
4086                   header (gnus-nov-parse-line article dependencies)))
4087           (when header
4088             (save-excursion
4089               (set-buffer gnus-summary-buffer)
4090               (push header gnus-newsgroup-headers)
4091               (if (memq (setq article (mail-header-number header))
4092                         gnus-newsgroup-unselected)
4093                   (progn
4094                     (setq gnus-newsgroup-unreads
4095                           (gnus-add-to-sorted-list
4096                            gnus-newsgroup-unreads article))
4097                     (setq gnus-newsgroup-unselected
4098                           (delq article gnus-newsgroup-unselected)))
4099                 (push article gnus-newsgroup-ancient)))
4100             (forward-line 1)))))))
4101
4102 (defun gnus-summary-update-article-line (article header)
4103   "Update the line for ARTICLE using HEADER."
4104   (let* ((id (mail-header-id header))
4105          (thread (gnus-id-to-thread id)))
4106     (unless thread
4107       (error "Article in no thread"))
4108     ;; Update the thread.
4109     (setcar thread header)
4110     (gnus-summary-goto-subject article)
4111     (let* ((datal (gnus-data-find-list article))
4112            (data (car datal))
4113            (buffer-read-only nil)
4114            (level (gnus-summary-thread-level)))
4115       (gnus-delete-line)
4116       (let ((inserted (- (point)
4117                          (progn
4118                            (gnus-summary-insert-line
4119                             header level nil
4120                             (memq article gnus-newsgroup-undownloaded)
4121                             (gnus-article-mark article)
4122                             (memq article gnus-newsgroup-replied)
4123                             (memq article gnus-newsgroup-expirable)
4124                             ;; Only insert the Subject string when it's different
4125                             ;; from the previous Subject string.
4126                             (if (and
4127                                  gnus-show-threads
4128                                  (gnus-subject-equal
4129                                   (condition-case ()
4130                                       (mail-header-subject
4131                                        (gnus-data-header
4132                                         (cadr
4133                                          (gnus-data-find-list
4134                                           article
4135                                           (gnus-data-list t)))))
4136                                     ;; Error on the side of excessive subjects.
4137                                     (error ""))
4138                                   (mail-header-subject header)))
4139                                 ""
4140                               (mail-header-subject header))
4141                             nil (cdr (assq article gnus-newsgroup-scored))
4142                             (memq article gnus-newsgroup-processable))
4143                            (point)))))
4144         (when (cdr datal)
4145           (gnus-data-update-list
4146            (cdr datal)
4147            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4148
4149 (defun gnus-summary-update-article (article &optional iheader)
4150   "Update ARTICLE in the summary buffer."
4151   (set-buffer gnus-summary-buffer)
4152   (let* ((header (gnus-summary-article-header article))
4153          (id (mail-header-id header))
4154          (data (gnus-data-find article))
4155          (thread (gnus-id-to-thread id))
4156          (references (mail-header-references header))
4157          (parent
4158           (gnus-id-to-thread
4159            (or (gnus-parent-id
4160                 (when (and references
4161                            (not (equal "" references)))
4162                   references))
4163                "none")))
4164          (buffer-read-only nil)
4165          (old (car thread)))
4166     (when thread
4167       (unless iheader
4168         (setcar thread nil)
4169         (when parent
4170           (delq thread parent)))
4171       (if (gnus-summary-insert-subject id header)
4172           ;; Set the (possibly) new article number in the data structure.
4173           (gnus-data-set-number data (gnus-id-to-article id))
4174         (setcar thread old)
4175         nil))))
4176
4177 (defun gnus-rebuild-thread (id &optional line)
4178   "Rebuild the thread containing ID.
4179 If LINE, insert the rebuilt thread starting on line LINE."
4180   (let ((buffer-read-only nil)
4181         old-pos current thread data)
4182     (if (not gnus-show-threads)
4183         (setq thread (list (car (gnus-id-to-thread id))))
4184       ;; Get the thread this article is part of.
4185       (setq thread (gnus-remove-thread id)))
4186     (setq old-pos (gnus-point-at-bol))
4187     (setq current (save-excursion
4188                     (and (re-search-backward "[\r\n]" nil t)
4189                          (gnus-summary-article-number))))
4190     ;; If this is a gathered thread, we have to go some re-gathering.
4191     (when (stringp (car thread))
4192       (let ((subject (car thread))
4193             roots thr)
4194         (setq thread (cdr thread))
4195         (while thread
4196           (unless (memq (setq thr (gnus-id-to-thread
4197                                    (gnus-root-id
4198                                     (mail-header-id (caar thread)))))
4199                         roots)
4200             (push thr roots))
4201           (setq thread (cdr thread)))
4202         ;; We now have all (unique) roots.
4203         (if (= (length roots) 1)
4204             ;; All the loose roots are now one solid root.
4205             (setq thread (car roots))
4206           (setq thread (cons subject (gnus-sort-threads roots))))))
4207     (let (threads)
4208       ;; We then insert this thread into the summary buffer.
4209       (when line
4210         (goto-char (point-min))
4211         (forward-line (1- line)))
4212       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4213         (if gnus-show-threads
4214             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4215           (gnus-summary-prepare-unthreaded thread))
4216         (setq data (nreverse gnus-newsgroup-data))
4217         (setq threads gnus-newsgroup-threads))
4218       ;; We splice the new data into the data structure.
4219       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4220       ;;!!! then we want to insert at the beginning of the buffer.
4221       ;;!!! That happens to be true with Gnus now, but that may
4222       ;;!!! change in the future.  Perhaps.
4223       (gnus-data-enter-list
4224        (if line nil current) data (- (point) old-pos))
4225       (setq gnus-newsgroup-threads
4226             (nconc threads gnus-newsgroup-threads))
4227       (gnus-data-compute-positions))))
4228
4229 (defun gnus-number-to-header (number)
4230   "Return the header for article NUMBER."
4231   (let ((headers gnus-newsgroup-headers))
4232     (while (and headers
4233                 (not (= number (mail-header-number (car headers)))))
4234       (pop headers))
4235     (when headers
4236       (car headers))))
4237
4238 (defun gnus-parent-headers (in-headers &optional generation)
4239   "Return the headers of the GENERATIONeth parent of HEADERS."
4240   (unless generation
4241     (setq generation 1))
4242   (let ((parent t)
4243         (headers in-headers)
4244         references)
4245     (while (and parent
4246                 (not (zerop generation))
4247                 (setq references (mail-header-references headers)))
4248       (setq headers (if (and references
4249                              (setq parent (gnus-parent-id references)))
4250                         (car (gnus-id-to-thread parent))
4251                       nil))
4252       (decf generation))
4253     (and (not (eq headers in-headers))
4254          headers)))
4255
4256 (defun gnus-id-to-thread (id)
4257   "Return the (sub-)thread where ID appears."
4258   (gnus-gethash id gnus-newsgroup-dependencies))
4259
4260 (defun gnus-id-to-article (id)
4261   "Return the article number of ID."
4262   (let ((thread (gnus-id-to-thread id)))
4263     (when (and thread
4264                (car thread))
4265       (mail-header-number (car thread)))))
4266
4267 (defun gnus-id-to-header (id)
4268   "Return the article headers of ID."
4269   (car (gnus-id-to-thread id)))
4270
4271 (defun gnus-article-displayed-root-p (article)
4272   "Say whether ARTICLE is a root(ish) article."
4273   (let ((level (gnus-summary-thread-level article))
4274         (refs (mail-header-references  (gnus-summary-article-header article)))
4275         particle)
4276     (cond
4277      ((null level) nil)
4278      ((zerop level) t)
4279      ((null refs) t)
4280      ((null (gnus-parent-id refs)) t)
4281      ((and (= 1 level)
4282            (null (setq particle (gnus-id-to-article
4283                                  (gnus-parent-id refs))))
4284            (null (gnus-summary-thread-level particle)))))))
4285
4286 (defun gnus-root-id (id)
4287   "Return the id of the root of the thread where ID appears."
4288   (let (last-id prev)
4289     (while (and id (setq prev (car (gnus-id-to-thread id))))
4290       (setq last-id id
4291             id (gnus-parent-id (mail-header-references prev))))
4292     last-id))
4293
4294 (defun gnus-articles-in-thread (thread)
4295   "Return the list of articles in THREAD."
4296   (cons (mail-header-number (car thread))
4297         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4298
4299 (defun gnus-remove-thread (id &optional dont-remove)
4300   "Remove the thread that has ID in it."
4301   (let (headers thread last-id)
4302     ;; First go up in this thread until we find the root.
4303     (setq last-id (gnus-root-id id)
4304           headers (message-flatten-list (gnus-id-to-thread last-id)))
4305     ;; We have now found the real root of this thread.  It might have
4306     ;; been gathered into some loose thread, so we have to search
4307     ;; through the threads to find the thread we wanted.
4308     (let ((threads gnus-newsgroup-threads)
4309           sub)
4310       (while threads
4311         (setq sub (car threads))
4312         (if (stringp (car sub))
4313             ;; This is a gathered thread, so we look at the roots
4314             ;; below it to find whether this article is in this
4315             ;; gathered root.
4316             (progn
4317               (setq sub (cdr sub))
4318               (while sub
4319                 (when (member (caar sub) headers)
4320                   (setq thread (car threads)
4321                         threads nil
4322                         sub nil))
4323                 (setq sub (cdr sub))))
4324           ;; It's an ordinary thread, so we check it.
4325           (when (eq (car sub) (car headers))
4326             (setq thread sub
4327                   threads nil)))
4328         (setq threads (cdr threads)))
4329       ;; If this article is in no thread, then it's a root.
4330       (if thread
4331           (unless dont-remove
4332             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4333         (setq thread (gnus-id-to-thread last-id)))
4334       (when thread
4335         (prog1
4336             thread                      ; We return this thread.
4337           (unless dont-remove
4338             (if (stringp (car thread))
4339                 (progn
4340                   ;; If we use dummy roots, then we have to remove the
4341                   ;; dummy root as well.
4342                   (when (eq gnus-summary-make-false-root 'dummy)
4343                     ;; We go to the dummy root by going to
4344                     ;; the first sub-"thread", and then one line up.
4345                     (gnus-summary-goto-article
4346                      (mail-header-number (caadr thread)))
4347                     (forward-line -1)
4348                     (gnus-delete-line)
4349                     (gnus-data-compute-positions))
4350                   (setq thread (cdr thread))
4351                   (while thread
4352                     (gnus-remove-thread-1 (car thread))
4353                     (setq thread (cdr thread))))
4354               (gnus-remove-thread-1 thread))))))))
4355
4356 (defun gnus-remove-thread-1 (thread)
4357   "Remove the thread THREAD recursively."
4358   (let ((number (mail-header-number (pop thread)))
4359         d)
4360     (setq thread (reverse thread))
4361     (while thread
4362       (gnus-remove-thread-1 (pop thread)))
4363     (when (setq d (gnus-data-find number))
4364       (goto-char (gnus-data-pos d))
4365       (gnus-summary-show-thread)
4366       (gnus-data-remove
4367        number
4368        (- (gnus-point-at-bol)
4369           (prog1
4370               (1+ (gnus-point-at-eol))
4371             (gnus-delete-line)))))))
4372
4373 (defun gnus-sort-threads-1 (threads func)
4374   (sort (mapcar (lambda (thread)
4375                   (cons (car thread)
4376                         (and (cdr thread)
4377                              (gnus-sort-threads-1 (cdr thread) func))))
4378                 threads) func))
4379
4380 (defun gnus-sort-threads (threads)
4381   "Sort THREADS."
4382   (if (not gnus-thread-sort-functions)
4383       threads
4384     (gnus-message 8 "Sorting threads...")
4385     (let ((max-lisp-eval-depth 5000))
4386       (prog1 (gnus-sort-threads-1
4387          threads
4388          (gnus-make-sort-function gnus-thread-sort-functions))
4389         (gnus-message 8 "Sorting threads...done")))))
4390
4391 (defun gnus-sort-articles (articles)
4392   "Sort ARTICLES."
4393   (when gnus-article-sort-functions
4394     (gnus-message 7 "Sorting articles...")
4395     (prog1
4396         (setq gnus-newsgroup-headers
4397               (sort articles (gnus-make-sort-function
4398                               gnus-article-sort-functions)))
4399       (gnus-message 7 "Sorting articles...done"))))
4400
4401 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4402 (defmacro gnus-thread-header (thread)
4403   "Return header of first article in THREAD.
4404 Note that THREAD must never, ever be anything else than a variable -
4405 using some other form will lead to serious barfage."
4406   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4407   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4408   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4409         (vector thread) 2))
4410
4411 (defsubst gnus-article-sort-by-number (h1 h2)
4412   "Sort articles by article number."
4413   (< (mail-header-number h1)
4414      (mail-header-number h2)))
4415
4416 (defun gnus-thread-sort-by-number (h1 h2)
4417   "Sort threads by root article number."
4418   (gnus-article-sort-by-number
4419    (gnus-thread-header h1) (gnus-thread-header h2)))
4420
4421 (defsubst gnus-article-sort-by-random (h1 h2)
4422   "Sort articles by article number."
4423   (zerop (random 2)))
4424
4425 (defun gnus-thread-sort-by-random (h1 h2)
4426   "Sort threads by root article number."
4427   (gnus-article-sort-by-random
4428    (gnus-thread-header h1) (gnus-thread-header h2)))
4429
4430 (defsubst gnus-article-sort-by-lines (h1 h2)
4431   "Sort articles by article Lines header."
4432   (< (mail-header-lines h1)
4433      (mail-header-lines h2)))
4434
4435 (defun gnus-thread-sort-by-lines (h1 h2)
4436   "Sort threads by root article Lines header."
4437   (gnus-article-sort-by-lines
4438    (gnus-thread-header h1) (gnus-thread-header h2)))
4439
4440 (defsubst gnus-article-sort-by-chars (h1 h2)
4441   "Sort articles by octet length."
4442   (< (mail-header-chars h1)
4443      (mail-header-chars h2)))
4444
4445 (defun gnus-thread-sort-by-chars (h1 h2)
4446   "Sort threads by root article octet length."
4447   (gnus-article-sort-by-chars
4448    (gnus-thread-header h1) (gnus-thread-header h2)))
4449
4450 (defsubst gnus-article-sort-by-author (h1 h2)
4451   "Sort articles by root author."
4452   (string-lessp
4453    (let ((extract (funcall
4454                    gnus-extract-address-components
4455                    (mail-header-from h1))))
4456      (or (car extract) (cadr extract) ""))
4457    (let ((extract (funcall
4458                    gnus-extract-address-components
4459                    (mail-header-from h2))))
4460      (or (car extract) (cadr extract) ""))))
4461
4462 (defun gnus-thread-sort-by-author (h1 h2)
4463   "Sort threads by root author."
4464   (gnus-article-sort-by-author
4465    (gnus-thread-header h1)  (gnus-thread-header h2)))
4466
4467 (defsubst gnus-article-sort-by-subject (h1 h2)
4468   "Sort articles by root subject."
4469   (string-lessp
4470    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4471    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4472
4473 (defun gnus-thread-sort-by-subject (h1 h2)
4474   "Sort threads by root subject."
4475   (gnus-article-sort-by-subject
4476    (gnus-thread-header h1) (gnus-thread-header h2)))
4477
4478 (defsubst gnus-article-sort-by-date (h1 h2)
4479   "Sort articles by root article date."
4480   (time-less-p
4481    (gnus-date-get-time (mail-header-date h1))
4482    (gnus-date-get-time (mail-header-date h2))))
4483
4484 (defun gnus-thread-sort-by-date (h1 h2)
4485   "Sort threads by root article date."
4486   (gnus-article-sort-by-date
4487    (gnus-thread-header h1) (gnus-thread-header h2)))
4488
4489 (defsubst gnus-article-sort-by-score (h1 h2)
4490   "Sort articles by root article score.
4491 Unscored articles will be counted as having a score of zero."
4492   (> (or (cdr (assq (mail-header-number h1)
4493                     gnus-newsgroup-scored))
4494          gnus-summary-default-score 0)
4495      (or (cdr (assq (mail-header-number h2)
4496                     gnus-newsgroup-scored))
4497          gnus-summary-default-score 0)))
4498
4499 (defun gnus-thread-sort-by-score (h1 h2)
4500   "Sort threads by root article score."
4501   (gnus-article-sort-by-score
4502    (gnus-thread-header h1) (gnus-thread-header h2)))
4503
4504 (defun gnus-thread-sort-by-total-score (h1 h2)
4505   "Sort threads by the sum of all scores in the thread.
4506 Unscored articles will be counted as having a score of zero."
4507   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4508
4509 (defun gnus-thread-total-score (thread)
4510   ;; This function find the total score of THREAD.
4511   (cond
4512    ((null thread)
4513     0)
4514    ((consp thread)
4515     (if (stringp (car thread))
4516         (apply gnus-thread-score-function 0
4517                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4518       (gnus-thread-total-score-1 thread)))
4519    (t
4520     (gnus-thread-total-score-1 (list thread)))))
4521
4522 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4523   "Sort threads such that the thread with the most recently arrived article comes first."
4524   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4525
4526 (defun gnus-thread-highest-number (thread)
4527   "Return the highest article number in THREAD."
4528   (apply 'max (mapcar (lambda (header)
4529                         (mail-header-number header))
4530                       (message-flatten-list thread))))
4531
4532 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4533   "Sort threads such that the thread with the most recently dated article comes first."
4534   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4535
4536 (defun gnus-thread-latest-date (thread)
4537   "Return the highest article date in THREAD."
4538   (let ((previous-time 0))
4539     (apply 'max
4540            (mapcar
4541             (lambda (header)
4542               (setq previous-time
4543                     (condition-case ()
4544                         (time-to-seconds (mail-header-parse-date
4545                                           (mail-header-date header)))
4546                       (error previous-time))))
4547             (sort
4548              (message-flatten-list thread)
4549              (lambda (h1 h2)
4550                (< (mail-header-number h1)
4551                   (mail-header-number h2))))))))
4552
4553 (defun gnus-thread-total-score-1 (root)
4554   ;; This function find the total score of the thread below ROOT.
4555   (setq root (car root))
4556   (apply gnus-thread-score-function
4557          (or (append
4558               (mapcar 'gnus-thread-total-score
4559                       (cdr (gnus-id-to-thread (mail-header-id root))))
4560               (when (> (mail-header-number root) 0)
4561                 (list (or (cdr (assq (mail-header-number root)
4562                                      gnus-newsgroup-scored))
4563                           gnus-summary-default-score 0))))
4564              (list gnus-summary-default-score)
4565              '(0))))
4566
4567 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4568 (defvar gnus-tmp-prev-subject nil)
4569 (defvar gnus-tmp-false-parent nil)
4570 (defvar gnus-tmp-root-expunged nil)
4571 (defvar gnus-tmp-dummy-line nil)
4572
4573 (eval-when-compile (defvar gnus-tmp-header))
4574 (defun gnus-extra-header (type &optional header)
4575   "Return the extra header of TYPE."
4576   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4577       ""))
4578
4579 (defvar gnus-tmp-thread-tree-header-string "")
4580
4581 (defcustom gnus-sum-thread-tree-root "> "
4582   "With %B spec, used for the root of a thread.
4583 If nil, use subject instead."
4584   :type '(radio (const :format "%v  " nil) (string :size 0))
4585   :group 'gnus-thread)
4586 (defcustom gnus-sum-thread-tree-false-root "> "
4587   "With %B spec, used for a false root of a thread.
4588 If nil, use subject instead."
4589   :type '(radio (const :format "%v  " nil) (string :size 0))
4590   :group 'gnus-thread)
4591 (defcustom gnus-sum-thread-tree-single-indent ""
4592   "With %B spec, used for a thread with just one message.
4593 If nil, use subject instead."
4594   :type '(radio (const :format "%v  " nil) (string :size 0))
4595   :group 'gnus-thread)
4596 (defcustom gnus-sum-thread-tree-vertical "| "
4597   "With %B spec, used for drawing a vertical line."
4598   :type 'string
4599   :group 'gnus-thread)
4600 (defcustom gnus-sum-thread-tree-indent "  "
4601   "With %B spec, used for indenting."
4602   :type 'string
4603   :group 'gnus-thread)
4604 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4605   "With %B spec, used for a leaf with brothers."
4606   :type 'string
4607   :group 'gnus-thread)
4608 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4609   "With %B spec, used for a leaf without brothers."
4610   :type 'string
4611   :group 'gnus-thread)
4612
4613 (defun gnus-summary-prepare-threads (threads)
4614   "Prepare summary buffer from THREADS and indentation LEVEL.
4615 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4616 or a straight list of headers."
4617   (gnus-message 7 "Generating summary...")
4618
4619   (setq gnus-newsgroup-threads threads)
4620   (beginning-of-line)
4621
4622   (let ((gnus-tmp-level 0)
4623         (default-score (or gnus-summary-default-score 0))
4624         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4625         (building-line-count gnus-summary-display-while-building)
4626         (building-count (integerp gnus-summary-display-while-building))
4627         thread number subject stack state gnus-tmp-gathered beg-match
4628         new-roots gnus-tmp-new-adopts thread-end simp-subject
4629         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4630         gnus-tmp-replied gnus-tmp-subject-or-nil
4631         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4632         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4633         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4634         tree-stack)
4635
4636     (setq gnus-tmp-prev-subject nil
4637           gnus-tmp-thread-tree-header-string "")
4638
4639     (if (vectorp (car threads))
4640         ;; If this is a straight (sic) list of headers, then a
4641         ;; threaded summary display isn't required, so we just create
4642         ;; an unthreaded one.
4643         (gnus-summary-prepare-unthreaded threads)
4644
4645       ;; Do the threaded display.
4646
4647       (if gnus-summary-display-while-building
4648           (switch-to-buffer (buffer-name)))
4649       (while (or threads stack gnus-tmp-new-adopts new-roots)
4650
4651         (if (and (= gnus-tmp-level 0)
4652                  (or (not stack)
4653                      (= (caar stack) 0))
4654                  (not gnus-tmp-false-parent)
4655                  (or gnus-tmp-new-adopts new-roots))
4656             (if gnus-tmp-new-adopts
4657                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4658                       thread (list (car gnus-tmp-new-adopts))
4659                       gnus-tmp-header (caar thread)
4660                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4661               (when new-roots
4662                 (setq thread (list (car new-roots))
4663                       gnus-tmp-header (caar thread)
4664                       new-roots (cdr new-roots))))
4665
4666           (if threads
4667               ;; If there are some threads, we do them before the
4668               ;; threads on the stack.
4669               (setq thread threads
4670                     gnus-tmp-header (caar thread))
4671             ;; There were no current threads, so we pop something off
4672             ;; the stack.
4673             (setq state (car stack)
4674                   gnus-tmp-level (car state)
4675                   tree-stack (cadr state)
4676                   thread (caddr state)
4677                   stack (cdr stack)
4678                   gnus-tmp-header (caar thread))))
4679
4680         (setq gnus-tmp-false-parent nil)
4681         (setq gnus-tmp-root-expunged nil)
4682         (setq thread-end nil)
4683
4684         (if (stringp gnus-tmp-header)
4685             ;; The header is a dummy root.
4686             (cond
4687              ((eq gnus-summary-make-false-root 'adopt)
4688               ;; We let the first article adopt the rest.
4689               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4690                                                (cddar thread)))
4691               (setq gnus-tmp-gathered
4692                     (nconc (mapcar
4693                             (lambda (h) (mail-header-number (car h)))
4694                             (cddar thread))
4695                            gnus-tmp-gathered))
4696               (setq thread (cons (list (caar thread)
4697                                        (cadar thread))
4698                                  (cdr thread)))
4699               (setq gnus-tmp-level -1
4700                     gnus-tmp-false-parent t))
4701              ((eq gnus-summary-make-false-root 'empty)
4702               ;; We print adopted articles with empty subject fields.
4703               (setq gnus-tmp-gathered
4704                     (nconc (mapcar
4705                             (lambda (h) (mail-header-number (car h)))
4706                             (cddar thread))
4707                            gnus-tmp-gathered))
4708               (setq gnus-tmp-level -1))
4709              ((eq gnus-summary-make-false-root 'dummy)
4710               ;; We remember that we probably want to output a dummy
4711               ;; root.
4712               (setq gnus-tmp-dummy-line gnus-tmp-header)
4713               (setq gnus-tmp-prev-subject gnus-tmp-header))
4714              (t
4715               ;; We do not make a root for the gathered
4716               ;; sub-threads at all.
4717               (setq gnus-tmp-level -1)))
4718
4719           (setq number (mail-header-number gnus-tmp-header)
4720                 subject (mail-header-subject gnus-tmp-header)
4721                 simp-subject (gnus-simplify-subject-fully subject))
4722
4723           (cond
4724            ;; If the thread has changed subject, we might want to make
4725            ;; this subthread into a root.
4726            ((and (null gnus-thread-ignore-subject)
4727                  (not (zerop gnus-tmp-level))
4728                  gnus-tmp-prev-subject
4729                  (not (string= gnus-tmp-prev-subject simp-subject)))
4730             (setq new-roots (nconc new-roots (list (car thread)))
4731                   thread-end t
4732                   gnus-tmp-header nil))
4733            ;; If the article lies outside the current limit,
4734            ;; then we do not display it.
4735            ((not (memq number gnus-newsgroup-limit))
4736             (setq gnus-tmp-gathered
4737                   (nconc (mapcar
4738                           (lambda (h) (mail-header-number (car h)))
4739                           (cdar thread))
4740                          gnus-tmp-gathered))
4741             (setq gnus-tmp-new-adopts (if (cdar thread)
4742                                           (append gnus-tmp-new-adopts
4743                                                   (cdar thread))
4744                                         gnus-tmp-new-adopts)
4745                   thread-end t
4746                   gnus-tmp-header nil)
4747             (when (zerop gnus-tmp-level)
4748               (setq gnus-tmp-root-expunged t)))
4749            ;; Perhaps this article is to be marked as read?
4750            ((and gnus-summary-mark-below
4751                  (< (or (cdr (assq number gnus-newsgroup-scored))
4752                         default-score)
4753                     gnus-summary-mark-below)
4754                  ;; Don't touch sparse articles.
4755                  (not (gnus-summary-article-sparse-p number))
4756                  (not (gnus-summary-article-ancient-p number)))
4757             (setq gnus-newsgroup-unreads
4758                   (delq number gnus-newsgroup-unreads))
4759             (if gnus-newsgroup-auto-expire
4760                 (setq gnus-newsgroup-expirable
4761                       (gnus-add-to-sorted-list
4762                        gnus-newsgroup-expirable number))
4763               (push (cons number gnus-low-score-mark)
4764                     gnus-newsgroup-reads))))
4765
4766           (when gnus-tmp-header
4767             ;; We may have an old dummy line to output before this
4768             ;; article.
4769             (when (and gnus-tmp-dummy-line
4770                        (gnus-subject-equal
4771                         gnus-tmp-dummy-line
4772                         (mail-header-subject gnus-tmp-header)))
4773               (gnus-summary-insert-dummy-line
4774                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4775               (setq gnus-tmp-dummy-line nil))
4776
4777             ;; Compute the mark.
4778             (setq gnus-tmp-unread (gnus-article-mark number))
4779
4780             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4781                                   gnus-tmp-header gnus-tmp-level)
4782                   gnus-newsgroup-data)
4783
4784             ;; Actually insert the line.
4785             (setq
4786              gnus-tmp-subject-or-nil
4787              (cond
4788               ((and gnus-thread-ignore-subject
4789                     gnus-tmp-prev-subject
4790                     (not (string= gnus-tmp-prev-subject simp-subject)))
4791                subject)
4792               ((zerop gnus-tmp-level)
4793                (if (and (eq gnus-summary-make-false-root 'empty)
4794                         (memq number gnus-tmp-gathered)
4795                         gnus-tmp-prev-subject
4796                         (string= gnus-tmp-prev-subject simp-subject))
4797                    gnus-summary-same-subject
4798                  subject))
4799               (t gnus-summary-same-subject)))
4800             (if (and (eq gnus-summary-make-false-root 'adopt)
4801                      (= gnus-tmp-level 1)
4802                      (memq number gnus-tmp-gathered))
4803                 (setq gnus-tmp-opening-bracket ?\<
4804                       gnus-tmp-closing-bracket ?\>)
4805               (setq gnus-tmp-opening-bracket ?\[
4806                     gnus-tmp-closing-bracket ?\]))
4807             (setq
4808              gnus-tmp-indentation
4809              (aref gnus-thread-indent-array gnus-tmp-level)
4810              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4811              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4812                                 gnus-summary-default-score 0)
4813              gnus-tmp-score-char
4814              (if (or (null gnus-summary-default-score)
4815                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4816                          gnus-summary-zcore-fuzz))
4817                  ?                      ;Whitespace
4818                (if (< gnus-tmp-score gnus-summary-default-score)
4819                    gnus-score-below-mark gnus-score-over-mark))
4820              gnus-tmp-replied
4821              (cond ((memq number gnus-newsgroup-processable)
4822                     gnus-process-mark)
4823                    ((memq number gnus-newsgroup-cached)
4824                     gnus-cached-mark)
4825                    ((memq number gnus-newsgroup-replied)
4826                     gnus-replied-mark)
4827                    ((memq number gnus-newsgroup-forwarded)
4828                     gnus-forwarded-mark)
4829                    ((memq number gnus-newsgroup-saved)
4830                     gnus-saved-mark)
4831                    ((memq number gnus-newsgroup-recent)
4832                     gnus-recent-mark)
4833                    ((memq number gnus-newsgroup-unseen)
4834                     gnus-unseen-mark)
4835                    (t gnus-no-mark))
4836              gnus-tmp-downloaded
4837              (cond ((memq number gnus-newsgroup-undownloaded)
4838                     gnus-undownloaded-mark)
4839                    (gnus-newsgroup-agentized
4840                     gnus-downloaded-mark)
4841                    (t
4842                     gnus-no-mark))
4843              gnus-tmp-from (mail-header-from gnus-tmp-header)
4844              gnus-tmp-name
4845              (cond
4846               ((string-match "<[^>]+> *$" gnus-tmp-from)
4847                (setq beg-match (match-beginning 0))
4848                (or (and (string-match "^\".+\"" gnus-tmp-from)
4849                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4850                    (substring gnus-tmp-from 0 beg-match)))
4851               ((string-match "(.+)" gnus-tmp-from)
4852                (substring gnus-tmp-from
4853                           (1+ (match-beginning 0)) (1- (match-end 0))))
4854               (t gnus-tmp-from))
4855
4856              ;; Do the %B string
4857              gnus-tmp-thread-tree-header-string
4858              (cond
4859               ((not gnus-show-threads) "")
4860               ((zerop gnus-tmp-level)
4861                (cond ((cdar thread)
4862                       (or gnus-sum-thread-tree-root subject))
4863                      (gnus-tmp-new-adopts
4864                       (or gnus-sum-thread-tree-false-root subject))
4865                      (t
4866                       (or gnus-sum-thread-tree-single-indent subject))))
4867               (t
4868                (concat (apply 'concat
4869                               (mapcar (lambda (item)
4870                                         (if (= item 1)
4871                                             gnus-sum-thread-tree-vertical
4872                                           gnus-sum-thread-tree-indent))
4873                                       (cdr (reverse tree-stack))))
4874                        (if (nth 1 thread)
4875                            gnus-sum-thread-tree-leaf-with-other
4876                          gnus-sum-thread-tree-single-leaf)))))
4877             (when (string= gnus-tmp-name "")
4878               (setq gnus-tmp-name gnus-tmp-from))
4879             (unless (numberp gnus-tmp-lines)
4880               (setq gnus-tmp-lines -1))
4881             (if (= gnus-tmp-lines -1)
4882                 (setq gnus-tmp-lines "?")
4883               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4884               (gnus-put-text-property
4885              (point)
4886              (progn (eval gnus-summary-line-format-spec) (point))
4887                'gnus-number number)
4888             (when gnus-visual-p
4889               (forward-line -1)
4890               (gnus-run-hooks 'gnus-summary-update-hook)
4891               (forward-line 1))
4892
4893             (setq gnus-tmp-prev-subject simp-subject)))
4894
4895         (when (nth 1 thread)
4896           (push (list (max 0 gnus-tmp-level)
4897                       (copy-sequence tree-stack)
4898                       (nthcdr 1 thread))
4899                 stack))
4900         (push (if (nth 1 thread) 1 0) tree-stack)
4901         (incf gnus-tmp-level)
4902         (setq threads (if thread-end nil (cdar thread)))
4903         (if gnus-summary-display-while-building
4904             (if building-count
4905                 (progn
4906                   ;; use a set frequency
4907                   (setq building-line-count (1- building-line-count))
4908                   (when (= building-line-count 0)
4909                     (sit-for 0)
4910                     (setq building-line-count
4911                           gnus-summary-display-while-building)))
4912               ;; always
4913               (sit-for 0)))
4914         (unless threads
4915           (setq gnus-tmp-level 0)))))
4916   (gnus-message 7 "Generating summary...done"))
4917
4918 (defun gnus-summary-prepare-unthreaded (headers)
4919   "Generate an unthreaded summary buffer based on HEADERS."
4920   (let (header number mark)
4921
4922     (beginning-of-line)
4923
4924     (while headers
4925       ;; We may have to root out some bad articles...
4926       (when (memq (setq number (mail-header-number
4927                                 (setq header (pop headers))))
4928                   gnus-newsgroup-limit)
4929         ;; Mark article as read when it has a low score.
4930         (when (and gnus-summary-mark-below
4931                    (< (or (cdr (assq number gnus-newsgroup-scored))
4932                           gnus-summary-default-score 0)
4933                       gnus-summary-mark-below)
4934                    (not (gnus-summary-article-ancient-p number)))
4935           (setq gnus-newsgroup-unreads
4936                 (delq number gnus-newsgroup-unreads))
4937           (if gnus-newsgroup-auto-expire
4938               (push number gnus-newsgroup-expirable)
4939             (push (cons number gnus-low-score-mark)
4940                   gnus-newsgroup-reads)))
4941
4942         (setq mark (gnus-article-mark number))
4943         (push (gnus-data-make number mark (1+ (point)) header 0)
4944               gnus-newsgroup-data)
4945         (gnus-summary-insert-line
4946          header 0 number
4947          (memq number gnus-newsgroup-undownloaded)
4948          mark (memq number gnus-newsgroup-replied)
4949          (memq number gnus-newsgroup-expirable)
4950          (mail-header-subject header) nil
4951          (cdr (assq number gnus-newsgroup-scored))
4952          (memq number gnus-newsgroup-processable))))))
4953
4954 (defun gnus-summary-remove-list-identifiers ()
4955   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4956   (let ((regexp (if (consp gnus-list-identifiers)
4957                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4958                   gnus-list-identifiers))
4959         changed subject)
4960     (when regexp
4961       (dolist (header gnus-newsgroup-headers)
4962         (setq subject (mail-header-subject header)
4963               changed nil)
4964         (while (string-match
4965                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4966                 subject)
4967           (setq subject
4968                 (concat (substring subject 0 (match-beginning 2))
4969                         (substring subject (match-end 0)))
4970                 changed t))
4971         (when (and changed
4972                    (string-match
4973                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4974           (setq subject
4975                 (concat (substring subject 0 (match-beginning 1))
4976                         (substring subject (match-end 1)))))
4977         (when changed
4978           (mail-header-set-subject header subject))))))
4979
4980 (defun gnus-fetch-headers (articles)
4981   "Fetch headers of ARTICLES."
4982   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4983     (gnus-message 5 "Fetching headers for %s..." name)
4984     (prog1
4985         (if (eq 'nov
4986                 (setq gnus-headers-retrieved-by
4987                       (gnus-retrieve-headers
4988                        articles gnus-newsgroup-name
4989                        ;; We might want to fetch old headers, but
4990                        ;; not if there is only 1 article.
4991                        (and (or (and
4992                                  (not (eq gnus-fetch-old-headers 'some))
4993                                  (not (numberp gnus-fetch-old-headers)))
4994                                 (> (length articles) 1))
4995                             gnus-fetch-old-headers))))
4996             (gnus-get-newsgroup-headers-xover
4997              articles nil nil gnus-newsgroup-name t)
4998           (gnus-get-newsgroup-headers))
4999       (gnus-message 5 "Fetching headers for %s...done" name))))
5000
5001 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5002   "Select newsgroup GROUP.
5003 If READ-ALL is non-nil, all articles in the group are selected.
5004 If SELECT-ARTICLES, only select those articles from GROUP."
5005   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5006          ;;!!! Dirty hack; should be removed.
5007          (gnus-summary-ignore-duplicates
5008           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5009               t
5010             gnus-summary-ignore-duplicates))
5011          (info (nth 2 entry))
5012          articles fetched-articles cached)
5013
5014     (unless (gnus-check-server
5015              (set (make-local-variable 'gnus-current-select-method)
5016                   (gnus-find-method-for-group group)))
5017       (error "Couldn't open server"))
5018
5019     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5020         (gnus-activate-group group)     ; Or we can activate it...
5021         (progn                          ; Or we bug out.
5022           (when (equal major-mode 'gnus-summary-mode)
5023             (gnus-kill-buffer (current-buffer)))
5024           (error "Couldn't activate group %s: %s"
5025                  group (gnus-status-message group))))
5026
5027     (unless (gnus-request-group group t)
5028       (when (equal major-mode 'gnus-summary-mode)
5029         (gnus-kill-buffer (current-buffer)))
5030       (error "Couldn't request group %s: %s"
5031              group (gnus-status-message group)))
5032
5033     (when gnus-agent
5034       ;; The agent may be storing articles that are no longer in the
5035       ;; server's active range.  If that is the case, the active range
5036       ;; needs to be expanded such that the agent's articles can be
5037       ;; included in the summary.
5038       (let* ((gnus-command-method (gnus-find-method-for-group group))
5039              (alist (gnus-agent-load-alist group))
5040              (active (gnus-active group)))
5041         (if (and (car alist)
5042                  (< (caar alist) (car active)))
5043             (gnus-set-active group (cons (caar alist) (cdr active)))))
5044
5045       (setq gnus-summary-use-undownloaded-faces
5046             (not (gnus-agent-find-parameter
5047                   group
5048                   'agent-disable-undownloaded-faces))))
5049
5050     (setq gnus-newsgroup-name group
5051           gnus-newsgroup-unselected nil
5052           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5053
5054     (let ((display (gnus-group-find-parameter group 'display)))
5055       (setq gnus-newsgroup-display
5056             (cond
5057              ((not (zerop (or (car-safe read-all) 0)))
5058               ;; The user entered the group with C-u SPC/RET, let's show
5059               ;; all articles.
5060               'gnus-not-ignore)
5061              ((eq display 'all)
5062               'gnus-not-ignore)
5063              ((arrayp display)
5064               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5065              ((numberp display)
5066               ;; The following is probably the "correct" solution, but
5067               ;; it makes Gnus fetch all headers and then limit the
5068               ;; articles (which is slow), so instead we hack the
5069               ;; select-articles parameter instead. -- Simon Josefsson
5070               ;; <jas@kth.se>
5071               ;;
5072               ;; (gnus-byte-compile
5073               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5074               ;;                         display)))))
5075               (setq select-articles
5076                     (gnus-uncompress-range
5077                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5078                              (if (> tmp 0)
5079                                  tmp
5080                                1))
5081                            (cdr (gnus-active group)))))
5082               nil)
5083              (t
5084               nil))))
5085
5086     (gnus-summary-setup-default-charset)
5087
5088     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5089     (when (gnus-virtual-group-p group)
5090       (setq cached gnus-newsgroup-cached))
5091
5092     (setq gnus-newsgroup-unreads
5093           (gnus-sorted-ndifference
5094            (gnus-sorted-ndifference gnus-newsgroup-unreads
5095                                     gnus-newsgroup-marked)
5096            gnus-newsgroup-dormant))
5097
5098     (setq gnus-newsgroup-processable nil)
5099
5100     (gnus-update-read-articles group gnus-newsgroup-unreads)
5101
5102     ;; Adjust and set lists of article marks.
5103     (when info
5104       (gnus-adjust-marked-articles info))
5105     (if (setq articles select-articles)
5106         (setq gnus-newsgroup-unselected
5107               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5108       (setq articles (gnus-articles-to-read group read-all)))
5109
5110     (cond
5111      ((null articles)
5112       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5113       'quit)
5114      ((eq articles 0) nil)
5115      (t
5116       ;; Init the dependencies hash table.
5117       (setq gnus-newsgroup-dependencies
5118             (gnus-make-hashtable (length articles)))
5119       (gnus-set-global-variables)
5120       ;; Retrieve the headers and read them in.
5121
5122       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5123
5124       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5125       (when cached
5126         (setq gnus-newsgroup-cached cached))
5127
5128       ;; Suppress duplicates?
5129       (when gnus-suppress-duplicates
5130         (gnus-dup-suppress-articles))
5131
5132       ;; Set the initial limit.
5133       (setq gnus-newsgroup-limit (copy-sequence articles))
5134       ;; Remove canceled articles from the list of unread articles.
5135       (setq fetched-articles
5136             (mapcar (lambda (headers) (mail-header-number headers))
5137                     gnus-newsgroup-headers))
5138       (setq gnus-newsgroup-articles fetched-articles)
5139       (setq gnus-newsgroup-unreads
5140             (gnus-sorted-nintersection
5141              gnus-newsgroup-unreads fetched-articles))
5142       (gnus-compute-unseen-list)
5143
5144       ;; Removed marked articles that do not exist.
5145       (gnus-update-missing-marks
5146        (gnus-sorted-difference articles fetched-articles))
5147       ;; We might want to build some more threads first.
5148       (when (and gnus-fetch-old-headers
5149                  (eq gnus-headers-retrieved-by 'nov))
5150         (if (eq gnus-fetch-old-headers 'invisible)
5151             (gnus-build-all-threads)
5152           (gnus-build-old-threads)))
5153       ;; Let the Gnus agent mark articles as read.
5154       (when gnus-agent
5155         (gnus-agent-get-undownloaded-list))
5156       ;; Remove list identifiers from subject
5157       (when gnus-list-identifiers
5158         (gnus-summary-remove-list-identifiers))
5159       ;; Check whether auto-expire is to be done in this group.
5160       (setq gnus-newsgroup-auto-expire
5161             (gnus-group-auto-expirable-p group))
5162       ;; Set up the article buffer now, if necessary.
5163       (unless gnus-single-article-buffer
5164         (gnus-article-setup-buffer))
5165       ;; First and last article in this newsgroup.
5166       (when gnus-newsgroup-headers
5167         (setq gnus-newsgroup-begin
5168               (mail-header-number (car gnus-newsgroup-headers))
5169               gnus-newsgroup-end
5170               (mail-header-number
5171                (gnus-last-element gnus-newsgroup-headers))))
5172       ;; GROUP is successfully selected.
5173       (or gnus-newsgroup-headers t)))))
5174
5175 (defun gnus-compute-unseen-list ()
5176   ;; The `seen' marks are treated specially.
5177   (if (not gnus-newsgroup-seen)
5178       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5179     (setq gnus-newsgroup-unseen
5180           (gnus-inverse-list-range-intersection
5181            gnus-newsgroup-articles gnus-newsgroup-seen))))
5182
5183 (defun gnus-summary-display-make-predicate (display)
5184   (require 'gnus-agent)
5185   (when (= (length display) 1)
5186     (setq display (car display)))
5187   (unless gnus-summary-display-cache
5188     (dolist (elem (append '((unread . unread)
5189                             (read . read)
5190                             (unseen . unseen))
5191                           gnus-article-mark-lists))
5192       (push (cons (cdr elem)
5193                   (gnus-byte-compile
5194                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5195             gnus-summary-display-cache)))
5196   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5197         (gnus-category-predicate-cache gnus-summary-display-cache))
5198     (gnus-get-predicate display)))
5199
5200 ;; Uses the dynamically bound `number' variable.
5201 (eval-when-compile
5202   (defvar number))
5203 (defun gnus-article-marked-p (type &optional article)
5204   (let ((article (or article number)))
5205     (cond
5206      ((eq type 'tick)
5207       (memq article gnus-newsgroup-marked))
5208      ((eq type 'spam)
5209       (memq article gnus-newsgroup-spam-marked))
5210      ((eq type 'unsend)
5211       (memq article gnus-newsgroup-unsendable))
5212      ((eq type 'undownload)
5213       (memq article gnus-newsgroup-undownloaded))
5214      ((eq type 'download)
5215       (memq article gnus-newsgroup-downloadable))
5216      ((eq type 'unread)
5217       (memq article gnus-newsgroup-unreads))
5218      ((eq type 'read)
5219       (memq article gnus-newsgroup-reads))
5220      ((eq type 'dormant)
5221       (memq article gnus-newsgroup-dormant) )
5222      ((eq type 'expire)
5223       (memq article gnus-newsgroup-expirable))
5224      ((eq type 'reply)
5225       (memq article gnus-newsgroup-replied))
5226      ((eq type 'killed)
5227       (memq article gnus-newsgroup-killed))
5228      ((eq type 'bookmark)
5229       (assq article gnus-newsgroup-bookmarks))
5230      ((eq type 'score)
5231       (assq article gnus-newsgroup-scored))
5232      ((eq type 'save)
5233       (memq article gnus-newsgroup-saved))
5234      ((eq type 'cache)
5235       (memq article gnus-newsgroup-cached))
5236      ((eq type 'forward)
5237       (memq article gnus-newsgroup-forwarded))
5238      ((eq type 'seen)
5239       (not (memq article gnus-newsgroup-unseen)))
5240      ((eq type 'recent)
5241       (memq article gnus-newsgroup-recent))
5242      (t t))))
5243
5244 (defun gnus-articles-to-read (group &optional read-all)
5245   "Find out what articles the user wants to read."
5246   (let* ((display (gnus-group-find-parameter group 'display))
5247          (articles
5248           ;; Select all articles if `read-all' is non-nil, or if there
5249           ;; are no unread articles.
5250           (if (or read-all
5251                   (and (zerop (length gnus-newsgroup-marked))
5252                        (zerop (length gnus-newsgroup-unreads)))
5253                   ;; Fetch all if the predicate is non-nil.
5254                   gnus-newsgroup-display)
5255               ;; We want to select the headers for all the articles in
5256               ;; the group, so we select either all the active
5257               ;; articles in the group, or (if that's nil), the
5258               ;; articles in the cache.
5259               (or
5260                (gnus-uncompress-range (gnus-active group))
5261                (gnus-cache-articles-in-group group))
5262             ;; Select only the "normal" subset of articles.
5263             (gnus-sorted-nunion
5264              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5265              gnus-newsgroup-unreads)))
5266          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5267          (scored (length scored-list))
5268          (number (length articles))
5269          (marked (+ (length gnus-newsgroup-marked)
5270                     (length gnus-newsgroup-dormant)))
5271          (select
5272           (cond
5273            ((numberp read-all)
5274             read-all)
5275            ((numberp gnus-newsgroup-display)
5276             gnus-newsgroup-display)
5277            (t
5278             (condition-case ()
5279                 (cond
5280                  ((and (or (<= scored marked) (= scored number))
5281                        (numberp gnus-large-newsgroup)
5282                        (> number gnus-large-newsgroup))
5283                   (let* ((cursor-in-echo-area nil)
5284                          (initial (gnus-parameter-large-newsgroup-initial
5285                                    gnus-newsgroup-name))
5286                          (input
5287                           (read-string
5288                            (format
5289                             "How many articles from %s (%s %d): "
5290                             (gnus-limit-string
5291                              (gnus-group-decoded-name gnus-newsgroup-name)
5292                              35)
5293                             (if initial "max" "default")
5294                             number)
5295                            (if initial
5296                                (cons (number-to-string initial)
5297                                      0)))))
5298                     (if (string-match "^[ \t]*$" input) number input)))
5299                  ((and (> scored marked) (< scored number)
5300                        (> (- scored number) 20))
5301                   (let ((input
5302                          (read-string
5303                           (format "%s %s (%d scored, %d total): "
5304                                   "How many articles from"
5305                                   (gnus-group-decoded-name group)
5306                                   scored number))))
5307                     (if (string-match "^[ \t]*$" input)
5308                         number input)))
5309                  (t number))
5310               (quit
5311                (message "Quit getting the articles to read")
5312                nil))))))
5313     (setq select (if (stringp select) (string-to-number select) select))
5314     (if (or (null select) (zerop select))
5315         select
5316       (if (and (not (zerop scored)) (<= (abs select) scored))
5317           (progn
5318             (setq articles (sort scored-list '<))
5319             (setq number (length articles)))
5320         (setq articles (copy-sequence articles)))
5321
5322       (when (< (abs select) number)
5323         (if (< select 0)
5324             ;; Select the N oldest articles.
5325             (setcdr (nthcdr (1- (abs select)) articles) nil)
5326           ;; Select the N most recent articles.
5327           (setq articles (nthcdr (- number select) articles))))
5328       (setq gnus-newsgroup-unselected
5329             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5330       (when gnus-alter-articles-to-read-function
5331         (setq articles
5332               (sort
5333                (funcall gnus-alter-articles-to-read-function
5334                         gnus-newsgroup-name articles)
5335                '<)))
5336       articles)))
5337
5338 (defun gnus-killed-articles (killed articles)
5339   (let (out)
5340     (while articles
5341       (when (inline (gnus-member-of-range (car articles) killed))
5342         (push (car articles) out))
5343       (setq articles (cdr articles)))
5344     out))
5345
5346 (defun gnus-uncompress-marks (marks)
5347   "Uncompress the mark ranges in MARKS."
5348   (let ((uncompressed '(score bookmark))
5349         out)
5350     (while marks
5351       (if (memq (caar marks) uncompressed)
5352           (push (car marks) out)
5353         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5354       (setq marks (cdr marks)))
5355     out))
5356
5357 (defun gnus-article-mark-to-type (mark)
5358   "Return the type of MARK."
5359   (or (cadr (assq mark gnus-article-special-mark-lists))
5360       'list))
5361
5362 (defun gnus-article-unpropagatable-p (mark)
5363   "Return whether MARK should be propagated to back end."
5364   (memq mark gnus-article-unpropagated-mark-lists))
5365
5366 (defun gnus-adjust-marked-articles (info)
5367   "Set all article lists and remove all marks that are no longer valid."
5368   (let* ((marked-lists (gnus-info-marks info))
5369          (active (gnus-active (gnus-info-group info)))
5370          (min (car active))
5371          (max (cdr active))
5372          (types gnus-article-mark-lists)
5373          marks var articles article mark mark-type)
5374
5375     (dolist (marks marked-lists)
5376       (setq mark (car marks)
5377             mark-type (gnus-article-mark-to-type mark)
5378             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5379
5380       ;; We set the variable according to the type of the marks list,
5381       ;; and then adjust the marks to a subset of the active articles.
5382       (cond
5383        ;; Adjust "simple" lists.
5384        ((eq mark-type 'list)
5385         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5386         (when (memq mark '(tick dormant expire reply save))
5387           (while articles
5388             (when (or (< (setq article (pop articles)) min) (> article max))
5389               (set var (delq article (symbol-value var)))))))
5390        ;; Adjust assocs.
5391        ((eq mark-type 'tuple)
5392         (set var (setq articles (cdr marks)))
5393         (when (not (listp (cdr (symbol-value var))))
5394           (set var (list (symbol-value var))))
5395         (when (not (listp (cdr articles)))
5396           (setq articles (list articles)))
5397         (while articles
5398           (when (or (not (consp (setq article (pop articles))))
5399                     (< (car article) min)
5400                     (> (car article) max))
5401             (set var (delq article (symbol-value var))))))
5402        ;; Adjust ranges (sloppily).
5403        ((eq mark-type 'range)
5404         (cond
5405          ((eq mark 'seen)
5406           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5407           ;; It should be (seen (NUM1 . NUM2)).
5408           (when (numberp (cddr marks))
5409             (setcdr marks (list (cdr marks))))
5410           (setq articles (cdr marks))
5411           (while (and articles
5412                       (or (and (consp (car articles))
5413                                (> min (cdar articles)))
5414                           (and (numberp (car articles))
5415                                (> min (car articles)))))
5416             (pop articles))
5417           (set var articles))))))))
5418
5419 (defun gnus-update-missing-marks (missing)
5420   "Go through the list of MISSING articles and remove them from the mark lists."
5421   (when missing
5422     (let (var m)
5423       ;; Go through all types.
5424       (dolist (elem gnus-article-mark-lists)
5425         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5426           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5427           (when (symbol-value var)
5428             ;; This list has articles.  So we delete all missing
5429             ;; articles from it.
5430             (setq m missing)
5431             (while m
5432               (set var (delq (pop m) (symbol-value var))))))))))
5433
5434 (defun gnus-update-marks ()
5435   "Enter the various lists of marked articles into the newsgroup info list."
5436   (let ((types gnus-article-mark-lists)
5437         (info (gnus-get-info gnus-newsgroup-name))
5438         type list newmarked symbol delta-marks)
5439     (when info
5440       ;; Add all marks lists to the list of marks lists.
5441       (while (setq type (pop types))
5442         (setq list (symbol-value
5443                     (setq symbol
5444                           (intern (format "gnus-newsgroup-%s" (car type))))))
5445
5446         (when list
5447           ;; Get rid of the entries of the articles that have the
5448           ;; default score.
5449           (when (and (eq (cdr type) 'score)
5450                      gnus-save-score
5451                      list)
5452             (let* ((arts list)
5453                    (prev (cons nil list))
5454                    (all prev))
5455               (while arts
5456                 (if (or (not (consp (car arts)))
5457                         (= (cdar arts) gnus-summary-default-score))
5458                     (setcdr prev (cdr arts))
5459                   (setq prev arts))
5460                 (setq arts (cdr arts)))
5461               (setq list (cdr all)))))
5462
5463         (when (eq (cdr type) 'seen)
5464           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5465
5466         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5467           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5468
5469         (when (and (gnus-check-backend-function
5470                     'request-set-mark gnus-newsgroup-name)
5471                    (not (gnus-article-unpropagatable-p (cdr type))))
5472           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5473                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5474                  (add (gnus-remove-from-range
5475                        (gnus-copy-sequence list) old)))
5476             (when add
5477               (push (list add 'add (list (cdr type))) delta-marks))
5478             (when del
5479               (push (list del 'del (list (cdr type))) delta-marks))))
5480
5481         (when list
5482           (push (cons (cdr type) list) newmarked)))
5483
5484       (when delta-marks
5485         (unless (gnus-check-group gnus-newsgroup-name)
5486           (error "Can't open server for %s" gnus-newsgroup-name))
5487         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5488
5489       ;; Enter these new marks into the info of the group.
5490       (if (nthcdr 3 info)
5491           (setcar (nthcdr 3 info) newmarked)
5492         ;; Add the marks lists to the end of the info.
5493         (when newmarked
5494           (setcdr (nthcdr 2 info) (list newmarked))))
5495
5496       ;; Cut off the end of the info if there's nothing else there.
5497       (let ((i 5))
5498         (while (and (> i 2)
5499                     (not (nth i info)))
5500           (when (nthcdr (decf i) info)
5501             (setcdr (nthcdr i info) nil)))))))
5502
5503 (defun gnus-set-mode-line (where)
5504   "Set the mode line of the article or summary buffers.
5505 If WHERE is `summary', the summary mode line format will be used."
5506   ;; Is this mode line one we keep updated?
5507   (when (and (memq where gnus-updated-mode-lines)
5508              (symbol-value
5509               (intern (format "gnus-%s-mode-line-format-spec" where))))
5510     (let (mode-string)
5511       (save-excursion
5512         ;; We evaluate this in the summary buffer since these
5513         ;; variables are buffer-local to that buffer.
5514         (set-buffer gnus-summary-buffer)
5515        ;; We bind all these variables that are used in the `eval' form
5516         ;; below.
5517         (let* ((mformat (symbol-value
5518                          (intern
5519                           (format "gnus-%s-mode-line-format-spec" where))))
5520                (gnus-tmp-group-name (gnus-group-decoded-name
5521                                      gnus-newsgroup-name))
5522                (gnus-tmp-article-number (or gnus-current-article 0))
5523                (gnus-tmp-unread gnus-newsgroup-unreads)
5524                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5525                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5526                (gnus-tmp-unread-and-unselected
5527                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5528                             (zerop gnus-tmp-unselected))
5529                        "")
5530                       ((zerop gnus-tmp-unselected)
5531                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5532                       (t (format "{%d(+%d) more}"
5533                                  gnus-tmp-unread-and-unticked
5534                                  gnus-tmp-unselected))))
5535                (gnus-tmp-subject
5536                 (if (and gnus-current-headers
5537                          (vectorp gnus-current-headers))
5538                     (gnus-mode-string-quote
5539                      (mail-header-subject gnus-current-headers))
5540                   ""))
5541                bufname-length max-len
5542                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5543           (setq mode-string (eval mformat))
5544           (setq bufname-length (if (string-match "%b" mode-string)
5545                                    (- (length
5546                                        (buffer-name
5547                                         (if (eq where 'summary)
5548                                             nil
5549                                           (get-buffer gnus-article-buffer))))
5550                                       2)
5551                                  0))
5552           (setq max-len (max 4 (if gnus-mode-non-string-length
5553                                    (- (window-width)
5554                                       gnus-mode-non-string-length
5555                                       bufname-length)
5556                                  (length mode-string))))
5557           ;; We might have to chop a bit of the string off...
5558           (when (> (length mode-string) max-len)
5559             (setq mode-string
5560                   (concat (truncate-string-to-width mode-string (- max-len 3))
5561                           "...")))
5562           ;; Pad the mode string a bit.
5563           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5564       ;; Update the mode line.
5565       (setq mode-line-buffer-identification
5566             (gnus-mode-line-buffer-identification (list mode-string)))
5567       (set-buffer-modified-p t))))
5568
5569 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5570   "Go through the HEADERS list and add all Xrefs to a hash table.
5571 The resulting hash table is returned, or nil if no Xrefs were found."
5572   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5573          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5574          (xref-hashtb (gnus-make-hashtable))
5575          start group entry number xrefs header)
5576     (while headers
5577       (setq header (pop headers))
5578       (when (and (setq xrefs (mail-header-xref header))
5579                  (not (memq (setq number (mail-header-number header))
5580                             unreads)))
5581         (setq start 0)
5582         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5583           (setq start (match-end 0))
5584           (setq group (if prefix
5585                           (concat prefix (substring xrefs (match-beginning 1)
5586                                                     (match-end 1)))
5587                         (substring xrefs (match-beginning 1) (match-end 1))))
5588           (setq number
5589                 (string-to-int (substring xrefs (match-beginning 2)
5590                                           (match-end 2))))
5591           (if (setq entry (gnus-gethash group xref-hashtb))
5592               (setcdr entry (cons number (cdr entry)))
5593             (gnus-sethash group (cons number nil) xref-hashtb)))))
5594     (and start xref-hashtb)))
5595
5596 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5597   "Look through all the headers and mark the Xrefs as read."
5598   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5599         name entry info xref-hashtb idlist method nth4)
5600     (save-excursion
5601       (set-buffer gnus-group-buffer)
5602       (when (setq xref-hashtb
5603                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5604         (mapatoms
5605          (lambda (group)
5606            (unless (string= from-newsgroup (setq name (symbol-name group)))
5607              (setq idlist (symbol-value group))
5608              ;; Dead groups are not updated.
5609              (and (prog1
5610                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5611                             info (nth 2 entry))
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-gethash group gnus-newsrc-hashtb))
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-gethash group gnus-newsrc-hashtb))
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       (save-excursion
5677         (set-buffer gnus-group-buffer)
5678         (gnus-undo-register
5679           `(progn
5680              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5681              (gnus-info-set-read ',info ',(gnus-info-read info))
5682              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5683              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5684              (gnus-group-update-group ,group t))))
5685       ;; Add the read articles to the range.
5686       (gnus-info-set-read info range)
5687       (gnus-request-set-mark group (list (list range 'add '(read))))
5688       ;; Then we have to re-compute how many unread
5689       ;; articles there are in this group.
5690       (when active
5691         (cond
5692          ((not range)
5693           (setq num (- (1+ (cdr active)) (car active))))
5694          ((not (listp (cdr range)))
5695           (setq num (- (cdr active) (- (1+ (cdr range))
5696                                        (car range)))))
5697          (t
5698           (while range
5699             (if (numberp (car range))
5700                 (setq num (1+ num))
5701               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5702             (setq range (cdr range)))
5703           (setq num (- (cdr active) num))))
5704         ;; Update the number of unread articles.
5705         (setcar entry num)
5706         ;; Update the group buffer.
5707         (unless (gnus-ephemeral-group-p group)
5708           (gnus-group-update-group group t))))))
5709
5710 (defvar gnus-newsgroup-none-id 0)
5711
5712 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5713   (let ((cur nntp-server-buffer)
5714         (dependencies
5715          (or dependencies
5716              (save-excursion (set-buffer gnus-summary-buffer)
5717                              gnus-newsgroup-dependencies)))
5718         headers id end ref
5719         (mail-parse-charset gnus-newsgroup-charset)
5720         (mail-parse-ignored-charsets
5721          (save-excursion (condition-case nil
5722                              (set-buffer gnus-summary-buffer)
5723                            (error))
5724                          gnus-newsgroup-ignored-charsets)))
5725     (save-excursion
5726       (set-buffer nntp-server-buffer)
5727       ;; Translate all TAB characters into SPACE characters.
5728       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5729       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5730       (ietf-drums-unfold-fws)
5731       (gnus-run-hooks 'gnus-parse-headers-hook)
5732       (let ((case-fold-search t)
5733             in-reply-to header p lines chars)
5734         (goto-char (point-min))
5735         ;; Search to the beginning of the next header.  Error messages
5736         ;; do not begin with 2 or 3.
5737         (while (re-search-forward "^[23][0-9]+ " nil t)
5738           (setq id nil
5739                 ref nil)
5740           ;; This implementation of this function, with nine
5741           ;; search-forwards instead of the one re-search-forward and
5742           ;; a case (which basically was the old function) is actually
5743           ;; about twice as fast, even though it looks messier.  You
5744           ;; can't have everything, I guess.  Speed and elegance
5745           ;; doesn't always go hand in hand.
5746           (setq
5747            header
5748            (vector
5749             ;; Number.
5750             (prog1
5751                 (read cur)
5752               (end-of-line)
5753               (setq p (point))
5754               (narrow-to-region (point)
5755                                 (or (and (search-forward "\n.\n" nil t)
5756                                          (- (point) 2))
5757                                     (point))))
5758             ;; Subject.
5759             (progn
5760               (goto-char p)
5761               (if (search-forward "\nsubject:" nil t)
5762                   (funcall gnus-decode-encoded-word-function
5763                            (nnheader-header-value))
5764                 "(none)"))
5765             ;; From.
5766             (progn
5767               (goto-char p)
5768               (if (search-forward "\nfrom:" nil t)
5769                   (funcall gnus-decode-encoded-word-function
5770                            (nnheader-header-value))
5771                 "(nobody)"))
5772             ;; Date.
5773             (progn
5774               (goto-char p)
5775               (if (search-forward "\ndate:" nil t)
5776                   (nnheader-header-value) ""))
5777             ;; Message-ID.
5778             (progn
5779               (goto-char p)
5780               (setq id (if (re-search-forward
5781                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5782                            ;; We do it this way to make sure the Message-ID
5783                            ;; is (somewhat) syntactically valid.
5784                            (buffer-substring (match-beginning 1)
5785                                              (match-end 1))
5786                          ;; If there was no message-id, we just fake one
5787                          ;; to make subsequent routines simpler.
5788                          (nnheader-generate-fake-message-id))))
5789             ;; References.
5790             (progn
5791               (goto-char p)
5792               (if (search-forward "\nreferences:" nil t)
5793                   (progn
5794                     (setq end (point))
5795                     (prog1
5796                         (nnheader-header-value)
5797                       (setq ref
5798                             (buffer-substring
5799                              (progn
5800                                (end-of-line)
5801                                (search-backward ">" end t)
5802                                (1+ (point)))
5803                              (progn
5804                                (search-backward "<" end t)
5805                                (point))))))
5806                 ;; Get the references from the in-reply-to header if there
5807                 ;; were no references and the in-reply-to header looks
5808                 ;; promising.
5809                 (if (and (search-forward "\nin-reply-to:" nil t)
5810                          (setq in-reply-to (nnheader-header-value))
5811                          (string-match "<[^>]+>" in-reply-to))
5812                     (let (ref2)
5813                       (setq ref (substring in-reply-to (match-beginning 0)
5814                                            (match-end 0)))
5815                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5816                         (setq ref2 (substring in-reply-to (match-beginning 0)
5817                                               (match-end 0)))
5818                         (when (> (length ref2) (length ref))
5819                           (setq ref ref2)))
5820                       ref)
5821                   (setq ref nil))))
5822             ;; Chars.
5823             (progn
5824               (goto-char p)
5825               (if (search-forward "\nchars: " nil t)
5826                   (if (numberp (setq chars (ignore-errors (read cur))))
5827                       chars -1)
5828                 -1))
5829             ;; Lines.
5830             (progn
5831               (goto-char p)
5832               (if (search-forward "\nlines: " nil t)
5833                   (if (numberp (setq lines (ignore-errors (read cur))))
5834                       lines -1)
5835                 -1))
5836             ;; Xref.
5837             (progn
5838               (goto-char p)
5839               (and (search-forward "\nxref:" nil t)
5840                    (nnheader-header-value)))
5841             ;; Extra.
5842             (when gnus-extra-headers
5843               (let ((extra gnus-extra-headers)
5844                     out)
5845                 (while extra
5846                   (goto-char p)
5847                   (when (search-forward
5848                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5849                     (push (cons (car extra) (nnheader-header-value))
5850                           out))
5851                   (pop extra))
5852                 out))))
5853           (when (equal id ref)
5854             (setq ref nil))
5855
5856           (when gnus-alter-header-function
5857             (funcall gnus-alter-header-function header)
5858             (setq id (mail-header-id header)
5859                   ref (gnus-parent-id (mail-header-references header))))
5860
5861           (when (setq header
5862                       (gnus-dependencies-add-header
5863                        header dependencies force-new))
5864             (push header headers))
5865           (goto-char (point-max))
5866           (widen))
5867         (nreverse headers)))))
5868
5869 ;; Goes through the xover lines and returns a list of vectors
5870 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5871                                                   force-new dependencies
5872                                                   group also-fetch-heads)
5873   "Parse the news overview data in the server buffer.
5874 Return a list of headers that match SEQUENCE (see
5875 `nntp-retrieve-headers')."
5876   ;; Get the Xref when the users reads the articles since most/some
5877   ;; NNTP servers do not include Xrefs when using XOVER.
5878   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5879   (let ((mail-parse-charset gnus-newsgroup-charset)
5880         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5881         (cur nntp-server-buffer)
5882         (dependencies (or dependencies gnus-newsgroup-dependencies))
5883         (allp (cond
5884                ((eq gnus-read-all-available-headers t)
5885                 t)
5886                ((stringp gnus-read-all-available-headers)
5887                 (string-match gnus-read-all-available-headers group))
5888                (t
5889                 nil)))
5890         number headers header)
5891     (save-excursion
5892       (set-buffer nntp-server-buffer)
5893       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5894       ;; Allow the user to mangle the headers before parsing them.
5895       (gnus-run-hooks 'gnus-parse-headers-hook)
5896       (goto-char (point-min))
5897       (gnus-parse-without-error
5898         (while (and (or sequence allp)
5899                     (not (eobp)))
5900           (setq number (read cur))
5901           (when (not allp)
5902             (while (and sequence
5903                         (< (car sequence) number))
5904               (setq sequence (cdr sequence))))
5905           (when (and (or allp
5906                          (and sequence
5907                               (eq number (car sequence))))
5908                      (progn
5909                        (setq sequence (cdr sequence))
5910                        (setq header (inline
5911                                       (gnus-nov-parse-line
5912                                        number dependencies force-new)))))
5913             (push header headers))
5914           (forward-line 1)))
5915       ;; A common bug in inn is that if you have posted an article and
5916       ;; then retrieves the active file, it will answer correctly --
5917       ;; the new article is included.  However, a NOV entry for the
5918       ;; article may not have been generated yet, so this may fail.
5919       ;; We work around this problem by retrieving the last few
5920       ;; headers using HEAD.
5921       (if (or (not also-fetch-heads)
5922               (not sequence))
5923           ;; We (probably) got all the headers.
5924           (nreverse headers)
5925         (let ((gnus-nov-is-evil t))
5926           (nconc
5927            (nreverse headers)
5928            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5929              (gnus-get-newsgroup-headers))))))))
5930
5931 (defun gnus-article-get-xrefs ()
5932   "Fill in the Xref value in `gnus-current-headers', if necessary.
5933 This is meant to be called in `gnus-article-internal-prepare-hook'."
5934   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5935                                  gnus-current-headers)))
5936     (or (not gnus-use-cross-reference)
5937         (not headers)
5938         (and (mail-header-xref headers)
5939              (not (string= (mail-header-xref headers) "")))
5940         (let ((case-fold-search t)
5941               xref)
5942           (save-restriction
5943             (nnheader-narrow-to-headers)
5944             (goto-char (point-min))
5945             (when (or (and (not (eobp))
5946                            (eq (downcase (char-after)) ?x)
5947                            (looking-at "Xref:"))
5948                       (search-forward "\nXref:" nil t))
5949               (goto-char (1+ (match-end 0)))
5950               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5951               (mail-header-set-xref headers xref)))))))
5952
5953 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5954   "Find article ID and insert the summary line for that article.
5955 OLD-HEADER can either be a header or a line number to insert
5956 the subject line on."
5957   (let* ((line (and (numberp old-header) old-header))
5958          (old-header (and (vectorp old-header) old-header))
5959          (header (cond ((and old-header use-old-header)
5960                         old-header)
5961                        ((and (numberp id)
5962                              (gnus-number-to-header id))
5963                         (gnus-number-to-header id))
5964                        (t
5965                         (gnus-read-header id))))
5966          (number (and (numberp id) id))
5967          d)
5968     (when header
5969       ;; Rebuild the thread that this article is part of and go to the
5970       ;; article we have fetched.
5971       (when (and (not gnus-show-threads)
5972                  old-header)
5973         (when (and number
5974                    (setq d (gnus-data-find (mail-header-number old-header))))
5975           (goto-char (gnus-data-pos d))
5976           (gnus-data-remove
5977            number
5978            (- (gnus-point-at-bol)
5979               (prog1
5980                   (1+ (gnus-point-at-eol))
5981                 (gnus-delete-line))))))
5982       (when old-header
5983         (mail-header-set-number header (mail-header-number old-header)))
5984       (setq gnus-newsgroup-sparse
5985             (delq (setq number (mail-header-number header))
5986                   gnus-newsgroup-sparse))
5987       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5988       (push number gnus-newsgroup-limit)
5989       (gnus-rebuild-thread (mail-header-id header) line)
5990       (gnus-summary-goto-subject number nil t))
5991     (when (and (numberp number)
5992                (> number 0))
5993       ;; We have to update the boundaries even if we can't fetch the
5994       ;; article if ID is a number -- so that the next `P' or `N'
5995       ;; command will fetch the previous (or next) article even
5996       ;; if the one we tried to fetch this time has been canceled.
5997       (when (> number gnus-newsgroup-end)
5998         (setq gnus-newsgroup-end number))
5999       (when (< number gnus-newsgroup-begin)
6000         (setq gnus-newsgroup-begin number))
6001       (setq gnus-newsgroup-unselected
6002             (delq number gnus-newsgroup-unselected)))
6003     ;; Report back a success?
6004     (and header (mail-header-number header))))
6005
6006 ;;; Process/prefix in the summary buffer
6007
6008 (defun gnus-summary-work-articles (n)
6009   "Return a list of articles to be worked upon.
6010 The prefix argument, the list of process marked articles, and the
6011 current article will be taken into consideration."
6012   (save-excursion
6013     (set-buffer gnus-summary-buffer)
6014     (cond
6015      (n
6016       ;; A numerical prefix has been given.
6017       (setq n (prefix-numeric-value n))
6018       (let ((backward (< n 0))
6019             (n (abs (prefix-numeric-value n)))
6020             articles article)
6021         (save-excursion
6022           (while
6023               (and (> n 0)
6024                    (push (setq article (gnus-summary-article-number))
6025                          articles)
6026                    (if backward
6027                        (gnus-summary-find-prev nil article)
6028                      (gnus-summary-find-next nil article)))
6029             (decf n)))
6030         (nreverse articles)))
6031      ((and (gnus-region-active-p) (mark))
6032       (message "region active")
6033       ;; Work on the region between point and mark.
6034       (let ((max (max (point) (mark)))
6035             articles article)
6036         (save-excursion
6037           (goto-char (min (min (point) (mark))))
6038           (while
6039               (and
6040                (push (setq article (gnus-summary-article-number)) articles)
6041                (gnus-summary-find-next nil article)
6042                (< (point) max)))
6043           (nreverse articles))))
6044      (gnus-newsgroup-processable
6045       ;; There are process-marked articles present.
6046       ;; Save current state.
6047       (gnus-summary-save-process-mark)
6048       ;; Return the list.
6049       (reverse gnus-newsgroup-processable))
6050      (t
6051       ;; Just return the current article.
6052       (list (gnus-summary-article-number))))))
6053
6054 (defmacro gnus-summary-iterate (arg &rest forms)
6055   "Iterate over the process/prefixed articles and do FORMS.
6056 ARG is the interactive prefix given to the command.  FORMS will be
6057 executed with point over the summary line of the articles."
6058   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6059     `(let ((,articles (gnus-summary-work-articles ,arg)))
6060        (while ,articles
6061          (gnus-summary-goto-subject (car ,articles))
6062          ,@forms
6063          (pop ,articles)))))
6064
6065 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6066 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6067
6068 (defun gnus-summary-save-process-mark ()
6069   "Push the current set of process marked articles on the stack."
6070   (interactive)
6071   (push (copy-sequence gnus-newsgroup-processable)
6072         gnus-newsgroup-process-stack))
6073
6074 (defun gnus-summary-kill-process-mark ()
6075   "Push the current set of process marked articles on the stack and unmark."
6076   (interactive)
6077   (gnus-summary-save-process-mark)
6078   (gnus-summary-unmark-all-processable))
6079
6080 (defun gnus-summary-yank-process-mark ()
6081   "Pop the last process mark state off the stack and restore it."
6082   (interactive)
6083   (unless gnus-newsgroup-process-stack
6084     (error "Empty mark stack"))
6085   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6086
6087 (defun gnus-summary-process-mark-set (set)
6088   "Make SET into the current process marked articles."
6089   (gnus-summary-unmark-all-processable)
6090   (while set
6091     (gnus-summary-set-process-mark (pop set))))
6092
6093 ;;; Searching and stuff
6094
6095 (defun gnus-summary-search-group (&optional backward use-level)
6096   "Search for next unread newsgroup.
6097 If optional argument BACKWARD is non-nil, search backward instead."
6098   (save-excursion
6099     (set-buffer gnus-group-buffer)
6100     (when (gnus-group-search-forward
6101            backward nil (if use-level (gnus-group-group-level) nil))
6102       (gnus-group-group-name))))
6103
6104 (defun gnus-summary-best-group (&optional exclude-group)
6105   "Find the name of the best unread group.
6106 If EXCLUDE-GROUP, do not go to this group."
6107   (save-excursion
6108     (set-buffer gnus-group-buffer)
6109     (save-excursion
6110       (gnus-group-best-unread-group exclude-group))))
6111
6112 (defun gnus-summary-find-next (&optional unread article backward)
6113   (if backward
6114       (gnus-summary-find-prev unread article)
6115     (let* ((dummy (gnus-summary-article-intangible-p))
6116            (article (or article (gnus-summary-article-number)))
6117            (data (gnus-data-find-list article))
6118            result)
6119       (when (and (not dummy)
6120                  (or (not gnus-summary-check-current)
6121                      (not unread)
6122                      (not (gnus-data-unread-p (car data)))))
6123         (setq data (cdr data)))
6124       (when (setq result
6125                   (if unread
6126                       (progn
6127                         (while data
6128                           (unless (memq (gnus-data-number (car data))
6129                                         (cond
6130                                          ((eq gnus-auto-goto-ignores
6131                                               'always-undownloaded)
6132                                           gnus-newsgroup-undownloaded)
6133                                          (gnus-plugged
6134                                           nil)
6135                                          ((eq gnus-auto-goto-ignores
6136                                               'unfetched)
6137                                           gnus-newsgroup-unfetched)
6138                                          ((eq gnus-auto-goto-ignores
6139                                               'undownloaded)
6140                                           gnus-newsgroup-undownloaded)))
6141                             (when (gnus-data-unread-p (car data))
6142                               (setq result (car data)
6143                                     data nil)))
6144                           (setq data (cdr data)))
6145                         result)
6146                     (car data)))
6147         (goto-char (gnus-data-pos result))
6148         (gnus-data-number result)))))
6149
6150 (defun gnus-summary-find-prev (&optional unread article)
6151   (let* ((eobp (eobp))
6152          (article (or article (gnus-summary-article-number)))
6153          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6154          result)
6155     (when (and (not eobp)
6156                (or (not gnus-summary-check-current)
6157                    (not unread)
6158                    (not (gnus-data-unread-p (car data)))))
6159       (setq data (cdr data)))
6160     (when (setq result
6161                 (if unread
6162                     (progn
6163                       (while data
6164                         (unless (memq (gnus-data-number (car data))
6165                                       (cond
6166                                        ((eq gnus-auto-goto-ignores
6167                                             'always-undownloaded)
6168                                         gnus-newsgroup-undownloaded)
6169                                        (gnus-plugged
6170                                         nil)
6171                                        ((eq gnus-auto-goto-ignores
6172                                             'unfetched)
6173                                         gnus-newsgroup-unfetched)
6174                                        ((eq gnus-auto-goto-ignores
6175                                             'undownloaded)
6176                                         gnus-newsgroup-undownloaded)))
6177                           (when (gnus-data-unread-p (car data))
6178                             (setq result (car data)
6179                                   data nil)))
6180                         (setq data (cdr data)))
6181                       result)
6182                   (car data)))
6183       (goto-char (gnus-data-pos result))
6184       (gnus-data-number result))))
6185
6186 (defun gnus-summary-find-subject (subject &optional unread backward article)
6187   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6188          (article (or article (gnus-summary-article-number)))
6189          (articles (gnus-data-list backward))
6190          (arts (gnus-data-find-list article articles))
6191          result)
6192     (when (or (not gnus-summary-check-current)
6193               (not unread)
6194               (not (gnus-data-unread-p (car arts))))
6195       (setq arts (cdr arts)))
6196     (while arts
6197       (and (or (not unread)
6198                (gnus-data-unread-p (car arts)))
6199            (vectorp (gnus-data-header (car arts)))
6200            (gnus-subject-equal
6201             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6202            (setq result (car arts)
6203                  arts nil))
6204       (setq arts (cdr arts)))
6205     (and result
6206          (goto-char (gnus-data-pos result))
6207          (gnus-data-number result))))
6208
6209 (defun gnus-summary-search-forward (&optional unread subject backward)
6210   "Search forward for an article.
6211 If UNREAD, look for unread articles.  If SUBJECT, look for
6212 articles with that subject.  If BACKWARD, search backward instead."
6213   (cond (subject (gnus-summary-find-subject subject unread backward))
6214         (backward (gnus-summary-find-prev unread))
6215         (t (gnus-summary-find-next unread))))
6216
6217 (defun gnus-recenter (&optional n)
6218   "Center point in window and redisplay frame.
6219 Also do horizontal recentering."
6220   (interactive "P")
6221   (when (and gnus-auto-center-summary
6222              (not (eq gnus-auto-center-summary 'vertical)))
6223     (gnus-horizontal-recenter))
6224   (recenter n))
6225
6226 (defun gnus-summary-recenter ()
6227   "Center point in the summary window.
6228 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6229 displayed, no centering will be performed."
6230   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6231   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6232   (interactive)
6233   ;; The user has to want it.
6234   (when gnus-auto-center-summary
6235     (let* ((top (cond ((< (window-height) 4) 0)
6236                       ((< (window-height) 7) 1)
6237                       (t (if (numberp gnus-auto-center-summary)
6238                              gnus-auto-center-summary
6239                            2))))
6240            (height (1- (window-height)))
6241            (bottom (save-excursion (goto-char (point-max))
6242                                    (forward-line (- height))
6243                                    (point)))
6244            (window (get-buffer-window (current-buffer))))
6245       (when (get-buffer-window gnus-article-buffer)
6246         ;; Only do recentering when the article buffer is displayed,
6247         ;; Set the window start to either `bottom', which is the biggest
6248         ;; possible valid number, or the second line from the top,
6249         ;; whichever is the least.
6250         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6251           (if (> bottom top-pos)
6252               ;; Keep the second line from the top visible
6253               (set-window-start window top-pos t)
6254             ;; Try to keep the bottom line visible; if it's partially
6255             ;; obscured, either scroll one more line to make it fully
6256             ;; visible, or revert to using TOP-POS.
6257             (save-excursion
6258               (goto-char (point-max))
6259               (forward-line -1)
6260               (let ((last-line-start (point)))
6261                 (goto-char bottom)
6262                 (set-window-start window (point) t)
6263                 (when (not (pos-visible-in-window-p last-line-start window))
6264                   (forward-line 1)
6265                   (set-window-start window (min (point) top-pos) t)))))))
6266       ;; Do horizontal recentering while we're at it.
6267       (when (and (get-buffer-window (current-buffer) t)
6268                  (not (eq gnus-auto-center-summary 'vertical)))
6269         (let ((selected (selected-window)))
6270           (select-window (get-buffer-window (current-buffer) t))
6271           (gnus-summary-position-point)
6272           (gnus-horizontal-recenter)
6273           (select-window selected))))))
6274
6275 (defun gnus-summary-jump-to-group (newsgroup)
6276   "Move point to NEWSGROUP in group mode buffer."
6277   ;; Keep update point of group mode buffer if visible.
6278   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6279       (save-window-excursion
6280         ;; Take care of tree window mode.
6281         (when (get-buffer-window gnus-group-buffer)
6282           (pop-to-buffer gnus-group-buffer))
6283         (gnus-group-jump-to-group newsgroup))
6284     (save-excursion
6285       ;; Take care of tree window mode.
6286       (if (get-buffer-window gnus-group-buffer)
6287           (pop-to-buffer gnus-group-buffer)
6288         (set-buffer gnus-group-buffer))
6289       (gnus-group-jump-to-group newsgroup))))
6290
6291 ;; This function returns a list of article numbers based on the
6292 ;; difference between the ranges of read articles in this group and
6293 ;; the range of active articles.
6294 (defun gnus-list-of-unread-articles (group)
6295   (let* ((read (gnus-info-read (gnus-get-info group)))
6296          (active (or (gnus-active group) (gnus-activate-group group)))
6297          (last (cdr active))
6298          first nlast unread)
6299     ;; If none are read, then all are unread.
6300     (if (not read)
6301         (setq first (car active))
6302       ;; If the range of read articles is a single range, then the
6303       ;; first unread article is the article after the last read
6304       ;; article.  Sounds logical, doesn't it?
6305       (if (and (not (listp (cdr read)))
6306                (or (< (car read) (car active))
6307                    (progn (setq read (list read))
6308                           nil)))
6309           (setq first (max (car active) (1+ (cdr read))))
6310         ;; `read' is a list of ranges.
6311         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6312                                   (caar read)))
6313                   1)
6314           (setq first (car active)))
6315         (while read
6316           (when first
6317             (while (< first nlast)
6318               (push first unread)
6319               (setq first (1+ first))))
6320           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6321           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6322           (setq read (cdr read)))))
6323     ;; And add the last unread articles.
6324     (while (<= first last)
6325       (push first unread)
6326       (setq first (1+ first)))
6327     ;; Return the list of unread articles.
6328     (delq 0 (nreverse unread))))
6329
6330 (defun gnus-list-of-read-articles (group)
6331   "Return a list of unread, unticked and non-dormant articles."
6332   (let* ((info (gnus-get-info group))
6333          (marked (gnus-info-marks info))
6334          (active (gnus-active group)))
6335     (and info active
6336          (gnus-list-range-difference
6337           (gnus-list-range-difference
6338            (gnus-sorted-complement
6339             (gnus-uncompress-range active)
6340             (gnus-list-of-unread-articles group))
6341            (cdr (assq 'dormant marked)))
6342           (cdr (assq 'tick marked))))))
6343
6344 ;; Various summary commands
6345
6346 (defun gnus-summary-select-article-buffer ()
6347   "Reconfigure windows to show article buffer."
6348   (interactive)
6349   (if (not (gnus-buffer-live-p gnus-article-buffer))
6350       (error "There is no article buffer for this summary buffer")
6351     (gnus-configure-windows 'article)
6352     (select-window (get-buffer-window gnus-article-buffer))))
6353
6354 (defun gnus-summary-universal-argument (arg)
6355   "Perform any operation on all articles that are process/prefixed."
6356   (interactive "P")
6357   (let ((articles (gnus-summary-work-articles arg))
6358         func article)
6359     (if (eq
6360          (setq
6361           func
6362           (key-binding
6363            (read-key-sequence
6364             (substitute-command-keys
6365              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6366          'undefined)
6367         (gnus-error 1 "Undefined key")
6368       (save-excursion
6369         (while articles
6370           (gnus-summary-goto-subject (setq article (pop articles)))
6371           (let (gnus-newsgroup-processable)
6372             (command-execute func))
6373           (gnus-summary-remove-process-mark article)))))
6374   (gnus-summary-position-point))
6375
6376 (defun gnus-summary-toggle-truncation (&optional arg)
6377   "Toggle truncation of summary lines.
6378 With ARG, turn line truncation on if ARG is positive."
6379   (interactive "P")
6380   (setq truncate-lines
6381         (if (null arg) (not truncate-lines)
6382           (> (prefix-numeric-value arg) 0)))
6383   (redraw-display))
6384
6385 (defun gnus-summary-find-for-reselect ()
6386   "Return the number of an article to stay on across a reselect.
6387 The current article is considered, then following articles, then previous
6388 articles.  An article is sought which is not cancelled and isn't a temporary
6389 insertion from another group.  If there's no such then return a dummy 0."
6390   (let (found)
6391     (dolist (rev '(nil t))
6392       (unless found      ; don't demand the reverse list if we don't need it
6393         (let ((data (gnus-data-find-list
6394                      (gnus-summary-article-number) (gnus-data-list rev))))
6395           (while (and data (not found))
6396             (if (and (< 0 (gnus-data-number (car data)))
6397                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6398                 (setq found (gnus-data-number (car data))))
6399             (setq data (cdr data))))))
6400     (or found 0)))
6401
6402 (defun gnus-summary-reselect-current-group (&optional all rescan)
6403   "Exit and then reselect the current newsgroup.
6404 The prefix argument ALL means to select all articles."
6405   (interactive "P")
6406   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6407     (error "Ephemeral groups can't be reselected"))
6408   (let ((current-subject (gnus-summary-find-for-reselect))
6409         (group gnus-newsgroup-name))
6410     (setq gnus-newsgroup-begin nil)
6411     (gnus-summary-exit nil 'leave-hidden)
6412     ;; We have to adjust the point of group mode buffer because
6413     ;; point was moved to the next unread newsgroup by exiting.
6414     (gnus-summary-jump-to-group group)
6415     (when rescan
6416       (save-excursion
6417         (gnus-group-get-new-news-this-group 1)))
6418     (gnus-group-read-group all t)
6419     (gnus-summary-goto-subject current-subject nil t)))
6420
6421 (defun gnus-summary-rescan-group (&optional all)
6422   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6423   (interactive "P")
6424   (gnus-summary-reselect-current-group all t))
6425
6426 (defun gnus-summary-update-info (&optional non-destructive)
6427   (save-excursion
6428     (let ((group gnus-newsgroup-name))
6429       (when group
6430         (when gnus-newsgroup-kill-headers
6431           (setq gnus-newsgroup-killed
6432                 (gnus-compress-sequence
6433                  (gnus-sorted-union
6434                   (gnus-list-range-intersection
6435                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6436                   gnus-newsgroup-unreads)
6437                  t)))
6438         (unless (listp (cdr gnus-newsgroup-killed))
6439           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6440         (let ((headers gnus-newsgroup-headers))
6441           ;; Set the new ranges of read articles.
6442           (save-excursion
6443             (set-buffer gnus-group-buffer)
6444             (gnus-undo-force-boundary))
6445           (gnus-update-read-articles
6446            group (gnus-sorted-union
6447                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6448           ;; Set the current article marks.
6449           (let ((gnus-newsgroup-scored
6450                  (if (and (not gnus-save-score)
6451                           (not non-destructive))
6452                      nil
6453                    gnus-newsgroup-scored)))
6454             (save-excursion
6455               (gnus-update-marks)))
6456           ;; Do the cross-ref thing.
6457           (when gnus-use-cross-reference
6458             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6459           ;; Do not switch windows but change the buffer to work.
6460           (set-buffer gnus-group-buffer)
6461           (unless (gnus-ephemeral-group-p group)
6462             (gnus-group-update-group group)))))))
6463
6464 (defun gnus-summary-save-newsrc (&optional force)
6465   "Save the current number of read/marked articles in the dribble buffer.
6466 The dribble buffer will then be saved.
6467 If FORCE (the prefix), also save the .newsrc file(s)."
6468   (interactive "P")
6469   (gnus-summary-update-info t)
6470   (if force
6471       (gnus-save-newsrc-file)
6472     (gnus-dribble-save)))
6473
6474 (defun gnus-summary-exit (&optional temporary leave-hidden)
6475   "Exit reading current newsgroup, and then return to group selection mode.
6476 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6477   (interactive)
6478   (gnus-set-global-variables)
6479   (when (gnus-buffer-live-p gnus-article-buffer)
6480     (save-excursion
6481       (set-buffer gnus-article-buffer)
6482       (mm-destroy-parts gnus-article-mime-handles)
6483       ;; Set it to nil for safety reason.
6484       (setq gnus-article-mime-handle-alist nil)
6485       (setq gnus-article-mime-handles nil)))
6486   (gnus-kill-save-kill-buffer)
6487   (gnus-async-halt-prefetch)
6488   (let* ((group gnus-newsgroup-name)
6489          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6490          (gnus-group-is-exiting-p t)
6491          (mode major-mode)
6492          (group-point nil)
6493          (buf (current-buffer)))
6494     (unless quit-config
6495       ;; Do adaptive scoring, and possibly save score files.
6496       (when gnus-newsgroup-adaptive
6497         (gnus-score-adaptive))
6498       (when gnus-use-scoring
6499         (gnus-score-save)))
6500     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6501     ;; If we have several article buffers, we kill them at exit.
6502     (unless gnus-single-article-buffer
6503       (gnus-kill-buffer gnus-original-article-buffer)
6504       (setq gnus-article-current nil))
6505     (when gnus-use-cache
6506       (gnus-cache-possibly-remove-articles)
6507       (gnus-cache-save-buffers))
6508     (gnus-async-prefetch-remove-group group)
6509     (when gnus-suppress-duplicates
6510       (gnus-dup-enter-articles))
6511     (when gnus-use-trees
6512       (gnus-tree-close group))
6513     (when gnus-use-cache
6514       (gnus-cache-write-active))
6515     ;; Remove entries for this group.
6516     (nnmail-purge-split-history (gnus-group-real-name group))
6517     ;; Make all changes in this group permanent.
6518     (unless quit-config
6519       (gnus-run-hooks 'gnus-exit-group-hook)
6520       (gnus-summary-update-info))
6521     (gnus-close-group group)
6522     ;; Make sure where we were, and go to next newsgroup.
6523     (set-buffer gnus-group-buffer)
6524     (unless quit-config
6525       (gnus-group-jump-to-group group))
6526     (gnus-run-hooks 'gnus-summary-exit-hook)
6527     (unless (or quit-config
6528                 ;; If this group has disappeared from the summary
6529                 ;; buffer, don't skip forwards.
6530                 (not (string= group (gnus-group-group-name))))
6531       (gnus-group-next-unread-group 1))
6532     (setq group-point (point))
6533     (if temporary
6534         nil                             ;Nothing to do.
6535       ;; If we have several article buffers, we kill them at exit.
6536       (unless gnus-single-article-buffer
6537         (gnus-kill-buffer gnus-article-buffer)
6538         (gnus-kill-buffer gnus-original-article-buffer)
6539         (setq gnus-article-current nil))
6540       (set-buffer buf)
6541       (if (not gnus-kill-summary-on-exit)
6542           (progn
6543             (gnus-deaden-summary)
6544             (setq mode nil))
6545         ;; We set all buffer-local variables to nil.  It is unclear why
6546         ;; this is needed, but if we don't, buffer-local variables are
6547         ;; not garbage-collected, it seems.  This would the lead to en
6548         ;; ever-growing Emacs.
6549         (gnus-summary-clear-local-variables)
6550         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6551           (gnus-summary-clear-local-variables))
6552         (when (get-buffer gnus-article-buffer)
6553           (bury-buffer gnus-article-buffer))
6554         ;; We clear the global counterparts of the buffer-local
6555         ;; variables as well, just to be on the safe side.
6556         (set-buffer gnus-group-buffer)
6557         (gnus-summary-clear-local-variables)
6558         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6559           (gnus-summary-clear-local-variables))
6560         ;; Return to group mode buffer.
6561         (when (eq mode 'gnus-summary-mode)
6562           (gnus-kill-buffer buf)))
6563       (setq gnus-current-select-method gnus-select-method)
6564       (if leave-hidden
6565           (set-buffer gnus-group-buffer)
6566         (pop-to-buffer gnus-group-buffer))
6567       (if (not quit-config)
6568           (progn
6569             (goto-char group-point)
6570             (unless leave-hidden
6571               (gnus-configure-windows 'group 'force)))
6572         (gnus-handle-ephemeral-exit quit-config))
6573       ;; Clear the current group name.
6574       (unless quit-config
6575         (setq gnus-newsgroup-name nil)))))
6576
6577 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6578 (defun gnus-summary-exit-no-update (&optional no-questions)
6579   "Quit reading current newsgroup without updating read article info."
6580   (interactive)
6581   (let* ((group gnus-newsgroup-name)
6582          (gnus-group-is-exiting-p t)
6583          (gnus-group-is-exiting-without-update-p t)
6584          (quit-config (gnus-group-quit-config group)))
6585     (when (or no-questions
6586               gnus-expert-user
6587               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6588       (gnus-async-halt-prefetch)
6589       (run-hooks 'gnus-summary-prepare-exit-hook)
6590       (when (gnus-buffer-live-p gnus-article-buffer)
6591         (save-excursion
6592           (set-buffer gnus-article-buffer)
6593           (mm-destroy-parts gnus-article-mime-handles)
6594           ;; Set it to nil for safety reason.
6595           (setq gnus-article-mime-handle-alist nil)
6596           (setq gnus-article-mime-handles nil)))
6597       ;; If we have several article buffers, we kill them at exit.
6598       (unless gnus-single-article-buffer
6599         (gnus-kill-buffer gnus-article-buffer)
6600         (gnus-kill-buffer gnus-original-article-buffer)
6601         (setq gnus-article-current nil))
6602       (if (not gnus-kill-summary-on-exit)
6603           (gnus-deaden-summary)
6604         (gnus-close-group group)
6605         (gnus-summary-clear-local-variables)
6606         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6607           (gnus-summary-clear-local-variables))
6608         (set-buffer gnus-group-buffer)
6609         (gnus-summary-clear-local-variables)
6610         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6611           (gnus-summary-clear-local-variables))
6612         (gnus-kill-buffer gnus-summary-buffer))
6613       (unless gnus-single-article-buffer
6614         (setq gnus-article-current nil))
6615       (when gnus-use-trees
6616         (gnus-tree-close group))
6617       (gnus-async-prefetch-remove-group group)
6618       (when (get-buffer gnus-article-buffer)
6619         (bury-buffer gnus-article-buffer))
6620       ;; Return to the group buffer.
6621       (gnus-configure-windows 'group 'force)
6622       ;; Clear the current group name.
6623       (setq gnus-newsgroup-name nil)
6624       (unless (gnus-ephemeral-group-p group)
6625         (gnus-group-update-group group))
6626       (when (equal (gnus-group-group-name) group)
6627         (gnus-group-next-unread-group 1))
6628       (when quit-config
6629         (gnus-handle-ephemeral-exit quit-config)))))
6630
6631 (defun gnus-handle-ephemeral-exit (quit-config)
6632   "Handle movement when leaving an ephemeral group.
6633 The state which existed when entering the ephemeral is reset."
6634   (if (not (buffer-name (car quit-config)))
6635       (gnus-configure-windows 'group 'force)
6636     (set-buffer (car quit-config))
6637     (cond ((eq major-mode 'gnus-summary-mode)
6638            (gnus-set-global-variables))
6639           ((eq major-mode 'gnus-article-mode)
6640            (save-excursion
6641              ;; The `gnus-summary-buffer' variable may point
6642              ;; to the old summary buffer when using a single
6643              ;; article buffer.
6644              (unless (gnus-buffer-live-p gnus-summary-buffer)
6645                (set-buffer gnus-group-buffer))
6646              (set-buffer gnus-summary-buffer)
6647              (gnus-set-global-variables))))
6648     (if (or (eq (cdr quit-config) 'article)
6649             (eq (cdr quit-config) 'pick))
6650         (progn
6651           ;; The current article may be from the ephemeral group
6652           ;; thus it is best that we reload this article
6653           ;;
6654           ;; If we're exiting from a large digest, this can be
6655           ;; extremely slow.  So, it's better not to reload it. -- jh.
6656           ;;(gnus-summary-show-article)
6657           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6658               (gnus-configure-windows 'pick 'force)
6659             (gnus-configure-windows (cdr quit-config) 'force)))
6660       (gnus-configure-windows (cdr quit-config) 'force))
6661     (when (eq major-mode 'gnus-summary-mode)
6662       (gnus-summary-next-subject 1 nil t)
6663       (gnus-summary-recenter)
6664       (gnus-summary-position-point))))
6665
6666 ;;; Dead summaries.
6667
6668 (defvar gnus-dead-summary-mode-map nil)
6669
6670 (unless gnus-dead-summary-mode-map
6671   (setq gnus-dead-summary-mode-map (make-keymap))
6672   (suppress-keymap gnus-dead-summary-mode-map)
6673   (substitute-key-definition
6674    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6675   (dolist (key '("\C-d" "\r" "\177" [delete]))
6676     (define-key gnus-dead-summary-mode-map
6677       key 'gnus-summary-wake-up-the-dead))
6678   (dolist (key '("q" "Q"))
6679     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6680
6681 (defvar gnus-dead-summary-mode nil
6682   "Minor mode for Gnus summary buffers.")
6683
6684 (defun gnus-dead-summary-mode (&optional arg)
6685   "Minor mode for Gnus summary buffers."
6686   (interactive "P")
6687   (when (eq major-mode 'gnus-summary-mode)
6688     (make-local-variable 'gnus-dead-summary-mode)
6689     (setq gnus-dead-summary-mode
6690           (if (null arg) (not gnus-dead-summary-mode)
6691             (> (prefix-numeric-value arg) 0)))
6692     (when gnus-dead-summary-mode
6693       (gnus-add-minor-mode
6694        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6695
6696 (defun gnus-deaden-summary ()
6697   "Make the current summary buffer into a dead summary buffer."
6698   ;; Kill any previous dead summary buffer.
6699   (when (and gnus-dead-summary
6700              (buffer-name gnus-dead-summary))
6701     (save-excursion
6702       (set-buffer gnus-dead-summary)
6703       (when gnus-dead-summary-mode
6704         (kill-buffer (current-buffer)))))
6705   ;; Make this the current dead summary.
6706   (setq gnus-dead-summary (current-buffer))
6707   (gnus-dead-summary-mode 1)
6708   (let ((name (buffer-name)))
6709     (when (string-match "Summary" name)
6710       (rename-buffer
6711        (concat (substring name 0 (match-beginning 0)) "Dead "
6712                (substring name (match-beginning 0)))
6713        t)
6714       (bury-buffer))))
6715
6716 (defun gnus-kill-or-deaden-summary (buffer)
6717   "Kill or deaden the summary BUFFER."
6718   (save-excursion
6719     (when (and (buffer-name buffer)
6720                (not gnus-single-article-buffer))
6721       (save-excursion
6722         (set-buffer buffer)
6723         (gnus-kill-buffer gnus-article-buffer)
6724         (gnus-kill-buffer gnus-original-article-buffer)))
6725     (cond
6726      ;; Kill the buffer.
6727      (gnus-kill-summary-on-exit
6728       (when (and gnus-use-trees
6729                  (gnus-buffer-exists-p buffer))
6730         (save-excursion
6731           (set-buffer buffer)
6732           (gnus-tree-close gnus-newsgroup-name)))
6733       (gnus-kill-buffer buffer))
6734      ;; Deaden the buffer.
6735      ((gnus-buffer-exists-p buffer)
6736       (save-excursion
6737         (set-buffer buffer)
6738         (gnus-deaden-summary))))))
6739
6740 (defun gnus-summary-wake-up-the-dead (&rest args)
6741   "Wake up the dead summary buffer."
6742   (interactive)
6743   (gnus-dead-summary-mode -1)
6744   (let ((name (buffer-name)))
6745     (when (string-match "Dead " name)
6746       (rename-buffer
6747        (concat (substring name 0 (match-beginning 0))
6748                (substring name (match-end 0)))
6749        t)))
6750   (gnus-message 3 "This dead summary is now alive again"))
6751
6752 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6753 (defun gnus-summary-fetch-faq (&optional faq-dir)
6754   "Fetch the FAQ for the current group.
6755 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6756 in."
6757   (interactive
6758    (list
6759     (when current-prefix-arg
6760       (completing-read
6761        "FAQ dir: " (and (listp gnus-group-faq-directory)
6762                         (mapcar (lambda (file) (list file))
6763                                 gnus-group-faq-directory))))))
6764   (let (gnus-faq-buffer)
6765     (when (setq gnus-faq-buffer
6766                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6767       (gnus-configure-windows 'summary-faq))))
6768
6769 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6770 (defun gnus-summary-describe-group (&optional force)
6771   "Describe the current newsgroup."
6772   (interactive "P")
6773   (gnus-group-describe-group force gnus-newsgroup-name))
6774
6775 (defun gnus-summary-describe-briefly ()
6776   "Describe summary mode commands briefly."
6777   (interactive)
6778   (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")))
6779
6780 ;; Walking around group mode buffer from summary mode.
6781
6782 (defun gnus-summary-next-group (&optional no-article target-group backward)
6783   "Exit current newsgroup and then select next unread newsgroup.
6784 If prefix argument NO-ARTICLE is non-nil, no article is selected
6785 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6786 previous group instead."
6787   (interactive "P")
6788   ;; Stop pre-fetching.
6789   (gnus-async-halt-prefetch)
6790   (let ((current-group gnus-newsgroup-name)
6791         (current-buffer (current-buffer))
6792         entered)
6793     ;; First we semi-exit this group to update Xrefs and all variables.
6794     ;; We can't do a real exit, because the window conf must remain
6795     ;; the same in case the user is prompted for info, and we don't
6796     ;; want the window conf to change before that...
6797     (gnus-summary-exit t)
6798     (while (not entered)
6799       ;; Then we find what group we are supposed to enter.
6800       (set-buffer gnus-group-buffer)
6801       (gnus-group-jump-to-group current-group)
6802       (setq target-group
6803             (or target-group
6804                 (if (eq gnus-keep-same-level 'best)
6805                     (gnus-summary-best-group gnus-newsgroup-name)
6806                   (gnus-summary-search-group backward gnus-keep-same-level))))
6807       (if (not target-group)
6808           ;; There are no further groups, so we return to the group
6809           ;; buffer.
6810           (progn
6811             (gnus-message 5 "Returning to the group buffer")
6812             (setq entered t)
6813             (when (gnus-buffer-live-p current-buffer)
6814               (set-buffer current-buffer)
6815               (gnus-summary-exit))
6816             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6817         ;; We try to enter the target group.
6818         (gnus-group-jump-to-group target-group)
6819         (let ((unreads (gnus-group-group-unread)))
6820           (if (and (or (eq t unreads)
6821                        (and unreads (not (zerop unreads))))
6822                    (gnus-summary-read-group
6823                     target-group nil no-article
6824                     (and (buffer-name current-buffer) current-buffer)
6825                     nil backward))
6826               (setq entered t)
6827             (setq current-group target-group
6828                   target-group nil)))))))
6829
6830 (defun gnus-summary-prev-group (&optional no-article)
6831   "Exit current newsgroup and then select previous unread newsgroup.
6832 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6833   (interactive "P")
6834   (gnus-summary-next-group no-article nil t))
6835
6836 ;; Walking around summary lines.
6837
6838 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6839   "Go to the first subject satisfying any non-nil constraint.
6840 If UNREAD is non-nil, the article should be unread.
6841 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6842 If UNSEEN is non-nil, the article should be unseen.
6843 Returns the article selected or nil if there are no matching articles."
6844   (interactive "P")
6845   (cond
6846    ;; Empty summary.
6847    ((null gnus-newsgroup-data)
6848     (gnus-message 3 "No articles in the group")
6849     nil)
6850    ;; Pick the first article.
6851    ((not (or unread undownloaded unseen))
6852     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6853     (gnus-data-number (car gnus-newsgroup-data)))
6854    ;; Find the first unread article.
6855    (t
6856     (let ((data gnus-newsgroup-data))
6857       (while (and data
6858                   (let ((num (gnus-data-number (car data))))
6859                     (or (memq num gnus-newsgroup-unfetched)
6860                         (not (or (and unread
6861                                       (memq num gnus-newsgroup-unreads))
6862                                  (and undownloaded
6863                                       (memq num gnus-newsgroup-undownloaded))
6864                                  (and unseen
6865                                       (memq num gnus-newsgroup-unseen)))))))
6866         (setq data (cdr data)))
6867       (prog1
6868           (if data
6869               (progn
6870                 (goto-char (gnus-data-pos (car data)))
6871                 (gnus-data-number (car data)))
6872             (gnus-message 3 "No more%s articles"
6873                           (let* ((r (when unread " unread"))
6874                                  (d (when undownloaded " undownloaded"))
6875                                  (s (when unseen " unseen"))
6876                                  (l (delq nil (list r d s))))
6877                             (cond ((= 3 (length l))
6878                                    (concat r "," d ", or" s))
6879                                   ((= 2 (length l))
6880                                    (concat (car l) ", or" (cadr l)))
6881                                   ((= 1 (length l))
6882                                    (car l))
6883                                   (t
6884                                    ""))))
6885             nil
6886             )
6887         (gnus-summary-position-point))))))
6888
6889 (defun gnus-summary-next-subject (n &optional unread dont-display)
6890   "Go to next N'th summary line.
6891 If N is negative, go to the previous N'th subject line.
6892 If UNREAD is non-nil, only unread articles are selected.
6893 The difference between N and the actual number of steps taken is
6894 returned."
6895   (interactive "p")
6896   (let ((backward (< n 0))
6897         (n (abs n)))
6898     (while (and (> n 0)
6899                 (if backward
6900                     (gnus-summary-find-prev unread)
6901                   (gnus-summary-find-next unread)))
6902       (unless (zerop (setq n (1- n)))
6903         (gnus-summary-show-thread)))
6904     (when (/= 0 n)
6905       (gnus-message 7 "No more%s articles"
6906                     (if unread " unread" "")))
6907     (unless dont-display
6908       (gnus-summary-recenter)
6909       (gnus-summary-position-point))
6910     n))
6911
6912 (defun gnus-summary-next-unread-subject (n)
6913   "Go to next N'th unread summary line."
6914   (interactive "p")
6915   (gnus-summary-next-subject n t))
6916
6917 (defun gnus-summary-prev-subject (n &optional unread)
6918   "Go to previous N'th summary line.
6919 If optional argument UNREAD is non-nil, only unread article is selected."
6920   (interactive "p")
6921   (gnus-summary-next-subject (- n) unread))
6922
6923 (defun gnus-summary-prev-unread-subject (n)
6924   "Go to previous N'th unread summary line."
6925   (interactive "p")
6926   (gnus-summary-next-subject (- n) t))
6927
6928 (defun gnus-summary-goto-subjects (articles)
6929   "Insert the subject header for ARTICLES in the current buffer."
6930   (save-excursion
6931     (dolist (article articles)
6932       (gnus-summary-goto-subject article t)))
6933   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6934   (gnus-summary-position-point))
6935  
6936 (defun gnus-summary-goto-subject (article &optional force silent)
6937   "Go the subject line of ARTICLE.
6938 If FORCE, also allow jumping to articles not currently shown."
6939   (interactive "nArticle number: ")
6940   (unless (numberp article)
6941     (error "Article %s is not a number" article))
6942   (let ((b (point))
6943         (data (gnus-data-find article)))
6944     ;; We read in the article if we have to.
6945     (and (not data)
6946          force
6947          (gnus-summary-insert-subject
6948           article
6949           (if (or (numberp force) (vectorp force)) force)
6950           t)
6951          (setq data (gnus-data-find article)))
6952     (goto-char b)
6953     (if (not data)
6954         (progn
6955           (unless silent
6956             (gnus-message 3 "Can't find article %d" article))
6957           nil)
6958       (let ((pt (gnus-data-pos data)))
6959         (goto-char pt)
6960         (gnus-summary-set-article-display-arrow pt))
6961       (gnus-summary-position-point)
6962       article)))
6963
6964 ;; Walking around summary lines with displaying articles.
6965
6966 (defun gnus-summary-expand-window (&optional arg)
6967   "Make the summary buffer take up the entire Emacs frame.
6968 Given a prefix, will force an `article' buffer configuration."
6969   (interactive "P")
6970   (if arg
6971       (gnus-configure-windows 'article 'force)
6972     (gnus-configure-windows 'summary 'force)))
6973
6974 (defun gnus-summary-display-article (article &optional all-header)
6975   "Display ARTICLE in article buffer."
6976   (when (gnus-buffer-live-p gnus-article-buffer)
6977     (with-current-buffer gnus-article-buffer
6978       (mm-enable-multibyte)))
6979   (gnus-set-global-variables)
6980   (when (gnus-buffer-live-p gnus-article-buffer)
6981     (with-current-buffer gnus-article-buffer
6982       (setq gnus-article-charset gnus-newsgroup-charset)
6983       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6984       (mm-enable-multibyte)))
6985   (if (null article)
6986       nil
6987     (prog1
6988         (if gnus-summary-display-article-function
6989             (funcall gnus-summary-display-article-function article all-header)
6990           (gnus-article-prepare article all-header))
6991       (gnus-run-hooks 'gnus-select-article-hook)
6992       (when (and gnus-current-article
6993                  (not (zerop gnus-current-article)))
6994         (gnus-summary-goto-subject gnus-current-article))
6995       (gnus-summary-recenter)
6996       (when (and gnus-use-trees gnus-show-threads)
6997         (gnus-possibly-generate-tree article)
6998         (gnus-highlight-selected-tree article))
6999       ;; Successfully display article.
7000       (gnus-article-set-window-start
7001        (cdr (assq article gnus-newsgroup-bookmarks))))))
7002
7003 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7004   "Select the current article.
7005 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7006 non-nil, the article will be re-fetched even if it already present in
7007 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7008 be displayed."
7009   ;; Make sure we are in the summary buffer to work around bbdb bug.
7010   (unless (eq major-mode 'gnus-summary-mode)
7011     (set-buffer gnus-summary-buffer))
7012   (let ((article (or article (gnus-summary-article-number)))
7013         (all-headers (not (not all-headers))) ;Must be t or nil.
7014         gnus-summary-display-article-function)
7015     (and (not pseudo)
7016          (gnus-summary-article-pseudo-p article)
7017          (error "This is a pseudo-article"))
7018     (save-excursion
7019       (set-buffer gnus-summary-buffer)
7020       (if (or (and gnus-single-article-buffer
7021                    (or (null gnus-current-article)
7022                        (null gnus-article-current)
7023                        (null (get-buffer gnus-article-buffer))
7024                        (not (eq article (cdr gnus-article-current)))
7025                        (not (equal (car gnus-article-current)
7026                                    gnus-newsgroup-name))))
7027               (and (not gnus-single-article-buffer)
7028                    (or (null gnus-current-article)
7029                        (not (eq gnus-current-article article))))
7030               force)
7031           ;; The requested article is different from the current article.
7032           (progn
7033             (gnus-summary-display-article article all-headers)
7034             (when (gnus-buffer-live-p gnus-article-buffer)
7035               (with-current-buffer gnus-article-buffer
7036                 (if (not gnus-article-decoded-p) ;; a local variable
7037                     (mm-disable-multibyte))))
7038             (gnus-article-set-window-start
7039              (cdr (assq article gnus-newsgroup-bookmarks)))
7040             article)
7041         'old))))
7042
7043 (defun gnus-summary-force-verify-and-decrypt ()
7044   "Display buttons for signed/encrypted parts and verify/decrypt them."
7045   (interactive)
7046   (let ((mm-verify-option 'known)
7047         (mm-decrypt-option 'known)
7048         (gnus-article-emulate-mime t)
7049         (gnus-buttonized-mime-types (append (list "multipart/signed"
7050                                                   "multipart/encrypted")
7051                                             gnus-buttonized-mime-types)))
7052     (gnus-summary-select-article nil 'force)))
7053
7054 (defun gnus-summary-set-current-mark (&optional current-mark)
7055   "Obsolete function."
7056   nil)
7057
7058 (defun gnus-summary-next-article (&optional unread subject backward push)
7059   "Select the next article.
7060 If UNREAD, only unread articles are selected.
7061 If SUBJECT, only articles with SUBJECT are selected.
7062 If BACKWARD, the previous article is selected instead of the next."
7063   (interactive "P")
7064   (cond
7065    ;; Is there such an article?
7066    ((and (gnus-summary-search-forward unread subject backward)
7067          (or (gnus-summary-display-article (gnus-summary-article-number))
7068              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7069     (gnus-summary-position-point))
7070    ;; If not, we try the first unread, if that is wanted.
7071    ((and subject
7072          gnus-auto-select-same
7073          (gnus-summary-first-unread-article))
7074     (gnus-summary-position-point)
7075     (gnus-message 6 "Wrapped"))
7076    ;; Try to get next/previous article not displayed in this group.
7077    ((and gnus-auto-extend-newsgroup
7078          (not unread) (not subject))
7079     (gnus-summary-goto-article
7080      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7081      nil (count-lines (point-min) (point))))
7082    ;; Go to next/previous group.
7083    (t
7084     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7085       (gnus-summary-jump-to-group gnus-newsgroup-name))
7086     (let ((cmd last-command-char)
7087           (point
7088            (save-excursion
7089              (set-buffer gnus-group-buffer)
7090              (point)))
7091           (group
7092            (if (eq gnus-keep-same-level 'best)
7093                (gnus-summary-best-group gnus-newsgroup-name)
7094              (gnus-summary-search-group backward gnus-keep-same-level))))
7095       ;; For some reason, the group window gets selected.  We change
7096       ;; it back.
7097       (select-window (get-buffer-window (current-buffer)))
7098       ;; Select next unread newsgroup automagically.
7099       (cond
7100        ((or (not gnus-auto-select-next)
7101             (not cmd))
7102         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7103        ((or (eq gnus-auto-select-next 'quietly)
7104             (and (eq gnus-auto-select-next 'slightly-quietly)
7105                  push)
7106             (and (eq gnus-auto-select-next 'almost-quietly)
7107                  (gnus-summary-last-article-p)))
7108         ;; Select quietly.
7109         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7110             (gnus-summary-exit)
7111           (gnus-message 7 "No more%s articles (%s)..."
7112                         (if unread " unread" "")
7113                         (if group (concat "selecting " group)
7114                           "exiting"))
7115           (gnus-summary-next-group nil group backward)))
7116        (t
7117         (when (gnus-key-press-event-p last-input-event)
7118           (gnus-summary-walk-group-buffer
7119            gnus-newsgroup-name cmd unread backward point))))))))
7120
7121 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7122   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7123                       (?\C-p (gnus-group-prev-unread-group 1))))
7124         (cursor-in-echo-area t)
7125         keve key group ended prompt)
7126     (save-excursion
7127       (set-buffer gnus-group-buffer)
7128       (goto-char start)
7129       (setq group
7130             (if (eq gnus-keep-same-level 'best)
7131                 (gnus-summary-best-group gnus-newsgroup-name)
7132               (gnus-summary-search-group backward gnus-keep-same-level))))
7133     (while (not ended)
7134       (setq prompt
7135             (format
7136              "No more%s articles%s " (if unread " unread" "")
7137              (if (and group
7138                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7139                  (format " (Type %s for %s [%s])"
7140                          (single-key-description cmd) group
7141                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7142                (format " (Type %s to exit %s)"
7143                        (single-key-description cmd)
7144                        gnus-newsgroup-name))))
7145       ;; Confirm auto selection.
7146       (setq key (car (setq keve (gnus-read-event-char prompt)))
7147             ended t)
7148       (cond
7149        ((assq key keystrokes)
7150         (let ((obuf (current-buffer)))
7151           (switch-to-buffer gnus-group-buffer)
7152           (when group
7153             (gnus-group-jump-to-group group))
7154           (eval (cadr (assq key keystrokes)))
7155           (setq group (gnus-group-group-name))
7156           (switch-to-buffer obuf))
7157         (setq ended nil))
7158        ((equal key cmd)
7159         (if (or (not group)
7160                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7161             (gnus-summary-exit)
7162           (gnus-summary-next-group nil group backward)))
7163        (t
7164         (push (cdr keve) unread-command-events))))))
7165
7166 (defun gnus-summary-next-unread-article ()
7167   "Select unread article after current one."
7168   (interactive)
7169   (gnus-summary-next-article
7170    (or (not (eq gnus-summary-goto-unread 'never))
7171        (gnus-summary-last-article-p (gnus-summary-article-number)))
7172    (and gnus-auto-select-same
7173         (gnus-summary-article-subject))))
7174
7175 (defun gnus-summary-prev-article (&optional unread subject)
7176   "Select the article after the current one.
7177 If UNREAD is non-nil, only unread articles are selected."
7178   (interactive "P")
7179   (gnus-summary-next-article unread subject t))
7180
7181 (defun gnus-summary-prev-unread-article ()
7182   "Select unread article before current one."
7183   (interactive)
7184   (gnus-summary-prev-article
7185    (or (not (eq gnus-summary-goto-unread 'never))
7186        (gnus-summary-first-article-p (gnus-summary-article-number)))
7187    (and gnus-auto-select-same
7188         (gnus-summary-article-subject))))
7189
7190 (defun gnus-summary-next-page (&optional lines circular stop)
7191   "Show next page of the selected article.
7192 If at the end of the current article, select the next article.
7193 LINES says how many lines should be scrolled up.
7194
7195 If CIRCULAR is non-nil, go to the start of the article instead of
7196 selecting the next article when reaching the end of the current
7197 article.
7198
7199 If STOP is non-nil, just stop when reaching the end of the message.
7200
7201 Also see the variable `gnus-article-skip-boring'."
7202   (interactive "P")
7203   (setq gnus-summary-buffer (current-buffer))
7204   (gnus-set-global-variables)
7205   (let ((article (gnus-summary-article-number))
7206         (article-window (get-buffer-window gnus-article-buffer t))
7207         endp)
7208     ;; If the buffer is empty, we have no article.
7209     (unless article
7210       (error "No article to select"))
7211     (gnus-configure-windows 'article)
7212     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7213         (if (and (eq gnus-summary-goto-unread 'never)
7214                  (not (gnus-summary-last-article-p article)))
7215             (gnus-summary-next-article)
7216           (gnus-summary-next-unread-article))
7217       (if (or (null gnus-current-article)
7218               (null gnus-article-current)
7219               (/= article (cdr gnus-article-current))
7220               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7221           ;; Selected subject is different from current article's.
7222           (gnus-summary-display-article article)
7223         (when article-window
7224           (gnus-eval-in-buffer-window gnus-article-buffer
7225             (setq endp (or (gnus-article-next-page lines)
7226                            (gnus-article-only-boring-p))))
7227           (when endp
7228             (cond (stop
7229                    (gnus-message 3 "End of message"))
7230                   (circular
7231                    (gnus-summary-beginning-of-article))
7232                   (lines
7233                    (gnus-message 3 "End of message"))
7234                   ((null lines)
7235                    (if (and (eq gnus-summary-goto-unread 'never)
7236                             (not (gnus-summary-last-article-p article)))
7237                        (gnus-summary-next-article)
7238                      (gnus-summary-next-unread-article))))))))
7239     (gnus-summary-recenter)
7240     (gnus-summary-position-point)))
7241
7242 (defun gnus-summary-prev-page (&optional lines move)
7243   "Show previous page of selected article.
7244 Argument LINES specifies lines to be scrolled down.
7245 If MOVE, move to the previous unread article if point is at
7246 the beginning of the buffer."
7247   (interactive "P")
7248   (let ((article (gnus-summary-article-number))
7249         (article-window (get-buffer-window gnus-article-buffer t))
7250         endp)
7251     (gnus-configure-windows 'article)
7252     (if (or (null gnus-current-article)
7253             (null gnus-article-current)
7254             (/= article (cdr gnus-article-current))
7255             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7256         ;; Selected subject is different from current article's.
7257         (gnus-summary-display-article article)
7258       (gnus-summary-recenter)
7259       (when article-window
7260         (gnus-eval-in-buffer-window gnus-article-buffer
7261           (setq endp (gnus-article-prev-page lines)))
7262         (when (and move endp)
7263           (cond (lines
7264                  (gnus-message 3 "Beginning of message"))
7265                 ((null lines)
7266                  (if (and (eq gnus-summary-goto-unread 'never)
7267                           (not (gnus-summary-first-article-p article)))
7268                      (gnus-summary-prev-article)
7269                    (gnus-summary-prev-unread-article))))))))
7270   (gnus-summary-position-point))
7271
7272 (defun gnus-summary-prev-page-or-article (&optional lines)
7273   "Show previous page of selected article.
7274 Argument LINES specifies lines to be scrolled down.
7275 If at the beginning of the article, go to the next article."
7276   (interactive "P")
7277   (gnus-summary-prev-page lines t))
7278
7279 (defun gnus-summary-scroll-up (lines)
7280   "Scroll up (or down) one line current article.
7281 Argument LINES specifies lines to be scrolled up (or down if negative)."
7282   (interactive "p")
7283   (gnus-configure-windows 'article)
7284   (gnus-summary-show-thread)
7285   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7286     (gnus-eval-in-buffer-window gnus-article-buffer
7287       (cond ((> lines 0)
7288              (when (gnus-article-next-page lines)
7289                (gnus-message 3 "End of message")))
7290             ((< lines 0)
7291              (gnus-article-prev-page (- lines))))))
7292   (gnus-summary-recenter)
7293   (gnus-summary-position-point))
7294
7295 (defun gnus-summary-scroll-down (lines)
7296   "Scroll down (or up) one line current article.
7297 Argument LINES specifies lines to be scrolled down (or up if negative)."
7298   (interactive "p")
7299   (gnus-summary-scroll-up (- lines)))
7300
7301 (defun gnus-summary-next-same-subject ()
7302   "Select next article which has the same subject as current one."
7303   (interactive)
7304   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7305
7306 (defun gnus-summary-prev-same-subject ()
7307   "Select previous article which has the same subject as current one."
7308   (interactive)
7309   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7310
7311 (defun gnus-summary-next-unread-same-subject ()
7312   "Select next unread article which has the same subject as current one."
7313   (interactive)
7314   (gnus-summary-next-article t (gnus-summary-article-subject)))
7315
7316 (defun gnus-summary-prev-unread-same-subject ()
7317   "Select previous unread article which has the same subject as current one."
7318   (interactive)
7319   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7320
7321 (defun gnus-summary-first-unread-article ()
7322   "Select the first unread article.
7323 Return nil if there are no unread articles."
7324   (interactive)
7325   (prog1
7326       (when (gnus-summary-first-subject t)
7327         (gnus-summary-show-thread)
7328         (gnus-summary-first-subject t)
7329         (gnus-summary-display-article (gnus-summary-article-number)))
7330     (gnus-summary-position-point)))
7331
7332 (defun gnus-summary-first-unread-subject ()
7333   "Place the point on the subject line of the first unread article.
7334 Return nil if there are no unread articles."
7335   (interactive)
7336   (prog1
7337       (when (gnus-summary-first-subject t)
7338         (gnus-summary-show-thread)
7339         (gnus-summary-first-subject t))
7340     (gnus-summary-position-point)))
7341
7342 (defun gnus-summary-first-unseen-subject ()
7343   "Place the point on the subject line of the first unseen article.
7344 Return nil if there are no unseen articles."
7345   (interactive)
7346   (prog1
7347       (when (gnus-summary-first-subject nil nil t)
7348         (gnus-summary-show-thread)
7349         (gnus-summary-first-subject nil nil t))
7350     (gnus-summary-position-point)))
7351
7352 (defun gnus-summary-first-unseen-or-unread-subject ()
7353   "Place the point on the subject line of the first unseen article or,
7354 if all article have been seen, on the subject line of the first unread
7355 article."
7356   (interactive)
7357   (prog1
7358       (unless (when (gnus-summary-first-subject nil nil t)
7359                 (gnus-summary-show-thread)
7360                 (gnus-summary-first-subject nil nil t))
7361         (when (gnus-summary-first-subject t)
7362           (gnus-summary-show-thread)
7363           (gnus-summary-first-subject t)))
7364     (gnus-summary-position-point)))
7365
7366 (defun gnus-summary-first-article ()
7367   "Select the first article.
7368 Return nil if there are no articles."
7369   (interactive)
7370   (prog1
7371       (when (gnus-summary-first-subject)
7372         (gnus-summary-show-thread)
7373         (gnus-summary-first-subject)
7374         (gnus-summary-display-article (gnus-summary-article-number)))
7375     (gnus-summary-position-point)))
7376
7377 (defun gnus-summary-best-unread-article (&optional arg)
7378   "Select the unread article with the highest score.
7379 If given a prefix argument, select the next unread article that has a
7380 score higher than the default score."
7381   (interactive "P")
7382   (let ((article (if arg
7383                      (gnus-summary-better-unread-subject)
7384                    (gnus-summary-best-unread-subject))))
7385     (if article
7386         (gnus-summary-goto-article article)
7387       (error "No unread articles"))))
7388
7389 (defun gnus-summary-best-unread-subject ()
7390   "Select the unread subject with the highest score."
7391   (interactive)
7392   (let ((best -1000000)
7393         (data gnus-newsgroup-data)
7394         article score)
7395     (while data
7396       (and (gnus-data-unread-p (car data))
7397            (> (setq score
7398                     (gnus-summary-article-score (gnus-data-number (car data))))
7399               best)
7400            (setq best score
7401                  article (gnus-data-number (car data))))
7402       (setq data (cdr data)))
7403     (when article
7404       (gnus-summary-goto-subject article))
7405     (gnus-summary-position-point)
7406     article))
7407
7408 (defun gnus-summary-better-unread-subject ()
7409   "Select the first unread subject that has a score over the default score."
7410   (interactive)
7411   (let ((data gnus-newsgroup-data)
7412         article score)
7413     (while (and (setq article (gnus-data-number (car data)))
7414                 (or (gnus-data-read-p (car data))
7415                     (not (> (gnus-summary-article-score article)
7416                             gnus-summary-default-score))))
7417       (setq data (cdr data)))
7418     (when article
7419       (gnus-summary-goto-subject article))
7420     (gnus-summary-position-point)
7421     article))
7422
7423 (defun gnus-summary-last-subject ()
7424   "Go to the last displayed subject line in the group."
7425   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7426     (when article
7427       (gnus-summary-goto-subject article))))
7428
7429 (defun gnus-summary-goto-article (article &optional all-headers force)
7430   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7431 If ALL-HEADERS is non-nil, no header lines are hidden.
7432 If FORCE, go to the article even if it isn't displayed.  If FORCE
7433 is a number, it is the line the article is to be displayed on."
7434   (interactive
7435    (list
7436     (completing-read
7437      "Article number or Message-ID: "
7438      (mapcar (lambda (number) (list (int-to-string number)))
7439              gnus-newsgroup-limit))
7440     current-prefix-arg
7441     t))
7442   (prog1
7443       (if (and (stringp article)
7444                (string-match "@\\|%40" article))
7445           (gnus-summary-refer-article article)
7446         (when (stringp article)
7447           (setq article (string-to-number article)))
7448         (if (gnus-summary-goto-subject article force)
7449             (gnus-summary-display-article article all-headers)
7450           (gnus-message 4 "Couldn't go to article %s" article) nil))
7451     (gnus-summary-position-point)))
7452
7453 (defun gnus-summary-goto-last-article ()
7454   "Go to the previously read article."
7455   (interactive)
7456   (prog1
7457       (when gnus-last-article
7458         (gnus-summary-goto-article gnus-last-article nil t))
7459     (gnus-summary-position-point)))
7460
7461 (defun gnus-summary-pop-article (number)
7462   "Pop one article off the history and go to the previous.
7463 NUMBER articles will be popped off."
7464   (interactive "p")
7465   (let (to)
7466     (setq gnus-newsgroup-history
7467           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7468     (if to
7469         (gnus-summary-goto-article (car to) nil t)
7470       (error "Article history empty")))
7471   (gnus-summary-position-point))
7472
7473 ;; Summary commands and functions for limiting the summary buffer.
7474
7475 (defun gnus-summary-limit-to-articles (n)
7476   "Limit the summary buffer to the next N articles.
7477 If not given a prefix, use the process marked articles instead."
7478   (interactive "P")
7479   (prog1
7480       (let ((articles (gnus-summary-work-articles n)))
7481         (setq gnus-newsgroup-processable nil)
7482         (gnus-summary-limit articles))
7483     (gnus-summary-position-point)))
7484
7485 (defun gnus-summary-pop-limit (&optional total)
7486   "Restore the previous limit.
7487 If given a prefix, remove all limits."
7488   (interactive "P")
7489   (when total
7490     (setq gnus-newsgroup-limits
7491           (list (mapcar (lambda (h) (mail-header-number h))
7492                         gnus-newsgroup-headers))))
7493   (unless gnus-newsgroup-limits
7494     (error "No limit to pop"))
7495   (prog1
7496       (gnus-summary-limit nil 'pop)
7497     (gnus-summary-position-point)))
7498
7499 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7500   "Limit the summary buffer to articles that have subjects that match a regexp.
7501 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7502   (interactive
7503    (list (read-string (if current-prefix-arg
7504                           "Exclude subject (regexp): "
7505                         "Limit to subject (regexp): "))
7506          nil current-prefix-arg))
7507   (unless header
7508     (setq header "subject"))
7509   (when (not (equal "" subject))
7510     (prog1
7511         (let ((articles (gnus-summary-find-matching
7512                          (or header "subject") subject 'all nil nil
7513                          not-matching)))
7514           (unless articles
7515             (error "Found no matches for \"%s\"" subject))
7516           (gnus-summary-limit articles))
7517       (gnus-summary-position-point))))
7518
7519 (defun gnus-summary-limit-to-author (from &optional not-matching)
7520   "Limit the summary buffer to articles that have authors that match a regexp.
7521 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7522   (interactive
7523    (list (read-string (if current-prefix-arg
7524                           "Exclude author (regexp): "
7525                         "Limit to author (regexp): "))
7526          current-prefix-arg))
7527   (gnus-summary-limit-to-subject from "from" not-matching))
7528
7529 (defun gnus-summary-limit-to-age (age &optional younger-p)
7530   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7531 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7532 articles that are younger than AGE days."
7533   (interactive
7534    (let ((younger current-prefix-arg)
7535          (days-got nil)
7536          days)
7537      (while (not days-got)
7538        (setq days (if younger
7539                       (read-string "Limit to articles younger than (in days, older when negative): ")
7540                     (read-string
7541                      "Limit to articles older than (in days, younger when negative): ")))
7542        (when (> (length days) 0)
7543          (setq days (read days)))
7544        (if (numberp days)
7545            (progn
7546              (setq days-got t)
7547              (if (< days 0)
7548                  (progn
7549                    (setq younger (not younger))
7550                    (setq days (* days -1)))))
7551          (message "Please enter a number.")
7552          (sleep-for 1)))
7553      (list days younger)))
7554   (prog1
7555       (let ((data gnus-newsgroup-data)
7556             (cutoff (days-to-time age))
7557             articles d date is-younger)
7558         (while (setq d (pop data))
7559           (when (and (vectorp (gnus-data-header d))
7560                      (setq date (mail-header-date (gnus-data-header d))))
7561             (setq is-younger (time-less-p
7562                               (time-since (condition-case ()
7563                                               (date-to-time date)
7564                                             (error '(0 0))))
7565                               cutoff))
7566             (when (if younger-p
7567                       is-younger
7568                     (not is-younger))
7569               (push (gnus-data-number d) articles))))
7570         (gnus-summary-limit (nreverse articles)))
7571     (gnus-summary-position-point)))
7572
7573 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7574   "Limit the summary buffer to articles that match an 'extra' header."
7575   (interactive
7576    (let ((header
7577           (intern
7578            (gnus-completing-read-with-default
7579             (symbol-name (car gnus-extra-headers))
7580             (if current-prefix-arg
7581                 "Exclude extra header:"
7582               "Limit extra header:")
7583             (mapcar (lambda (x)
7584                       (cons (symbol-name x) x))
7585                     gnus-extra-headers)
7586             nil
7587             t))))
7588      (list header
7589            (read-string (format "%s header %s (regexp): "
7590                                 (if current-prefix-arg "Exclude" "Limit to")
7591                                 header))
7592            current-prefix-arg)))
7593   (when (not (equal "" regexp))
7594     (prog1
7595         (let ((articles (gnus-summary-find-matching
7596                          (cons 'extra header) regexp 'all nil nil
7597                          not-matching)))
7598           (unless articles
7599             (error "Found no matches for \"%s\"" regexp))
7600           (gnus-summary-limit articles))
7601       (gnus-summary-position-point))))
7602
7603 (defun gnus-summary-limit-to-display-predicate ()
7604   "Limit the summary buffer to the predicated in the `display' group parameter."
7605   (interactive)
7606   (unless gnus-newsgroup-display
7607     (error "There is no `display' group parameter"))
7608   (let (articles)
7609     (dolist (number gnus-newsgroup-articles)
7610       (when (funcall gnus-newsgroup-display)
7611         (push number articles)))
7612     (gnus-summary-limit articles))
7613   (gnus-summary-position-point))
7614
7615 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7616 (make-obsolete
7617  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7618
7619 (defun gnus-summary-limit-to-unread (&optional all)
7620   "Limit the summary buffer to articles that are not marked as read.
7621 If ALL is non-nil, limit strictly to unread articles."
7622   (interactive "P")
7623   (if all
7624       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7625     (gnus-summary-limit-to-marks
7626      ;; Concat all the marks that say that an article is read and have
7627      ;; those removed.
7628      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7629            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7630            gnus-low-score-mark gnus-expirable-mark
7631            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7632            gnus-duplicate-mark gnus-souped-mark)
7633      'reverse)))
7634
7635 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7636 (make-obsolete 'gnus-summary-delete-marked-with
7637                'gnus-summary-limit-exclude-marks)
7638
7639 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7640   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7641 If REVERSE, limit the summary buffer to articles that are marked
7642 with MARKS.  MARKS can either be a string of marks or a list of marks.
7643 Returns how many articles were removed."
7644   (interactive "sMarks: ")
7645   (gnus-summary-limit-to-marks marks t))
7646
7647 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7648   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7649 If REVERSE (the prefix), limit the summary buffer to articles that are
7650 not marked with MARKS.  MARKS can either be a string of marks or a
7651 list of marks.
7652 Returns how many articles were removed."
7653   (interactive "sMarks: \nP")
7654   (prog1
7655       (let ((data gnus-newsgroup-data)
7656             (marks (if (listp marks) marks
7657                      (append marks nil))) ; Transform to list.
7658             articles)
7659         (while data
7660           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7661                   (memq (gnus-data-mark (car data)) marks))
7662             (push (gnus-data-number (car data)) articles))
7663           (setq data (cdr data)))
7664         (gnus-summary-limit articles))
7665     (gnus-summary-position-point)))
7666
7667 (defun gnus-summary-limit-to-score (score)
7668   "Limit to articles with score at or above SCORE."
7669   (interactive "NLimit to articles with score of at least: ")
7670   (let ((data gnus-newsgroup-data)
7671         articles)
7672     (while data
7673       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7674                 score)
7675         (push (gnus-data-number (car data)) articles))
7676       (setq data (cdr data)))
7677     (prog1
7678         (gnus-summary-limit articles)
7679       (gnus-summary-position-point))))
7680
7681 (defun gnus-summary-limit-to-unseen ()
7682   "Limit to unseen articles."
7683   (interactive)
7684   (prog1
7685       (gnus-summary-limit gnus-newsgroup-unseen)
7686     (gnus-summary-position-point)))
7687
7688 (defun gnus-summary-limit-include-thread (id)
7689   "Display all the hidden articles that is in the thread with ID in it.
7690 When called interactively, ID is the Message-ID of the current
7691 article."
7692   (interactive (list (mail-header-id (gnus-summary-article-header))))
7693   (let ((articles (gnus-articles-in-thread
7694                    (gnus-id-to-thread (gnus-root-id id)))))
7695     (prog1
7696         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7697       (gnus-summary-limit-include-matching-articles
7698        "subject"
7699        (regexp-quote (gnus-simplify-subject-re
7700                       (mail-header-subject (gnus-id-to-header id)))))
7701       (gnus-summary-position-point))))
7702
7703 (defun gnus-summary-limit-include-matching-articles (header regexp)
7704   "Display all the hidden articles that have HEADERs that match REGEXP."
7705   (interactive (list (read-string "Match on header: ")
7706                      (read-string "Regexp: ")))
7707   (let ((articles (gnus-find-matching-articles header regexp)))
7708     (prog1
7709         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7710       (gnus-summary-position-point))))
7711
7712 (defun gnus-summary-insert-dormant-articles ()
7713   "Insert all the dormant articles for this group into the current buffer."
7714   (interactive)
7715   (let ((gnus-verbose (max 6 gnus-verbose)))
7716     (if (not gnus-newsgroup-dormant)
7717         (gnus-message 3 "No cached articles for this group")
7718       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7719
7720 (defun gnus-summary-limit-include-dormant ()
7721   "Display all the hidden articles that are marked as dormant.
7722 Note that this command only works on a subset of the articles currently
7723 fetched for this group."
7724   (interactive)
7725   (unless gnus-newsgroup-dormant
7726     (error "There are no dormant articles in this group"))
7727   (prog1
7728       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7729     (gnus-summary-position-point)))
7730
7731 (defun gnus-summary-limit-exclude-dormant ()
7732   "Hide all dormant articles."
7733   (interactive)
7734   (prog1
7735       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7736     (gnus-summary-position-point)))
7737
7738 (defun gnus-summary-limit-exclude-childless-dormant ()
7739   "Hide all dormant articles that have no children."
7740   (interactive)
7741   (let ((data (gnus-data-list t))
7742         articles d children)
7743     ;; Find all articles that are either not dormant or have
7744     ;; children.
7745     (while (setq d (pop data))
7746       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7747                 (and (setq children
7748                            (gnus-article-children (gnus-data-number d)))
7749                      (let (found)
7750                        (while children
7751                          (when (memq (car children) articles)
7752                            (setq children nil
7753                                  found t))
7754                          (pop children))
7755                        found)))
7756         (push (gnus-data-number d) articles)))
7757     ;; Do the limiting.
7758     (prog1
7759         (gnus-summary-limit articles)
7760       (gnus-summary-position-point))))
7761
7762 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7763   "Mark all unread excluded articles as read.
7764 If ALL, mark even excluded ticked and dormants as read."
7765   (interactive "P")
7766   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7767   (let ((articles (gnus-sorted-ndifference
7768                    (sort
7769                     (mapcar (lambda (h) (mail-header-number h))
7770                             gnus-newsgroup-headers)
7771                     '<)
7772                    gnus-newsgroup-limit))
7773         article)
7774     (setq gnus-newsgroup-unreads
7775           (gnus-sorted-intersection gnus-newsgroup-unreads
7776                                     gnus-newsgroup-limit))
7777     (if all
7778         (setq gnus-newsgroup-dormant nil
7779               gnus-newsgroup-marked nil
7780               gnus-newsgroup-reads
7781               (nconc
7782                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7783                gnus-newsgroup-reads))
7784       (while (setq article (pop articles))
7785         (unless (or (memq article gnus-newsgroup-dormant)
7786                     (memq article gnus-newsgroup-marked))
7787           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7788
7789 (defun gnus-summary-limit (articles &optional pop)
7790   (if pop
7791       ;; We pop the previous limit off the stack and use that.
7792       (setq articles (car gnus-newsgroup-limits)
7793             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7794     ;; We use the new limit, so we push the old limit on the stack.
7795     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7796   ;; Set the limit.
7797   (setq gnus-newsgroup-limit articles)
7798   (let ((total (length gnus-newsgroup-data))
7799         (data (gnus-data-find-list (gnus-summary-article-number)))
7800         (gnus-summary-mark-below nil)   ; Inhibit this.
7801         found)
7802     ;; This will do all the work of generating the new summary buffer
7803     ;; according to the new limit.
7804     (gnus-summary-prepare)
7805     ;; Hide any threads, possibly.
7806     (gnus-summary-maybe-hide-threads)
7807     ;; Try to return to the article you were at, or one in the
7808     ;; neighborhood.
7809     (when data
7810       ;; We try to find some article after the current one.
7811       (while data
7812         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7813           (setq data nil
7814                 found t))
7815         (setq data (cdr data))))
7816     (unless found
7817       ;; If there is no data, that means that we were after the last
7818       ;; article.  The same goes when we can't find any articles
7819       ;; after the current one.
7820       (goto-char (point-max))
7821       (gnus-summary-find-prev))
7822     (gnus-set-mode-line 'summary)
7823     ;; We return how many articles were removed from the summary
7824     ;; buffer as a result of the new limit.
7825     (- total (length gnus-newsgroup-data))))
7826
7827 (defsubst gnus-invisible-cut-children (threads)
7828   (let ((num 0))
7829     (while threads
7830       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7831         (incf num))
7832       (pop threads))
7833     (< num 2)))
7834
7835 (defsubst gnus-cut-thread (thread)
7836   "Go forwards in the thread until we find an article that we want to display."
7837   (when (or (eq gnus-fetch-old-headers 'some)
7838             (eq gnus-fetch-old-headers 'invisible)
7839             (numberp gnus-fetch-old-headers)
7840             (eq gnus-build-sparse-threads 'some)
7841             (eq gnus-build-sparse-threads 'more))
7842     ;; Deal with old-fetched headers and sparse threads.
7843     (while (and
7844             thread
7845             (or
7846              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7847              (gnus-summary-article-ancient-p
7848               (mail-header-number (car thread))))
7849             (if (or (<= (length (cdr thread)) 1)
7850                     (eq gnus-fetch-old-headers 'invisible))
7851                 (setq gnus-newsgroup-limit
7852                       (delq (mail-header-number (car thread))
7853                             gnus-newsgroup-limit)
7854                       thread (cadr thread))
7855               (when (gnus-invisible-cut-children (cdr thread))
7856                 (let ((th (cdr thread)))
7857                   (while th
7858                     (if (memq (mail-header-number (caar th))
7859                               gnus-newsgroup-limit)
7860                         (setq thread (car th)
7861                               th nil)
7862                       (setq th (cdr th))))))))))
7863   thread)
7864
7865 (defun gnus-cut-threads (threads)
7866   "Cut off all uninteresting articles from the beginning of THREADS."
7867   (when (or (eq gnus-fetch-old-headers 'some)
7868             (eq gnus-fetch-old-headers 'invisible)
7869             (numberp gnus-fetch-old-headers)
7870             (eq gnus-build-sparse-threads 'some)
7871             (eq gnus-build-sparse-threads 'more))
7872     (let ((th threads))
7873       (while th
7874         (setcar th (gnus-cut-thread (car th)))
7875         (setq th (cdr th)))))
7876   ;; Remove nixed out threads.
7877   (delq nil threads))
7878
7879 (defun gnus-summary-initial-limit (&optional show-if-empty)
7880   "Figure out what the initial limit is supposed to be on group entry.
7881 This entails weeding out unwanted dormants, low-scored articles,
7882 fetch-old-headers verbiage, and so on."
7883   ;; Most groups have nothing to remove.
7884   (if (or gnus-inhibit-limiting
7885           (and (null gnus-newsgroup-dormant)
7886                (eq gnus-newsgroup-display 'gnus-not-ignore)
7887                (not (eq gnus-fetch-old-headers 'some))
7888                (not (numberp gnus-fetch-old-headers))
7889                (not (eq gnus-fetch-old-headers 'invisible))
7890                (null gnus-summary-expunge-below)
7891                (not (eq gnus-build-sparse-threads 'some))
7892                (not (eq gnus-build-sparse-threads 'more))
7893                (null gnus-thread-expunge-below)
7894                (not gnus-use-nocem)))
7895       ()                                ; Do nothing.
7896     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7897     (setq gnus-newsgroup-limit nil)
7898     (mapatoms
7899      (lambda (node)
7900        (unless (car (symbol-value node))
7901          ;; These threads have no parents -- they are roots.
7902          (let ((nodes (cdr (symbol-value node)))
7903                thread)
7904            (while nodes
7905              (if (and gnus-thread-expunge-below
7906                       (< (gnus-thread-total-score (car nodes))
7907                          gnus-thread-expunge-below))
7908                  (gnus-expunge-thread (pop nodes))
7909                (setq thread (pop nodes))
7910                (gnus-summary-limit-children thread))))))
7911      gnus-newsgroup-dependencies)
7912     ;; If this limitation resulted in an empty group, we might
7913     ;; pop the previous limit and use it instead.
7914     (when (and (not gnus-newsgroup-limit)
7915                show-if-empty)
7916       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7917     gnus-newsgroup-limit))
7918
7919 (defun gnus-summary-limit-children (thread)
7920   "Return 1 if this subthread is visible and 0 if it is not."
7921   ;; First we get the number of visible children to this thread.  This
7922   ;; is done by recursing down the thread using this function, so this
7923   ;; will really go down to a leaf article first, before slowly
7924   ;; working its way up towards the root.
7925   (when thread
7926     (let* ((max-lisp-eval-depth 5000)
7927            (children
7928            (if (cdr thread)
7929                (apply '+ (mapcar 'gnus-summary-limit-children
7930                                  (cdr thread)))
7931              0))
7932           (number (mail-header-number (car thread)))
7933           score)
7934       (if (and
7935            (not (memq number gnus-newsgroup-marked))
7936            (or
7937             ;; If this article is dormant and has absolutely no visible
7938             ;; children, then this article isn't visible.
7939             (and (memq number gnus-newsgroup-dormant)
7940                  (zerop children))
7941             ;; If this is "fetch-old-headered" and there is no
7942             ;; visible children, then we don't want this article.
7943             (and (or (eq gnus-fetch-old-headers 'some)
7944                      (numberp gnus-fetch-old-headers))
7945                  (gnus-summary-article-ancient-p number)
7946                  (zerop children))
7947             ;; If this is "fetch-old-headered" and `invisible', then
7948             ;; we don't want this article.
7949             (and (eq gnus-fetch-old-headers 'invisible)
7950                  (gnus-summary-article-ancient-p number))
7951             ;; If this is a sparsely inserted article with no children,
7952             ;; we don't want it.
7953             (and (eq gnus-build-sparse-threads 'some)
7954                  (gnus-summary-article-sparse-p number)
7955                  (zerop children))
7956             ;; If we use expunging, and this article is really
7957             ;; low-scored, then we don't want this article.
7958             (when (and gnus-summary-expunge-below
7959                        (< (setq score
7960                                 (or (cdr (assq number gnus-newsgroup-scored))
7961                                     gnus-summary-default-score))
7962                           gnus-summary-expunge-below))
7963               ;; We increase the expunge-tally here, but that has
7964               ;; nothing to do with the limits, really.
7965               (incf gnus-newsgroup-expunged-tally)
7966               ;; We also mark as read here, if that's wanted.
7967               (when (and gnus-summary-mark-below
7968                          (< score gnus-summary-mark-below))
7969                 (setq gnus-newsgroup-unreads
7970                       (delq number gnus-newsgroup-unreads))
7971                 (if gnus-newsgroup-auto-expire
7972                     (push number gnus-newsgroup-expirable)
7973                   (push (cons number gnus-low-score-mark)
7974                         gnus-newsgroup-reads)))
7975               t)
7976             ;; Do the `display' group parameter.
7977             (and gnus-newsgroup-display
7978                  (not (funcall gnus-newsgroup-display)))
7979             ;; Check NoCeM things.
7980             (if (and gnus-use-nocem
7981                      (gnus-nocem-unwanted-article-p
7982                       (mail-header-id (car thread))))
7983                 (progn
7984                   (setq gnus-newsgroup-unreads
7985                         (delq number gnus-newsgroup-unreads))
7986                   t))))
7987           ;; Nope, invisible article.
7988           0
7989         ;; Ok, this article is to be visible, so we add it to the limit
7990         ;; and return 1.
7991         (push number gnus-newsgroup-limit)
7992         1))))
7993
7994 (defun gnus-expunge-thread (thread)
7995   "Mark all articles in THREAD as read."
7996   (let* ((number (mail-header-number (car thread))))
7997     (incf gnus-newsgroup-expunged-tally)
7998     ;; We also mark as read here, if that's wanted.
7999     (setq gnus-newsgroup-unreads
8000           (delq number gnus-newsgroup-unreads))
8001     (if gnus-newsgroup-auto-expire
8002         (push number gnus-newsgroup-expirable)
8003       (push (cons number gnus-low-score-mark)
8004             gnus-newsgroup-reads)))
8005   ;; Go recursively through all subthreads.
8006   (mapcar 'gnus-expunge-thread (cdr thread)))
8007
8008 ;; Summary article oriented commands
8009
8010 (defun gnus-summary-refer-parent-article (n)
8011   "Refer parent article N times.
8012 If N is negative, go to ancestor -N instead.
8013 The difference between N and the number of articles fetched is returned."
8014   (interactive "p")
8015   (let ((skip 1)
8016         error header ref)
8017     (when (not (natnump n))
8018       (setq skip (abs n)
8019             n 1))
8020     (while (and (> n 0)
8021                 (not error))
8022       (setq header (gnus-summary-article-header))
8023       (if (and (eq (mail-header-number header)
8024                    (cdr gnus-article-current))
8025                (equal gnus-newsgroup-name
8026                       (car gnus-article-current)))
8027           ;; If we try to find the parent of the currently
8028           ;; displayed article, then we take a look at the actual
8029           ;; References header, since this is slightly more
8030           ;; reliable than the References field we got from the
8031           ;; server.
8032           (save-excursion
8033             (set-buffer gnus-original-article-buffer)
8034             (nnheader-narrow-to-headers)
8035             (unless (setq ref (message-fetch-field "references"))
8036               (when (setq ref (message-fetch-field "in-reply-to"))
8037                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8038             (widen))
8039         (setq ref
8040               ;; It's not the current article, so we take a bet on
8041               ;; the value we got from the server.
8042               (mail-header-references header)))
8043       (if (and ref
8044                (not (equal ref "")))
8045           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8046             (gnus-message 1 "Couldn't find parent"))
8047         (gnus-message 1 "No references in article %d"
8048                       (gnus-summary-article-number))
8049         (setq error t))
8050       (decf n))
8051     (gnus-summary-position-point)
8052     n))
8053
8054 (defun gnus-summary-refer-references ()
8055   "Fetch all articles mentioned in the References header.
8056 Return the number of articles fetched."
8057   (interactive)
8058   (let ((ref (mail-header-references (gnus-summary-article-header)))
8059         (current (gnus-summary-article-number))
8060         (n 0))
8061     (if (or (not ref)
8062             (equal ref ""))
8063         (error "No References in the current article")
8064       ;; For each Message-ID in the References header...
8065       (while (string-match "<[^>]*>" ref)
8066         (incf n)
8067         ;; ... fetch that article.
8068         (gnus-summary-refer-article
8069          (prog1 (match-string 0 ref)
8070            (setq ref (substring ref (match-end 0))))))
8071       (gnus-summary-goto-subject current)
8072       (gnus-summary-position-point)
8073       n)))
8074
8075 (defun gnus-summary-refer-thread (&optional limit)
8076   "Fetch all articles in the current thread.
8077 If LIMIT (the numerical prefix), fetch that many old headers instead
8078 of what's specified by the `gnus-refer-thread-limit' variable."
8079   (interactive "P")
8080   (let ((id (mail-header-id (gnus-summary-article-header)))
8081         (limit (if limit (prefix-numeric-value limit)
8082                  gnus-refer-thread-limit)))
8083     (unless (eq gnus-fetch-old-headers 'invisible)
8084       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8085       ;; Retrieve the headers and read them in.
8086       (if (eq (if (numberp limit)
8087                   (gnus-retrieve-headers
8088                    (list (min
8089                           (+ (mail-header-number
8090                               (gnus-summary-article-header))
8091                              limit)
8092                           gnus-newsgroup-end))
8093                    gnus-newsgroup-name (* limit 2))
8094                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8095                 ;; headers.
8096                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8097                                        gnus-newsgroup-name limit))
8098               'nov)
8099           (gnus-build-all-threads)
8100         (error "Can't fetch thread from back ends that don't support NOV"))
8101       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8102     (gnus-summary-limit-include-thread id)))
8103
8104 (defun gnus-summary-refer-article (message-id)
8105   "Fetch an article specified by MESSAGE-ID."
8106   (interactive "sMessage-ID: ")
8107   (when (and (stringp message-id)
8108              (not (zerop (length message-id))))
8109     (setq message-id (gnus-replace-in-string message-id " " ""))
8110     ;; Construct the correct Message-ID if necessary.
8111     ;; Suggested by tale@pawl.rpi.edu.
8112     (unless (string-match "^<" message-id)
8113       (setq message-id (concat "<" message-id)))
8114     (unless (string-match ">$" message-id)
8115       (setq message-id (concat message-id ">")))
8116     ;; People often post MIDs from URLs, so unhex it:
8117     (unless (string-match "@" message-id)
8118       (setq message-id (gnus-url-unhex-string message-id)))
8119     (let* ((header (gnus-id-to-header message-id))
8120            (sparse (and header
8121                         (gnus-summary-article-sparse-p
8122                          (mail-header-number header))
8123                         (memq (mail-header-number header)
8124                               gnus-newsgroup-limit)))
8125            number)
8126       (cond
8127        ;; If the article is present in the buffer we just go to it.
8128        ((and header
8129              (or (not (gnus-summary-article-sparse-p
8130                        (mail-header-number header)))
8131                  sparse))
8132         (prog1
8133             (gnus-summary-goto-article
8134              (mail-header-number header) nil t)
8135           (when sparse
8136             (gnus-summary-update-article (mail-header-number header)))))
8137        (t
8138         ;; We fetch the article.
8139         (catch 'found
8140           (dolist (gnus-override-method (gnus-refer-article-methods))
8141             (when (and (gnus-check-server gnus-override-method)
8142                        ;; Fetch the header,
8143                        (setq number (gnus-summary-insert-subject message-id)))
8144               ;; and display the article.
8145               (gnus-summary-select-article nil nil nil number)
8146               (throw 'found t)))
8147           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8148
8149 (defun gnus-refer-article-methods ()
8150   "Return a list of referable methods."
8151   (cond
8152    ;; No method, so we default to current and native.
8153    ((null gnus-refer-article-method)
8154     (list gnus-current-select-method gnus-select-method))
8155    ;; Current.
8156    ((eq 'current gnus-refer-article-method)
8157     (list gnus-current-select-method))
8158    ;; List of select methods.
8159    ((not (and (symbolp (car gnus-refer-article-method))
8160               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8161     (let (out)
8162       (dolist (method gnus-refer-article-method)
8163         (push (if (eq 'current method)
8164                   gnus-current-select-method
8165                 method)
8166               out))
8167       (nreverse out)))
8168    ;; One single select method.
8169    (t
8170     (list gnus-refer-article-method))))
8171
8172 (defun gnus-summary-edit-parameters ()
8173   "Edit the group parameters of the current group."
8174   (interactive)
8175   (gnus-group-edit-group gnus-newsgroup-name 'params))
8176
8177 (defun gnus-summary-customize-parameters ()
8178   "Customize the group parameters of the current group."
8179   (interactive)
8180   (gnus-group-customize gnus-newsgroup-name))
8181
8182 (defun gnus-summary-enter-digest-group (&optional force)
8183   "Enter an nndoc group based on the current article.
8184 If FORCE, force a digest interpretation.  If not, try
8185 to guess what the document format is."
8186   (interactive "P")
8187   (let ((conf gnus-current-window-configuration))
8188     (save-window-excursion
8189       (save-excursion
8190         (let (gnus-article-prepare-hook
8191               gnus-display-mime-function
8192               gnus-break-pages)
8193           (gnus-summary-select-article))))
8194     (setq gnus-current-window-configuration conf)
8195     (let* ((name (format "%s-%d"
8196                          (gnus-group-prefixed-name
8197                           gnus-newsgroup-name (list 'nndoc ""))
8198                          (save-excursion
8199                            (set-buffer gnus-summary-buffer)
8200                            gnus-current-article)))
8201            (ogroup gnus-newsgroup-name)
8202            (params (append (gnus-info-params (gnus-get-info ogroup))
8203                            (list (cons 'to-group ogroup))
8204                            (list (cons 'parent-group ogroup))
8205                            (list (cons 'save-article-group ogroup))))
8206            (case-fold-search t)
8207            (buf (current-buffer))
8208            dig to-address)
8209       (save-excursion
8210         (set-buffer gnus-original-article-buffer)
8211         ;; Have the digest group inherit the main mail address of
8212         ;; the parent article.
8213         (when (setq to-address (or (gnus-fetch-field "reply-to")
8214                                    (gnus-fetch-field "from")))
8215           (setq params (append
8216                         (list (cons 'to-address
8217                                     (funcall gnus-decode-encoded-word-function
8218                                              to-address))))))
8219         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8220         (insert-buffer-substring gnus-original-article-buffer)
8221         ;; Remove lines that may lead nndoc to misinterpret the
8222         ;; document type.
8223         (narrow-to-region
8224          (goto-char (point-min))
8225          (or (search-forward "\n\n" nil t) (point)))
8226         (goto-char (point-min))
8227         (delete-matching-lines "^Path:\\|^From ")
8228         (widen))
8229       (unwind-protect
8230           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8231                     (gnus-newsgroup-ephemeral-ignored-charsets
8232                      gnus-newsgroup-ignored-charsets))
8233                 (gnus-group-read-ephemeral-group
8234                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8235                               (nndoc-article-type
8236                                ,(if force 'mbox 'guess)))
8237                  t nil nil nil
8238                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8239                                                         "ADAPT")))))
8240               ;; Make all postings to this group go to the parent group.
8241               (nconc (gnus-info-params (gnus-get-info name))
8242                      params)
8243             ;; Couldn't select this doc group.
8244             (switch-to-buffer buf)
8245             (gnus-set-global-variables)
8246             (gnus-configure-windows 'summary)
8247             (gnus-message 3 "Article couldn't be entered?"))
8248         (kill-buffer dig)))))
8249
8250 (defun gnus-summary-read-document (n)
8251   "Open a new group based on the current article(s).
8252 This will allow you to read digests and other similar
8253 documents as newsgroups.
8254 Obeys the standard process/prefix convention."
8255   (interactive "P")
8256   (let* ((articles (gnus-summary-work-articles n))
8257          (ogroup gnus-newsgroup-name)
8258          (params (append (gnus-info-params (gnus-get-info ogroup))
8259                          (list (cons 'to-group ogroup))))
8260          article group egroup groups vgroup)
8261     (while (setq article (pop articles))
8262       (setq group (format "%s-%d" gnus-newsgroup-name article))
8263       (gnus-summary-remove-process-mark article)
8264       (when (gnus-summary-display-article article)
8265         (save-excursion
8266           (with-temp-buffer
8267             (insert-buffer-substring gnus-original-article-buffer)
8268             ;; Remove some headers that may lead nndoc to make
8269             ;; the wrong guess.
8270             (message-narrow-to-head)
8271             (goto-char (point-min))
8272             (delete-matching-lines "^\\(Path\\):\\|^From ")
8273             (widen)
8274             (if (setq egroup
8275                       (gnus-group-read-ephemeral-group
8276                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8277                                      (nndoc-article-type guess))
8278                        t nil t))
8279                 (progn
8280             ;; Make all postings to this group go to the parent group.
8281                   (nconc (gnus-info-params (gnus-get-info egroup))
8282                          params)
8283                   (push egroup groups))
8284               ;; Couldn't select this doc group.
8285               (gnus-error 3 "Article couldn't be entered"))))))
8286     ;; Now we have selected all the documents.
8287     (cond
8288      ((not groups)
8289       (error "None of the articles could be interpreted as documents"))
8290      ((gnus-group-read-ephemeral-group
8291        (setq vgroup (format
8292                      "nnvirtual:%s-%s" gnus-newsgroup-name
8293                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8294        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8295        t
8296        (cons (current-buffer) 'summary)))
8297      (t
8298       (error "Couldn't select virtual nndoc group")))))
8299
8300 (defun gnus-summary-isearch-article (&optional regexp-p)
8301   "Do incremental search forward on the current article.
8302 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8303   (interactive "P")
8304   (gnus-summary-select-article)
8305   (gnus-configure-windows 'article)
8306   (gnus-eval-in-buffer-window gnus-article-buffer
8307     (save-restriction
8308       (widen)
8309       (isearch-forward regexp-p))))
8310
8311 (defun gnus-summary-search-article-forward (regexp &optional backward)
8312   "Search for an article containing REGEXP forward.
8313 If BACKWARD, search backward instead."
8314   (interactive
8315    (list (read-string
8316           (format "Search article %s (regexp%s): "
8317                   (if current-prefix-arg "backward" "forward")
8318                   (if gnus-last-search-regexp
8319                       (concat ", default " gnus-last-search-regexp)
8320                     "")))
8321          current-prefix-arg))
8322   (if (string-equal regexp "")
8323       (setq regexp (or gnus-last-search-regexp ""))
8324     (setq gnus-last-search-regexp regexp)
8325     (setq gnus-article-before-search gnus-current-article))
8326   ;; Intentionally set gnus-last-article.
8327   (setq gnus-last-article gnus-article-before-search)
8328   (let ((gnus-last-article gnus-last-article))
8329     (if (gnus-summary-search-article regexp backward)
8330         (gnus-summary-show-thread)
8331       (error "Search failed: \"%s\"" regexp))))
8332
8333 (defun gnus-summary-search-article-backward (regexp)
8334   "Search for an article containing REGEXP backward."
8335   (interactive
8336    (list (read-string
8337           (format "Search article backward (regexp%s): "
8338                   (if gnus-last-search-regexp
8339                       (concat ", default " gnus-last-search-regexp)
8340                     "")))))
8341   (gnus-summary-search-article-forward regexp 'backward))
8342
8343 (defun gnus-summary-search-article (regexp &optional backward)
8344   "Search for an article containing REGEXP.
8345 Optional argument BACKWARD means do search for backward.
8346 `gnus-select-article-hook' is not called during the search."
8347   ;; We have to require this here to make sure that the following
8348   ;; dynamic binding isn't shadowed by autoloading.
8349   (require 'gnus-async)
8350   (require 'gnus-art)
8351   (let ((gnus-select-article-hook nil)  ;Disable hook.
8352         (gnus-article-prepare-hook nil)
8353         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8354         (gnus-use-article-prefetch nil)
8355         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8356         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8357         (gnus-visual nil)
8358         (gnus-keep-backlog nil)
8359         (gnus-break-pages nil)
8360         (gnus-summary-display-arrow nil)
8361         (gnus-updated-mode-lines nil)
8362         (gnus-auto-center-summary nil)
8363         (sum (current-buffer))
8364         (gnus-display-mime-function nil)
8365         (found nil)
8366         point)
8367     (gnus-save-hidden-threads
8368       (gnus-summary-select-article)
8369       (set-buffer gnus-article-buffer)
8370       (goto-char (window-point (get-buffer-window (current-buffer))))
8371       (when backward
8372         (forward-line -1))
8373       (while (not found)
8374         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8375         (if (if backward
8376                 (re-search-backward regexp nil t)
8377               (re-search-forward regexp nil t))
8378             ;; We found the regexp.
8379             (progn
8380               (setq found 'found)
8381               (beginning-of-line)
8382               (set-window-start
8383                (get-buffer-window (current-buffer))
8384                (point))
8385               (forward-line 1)
8386               (set-window-point
8387                (get-buffer-window (current-buffer))
8388                (point))
8389               (set-buffer sum)
8390               (setq point (point)))
8391           ;; We didn't find it, so we go to the next article.
8392           (set-buffer sum)
8393           (setq found 'not)
8394           (while (eq found 'not)
8395             (if (not (if backward (gnus-summary-find-prev)
8396                        (gnus-summary-find-next)))
8397                 ;; No more articles.
8398                 (setq found t)
8399               ;; Select the next article and adjust point.
8400               (unless (gnus-summary-article-sparse-p
8401                        (gnus-summary-article-number))
8402                 (setq found nil)
8403                 (gnus-summary-select-article)
8404                 (set-buffer gnus-article-buffer)
8405                 (widen)
8406                 (goto-char (if backward (point-max) (point-min))))))))
8407       (gnus-message 7 ""))
8408     ;; Return whether we found the regexp.
8409     (when (eq found 'found)
8410       (goto-char point)
8411       (gnus-summary-show-thread)
8412       (gnus-summary-goto-subject gnus-current-article)
8413       (gnus-summary-position-point)
8414       t)))
8415
8416 (defun gnus-find-matching-articles (header regexp)
8417   "Return a list of all articles that match REGEXP on HEADER.
8418 This search includes all articles in the current group that Gnus has
8419 fetched headers for, whether they are displayed or not."
8420   (let ((articles nil)
8421         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8422         (case-fold-search t))
8423     (dolist (header gnus-newsgroup-headers)
8424       (when (string-match regexp (funcall func header))
8425         (push (mail-header-number header) articles)))
8426     (nreverse articles)))
8427
8428 (defun gnus-summary-find-matching (header regexp &optional backward unread
8429                                           not-case-fold not-matching)
8430   "Return a list of all articles that match REGEXP on HEADER.
8431 The search stars on the current article and goes forwards unless
8432 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8433 If UNREAD is non-nil, only unread articles will
8434 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8435 in the comparisons. If NOT-MATCHING, return a list of all articles that
8436 not match REGEXP on HEADER."
8437   (let ((case-fold-search (not not-case-fold))
8438         articles d func)
8439     (if (consp header)
8440         (if (eq (car header) 'extra)
8441             (setq func
8442                   `(lambda (h)
8443                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8444                          "")))
8445           (error "%s is an invalid header" header))
8446       (unless (fboundp (intern (concat "mail-header-" header)))
8447         (error "%s is not a valid header" header))
8448       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8449     (dolist (d (if (eq backward 'all)
8450                    gnus-newsgroup-data
8451                  (gnus-data-find-list
8452                   (gnus-summary-article-number)
8453                   (gnus-data-list backward))))
8454       (when (and (or (not unread)       ; We want all articles...
8455                      (gnus-data-unread-p d)) ; Or just unreads.
8456                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8457                  (if not-matching
8458                      (not (string-match
8459                            regexp
8460                            (funcall func (gnus-data-header d))))
8461                    (string-match regexp
8462                                  (funcall func (gnus-data-header d)))))
8463         (push (gnus-data-number d) articles))) ; Success!
8464     (nreverse articles)))
8465
8466 (defun gnus-summary-execute-command (header regexp command &optional backward)
8467   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8468 If HEADER is an empty string (or nil), the match is done on the entire
8469 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8470   (interactive
8471    (list (let ((completion-ignore-case t))
8472            (completing-read
8473             "Header name: "
8474             (mapcar (lambda (header) (list (format "%s" header)))
8475                     (append
8476                      '("Number" "Subject" "From" "Lines" "Date"
8477                        "Message-ID" "Xref" "References" "Body")
8478                      gnus-extra-headers))
8479             nil 'require-match))
8480          (read-string "Regexp: ")
8481          (read-key-sequence "Command: ")
8482          current-prefix-arg))
8483   (when (equal header "Body")
8484     (setq header ""))
8485   ;; Hidden thread subtrees must be searched as well.
8486   (gnus-summary-show-all-threads)
8487   ;; We don't want to change current point nor window configuration.
8488   (save-excursion
8489     (save-window-excursion
8490       (let (gnus-visual
8491             gnus-treat-strip-trailing-blank-lines
8492             gnus-treat-strip-leading-blank-lines
8493             gnus-treat-strip-multiple-blank-lines
8494             gnus-treat-hide-boring-headers
8495             gnus-treat-fold-newsgroups
8496             gnus-article-prepare-hook)
8497         (gnus-message 6 "Executing %s..." (key-description command))
8498         ;; We'd like to execute COMMAND interactively so as to give arguments.
8499         (gnus-execute header regexp
8500                       `(call-interactively ',(key-binding command))
8501                       backward)
8502         (gnus-message 6 "Executing %s...done" (key-description command))))))
8503
8504 (defun gnus-summary-beginning-of-article ()
8505   "Scroll the article back to the beginning."
8506   (interactive)
8507   (gnus-summary-select-article)
8508   (gnus-configure-windows 'article)
8509   (gnus-eval-in-buffer-window gnus-article-buffer
8510     (widen)
8511     (goto-char (point-min))
8512     (when gnus-break-pages
8513       (gnus-narrow-to-page))))
8514
8515 (defun gnus-summary-end-of-article ()
8516   "Scroll to the end of the article."
8517   (interactive)
8518   (gnus-summary-select-article)
8519   (gnus-configure-windows 'article)
8520   (gnus-eval-in-buffer-window gnus-article-buffer
8521     (widen)
8522     (goto-char (point-max))
8523     (recenter -3)
8524     (when gnus-break-pages
8525       (when (re-search-backward page-delimiter nil t)
8526         (narrow-to-region (match-end 0) (point-max)))
8527       (gnus-narrow-to-page))))
8528
8529 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8530   "Truncate to LEN and quote all \"(\"'s in STRING."
8531   (gnus-replace-in-string (if (and len (> (length string) len))
8532                               (substring string 0 len)
8533                             string)
8534                           "[()]" "\\\\\\&"))
8535
8536 (defun gnus-summary-print-article (&optional filename n)
8537   "Generate and print a PostScript image of the N next (mail) articles.
8538
8539 If N is negative, print the N previous articles.  If N is nil and articles
8540 have been marked with the process mark, print these instead.
8541
8542 If the optional first argument FILENAME is nil, send the image to the
8543 printer.  If FILENAME is a string, save the PostScript image in a file with
8544 that name.  If FILENAME is a number, prompt the user for the name of the file
8545 to save in."
8546   (interactive (list (ps-print-preprint current-prefix-arg)))
8547   (dolist (article (gnus-summary-work-articles n))
8548     (gnus-summary-select-article nil nil 'pseudo article)
8549     (gnus-eval-in-buffer-window gnus-article-buffer
8550       (gnus-print-buffer))
8551     (gnus-summary-remove-process-mark article))
8552   (ps-despool filename))
8553
8554 (defun gnus-print-buffer ()
8555   (let ((buffer (generate-new-buffer " *print*")))
8556     (unwind-protect
8557         (progn
8558           (copy-to-buffer buffer (point-min) (point-max))
8559           (set-buffer buffer)
8560           (gnus-remove-text-with-property 'gnus-decoration)
8561           (when (gnus-visual-p 'article-highlight 'highlight)
8562             ;; Copy-to-buffer doesn't copy overlay.  So redo
8563             ;; highlight.
8564             (let ((gnus-article-buffer buffer))
8565               (gnus-article-highlight-citation t)
8566               (gnus-article-highlight-signature)
8567               (gnus-article-emphasize)
8568               (gnus-article-delete-invisible-text)))
8569           (let ((ps-left-header
8570                  (list
8571                   (concat "("
8572                           (gnus-summary-print-truncate-and-quote
8573                            (mail-header-subject gnus-current-headers)
8574                            66) ")")
8575                   (concat "("
8576                           (gnus-summary-print-truncate-and-quote
8577                            (mail-header-from gnus-current-headers)
8578                            45) ")")))
8579                 (ps-right-header
8580                  (list
8581                   "/pagenumberstring load"
8582                   (concat "("
8583                           (mail-header-date gnus-current-headers) ")"))))
8584             (gnus-run-hooks 'gnus-ps-print-hook)
8585             (save-excursion
8586               (if window-system
8587                   (ps-spool-buffer-with-faces)
8588                 (ps-spool-buffer)))))
8589       (kill-buffer buffer))))
8590
8591 (defun gnus-summary-show-article (&optional arg)
8592   "Force redisplaying of the current article.
8593 If ARG (the prefix) is a number, show the article with the charset
8594 defined in `gnus-summary-show-article-charset-alist', or the charset
8595 input.
8596 If ARG (the prefix) is non-nil and not a number, show the raw article
8597 without any article massaging functions being run.  Normally, the key
8598 strokes are `C-u g'."
8599   (interactive "P")
8600   (cond
8601    ((numberp arg)
8602     (gnus-summary-show-article t)
8603     (let ((gnus-newsgroup-charset
8604            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8605                (mm-read-coding-system
8606                 "View as charset: " ;; actually it is coding system.
8607                 (save-excursion
8608                   (set-buffer gnus-article-buffer)
8609                   (mm-detect-coding-region (point) (point-max))))))
8610           (gnus-newsgroup-ignored-charsets 'gnus-all))
8611       (gnus-summary-select-article nil 'force)
8612       (let ((deps gnus-newsgroup-dependencies)
8613             head header lines)
8614         (save-excursion
8615           (set-buffer gnus-original-article-buffer)
8616           (save-restriction
8617             (message-narrow-to-head)
8618             (setq head (buffer-string))
8619             (goto-char (point-min))
8620             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8621               (goto-char (point-max))
8622               (widen)
8623               (setq lines (1- (count-lines (point) (point-max))))))
8624           (with-temp-buffer
8625             (insert (format "211 %d Article retrieved.\n"
8626                             (cdr gnus-article-current)))
8627             (insert head)
8628             (if lines (insert (format "Lines: %d\n" lines)))
8629             (insert ".\n")
8630             (let ((nntp-server-buffer (current-buffer)))
8631               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8632         (gnus-data-set-header
8633          (gnus-data-find (cdr gnus-article-current))
8634          header)
8635         (gnus-summary-update-article-line
8636          (cdr gnus-article-current) header)
8637         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8638           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8639    ((not arg)
8640     ;; Select the article the normal way.
8641     (gnus-summary-select-article nil 'force))
8642    (t
8643     ;; We have to require this here to make sure that the following
8644     ;; dynamic binding isn't shadowed by autoloading.
8645     (require 'gnus-async)
8646     (require 'gnus-art)
8647     ;; Bind the article treatment functions to nil.
8648     (let ((gnus-have-all-headers t)
8649           gnus-article-prepare-hook
8650           gnus-article-decode-hook
8651           gnus-display-mime-function
8652           gnus-break-pages)
8653       ;; Destroy any MIME parts.
8654       (when (gnus-buffer-live-p gnus-article-buffer)
8655         (save-excursion
8656           (set-buffer gnus-article-buffer)
8657           (mm-destroy-parts gnus-article-mime-handles)
8658           ;; Set it to nil for safety reason.
8659           (setq gnus-article-mime-handle-alist nil)
8660           (setq gnus-article-mime-handles nil)))
8661       (gnus-summary-select-article nil 'force))))
8662   (gnus-summary-goto-subject gnus-current-article)
8663   (gnus-summary-position-point))
8664
8665 (defun gnus-summary-show-raw-article ()
8666   "Show the raw article without any article massaging functions being run."
8667   (interactive)
8668   (gnus-summary-show-article t))
8669
8670 (defun gnus-summary-verbose-headers (&optional arg)
8671   "Toggle permanent full header display.
8672 If ARG is a positive number, turn header display on.
8673 If ARG is a negative number, turn header display off."
8674   (interactive "P")
8675   (setq gnus-show-all-headers
8676         (cond ((or (not (numberp arg))
8677                    (zerop arg))
8678                (not gnus-show-all-headers))
8679               ((natnump arg)
8680                t)))
8681   (gnus-summary-show-article))
8682
8683 (defun gnus-summary-toggle-header (&optional arg)
8684   "Show the headers if they are hidden, or hide them if they are shown.
8685 If ARG is a positive number, show the entire header.
8686 If ARG is a negative number, hide the unwanted header lines."
8687   (interactive "P")
8688   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8689                      (get-buffer-window gnus-article-buffer t))))
8690     (with-current-buffer gnus-article-buffer
8691       (widen)
8692       (article-narrow-to-head)
8693       (let* ((buffer-read-only nil)
8694              (inhibit-point-motion-hooks t)
8695              (hidden (if (numberp arg)
8696                          (>= arg 0)
8697                        (gnus-article-hidden-text-p 'headers)))
8698              s e)
8699         (delete-region (point-min) (point-max))
8700         (with-current-buffer gnus-original-article-buffer
8701           (goto-char (setq s (point-min)))
8702           (setq e (if (search-forward "\n\n" nil t)
8703                       (1- (point))
8704                     (point-max))))
8705         (insert-buffer-substring gnus-original-article-buffer s e)
8706         (run-hooks 'gnus-article-decode-hook)
8707         (if hidden
8708             (let ((gnus-treat-hide-headers nil)
8709                   (gnus-treat-hide-boring-headers nil))
8710               (gnus-delete-wash-type 'headers)
8711               (gnus-treat-article 'head))
8712           (gnus-treat-article 'head))
8713         (widen)
8714         (if window
8715             (set-window-start window (goto-char (point-min))))
8716         (if gnus-break-pages
8717             (gnus-narrow-to-page)
8718           (when (gnus-visual-p 'page-marker)
8719             (let ((buffer-read-only nil))
8720               (gnus-remove-text-with-property 'gnus-prev)
8721               (gnus-remove-text-with-property 'gnus-next))))
8722         (gnus-set-mode-line 'article)))))
8723
8724 (defun gnus-summary-show-all-headers ()
8725   "Make all header lines visible."
8726   (interactive)
8727   (gnus-summary-toggle-header 1))
8728
8729 (defun gnus-summary-caesar-message (&optional arg)
8730   "Caesar rotate the current article by 13.
8731 The numerical prefix specifies how many places to rotate each letter
8732 forward."
8733   (interactive "P")
8734   (gnus-summary-select-article)
8735   (let ((mail-header-separator ""))
8736     (gnus-eval-in-buffer-window gnus-article-buffer
8737       (save-restriction
8738         (widen)
8739         (let ((start (window-start))
8740               buffer-read-only)
8741           (message-caesar-buffer-body arg)
8742           (set-window-start (get-buffer-window (current-buffer)) start))))))
8743
8744 (autoload 'unmorse-region "morse"
8745   "Convert morse coded text in region to ordinary ASCII text."
8746   t)
8747
8748 (defun gnus-summary-morse-message (&optional arg)
8749   "Morse decode the current article."
8750   (interactive "P")
8751   (gnus-summary-select-article)
8752   (let ((mail-header-separator ""))
8753     (gnus-eval-in-buffer-window gnus-article-buffer
8754       (save-excursion
8755         (save-restriction
8756           (widen)
8757           (let ((pos (window-start))
8758                 buffer-read-only)
8759             (goto-char (point-min))
8760             (when (message-goto-body)
8761               (gnus-narrow-to-body))
8762             (goto-char (point-min))
8763             (while (re-search-forward "·" (point-max) t)
8764               (replace-match "."))
8765             (unmorse-region (point-min) (point-max))
8766             (widen)
8767             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8768
8769 (defun gnus-summary-stop-page-breaking ()
8770   "Stop page breaking in the current article."
8771   (interactive)
8772   (gnus-summary-select-article)
8773   (gnus-eval-in-buffer-window gnus-article-buffer
8774     (widen)
8775     (when (gnus-visual-p 'page-marker)
8776       (let ((buffer-read-only nil))
8777         (gnus-remove-text-with-property 'gnus-prev)
8778         (gnus-remove-text-with-property 'gnus-next))
8779       (setq gnus-page-broken nil))))
8780
8781 (defun gnus-summary-move-article (&optional n to-newsgroup
8782                                             select-method action)
8783   "Move the current article to a different newsgroup.
8784 If N is a positive number, move the N next articles.
8785 If N is a negative number, move the N previous articles.
8786 If N is nil and any articles have been marked with the process mark,
8787 move those articles instead.
8788 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8789 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8790 re-spool using this method.
8791
8792 When called interactively with TO-NEWSGROUP being nil, the value of
8793 the variable `gnus-move-split-methods' is used for finding a default
8794 for the target newsgroup.
8795
8796 For this function to work, both the current newsgroup and the
8797 newsgroup that you want to move to have to support the `request-move'
8798 and `request-accept' functions.
8799
8800 ACTION can be either `move' (the default), `crosspost' or `copy'."
8801   (interactive "P")
8802   (unless action
8803     (setq action 'move))
8804   ;; Check whether the source group supports the required functions.
8805   (cond ((and (eq action 'move)
8806               (not (gnus-check-backend-function
8807                     'request-move-article gnus-newsgroup-name)))
8808          (error "The current group does not support article moving"))
8809         ((and (eq action 'crosspost)
8810               (not (gnus-check-backend-function
8811                     'request-replace-article gnus-newsgroup-name)))
8812          (error "The current group does not support article editing")))
8813   (let ((articles (gnus-summary-work-articles n))
8814         (prefix (if (gnus-check-backend-function
8815                      'request-move-article gnus-newsgroup-name)
8816                     (gnus-group-real-prefix gnus-newsgroup-name)
8817                   ""))
8818         (names '((move "Move" "Moving")
8819                  (copy "Copy" "Copying")
8820                  (crosspost "Crosspost" "Crossposting")))
8821         (copy-buf (save-excursion
8822                     (nnheader-set-temp-buffer " *copy article*")))
8823         art-group to-method new-xref article to-groups)
8824     (unless (assq action names)
8825       (error "Unknown action %s" action))
8826     ;; Read the newsgroup name.
8827     (when (and (not to-newsgroup)
8828                (not select-method))
8829       (if (and gnus-move-split-methods
8830                (not
8831                 (and (memq gnus-current-article articles)
8832                      (gnus-buffer-live-p gnus-original-article-buffer))))
8833           ;; When `gnus-move-split-methods' is non-nil, we have to
8834           ;; select an article to give `gnus-read-move-group-name' an
8835           ;; opportunity to suggest an appropriate default.  However,
8836           ;; we needn't render or mark the article.
8837           (let ((gnus-display-mime-function nil)
8838                 (gnus-article-prepare-hook nil)
8839                 (gnus-mark-article-hook nil))
8840             (gnus-summary-select-article nil nil nil (car articles))))
8841       (setq to-newsgroup
8842             (gnus-read-move-group-name
8843              (cadr (assq action names))
8844              (symbol-value (intern (format "gnus-current-%s-group" action)))
8845              articles prefix))
8846       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8847     (setq to-method (or select-method
8848                         (gnus-server-to-method
8849                          (gnus-group-method to-newsgroup))))
8850     ;; Check the method we are to move this article to...
8851     (unless (gnus-check-backend-function
8852              'request-accept-article (car to-method))
8853       (error "%s does not support article copying" (car to-method)))
8854     (unless (gnus-check-server to-method)
8855       (error "Can't open server %s" (car to-method)))
8856     (gnus-message 6 "%s to %s: %s..."
8857                   (caddr (assq action names))
8858                   (or (car select-method) to-newsgroup) articles)
8859     (while articles
8860       (setq article (pop articles))
8861       (setq
8862        art-group
8863        (cond
8864         ;; Move the article.
8865         ((eq action 'move)
8866          ;; Remove this article from future suppression.
8867          (gnus-dup-unsuppress-article article)
8868          (gnus-request-move-article
8869           article                       ; Article to move
8870           gnus-newsgroup-name           ; From newsgroup
8871           (nth 1 (gnus-find-method-for-group
8872                   gnus-newsgroup-name)) ; Server
8873           (list 'gnus-request-accept-article
8874                 to-newsgroup (list 'quote select-method)
8875                 (not articles) t)       ; Accept form
8876           (not articles)))              ; Only save nov last time
8877         ;; Copy the article.
8878         ((eq action 'copy)
8879          (save-excursion
8880            (set-buffer copy-buf)
8881            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8882              (gnus-request-accept-article
8883               to-newsgroup select-method (not articles) t))))
8884         ;; Crosspost the article.
8885         ((eq action 'crosspost)
8886          (let ((xref (message-tokenize-header
8887                       (mail-header-xref (gnus-summary-article-header article))
8888                       " ")))
8889            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8890                                   ":" (number-to-string article)))
8891            (unless xref
8892              (setq xref (list (system-name))))
8893            (setq new-xref
8894                  (concat
8895                   (mapconcat 'identity
8896                              (delete "Xref:" (delete new-xref xref))
8897                              " ")
8898                   " " new-xref))
8899            (save-excursion
8900              (set-buffer copy-buf)
8901              ;; First put the article in the destination group.
8902              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8903              (when (consp (setq art-group
8904                                 (gnus-request-accept-article
8905                                  to-newsgroup select-method (not articles))))
8906                (setq new-xref (concat new-xref " " (car art-group)
8907                                       ":"
8908                                       (number-to-string (cdr art-group))))
8909                ;; Now we have the new Xrefs header, so we insert
8910                ;; it and replace the new article.
8911                (nnheader-replace-header "Xref" new-xref)
8912                (gnus-request-replace-article
8913                 (cdr art-group) to-newsgroup (current-buffer))
8914                art-group))))))
8915       (cond
8916        ((not art-group)
8917         (gnus-message 1 "Couldn't %s article %s: %s"
8918                       (cadr (assq action names)) article
8919                       (nnheader-get-report (car to-method))))
8920        ((eq art-group 'junk)
8921         (when (eq action 'move)
8922           (gnus-summary-mark-article article gnus-canceled-mark)
8923           (gnus-message 4 "Deleted article %s" article)
8924           ;; run the delete hook
8925           (run-hook-with-args 'gnus-summary-article-delete-hook
8926                               action
8927                               (gnus-data-header
8928                                (assoc article (gnus-data-list nil)))
8929                               gnus-newsgroup-name nil
8930                               select-method)))
8931        (t
8932         (let* ((pto-group (gnus-group-prefixed-name
8933                            (car art-group) to-method))
8934                (entry
8935                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8936                (info (nth 2 entry))
8937                (to-group (gnus-info-group info))
8938                to-marks)
8939           ;; Update the group that has been moved to.
8940           (when (and info
8941                      (memq action '(move copy)))
8942             (unless (member to-group to-groups)
8943               (push to-group to-groups))
8944
8945             (unless (memq article gnus-newsgroup-unreads)
8946               (push 'read to-marks)
8947               (gnus-info-set-read
8948                info (gnus-add-to-range (gnus-info-read info)
8949                                        (list (cdr art-group)))))
8950
8951             ;; See whether the article is to be put in the cache.
8952             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8953                              gnus-article-mark-lists
8954                            (delete '(expirable . expire)
8955                                    (copy-sequence gnus-article-mark-lists))))
8956                   (to-article (cdr art-group)))
8957
8958               ;; Enter the article into the cache in the new group,
8959               ;; if that is required.
8960               (when gnus-use-cache
8961                 (gnus-cache-possibly-enter-article
8962                  to-group to-article
8963                  (memq article gnus-newsgroup-marked)
8964                  (memq article gnus-newsgroup-dormant)
8965                  (memq article gnus-newsgroup-unreads)))
8966
8967               (when gnus-preserve-marks
8968                 ;; Copy any marks over to the new group.
8969                 (when (and (equal to-group gnus-newsgroup-name)
8970                            (not (memq article gnus-newsgroup-unreads)))
8971                   ;; Mark this article as read in this group.
8972                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8973                   (setcdr (gnus-active to-group) to-article)
8974                   (setcdr gnus-newsgroup-active to-article))
8975
8976                 (while marks
8977                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8978                     (when (memq article (symbol-value
8979                                          (intern (format "gnus-newsgroup-%s"
8980                                                          (caar marks)))))
8981                       (push (cdar marks) to-marks)
8982                       ;; If the other group is the same as this group,
8983                       ;; then we have to add the mark to the list.
8984                       (when (equal to-group gnus-newsgroup-name)
8985                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8986                              (cons to-article
8987                                    (symbol-value
8988                                     (intern (format "gnus-newsgroup-%s"
8989                                                     (caar marks)))))))
8990                       ;; Copy the marks to other group.
8991                       (gnus-add-marked-articles
8992                        to-group (cdar marks) (list to-article) info)))
8993                   (setq marks (cdr marks)))
8994
8995                 (gnus-request-set-mark
8996                  to-group (list (list (list to-article) 'add to-marks))))
8997
8998               (gnus-dribble-enter
8999                (concat "(gnus-group-set-info '"
9000                        (gnus-prin1-to-string (gnus-get-info to-group))
9001                        ")"))))
9002
9003           ;; Update the Xref header in this article to point to
9004           ;; the new crossposted article we have just created.
9005           (when (eq action 'crosspost)
9006             (save-excursion
9007               (set-buffer copy-buf)
9008               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9009               (nnheader-replace-header "Xref" new-xref)
9010               (gnus-request-replace-article
9011                article gnus-newsgroup-name (current-buffer))))
9012
9013           ;; run the move/copy/crosspost/respool hook
9014           (run-hook-with-args 'gnus-summary-article-move-hook
9015                               action
9016                               (gnus-data-header
9017                                (assoc article (gnus-data-list nil)))
9018                               gnus-newsgroup-name
9019                               to-newsgroup
9020                               select-method))
9021
9022         ;;;!!!Why is this necessary?
9023         (set-buffer gnus-summary-buffer)
9024         
9025         (gnus-summary-goto-subject article)
9026         (when (eq action 'move)
9027           (gnus-summary-mark-article article gnus-canceled-mark))))
9028       (gnus-summary-remove-process-mark article))
9029     ;; Re-activate all groups that have been moved to.
9030     (save-excursion
9031       (set-buffer gnus-group-buffer)
9032       (let ((gnus-group-marked to-groups))
9033         (gnus-group-get-new-news-this-group nil t)))
9034
9035     (gnus-kill-buffer copy-buf)
9036     (gnus-summary-position-point)
9037     (gnus-set-mode-line 'summary)))
9038
9039 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9040   "Move the current article to a different newsgroup.
9041 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9042 When called interactively, if TO-NEWSGROUP is nil, use the value of
9043 the variable `gnus-move-split-methods' for finding a default target
9044 newsgroup.
9045 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9046 re-spool using this method."
9047   (interactive "P")
9048   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9049
9050 (defun gnus-summary-crosspost-article (&optional n)
9051   "Crosspost the current article to some other group."
9052   (interactive "P")
9053   (gnus-summary-move-article n nil nil 'crosspost))
9054
9055 (defcustom gnus-summary-respool-default-method nil
9056   "Default method type for respooling an article.
9057 If nil, use to the current newsgroup method."
9058   :type 'symbol
9059   :group 'gnus-summary-mail)
9060
9061 (defcustom gnus-summary-display-while-building nil
9062   "If non-nil, show and update the summary buffer as it's being built.
9063 If the value is t, update the buffer after every line is inserted.  If
9064 the value is an integer (N), update the display every N lines."
9065   :group 'gnus-thread
9066   :type '(choice (const :tag "off" nil)
9067                  number
9068                  (const :tag "frequently" t)))
9069
9070 (defun gnus-summary-respool-article (&optional n method)
9071   "Respool the current article.
9072 The article will be squeezed through the mail spooling process again,
9073 which means that it will be put in some mail newsgroup or other
9074 depending on `nnmail-split-methods'.
9075 If N is a positive number, respool the N next articles.
9076 If N is a negative number, respool the N previous articles.
9077 If N is nil and any articles have been marked with the process mark,
9078 respool those articles instead.
9079
9080 Respooling can be done both from mail groups and \"real\" newsgroups.
9081 In the former case, the articles in question will be moved from the
9082 current group into whatever groups they are destined to.  In the
9083 latter case, they will be copied into the relevant groups."
9084   (interactive
9085    (list current-prefix-arg
9086          (let* ((methods (gnus-methods-using 'respool))
9087                 (methname
9088                  (symbol-name (or gnus-summary-respool-default-method
9089                                   (car (gnus-find-method-for-group
9090                                         gnus-newsgroup-name)))))
9091                 (method
9092                  (gnus-completing-read-with-default
9093                   methname "What backend do you want to use when respooling?"
9094                   methods nil t nil 'gnus-mail-method-history))
9095                 ms)
9096            (cond
9097             ((zerop (length (setq ms (gnus-servers-using-backend
9098                                       (intern method)))))
9099              (list (intern method) ""))
9100             ((= 1 (length ms))
9101              (car ms))
9102             (t
9103              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9104                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9105                            ms-alist))))))))
9106   (unless method
9107     (error "No method given for respooling"))
9108   (if (assoc (symbol-name
9109               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9110              (gnus-methods-using 'respool))
9111       (gnus-summary-move-article n nil method)
9112     (gnus-summary-copy-article n nil method)))
9113
9114 (defun gnus-summary-import-article (file &optional edit)
9115   "Import an arbitrary file into a mail newsgroup."
9116   (interactive "fImport file: \nP")
9117   (let ((group gnus-newsgroup-name)
9118         (now (current-time))
9119         atts lines group-art)
9120     (unless (gnus-check-backend-function 'request-accept-article group)
9121       (error "%s does not support article importing" group))
9122     (or (file-readable-p file)
9123         (not (file-regular-p file))
9124         (error "Can't read %s" file))
9125     (save-excursion
9126       (set-buffer (gnus-get-buffer-create " *import file*"))
9127       (erase-buffer)
9128       (nnheader-insert-file-contents file)
9129       (goto-char (point-min))
9130       (if (nnheader-article-p)
9131           (save-restriction
9132             (goto-char (point-min))
9133             (search-forward "\n\n" nil t)
9134             (narrow-to-region (point-min) (1- (point)))
9135             (goto-char (point-min))
9136             (unless (re-search-forward "^date:" nil t)
9137               (goto-char (point-max))
9138               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9139        ;; This doesn't look like an article, so we fudge some headers.
9140         (setq atts (file-attributes file)
9141               lines (count-lines (point-min) (point-max)))
9142         (insert "From: " (read-string "From: ") "\n"
9143                 "Subject: " (read-string "Subject: ") "\n"
9144                 "Date: " (message-make-date (nth 5 atts)) "\n"
9145                 "Message-ID: " (message-make-message-id) "\n"
9146                 "Lines: " (int-to-string lines) "\n"
9147                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9148       (setq group-art (gnus-request-accept-article group nil t))
9149       (kill-buffer (current-buffer)))
9150     (setq gnus-newsgroup-active (gnus-activate-group group))
9151     (forward-line 1)
9152     (gnus-summary-goto-article (cdr group-art) nil t)
9153     (when edit
9154       (gnus-summary-edit-article))))
9155
9156 (defun gnus-summary-create-article ()
9157   "Create an article in a mail newsgroup."
9158   (interactive)
9159   (let ((group gnus-newsgroup-name)
9160         (now (current-time))
9161         group-art)
9162     (unless (gnus-check-backend-function 'request-accept-article group)
9163       (error "%s does not support article importing" group))
9164     (save-excursion
9165       (set-buffer (gnus-get-buffer-create " *import file*"))
9166       (erase-buffer)
9167       (goto-char (point-min))
9168       ;; This doesn't look like an article, so we fudge some headers.
9169       (insert "From: " (read-string "From: ") "\n"
9170               "Subject: " (read-string "Subject: ") "\n"
9171               "Date: " (message-make-date now) "\n"
9172               "Message-ID: " (message-make-message-id) "\n")
9173       (setq group-art (gnus-request-accept-article group nil t))
9174       (kill-buffer (current-buffer)))
9175     (setq gnus-newsgroup-active (gnus-activate-group group))
9176     (forward-line 1)
9177     (gnus-summary-goto-article (cdr group-art) nil t)
9178     (gnus-summary-edit-article)))
9179
9180 (defun gnus-summary-article-posted-p ()
9181   "Say whether the current (mail) article is available from news as well.
9182 This will be the case if the article has both been mailed and posted."
9183   (interactive)
9184   (let ((id (mail-header-references (gnus-summary-article-header)))
9185         (gnus-override-method (car (gnus-refer-article-methods))))
9186     (if (gnus-request-head id "")
9187         (gnus-message 2 "The current message was found on %s"
9188                       gnus-override-method)
9189       (gnus-message 2 "The current message couldn't be found on %s"
9190                     gnus-override-method)
9191       nil)))
9192
9193 (defun gnus-summary-expire-articles (&optional now)
9194   "Expire all articles that are marked as expirable in the current group."
9195   (interactive)
9196   (when (and (not gnus-group-is-exiting-without-update-p)
9197              (gnus-check-backend-function
9198               'request-expire-articles gnus-newsgroup-name))
9199     ;; This backend supports expiry.
9200     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9201            (expirable (if total
9202                           (progn
9203                             ;; We need to update the info for
9204                             ;; this group for `gnus-list-of-read-articles'
9205                             ;; to give us the right answer.
9206                             (gnus-run-hooks 'gnus-exit-group-hook)
9207                             (gnus-summary-update-info)
9208                             (gnus-list-of-read-articles gnus-newsgroup-name))
9209                         (setq gnus-newsgroup-expirable
9210                               (sort gnus-newsgroup-expirable '<))))
9211            (expiry-wait (if now 'immediate
9212                           (gnus-group-find-parameter
9213                            gnus-newsgroup-name 'expiry-wait)))
9214            (nnmail-expiry-target
9215             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9216                 nnmail-expiry-target))
9217            es)
9218       (when expirable
9219         ;; There are expirable articles in this group, so we run them
9220         ;; through the expiry process.
9221         (gnus-message 6 "Expiring articles...")
9222         (unless (gnus-check-group gnus-newsgroup-name)
9223           (error "Can't open server for %s" gnus-newsgroup-name))
9224         ;; The list of articles that weren't expired is returned.
9225         (save-excursion
9226           (if expiry-wait
9227               (let ((nnmail-expiry-wait-function nil)
9228                     (nnmail-expiry-wait expiry-wait))
9229                 (setq es (gnus-request-expire-articles
9230                           expirable gnus-newsgroup-name)))
9231             (setq es (gnus-request-expire-articles
9232                       expirable gnus-newsgroup-name)))
9233           (unless total
9234             (setq gnus-newsgroup-expirable es))
9235           ;; We go through the old list of expirable, and mark all
9236           ;; really expired articles as nonexistent.
9237           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9238             (let ((gnus-use-cache nil))
9239               (dolist (article expirable)
9240                 (when (and (not (memq article es))
9241                            (gnus-data-find article))
9242                   (gnus-summary-mark-article article gnus-canceled-mark)
9243                   (run-hook-with-args 'gnus-summary-article-expire-hook
9244                                       'delete
9245                                       (gnus-data-header
9246                                        (assoc article (gnus-data-list nil)))
9247                                       gnus-newsgroup-name
9248                                       nil
9249                                       nil))))))
9250         (gnus-message 6 "Expiring articles...done")))))
9251
9252 (defun gnus-summary-expire-articles-now ()
9253   "Expunge all expirable articles in the current group.
9254 This means that *all* articles that are marked as expirable will be
9255 deleted forever, right now."
9256   (interactive)
9257   (or gnus-expert-user
9258       (gnus-yes-or-no-p
9259        "Are you really, really, really sure you want to delete all these messages? ")
9260       (error "Phew!"))
9261   (gnus-summary-expire-articles t))
9262
9263 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9264 (defun gnus-summary-delete-article (&optional n)
9265   "Delete the N next (mail) articles.
9266 This command actually deletes articles.  This is not a marking
9267 command.  The article will disappear forever from your life, never to
9268 return.
9269
9270 If N is negative, delete backwards.
9271 If N is nil and articles have been marked with the process mark,
9272 delete these instead.
9273
9274 If `gnus-novice-user' is non-nil you will be asked for
9275 confirmation before the articles are deleted."
9276   (interactive "P")
9277   (unless (gnus-check-backend-function 'request-expire-articles
9278                                        gnus-newsgroup-name)
9279     (error "The current newsgroup does not support article deletion"))
9280   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9281     (error "Couldn't open server"))
9282   ;; Compute the list of articles to delete.
9283   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9284         (nnmail-expiry-target 'delete)
9285         not-deleted)
9286     (if (and gnus-novice-user
9287              (not (gnus-yes-or-no-p
9288                    (format "Do you really want to delete %s forever? "
9289                            (if (> (length articles) 1)
9290                                (format "these %s articles" (length articles))
9291                              "this article")))))
9292         ()
9293       ;; Delete the articles.
9294       (setq not-deleted (gnus-request-expire-articles
9295                          articles gnus-newsgroup-name 'force))
9296       (while articles
9297         (gnus-summary-remove-process-mark (car articles))
9298         ;; The backend might not have been able to delete the article
9299         ;; after all.
9300         (unless (memq (car articles) not-deleted)
9301           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9302         (let* ((article (car articles))
9303                (id (mail-header-id (gnus-data-header
9304                                     (assoc article (gnus-data-list nil))))))
9305           (run-hook-with-args 'gnus-summary-article-delete-hook
9306                               'delete id gnus-newsgroup-name nil
9307                               nil))
9308         (setq articles (cdr articles)))
9309       (when not-deleted
9310         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9311     (gnus-summary-position-point)
9312     (gnus-set-mode-line 'summary)
9313     not-deleted))
9314
9315 (defun gnus-summary-edit-article (&optional arg)
9316   "Edit the current article.
9317 This will have permanent effect only in mail groups.
9318 If ARG is nil, edit the decoded articles.
9319 If ARG is 1, edit the raw articles.
9320 If ARG is 2, edit the raw articles even in read-only groups.
9321 If ARG is 3, edit the articles with the current handles.
9322 Otherwise, allow editing of articles even in read-only
9323 groups."
9324   (interactive "P")
9325   (let (force raw current-handles)
9326     (cond
9327      ((null arg))
9328      ((eq arg 1)
9329       (setq raw t))
9330      ((eq arg 2)
9331       (setq raw t
9332             force t))
9333      ((eq arg 3)
9334       (setq current-handles
9335             (and (gnus-buffer-live-p gnus-article-buffer)
9336                  (with-current-buffer gnus-article-buffer
9337                    (prog1
9338                        gnus-article-mime-handles
9339                      (setq gnus-article-mime-handles nil))))))
9340      (t
9341       (setq force t)))
9342     (when (and raw (not force)
9343                (member gnus-newsgroup-name '("nndraft:delayed"
9344                                              "nndraft:drafts"
9345                                              "nndraft:queue")))
9346       (error "Can't edit the raw article in group %s"
9347              gnus-newsgroup-name))
9348     (save-excursion
9349       (set-buffer gnus-summary-buffer)
9350       (let ((mail-parse-charset gnus-newsgroup-charset)
9351             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9352         (gnus-set-global-variables)
9353         (when (and (not force)
9354                    (gnus-group-read-only-p))
9355           (error "The current newsgroup does not support article editing"))
9356         (gnus-summary-show-article t)
9357         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9358           (with-current-buffer gnus-article-buffer
9359             (mm-enable-multibyte)))
9360         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9361             (setq raw t))
9362         (gnus-article-edit-article
9363          (if raw 'ignore
9364            `(lambda ()
9365               (let ((mbl mml-buffer-list))
9366                 (setq mml-buffer-list nil)
9367                 (mime-to-mml ,'current-handles)
9368                 (let ((mbl1 mml-buffer-list))
9369                   (setq mml-buffer-list mbl)
9370                   (set (make-local-variable 'mml-buffer-list) mbl1))
9371                 (gnus-make-local-hook 'kill-buffer-hook)
9372                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9373          `(lambda (no-highlight)
9374             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9375                   (message-options message-options)
9376                   (message-options-set-recipient)
9377                   (mail-parse-ignored-charsets
9378                    ',gnus-newsgroup-ignored-charsets))
9379               ,(if (not raw) '(progn
9380                                 (mml-to-mime)
9381                                 (mml-destroy-buffers)
9382                                 (remove-hook 'kill-buffer-hook
9383                                              'mml-destroy-buffers t)
9384                                 (kill-local-variable 'mml-buffer-list)))
9385               (gnus-summary-edit-article-done
9386                ,(or (mail-header-references gnus-current-headers) "")
9387                ,(gnus-group-read-only-p)
9388                ,gnus-summary-buffer no-highlight))))))))
9389
9390 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9391
9392 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9393                                                  no-highlight)
9394   "Make edits to the current article permanent."
9395   (interactive)
9396   (save-excursion
9397    ;; The buffer restriction contains the entire article if it exists.
9398     (when (article-goto-body)
9399       (let ((lines (count-lines (point) (point-max)))
9400             (length (- (point-max) (point)))
9401             (case-fold-search t)
9402             (body (copy-marker (point))))
9403         (goto-char (point-min))
9404         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9405           (delete-region (match-beginning 1) (match-end 1))
9406           (insert (number-to-string length)))
9407         (goto-char (point-min))
9408         (when (re-search-forward
9409                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9410           (delete-region (match-beginning 1) (match-end 1))
9411           (insert (number-to-string length)))
9412         (goto-char (point-min))
9413         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9414           (delete-region (match-beginning 1) (match-end 1))
9415           (insert (number-to-string lines))))))
9416   ;; Replace the article.
9417   (let ((buf (current-buffer)))
9418     (with-temp-buffer
9419       (insert-buffer-substring buf)
9420
9421       (if (and (not read-only)
9422                (not (gnus-request-replace-article
9423                      (cdr gnus-article-current) (car gnus-article-current)
9424                      (current-buffer) t)))
9425           (error "Couldn't replace article")
9426         ;; Update the summary buffer.
9427         (if (and references
9428                  (equal (message-tokenize-header references " ")
9429                         (message-tokenize-header
9430                          (or (message-fetch-field "references") "") " ")))
9431             ;; We only have to update this line.
9432             (save-excursion
9433               (save-restriction
9434                 (message-narrow-to-head)
9435                 (let ((head (buffer-string))
9436                       header)
9437                   (with-temp-buffer
9438                     (insert (format "211 %d Article retrieved.\n"
9439                                     (cdr gnus-article-current)))
9440                     (insert head)
9441                     (insert ".\n")
9442                     (let ((nntp-server-buffer (current-buffer)))
9443                       (setq header (car (gnus-get-newsgroup-headers
9444                                          nil t))))
9445                     (save-excursion
9446                       (set-buffer gnus-summary-buffer)
9447                       (gnus-data-set-header
9448                        (gnus-data-find (cdr gnus-article-current))
9449                        header)
9450                       (gnus-summary-update-article-line
9451                        (cdr gnus-article-current) header)
9452                       (if (gnus-summary-goto-subject
9453                            (cdr gnus-article-current) nil t)
9454                           (gnus-summary-update-secondary-mark
9455                            (cdr gnus-article-current))))))))
9456           ;; Update threads.
9457           (set-buffer (or buffer gnus-summary-buffer))
9458           (gnus-summary-update-article (cdr gnus-article-current))
9459           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9460               (gnus-summary-update-secondary-mark
9461                (cdr gnus-article-current))))
9462         ;; Prettify the article buffer again.
9463         (unless no-highlight
9464           (save-excursion
9465             (set-buffer gnus-article-buffer)
9466             ;;;!!! Fix this -- article should be rehighlighted.
9467             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9468             (set-buffer gnus-original-article-buffer)
9469             (gnus-request-article
9470              (cdr gnus-article-current)
9471              (car gnus-article-current) (current-buffer))))
9472         ;; Prettify the summary buffer line.
9473         (when (gnus-visual-p 'summary-highlight 'highlight)
9474           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9475
9476 (defun gnus-summary-edit-wash (key)
9477   "Perform editing command KEY in the article buffer."
9478   (interactive
9479    (list
9480     (progn
9481       (message "%s" (concat (this-command-keys) "- "))
9482       (read-char))))
9483   (message "")
9484   (gnus-summary-edit-article)
9485   (execute-kbd-macro (concat (this-command-keys) key))
9486   (gnus-article-edit-done))
9487
9488 ;;; Respooling
9489
9490 (defun gnus-summary-respool-query (&optional silent trace)
9491   "Query where the respool algorithm would put this article."
9492   (interactive)
9493   (let (gnus-mark-article-hook)
9494     (gnus-summary-select-article)
9495     (save-excursion
9496       (set-buffer gnus-original-article-buffer)
9497       (let ((groups (nnmail-article-group 'identity trace)))
9498         (unless silent
9499           (if groups
9500               (message "This message would go to %s"
9501                        (mapconcat 'car groups ", "))
9502             (message "This message would go to no groups"))
9503           groups)))))
9504
9505 (defun gnus-summary-respool-trace ()
9506   "Trace where the respool algorithm would put this article.
9507 Display a buffer showing all fancy splitting patterns which matched."
9508   (interactive)
9509   (gnus-summary-respool-query nil t))
9510
9511 ;; Summary marking commands.
9512
9513 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9514   "Mark articles which has the same subject as read, and then select the next.
9515 If UNMARK is positive, remove any kind of mark.
9516 If UNMARK is negative, tick articles."
9517   (interactive "P")
9518   (when unmark
9519     (setq unmark (prefix-numeric-value unmark)))
9520   (let ((count
9521          (gnus-summary-mark-same-subject
9522           (gnus-summary-article-subject) unmark)))
9523     ;; Select next unread article.  If auto-select-same mode, should
9524     ;; select the first unread article.
9525     (gnus-summary-next-article t (and gnus-auto-select-same
9526                                       (gnus-summary-article-subject)))
9527     (gnus-message 7 "%d article%s marked as %s"
9528                   count (if (= count 1) " is" "s are")
9529                   (if unmark "unread" "read"))))
9530
9531 (defun gnus-summary-kill-same-subject (&optional unmark)
9532   "Mark articles which has the same subject as read.
9533 If UNMARK is positive, remove any kind of mark.
9534 If UNMARK is negative, tick articles."
9535   (interactive "P")
9536   (when unmark
9537     (setq unmark (prefix-numeric-value unmark)))
9538   (let ((count
9539          (gnus-summary-mark-same-subject
9540           (gnus-summary-article-subject) unmark)))
9541     ;; If marked as read, go to next unread subject.
9542     (when (null unmark)
9543       ;; Go to next unread subject.
9544       (gnus-summary-next-subject 1 t))
9545     (gnus-message 7 "%d articles are marked as %s"
9546                   count (if unmark "unread" "read"))))
9547
9548 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9549   "Mark articles with same SUBJECT as read, and return marked number.
9550 If optional argument UNMARK is positive, remove any kinds of marks.
9551 If optional argument UNMARK is negative, mark articles as unread instead."
9552   (let ((count 1))
9553     (save-excursion
9554       (cond
9555        ((null unmark)                   ; Mark as read.
9556         (while (and
9557                 (progn
9558                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9559                   (gnus-summary-show-thread) t)
9560                 (gnus-summary-find-subject subject))
9561           (setq count (1+ count))))
9562        ((> unmark 0)                    ; Tick.
9563         (while (and
9564                 (progn
9565                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9566                   (gnus-summary-show-thread) t)
9567                 (gnus-summary-find-subject subject))
9568           (setq count (1+ count))))
9569        (t                               ; Mark as unread.
9570         (while (and
9571                 (progn
9572                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9573                   (gnus-summary-show-thread) t)
9574                 (gnus-summary-find-subject subject))
9575           (setq count (1+ count)))))
9576       (gnus-set-mode-line 'summary)
9577       ;; Return the number of marked articles.
9578       count)))
9579
9580 (defun gnus-summary-mark-as-processable (n &optional unmark)
9581   "Set the process mark on the next N articles.
9582 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9583 the process mark instead.  The difference between N and the actual
9584 number of articles marked is returned."
9585   (interactive "P")
9586   (if (and (null n) (gnus-region-active-p))
9587       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9588     (setq n (prefix-numeric-value n))
9589     (let ((backward (< n 0))
9590           (n (abs n)))
9591       (while (and
9592               (> n 0)
9593               (if unmark
9594                   (gnus-summary-remove-process-mark
9595                    (gnus-summary-article-number))
9596                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9597               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9598         (setq n (1- n)))
9599       (when (/= 0 n)
9600         (gnus-message 7 "No more articles"))
9601       (gnus-summary-recenter)
9602       (gnus-summary-position-point)
9603       n)))
9604
9605 (defun gnus-summary-unmark-as-processable (n)
9606   "Remove the process mark from the next N articles.
9607 If N is negative, unmark backward instead.  The difference between N and
9608 the actual number of articles unmarked is returned."
9609   (interactive "P")
9610   (gnus-summary-mark-as-processable n t))
9611
9612 (defun gnus-summary-unmark-all-processable ()
9613   "Remove the process mark from all articles."
9614   (interactive)
9615   (save-excursion
9616     (while gnus-newsgroup-processable
9617       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9618   (gnus-summary-position-point))
9619
9620 (defun gnus-summary-add-mark (article type)
9621   "Mark ARTICLE with a mark of TYPE."
9622   (let ((vtype (car (assq type gnus-article-mark-lists)))
9623         var)
9624     (if (not vtype)
9625         (error "No such mark type: %s" type)
9626       (setq var (intern (format "gnus-newsgroup-%s" type)))
9627       (set var (cons article (symbol-value var)))
9628       (if (memq type '(processable cached replied forwarded recent saved))
9629           (gnus-summary-update-secondary-mark article)
9630         ;;; !!! This is bogus.  We should find out what primary
9631         ;;; !!! mark we want to set.
9632         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9633
9634 (defun gnus-summary-mark-as-expirable (n)
9635   "Mark N articles forward as expirable.
9636 If N is negative, mark backward instead.  The difference between N and
9637 the actual number of articles marked is returned."
9638   (interactive "p")
9639   (gnus-summary-mark-forward n gnus-expirable-mark))
9640
9641 (defun gnus-summary-mark-as-spam (n)
9642   "Mark N articles forward as spam.
9643 If N is negative, mark backward instead.  The difference between N and
9644 the actual number of articles marked is returned."
9645   (interactive "p")
9646   (gnus-summary-mark-forward n gnus-spam-mark))
9647
9648 (defun gnus-summary-mark-article-as-replied (article)
9649   "Mark ARTICLE as replied to and update the summary line.
9650 ARTICLE can also be a list of articles."
9651   (interactive (list (gnus-summary-article-number)))
9652   (let ((articles (if (listp article) article (list article))))
9653     (dolist (article articles)
9654       (unless (numberp article)
9655         (error "%s is not a number" article))
9656       (push article gnus-newsgroup-replied)
9657       (let ((buffer-read-only nil))
9658         (when (gnus-summary-goto-subject article nil t)
9659           (gnus-summary-update-secondary-mark article))))))
9660
9661 (defun gnus-summary-mark-article-as-forwarded (article)
9662   "Mark ARTICLE as forwarded and update the summary line.
9663 ARTICLE can also be a list of articles."
9664   (let ((articles (if (listp article) article (list article))))
9665     (dolist (article articles)
9666       (push article gnus-newsgroup-forwarded)
9667       (let ((buffer-read-only nil))
9668         (when (gnus-summary-goto-subject article nil t)
9669           (gnus-summary-update-secondary-mark article))))))
9670
9671 (defun gnus-summary-set-bookmark (article)
9672   "Set a bookmark in current article."
9673   (interactive (list (gnus-summary-article-number)))
9674   (when (or (not (get-buffer gnus-article-buffer))
9675             (not gnus-current-article)
9676             (not gnus-article-current)
9677             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9678     (error "No current article selected"))
9679   ;; Remove old bookmark, if one exists.
9680   (gnus-pull article gnus-newsgroup-bookmarks)
9681   ;; Set the new bookmark, which is on the form
9682   ;; (article-number . line-number-in-body).
9683   (push
9684    (cons article
9685          (save-excursion
9686            (set-buffer gnus-article-buffer)
9687            (count-lines
9688             (min (point)
9689                  (save-excursion
9690                    (article-goto-body)
9691                    (point)))
9692             (point))))
9693    gnus-newsgroup-bookmarks)
9694   (gnus-message 6 "A bookmark has been added to the current article."))
9695
9696 (defun gnus-summary-remove-bookmark (article)
9697   "Remove the bookmark from the current article."
9698   (interactive (list (gnus-summary-article-number)))
9699   ;; Remove old bookmark, if one exists.
9700   (if (not (assq article gnus-newsgroup-bookmarks))
9701       (gnus-message 6 "No bookmark in current article.")
9702     (gnus-pull article gnus-newsgroup-bookmarks)
9703     (gnus-message 6 "Removed bookmark.")))
9704
9705 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9706 (defun gnus-summary-mark-as-dormant (n)
9707   "Mark N articles forward as dormant.
9708 If N is negative, mark backward instead.  The difference between N and
9709 the actual number of articles marked is returned."
9710   (interactive "p")
9711   (gnus-summary-mark-forward n gnus-dormant-mark))
9712
9713 (defun gnus-summary-set-process-mark (article)
9714   "Set the process mark on ARTICLE and update the summary line."
9715   (setq gnus-newsgroup-processable
9716         (cons article
9717               (delq article gnus-newsgroup-processable)))
9718   (when (gnus-summary-goto-subject article)
9719     (gnus-summary-show-thread)
9720     (gnus-summary-goto-subject article)
9721     (gnus-summary-update-secondary-mark article)))
9722
9723 (defun gnus-summary-remove-process-mark (article)
9724   "Remove the process mark from ARTICLE and update the summary line."
9725   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9726   (when (gnus-summary-goto-subject article)
9727     (gnus-summary-show-thread)
9728     (gnus-summary-goto-subject article)
9729     (gnus-summary-update-secondary-mark article)))
9730
9731 (defun gnus-summary-set-saved-mark (article)
9732   "Set the process mark on ARTICLE and update the summary line."
9733   (push article gnus-newsgroup-saved)
9734   (when (gnus-summary-goto-subject article)
9735     (gnus-summary-update-secondary-mark article)))
9736
9737 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9738   "Mark N articles as read forwards.
9739 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9740 The difference between N and the actual number of articles marked is
9741 returned.
9742 If NO-EXPIRE, auto-expiry will be inhibited."
9743   (interactive "p")
9744   (gnus-summary-show-thread)
9745   (let ((backward (< n 0))
9746         (gnus-summary-goto-unread
9747          (and gnus-summary-goto-unread
9748               (not (eq gnus-summary-goto-unread 'never))
9749               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9750                                     gnus-ticked-mark gnus-dormant-mark)))))
9751         (n (abs n))
9752         (mark (or mark gnus-del-mark)))
9753     (while (and (> n 0)
9754                 (gnus-summary-mark-article nil mark no-expire)
9755                 (zerop (gnus-summary-next-subject
9756                         (if backward -1 1)
9757                         (and gnus-summary-goto-unread
9758                              (not (eq gnus-summary-goto-unread 'never)))
9759                         t)))
9760       (setq n (1- n)))
9761     (when (/= 0 n)
9762       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9763     (gnus-summary-recenter)
9764     (gnus-summary-position-point)
9765     (gnus-set-mode-line 'summary)
9766     n))
9767
9768 (defun gnus-summary-mark-article-as-read (mark)
9769   "Mark the current article quickly as read with MARK."
9770   (let ((article (gnus-summary-article-number)))
9771     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9772     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9773     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9774     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9775     (push (cons article mark) gnus-newsgroup-reads)
9776     ;; Possibly remove from cache, if that is used.
9777     (when gnus-use-cache
9778       (gnus-cache-enter-remove-article article))
9779     ;; Allow the backend to change the mark.
9780     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9781     ;; Check for auto-expiry.
9782     (when (and gnus-newsgroup-auto-expire
9783                (memq mark gnus-auto-expirable-marks))
9784       (setq mark gnus-expirable-mark)
9785       ;; Let the backend know about the mark change.
9786       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9787       (push article gnus-newsgroup-expirable))
9788     ;; Set the mark in the buffer.
9789     (gnus-summary-update-mark mark 'unread)
9790     t))
9791
9792 (defun gnus-summary-mark-article-as-unread (mark)
9793   "Mark the current article quickly as unread with MARK."
9794   (let* ((article (gnus-summary-article-number))
9795          (old-mark (gnus-summary-article-mark article)))
9796     ;; Allow the backend to change the mark.
9797     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9798     (if (eq mark old-mark)
9799         t
9800       (if (<= article 0)
9801           (progn
9802             (gnus-error 1 "Can't mark negative article numbers")
9803             nil)
9804         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9805         (setq gnus-newsgroup-spam-marked
9806               (delq article gnus-newsgroup-spam-marked))
9807         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9808         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9809         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9810         (cond ((= mark gnus-ticked-mark)
9811                (setq gnus-newsgroup-marked
9812                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9813                                               article)))
9814               ((= mark gnus-spam-mark)
9815                (setq gnus-newsgroup-spam-marked
9816                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9817                                               article)))
9818               ((= mark gnus-dormant-mark)
9819                (setq gnus-newsgroup-dormant
9820                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9821                                               article)))
9822               (t
9823                (setq gnus-newsgroup-unreads
9824                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9825                                               article))))
9826         (gnus-pull article gnus-newsgroup-reads)
9827
9828         ;; See whether the article is to be put in the cache.
9829         (and gnus-use-cache
9830              (vectorp (gnus-summary-article-header article))
9831              (save-excursion
9832                (gnus-cache-possibly-enter-article
9833                 gnus-newsgroup-name article
9834                 (= mark gnus-ticked-mark)
9835                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9836
9837         ;; Fix the mark.
9838         (gnus-summary-update-mark mark 'unread)
9839         t))))
9840
9841 (defun gnus-summary-mark-article (&optional article mark no-expire)
9842   "Mark ARTICLE with MARK.  MARK can be any character.
9843 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9844 `??' (dormant) and `?E' (expirable).
9845 If MARK is nil, then the default character `?r' is used.
9846 If ARTICLE is nil, then the article on the current line will be
9847 marked.
9848 If NO-EXPIRE, auto-expiry will be inhibited."
9849   ;; The mark might be a string.
9850   (when (stringp mark)
9851     (setq mark (aref mark 0)))
9852   ;; If no mark is given, then we check auto-expiring.
9853   (when (null mark)
9854     (setq mark gnus-del-mark))
9855   (when (and (not no-expire)
9856              gnus-newsgroup-auto-expire
9857              (memq mark gnus-auto-expirable-marks))
9858     (setq mark gnus-expirable-mark))
9859   (let ((article (or article (gnus-summary-article-number)))
9860         (old-mark (gnus-summary-article-mark article)))
9861     ;; Allow the backend to change the mark.
9862     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9863     (if (eq mark old-mark)
9864         t
9865       (unless article
9866         (error "No article on current line"))
9867       (if (not (if (or (= mark gnus-unread-mark)
9868                        (= mark gnus-ticked-mark)
9869                        (= mark gnus-spam-mark)
9870                        (= mark gnus-dormant-mark))
9871                    (gnus-mark-article-as-unread article mark)
9872                  (gnus-mark-article-as-read article mark)))
9873           t
9874         ;; See whether the article is to be put in the cache.
9875         (and gnus-use-cache
9876              (not (= mark gnus-canceled-mark))
9877              (vectorp (gnus-summary-article-header article))
9878              (save-excursion
9879                (gnus-cache-possibly-enter-article
9880                 gnus-newsgroup-name article
9881                 (= mark gnus-ticked-mark)
9882                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9883
9884         (when (gnus-summary-goto-subject article nil t)
9885           (let ((buffer-read-only nil))
9886             (gnus-summary-show-thread)
9887             ;; Fix the mark.
9888             (gnus-summary-update-mark mark 'unread)
9889             t))))))
9890
9891 (defun gnus-summary-update-secondary-mark (article)
9892   "Update the secondary (read, process, cache) mark."
9893   (gnus-summary-update-mark
9894    (cond ((memq article gnus-newsgroup-processable)
9895           gnus-process-mark)
9896          ((memq article gnus-newsgroup-cached)
9897           gnus-cached-mark)
9898          ((memq article gnus-newsgroup-replied)
9899           gnus-replied-mark)
9900          ((memq article gnus-newsgroup-forwarded)
9901           gnus-forwarded-mark)
9902          ((memq article gnus-newsgroup-saved)
9903           gnus-saved-mark)
9904          ((memq article gnus-newsgroup-recent)
9905           gnus-recent-mark)
9906          ((memq article gnus-newsgroup-unseen)
9907           gnus-unseen-mark)
9908          (t gnus-no-mark))
9909    'replied)
9910   (when (gnus-visual-p 'summary-highlight 'highlight)
9911     (gnus-run-hooks 'gnus-summary-update-hook))
9912   t)
9913
9914 (defun gnus-summary-update-download-mark (article)
9915   "Update the download mark."
9916   (gnus-summary-update-mark
9917    (cond ((memq article gnus-newsgroup-undownloaded)
9918           gnus-undownloaded-mark)
9919          (gnus-newsgroup-agentized
9920           gnus-downloaded-mark)
9921          (t
9922           gnus-no-mark))
9923    'download)
9924   (gnus-summary-update-line t)
9925   t)
9926
9927 (defun gnus-summary-update-mark (mark type)
9928   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9929         (buffer-read-only nil))
9930     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9931     (when forward
9932       (when (looking-at "\r")
9933         (incf forward))
9934       (when (<= (+ forward (point)) (point-max))
9935         ;; Go to the right position on the line.
9936         (goto-char (+ forward (point)))
9937         ;; Replace the old mark with the new mark.
9938         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9939         ;; Optionally update the marks by some user rule.
9940         (when (eq type 'unread)
9941           (gnus-data-set-mark
9942            (gnus-data-find (gnus-summary-article-number)) mark)
9943           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9944
9945 (defun gnus-mark-article-as-read (article &optional mark)
9946   "Enter ARTICLE in the pertinent lists and remove it from others."
9947   ;; Make the article expirable.
9948   (let ((mark (or mark gnus-del-mark)))
9949     (setq gnus-newsgroup-expirable
9950           (if (= mark gnus-expirable-mark)
9951               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9952             (delq article gnus-newsgroup-expirable)))
9953     ;; Remove from unread and marked lists.
9954     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9955     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9956     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9957     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9958     (push (cons article mark) gnus-newsgroup-reads)
9959     ;; Possibly remove from cache, if that is used.
9960     (when gnus-use-cache
9961       (gnus-cache-enter-remove-article article))
9962     t))
9963
9964 (defun gnus-mark-article-as-unread (article &optional mark)
9965   "Enter ARTICLE in the pertinent lists and remove it from others."
9966   (let ((mark (or mark gnus-ticked-mark)))
9967     (if (<= article 0)
9968         (progn
9969           (gnus-error 1 "Can't mark negative article numbers")
9970           nil)
9971       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9972             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9973             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9974             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9975             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9976
9977       ;; Unsuppress duplicates?
9978       (when gnus-suppress-duplicates
9979         (gnus-dup-unsuppress-article article))
9980
9981       (cond ((= mark gnus-ticked-mark)
9982              (setq gnus-newsgroup-marked
9983                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9984             ((= mark gnus-spam-mark)
9985              (setq gnus-newsgroup-spam-marked
9986                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9987                                             article)))
9988             ((= mark gnus-dormant-mark)
9989              (setq gnus-newsgroup-dormant
9990                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9991             (t
9992              (setq gnus-newsgroup-unreads
9993                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9994       (gnus-pull article gnus-newsgroup-reads)
9995       t)))
9996
9997 (defalias 'gnus-summary-mark-as-unread-forward
9998   'gnus-summary-tick-article-forward)
9999 (make-obsolete 'gnus-summary-mark-as-unread-forward
10000                'gnus-summary-tick-article-forward)
10001 (defun gnus-summary-tick-article-forward (n)
10002   "Tick N articles forwards.
10003 If N is negative, tick backwards instead.
10004 The difference between N and the number of articles ticked is returned."
10005   (interactive "p")
10006   (gnus-summary-mark-forward n gnus-ticked-mark))
10007
10008 (defalias 'gnus-summary-mark-as-unread-backward
10009   'gnus-summary-tick-article-backward)
10010 (make-obsolete 'gnus-summary-mark-as-unread-backward
10011                'gnus-summary-tick-article-backward)
10012 (defun gnus-summary-tick-article-backward (n)
10013   "Tick N articles backwards.
10014 The difference between N and the number of articles ticked is returned."
10015   (interactive "p")
10016   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10017
10018 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10019 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10020 (defun gnus-summary-tick-article (&optional article clear-mark)
10021   "Mark current article as unread.
10022 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10023 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10024   (interactive)
10025   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10026                                        gnus-ticked-mark)))
10027
10028 (defun gnus-summary-mark-as-read-forward (n)
10029   "Mark N articles as read forwards.
10030 If N is negative, mark backwards instead.
10031 The difference between N and the actual number of articles marked is
10032 returned."
10033   (interactive "p")
10034   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10035
10036 (defun gnus-summary-mark-as-read-backward (n)
10037   "Mark the N articles as read backwards.
10038 The difference between N and the actual number of articles marked is
10039 returned."
10040   (interactive "p")
10041   (gnus-summary-mark-forward
10042    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10043
10044 (defun gnus-summary-mark-as-read (&optional article mark)
10045   "Mark current article as read.
10046 ARTICLE specifies the article to be marked as read.
10047 MARK specifies a string to be inserted at the beginning of the line."
10048   (gnus-summary-mark-article article mark))
10049
10050 (defun gnus-summary-clear-mark-forward (n)
10051   "Clear marks from N articles forward.
10052 If N is negative, clear backward instead.
10053 The difference between N and the number of marks cleared is returned."
10054   (interactive "p")
10055   (gnus-summary-mark-forward n gnus-unread-mark))
10056
10057 (defun gnus-summary-clear-mark-backward (n)
10058   "Clear marks from N articles backward.
10059 The difference between N and the number of marks cleared is returned."
10060   (interactive "p")
10061   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10062
10063 (defun gnus-summary-mark-unread-as-read ()
10064   "Intended to be used by `gnus-summary-mark-article-hook'."
10065   (when (memq gnus-current-article gnus-newsgroup-unreads)
10066     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10067
10068 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10069   "Intended to be used by `gnus-summary-mark-article-hook'."
10070   (let ((mark (gnus-summary-article-mark)))
10071     (when (or (gnus-unread-mark-p mark)
10072               (gnus-read-mark-p mark))
10073       (gnus-summary-mark-article gnus-current-article
10074                                  (or new-mark gnus-read-mark)))))
10075
10076 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10077   "Intended to be used by `gnus-summary-mark-article-hook'."
10078   (let ((mark (gnus-summary-article-mark)))
10079     (when (or (gnus-unread-mark-p mark)
10080               (gnus-read-mark-p mark))
10081       (gnus-summary-mark-article (gnus-summary-article-number)
10082                                  (or new-mark gnus-read-mark)))))
10083
10084 (defun gnus-summary-mark-unread-as-ticked ()
10085   "Intended to be used by `gnus-summary-mark-article-hook'."
10086   (when (memq gnus-current-article gnus-newsgroup-unreads)
10087     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10088
10089 (defun gnus-summary-mark-region-as-read (point mark all)
10090   "Mark all unread articles between point and mark as read.
10091 If given a prefix, mark all articles between point and mark as read,
10092 even ticked and dormant ones."
10093   (interactive "r\nP")
10094   (save-excursion
10095     (let (article)
10096       (goto-char point)
10097       (beginning-of-line)
10098       (while (and
10099               (< (point) mark)
10100               (progn
10101                 (when (or all
10102                           (memq (setq article (gnus-summary-article-number))
10103                                 gnus-newsgroup-unreads))
10104                   (gnus-summary-mark-article article gnus-del-mark))
10105                 t)
10106               (gnus-summary-find-next))))))
10107
10108 (defun gnus-summary-mark-below (score mark)
10109   "Mark articles with score less than SCORE with MARK."
10110   (interactive "P\ncMark: ")
10111   (setq score (if score
10112                   (prefix-numeric-value score)
10113                 (or gnus-summary-default-score 0)))
10114   (save-excursion
10115     (set-buffer gnus-summary-buffer)
10116     (goto-char (point-min))
10117     (while
10118         (progn
10119           (and (< (gnus-summary-article-score) score)
10120                (gnus-summary-mark-article nil mark))
10121           (gnus-summary-find-next)))))
10122
10123 (defun gnus-summary-kill-below (&optional score)
10124   "Mark articles with score below SCORE as read."
10125   (interactive "P")
10126   (gnus-summary-mark-below score gnus-killed-mark))
10127
10128 (defun gnus-summary-clear-above (&optional score)
10129   "Clear all marks from articles with score above SCORE."
10130   (interactive "P")
10131   (gnus-summary-mark-above score gnus-unread-mark))
10132
10133 (defun gnus-summary-tick-above (&optional score)
10134   "Tick all articles with score above SCORE."
10135   (interactive "P")
10136   (gnus-summary-mark-above score gnus-ticked-mark))
10137
10138 (defun gnus-summary-mark-above (score mark)
10139   "Mark articles with score over SCORE with MARK."
10140   (interactive "P\ncMark: ")
10141   (setq score (if score
10142                   (prefix-numeric-value score)
10143                 (or gnus-summary-default-score 0)))
10144   (save-excursion
10145     (set-buffer gnus-summary-buffer)
10146     (goto-char (point-min))
10147     (while (and (progn
10148                   (when (> (gnus-summary-article-score) score)
10149                     (gnus-summary-mark-article nil mark))
10150                   t)
10151                 (gnus-summary-find-next)))))
10152
10153 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10154 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10155 (defun gnus-summary-limit-include-expunged (&optional no-error)
10156   "Display all the hidden articles that were expunged for low scores."
10157   (interactive)
10158   (let ((buffer-read-only nil))
10159     (let ((scored gnus-newsgroup-scored)
10160           headers h)
10161       (while scored
10162         (unless (gnus-summary-article-header (caar scored))
10163           (and (setq h (gnus-number-to-header (caar scored)))
10164                (< (cdar scored) gnus-summary-expunge-below)
10165                (push h headers)))
10166         (setq scored (cdr scored)))
10167       (if (not headers)
10168           (when (not no-error)
10169             (error "No expunged articles hidden"))
10170         (goto-char (point-min))
10171         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10172         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10173         (mapcar (lambda (x) (push (mail-header-number x)
10174                                   gnus-newsgroup-limit))
10175                 headers)
10176         (gnus-summary-prepare-unthreaded (nreverse headers))
10177         (goto-char (point-min))
10178         (gnus-summary-position-point)
10179         t))))
10180
10181 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10182   "Mark all unread articles in this newsgroup as read.
10183 If prefix argument ALL is non-nil, ticked and dormant articles will
10184 also be marked as read.
10185 If QUIETLY is non-nil, no questions will be asked.
10186
10187 If TO-HERE is non-nil, it should be a point in the buffer.  All
10188 articles before (after, if REVERSE is set) this point will be marked
10189 as read.
10190
10191 Note that this function will only catch up the unread article
10192 in the current summary buffer limitation.
10193
10194 The number of articles marked as read is returned."
10195   (interactive "P")
10196   (prog1
10197       (save-excursion
10198         (when (or quietly
10199                   (not gnus-interactive-catchup) ;Without confirmation?
10200                   gnus-expert-user
10201                   (gnus-y-or-n-p
10202                    (if all
10203                        "Mark absolutely all articles as read? "
10204                      "Mark all unread articles as read? ")))
10205           (if (and not-mark
10206                    (not gnus-newsgroup-adaptive)
10207                    (not gnus-newsgroup-auto-expire)
10208                    (not gnus-suppress-duplicates)
10209                    (or (not gnus-use-cache)
10210                        (eq gnus-use-cache 'passive)))
10211               (progn
10212                 (when all
10213                   (setq gnus-newsgroup-marked nil
10214                         gnus-newsgroup-spam-marked nil
10215                         gnus-newsgroup-dormant nil))
10216                 (setq gnus-newsgroup-unreads
10217                       (gnus-sorted-nunion
10218                        (gnus-intersection gnus-newsgroup-unreads
10219                                           gnus-newsgroup-downloadable)
10220                        gnus-newsgroup-unfetched)))
10221             ;; We actually mark all articles as canceled, which we
10222             ;; have to do when using auto-expiry or adaptive scoring.
10223             (gnus-summary-show-all-threads)
10224             (if (and to-here reverse)
10225                 (progn
10226                   (goto-char to-here)
10227                   (gnus-summary-mark-current-read-and-unread-as-read
10228                    gnus-catchup-mark)
10229                   (while (gnus-summary-find-next (not all))
10230                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10231               (when (gnus-summary-first-subject (not all))
10232                 (while (and
10233                         (if to-here (< (point) to-here) t)
10234                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10235                         (gnus-summary-find-next (not all))))))
10236             (gnus-set-mode-line 'summary))
10237           t))
10238     (gnus-summary-position-point)))
10239
10240 (defun gnus-summary-catchup-to-here (&optional all)
10241   "Mark all unticked articles before the current one as read.
10242 If ALL is non-nil, also mark ticked and dormant articles as read."
10243   (interactive "P")
10244   (save-excursion
10245     (gnus-save-hidden-threads
10246       (let ((beg (point)))
10247         ;; We check that there are unread articles.
10248         (when (or all (gnus-summary-find-prev))
10249           (gnus-summary-catchup all t beg)))))
10250   (gnus-summary-position-point))
10251
10252 (defun gnus-summary-catchup-from-here (&optional all)
10253   "Mark all unticked articles after (and including) the current one as read.
10254 If ALL is non-nil, also mark ticked and dormant articles as read."
10255   (interactive "P")
10256   (save-excursion
10257     (gnus-save-hidden-threads
10258       (let ((beg (point)))
10259         ;; We check that there are unread articles.
10260         (when (or all (gnus-summary-find-next))
10261           (gnus-summary-catchup all t beg nil t)))))
10262   (gnus-summary-position-point))
10263
10264 (defun gnus-summary-catchup-all (&optional quietly)
10265   "Mark all articles in this newsgroup as read.
10266 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10267 instead, which marks only unread articles as read."
10268   (interactive "P")
10269   (gnus-summary-catchup t quietly))
10270
10271 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10272   "Mark all unread articles in this group as read, then exit.
10273 If prefix argument ALL is non-nil, all articles are marked as read.
10274 If QUIETLY is non-nil, no questions will be asked."
10275   (interactive "P")
10276   (when (gnus-summary-catchup all quietly nil 'fast)
10277     ;; Select next newsgroup or exit.
10278     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10279              (eq gnus-auto-select-next 'quietly))
10280         (gnus-summary-next-group nil)
10281       (gnus-summary-exit))))
10282
10283 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10284   "Mark all articles in this newsgroup as read, and then exit.
10285 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10286 instead, which marks only unread articles as read."
10287   (interactive "P")
10288   (gnus-summary-catchup-and-exit t quietly))
10289
10290 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10291   "Mark all articles in this group as read and select the next group.
10292 If given a prefix, mark all articles, unread as well as ticked, as
10293 read."
10294   (interactive "P")
10295   (save-excursion
10296     (gnus-summary-catchup all))
10297   (gnus-summary-next-group))
10298
10299 ;;;
10300 ;;; with article
10301 ;;;
10302
10303 (defmacro gnus-with-article (article &rest forms)
10304   "Select ARTICLE and perform FORMS in the original article buffer.
10305 Then replace the article with the result."
10306   `(progn
10307      ;; We don't want the article to be marked as read.
10308      (let (gnus-mark-article-hook)
10309        (gnus-summary-select-article t t nil ,article))
10310      (set-buffer gnus-original-article-buffer)
10311      ,@forms
10312      (if (not (gnus-check-backend-function
10313                'request-replace-article (car gnus-article-current)))
10314          (gnus-message 5 "Read-only group; not replacing")
10315        (unless (gnus-request-replace-article
10316                 ,article (car gnus-article-current)
10317                 (current-buffer) t)
10318          (error "Couldn't replace article")))
10319      ;; The cache and backlog have to be flushed somewhat.
10320      (when gnus-keep-backlog
10321        (gnus-backlog-remove-article
10322         (car gnus-article-current) (cdr gnus-article-current)))
10323      (when gnus-use-cache
10324        (gnus-cache-update-article
10325         (car gnus-article-current) (cdr gnus-article-current)))))
10326
10327 (put 'gnus-with-article 'lisp-indent-function 1)
10328 (put 'gnus-with-article 'edebug-form-spec '(form body))
10329
10330 ;; Thread-based commands.
10331
10332 (defun gnus-summary-articles-in-thread (&optional article)
10333   "Return a list of all articles in the current thread.
10334 If ARTICLE is non-nil, return all articles in the thread that starts
10335 with that article."
10336   (let* ((article (or article (gnus-summary-article-number)))
10337          (data (gnus-data-find-list article))
10338          (top-level (gnus-data-level (car data)))
10339          (top-subject
10340           (cond ((null gnus-thread-operation-ignore-subject)
10341                  (gnus-simplify-subject-re
10342                   (mail-header-subject (gnus-data-header (car data)))))
10343                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10344                  (gnus-simplify-subject-fuzzy
10345                   (mail-header-subject (gnus-data-header (car data)))))
10346                 (t nil)))
10347          (end-point (save-excursion
10348                       (if (gnus-summary-go-to-next-thread)
10349                           (point) (point-max))))
10350          articles)
10351     (while (and data
10352                 (< (gnus-data-pos (car data)) end-point))
10353       (when (or (not top-subject)
10354                 (string= top-subject
10355                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10356                              (gnus-simplify-subject-fuzzy
10357                               (mail-header-subject
10358                                (gnus-data-header (car data))))
10359                            (gnus-simplify-subject-re
10360                             (mail-header-subject
10361                              (gnus-data-header (car data)))))))
10362         (push (gnus-data-number (car data)) articles))
10363       (unless (and (setq data (cdr data))
10364                    (> (gnus-data-level (car data)) top-level))
10365         (setq data nil)))
10366     ;; Return the list of articles.
10367     (nreverse articles)))
10368
10369 (defun gnus-summary-rethread-current ()
10370   "Rethread the thread the current article is part of."
10371   (interactive)
10372   (let* ((gnus-show-threads t)
10373          (article (gnus-summary-article-number))
10374          (id (mail-header-id (gnus-summary-article-header)))
10375          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10376     (unless id
10377       (error "No article on the current line"))
10378     (gnus-rebuild-thread id)
10379     (gnus-summary-goto-subject article)))
10380
10381 (defun gnus-summary-reparent-thread ()
10382   "Make the current article child of the marked (or previous) article.
10383
10384 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10385 is non-nil or the Subject: of both articles are the same."
10386   (interactive)
10387   (unless (not (gnus-group-read-only-p))
10388     (error "The current newsgroup does not support article editing"))
10389   (unless (<= (length gnus-newsgroup-processable) 1)
10390     (error "No more than one article may be marked"))
10391   (save-window-excursion
10392     (let ((gnus-article-buffer " *reparent*")
10393           (current-article (gnus-summary-article-number))
10394           ;; First grab the marked article, otherwise one line up.
10395           (parent-article (if (not (null gnus-newsgroup-processable))
10396                               (car gnus-newsgroup-processable)
10397                             (save-excursion
10398                               (if (eq (forward-line -1) 0)
10399                                   (gnus-summary-article-number)
10400                                 (error "Beginning of summary buffer"))))))
10401       (unless (not (eq current-article parent-article))
10402         (error "An article may not be self-referential"))
10403       (let ((message-id (mail-header-id
10404                          (gnus-summary-article-header parent-article))))
10405         (unless (and message-id (not (equal message-id "")))
10406           (error "No message-id in desired parent"))
10407         (gnus-with-article current-article
10408           (save-restriction
10409             (goto-char (point-min))
10410             (message-narrow-to-head)
10411             (if (re-search-forward "^References: " nil t)
10412                 (progn
10413                   (re-search-forward "^[^ \t]" nil t)
10414                   (forward-line -1)
10415                   (end-of-line)
10416                   (insert " " message-id))
10417               (insert "References: " message-id "\n"))))
10418         (set-buffer gnus-summary-buffer)
10419         (gnus-summary-unmark-all-processable)
10420         (gnus-summary-update-article current-article)
10421         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10422             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10423         (gnus-summary-rethread-current)
10424         (gnus-message 3 "Article %d is now the child of article %d"
10425                       current-article parent-article)))))
10426
10427 (defun gnus-summary-toggle-threads (&optional arg)
10428   "Toggle showing conversation threads.
10429 If ARG is positive number, turn showing conversation threads on."
10430   (interactive "P")
10431   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10432     (setq gnus-show-threads
10433           (if (null arg) (not gnus-show-threads)
10434             (> (prefix-numeric-value arg) 0)))
10435     (gnus-summary-prepare)
10436     (gnus-summary-goto-subject current)
10437     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10438     (gnus-summary-position-point)))
10439
10440 (defun gnus-summary-show-all-threads ()
10441   "Show all threads."
10442   (interactive)
10443   (save-excursion
10444     (let ((buffer-read-only nil))
10445       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10446   (gnus-summary-position-point))
10447
10448 (defun gnus-summary-show-thread ()
10449   "Show thread subtrees.
10450 Returns nil if no thread was there to be shown."
10451   (interactive)
10452   (let ((buffer-read-only nil)
10453         (orig (point))
10454         (end (gnus-point-at-eol))
10455         ;; Leave point at bol
10456         (beg (progn (beginning-of-line) (point))))
10457     (prog1
10458         ;; Any hidden lines here?
10459         (search-forward "\r" end t)
10460       (subst-char-in-region beg end ?\^M ?\n t)
10461       (goto-char orig)
10462       (gnus-summary-position-point))))
10463
10464 (defun gnus-summary-maybe-hide-threads ()
10465   "If requested, hide the threads that should be hidden."
10466   (when (and gnus-show-threads
10467              gnus-thread-hide-subtree)
10468     (gnus-summary-hide-all-threads
10469      (if (or (consp gnus-thread-hide-subtree)
10470              (functionp gnus-thread-hide-subtree))
10471          (gnus-make-predicate gnus-thread-hide-subtree)
10472        nil))))
10473
10474 ;;; Hiding predicates.
10475
10476 (defun gnus-article-unread-p (header)
10477   (memq (mail-header-number header) gnus-newsgroup-unreads))
10478
10479 (defun gnus-article-unseen-p (header)
10480   (memq (mail-header-number header) gnus-newsgroup-unseen))
10481
10482 (defun gnus-map-articles (predicate articles)
10483   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10484   (apply 'gnus-or (mapcar predicate
10485                           (mapcar 'gnus-summary-article-header articles))))
10486
10487 (defun gnus-summary-hide-all-threads (&optional predicate)
10488   "Hide all thread subtrees.
10489 If PREDICATE is supplied, threads that satisfy this predicate
10490 will not be hidden."
10491   (interactive)
10492   (save-excursion
10493     (goto-char (point-min))
10494     (let ((end nil))
10495       (while (not end)
10496         (when (or (not predicate)
10497                   (gnus-map-articles
10498                    predicate (gnus-summary-article-children)))
10499             (gnus-summary-hide-thread))
10500         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10501   (gnus-summary-position-point))
10502
10503 (defun gnus-summary-hide-thread ()
10504   "Hide thread subtrees.
10505 If PREDICATE is supplied, threads that satisfy this predicate
10506 will not be hidden.
10507 Returns nil if no threads were there to be hidden."
10508   (interactive)
10509   (let ((buffer-read-only nil)
10510         (start (point))
10511         (article (gnus-summary-article-number)))
10512     (goto-char start)
10513     ;; Go forward until either the buffer ends or the subthread
10514     ;; ends.
10515     (when (and (not (eobp))
10516                (or (zerop (gnus-summary-next-thread 1 t))
10517                    (goto-char (point-max))))
10518       (prog1
10519           (if (and (> (point) start)
10520                    (search-backward "\n" start t))
10521               (progn
10522                 (subst-char-in-region start (point) ?\n ?\^M)
10523                 (gnus-summary-goto-subject article))
10524             (goto-char start)
10525             nil)))))
10526
10527 (defun gnus-summary-go-to-next-thread (&optional previous)
10528   "Go to the same level (or less) next thread.
10529 If PREVIOUS is non-nil, go to previous thread instead.
10530 Return the article number moved to, or nil if moving was impossible."
10531   (let ((level (gnus-summary-thread-level))
10532         (way (if previous -1 1))
10533         (beg (point)))
10534     (forward-line way)
10535     (while (and (not (eobp))
10536                 (< level (gnus-summary-thread-level)))
10537       (forward-line way))
10538     (if (eobp)
10539         (progn
10540           (goto-char beg)
10541           nil)
10542       (setq beg (point))
10543       (prog1
10544           (gnus-summary-article-number)
10545         (goto-char beg)))))
10546
10547 (defun gnus-summary-next-thread (n &optional silent)
10548   "Go to the same level next N'th thread.
10549 If N is negative, search backward instead.
10550 Returns the difference between N and the number of skips actually
10551 done.
10552
10553 If SILENT, don't output messages."
10554   (interactive "p")
10555   (let ((backward (< n 0))
10556         (n (abs n)))
10557     (while (and (> n 0)
10558                 (gnus-summary-go-to-next-thread backward))
10559       (decf n))
10560     (unless silent
10561       (gnus-summary-position-point))
10562     (when (and (not silent) (/= 0 n))
10563       (gnus-message 7 "No more threads"))
10564     n))
10565
10566 (defun gnus-summary-prev-thread (n)
10567   "Go to the same level previous N'th thread.
10568 Returns the difference between N and the number of skips actually
10569 done."
10570   (interactive "p")
10571   (gnus-summary-next-thread (- n)))
10572
10573 (defun gnus-summary-go-down-thread ()
10574   "Go down one level in the current thread."
10575   (let ((children (gnus-summary-article-children)))
10576     (when children
10577       (gnus-summary-goto-subject (car children)))))
10578
10579 (defun gnus-summary-go-up-thread ()
10580   "Go up one level in the current thread."
10581   (let ((parent (gnus-summary-article-parent)))
10582     (when parent
10583       (gnus-summary-goto-subject parent))))
10584
10585 (defun gnus-summary-down-thread (n)
10586   "Go down thread N steps.
10587 If N is negative, go up instead.
10588 Returns the difference between N and how many steps down that were
10589 taken."
10590   (interactive "p")
10591   (let ((up (< n 0))
10592         (n (abs n)))
10593     (while (and (> n 0)
10594                 (if up (gnus-summary-go-up-thread)
10595                   (gnus-summary-go-down-thread)))
10596       (setq n (1- n)))
10597     (gnus-summary-position-point)
10598     (when (/= 0 n)
10599       (gnus-message 7 "Can't go further"))
10600     n))
10601
10602 (defun gnus-summary-up-thread (n)
10603   "Go up thread N steps.
10604 If N is negative, go down instead.
10605 Returns the difference between N and how many steps down that were
10606 taken."
10607   (interactive "p")
10608   (gnus-summary-down-thread (- n)))
10609
10610 (defun gnus-summary-top-thread ()
10611   "Go to the top of the thread."
10612   (interactive)
10613   (while (gnus-summary-go-up-thread))
10614   (gnus-summary-article-number))
10615
10616 (defun gnus-summary-kill-thread (&optional unmark)
10617   "Mark articles under current thread as read.
10618 If the prefix argument is positive, remove any kinds of marks.
10619 If the prefix argument is negative, tick articles instead."
10620   (interactive "P")
10621   (when unmark
10622     (setq unmark (prefix-numeric-value unmark)))
10623   (let ((articles (gnus-summary-articles-in-thread)))
10624     (save-excursion
10625       ;; Expand the thread.
10626       (gnus-summary-show-thread)
10627       ;; Mark all the articles.
10628       (while articles
10629         (gnus-summary-goto-subject (car articles))
10630         (cond ((null unmark)
10631                (gnus-summary-mark-article-as-read gnus-killed-mark))
10632               ((> unmark 0)
10633                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10634               (t
10635                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10636         (setq articles (cdr articles))))
10637     ;; Hide killed subtrees.
10638     (and (null unmark)
10639          gnus-thread-hide-killed
10640          (gnus-summary-hide-thread))
10641     ;; If marked as read, go to next unread subject.
10642     (when (null unmark)
10643       ;; Go to next unread subject.
10644       (gnus-summary-next-subject 1 t)))
10645   (gnus-set-mode-line 'summary))
10646
10647 ;; Summary sorting commands
10648
10649 (defun gnus-summary-sort-by-number (&optional reverse)
10650   "Sort the summary buffer by article number.
10651 Argument REVERSE means reverse order."
10652   (interactive "P")
10653   (gnus-summary-sort 'number reverse))
10654
10655 (defun gnus-summary-sort-by-random (&optional reverse)
10656   "Randomize the order in the summary buffer.
10657 Argument REVERSE means to randomize in reverse order."
10658   (interactive "P")
10659   (gnus-summary-sort 'random reverse))
10660
10661 (defun gnus-summary-sort-by-author (&optional reverse)
10662   "Sort the summary buffer by author name alphabetically.
10663 If `case-fold-search' is non-nil, case of letters is ignored.
10664 Argument REVERSE means reverse order."
10665   (interactive "P")
10666   (gnus-summary-sort 'author reverse))
10667
10668 (defun gnus-summary-sort-by-subject (&optional reverse)
10669   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10670 If `case-fold-search' is non-nil, case of letters is ignored.
10671 Argument REVERSE means reverse order."
10672   (interactive "P")
10673   (gnus-summary-sort 'subject reverse))
10674
10675 (defun gnus-summary-sort-by-date (&optional reverse)
10676   "Sort the summary buffer by date.
10677 Argument REVERSE means reverse order."
10678   (interactive "P")
10679   (gnus-summary-sort 'date reverse))
10680
10681 (defun gnus-summary-sort-by-score (&optional reverse)
10682   "Sort the summary buffer by score.
10683 Argument REVERSE means reverse order."
10684   (interactive "P")
10685   (gnus-summary-sort 'score reverse))
10686
10687 (defun gnus-summary-sort-by-lines (&optional reverse)
10688   "Sort the summary buffer by the number of lines.
10689 Argument REVERSE means reverse order."
10690   (interactive "P")
10691   (gnus-summary-sort 'lines reverse))
10692
10693 (defun gnus-summary-sort-by-chars (&optional reverse)
10694   "Sort the summary buffer by article length.
10695 Argument REVERSE means reverse order."
10696   (interactive "P")
10697   (gnus-summary-sort 'chars reverse))
10698
10699 (defun gnus-summary-sort-by-original (&optional reverse)
10700   "Sort the summary buffer using the default sorting method.
10701 Argument REVERSE means reverse order."
10702   (interactive "P")
10703   (let* ((buffer-read-only)
10704          (gnus-summary-prepare-hook nil))
10705     ;; We do the sorting by regenerating the threads.
10706     (gnus-summary-prepare)
10707     ;; Hide subthreads if needed.
10708     (gnus-summary-maybe-hide-threads)))
10709
10710 (defun gnus-summary-sort (predicate reverse)
10711   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10712   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10713          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10714          (gnus-thread-sort-functions
10715           (if (not reverse)
10716               thread
10717             `(lambda (t1 t2)
10718                (,thread t2 t1))))
10719          (gnus-sort-gathered-threads-function
10720           gnus-thread-sort-functions)
10721          (gnus-article-sort-functions
10722           (if (not reverse)
10723               article
10724             `(lambda (t1 t2)
10725                (,article t2 t1))))
10726          (buffer-read-only)
10727          (gnus-summary-prepare-hook nil))
10728     ;; We do the sorting by regenerating the threads.
10729     (gnus-summary-prepare)
10730     ;; Hide subthreads if needed.
10731     (gnus-summary-maybe-hide-threads)))
10732
10733 ;; Summary saving commands.
10734
10735 (defun gnus-summary-save-article (&optional n not-saved)
10736   "Save the current article using the default saver function.
10737 If N is a positive number, save the N next articles.
10738 If N is a negative number, save the N previous articles.
10739 If N is nil and any articles have been marked with the process mark,
10740 save those articles instead.
10741 The variable `gnus-default-article-saver' specifies the saver function."
10742   (interactive "P")
10743   (let* ((articles (gnus-summary-work-articles n))
10744          (save-buffer (save-excursion
10745                         (nnheader-set-temp-buffer " *Gnus Save*")))
10746          (num (length articles))
10747          header file)
10748     (dolist (article articles)
10749       (setq header (gnus-summary-article-header article))
10750       (if (not (vectorp header))
10751           ;; This is a pseudo-article.
10752           (if (assq 'name header)
10753               (gnus-copy-file (cdr (assq 'name header)))
10754             (gnus-message 1 "Article %d is unsaveable" article))
10755         ;; This is a real article.
10756         (save-window-excursion
10757           (let ((gnus-display-mime-function nil)
10758                 (gnus-article-prepare-hook nil))
10759             (gnus-summary-select-article t nil nil article)))
10760         (save-excursion
10761           (set-buffer save-buffer)
10762           (erase-buffer)
10763           (insert-buffer-substring gnus-original-article-buffer))
10764         (setq file (gnus-article-save save-buffer file num))
10765         (gnus-summary-remove-process-mark article)
10766         (unless not-saved
10767           (gnus-summary-set-saved-mark article))))
10768     (gnus-kill-buffer save-buffer)
10769     (gnus-summary-position-point)
10770     (gnus-set-mode-line 'summary)
10771     n))
10772
10773 (defun gnus-summary-pipe-output (&optional arg headers)
10774   "Pipe the current article to a subprocess.
10775 If N is a positive number, pipe the N next articles.
10776 If N is a negative number, pipe the N previous articles.
10777 If N is nil and any articles have been marked with the process mark,
10778 pipe those articles instead.
10779 If HEADERS (the symbolic prefix), include the headers, too."
10780   (interactive (gnus-interactive "P\ny"))
10781   (require 'gnus-art)
10782   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10783         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10784     (gnus-summary-save-article arg t))
10785   (let ((buffer (get-buffer "*Shell Command Output*")))
10786     (when (and buffer
10787                (not (zerop (buffer-size buffer))))
10788       (gnus-configure-windows 'pipe))))
10789
10790 (defun gnus-summary-save-article-mail (&optional arg)
10791   "Append the current article to an mail file.
10792 If N is a positive number, save the N next articles.
10793 If N is a negative number, save the N previous articles.
10794 If N is nil and any articles have been marked with the process mark,
10795 save those articles instead."
10796   (interactive "P")
10797   (require 'gnus-art)
10798   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10799     (gnus-summary-save-article arg)))
10800
10801 (defun gnus-summary-save-article-rmail (&optional arg)
10802   "Append the current article to an rmail file.
10803 If N is a positive number, save the N next articles.
10804 If N is a negative number, save the N previous articles.
10805 If N is nil and any articles have been marked with the process mark,
10806 save those articles instead."
10807   (interactive "P")
10808   (require 'gnus-art)
10809   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10810     (gnus-summary-save-article arg)))
10811
10812 (defun gnus-summary-save-article-file (&optional arg)
10813   "Append the current article to a file.
10814 If N is a positive number, save the N next articles.
10815 If N is a negative number, save the N previous articles.
10816 If N is nil and any articles have been marked with the process mark,
10817 save those articles instead."
10818   (interactive "P")
10819   (require 'gnus-art)
10820   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10821     (gnus-summary-save-article arg)))
10822
10823 (defun gnus-summary-write-article-file (&optional arg)
10824   "Write the current article to a file, deleting the previous file.
10825 If N is a positive number, save the N next articles.
10826 If N is a negative number, save the N previous articles.
10827 If N is nil and any articles have been marked with the process mark,
10828 save those articles instead."
10829   (interactive "P")
10830   (require 'gnus-art)
10831   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10832     (gnus-summary-save-article arg)))
10833
10834 (defun gnus-summary-save-article-body-file (&optional arg)
10835   "Append the current article body to a file.
10836 If N is a positive number, save the N next articles.
10837 If N is a negative number, save the N previous articles.
10838 If N is nil and any articles have been marked with the process mark,
10839 save those articles instead."
10840   (interactive "P")
10841   (require 'gnus-art)
10842   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10843     (gnus-summary-save-article arg)))
10844
10845 (defun gnus-summary-muttprint (&optional arg)
10846   "Print the current article using Muttprint.
10847 If N is a positive number, save the N next articles.
10848 If N is a negative number, save the N previous articles.
10849 If N is nil and any articles have been marked with the process mark,
10850 save those articles instead."
10851   (interactive "P")
10852   (require 'gnus-art)
10853   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10854     (gnus-summary-save-article arg t)))
10855
10856 (defun gnus-summary-pipe-message (program)
10857   "Pipe the current article through PROGRAM."
10858   (interactive "sProgram: ")
10859   (gnus-summary-select-article)
10860   (let ((mail-header-separator ""))
10861     (gnus-eval-in-buffer-window gnus-article-buffer
10862       (save-restriction
10863         (widen)
10864         (let ((start (window-start))
10865               buffer-read-only)
10866           (message-pipe-buffer-body program)
10867           (set-window-start (get-buffer-window (current-buffer)) start))))))
10868
10869 (defun gnus-get-split-value (methods)
10870   "Return a value based on the split METHODS."
10871   (let (split-name method result match)
10872     (when methods
10873       (save-excursion
10874         (set-buffer gnus-original-article-buffer)
10875         (save-restriction
10876           (nnheader-narrow-to-headers)
10877           (while (and methods (not split-name))
10878             (goto-char (point-min))
10879             (setq method (pop methods))
10880             (setq match (car method))
10881             (when (cond
10882                    ((stringp match)
10883                     ;; Regular expression.
10884                     (ignore-errors
10885                       (re-search-forward match nil t)))
10886                    ((functionp match)
10887                     ;; Function.
10888                     (save-restriction
10889                       (widen)
10890                       (setq result (funcall match gnus-newsgroup-name))))
10891                    ((consp match)
10892                     ;; Form.
10893                     (save-restriction
10894                       (widen)
10895                       (setq result (eval match)))))
10896               (setq split-name (cdr method))
10897               (cond ((stringp result)
10898                      (push (expand-file-name
10899                             result gnus-article-save-directory)
10900                            split-name))
10901                     ((consp result)
10902                      (setq split-name (append result split-name)))))))))
10903     (nreverse split-name)))
10904
10905 (defun gnus-valid-move-group-p (group)
10906   (and (boundp group)
10907        (symbol-name group)
10908        (symbol-value group)
10909        (gnus-get-function (gnus-find-method-for-group
10910                            (symbol-name group)) 'request-accept-article t)))
10911
10912 (defun gnus-read-move-group-name (prompt default articles prefix)
10913   "Read a group name."
10914   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10915          (minibuffer-confirm-incomplete nil) ; XEmacs
10916          (prom
10917           (format "%s %s to:"
10918                   prompt
10919                   (if (> (length articles) 1)
10920                       (format "these %d articles" (length articles))
10921                     "this article")))
10922          (to-newsgroup
10923           (cond
10924            ((null split-name)
10925             (gnus-completing-read-with-default
10926              default prom
10927              gnus-active-hashtb
10928              'gnus-valid-move-group-p
10929              nil prefix
10930              'gnus-group-history))
10931            ((= 1 (length split-name))
10932             (gnus-completing-read-with-default
10933              (car split-name) prom
10934              gnus-active-hashtb
10935              'gnus-valid-move-group-p
10936              nil nil
10937              'gnus-group-history))
10938            (t
10939             (gnus-completing-read-with-default
10940              nil prom
10941              (mapcar (lambda (el) (list el))
10942                      (nreverse split-name))
10943              nil nil nil
10944              'gnus-group-history))))
10945          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10946     (when to-newsgroup
10947       (if (or (string= to-newsgroup "")
10948               (string= to-newsgroup prefix))
10949           (setq to-newsgroup default))
10950       (unless to-newsgroup
10951         (error "No group name entered"))
10952       (or (gnus-active to-newsgroup)
10953           (gnus-activate-group to-newsgroup nil nil to-method)
10954           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10955                                      to-newsgroup))
10956               (or (and (gnus-request-create-group to-newsgroup to-method)
10957                        (gnus-activate-group
10958                         to-newsgroup nil nil to-method)
10959                        (gnus-subscribe-group to-newsgroup))
10960                   (error "Couldn't create group %s" to-newsgroup)))
10961           (error "No such group: %s" to-newsgroup)))
10962     to-newsgroup))
10963
10964 (defun gnus-summary-save-parts (type dir n &optional reverse)
10965   "Save parts matching TYPE to DIR.
10966 If REVERSE, save parts that do not match TYPE."
10967   (interactive
10968    (list (read-string "Save parts of type: "
10969                       (or (car gnus-summary-save-parts-type-history)
10970                           gnus-summary-save-parts-default-mime)
10971                       'gnus-summary-save-parts-type-history)
10972          (setq gnus-summary-save-parts-last-directory
10973                (read-file-name "Save to directory: "
10974                                gnus-summary-save-parts-last-directory
10975                                nil t))
10976          current-prefix-arg))
10977   (gnus-summary-iterate n
10978     (let ((gnus-display-mime-function nil)
10979           (gnus-inhibit-treatment t))
10980       (gnus-summary-select-article))
10981     (save-excursion
10982       (set-buffer gnus-article-buffer)
10983       (let ((handles (or gnus-article-mime-handles
10984                          (mm-dissect-buffer nil gnus-article-loose-mime)
10985                          (and gnus-article-emulate-mime
10986                               (mm-uu-dissect)))))
10987         (when handles
10988           (gnus-summary-save-parts-1 type dir handles reverse)
10989           (unless gnus-article-mime-handles ;; Don't destroy this case.
10990             (mm-destroy-parts handles)))))))
10991
10992 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10993   (if (stringp (car handle))
10994       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10995               (cdr handle))
10996     (when (if reverse
10997               (not (string-match type (mm-handle-media-type handle)))
10998             (string-match type (mm-handle-media-type handle)))
10999       (let ((file (expand-file-name
11000                    (gnus-map-function
11001                     mm-file-name-rewrite-functions
11002                     (file-name-nondirectory
11003                      (or
11004                       (mail-content-type-get
11005                        (mm-handle-disposition handle) 'filename)
11006                       (mail-content-type-get
11007                        (mm-handle-type handle) 'name)
11008                       (concat gnus-newsgroup-name
11009                               "." (number-to-string
11010                                    (cdr gnus-article-current))))))
11011                    dir)))
11012         (unless (file-exists-p file)
11013           (mm-save-part-to-file handle file))))))
11014
11015 ;; Summary extract commands
11016
11017 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11018   (let ((buffer-read-only nil)
11019         (article (gnus-summary-article-number))
11020         after-article b e)
11021     (unless (gnus-summary-goto-subject article)
11022       (error "No such article: %d" article))
11023     (gnus-summary-position-point)
11024     ;; If all commands are to be bunched up on one line, we collect
11025     ;; them here.
11026     (unless gnus-view-pseudos-separately
11027       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11028             files action)
11029         (while ps
11030           (setq action (cdr (assq 'action (car ps))))
11031           (setq files (list (cdr (assq 'name (car ps)))))
11032           (while (and ps (cdr ps)
11033                       (string= (or action "1")
11034                                (or (cdr (assq 'action (cadr ps))) "2")))
11035             (push (cdr (assq 'name (cadr ps))) files)
11036             (setcdr ps (cddr ps)))
11037           (when files
11038             (when (not (string-match "%s" action))
11039               (push " " files))
11040             (push " " files)
11041             (when (assq 'execute (car ps))
11042               (setcdr (assq 'execute (car ps))
11043                       (funcall (if (string-match "%s" action)
11044                                    'format 'concat)
11045                                action
11046                                (mapconcat
11047                                 (lambda (f)
11048                                   (if (equal f " ")
11049                                       f
11050                                     (mm-quote-arg f)))
11051                                 files " ")))))
11052           (setq ps (cdr ps)))))
11053     (if (and gnus-view-pseudos (not not-view))
11054         (while pslist
11055           (when (assq 'execute (car pslist))
11056             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11057                                   (eq gnus-view-pseudos 'not-confirm)))
11058           (setq pslist (cdr pslist)))
11059       (save-excursion
11060         (while pslist
11061           (setq after-article (or (cdr (assq 'article (car pslist)))
11062                                   (gnus-summary-article-number)))
11063           (gnus-summary-goto-subject after-article)
11064           (forward-line 1)
11065           (setq b (point))
11066           (insert "    " (file-name-nondirectory
11067                           (cdr (assq 'name (car pslist))))
11068                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11069           (setq e (point))
11070           (forward-line -1)             ; back to `b'
11071           (gnus-add-text-properties
11072            b (1- e) (list 'gnus-number gnus-reffed-article-number
11073                           gnus-mouse-face-prop gnus-mouse-face))
11074           (gnus-data-enter
11075            after-article gnus-reffed-article-number
11076            gnus-unread-mark b (car pslist) 0 (- e b))
11077           (setq gnus-newsgroup-unreads
11078                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11079                                          gnus-reffed-article-number))
11080           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11081           (setq pslist (cdr pslist)))))))
11082
11083 (defun gnus-pseudos< (p1 p2)
11084   (let ((c1 (cdr (assq 'action p1)))
11085         (c2 (cdr (assq 'action p2))))
11086     (and c1 c2 (string< c1 c2))))
11087
11088 (defun gnus-request-pseudo-article (props)
11089   (cond ((assq 'execute props)
11090          (gnus-execute-command (cdr (assq 'execute props)))))
11091   (let ((gnus-current-article (gnus-summary-article-number)))
11092     (gnus-run-hooks 'gnus-mark-article-hook)))
11093
11094 (defun gnus-execute-command (command &optional automatic)
11095   (save-excursion
11096     (gnus-article-setup-buffer)
11097     (set-buffer gnus-article-buffer)
11098     (setq buffer-read-only nil)
11099     (let ((command (if automatic command
11100                      (read-string "Command: " (cons command 0)))))
11101       (erase-buffer)
11102       (insert "$ " command "\n\n")
11103       (if gnus-view-pseudo-asynchronously
11104           (start-process "gnus-execute" (current-buffer) shell-file-name
11105                          shell-command-switch command)
11106         (call-process shell-file-name nil t nil
11107                       shell-command-switch command)))))
11108
11109 ;; Summary kill commands.
11110
11111 (defun gnus-summary-edit-global-kill (article)
11112   "Edit the \"global\" kill file."
11113   (interactive (list (gnus-summary-article-number)))
11114   (gnus-group-edit-global-kill article))
11115
11116 (defun gnus-summary-edit-local-kill ()
11117   "Edit a local kill file applied to the current newsgroup."
11118   (interactive)
11119   (setq gnus-current-headers (gnus-summary-article-header))
11120   (gnus-group-edit-local-kill
11121    (gnus-summary-article-number) gnus-newsgroup-name))
11122
11123 ;;; Header reading.
11124
11125 (defun gnus-read-header (id &optional header)
11126   "Read the headers of article ID and enter them into the Gnus system."
11127   (let ((group gnus-newsgroup-name)
11128         (gnus-override-method
11129          (or
11130           gnus-override-method
11131           (and (gnus-news-group-p gnus-newsgroup-name)
11132                (car (gnus-refer-article-methods)))))
11133         where)
11134     ;; First we check to see whether the header in question is already
11135     ;; fetched.
11136     (if (stringp id)
11137         ;; This is a Message-ID.
11138         (setq header (or header (gnus-id-to-header id)))
11139       ;; This is an article number.
11140       (setq header (or header (gnus-summary-article-header id))))
11141     (if (and header
11142              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11143         ;; We have found the header.
11144         header
11145       ;; If this is a sparse article, we have to nix out its
11146       ;; previous entry in the thread hashtb.
11147       (when (and header
11148                  (gnus-summary-article-sparse-p (mail-header-number header)))
11149         (let* ((parent (gnus-parent-id (mail-header-references header)))
11150                (thread (and parent (gnus-id-to-thread parent))))
11151           (when thread
11152             (delq (assq header thread) thread))))
11153       ;; We have to really fetch the header to this article.
11154       (save-excursion
11155         (set-buffer nntp-server-buffer)
11156         (when (setq where (gnus-request-head id group))
11157           (nnheader-fold-continuation-lines)
11158           (goto-char (point-max))
11159           (insert ".\n")
11160           (goto-char (point-min))
11161           (insert "211 ")
11162           (princ (cond
11163                   ((numberp id) id)
11164                   ((cdr where) (cdr where))
11165                   (header (mail-header-number header))
11166                   (t gnus-reffed-article-number))
11167                  (current-buffer))
11168           (insert " Article retrieved.\n"))
11169         (if (or (not where)
11170                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11171             ()                          ; Malformed head.
11172           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11173             (when (and (stringp id)
11174                        (not (string= (gnus-group-real-name group)
11175                                      (car where))))
11176               ;; If we fetched by Message-ID and the article came
11177               ;; from a different group, we fudge some bogus article
11178               ;; numbers for this article.
11179               (mail-header-set-number header gnus-reffed-article-number))
11180             (save-excursion
11181               (set-buffer gnus-summary-buffer)
11182               (decf gnus-reffed-article-number)
11183               (gnus-remove-header (mail-header-number header))
11184               (push header gnus-newsgroup-headers)
11185               (setq gnus-current-headers header)
11186               (push (mail-header-number header) gnus-newsgroup-limit)))
11187           header)))))
11188
11189 (defun gnus-remove-header (number)
11190   "Remove header NUMBER from `gnus-newsgroup-headers'."
11191   (if (and gnus-newsgroup-headers
11192            (= number (mail-header-number (car gnus-newsgroup-headers))))
11193       (pop gnus-newsgroup-headers)
11194     (let ((headers gnus-newsgroup-headers))
11195       (while (and (cdr headers)
11196                   (not (= number (mail-header-number (cadr headers)))))
11197         (pop headers))
11198       (when (cdr headers)
11199         (setcdr headers (cddr headers))))))
11200
11201 ;;;
11202 ;;; summary highlights
11203 ;;;
11204
11205 (defun gnus-highlight-selected-summary ()
11206   "Highlight selected article in summary buffer."
11207   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11208   (when gnus-summary-selected-face
11209     (save-excursion
11210       (let* ((beg (gnus-point-at-bol))
11211              (end (gnus-point-at-eol))
11212              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11213              (from (if (get-text-property beg gnus-mouse-face-prop)
11214                        beg
11215                      (or (next-single-property-change
11216                           beg gnus-mouse-face-prop nil end)
11217                          beg)))
11218              (to
11219               (if (= from end)
11220                   (- from 2)
11221                 (or (next-single-property-change
11222                      from gnus-mouse-face-prop nil end)
11223                     end))))
11224         ;; If no mouse-face prop on line we will have to = from = end,
11225         ;; so we highlight the entire line instead.
11226         (when (= (+ to 2) from)
11227           (setq from beg)
11228           (setq to end))
11229         (if gnus-newsgroup-selected-overlay
11230             ;; Move old overlay.
11231             (gnus-move-overlay
11232              gnus-newsgroup-selected-overlay from to (current-buffer))
11233           ;; Create new overlay.
11234           (gnus-overlay-put
11235            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11236            'face gnus-summary-selected-face))))))
11237
11238 (defvar gnus-summary-highlight-line-cached nil)
11239 (defvar gnus-summary-highlight-line-trigger nil)
11240
11241 (defun gnus-summary-highlight-line-0 ()
11242   (if (and (eq gnus-summary-highlight-line-trigger
11243                gnus-summary-highlight)
11244            gnus-summary-highlight-line-cached)
11245       gnus-summary-highlight-line-cached
11246     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11247           gnus-summary-highlight-line-cached
11248           (let* ((cond (list 'cond))
11249                  (c cond)
11250                  (list gnus-summary-highlight))
11251             (while list
11252               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11253                               nil))
11254               (setq c (cdr c)
11255                     list (cdr list)))
11256             (gnus-byte-compile (list 'lambda nil cond))))))
11257
11258 (defun gnus-summary-highlight-line ()
11259   "Highlight current line according to `gnus-summary-highlight'."
11260   (let* ((beg (gnus-point-at-bol))
11261          (article (or (gnus-summary-article-number) gnus-current-article))
11262          (score (or (cdr (assq article
11263                                gnus-newsgroup-scored))
11264                     gnus-summary-default-score 0))
11265          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11266          (inhibit-read-only t)
11267          (default gnus-summary-default-score)
11268          (default-high gnus-summary-default-high-score)
11269          (default-low gnus-summary-default-low-score)
11270          (uncached (and gnus-summary-use-undownloaded-faces
11271                         (memq article gnus-newsgroup-undownloaded))))
11272     (let ((face (funcall (gnus-summary-highlight-line-0))))
11273       (unless (eq face (get-text-property beg 'face))
11274         (gnus-put-text-property-excluding-characters-with-faces
11275          beg (gnus-point-at-eol) 'face
11276          (setq face (if (boundp face) (symbol-value face) face)))
11277         (when gnus-summary-highlight-line-function
11278           (funcall gnus-summary-highlight-line-function article face))))))
11279
11280 (defun gnus-update-read-articles (group unread &optional compute)
11281   "Update the list of read articles in GROUP.
11282 UNREAD is a sorted list."
11283   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11284          (entry (gnus-gethash group gnus-newsrc-hashtb))
11285          (info (nth 2 entry))
11286          (prev 1)
11287          read)
11288     (if (or (not info) (not active))
11289         ;; There is no info on this group if it was, in fact,
11290         ;; killed.  Gnus stores no information on killed groups, so
11291         ;; there's nothing to be done.
11292         ;; One could store the information somewhere temporarily,
11293         ;; perhaps...  Hmmm...
11294         ()
11295       ;; Remove any negative articles numbers.
11296       (while (and unread (< (car unread) 0))
11297         (setq unread (cdr unread)))
11298       ;; Remove any expired article numbers
11299       (while (and unread (< (car unread) (car active)))
11300         (setq unread (cdr unread)))
11301       ;; Compute the ranges of read articles by looking at the list of
11302       ;; unread articles.
11303       (while unread
11304         (when (/= (car unread) prev)
11305           (push (if (= prev (1- (car unread))) prev
11306                   (cons prev (1- (car unread))))
11307                 read))
11308         (setq prev (1+ (car unread)))
11309         (setq unread (cdr unread)))
11310       (when (<= prev (cdr active))
11311         (push (cons prev (cdr active)) read))
11312       (setq read (if (> (length read) 1) (nreverse read) read))
11313       (if compute
11314           read
11315         (save-excursion
11316           (let (setmarkundo)
11317             ;; Propagate the read marks to the backend.
11318             (when (gnus-check-backend-function 'request-set-mark group)
11319               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11320                     (add (gnus-remove-from-range read (gnus-info-read info))))
11321                 (when (or add del)
11322                   (unless (gnus-check-group group)
11323                     (error "Can't open server for %s" group))
11324                   (gnus-request-set-mark
11325                    group (delq nil (list (if add (list add 'add '(read)))
11326                                          (if del (list del 'del '(read))))))
11327                   (setq setmarkundo
11328                         `(gnus-request-set-mark
11329                           ,group
11330                           ',(delq nil (list
11331                                        (if del (list del 'add '(read)))
11332                                        (if add (list add 'del '(read))))))))))
11333             (set-buffer gnus-group-buffer)
11334             (gnus-undo-register
11335               `(progn
11336                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11337                  (gnus-info-set-read ',info ',(gnus-info-read info))
11338                  (gnus-get-unread-articles-in-group ',info
11339                                                     (gnus-active ,group))
11340                  (gnus-group-update-group ,group t)
11341                  ,setmarkundo))))
11342         ;; Enter this list into the group info.
11343         (gnus-info-set-read info read)
11344         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11345         (gnus-get-unread-articles-in-group info (gnus-active group))
11346         t))))
11347
11348 (defun gnus-offer-save-summaries ()
11349   "Offer to save all active summary buffers."
11350   (let (buffers)
11351     ;; Go through all buffers and find all summaries.
11352     (dolist (buffer (buffer-list))
11353       (when (and (setq buffer (buffer-name buffer))
11354                  (string-match "Summary" buffer)
11355                  (save-excursion
11356                    (set-buffer buffer)
11357                    ;; We check that this is, indeed, a summary buffer.
11358                    (and (eq major-mode 'gnus-summary-mode)
11359                         ;; Also make sure this isn't bogus.
11360                         gnus-newsgroup-prepared
11361                         ;; Also make sure that this isn't a
11362                         ;; dead summary buffer.
11363                         (not gnus-dead-summary-mode))))
11364         (push buffer buffers)))
11365     ;; Go through all these summary buffers and offer to save them.
11366     (when buffers
11367       (save-excursion
11368         (map-y-or-n-p
11369          "Update summary buffer %s? "
11370          (lambda (buf)
11371            (switch-to-buffer buf)
11372            (gnus-summary-exit))
11373          buffers)))))
11374
11375 (defun gnus-summary-setup-default-charset ()
11376   "Setup newsgroup default charset."
11377   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11378       (setq gnus-newsgroup-charset nil)
11379     (let* ((ignored-charsets
11380             (or gnus-newsgroup-ephemeral-ignored-charsets
11381                 (append
11382                  (and gnus-newsgroup-name
11383                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11384                  gnus-newsgroup-ignored-charsets))))
11385       (setq gnus-newsgroup-charset
11386             (or gnus-newsgroup-ephemeral-charset
11387                 (and gnus-newsgroup-name
11388                      (gnus-parameter-charset gnus-newsgroup-name))
11389                 gnus-default-charset))
11390       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11391            ignored-charsets))))
11392
11393 ;;;
11394 ;;; Mime Commands
11395 ;;;
11396
11397 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11398   "Display the current article buffer fully MIME-buttonized.
11399 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11400 treated as multipart/mixed."
11401   (interactive "P")
11402   (require 'gnus-art)
11403   (let ((gnus-unbuttonized-mime-types nil)
11404         (gnus-mime-display-multipart-as-mixed show-all-parts))
11405     (gnus-summary-show-article)))
11406
11407 (defun gnus-summary-repair-multipart (article)
11408   "Add a Content-Type header to a multipart article without one."
11409   (interactive (list (gnus-summary-article-number)))
11410   (gnus-with-article article
11411     (message-narrow-to-head)
11412     (message-remove-header "Mime-Version")
11413     (goto-char (point-max))
11414     (insert "Mime-Version: 1.0\n")
11415     (widen)
11416     (when (search-forward "\n--" nil t)
11417       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11418         (message-narrow-to-head)
11419         (message-remove-header "Content-Type")
11420         (goto-char (point-max))
11421         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11422                         separator))
11423         (widen))))
11424   (let (gnus-mark-article-hook)
11425     (gnus-summary-select-article t t nil article)))
11426
11427 (defun gnus-summary-toggle-display-buttonized ()
11428   "Toggle the buttonizing of the article buffer."
11429   (interactive)
11430   (require 'gnus-art)
11431   (if (setq gnus-inhibit-mime-unbuttonizing
11432             (not gnus-inhibit-mime-unbuttonizing))
11433       (let ((gnus-unbuttonized-mime-types nil))
11434         (gnus-summary-show-article))
11435     (gnus-summary-show-article)))
11436
11437 ;;;
11438 ;;; Generic summary marking commands
11439 ;;;
11440
11441 (defvar gnus-summary-marking-alist
11442   '((read gnus-del-mark "d")
11443     (unread gnus-unread-mark "u")
11444     (ticked gnus-ticked-mark "!")
11445     (dormant gnus-dormant-mark "?")
11446     (expirable gnus-expirable-mark "e"))
11447   "An alist of names/marks/keystrokes.")
11448
11449 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11450 (defvar gnus-summary-mark-map)
11451
11452 (defun gnus-summary-make-all-marking-commands ()
11453   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11454   (dolist (elem gnus-summary-marking-alist)
11455     (apply 'gnus-summary-make-marking-command elem)))
11456
11457 (defun gnus-summary-make-marking-command (name mark keystroke)
11458   (let ((map (make-sparse-keymap)))
11459     (define-key gnus-summary-generic-mark-map keystroke map)
11460     (dolist (lway `((next "next" next nil "n")
11461                     (next-unread "next unread" next t "N")
11462                     (prev "previous" prev nil "p")
11463                     (prev-unread "previous unread" prev t "P")
11464                     (nomove "" nil nil ,keystroke)))
11465       (let ((func (gnus-summary-make-marking-command-1
11466                    mark (car lway) lway name)))
11467         (setq func (eval func))
11468         (define-key map (nth 4 lway) func)))))
11469
11470 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11471   `(defun ,(intern
11472             (format "gnus-summary-put-mark-as-%s%s"
11473                     name (if (eq way 'nomove)
11474                              ""
11475                            (concat "-" (symbol-name way)))))
11476      (n)
11477      ,(format
11478        "Mark the current article as %s%s.
11479 If N, the prefix, then repeat N times.
11480 If N is negative, move in reverse order.
11481 The difference between N and the actual number of articles marked is
11482 returned."
11483        name (cadr lway))
11484      (interactive "p")
11485      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11486
11487 (defun gnus-summary-generic-mark (n mark move unread)
11488   "Mark N articles with MARK."
11489   (unless (eq major-mode 'gnus-summary-mode)
11490     (error "This command can only be used in the summary buffer"))
11491   (gnus-summary-show-thread)
11492   (let ((nummove
11493          (cond
11494           ((eq move 'next) 1)
11495           ((eq move 'prev) -1)
11496           (t 0))))
11497     (if (zerop nummove)
11498         (setq n 1)
11499       (when (< n 0)
11500         (setq n (abs n)
11501               nummove (* -1 nummove))))
11502     (while (and (> n 0)
11503                 (gnus-summary-mark-article nil mark)
11504                 (zerop (gnus-summary-next-subject nummove unread t)))
11505       (setq n (1- n)))
11506     (when (/= 0 n)
11507       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11508     (gnus-summary-recenter)
11509     (gnus-summary-position-point)
11510     (gnus-set-mode-line 'summary)
11511     n))
11512
11513 (defun gnus-summary-insert-articles (articles)
11514   (when (setq articles
11515               (gnus-sorted-difference articles
11516                                       (mapcar (lambda (h)
11517                                                 (mail-header-number h))
11518                                               gnus-newsgroup-headers)))
11519     (setq gnus-newsgroup-headers
11520           (gnus-merge 'list
11521                       gnus-newsgroup-headers
11522                       (gnus-fetch-headers articles)
11523                       'gnus-article-sort-by-number))
11524     ;; Suppress duplicates?
11525     (when gnus-suppress-duplicates
11526       (gnus-dup-suppress-articles))
11527
11528     ;; We might want to build some more threads first.
11529     (when (and gnus-fetch-old-headers
11530                (eq gnus-headers-retrieved-by 'nov))
11531       (if (eq gnus-fetch-old-headers 'invisible)
11532           (gnus-build-all-threads)
11533         (gnus-build-old-threads)))
11534     ;; Let the Gnus agent mark articles as read.
11535     (when gnus-agent
11536       (gnus-agent-get-undownloaded-list))
11537     ;; Remove list identifiers from subject
11538     (when gnus-list-identifiers
11539       (gnus-summary-remove-list-identifiers))
11540     ;; First and last article in this newsgroup.
11541     (when gnus-newsgroup-headers
11542       (setq gnus-newsgroup-begin
11543             (mail-header-number (car gnus-newsgroup-headers))
11544             gnus-newsgroup-end
11545             (mail-header-number
11546              (gnus-last-element gnus-newsgroup-headers))))
11547     (when gnus-use-scoring
11548       (gnus-possibly-score-headers))))
11549
11550 (defun gnus-summary-insert-old-articles (&optional all)
11551   "Insert all old articles in this group.
11552 If ALL is non-nil, already read articles become readable.
11553 If ALL is a number, fetch this number of articles."
11554   (interactive "P")
11555   (prog1
11556       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11557             older len)
11558         (setq older
11559               ;; Some nntp servers lie about their active range.  When
11560               ;; this happens, the active range can be in the millions.
11561               ;; Use a compressed range to avoid creating a huge list.
11562               (gnus-range-difference (list gnus-newsgroup-active) old))
11563         (setq len (gnus-range-length older))
11564         (cond
11565          ((null older) nil)
11566          ((numberp all)
11567           (if (< all len)
11568               (let ((older-range (nreverse older)))
11569                 (setq older nil)
11570
11571                 (while (> all 0)
11572                   (let* ((r (pop older-range))
11573                          (min (if (numberp r) r (car r)))
11574                          (max (if (numberp r) r (cdr r))))
11575                     (while (and (<= min max)
11576                                 (> all 0))
11577                       (push max older)
11578                       (setq all (1- all)
11579                             max (1- max))))))
11580             (setq older (gnus-uncompress-range older))))
11581          (all
11582           (setq older (gnus-uncompress-range older)))
11583          (t
11584           (when (and (numberp gnus-large-newsgroup)
11585                    (> len gnus-large-newsgroup))
11586               (let* ((cursor-in-echo-area nil)
11587                      (initial (gnus-parameter-large-newsgroup-initial
11588                                gnus-newsgroup-name))
11589                      (input
11590                       (read-string
11591                        (format
11592                         "How many articles from %s (%s %d): "
11593                         (gnus-limit-string
11594                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11595                         (if initial "max" "default")
11596                         len)
11597                        (if initial
11598                            (cons (number-to-string initial)
11599                                  0)))))
11600                 (unless (string-match "^[ \t]*$" input)
11601                   (setq all (string-to-number input))
11602                   (if (< all len)
11603                       (let ((older-range (nreverse older)))
11604                         (setq older nil)
11605
11606                         (while (> all 0)
11607                           (let* ((r (pop older-range))
11608                                  (min (if (numberp r) r (car r)))
11609                                  (max (if (numberp r) r (cdr r))))
11610                             (while (and (<= min max)
11611                                         (> all 0))
11612                               (push max older)
11613                               (setq all (1- all)
11614                                     max (1- max))))))))))
11615           (setq older (gnus-uncompress-range older))))
11616         (if (not older)
11617             (message "No old news.")
11618           (gnus-summary-insert-articles older)
11619           (gnus-summary-limit (gnus-sorted-nunion old older))))
11620     (gnus-summary-position-point)))
11621
11622 (defun gnus-summary-insert-new-articles ()
11623   "Insert all new articles in this group."
11624   (interactive)
11625   (prog1
11626       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11627             (old-active gnus-newsgroup-active)
11628             (nnmail-fetched-sources (list t))
11629             i new)
11630         (setq gnus-newsgroup-active
11631               (gnus-activate-group gnus-newsgroup-name 'scan))
11632         (setq i (cdr gnus-newsgroup-active))
11633         (while (> i (cdr old-active))
11634           (push i new)
11635           (decf i))
11636         (if (not new)
11637             (message "No gnus is bad news.")
11638           (gnus-summary-insert-articles new)
11639           (setq gnus-newsgroup-unreads
11640                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11641           (gnus-summary-limit (gnus-sorted-nunion old new))))
11642     (gnus-summary-position-point)))
11643
11644 (gnus-summary-make-all-marking-commands)
11645
11646 (gnus-ems-redefine)
11647
11648 (provide 'gnus-sum)
11649
11650 (run-hooks 'gnus-sum-load-hook)
11651
11652 ;; Local Variables:
11653 ;; coding: iso-8859-1
11654 ;; End:
11655
11656 ;;; gnus-sum.el ends here