* gnus-art.el (gnus-treat-ansi-sequences, article-treat-ansi-sequences): New
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload '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   "A" gnus-article-treat-ansi-sequences
1849   "h" gnus-article-wash-html
1850   "u" gnus-article-unsplit-urls
1851   "s" gnus-summary-force-verify-and-decrypt
1852   "f" gnus-article-display-x-face
1853   "l" gnus-summary-stop-page-breaking
1854   "r" gnus-summary-caesar-message
1855   "m" gnus-summary-morse-message
1856   "t" gnus-summary-toggle-header
1857   "g" gnus-treat-smiley
1858   "v" gnus-summary-verbose-headers
1859   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1860   "p" gnus-article-verify-x-pgp-sig
1861   "d" gnus-article-treat-dumbquotes)
1862
1863 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1864   ;; mnemonic: deuglif*Y*
1865   "u" gnus-article-outlook-unwrap-lines
1866   "a" gnus-article-outlook-repair-attribution
1867   "c" gnus-article-outlook-rearrange-citation
1868   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1869
1870 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1871   "a" gnus-article-hide
1872   "h" gnus-article-hide-headers
1873   "b" gnus-article-hide-boring-headers
1874   "s" gnus-article-hide-signature
1875   "c" gnus-article-hide-citation
1876   "C" gnus-article-hide-citation-in-followups
1877   "l" gnus-article-hide-list-identifiers
1878   "B" gnus-article-strip-banner
1879   "P" gnus-article-hide-pem
1880   "\C-c" gnus-article-hide-citation-maybe)
1881
1882 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1883   "a" gnus-article-highlight
1884   "h" gnus-article-highlight-headers
1885   "c" gnus-article-highlight-citation
1886   "s" gnus-article-highlight-signature)
1887
1888 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1889   "f" gnus-article-treat-fold-headers
1890   "u" gnus-article-treat-unfold-headers
1891   "n" gnus-article-treat-fold-newsgroups)
1892
1893 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1894   "x" gnus-article-display-x-face
1895   "d" gnus-article-display-face
1896   "s" gnus-treat-smiley
1897   "D" gnus-article-remove-images
1898   "f" gnus-treat-from-picon
1899   "m" gnus-treat-mail-picon
1900   "n" gnus-treat-newsgroups-picon)
1901
1902 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1903   "w" gnus-article-decode-mime-words
1904   "c" gnus-article-decode-charset
1905   "v" gnus-mime-view-all-parts
1906   "b" gnus-article-view-part)
1907
1908 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1909   "z" gnus-article-date-ut
1910   "u" gnus-article-date-ut
1911   "l" gnus-article-date-local
1912   "p" gnus-article-date-english
1913   "e" gnus-article-date-lapsed
1914   "o" gnus-article-date-original
1915   "i" gnus-article-date-iso8601
1916   "s" gnus-article-date-user)
1917
1918 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1919   "t" gnus-article-remove-trailing-blank-lines
1920   "l" gnus-article-strip-leading-blank-lines
1921   "m" gnus-article-strip-multiple-blank-lines
1922   "a" gnus-article-strip-blank-lines
1923   "A" gnus-article-strip-all-blank-lines
1924   "s" gnus-article-strip-leading-space
1925   "e" gnus-article-strip-trailing-space
1926   "w" gnus-article-remove-leading-whitespace)
1927
1928 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1929   "v" gnus-version
1930   "f" gnus-summary-fetch-faq
1931   "d" gnus-summary-describe-group
1932   "h" gnus-summary-describe-briefly
1933   "i" gnus-info-find-node
1934   "c" gnus-group-fetch-charter
1935   "C" gnus-group-fetch-control)
1936
1937 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1938   "e" gnus-summary-expire-articles
1939   "\M-\C-e" gnus-summary-expire-articles-now
1940   "\177" gnus-summary-delete-article
1941   [delete] gnus-summary-delete-article
1942   [backspace] gnus-summary-delete-article
1943   "m" gnus-summary-move-article
1944   "r" gnus-summary-respool-article
1945   "w" gnus-summary-edit-article
1946   "c" gnus-summary-copy-article
1947   "B" gnus-summary-crosspost-article
1948   "q" gnus-summary-respool-query
1949   "t" gnus-summary-respool-trace
1950   "i" gnus-summary-import-article
1951   "I" gnus-summary-create-article
1952   "p" gnus-summary-article-posted-p)
1953
1954 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1955   "o" gnus-summary-save-article
1956   "m" gnus-summary-save-article-mail
1957   "F" gnus-summary-write-article-file
1958   "r" gnus-summary-save-article-rmail
1959   "f" gnus-summary-save-article-file
1960   "b" gnus-summary-save-article-body-file
1961   "h" gnus-summary-save-article-folder
1962   "v" gnus-summary-save-article-vm
1963   "p" gnus-summary-pipe-output
1964   "P" gnus-summary-muttprint
1965   "s" gnus-soup-add-article)
1966
1967 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1968   "b" gnus-summary-display-buttonized
1969   "m" gnus-summary-repair-multipart
1970   "v" gnus-article-view-part
1971   "o" gnus-article-save-part
1972   "c" gnus-article-copy-part
1973   "C" gnus-article-view-part-as-charset
1974   "e" gnus-article-view-part-externally
1975   "E" gnus-article-encrypt-body
1976   "i" gnus-article-inline-part
1977   "|" gnus-article-pipe-part)
1978
1979 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1980   "p" gnus-summary-mark-as-processable
1981   "u" gnus-summary-unmark-as-processable
1982   "U" gnus-summary-unmark-all-processable
1983   "v" gnus-uu-mark-over
1984   "s" gnus-uu-mark-series
1985   "r" gnus-uu-mark-region
1986   "g" gnus-uu-unmark-region
1987   "R" gnus-uu-mark-by-regexp
1988   "G" gnus-uu-unmark-by-regexp
1989   "t" gnus-uu-mark-thread
1990   "T" gnus-uu-unmark-thread
1991   "a" gnus-uu-mark-all
1992   "b" gnus-uu-mark-buffer
1993   "S" gnus-uu-mark-sparse
1994   "k" gnus-summary-kill-process-mark
1995   "y" gnus-summary-yank-process-mark
1996   "w" gnus-summary-save-process-mark
1997   "i" gnus-uu-invert-processable)
1998
1999 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2000   ;;"x" gnus-uu-extract-any
2001   "m" gnus-summary-save-parts
2002   "u" gnus-uu-decode-uu
2003   "U" gnus-uu-decode-uu-and-save
2004   "s" gnus-uu-decode-unshar
2005   "S" gnus-uu-decode-unshar-and-save
2006   "o" gnus-uu-decode-save
2007   "O" gnus-uu-decode-save
2008   "b" gnus-uu-decode-binhex
2009   "B" gnus-uu-decode-binhex
2010   "p" gnus-uu-decode-postscript
2011   "P" gnus-uu-decode-postscript-and-save)
2012
2013 (gnus-define-keys
2014     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2015   "u" gnus-uu-decode-uu-view
2016   "U" gnus-uu-decode-uu-and-save-view
2017   "s" gnus-uu-decode-unshar-view
2018   "S" gnus-uu-decode-unshar-and-save-view
2019   "o" gnus-uu-decode-save-view
2020   "O" gnus-uu-decode-save-view
2021   "b" gnus-uu-decode-binhex-view
2022   "B" gnus-uu-decode-binhex-view
2023   "p" gnus-uu-decode-postscript-view
2024   "P" gnus-uu-decode-postscript-and-save-view)
2025
2026 (defvar gnus-article-post-menu nil)
2027
2028 (defconst gnus-summary-menu-maxlen 20)
2029
2030 (defun gnus-summary-menu-split (menu)
2031   ;; If we have lots of elements, divide them into groups of 20
2032   ;; and make a pane (or submenu) for each one.
2033   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2034       (let ((menu menu) sublists next
2035             (i 1))
2036         (while menu
2037           ;; Pull off the next gnus-summary-menu-maxlen elements
2038           ;; and make them the next element of sublist.
2039           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2040           (if next
2041               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2042                       nil))
2043           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2044                                              (aref (car (last menu)) 0)) menu)
2045                                sublists))
2046           (setq i (1+ i))
2047           (setq menu next))
2048         (nreverse sublists))
2049     ;; Few elements--put them all in one pane.
2050     menu))
2051
2052 (defun gnus-summary-make-menu-bar ()
2053   (gnus-turn-off-edit-menu 'summary)
2054
2055   (unless (boundp 'gnus-summary-misc-menu)
2056
2057     (easy-menu-define
2058       gnus-summary-kill-menu gnus-summary-mode-map ""
2059       (cons
2060        "Score"
2061        (nconc
2062         (list
2063          ["Customize" gnus-score-customize t])
2064         (gnus-make-score-map 'increase)
2065         (gnus-make-score-map 'lower)
2066         '(("Mark"
2067            ["Kill below" gnus-summary-kill-below t]
2068            ["Mark above" gnus-summary-mark-above t]
2069            ["Tick above" gnus-summary-tick-above t]
2070            ["Clear above" gnus-summary-clear-above t])
2071           ["Current score" gnus-summary-current-score t]
2072           ["Set score" gnus-summary-set-score t]
2073           ["Switch current score file..." gnus-score-change-score-file t]
2074           ["Set mark below..." gnus-score-set-mark-below t]
2075           ["Set expunge below..." gnus-score-set-expunge-below t]
2076           ["Edit current score file" gnus-score-edit-current-scores t]
2077           ["Edit score file" gnus-score-edit-file t]
2078           ["Trace score" gnus-score-find-trace t]
2079           ["Find words" gnus-score-find-favourite-words t]
2080           ["Rescore buffer" gnus-summary-rescore t]
2081           ["Increase score..." gnus-summary-increase-score t]
2082           ["Lower score..." gnus-summary-lower-score t]))))
2083
2084     ;; Define both the Article menu in the summary buffer and the
2085     ;; equivalent Commands menu in the article buffer here for
2086     ;; consistency.
2087     (let ((innards
2088            `(("Hide"
2089               ["All" gnus-article-hide t]
2090               ["Headers" gnus-article-hide-headers t]
2091               ["Signature" gnus-article-hide-signature t]
2092               ["Citation" gnus-article-hide-citation t]
2093               ["List identifiers" gnus-article-hide-list-identifiers t]
2094               ["Banner" gnus-article-strip-banner t]
2095               ["Boring headers" gnus-article-hide-boring-headers t])
2096              ("Highlight"
2097               ["All" gnus-article-highlight t]
2098               ["Headers" gnus-article-highlight-headers t]
2099               ["Signature" gnus-article-highlight-signature t]
2100               ["Citation" gnus-article-highlight-citation t])
2101              ("MIME"
2102               ["Words" gnus-article-decode-mime-words t]
2103               ["Charset" gnus-article-decode-charset t]
2104               ["QP" gnus-article-de-quoted-unreadable t]
2105               ["Base64" gnus-article-de-base64-unreadable t]
2106               ["View MIME buttons" gnus-summary-display-buttonized t]
2107               ["View all" gnus-mime-view-all-parts t]
2108               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2109               ["Encrypt body" gnus-article-encrypt-body
2110                :active (not (gnus-group-read-only-p))
2111                ,@(if (featurep 'xemacs) nil
2112                    '(:help "Encrypt the message body on disk"))]
2113               ["Extract all parts..." gnus-summary-save-parts t]
2114               ("Multipart"
2115                ["Repair multipart" gnus-summary-repair-multipart t]
2116                ["Pipe part..." gnus-article-pipe-part t]
2117                ["Inline part" gnus-article-inline-part t]
2118                ["Encrypt body" gnus-article-encrypt-body
2119                 :active (not (gnus-group-read-only-p))
2120                ,@(if (featurep 'xemacs) nil
2121                    '(:help "Encrypt the message body on disk"))]
2122                ["View part externally" gnus-article-view-part-externally t]
2123                ["View part with charset..." gnus-article-view-part-as-charset t]
2124                ["Copy part" gnus-article-copy-part t]
2125                ["Save part..." gnus-article-save-part t]
2126                ["View part" gnus-article-view-part t]))
2127              ("Date"
2128               ["Local" gnus-article-date-local t]
2129               ["ISO8601" gnus-article-date-iso8601 t]
2130               ["UT" gnus-article-date-ut t]
2131               ["Original" gnus-article-date-original t]
2132               ["Lapsed" gnus-article-date-lapsed t]
2133               ["User-defined" gnus-article-date-user t])
2134              ("Display"
2135               ["Remove images" gnus-article-remove-images t]
2136               ["Toggle smiley" gnus-treat-smiley t]
2137               ["Show X-Face" gnus-article-display-x-face t]
2138               ["Show picons in From" gnus-treat-from-picon t]
2139               ["Show picons in mail headers" gnus-treat-mail-picon t]
2140               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2141               ("View as different encoding"
2142                ,@(gnus-summary-menu-split
2143                   (mapcar
2144                    (lambda (cs)
2145                      ;; Since easymenu under Emacs doesn't allow
2146                      ;; lambda forms for menu commands, we should
2147                      ;; provide intern'ed function symbols.
2148                      (let ((command (intern (format "\
2149 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2150                        (fset command
2151                              `(lambda ()
2152                                 (interactive)
2153                                 (let ((gnus-summary-show-article-charset-alist
2154                                        '((1 . ,cs))))
2155                                   (gnus-summary-show-article 1))))
2156                        `[,(symbol-name cs) ,command t]))
2157                    (sort (if (fboundp 'coding-system-list)
2158                              (coding-system-list)
2159                            (mapcar 'car mm-mime-mule-charset-alist))
2160                          'string<)))))
2161              ("Washing"
2162               ("Remove Blanks"
2163                ["Leading" gnus-article-strip-leading-blank-lines t]
2164                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2165                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2166                ["All of the above" gnus-article-strip-blank-lines t]
2167                ["All" gnus-article-strip-all-blank-lines t]
2168                ["Leading space" gnus-article-strip-leading-space t]
2169                ["Trailing space" gnus-article-strip-trailing-space t]
2170                ["Leading space in headers"
2171                 gnus-article-remove-leading-whitespace t])
2172               ["Overstrike" gnus-article-treat-overstrike t]
2173               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2174               ["Emphasis" gnus-article-emphasize t]
2175               ["Word wrap" gnus-article-fill-cited-article t]
2176               ["Fill long lines" gnus-article-fill-long-lines t]
2177               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2178               ["Remove CR" gnus-article-remove-cr t]
2179               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2180               ["Base64" gnus-article-de-base64-unreadable t]
2181               ["Rot 13" gnus-summary-caesar-message
2182                ,@(if (featurep 'xemacs) '(t)
2183                    '(:help "\"Caesar rotate\" article by 13"))]
2184               ["Morse decode" gnus-summary-morse-message t]
2185               ["Unix pipe..." gnus-summary-pipe-message t]
2186               ["Add buttons" gnus-article-add-buttons t]
2187               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2188               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2189               ["Verbose header" gnus-summary-verbose-headers t]
2190               ["Toggle header" gnus-summary-toggle-header t]
2191               ["Unfold headers" gnus-article-treat-unfold-headers t]
2192               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2193               ["Html" gnus-article-wash-html t]
2194               ["Unsplit URLs" gnus-article-unsplit-urls t]
2195               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2196               ["Decode HZ" gnus-article-decode-HZ t]
2197               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2198               ("(Outlook) Deuglify"
2199                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2200                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2201                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2202                ["Full (Outlook) deuglify"
2203                 gnus-article-outlook-deuglify-article t])
2204               )
2205              ("Output"
2206               ["Save in default format..." gnus-summary-save-article
2207                ,@(if (featurep 'xemacs) '(t)
2208                    '(:help "Save article using default method"))]
2209               ["Save in file..." gnus-summary-save-article-file
2210                ,@(if (featurep 'xemacs) '(t)
2211                    '(:help "Save article in file"))]
2212               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2213               ["Save in MH folder..." gnus-summary-save-article-folder t]
2214               ["Save in VM folder..." gnus-summary-save-article-vm t]
2215               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2216               ["Save body in file..." gnus-summary-save-article-body-file t]
2217               ["Pipe through a filter..." gnus-summary-pipe-output t]
2218               ["Add to SOUP packet" gnus-soup-add-article t]
2219               ["Print with Muttprint..." gnus-summary-muttprint t]
2220               ["Print" gnus-summary-print-article t])
2221              ("Backend"
2222               ["Respool article..." gnus-summary-respool-article t]
2223               ["Move article..." gnus-summary-move-article
2224                (gnus-check-backend-function
2225                 'request-move-article gnus-newsgroup-name)]
2226               ["Copy article..." gnus-summary-copy-article t]
2227               ["Crosspost article..." gnus-summary-crosspost-article
2228                (gnus-check-backend-function
2229                 'request-replace-article gnus-newsgroup-name)]
2230               ["Import file..." gnus-summary-import-article
2231                (gnus-check-backend-function
2232                 'request-accept-article gnus-newsgroup-name)]
2233               ["Create article..." gnus-summary-create-article
2234                (gnus-check-backend-function
2235                 'request-accept-article gnus-newsgroup-name)]
2236               ["Check if posted" gnus-summary-article-posted-p t]
2237               ["Edit article" gnus-summary-edit-article
2238                (not (gnus-group-read-only-p))]
2239               ["Delete article" gnus-summary-delete-article
2240                (gnus-check-backend-function
2241                 'request-expire-articles gnus-newsgroup-name)]
2242               ["Query respool" gnus-summary-respool-query t]
2243               ["Trace respool" gnus-summary-respool-trace t]
2244               ["Delete expirable articles" gnus-summary-expire-articles-now
2245                (gnus-check-backend-function
2246                 'request-expire-articles gnus-newsgroup-name)])
2247              ("Extract"
2248               ["Uudecode" gnus-uu-decode-uu
2249                ,@(if (featurep 'xemacs) '(t)
2250                    '(:help "Decode uuencoded article(s)"))]
2251               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2252               ["Unshar" gnus-uu-decode-unshar t]
2253               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2254               ["Save" gnus-uu-decode-save t]
2255               ["Binhex" gnus-uu-decode-binhex t]
2256               ["Postscript" gnus-uu-decode-postscript t]
2257               ["All MIME parts" gnus-summary-save-parts t])
2258              ("Cache"
2259               ["Enter article" gnus-cache-enter-article t]
2260               ["Remove article" gnus-cache-remove-article t])
2261              ["Translate" gnus-article-babel t]
2262              ["Select article buffer" gnus-summary-select-article-buffer t]
2263              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2264              ["Isearch article..." gnus-summary-isearch-article t]
2265              ["Beginning of the article" gnus-summary-beginning-of-article t]
2266              ["End of the article" gnus-summary-end-of-article t]
2267              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2268              ["Fetch referenced articles" gnus-summary-refer-references t]
2269              ["Fetch current thread" gnus-summary-refer-thread t]
2270              ["Fetch article with id..." gnus-summary-refer-article t]
2271              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2272              ["Redisplay" gnus-summary-show-article t]
2273              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2274       (easy-menu-define
2275         gnus-summary-article-menu gnus-summary-mode-map ""
2276         (cons "Article" innards))
2277
2278       (if (not (keymapp gnus-summary-article-menu))
2279           (easy-menu-define
2280             gnus-article-commands-menu gnus-article-mode-map ""
2281             (cons "Commands" innards))
2282         ;; in Emacs, don't share menu.
2283         (setq gnus-article-commands-menu
2284               (copy-keymap gnus-summary-article-menu))
2285         (define-key gnus-article-mode-map [menu-bar commands]
2286           (cons "Commands" gnus-article-commands-menu))))
2287
2288     (easy-menu-define
2289       gnus-summary-thread-menu gnus-summary-mode-map ""
2290       '("Threads"
2291         ["Find all messages in thread" gnus-summary-refer-thread t]
2292         ["Toggle threading" gnus-summary-toggle-threads t]
2293         ["Hide threads" gnus-summary-hide-all-threads t]
2294         ["Show threads" gnus-summary-show-all-threads t]
2295         ["Hide thread" gnus-summary-hide-thread t]
2296         ["Show thread" gnus-summary-show-thread t]
2297         ["Go to next thread" gnus-summary-next-thread t]
2298         ["Go to previous thread" gnus-summary-prev-thread t]
2299         ["Go down thread" gnus-summary-down-thread t]
2300         ["Go up thread" gnus-summary-up-thread t]
2301         ["Top of thread" gnus-summary-top-thread t]
2302         ["Mark thread as read" gnus-summary-kill-thread t]
2303         ["Lower thread score" gnus-summary-lower-thread t]
2304         ["Raise thread score" gnus-summary-raise-thread t]
2305         ["Rethread current" gnus-summary-rethread-current t]))
2306
2307     (easy-menu-define
2308       gnus-summary-post-menu gnus-summary-mode-map ""
2309       `("Post"
2310         ["Send a message (mail or news)" gnus-summary-post-news
2311          ,@(if (featurep 'xemacs) '(t)
2312              '(:help "Post an article"))]
2313         ["Followup" gnus-summary-followup
2314          ,@(if (featurep 'xemacs) '(t)
2315              '(:help "Post followup to this article"))]
2316         ["Followup and yank" gnus-summary-followup-with-original
2317          ,@(if (featurep 'xemacs) '(t)
2318              '(:help "Post followup to this article, quoting its contents"))]
2319         ["Supersede article" gnus-summary-supersede-article t]
2320         ["Cancel article" gnus-summary-cancel-article
2321          ,@(if (featurep 'xemacs) '(t)
2322              '(:help "Cancel an article you posted"))]
2323         ["Reply" gnus-summary-reply t]
2324         ["Reply and yank" gnus-summary-reply-with-original t]
2325         ["Wide reply" gnus-summary-wide-reply t]
2326         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2327          ,@(if (featurep 'xemacs) '(t)
2328              '(:help "Mail a reply, quoting this article"))]
2329         ["Very wide reply" gnus-summary-very-wide-reply t]
2330         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2331          ,@(if (featurep 'xemacs) '(t)
2332              '(:help "Mail a very wide reply, quoting this article"))]
2333         ["Mail forward" gnus-summary-mail-forward t]
2334         ["Post forward" gnus-summary-post-forward t]
2335         ["Digest and mail" gnus-uu-digest-mail-forward t]
2336         ["Digest and post" gnus-uu-digest-post-forward t]
2337         ["Resend message" gnus-summary-resend-message t]
2338         ["Resend message edit" gnus-summary-resend-message-edit t]
2339         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2340         ["Send a mail" gnus-summary-mail-other-window t]
2341         ["Create a local message" gnus-summary-news-other-window t]
2342         ["Uuencode and post" gnus-uu-post-news
2343          ,@(if (featurep 'xemacs) '(t)
2344              '(:help "Post a uuencoded article"))]
2345         ["Followup via news" gnus-summary-followup-to-mail t]
2346         ["Followup via news and yank"
2347          gnus-summary-followup-to-mail-with-original t]
2348         ;;("Draft"
2349         ;;["Send" gnus-summary-send-draft t]
2350         ;;["Send bounced" gnus-resend-bounced-mail t])
2351         ))
2352
2353     (cond
2354      ((not (keymapp gnus-summary-post-menu))
2355       (setq gnus-article-post-menu gnus-summary-post-menu))
2356      ((not gnus-article-post-menu)
2357       ;; Don't share post menu.
2358       (setq gnus-article-post-menu
2359             (copy-keymap gnus-summary-post-menu))))
2360     (define-key gnus-article-mode-map [menu-bar post]
2361       (cons "Post" gnus-article-post-menu))
2362
2363     (easy-menu-define
2364       gnus-summary-misc-menu gnus-summary-mode-map ""
2365       `("Gnus"
2366         ("Mark Read"
2367          ["Mark as read" gnus-summary-mark-as-read-forward t]
2368          ["Mark same subject and select"
2369           gnus-summary-kill-same-subject-and-select t]
2370          ["Mark same subject" gnus-summary-kill-same-subject t]
2371          ["Catchup" gnus-summary-catchup
2372           ,@(if (featurep 'xemacs) '(t)
2373               '(:help "Mark unread articles in this group as read"))]
2374          ["Catchup all" gnus-summary-catchup-all t]
2375          ["Catchup to here" gnus-summary-catchup-to-here t]
2376          ["Catchup from here" gnus-summary-catchup-from-here t]
2377          ["Catchup region" gnus-summary-mark-region-as-read
2378           (gnus-mark-active-p)]
2379          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2380         ("Mark Various"
2381          ["Tick" gnus-summary-tick-article-forward t]
2382          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2383          ["Remove marks" gnus-summary-clear-mark-forward t]
2384          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2385          ["Set bookmark" gnus-summary-set-bookmark t]
2386          ["Remove bookmark" gnus-summary-remove-bookmark t])
2387         ("Limit to"
2388          ["Marks..." gnus-summary-limit-to-marks t]
2389          ["Subject..." gnus-summary-limit-to-subject t]
2390          ["Author..." gnus-summary-limit-to-author t]
2391          ["Age..." gnus-summary-limit-to-age t]
2392          ["Extra..." gnus-summary-limit-to-extra t]
2393          ["Score..." gnus-summary-limit-to-score t]
2394          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2395          ["Unread" gnus-summary-limit-to-unread t]
2396          ["Unseen" gnus-summary-limit-to-unseen t]
2397          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2398          ["Next articles" gnus-summary-limit-to-articles t]
2399          ["Pop limit" gnus-summary-pop-limit t]
2400          ["Show dormant" gnus-summary-limit-include-dormant t]
2401          ["Hide childless dormant"
2402           gnus-summary-limit-exclude-childless-dormant t]
2403          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2404          ["Hide marked" gnus-summary-limit-exclude-marks t]
2405          ["Show expunged" gnus-summary-limit-include-expunged t])
2406         ("Process Mark"
2407          ["Set mark" gnus-summary-mark-as-processable t]
2408          ["Remove mark" gnus-summary-unmark-as-processable t]
2409          ["Remove all marks" gnus-summary-unmark-all-processable t]
2410          ["Mark above" gnus-uu-mark-over t]
2411          ["Mark series" gnus-uu-mark-series t]
2412          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2413          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2414          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2415          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2416          ["Mark all" gnus-uu-mark-all t]
2417          ["Mark buffer" gnus-uu-mark-buffer t]
2418          ["Mark sparse" gnus-uu-mark-sparse t]
2419          ["Mark thread" gnus-uu-mark-thread t]
2420          ["Unmark thread" gnus-uu-unmark-thread t]
2421          ("Process Mark Sets"
2422           ["Kill" gnus-summary-kill-process-mark t]
2423           ["Yank" gnus-summary-yank-process-mark
2424            gnus-newsgroup-process-stack]
2425           ["Save" gnus-summary-save-process-mark t]
2426           ["Run command on marked..." gnus-summary-universal-argument t]))
2427         ("Scroll article"
2428          ["Page forward" gnus-summary-next-page
2429           ,@(if (featurep 'xemacs) '(t)
2430               '(:help "Show next page of article"))]
2431          ["Page backward" gnus-summary-prev-page
2432           ,@(if (featurep 'xemacs) '(t)
2433               '(:help "Show previous page of article"))]
2434          ["Line forward" gnus-summary-scroll-up t])
2435         ("Move"
2436          ["Next unread article" gnus-summary-next-unread-article t]
2437          ["Previous unread article" gnus-summary-prev-unread-article t]
2438          ["Next article" gnus-summary-next-article t]
2439          ["Previous article" gnus-summary-prev-article t]
2440          ["Next unread subject" gnus-summary-next-unread-subject t]
2441          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2442          ["Next article same subject" gnus-summary-next-same-subject t]
2443          ["Previous article same subject" gnus-summary-prev-same-subject t]
2444          ["First unread article" gnus-summary-first-unread-article t]
2445          ["Best unread article" gnus-summary-best-unread-article t]
2446          ["Go to subject number..." gnus-summary-goto-subject t]
2447          ["Go to article number..." gnus-summary-goto-article t]
2448          ["Go to the last article" gnus-summary-goto-last-article t]
2449          ["Pop article off history" gnus-summary-pop-article t])
2450         ("Sort"
2451          ["Sort by number" gnus-summary-sort-by-number t]
2452          ["Sort by author" gnus-summary-sort-by-author t]
2453          ["Sort by subject" gnus-summary-sort-by-subject t]
2454          ["Sort by date" gnus-summary-sort-by-date t]
2455          ["Sort by score" gnus-summary-sort-by-score t]
2456          ["Sort by lines" gnus-summary-sort-by-lines t]
2457          ["Sort by characters" gnus-summary-sort-by-chars t]
2458          ["Randomize" gnus-summary-sort-by-random t]
2459          ["Original sort" gnus-summary-sort-by-original t])
2460         ("Help"
2461          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2462          ["Describe group" gnus-summary-describe-group t]
2463          ["Fetch charter" gnus-group-fetch-charter
2464           ,@(if (featurep 'xemacs) nil
2465               '(:help "Display the charter of the current group"))]
2466          ["Fetch control message" gnus-group-fetch-control
2467           ,@(if (featurep 'xemacs) nil
2468               '(:help "Display the archived control message for the current group"))]
2469          ["Read manual" gnus-info-find-node t])
2470         ("Modes"
2471          ["Pick and read" gnus-pick-mode t]
2472          ["Binary" gnus-binary-mode t])
2473         ("Regeneration"
2474          ["Regenerate" gnus-summary-prepare t]
2475          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2476          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2477          ["Toggle threading" gnus-summary-toggle-threads t])
2478         ["See old articles" gnus-summary-insert-old-articles t]
2479         ["See new articles" gnus-summary-insert-new-articles t]
2480         ["Filter articles..." gnus-summary-execute-command t]
2481         ["Run command on articles..." gnus-summary-universal-argument t]
2482         ["Search articles forward..." gnus-summary-search-article-forward t]
2483         ["Search articles backward..." gnus-summary-search-article-backward t]
2484         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2485         ["Expand window" gnus-summary-expand-window t]
2486         ["Expire expirable articles" gnus-summary-expire-articles
2487          (gnus-check-backend-function
2488           'request-expire-articles gnus-newsgroup-name)]
2489         ["Edit local kill file" gnus-summary-edit-local-kill t]
2490         ["Edit main kill file" gnus-summary-edit-global-kill t]
2491         ["Edit group parameters" gnus-summary-edit-parameters t]
2492         ["Customize group parameters" gnus-summary-customize-parameters t]
2493         ["Send a bug report" gnus-bug t]
2494         ("Exit"
2495          ["Catchup and exit" gnus-summary-catchup-and-exit
2496           ,@(if (featurep 'xemacs) '(t)
2497               '(:help "Mark unread articles in this group as read, then exit"))]
2498          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2499          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2500          ["Exit group" gnus-summary-exit
2501           ,@(if (featurep 'xemacs) '(t)
2502               '(:help "Exit current group, return to group selection mode"))]
2503          ["Exit group without updating" gnus-summary-exit-no-update t]
2504          ["Exit and goto next group" gnus-summary-next-group t]
2505          ["Exit and goto prev group" gnus-summary-prev-group t]
2506          ["Reselect group" gnus-summary-reselect-current-group t]
2507          ["Rescan group" gnus-summary-rescan-group t]
2508          ["Update dribble" gnus-summary-save-newsrc t])))
2509
2510     (gnus-run-hooks 'gnus-summary-menu-hook)))
2511
2512 (defvar gnus-summary-tool-bar-map nil)
2513
2514 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2515 (defun gnus-summary-make-tool-bar ()
2516   (if (and (fboundp 'tool-bar-add-item-from-menu)
2517            (default-value 'tool-bar-mode)
2518            (not gnus-summary-tool-bar-map))
2519       (setq gnus-summary-tool-bar-map
2520             (let ((tool-bar-map (make-sparse-keymap))
2521                   (load-path (mm-image-load-path)))
2522               (tool-bar-add-item-from-menu
2523                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2524               (tool-bar-add-item-from-menu
2525                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2526               (tool-bar-add-item-from-menu
2527                'gnus-summary-post-news "post" gnus-summary-mode-map)
2528               (tool-bar-add-item-from-menu
2529                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2530               (tool-bar-add-item-from-menu
2531                'gnus-summary-followup "followup" gnus-summary-mode-map)
2532               (tool-bar-add-item-from-menu
2533                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2534               (tool-bar-add-item-from-menu
2535                'gnus-summary-reply "reply" gnus-summary-mode-map)
2536               (tool-bar-add-item-from-menu
2537                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2538               (tool-bar-add-item-from-menu
2539                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2540               (tool-bar-add-item-from-menu
2541                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2542               (tool-bar-add-item-from-menu
2543                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2544               (tool-bar-add-item-from-menu
2545                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2546               (tool-bar-add-item-from-menu
2547                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2548               (tool-bar-add-item-from-menu
2549                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2550               (tool-bar-add-item-from-menu
2551                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2552               tool-bar-map)))
2553   (if gnus-summary-tool-bar-map
2554       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2555
2556 (defun gnus-score-set-default (var value)
2557   "A version of set that updates the GNU Emacs menu-bar."
2558   (set var value)
2559   ;; It is the message that forces the active status to be updated.
2560   (message ""))
2561
2562 (defun gnus-make-score-map (type)
2563   "Make a summary score map of type TYPE."
2564   (if t
2565       nil
2566     (let ((headers '(("author" "from" string)
2567                      ("subject" "subject" string)
2568                      ("article body" "body" string)
2569                      ("article head" "head" string)
2570                      ("xref" "xref" string)
2571                      ("extra header" "extra" string)
2572                      ("lines" "lines" number)
2573                      ("followups to author" "followup" string)))
2574           (types '((number ("less than" <)
2575                            ("greater than" >)
2576                            ("equal" =))
2577                    (string ("substring" s)
2578                            ("exact string" e)
2579                            ("fuzzy string" f)
2580                            ("regexp" r))))
2581           (perms '(("temporary" (current-time-string))
2582                    ("permanent" nil)
2583                    ("immediate" now)))
2584           header)
2585       (list
2586        (apply
2587         'nconc
2588         (list
2589          (if (eq type 'lower)
2590              "Lower score"
2591            "Increase score"))
2592         (let (outh)
2593           (while headers
2594             (setq header (car headers))
2595             (setq outh
2596                   (cons
2597                    (apply
2598                     'nconc
2599                     (list (car header))
2600                     (let ((ts (cdr (assoc (nth 2 header) types)))
2601                           outt)
2602                       (while ts
2603                         (setq outt
2604                               (cons
2605                                (apply
2606                                 'nconc
2607                                 (list (caar ts))
2608                                 (let ((ps perms)
2609                                       outp)
2610                                   (while ps
2611                                     (setq outp
2612                                           (cons
2613                                            (vector
2614                                             (caar ps)
2615                                             (list
2616                                              'gnus-summary-score-entry
2617                                              (nth 1 header)
2618                                              (if (or (string= (nth 1 header)
2619                                                               "head")
2620                                                      (string= (nth 1 header)
2621                                                               "body"))
2622                                                  ""
2623                                                (list 'gnus-summary-header
2624                                                      (nth 1 header)))
2625                                              (list 'quote (nth 1 (car ts)))
2626                                              (list 'gnus-score-delta-default
2627                                                    nil)
2628                                              (nth 1 (car ps))
2629                                              t)
2630                                             t)
2631                                            outp))
2632                                     (setq ps (cdr ps)))
2633                                   (list (nreverse outp))))
2634                                outt))
2635                         (setq ts (cdr ts)))
2636                       (list (nreverse outt))))
2637                    outh))
2638             (setq headers (cdr headers)))
2639           (list (nreverse outh))))))))
2640
2641 \f
2642
2643 (defun gnus-summary-mode (&optional group)
2644   "Major mode for reading articles.
2645
2646 All normal editing commands are switched off.
2647 \\<gnus-summary-mode-map>
2648 Each line in this buffer represents one article.  To read an
2649 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2650 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2651 respectively.
2652
2653 You can also post articles and send mail from this buffer.  To
2654 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2655 of an article, type `\\[gnus-summary-reply]'.
2656
2657 There are approx. one gazillion commands you can execute in this
2658 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2659
2660 The following commands are available:
2661
2662 \\{gnus-summary-mode-map}"
2663   (interactive)
2664   (kill-all-local-variables)
2665   (when (gnus-visual-p 'summary-menu 'menu)
2666     (gnus-summary-make-menu-bar)
2667     (gnus-summary-make-tool-bar))
2668   (gnus-summary-make-local-variables)
2669   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2670     (gnus-summary-make-local-variables))
2671   (gnus-make-thread-indent-array)
2672   (gnus-simplify-mode-line)
2673   (setq major-mode 'gnus-summary-mode)
2674   (setq mode-name "Summary")
2675   (make-local-variable 'minor-mode-alist)
2676   (use-local-map gnus-summary-mode-map)
2677   (buffer-disable-undo)
2678   (setq buffer-read-only t)             ;Disable modification
2679   (setq truncate-lines t)
2680   (setq selective-display t)
2681   (setq selective-display-ellipses t)   ;Display `...'
2682   (gnus-summary-set-display-table)
2683   (gnus-set-default-directory)
2684   (setq gnus-newsgroup-name group)
2685   (make-local-variable 'gnus-summary-line-format)
2686   (make-local-variable 'gnus-summary-line-format-spec)
2687   (make-local-variable 'gnus-summary-dummy-line-format)
2688   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2689   (make-local-variable 'gnus-summary-mark-positions)
2690   (gnus-make-local-hook 'pre-command-hook)
2691   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2692   (gnus-run-hooks 'gnus-summary-mode-hook)
2693   (turn-on-gnus-mailing-list-mode)
2694   (mm-enable-multibyte)
2695   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2696   (gnus-update-summary-mark-positions))
2697
2698 (defun gnus-summary-make-local-variables ()
2699   "Make all the local summary buffer variables."
2700   (let (global)
2701     (dolist (local gnus-summary-local-variables)
2702       (if (consp local)
2703           (progn
2704             (if (eq (cdr local) 'global)
2705                 ;; Copy the global value of the variable.
2706                 (setq global (symbol-value (car local)))
2707               ;; Use the value from the list.
2708               (setq global (eval (cdr local))))
2709             (set (make-local-variable (car local)) global))
2710         ;; Simple nil-valued local variable.
2711         (set (make-local-variable local) nil)))))
2712
2713 (defun gnus-summary-clear-local-variables ()
2714   (let ((locals gnus-summary-local-variables))
2715     (while locals
2716       (if (consp (car locals))
2717           (and (vectorp (caar locals))
2718                (set (caar locals) nil))
2719         (and (vectorp (car locals))
2720              (set (car locals) nil)))
2721       (setq locals (cdr locals)))))
2722
2723 ;; Summary data functions.
2724
2725 (defmacro gnus-data-number (data)
2726   `(car ,data))
2727
2728 (defmacro gnus-data-set-number (data number)
2729   `(setcar ,data ,number))
2730
2731 (defmacro gnus-data-mark (data)
2732   `(nth 1 ,data))
2733
2734 (defmacro gnus-data-set-mark (data mark)
2735   `(setcar (nthcdr 1 ,data) ,mark))
2736
2737 (defmacro gnus-data-pos (data)
2738   `(nth 2 ,data))
2739
2740 (defmacro gnus-data-set-pos (data pos)
2741   `(setcar (nthcdr 2 ,data) ,pos))
2742
2743 (defmacro gnus-data-header (data)
2744   `(nth 3 ,data))
2745
2746 (defmacro gnus-data-set-header (data header)
2747   `(setf (nth 3 ,data) ,header))
2748
2749 (defmacro gnus-data-level (data)
2750   `(nth 4 ,data))
2751
2752 (defmacro gnus-data-unread-p (data)
2753   `(= (nth 1 ,data) gnus-unread-mark))
2754
2755 (defmacro gnus-data-read-p (data)
2756   `(/= (nth 1 ,data) gnus-unread-mark))
2757
2758 (defmacro gnus-data-pseudo-p (data)
2759   `(consp (nth 3 ,data)))
2760
2761 (defmacro gnus-data-find (number)
2762   `(assq ,number gnus-newsgroup-data))
2763
2764 (defmacro gnus-data-find-list (number &optional data)
2765   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2766      (memq (assq ,number bdata)
2767            bdata)))
2768
2769 (defmacro gnus-data-make (number mark pos header level)
2770   `(list ,number ,mark ,pos ,header ,level))
2771
2772 (defun gnus-data-enter (after-article number mark pos header level offset)
2773   (let ((data (gnus-data-find-list after-article)))
2774     (unless data
2775       (error "No such article: %d" after-article))
2776     (setcdr data (cons (gnus-data-make number mark pos header level)
2777                        (cdr data)))
2778     (setq gnus-newsgroup-data-reverse nil)
2779     (gnus-data-update-list (cddr data) offset)))
2780
2781 (defun gnus-data-enter-list (after-article list &optional offset)
2782   (when list
2783     (let ((data (and after-article (gnus-data-find-list after-article)))
2784           (ilist list))
2785       (if (not (or data
2786                    after-article))
2787           (let ((odata gnus-newsgroup-data))
2788             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2789             (when offset
2790               (gnus-data-update-list odata offset)))
2791       ;; Find the last element in the list to be spliced into the main
2792         ;; list.
2793         (while (cdr list)
2794           (setq list (cdr list)))
2795         (if (not data)
2796             (progn
2797               (setcdr list gnus-newsgroup-data)
2798               (setq gnus-newsgroup-data ilist)
2799               (when offset
2800                 (gnus-data-update-list (cdr list) offset)))
2801           (setcdr list (cdr data))
2802           (setcdr data ilist)
2803           (when offset
2804             (gnus-data-update-list (cdr list) offset))))
2805       (setq gnus-newsgroup-data-reverse nil))))
2806
2807 (defun gnus-data-remove (article &optional offset)
2808   (let ((data gnus-newsgroup-data))
2809     (if (= (gnus-data-number (car data)) article)
2810         (progn
2811           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2812                 gnus-newsgroup-data-reverse nil)
2813           (when offset
2814             (gnus-data-update-list gnus-newsgroup-data offset)))
2815       (while (cdr data)
2816         (when (= (gnus-data-number (cadr data)) article)
2817           (setcdr data (cddr data))
2818           (when offset
2819             (gnus-data-update-list (cdr data) offset))
2820           (setq data nil
2821                 gnus-newsgroup-data-reverse nil))
2822         (setq data (cdr data))))))
2823
2824 (defmacro gnus-data-list (backward)
2825   `(if ,backward
2826        (or gnus-newsgroup-data-reverse
2827            (setq gnus-newsgroup-data-reverse
2828                  (reverse gnus-newsgroup-data)))
2829      gnus-newsgroup-data))
2830
2831 (defun gnus-data-update-list (data offset)
2832   "Add OFFSET to the POS of all data entries in DATA."
2833   (setq gnus-newsgroup-data-reverse nil)
2834   (while data
2835     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2836     (setq data (cdr data))))
2837
2838 (defun gnus-summary-article-pseudo-p (article)
2839   "Say whether this article is a pseudo article or not."
2840   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2841
2842 (defmacro gnus-summary-article-sparse-p (article)
2843   "Say whether this article is a sparse article or not."
2844   `(memq ,article gnus-newsgroup-sparse))
2845
2846 (defmacro gnus-summary-article-ancient-p (article)
2847   "Say whether this article is a sparse article or not."
2848   `(memq ,article gnus-newsgroup-ancient))
2849
2850 (defun gnus-article-parent-p (number)
2851   "Say whether this article is a parent or not."
2852   (let ((data (gnus-data-find-list number)))
2853     (and (cdr data)              ; There has to be an article after...
2854          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2855             (gnus-data-level (nth 1 data))))))
2856
2857 (defun gnus-article-children (number)
2858   "Return a list of all children to NUMBER."
2859   (let* ((data (gnus-data-find-list number))
2860          (level (gnus-data-level (car data)))
2861          children)
2862     (setq data (cdr data))
2863     (while (and data
2864                 (= (gnus-data-level (car data)) (1+ level)))
2865       (push (gnus-data-number (car data)) children)
2866       (setq data (cdr data)))
2867     children))
2868
2869 (defmacro gnus-summary-skip-intangible ()
2870   "If the current article is intangible, then jump to a different article."
2871   '(let ((to (get-text-property (point) 'gnus-intangible)))
2872      (and to (gnus-summary-goto-subject to))))
2873
2874 (defmacro gnus-summary-article-intangible-p ()
2875   "Say whether this article is intangible or not."
2876   '(get-text-property (point) 'gnus-intangible))
2877
2878 (defun gnus-article-read-p (article)
2879   "Say whether ARTICLE is read or not."
2880   (not (or (memq article gnus-newsgroup-marked)
2881            (memq article gnus-newsgroup-spam-marked)
2882            (memq article gnus-newsgroup-unreads)
2883            (memq article gnus-newsgroup-unselected)
2884            (memq article gnus-newsgroup-dormant))))
2885
2886 ;; Some summary mode macros.
2887
2888 (defmacro gnus-summary-article-number ()
2889   "The article number of the article on the current line.
2890 If there isn't an article number here, then we return the current
2891 article number."
2892   '(progn
2893      (gnus-summary-skip-intangible)
2894      (or (get-text-property (point) 'gnus-number)
2895          (gnus-summary-last-subject))))
2896
2897 (defmacro gnus-summary-article-header (&optional number)
2898   "Return the header of article NUMBER."
2899   `(gnus-data-header (gnus-data-find
2900                       ,(or number '(gnus-summary-article-number)))))
2901
2902 (defmacro gnus-summary-thread-level (&optional number)
2903   "Return the level of thread that starts with article NUMBER."
2904   `(if (and (eq gnus-summary-make-false-root 'dummy)
2905             (get-text-property (point) 'gnus-intangible))
2906        0
2907      (gnus-data-level (gnus-data-find
2908                        ,(or number '(gnus-summary-article-number))))))
2909
2910 (defmacro gnus-summary-article-mark (&optional number)
2911   "Return the mark of article NUMBER."
2912   `(gnus-data-mark (gnus-data-find
2913                     ,(or number '(gnus-summary-article-number)))))
2914
2915 (defmacro gnus-summary-article-pos (&optional number)
2916   "Return the position of the line of article NUMBER."
2917   `(gnus-data-pos (gnus-data-find
2918                    ,(or number '(gnus-summary-article-number)))))
2919
2920 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2921 (defmacro gnus-summary-article-subject (&optional number)
2922   "Return current subject string or nil if nothing."
2923   `(let ((headers
2924           ,(if number
2925                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2926              '(gnus-data-header (assq (gnus-summary-article-number)
2927                                       gnus-newsgroup-data)))))
2928      (and headers
2929           (vectorp headers)
2930           (mail-header-subject headers))))
2931
2932 (defmacro gnus-summary-article-score (&optional number)
2933   "Return current article score."
2934   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2935                   gnus-newsgroup-scored))
2936        gnus-summary-default-score 0))
2937
2938 (defun gnus-summary-article-children (&optional number)
2939   "Return a list of article numbers that are children of article NUMBER."
2940   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2941          (level (gnus-data-level (car data)))
2942          l children)
2943     (while (and (setq data (cdr data))
2944                 (> (setq l (gnus-data-level (car data))) level))
2945       (and (= (1+ level) l)
2946            (push (gnus-data-number (car data))
2947                  children)))
2948     (nreverse children)))
2949
2950 (defun gnus-summary-article-parent (&optional number)
2951   "Return the article number of the parent of article NUMBER."
2952   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2953                                     (gnus-data-list t)))
2954          (level (gnus-data-level (car data))))
2955     (if (zerop level)
2956         ()                              ; This is a root.
2957       ;; We search until we find an article with a level less than
2958       ;; this one.  That function has to be the parent.
2959       (while (and (setq data (cdr data))
2960                   (not (< (gnus-data-level (car data)) level))))
2961       (and data (gnus-data-number (car data))))))
2962
2963 (defun gnus-unread-mark-p (mark)
2964   "Say whether MARK is the unread mark."
2965   (= mark gnus-unread-mark))
2966
2967 (defun gnus-read-mark-p (mark)
2968   "Say whether MARK is one of the marks that mark as read.
2969 This is all marks except unread, ticked, dormant, and expirable."
2970   (not (or (= mark gnus-unread-mark)
2971            (= mark gnus-ticked-mark)
2972            (= mark gnus-spam-mark)
2973            (= mark gnus-dormant-mark)
2974            (= mark gnus-expirable-mark))))
2975
2976 (defmacro gnus-article-mark (number)
2977   "Return the MARK of article NUMBER.
2978 This macro should only be used when computing the mark the \"first\"
2979 time; i.e., when generating the summary lines.  After that,
2980 `gnus-summary-article-mark' should be used to examine the
2981 marks of articles."
2982   `(cond
2983     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2984     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2985     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2986     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2987     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2988     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2989     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2990     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2991            gnus-ancient-mark))))
2992
2993 ;; Saving hidden threads.
2994
2995 (defmacro gnus-save-hidden-threads (&rest forms)
2996   "Save hidden threads, eval FORMS, and restore the hidden threads."
2997   (let ((config (make-symbol "config")))
2998     `(let ((,config (gnus-hidden-threads-configuration)))
2999        (unwind-protect
3000            (save-excursion
3001              ,@forms)
3002          (gnus-restore-hidden-threads-configuration ,config)))))
3003 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3004 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3005
3006 (defun gnus-data-compute-positions ()
3007   "Compute the positions of all articles."
3008   (setq gnus-newsgroup-data-reverse nil)
3009   (let ((data gnus-newsgroup-data))
3010     (save-excursion
3011       (gnus-save-hidden-threads
3012         (gnus-summary-show-all-threads)
3013         (goto-char (point-min))
3014         (while data
3015           (while (get-text-property (point) 'gnus-intangible)
3016             (forward-line 1))
3017           (gnus-data-set-pos (car data) (+ (point) 3))
3018           (setq data (cdr data))
3019           (forward-line 1))))))
3020
3021 (defun gnus-hidden-threads-configuration ()
3022   "Return the current hidden threads configuration."
3023   (save-excursion
3024     (let (config)
3025       (goto-char (point-min))
3026       (while (search-forward "\r" nil t)
3027         (push (1- (point)) config))
3028       config)))
3029
3030 (defun gnus-restore-hidden-threads-configuration (config)
3031   "Restore hidden threads configuration from CONFIG."
3032   (save-excursion
3033     (let (point buffer-read-only)
3034       (while (setq point (pop config))
3035         (when (and (< point (point-max))
3036                    (goto-char point)
3037                    (eq (char-after) ?\n))
3038           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3039
3040 ;; Various summary mode internalish functions.
3041
3042 (defun gnus-mouse-pick-article (e)
3043   (interactive "e")
3044   (mouse-set-point e)
3045   (gnus-summary-next-page nil t))
3046
3047 (defun gnus-summary-set-display-table ()
3048   "Change the display table.
3049 Odd characters have a tendency to mess
3050 up nicely formatted displays - we make all possible glyphs
3051 display only a single character."
3052
3053   ;; We start from the standard display table, if any.
3054   (let ((table (or (copy-sequence standard-display-table)
3055                    (make-display-table)))
3056         (i 32))
3057     ;; Nix out all the control chars...
3058     (while (>= (setq i (1- i)) 0)
3059       (aset table i [??]))
3060    ;; ... but not newline and cr, of course.  (cr is necessary for the
3061     ;; selective display).
3062     (aset table ?\n nil)
3063     (aset table ?\r nil)
3064     ;; We keep TAB as well.
3065     (aset table ?\t nil)
3066     ;; We nix out any glyphs over 126 that are not set already.
3067     (let ((i 256))
3068       (while (>= (setq i (1- i)) 127)
3069         ;; Only modify if the entry is nil.
3070         (unless (aref table i)
3071           (aset table i [??]))))
3072     (setq buffer-display-table table)))
3073
3074 (defun gnus-summary-set-article-display-arrow (pos)
3075   "Update the overlay arrow to point to line at position POS."
3076   (when (and gnus-summary-display-arrow
3077              (boundp 'overlay-arrow-position)
3078              (boundp 'overlay-arrow-string))
3079     (save-excursion
3080       (goto-char pos)
3081       (beginning-of-line)
3082       (unless overlay-arrow-position
3083         (setq overlay-arrow-position (make-marker)))
3084       (setq overlay-arrow-string "=>"
3085             overlay-arrow-position (set-marker overlay-arrow-position
3086                                                (point)
3087                                                (current-buffer))))))
3088
3089 (defun gnus-summary-setup-buffer (group)
3090   "Initialize summary buffer."
3091   (let ((buffer (gnus-summary-buffer-name group))
3092         (dead-name (concat "*Dead Summary "
3093                            (gnus-group-decoded-name group) "*")))
3094     ;; If a dead summary buffer exists, we kill it.
3095     (when (gnus-buffer-live-p dead-name)
3096       (gnus-kill-buffer dead-name))
3097     (if (get-buffer buffer)
3098         (progn
3099           (set-buffer buffer)
3100           (setq gnus-summary-buffer (current-buffer))
3101           (not gnus-newsgroup-prepared))
3102       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3103       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3104       (gnus-summary-mode group)
3105       (when gnus-carpal
3106         (gnus-carpal-setup-buffer 'summary))
3107       (unless gnus-single-article-buffer
3108         (make-local-variable 'gnus-article-buffer)
3109         (make-local-variable 'gnus-article-current)
3110         (make-local-variable 'gnus-original-article-buffer))
3111       (setq gnus-newsgroup-name group)
3112       ;; Set any local variables in the group parameters.
3113       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3114       t)))
3115
3116 (defun gnus-set-global-variables ()
3117   "Set the global equivalents of the buffer-local variables.
3118 They are set to the latest values they had.  These reflect the summary
3119 buffer that was in action when the last article was fetched."
3120   (when (eq major-mode 'gnus-summary-mode)
3121     (setq gnus-summary-buffer (current-buffer))
3122     (let ((name gnus-newsgroup-name)
3123           (marked gnus-newsgroup-marked)
3124           (spam gnus-newsgroup-spam-marked)
3125           (unread gnus-newsgroup-unreads)
3126           (headers gnus-current-headers)
3127           (data gnus-newsgroup-data)
3128           (summary gnus-summary-buffer)
3129           (article-buffer gnus-article-buffer)
3130           (original gnus-original-article-buffer)
3131           (gac gnus-article-current)
3132           (reffed gnus-reffed-article-number)
3133           (score-file gnus-current-score-file)
3134           (default-charset gnus-newsgroup-charset)
3135           vlist)
3136       (let ((locals gnus-newsgroup-variables))
3137         (while locals
3138           (if (consp (car locals))
3139               (push (eval (caar locals)) vlist)
3140             (push (eval (car locals)) vlist))
3141           (setq locals (cdr locals)))
3142         (setq vlist (nreverse vlist)))
3143       (save-excursion
3144         (set-buffer gnus-group-buffer)
3145         (setq gnus-newsgroup-name name
3146               gnus-newsgroup-marked marked
3147               gnus-newsgroup-spam-marked spam
3148               gnus-newsgroup-unreads unread
3149               gnus-current-headers headers
3150               gnus-newsgroup-data data
3151               gnus-article-current gac
3152               gnus-summary-buffer summary
3153               gnus-article-buffer article-buffer
3154               gnus-original-article-buffer original
3155               gnus-reffed-article-number reffed
3156               gnus-current-score-file score-file
3157               gnus-newsgroup-charset default-charset)
3158         (let ((locals gnus-newsgroup-variables))
3159           (while locals
3160             (if (consp (car locals))
3161                 (set (caar locals) (pop vlist))
3162               (set (car locals) (pop vlist)))
3163             (setq locals (cdr locals))))
3164         ;; The article buffer also has local variables.
3165         (when (gnus-buffer-live-p gnus-article-buffer)
3166           (set-buffer gnus-article-buffer)
3167           (setq gnus-summary-buffer summary))))))
3168
3169 (defun gnus-summary-article-unread-p (article)
3170   "Say whether ARTICLE is unread or not."
3171   (memq article gnus-newsgroup-unreads))
3172
3173 (defun gnus-summary-first-article-p (&optional article)
3174   "Return whether ARTICLE is the first article in the buffer."
3175   (if (not (setq article (or article (gnus-summary-article-number))))
3176       nil
3177     (eq article (caar gnus-newsgroup-data))))
3178
3179 (defun gnus-summary-last-article-p (&optional article)
3180   "Return whether ARTICLE is the last article in the buffer."
3181   (if (not (setq article (or article (gnus-summary-article-number))))
3182       ;; All non-existent numbers are the last article.  :-)
3183       t
3184     (not (cdr (gnus-data-find-list article)))))
3185
3186 (defun gnus-make-thread-indent-array ()
3187   (let ((n 200))
3188     (unless (and gnus-thread-indent-array
3189                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3190       (setq gnus-thread-indent-array (make-vector 201 "")
3191             gnus-thread-indent-array-level gnus-thread-indent-level)
3192       (while (>= n 0)
3193         (aset gnus-thread-indent-array n
3194               (make-string (* n gnus-thread-indent-level) ? ))
3195         (setq n (1- n))))))
3196
3197 (defun gnus-update-summary-mark-positions ()
3198   "Compute where the summary marks are to go."
3199   (save-excursion
3200     (when (gnus-buffer-exists-p gnus-summary-buffer)
3201       (set-buffer gnus-summary-buffer))
3202     (let ((gnus-replied-mark 129)
3203           (gnus-score-below-mark 130)
3204           (gnus-score-over-mark 130)
3205           (gnus-undownloaded-mark 131)
3206           (spec gnus-summary-line-format-spec)
3207           gnus-visual pos)
3208       (save-excursion
3209         (gnus-set-work-buffer)
3210         (let ((gnus-summary-line-format-spec spec)
3211               (gnus-newsgroup-downloadable '(0)))
3212           (gnus-summary-insert-line
3213            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3214            0 nil t 128 t nil "" nil 1)
3215           (goto-char (point-min))
3216           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3217                                              (- (point) (point-min) 1)))))
3218           (goto-char (point-min))
3219           (push (cons 'replied (and (search-forward "\201" nil t)
3220                                     (- (point) (point-min) 1)))
3221                 pos)
3222           (goto-char (point-min))
3223           (push (cons 'score (and (search-forward "\202" nil t)
3224                                   (- (point) (point-min) 1)))
3225                 pos)
3226           (goto-char (point-min))
3227           (push (cons 'download
3228                       (and (search-forward "\203" nil t)
3229                            (- (point) (point-min) 1)))
3230                 pos)))
3231       (setq gnus-summary-mark-positions pos))))
3232
3233 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3234   "Insert a dummy root in the summary buffer."
3235   (beginning-of-line)
3236   (gnus-add-text-properties
3237    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3238    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3239
3240 (defun gnus-summary-extract-address-component (from)
3241   (or (car (funcall gnus-extract-address-components from))
3242       from))
3243
3244 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3245   (let ((mail-parse-charset gnus-newsgroup-charset)
3246         ; Is it really necessary to do this next part for each summary line?
3247         ; Luckily, doesn't seem to slow things down much.
3248         (mail-parse-ignored-charsets
3249          (save-excursion (set-buffer gnus-summary-buffer)
3250                          gnus-newsgroup-ignored-charsets)))
3251     (or
3252      (and gnus-ignored-from-addresses
3253           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3254           (let ((extra-headers (mail-header-extra header))
3255                 to
3256                 newsgroups)
3257             (cond
3258              ((setq to (cdr (assq 'To extra-headers)))
3259               (concat "-> "
3260                       (inline
3261                         (gnus-summary-extract-address-component
3262                          (funcall gnus-decode-encoded-word-function to)))))
3263              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3264               (concat "=> " newsgroups)))))
3265      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3266
3267 (defun gnus-summary-insert-line (gnus-tmp-header
3268                                  gnus-tmp-level gnus-tmp-current
3269                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3270                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3271                                  &optional gnus-tmp-dummy gnus-tmp-score
3272                                  gnus-tmp-process)
3273   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3274          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3275          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3276          (gnus-tmp-score-char
3277           (if (or (null gnus-summary-default-score)
3278                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3279                       gnus-summary-zcore-fuzz))
3280               ?                         ;Whitespace
3281             (if (< gnus-tmp-score gnus-summary-default-score)
3282                 gnus-score-below-mark gnus-score-over-mark)))
3283          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3284          (gnus-tmp-replied
3285           (cond (gnus-tmp-process gnus-process-mark)
3286                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3287                  gnus-cached-mark)
3288                 (gnus-tmp-replied gnus-replied-mark)
3289                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3290                  gnus-forwarded-mark)
3291                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3292                  gnus-saved-mark)
3293                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3294                  gnus-recent-mark)
3295                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3296                  gnus-unseen-mark)
3297                 (t gnus-no-mark)))
3298          (gnus-tmp-downloaded
3299           (cond (undownloaded
3300                  gnus-undownloaded-mark)
3301                 (gnus-newsgroup-agentized
3302                  gnus-downloaded-mark)
3303                 (t
3304                  gnus-no-mark)))
3305          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3306          (gnus-tmp-name
3307           (cond
3308            ((string-match "<[^>]+> *$" gnus-tmp-from)
3309             (let ((beg (match-beginning 0)))
3310               (or (and (string-match "^\".+\"" gnus-tmp-from)
3311                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3312                   (substring gnus-tmp-from 0 beg))))
3313            ((string-match "(.+)" gnus-tmp-from)
3314             (substring gnus-tmp-from
3315                        (1+ (match-beginning 0)) (1- (match-end 0))))
3316            (t gnus-tmp-from)))
3317          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3318          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3319          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3320          (buffer-read-only nil))
3321     (when (string= gnus-tmp-name "")
3322       (setq gnus-tmp-name gnus-tmp-from))
3323     (unless (numberp gnus-tmp-lines)
3324       (setq gnus-tmp-lines -1))
3325     (if (= gnus-tmp-lines -1)
3326         (setq gnus-tmp-lines "?")
3327       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3328       (gnus-put-text-property
3329      (point)
3330      (progn (eval gnus-summary-line-format-spec) (point))
3331        'gnus-number gnus-tmp-number)
3332     (when (gnus-visual-p 'summary-highlight 'highlight)
3333       (forward-line -1)
3334       (gnus-run-hooks 'gnus-summary-update-hook)
3335       (forward-line 1))))
3336
3337 (defun gnus-summary-update-line (&optional dont-update)
3338   "Update summary line after change."
3339   (when (and gnus-summary-default-score
3340              (not gnus-summary-inhibit-highlight))
3341     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3342            (article (gnus-summary-article-number))
3343            (score (gnus-summary-article-score article)))
3344       (unless dont-update
3345         (if (and gnus-summary-mark-below
3346                  (< (gnus-summary-article-score)
3347                     gnus-summary-mark-below))
3348             ;; This article has a low score, so we mark it as read.
3349             (when (memq article gnus-newsgroup-unreads)
3350               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3351           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3352             ;; This article was previously marked as read on account
3353             ;; of a low score, but now it has risen, so we mark it as
3354             ;; unread.
3355             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3356         (gnus-summary-update-mark
3357          (if (or (null gnus-summary-default-score)
3358                  (<= (abs (- score gnus-summary-default-score))
3359                      gnus-summary-zcore-fuzz))
3360              ?                          ;Whitespace
3361            (if (< score gnus-summary-default-score)
3362                gnus-score-below-mark gnus-score-over-mark))
3363          'score))
3364       ;; Do visual highlighting.
3365       (when (gnus-visual-p 'summary-highlight 'highlight)
3366         (gnus-run-hooks 'gnus-summary-update-hook)))))
3367
3368 (defvar gnus-tmp-new-adopts nil)
3369
3370 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3371   "Return the number of articles in THREAD.
3372 This may be 0 in some cases -- if none of the articles in
3373 the thread are to be displayed."
3374   (let* ((number
3375          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3376           (cond
3377            ((not (listp thread))
3378             1)
3379            ((and (consp thread) (cdr thread))
3380             (apply
3381              '+ 1 (mapcar
3382                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3383            ((null thread)
3384             1)
3385            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3386             1)
3387            (t 0))))
3388     (when (and level (zerop level) gnus-tmp-new-adopts)
3389       (incf number
3390             (apply '+ (mapcar
3391                        'gnus-summary-number-of-articles-in-thread
3392                        gnus-tmp-new-adopts))))
3393     (if char
3394         (if (> number 1) gnus-not-empty-thread-mark
3395           gnus-empty-thread-mark)
3396       number)))
3397
3398 (defsubst gnus-summary-line-message-size (head)
3399   "Return pretty-printed version of message size.
3400 This function is intended to be used in
3401 `gnus-summary-line-format-alist'."
3402   (let ((c (or (mail-header-chars head) -1)))
3403     (cond ((< c 0) "n/a")               ; chars not available
3404           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3405           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3406           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3407           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3408
3409
3410 (defun gnus-summary-set-local-parameters (group)
3411   "Go through the local params of GROUP and set all variable specs in that list."
3412   (let ((params (gnus-group-find-parameter group))
3413         (vars '(quit-config))           ; Ignore quit-config.
3414         elem)
3415     (while params
3416       (setq elem (car params)
3417             params (cdr params))
3418       (and (consp elem)                 ; Has to be a cons.
3419            (consp (cdr elem))           ; The cdr has to be a list.
3420            (symbolp (car elem))         ; Has to be a symbol in there.
3421            (not (memq (car elem) vars))
3422            (ignore-errors               ; So we set it.
3423              (push (car elem) vars)
3424              (make-local-variable (car elem))
3425              (set (car elem) (eval (nth 1 elem))))))))
3426
3427 (defun gnus-summary-read-group (group &optional show-all no-article
3428                                       kill-buffer no-display backward
3429                                       select-articles)
3430   "Start reading news in newsgroup GROUP.
3431 If SHOW-ALL is non-nil, already read articles are also listed.
3432 If NO-ARTICLE is non-nil, no article is selected initially.
3433 If NO-DISPLAY, don't generate a summary buffer."
3434   (let (result)
3435     (while (and group
3436                 (null (setq result
3437                             (let ((gnus-auto-select-next nil))
3438                               (or (gnus-summary-read-group-1
3439                                    group show-all no-article
3440                                    kill-buffer no-display
3441                                    select-articles)
3442                                   (setq show-all nil
3443                                         select-articles nil)))))
3444                 (eq gnus-auto-select-next 'quietly))
3445       (set-buffer gnus-group-buffer)
3446       ;; The entry function called above goes to the next
3447       ;; group automatically, so we go two groups back
3448       ;; if we are searching for the previous group.
3449       (when backward
3450         (gnus-group-prev-unread-group 2))
3451       (if (not (equal group (gnus-group-group-name)))
3452           (setq group (gnus-group-group-name))
3453         (setq group nil)))
3454     result))
3455
3456 (defun gnus-summary-read-group-1 (group show-all no-article
3457                                         kill-buffer no-display
3458                                         &optional select-articles)
3459   ;; Killed foreign groups can't be entered.
3460   ;;  (when (and (not (gnus-group-native-p group))
3461   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3462   ;;    (error "Dead non-native groups can't be entered"))
3463   (gnus-message 5 "Retrieving newsgroup: %s..."
3464                 (gnus-group-decoded-name group))
3465   (let* ((new-group (gnus-summary-setup-buffer group))
3466          (quit-config (gnus-group-quit-config group))
3467          (did-select (and new-group (gnus-select-newsgroup
3468                                      group show-all select-articles))))
3469     (cond
3470      ;; This summary buffer exists already, so we just select it.
3471      ((not new-group)
3472       (gnus-set-global-variables)
3473       (when kill-buffer
3474         (gnus-kill-or-deaden-summary kill-buffer))
3475       (gnus-configure-windows 'summary 'force)
3476       (gnus-set-mode-line 'summary)
3477       (gnus-summary-position-point)
3478       (message "")
3479       t)
3480      ;; We couldn't select this group.
3481      ((null did-select)
3482       (when (and (eq major-mode 'gnus-summary-mode)
3483                  (not (equal (current-buffer) kill-buffer)))
3484         (kill-buffer (current-buffer))
3485         (if (not quit-config)
3486             (progn
3487               ;; Update the info -- marks might need to be removed,
3488               ;; for instance.
3489               (gnus-summary-update-info)
3490               (set-buffer gnus-group-buffer)
3491               (gnus-group-jump-to-group group)
3492               (gnus-group-next-unread-group 1))
3493           (gnus-handle-ephemeral-exit quit-config)))
3494       (let ((grpinfo (gnus-get-info group)))
3495         (if (null (gnus-info-read grpinfo))
3496             (gnus-message 3 "Group %s contains no messages"
3497                           (gnus-group-decoded-name group))
3498           (gnus-message 3 "Can't select group")))
3499       nil)
3500      ;; The user did a `C-g' while prompting for number of articles,
3501      ;; so we exit this group.
3502      ((eq did-select 'quit)
3503       (and (eq major-mode 'gnus-summary-mode)
3504            (not (equal (current-buffer) kill-buffer))
3505            (kill-buffer (current-buffer)))
3506       (when kill-buffer
3507         (gnus-kill-or-deaden-summary kill-buffer))
3508       (if (not quit-config)
3509           (progn
3510             (set-buffer gnus-group-buffer)
3511             (gnus-group-jump-to-group group)
3512             (gnus-group-next-unread-group 1)
3513             (gnus-configure-windows 'group 'force))
3514         (gnus-handle-ephemeral-exit quit-config))
3515       ;; Finally signal the quit.
3516       (signal 'quit nil))
3517      ;; The group was successfully selected.
3518      (t
3519       (gnus-set-global-variables)
3520       ;; Save the active value in effect when the group was entered.
3521       (setq gnus-newsgroup-active
3522             (gnus-copy-sequence
3523              (gnus-active gnus-newsgroup-name)))
3524       ;; You can change the summary buffer in some way with this hook.
3525       (gnus-run-hooks 'gnus-select-group-hook)
3526       (gnus-update-format-specifications
3527        nil 'summary 'summary-mode 'summary-dummy)
3528       (gnus-update-summary-mark-positions)
3529       ;; Do score processing.
3530       (when gnus-use-scoring
3531         (gnus-possibly-score-headers))
3532       ;; Check whether to fill in the gaps in the threads.
3533       (when gnus-build-sparse-threads
3534         (gnus-build-sparse-threads))
3535       ;; Find the initial limit.
3536       (if gnus-show-threads
3537           (if show-all
3538               (let ((gnus-newsgroup-dormant nil))
3539                 (gnus-summary-initial-limit show-all))
3540             (gnus-summary-initial-limit show-all))
3541         ;; When unthreaded, all articles are always shown.
3542         (setq gnus-newsgroup-limit
3543               (mapcar
3544                (lambda (header) (mail-header-number header))
3545                gnus-newsgroup-headers)))
3546       ;; Generate the summary buffer.
3547       (unless no-display
3548         (gnus-summary-prepare))
3549       (when gnus-use-trees
3550         (gnus-tree-open group)
3551         (setq gnus-summary-highlight-line-function
3552               'gnus-tree-highlight-article))
3553       ;; If the summary buffer is empty, but there are some low-scored
3554       ;; articles or some excluded dormants, we include these in the
3555       ;; buffer.
3556       (when (and (zerop (buffer-size))
3557                  (not no-display))
3558         (cond (gnus-newsgroup-dormant
3559                (gnus-summary-limit-include-dormant))
3560               ((and gnus-newsgroup-scored show-all)
3561                (gnus-summary-limit-include-expunged t))))
3562       ;; Function `gnus-apply-kill-file' must be called in this hook.
3563       (gnus-run-hooks 'gnus-apply-kill-hook)
3564       (if (and (zerop (buffer-size))
3565                (not no-display))
3566           (progn
3567             ;; This newsgroup is empty.
3568             (gnus-summary-catchup-and-exit nil t)
3569             (gnus-message 6 "No unread news")
3570             (when kill-buffer
3571               (gnus-kill-or-deaden-summary kill-buffer))
3572             ;; Return nil from this function.
3573             nil)
3574         ;; Hide conversation thread subtrees.  We cannot do this in
3575         ;; gnus-summary-prepare-hook since kill processing may not
3576         ;; work with hidden articles.
3577         (gnus-summary-maybe-hide-threads)
3578         (when kill-buffer
3579           (gnus-kill-or-deaden-summary kill-buffer))
3580         (gnus-summary-auto-select-subject)
3581         ;; Show first unread article if requested.
3582         (if (and (not no-article)
3583                  (not no-display)
3584                  gnus-newsgroup-unreads
3585                  gnus-auto-select-first)
3586             (progn
3587               (gnus-configure-windows 'summary)
3588               (let ((art (gnus-summary-article-number)))
3589                 (unless (and (not gnus-plugged)
3590                              (or (memq art gnus-newsgroup-undownloaded)
3591                                  (memq art gnus-newsgroup-downloadable)))
3592                   (gnus-summary-goto-article art))))
3593           ;; Don't select any articles.
3594           (gnus-summary-position-point)
3595           (gnus-configure-windows 'summary 'force)
3596           (gnus-set-mode-line 'summary))
3597         (when (and gnus-auto-center-group
3598                    (get-buffer-window gnus-group-buffer t))
3599           ;; Gotta use windows, because recenter does weird stuff if
3600           ;; the current buffer ain't the displayed window.
3601           (let ((owin (selected-window)))
3602             (select-window (get-buffer-window gnus-group-buffer t))
3603             (when (gnus-group-goto-group group)
3604               (recenter))
3605             (select-window owin)))
3606         ;; Mark this buffer as "prepared".
3607         (setq gnus-newsgroup-prepared t)
3608         (gnus-run-hooks 'gnus-summary-prepared-hook)
3609         (unless (gnus-ephemeral-group-p group)
3610           (gnus-group-update-group group))
3611         t)))))
3612
3613 (defun gnus-summary-auto-select-subject ()
3614   "Select the subject line on initial group entry."
3615   (goto-char (point-min))
3616   (cond
3617    ((eq gnus-auto-select-subject 'best)
3618     (gnus-summary-best-unread-subject))
3619    ((eq gnus-auto-select-subject 'unread)
3620     (gnus-summary-first-unread-subject))
3621    ((eq gnus-auto-select-subject 'unseen)
3622     (gnus-summary-first-unseen-subject))
3623    ((eq gnus-auto-select-subject 'unseen-or-unread)
3624     (gnus-summary-first-unseen-or-unread-subject))
3625    ((eq gnus-auto-select-subject 'first)
3626     ;; Do nothing.
3627     )
3628    ((functionp gnus-auto-select-subject)
3629     (funcall gnus-auto-select-subject))))
3630
3631 (defun gnus-summary-prepare ()
3632   "Generate the summary buffer."
3633   (interactive)
3634   (let ((buffer-read-only nil))
3635     (erase-buffer)
3636     (setq gnus-newsgroup-data nil
3637           gnus-newsgroup-data-reverse nil)
3638     (gnus-run-hooks 'gnus-summary-generate-hook)
3639     ;; Generate the buffer, either with threads or without.
3640     (when gnus-newsgroup-headers
3641       (gnus-summary-prepare-threads
3642        (if gnus-show-threads
3643            (gnus-sort-gathered-threads
3644             (funcall gnus-summary-thread-gathering-function
3645                      (gnus-sort-threads
3646                       (gnus-cut-threads (gnus-make-threads)))))
3647          ;; Unthreaded display.
3648          (gnus-sort-articles gnus-newsgroup-headers))))
3649     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3650     ;; Call hooks for modifying summary buffer.
3651     (goto-char (point-min))
3652     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3653
3654 (defsubst gnus-general-simplify-subject (subject)
3655   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3656   (setq subject
3657         (cond
3658          ;; Truncate the subject.
3659          (gnus-simplify-subject-functions
3660           (gnus-map-function gnus-simplify-subject-functions subject))
3661          ((numberp gnus-summary-gather-subject-limit)
3662           (setq subject (gnus-simplify-subject-re subject))
3663           (if (> (length subject) gnus-summary-gather-subject-limit)
3664               (substring subject 0 gnus-summary-gather-subject-limit)
3665             subject))
3666          ;; Fuzzily simplify it.
3667          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3668           (gnus-simplify-subject-fuzzy subject))
3669          ;; Just remove the leading "Re:".
3670          (t
3671           (gnus-simplify-subject-re subject))))
3672
3673   (if (and gnus-summary-gather-exclude-subject
3674            (string-match gnus-summary-gather-exclude-subject subject))
3675       nil                         ; This article shouldn't be gathered
3676     subject))
3677
3678 (defun gnus-summary-simplify-subject-query ()
3679   "Query where the respool algorithm would put this article."
3680   (interactive)
3681   (gnus-summary-select-article)
3682   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3683
3684 (defun gnus-gather-threads-by-subject (threads)
3685   "Gather threads by looking at Subject headers."
3686   (if (not gnus-summary-make-false-root)
3687       threads
3688     (let ((hashtb (gnus-make-hashtable 1024))
3689           (prev threads)
3690           (result threads)
3691           subject hthread whole-subject)
3692       (while threads
3693         (setq subject (gnus-general-simplify-subject
3694                        (setq whole-subject (mail-header-subject
3695                                             (caar threads)))))
3696         (when subject
3697           (if (setq hthread (gnus-gethash subject hashtb))
3698               (progn
3699                 ;; We enter a dummy root into the thread, if we
3700                 ;; haven't done that already.
3701                 (unless (stringp (caar hthread))
3702                   (setcar hthread (list whole-subject (car hthread))))
3703                 ;; We add this new gathered thread to this gathered
3704                 ;; thread.
3705                 (setcdr (car hthread)
3706                         (nconc (cdar hthread) (list (car threads))))
3707                 ;; Remove it from the list of threads.
3708                 (setcdr prev (cdr threads))
3709                 (setq threads prev))
3710             ;; Enter this thread into the hash table.
3711             (gnus-sethash subject
3712                           (if gnus-summary-make-false-root-always
3713                               (progn
3714                                 ;; If you want a dummy root above all
3715                                 ;; threads...
3716                                 (setcar threads (list whole-subject
3717                                                       (car threads)))
3718                                 threads)
3719                             threads)
3720                           hashtb)))
3721         (setq prev threads)
3722         (setq threads (cdr threads)))
3723       result)))
3724
3725 (defun gnus-gather-threads-by-references (threads)
3726   "Gather threads by looking at References headers."
3727   (let ((idhashtb (gnus-make-hashtable 1024))
3728         (thhashtb (gnus-make-hashtable 1024))
3729         (prev threads)
3730         (result threads)
3731         ids references id gthread gid entered ref)
3732     (while threads
3733       (when (setq references (mail-header-references (caar threads)))
3734         (setq id (mail-header-id (caar threads))
3735               ids (inline (gnus-split-references references))
3736               entered nil)
3737         (while (setq ref (pop ids))
3738           (setq ids (delete ref ids))
3739           (if (not (setq gid (gnus-gethash ref idhashtb)))
3740               (progn
3741                 (gnus-sethash ref id idhashtb)
3742                 (gnus-sethash id threads thhashtb))
3743             (setq gthread (gnus-gethash gid thhashtb))
3744             (unless entered
3745               ;; We enter a dummy root into the thread, if we
3746               ;; haven't done that already.
3747               (unless (stringp (caar gthread))
3748                 (setcar gthread (list (mail-header-subject (caar gthread))
3749                                       (car gthread))))
3750               ;; We add this new gathered thread to this gathered
3751               ;; thread.
3752               (setcdr (car gthread)
3753                       (nconc (cdar gthread) (list (car threads)))))
3754             ;; Add it into the thread hash table.
3755             (gnus-sethash id gthread thhashtb)
3756             (setq entered t)
3757             ;; Remove it from the list of threads.
3758             (setcdr prev (cdr threads))
3759             (setq threads prev))))
3760       (setq prev threads)
3761       (setq threads (cdr threads)))
3762     result))
3763
3764 (defun gnus-sort-gathered-threads (threads)
3765   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3766   (let ((result threads))
3767     (while threads
3768       (when (stringp (caar threads))
3769         (setcdr (car threads)
3770                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3771       (setq threads (cdr threads)))
3772     result))
3773
3774 (defun gnus-thread-loop-p (root thread)
3775   "Say whether ROOT is in THREAD."
3776   (let ((stack (list thread))
3777         (infloop 0)
3778         th)
3779     (while (setq thread (pop stack))
3780       (setq th (cdr thread))
3781       (while (and th
3782                   (not (eq (caar th) root)))
3783         (pop th))
3784       (if th
3785           ;; We have found a loop.
3786           (let (ref-dep)
3787             (setcdr thread (delq (car th) (cdr thread)))
3788             (if (boundp (setq ref-dep (intern "none"
3789                                               gnus-newsgroup-dependencies)))
3790                 (setcdr (symbol-value ref-dep)
3791                         (nconc (cdr (symbol-value ref-dep))
3792                                (list (car th))))
3793               (set ref-dep (list nil (car th))))
3794             (setq infloop 1
3795                   stack nil))
3796         ;; Push all the subthreads onto the stack.
3797         (push (cdr thread) stack)))
3798     infloop))
3799
3800 (defun gnus-make-threads ()
3801   "Go through the dependency hashtb and find the roots.  Return all threads."
3802   (let (threads)
3803     (while (catch 'infloop
3804              (mapatoms
3805               (lambda (refs)
3806                 ;; Deal with self-referencing References loops.
3807                 (when (and (car (symbol-value refs))
3808                            (not (zerop
3809                                  (apply
3810                                   '+
3811                                   (mapcar
3812                                    (lambda (thread)
3813                                      (gnus-thread-loop-p
3814                                       (car (symbol-value refs)) thread))
3815                                    (cdr (symbol-value refs)))))))
3816                   (setq threads nil)
3817                   (throw 'infloop t))
3818                 (unless (car (symbol-value refs))
3819                   ;; These threads do not refer back to any other
3820                   ;; articles, so they're roots.
3821                   (setq threads (append (cdr (symbol-value refs)) threads))))
3822               gnus-newsgroup-dependencies)))
3823     threads))
3824
3825 ;; Build the thread tree.
3826 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3827   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3828
3829 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3830 if it was already present.
3831
3832 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3833 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3834 Message-IDs will be renamed to a unique Message-ID before being
3835 entered.
3836
3837 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3838   (let* ((id (mail-header-id header))
3839          (id-dep (and id (intern id dependencies)))
3840          parent-id ref ref-dep ref-header replaced)
3841     ;; Enter this `header' in the `dependencies' table.
3842     (cond
3843      ((not id-dep)
3844       (setq header nil))
3845      ;; The first two cases do the normal part: enter a new `header'
3846      ;; in the `dependencies' table.
3847      ((not (boundp id-dep))
3848       (set id-dep (list header)))
3849      ((null (car (symbol-value id-dep)))
3850       (setcar (symbol-value id-dep) header))
3851
3852      ;; From here the `header' was already present in the
3853      ;; `dependencies' table.
3854      (force-new
3855       ;; Overrides an existing entry;
3856       ;; just set the header part of the entry.
3857       (setcar (symbol-value id-dep) header)
3858       (setq replaced t))
3859
3860      ;; Renames the existing `header' to a unique Message-ID.
3861      ((not gnus-summary-ignore-duplicates)
3862       ;; An article with this Message-ID has already been seen.
3863       ;; We rename the Message-ID.
3864       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3865            (list header))
3866       (mail-header-set-id header id))
3867
3868      ;; The last case ignores an existing entry, except it adds any
3869      ;; additional Xrefs (in case the two articles came from different
3870      ;; servers.
3871      ;; Also sets `header' to `nil' meaning that the `dependencies'
3872      ;; table was *not* modified.
3873      (t
3874       (mail-header-set-xref
3875        (car (symbol-value id-dep))
3876        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3877                    "")
3878                (or (mail-header-xref header) "")))
3879       (setq header nil)))
3880
3881     (when (and header (not replaced))
3882       ;; First check that we are not creating a References loop.
3883       (setq parent-id (gnus-parent-id (mail-header-references header)))
3884       (setq ref parent-id)
3885       (while (and ref
3886                   (setq ref-dep (intern-soft ref dependencies))
3887                   (boundp ref-dep)
3888                   (setq ref-header (car (symbol-value ref-dep))))
3889         (if (string= id ref)
3890             ;; Yuk!  This is a reference loop.  Make the article be a
3891             ;; root article.
3892             (progn
3893               (mail-header-set-references (car (symbol-value id-dep)) "none")
3894               (setq ref nil)
3895               (setq parent-id nil))
3896           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3897       (setq ref-dep (intern (or parent-id "none") dependencies))
3898       (if (boundp ref-dep)
3899           (setcdr (symbol-value ref-dep)
3900                   (nconc (cdr (symbol-value ref-dep))
3901                          (list (symbol-value id-dep))))
3902         (set ref-dep (list nil (symbol-value id-dep)))))
3903     header))
3904
3905 (defun gnus-extract-message-id-from-in-reply-to (string)
3906   (if (string-match "<[^>]+>" string)
3907       (substring string (match-beginning 0) (match-end 0))
3908     nil))
3909
3910 (defun gnus-build-sparse-threads ()
3911   (let ((headers gnus-newsgroup-headers)
3912         (mail-parse-charset gnus-newsgroup-charset)
3913         (gnus-summary-ignore-duplicates t)
3914         header references generation relations
3915         subject child end new-child date)
3916     ;; First we create an alist of generations/relations, where
3917     ;; generations is how much we trust the relation, and the relation
3918     ;; is parent/child.
3919     (gnus-message 7 "Making sparse threads...")
3920     (save-excursion
3921       (nnheader-set-temp-buffer " *gnus sparse threads*")
3922       (while (setq header (pop headers))
3923         (when (and (setq references (mail-header-references header))
3924                    (not (string= references "")))
3925           (insert references)
3926           (setq child (mail-header-id header)
3927                 subject (mail-header-subject header)
3928                 date (mail-header-date header)
3929                 generation 0)
3930           (while (search-backward ">" nil t)
3931             (setq end (1+ (point)))
3932             (when (search-backward "<" nil t)
3933               (setq new-child (buffer-substring (point) end))
3934               (push (list (incf generation)
3935                           child (setq child new-child)
3936                           subject date)
3937                     relations)))
3938           (when child
3939             (push (list (1+ generation) child nil subject) relations))
3940           (erase-buffer)))
3941       (kill-buffer (current-buffer)))
3942     ;; Sort over trustworthiness.
3943     (mapcar
3944      (lambda (relation)
3945        (when (gnus-dependencies-add-header
3946               (make-full-mail-header
3947                gnus-reffed-article-number
3948                (nth 3 relation) "" (or (nth 4 relation) "")
3949                (nth 1 relation)
3950                (or (nth 2 relation) "") 0 0 "")
3951               gnus-newsgroup-dependencies nil)
3952          (push gnus-reffed-article-number gnus-newsgroup-limit)
3953          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3954          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3955                gnus-newsgroup-reads)
3956          (decf gnus-reffed-article-number)))
3957      (sort relations 'car-less-than-car))
3958     (gnus-message 7 "Making sparse threads...done")))
3959
3960 (defun gnus-build-old-threads ()
3961   ;; Look at all the articles that refer back to old articles, and
3962   ;; fetch the headers for the articles that aren't there.  This will
3963   ;; build complete threads - if the roots haven't been expired by the
3964   ;; server, that is.
3965   (let ((mail-parse-charset gnus-newsgroup-charset)
3966         id heads)
3967     (mapatoms
3968      (lambda (refs)
3969        (when (not (car (symbol-value refs)))
3970          (setq heads (cdr (symbol-value refs)))
3971          (while heads
3972            (if (memq (mail-header-number (caar heads))
3973                      gnus-newsgroup-dormant)
3974                (setq heads (cdr heads))
3975              (setq id (symbol-name refs))
3976              (while (and (setq id (gnus-build-get-header id))
3977                          (not (car (gnus-id-to-thread id)))))
3978              (setq heads nil)))))
3979      gnus-newsgroup-dependencies)))
3980
3981 (defsubst gnus-remove-odd-characters (string)
3982   "Translate STRING into something that doesn't contain weird characters."
3983   (mm-subst-char-in-string
3984    ?\r ?\-
3985    (mm-subst-char-in-string
3986     ?\n ?\- string)))
3987
3988 ;; This function has to be called with point after the article number
3989 ;; on the beginning of the line.
3990 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3991   (let ((eol (point-at-eol))
3992         (buffer (current-buffer))
3993         header references in-reply-to)
3994
3995     ;; overview: [num subject from date id refs chars lines misc]
3996     (unwind-protect
3997         (let (x)
3998           (narrow-to-region (point) eol)
3999           (unless (eobp)
4000             (forward-char))
4001
4002           (setq header
4003                 (make-full-mail-header
4004                  number                 ; number
4005                  (condition-case ()     ; subject
4006                      (gnus-remove-odd-characters
4007                       (funcall gnus-decode-encoded-word-function
4008                                (setq x (nnheader-nov-field))))
4009                    (error x))
4010                  (condition-case ()     ; from
4011                      (gnus-remove-odd-characters
4012                       (funcall gnus-decode-encoded-word-function
4013                                (setq x (nnheader-nov-field))))
4014                    (error x))
4015                  (nnheader-nov-field)   ; date
4016                  (nnheader-nov-read-message-id) ; id
4017                  (setq references (nnheader-nov-field)) ; refs
4018                  (nnheader-nov-read-integer) ; chars
4019                  (nnheader-nov-read-integer) ; lines
4020                  (unless (eobp)
4021                    (if (looking-at "Xref: ")
4022                        (goto-char (match-end 0)))
4023                    (nnheader-nov-field)) ; Xref
4024                  (nnheader-nov-parse-extra)))) ; extra
4025
4026       (widen))
4027
4028     (when (and (string= references "")
4029                (setq in-reply-to (mail-header-extra header))
4030                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4031       (mail-header-set-references
4032        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4033
4034     (when gnus-alter-header-function
4035       (funcall gnus-alter-header-function header))
4036     (gnus-dependencies-add-header header dependencies force-new)))
4037
4038 (defun gnus-build-get-header (id)
4039   "Look through the buffer of NOV lines and find the header to ID.
4040 Enter this line into the dependencies hash table, and return
4041 the id of the parent article (if any)."
4042   (let ((deps gnus-newsgroup-dependencies)
4043         found header)
4044     (prog1
4045         (save-excursion
4046           (set-buffer nntp-server-buffer)
4047           (let ((case-fold-search nil))
4048             (goto-char (point-min))
4049             (while (and (not found)
4050                         (search-forward id nil t))
4051               (beginning-of-line)
4052               (setq found (looking-at
4053                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4054                                    (regexp-quote id))))
4055               (or found (beginning-of-line 2)))
4056             (when found
4057               (beginning-of-line)
4058               (and
4059                (setq header (gnus-nov-parse-line
4060                              (read (current-buffer)) deps))
4061                (gnus-parent-id (mail-header-references header))))))
4062       (when header
4063         (let ((number (mail-header-number header)))
4064           (push number gnus-newsgroup-limit)
4065           (push header gnus-newsgroup-headers)
4066           (if (memq number gnus-newsgroup-unselected)
4067               (progn
4068                 (setq gnus-newsgroup-unreads
4069                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4070                                                number))
4071                 (setq gnus-newsgroup-unselected
4072                       (delq number gnus-newsgroup-unselected)))
4073             (push number gnus-newsgroup-ancient)))))))
4074
4075 (defun gnus-build-all-threads ()
4076   "Read all the headers."
4077   (let ((gnus-summary-ignore-duplicates t)
4078         (mail-parse-charset gnus-newsgroup-charset)
4079         (dependencies gnus-newsgroup-dependencies)
4080         header article)
4081     (save-excursion
4082       (set-buffer nntp-server-buffer)
4083       (let ((case-fold-search nil))
4084         (goto-char (point-min))
4085         (while (not (eobp))
4086           (ignore-errors
4087             (setq article (read (current-buffer))
4088                   header (gnus-nov-parse-line article dependencies)))
4089           (when header
4090             (save-excursion
4091               (set-buffer gnus-summary-buffer)
4092               (push header gnus-newsgroup-headers)
4093               (if (memq (setq article (mail-header-number header))
4094                         gnus-newsgroup-unselected)
4095                   (progn
4096                     (setq gnus-newsgroup-unreads
4097                           (gnus-add-to-sorted-list
4098                            gnus-newsgroup-unreads article))
4099                     (setq gnus-newsgroup-unselected
4100                           (delq article gnus-newsgroup-unselected)))
4101                 (push article gnus-newsgroup-ancient)))
4102             (forward-line 1)))))))
4103
4104 (defun gnus-summary-update-article-line (article header)
4105   "Update the line for ARTICLE using HEADER."
4106   (let* ((id (mail-header-id header))
4107          (thread (gnus-id-to-thread id)))
4108     (unless thread
4109       (error "Article in no thread"))
4110     ;; Update the thread.
4111     (setcar thread header)
4112     (gnus-summary-goto-subject article)
4113     (let* ((datal (gnus-data-find-list article))
4114            (data (car datal))
4115            (buffer-read-only nil)
4116            (level (gnus-summary-thread-level)))
4117       (gnus-delete-line)
4118       (let ((inserted (- (point)
4119                          (progn
4120                            (gnus-summary-insert-line
4121                             header level nil
4122                             (memq article gnus-newsgroup-undownloaded)
4123                             (gnus-article-mark article)
4124                             (memq article gnus-newsgroup-replied)
4125                             (memq article gnus-newsgroup-expirable)
4126                             ;; Only insert the Subject string when it's different
4127                             ;; from the previous Subject string.
4128                             (if (and
4129                                  gnus-show-threads
4130                                  (gnus-subject-equal
4131                                   (condition-case ()
4132                                       (mail-header-subject
4133                                        (gnus-data-header
4134                                         (cadr
4135                                          (gnus-data-find-list
4136                                           article
4137                                           (gnus-data-list t)))))
4138                                     ;; Error on the side of excessive subjects.
4139                                     (error ""))
4140                                   (mail-header-subject header)))
4141                                 ""
4142                               (mail-header-subject header))
4143                             nil (cdr (assq article gnus-newsgroup-scored))
4144                             (memq article gnus-newsgroup-processable))
4145                            (point)))))
4146         (when (cdr datal)
4147           (gnus-data-update-list
4148            (cdr datal)
4149            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4150
4151 (defun gnus-summary-update-article (article &optional iheader)
4152   "Update ARTICLE in the summary buffer."
4153   (set-buffer gnus-summary-buffer)
4154   (let* ((header (gnus-summary-article-header article))
4155          (id (mail-header-id header))
4156          (data (gnus-data-find article))
4157          (thread (gnus-id-to-thread id))
4158          (references (mail-header-references header))
4159          (parent
4160           (gnus-id-to-thread
4161            (or (gnus-parent-id
4162                 (when (and references
4163                            (not (equal "" references)))
4164                   references))
4165                "none")))
4166          (buffer-read-only nil)
4167          (old (car thread)))
4168     (when thread
4169       (unless iheader
4170         (setcar thread nil)
4171         (when parent
4172           (delq thread parent)))
4173       (if (gnus-summary-insert-subject id header)
4174           ;; Set the (possibly) new article number in the data structure.
4175           (gnus-data-set-number data (gnus-id-to-article id))
4176         (setcar thread old)
4177         nil))))
4178
4179 (defun gnus-rebuild-thread (id &optional line)
4180   "Rebuild the thread containing ID.
4181 If LINE, insert the rebuilt thread starting on line LINE."
4182   (let ((buffer-read-only nil)
4183         old-pos current thread data)
4184     (if (not gnus-show-threads)
4185         (setq thread (list (car (gnus-id-to-thread id))))
4186       ;; Get the thread this article is part of.
4187       (setq thread (gnus-remove-thread id)))
4188     (setq old-pos (point-at-bol))
4189     (setq current (save-excursion
4190                     (and (re-search-backward "[\r\n]" nil t)
4191                          (gnus-summary-article-number))))
4192     ;; If this is a gathered thread, we have to go some re-gathering.
4193     (when (stringp (car thread))
4194       (let ((subject (car thread))
4195             roots thr)
4196         (setq thread (cdr thread))
4197         (while thread
4198           (unless (memq (setq thr (gnus-id-to-thread
4199                                    (gnus-root-id
4200                                     (mail-header-id (caar thread)))))
4201                         roots)
4202             (push thr roots))
4203           (setq thread (cdr thread)))
4204         ;; We now have all (unique) roots.
4205         (if (= (length roots) 1)
4206             ;; All the loose roots are now one solid root.
4207             (setq thread (car roots))
4208           (setq thread (cons subject (gnus-sort-threads roots))))))
4209     (let (threads)
4210       ;; We then insert this thread into the summary buffer.
4211       (when line
4212         (goto-char (point-min))
4213         (forward-line (1- line)))
4214       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4215         (if gnus-show-threads
4216             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4217           (gnus-summary-prepare-unthreaded thread))
4218         (setq data (nreverse gnus-newsgroup-data))
4219         (setq threads gnus-newsgroup-threads))
4220       ;; We splice the new data into the data structure.
4221       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4222       ;;!!! then we want to insert at the beginning of the buffer.
4223       ;;!!! That happens to be true with Gnus now, but that may
4224       ;;!!! change in the future.  Perhaps.
4225       (gnus-data-enter-list
4226        (if line nil current) data (- (point) old-pos))
4227       (setq gnus-newsgroup-threads
4228             (nconc threads gnus-newsgroup-threads))
4229       (gnus-data-compute-positions))))
4230
4231 (defun gnus-number-to-header (number)
4232   "Return the header for article NUMBER."
4233   (let ((headers gnus-newsgroup-headers))
4234     (while (and headers
4235                 (not (= number (mail-header-number (car headers)))))
4236       (pop headers))
4237     (when headers
4238       (car headers))))
4239
4240 (defun gnus-parent-headers (in-headers &optional generation)
4241   "Return the headers of the GENERATIONeth parent of HEADERS."
4242   (unless generation
4243     (setq generation 1))
4244   (let ((parent t)
4245         (headers in-headers)
4246         references)
4247     (while (and parent
4248                 (not (zerop generation))
4249                 (setq references (mail-header-references headers)))
4250       (setq headers (if (and references
4251                              (setq parent (gnus-parent-id references)))
4252                         (car (gnus-id-to-thread parent))
4253                       nil))
4254       (decf generation))
4255     (and (not (eq headers in-headers))
4256          headers)))
4257
4258 (defun gnus-id-to-thread (id)
4259   "Return the (sub-)thread where ID appears."
4260   (gnus-gethash id gnus-newsgroup-dependencies))
4261
4262 (defun gnus-id-to-article (id)
4263   "Return the article number of ID."
4264   (let ((thread (gnus-id-to-thread id)))
4265     (when (and thread
4266                (car thread))
4267       (mail-header-number (car thread)))))
4268
4269 (defun gnus-id-to-header (id)
4270   "Return the article headers of ID."
4271   (car (gnus-id-to-thread id)))
4272
4273 (defun gnus-article-displayed-root-p (article)
4274   "Say whether ARTICLE is a root(ish) article."
4275   (let ((level (gnus-summary-thread-level article))
4276         (refs (mail-header-references  (gnus-summary-article-header article)))
4277         particle)
4278     (cond
4279      ((null level) nil)
4280      ((zerop level) t)
4281      ((null refs) t)
4282      ((null (gnus-parent-id refs)) t)
4283      ((and (= 1 level)
4284            (null (setq particle (gnus-id-to-article
4285                                  (gnus-parent-id refs))))
4286            (null (gnus-summary-thread-level particle)))))))
4287
4288 (defun gnus-root-id (id)
4289   "Return the id of the root of the thread where ID appears."
4290   (let (last-id prev)
4291     (while (and id (setq prev (car (gnus-id-to-thread id))))
4292       (setq last-id id
4293             id (gnus-parent-id (mail-header-references prev))))
4294     last-id))
4295
4296 (defun gnus-articles-in-thread (thread)
4297   "Return the list of articles in THREAD."
4298   (cons (mail-header-number (car thread))
4299         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4300
4301 (defun gnus-remove-thread (id &optional dont-remove)
4302   "Remove the thread that has ID in it."
4303   (let (headers thread last-id)
4304     ;; First go up in this thread until we find the root.
4305     (setq last-id (gnus-root-id id)
4306           headers (message-flatten-list (gnus-id-to-thread last-id)))
4307     ;; We have now found the real root of this thread.  It might have
4308     ;; been gathered into some loose thread, so we have to search
4309     ;; through the threads to find the thread we wanted.
4310     (let ((threads gnus-newsgroup-threads)
4311           sub)
4312       (while threads
4313         (setq sub (car threads))
4314         (if (stringp (car sub))
4315             ;; This is a gathered thread, so we look at the roots
4316             ;; below it to find whether this article is in this
4317             ;; gathered root.
4318             (progn
4319               (setq sub (cdr sub))
4320               (while sub
4321                 (when (member (caar sub) headers)
4322                   (setq thread (car threads)
4323                         threads nil
4324                         sub nil))
4325                 (setq sub (cdr sub))))
4326           ;; It's an ordinary thread, so we check it.
4327           (when (eq (car sub) (car headers))
4328             (setq thread sub
4329                   threads nil)))
4330         (setq threads (cdr threads)))
4331       ;; If this article is in no thread, then it's a root.
4332       (if thread
4333           (unless dont-remove
4334             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4335         (setq thread (gnus-id-to-thread last-id)))
4336       (when thread
4337         (prog1
4338             thread                      ; We return this thread.
4339           (unless dont-remove
4340             (if (stringp (car thread))
4341                 (progn
4342                   ;; If we use dummy roots, then we have to remove the
4343                   ;; dummy root as well.
4344                   (when (eq gnus-summary-make-false-root 'dummy)
4345                     ;; We go to the dummy root by going to
4346                     ;; the first sub-"thread", and then one line up.
4347                     (gnus-summary-goto-article
4348                      (mail-header-number (caadr thread)))
4349                     (forward-line -1)
4350                     (gnus-delete-line)
4351                     (gnus-data-compute-positions))
4352                   (setq thread (cdr thread))
4353                   (while thread
4354                     (gnus-remove-thread-1 (car thread))
4355                     (setq thread (cdr thread))))
4356               (gnus-remove-thread-1 thread))))))))
4357
4358 (defun gnus-remove-thread-1 (thread)
4359   "Remove the thread THREAD recursively."
4360   (let ((number (mail-header-number (pop thread)))
4361         d)
4362     (setq thread (reverse thread))
4363     (while thread
4364       (gnus-remove-thread-1 (pop thread)))
4365     (when (setq d (gnus-data-find number))
4366       (goto-char (gnus-data-pos d))
4367       (gnus-summary-show-thread)
4368       (gnus-data-remove
4369        number
4370        (- (point-at-bol)
4371           (prog1
4372               (1+ (point-at-eol))
4373             (gnus-delete-line)))))))
4374
4375 (defun gnus-sort-threads-1 (threads func)
4376   (sort (mapcar (lambda (thread)
4377                   (cons (car thread)
4378                         (and (cdr thread)
4379                              (gnus-sort-threads-1 (cdr thread) func))))
4380                 threads) func))
4381
4382 (defun gnus-sort-threads (threads)
4383   "Sort THREADS."
4384   (if (not gnus-thread-sort-functions)
4385       threads
4386     (gnus-message 8 "Sorting threads...")
4387     (let ((max-lisp-eval-depth 5000))
4388       (prog1 (gnus-sort-threads-1
4389          threads
4390          (gnus-make-sort-function gnus-thread-sort-functions))
4391         (gnus-message 8 "Sorting threads...done")))))
4392
4393 (defun gnus-sort-articles (articles)
4394   "Sort ARTICLES."
4395   (when gnus-article-sort-functions
4396     (gnus-message 7 "Sorting articles...")
4397     (prog1
4398         (setq gnus-newsgroup-headers
4399               (sort articles (gnus-make-sort-function
4400                               gnus-article-sort-functions)))
4401       (gnus-message 7 "Sorting articles...done"))))
4402
4403 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4404 (defmacro gnus-thread-header (thread)
4405   "Return header of first article in THREAD.
4406 Note that THREAD must never, ever be anything else than a variable -
4407 using some other form will lead to serious barfage."
4408   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4409   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4410   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4411         (vector thread) 2))
4412
4413 (defsubst gnus-article-sort-by-number (h1 h2)
4414   "Sort articles by article number."
4415   (< (mail-header-number h1)
4416      (mail-header-number h2)))
4417
4418 (defun gnus-thread-sort-by-number (h1 h2)
4419   "Sort threads by root article number."
4420   (gnus-article-sort-by-number
4421    (gnus-thread-header h1) (gnus-thread-header h2)))
4422
4423 (defsubst gnus-article-sort-by-random (h1 h2)
4424   "Sort articles by article number."
4425   (zerop (random 2)))
4426
4427 (defun gnus-thread-sort-by-random (h1 h2)
4428   "Sort threads by root article number."
4429   (gnus-article-sort-by-random
4430    (gnus-thread-header h1) (gnus-thread-header h2)))
4431
4432 (defsubst gnus-article-sort-by-lines (h1 h2)
4433   "Sort articles by article Lines header."
4434   (< (mail-header-lines h1)
4435      (mail-header-lines h2)))
4436
4437 (defun gnus-thread-sort-by-lines (h1 h2)
4438   "Sort threads by root article Lines header."
4439   (gnus-article-sort-by-lines
4440    (gnus-thread-header h1) (gnus-thread-header h2)))
4441
4442 (defsubst gnus-article-sort-by-chars (h1 h2)
4443   "Sort articles by octet length."
4444   (< (mail-header-chars h1)
4445      (mail-header-chars h2)))
4446
4447 (defun gnus-thread-sort-by-chars (h1 h2)
4448   "Sort threads by root article octet length."
4449   (gnus-article-sort-by-chars
4450    (gnus-thread-header h1) (gnus-thread-header h2)))
4451
4452 (defsubst gnus-article-sort-by-author (h1 h2)
4453   "Sort articles by root author."
4454   (string-lessp
4455    (let ((extract (funcall
4456                    gnus-extract-address-components
4457                    (mail-header-from h1))))
4458      (or (car extract) (cadr extract) ""))
4459    (let ((extract (funcall
4460                    gnus-extract-address-components
4461                    (mail-header-from h2))))
4462      (or (car extract) (cadr extract) ""))))
4463
4464 (defun gnus-thread-sort-by-author (h1 h2)
4465   "Sort threads by root author."
4466   (gnus-article-sort-by-author
4467    (gnus-thread-header h1)  (gnus-thread-header h2)))
4468
4469 (defsubst gnus-article-sort-by-subject (h1 h2)
4470   "Sort articles by root subject."
4471   (string-lessp
4472    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4473    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4474
4475 (defun gnus-thread-sort-by-subject (h1 h2)
4476   "Sort threads by root subject."
4477   (gnus-article-sort-by-subject
4478    (gnus-thread-header h1) (gnus-thread-header h2)))
4479
4480 (defsubst gnus-article-sort-by-date (h1 h2)
4481   "Sort articles by root article date."
4482   (time-less-p
4483    (gnus-date-get-time (mail-header-date h1))
4484    (gnus-date-get-time (mail-header-date h2))))
4485
4486 (defun gnus-thread-sort-by-date (h1 h2)
4487   "Sort threads by root article date."
4488   (gnus-article-sort-by-date
4489    (gnus-thread-header h1) (gnus-thread-header h2)))
4490
4491 (defsubst gnus-article-sort-by-score (h1 h2)
4492   "Sort articles by root article score.
4493 Unscored articles will be counted as having a score of zero."
4494   (> (or (cdr (assq (mail-header-number h1)
4495                     gnus-newsgroup-scored))
4496          gnus-summary-default-score 0)
4497      (or (cdr (assq (mail-header-number h2)
4498                     gnus-newsgroup-scored))
4499          gnus-summary-default-score 0)))
4500
4501 (defun gnus-thread-sort-by-score (h1 h2)
4502   "Sort threads by root article score."
4503   (gnus-article-sort-by-score
4504    (gnus-thread-header h1) (gnus-thread-header h2)))
4505
4506 (defun gnus-thread-sort-by-total-score (h1 h2)
4507   "Sort threads by the sum of all scores in the thread.
4508 Unscored articles will be counted as having a score of zero."
4509   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4510
4511 (defun gnus-thread-total-score (thread)
4512   ;; This function find the total score of THREAD.
4513   (cond
4514    ((null thread)
4515     0)
4516    ((consp thread)
4517     (if (stringp (car thread))
4518         (apply gnus-thread-score-function 0
4519                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4520       (gnus-thread-total-score-1 thread)))
4521    (t
4522     (gnus-thread-total-score-1 (list thread)))))
4523
4524 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4525   "Sort threads such that the thread with the most recently arrived article comes first."
4526   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4527
4528 (defun gnus-thread-highest-number (thread)
4529   "Return the highest article number in THREAD."
4530   (apply 'max (mapcar (lambda (header)
4531                         (mail-header-number header))
4532                       (message-flatten-list thread))))
4533
4534 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4535   "Sort threads such that the thread with the most recently dated article comes first."
4536   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4537
4538 (defun gnus-thread-latest-date (thread)
4539   "Return the highest article date in THREAD."
4540   (let ((previous-time 0))
4541     (apply 'max
4542            (mapcar
4543             (lambda (header)
4544               (setq previous-time
4545                     (condition-case ()
4546                         (time-to-seconds (mail-header-parse-date
4547                                           (mail-header-date header)))
4548                       (error previous-time))))
4549             (sort
4550              (message-flatten-list thread)
4551              (lambda (h1 h2)
4552                (< (mail-header-number h1)
4553                   (mail-header-number h2))))))))
4554
4555 (defun gnus-thread-total-score-1 (root)
4556   ;; This function find the total score of the thread below ROOT.
4557   (setq root (car root))
4558   (apply gnus-thread-score-function
4559          (or (append
4560               (mapcar 'gnus-thread-total-score
4561                       (cdr (gnus-id-to-thread (mail-header-id root))))
4562               (when (> (mail-header-number root) 0)
4563                 (list (or (cdr (assq (mail-header-number root)
4564                                      gnus-newsgroup-scored))
4565                           gnus-summary-default-score 0))))
4566              (list gnus-summary-default-score)
4567              '(0))))
4568
4569 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4570 (defvar gnus-tmp-prev-subject nil)
4571 (defvar gnus-tmp-false-parent nil)
4572 (defvar gnus-tmp-root-expunged nil)
4573 (defvar gnus-tmp-dummy-line nil)
4574
4575 (eval-when-compile (defvar gnus-tmp-header))
4576 (defun gnus-extra-header (type &optional header)
4577   "Return the extra header of TYPE."
4578   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4579       ""))
4580
4581 (defvar gnus-tmp-thread-tree-header-string "")
4582
4583 (defcustom gnus-sum-thread-tree-root "> "
4584   "With %B spec, used for the root of a thread.
4585 If nil, use subject instead."
4586   :type '(radio (const :format "%v  " nil) (string :size 0))
4587   :group 'gnus-thread)
4588 (defcustom gnus-sum-thread-tree-false-root "> "
4589   "With %B spec, used for a false root of a thread.
4590 If nil, use subject instead."
4591   :type '(radio (const :format "%v  " nil) (string :size 0))
4592   :group 'gnus-thread)
4593 (defcustom gnus-sum-thread-tree-single-indent ""
4594   "With %B spec, used for a thread with just one message.
4595 If nil, use subject instead."
4596   :type '(radio (const :format "%v  " nil) (string :size 0))
4597   :group 'gnus-thread)
4598 (defcustom gnus-sum-thread-tree-vertical "| "
4599   "With %B spec, used for drawing a vertical line."
4600   :type 'string
4601   :group 'gnus-thread)
4602 (defcustom gnus-sum-thread-tree-indent "  "
4603   "With %B spec, used for indenting."
4604   :type 'string
4605   :group 'gnus-thread)
4606 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4607   "With %B spec, used for a leaf with brothers."
4608   :type 'string
4609   :group 'gnus-thread)
4610 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4611   "With %B spec, used for a leaf without brothers."
4612   :type 'string
4613   :group 'gnus-thread)
4614
4615 (defun gnus-summary-prepare-threads (threads)
4616   "Prepare summary buffer from THREADS and indentation LEVEL.
4617 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4618 or a straight list of headers."
4619   (gnus-message 7 "Generating summary...")
4620
4621   (setq gnus-newsgroup-threads threads)
4622   (beginning-of-line)
4623
4624   (let ((gnus-tmp-level 0)
4625         (default-score (or gnus-summary-default-score 0))
4626         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4627         (building-line-count gnus-summary-display-while-building)
4628         (building-count (integerp gnus-summary-display-while-building))
4629         thread number subject stack state gnus-tmp-gathered beg-match
4630         new-roots gnus-tmp-new-adopts thread-end simp-subject
4631         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4632         gnus-tmp-replied gnus-tmp-subject-or-nil
4633         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4634         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4635         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4636         tree-stack)
4637
4638     (setq gnus-tmp-prev-subject nil
4639           gnus-tmp-thread-tree-header-string "")
4640
4641     (if (vectorp (car threads))
4642         ;; If this is a straight (sic) list of headers, then a
4643         ;; threaded summary display isn't required, so we just create
4644         ;; an unthreaded one.
4645         (gnus-summary-prepare-unthreaded threads)
4646
4647       ;; Do the threaded display.
4648
4649       (if gnus-summary-display-while-building
4650           (switch-to-buffer (buffer-name)))
4651       (while (or threads stack gnus-tmp-new-adopts new-roots)
4652
4653         (if (and (= gnus-tmp-level 0)
4654                  (or (not stack)
4655                      (= (caar stack) 0))
4656                  (not gnus-tmp-false-parent)
4657                  (or gnus-tmp-new-adopts new-roots))
4658             (if gnus-tmp-new-adopts
4659                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4660                       thread (list (car gnus-tmp-new-adopts))
4661                       gnus-tmp-header (caar thread)
4662                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4663               (when new-roots
4664                 (setq thread (list (car new-roots))
4665                       gnus-tmp-header (caar thread)
4666                       new-roots (cdr new-roots))))
4667
4668           (if threads
4669               ;; If there are some threads, we do them before the
4670               ;; threads on the stack.
4671               (setq thread threads
4672                     gnus-tmp-header (caar thread))
4673             ;; There were no current threads, so we pop something off
4674             ;; the stack.
4675             (setq state (car stack)
4676                   gnus-tmp-level (car state)
4677                   tree-stack (cadr state)
4678                   thread (caddr state)
4679                   stack (cdr stack)
4680                   gnus-tmp-header (caar thread))))
4681
4682         (setq gnus-tmp-false-parent nil)
4683         (setq gnus-tmp-root-expunged nil)
4684         (setq thread-end nil)
4685
4686         (if (stringp gnus-tmp-header)
4687             ;; The header is a dummy root.
4688             (cond
4689              ((eq gnus-summary-make-false-root 'adopt)
4690               ;; We let the first article adopt the rest.
4691               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4692                                                (cddar thread)))
4693               (setq gnus-tmp-gathered
4694                     (nconc (mapcar
4695                             (lambda (h) (mail-header-number (car h)))
4696                             (cddar thread))
4697                            gnus-tmp-gathered))
4698               (setq thread (cons (list (caar thread)
4699                                        (cadar thread))
4700                                  (cdr thread)))
4701               (setq gnus-tmp-level -1
4702                     gnus-tmp-false-parent t))
4703              ((eq gnus-summary-make-false-root 'empty)
4704               ;; We print adopted articles with empty subject fields.
4705               (setq gnus-tmp-gathered
4706                     (nconc (mapcar
4707                             (lambda (h) (mail-header-number (car h)))
4708                             (cddar thread))
4709                            gnus-tmp-gathered))
4710               (setq gnus-tmp-level -1))
4711              ((eq gnus-summary-make-false-root 'dummy)
4712               ;; We remember that we probably want to output a dummy
4713               ;; root.
4714               (setq gnus-tmp-dummy-line gnus-tmp-header)
4715               (setq gnus-tmp-prev-subject gnus-tmp-header))
4716              (t
4717               ;; We do not make a root for the gathered
4718               ;; sub-threads at all.
4719               (setq gnus-tmp-level -1)))
4720
4721           (setq number (mail-header-number gnus-tmp-header)
4722                 subject (mail-header-subject gnus-tmp-header)
4723                 simp-subject (gnus-simplify-subject-fully subject))
4724
4725           (cond
4726            ;; If the thread has changed subject, we might want to make
4727            ;; this subthread into a root.
4728            ((and (null gnus-thread-ignore-subject)
4729                  (not (zerop gnus-tmp-level))
4730                  gnus-tmp-prev-subject
4731                  (not (string= gnus-tmp-prev-subject simp-subject)))
4732             (setq new-roots (nconc new-roots (list (car thread)))
4733                   thread-end t
4734                   gnus-tmp-header nil))
4735            ;; If the article lies outside the current limit,
4736            ;; then we do not display it.
4737            ((not (memq number gnus-newsgroup-limit))
4738             (setq gnus-tmp-gathered
4739                   (nconc (mapcar
4740                           (lambda (h) (mail-header-number (car h)))
4741                           (cdar thread))
4742                          gnus-tmp-gathered))
4743             (setq gnus-tmp-new-adopts (if (cdar thread)
4744                                           (append gnus-tmp-new-adopts
4745                                                   (cdar thread))
4746                                         gnus-tmp-new-adopts)
4747                   thread-end t
4748                   gnus-tmp-header nil)
4749             (when (zerop gnus-tmp-level)
4750               (setq gnus-tmp-root-expunged t)))
4751            ;; Perhaps this article is to be marked as read?
4752            ((and gnus-summary-mark-below
4753                  (< (or (cdr (assq number gnus-newsgroup-scored))
4754                         default-score)
4755                     gnus-summary-mark-below)
4756                  ;; Don't touch sparse articles.
4757                  (not (gnus-summary-article-sparse-p number))
4758                  (not (gnus-summary-article-ancient-p number)))
4759             (setq gnus-newsgroup-unreads
4760                   (delq number gnus-newsgroup-unreads))
4761             (if gnus-newsgroup-auto-expire
4762                 (setq gnus-newsgroup-expirable
4763                       (gnus-add-to-sorted-list
4764                        gnus-newsgroup-expirable number))
4765               (push (cons number gnus-low-score-mark)
4766                     gnus-newsgroup-reads))))
4767
4768           (when gnus-tmp-header
4769             ;; We may have an old dummy line to output before this
4770             ;; article.
4771             (when (and gnus-tmp-dummy-line
4772                        (gnus-subject-equal
4773                         gnus-tmp-dummy-line
4774                         (mail-header-subject gnus-tmp-header)))
4775               (gnus-summary-insert-dummy-line
4776                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4777               (setq gnus-tmp-dummy-line nil))
4778
4779             ;; Compute the mark.
4780             (setq gnus-tmp-unread (gnus-article-mark number))
4781
4782             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4783                                   gnus-tmp-header gnus-tmp-level)
4784                   gnus-newsgroup-data)
4785
4786             ;; Actually insert the line.
4787             (setq
4788              gnus-tmp-subject-or-nil
4789              (cond
4790               ((and gnus-thread-ignore-subject
4791                     gnus-tmp-prev-subject
4792                     (not (string= gnus-tmp-prev-subject simp-subject)))
4793                subject)
4794               ((zerop gnus-tmp-level)
4795                (if (and (eq gnus-summary-make-false-root 'empty)
4796                         (memq number gnus-tmp-gathered)
4797                         gnus-tmp-prev-subject
4798                         (string= gnus-tmp-prev-subject simp-subject))
4799                    gnus-summary-same-subject
4800                  subject))
4801               (t gnus-summary-same-subject)))
4802             (if (and (eq gnus-summary-make-false-root 'adopt)
4803                      (= gnus-tmp-level 1)
4804                      (memq number gnus-tmp-gathered))
4805                 (setq gnus-tmp-opening-bracket ?\<
4806                       gnus-tmp-closing-bracket ?\>)
4807               (setq gnus-tmp-opening-bracket ?\[
4808                     gnus-tmp-closing-bracket ?\]))
4809             (setq
4810              gnus-tmp-indentation
4811              (aref gnus-thread-indent-array gnus-tmp-level)
4812              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4813              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4814                                 gnus-summary-default-score 0)
4815              gnus-tmp-score-char
4816              (if (or (null gnus-summary-default-score)
4817                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4818                          gnus-summary-zcore-fuzz))
4819                  ?                      ;Whitespace
4820                (if (< gnus-tmp-score gnus-summary-default-score)
4821                    gnus-score-below-mark gnus-score-over-mark))
4822              gnus-tmp-replied
4823              (cond ((memq number gnus-newsgroup-processable)
4824                     gnus-process-mark)
4825                    ((memq number gnus-newsgroup-cached)
4826                     gnus-cached-mark)
4827                    ((memq number gnus-newsgroup-replied)
4828                     gnus-replied-mark)
4829                    ((memq number gnus-newsgroup-forwarded)
4830                     gnus-forwarded-mark)
4831                    ((memq number gnus-newsgroup-saved)
4832                     gnus-saved-mark)
4833                    ((memq number gnus-newsgroup-recent)
4834                     gnus-recent-mark)
4835                    ((memq number gnus-newsgroup-unseen)
4836                     gnus-unseen-mark)
4837                    (t gnus-no-mark))
4838              gnus-tmp-downloaded
4839              (cond ((memq number gnus-newsgroup-undownloaded)
4840                     gnus-undownloaded-mark)
4841                    (gnus-newsgroup-agentized
4842                     gnus-downloaded-mark)
4843                    (t
4844                     gnus-no-mark))
4845              gnus-tmp-from (mail-header-from gnus-tmp-header)
4846              gnus-tmp-name
4847              (cond
4848               ((string-match "<[^>]+> *$" gnus-tmp-from)
4849                (setq beg-match (match-beginning 0))
4850                (or (and (string-match "^\".+\"" gnus-tmp-from)
4851                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4852                    (substring gnus-tmp-from 0 beg-match)))
4853               ((string-match "(.+)" gnus-tmp-from)
4854                (substring gnus-tmp-from
4855                           (1+ (match-beginning 0)) (1- (match-end 0))))
4856               (t gnus-tmp-from))
4857
4858              ;; Do the %B string
4859              gnus-tmp-thread-tree-header-string
4860              (cond
4861               ((not gnus-show-threads) "")
4862               ((zerop gnus-tmp-level)
4863                (cond ((cdar thread)
4864                       (or gnus-sum-thread-tree-root subject))
4865                      (gnus-tmp-new-adopts
4866                       (or gnus-sum-thread-tree-false-root subject))
4867                      (t
4868                       (or gnus-sum-thread-tree-single-indent subject))))
4869               (t
4870                (concat (apply 'concat
4871                               (mapcar (lambda (item)
4872                                         (if (= item 1)
4873                                             gnus-sum-thread-tree-vertical
4874                                           gnus-sum-thread-tree-indent))
4875                                       (cdr (reverse tree-stack))))
4876                        (if (nth 1 thread)
4877                            gnus-sum-thread-tree-leaf-with-other
4878                          gnus-sum-thread-tree-single-leaf)))))
4879             (when (string= gnus-tmp-name "")
4880               (setq gnus-tmp-name gnus-tmp-from))
4881             (unless (numberp gnus-tmp-lines)
4882               (setq gnus-tmp-lines -1))
4883             (if (= gnus-tmp-lines -1)
4884                 (setq gnus-tmp-lines "?")
4885               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4886               (gnus-put-text-property
4887              (point)
4888              (progn (eval gnus-summary-line-format-spec) (point))
4889                'gnus-number number)
4890             (when gnus-visual-p
4891               (forward-line -1)
4892               (gnus-run-hooks 'gnus-summary-update-hook)
4893               (forward-line 1))
4894
4895             (setq gnus-tmp-prev-subject simp-subject)))
4896
4897         (when (nth 1 thread)
4898           (push (list (max 0 gnus-tmp-level)
4899                       (copy-sequence tree-stack)
4900                       (nthcdr 1 thread))
4901                 stack))
4902         (push (if (nth 1 thread) 1 0) tree-stack)
4903         (incf gnus-tmp-level)
4904         (setq threads (if thread-end nil (cdar thread)))
4905         (if gnus-summary-display-while-building
4906             (if building-count
4907                 (progn
4908                   ;; use a set frequency
4909                   (setq building-line-count (1- building-line-count))
4910                   (when (= building-line-count 0)
4911                     (sit-for 0)
4912                     (setq building-line-count
4913                           gnus-summary-display-while-building)))
4914               ;; always
4915               (sit-for 0)))
4916         (unless threads
4917           (setq gnus-tmp-level 0)))))
4918   (gnus-message 7 "Generating summary...done"))
4919
4920 (defun gnus-summary-prepare-unthreaded (headers)
4921   "Generate an unthreaded summary buffer based on HEADERS."
4922   (let (header number mark)
4923
4924     (beginning-of-line)
4925
4926     (while headers
4927       ;; We may have to root out some bad articles...
4928       (when (memq (setq number (mail-header-number
4929                                 (setq header (pop headers))))
4930                   gnus-newsgroup-limit)
4931         ;; Mark article as read when it has a low score.
4932         (when (and gnus-summary-mark-below
4933                    (< (or (cdr (assq number gnus-newsgroup-scored))
4934                           gnus-summary-default-score 0)
4935                       gnus-summary-mark-below)
4936                    (not (gnus-summary-article-ancient-p number)))
4937           (setq gnus-newsgroup-unreads
4938                 (delq number gnus-newsgroup-unreads))
4939           (if gnus-newsgroup-auto-expire
4940               (push number gnus-newsgroup-expirable)
4941             (push (cons number gnus-low-score-mark)
4942                   gnus-newsgroup-reads)))
4943
4944         (setq mark (gnus-article-mark number))
4945         (push (gnus-data-make number mark (1+ (point)) header 0)
4946               gnus-newsgroup-data)
4947         (gnus-summary-insert-line
4948          header 0 number
4949          (memq number gnus-newsgroup-undownloaded)
4950          mark (memq number gnus-newsgroup-replied)
4951          (memq number gnus-newsgroup-expirable)
4952          (mail-header-subject header) nil
4953          (cdr (assq number gnus-newsgroup-scored))
4954          (memq number gnus-newsgroup-processable))))))
4955
4956 (defun gnus-summary-remove-list-identifiers ()
4957   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4958   (let ((regexp (if (consp gnus-list-identifiers)
4959                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4960                   gnus-list-identifiers))
4961         changed subject)
4962     (when regexp
4963       (dolist (header gnus-newsgroup-headers)
4964         (setq subject (mail-header-subject header)
4965               changed nil)
4966         (while (string-match
4967                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4968                 subject)
4969           (setq subject
4970                 (concat (substring subject 0 (match-beginning 2))
4971                         (substring subject (match-end 0)))
4972                 changed t))
4973         (when (and changed
4974                    (string-match
4975                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4976           (setq subject
4977                 (concat (substring subject 0 (match-beginning 1))
4978                         (substring subject (match-end 1)))))
4979         (when changed
4980           (mail-header-set-subject header subject))))))
4981
4982 (defun gnus-fetch-headers (articles)
4983   "Fetch headers of ARTICLES."
4984   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4985     (gnus-message 5 "Fetching headers for %s..." name)
4986     (prog1
4987         (if (eq 'nov
4988                 (setq gnus-headers-retrieved-by
4989                       (gnus-retrieve-headers
4990                        articles gnus-newsgroup-name
4991                        ;; We might want to fetch old headers, but
4992                        ;; not if there is only 1 article.
4993                        (and (or (and
4994                                  (not (eq gnus-fetch-old-headers 'some))
4995                                  (not (numberp gnus-fetch-old-headers)))
4996                                 (> (length articles) 1))
4997                             gnus-fetch-old-headers))))
4998             (gnus-get-newsgroup-headers-xover
4999              articles nil nil gnus-newsgroup-name t)
5000           (gnus-get-newsgroup-headers))
5001       (gnus-message 5 "Fetching headers for %s...done" name))))
5002
5003 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5004   "Select newsgroup GROUP.
5005 If READ-ALL is non-nil, all articles in the group are selected.
5006 If SELECT-ARTICLES, only select those articles from GROUP."
5007   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5008          ;;!!! Dirty hack; should be removed.
5009          (gnus-summary-ignore-duplicates
5010           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5011               t
5012             gnus-summary-ignore-duplicates))
5013          (info (nth 2 entry))
5014          articles fetched-articles cached)
5015
5016     (unless (gnus-check-server
5017              (set (make-local-variable 'gnus-current-select-method)
5018                   (gnus-find-method-for-group group)))
5019       (error "Couldn't open server"))
5020
5021     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5022         (gnus-activate-group group)     ; Or we can activate it...
5023         (progn                          ; Or we bug out.
5024           (when (equal major-mode 'gnus-summary-mode)
5025             (gnus-kill-buffer (current-buffer)))
5026           (error "Couldn't activate group %s: %s"
5027                  group (gnus-status-message group))))
5028
5029     (unless (gnus-request-group group t)
5030       (when (equal major-mode 'gnus-summary-mode)
5031         (gnus-kill-buffer (current-buffer)))
5032       (error "Couldn't request group %s: %s"
5033              group (gnus-status-message group)))
5034
5035     (when gnus-agent
5036       ;; The agent may be storing articles that are no longer in the
5037       ;; server's active range.  If that is the case, the active range
5038       ;; needs to be expanded such that the agent's articles can be
5039       ;; included in the summary.
5040       (let* ((gnus-command-method (gnus-find-method-for-group group))
5041              (alist (gnus-agent-load-alist group))
5042              (active (gnus-active group)))
5043         (if (and (car alist)
5044                  (< (caar alist) (car active)))
5045             (gnus-set-active group (cons (caar alist) (cdr active)))))
5046
5047       (setq gnus-summary-use-undownloaded-faces
5048             (gnus-agent-find-parameter
5049              group
5050              'agent-enable-undownloaded-faces)))
5051
5052     (setq gnus-newsgroup-name group
5053           gnus-newsgroup-unselected nil
5054           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5055
5056     (let ((display (gnus-group-find-parameter group 'display)))
5057       (setq gnus-newsgroup-display
5058             (cond
5059              ((not (zerop (or (car-safe read-all) 0)))
5060               ;; The user entered the group with C-u SPC/RET, let's show
5061               ;; all articles.
5062               'gnus-not-ignore)
5063              ((eq display 'all)
5064               'gnus-not-ignore)
5065              ((arrayp display)
5066               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5067              ((numberp display)
5068               ;; The following is probably the "correct" solution, but
5069               ;; it makes Gnus fetch all headers and then limit the
5070               ;; articles (which is slow), so instead we hack the
5071               ;; select-articles parameter instead. -- Simon Josefsson
5072               ;; <jas@kth.se>
5073               ;;
5074               ;; (gnus-byte-compile
5075               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5076               ;;                         display)))))
5077               (setq select-articles
5078                     (gnus-uncompress-range
5079                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5080                              (if (> tmp 0)
5081                                  tmp
5082                                1))
5083                            (cdr (gnus-active group)))))
5084               nil)
5085              (t
5086               nil))))
5087
5088     (gnus-summary-setup-default-charset)
5089
5090     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5091     (when (gnus-virtual-group-p group)
5092       (setq cached gnus-newsgroup-cached))
5093
5094     (setq gnus-newsgroup-unreads
5095           (gnus-sorted-ndifference
5096            (gnus-sorted-ndifference gnus-newsgroup-unreads
5097                                     gnus-newsgroup-marked)
5098            gnus-newsgroup-dormant))
5099
5100     (setq gnus-newsgroup-processable nil)
5101
5102     (gnus-update-read-articles group gnus-newsgroup-unreads)
5103
5104     ;; Adjust and set lists of article marks.
5105     (when info
5106       (gnus-adjust-marked-articles info))
5107     (if (setq articles select-articles)
5108         (setq gnus-newsgroup-unselected
5109               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5110       (setq articles (gnus-articles-to-read group read-all)))
5111
5112     (cond
5113      ((null articles)
5114       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5115       'quit)
5116      ((eq articles 0) nil)
5117      (t
5118       ;; Init the dependencies hash table.
5119       (setq gnus-newsgroup-dependencies
5120             (gnus-make-hashtable (length articles)))
5121       (gnus-set-global-variables)
5122       ;; Retrieve the headers and read them in.
5123
5124       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5125
5126       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5127       (when cached
5128         (setq gnus-newsgroup-cached cached))
5129
5130       ;; Suppress duplicates?
5131       (when gnus-suppress-duplicates
5132         (gnus-dup-suppress-articles))
5133
5134       ;; Set the initial limit.
5135       (setq gnus-newsgroup-limit (copy-sequence articles))
5136       ;; Remove canceled articles from the list of unread articles.
5137       (setq fetched-articles
5138             (mapcar (lambda (headers) (mail-header-number headers))
5139                     gnus-newsgroup-headers))
5140       (setq gnus-newsgroup-articles fetched-articles)
5141       (setq gnus-newsgroup-unreads
5142             (gnus-sorted-nintersection
5143              gnus-newsgroup-unreads fetched-articles))
5144       (gnus-compute-unseen-list)
5145
5146       ;; Removed marked articles that do not exist.
5147       (gnus-update-missing-marks
5148        (gnus-sorted-difference articles fetched-articles))
5149       ;; We might want to build some more threads first.
5150       (when (and gnus-fetch-old-headers
5151                  (eq gnus-headers-retrieved-by 'nov))
5152         (if (eq gnus-fetch-old-headers 'invisible)
5153             (gnus-build-all-threads)
5154           (gnus-build-old-threads)))
5155       ;; Let the Gnus agent mark articles as read.
5156       (when gnus-agent
5157         (gnus-agent-get-undownloaded-list))
5158       ;; Remove list identifiers from subject
5159       (when gnus-list-identifiers
5160         (gnus-summary-remove-list-identifiers))
5161       ;; Check whether auto-expire is to be done in this group.
5162       (setq gnus-newsgroup-auto-expire
5163             (gnus-group-auto-expirable-p group))
5164       ;; Set up the article buffer now, if necessary.
5165       (unless gnus-single-article-buffer
5166         (gnus-article-setup-buffer))
5167       ;; First and last article in this newsgroup.
5168       (when gnus-newsgroup-headers
5169         (setq gnus-newsgroup-begin
5170               (mail-header-number (car gnus-newsgroup-headers))
5171               gnus-newsgroup-end
5172               (mail-header-number
5173                (gnus-last-element gnus-newsgroup-headers))))
5174       ;; GROUP is successfully selected.
5175       (or gnus-newsgroup-headers t)))))
5176
5177 (defun gnus-compute-unseen-list ()
5178   ;; The `seen' marks are treated specially.
5179   (if (not gnus-newsgroup-seen)
5180       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5181     (setq gnus-newsgroup-unseen
5182           (gnus-inverse-list-range-intersection
5183            gnus-newsgroup-articles gnus-newsgroup-seen))))
5184
5185 (defun gnus-summary-display-make-predicate (display)
5186   (require 'gnus-agent)
5187   (when (= (length display) 1)
5188     (setq display (car display)))
5189   (unless gnus-summary-display-cache
5190     (dolist (elem (append '((unread . unread)
5191                             (read . read)
5192                             (unseen . unseen))
5193                           gnus-article-mark-lists))
5194       (push (cons (cdr elem)
5195                   (gnus-byte-compile
5196                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5197             gnus-summary-display-cache)))
5198   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5199         (gnus-category-predicate-cache gnus-summary-display-cache))
5200     (gnus-get-predicate display)))
5201
5202 ;; Uses the dynamically bound `number' variable.
5203 (eval-when-compile
5204   (defvar number))
5205 (defun gnus-article-marked-p (type &optional article)
5206   (let ((article (or article number)))
5207     (cond
5208      ((eq type 'tick)
5209       (memq article gnus-newsgroup-marked))
5210      ((eq type 'spam)
5211       (memq article gnus-newsgroup-spam-marked))
5212      ((eq type 'unsend)
5213       (memq article gnus-newsgroup-unsendable))
5214      ((eq type 'undownload)
5215       (memq article gnus-newsgroup-undownloaded))
5216      ((eq type 'download)
5217       (memq article gnus-newsgroup-downloadable))
5218      ((eq type 'unread)
5219       (memq article gnus-newsgroup-unreads))
5220      ((eq type 'read)
5221       (memq article gnus-newsgroup-reads))
5222      ((eq type 'dormant)
5223       (memq article gnus-newsgroup-dormant) )
5224      ((eq type 'expire)
5225       (memq article gnus-newsgroup-expirable))
5226      ((eq type 'reply)
5227       (memq article gnus-newsgroup-replied))
5228      ((eq type 'killed)
5229       (memq article gnus-newsgroup-killed))
5230      ((eq type 'bookmark)
5231       (assq article gnus-newsgroup-bookmarks))
5232      ((eq type 'score)
5233       (assq article gnus-newsgroup-scored))
5234      ((eq type 'save)
5235       (memq article gnus-newsgroup-saved))
5236      ((eq type 'cache)
5237       (memq article gnus-newsgroup-cached))
5238      ((eq type 'forward)
5239       (memq article gnus-newsgroup-forwarded))
5240      ((eq type 'seen)
5241       (not (memq article gnus-newsgroup-unseen)))
5242      ((eq type 'recent)
5243       (memq article gnus-newsgroup-recent))
5244      (t t))))
5245
5246 (defun gnus-articles-to-read (group &optional read-all)
5247   "Find out what articles the user wants to read."
5248   (let* ((display (gnus-group-find-parameter group 'display))
5249          (articles
5250           ;; Select all articles if `read-all' is non-nil, or if there
5251           ;; are no unread articles.
5252           (if (or read-all
5253                   (and (zerop (length gnus-newsgroup-marked))
5254                        (zerop (length gnus-newsgroup-unreads)))
5255                   ;; Fetch all if the predicate is non-nil.
5256                   gnus-newsgroup-display)
5257               ;; We want to select the headers for all the articles in
5258               ;; the group, so we select either all the active
5259               ;; articles in the group, or (if that's nil), the
5260               ;; articles in the cache.
5261               (or
5262                (gnus-uncompress-range (gnus-active group))
5263                (gnus-cache-articles-in-group group))
5264             ;; Select only the "normal" subset of articles.
5265             (gnus-sorted-nunion
5266              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5267              gnus-newsgroup-unreads)))
5268          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5269          (scored (length scored-list))
5270          (number (length articles))
5271          (marked (+ (length gnus-newsgroup-marked)
5272                     (length gnus-newsgroup-dormant)))
5273          (select
5274           (cond
5275            ((numberp read-all)
5276             read-all)
5277            ((numberp gnus-newsgroup-display)
5278             gnus-newsgroup-display)
5279            (t
5280             (condition-case ()
5281                 (cond
5282                  ((and (or (<= scored marked) (= scored number))
5283                        (numberp gnus-large-newsgroup)
5284                        (> number gnus-large-newsgroup))
5285                   (let* ((cursor-in-echo-area nil)
5286                          (initial (gnus-parameter-large-newsgroup-initial
5287                                    gnus-newsgroup-name))
5288                          (input
5289                           (read-string
5290                            (format
5291                             "How many articles from %s (%s %d): "
5292                             (gnus-limit-string
5293                              (gnus-group-decoded-name gnus-newsgroup-name)
5294                              35)
5295                             (if initial "max" "default")
5296                             number)
5297                            (if initial
5298                                (cons (number-to-string initial)
5299                                      0)))))
5300                     (if (string-match "^[ \t]*$" input) number input)))
5301                  ((and (> scored marked) (< scored number)
5302                        (> (- scored number) 20))
5303                   (let ((input
5304                          (read-string
5305                           (format "%s %s (%d scored, %d total): "
5306                                   "How many articles from"
5307                                   (gnus-group-decoded-name group)
5308                                   scored number))))
5309                     (if (string-match "^[ \t]*$" input)
5310                         number input)))
5311                  (t number))
5312               (quit
5313                (message "Quit getting the articles to read")
5314                nil))))))
5315     (setq select (if (stringp select) (string-to-number select) select))
5316     (if (or (null select) (zerop select))
5317         select
5318       (if (and (not (zerop scored)) (<= (abs select) scored))
5319           (progn
5320             (setq articles (sort scored-list '<))
5321             (setq number (length articles)))
5322         (setq articles (copy-sequence articles)))
5323
5324       (when (< (abs select) number)
5325         (if (< select 0)
5326             ;; Select the N oldest articles.
5327             (setcdr (nthcdr (1- (abs select)) articles) nil)
5328           ;; Select the N most recent articles.
5329           (setq articles (nthcdr (- number select) articles))))
5330       (setq gnus-newsgroup-unselected
5331             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5332       (when gnus-alter-articles-to-read-function
5333         (setq articles
5334               (sort
5335                (funcall gnus-alter-articles-to-read-function
5336                         gnus-newsgroup-name articles)
5337                '<)))
5338       articles)))
5339
5340 (defun gnus-killed-articles (killed articles)
5341   (let (out)
5342     (while articles
5343       (when (inline (gnus-member-of-range (car articles) killed))
5344         (push (car articles) out))
5345       (setq articles (cdr articles)))
5346     out))
5347
5348 (defun gnus-uncompress-marks (marks)
5349   "Uncompress the mark ranges in MARKS."
5350   (let ((uncompressed '(score bookmark))
5351         out)
5352     (while marks
5353       (if (memq (caar marks) uncompressed)
5354           (push (car marks) out)
5355         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5356       (setq marks (cdr marks)))
5357     out))
5358
5359 (defun gnus-article-mark-to-type (mark)
5360   "Return the type of MARK."
5361   (or (cadr (assq mark gnus-article-special-mark-lists))
5362       'list))
5363
5364 (defun gnus-article-unpropagatable-p (mark)
5365   "Return whether MARK should be propagated to back end."
5366   (memq mark gnus-article-unpropagated-mark-lists))
5367
5368 (defun gnus-adjust-marked-articles (info)
5369   "Set all article lists and remove all marks that are no longer valid."
5370   (let* ((marked-lists (gnus-info-marks info))
5371          (active (gnus-active (gnus-info-group info)))
5372          (min (car active))
5373          (max (cdr active))
5374          (types gnus-article-mark-lists)
5375          marks var articles article mark mark-type)
5376
5377     (dolist (marks marked-lists)
5378       (setq mark (car marks)
5379             mark-type (gnus-article-mark-to-type mark)
5380             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5381
5382       ;; We set the variable according to the type of the marks list,
5383       ;; and then adjust the marks to a subset of the active articles.
5384       (cond
5385        ;; Adjust "simple" lists.
5386        ((eq mark-type 'list)
5387         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5388         (when (memq mark '(tick dormant expire reply save))
5389           (while articles
5390             (when (or (< (setq article (pop articles)) min) (> article max))
5391               (set var (delq article (symbol-value var)))))))
5392        ;; Adjust assocs.
5393        ((eq mark-type 'tuple)
5394         (set var (setq articles (cdr marks)))
5395         (when (not (listp (cdr (symbol-value var))))
5396           (set var (list (symbol-value var))))
5397         (when (not (listp (cdr articles)))
5398           (setq articles (list articles)))
5399         (while articles
5400           (when (or (not (consp (setq article (pop articles))))
5401                     (< (car article) min)
5402                     (> (car article) max))
5403             (set var (delq article (symbol-value var))))))
5404        ;; Adjust ranges (sloppily).
5405        ((eq mark-type 'range)
5406         (cond
5407          ((eq mark 'seen)
5408           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5409           ;; It should be (seen (NUM1 . NUM2)).
5410           (when (numberp (cddr marks))
5411             (setcdr marks (list (cdr marks))))
5412           (setq articles (cdr marks))
5413           (while (and articles
5414                       (or (and (consp (car articles))
5415                                (> min (cdar articles)))
5416                           (and (numberp (car articles))
5417                                (> min (car articles)))))
5418             (pop articles))
5419           (set var articles))))))))
5420
5421 (defun gnus-update-missing-marks (missing)
5422   "Go through the list of MISSING articles and remove them from the mark lists."
5423   (when missing
5424     (let (var m)
5425       ;; Go through all types.
5426       (dolist (elem gnus-article-mark-lists)
5427         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5428           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5429           (when (symbol-value var)
5430             ;; This list has articles.  So we delete all missing
5431             ;; articles from it.
5432             (setq m missing)
5433             (while m
5434               (set var (delq (pop m) (symbol-value var))))))))))
5435
5436 (defun gnus-update-marks ()
5437   "Enter the various lists of marked articles into the newsgroup info list."
5438   (let ((types gnus-article-mark-lists)
5439         (info (gnus-get-info gnus-newsgroup-name))
5440         type list newmarked symbol delta-marks)
5441     (when info
5442       ;; Add all marks lists to the list of marks lists.
5443       (while (setq type (pop types))
5444         (setq list (symbol-value
5445                     (setq symbol
5446                           (intern (format "gnus-newsgroup-%s" (car type))))))
5447
5448         (when list
5449           ;; Get rid of the entries of the articles that have the
5450           ;; default score.
5451           (when (and (eq (cdr type) 'score)
5452                      gnus-save-score
5453                      list)
5454             (let* ((arts list)
5455                    (prev (cons nil list))
5456                    (all prev))
5457               (while arts
5458                 (if (or (not (consp (car arts)))
5459                         (= (cdar arts) gnus-summary-default-score))
5460                     (setcdr prev (cdr arts))
5461                   (setq prev arts))
5462                 (setq arts (cdr arts)))
5463               (setq list (cdr all)))))
5464
5465         (when (eq (cdr type) 'seen)
5466           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5467
5468         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5469           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5470
5471         (when (and (gnus-check-backend-function
5472                     'request-set-mark gnus-newsgroup-name)
5473                    (not (gnus-article-unpropagatable-p (cdr type))))
5474           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5475                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5476                  (add (gnus-remove-from-range
5477                        (gnus-copy-sequence list) old)))
5478             (when add
5479               (push (list add 'add (list (cdr type))) delta-marks))
5480             (when del
5481               (push (list del 'del (list (cdr type))) delta-marks))))
5482
5483         (when list
5484           (push (cons (cdr type) list) newmarked)))
5485
5486       (when delta-marks
5487         (unless (gnus-check-group gnus-newsgroup-name)
5488           (error "Can't open server for %s" gnus-newsgroup-name))
5489         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5490
5491       ;; Enter these new marks into the info of the group.
5492       (if (nthcdr 3 info)
5493           (setcar (nthcdr 3 info) newmarked)
5494         ;; Add the marks lists to the end of the info.
5495         (when newmarked
5496           (setcdr (nthcdr 2 info) (list newmarked))))
5497
5498       ;; Cut off the end of the info if there's nothing else there.
5499       (let ((i 5))
5500         (while (and (> i 2)
5501                     (not (nth i info)))
5502           (when (nthcdr (decf i) info)
5503             (setcdr (nthcdr i info) nil)))))))
5504
5505 (defun gnus-set-mode-line (where)
5506   "Set the mode line of the article or summary buffers.
5507 If WHERE is `summary', the summary mode line format will be used."
5508   ;; Is this mode line one we keep updated?
5509   (when (and (memq where gnus-updated-mode-lines)
5510              (symbol-value
5511               (intern (format "gnus-%s-mode-line-format-spec" where))))
5512     (let (mode-string)
5513       (save-excursion
5514         ;; We evaluate this in the summary buffer since these
5515         ;; variables are buffer-local to that buffer.
5516         (set-buffer gnus-summary-buffer)
5517        ;; We bind all these variables that are used in the `eval' form
5518         ;; below.
5519         (let* ((mformat (symbol-value
5520                          (intern
5521                           (format "gnus-%s-mode-line-format-spec" where))))
5522                (gnus-tmp-group-name (gnus-group-decoded-name
5523                                      gnus-newsgroup-name))
5524                (gnus-tmp-article-number (or gnus-current-article 0))
5525                (gnus-tmp-unread gnus-newsgroup-unreads)
5526                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5527                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5528                (gnus-tmp-unread-and-unselected
5529                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5530                             (zerop gnus-tmp-unselected))
5531                        "")
5532                       ((zerop gnus-tmp-unselected)
5533                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5534                       (t (format "{%d(+%d) more}"
5535                                  gnus-tmp-unread-and-unticked
5536                                  gnus-tmp-unselected))))
5537                (gnus-tmp-subject
5538                 (if (and gnus-current-headers
5539                          (vectorp gnus-current-headers))
5540                     (gnus-mode-string-quote
5541                      (mail-header-subject gnus-current-headers))
5542                   ""))
5543                bufname-length max-len
5544                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5545           (setq mode-string (eval mformat))
5546           (setq bufname-length (if (string-match "%b" mode-string)
5547                                    (- (length
5548                                        (buffer-name
5549                                         (if (eq where 'summary)
5550                                             nil
5551                                           (get-buffer gnus-article-buffer))))
5552                                       2)
5553                                  0))
5554           (setq max-len (max 4 (if gnus-mode-non-string-length
5555                                    (- (window-width)
5556                                       gnus-mode-non-string-length
5557                                       bufname-length)
5558                                  (length mode-string))))
5559           ;; We might have to chop a bit of the string off...
5560           (when (> (length mode-string) max-len)
5561             (setq mode-string
5562                   (concat (truncate-string-to-width mode-string (- max-len 3))
5563                           "...")))
5564           ;; Pad the mode string a bit.
5565           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5566       ;; Update the mode line.
5567       (setq mode-line-buffer-identification
5568             (gnus-mode-line-buffer-identification (list mode-string)))
5569       (set-buffer-modified-p t))))
5570
5571 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5572   "Go through the HEADERS list and add all Xrefs to a hash table.
5573 The resulting hash table is returned, or nil if no Xrefs were found."
5574   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5575          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5576          (xref-hashtb (gnus-make-hashtable))
5577          start group entry number xrefs header)
5578     (while headers
5579       (setq header (pop headers))
5580       (when (and (setq xrefs (mail-header-xref header))
5581                  (not (memq (setq number (mail-header-number header))
5582                             unreads)))
5583         (setq start 0)
5584         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5585           (setq start (match-end 0))
5586           (setq group (if prefix
5587                           (concat prefix (substring xrefs (match-beginning 1)
5588                                                     (match-end 1)))
5589                         (substring xrefs (match-beginning 1) (match-end 1))))
5590           (setq number
5591                 (string-to-int (substring xrefs (match-beginning 2)
5592                                           (match-end 2))))
5593           (if (setq entry (gnus-gethash group xref-hashtb))
5594               (setcdr entry (cons number (cdr entry)))
5595             (gnus-sethash group (cons number nil) xref-hashtb)))))
5596     (and start xref-hashtb)))
5597
5598 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5599   "Look through all the headers and mark the Xrefs as read."
5600   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5601         name entry info xref-hashtb idlist method nth4)
5602     (save-excursion
5603       (set-buffer gnus-group-buffer)
5604       (when (setq xref-hashtb
5605                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5606         (mapatoms
5607          (lambda (group)
5608            (unless (string= from-newsgroup (setq name (symbol-name group)))
5609              (setq idlist (symbol-value group))
5610              ;; Dead groups are not updated.
5611              (and (prog1
5612                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5613                             info (nth 2 entry))
5614                     (when (stringp (setq nth4 (gnus-info-method info)))
5615                       (setq nth4 (gnus-server-to-method nth4))))
5616                   ;; Only do the xrefs if the group has the same
5617                   ;; select method as the group we have just read.
5618                   (or (gnus-methods-equal-p
5619                        nth4 (gnus-find-method-for-group from-newsgroup))
5620                       virtual
5621                       (equal nth4 (setq method (gnus-find-method-for-group
5622                                                 from-newsgroup)))
5623                       (and (equal (car nth4) (car method))
5624                            (equal (nth 1 nth4) (nth 1 method))))
5625                   gnus-use-cross-reference
5626                   (or (not (eq gnus-use-cross-reference t))
5627                       virtual
5628                       ;; Only do cross-references on subscribed
5629                       ;; groups, if that is what is wanted.
5630                       (<= (gnus-info-level info) gnus-level-subscribed))
5631                   (gnus-group-make-articles-read name idlist))))
5632          xref-hashtb)))))
5633
5634 (defun gnus-compute-read-articles (group articles)
5635   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5636          (info (nth 2 entry))
5637          (active (gnus-active group))
5638          ninfo)
5639     (when entry
5640       ;; First peel off all invalid article numbers.
5641       (when active
5642         (let ((ids articles)
5643               id first)
5644           (while (setq id (pop ids))
5645             (when (and first (> id (cdr active)))
5646               ;; We'll end up in this situation in one particular
5647               ;; obscure situation.  If you re-scan a group and get
5648               ;; a new article that is cross-posted to a different
5649               ;; group that has not been re-scanned, you might get
5650               ;; crossposted article that has a higher number than
5651               ;; Gnus believes possible.  So we re-activate this
5652               ;; group as well.  This might mean doing the
5653               ;; crossposting thingy will *increase* the number
5654               ;; of articles in some groups.  Tsk, tsk.
5655               (setq active (or (gnus-activate-group group) active)))
5656             (when (or (> id (cdr active))
5657                       (< id (car active)))
5658               (setq articles (delq id articles))))))
5659       ;; If the read list is nil, we init it.
5660       (if (and active
5661                (null (gnus-info-read info))
5662                (> (car active) 1))
5663           (setq ninfo (cons 1 (1- (car active))))
5664         (setq ninfo (gnus-info-read info)))
5665       ;; Then we add the read articles to the range.
5666       (gnus-add-to-range
5667        ninfo (setq articles (sort articles '<))))))
5668
5669 (defun gnus-group-make-articles-read (group articles)
5670   "Update the info of GROUP to say that ARTICLES are read."
5671   (let* ((num 0)
5672          (entry (gnus-gethash group gnus-newsrc-hashtb))
5673          (info (nth 2 entry))
5674          (active (gnus-active group))
5675          range)
5676     (when entry
5677       (setq range (gnus-compute-read-articles group articles))
5678       (save-excursion
5679         (set-buffer gnus-group-buffer)
5680         (gnus-undo-register
5681           `(progn
5682              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5683              (gnus-info-set-read ',info ',(gnus-info-read info))
5684              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5685              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5686              (gnus-group-update-group ,group t))))
5687       ;; Add the read articles to the range.
5688       (gnus-info-set-read info range)
5689       (gnus-request-set-mark group (list (list range 'add '(read))))
5690       ;; Then we have to re-compute how many unread
5691       ;; articles there are in this group.
5692       (when active
5693         (cond
5694          ((not range)
5695           (setq num (- (1+ (cdr active)) (car active))))
5696          ((not (listp (cdr range)))
5697           (setq num (- (cdr active) (- (1+ (cdr range))
5698                                        (car range)))))
5699          (t
5700           (while range
5701             (if (numberp (car range))
5702                 (setq num (1+ num))
5703               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5704             (setq range (cdr range)))
5705           (setq num (- (cdr active) num))))
5706         ;; Update the number of unread articles.
5707         (setcar entry num)
5708         ;; Update the group buffer.
5709         (unless (gnus-ephemeral-group-p group)
5710           (gnus-group-update-group group t))))))
5711
5712 (defvar gnus-newsgroup-none-id 0)
5713
5714 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5715   (let ((cur nntp-server-buffer)
5716         (dependencies
5717          (or dependencies
5718              (save-excursion (set-buffer gnus-summary-buffer)
5719                              gnus-newsgroup-dependencies)))
5720         headers id end ref
5721         (mail-parse-charset gnus-newsgroup-charset)
5722         (mail-parse-ignored-charsets
5723          (save-excursion (condition-case nil
5724                              (set-buffer gnus-summary-buffer)
5725                            (error))
5726                          gnus-newsgroup-ignored-charsets)))
5727     (save-excursion
5728       (set-buffer nntp-server-buffer)
5729       ;; Translate all TAB characters into SPACE characters.
5730       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5731       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5732       (ietf-drums-unfold-fws)
5733       (gnus-run-hooks 'gnus-parse-headers-hook)
5734       (let ((case-fold-search t)
5735             in-reply-to header p lines chars)
5736         (goto-char (point-min))
5737         ;; Search to the beginning of the next header.  Error messages
5738         ;; do not begin with 2 or 3.
5739         (while (re-search-forward "^[23][0-9]+ " nil t)
5740           (setq id nil
5741                 ref nil)
5742           ;; This implementation of this function, with nine
5743           ;; search-forwards instead of the one re-search-forward and
5744           ;; a case (which basically was the old function) is actually
5745           ;; about twice as fast, even though it looks messier.  You
5746           ;; can't have everything, I guess.  Speed and elegance
5747           ;; doesn't always go hand in hand.
5748           (setq
5749            header
5750            (vector
5751             ;; Number.
5752             (prog1
5753                 (read cur)
5754               (end-of-line)
5755               (setq p (point))
5756               (narrow-to-region (point)
5757                                 (or (and (search-forward "\n.\n" nil t)
5758                                          (- (point) 2))
5759                                     (point))))
5760             ;; Subject.
5761             (progn
5762               (goto-char p)
5763               (if (search-forward "\nsubject:" nil t)
5764                   (funcall gnus-decode-encoded-word-function
5765                            (nnheader-header-value))
5766                 "(none)"))
5767             ;; From.
5768             (progn
5769               (goto-char p)
5770               (if (search-forward "\nfrom:" nil t)
5771                   (funcall gnus-decode-encoded-word-function
5772                            (nnheader-header-value))
5773                 "(nobody)"))
5774             ;; Date.
5775             (progn
5776               (goto-char p)
5777               (if (search-forward "\ndate:" nil t)
5778                   (nnheader-header-value) ""))
5779             ;; Message-ID.
5780             (progn
5781               (goto-char p)
5782               (setq id (if (re-search-forward
5783                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5784                            ;; We do it this way to make sure the Message-ID
5785                            ;; is (somewhat) syntactically valid.
5786                            (buffer-substring (match-beginning 1)
5787                                              (match-end 1))
5788                          ;; If there was no message-id, we just fake one
5789                          ;; to make subsequent routines simpler.
5790                          (nnheader-generate-fake-message-id))))
5791             ;; References.
5792             (progn
5793               (goto-char p)
5794               (if (search-forward "\nreferences:" nil t)
5795                   (progn
5796                     (setq end (point))
5797                     (prog1
5798                         (nnheader-header-value)
5799                       (setq ref
5800                             (buffer-substring
5801                              (progn
5802                                (end-of-line)
5803                                (search-backward ">" end t)
5804                                (1+ (point)))
5805                              (progn
5806                                (search-backward "<" end t)
5807                                (point))))))
5808                 ;; Get the references from the in-reply-to header if there
5809                 ;; were no references and the in-reply-to header looks
5810                 ;; promising.
5811                 (if (and (search-forward "\nin-reply-to:" nil t)
5812                          (setq in-reply-to (nnheader-header-value))
5813                          (string-match "<[^>]+>" in-reply-to))
5814                     (let (ref2)
5815                       (setq ref (substring in-reply-to (match-beginning 0)
5816                                            (match-end 0)))
5817                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5818                         (setq ref2 (substring in-reply-to (match-beginning 0)
5819                                               (match-end 0)))
5820                         (when (> (length ref2) (length ref))
5821                           (setq ref ref2)))
5822                       ref)
5823                   (setq ref nil))))
5824             ;; Chars.
5825             (progn
5826               (goto-char p)
5827               (if (search-forward "\nchars: " nil t)
5828                   (if (numberp (setq chars (ignore-errors (read cur))))
5829                       chars -1)
5830                 -1))
5831             ;; Lines.
5832             (progn
5833               (goto-char p)
5834               (if (search-forward "\nlines: " nil t)
5835                   (if (numberp (setq lines (ignore-errors (read cur))))
5836                       lines -1)
5837                 -1))
5838             ;; Xref.
5839             (progn
5840               (goto-char p)
5841               (and (search-forward "\nxref:" nil t)
5842                    (nnheader-header-value)))
5843             ;; Extra.
5844             (when gnus-extra-headers
5845               (let ((extra gnus-extra-headers)
5846                     out)
5847                 (while extra
5848                   (goto-char p)
5849                   (when (search-forward
5850                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5851                     (push (cons (car extra) (nnheader-header-value))
5852                           out))
5853                   (pop extra))
5854                 out))))
5855           (when (equal id ref)
5856             (setq ref nil))
5857
5858           (when gnus-alter-header-function
5859             (funcall gnus-alter-header-function header)
5860             (setq id (mail-header-id header)
5861                   ref (gnus-parent-id (mail-header-references header))))
5862
5863           (when (setq header
5864                       (gnus-dependencies-add-header
5865                        header dependencies force-new))
5866             (push header headers))
5867           (goto-char (point-max))
5868           (widen))
5869         (nreverse headers)))))
5870
5871 ;; Goes through the xover lines and returns a list of vectors
5872 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5873                                                   force-new dependencies
5874                                                   group also-fetch-heads)
5875   "Parse the news overview data in the server buffer.
5876 Return a list of headers that match SEQUENCE (see
5877 `nntp-retrieve-headers')."
5878   ;; Get the Xref when the users reads the articles since most/some
5879   ;; NNTP servers do not include Xrefs when using XOVER.
5880   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5881   (let ((mail-parse-charset gnus-newsgroup-charset)
5882         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5883         (cur nntp-server-buffer)
5884         (dependencies (or dependencies gnus-newsgroup-dependencies))
5885         (allp (cond
5886                ((eq gnus-read-all-available-headers t)
5887                 t)
5888                ((stringp gnus-read-all-available-headers)
5889                 (string-match gnus-read-all-available-headers group))
5890                (t
5891                 nil)))
5892         number headers header)
5893     (save-excursion
5894       (set-buffer nntp-server-buffer)
5895       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5896       ;; Allow the user to mangle the headers before parsing them.
5897       (gnus-run-hooks 'gnus-parse-headers-hook)
5898       (goto-char (point-min))
5899       (gnus-parse-without-error
5900         (while (and (or sequence allp)
5901                     (not (eobp)))
5902           (setq number (read cur))
5903           (when (not allp)
5904             (while (and sequence
5905                         (< (car sequence) number))
5906               (setq sequence (cdr sequence))))
5907           (when (and (or allp
5908                          (and sequence
5909                               (eq number (car sequence))))
5910                      (progn
5911                        (setq sequence (cdr sequence))
5912                        (setq header (inline
5913                                       (gnus-nov-parse-line
5914                                        number dependencies force-new)))))
5915             (push header headers))
5916           (forward-line 1)))
5917       ;; A common bug in inn is that if you have posted an article and
5918       ;; then retrieves the active file, it will answer correctly --
5919       ;; the new article is included.  However, a NOV entry for the
5920       ;; article may not have been generated yet, so this may fail.
5921       ;; We work around this problem by retrieving the last few
5922       ;; headers using HEAD.
5923       (if (or (not also-fetch-heads)
5924               (not sequence))
5925           ;; We (probably) got all the headers.
5926           (nreverse headers)
5927         (let ((gnus-nov-is-evil t))
5928           (nconc
5929            (nreverse headers)
5930            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5931              (gnus-get-newsgroup-headers))))))))
5932
5933 (defun gnus-article-get-xrefs ()
5934   "Fill in the Xref value in `gnus-current-headers', if necessary.
5935 This is meant to be called in `gnus-article-internal-prepare-hook'."
5936   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5937                                  gnus-current-headers)))
5938     (or (not gnus-use-cross-reference)
5939         (not headers)
5940         (and (mail-header-xref headers)
5941              (not (string= (mail-header-xref headers) "")))
5942         (let ((case-fold-search t)
5943               xref)
5944           (save-restriction
5945             (nnheader-narrow-to-headers)
5946             (goto-char (point-min))
5947             (when (or (and (not (eobp))
5948                            (eq (downcase (char-after)) ?x)
5949                            (looking-at "Xref:"))
5950                       (search-forward "\nXref:" nil t))
5951               (goto-char (1+ (match-end 0)))
5952               (setq xref (buffer-substring (point) (point-at-eol)))
5953               (mail-header-set-xref headers xref)))))))
5954
5955 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5956   "Find article ID and insert the summary line for that article.
5957 OLD-HEADER can either be a header or a line number to insert
5958 the subject line on."
5959   (let* ((line (and (numberp old-header) old-header))
5960          (old-header (and (vectorp old-header) old-header))
5961          (header (cond ((and old-header use-old-header)
5962                         old-header)
5963                        ((and (numberp id)
5964                              (gnus-number-to-header id))
5965                         (gnus-number-to-header id))
5966                        (t
5967                         (gnus-read-header id))))
5968          (number (and (numberp id) id))
5969          d)
5970     (when header
5971       ;; Rebuild the thread that this article is part of and go to the
5972       ;; article we have fetched.
5973       (when (and (not gnus-show-threads)
5974                  old-header)
5975         (when (and number
5976                    (setq d (gnus-data-find (mail-header-number old-header))))
5977           (goto-char (gnus-data-pos d))
5978           (gnus-data-remove
5979            number
5980            (- (point-at-bol)
5981               (prog1
5982                   (1+ (point-at-eol))
5983                 (gnus-delete-line))))))
5984       (when old-header
5985         (mail-header-set-number header (mail-header-number old-header)))
5986       (setq gnus-newsgroup-sparse
5987             (delq (setq number (mail-header-number header))
5988                   gnus-newsgroup-sparse))
5989       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5990       (push number gnus-newsgroup-limit)
5991       (gnus-rebuild-thread (mail-header-id header) line)
5992       (gnus-summary-goto-subject number nil t))
5993     (when (and (numberp number)
5994                (> number 0))
5995       ;; We have to update the boundaries even if we can't fetch the
5996       ;; article if ID is a number -- so that the next `P' or `N'
5997       ;; command will fetch the previous (or next) article even
5998       ;; if the one we tried to fetch this time has been canceled.
5999       (when (> number gnus-newsgroup-end)
6000         (setq gnus-newsgroup-end number))
6001       (when (< number gnus-newsgroup-begin)
6002         (setq gnus-newsgroup-begin number))
6003       (setq gnus-newsgroup-unselected
6004             (delq number gnus-newsgroup-unselected)))
6005     ;; Report back a success?
6006     (and header (mail-header-number header))))
6007
6008 ;;; Process/prefix in the summary buffer
6009
6010 (defun gnus-summary-work-articles (n)
6011   "Return a list of articles to be worked upon.
6012 The prefix argument, the list of process marked articles, and the
6013 current article will be taken into consideration."
6014   (save-excursion
6015     (set-buffer gnus-summary-buffer)
6016     (cond
6017      (n
6018       ;; A numerical prefix has been given.
6019       (setq n (prefix-numeric-value n))
6020       (let ((backward (< n 0))
6021             (n (abs (prefix-numeric-value n)))
6022             articles article)
6023         (save-excursion
6024           (while
6025               (and (> n 0)
6026                    (push (setq article (gnus-summary-article-number))
6027                          articles)
6028                    (if backward
6029                        (gnus-summary-find-prev nil article)
6030                      (gnus-summary-find-next nil article)))
6031             (decf n)))
6032         (nreverse articles)))
6033      ((and (gnus-region-active-p) (mark))
6034       (message "region active")
6035       ;; Work on the region between point and mark.
6036       (let ((max (max (point) (mark)))
6037             articles article)
6038         (save-excursion
6039           (goto-char (min (min (point) (mark))))
6040           (while
6041               (and
6042                (push (setq article (gnus-summary-article-number)) articles)
6043                (gnus-summary-find-next nil article)
6044                (< (point) max)))
6045           (nreverse articles))))
6046      (gnus-newsgroup-processable
6047       ;; There are process-marked articles present.
6048       ;; Save current state.
6049       (gnus-summary-save-process-mark)
6050       ;; Return the list.
6051       (reverse gnus-newsgroup-processable))
6052      (t
6053       ;; Just return the current article.
6054       (list (gnus-summary-article-number))))))
6055
6056 (defmacro gnus-summary-iterate (arg &rest forms)
6057   "Iterate over the process/prefixed articles and do FORMS.
6058 ARG is the interactive prefix given to the command.  FORMS will be
6059 executed with point over the summary line of the articles."
6060   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6061     `(let ((,articles (gnus-summary-work-articles ,arg)))
6062        (while ,articles
6063          (gnus-summary-goto-subject (car ,articles))
6064          ,@forms
6065          (pop ,articles)))))
6066
6067 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6068 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6069
6070 (defun gnus-summary-save-process-mark ()
6071   "Push the current set of process marked articles on the stack."
6072   (interactive)
6073   (push (copy-sequence gnus-newsgroup-processable)
6074         gnus-newsgroup-process-stack))
6075
6076 (defun gnus-summary-kill-process-mark ()
6077   "Push the current set of process marked articles on the stack and unmark."
6078   (interactive)
6079   (gnus-summary-save-process-mark)
6080   (gnus-summary-unmark-all-processable))
6081
6082 (defun gnus-summary-yank-process-mark ()
6083   "Pop the last process mark state off the stack and restore it."
6084   (interactive)
6085   (unless gnus-newsgroup-process-stack
6086     (error "Empty mark stack"))
6087   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6088
6089 (defun gnus-summary-process-mark-set (set)
6090   "Make SET into the current process marked articles."
6091   (gnus-summary-unmark-all-processable)
6092   (while set
6093     (gnus-summary-set-process-mark (pop set))))
6094
6095 ;;; Searching and stuff
6096
6097 (defun gnus-summary-search-group (&optional backward use-level)
6098   "Search for next unread newsgroup.
6099 If optional argument BACKWARD is non-nil, search backward instead."
6100   (save-excursion
6101     (set-buffer gnus-group-buffer)
6102     (when (gnus-group-search-forward
6103            backward nil (if use-level (gnus-group-group-level) nil))
6104       (gnus-group-group-name))))
6105
6106 (defun gnus-summary-best-group (&optional exclude-group)
6107   "Find the name of the best unread group.
6108 If EXCLUDE-GROUP, do not go to this group."
6109   (save-excursion
6110     (set-buffer gnus-group-buffer)
6111     (save-excursion
6112       (gnus-group-best-unread-group exclude-group))))
6113
6114 (defun gnus-summary-find-next (&optional unread article backward)
6115   (if backward
6116       (gnus-summary-find-prev unread article)
6117     (let* ((dummy (gnus-summary-article-intangible-p))
6118            (article (or article (gnus-summary-article-number)))
6119            (data (gnus-data-find-list article))
6120            result)
6121       (when (and (not dummy)
6122                  (or (not gnus-summary-check-current)
6123                      (not unread)
6124                      (not (gnus-data-unread-p (car data)))))
6125         (setq data (cdr data)))
6126       (when (setq result
6127                   (if unread
6128                       (progn
6129                         (while data
6130                           (unless (memq (gnus-data-number (car data))
6131                                         (cond
6132                                          ((eq gnus-auto-goto-ignores
6133                                               'always-undownloaded)
6134                                           gnus-newsgroup-undownloaded)
6135                                          (gnus-plugged
6136                                           nil)
6137                                          ((eq gnus-auto-goto-ignores
6138                                               'unfetched)
6139                                           gnus-newsgroup-unfetched)
6140                                          ((eq gnus-auto-goto-ignores
6141                                               'undownloaded)
6142                                           gnus-newsgroup-undownloaded)))
6143                             (when (gnus-data-unread-p (car data))
6144                               (setq result (car data)
6145                                     data nil)))
6146                           (setq data (cdr data)))
6147                         result)
6148                     (car data)))
6149         (goto-char (gnus-data-pos result))
6150         (gnus-data-number result)))))
6151
6152 (defun gnus-summary-find-prev (&optional unread article)
6153   (let* ((eobp (eobp))
6154          (article (or article (gnus-summary-article-number)))
6155          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6156          result)
6157     (when (and (not eobp)
6158                (or (not gnus-summary-check-current)
6159                    (not unread)
6160                    (not (gnus-data-unread-p (car data)))))
6161       (setq data (cdr data)))
6162     (when (setq result
6163                 (if unread
6164                     (progn
6165                       (while data
6166                         (unless (memq (gnus-data-number (car data))
6167                                       (cond
6168                                        ((eq gnus-auto-goto-ignores
6169                                             'always-undownloaded)
6170                                         gnus-newsgroup-undownloaded)
6171                                        (gnus-plugged
6172                                         nil)
6173                                        ((eq gnus-auto-goto-ignores
6174                                             'unfetched)
6175                                         gnus-newsgroup-unfetched)
6176                                        ((eq gnus-auto-goto-ignores
6177                                             'undownloaded)
6178                                         gnus-newsgroup-undownloaded)))
6179                           (when (gnus-data-unread-p (car data))
6180                             (setq result (car data)
6181                                   data nil)))
6182                         (setq data (cdr data)))
6183                       result)
6184                   (car data)))
6185       (goto-char (gnus-data-pos result))
6186       (gnus-data-number result))))
6187
6188 (defun gnus-summary-find-subject (subject &optional unread backward article)
6189   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6190          (article (or article (gnus-summary-article-number)))
6191          (articles (gnus-data-list backward))
6192          (arts (gnus-data-find-list article articles))
6193          result)
6194     (when (or (not gnus-summary-check-current)
6195               (not unread)
6196               (not (gnus-data-unread-p (car arts))))
6197       (setq arts (cdr arts)))
6198     (while arts
6199       (and (or (not unread)
6200                (gnus-data-unread-p (car arts)))
6201            (vectorp (gnus-data-header (car arts)))
6202            (gnus-subject-equal
6203             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6204            (setq result (car arts)
6205                  arts nil))
6206       (setq arts (cdr arts)))
6207     (and result
6208          (goto-char (gnus-data-pos result))
6209          (gnus-data-number result))))
6210
6211 (defun gnus-summary-search-forward (&optional unread subject backward)
6212   "Search forward for an article.
6213 If UNREAD, look for unread articles.  If SUBJECT, look for
6214 articles with that subject.  If BACKWARD, search backward instead."
6215   (cond (subject (gnus-summary-find-subject subject unread backward))
6216         (backward (gnus-summary-find-prev unread))
6217         (t (gnus-summary-find-next unread))))
6218
6219 (defun gnus-recenter (&optional n)
6220   "Center point in window and redisplay frame.
6221 Also do horizontal recentering."
6222   (interactive "P")
6223   (when (and gnus-auto-center-summary
6224              (not (eq gnus-auto-center-summary 'vertical)))
6225     (gnus-horizontal-recenter))
6226   (recenter n))
6227
6228 (defun gnus-summary-recenter ()
6229   "Center point in the summary window.
6230 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6231 displayed, no centering will be performed."
6232   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6233   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6234   (interactive)
6235   ;; The user has to want it.
6236   (when gnus-auto-center-summary
6237     (let* ((top (cond ((< (window-height) 4) 0)
6238                       ((< (window-height) 7) 1)
6239                       (t (if (numberp gnus-auto-center-summary)
6240                              gnus-auto-center-summary
6241                            2))))
6242            (height (1- (window-height)))
6243            (bottom (save-excursion (goto-char (point-max))
6244                                    (forward-line (- height))
6245                                    (point)))
6246            (window (get-buffer-window (current-buffer))))
6247       (when (get-buffer-window gnus-article-buffer)
6248         ;; Only do recentering when the article buffer is displayed,
6249         ;; Set the window start to either `bottom', which is the biggest
6250         ;; possible valid number, or the second line from the top,
6251         ;; whichever is the least.
6252         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6253           (if (> bottom top-pos)
6254               ;; Keep the second line from the top visible
6255               (set-window-start window top-pos t)
6256             ;; Try to keep the bottom line visible; if it's partially
6257             ;; obscured, either scroll one more line to make it fully
6258             ;; visible, or revert to using TOP-POS.
6259             (save-excursion
6260               (goto-char (point-max))
6261               (forward-line -1)
6262               (let ((last-line-start (point)))
6263                 (goto-char bottom)
6264                 (set-window-start window (point) t)
6265                 (when (not (pos-visible-in-window-p last-line-start window))
6266                   (forward-line 1)
6267                   (set-window-start window (min (point) top-pos) t)))))))
6268       ;; Do horizontal recentering while we're at it.
6269       (when (and (get-buffer-window (current-buffer) t)
6270                  (not (eq gnus-auto-center-summary 'vertical)))
6271         (let ((selected (selected-window)))
6272           (select-window (get-buffer-window (current-buffer) t))
6273           (gnus-summary-position-point)
6274           (gnus-horizontal-recenter)
6275           (select-window selected))))))
6276
6277 (defun gnus-summary-jump-to-group (newsgroup)
6278   "Move point to NEWSGROUP in group mode buffer."
6279   ;; Keep update point of group mode buffer if visible.
6280   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6281       (save-window-excursion
6282         ;; Take care of tree window mode.
6283         (when (get-buffer-window gnus-group-buffer)
6284           (pop-to-buffer gnus-group-buffer))
6285         (gnus-group-jump-to-group newsgroup))
6286     (save-excursion
6287       ;; Take care of tree window mode.
6288       (if (get-buffer-window gnus-group-buffer)
6289           (pop-to-buffer gnus-group-buffer)
6290         (set-buffer gnus-group-buffer))
6291       (gnus-group-jump-to-group newsgroup))))
6292
6293 ;; This function returns a list of article numbers based on the
6294 ;; difference between the ranges of read articles in this group and
6295 ;; the range of active articles.
6296 (defun gnus-list-of-unread-articles (group)
6297   (let* ((read (gnus-info-read (gnus-get-info group)))
6298          (active (or (gnus-active group) (gnus-activate-group group)))
6299          (last (cdr active))
6300          first nlast unread)
6301     ;; If none are read, then all are unread.
6302     (if (not read)
6303         (setq first (car active))
6304       ;; If the range of read articles is a single range, then the
6305       ;; first unread article is the article after the last read
6306       ;; article.  Sounds logical, doesn't it?
6307       (if (and (not (listp (cdr read)))
6308                (or (< (car read) (car active))
6309                    (progn (setq read (list read))
6310                           nil)))
6311           (setq first (max (car active) (1+ (cdr read))))
6312         ;; `read' is a list of ranges.
6313         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6314                                   (caar read)))
6315                   1)
6316           (setq first (car active)))
6317         (while read
6318           (when first
6319             (while (< first nlast)
6320               (push first unread)
6321               (setq first (1+ first))))
6322           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6323           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6324           (setq read (cdr read)))))
6325     ;; And add the last unread articles.
6326     (while (<= first last)
6327       (push first unread)
6328       (setq first (1+ first)))
6329     ;; Return the list of unread articles.
6330     (delq 0 (nreverse unread))))
6331
6332 (defun gnus-list-of-read-articles (group)
6333   "Return a list of unread, unticked and non-dormant articles."
6334   (let* ((info (gnus-get-info group))
6335          (marked (gnus-info-marks info))
6336          (active (gnus-active group)))
6337     (and info active
6338          (gnus-list-range-difference
6339           (gnus-list-range-difference
6340            (gnus-sorted-complement
6341             (gnus-uncompress-range active)
6342             (gnus-list-of-unread-articles group))
6343            (cdr (assq 'dormant marked)))
6344           (cdr (assq 'tick marked))))))
6345
6346 ;; Various summary commands
6347
6348 (defun gnus-summary-select-article-buffer ()
6349   "Reconfigure windows to show article buffer."
6350   (interactive)
6351   (if (not (gnus-buffer-live-p gnus-article-buffer))
6352       (error "There is no article buffer for this summary buffer")
6353     (gnus-configure-windows 'article)
6354     (select-window (get-buffer-window gnus-article-buffer))))
6355
6356 (defun gnus-summary-universal-argument (arg)
6357   "Perform any operation on all articles that are process/prefixed."
6358   (interactive "P")
6359   (let ((articles (gnus-summary-work-articles arg))
6360         func article)
6361     (if (eq
6362          (setq
6363           func
6364           (key-binding
6365            (read-key-sequence
6366             (substitute-command-keys
6367              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6368          'undefined)
6369         (gnus-error 1 "Undefined key")
6370       (save-excursion
6371         (while articles
6372           (gnus-summary-goto-subject (setq article (pop articles)))
6373           (let (gnus-newsgroup-processable)
6374             (command-execute func))
6375           (gnus-summary-remove-process-mark article)))))
6376   (gnus-summary-position-point))
6377
6378 (defun gnus-summary-toggle-truncation (&optional arg)
6379   "Toggle truncation of summary lines.
6380 With ARG, turn line truncation on if ARG is positive."
6381   (interactive "P")
6382   (setq truncate-lines
6383         (if (null arg) (not truncate-lines)
6384           (> (prefix-numeric-value arg) 0)))
6385   (redraw-display))
6386
6387 (defun gnus-summary-find-for-reselect ()
6388   "Return the number of an article to stay on across a reselect.
6389 The current article is considered, then following articles, then previous
6390 articles.  An article is sought which is not cancelled and isn't a temporary
6391 insertion from another group.  If there's no such then return a dummy 0."
6392   (let (found)
6393     (dolist (rev '(nil t))
6394       (unless found      ; don't demand the reverse list if we don't need it
6395         (let ((data (gnus-data-find-list
6396                      (gnus-summary-article-number) (gnus-data-list rev))))
6397           (while (and data (not found))
6398             (if (and (< 0 (gnus-data-number (car data)))
6399                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6400                 (setq found (gnus-data-number (car data))))
6401             (setq data (cdr data))))))
6402     (or found 0)))
6403
6404 (defun gnus-summary-reselect-current-group (&optional all rescan)
6405   "Exit and then reselect the current newsgroup.
6406 The prefix argument ALL means to select all articles."
6407   (interactive "P")
6408   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6409     (error "Ephemeral groups can't be reselected"))
6410   (let ((current-subject (gnus-summary-find-for-reselect))
6411         (group gnus-newsgroup-name))
6412     (setq gnus-newsgroup-begin nil)
6413     (gnus-summary-exit nil 'leave-hidden)
6414     ;; We have to adjust the point of group mode buffer because
6415     ;; point was moved to the next unread newsgroup by exiting.
6416     (gnus-summary-jump-to-group group)
6417     (when rescan
6418       (save-excursion
6419         (gnus-group-get-new-news-this-group 1)))
6420     (gnus-group-read-group all t)
6421     (gnus-summary-goto-subject current-subject nil t)))
6422
6423 (defun gnus-summary-rescan-group (&optional all)
6424   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6425   (interactive "P")
6426   (gnus-summary-reselect-current-group all t))
6427
6428 (defun gnus-summary-update-info (&optional non-destructive)
6429   (save-excursion
6430     (let ((group gnus-newsgroup-name))
6431       (when group
6432         (when gnus-newsgroup-kill-headers
6433           (setq gnus-newsgroup-killed
6434                 (gnus-compress-sequence
6435                  (gnus-sorted-union
6436                   (gnus-list-range-intersection
6437                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6438                   gnus-newsgroup-unreads)
6439                  t)))
6440         (unless (listp (cdr gnus-newsgroup-killed))
6441           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6442         (let ((headers gnus-newsgroup-headers))
6443           ;; Set the new ranges of read articles.
6444           (save-excursion
6445             (set-buffer gnus-group-buffer)
6446             (gnus-undo-force-boundary))
6447           (gnus-update-read-articles
6448            group (gnus-sorted-union
6449                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6450           ;; Set the current article marks.
6451           (let ((gnus-newsgroup-scored
6452                  (if (and (not gnus-save-score)
6453                           (not non-destructive))
6454                      nil
6455                    gnus-newsgroup-scored)))
6456             (save-excursion
6457               (gnus-update-marks)))
6458           ;; Do the cross-ref thing.
6459           (when gnus-use-cross-reference
6460             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6461           ;; Do not switch windows but change the buffer to work.
6462           (set-buffer gnus-group-buffer)
6463           (unless (gnus-ephemeral-group-p group)
6464             (gnus-group-update-group group)))))))
6465
6466 (defun gnus-summary-save-newsrc (&optional force)
6467   "Save the current number of read/marked articles in the dribble buffer.
6468 The dribble buffer will then be saved.
6469 If FORCE (the prefix), also save the .newsrc file(s)."
6470   (interactive "P")
6471   (gnus-summary-update-info t)
6472   (if force
6473       (gnus-save-newsrc-file)
6474     (gnus-dribble-save)))
6475
6476 (defun gnus-summary-exit (&optional temporary leave-hidden)
6477   "Exit reading current newsgroup, and then return to group selection mode.
6478 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6479   (interactive)
6480   (gnus-set-global-variables)
6481   (when (gnus-buffer-live-p gnus-article-buffer)
6482     (save-excursion
6483       (set-buffer gnus-article-buffer)
6484       (mm-destroy-parts gnus-article-mime-handles)
6485       ;; Set it to nil for safety reason.
6486       (setq gnus-article-mime-handle-alist nil)
6487       (setq gnus-article-mime-handles nil)))
6488   (gnus-kill-save-kill-buffer)
6489   (gnus-async-halt-prefetch)
6490   (let* ((group gnus-newsgroup-name)
6491          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6492          (gnus-group-is-exiting-p t)
6493          (mode major-mode)
6494          (group-point nil)
6495          (buf (current-buffer)))
6496     (unless quit-config
6497       ;; Do adaptive scoring, and possibly save score files.
6498       (when gnus-newsgroup-adaptive
6499         (gnus-score-adaptive))
6500       (when gnus-use-scoring
6501         (gnus-score-save)))
6502     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6503     ;; If we have several article buffers, we kill them at exit.
6504     (unless gnus-single-article-buffer
6505       (gnus-kill-buffer gnus-original-article-buffer)
6506       (setq gnus-article-current nil))
6507     (when gnus-use-cache
6508       (gnus-cache-possibly-remove-articles)
6509       (gnus-cache-save-buffers))
6510     (gnus-async-prefetch-remove-group group)
6511     (when gnus-suppress-duplicates
6512       (gnus-dup-enter-articles))
6513     (when gnus-use-trees
6514       (gnus-tree-close group))
6515     (when gnus-use-cache
6516       (gnus-cache-write-active))
6517     ;; Remove entries for this group.
6518     (nnmail-purge-split-history (gnus-group-real-name group))
6519     ;; Make all changes in this group permanent.
6520     (unless quit-config
6521       (gnus-run-hooks 'gnus-exit-group-hook)
6522       (gnus-summary-update-info))
6523     (gnus-close-group group)
6524     ;; Make sure where we were, and go to next newsgroup.
6525     (set-buffer gnus-group-buffer)
6526     (unless quit-config
6527       (gnus-group-jump-to-group group))
6528     (gnus-run-hooks 'gnus-summary-exit-hook)
6529     (unless (or quit-config
6530                 ;; If this group has disappeared from the summary
6531                 ;; buffer, don't skip forwards.
6532                 (not (string= group (gnus-group-group-name))))
6533       (gnus-group-next-unread-group 1))
6534     (setq group-point (point))
6535     (if temporary
6536         nil                             ;Nothing to do.
6537       ;; If we have several article buffers, we kill them at exit.
6538       (unless gnus-single-article-buffer
6539         (gnus-kill-buffer gnus-article-buffer)
6540         (gnus-kill-buffer gnus-original-article-buffer)
6541         (setq gnus-article-current nil))
6542       (set-buffer buf)
6543       (if (not gnus-kill-summary-on-exit)
6544           (progn
6545             (gnus-deaden-summary)
6546             (setq mode nil))
6547         ;; We set all buffer-local variables to nil.  It is unclear why
6548         ;; this is needed, but if we don't, buffer-local variables are
6549         ;; not garbage-collected, it seems.  This would the lead to en
6550         ;; ever-growing Emacs.
6551         (gnus-summary-clear-local-variables)
6552         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6553           (gnus-summary-clear-local-variables))
6554         (when (get-buffer gnus-article-buffer)
6555           (bury-buffer gnus-article-buffer))
6556         ;; We clear the global counterparts of the buffer-local
6557         ;; variables as well, just to be on the safe side.
6558         (set-buffer gnus-group-buffer)
6559         (gnus-summary-clear-local-variables)
6560         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6561           (gnus-summary-clear-local-variables))
6562         ;; Return to group mode buffer.
6563         (when (eq mode 'gnus-summary-mode)
6564           (gnus-kill-buffer buf)))
6565       (setq gnus-current-select-method gnus-select-method)
6566       (if leave-hidden
6567           (set-buffer gnus-group-buffer)
6568         (pop-to-buffer gnus-group-buffer))
6569       (if (not quit-config)
6570           (progn
6571             (goto-char group-point)
6572             (unless leave-hidden
6573               (gnus-configure-windows 'group 'force)))
6574         (gnus-handle-ephemeral-exit quit-config))
6575       ;; Clear the current group name.
6576       (unless quit-config
6577         (setq gnus-newsgroup-name nil)))))
6578
6579 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6580 (defun gnus-summary-exit-no-update (&optional no-questions)
6581   "Quit reading current newsgroup without updating read article info."
6582   (interactive)
6583   (let* ((group gnus-newsgroup-name)
6584          (gnus-group-is-exiting-p t)
6585          (gnus-group-is-exiting-without-update-p t)
6586          (quit-config (gnus-group-quit-config group)))
6587     (when (or no-questions
6588               gnus-expert-user
6589               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6590       (gnus-async-halt-prefetch)
6591       (run-hooks 'gnus-summary-prepare-exit-hook)
6592       (when (gnus-buffer-live-p gnus-article-buffer)
6593         (save-excursion
6594           (set-buffer gnus-article-buffer)
6595           (mm-destroy-parts gnus-article-mime-handles)
6596           ;; Set it to nil for safety reason.
6597           (setq gnus-article-mime-handle-alist nil)
6598           (setq gnus-article-mime-handles nil)))
6599       ;; If we have several article buffers, we kill them at exit.
6600       (unless gnus-single-article-buffer
6601         (gnus-kill-buffer gnus-article-buffer)
6602         (gnus-kill-buffer gnus-original-article-buffer)
6603         (setq gnus-article-current nil))
6604       (if (not gnus-kill-summary-on-exit)
6605           (gnus-deaden-summary)
6606         (gnus-close-group group)
6607         (gnus-summary-clear-local-variables)
6608         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6609           (gnus-summary-clear-local-variables))
6610         (set-buffer gnus-group-buffer)
6611         (gnus-summary-clear-local-variables)
6612         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6613           (gnus-summary-clear-local-variables))
6614         (gnus-kill-buffer gnus-summary-buffer))
6615       (unless gnus-single-article-buffer
6616         (setq gnus-article-current nil))
6617       (when gnus-use-trees
6618         (gnus-tree-close group))
6619       (gnus-async-prefetch-remove-group group)
6620       (when (get-buffer gnus-article-buffer)
6621         (bury-buffer gnus-article-buffer))
6622       ;; Return to the group buffer.
6623       (gnus-configure-windows 'group 'force)
6624       ;; Clear the current group name.
6625       (setq gnus-newsgroup-name nil)
6626       (unless (gnus-ephemeral-group-p group)
6627         (gnus-group-update-group group))
6628       (when (equal (gnus-group-group-name) group)
6629         (gnus-group-next-unread-group 1))
6630       (when quit-config
6631         (gnus-handle-ephemeral-exit quit-config)))))
6632
6633 (defun gnus-handle-ephemeral-exit (quit-config)
6634   "Handle movement when leaving an ephemeral group.
6635 The state which existed when entering the ephemeral is reset."
6636   (if (not (buffer-name (car quit-config)))
6637       (gnus-configure-windows 'group 'force)
6638     (set-buffer (car quit-config))
6639     (cond ((eq major-mode 'gnus-summary-mode)
6640            (gnus-set-global-variables))
6641           ((eq major-mode 'gnus-article-mode)
6642            (save-excursion
6643              ;; The `gnus-summary-buffer' variable may point
6644              ;; to the old summary buffer when using a single
6645              ;; article buffer.
6646              (unless (gnus-buffer-live-p gnus-summary-buffer)
6647                (set-buffer gnus-group-buffer))
6648              (set-buffer gnus-summary-buffer)
6649              (gnus-set-global-variables))))
6650     (if (or (eq (cdr quit-config) 'article)
6651             (eq (cdr quit-config) 'pick))
6652         (progn
6653           ;; The current article may be from the ephemeral group
6654           ;; thus it is best that we reload this article
6655           ;;
6656           ;; If we're exiting from a large digest, this can be
6657           ;; extremely slow.  So, it's better not to reload it. -- jh.
6658           ;;(gnus-summary-show-article)
6659           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6660               (gnus-configure-windows 'pick 'force)
6661             (gnus-configure-windows (cdr quit-config) 'force)))
6662       (gnus-configure-windows (cdr quit-config) 'force))
6663     (when (eq major-mode 'gnus-summary-mode)
6664       (gnus-summary-next-subject 1 nil t)
6665       (gnus-summary-recenter)
6666       (gnus-summary-position-point))))
6667
6668 ;;; Dead summaries.
6669
6670 (defvar gnus-dead-summary-mode-map nil)
6671
6672 (unless gnus-dead-summary-mode-map
6673   (setq gnus-dead-summary-mode-map (make-keymap))
6674   (suppress-keymap gnus-dead-summary-mode-map)
6675   (substitute-key-definition
6676    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6677   (dolist (key '("\C-d" "\r" "\177" [delete]))
6678     (define-key gnus-dead-summary-mode-map
6679       key 'gnus-summary-wake-up-the-dead))
6680   (dolist (key '("q" "Q"))
6681     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6682
6683 (defvar gnus-dead-summary-mode nil
6684   "Minor mode for Gnus summary buffers.")
6685
6686 (defun gnus-dead-summary-mode (&optional arg)
6687   "Minor mode for Gnus summary buffers."
6688   (interactive "P")
6689   (when (eq major-mode 'gnus-summary-mode)
6690     (make-local-variable 'gnus-dead-summary-mode)
6691     (setq gnus-dead-summary-mode
6692           (if (null arg) (not gnus-dead-summary-mode)
6693             (> (prefix-numeric-value arg) 0)))
6694     (when gnus-dead-summary-mode
6695       (add-minor-mode
6696        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6697
6698 (defun gnus-deaden-summary ()
6699   "Make the current summary buffer into a dead summary buffer."
6700   ;; Kill any previous dead summary buffer.
6701   (when (and gnus-dead-summary
6702              (buffer-name gnus-dead-summary))
6703     (save-excursion
6704       (set-buffer gnus-dead-summary)
6705       (when gnus-dead-summary-mode
6706         (kill-buffer (current-buffer)))))
6707   ;; Make this the current dead summary.
6708   (setq gnus-dead-summary (current-buffer))
6709   (gnus-dead-summary-mode 1)
6710   (let ((name (buffer-name)))
6711     (when (string-match "Summary" name)
6712       (rename-buffer
6713        (concat (substring name 0 (match-beginning 0)) "Dead "
6714                (substring name (match-beginning 0)))
6715        t)
6716       (bury-buffer))))
6717
6718 (defun gnus-kill-or-deaden-summary (buffer)
6719   "Kill or deaden the summary BUFFER."
6720   (save-excursion
6721     (when (and (buffer-name buffer)
6722                (not gnus-single-article-buffer))
6723       (save-excursion
6724         (set-buffer buffer)
6725         (gnus-kill-buffer gnus-article-buffer)
6726         (gnus-kill-buffer gnus-original-article-buffer)))
6727     (cond
6728      ;; Kill the buffer.
6729      (gnus-kill-summary-on-exit
6730       (when (and gnus-use-trees
6731                  (gnus-buffer-exists-p buffer))
6732         (save-excursion
6733           (set-buffer buffer)
6734           (gnus-tree-close gnus-newsgroup-name)))
6735       (gnus-kill-buffer buffer))
6736      ;; Deaden the buffer.
6737      ((gnus-buffer-exists-p buffer)
6738       (save-excursion
6739         (set-buffer buffer)
6740         (gnus-deaden-summary))))))
6741
6742 (defun gnus-summary-wake-up-the-dead (&rest args)
6743   "Wake up the dead summary buffer."
6744   (interactive)
6745   (gnus-dead-summary-mode -1)
6746   (let ((name (buffer-name)))
6747     (when (string-match "Dead " name)
6748       (rename-buffer
6749        (concat (substring name 0 (match-beginning 0))
6750                (substring name (match-end 0)))
6751        t)))
6752   (gnus-message 3 "This dead summary is now alive again"))
6753
6754 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6755 (defun gnus-summary-fetch-faq (&optional faq-dir)
6756   "Fetch the FAQ for the current group.
6757 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6758 in."
6759   (interactive
6760    (list
6761     (when current-prefix-arg
6762       (completing-read
6763        "FAQ dir: " (and (listp gnus-group-faq-directory)
6764                         (mapcar (lambda (file) (list file))
6765                                 gnus-group-faq-directory))))))
6766   (let (gnus-faq-buffer)
6767     (when (setq gnus-faq-buffer
6768                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6769       (gnus-configure-windows 'summary-faq))))
6770
6771 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6772 (defun gnus-summary-describe-group (&optional force)
6773   "Describe the current newsgroup."
6774   (interactive "P")
6775   (gnus-group-describe-group force gnus-newsgroup-name))
6776
6777 (defun gnus-summary-describe-briefly ()
6778   "Describe summary mode commands briefly."
6779   (interactive)
6780   (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")))
6781
6782 ;; Walking around group mode buffer from summary mode.
6783
6784 (defun gnus-summary-next-group (&optional no-article target-group backward)
6785   "Exit current newsgroup and then select next unread newsgroup.
6786 If prefix argument NO-ARTICLE is non-nil, no article is selected
6787 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6788 previous group instead."
6789   (interactive "P")
6790   ;; Stop pre-fetching.
6791   (gnus-async-halt-prefetch)
6792   (let ((current-group gnus-newsgroup-name)
6793         (current-buffer (current-buffer))
6794         entered)
6795     ;; First we semi-exit this group to update Xrefs and all variables.
6796     ;; We can't do a real exit, because the window conf must remain
6797     ;; the same in case the user is prompted for info, and we don't
6798     ;; want the window conf to change before that...
6799     (gnus-summary-exit t)
6800     (while (not entered)
6801       ;; Then we find what group we are supposed to enter.
6802       (set-buffer gnus-group-buffer)
6803       (gnus-group-jump-to-group current-group)
6804       (setq target-group
6805             (or target-group
6806                 (if (eq gnus-keep-same-level 'best)
6807                     (gnus-summary-best-group gnus-newsgroup-name)
6808                   (gnus-summary-search-group backward gnus-keep-same-level))))
6809       (if (not target-group)
6810           ;; There are no further groups, so we return to the group
6811           ;; buffer.
6812           (progn
6813             (gnus-message 5 "Returning to the group buffer")
6814             (setq entered t)
6815             (when (gnus-buffer-live-p current-buffer)
6816               (set-buffer current-buffer)
6817               (gnus-summary-exit))
6818             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6819         ;; We try to enter the target group.
6820         (gnus-group-jump-to-group target-group)
6821         (let ((unreads (gnus-group-group-unread)))
6822           (if (and (or (eq t unreads)
6823                        (and unreads (not (zerop unreads))))
6824                    (gnus-summary-read-group
6825                     target-group nil no-article
6826                     (and (buffer-name current-buffer) current-buffer)
6827                     nil backward))
6828               (setq entered t)
6829             (setq current-group target-group
6830                   target-group nil)))))))
6831
6832 (defun gnus-summary-prev-group (&optional no-article)
6833   "Exit current newsgroup and then select previous unread newsgroup.
6834 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6835   (interactive "P")
6836   (gnus-summary-next-group no-article nil t))
6837
6838 ;; Walking around summary lines.
6839
6840 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6841   "Go to the first subject satisfying any non-nil constraint.
6842 If UNREAD is non-nil, the article should be unread.
6843 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6844 If UNSEEN is non-nil, the article should be unseen.
6845 Returns the article selected or nil if there are no matching articles."
6846   (interactive "P")
6847   (cond
6848    ;; Empty summary.
6849    ((null gnus-newsgroup-data)
6850     (gnus-message 3 "No articles in the group")
6851     nil)
6852    ;; Pick the first article.
6853    ((not (or unread undownloaded unseen))
6854     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6855     (gnus-data-number (car gnus-newsgroup-data)))
6856    ;; Find the first unread article.
6857    (t
6858     (let ((data gnus-newsgroup-data))
6859       (while (and data
6860                   (let ((num (gnus-data-number (car data))))
6861                     (or (memq num gnus-newsgroup-unfetched)
6862                         (not (or (and unread
6863                                       (memq num gnus-newsgroup-unreads))
6864                                  (and undownloaded
6865                                       (memq num gnus-newsgroup-undownloaded))
6866                                  (and unseen
6867                                       (memq num gnus-newsgroup-unseen)))))))
6868         (setq data (cdr data)))
6869       (prog1
6870           (if data
6871               (progn
6872                 (goto-char (gnus-data-pos (car data)))
6873                 (gnus-data-number (car data)))
6874             (gnus-message 3 "No more%s articles"
6875                           (let* ((r (when unread " unread"))
6876                                  (d (when undownloaded " undownloaded"))
6877                                  (s (when unseen " unseen"))
6878                                  (l (delq nil (list r d s))))
6879                             (cond ((= 3 (length l))
6880                                    (concat r "," d ", or" s))
6881                                   ((= 2 (length l))
6882                                    (concat (car l) ", or" (cadr l)))
6883                                   ((= 1 (length l))
6884                                    (car l))
6885                                   (t
6886                                    ""))))
6887             nil
6888             )
6889         (gnus-summary-position-point))))))
6890
6891 (defun gnus-summary-next-subject (n &optional unread dont-display)
6892   "Go to next N'th summary line.
6893 If N is negative, go to the previous N'th subject line.
6894 If UNREAD is non-nil, only unread articles are selected.
6895 The difference between N and the actual number of steps taken is
6896 returned."
6897   (interactive "p")
6898   (let ((backward (< n 0))
6899         (n (abs n)))
6900     (while (and (> n 0)
6901                 (if backward
6902                     (gnus-summary-find-prev unread)
6903                   (gnus-summary-find-next unread)))
6904       (unless (zerop (setq n (1- n)))
6905         (gnus-summary-show-thread)))
6906     (when (/= 0 n)
6907       (gnus-message 7 "No more%s articles"
6908                     (if unread " unread" "")))
6909     (unless dont-display
6910       (gnus-summary-recenter)
6911       (gnus-summary-position-point))
6912     n))
6913
6914 (defun gnus-summary-next-unread-subject (n)
6915   "Go to next N'th unread summary line."
6916   (interactive "p")
6917   (gnus-summary-next-subject n t))
6918
6919 (defun gnus-summary-prev-subject (n &optional unread)
6920   "Go to previous N'th summary line.
6921 If optional argument UNREAD is non-nil, only unread article is selected."
6922   (interactive "p")
6923   (gnus-summary-next-subject (- n) unread))
6924
6925 (defun gnus-summary-prev-unread-subject (n)
6926   "Go to previous N'th unread summary line."
6927   (interactive "p")
6928   (gnus-summary-next-subject (- n) t))
6929
6930 (defun gnus-summary-goto-subjects (articles)
6931   "Insert the subject header for ARTICLES in the current buffer."
6932   (save-excursion
6933     (dolist (article articles)
6934       (gnus-summary-goto-subject article t)))
6935   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6936   (gnus-summary-position-point))
6937  
6938 (defun gnus-summary-goto-subject (article &optional force silent)
6939   "Go the subject line of ARTICLE.
6940 If FORCE, also allow jumping to articles not currently shown."
6941   (interactive "nArticle number: ")
6942   (unless (numberp article)
6943     (error "Article %s is not a number" article))
6944   (let ((b (point))
6945         (data (gnus-data-find article)))
6946     ;; We read in the article if we have to.
6947     (and (not data)
6948          force
6949          (gnus-summary-insert-subject
6950           article
6951           (if (or (numberp force) (vectorp force)) force)
6952           t)
6953          (setq data (gnus-data-find article)))
6954     (goto-char b)
6955     (if (not data)
6956         (progn
6957           (unless silent
6958             (gnus-message 3 "Can't find article %d" article))
6959           nil)
6960       (let ((pt (gnus-data-pos data)))
6961         (goto-char pt)
6962         (gnus-summary-set-article-display-arrow pt))
6963       (gnus-summary-position-point)
6964       article)))
6965
6966 ;; Walking around summary lines with displaying articles.
6967
6968 (defun gnus-summary-expand-window (&optional arg)
6969   "Make the summary buffer take up the entire Emacs frame.
6970 Given a prefix, will force an `article' buffer configuration."
6971   (interactive "P")
6972   (if arg
6973       (gnus-configure-windows 'article 'force)
6974     (gnus-configure-windows 'summary 'force)))
6975
6976 (defun gnus-summary-display-article (article &optional all-header)
6977   "Display ARTICLE in article buffer."
6978   (when (gnus-buffer-live-p gnus-article-buffer)
6979     (with-current-buffer gnus-article-buffer
6980       (mm-enable-multibyte)))
6981   (gnus-set-global-variables)
6982   (when (gnus-buffer-live-p gnus-article-buffer)
6983     (with-current-buffer gnus-article-buffer
6984       (setq gnus-article-charset gnus-newsgroup-charset)
6985       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6986       (mm-enable-multibyte)))
6987   (if (null article)
6988       nil
6989     (prog1
6990         (if gnus-summary-display-article-function
6991             (funcall gnus-summary-display-article-function article all-header)
6992           (gnus-article-prepare article all-header))
6993       (gnus-run-hooks 'gnus-select-article-hook)
6994       (when (and gnus-current-article
6995                  (not (zerop gnus-current-article)))
6996         (gnus-summary-goto-subject gnus-current-article))
6997       (gnus-summary-recenter)
6998       (when (and gnus-use-trees gnus-show-threads)
6999         (gnus-possibly-generate-tree article)
7000         (gnus-highlight-selected-tree article))
7001       ;; Successfully display article.
7002       (gnus-article-set-window-start
7003        (cdr (assq article gnus-newsgroup-bookmarks))))))
7004
7005 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7006   "Select the current article.
7007 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7008 non-nil, the article will be re-fetched even if it already present in
7009 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7010 be displayed."
7011   ;; Make sure we are in the summary buffer to work around bbdb bug.
7012   (unless (eq major-mode 'gnus-summary-mode)
7013     (set-buffer gnus-summary-buffer))
7014   (let ((article (or article (gnus-summary-article-number)))
7015         (all-headers (not (not all-headers))) ;Must be t or nil.
7016         gnus-summary-display-article-function)
7017     (and (not pseudo)
7018          (gnus-summary-article-pseudo-p article)
7019          (error "This is a pseudo-article"))
7020     (save-excursion
7021       (set-buffer gnus-summary-buffer)
7022       (if (or (and gnus-single-article-buffer
7023                    (or (null gnus-current-article)
7024                        (null gnus-article-current)
7025                        (null (get-buffer gnus-article-buffer))
7026                        (not (eq article (cdr gnus-article-current)))
7027                        (not (equal (car gnus-article-current)
7028                                    gnus-newsgroup-name))))
7029               (and (not gnus-single-article-buffer)
7030                    (or (null gnus-current-article)
7031                        (not (eq gnus-current-article article))))
7032               force)
7033           ;; The requested article is different from the current article.
7034           (progn
7035             (gnus-summary-display-article article all-headers)
7036             (when (gnus-buffer-live-p gnus-article-buffer)
7037               (with-current-buffer gnus-article-buffer
7038                 (if (not gnus-article-decoded-p) ;; a local variable
7039                     (mm-disable-multibyte))))
7040             (gnus-article-set-window-start
7041              (cdr (assq article gnus-newsgroup-bookmarks)))
7042             article)
7043         'old))))
7044
7045 (defun gnus-summary-force-verify-and-decrypt ()
7046   "Display buttons for signed/encrypted parts and verify/decrypt them."
7047   (interactive)
7048   (let ((mm-verify-option 'known)
7049         (mm-decrypt-option 'known)
7050         (gnus-article-emulate-mime t)
7051         (gnus-buttonized-mime-types (append (list "multipart/signed"
7052                                                   "multipart/encrypted")
7053                                             gnus-buttonized-mime-types)))
7054     (gnus-summary-select-article nil 'force)))
7055
7056 (defun gnus-summary-set-current-mark (&optional current-mark)
7057   "Obsolete function."
7058   nil)
7059
7060 (defun gnus-summary-next-article (&optional unread subject backward push)
7061   "Select the next article.
7062 If UNREAD, only unread articles are selected.
7063 If SUBJECT, only articles with SUBJECT are selected.
7064 If BACKWARD, the previous article is selected instead of the next."
7065   (interactive "P")
7066   (cond
7067    ;; Is there such an article?
7068    ((and (gnus-summary-search-forward unread subject backward)
7069          (or (gnus-summary-display-article (gnus-summary-article-number))
7070              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7071     (gnus-summary-position-point))
7072    ;; If not, we try the first unread, if that is wanted.
7073    ((and subject
7074          gnus-auto-select-same
7075          (gnus-summary-first-unread-article))
7076     (gnus-summary-position-point)
7077     (gnus-message 6 "Wrapped"))
7078    ;; Try to get next/previous article not displayed in this group.
7079    ((and gnus-auto-extend-newsgroup
7080          (not unread) (not subject))
7081     (gnus-summary-goto-article
7082      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7083      nil (count-lines (point-min) (point))))
7084    ;; Go to next/previous group.
7085    (t
7086     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7087       (gnus-summary-jump-to-group gnus-newsgroup-name))
7088     (let ((cmd last-command-char)
7089           (point
7090            (save-excursion
7091              (set-buffer gnus-group-buffer)
7092              (point)))
7093           (group
7094            (if (eq gnus-keep-same-level 'best)
7095                (gnus-summary-best-group gnus-newsgroup-name)
7096              (gnus-summary-search-group backward gnus-keep-same-level))))
7097       ;; For some reason, the group window gets selected.  We change
7098       ;; it back.
7099       (select-window (get-buffer-window (current-buffer)))
7100       ;; Select next unread newsgroup automagically.
7101       (cond
7102        ((or (not gnus-auto-select-next)
7103             (not cmd))
7104         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7105        ((or (eq gnus-auto-select-next 'quietly)
7106             (and (eq gnus-auto-select-next 'slightly-quietly)
7107                  push)
7108             (and (eq gnus-auto-select-next 'almost-quietly)
7109                  (gnus-summary-last-article-p)))
7110         ;; Select quietly.
7111         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7112             (gnus-summary-exit)
7113           (gnus-message 7 "No more%s articles (%s)..."
7114                         (if unread " unread" "")
7115                         (if group (concat "selecting " group)
7116                           "exiting"))
7117           (gnus-summary-next-group nil group backward)))
7118        (t
7119         (when (gnus-key-press-event-p last-input-event)
7120           (gnus-summary-walk-group-buffer
7121            gnus-newsgroup-name cmd unread backward point))))))))
7122
7123 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7124   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7125                       (?\C-p (gnus-group-prev-unread-group 1))))
7126         (cursor-in-echo-area t)
7127         keve key group ended prompt)
7128     (save-excursion
7129       (set-buffer gnus-group-buffer)
7130       (goto-char start)
7131       (setq group
7132             (if (eq gnus-keep-same-level 'best)
7133                 (gnus-summary-best-group gnus-newsgroup-name)
7134               (gnus-summary-search-group backward gnus-keep-same-level))))
7135     (while (not ended)
7136       (setq prompt
7137             (format
7138              "No more%s articles%s " (if unread " unread" "")
7139              (if (and group
7140                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7141                  (format " (Type %s for %s [%s])"
7142                          (single-key-description cmd) group
7143                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7144                (format " (Type %s to exit %s)"
7145                        (single-key-description cmd)
7146                        gnus-newsgroup-name))))
7147       ;; Confirm auto selection.
7148       (setq key (car (setq keve (gnus-read-event-char prompt)))
7149             ended t)
7150       (cond
7151        ((assq key keystrokes)
7152         (let ((obuf (current-buffer)))
7153           (switch-to-buffer gnus-group-buffer)
7154           (when group
7155             (gnus-group-jump-to-group group))
7156           (eval (cadr (assq key keystrokes)))
7157           (setq group (gnus-group-group-name))
7158           (switch-to-buffer obuf))
7159         (setq ended nil))
7160        ((equal key cmd)
7161         (if (or (not group)
7162                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7163             (gnus-summary-exit)
7164           (gnus-summary-next-group nil group backward)))
7165        (t
7166         (push (cdr keve) unread-command-events))))))
7167
7168 (defun gnus-summary-next-unread-article ()
7169   "Select unread article after current one."
7170   (interactive)
7171   (gnus-summary-next-article
7172    (or (not (eq gnus-summary-goto-unread 'never))
7173        (gnus-summary-last-article-p (gnus-summary-article-number)))
7174    (and gnus-auto-select-same
7175         (gnus-summary-article-subject))))
7176
7177 (defun gnus-summary-prev-article (&optional unread subject)
7178   "Select the article after the current one.
7179 If UNREAD is non-nil, only unread articles are selected."
7180   (interactive "P")
7181   (gnus-summary-next-article unread subject t))
7182
7183 (defun gnus-summary-prev-unread-article ()
7184   "Select unread article before current one."
7185   (interactive)
7186   (gnus-summary-prev-article
7187    (or (not (eq gnus-summary-goto-unread 'never))
7188        (gnus-summary-first-article-p (gnus-summary-article-number)))
7189    (and gnus-auto-select-same
7190         (gnus-summary-article-subject))))
7191
7192 (defun gnus-summary-next-page (&optional lines circular stop)
7193   "Show next page of the selected article.
7194 If at the end of the current article, select the next article.
7195 LINES says how many lines should be scrolled up.
7196
7197 If CIRCULAR is non-nil, go to the start of the article instead of
7198 selecting the next article when reaching the end of the current
7199 article.
7200
7201 If STOP is non-nil, just stop when reaching the end of the message.
7202
7203 Also see the variable `gnus-article-skip-boring'."
7204   (interactive "P")
7205   (setq gnus-summary-buffer (current-buffer))
7206   (gnus-set-global-variables)
7207   (let ((article (gnus-summary-article-number))
7208         (article-window (get-buffer-window gnus-article-buffer t))
7209         endp)
7210     ;; If the buffer is empty, we have no article.
7211     (unless article
7212       (error "No article to select"))
7213     (gnus-configure-windows 'article)
7214     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7215         (if (and (eq gnus-summary-goto-unread 'never)
7216                  (not (gnus-summary-last-article-p article)))
7217             (gnus-summary-next-article)
7218           (gnus-summary-next-unread-article))
7219       (if (or (null gnus-current-article)
7220               (null gnus-article-current)
7221               (/= article (cdr gnus-article-current))
7222               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7223           ;; Selected subject is different from current article's.
7224           (gnus-summary-display-article article)
7225         (when article-window
7226           (gnus-eval-in-buffer-window gnus-article-buffer
7227             (setq endp (or (gnus-article-next-page lines)
7228                            (gnus-article-only-boring-p))))
7229           (when endp
7230             (cond (stop
7231                    (gnus-message 3 "End of message"))
7232                   (circular
7233                    (gnus-summary-beginning-of-article))
7234                   (lines
7235                    (gnus-message 3 "End of message"))
7236                   ((null lines)
7237                    (if (and (eq gnus-summary-goto-unread 'never)
7238                             (not (gnus-summary-last-article-p article)))
7239                        (gnus-summary-next-article)
7240                      (gnus-summary-next-unread-article))))))))
7241     (gnus-summary-recenter)
7242     (gnus-summary-position-point)))
7243
7244 (defun gnus-summary-prev-page (&optional lines move)
7245   "Show previous page of selected article.
7246 Argument LINES specifies lines to be scrolled down.
7247 If MOVE, move to the previous unread article if point is at
7248 the beginning of the buffer."
7249   (interactive "P")
7250   (let ((article (gnus-summary-article-number))
7251         (article-window (get-buffer-window gnus-article-buffer t))
7252         endp)
7253     (gnus-configure-windows 'article)
7254     (if (or (null gnus-current-article)
7255             (null gnus-article-current)
7256             (/= article (cdr gnus-article-current))
7257             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7258         ;; Selected subject is different from current article's.
7259         (gnus-summary-display-article article)
7260       (gnus-summary-recenter)
7261       (when article-window
7262         (gnus-eval-in-buffer-window gnus-article-buffer
7263           (setq endp (gnus-article-prev-page lines)))
7264         (when (and move endp)
7265           (cond (lines
7266                  (gnus-message 3 "Beginning of message"))
7267                 ((null lines)
7268                  (if (and (eq gnus-summary-goto-unread 'never)
7269                           (not (gnus-summary-first-article-p article)))
7270                      (gnus-summary-prev-article)
7271                    (gnus-summary-prev-unread-article))))))))
7272   (gnus-summary-position-point))
7273
7274 (defun gnus-summary-prev-page-or-article (&optional lines)
7275   "Show previous page of selected article.
7276 Argument LINES specifies lines to be scrolled down.
7277 If at the beginning of the article, go to the next article."
7278   (interactive "P")
7279   (gnus-summary-prev-page lines t))
7280
7281 (defun gnus-summary-scroll-up (lines)
7282   "Scroll up (or down) one line current article.
7283 Argument LINES specifies lines to be scrolled up (or down if negative)."
7284   (interactive "p")
7285   (gnus-configure-windows 'article)
7286   (gnus-summary-show-thread)
7287   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7288     (gnus-eval-in-buffer-window gnus-article-buffer
7289       (cond ((> lines 0)
7290              (when (gnus-article-next-page lines)
7291                (gnus-message 3 "End of message")))
7292             ((< lines 0)
7293              (gnus-article-prev-page (- lines))))))
7294   (gnus-summary-recenter)
7295   (gnus-summary-position-point))
7296
7297 (defun gnus-summary-scroll-down (lines)
7298   "Scroll down (or up) one line current article.
7299 Argument LINES specifies lines to be scrolled down (or up if negative)."
7300   (interactive "p")
7301   (gnus-summary-scroll-up (- lines)))
7302
7303 (defun gnus-summary-next-same-subject ()
7304   "Select next article which has the same subject as current one."
7305   (interactive)
7306   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7307
7308 (defun gnus-summary-prev-same-subject ()
7309   "Select previous article which has the same subject as current one."
7310   (interactive)
7311   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7312
7313 (defun gnus-summary-next-unread-same-subject ()
7314   "Select next unread article which has the same subject as current one."
7315   (interactive)
7316   (gnus-summary-next-article t (gnus-summary-article-subject)))
7317
7318 (defun gnus-summary-prev-unread-same-subject ()
7319   "Select previous unread article which has the same subject as current one."
7320   (interactive)
7321   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7322
7323 (defun gnus-summary-first-unread-article ()
7324   "Select the first unread article.
7325 Return nil if there are no unread articles."
7326   (interactive)
7327   (prog1
7328       (when (gnus-summary-first-subject t)
7329         (gnus-summary-show-thread)
7330         (gnus-summary-first-subject t)
7331         (gnus-summary-display-article (gnus-summary-article-number)))
7332     (gnus-summary-position-point)))
7333
7334 (defun gnus-summary-first-unread-subject ()
7335   "Place the point on the subject line of the first unread article.
7336 Return nil if there are no unread articles."
7337   (interactive)
7338   (prog1
7339       (when (gnus-summary-first-subject t)
7340         (gnus-summary-show-thread)
7341         (gnus-summary-first-subject t))
7342     (gnus-summary-position-point)))
7343
7344 (defun gnus-summary-first-unseen-subject ()
7345   "Place the point on the subject line of the first unseen article.
7346 Return nil if there are no unseen articles."
7347   (interactive)
7348   (prog1
7349       (when (gnus-summary-first-subject nil nil t)
7350         (gnus-summary-show-thread)
7351         (gnus-summary-first-subject nil nil t))
7352     (gnus-summary-position-point)))
7353
7354 (defun gnus-summary-first-unseen-or-unread-subject ()
7355   "Place the point on the subject line of the first unseen article or,
7356 if all article have been seen, on the subject line of the first unread
7357 article."
7358   (interactive)
7359   (prog1
7360       (unless (when (gnus-summary-first-subject nil nil t)
7361                 (gnus-summary-show-thread)
7362                 (gnus-summary-first-subject nil nil t))
7363         (when (gnus-summary-first-subject t)
7364           (gnus-summary-show-thread)
7365           (gnus-summary-first-subject t)))
7366     (gnus-summary-position-point)))
7367
7368 (defun gnus-summary-first-article ()
7369   "Select the first article.
7370 Return nil if there are no articles."
7371   (interactive)
7372   (prog1
7373       (when (gnus-summary-first-subject)
7374         (gnus-summary-show-thread)
7375         (gnus-summary-first-subject)
7376         (gnus-summary-display-article (gnus-summary-article-number)))
7377     (gnus-summary-position-point)))
7378
7379 (defun gnus-summary-best-unread-article (&optional arg)
7380   "Select the unread article with the highest score.
7381 If given a prefix argument, select the next unread article that has a
7382 score higher than the default score."
7383   (interactive "P")
7384   (let ((article (if arg
7385                      (gnus-summary-better-unread-subject)
7386                    (gnus-summary-best-unread-subject))))
7387     (if article
7388         (gnus-summary-goto-article article)
7389       (error "No unread articles"))))
7390
7391 (defun gnus-summary-best-unread-subject ()
7392   "Select the unread subject with the highest score."
7393   (interactive)
7394   (let ((best -1000000)
7395         (data gnus-newsgroup-data)
7396         article score)
7397     (while data
7398       (and (gnus-data-unread-p (car data))
7399            (> (setq score
7400                     (gnus-summary-article-score (gnus-data-number (car data))))
7401               best)
7402            (setq best score
7403                  article (gnus-data-number (car data))))
7404       (setq data (cdr data)))
7405     (when article
7406       (gnus-summary-goto-subject article))
7407     (gnus-summary-position-point)
7408     article))
7409
7410 (defun gnus-summary-better-unread-subject ()
7411   "Select the first unread subject that has a score over the default score."
7412   (interactive)
7413   (let ((data gnus-newsgroup-data)
7414         article score)
7415     (while (and (setq article (gnus-data-number (car data)))
7416                 (or (gnus-data-read-p (car data))
7417                     (not (> (gnus-summary-article-score article)
7418                             gnus-summary-default-score))))
7419       (setq data (cdr data)))
7420     (when article
7421       (gnus-summary-goto-subject article))
7422     (gnus-summary-position-point)
7423     article))
7424
7425 (defun gnus-summary-last-subject ()
7426   "Go to the last displayed subject line in the group."
7427   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7428     (when article
7429       (gnus-summary-goto-subject article))))
7430
7431 (defun gnus-summary-goto-article (article &optional all-headers force)
7432   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7433 If ALL-HEADERS is non-nil, no header lines are hidden.
7434 If FORCE, go to the article even if it isn't displayed.  If FORCE
7435 is a number, it is the line the article is to be displayed on."
7436   (interactive
7437    (list
7438     (completing-read
7439      "Article number or Message-ID: "
7440      (mapcar (lambda (number) (list (int-to-string number)))
7441              gnus-newsgroup-limit))
7442     current-prefix-arg
7443     t))
7444   (prog1
7445       (if (and (stringp article)
7446                (string-match "@\\|%40" article))
7447           (gnus-summary-refer-article article)
7448         (when (stringp article)
7449           (setq article (string-to-number article)))
7450         (if (gnus-summary-goto-subject article force)
7451             (gnus-summary-display-article article all-headers)
7452           (gnus-message 4 "Couldn't go to article %s" article) nil))
7453     (gnus-summary-position-point)))
7454
7455 (defun gnus-summary-goto-last-article ()
7456   "Go to the previously read article."
7457   (interactive)
7458   (prog1
7459       (when gnus-last-article
7460         (gnus-summary-goto-article gnus-last-article nil t))
7461     (gnus-summary-position-point)))
7462
7463 (defun gnus-summary-pop-article (number)
7464   "Pop one article off the history and go to the previous.
7465 NUMBER articles will be popped off."
7466   (interactive "p")
7467   (let (to)
7468     (setq gnus-newsgroup-history
7469           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7470     (if to
7471         (gnus-summary-goto-article (car to) nil t)
7472       (error "Article history empty")))
7473   (gnus-summary-position-point))
7474
7475 ;; Summary commands and functions for limiting the summary buffer.
7476
7477 (defun gnus-summary-limit-to-articles (n)
7478   "Limit the summary buffer to the next N articles.
7479 If not given a prefix, use the process marked articles instead."
7480   (interactive "P")
7481   (prog1
7482       (let ((articles (gnus-summary-work-articles n)))
7483         (setq gnus-newsgroup-processable nil)
7484         (gnus-summary-limit articles))
7485     (gnus-summary-position-point)))
7486
7487 (defun gnus-summary-pop-limit (&optional total)
7488   "Restore the previous limit.
7489 If given a prefix, remove all limits."
7490   (interactive "P")
7491   (when total
7492     (setq gnus-newsgroup-limits
7493           (list (mapcar (lambda (h) (mail-header-number h))
7494                         gnus-newsgroup-headers))))
7495   (unless gnus-newsgroup-limits
7496     (error "No limit to pop"))
7497   (prog1
7498       (gnus-summary-limit nil 'pop)
7499     (gnus-summary-position-point)))
7500
7501 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7502   "Limit the summary buffer to articles that have subjects that match a regexp.
7503 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7504   (interactive
7505    (list (read-string (if current-prefix-arg
7506                           "Exclude subject (regexp): "
7507                         "Limit to subject (regexp): "))
7508          nil current-prefix-arg))
7509   (unless header
7510     (setq header "subject"))
7511   (when (not (equal "" subject))
7512     (prog1
7513         (let ((articles (gnus-summary-find-matching
7514                          (or header "subject") subject 'all nil nil
7515                          not-matching)))
7516           (unless articles
7517             (error "Found no matches for \"%s\"" subject))
7518           (gnus-summary-limit articles))
7519       (gnus-summary-position-point))))
7520
7521 (defun gnus-summary-limit-to-author (from &optional not-matching)
7522   "Limit the summary buffer to articles that have authors that match a regexp.
7523 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7524   (interactive
7525    (list (read-string (if current-prefix-arg
7526                           "Exclude author (regexp): "
7527                         "Limit to author (regexp): "))
7528          current-prefix-arg))
7529   (gnus-summary-limit-to-subject from "from" not-matching))
7530
7531 (defun gnus-summary-limit-to-age (age &optional younger-p)
7532   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7533 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7534 articles that are younger than AGE days."
7535   (interactive
7536    (let ((younger current-prefix-arg)
7537          (days-got nil)
7538          days)
7539      (while (not days-got)
7540        (setq days (if younger
7541                       (read-string "Limit to articles younger than (in days, older when negative): ")
7542                     (read-string
7543                      "Limit to articles older than (in days, younger when negative): ")))
7544        (when (> (length days) 0)
7545          (setq days (read days)))
7546        (if (numberp days)
7547            (progn
7548              (setq days-got t)
7549              (if (< days 0)
7550                  (progn
7551                    (setq younger (not younger))
7552                    (setq days (* days -1)))))
7553          (message "Please enter a number.")
7554          (sleep-for 1)))
7555      (list days younger)))
7556   (prog1
7557       (let ((data gnus-newsgroup-data)
7558             (cutoff (days-to-time age))
7559             articles d date is-younger)
7560         (while (setq d (pop data))
7561           (when (and (vectorp (gnus-data-header d))
7562                      (setq date (mail-header-date (gnus-data-header d))))
7563             (setq is-younger (time-less-p
7564                               (time-since (condition-case ()
7565                                               (date-to-time date)
7566                                             (error '(0 0))))
7567                               cutoff))
7568             (when (if younger-p
7569                       is-younger
7570                     (not is-younger))
7571               (push (gnus-data-number d) articles))))
7572         (gnus-summary-limit (nreverse articles)))
7573     (gnus-summary-position-point)))
7574
7575 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7576   "Limit the summary buffer to articles that match an 'extra' header."
7577   (interactive
7578    (let ((header
7579           (intern
7580            (gnus-completing-read-with-default
7581             (symbol-name (car gnus-extra-headers))
7582             (if current-prefix-arg
7583                 "Exclude extra header:"
7584               "Limit extra header:")
7585             (mapcar (lambda (x)
7586                       (cons (symbol-name x) x))
7587                     gnus-extra-headers)
7588             nil
7589             t))))
7590      (list header
7591            (read-string (format "%s header %s (regexp): "
7592                                 (if current-prefix-arg "Exclude" "Limit to")
7593                                 header))
7594            current-prefix-arg)))
7595   (when (not (equal "" regexp))
7596     (prog1
7597         (let ((articles (gnus-summary-find-matching
7598                          (cons 'extra header) regexp 'all nil nil
7599                          not-matching)))
7600           (unless articles
7601             (error "Found no matches for \"%s\"" regexp))
7602           (gnus-summary-limit articles))
7603       (gnus-summary-position-point))))
7604
7605 (defun gnus-summary-limit-to-display-predicate ()
7606   "Limit the summary buffer to the predicated in the `display' group parameter."
7607   (interactive)
7608   (unless gnus-newsgroup-display
7609     (error "There is no `display' group parameter"))
7610   (let (articles)
7611     (dolist (number gnus-newsgroup-articles)
7612       (when (funcall gnus-newsgroup-display)
7613         (push number articles)))
7614     (gnus-summary-limit articles))
7615   (gnus-summary-position-point))
7616
7617 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7618 (make-obsolete
7619  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7620
7621 (defun gnus-summary-limit-to-unread (&optional all)
7622   "Limit the summary buffer to articles that are not marked as read.
7623 If ALL is non-nil, limit strictly to unread articles."
7624   (interactive "P")
7625   (if all
7626       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7627     (gnus-summary-limit-to-marks
7628      ;; Concat all the marks that say that an article is read and have
7629      ;; those removed.
7630      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7631            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7632            gnus-low-score-mark gnus-expirable-mark
7633            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7634            gnus-duplicate-mark gnus-souped-mark)
7635      'reverse)))
7636
7637 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7638 (make-obsolete 'gnus-summary-delete-marked-with
7639                'gnus-summary-limit-exclude-marks)
7640
7641 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7642   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7643 If REVERSE, limit the summary buffer to articles that are marked
7644 with MARKS.  MARKS can either be a string of marks or a list of marks.
7645 Returns how many articles were removed."
7646   (interactive "sMarks: ")
7647   (gnus-summary-limit-to-marks marks t))
7648
7649 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7650   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7651 If REVERSE (the prefix), limit the summary buffer to articles that are
7652 not marked with MARKS.  MARKS can either be a string of marks or a
7653 list of marks.
7654 Returns how many articles were removed."
7655   (interactive "sMarks: \nP")
7656   (prog1
7657       (let ((data gnus-newsgroup-data)
7658             (marks (if (listp marks) marks
7659                      (append marks nil))) ; Transform to list.
7660             articles)
7661         (while data
7662           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7663                   (memq (gnus-data-mark (car data)) marks))
7664             (push (gnus-data-number (car data)) articles))
7665           (setq data (cdr data)))
7666         (gnus-summary-limit articles))
7667     (gnus-summary-position-point)))
7668
7669 (defun gnus-summary-limit-to-score (score)
7670   "Limit to articles with score at or above SCORE."
7671   (interactive "NLimit to articles with score of at least: ")
7672   (let ((data gnus-newsgroup-data)
7673         articles)
7674     (while data
7675       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7676                 score)
7677         (push (gnus-data-number (car data)) articles))
7678       (setq data (cdr data)))
7679     (prog1
7680         (gnus-summary-limit articles)
7681       (gnus-summary-position-point))))
7682
7683 (defun gnus-summary-limit-to-unseen ()
7684   "Limit to unseen articles."
7685   (interactive)
7686   (prog1
7687       (gnus-summary-limit gnus-newsgroup-unseen)
7688     (gnus-summary-position-point)))
7689
7690 (defun gnus-summary-limit-include-thread (id)
7691   "Display all the hidden articles that is in the thread with ID in it.
7692 When called interactively, ID is the Message-ID of the current
7693 article."
7694   (interactive (list (mail-header-id (gnus-summary-article-header))))
7695   (let ((articles (gnus-articles-in-thread
7696                    (gnus-id-to-thread (gnus-root-id id)))))
7697     (prog1
7698         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7699       (gnus-summary-limit-include-matching-articles
7700        "subject"
7701        (regexp-quote (gnus-simplify-subject-re
7702                       (mail-header-subject (gnus-id-to-header id)))))
7703       (gnus-summary-position-point))))
7704
7705 (defun gnus-summary-limit-include-matching-articles (header regexp)
7706   "Display all the hidden articles that have HEADERs that match REGEXP."
7707   (interactive (list (read-string "Match on header: ")
7708                      (read-string "Regexp: ")))
7709   (let ((articles (gnus-find-matching-articles header regexp)))
7710     (prog1
7711         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7712       (gnus-summary-position-point))))
7713
7714 (defun gnus-summary-insert-dormant-articles ()
7715   "Insert all the dormant articles for this group into the current buffer."
7716   (interactive)
7717   (let ((gnus-verbose (max 6 gnus-verbose)))
7718     (if (not gnus-newsgroup-dormant)
7719         (gnus-message 3 "No cached articles for this group")
7720       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7721
7722 (defun gnus-summary-limit-include-dormant ()
7723   "Display all the hidden articles that are marked as dormant.
7724 Note that this command only works on a subset of the articles currently
7725 fetched for this group."
7726   (interactive)
7727   (unless gnus-newsgroup-dormant
7728     (error "There are no dormant articles in this group"))
7729   (prog1
7730       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7731     (gnus-summary-position-point)))
7732
7733 (defun gnus-summary-limit-exclude-dormant ()
7734   "Hide all dormant articles."
7735   (interactive)
7736   (prog1
7737       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7738     (gnus-summary-position-point)))
7739
7740 (defun gnus-summary-limit-exclude-childless-dormant ()
7741   "Hide all dormant articles that have no children."
7742   (interactive)
7743   (let ((data (gnus-data-list t))
7744         articles d children)
7745     ;; Find all articles that are either not dormant or have
7746     ;; children.
7747     (while (setq d (pop data))
7748       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7749                 (and (setq children
7750                            (gnus-article-children (gnus-data-number d)))
7751                      (let (found)
7752                        (while children
7753                          (when (memq (car children) articles)
7754                            (setq children nil
7755                                  found t))
7756                          (pop children))
7757                        found)))
7758         (push (gnus-data-number d) articles)))
7759     ;; Do the limiting.
7760     (prog1
7761         (gnus-summary-limit articles)
7762       (gnus-summary-position-point))))
7763
7764 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7765   "Mark all unread excluded articles as read.
7766 If ALL, mark even excluded ticked and dormants as read."
7767   (interactive "P")
7768   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7769   (let ((articles (gnus-sorted-ndifference
7770                    (sort
7771                     (mapcar (lambda (h) (mail-header-number h))
7772                             gnus-newsgroup-headers)
7773                     '<)
7774                    gnus-newsgroup-limit))
7775         article)
7776     (setq gnus-newsgroup-unreads
7777           (gnus-sorted-intersection gnus-newsgroup-unreads
7778                                     gnus-newsgroup-limit))
7779     (if all
7780         (setq gnus-newsgroup-dormant nil
7781               gnus-newsgroup-marked nil
7782               gnus-newsgroup-reads
7783               (nconc
7784                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7785                gnus-newsgroup-reads))
7786       (while (setq article (pop articles))
7787         (unless (or (memq article gnus-newsgroup-dormant)
7788                     (memq article gnus-newsgroup-marked))
7789           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7790
7791 (defun gnus-summary-limit (articles &optional pop)
7792   (if pop
7793       ;; We pop the previous limit off the stack and use that.
7794       (setq articles (car gnus-newsgroup-limits)
7795             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7796     ;; We use the new limit, so we push the old limit on the stack.
7797     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7798   ;; Set the limit.
7799   (setq gnus-newsgroup-limit articles)
7800   (let ((total (length gnus-newsgroup-data))
7801         (data (gnus-data-find-list (gnus-summary-article-number)))
7802         (gnus-summary-mark-below nil)   ; Inhibit this.
7803         found)
7804     ;; This will do all the work of generating the new summary buffer
7805     ;; according to the new limit.
7806     (gnus-summary-prepare)
7807     ;; Hide any threads, possibly.
7808     (gnus-summary-maybe-hide-threads)
7809     ;; Try to return to the article you were at, or one in the
7810     ;; neighborhood.
7811     (when data
7812       ;; We try to find some article after the current one.
7813       (while data
7814         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7815           (setq data nil
7816                 found t))
7817         (setq data (cdr data))))
7818     (unless found
7819       ;; If there is no data, that means that we were after the last
7820       ;; article.  The same goes when we can't find any articles
7821       ;; after the current one.
7822       (goto-char (point-max))
7823       (gnus-summary-find-prev))
7824     (gnus-set-mode-line 'summary)
7825     ;; We return how many articles were removed from the summary
7826     ;; buffer as a result of the new limit.
7827     (- total (length gnus-newsgroup-data))))
7828
7829 (defsubst gnus-invisible-cut-children (threads)
7830   (let ((num 0))
7831     (while threads
7832       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7833         (incf num))
7834       (pop threads))
7835     (< num 2)))
7836
7837 (defsubst gnus-cut-thread (thread)
7838   "Go forwards in the thread until we find an article that we want to display."
7839   (when (or (eq gnus-fetch-old-headers 'some)
7840             (eq gnus-fetch-old-headers 'invisible)
7841             (numberp gnus-fetch-old-headers)
7842             (eq gnus-build-sparse-threads 'some)
7843             (eq gnus-build-sparse-threads 'more))
7844     ;; Deal with old-fetched headers and sparse threads.
7845     (while (and
7846             thread
7847             (or
7848              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7849              (gnus-summary-article-ancient-p
7850               (mail-header-number (car thread))))
7851             (if (or (<= (length (cdr thread)) 1)
7852                     (eq gnus-fetch-old-headers 'invisible))
7853                 (setq gnus-newsgroup-limit
7854                       (delq (mail-header-number (car thread))
7855                             gnus-newsgroup-limit)
7856                       thread (cadr thread))
7857               (when (gnus-invisible-cut-children (cdr thread))
7858                 (let ((th (cdr thread)))
7859                   (while th
7860                     (if (memq (mail-header-number (caar th))
7861                               gnus-newsgroup-limit)
7862                         (setq thread (car th)
7863                               th nil)
7864                       (setq th (cdr th))))))))))
7865   thread)
7866
7867 (defun gnus-cut-threads (threads)
7868   "Cut off all uninteresting articles from the beginning of THREADS."
7869   (when (or (eq gnus-fetch-old-headers 'some)
7870             (eq gnus-fetch-old-headers 'invisible)
7871             (numberp gnus-fetch-old-headers)
7872             (eq gnus-build-sparse-threads 'some)
7873             (eq gnus-build-sparse-threads 'more))
7874     (let ((th threads))
7875       (while th
7876         (setcar th (gnus-cut-thread (car th)))
7877         (setq th (cdr th)))))
7878   ;; Remove nixed out threads.
7879   (delq nil threads))
7880
7881 (defun gnus-summary-initial-limit (&optional show-if-empty)
7882   "Figure out what the initial limit is supposed to be on group entry.
7883 This entails weeding out unwanted dormants, low-scored articles,
7884 fetch-old-headers verbiage, and so on."
7885   ;; Most groups have nothing to remove.
7886   (if (or gnus-inhibit-limiting
7887           (and (null gnus-newsgroup-dormant)
7888                (eq gnus-newsgroup-display 'gnus-not-ignore)
7889                (not (eq gnus-fetch-old-headers 'some))
7890                (not (numberp gnus-fetch-old-headers))
7891                (not (eq gnus-fetch-old-headers 'invisible))
7892                (null gnus-summary-expunge-below)
7893                (not (eq gnus-build-sparse-threads 'some))
7894                (not (eq gnus-build-sparse-threads 'more))
7895                (null gnus-thread-expunge-below)
7896                (not gnus-use-nocem)))
7897       ()                                ; Do nothing.
7898     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7899     (setq gnus-newsgroup-limit nil)
7900     (mapatoms
7901      (lambda (node)
7902        (unless (car (symbol-value node))
7903          ;; These threads have no parents -- they are roots.
7904          (let ((nodes (cdr (symbol-value node)))
7905                thread)
7906            (while nodes
7907              (if (and gnus-thread-expunge-below
7908                       (< (gnus-thread-total-score (car nodes))
7909                          gnus-thread-expunge-below))
7910                  (gnus-expunge-thread (pop nodes))
7911                (setq thread (pop nodes))
7912                (gnus-summary-limit-children thread))))))
7913      gnus-newsgroup-dependencies)
7914     ;; If this limitation resulted in an empty group, we might
7915     ;; pop the previous limit and use it instead.
7916     (when (and (not gnus-newsgroup-limit)
7917                show-if-empty)
7918       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7919     gnus-newsgroup-limit))
7920
7921 (defun gnus-summary-limit-children (thread)
7922   "Return 1 if this subthread is visible and 0 if it is not."
7923   ;; First we get the number of visible children to this thread.  This
7924   ;; is done by recursing down the thread using this function, so this
7925   ;; will really go down to a leaf article first, before slowly
7926   ;; working its way up towards the root.
7927   (when thread
7928     (let* ((max-lisp-eval-depth 5000)
7929            (children
7930            (if (cdr thread)
7931                (apply '+ (mapcar 'gnus-summary-limit-children
7932                                  (cdr thread)))
7933              0))
7934           (number (mail-header-number (car thread)))
7935           score)
7936       (if (and
7937            (not (memq number gnus-newsgroup-marked))
7938            (or
7939             ;; If this article is dormant and has absolutely no visible
7940             ;; children, then this article isn't visible.
7941             (and (memq number gnus-newsgroup-dormant)
7942                  (zerop children))
7943             ;; If this is "fetch-old-headered" and there is no
7944             ;; visible children, then we don't want this article.
7945             (and (or (eq gnus-fetch-old-headers 'some)
7946                      (numberp gnus-fetch-old-headers))
7947                  (gnus-summary-article-ancient-p number)
7948                  (zerop children))
7949             ;; If this is "fetch-old-headered" and `invisible', then
7950             ;; we don't want this article.
7951             (and (eq gnus-fetch-old-headers 'invisible)
7952                  (gnus-summary-article-ancient-p number))
7953             ;; If this is a sparsely inserted article with no children,
7954             ;; we don't want it.
7955             (and (eq gnus-build-sparse-threads 'some)
7956                  (gnus-summary-article-sparse-p number)
7957                  (zerop children))
7958             ;; If we use expunging, and this article is really
7959             ;; low-scored, then we don't want this article.
7960             (when (and gnus-summary-expunge-below
7961                        (< (setq score
7962                                 (or (cdr (assq number gnus-newsgroup-scored))
7963                                     gnus-summary-default-score))
7964                           gnus-summary-expunge-below))
7965               ;; We increase the expunge-tally here, but that has
7966               ;; nothing to do with the limits, really.
7967               (incf gnus-newsgroup-expunged-tally)
7968               ;; We also mark as read here, if that's wanted.
7969               (when (and gnus-summary-mark-below
7970                          (< score gnus-summary-mark-below))
7971                 (setq gnus-newsgroup-unreads
7972                       (delq number gnus-newsgroup-unreads))
7973                 (if gnus-newsgroup-auto-expire
7974                     (push number gnus-newsgroup-expirable)
7975                   (push (cons number gnus-low-score-mark)
7976                         gnus-newsgroup-reads)))
7977               t)
7978             ;; Do the `display' group parameter.
7979             (and gnus-newsgroup-display
7980                  (not (funcall gnus-newsgroup-display)))
7981             ;; Check NoCeM things.
7982             (if (and gnus-use-nocem
7983                      (gnus-nocem-unwanted-article-p
7984                       (mail-header-id (car thread))))
7985                 (progn
7986                   (setq gnus-newsgroup-unreads
7987                         (delq number gnus-newsgroup-unreads))
7988                   t))))
7989           ;; Nope, invisible article.
7990           0
7991         ;; Ok, this article is to be visible, so we add it to the limit
7992         ;; and return 1.
7993         (push number gnus-newsgroup-limit)
7994         1))))
7995
7996 (defun gnus-expunge-thread (thread)
7997   "Mark all articles in THREAD as read."
7998   (let* ((number (mail-header-number (car thread))))
7999     (incf gnus-newsgroup-expunged-tally)
8000     ;; We also mark as read here, if that's wanted.
8001     (setq gnus-newsgroup-unreads
8002           (delq number gnus-newsgroup-unreads))
8003     (if gnus-newsgroup-auto-expire
8004         (push number gnus-newsgroup-expirable)
8005       (push (cons number gnus-low-score-mark)
8006             gnus-newsgroup-reads)))
8007   ;; Go recursively through all subthreads.
8008   (mapcar 'gnus-expunge-thread (cdr thread)))
8009
8010 ;; Summary article oriented commands
8011
8012 (defun gnus-summary-refer-parent-article (n)
8013   "Refer parent article N times.
8014 If N is negative, go to ancestor -N instead.
8015 The difference between N and the number of articles fetched is returned."
8016   (interactive "p")
8017   (let ((skip 1)
8018         error header ref)
8019     (when (not (natnump n))
8020       (setq skip (abs n)
8021             n 1))
8022     (while (and (> n 0)
8023                 (not error))
8024       (setq header (gnus-summary-article-header))
8025       (if (and (eq (mail-header-number header)
8026                    (cdr gnus-article-current))
8027                (equal gnus-newsgroup-name
8028                       (car gnus-article-current)))
8029           ;; If we try to find the parent of the currently
8030           ;; displayed article, then we take a look at the actual
8031           ;; References header, since this is slightly more
8032           ;; reliable than the References field we got from the
8033           ;; server.
8034           (save-excursion
8035             (set-buffer gnus-original-article-buffer)
8036             (nnheader-narrow-to-headers)
8037             (unless (setq ref (message-fetch-field "references"))
8038               (when (setq ref (message-fetch-field "in-reply-to"))
8039                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8040             (widen))
8041         (setq ref
8042               ;; It's not the current article, so we take a bet on
8043               ;; the value we got from the server.
8044               (mail-header-references header)))
8045       (if (and ref
8046                (not (equal ref "")))
8047           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8048             (gnus-message 1 "Couldn't find parent"))
8049         (gnus-message 1 "No references in article %d"
8050                       (gnus-summary-article-number))
8051         (setq error t))
8052       (decf n))
8053     (gnus-summary-position-point)
8054     n))
8055
8056 (defun gnus-summary-refer-references ()
8057   "Fetch all articles mentioned in the References header.
8058 Return the number of articles fetched."
8059   (interactive)
8060   (let ((ref (mail-header-references (gnus-summary-article-header)))
8061         (current (gnus-summary-article-number))
8062         (n 0))
8063     (if (or (not ref)
8064             (equal ref ""))
8065         (error "No References in the current article")
8066       ;; For each Message-ID in the References header...
8067       (while (string-match "<[^>]*>" ref)
8068         (incf n)
8069         ;; ... fetch that article.
8070         (gnus-summary-refer-article
8071          (prog1 (match-string 0 ref)
8072            (setq ref (substring ref (match-end 0))))))
8073       (gnus-summary-goto-subject current)
8074       (gnus-summary-position-point)
8075       n)))
8076
8077 (defun gnus-summary-refer-thread (&optional limit)
8078   "Fetch all articles in the current thread.
8079 If LIMIT (the numerical prefix), fetch that many old headers instead
8080 of what's specified by the `gnus-refer-thread-limit' variable."
8081   (interactive "P")
8082   (let ((id (mail-header-id (gnus-summary-article-header)))
8083         (limit (if limit (prefix-numeric-value limit)
8084                  gnus-refer-thread-limit)))
8085     (unless (eq gnus-fetch-old-headers 'invisible)
8086       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8087       ;; Retrieve the headers and read them in.
8088       (if (eq (if (numberp limit)
8089                   (gnus-retrieve-headers
8090                    (list (min
8091                           (+ (mail-header-number
8092                               (gnus-summary-article-header))
8093                              limit)
8094                           gnus-newsgroup-end))
8095                    gnus-newsgroup-name (* limit 2))
8096                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8097                 ;; headers.
8098                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8099                                        gnus-newsgroup-name limit))
8100               'nov)
8101           (gnus-build-all-threads)
8102         (error "Can't fetch thread from back ends that don't support NOV"))
8103       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8104     (gnus-summary-limit-include-thread id)))
8105
8106 (defun gnus-summary-refer-article (message-id)
8107   "Fetch an article specified by MESSAGE-ID."
8108   (interactive "sMessage-ID: ")
8109   (when (and (stringp message-id)
8110              (not (zerop (length message-id))))
8111     (setq message-id (gnus-replace-in-string message-id " " ""))
8112     ;; Construct the correct Message-ID if necessary.
8113     ;; Suggested by tale@pawl.rpi.edu.
8114     (unless (string-match "^<" message-id)
8115       (setq message-id (concat "<" message-id)))
8116     (unless (string-match ">$" message-id)
8117       (setq message-id (concat message-id ">")))
8118     ;; People often post MIDs from URLs, so unhex it:
8119     (unless (string-match "@" message-id)
8120       (setq message-id (gnus-url-unhex-string message-id)))
8121     (let* ((header (gnus-id-to-header message-id))
8122            (sparse (and header
8123                         (gnus-summary-article-sparse-p
8124                          (mail-header-number header))
8125                         (memq (mail-header-number header)
8126                               gnus-newsgroup-limit)))
8127            number)
8128       (cond
8129        ;; If the article is present in the buffer we just go to it.
8130        ((and header
8131              (or (not (gnus-summary-article-sparse-p
8132                        (mail-header-number header)))
8133                  sparse))
8134         (prog1
8135             (gnus-summary-goto-article
8136              (mail-header-number header) nil t)
8137           (when sparse
8138             (gnus-summary-update-article (mail-header-number header)))))
8139        (t
8140         ;; We fetch the article.
8141         (catch 'found
8142           (dolist (gnus-override-method (gnus-refer-article-methods))
8143             (when (and (gnus-check-server gnus-override-method)
8144                        ;; Fetch the header,
8145                        (setq number (gnus-summary-insert-subject message-id)))
8146               ;; and display the article.
8147               (gnus-summary-select-article nil nil nil number)
8148               (throw 'found t)))
8149           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8150
8151 (defun gnus-refer-article-methods ()
8152   "Return a list of referable methods."
8153   (cond
8154    ;; No method, so we default to current and native.
8155    ((null gnus-refer-article-method)
8156     (list gnus-current-select-method gnus-select-method))
8157    ;; Current.
8158    ((eq 'current gnus-refer-article-method)
8159     (list gnus-current-select-method))
8160    ;; List of select methods.
8161    ((not (and (symbolp (car gnus-refer-article-method))
8162               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8163     (let (out)
8164       (dolist (method gnus-refer-article-method)
8165         (push (if (eq 'current method)
8166                   gnus-current-select-method
8167                 method)
8168               out))
8169       (nreverse out)))
8170    ;; One single select method.
8171    (t
8172     (list gnus-refer-article-method))))
8173
8174 (defun gnus-summary-edit-parameters ()
8175   "Edit the group parameters of the current group."
8176   (interactive)
8177   (gnus-group-edit-group gnus-newsgroup-name 'params))
8178
8179 (defun gnus-summary-customize-parameters ()
8180   "Customize the group parameters of the current group."
8181   (interactive)
8182   (gnus-group-customize gnus-newsgroup-name))
8183
8184 (defun gnus-summary-enter-digest-group (&optional force)
8185   "Enter an nndoc group based on the current article.
8186 If FORCE, force a digest interpretation.  If not, try
8187 to guess what the document format is."
8188   (interactive "P")
8189   (let ((conf gnus-current-window-configuration))
8190     (save-window-excursion
8191       (save-excursion
8192         (let (gnus-article-prepare-hook
8193               gnus-display-mime-function
8194               gnus-break-pages)
8195           (gnus-summary-select-article))))
8196     (setq gnus-current-window-configuration conf)
8197     (let* ((name (format "%s-%d"
8198                          (gnus-group-prefixed-name
8199                           gnus-newsgroup-name (list 'nndoc ""))
8200                          (save-excursion
8201                            (set-buffer gnus-summary-buffer)
8202                            gnus-current-article)))
8203            (ogroup gnus-newsgroup-name)
8204            (params (append (gnus-info-params (gnus-get-info ogroup))
8205                            (list (cons 'to-group ogroup))
8206                            (list (cons 'parent-group ogroup))
8207                            (list (cons 'save-article-group ogroup))))
8208            (case-fold-search t)
8209            (buf (current-buffer))
8210            dig to-address)
8211       (save-excursion
8212         (set-buffer gnus-original-article-buffer)
8213         ;; Have the digest group inherit the main mail address of
8214         ;; the parent article.
8215         (when (setq to-address (or (gnus-fetch-field "reply-to")
8216                                    (gnus-fetch-field "from")))
8217           (setq params (append
8218                         (list (cons 'to-address
8219                                     (funcall gnus-decode-encoded-word-function
8220                                              to-address))))))
8221         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8222         (insert-buffer-substring gnus-original-article-buffer)
8223         ;; Remove lines that may lead nndoc to misinterpret the
8224         ;; document type.
8225         (narrow-to-region
8226          (goto-char (point-min))
8227          (or (search-forward "\n\n" nil t) (point)))
8228         (goto-char (point-min))
8229         (delete-matching-lines "^Path:\\|^From ")
8230         (widen))
8231       (unwind-protect
8232           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8233                     (gnus-newsgroup-ephemeral-ignored-charsets
8234                      gnus-newsgroup-ignored-charsets))
8235                 (gnus-group-read-ephemeral-group
8236                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8237                               (nndoc-article-type
8238                                ,(if force 'mbox 'guess)))
8239                  t nil nil nil
8240                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8241                                                         "ADAPT")))))
8242               ;; Make all postings to this group go to the parent group.
8243               (nconc (gnus-info-params (gnus-get-info name))
8244                      params)
8245             ;; Couldn't select this doc group.
8246             (switch-to-buffer buf)
8247             (gnus-set-global-variables)
8248             (gnus-configure-windows 'summary)
8249             (gnus-message 3 "Article couldn't be entered?"))
8250         (kill-buffer dig)))))
8251
8252 (defun gnus-summary-read-document (n)
8253   "Open a new group based on the current article(s).
8254 This will allow you to read digests and other similar
8255 documents as newsgroups.
8256 Obeys the standard process/prefix convention."
8257   (interactive "P")
8258   (let* ((articles (gnus-summary-work-articles n))
8259          (ogroup gnus-newsgroup-name)
8260          (params (append (gnus-info-params (gnus-get-info ogroup))
8261                          (list (cons 'to-group ogroup))))
8262          article group egroup groups vgroup)
8263     (while (setq article (pop articles))
8264       (setq group (format "%s-%d" gnus-newsgroup-name article))
8265       (gnus-summary-remove-process-mark article)
8266       (when (gnus-summary-display-article article)
8267         (save-excursion
8268           (with-temp-buffer
8269             (insert-buffer-substring gnus-original-article-buffer)
8270             ;; Remove some headers that may lead nndoc to make
8271             ;; the wrong guess.
8272             (message-narrow-to-head)
8273             (goto-char (point-min))
8274             (delete-matching-lines "^\\(Path\\):\\|^From ")
8275             (widen)
8276             (if (setq egroup
8277                       (gnus-group-read-ephemeral-group
8278                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8279                                      (nndoc-article-type guess))
8280                        t nil t))
8281                 (progn
8282             ;; Make all postings to this group go to the parent group.
8283                   (nconc (gnus-info-params (gnus-get-info egroup))
8284                          params)
8285                   (push egroup groups))
8286               ;; Couldn't select this doc group.
8287               (gnus-error 3 "Article couldn't be entered"))))))
8288     ;; Now we have selected all the documents.
8289     (cond
8290      ((not groups)
8291       (error "None of the articles could be interpreted as documents"))
8292      ((gnus-group-read-ephemeral-group
8293        (setq vgroup (format
8294                      "nnvirtual:%s-%s" gnus-newsgroup-name
8295                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8296        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8297        t
8298        (cons (current-buffer) 'summary)))
8299      (t
8300       (error "Couldn't select virtual nndoc group")))))
8301
8302 (defun gnus-summary-isearch-article (&optional regexp-p)
8303   "Do incremental search forward on the current article.
8304 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8305   (interactive "P")
8306   (gnus-summary-select-article)
8307   (gnus-configure-windows 'article)
8308   (gnus-eval-in-buffer-window gnus-article-buffer
8309     (save-restriction
8310       (widen)
8311       (isearch-forward regexp-p))))
8312
8313 (defun gnus-summary-search-article-forward (regexp &optional backward)
8314   "Search for an article containing REGEXP forward.
8315 If BACKWARD, search backward instead."
8316   (interactive
8317    (list (read-string
8318           (format "Search article %s (regexp%s): "
8319                   (if current-prefix-arg "backward" "forward")
8320                   (if gnus-last-search-regexp
8321                       (concat ", default " gnus-last-search-regexp)
8322                     "")))
8323          current-prefix-arg))
8324   (if (string-equal regexp "")
8325       (setq regexp (or gnus-last-search-regexp ""))
8326     (setq gnus-last-search-regexp regexp)
8327     (setq gnus-article-before-search gnus-current-article))
8328   ;; Intentionally set gnus-last-article.
8329   (setq gnus-last-article gnus-article-before-search)
8330   (let ((gnus-last-article gnus-last-article))
8331     (if (gnus-summary-search-article regexp backward)
8332         (gnus-summary-show-thread)
8333       (error "Search failed: \"%s\"" regexp))))
8334
8335 (defun gnus-summary-search-article-backward (regexp)
8336   "Search for an article containing REGEXP backward."
8337   (interactive
8338    (list (read-string
8339           (format "Search article backward (regexp%s): "
8340                   (if gnus-last-search-regexp
8341                       (concat ", default " gnus-last-search-regexp)
8342                     "")))))
8343   (gnus-summary-search-article-forward regexp 'backward))
8344
8345 (defun gnus-summary-search-article (regexp &optional backward)
8346   "Search for an article containing REGEXP.
8347 Optional argument BACKWARD means do search for backward.
8348 `gnus-select-article-hook' is not called during the search."
8349   ;; We have to require this here to make sure that the following
8350   ;; dynamic binding isn't shadowed by autoloading.
8351   (require 'gnus-async)
8352   (require 'gnus-art)
8353   (let ((gnus-select-article-hook nil)  ;Disable hook.
8354         (gnus-article-prepare-hook nil)
8355         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8356         (gnus-use-article-prefetch nil)
8357         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8358         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8359         (gnus-visual nil)
8360         (gnus-keep-backlog nil)
8361         (gnus-break-pages nil)
8362         (gnus-summary-display-arrow nil)
8363         (gnus-updated-mode-lines nil)
8364         (gnus-auto-center-summary nil)
8365         (sum (current-buffer))
8366         (gnus-display-mime-function nil)
8367         (found nil)
8368         point)
8369     (gnus-save-hidden-threads
8370       (gnus-summary-select-article)
8371       (set-buffer gnus-article-buffer)
8372       (goto-char (window-point (get-buffer-window (current-buffer))))
8373       (when backward
8374         (forward-line -1))
8375       (while (not found)
8376         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8377         (if (if backward
8378                 (re-search-backward regexp nil t)
8379               (re-search-forward regexp nil t))
8380             ;; We found the regexp.
8381             (progn
8382               (setq found 'found)
8383               (beginning-of-line)
8384               (set-window-start
8385                (get-buffer-window (current-buffer))
8386                (point))
8387               (forward-line 1)
8388               (set-window-point
8389                (get-buffer-window (current-buffer))
8390                (point))
8391               (set-buffer sum)
8392               (setq point (point)))
8393           ;; We didn't find it, so we go to the next article.
8394           (set-buffer sum)
8395           (setq found 'not)
8396           (while (eq found 'not)
8397             (if (not (if backward (gnus-summary-find-prev)
8398                        (gnus-summary-find-next)))
8399                 ;; No more articles.
8400                 (setq found t)
8401               ;; Select the next article and adjust point.
8402               (unless (gnus-summary-article-sparse-p
8403                        (gnus-summary-article-number))
8404                 (setq found nil)
8405                 (gnus-summary-select-article)
8406                 (set-buffer gnus-article-buffer)
8407                 (widen)
8408                 (goto-char (if backward (point-max) (point-min))))))))
8409       (gnus-message 7 ""))
8410     ;; Return whether we found the regexp.
8411     (when (eq found 'found)
8412       (goto-char point)
8413       (gnus-summary-show-thread)
8414       (gnus-summary-goto-subject gnus-current-article)
8415       (gnus-summary-position-point)
8416       t)))
8417
8418 (defun gnus-find-matching-articles (header regexp)
8419   "Return a list of all articles that match REGEXP on HEADER.
8420 This search includes all articles in the current group that Gnus has
8421 fetched headers for, whether they are displayed or not."
8422   (let ((articles nil)
8423         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8424         (case-fold-search t))
8425     (dolist (header gnus-newsgroup-headers)
8426       (when (string-match regexp (funcall func header))
8427         (push (mail-header-number header) articles)))
8428     (nreverse articles)))
8429
8430 (defun gnus-summary-find-matching (header regexp &optional backward unread
8431                                           not-case-fold not-matching)
8432   "Return a list of all articles that match REGEXP on HEADER.
8433 The search stars on the current article and goes forwards unless
8434 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8435 If UNREAD is non-nil, only unread articles will
8436 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8437 in the comparisons. If NOT-MATCHING, return a list of all articles that
8438 not match REGEXP on HEADER."
8439   (let ((case-fold-search (not not-case-fold))
8440         articles d func)
8441     (if (consp header)
8442         (if (eq (car header) 'extra)
8443             (setq func
8444                   `(lambda (h)
8445                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8446                          "")))
8447           (error "%s is an invalid header" header))
8448       (unless (fboundp (intern (concat "mail-header-" header)))
8449         (error "%s is not a valid header" header))
8450       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8451     (dolist (d (if (eq backward 'all)
8452                    gnus-newsgroup-data
8453                  (gnus-data-find-list
8454                   (gnus-summary-article-number)
8455                   (gnus-data-list backward))))
8456       (when (and (or (not unread)       ; We want all articles...
8457                      (gnus-data-unread-p d)) ; Or just unreads.
8458                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8459                  (if not-matching
8460                      (not (string-match
8461                            regexp
8462                            (funcall func (gnus-data-header d))))
8463                    (string-match regexp
8464                                  (funcall func (gnus-data-header d)))))
8465         (push (gnus-data-number d) articles))) ; Success!
8466     (nreverse articles)))
8467
8468 (defun gnus-summary-execute-command (header regexp command &optional backward)
8469   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8470 If HEADER is an empty string (or nil), the match is done on the entire
8471 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8472   (interactive
8473    (list (let ((completion-ignore-case t))
8474            (completing-read
8475             "Header name: "
8476             (mapcar (lambda (header) (list (format "%s" header)))
8477                     (append
8478                      '("Number" "Subject" "From" "Lines" "Date"
8479                        "Message-ID" "Xref" "References" "Body")
8480                      gnus-extra-headers))
8481             nil 'require-match))
8482          (read-string "Regexp: ")
8483          (read-key-sequence "Command: ")
8484          current-prefix-arg))
8485   (when (equal header "Body")
8486     (setq header ""))
8487   ;; Hidden thread subtrees must be searched as well.
8488   (gnus-summary-show-all-threads)
8489   ;; We don't want to change current point nor window configuration.
8490   (save-excursion
8491     (save-window-excursion
8492       (let (gnus-visual
8493             gnus-treat-strip-trailing-blank-lines
8494             gnus-treat-strip-leading-blank-lines
8495             gnus-treat-strip-multiple-blank-lines
8496             gnus-treat-hide-boring-headers
8497             gnus-treat-fold-newsgroups
8498             gnus-article-prepare-hook)
8499         (gnus-message 6 "Executing %s..." (key-description command))
8500         ;; We'd like to execute COMMAND interactively so as to give arguments.
8501         (gnus-execute header regexp
8502                       `(call-interactively ',(key-binding command))
8503                       backward)
8504         (gnus-message 6 "Executing %s...done" (key-description command))))))
8505
8506 (defun gnus-summary-beginning-of-article ()
8507   "Scroll the article back to the beginning."
8508   (interactive)
8509   (gnus-summary-select-article)
8510   (gnus-configure-windows 'article)
8511   (gnus-eval-in-buffer-window gnus-article-buffer
8512     (widen)
8513     (goto-char (point-min))
8514     (when gnus-break-pages
8515       (gnus-narrow-to-page))))
8516
8517 (defun gnus-summary-end-of-article ()
8518   "Scroll to the end of the article."
8519   (interactive)
8520   (gnus-summary-select-article)
8521   (gnus-configure-windows 'article)
8522   (gnus-eval-in-buffer-window gnus-article-buffer
8523     (widen)
8524     (goto-char (point-max))
8525     (recenter -3)
8526     (when gnus-break-pages
8527       (when (re-search-backward page-delimiter nil t)
8528         (narrow-to-region (match-end 0) (point-max)))
8529       (gnus-narrow-to-page))))
8530
8531 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8532   "Truncate to LEN and quote all \"(\"'s in STRING."
8533   (gnus-replace-in-string (if (and len (> (length string) len))
8534                               (substring string 0 len)
8535                             string)
8536                           "[()]" "\\\\\\&"))
8537
8538 (defun gnus-summary-print-article (&optional filename n)
8539   "Generate and print a PostScript image of the process-marked (mail) articles.
8540
8541 If used interactively, print the current article if none are
8542 process-marked.  With prefix arg, prompt the user for the name of the
8543 file to save in.
8544
8545 When used from Lisp, accept two optional args FILENAME and N.  N means
8546 to print the next N articles.  If N is negative, print the N previous
8547 articles.  If N is nil and articles have been marked with the process
8548 mark, print these instead.
8549
8550 If the optional first argument FILENAME is nil, send the image to the
8551 printer.  If FILENAME is a string, save the PostScript image in a file with
8552 that name.  If FILENAME is a number, prompt the user for the name of the file
8553 to save in."
8554   (interactive (list (ps-print-preprint current-prefix-arg)))
8555   (dolist (article (gnus-summary-work-articles n))
8556     (gnus-summary-select-article nil nil 'pseudo article)
8557     (gnus-eval-in-buffer-window gnus-article-buffer
8558       (gnus-print-buffer))
8559     (gnus-summary-remove-process-mark article))
8560   (ps-despool filename))
8561
8562 (defun gnus-print-buffer ()
8563   (let ((buffer (generate-new-buffer " *print*")))
8564     (unwind-protect
8565         (progn
8566           (copy-to-buffer buffer (point-min) (point-max))
8567           (set-buffer buffer)
8568           (gnus-remove-text-with-property 'gnus-decoration)
8569           (when (gnus-visual-p 'article-highlight 'highlight)
8570             ;; Copy-to-buffer doesn't copy overlay.  So redo
8571             ;; highlight.
8572             (let ((gnus-article-buffer buffer))
8573               (gnus-article-highlight-citation t)
8574               (gnus-article-highlight-signature)
8575               (gnus-article-emphasize)
8576               (gnus-article-delete-invisible-text)))
8577           (let ((ps-left-header
8578                  (list
8579                   (concat "("
8580                           (gnus-summary-print-truncate-and-quote
8581                            (mail-header-subject gnus-current-headers)
8582                            66) ")")
8583                   (concat "("
8584                           (gnus-summary-print-truncate-and-quote
8585                            (mail-header-from gnus-current-headers)
8586                            45) ")")))
8587                 (ps-right-header
8588                  (list
8589                   "/pagenumberstring load"
8590                   (concat "("
8591                           (mail-header-date gnus-current-headers) ")"))))
8592             (gnus-run-hooks 'gnus-ps-print-hook)
8593             (save-excursion
8594               (if window-system
8595                   (ps-spool-buffer-with-faces)
8596                 (ps-spool-buffer)))))
8597       (kill-buffer buffer))))
8598
8599 (defun gnus-summary-show-article (&optional arg)
8600   "Force redisplaying of the current article.
8601 If ARG (the prefix) is a number, show the article with the charset
8602 defined in `gnus-summary-show-article-charset-alist', or the charset
8603 input.
8604 If ARG (the prefix) is non-nil and not a number, show the raw article
8605 without any article massaging functions being run.  Normally, the key
8606 strokes are `C-u g'."
8607   (interactive "P")
8608   (cond
8609    ((numberp arg)
8610     (gnus-summary-show-article t)
8611     (let ((gnus-newsgroup-charset
8612            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8613                (mm-read-coding-system
8614                 "View as charset: " ;; actually it is coding system.
8615                 (save-excursion
8616                   (set-buffer gnus-article-buffer)
8617                   (mm-detect-coding-region (point) (point-max))))))
8618           (gnus-newsgroup-ignored-charsets 'gnus-all))
8619       (gnus-summary-select-article nil 'force)
8620       (let ((deps gnus-newsgroup-dependencies)
8621             head header lines)
8622         (save-excursion
8623           (set-buffer gnus-original-article-buffer)
8624           (save-restriction
8625             (message-narrow-to-head)
8626             (setq head (buffer-string))
8627             (goto-char (point-min))
8628             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8629               (goto-char (point-max))
8630               (widen)
8631               (setq lines (1- (count-lines (point) (point-max))))))
8632           (with-temp-buffer
8633             (insert (format "211 %d Article retrieved.\n"
8634                             (cdr gnus-article-current)))
8635             (insert head)
8636             (if lines (insert (format "Lines: %d\n" lines)))
8637             (insert ".\n")
8638             (let ((nntp-server-buffer (current-buffer)))
8639               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8640         (gnus-data-set-header
8641          (gnus-data-find (cdr gnus-article-current))
8642          header)
8643         (gnus-summary-update-article-line
8644          (cdr gnus-article-current) header)
8645         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8646           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8647    ((not arg)
8648     ;; Select the article the normal way.
8649     (gnus-summary-select-article nil 'force))
8650    (t
8651     ;; We have to require this here to make sure that the following
8652     ;; dynamic binding isn't shadowed by autoloading.
8653     (require 'gnus-async)
8654     (require 'gnus-art)
8655     ;; Bind the article treatment functions to nil.
8656     (let ((gnus-have-all-headers t)
8657           gnus-article-prepare-hook
8658           gnus-article-decode-hook
8659           gnus-display-mime-function
8660           gnus-break-pages)
8661       ;; Destroy any MIME parts.
8662       (when (gnus-buffer-live-p gnus-article-buffer)
8663         (save-excursion
8664           (set-buffer gnus-article-buffer)
8665           (mm-destroy-parts gnus-article-mime-handles)
8666           ;; Set it to nil for safety reason.
8667           (setq gnus-article-mime-handle-alist nil)
8668           (setq gnus-article-mime-handles nil)))
8669       (gnus-summary-select-article nil 'force))))
8670   (gnus-summary-goto-subject gnus-current-article)
8671   (gnus-summary-position-point))
8672
8673 (defun gnus-summary-show-raw-article ()
8674   "Show the raw article without any article massaging functions being run."
8675   (interactive)
8676   (gnus-summary-show-article t))
8677
8678 (defun gnus-summary-verbose-headers (&optional arg)
8679   "Toggle permanent full header display.
8680 If ARG is a positive number, turn header display on.
8681 If ARG is a negative number, turn header display off."
8682   (interactive "P")
8683   (setq gnus-show-all-headers
8684         (cond ((or (not (numberp arg))
8685                    (zerop arg))
8686                (not gnus-show-all-headers))
8687               ((natnump arg)
8688                t)))
8689   (gnus-summary-show-article))
8690
8691 (defun gnus-summary-toggle-header (&optional arg)
8692   "Show the headers if they are hidden, or hide them if they are shown.
8693 If ARG is a positive number, show the entire header.
8694 If ARG is a negative number, hide the unwanted header lines."
8695   (interactive "P")
8696   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8697                      (get-buffer-window gnus-article-buffer t))))
8698     (with-current-buffer gnus-article-buffer
8699       (widen)
8700       (article-narrow-to-head)
8701       (let* ((buffer-read-only nil)
8702              (inhibit-point-motion-hooks t)
8703              (hidden (if (numberp arg)
8704                          (>= arg 0)
8705                        (gnus-article-hidden-text-p 'headers)))
8706              s e)
8707         (delete-region (point-min) (point-max))
8708         (with-current-buffer gnus-original-article-buffer
8709           (goto-char (setq s (point-min)))
8710           (setq e (if (search-forward "\n\n" nil t)
8711                       (1- (point))
8712                     (point-max))))
8713         (insert-buffer-substring gnus-original-article-buffer s e)
8714         (run-hooks 'gnus-article-decode-hook)
8715         (if hidden
8716             (let ((gnus-treat-hide-headers nil)
8717                   (gnus-treat-hide-boring-headers nil))
8718               (gnus-delete-wash-type 'headers)
8719               (gnus-treat-article 'head))
8720           (gnus-treat-article 'head))
8721         (widen)
8722         (if window
8723             (set-window-start window (goto-char (point-min))))
8724         (if gnus-break-pages
8725             (gnus-narrow-to-page)
8726           (when (gnus-visual-p 'page-marker)
8727             (let ((buffer-read-only nil))
8728               (gnus-remove-text-with-property 'gnus-prev)
8729               (gnus-remove-text-with-property 'gnus-next))))
8730         (gnus-set-mode-line 'article)))))
8731
8732 (defun gnus-summary-show-all-headers ()
8733   "Make all header lines visible."
8734   (interactive)
8735   (gnus-summary-toggle-header 1))
8736
8737 (defun gnus-summary-caesar-message (&optional arg)
8738   "Caesar rotate the current article by 13.
8739 The numerical prefix specifies how many places to rotate each letter
8740 forward."
8741   (interactive "P")
8742   (gnus-summary-select-article)
8743   (let ((mail-header-separator ""))
8744     (gnus-eval-in-buffer-window gnus-article-buffer
8745       (save-restriction
8746         (widen)
8747         (let ((start (window-start))
8748               buffer-read-only)
8749           (message-caesar-buffer-body arg)
8750           (set-window-start (get-buffer-window (current-buffer)) start))))))
8751
8752 (autoload 'unmorse-region "morse"
8753   "Convert morse coded text in region to ordinary ASCII text."
8754   t)
8755
8756 (defun gnus-summary-morse-message (&optional arg)
8757   "Morse decode the current article."
8758   (interactive "P")
8759   (gnus-summary-select-article)
8760   (let ((mail-header-separator ""))
8761     (gnus-eval-in-buffer-window gnus-article-buffer
8762       (save-excursion
8763         (save-restriction
8764           (widen)
8765           (let ((pos (window-start))
8766                 buffer-read-only)
8767             (goto-char (point-min))
8768             (when (message-goto-body)
8769               (gnus-narrow-to-body))
8770             (goto-char (point-min))
8771             (while (re-search-forward "·" (point-max) t)
8772               (replace-match "."))
8773             (unmorse-region (point-min) (point-max))
8774             (widen)
8775             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8776
8777 (defun gnus-summary-stop-page-breaking ()
8778   "Stop page breaking in the current article."
8779   (interactive)
8780   (gnus-summary-select-article)
8781   (gnus-eval-in-buffer-window gnus-article-buffer
8782     (widen)
8783     (when (gnus-visual-p 'page-marker)
8784       (let ((buffer-read-only nil))
8785         (gnus-remove-text-with-property 'gnus-prev)
8786         (gnus-remove-text-with-property 'gnus-next))
8787       (setq gnus-page-broken nil))))
8788
8789 (defun gnus-summary-move-article (&optional n to-newsgroup
8790                                             select-method action)
8791   "Move the current article to a different newsgroup.
8792 If N is a positive number, move the N next articles.
8793 If N is a negative number, move the N previous articles.
8794 If N is nil and any articles have been marked with the process mark,
8795 move those articles instead.
8796 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8797 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8798 re-spool using this method.
8799
8800 When called interactively with TO-NEWSGROUP being nil, the value of
8801 the variable `gnus-move-split-methods' is used for finding a default
8802 for the target newsgroup.
8803
8804 For this function to work, both the current newsgroup and the
8805 newsgroup that you want to move to have to support the `request-move'
8806 and `request-accept' functions.
8807
8808 ACTION can be either `move' (the default), `crosspost' or `copy'."
8809   (interactive "P")
8810   (unless action
8811     (setq action 'move))
8812   ;; Check whether the source group supports the required functions.
8813   (cond ((and (eq action 'move)
8814               (not (gnus-check-backend-function
8815                     'request-move-article gnus-newsgroup-name)))
8816          (error "The current group does not support article moving"))
8817         ((and (eq action 'crosspost)
8818               (not (gnus-check-backend-function
8819                     'request-replace-article gnus-newsgroup-name)))
8820          (error "The current group does not support article editing")))
8821   (let ((articles (gnus-summary-work-articles n))
8822         (prefix (if (gnus-check-backend-function
8823                      'request-move-article gnus-newsgroup-name)
8824                     (gnus-group-real-prefix gnus-newsgroup-name)
8825                   ""))
8826         (names '((move "Move" "Moving")
8827                  (copy "Copy" "Copying")
8828                  (crosspost "Crosspost" "Crossposting")))
8829         (copy-buf (save-excursion
8830                     (nnheader-set-temp-buffer " *copy article*")))
8831         art-group to-method new-xref article to-groups)
8832     (unless (assq action names)
8833       (error "Unknown action %s" action))
8834     ;; Read the newsgroup name.
8835     (when (and (not to-newsgroup)
8836                (not select-method))
8837       (if (and gnus-move-split-methods
8838                (not
8839                 (and (memq gnus-current-article articles)
8840                      (gnus-buffer-live-p gnus-original-article-buffer))))
8841           ;; When `gnus-move-split-methods' is non-nil, we have to
8842           ;; select an article to give `gnus-read-move-group-name' an
8843           ;; opportunity to suggest an appropriate default.  However,
8844           ;; we needn't render or mark the article.
8845           (let ((gnus-display-mime-function nil)
8846                 (gnus-article-prepare-hook nil)
8847                 (gnus-mark-article-hook nil))
8848             (gnus-summary-select-article nil nil nil (car articles))))
8849       (setq to-newsgroup
8850             (gnus-read-move-group-name
8851              (cadr (assq action names))
8852              (symbol-value (intern (format "gnus-current-%s-group" action)))
8853              articles prefix))
8854       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8855     (setq to-method (or select-method
8856                         (gnus-server-to-method
8857                          (gnus-group-method to-newsgroup))))
8858     ;; Check the method we are to move this article to...
8859     (unless (gnus-check-backend-function
8860              'request-accept-article (car to-method))
8861       (error "%s does not support article copying" (car to-method)))
8862     (unless (gnus-check-server to-method)
8863       (error "Can't open server %s" (car to-method)))
8864     (gnus-message 6 "%s to %s: %s..."
8865                   (caddr (assq action names))
8866                   (or (car select-method) to-newsgroup) articles)
8867     (while articles
8868       (setq article (pop articles))
8869       (setq
8870        art-group
8871        (cond
8872         ;; Move the article.
8873         ((eq action 'move)
8874          ;; Remove this article from future suppression.
8875          (gnus-dup-unsuppress-article article)
8876          (gnus-request-move-article
8877           article                       ; Article to move
8878           gnus-newsgroup-name           ; From newsgroup
8879           (nth 1 (gnus-find-method-for-group
8880                   gnus-newsgroup-name)) ; Server
8881           (list 'gnus-request-accept-article
8882                 to-newsgroup (list 'quote select-method)
8883                 (not articles) t)       ; Accept form
8884           (not articles)))              ; Only save nov last time
8885         ;; Copy the article.
8886         ((eq action 'copy)
8887          (save-excursion
8888            (set-buffer copy-buf)
8889            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8890              (gnus-request-accept-article
8891               to-newsgroup select-method (not articles) t))))
8892         ;; Crosspost the article.
8893         ((eq action 'crosspost)
8894          (let ((xref (message-tokenize-header
8895                       (mail-header-xref (gnus-summary-article-header article))
8896                       " ")))
8897            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8898                                   ":" (number-to-string article)))
8899            (unless xref
8900              (setq xref (list (system-name))))
8901            (setq new-xref
8902                  (concat
8903                   (mapconcat 'identity
8904                              (delete "Xref:" (delete new-xref xref))
8905                              " ")
8906                   " " new-xref))
8907            (save-excursion
8908              (set-buffer copy-buf)
8909              ;; First put the article in the destination group.
8910              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8911              (when (consp (setq art-group
8912                                 (gnus-request-accept-article
8913                                  to-newsgroup select-method (not articles))))
8914                (setq new-xref (concat new-xref " " (car art-group)
8915                                       ":"
8916                                       (number-to-string (cdr art-group))))
8917                ;; Now we have the new Xrefs header, so we insert
8918                ;; it and replace the new article.
8919                (nnheader-replace-header "Xref" new-xref)
8920                (gnus-request-replace-article
8921                 (cdr art-group) to-newsgroup (current-buffer))
8922                art-group))))))
8923       (cond
8924        ((not art-group)
8925         (gnus-message 1 "Couldn't %s article %s: %s"
8926                       (cadr (assq action names)) article
8927                       (nnheader-get-report (car to-method))))
8928        ((eq art-group 'junk)
8929         (when (eq action 'move)
8930           (gnus-summary-mark-article article gnus-canceled-mark)
8931           (gnus-message 4 "Deleted article %s" article)
8932           ;; run the delete hook
8933           (run-hook-with-args 'gnus-summary-article-delete-hook
8934                               action
8935                               (gnus-data-header
8936                                (assoc article (gnus-data-list nil)))
8937                               gnus-newsgroup-name nil
8938                               select-method)))
8939        (t
8940         (let* ((pto-group (gnus-group-prefixed-name
8941                            (car art-group) to-method))
8942                (entry
8943                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8944                (info (nth 2 entry))
8945                (to-group (gnus-info-group info))
8946                to-marks)
8947           ;; Update the group that has been moved to.
8948           (when (and info
8949                      (memq action '(move copy)))
8950             (unless (member to-group to-groups)
8951               (push to-group to-groups))
8952
8953             (unless (memq article gnus-newsgroup-unreads)
8954               (push 'read to-marks)
8955               (gnus-info-set-read
8956                info (gnus-add-to-range (gnus-info-read info)
8957                                        (list (cdr art-group)))))
8958
8959             ;; See whether the article is to be put in the cache.
8960             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8961                              gnus-article-mark-lists
8962                            (delete '(expirable . expire)
8963                                    (copy-sequence gnus-article-mark-lists))))
8964                   (to-article (cdr art-group)))
8965
8966               ;; Enter the article into the cache in the new group,
8967               ;; if that is required.
8968               (when gnus-use-cache
8969                 (gnus-cache-possibly-enter-article
8970                  to-group to-article
8971                  (memq article gnus-newsgroup-marked)
8972                  (memq article gnus-newsgroup-dormant)
8973                  (memq article gnus-newsgroup-unreads)))
8974
8975               (when gnus-preserve-marks
8976                 ;; Copy any marks over to the new group.
8977                 (when (and (equal to-group gnus-newsgroup-name)
8978                            (not (memq article gnus-newsgroup-unreads)))
8979                   ;; Mark this article as read in this group.
8980                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8981                   (setcdr (gnus-active to-group) to-article)
8982                   (setcdr gnus-newsgroup-active to-article))
8983
8984                 (while marks
8985                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8986                     (when (memq article (symbol-value
8987                                          (intern (format "gnus-newsgroup-%s"
8988                                                          (caar marks)))))
8989                       (push (cdar marks) to-marks)
8990                       ;; If the other group is the same as this group,
8991                       ;; then we have to add the mark to the list.
8992                       (when (equal to-group gnus-newsgroup-name)
8993                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8994                              (cons to-article
8995                                    (symbol-value
8996                                     (intern (format "gnus-newsgroup-%s"
8997                                                     (caar marks)))))))
8998                       ;; Copy the marks to other group.
8999                       (gnus-add-marked-articles
9000                        to-group (cdar marks) (list to-article) info)))
9001                   (setq marks (cdr marks)))
9002
9003                 (gnus-request-set-mark
9004                  to-group (list (list (list to-article) 'add to-marks))))
9005
9006               (gnus-dribble-enter
9007                (concat "(gnus-group-set-info '"
9008                        (gnus-prin1-to-string (gnus-get-info to-group))
9009                        ")"))))
9010
9011           ;; Update the Xref header in this article to point to
9012           ;; the new crossposted article we have just created.
9013           (when (eq action 'crosspost)
9014             (save-excursion
9015               (set-buffer copy-buf)
9016               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9017               (nnheader-replace-header "Xref" new-xref)
9018               (gnus-request-replace-article
9019                article gnus-newsgroup-name (current-buffer))))
9020
9021           ;; run the move/copy/crosspost/respool hook
9022           (run-hook-with-args 'gnus-summary-article-move-hook
9023                               action
9024                               (gnus-data-header
9025                                (assoc article (gnus-data-list nil)))
9026                               gnus-newsgroup-name
9027                               to-newsgroup
9028                               select-method))
9029
9030         ;;;!!!Why is this necessary?
9031         (set-buffer gnus-summary-buffer)
9032         
9033         (gnus-summary-goto-subject article)
9034         (when (eq action 'move)
9035           (gnus-summary-mark-article article gnus-canceled-mark))))
9036       (gnus-summary-remove-process-mark article))
9037     ;; Re-activate all groups that have been moved to.
9038     (save-excursion
9039       (set-buffer gnus-group-buffer)
9040       (let ((gnus-group-marked to-groups))
9041         (gnus-group-get-new-news-this-group nil t)))
9042
9043     (gnus-kill-buffer copy-buf)
9044     (gnus-summary-position-point)
9045     (gnus-set-mode-line 'summary)))
9046
9047 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9048   "Move the current article to a different newsgroup.
9049 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9050 When called interactively, if TO-NEWSGROUP is nil, use the value of
9051 the variable `gnus-move-split-methods' for finding a default target
9052 newsgroup.
9053 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9054 re-spool using this method."
9055   (interactive "P")
9056   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9057
9058 (defun gnus-summary-crosspost-article (&optional n)
9059   "Crosspost the current article to some other group."
9060   (interactive "P")
9061   (gnus-summary-move-article n nil nil 'crosspost))
9062
9063 (defcustom gnus-summary-respool-default-method nil
9064   "Default method type for respooling an article.
9065 If nil, use to the current newsgroup method."
9066   :type 'symbol
9067   :group 'gnus-summary-mail)
9068
9069 (defcustom gnus-summary-display-while-building nil
9070   "If non-nil, show and update the summary buffer as it's being built.
9071 If the value is t, update the buffer after every line is inserted.  If
9072 the value is an integer (N), update the display every N lines."
9073   :group 'gnus-thread
9074   :type '(choice (const :tag "off" nil)
9075                  number
9076                  (const :tag "frequently" t)))
9077
9078 (defun gnus-summary-respool-article (&optional n method)
9079   "Respool the current article.
9080 The article will be squeezed through the mail spooling process again,
9081 which means that it will be put in some mail newsgroup or other
9082 depending on `nnmail-split-methods'.
9083 If N is a positive number, respool the N next articles.
9084 If N is a negative number, respool the N previous articles.
9085 If N is nil and any articles have been marked with the process mark,
9086 respool those articles instead.
9087
9088 Respooling can be done both from mail groups and \"real\" newsgroups.
9089 In the former case, the articles in question will be moved from the
9090 current group into whatever groups they are destined to.  In the
9091 latter case, they will be copied into the relevant groups."
9092   (interactive
9093    (list current-prefix-arg
9094          (let* ((methods (gnus-methods-using 'respool))
9095                 (methname
9096                  (symbol-name (or gnus-summary-respool-default-method
9097                                   (car (gnus-find-method-for-group
9098                                         gnus-newsgroup-name)))))
9099                 (method
9100                  (gnus-completing-read-with-default
9101                   methname "What backend do you want to use when respooling?"
9102                   methods nil t nil 'gnus-mail-method-history))
9103                 ms)
9104            (cond
9105             ((zerop (length (setq ms (gnus-servers-using-backend
9106                                       (intern method)))))
9107              (list (intern method) ""))
9108             ((= 1 (length ms))
9109              (car ms))
9110             (t
9111              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9112                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9113                            ms-alist))))))))
9114   (unless method
9115     (error "No method given for respooling"))
9116   (if (assoc (symbol-name
9117               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9118              (gnus-methods-using 'respool))
9119       (gnus-summary-move-article n nil method)
9120     (gnus-summary-copy-article n nil method)))
9121
9122 (defun gnus-summary-import-article (file &optional edit)
9123   "Import an arbitrary file into a mail newsgroup."
9124   (interactive "fImport file: \nP")
9125   (let ((group gnus-newsgroup-name)
9126         (now (current-time))
9127         atts lines group-art)
9128     (unless (gnus-check-backend-function 'request-accept-article group)
9129       (error "%s does not support article importing" group))
9130     (or (file-readable-p file)
9131         (not (file-regular-p file))
9132         (error "Can't read %s" file))
9133     (save-excursion
9134       (set-buffer (gnus-get-buffer-create " *import file*"))
9135       (erase-buffer)
9136       (nnheader-insert-file-contents file)
9137       (goto-char (point-min))
9138       (if (nnheader-article-p)
9139           (save-restriction
9140             (goto-char (point-min))
9141             (search-forward "\n\n" nil t)
9142             (narrow-to-region (point-min) (1- (point)))
9143             (goto-char (point-min))
9144             (unless (re-search-forward "^date:" nil t)
9145               (goto-char (point-max))
9146               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9147        ;; This doesn't look like an article, so we fudge some headers.
9148         (setq atts (file-attributes file)
9149               lines (count-lines (point-min) (point-max)))
9150         (insert "From: " (read-string "From: ") "\n"
9151                 "Subject: " (read-string "Subject: ") "\n"
9152                 "Date: " (message-make-date (nth 5 atts)) "\n"
9153                 "Message-ID: " (message-make-message-id) "\n"
9154                 "Lines: " (int-to-string lines) "\n"
9155                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9156       (setq group-art (gnus-request-accept-article group nil t))
9157       (kill-buffer (current-buffer)))
9158     (setq gnus-newsgroup-active (gnus-activate-group group))
9159     (forward-line 1)
9160     (gnus-summary-goto-article (cdr group-art) nil t)
9161     (when edit
9162       (gnus-summary-edit-article))))
9163
9164 (defun gnus-summary-create-article ()
9165   "Create an article in a mail newsgroup."
9166   (interactive)
9167   (let ((group gnus-newsgroup-name)
9168         (now (current-time))
9169         group-art)
9170     (unless (gnus-check-backend-function 'request-accept-article group)
9171       (error "%s does not support article importing" group))
9172     (save-excursion
9173       (set-buffer (gnus-get-buffer-create " *import file*"))
9174       (erase-buffer)
9175       (goto-char (point-min))
9176       ;; This doesn't look like an article, so we fudge some headers.
9177       (insert "From: " (read-string "From: ") "\n"
9178               "Subject: " (read-string "Subject: ") "\n"
9179               "Date: " (message-make-date now) "\n"
9180               "Message-ID: " (message-make-message-id) "\n")
9181       (setq group-art (gnus-request-accept-article group nil t))
9182       (kill-buffer (current-buffer)))
9183     (setq gnus-newsgroup-active (gnus-activate-group group))
9184     (forward-line 1)
9185     (gnus-summary-goto-article (cdr group-art) nil t)
9186     (gnus-summary-edit-article)))
9187
9188 (defun gnus-summary-article-posted-p ()
9189   "Say whether the current (mail) article is available from news as well.
9190 This will be the case if the article has both been mailed and posted."
9191   (interactive)
9192   (let ((id (mail-header-references (gnus-summary-article-header)))
9193         (gnus-override-method (car (gnus-refer-article-methods))))
9194     (if (gnus-request-head id "")
9195         (gnus-message 2 "The current message was found on %s"
9196                       gnus-override-method)
9197       (gnus-message 2 "The current message couldn't be found on %s"
9198                     gnus-override-method)
9199       nil)))
9200
9201 (defun gnus-summary-expire-articles (&optional now)
9202   "Expire all articles that are marked as expirable in the current group."
9203   (interactive)
9204   (when (and (not gnus-group-is-exiting-without-update-p)
9205              (gnus-check-backend-function
9206               'request-expire-articles gnus-newsgroup-name))
9207     ;; This backend supports expiry.
9208     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9209            (expirable (if total
9210                           (progn
9211                             ;; We need to update the info for
9212                             ;; this group for `gnus-list-of-read-articles'
9213                             ;; to give us the right answer.
9214                             (gnus-run-hooks 'gnus-exit-group-hook)
9215                             (gnus-summary-update-info)
9216                             (gnus-list-of-read-articles gnus-newsgroup-name))
9217                         (setq gnus-newsgroup-expirable
9218                               (sort gnus-newsgroup-expirable '<))))
9219            (expiry-wait (if now 'immediate
9220                           (gnus-group-find-parameter
9221                            gnus-newsgroup-name 'expiry-wait)))
9222            (nnmail-expiry-target
9223             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9224                 nnmail-expiry-target))
9225            es)
9226       (when expirable
9227         ;; There are expirable articles in this group, so we run them
9228         ;; through the expiry process.
9229         (gnus-message 6 "Expiring articles...")
9230         (unless (gnus-check-group gnus-newsgroup-name)
9231           (error "Can't open server for %s" gnus-newsgroup-name))
9232         ;; The list of articles that weren't expired is returned.
9233         (save-excursion
9234           (if expiry-wait
9235               (let ((nnmail-expiry-wait-function nil)
9236                     (nnmail-expiry-wait expiry-wait))
9237                 (setq es (gnus-request-expire-articles
9238                           expirable gnus-newsgroup-name)))
9239             (setq es (gnus-request-expire-articles
9240                       expirable gnus-newsgroup-name)))
9241           (unless total
9242             (setq gnus-newsgroup-expirable es))
9243           ;; We go through the old list of expirable, and mark all
9244           ;; really expired articles as nonexistent.
9245           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9246             (let ((gnus-use-cache nil))
9247               (dolist (article expirable)
9248                 (when (and (not (memq article es))
9249                            (gnus-data-find article))
9250                   (gnus-summary-mark-article article gnus-canceled-mark)
9251                   (run-hook-with-args 'gnus-summary-article-expire-hook
9252                                       'delete
9253                                       (gnus-data-header
9254                                        (assoc article (gnus-data-list nil)))
9255                                       gnus-newsgroup-name
9256                                       nil
9257                                       nil))))))
9258         (gnus-message 6 "Expiring articles...done")))))
9259
9260 (defun gnus-summary-expire-articles-now ()
9261   "Expunge all expirable articles in the current group.
9262 This means that *all* articles that are marked as expirable will be
9263 deleted forever, right now."
9264   (interactive)
9265   (or gnus-expert-user
9266       (gnus-yes-or-no-p
9267        "Are you really, really, really sure you want to delete all these messages? ")
9268       (error "Phew!"))
9269   (gnus-summary-expire-articles t))
9270
9271 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9272 (defun gnus-summary-delete-article (&optional n)
9273   "Delete the N next (mail) articles.
9274 This command actually deletes articles.  This is not a marking
9275 command.  The article will disappear forever from your life, never to
9276 return.
9277
9278 If N is negative, delete backwards.
9279 If N is nil and articles have been marked with the process mark,
9280 delete these instead.
9281
9282 If `gnus-novice-user' is non-nil you will be asked for
9283 confirmation before the articles are deleted."
9284   (interactive "P")
9285   (unless (gnus-check-backend-function 'request-expire-articles
9286                                        gnus-newsgroup-name)
9287     (error "The current newsgroup does not support article deletion"))
9288   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9289     (error "Couldn't open server"))
9290   ;; Compute the list of articles to delete.
9291   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9292         (nnmail-expiry-target 'delete)
9293         not-deleted)
9294     (if (and gnus-novice-user
9295              (not (gnus-yes-or-no-p
9296                    (format "Do you really want to delete %s forever? "
9297                            (if (> (length articles) 1)
9298                                (format "these %s articles" (length articles))
9299                              "this article")))))
9300         ()
9301       ;; Delete the articles.
9302       (setq not-deleted (gnus-request-expire-articles
9303                          articles gnus-newsgroup-name 'force))
9304       (while articles
9305         (gnus-summary-remove-process-mark (car articles))
9306         ;; The backend might not have been able to delete the article
9307         ;; after all.
9308         (unless (memq (car articles) not-deleted)
9309           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9310         (let* ((article (car articles))
9311                (id (mail-header-id (gnus-data-header
9312                                     (assoc article (gnus-data-list nil))))))
9313           (run-hook-with-args 'gnus-summary-article-delete-hook
9314                               'delete id gnus-newsgroup-name nil
9315                               nil))
9316         (setq articles (cdr articles)))
9317       (when not-deleted
9318         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9319     (gnus-summary-position-point)
9320     (gnus-set-mode-line 'summary)
9321     not-deleted))
9322
9323 (defun gnus-summary-edit-article (&optional arg)
9324   "Edit the current article.
9325 This will have permanent effect only in mail groups.
9326 If ARG is nil, edit the decoded articles.
9327 If ARG is 1, edit the raw articles.
9328 If ARG is 2, edit the raw articles even in read-only groups.
9329 If ARG is 3, edit the articles with the current handles.
9330 Otherwise, allow editing of articles even in read-only
9331 groups."
9332   (interactive "P")
9333   (let (force raw current-handles)
9334     (cond
9335      ((null arg))
9336      ((eq arg 1)
9337       (setq raw t))
9338      ((eq arg 2)
9339       (setq raw t
9340             force t))
9341      ((eq arg 3)
9342       (setq current-handles
9343             (and (gnus-buffer-live-p gnus-article-buffer)
9344                  (with-current-buffer gnus-article-buffer
9345                    (prog1
9346                        gnus-article-mime-handles
9347                      (setq gnus-article-mime-handles nil))))))
9348      (t
9349       (setq force t)))
9350     (when (and raw (not force)
9351                (member gnus-newsgroup-name '("nndraft:delayed"
9352                                              "nndraft:drafts"
9353                                              "nndraft:queue")))
9354       (error "Can't edit the raw article in group %s"
9355              gnus-newsgroup-name))
9356     (save-excursion
9357       (set-buffer gnus-summary-buffer)
9358       (let ((mail-parse-charset gnus-newsgroup-charset)
9359             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9360         (gnus-set-global-variables)
9361         (when (and (not force)
9362                    (gnus-group-read-only-p))
9363           (error "The current newsgroup does not support article editing"))
9364         (gnus-summary-show-article t)
9365         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9366           (with-current-buffer gnus-article-buffer
9367             (mm-enable-multibyte)))
9368         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9369             (setq raw t))
9370         (gnus-article-edit-article
9371          (if raw 'ignore
9372            `(lambda ()
9373               (let ((mbl mml-buffer-list))
9374                 (setq mml-buffer-list nil)
9375                 (mime-to-mml ,'current-handles)
9376                 (let ((mbl1 mml-buffer-list))
9377                   (setq mml-buffer-list mbl)
9378                   (set (make-local-variable 'mml-buffer-list) mbl1))
9379                 (gnus-make-local-hook 'kill-buffer-hook)
9380                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9381          `(lambda (no-highlight)
9382             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9383                   (message-options message-options)
9384                   (message-options-set-recipient)
9385                   (mail-parse-ignored-charsets
9386                    ',gnus-newsgroup-ignored-charsets))
9387               ,(if (not raw) '(progn
9388                                 (mml-to-mime)
9389                                 (mml-destroy-buffers)
9390                                 (remove-hook 'kill-buffer-hook
9391                                              'mml-destroy-buffers t)
9392                                 (kill-local-variable 'mml-buffer-list)))
9393               (gnus-summary-edit-article-done
9394                ,(or (mail-header-references gnus-current-headers) "")
9395                ,(gnus-group-read-only-p)
9396                ,gnus-summary-buffer no-highlight))))))))
9397
9398 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9399
9400 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9401                                                  no-highlight)
9402   "Make edits to the current article permanent."
9403   (interactive)
9404   (save-excursion
9405    ;; The buffer restriction contains the entire article if it exists.
9406     (when (article-goto-body)
9407       (let ((lines (count-lines (point) (point-max)))
9408             (length (- (point-max) (point)))
9409             (case-fold-search t)
9410             (body (copy-marker (point))))
9411         (goto-char (point-min))
9412         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9413           (delete-region (match-beginning 1) (match-end 1))
9414           (insert (number-to-string length)))
9415         (goto-char (point-min))
9416         (when (re-search-forward
9417                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9418           (delete-region (match-beginning 1) (match-end 1))
9419           (insert (number-to-string length)))
9420         (goto-char (point-min))
9421         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9422           (delete-region (match-beginning 1) (match-end 1))
9423           (insert (number-to-string lines))))))
9424   ;; Replace the article.
9425   (let ((buf (current-buffer)))
9426     (with-temp-buffer
9427       (insert-buffer-substring buf)
9428
9429       (if (and (not read-only)
9430                (not (gnus-request-replace-article
9431                      (cdr gnus-article-current) (car gnus-article-current)
9432                      (current-buffer) t)))
9433           (error "Couldn't replace article")
9434         ;; Update the summary buffer.
9435         (if (and references
9436                  (equal (message-tokenize-header references " ")
9437                         (message-tokenize-header
9438                          (or (message-fetch-field "references") "") " ")))
9439             ;; We only have to update this line.
9440             (save-excursion
9441               (save-restriction
9442                 (message-narrow-to-head)
9443                 (let ((head (buffer-string))
9444                       header)
9445                   (with-temp-buffer
9446                     (insert (format "211 %d Article retrieved.\n"
9447                                     (cdr gnus-article-current)))
9448                     (insert head)
9449                     (insert ".\n")
9450                     (let ((nntp-server-buffer (current-buffer)))
9451                       (setq header (car (gnus-get-newsgroup-headers
9452                                          nil t))))
9453                     (save-excursion
9454                       (set-buffer gnus-summary-buffer)
9455                       (gnus-data-set-header
9456                        (gnus-data-find (cdr gnus-article-current))
9457                        header)
9458                       (gnus-summary-update-article-line
9459                        (cdr gnus-article-current) header)
9460                       (if (gnus-summary-goto-subject
9461                            (cdr gnus-article-current) nil t)
9462                           (gnus-summary-update-secondary-mark
9463                            (cdr gnus-article-current))))))))
9464           ;; Update threads.
9465           (set-buffer (or buffer gnus-summary-buffer))
9466           (gnus-summary-update-article (cdr gnus-article-current))
9467           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9468               (gnus-summary-update-secondary-mark
9469                (cdr gnus-article-current))))
9470         ;; Prettify the article buffer again.
9471         (unless no-highlight
9472           (save-excursion
9473             (set-buffer gnus-article-buffer)
9474             ;;;!!! Fix this -- article should be rehighlighted.
9475             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9476             (set-buffer gnus-original-article-buffer)
9477             (gnus-request-article
9478              (cdr gnus-article-current)
9479              (car gnus-article-current) (current-buffer))))
9480         ;; Prettify the summary buffer line.
9481         (when (gnus-visual-p 'summary-highlight 'highlight)
9482           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9483
9484 (defun gnus-summary-edit-wash (key)
9485   "Perform editing command KEY in the article buffer."
9486   (interactive
9487    (list
9488     (progn
9489       (message "%s" (concat (this-command-keys) "- "))
9490       (read-char))))
9491   (message "")
9492   (gnus-summary-edit-article)
9493   (execute-kbd-macro (concat (this-command-keys) key))
9494   (gnus-article-edit-done))
9495
9496 ;;; Respooling
9497
9498 (defun gnus-summary-respool-query (&optional silent trace)
9499   "Query where the respool algorithm would put this article."
9500   (interactive)
9501   (let (gnus-mark-article-hook)
9502     (gnus-summary-select-article)
9503     (save-excursion
9504       (set-buffer gnus-original-article-buffer)
9505       (let ((groups (nnmail-article-group 'identity trace)))
9506         (unless silent
9507           (if groups
9508               (message "This message would go to %s"
9509                        (mapconcat 'car groups ", "))
9510             (message "This message would go to no groups"))
9511           groups)))))
9512
9513 (defun gnus-summary-respool-trace ()
9514   "Trace where the respool algorithm would put this article.
9515 Display a buffer showing all fancy splitting patterns which matched."
9516   (interactive)
9517   (gnus-summary-respool-query nil t))
9518
9519 ;; Summary marking commands.
9520
9521 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9522   "Mark articles which has the same subject as read, and then select the next.
9523 If UNMARK is positive, remove any kind of mark.
9524 If UNMARK is negative, tick articles."
9525   (interactive "P")
9526   (when unmark
9527     (setq unmark (prefix-numeric-value unmark)))
9528   (let ((count
9529          (gnus-summary-mark-same-subject
9530           (gnus-summary-article-subject) unmark)))
9531     ;; Select next unread article.  If auto-select-same mode, should
9532     ;; select the first unread article.
9533     (gnus-summary-next-article t (and gnus-auto-select-same
9534                                       (gnus-summary-article-subject)))
9535     (gnus-message 7 "%d article%s marked as %s"
9536                   count (if (= count 1) " is" "s are")
9537                   (if unmark "unread" "read"))))
9538
9539 (defun gnus-summary-kill-same-subject (&optional unmark)
9540   "Mark articles which has the same subject as read.
9541 If UNMARK is positive, remove any kind of mark.
9542 If UNMARK is negative, tick articles."
9543   (interactive "P")
9544   (when unmark
9545     (setq unmark (prefix-numeric-value unmark)))
9546   (let ((count
9547          (gnus-summary-mark-same-subject
9548           (gnus-summary-article-subject) unmark)))
9549     ;; If marked as read, go to next unread subject.
9550     (when (null unmark)
9551       ;; Go to next unread subject.
9552       (gnus-summary-next-subject 1 t))
9553     (gnus-message 7 "%d articles are marked as %s"
9554                   count (if unmark "unread" "read"))))
9555
9556 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9557   "Mark articles with same SUBJECT as read, and return marked number.
9558 If optional argument UNMARK is positive, remove any kinds of marks.
9559 If optional argument UNMARK is negative, mark articles as unread instead."
9560   (let ((count 1))
9561     (save-excursion
9562       (cond
9563        ((null unmark)                   ; Mark as read.
9564         (while (and
9565                 (progn
9566                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9567                   (gnus-summary-show-thread) t)
9568                 (gnus-summary-find-subject subject))
9569           (setq count (1+ count))))
9570        ((> unmark 0)                    ; Tick.
9571         (while (and
9572                 (progn
9573                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9574                   (gnus-summary-show-thread) t)
9575                 (gnus-summary-find-subject subject))
9576           (setq count (1+ count))))
9577        (t                               ; Mark as unread.
9578         (while (and
9579                 (progn
9580                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9581                   (gnus-summary-show-thread) t)
9582                 (gnus-summary-find-subject subject))
9583           (setq count (1+ count)))))
9584       (gnus-set-mode-line 'summary)
9585       ;; Return the number of marked articles.
9586       count)))
9587
9588 (defun gnus-summary-mark-as-processable (n &optional unmark)
9589   "Set the process mark on the next N articles.
9590 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9591 the process mark instead.  The difference between N and the actual
9592 number of articles marked is returned."
9593   (interactive "P")
9594   (if (and (null n) (gnus-region-active-p))
9595       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9596     (setq n (prefix-numeric-value n))
9597     (let ((backward (< n 0))
9598           (n (abs n)))
9599       (while (and
9600               (> n 0)
9601               (if unmark
9602                   (gnus-summary-remove-process-mark
9603                    (gnus-summary-article-number))
9604                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9605               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9606         (setq n (1- n)))
9607       (when (/= 0 n)
9608         (gnus-message 7 "No more articles"))
9609       (gnus-summary-recenter)
9610       (gnus-summary-position-point)
9611       n)))
9612
9613 (defun gnus-summary-unmark-as-processable (n)
9614   "Remove the process mark from the next N articles.
9615 If N is negative, unmark backward instead.  The difference between N and
9616 the actual number of articles unmarked is returned."
9617   (interactive "P")
9618   (gnus-summary-mark-as-processable n t))
9619
9620 (defun gnus-summary-unmark-all-processable ()
9621   "Remove the process mark from all articles."
9622   (interactive)
9623   (save-excursion
9624     (while gnus-newsgroup-processable
9625       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9626   (gnus-summary-position-point))
9627
9628 (defun gnus-summary-add-mark (article type)
9629   "Mark ARTICLE with a mark of TYPE."
9630   (let ((vtype (car (assq type gnus-article-mark-lists)))
9631         var)
9632     (if (not vtype)
9633         (error "No such mark type: %s" type)
9634       (setq var (intern (format "gnus-newsgroup-%s" type)))
9635       (set var (cons article (symbol-value var)))
9636       (if (memq type '(processable cached replied forwarded recent saved))
9637           (gnus-summary-update-secondary-mark article)
9638         ;;; !!! This is bogus.  We should find out what primary
9639         ;;; !!! mark we want to set.
9640         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9641
9642 (defun gnus-summary-mark-as-expirable (n)
9643   "Mark N articles forward as expirable.
9644 If N is negative, mark backward instead.  The difference between N and
9645 the actual number of articles marked is returned."
9646   (interactive "p")
9647   (gnus-summary-mark-forward n gnus-expirable-mark))
9648
9649 (defun gnus-summary-mark-as-spam (n)
9650   "Mark N articles forward as spam.
9651 If N is negative, mark backward instead.  The difference between N and
9652 the actual number of articles marked is returned."
9653   (interactive "p")
9654   (gnus-summary-mark-forward n gnus-spam-mark))
9655
9656 (defun gnus-summary-mark-article-as-replied (article)
9657   "Mark ARTICLE as replied to and update the summary line.
9658 ARTICLE can also be a list of articles."
9659   (interactive (list (gnus-summary-article-number)))
9660   (let ((articles (if (listp article) article (list article))))
9661     (dolist (article articles)
9662       (unless (numberp article)
9663         (error "%s is not a number" article))
9664       (push article gnus-newsgroup-replied)
9665       (let ((buffer-read-only nil))
9666         (when (gnus-summary-goto-subject article nil t)
9667           (gnus-summary-update-secondary-mark article))))))
9668
9669 (defun gnus-summary-mark-article-as-forwarded (article)
9670   "Mark ARTICLE as forwarded and update the summary line.
9671 ARTICLE can also be a list of articles."
9672   (let ((articles (if (listp article) article (list article))))
9673     (dolist (article articles)
9674       (push article gnus-newsgroup-forwarded)
9675       (let ((buffer-read-only nil))
9676         (when (gnus-summary-goto-subject article nil t)
9677           (gnus-summary-update-secondary-mark article))))))
9678
9679 (defun gnus-summary-set-bookmark (article)
9680   "Set a bookmark in current article."
9681   (interactive (list (gnus-summary-article-number)))
9682   (when (or (not (get-buffer gnus-article-buffer))
9683             (not gnus-current-article)
9684             (not gnus-article-current)
9685             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9686     (error "No current article selected"))
9687   ;; Remove old bookmark, if one exists.
9688   (gnus-pull article gnus-newsgroup-bookmarks)
9689   ;; Set the new bookmark, which is on the form
9690   ;; (article-number . line-number-in-body).
9691   (push
9692    (cons article
9693          (save-excursion
9694            (set-buffer gnus-article-buffer)
9695            (count-lines
9696             (min (point)
9697                  (save-excursion
9698                    (article-goto-body)
9699                    (point)))
9700             (point))))
9701    gnus-newsgroup-bookmarks)
9702   (gnus-message 6 "A bookmark has been added to the current article."))
9703
9704 (defun gnus-summary-remove-bookmark (article)
9705   "Remove the bookmark from the current article."
9706   (interactive (list (gnus-summary-article-number)))
9707   ;; Remove old bookmark, if one exists.
9708   (if (not (assq article gnus-newsgroup-bookmarks))
9709       (gnus-message 6 "No bookmark in current article.")
9710     (gnus-pull article gnus-newsgroup-bookmarks)
9711     (gnus-message 6 "Removed bookmark.")))
9712
9713 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9714 (defun gnus-summary-mark-as-dormant (n)
9715   "Mark N articles forward as dormant.
9716 If N is negative, mark backward instead.  The difference between N and
9717 the actual number of articles marked is returned."
9718   (interactive "p")
9719   (gnus-summary-mark-forward n gnus-dormant-mark))
9720
9721 (defun gnus-summary-set-process-mark (article)
9722   "Set the process mark on ARTICLE and update the summary line."
9723   (setq gnus-newsgroup-processable
9724         (cons article
9725               (delq article gnus-newsgroup-processable)))
9726   (when (gnus-summary-goto-subject article)
9727     (gnus-summary-show-thread)
9728     (gnus-summary-goto-subject article)
9729     (gnus-summary-update-secondary-mark article)))
9730
9731 (defun gnus-summary-remove-process-mark (article)
9732   "Remove the process mark from ARTICLE and update the summary line."
9733   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9734   (when (gnus-summary-goto-subject article)
9735     (gnus-summary-show-thread)
9736     (gnus-summary-goto-subject article)
9737     (gnus-summary-update-secondary-mark article)))
9738
9739 (defun gnus-summary-set-saved-mark (article)
9740   "Set the process mark on ARTICLE and update the summary line."
9741   (push article gnus-newsgroup-saved)
9742   (when (gnus-summary-goto-subject article)
9743     (gnus-summary-update-secondary-mark article)))
9744
9745 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9746   "Mark N articles as read forwards.
9747 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9748 The difference between N and the actual number of articles marked is
9749 returned.
9750 If NO-EXPIRE, auto-expiry will be inhibited."
9751   (interactive "p")
9752   (gnus-summary-show-thread)
9753   (let ((backward (< n 0))
9754         (gnus-summary-goto-unread
9755          (and gnus-summary-goto-unread
9756               (not (eq gnus-summary-goto-unread 'never))
9757               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9758                                     gnus-ticked-mark gnus-dormant-mark)))))
9759         (n (abs n))
9760         (mark (or mark gnus-del-mark)))
9761     (while (and (> n 0)
9762                 (gnus-summary-mark-article nil mark no-expire)
9763                 (zerop (gnus-summary-next-subject
9764                         (if backward -1 1)
9765                         (and gnus-summary-goto-unread
9766                              (not (eq gnus-summary-goto-unread 'never)))
9767                         t)))
9768       (setq n (1- n)))
9769     (when (/= 0 n)
9770       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9771     (gnus-summary-recenter)
9772     (gnus-summary-position-point)
9773     (gnus-set-mode-line 'summary)
9774     n))
9775
9776 (defun gnus-summary-mark-article-as-read (mark)
9777   "Mark the current article quickly as read with MARK."
9778   (let ((article (gnus-summary-article-number)))
9779     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9780     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9781     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9782     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9783     (push (cons article mark) gnus-newsgroup-reads)
9784     ;; Possibly remove from cache, if that is used.
9785     (when gnus-use-cache
9786       (gnus-cache-enter-remove-article article))
9787     ;; Allow the backend to change the mark.
9788     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9789     ;; Check for auto-expiry.
9790     (when (and gnus-newsgroup-auto-expire
9791                (memq mark gnus-auto-expirable-marks))
9792       (setq mark gnus-expirable-mark)
9793       ;; Let the backend know about the mark change.
9794       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9795       (push article gnus-newsgroup-expirable))
9796     ;; Set the mark in the buffer.
9797     (gnus-summary-update-mark mark 'unread)
9798     t))
9799
9800 (defun gnus-summary-mark-article-as-unread (mark)
9801   "Mark the current article quickly as unread with MARK."
9802   (let* ((article (gnus-summary-article-number))
9803          (old-mark (gnus-summary-article-mark article)))
9804     ;; Allow the backend to change the mark.
9805     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9806     (if (eq mark old-mark)
9807         t
9808       (if (<= article 0)
9809           (progn
9810             (gnus-error 1 "Can't mark negative article numbers")
9811             nil)
9812         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9813         (setq gnus-newsgroup-spam-marked
9814               (delq article gnus-newsgroup-spam-marked))
9815         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9816         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9817         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9818         (cond ((= mark gnus-ticked-mark)
9819                (setq gnus-newsgroup-marked
9820                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9821                                               article)))
9822               ((= mark gnus-spam-mark)
9823                (setq gnus-newsgroup-spam-marked
9824                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9825                                               article)))
9826               ((= mark gnus-dormant-mark)
9827                (setq gnus-newsgroup-dormant
9828                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9829                                               article)))
9830               (t
9831                (setq gnus-newsgroup-unreads
9832                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9833                                               article))))
9834         (gnus-pull article gnus-newsgroup-reads)
9835
9836         ;; See whether the article is to be put in the cache.
9837         (and gnus-use-cache
9838              (vectorp (gnus-summary-article-header article))
9839              (save-excursion
9840                (gnus-cache-possibly-enter-article
9841                 gnus-newsgroup-name article
9842                 (= mark gnus-ticked-mark)
9843                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9844
9845         ;; Fix the mark.
9846         (gnus-summary-update-mark mark 'unread)
9847         t))))
9848
9849 (defun gnus-summary-mark-article (&optional article mark no-expire)
9850   "Mark ARTICLE with MARK.  MARK can be any character.
9851 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9852 `??' (dormant) and `?E' (expirable).
9853 If MARK is nil, then the default character `?r' is used.
9854 If ARTICLE is nil, then the article on the current line will be
9855 marked.
9856 If NO-EXPIRE, auto-expiry will be inhibited."
9857   ;; The mark might be a string.
9858   (when (stringp mark)
9859     (setq mark (aref mark 0)))
9860   ;; If no mark is given, then we check auto-expiring.
9861   (when (null mark)
9862     (setq mark gnus-del-mark))
9863   (when (and (not no-expire)
9864              gnus-newsgroup-auto-expire
9865              (memq mark gnus-auto-expirable-marks))
9866     (setq mark gnus-expirable-mark))
9867   (let ((article (or article (gnus-summary-article-number)))
9868         (old-mark (gnus-summary-article-mark article)))
9869     ;; Allow the backend to change the mark.
9870     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9871     (if (eq mark old-mark)
9872         t
9873       (unless article
9874         (error "No article on current line"))
9875       (if (not (if (or (= mark gnus-unread-mark)
9876                        (= mark gnus-ticked-mark)
9877                        (= mark gnus-spam-mark)
9878                        (= mark gnus-dormant-mark))
9879                    (gnus-mark-article-as-unread article mark)
9880                  (gnus-mark-article-as-read article mark)))
9881           t
9882         ;; See whether the article is to be put in the cache.
9883         (and gnus-use-cache
9884              (not (= mark gnus-canceled-mark))
9885              (vectorp (gnus-summary-article-header article))
9886              (save-excursion
9887                (gnus-cache-possibly-enter-article
9888                 gnus-newsgroup-name article
9889                 (= mark gnus-ticked-mark)
9890                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9891
9892         (when (gnus-summary-goto-subject article nil t)
9893           (let ((buffer-read-only nil))
9894             (gnus-summary-show-thread)
9895             ;; Fix the mark.
9896             (gnus-summary-update-mark mark 'unread)
9897             t))))))
9898
9899 (defun gnus-summary-update-secondary-mark (article)
9900   "Update the secondary (read, process, cache) mark."
9901   (gnus-summary-update-mark
9902    (cond ((memq article gnus-newsgroup-processable)
9903           gnus-process-mark)
9904          ((memq article gnus-newsgroup-cached)
9905           gnus-cached-mark)
9906          ((memq article gnus-newsgroup-replied)
9907           gnus-replied-mark)
9908          ((memq article gnus-newsgroup-forwarded)
9909           gnus-forwarded-mark)
9910          ((memq article gnus-newsgroup-saved)
9911           gnus-saved-mark)
9912          ((memq article gnus-newsgroup-recent)
9913           gnus-recent-mark)
9914          ((memq article gnus-newsgroup-unseen)
9915           gnus-unseen-mark)
9916          (t gnus-no-mark))
9917    'replied)
9918   (when (gnus-visual-p 'summary-highlight 'highlight)
9919     (gnus-run-hooks 'gnus-summary-update-hook))
9920   t)
9921
9922 (defun gnus-summary-update-download-mark (article)
9923   "Update the download mark."
9924   (gnus-summary-update-mark
9925    (cond ((memq article gnus-newsgroup-undownloaded)
9926           gnus-undownloaded-mark)
9927          (gnus-newsgroup-agentized
9928           gnus-downloaded-mark)
9929          (t
9930           gnus-no-mark))
9931    'download)
9932   (gnus-summary-update-line t)
9933   t)
9934
9935 (defun gnus-summary-update-mark (mark type)
9936   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9937         (buffer-read-only nil))
9938     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
9939     (when forward
9940       (when (looking-at "\r")
9941         (incf forward))
9942       (when (<= (+ forward (point)) (point-max))
9943         ;; Go to the right position on the line.
9944         (goto-char (+ forward (point)))
9945         ;; Replace the old mark with the new mark.
9946         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9947         ;; Optionally update the marks by some user rule.
9948         (when (eq type 'unread)
9949           (gnus-data-set-mark
9950            (gnus-data-find (gnus-summary-article-number)) mark)
9951           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9952
9953 (defun gnus-mark-article-as-read (article &optional mark)
9954   "Enter ARTICLE in the pertinent lists and remove it from others."
9955   ;; Make the article expirable.
9956   (let ((mark (or mark gnus-del-mark)))
9957     (setq gnus-newsgroup-expirable
9958           (if (= mark gnus-expirable-mark)
9959               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9960             (delq article gnus-newsgroup-expirable)))
9961     ;; Remove from unread and marked lists.
9962     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9963     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9964     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9965     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9966     (push (cons article mark) gnus-newsgroup-reads)
9967     ;; Possibly remove from cache, if that is used.
9968     (when gnus-use-cache
9969       (gnus-cache-enter-remove-article article))
9970     t))
9971
9972 (defun gnus-mark-article-as-unread (article &optional mark)
9973   "Enter ARTICLE in the pertinent lists and remove it from others."
9974   (let ((mark (or mark gnus-ticked-mark)))
9975     (if (<= article 0)
9976         (progn
9977           (gnus-error 1 "Can't mark negative article numbers")
9978           nil)
9979       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9980             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9981             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9982             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9983             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9984
9985       ;; Unsuppress duplicates?
9986       (when gnus-suppress-duplicates
9987         (gnus-dup-unsuppress-article article))
9988
9989       (cond ((= mark gnus-ticked-mark)
9990              (setq gnus-newsgroup-marked
9991                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9992             ((= mark gnus-spam-mark)
9993              (setq gnus-newsgroup-spam-marked
9994                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9995                                             article)))
9996             ((= mark gnus-dormant-mark)
9997              (setq gnus-newsgroup-dormant
9998                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9999             (t
10000              (setq gnus-newsgroup-unreads
10001                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10002       (gnus-pull article gnus-newsgroup-reads)
10003       t)))
10004
10005 (defalias 'gnus-summary-mark-as-unread-forward
10006   'gnus-summary-tick-article-forward)
10007 (make-obsolete 'gnus-summary-mark-as-unread-forward
10008                'gnus-summary-tick-article-forward)
10009 (defun gnus-summary-tick-article-forward (n)
10010   "Tick N articles forwards.
10011 If N is negative, tick backwards instead.
10012 The difference between N and the number of articles ticked is returned."
10013   (interactive "p")
10014   (gnus-summary-mark-forward n gnus-ticked-mark))
10015
10016 (defalias 'gnus-summary-mark-as-unread-backward
10017   'gnus-summary-tick-article-backward)
10018 (make-obsolete 'gnus-summary-mark-as-unread-backward
10019                'gnus-summary-tick-article-backward)
10020 (defun gnus-summary-tick-article-backward (n)
10021   "Tick N articles backwards.
10022 The difference between N and the number of articles ticked is returned."
10023   (interactive "p")
10024   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10025
10026 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10027 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10028 (defun gnus-summary-tick-article (&optional article clear-mark)
10029   "Mark current article as unread.
10030 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10031 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10032   (interactive)
10033   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10034                                        gnus-ticked-mark)))
10035
10036 (defun gnus-summary-mark-as-read-forward (n)
10037   "Mark N articles as read forwards.
10038 If N is negative, mark backwards instead.
10039 The difference between N and the actual number of articles marked is
10040 returned."
10041   (interactive "p")
10042   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10043
10044 (defun gnus-summary-mark-as-read-backward (n)
10045   "Mark the N articles as read backwards.
10046 The difference between N and the actual number of articles marked is
10047 returned."
10048   (interactive "p")
10049   (gnus-summary-mark-forward
10050    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10051
10052 (defun gnus-summary-mark-as-read (&optional article mark)
10053   "Mark current article as read.
10054 ARTICLE specifies the article to be marked as read.
10055 MARK specifies a string to be inserted at the beginning of the line."
10056   (gnus-summary-mark-article article mark))
10057
10058 (defun gnus-summary-clear-mark-forward (n)
10059   "Clear marks from N articles forward.
10060 If N is negative, clear backward instead.
10061 The difference between N and the number of marks cleared is returned."
10062   (interactive "p")
10063   (gnus-summary-mark-forward n gnus-unread-mark))
10064
10065 (defun gnus-summary-clear-mark-backward (n)
10066   "Clear marks from N articles backward.
10067 The difference between N and the number of marks cleared is returned."
10068   (interactive "p")
10069   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10070
10071 (defun gnus-summary-mark-unread-as-read ()
10072   "Intended to be used by `gnus-summary-mark-article-hook'."
10073   (when (memq gnus-current-article gnus-newsgroup-unreads)
10074     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10075
10076 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10077   "Intended to be used by `gnus-summary-mark-article-hook'."
10078   (let ((mark (gnus-summary-article-mark)))
10079     (when (or (gnus-unread-mark-p mark)
10080               (gnus-read-mark-p mark))
10081       (gnus-summary-mark-article gnus-current-article
10082                                  (or new-mark gnus-read-mark)))))
10083
10084 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10085   "Intended to be used by `gnus-summary-mark-article-hook'."
10086   (let ((mark (gnus-summary-article-mark)))
10087     (when (or (gnus-unread-mark-p mark)
10088               (gnus-read-mark-p mark))
10089       (gnus-summary-mark-article (gnus-summary-article-number)
10090                                  (or new-mark gnus-read-mark)))))
10091
10092 (defun gnus-summary-mark-unread-as-ticked ()
10093   "Intended to be used by `gnus-summary-mark-article-hook'."
10094   (when (memq gnus-current-article gnus-newsgroup-unreads)
10095     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10096
10097 (defun gnus-summary-mark-region-as-read (point mark all)
10098   "Mark all unread articles between point and mark as read.
10099 If given a prefix, mark all articles between point and mark as read,
10100 even ticked and dormant ones."
10101   (interactive "r\nP")
10102   (save-excursion
10103     (let (article)
10104       (goto-char point)
10105       (beginning-of-line)
10106       (while (and
10107               (< (point) mark)
10108               (progn
10109                 (when (or all
10110                           (memq (setq article (gnus-summary-article-number))
10111                                 gnus-newsgroup-unreads))
10112                   (gnus-summary-mark-article article gnus-del-mark))
10113                 t)
10114               (gnus-summary-find-next))))))
10115
10116 (defun gnus-summary-mark-below (score mark)
10117   "Mark articles with score less than SCORE with MARK."
10118   (interactive "P\ncMark: ")
10119   (setq score (if score
10120                   (prefix-numeric-value score)
10121                 (or gnus-summary-default-score 0)))
10122   (save-excursion
10123     (set-buffer gnus-summary-buffer)
10124     (goto-char (point-min))
10125     (while
10126         (progn
10127           (and (< (gnus-summary-article-score) score)
10128                (gnus-summary-mark-article nil mark))
10129           (gnus-summary-find-next)))))
10130
10131 (defun gnus-summary-kill-below (&optional score)
10132   "Mark articles with score below SCORE as read."
10133   (interactive "P")
10134   (gnus-summary-mark-below score gnus-killed-mark))
10135
10136 (defun gnus-summary-clear-above (&optional score)
10137   "Clear all marks from articles with score above SCORE."
10138   (interactive "P")
10139   (gnus-summary-mark-above score gnus-unread-mark))
10140
10141 (defun gnus-summary-tick-above (&optional score)
10142   "Tick all articles with score above SCORE."
10143   (interactive "P")
10144   (gnus-summary-mark-above score gnus-ticked-mark))
10145
10146 (defun gnus-summary-mark-above (score mark)
10147   "Mark articles with score over SCORE with MARK."
10148   (interactive "P\ncMark: ")
10149   (setq score (if score
10150                   (prefix-numeric-value score)
10151                 (or gnus-summary-default-score 0)))
10152   (save-excursion
10153     (set-buffer gnus-summary-buffer)
10154     (goto-char (point-min))
10155     (while (and (progn
10156                   (when (> (gnus-summary-article-score) score)
10157                     (gnus-summary-mark-article nil mark))
10158                   t)
10159                 (gnus-summary-find-next)))))
10160
10161 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10162 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10163 (defun gnus-summary-limit-include-expunged (&optional no-error)
10164   "Display all the hidden articles that were expunged for low scores."
10165   (interactive)
10166   (let ((buffer-read-only nil))
10167     (let ((scored gnus-newsgroup-scored)
10168           headers h)
10169       (while scored
10170         (unless (gnus-summary-article-header (caar scored))
10171           (and (setq h (gnus-number-to-header (caar scored)))
10172                (< (cdar scored) gnus-summary-expunge-below)
10173                (push h headers)))
10174         (setq scored (cdr scored)))
10175       (if (not headers)
10176           (when (not no-error)
10177             (error "No expunged articles hidden"))
10178         (goto-char (point-min))
10179         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10180         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10181         (mapcar (lambda (x) (push (mail-header-number x)
10182                                   gnus-newsgroup-limit))
10183                 headers)
10184         (gnus-summary-prepare-unthreaded (nreverse headers))
10185         (goto-char (point-min))
10186         (gnus-summary-position-point)
10187         t))))
10188
10189 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10190   "Mark all unread articles in this newsgroup as read.
10191 If prefix argument ALL is non-nil, ticked and dormant articles will
10192 also be marked as read.
10193 If QUIETLY is non-nil, no questions will be asked.
10194
10195 If TO-HERE is non-nil, it should be a point in the buffer.  All
10196 articles before (after, if REVERSE is set) this point will be marked
10197 as read.
10198
10199 Note that this function will only catch up the unread article
10200 in the current summary buffer limitation.
10201
10202 The number of articles marked as read is returned."
10203   (interactive "P")
10204   (prog1
10205       (save-excursion
10206         (when (or quietly
10207                   (not gnus-interactive-catchup) ;Without confirmation?
10208                   gnus-expert-user
10209                   (gnus-y-or-n-p
10210                    (if all
10211                        "Mark absolutely all articles as read? "
10212                      "Mark all unread articles as read? ")))
10213           (if (and not-mark
10214                    (not gnus-newsgroup-adaptive)
10215                    (not gnus-newsgroup-auto-expire)
10216                    (not gnus-suppress-duplicates)
10217                    (or (not gnus-use-cache)
10218                        (eq gnus-use-cache 'passive)))
10219               (progn
10220                 (when all
10221                   (setq gnus-newsgroup-marked nil
10222                         gnus-newsgroup-spam-marked nil
10223                         gnus-newsgroup-dormant nil))
10224                 (setq gnus-newsgroup-unreads
10225                       (gnus-sorted-nunion
10226                        (gnus-intersection gnus-newsgroup-unreads
10227                                           gnus-newsgroup-downloadable)
10228                        gnus-newsgroup-unfetched)))
10229             ;; We actually mark all articles as canceled, which we
10230             ;; have to do when using auto-expiry or adaptive scoring.
10231             (gnus-summary-show-all-threads)
10232             (if (and to-here reverse)
10233                 (progn
10234                   (goto-char to-here)
10235                   (gnus-summary-mark-current-read-and-unread-as-read
10236                    gnus-catchup-mark)
10237                   (while (gnus-summary-find-next (not all))
10238                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10239               (when (gnus-summary-first-subject (not all))
10240                 (while (and
10241                         (if to-here (< (point) to-here) t)
10242                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10243                         (gnus-summary-find-next (not all))))))
10244             (gnus-set-mode-line 'summary))
10245           t))
10246     (gnus-summary-position-point)))
10247
10248 (defun gnus-summary-catchup-to-here (&optional all)
10249   "Mark all unticked articles before the current one as read.
10250 If ALL is non-nil, also mark ticked and dormant articles as read."
10251   (interactive "P")
10252   (save-excursion
10253     (gnus-save-hidden-threads
10254       (let ((beg (point)))
10255         ;; We check that there are unread articles.
10256         (when (or all (gnus-summary-find-prev))
10257           (gnus-summary-catchup all t beg)))))
10258   (gnus-summary-position-point))
10259
10260 (defun gnus-summary-catchup-from-here (&optional all)
10261   "Mark all unticked articles after (and including) the current one as read.
10262 If ALL is non-nil, also mark ticked and dormant articles as read."
10263   (interactive "P")
10264   (save-excursion
10265     (gnus-save-hidden-threads
10266       (let ((beg (point)))
10267         ;; We check that there are unread articles.
10268         (when (or all (gnus-summary-find-next))
10269           (gnus-summary-catchup all t beg nil t)))))
10270   (gnus-summary-position-point))
10271
10272 (defun gnus-summary-catchup-all (&optional quietly)
10273   "Mark all articles in this newsgroup as read.
10274 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10275 instead, which marks only unread articles as read."
10276   (interactive "P")
10277   (gnus-summary-catchup t quietly))
10278
10279 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10280   "Mark all unread articles in this group as read, then exit.
10281 If prefix argument ALL is non-nil, all articles are marked as read.
10282 If QUIETLY is non-nil, no questions will be asked."
10283   (interactive "P")
10284   (when (gnus-summary-catchup all quietly nil 'fast)
10285     ;; Select next newsgroup or exit.
10286     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10287              (eq gnus-auto-select-next 'quietly))
10288         (gnus-summary-next-group nil)
10289       (gnus-summary-exit))))
10290
10291 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10292   "Mark all articles in this newsgroup as read, and then exit.
10293 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10294 instead, which marks only unread articles as read."
10295   (interactive "P")
10296   (gnus-summary-catchup-and-exit t quietly))
10297
10298 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10299   "Mark all articles in this group as read and select the next group.
10300 If given a prefix, mark all articles, unread as well as ticked, as
10301 read."
10302   (interactive "P")
10303   (save-excursion
10304     (gnus-summary-catchup all))
10305   (gnus-summary-next-group))
10306
10307 ;;;
10308 ;;; with article
10309 ;;;
10310
10311 (defmacro gnus-with-article (article &rest forms)
10312   "Select ARTICLE and perform FORMS in the original article buffer.
10313 Then replace the article with the result."
10314   `(progn
10315      ;; We don't want the article to be marked as read.
10316      (let (gnus-mark-article-hook)
10317        (gnus-summary-select-article t t nil ,article))
10318      (set-buffer gnus-original-article-buffer)
10319      ,@forms
10320      (if (not (gnus-check-backend-function
10321                'request-replace-article (car gnus-article-current)))
10322          (gnus-message 5 "Read-only group; not replacing")
10323        (unless (gnus-request-replace-article
10324                 ,article (car gnus-article-current)
10325                 (current-buffer) t)
10326          (error "Couldn't replace article")))
10327      ;; The cache and backlog have to be flushed somewhat.
10328      (when gnus-keep-backlog
10329        (gnus-backlog-remove-article
10330         (car gnus-article-current) (cdr gnus-article-current)))
10331      (when gnus-use-cache
10332        (gnus-cache-update-article
10333         (car gnus-article-current) (cdr gnus-article-current)))))
10334
10335 (put 'gnus-with-article 'lisp-indent-function 1)
10336 (put 'gnus-with-article 'edebug-form-spec '(form body))
10337
10338 ;; Thread-based commands.
10339
10340 (defun gnus-summary-articles-in-thread (&optional article)
10341   "Return a list of all articles in the current thread.
10342 If ARTICLE is non-nil, return all articles in the thread that starts
10343 with that article."
10344   (let* ((article (or article (gnus-summary-article-number)))
10345          (data (gnus-data-find-list article))
10346          (top-level (gnus-data-level (car data)))
10347          (top-subject
10348           (cond ((null gnus-thread-operation-ignore-subject)
10349                  (gnus-simplify-subject-re
10350                   (mail-header-subject (gnus-data-header (car data)))))
10351                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10352                  (gnus-simplify-subject-fuzzy
10353                   (mail-header-subject (gnus-data-header (car data)))))
10354                 (t nil)))
10355          (end-point (save-excursion
10356                       (if (gnus-summary-go-to-next-thread)
10357                           (point) (point-max))))
10358          articles)
10359     (while (and data
10360                 (< (gnus-data-pos (car data)) end-point))
10361       (when (or (not top-subject)
10362                 (string= top-subject
10363                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10364                              (gnus-simplify-subject-fuzzy
10365                               (mail-header-subject
10366                                (gnus-data-header (car data))))
10367                            (gnus-simplify-subject-re
10368                             (mail-header-subject
10369                              (gnus-data-header (car data)))))))
10370         (push (gnus-data-number (car data)) articles))
10371       (unless (and (setq data (cdr data))
10372                    (> (gnus-data-level (car data)) top-level))
10373         (setq data nil)))
10374     ;; Return the list of articles.
10375     (nreverse articles)))
10376
10377 (defun gnus-summary-rethread-current ()
10378   "Rethread the thread the current article is part of."
10379   (interactive)
10380   (let* ((gnus-show-threads t)
10381          (article (gnus-summary-article-number))
10382          (id (mail-header-id (gnus-summary-article-header)))
10383          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10384     (unless id
10385       (error "No article on the current line"))
10386     (gnus-rebuild-thread id)
10387     (gnus-summary-goto-subject article)))
10388
10389 (defun gnus-summary-reparent-thread ()
10390   "Make the current article child of the marked (or previous) article.
10391
10392 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10393 is non-nil or the Subject: of both articles are the same."
10394   (interactive)
10395   (unless (not (gnus-group-read-only-p))
10396     (error "The current newsgroup does not support article editing"))
10397   (unless (<= (length gnus-newsgroup-processable) 1)
10398     (error "No more than one article may be marked"))
10399   (save-window-excursion
10400     (let ((gnus-article-buffer " *reparent*")
10401           (current-article (gnus-summary-article-number))
10402           ;; First grab the marked article, otherwise one line up.
10403           (parent-article (if (not (null gnus-newsgroup-processable))
10404                               (car gnus-newsgroup-processable)
10405                             (save-excursion
10406                               (if (eq (forward-line -1) 0)
10407                                   (gnus-summary-article-number)
10408                                 (error "Beginning of summary buffer"))))))
10409       (unless (not (eq current-article parent-article))
10410         (error "An article may not be self-referential"))
10411       (let ((message-id (mail-header-id
10412                          (gnus-summary-article-header parent-article))))
10413         (unless (and message-id (not (equal message-id "")))
10414           (error "No message-id in desired parent"))
10415         (gnus-with-article current-article
10416           (save-restriction
10417             (goto-char (point-min))
10418             (message-narrow-to-head)
10419             (if (re-search-forward "^References: " nil t)
10420                 (progn
10421                   (re-search-forward "^[^ \t]" nil t)
10422                   (forward-line -1)
10423                   (end-of-line)
10424                   (insert " " message-id))
10425               (insert "References: " message-id "\n"))))
10426         (set-buffer gnus-summary-buffer)
10427         (gnus-summary-unmark-all-processable)
10428         (gnus-summary-update-article current-article)
10429         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10430             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10431         (gnus-summary-rethread-current)
10432         (gnus-message 3 "Article %d is now the child of article %d"
10433                       current-article parent-article)))))
10434
10435 (defun gnus-summary-toggle-threads (&optional arg)
10436   "Toggle showing conversation threads.
10437 If ARG is positive number, turn showing conversation threads on."
10438   (interactive "P")
10439   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10440     (setq gnus-show-threads
10441           (if (null arg) (not gnus-show-threads)
10442             (> (prefix-numeric-value arg) 0)))
10443     (gnus-summary-prepare)
10444     (gnus-summary-goto-subject current)
10445     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10446     (gnus-summary-position-point)))
10447
10448 (defun gnus-summary-show-all-threads ()
10449   "Show all threads."
10450   (interactive)
10451   (save-excursion
10452     (let ((buffer-read-only nil))
10453       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10454   (gnus-summary-position-point))
10455
10456 (defun gnus-summary-show-thread ()
10457   "Show thread subtrees.
10458 Returns nil if no thread was there to be shown."
10459   (interactive)
10460   (let ((buffer-read-only nil)
10461         (orig (point))
10462         (end (point-at-eol))
10463         ;; Leave point at bol
10464         (beg (progn (beginning-of-line) (point))))
10465     (prog1
10466         ;; Any hidden lines here?
10467         (search-forward "\r" end t)
10468       (subst-char-in-region beg end ?\^M ?\n t)
10469       (goto-char orig)
10470       (gnus-summary-position-point))))
10471
10472 (defun gnus-summary-maybe-hide-threads ()
10473   "If requested, hide the threads that should be hidden."
10474   (when (and gnus-show-threads
10475              gnus-thread-hide-subtree)
10476     (gnus-summary-hide-all-threads
10477      (if (or (consp gnus-thread-hide-subtree)
10478              (functionp gnus-thread-hide-subtree))
10479          (gnus-make-predicate gnus-thread-hide-subtree)
10480        nil))))
10481
10482 ;;; Hiding predicates.
10483
10484 (defun gnus-article-unread-p (header)
10485   (memq (mail-header-number header) gnus-newsgroup-unreads))
10486
10487 (defun gnus-article-unseen-p (header)
10488   (memq (mail-header-number header) gnus-newsgroup-unseen))
10489
10490 (defun gnus-map-articles (predicate articles)
10491   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10492   (apply 'gnus-or (mapcar predicate
10493                           (mapcar 'gnus-summary-article-header articles))))
10494
10495 (defun gnus-summary-hide-all-threads (&optional predicate)
10496   "Hide all thread subtrees.
10497 If PREDICATE is supplied, threads that satisfy this predicate
10498 will not be hidden."
10499   (interactive)
10500   (save-excursion
10501     (goto-char (point-min))
10502     (let ((end nil))
10503       (while (not end)
10504         (when (or (not predicate)
10505                   (gnus-map-articles
10506                    predicate (gnus-summary-article-children)))
10507             (gnus-summary-hide-thread))
10508         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10509   (gnus-summary-position-point))
10510
10511 (defun gnus-summary-hide-thread ()
10512   "Hide thread subtrees.
10513 If PREDICATE is supplied, threads that satisfy this predicate
10514 will not be hidden.
10515 Returns nil if no threads were there to be hidden."
10516   (interactive)
10517   (let ((buffer-read-only nil)
10518         (start (point))
10519         (article (gnus-summary-article-number)))
10520     (goto-char start)
10521     ;; Go forward until either the buffer ends or the subthread
10522     ;; ends.
10523     (when (and (not (eobp))
10524                (or (zerop (gnus-summary-next-thread 1 t))
10525                    (goto-char (point-max))))
10526       (prog1
10527           (if (and (> (point) start)
10528                    (search-backward "\n" start t))
10529               (progn
10530                 (subst-char-in-region start (point) ?\n ?\^M)
10531                 (gnus-summary-goto-subject article))
10532             (goto-char start)
10533             nil)))))
10534
10535 (defun gnus-summary-go-to-next-thread (&optional previous)
10536   "Go to the same level (or less) next thread.
10537 If PREVIOUS is non-nil, go to previous thread instead.
10538 Return the article number moved to, or nil if moving was impossible."
10539   (let ((level (gnus-summary-thread-level))
10540         (way (if previous -1 1))
10541         (beg (point)))
10542     (forward-line way)
10543     (while (and (not (eobp))
10544                 (< level (gnus-summary-thread-level)))
10545       (forward-line way))
10546     (if (eobp)
10547         (progn
10548           (goto-char beg)
10549           nil)
10550       (setq beg (point))
10551       (prog1
10552           (gnus-summary-article-number)
10553         (goto-char beg)))))
10554
10555 (defun gnus-summary-next-thread (n &optional silent)
10556   "Go to the same level next N'th thread.
10557 If N is negative, search backward instead.
10558 Returns the difference between N and the number of skips actually
10559 done.
10560
10561 If SILENT, don't output messages."
10562   (interactive "p")
10563   (let ((backward (< n 0))
10564         (n (abs n)))
10565     (while (and (> n 0)
10566                 (gnus-summary-go-to-next-thread backward))
10567       (decf n))
10568     (unless silent
10569       (gnus-summary-position-point))
10570     (when (and (not silent) (/= 0 n))
10571       (gnus-message 7 "No more threads"))
10572     n))
10573
10574 (defun gnus-summary-prev-thread (n)
10575   "Go to the same level previous N'th thread.
10576 Returns the difference between N and the number of skips actually
10577 done."
10578   (interactive "p")
10579   (gnus-summary-next-thread (- n)))
10580
10581 (defun gnus-summary-go-down-thread ()
10582   "Go down one level in the current thread."
10583   (let ((children (gnus-summary-article-children)))
10584     (when children
10585       (gnus-summary-goto-subject (car children)))))
10586
10587 (defun gnus-summary-go-up-thread ()
10588   "Go up one level in the current thread."
10589   (let ((parent (gnus-summary-article-parent)))
10590     (when parent
10591       (gnus-summary-goto-subject parent))))
10592
10593 (defun gnus-summary-down-thread (n)
10594   "Go down thread N steps.
10595 If N is negative, go up instead.
10596 Returns the difference between N and how many steps down that were
10597 taken."
10598   (interactive "p")
10599   (let ((up (< n 0))
10600         (n (abs n)))
10601     (while (and (> n 0)
10602                 (if up (gnus-summary-go-up-thread)
10603                   (gnus-summary-go-down-thread)))
10604       (setq n (1- n)))
10605     (gnus-summary-position-point)
10606     (when (/= 0 n)
10607       (gnus-message 7 "Can't go further"))
10608     n))
10609
10610 (defun gnus-summary-up-thread (n)
10611   "Go up thread N steps.
10612 If N is negative, go down instead.
10613 Returns the difference between N and how many steps down that were
10614 taken."
10615   (interactive "p")
10616   (gnus-summary-down-thread (- n)))
10617
10618 (defun gnus-summary-top-thread ()
10619   "Go to the top of the thread."
10620   (interactive)
10621   (while (gnus-summary-go-up-thread))
10622   (gnus-summary-article-number))
10623
10624 (defun gnus-summary-kill-thread (&optional unmark)
10625   "Mark articles under current thread as read.
10626 If the prefix argument is positive, remove any kinds of marks.
10627 If the prefix argument is negative, tick articles instead."
10628   (interactive "P")
10629   (when unmark
10630     (setq unmark (prefix-numeric-value unmark)))
10631   (let ((articles (gnus-summary-articles-in-thread)))
10632     (save-excursion
10633       ;; Expand the thread.
10634       (gnus-summary-show-thread)
10635       ;; Mark all the articles.
10636       (while articles
10637         (gnus-summary-goto-subject (car articles))
10638         (cond ((null unmark)
10639                (gnus-summary-mark-article-as-read gnus-killed-mark))
10640               ((> unmark 0)
10641                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10642               (t
10643                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10644         (setq articles (cdr articles))))
10645     ;; Hide killed subtrees.
10646     (and (null unmark)
10647          gnus-thread-hide-killed
10648          (gnus-summary-hide-thread))
10649     ;; If marked as read, go to next unread subject.
10650     (when (null unmark)
10651       ;; Go to next unread subject.
10652       (gnus-summary-next-subject 1 t)))
10653   (gnus-set-mode-line 'summary))
10654
10655 ;; Summary sorting commands
10656
10657 (defun gnus-summary-sort-by-number (&optional reverse)
10658   "Sort the summary buffer by article number.
10659 Argument REVERSE means reverse order."
10660   (interactive "P")
10661   (gnus-summary-sort 'number reverse))
10662
10663 (defun gnus-summary-sort-by-random (&optional reverse)
10664   "Randomize the order in the summary buffer.
10665 Argument REVERSE means to randomize in reverse order."
10666   (interactive "P")
10667   (gnus-summary-sort 'random reverse))
10668
10669 (defun gnus-summary-sort-by-author (&optional reverse)
10670   "Sort the summary buffer by author name alphabetically.
10671 If `case-fold-search' is non-nil, case of letters is ignored.
10672 Argument REVERSE means reverse order."
10673   (interactive "P")
10674   (gnus-summary-sort 'author reverse))
10675
10676 (defun gnus-summary-sort-by-subject (&optional reverse)
10677   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10678 If `case-fold-search' is non-nil, case of letters is ignored.
10679 Argument REVERSE means reverse order."
10680   (interactive "P")
10681   (gnus-summary-sort 'subject reverse))
10682
10683 (defun gnus-summary-sort-by-date (&optional reverse)
10684   "Sort the summary buffer by date.
10685 Argument REVERSE means reverse order."
10686   (interactive "P")
10687   (gnus-summary-sort 'date reverse))
10688
10689 (defun gnus-summary-sort-by-score (&optional reverse)
10690   "Sort the summary buffer by score.
10691 Argument REVERSE means reverse order."
10692   (interactive "P")
10693   (gnus-summary-sort 'score reverse))
10694
10695 (defun gnus-summary-sort-by-lines (&optional reverse)
10696   "Sort the summary buffer by the number of lines.
10697 Argument REVERSE means reverse order."
10698   (interactive "P")
10699   (gnus-summary-sort 'lines reverse))
10700
10701 (defun gnus-summary-sort-by-chars (&optional reverse)
10702   "Sort the summary buffer by article length.
10703 Argument REVERSE means reverse order."
10704   (interactive "P")
10705   (gnus-summary-sort 'chars reverse))
10706
10707 (defun gnus-summary-sort-by-original (&optional reverse)
10708   "Sort the summary buffer using the default sorting method.
10709 Argument REVERSE means reverse order."
10710   (interactive "P")
10711   (let* ((buffer-read-only)
10712          (gnus-summary-prepare-hook nil))
10713     ;; We do the sorting by regenerating the threads.
10714     (gnus-summary-prepare)
10715     ;; Hide subthreads if needed.
10716     (gnus-summary-maybe-hide-threads)))
10717
10718 (defun gnus-summary-sort (predicate reverse)
10719   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10720   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10721          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10722          (gnus-thread-sort-functions
10723           (if (not reverse)
10724               thread
10725             `(lambda (t1 t2)
10726                (,thread t2 t1))))
10727          (gnus-sort-gathered-threads-function
10728           gnus-thread-sort-functions)
10729          (gnus-article-sort-functions
10730           (if (not reverse)
10731               article
10732             `(lambda (t1 t2)
10733                (,article t2 t1))))
10734          (buffer-read-only)
10735          (gnus-summary-prepare-hook nil))
10736     ;; We do the sorting by regenerating the threads.
10737     (gnus-summary-prepare)
10738     ;; Hide subthreads if needed.
10739     (gnus-summary-maybe-hide-threads)))
10740
10741 ;; Summary saving commands.
10742
10743 (defun gnus-summary-save-article (&optional n not-saved)
10744   "Save the current article using the default saver function.
10745 If N is a positive number, save the N next articles.
10746 If N is a negative number, save the N previous articles.
10747 If N is nil and any articles have been marked with the process mark,
10748 save those articles instead.
10749 The variable `gnus-default-article-saver' specifies the saver function."
10750   (interactive "P")
10751   (let* ((articles (gnus-summary-work-articles n))
10752          (save-buffer (save-excursion
10753                         (nnheader-set-temp-buffer " *Gnus Save*")))
10754          (num (length articles))
10755          header file)
10756     (dolist (article articles)
10757       (setq header (gnus-summary-article-header article))
10758       (if (not (vectorp header))
10759           ;; This is a pseudo-article.
10760           (if (assq 'name header)
10761               (gnus-copy-file (cdr (assq 'name header)))
10762             (gnus-message 1 "Article %d is unsaveable" article))
10763         ;; This is a real article.
10764         (save-window-excursion
10765           (let ((gnus-display-mime-function nil)
10766                 (gnus-article-prepare-hook nil))
10767             (gnus-summary-select-article t nil nil article)))
10768         (save-excursion
10769           (set-buffer save-buffer)
10770           (erase-buffer)
10771           (insert-buffer-substring gnus-original-article-buffer))
10772         (setq file (gnus-article-save save-buffer file num))
10773         (gnus-summary-remove-process-mark article)
10774         (unless not-saved
10775           (gnus-summary-set-saved-mark article))))
10776     (gnus-kill-buffer save-buffer)
10777     (gnus-summary-position-point)
10778     (gnus-set-mode-line 'summary)
10779     n))
10780
10781 (defun gnus-summary-pipe-output (&optional arg headers)
10782   "Pipe the current article to a subprocess.
10783 If N is a positive number, pipe the N next articles.
10784 If N is a negative number, pipe the N previous articles.
10785 If N is nil and any articles have been marked with the process mark,
10786 pipe those articles instead.
10787 If HEADERS (the symbolic prefix), include the headers, too."
10788   (interactive (gnus-interactive "P\ny"))
10789   (require 'gnus-art)
10790   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10791         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10792     (gnus-summary-save-article arg t))
10793   (let ((buffer (get-buffer "*Shell Command Output*")))
10794     (when (and buffer
10795                (not (zerop (buffer-size buffer))))
10796       (gnus-configure-windows 'pipe))))
10797
10798 (defun gnus-summary-save-article-mail (&optional arg)
10799   "Append the current article to an mail file.
10800 If N is a positive number, save the N next articles.
10801 If N is a negative number, save the N previous articles.
10802 If N is nil and any articles have been marked with the process mark,
10803 save those articles instead."
10804   (interactive "P")
10805   (require 'gnus-art)
10806   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10807     (gnus-summary-save-article arg)))
10808
10809 (defun gnus-summary-save-article-rmail (&optional arg)
10810   "Append the current article to an rmail file.
10811 If N is a positive number, save the N next articles.
10812 If N is a negative number, save the N previous articles.
10813 If N is nil and any articles have been marked with the process mark,
10814 save those articles instead."
10815   (interactive "P")
10816   (require 'gnus-art)
10817   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10818     (gnus-summary-save-article arg)))
10819
10820 (defun gnus-summary-save-article-file (&optional arg)
10821   "Append the current article to a file.
10822 If N is a positive number, save the N next articles.
10823 If N is a negative number, save the N previous articles.
10824 If N is nil and any articles have been marked with the process mark,
10825 save those articles instead."
10826   (interactive "P")
10827   (require 'gnus-art)
10828   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10829     (gnus-summary-save-article arg)))
10830
10831 (defun gnus-summary-write-article-file (&optional arg)
10832   "Write the current article to a file, deleting the previous file.
10833 If N is a positive number, save the N next articles.
10834 If N is a negative number, save the N previous articles.
10835 If N is nil and any articles have been marked with the process mark,
10836 save those articles instead."
10837   (interactive "P")
10838   (require 'gnus-art)
10839   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10840     (gnus-summary-save-article arg)))
10841
10842 (defun gnus-summary-save-article-body-file (&optional arg)
10843   "Append the current article body to a file.
10844 If N is a positive number, save the N next articles.
10845 If N is a negative number, save the N previous articles.
10846 If N is nil and any articles have been marked with the process mark,
10847 save those articles instead."
10848   (interactive "P")
10849   (require 'gnus-art)
10850   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10851     (gnus-summary-save-article arg)))
10852
10853 (defun gnus-summary-muttprint (&optional arg)
10854   "Print the current article using Muttprint.
10855 If N is a positive number, save the N next articles.
10856 If N is a negative number, save the N previous articles.
10857 If N is nil and any articles have been marked with the process mark,
10858 save those articles instead."
10859   (interactive "P")
10860   (require 'gnus-art)
10861   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10862     (gnus-summary-save-article arg t)))
10863
10864 (defun gnus-summary-pipe-message (program)
10865   "Pipe the current article through PROGRAM."
10866   (interactive "sProgram: ")
10867   (gnus-summary-select-article)
10868   (let ((mail-header-separator ""))
10869     (gnus-eval-in-buffer-window gnus-article-buffer
10870       (save-restriction
10871         (widen)
10872         (let ((start (window-start))
10873               buffer-read-only)
10874           (message-pipe-buffer-body program)
10875           (set-window-start (get-buffer-window (current-buffer)) start))))))
10876
10877 (defun gnus-get-split-value (methods)
10878   "Return a value based on the split METHODS."
10879   (let (split-name method result match)
10880     (when methods
10881       (save-excursion
10882         (set-buffer gnus-original-article-buffer)
10883         (save-restriction
10884           (nnheader-narrow-to-headers)
10885           (while (and methods (not split-name))
10886             (goto-char (point-min))
10887             (setq method (pop methods))
10888             (setq match (car method))
10889             (when (cond
10890                    ((stringp match)
10891                     ;; Regular expression.
10892                     (ignore-errors
10893                       (re-search-forward match nil t)))
10894                    ((functionp match)
10895                     ;; Function.
10896                     (save-restriction
10897                       (widen)
10898                       (setq result (funcall match gnus-newsgroup-name))))
10899                    ((consp match)
10900                     ;; Form.
10901                     (save-restriction
10902                       (widen)
10903                       (setq result (eval match)))))
10904               (setq split-name (cdr method))
10905               (cond ((stringp result)
10906                      (push (expand-file-name
10907                             result gnus-article-save-directory)
10908                            split-name))
10909                     ((consp result)
10910                      (setq split-name (append result split-name)))))))))
10911     (nreverse split-name)))
10912
10913 (defun gnus-valid-move-group-p (group)
10914   (and (boundp group)
10915        (symbol-name group)
10916        (symbol-value group)
10917        (gnus-get-function (gnus-find-method-for-group
10918                            (symbol-name group)) 'request-accept-article t)))
10919
10920 (defun gnus-read-move-group-name (prompt default articles prefix)
10921   "Read a group name."
10922   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10923          (minibuffer-confirm-incomplete nil) ; XEmacs
10924          (prom
10925           (format "%s %s to:"
10926                   prompt
10927                   (if (> (length articles) 1)
10928                       (format "these %d articles" (length articles))
10929                     "this article")))
10930          (to-newsgroup
10931           (cond
10932            ((null split-name)
10933             (gnus-completing-read-with-default
10934              default prom
10935              gnus-active-hashtb
10936              'gnus-valid-move-group-p
10937              nil prefix
10938              'gnus-group-history))
10939            ((= 1 (length split-name))
10940             (gnus-completing-read-with-default
10941              (car split-name) prom
10942              gnus-active-hashtb
10943              'gnus-valid-move-group-p
10944              nil nil
10945              'gnus-group-history))
10946            (t
10947             (gnus-completing-read-with-default
10948              nil prom
10949              (mapcar (lambda (el) (list el))
10950                      (nreverse split-name))
10951              nil nil nil
10952              'gnus-group-history))))
10953          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10954     (when to-newsgroup
10955       (if (or (string= to-newsgroup "")
10956               (string= to-newsgroup prefix))
10957           (setq to-newsgroup default))
10958       (unless to-newsgroup
10959         (error "No group name entered"))
10960       (or (gnus-active to-newsgroup)
10961           (gnus-activate-group to-newsgroup nil nil to-method)
10962           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10963                                      to-newsgroup))
10964               (or (and (gnus-request-create-group to-newsgroup to-method)
10965                        (gnus-activate-group
10966                         to-newsgroup nil nil to-method)
10967                        (gnus-subscribe-group to-newsgroup))
10968                   (error "Couldn't create group %s" to-newsgroup)))
10969           (error "No such group: %s" to-newsgroup)))
10970     to-newsgroup))
10971
10972 (defun gnus-summary-save-parts (type dir n &optional reverse)
10973   "Save parts matching TYPE to DIR.
10974 If REVERSE, save parts that do not match TYPE."
10975   (interactive
10976    (list (read-string "Save parts of type: "
10977                       (or (car gnus-summary-save-parts-type-history)
10978                           gnus-summary-save-parts-default-mime)
10979                       'gnus-summary-save-parts-type-history)
10980          (setq gnus-summary-save-parts-last-directory
10981                (read-file-name "Save to directory: "
10982                                gnus-summary-save-parts-last-directory
10983                                nil t))
10984          current-prefix-arg))
10985   (gnus-summary-iterate n
10986     (let ((gnus-display-mime-function nil)
10987           (gnus-inhibit-treatment t))
10988       (gnus-summary-select-article))
10989     (save-excursion
10990       (set-buffer gnus-article-buffer)
10991       (let ((handles (or gnus-article-mime-handles
10992                          (mm-dissect-buffer nil gnus-article-loose-mime)
10993                          (and gnus-article-emulate-mime
10994                               (mm-uu-dissect)))))
10995         (when handles
10996           (gnus-summary-save-parts-1 type dir handles reverse)
10997           (unless gnus-article-mime-handles ;; Don't destroy this case.
10998             (mm-destroy-parts handles)))))))
10999
11000 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11001   (if (stringp (car handle))
11002       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11003               (cdr handle))
11004     (when (if reverse
11005               (not (string-match type (mm-handle-media-type handle)))
11006             (string-match type (mm-handle-media-type handle)))
11007       (let ((file (expand-file-name
11008                    (gnus-map-function
11009                     mm-file-name-rewrite-functions
11010                     (file-name-nondirectory
11011                      (or
11012                       (mail-content-type-get
11013                        (mm-handle-disposition handle) 'filename)
11014                       (mail-content-type-get
11015                        (mm-handle-type handle) 'name)
11016                       (concat gnus-newsgroup-name
11017                               "." (number-to-string
11018                                    (cdr gnus-article-current))))))
11019                    dir)))
11020         (unless (file-exists-p file)
11021           (mm-save-part-to-file handle file))))))
11022
11023 ;; Summary extract commands
11024
11025 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11026   (let ((buffer-read-only nil)
11027         (article (gnus-summary-article-number))
11028         after-article b e)
11029     (unless (gnus-summary-goto-subject article)
11030       (error "No such article: %d" article))
11031     (gnus-summary-position-point)
11032     ;; If all commands are to be bunched up on one line, we collect
11033     ;; them here.
11034     (unless gnus-view-pseudos-separately
11035       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11036             files action)
11037         (while ps
11038           (setq action (cdr (assq 'action (car ps))))
11039           (setq files (list (cdr (assq 'name (car ps)))))
11040           (while (and ps (cdr ps)
11041                       (string= (or action "1")
11042                                (or (cdr (assq 'action (cadr ps))) "2")))
11043             (push (cdr (assq 'name (cadr ps))) files)
11044             (setcdr ps (cddr ps)))
11045           (when files
11046             (when (not (string-match "%s" action))
11047               (push " " files))
11048             (push " " files)
11049             (when (assq 'execute (car ps))
11050               (setcdr (assq 'execute (car ps))
11051                       (funcall (if (string-match "%s" action)
11052                                    'format 'concat)
11053                                action
11054                                (mapconcat
11055                                 (lambda (f)
11056                                   (if (equal f " ")
11057                                       f
11058                                     (shell-quote-argument f)))
11059                                 files " ")))))
11060           (setq ps (cdr ps)))))
11061     (if (and gnus-view-pseudos (not not-view))
11062         (while pslist
11063           (when (assq 'execute (car pslist))
11064             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11065                                   (eq gnus-view-pseudos 'not-confirm)))
11066           (setq pslist (cdr pslist)))
11067       (save-excursion
11068         (while pslist
11069           (setq after-article (or (cdr (assq 'article (car pslist)))
11070                                   (gnus-summary-article-number)))
11071           (gnus-summary-goto-subject after-article)
11072           (forward-line 1)
11073           (setq b (point))
11074           (insert "    " (file-name-nondirectory
11075                           (cdr (assq 'name (car pslist))))
11076                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11077           (setq e (point))
11078           (forward-line -1)             ; back to `b'
11079           (gnus-add-text-properties
11080            b (1- e) (list 'gnus-number gnus-reffed-article-number
11081                           gnus-mouse-face-prop gnus-mouse-face))
11082           (gnus-data-enter
11083            after-article gnus-reffed-article-number
11084            gnus-unread-mark b (car pslist) 0 (- e b))
11085           (setq gnus-newsgroup-unreads
11086                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11087                                          gnus-reffed-article-number))
11088           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11089           (setq pslist (cdr pslist)))))))
11090
11091 (defun gnus-pseudos< (p1 p2)
11092   (let ((c1 (cdr (assq 'action p1)))
11093         (c2 (cdr (assq 'action p2))))
11094     (and c1 c2 (string< c1 c2))))
11095
11096 (defun gnus-request-pseudo-article (props)
11097   (cond ((assq 'execute props)
11098          (gnus-execute-command (cdr (assq 'execute props)))))
11099   (let ((gnus-current-article (gnus-summary-article-number)))
11100     (gnus-run-hooks 'gnus-mark-article-hook)))
11101
11102 (defun gnus-execute-command (command &optional automatic)
11103   (save-excursion
11104     (gnus-article-setup-buffer)
11105     (set-buffer gnus-article-buffer)
11106     (setq buffer-read-only nil)
11107     (let ((command (if automatic command
11108                      (read-string "Command: " (cons command 0)))))
11109       (erase-buffer)
11110       (insert "$ " command "\n\n")
11111       (if gnus-view-pseudo-asynchronously
11112           (start-process "gnus-execute" (current-buffer) shell-file-name
11113                          shell-command-switch command)
11114         (call-process shell-file-name nil t nil
11115                       shell-command-switch command)))))
11116
11117 ;; Summary kill commands.
11118
11119 (defun gnus-summary-edit-global-kill (article)
11120   "Edit the \"global\" kill file."
11121   (interactive (list (gnus-summary-article-number)))
11122   (gnus-group-edit-global-kill article))
11123
11124 (defun gnus-summary-edit-local-kill ()
11125   "Edit a local kill file applied to the current newsgroup."
11126   (interactive)
11127   (setq gnus-current-headers (gnus-summary-article-header))
11128   (gnus-group-edit-local-kill
11129    (gnus-summary-article-number) gnus-newsgroup-name))
11130
11131 ;;; Header reading.
11132
11133 (defun gnus-read-header (id &optional header)
11134   "Read the headers of article ID and enter them into the Gnus system."
11135   (let ((group gnus-newsgroup-name)
11136         (gnus-override-method
11137          (or
11138           gnus-override-method
11139           (and (gnus-news-group-p gnus-newsgroup-name)
11140                (car (gnus-refer-article-methods)))))
11141         where)
11142     ;; First we check to see whether the header in question is already
11143     ;; fetched.
11144     (if (stringp id)
11145         ;; This is a Message-ID.
11146         (setq header (or header (gnus-id-to-header id)))
11147       ;; This is an article number.
11148       (setq header (or header (gnus-summary-article-header id))))
11149     (if (and header
11150              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11151         ;; We have found the header.
11152         header
11153       ;; If this is a sparse article, we have to nix out its
11154       ;; previous entry in the thread hashtb.
11155       (when (and header
11156                  (gnus-summary-article-sparse-p (mail-header-number header)))
11157         (let* ((parent (gnus-parent-id (mail-header-references header)))
11158                (thread (and parent (gnus-id-to-thread parent))))
11159           (when thread
11160             (delq (assq header thread) thread))))
11161       ;; We have to really fetch the header to this article.
11162       (save-excursion
11163         (set-buffer nntp-server-buffer)
11164         (when (setq where (gnus-request-head id group))
11165           (nnheader-fold-continuation-lines)
11166           (goto-char (point-max))
11167           (insert ".\n")
11168           (goto-char (point-min))
11169           (insert "211 ")
11170           (princ (cond
11171                   ((numberp id) id)
11172                   ((cdr where) (cdr where))
11173                   (header (mail-header-number header))
11174                   (t gnus-reffed-article-number))
11175                  (current-buffer))
11176           (insert " Article retrieved.\n"))
11177         (if (or (not where)
11178                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11179             ()                          ; Malformed head.
11180           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11181             (when (and (stringp id)
11182                        (not (string= (gnus-group-real-name group)
11183                                      (car where))))
11184               ;; If we fetched by Message-ID and the article came
11185               ;; from a different group, we fudge some bogus article
11186               ;; numbers for this article.
11187               (mail-header-set-number header gnus-reffed-article-number))
11188             (save-excursion
11189               (set-buffer gnus-summary-buffer)
11190               (decf gnus-reffed-article-number)
11191               (gnus-remove-header (mail-header-number header))
11192               (push header gnus-newsgroup-headers)
11193               (setq gnus-current-headers header)
11194               (push (mail-header-number header) gnus-newsgroup-limit)))
11195           header)))))
11196
11197 (defun gnus-remove-header (number)
11198   "Remove header NUMBER from `gnus-newsgroup-headers'."
11199   (if (and gnus-newsgroup-headers
11200            (= number (mail-header-number (car gnus-newsgroup-headers))))
11201       (pop gnus-newsgroup-headers)
11202     (let ((headers gnus-newsgroup-headers))
11203       (while (and (cdr headers)
11204                   (not (= number (mail-header-number (cadr headers)))))
11205         (pop headers))
11206       (when (cdr headers)
11207         (setcdr headers (cddr headers))))))
11208
11209 ;;;
11210 ;;; summary highlights
11211 ;;;
11212
11213 (defun gnus-highlight-selected-summary ()
11214   "Highlight selected article in summary buffer."
11215   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11216   (when gnus-summary-selected-face
11217     (save-excursion
11218       (let* ((beg (point-at-bol))
11219              (end (point-at-eol))
11220              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11221              (from (if (get-text-property beg gnus-mouse-face-prop)
11222                        beg
11223                      (or (next-single-property-change
11224                           beg gnus-mouse-face-prop nil end)
11225                          beg)))
11226              (to
11227               (if (= from end)
11228                   (- from 2)
11229                 (or (next-single-property-change
11230                      from gnus-mouse-face-prop nil end)
11231                     end))))
11232         ;; If no mouse-face prop on line we will have to = from = end,
11233         ;; so we highlight the entire line instead.
11234         (when (= (+ to 2) from)
11235           (setq from beg)
11236           (setq to end))
11237         (if gnus-newsgroup-selected-overlay
11238             ;; Move old overlay.
11239             (gnus-move-overlay
11240              gnus-newsgroup-selected-overlay from to (current-buffer))
11241           ;; Create new overlay.
11242           (gnus-overlay-put
11243            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11244            'face gnus-summary-selected-face))))))
11245
11246 (defvar gnus-summary-highlight-line-cached nil)
11247 (defvar gnus-summary-highlight-line-trigger nil)
11248
11249 (defun gnus-summary-highlight-line-0 ()
11250   (if (and (eq gnus-summary-highlight-line-trigger
11251                gnus-summary-highlight)
11252            gnus-summary-highlight-line-cached)
11253       gnus-summary-highlight-line-cached
11254     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11255           gnus-summary-highlight-line-cached
11256           (let* ((cond (list 'cond))
11257                  (c cond)
11258                  (list gnus-summary-highlight))
11259             (while list
11260               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11261                               nil))
11262               (setq c (cdr c)
11263                     list (cdr list)))
11264             (gnus-byte-compile (list 'lambda nil cond))))))
11265
11266 (defun gnus-summary-highlight-line ()
11267   "Highlight current line according to `gnus-summary-highlight'."
11268   (let* ((beg (point-at-bol))
11269          (article (or (gnus-summary-article-number) gnus-current-article))
11270          (score (or (cdr (assq article
11271                                gnus-newsgroup-scored))
11272                     gnus-summary-default-score 0))
11273          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11274          (inhibit-read-only t)
11275          (default gnus-summary-default-score)
11276          (default-high gnus-summary-default-high-score)
11277          (default-low gnus-summary-default-low-score)
11278          (uncached (and gnus-summary-use-undownloaded-faces
11279                         (memq article gnus-newsgroup-undownloaded))))
11280     (let ((face (funcall (gnus-summary-highlight-line-0))))
11281       (unless (eq face (get-text-property beg 'face))
11282         (gnus-put-text-property-excluding-characters-with-faces
11283          beg (point-at-eol) 'face
11284          (setq face (if (boundp face) (symbol-value face) face)))
11285         (when gnus-summary-highlight-line-function
11286           (funcall gnus-summary-highlight-line-function article face))))))
11287
11288 (defun gnus-update-read-articles (group unread &optional compute)
11289   "Update the list of read articles in GROUP.
11290 UNREAD is a sorted list."
11291   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11292          (entry (gnus-gethash group gnus-newsrc-hashtb))
11293          (info (nth 2 entry))
11294          (prev 1)
11295          read)
11296     (if (or (not info) (not active))
11297         ;; There is no info on this group if it was, in fact,
11298         ;; killed.  Gnus stores no information on killed groups, so
11299         ;; there's nothing to be done.
11300         ;; One could store the information somewhere temporarily,
11301         ;; perhaps...  Hmmm...
11302         ()
11303       ;; Remove any negative articles numbers.
11304       (while (and unread (< (car unread) 0))
11305         (setq unread (cdr unread)))
11306       ;; Remove any expired article numbers
11307       (while (and unread (< (car unread) (car active)))
11308         (setq unread (cdr unread)))
11309       ;; Compute the ranges of read articles by looking at the list of
11310       ;; unread articles.
11311       (while unread
11312         (when (/= (car unread) prev)
11313           (push (if (= prev (1- (car unread))) prev
11314                   (cons prev (1- (car unread))))
11315                 read))
11316         (setq prev (1+ (car unread)))
11317         (setq unread (cdr unread)))
11318       (when (<= prev (cdr active))
11319         (push (cons prev (cdr active)) read))
11320       (setq read (if (> (length read) 1) (nreverse read) read))
11321       (if compute
11322           read
11323         (save-excursion
11324           (let (setmarkundo)
11325             ;; Propagate the read marks to the backend.
11326             (when (gnus-check-backend-function 'request-set-mark group)
11327               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11328                     (add (gnus-remove-from-range read (gnus-info-read info))))
11329                 (when (or add del)
11330                   (unless (gnus-check-group group)
11331                     (error "Can't open server for %s" group))
11332                   (gnus-request-set-mark
11333                    group (delq nil (list (if add (list add 'add '(read)))
11334                                          (if del (list del 'del '(read))))))
11335                   (setq setmarkundo
11336                         `(gnus-request-set-mark
11337                           ,group
11338                           ',(delq nil (list
11339                                        (if del (list del 'add '(read)))
11340                                        (if add (list add 'del '(read))))))))))
11341             (set-buffer gnus-group-buffer)
11342             (gnus-undo-register
11343               `(progn
11344                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11345                  (gnus-info-set-read ',info ',(gnus-info-read info))
11346                  (gnus-get-unread-articles-in-group ',info
11347                                                     (gnus-active ,group))
11348                  (gnus-group-update-group ,group t)
11349                  ,setmarkundo))))
11350         ;; Enter this list into the group info.
11351         (gnus-info-set-read info read)
11352         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11353         (gnus-get-unread-articles-in-group info (gnus-active group))
11354         t))))
11355
11356 (defun gnus-offer-save-summaries ()
11357   "Offer to save all active summary buffers."
11358   (let (buffers)
11359     ;; Go through all buffers and find all summaries.
11360     (dolist (buffer (buffer-list))
11361       (when (and (setq buffer (buffer-name buffer))
11362                  (string-match "Summary" buffer)
11363                  (save-excursion
11364                    (set-buffer buffer)
11365                    ;; We check that this is, indeed, a summary buffer.
11366                    (and (eq major-mode 'gnus-summary-mode)
11367                         ;; Also make sure this isn't bogus.
11368                         gnus-newsgroup-prepared
11369                         ;; Also make sure that this isn't a
11370                         ;; dead summary buffer.
11371                         (not gnus-dead-summary-mode))))
11372         (push buffer buffers)))
11373     ;; Go through all these summary buffers and offer to save them.
11374     (when buffers
11375       (save-excursion
11376         (map-y-or-n-p
11377          "Update summary buffer %s? "
11378          (lambda (buf)
11379            (switch-to-buffer buf)
11380            (gnus-summary-exit))
11381          buffers)))))
11382
11383 (defun gnus-summary-setup-default-charset ()
11384   "Setup newsgroup default charset."
11385   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11386       (setq gnus-newsgroup-charset nil)
11387     (let* ((ignored-charsets
11388             (or gnus-newsgroup-ephemeral-ignored-charsets
11389                 (append
11390                  (and gnus-newsgroup-name
11391                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11392                  gnus-newsgroup-ignored-charsets))))
11393       (setq gnus-newsgroup-charset
11394             (or gnus-newsgroup-ephemeral-charset
11395                 (and gnus-newsgroup-name
11396                      (gnus-parameter-charset gnus-newsgroup-name))
11397                 gnus-default-charset))
11398       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11399            ignored-charsets))))
11400
11401 ;;;
11402 ;;; Mime Commands
11403 ;;;
11404
11405 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11406   "Display the current article buffer fully MIME-buttonized.
11407 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11408 treated as multipart/mixed."
11409   (interactive "P")
11410   (require 'gnus-art)
11411   (let ((gnus-unbuttonized-mime-types nil)
11412         (gnus-mime-display-multipart-as-mixed show-all-parts))
11413     (gnus-summary-show-article)))
11414
11415 (defun gnus-summary-repair-multipart (article)
11416   "Add a Content-Type header to a multipart article without one."
11417   (interactive (list (gnus-summary-article-number)))
11418   (gnus-with-article article
11419     (message-narrow-to-head)
11420     (message-remove-header "Mime-Version")
11421     (goto-char (point-max))
11422     (insert "Mime-Version: 1.0\n")
11423     (widen)
11424     (when (search-forward "\n--" nil t)
11425       (let ((separator (buffer-substring (point) (point-at-eol))))
11426         (message-narrow-to-head)
11427         (message-remove-header "Content-Type")
11428         (goto-char (point-max))
11429         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11430                         separator))
11431         (widen))))
11432   (let (gnus-mark-article-hook)
11433     (gnus-summary-select-article t t nil article)))
11434
11435 (defun gnus-summary-toggle-display-buttonized ()
11436   "Toggle the buttonizing of the article buffer."
11437   (interactive)
11438   (require 'gnus-art)
11439   (if (setq gnus-inhibit-mime-unbuttonizing
11440             (not gnus-inhibit-mime-unbuttonizing))
11441       (let ((gnus-unbuttonized-mime-types nil))
11442         (gnus-summary-show-article))
11443     (gnus-summary-show-article)))
11444
11445 ;;;
11446 ;;; Generic summary marking commands
11447 ;;;
11448
11449 (defvar gnus-summary-marking-alist
11450   '((read gnus-del-mark "d")
11451     (unread gnus-unread-mark "u")
11452     (ticked gnus-ticked-mark "!")
11453     (dormant gnus-dormant-mark "?")
11454     (expirable gnus-expirable-mark "e"))
11455   "An alist of names/marks/keystrokes.")
11456
11457 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11458 (defvar gnus-summary-mark-map)
11459
11460 (defun gnus-summary-make-all-marking-commands ()
11461   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11462   (dolist (elem gnus-summary-marking-alist)
11463     (apply 'gnus-summary-make-marking-command elem)))
11464
11465 (defun gnus-summary-make-marking-command (name mark keystroke)
11466   (let ((map (make-sparse-keymap)))
11467     (define-key gnus-summary-generic-mark-map keystroke map)
11468     (dolist (lway `((next "next" next nil "n")
11469                     (next-unread "next unread" next t "N")
11470                     (prev "previous" prev nil "p")
11471                     (prev-unread "previous unread" prev t "P")
11472                     (nomove "" nil nil ,keystroke)))
11473       (let ((func (gnus-summary-make-marking-command-1
11474                    mark (car lway) lway name)))
11475         (setq func (eval func))
11476         (define-key map (nth 4 lway) func)))))
11477
11478 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11479   `(defun ,(intern
11480             (format "gnus-summary-put-mark-as-%s%s"
11481                     name (if (eq way 'nomove)
11482                              ""
11483                            (concat "-" (symbol-name way)))))
11484      (n)
11485      ,(format
11486        "Mark the current article as %s%s.
11487 If N, the prefix, then repeat N times.
11488 If N is negative, move in reverse order.
11489 The difference between N and the actual number of articles marked is
11490 returned."
11491        name (cadr lway))
11492      (interactive "p")
11493      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11494
11495 (defun gnus-summary-generic-mark (n mark move unread)
11496   "Mark N articles with MARK."
11497   (unless (eq major-mode 'gnus-summary-mode)
11498     (error "This command can only be used in the summary buffer"))
11499   (gnus-summary-show-thread)
11500   (let ((nummove
11501          (cond
11502           ((eq move 'next) 1)
11503           ((eq move 'prev) -1)
11504           (t 0))))
11505     (if (zerop nummove)
11506         (setq n 1)
11507       (when (< n 0)
11508         (setq n (abs n)
11509               nummove (* -1 nummove))))
11510     (while (and (> n 0)
11511                 (gnus-summary-mark-article nil mark)
11512                 (zerop (gnus-summary-next-subject nummove unread t)))
11513       (setq n (1- n)))
11514     (when (/= 0 n)
11515       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11516     (gnus-summary-recenter)
11517     (gnus-summary-position-point)
11518     (gnus-set-mode-line 'summary)
11519     n))
11520
11521 (defun gnus-summary-insert-articles (articles)
11522   (when (setq articles
11523               (gnus-sorted-difference articles
11524                                       (mapcar (lambda (h)
11525                                                 (mail-header-number h))
11526                                               gnus-newsgroup-headers)))
11527     (setq gnus-newsgroup-headers
11528           (gnus-merge 'list
11529                       gnus-newsgroup-headers
11530                       (gnus-fetch-headers articles)
11531                       'gnus-article-sort-by-number))
11532     ;; Suppress duplicates?
11533     (when gnus-suppress-duplicates
11534       (gnus-dup-suppress-articles))
11535
11536     ;; We might want to build some more threads first.
11537     (when (and gnus-fetch-old-headers
11538                (eq gnus-headers-retrieved-by 'nov))
11539       (if (eq gnus-fetch-old-headers 'invisible)
11540           (gnus-build-all-threads)
11541         (gnus-build-old-threads)))
11542     ;; Let the Gnus agent mark articles as read.
11543     (when gnus-agent
11544       (gnus-agent-get-undownloaded-list))
11545     ;; Remove list identifiers from subject
11546     (when gnus-list-identifiers
11547       (gnus-summary-remove-list-identifiers))
11548     ;; First and last article in this newsgroup.
11549     (when gnus-newsgroup-headers
11550       (setq gnus-newsgroup-begin
11551             (mail-header-number (car gnus-newsgroup-headers))
11552             gnus-newsgroup-end
11553             (mail-header-number
11554              (gnus-last-element gnus-newsgroup-headers))))
11555     (when gnus-use-scoring
11556       (gnus-possibly-score-headers))))
11557
11558 (defun gnus-summary-insert-old-articles (&optional all)
11559   "Insert all old articles in this group.
11560 If ALL is non-nil, already read articles become readable.
11561 If ALL is a number, fetch this number of articles."
11562   (interactive "P")
11563   (prog1
11564       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11565             older len)
11566         (setq older
11567               ;; Some nntp servers lie about their active range.  When
11568               ;; this happens, the active range can be in the millions.
11569               ;; Use a compressed range to avoid creating a huge list.
11570               (gnus-range-difference (list gnus-newsgroup-active) old))
11571         (setq len (gnus-range-length older))
11572         (cond
11573          ((null older) nil)
11574          ((numberp all)
11575           (if (< all len)
11576               (let ((older-range (nreverse older)))
11577                 (setq older nil)
11578
11579                 (while (> all 0)
11580                   (let* ((r (pop older-range))
11581                          (min (if (numberp r) r (car r)))
11582                          (max (if (numberp r) r (cdr r))))
11583                     (while (and (<= min max)
11584                                 (> all 0))
11585                       (push max older)
11586                       (setq all (1- all)
11587                             max (1- max))))))
11588             (setq older (gnus-uncompress-range older))))
11589          (all
11590           (setq older (gnus-uncompress-range older)))
11591          (t
11592           (when (and (numberp gnus-large-newsgroup)
11593                    (> len gnus-large-newsgroup))
11594               (let* ((cursor-in-echo-area nil)
11595                      (initial (gnus-parameter-large-newsgroup-initial
11596                                gnus-newsgroup-name))
11597                      (input
11598                       (read-string
11599                        (format
11600                         "How many articles from %s (%s %d): "
11601                         (gnus-limit-string
11602                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11603                         (if initial "max" "default")
11604                         len)
11605                        (if initial
11606                            (cons (number-to-string initial)
11607                                  0)))))
11608                 (unless (string-match "^[ \t]*$" input)
11609                   (setq all (string-to-number input))
11610                   (if (< all len)
11611                       (let ((older-range (nreverse older)))
11612                         (setq older nil)
11613
11614                         (while (> all 0)
11615                           (let* ((r (pop older-range))
11616                                  (min (if (numberp r) r (car r)))
11617                                  (max (if (numberp r) r (cdr r))))
11618                             (while (and (<= min max)
11619                                         (> all 0))
11620                               (push max older)
11621                               (setq all (1- all)
11622                                     max (1- max))))))))))
11623           (setq older (gnus-uncompress-range older))))
11624         (if (not older)
11625             (message "No old news.")
11626           (gnus-summary-insert-articles older)
11627           (gnus-summary-limit (gnus-sorted-nunion old older))))
11628     (gnus-summary-position-point)))
11629
11630 (defun gnus-summary-insert-new-articles ()
11631   "Insert all new articles in this group."
11632   (interactive)
11633   (prog1
11634       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11635             (old-active gnus-newsgroup-active)
11636             (nnmail-fetched-sources (list t))
11637             i new)
11638         (setq gnus-newsgroup-active
11639               (gnus-activate-group gnus-newsgroup-name 'scan))
11640         (setq i (cdr gnus-newsgroup-active))
11641         (while (> i (cdr old-active))
11642           (push i new)
11643           (decf i))
11644         (if (not new)
11645             (message "No gnus is bad news")
11646           (gnus-summary-insert-articles new)
11647           (setq gnus-newsgroup-unreads
11648                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11649           (gnus-summary-limit (gnus-sorted-nunion old new))))
11650     (gnus-summary-position-point)))
11651
11652 (gnus-summary-make-all-marking-commands)
11653
11654 (gnus-ems-redefine)
11655
11656 (provide 'gnus-sum)
11657
11658 (run-hooks 'gnus-sum-load-hook)
11659
11660 ;; Local Variables:
11661 ;; coding: iso-8859-1
11662 ;; End:
11663
11664 ;;; gnus-sum.el ends here