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