(gnus-summary-exit): Kill the correct article buffer on exit from a `C-d' group.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2011 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 3 of the License, or
13 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (eval-when-compile
33   (when (featurep 'xemacs)
34     (require 'easy-mmode))) ; for `define-minor-mode'
35
36 (defvar tool-bar-mode)
37 (defvar gnus-tmp-header)
38
39 (require 'gnus)
40 (require 'gnus-group)
41 (require 'gnus-spec)
42 (require 'gnus-range)
43 (require 'gnus-int)
44 (require 'gnus-undo)
45 (require 'gnus-util)
46 (require 'gmm-utils)
47 (require 'mm-decode)
48 (require 'nnoo)
49
50 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
51 (autoload 'gnus-cache-write-active "gnus-cache")
52 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
53 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
54 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
55 (autoload 'mm-uu-dissect "mm-uu")
56 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
57   "Deuglify broken Outlook (Express) articles and redisplay."
58   t)
59 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
60 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
61 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
62 (autoload 'nnir-article-rsv "nnir" nil nil 'macro)
63 (autoload 'nnir-article-group "nnir" nil nil 'macro)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-summary-next-group-on-exit t
73   "If non-nil, go to the next unread newsgroup on summary exit.
74 See `gnus-group-goto-unread'."
75   :link '(custom-manual "(gnus)Group Maneuvering")
76   :group 'gnus-summary-exit
77   :version "23.1" ;; No Gnus
78   :type 'boolean)
79
80 (defcustom gnus-summary-stop-at-end-of-message nil
81   "If non-nil, don't select the next message when using `SPC'."
82   :link '(custom-manual "(gnus)Group Maneuvering")
83   :group 'gnus-summary-maneuvering
84   :version "24.1"
85   :type 'boolean)
86
87 (defcustom gnus-fetch-old-headers nil
88   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
89 If an unread article in the group refers to an older, already
90 read (or just marked as read) article, the old article will not
91 normally be displayed in the Summary buffer.  If this variable is
92 t, Gnus will attempt to grab the headers to the old articles, and
93 thereby build complete threads.  If it has the value `some', all
94 old headers will be fetched but only enough headers to connect
95 otherwise loose threads will be displayed.  This variable can
96 also be a number.  In that case, no more than that number of old
97 headers will be fetched.  If it has the value `invisible', all
98 old headers will be fetched, but none will be displayed.
99
100 The server has to support NOV for any of this to work.
101
102 This feature can seriously impact performance it ignores all
103 locally cached header entries.  Setting it to t for groups for a
104 server that doesn't expire articles (such as news.gmane.org),
105 leads to very slow summary generation."
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const :tag "on" t)
109                  (const some)
110                  (const invisible)
111                  number
112                  (sexp :menu-tag "other" t)))
113
114 (defcustom gnus-refer-thread-limit 500
115   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
116 If t, fetch all the available old headers."
117   :group 'gnus-thread
118   :type '(choice number
119                  (sexp :menu-tag "other" t)))
120
121 (defcustom gnus-summary-make-false-root 'adopt
122   "*nil means that Gnus won't gather loose threads.
123 If the root of a thread has expired or been read in a previous
124 session, the information necessary to build a complete thread has been
125 lost.  Instead of having many small sub-threads from this original thread
126 scattered all over the summary buffer, Gnus can gather them.
127
128 If non-nil, Gnus will try to gather all loose sub-threads from an
129 original thread into one large thread.
130
131 If this variable is non-nil, it should be one of `none', `adopt',
132 `dummy' or `empty'.
133
134 If this variable is `none', Gnus will not make a false root, but just
135 present the sub-threads after another.
136 If this variable is `dummy', Gnus will create a dummy root that will
137 have all the sub-threads as children.
138 If this variable is `adopt', Gnus will make one of the \"children\"
139 the parent and mark all the step-children as such.
140 If this variable is `empty', the \"children\" are printed with empty
141 subject fields.  (Or rather, they will be printed with a string
142 given by the `gnus-summary-same-subject' variable.)"
143   :group 'gnus-thread
144   :type '(choice (const :tag "off" nil)
145                  (const none)
146                  (const dummy)
147                  (const adopt)
148                  (const empty)))
149
150 (defcustom gnus-summary-make-false-root-always nil
151   "Always make a false dummy root."
152   :version "22.1"
153   :group 'gnus-thread
154   :type 'boolean)
155
156 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
157   "*A regexp to match subjects to be excluded from loose thread gathering.
158 As loose thread gathering is done on subjects only, that means that
159 there can be many false gatherings performed.  By rooting out certain
160 common subjects, gathering might become saner."
161   :group 'gnus-thread
162   :type 'regexp)
163
164 (defcustom gnus-summary-gather-subject-limit nil
165   "*Maximum length of subject comparisons when gathering loose threads.
166 Use nil to compare full subjects.  Setting this variable to a low
167 number will help gather threads that have been corrupted by
168 newsreaders chopping off subject lines, but it might also mean that
169 unrelated articles that have subject that happen to begin with the
170 same few characters will be incorrectly gathered.
171
172 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
173 comparing subjects."
174   :group 'gnus-thread
175   :type '(choice (const :tag "off" nil)
176                  (const fuzzy)
177                  (sexp :menu-tag "on" t)))
178
179 (defcustom gnus-simplify-subject-functions nil
180   "List of functions taking a string argument that simplify subjects.
181 The functions are applied recursively.
182
183 Useful functions to put in this list include:
184 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
185 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
186   :group 'gnus-thread
187   :type '(repeat function))
188
189 (defcustom gnus-simplify-ignored-prefixes nil
190   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
191   :group 'gnus-thread
192   :type '(choice (const :tag "off" nil)
193                  regexp))
194
195 (defcustom gnus-build-sparse-threads nil
196   "*If non-nil, fill in the gaps in threads.
197 If `some', only fill in the gaps that are needed to tie loose threads
198 together.  If `more', fill in all leaf nodes that Gnus can find.  If
199 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
200   :group 'gnus-thread
201   :type '(choice (const :tag "off" nil)
202                  (const some)
203                  (const more)
204                  (sexp :menu-tag "all" t)))
205
206 (defcustom gnus-summary-thread-gathering-function
207   'gnus-gather-threads-by-subject
208   "*Function used for gathering loose threads.
209 There are two pre-defined functions: `gnus-gather-threads-by-subject',
210 which only takes Subjects into consideration; and
211 `gnus-gather-threads-by-references', which compared the References
212 headers of the articles to find matches."
213   :group 'gnus-thread
214   :type '(radio (function-item gnus-gather-threads-by-subject)
215                 (function-item gnus-gather-threads-by-references)
216                 (function :tag "other")))
217
218 (defcustom gnus-summary-same-subject ""
219   "*String indicating that the current article has the same subject as the previous.
220 This variable will only be used if the value of
221 `gnus-summary-make-false-root' is `empty'."
222   :group 'gnus-summary-format
223   :type 'string)
224
225 (defcustom gnus-summary-goto-unread nil
226   "*If t, many commands will go to the next unread article.
227 This applies to marking commands as well as other commands that
228 \"naturally\" select the next article, like, for instance, `SPC' at
229 the end of an article.
230
231 If nil, the marking commands do NOT go to the next unread article
232 \(they go to the next article instead).  If `never', commands that
233 usually go to the next unread article, will go to the next article,
234 whether it is read or not."
235   :version "24.1"
236   :group 'gnus-summary-marks
237   :link '(custom-manual "(gnus)Setting Marks")
238   :type '(choice (const :tag "off" nil)
239                  (const never)
240                  (sexp :menu-tag "on" t)))
241
242 (defcustom gnus-summary-default-score 0
243   "*Default article score level.
244 All scores generated by the score files will be added to this score.
245 If this variable is nil, scoring will be disabled."
246   :group 'gnus-score-default
247   :type '(choice (const :tag "disable")
248                  integer))
249
250 (defcustom gnus-summary-default-high-score 0
251   "*Default threshold for a high scored article.
252 An article will be highlighted as high scored if its score is greater
253 than this score."
254   :version "22.1"
255   :group 'gnus-score-default
256   :type 'integer)
257
258 (defcustom gnus-summary-default-low-score 0
259   "*Default threshold for a low scored article.
260 An article will be highlighted as low scored if its score is smaller
261 than this score."
262   :version "22.1"
263   :group 'gnus-score-default
264   :type 'integer)
265
266 (defcustom gnus-summary-zcore-fuzz 0
267   "*Fuzziness factor for the zcore in the summary buffer.
268 Articles with scores closer than this to `gnus-summary-default-score'
269 will not be marked."
270   :group 'gnus-summary-format
271   :type 'integer)
272
273 (defcustom gnus-simplify-subject-fuzzy-regexp nil
274   "*Strings to be removed when doing fuzzy matches.
275 This can either be a regular expression or list of regular expressions
276 that will be removed from subject strings if fuzzy subject
277 simplification is selected."
278   :group 'gnus-thread
279   :type '(repeat regexp))
280
281 (defcustom gnus-show-threads t
282   "*If non-nil, display threads in summary mode."
283   :group 'gnus-thread
284   :type 'boolean)
285
286 (defcustom gnus-thread-hide-subtree nil
287   "*If non-nil, hide all threads initially.
288 This can be a predicate specifier which says which threads to hide.
289 If threads are hidden, you have to run the command
290 `gnus-summary-show-thread' by hand or select an article."
291   :group 'gnus-thread
292   :type '(radio (sexp :format "Non-nil\n"
293                       :match (lambda (widget value)
294                                (not (or (consp value) (functionp value))))
295                       :value t)
296                 (const nil)
297                 (sexp :tag "Predicate specifier")))
298
299 (defcustom gnus-thread-hide-killed t
300   "*If non-nil, hide killed threads automatically."
301   :group 'gnus-thread
302   :type 'boolean)
303
304 (defcustom gnus-thread-ignore-subject t
305   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
306 If nil, articles that have different subjects from their parents will
307 start separate threads."
308   :group 'gnus-thread
309   :type 'boolean)
310
311 (defcustom gnus-thread-operation-ignore-subject t
312   "*If non-nil, subjects will be ignored when doing thread commands.
313 This affects commands like `gnus-summary-kill-thread' and
314 `gnus-summary-lower-thread'.
315
316 If this variable is nil, articles in the same thread with different
317 subjects will not be included in the operation in question.  If this
318 variable is `fuzzy', only articles that have subjects that are fuzzily
319 equal will be included."
320   :group 'gnus-thread
321   :type '(choice (const :tag "off" nil)
322                  (const fuzzy)
323                  (sexp :tag "on" t)))
324
325 (defcustom gnus-thread-indent-level 4
326   "*Number that says how much each sub-thread should be indented."
327   :group 'gnus-thread
328   :type 'integer)
329
330 (defcustom gnus-auto-extend-newsgroup t
331   "*If non-nil, extend newsgroup forward and backward when requested."
332   :group 'gnus-summary-choose
333   :type 'boolean)
334
335 (defcustom gnus-auto-select-first t
336   "If non-nil, select an article on group entry.
337 An article is selected automatically when entering a group
338 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
339 `gnus-summary-catchup-and-goto-next-group'.
340
341 Which article is selected is controlled by the variable
342 `gnus-auto-select-subject'.
343
344 If you want to prevent automatic selection of articles in some
345 newsgroups, set the variable to nil in `gnus-select-group-hook'."
346   ;; Commands include...
347   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
348   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
349   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
350   :group 'gnus-group-select
351   :type '(choice (const :tag "none" nil)
352                  (sexp :menu-tag "first" t)))
353
354 (defcustom gnus-auto-select-subject 'unseen-or-unread
355   "*Says what subject to place under point when entering a group.
356
357 This variable can either be the symbols `first' (place point on the
358 first subject), `unread' (place point on the subject line of the first
359 unread article), `best' (place point on the subject line of the
360 higest-scored article), `unseen' (place point on the subject line of
361 the first unseen article), `unseen-or-unread' (place point on the subject
362 line of the first unseen article or, if all articles have been seen, on the
363 subject line of the first unread article), or a function to be called to
364 place point on some subject line."
365   :version "24.1"
366   :group 'gnus-group-select
367   :type '(choice (const best)
368                  (const unread)
369                  (const first)
370                  (const unseen)
371                  (const unseen-or-unread)))
372
373 (defcustom gnus-auto-select-next t
374   "*If non-nil, offer to go to the next group from the end of the previous.
375 If the value is t and the next newsgroup is empty, Gnus will exit
376 summary mode and go back to group mode.  If the value is neither nil
377 nor t, Gnus will select the following unread newsgroup.  In
378 particular, if the value is the symbol `quietly', the next unread
379 newsgroup will be selected without any confirmation, and if it is
380 `almost-quietly', the next group will be selected without any
381 confirmation if you are located on the last article in the group.
382 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
383 will go to the next group without confirmation."
384   :group 'gnus-summary-maneuvering
385   :type '(choice (const :tag "off" nil)
386                  (const quietly)
387                  (const almost-quietly)
388                  (const slightly-quietly)
389                  (sexp :menu-tag "on" t)))
390
391 (defcustom gnus-auto-select-same nil
392   "*If non-nil, select the next article with the same subject.
393 If there are no more articles with the same subject, go to
394 the first unread article."
395   :group 'gnus-summary-maneuvering
396   :type 'boolean)
397
398 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
399   "What article should be selected after exiting an ephemeral group.
400 Valid values include:
401
402 `next'
403   Select the next article.
404 `next-unread'
405   Select the next unread article.
406 `next-noselect'
407   Move the cursor to the next article.  This is the default.
408 `next-unread-noselect'
409   Move the cursor to the next unread article.
410
411 If it has any other value or there is no next (unread) article, the
412 article selected before entering to the ephemeral group will appear."
413   :version "23.1" ;; No Gnus
414   :group 'gnus-summary-maneuvering
415   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
416                  (const next) (const next-unread)
417                  (const next-noselect) (const next-unread-noselect)
418                  (sexp :tag "other" :value nil)))
419
420 (defcustom gnus-auto-goto-ignores 'unfetched
421   "*Says how to handle unfetched articles when maneuvering.
422
423 This variable can either be the symbols nil (maneuver to any
424 article), `undownloaded' (maneuvering while unplugged ignores articles
425 that have not been fetched), `always-undownloaded' (maneuvering always
426 ignores articles that have not been fetched), `unfetched' (maneuvering
427 ignores articles whose headers have not been fetched).
428
429 NOTE: The list of unfetched articles will always be nil when plugged
430 and, when unplugged, a subset of the undownloaded article list."
431   :version "22.1"
432   :group 'gnus-summary-maneuvering
433   :type '(choice (const :tag "None" nil)
434                  (const :tag "Undownloaded when unplugged" undownloaded)
435                  (const :tag "Undownloaded" always-undownloaded)
436                  (const :tag "Unfetched" unfetched)))
437
438 (defcustom gnus-summary-check-current nil
439   "*If non-nil, consider the current article when moving.
440 The \"unread\" movement commands will stay on the same line if the
441 current article is unread."
442   :group 'gnus-summary-maneuvering
443   :type 'boolean)
444
445 (defcustom gnus-auto-center-summary 2
446   "*If non-nil, always center the current summary buffer.
447 In particular, if `vertical' do only vertical recentering.  If non-nil
448 and non-`vertical', do both horizontal and vertical recentering."
449   :group 'gnus-summary-maneuvering
450   :type '(choice (const :tag "none" nil)
451                  (const vertical)
452                  (integer :tag "height")
453                  (sexp :menu-tag "both" t)))
454
455 (defcustom gnus-auto-center-group t
456   "If non-nil, always center the group buffer."
457   :group 'gnus-summary-maneuvering
458   :type 'boolean)
459
460 (defcustom gnus-show-all-headers nil
461   "*If non-nil, don't hide any headers."
462   :group 'gnus-article-hiding
463   :group 'gnus-article-headers
464   :type 'boolean)
465
466 (defcustom gnus-summary-ignore-duplicates nil
467   "*If non-nil, ignore articles with identical Message-ID headers."
468   :group 'gnus-summary
469   :type 'boolean)
470
471 (defcustom gnus-single-article-buffer nil
472   "*If non-nil, display all articles in the same buffer.
473 If nil, each group will get its own article buffer."
474   :version "24.1"
475   :group 'gnus-article-various
476   :type 'boolean)
477
478 (defcustom gnus-widen-article-window nil
479   "If non-nil, selecting the article buffer will display only the article buffer."
480   :version "24.1"
481   :group 'gnus-article-various
482   :type 'boolean)
483
484 (defcustom gnus-break-pages t
485   "*If non-nil, do page breaking on articles.
486 The page delimiter is specified by the `gnus-page-delimiter'
487 variable."
488   :group 'gnus-article-various
489   :type 'boolean)
490
491 (defcustom gnus-move-split-methods nil
492   "*Variable used to suggest where articles are to be moved to.
493 It uses the same syntax as the `gnus-split-methods' variable.
494 However, whereas `gnus-split-methods' specifies file names as targets,
495 this variable specifies group names."
496   :group 'gnus-summary-mail
497   :type '(repeat (choice (list :value (fun) function)
498                          (cons :value ("" "") regexp (repeat string))
499                          (sexp :value nil))))
500
501 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
502   "Function used to compute default prefix for article move/copy/etc prompts.
503 The function should take one argument, a group name, and return a
504 string with the suggested prefix."
505   :group 'gnus-summary-mail
506   :type 'function)
507
508 ;; FIXME: Although the custom type is `character' for the following variables,
509 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
510
511 (defcustom gnus-unread-mark ?           ;Whitespace
512   "*Mark used for unread articles."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-ticked-mark ?!
517   "*Mark used for ticked articles."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-dormant-mark ??
522   "*Mark used for dormant articles."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-del-mark ?r
527   "*Mark used for del'd articles."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-read-mark ?R
532   "*Mark used for read articles."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-expirable-mark ?E
537   "*Mark used for expirable articles."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-killed-mark ?K
542   "*Mark used for killed articles."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-spam-mark ?$
547   "*Mark used for spam articles."
548   :version "22.1"
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-kill-file-mark ?X
553   "*Mark used for articles killed by kill files."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-low-score-mark ?Y
558   "*Mark used for articles with a low score."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-catchup-mark ?C
563   "*Mark used for articles that are caught up."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-replied-mark ?A
568   "*Mark used for articles that have been replied to."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-forwarded-mark ?F
573   "*Mark used for articles that have been forwarded."
574   :version "22.1"
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-recent-mark ?N
579   "*Mark used for articles that are recent."
580   :version "22.1"
581   :group 'gnus-summary-marks
582   :type 'character)
583
584 (defcustom gnus-cached-mark ?*
585   "*Mark used for articles that are in the cache."
586   :group 'gnus-summary-marks
587   :type 'character)
588
589 (defcustom gnus-saved-mark ?S
590   "*Mark used for articles that have been saved."
591   :group 'gnus-summary-marks
592   :type 'character)
593
594 (defcustom gnus-unseen-mark ?.
595   "*Mark used for articles that haven't been seen."
596   :version "22.1"
597   :group 'gnus-summary-marks
598   :type 'character)
599
600 (defcustom gnus-no-mark ?               ;Whitespace
601   "*Mark used for articles that have no other secondary mark."
602   :version "22.1"
603   :group 'gnus-summary-marks
604   :type 'character)
605
606 (defcustom gnus-ancient-mark ?O
607   "*Mark used for ancient articles."
608   :group 'gnus-summary-marks
609   :type 'character)
610
611 (defcustom gnus-sparse-mark ?Q
612   "*Mark used for sparsely reffed articles."
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-canceled-mark ?G
617   "*Mark used for canceled articles."
618   :group 'gnus-summary-marks
619   :type 'character)
620
621 (defcustom gnus-duplicate-mark ?M
622   "*Mark used for duplicate articles."
623   :group 'gnus-summary-marks
624   :type 'character)
625
626 (defcustom gnus-undownloaded-mark ?-
627   "*Mark used for articles that weren't downloaded."
628   :version "22.1"
629   :group 'gnus-summary-marks
630   :type 'character)
631
632 (defcustom gnus-downloaded-mark ?+
633   "*Mark used for articles that were downloaded."
634   :group 'gnus-summary-marks
635   :type 'character)
636
637 (defcustom gnus-downloadable-mark ?%
638   "*Mark used for articles that are to be downloaded."
639   :group 'gnus-summary-marks
640   :type 'character)
641
642 (defcustom gnus-unsendable-mark ?=
643   "*Mark used for articles that won't be sent."
644   :group 'gnus-summary-marks
645   :type 'character)
646
647 (defcustom gnus-score-over-mark ?+
648   "*Score mark used for articles with high scores."
649   :group 'gnus-summary-marks
650   :type 'character)
651
652 (defcustom gnus-score-below-mark ?-
653   "*Score mark used for articles with low scores."
654   :group 'gnus-summary-marks
655   :type 'character)
656
657 (defcustom gnus-empty-thread-mark ?     ;Whitespace
658   "*There is no thread under the article."
659   :group 'gnus-summary-marks
660   :type 'character)
661
662 (defcustom gnus-not-empty-thread-mark ?=
663   "*There is a thread under the article."
664   :group 'gnus-summary-marks
665   :type 'character)
666
667 (defcustom gnus-view-pseudo-asynchronously nil
668   "*If non-nil, Gnus will view pseudo-articles asynchronously."
669   :group 'gnus-extract-view
670   :type 'boolean)
671
672 (defcustom gnus-auto-expirable-marks
673   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
674         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
675         gnus-duplicate-mark)
676   "*The list of marks converted into expiration if a group is auto-expirable."
677   :version "24.1"
678   :group 'gnus-summary
679   :type '(repeat character))
680
681 (defcustom gnus-inhibit-user-auto-expire t
682   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
683   :version "21.1"
684   :group 'gnus-summary
685   :type 'boolean)
686
687 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
688   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
689 If nil, the expirable marks will be unchanged except that the marks
690 will be removed when copying or moving articles to a group that has
691 not turned auto-expire on.  If non-nil, articles that have been read
692 will be marked as expirable when being copied or moved to a group in
693 which auto-expire is turned on."
694   :version "23.2"
695   :type 'boolean
696   :group 'gnus-summary-marks)
697
698 (defcustom gnus-view-pseudos nil
699   "*If `automatic', pseudo-articles will be viewed automatically.
700 If `not-confirm', pseudos will be viewed automatically, and the user
701 will not be asked to confirm the command."
702   :group 'gnus-extract-view
703   :type '(choice (const :tag "off" nil)
704                  (const automatic)
705                  (const not-confirm)))
706
707 (defcustom gnus-view-pseudos-separately t
708   "*If non-nil, one pseudo-article will be created for each file to be viewed.
709 If nil, all files that use the same viewing command will be given as a
710 list of parameters to that command."
711   :group 'gnus-extract-view
712   :type 'boolean)
713
714 (defcustom gnus-insert-pseudo-articles t
715   "*If non-nil, insert pseudo-articles when decoding articles."
716   :group 'gnus-extract-view
717   :type 'boolean)
718
719 (defcustom gnus-summary-dummy-line-format
720   "   %(:                             :%) %S\n"
721   "*The format specification for the dummy roots in the summary buffer.
722 It works along the same lines as a normal formatting string,
723 with some simple extensions.
724
725 %S  The subject
726
727 General format specifiers can also be used.
728 See `(gnus)Formatting Variables'."
729   :link '(custom-manual "(gnus)Formatting Variables")
730   :group 'gnus-threading
731   :type 'string)
732
733 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
734   "*The format specification for the summary mode line.
735 It works along the same lines as a normal formatting string,
736 with some simple extensions:
737
738 %G  Group name
739 %p  Unprefixed group name
740 %A  Current article number
741 %z  Current article score
742 %V  Gnus version
743 %U  Number of unread articles in the group
744 %e  Number of unselected articles in the group
745 %Z  A string with unread/unselected article counts
746 %g  Shortish group name
747 %S  Subject of the current article
748 %u  User-defined spec
749 %s  Current score file name
750 %d  Number of dormant articles
751 %r  Number of articles that have been marked as read in this session
752 %E  Number of articles expunged by the score files"
753   :group 'gnus-summary-format
754   :type 'string)
755
756 (defcustom gnus-list-identifiers nil
757   "Regexp that matches list identifiers to be removed from subject.
758 This can also be a list of regexps."
759   :version "21.1"
760   :group 'gnus-summary-format
761   :group 'gnus-article-hiding
762   :type '(choice (const :tag "none" nil)
763                  (regexp :value ".*")
764                  (repeat :value (".*") regexp)))
765
766 (defcustom gnus-summary-mark-below 0
767   "*Mark all articles with a score below this variable as read.
768 This variable is local to each summary buffer and usually set by the
769 score file."
770   :group 'gnus-score-default
771   :type 'integer)
772
773 (defun gnus-widget-reversible-match (widget value)
774   "Ignoring WIDGET, convert VALUE to internal form.
775 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
776   ;; (debug value)
777   (or (symbolp value)
778       (and (listp value)
779            (eq (length value) 2)
780            (eq (nth 0 value) 'not)
781            (symbolp (nth 1 value)))))
782
783 (defun gnus-widget-reversible-to-internal (widget value)
784   "Ignoring WIDGET, convert VALUE to internal form.
785 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
786 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
787   ;; (debug value)
788   (if (atom value)
789       (list value nil)
790     (list (nth 1 value) t)))
791
792 (defun gnus-widget-reversible-to-external (widget value)
793   "Ignoring WIDGET, convert VALUE to external form.
794 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
795 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
796   ;; (debug value)
797   (if (nth 1 value)
798       (list 'not (nth 0 value))
799     (nth 0 value)))
800
801 (define-widget 'gnus-widget-reversible 'group
802   "A `group' that convert values."
803   :match 'gnus-widget-reversible-match
804   :value-to-internal 'gnus-widget-reversible-to-internal
805   :value-to-external 'gnus-widget-reversible-to-external)
806
807 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
808   "*List of functions used for sorting articles in the summary buffer.
809
810 Each function takes two articles and returns non-nil if the first
811 article should be sorted before the other.  If you use more than one
812 function, the primary sort function should be the last.  You should
813 probably always include `gnus-article-sort-by-number' in the list of
814 sorting functions -- preferably first.  Also note that sorting by date
815 is often much slower than sorting by number, and the sorting order is
816 very similar.  (Sorting by date means sorting by the time the message
817 was sent, sorting by number means sorting by arrival time.)
818
819 Each item can also be a list `(not F)' where F is a function;
820 this reverses the sort order.
821
822 Ready-made functions include `gnus-article-sort-by-number',
823 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
824 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
825 and `gnus-article-sort-by-score'.
826
827 When threading is turned on, the variable `gnus-thread-sort-functions'
828 controls how articles are sorted."
829   :group 'gnus-summary-sort
830   :type '(repeat (gnus-widget-reversible
831                   (choice (function-item gnus-article-sort-by-number)
832                           (function-item gnus-article-sort-by-author)
833                           (function-item gnus-article-sort-by-subject)
834                           (function-item gnus-article-sort-by-date)
835                           (function-item gnus-article-sort-by-score)
836                           (function-item gnus-article-sort-by-random)
837                           (function :tag "other"))
838                   (boolean :tag "Reverse order"))))
839
840
841 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
842   "*List of functions used for sorting threads in the summary buffer.
843 By default, threads are sorted by article number.
844
845 Each function takes two threads and returns non-nil if the first
846 thread should be sorted before the other.  If you use more than one
847 function, the primary sort function should be the last.  You should
848 probably always include `gnus-thread-sort-by-number' in the list of
849 sorting functions -- preferably first.  Also note that sorting by date
850 is often much slower than sorting by number, and the sorting order is
851 very similar.  (Sorting by date means sorting by the time the message
852 was sent, sorting by number means sorting by arrival time.)
853
854 Each list item can also be a list `(not F)' where F is a
855 function; this specifies reversed sort order.
856
857 Ready-made functions include `gnus-thread-sort-by-number',
858 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
859 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
860 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
861 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
862 and `gnus-thread-sort-by-total-score' (see
863 `gnus-thread-score-function').
864
865 When threading is turned off, the variable
866 `gnus-article-sort-functions' controls how articles are sorted."
867   :group 'gnus-summary-sort
868   :type '(repeat
869           (gnus-widget-reversible
870            (choice (function-item gnus-thread-sort-by-number)
871                    (function-item gnus-thread-sort-by-author)
872                    (function-item gnus-thread-sort-by-recipient)
873                    (function-item gnus-thread-sort-by-subject)
874                    (function-item gnus-thread-sort-by-date)
875                    (function-item gnus-thread-sort-by-score)
876                    (function-item gnus-thread-sort-by-most-recent-number)
877                    (function-item gnus-thread-sort-by-most-recent-date)
878                    (function-item gnus-thread-sort-by-random)
879                    (function-item gnus-thread-sort-by-total-score)
880                    (function :tag "other"))
881            (boolean :tag "Reverse order"))))
882
883 (defcustom gnus-thread-score-function '+
884   "*Function used for calculating the total score of a thread.
885
886 The function is called with the scores of the article and each
887 subthread and should then return the score of the thread.
888
889 Some functions you can use are `+', `max', or `min'."
890   :group 'gnus-summary-sort
891   :type 'function)
892
893 (defcustom gnus-summary-expunge-below nil
894   "All articles that have a score less than this variable will be expunged.
895 This variable is local to the summary buffers."
896   :group 'gnus-score-default
897   :type '(choice (const :tag "off" nil)
898                  integer))
899
900 (defcustom gnus-thread-expunge-below nil
901   "All threads that have a total score less than this variable will be expunged.
902 See `gnus-thread-score-function' for en explanation of what a
903 \"thread score\" is.
904
905 This variable is local to the summary buffers."
906   :group 'gnus-threading
907   :group 'gnus-score-default
908   :type '(choice (const :tag "off" nil)
909                  integer))
910
911 (defcustom gnus-summary-mode-hook nil
912   "*A hook for Gnus summary mode.
913 This hook is run before any variables are set in the summary buffer."
914   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
915   :group 'gnus-summary-various
916   :type 'hook)
917
918 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
919 (when (featurep 'xemacs)
920   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
921   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
922   (add-hook 'gnus-summary-mode-hook
923             'gnus-xmas-switch-horizontal-scrollbar-off))
924
925 (defcustom gnus-summary-menu-hook nil
926   "*Hook run after the creation of the summary mode menu."
927   :group 'gnus-summary-visual
928   :type 'hook)
929
930 (defcustom gnus-summary-exit-hook nil
931   "*A hook called on exit from the summary buffer.
932 It will be called with point in the group buffer."
933   :group 'gnus-summary-exit
934   :type 'hook)
935
936 (defcustom gnus-summary-prepare-hook nil
937   "*A hook called after the summary buffer has been generated.
938 If you want to modify the summary buffer, you can use this hook."
939   :group 'gnus-summary-various
940   :type 'hook)
941
942 (defcustom gnus-summary-prepared-hook nil
943   "*A hook called as the last thing after the summary buffer has been generated."
944   :group 'gnus-summary-various
945   :type 'hook)
946
947 (defcustom gnus-summary-generate-hook nil
948   "*A hook run just before generating the summary buffer.
949 This hook is commonly used to customize threading variables and the
950 like."
951   :group 'gnus-summary-various
952   :type 'hook)
953
954 (defcustom gnus-select-group-hook nil
955   "*A hook called when a newsgroup is selected.
956
957 If you'd like to simplify subjects like the
958 `gnus-summary-next-same-subject' command does, you can use the
959 following hook:
960
961  (add-hook gnus-select-group-hook
962            (lambda ()
963              (mapcar (lambda (header)
964                        (mail-header-set-subject
965                         header
966                         (gnus-simplify-subject
967                          (mail-header-subject header) 're-only)))
968                      gnus-newsgroup-headers)))"
969   :group 'gnus-group-select
970   :type 'hook)
971
972 (defcustom gnus-select-article-hook nil
973   "*A hook called when an article is selected."
974   :group 'gnus-summary-choose
975   :options '(gnus-agent-fetch-selected-article)
976   :type 'hook)
977
978 (defcustom gnus-visual-mark-article-hook
979   (list 'gnus-highlight-selected-summary)
980   "*Hook run after selecting an article in the summary buffer.
981 It is meant to be used for highlighting the article in some way.  It
982 is not run if `gnus-visual' is nil."
983   :group 'gnus-summary-visual
984   :type 'hook)
985
986 (defcustom gnus-parse-headers-hook nil
987   "*A hook called before parsing the headers."
988   :group 'gnus-various
989   :type 'hook)
990
991 (defcustom gnus-exit-group-hook nil
992   "*A hook called when exiting summary mode.
993 This hook is not called from the non-updating exit commands like `Q'."
994   :group 'gnus-various
995   :type 'hook)
996
997 (defcustom gnus-summary-update-hook nil
998   "*A hook called when a summary line is changed.
999 The hook will not be called if `gnus-visual' is nil.
1000
1001 The default function `gnus-summary-highlight-line' will
1002 highlight the line according to the `gnus-summary-highlight'
1003 variable."
1004   :group 'gnus-summary-visual
1005   :type 'hook)
1006
1007 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1008   "*A hook called when an article is selected for the first time.
1009 The hook is intended to mark an article as read (or unread)
1010 automatically when it is selected."
1011   :group 'gnus-summary-choose
1012   :type 'hook)
1013
1014 (defcustom gnus-group-no-more-groups-hook nil
1015   "*A hook run when returning to group mode having no more (unread) groups."
1016   :group 'gnus-group-select
1017   :type 'hook)
1018
1019 (defcustom gnus-ps-print-hook nil
1020   "*A hook run before ps-printing something from Gnus."
1021   :group 'gnus-summary
1022   :type 'hook)
1023
1024 (defcustom gnus-summary-article-move-hook nil
1025   "*A hook called after an article is moved, copied, respooled, or crossposted."
1026   :version "22.1"
1027   :group 'gnus-summary
1028   :type 'hook)
1029
1030 (defcustom gnus-summary-article-delete-hook nil
1031   "*A hook called after an article is deleted."
1032   :version "22.1"
1033   :group 'gnus-summary
1034   :type 'hook)
1035
1036 (defcustom gnus-summary-article-expire-hook nil
1037   "*A hook called after an article is expired."
1038   :version "22.1"
1039   :group 'gnus-summary
1040   :type 'hook)
1041
1042 (defcustom gnus-summary-display-arrow
1043   (and (fboundp 'display-graphic-p)
1044        (display-graphic-p))
1045   "*If non-nil, display an arrow highlighting the current article."
1046   :version "22.1"
1047   :group 'gnus-summary
1048   :type 'boolean)
1049
1050 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1051   "Face used for highlighting the current article in the summary buffer."
1052   :group 'gnus-summary-visual
1053   :type 'face)
1054
1055 (defvar gnus-tmp-downloaded nil)
1056
1057 (defcustom gnus-summary-highlight
1058   '(((eq mark gnus-canceled-mark)
1059      . gnus-summary-cancelled)
1060     ((and uncached (> score default-high))
1061      . gnus-summary-high-undownloaded)
1062     ((and uncached (< score default-low))
1063      . gnus-summary-low-undownloaded)
1064     (uncached
1065      . gnus-summary-normal-undownloaded)
1066     ((and (> score default-high)
1067           (or (eq mark gnus-dormant-mark)
1068               (eq mark gnus-ticked-mark)))
1069      . gnus-summary-high-ticked)
1070     ((and (< score default-low)
1071           (or (eq mark gnus-dormant-mark)
1072               (eq mark gnus-ticked-mark)))
1073      . gnus-summary-low-ticked)
1074     ((or (eq mark gnus-dormant-mark)
1075          (eq mark gnus-ticked-mark))
1076      . gnus-summary-normal-ticked)
1077     ((and (> score default-high) (eq mark gnus-ancient-mark))
1078      . gnus-summary-high-ancient)
1079     ((and (< score default-low) (eq mark gnus-ancient-mark))
1080      . gnus-summary-low-ancient)
1081     ((eq mark gnus-ancient-mark)
1082      . gnus-summary-normal-ancient)
1083     ((and (> score default-high) (eq mark gnus-unread-mark))
1084      . gnus-summary-high-unread)
1085     ((and (< score default-low) (eq mark gnus-unread-mark))
1086      . gnus-summary-low-unread)
1087     ((eq mark gnus-unread-mark)
1088      . gnus-summary-normal-unread)
1089     ((> score default-high)
1090      . gnus-summary-high-read)
1091     ((< score default-low)
1092      . gnus-summary-low-read)
1093     (t
1094      . gnus-summary-normal-read))
1095   "*Controls the highlighting of summary buffer lines.
1096
1097 A list of (FORM . FACE) pairs.  When deciding how a particular
1098 summary line should be displayed, each form is evaluated.  The content
1099 of the face field after the first true form is used.  You can change
1100 how those summary lines are displayed, by editing the face field.
1101
1102 You can use the following variables in the FORM field.
1103
1104 score:        The article's score.
1105 default:      The default article score.
1106 default-high: The default score for high scored articles.
1107 default-low:  The default score for low scored articles.
1108 below:        The score below which articles are automatically marked as read.
1109 mark:         The article's mark.
1110 uncached:     Non-nil if the article is uncached."
1111   :group 'gnus-summary-visual
1112   :type '(repeat (cons (sexp :tag "Form" nil)
1113                        face)))
1114 (put 'gnus-summary-highlight 'risky-local-variable t)
1115
1116 (defcustom gnus-alter-header-function nil
1117   "Function called to allow alteration of article header structures.
1118 The function is called with one parameter, the article header vector,
1119 which it may alter in any way."
1120   :type '(choice (const :tag "None" nil)
1121                  function)
1122   :group 'gnus-summary)
1123
1124 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1125   "Function used to decode a string with encoded words.")
1126
1127 (defvar gnus-decode-encoded-address-function
1128   'mail-decode-encoded-address-string
1129   "Function used to decode addresses with encoded words.")
1130
1131 (defcustom gnus-extra-headers '(To Newsgroups)
1132   "*Extra headers to parse."
1133   :version "21.1"
1134   :group 'gnus-summary
1135   :type '(repeat symbol))
1136
1137 (defcustom gnus-ignored-from-addresses
1138   (and user-mail-address
1139        (not (string= user-mail-address ""))
1140        (regexp-quote user-mail-address))
1141   "*From headers that may be suppressed in favor of To headers.
1142 This can be a regexp or a list of regexps."
1143   :version "21.1"
1144   :group 'gnus-summary
1145   :type '(choice regexp
1146                  (repeat :tag "Regexp List" regexp)))
1147
1148 (defsubst gnus-ignored-from-addresses ()
1149   (gmm-regexp-concat gnus-ignored-from-addresses))
1150
1151 (defcustom gnus-summary-to-prefix "-> "
1152   "*String prefixed to the To field in the summary line when
1153 using `gnus-ignored-from-addresses'."
1154   :version "22.1"
1155   :group 'gnus-summary
1156   :type 'string)
1157
1158 (defcustom gnus-summary-newsgroup-prefix "=> "
1159   "*String prefixed to the Newsgroup field in the summary
1160 line when using `gnus-ignored-from-addresses'."
1161   :version "22.1"
1162   :group 'gnus-summary
1163   :type 'string)
1164
1165 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1166   "List of charsets that should be ignored.
1167 When these charsets are used in the \"charset\" parameter, the
1168 default charset will be used instead."
1169   :version "21.1"
1170   :type '(repeat symbol)
1171   :group 'gnus-charset)
1172
1173 (defcustom gnus-newsgroup-maximum-articles nil
1174   "The maximum number of articles a newsgroup.
1175 If this is a number, old articles in a newsgroup exceeding this number
1176 are silently ignored.  If it is nil, no article is ignored.  Note that
1177 setting this variable to a number might prevent you from reading very
1178 old articles."
1179   :group 'gnus-group-select
1180   :version "22.2"
1181   :type '(choice (const :tag "No limit" nil)
1182                  integer))
1183
1184 (gnus-define-group-parameter
1185  ignored-charsets
1186  :type list
1187  :function-document
1188  "Return the ignored charsets of GROUP."
1189  :variable gnus-group-ignored-charsets-alist
1190  :variable-default
1191  '(("alt\\.chinese\\.text" iso-8859-1))
1192  :variable-document
1193  "Alist of regexps (to match group names) and charsets that should be ignored.
1194 When these charsets are used in the \"charset\" parameter, the
1195 default charset will be used instead."
1196  :variable-group gnus-charset
1197  :variable-type '(repeat (cons (regexp :tag "Group")
1198                                (repeat symbol)))
1199  :parameter-type '(choice :tag "Ignored charsets"
1200                           :value nil
1201                           (repeat (symbol)))
1202  :parameter-document       "\
1203 List of charsets that should be ignored.
1204
1205 When these charsets are used in the \"charset\" parameter, the
1206 default charset will be used instead.")
1207
1208 (defcustom gnus-group-highlight-words-alist nil
1209   "Alist of group regexps and highlight regexps.
1210 This variable uses the same syntax as `gnus-emphasis-alist'."
1211   :version "21.1"
1212   :type '(repeat (cons (regexp :tag "Group")
1213                        (repeat (list (regexp :tag "Highlight regexp")
1214                                      (number :tag "Group for entire word" 0)
1215                                      (number :tag "Group for displayed part" 0)
1216                                      (symbol :tag "Face"
1217                                              gnus-emphasis-highlight-words)))))
1218   :group 'gnus-summary-visual)
1219
1220 (defcustom gnus-summary-show-article-charset-alist
1221   nil
1222   "Alist of number and charset.
1223 The article will be shown with the charset corresponding to the
1224 numbered argument.
1225 For example: ((1 . cn-gb-2312) (2 . big5))."
1226   :version "21.1"
1227   :type '(repeat (cons (number :tag "Argument" 1)
1228                        (symbol :tag "Charset")))
1229   :group 'gnus-charset)
1230
1231 (defcustom gnus-preserve-marks t
1232   "Whether marks are preserved when moving, copying and respooling messages."
1233   :version "21.1"
1234   :type 'boolean
1235   :group 'gnus-summary-marks)
1236
1237 (defcustom gnus-propagate-marks nil
1238   "If non-nil, Gnus will store and retrieve marks from the backends.
1239 This means that marks will be stored both in .newsrc.eld and in
1240 the backend, and will slow operation down somewhat."
1241   :version "24.1"
1242   :type 'boolean
1243   :group 'gnus-summary-marks)
1244
1245 (defcustom gnus-alter-articles-to-read-function nil
1246   "Function to be called to alter the list of articles to be selected."
1247   :type '(choice (const nil) function)
1248   :group 'gnus-summary)
1249
1250 (defcustom gnus-orphan-score nil
1251   "*All orphans get this score added.  Set in the score file."
1252   :group 'gnus-score-default
1253   :type '(choice (const nil)
1254                  integer))
1255
1256 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1257   "*A regexp to match MIME parts when saving multiple parts of a
1258 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1259 This regexp will be used by default when prompting the user for which
1260 type of files to save."
1261   :group 'gnus-summary
1262   :type 'regexp)
1263
1264 (defcustom gnus-read-all-available-headers nil
1265   "Whether Gnus should parse all headers made available to it.
1266 This is mostly relevant for slow back ends where the user may
1267 wish to widen the summary buffer to include all headers
1268 that were fetched."
1269   :version "22.1"
1270   :group 'gnus-summary
1271   :type '(choice boolean regexp))
1272
1273 (defcustom gnus-summary-pipe-output-default-command nil
1274   "Command (and optional arguments) used to pipe article to subprocess.
1275 This will be used as the default command if it is non-nil.  The value
1276 will be updated if you modify it when executing the command
1277 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1278   :version "23.1" ;; No Gnus
1279   :group 'gnus-summary
1280   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1281
1282 (defcustom gnus-summary-muttprint-program "muttprint"
1283   "Command (and optional arguments) used to run Muttprint.
1284 The value will be updated if you modify it when executing the command
1285 `gnus-summary-muttprint'."
1286   :version "22.1"
1287   :group 'gnus-summary
1288   :type 'string)
1289
1290 (defcustom gnus-article-loose-mime t
1291   "If non-nil, don't require MIME-Version header.
1292 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1293 supply the MIME-Version header or deliberately strip it from the mail.
1294 If non-nil (the default), Gnus will treat some articles as MIME
1295 even if the MIME-Version header is missing."
1296   :version "22.1"
1297   :type 'boolean
1298   :group 'gnus-article-mime)
1299
1300 (defcustom gnus-article-emulate-mime t
1301   "If non-nil, use MIME emulation for uuencode and the like.
1302 This means that Gnus will search message bodies for text that look
1303 like uuencoded bits, yEncoded bits, and so on, and present that using
1304 the normal Gnus MIME machinery."
1305   :version "22.1"
1306   :type 'boolean
1307   :group 'gnus-article-mime)
1308
1309 ;;; Internal variables
1310
1311 (defvar gnus-summary-display-cache nil)
1312 (defvar gnus-article-mime-handles nil)
1313 (defvar gnus-article-decoded-p nil)
1314 (defvar gnus-article-charset nil)
1315 (defvar gnus-article-ignored-charsets nil)
1316 (defvar gnus-scores-exclude-files nil)
1317 (defvar gnus-page-broken nil)
1318
1319 (defvar gnus-original-article nil)
1320 (defvar gnus-article-internal-prepare-hook nil)
1321 (defvar gnus-newsgroup-process-stack nil)
1322
1323 (defvar gnus-thread-indent-array nil)
1324 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1325 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1326   "Function called to sort the articles within a thread after it has been gathered together.")
1327
1328 (defvar gnus-summary-save-parts-type-history nil)
1329 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1330
1331 ;; Avoid highlighting in kill files.
1332 (defvar gnus-summary-inhibit-highlight nil)
1333 (defvar gnus-newsgroup-selected-overlay nil)
1334 (defvar gnus-inhibit-limiting nil)
1335 (defvar gnus-newsgroup-adaptive-score-file nil)
1336 (defvar gnus-current-score-file nil)
1337 (defvar gnus-current-move-group nil)
1338 (defvar gnus-current-copy-group nil)
1339 (defvar gnus-current-crosspost-group nil)
1340 (defvar gnus-newsgroup-display nil)
1341
1342 (defvar gnus-newsgroup-dependencies nil)
1343 (defvar gnus-newsgroup-adaptive nil)
1344 (defvar gnus-summary-display-article-function nil)
1345 (defvar gnus-summary-highlight-line-function nil
1346   "Function called after highlighting a summary line.")
1347
1348 (defvar gnus-summary-line-format-alist
1349   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1350     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1351     (?s gnus-tmp-subject-or-nil ?s)
1352     (?n gnus-tmp-name ?s)
1353     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1354         ?s)
1355     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1356             gnus-tmp-from) ?s)
1357     (?F gnus-tmp-from ?s)
1358     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1359     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1360     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1361     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1362     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1363     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1364     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1365     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1366     (?L gnus-tmp-lines ?s)
1367     (?Z (or ,(gnus-macroexpand-all
1368               '(nnir-article-rsv (mail-header-number gnus-tmp-header)))
1369             0) ?d)
1370     (?G (or ,(gnus-macroexpand-all
1371               '(nnir-article-group (mail-header-number gnus-tmp-header)))
1372             "") ?s)
1373     (?g (or ,(gnus-macroexpand-all
1374               '(gnus-group-short-name
1375                 (nnir-article-group (mail-header-number gnus-tmp-header))))
1376             "") ?s)
1377     (?O gnus-tmp-downloaded ?c)
1378     (?I gnus-tmp-indentation ?s)
1379     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1380     (?R gnus-tmp-replied ?c)
1381     (?\[ gnus-tmp-opening-bracket ?c)
1382     (?\] gnus-tmp-closing-bracket ?c)
1383     (?\> (make-string gnus-tmp-level ? ) ?s)
1384     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1385     (?i gnus-tmp-score ?d)
1386     (?z gnus-tmp-score-char ?c)
1387     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1388     (?U gnus-tmp-unread ?c)
1389     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1390         ?s)
1391     (?t (gnus-summary-number-of-articles-in-thread
1392          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1393         ?d)
1394     (?e (gnus-summary-number-of-articles-in-thread
1395          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1396         ?c)
1397     (?u gnus-tmp-user-defined ?s)
1398     (?P (gnus-pick-line-number) ?d)
1399     (?B gnus-tmp-thread-tree-header-string ?s)
1400     (user-date (gnus-user-date
1401                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1402   "An alist of format specifications that can appear in summary lines.
1403 These are paired with what variables they correspond with, along with
1404 the type of the variable (string, integer, character, etc).")
1405
1406 (defvar gnus-summary-dummy-line-format-alist
1407   `((?S gnus-tmp-subject ?s)
1408     (?N gnus-tmp-number ?d)
1409     (?u gnus-tmp-user-defined ?s)))
1410
1411 (defvar gnus-summary-mode-line-format-alist
1412   `((?G gnus-tmp-group-name ?s)
1413     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1414     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1415     (?A gnus-tmp-article-number ?d)
1416     (?Z gnus-tmp-unread-and-unselected ?s)
1417     (?V gnus-version ?s)
1418     (?U gnus-tmp-unread-and-unticked ?d)
1419     (?S gnus-tmp-subject ?s)
1420     (?e gnus-tmp-unselected ?d)
1421     (?u gnus-tmp-user-defined ?s)
1422     (?d (length gnus-newsgroup-dormant) ?d)
1423     (?t (length gnus-newsgroup-marked) ?d)
1424     (?h (length gnus-newsgroup-spam-marked) ?d)
1425     (?r (length gnus-newsgroup-reads) ?d)
1426     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1427     (?E gnus-newsgroup-expunged-tally ?d)
1428     (?s (gnus-current-score-file-nondirectory) ?s)))
1429
1430 ;; This is here rather than in gnus-art for compilation reasons.
1431 (defvar gnus-article-mode-line-format-alist
1432   (nconc '((?w (gnus-article-wash-status) ?s)
1433            (?m (gnus-article-mime-part-status) ?s))
1434          gnus-summary-mode-line-format-alist))
1435
1436 (defvar gnus-last-search-regexp nil
1437   "Default regexp for article search command.")
1438
1439 (defvar gnus-last-shell-command nil
1440   "Default shell command on article.")
1441
1442 (defvar gnus-newsgroup-agentized nil
1443   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1444 (defvar gnus-newsgroup-begin nil)
1445 (defvar gnus-newsgroup-end nil)
1446 (defvar gnus-newsgroup-last-rmail nil)
1447 (defvar gnus-newsgroup-last-mail nil)
1448 (defvar gnus-newsgroup-last-folder nil)
1449 (defvar gnus-newsgroup-last-file nil)
1450 (defvar gnus-newsgroup-last-directory nil)
1451 (defvar gnus-newsgroup-auto-expire nil)
1452 (defvar gnus-newsgroup-active nil)
1453 (defvar gnus-newsgroup-highest nil)
1454
1455 (defvar gnus-newsgroup-data nil)
1456 (defvar gnus-newsgroup-data-reverse nil)
1457 (defvar gnus-newsgroup-limit nil)
1458 (defvar gnus-newsgroup-limits nil)
1459 (defvar gnus-summary-use-undownloaded-faces nil)
1460
1461 (defvar gnus-newsgroup-unreads nil
1462   "Sorted list of unread articles in the current newsgroup.")
1463
1464 (defvar gnus-newsgroup-unselected nil
1465   "Sorted list of unselected unread articles in the current newsgroup.")
1466
1467 (defvar gnus-newsgroup-reads nil
1468   "Alist of read articles and article marks in the current newsgroup.")
1469
1470 (defvar gnus-newsgroup-expunged-tally nil)
1471
1472 (defvar gnus-newsgroup-marked nil
1473   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1474
1475 (defvar gnus-newsgroup-spam-marked nil
1476   "List of ranges of articles that have been marked as spam.")
1477
1478 (defvar gnus-newsgroup-killed nil
1479   "List of ranges of articles that have been through the scoring process.")
1480
1481 (defvar gnus-newsgroup-cached nil
1482   "Sorted list of articles that come from the article cache.")
1483
1484 (defvar gnus-newsgroup-saved nil
1485   "List of articles that have been saved.")
1486
1487 (defvar gnus-newsgroup-kill-headers nil)
1488
1489 (defvar gnus-newsgroup-replied nil
1490   "List of articles that have been replied to in the current newsgroup.")
1491
1492 (defvar gnus-newsgroup-forwarded nil
1493   "List of articles that have been forwarded in the current newsgroup.")
1494
1495 (defvar gnus-newsgroup-recent nil
1496   "List of articles that have are recent in the current newsgroup.")
1497
1498 (defvar gnus-newsgroup-expirable nil
1499   "Sorted list of articles in the current newsgroup that can be expired.")
1500
1501 (defvar gnus-newsgroup-processable nil
1502   "List of articles in the current newsgroup that can be processed.")
1503
1504 (defvar gnus-newsgroup-downloadable nil
1505   "Sorted list of articles in the current newsgroup that can be processed.")
1506
1507 (defvar gnus-newsgroup-unfetched nil
1508   "Sorted list of articles in the current newsgroup whose headers have
1509 not been fetched into the agent.
1510
1511 This list will always be a subset of gnus-newsgroup-undownloaded.")
1512
1513 (defvar gnus-newsgroup-undownloaded nil
1514   "List of articles in the current newsgroup that haven't been downloaded.")
1515
1516 (defvar gnus-newsgroup-unsendable nil
1517   "List of articles in the current newsgroup that won't be sent.")
1518
1519 (defvar gnus-newsgroup-bookmarks nil
1520   "List of articles in the current newsgroup that have bookmarks.")
1521
1522 (defvar gnus-newsgroup-dormant nil
1523   "Sorted list of dormant articles in the current newsgroup.")
1524
1525 (defvar gnus-newsgroup-unseen nil
1526   "List of unseen articles in the current newsgroup.")
1527
1528 (defvar gnus-newsgroup-seen nil
1529   "Range of seen articles in the current newsgroup.")
1530
1531 (defvar gnus-newsgroup-articles nil
1532   "List of articles in the current newsgroup.")
1533
1534 (defvar gnus-newsgroup-scored nil
1535   "List of scored articles in the current newsgroup.")
1536
1537 (defvar gnus-newsgroup-headers nil
1538   "List of article headers in the current newsgroup.")
1539
1540 (defvar gnus-newsgroup-threads nil)
1541
1542 (defvar gnus-newsgroup-prepared nil
1543   "Whether the current group has been prepared properly.")
1544
1545 (defvar gnus-newsgroup-ancient nil
1546   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1547
1548 (defvar gnus-newsgroup-sparse nil)
1549
1550 (defvar gnus-current-article nil)
1551 (defvar gnus-article-current nil)
1552 (defvar gnus-current-headers nil)
1553 (defvar gnus-have-all-headers nil)
1554 (defvar gnus-last-article nil)
1555 (defvar gnus-newsgroup-history nil)
1556 (defvar gnus-newsgroup-charset nil)
1557 (defvar gnus-newsgroup-ephemeral-charset nil)
1558 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1559
1560 (defvar gnus-article-before-search nil)
1561
1562 (defvar gnus-summary-local-variables
1563   '(gnus-newsgroup-name
1564
1565     ;; Marks lists
1566     gnus-newsgroup-unreads
1567     gnus-newsgroup-unselected
1568     gnus-newsgroup-marked
1569     gnus-newsgroup-spam-marked
1570     gnus-newsgroup-reads
1571     gnus-newsgroup-saved
1572     gnus-newsgroup-replied
1573     gnus-newsgroup-forwarded
1574     gnus-newsgroup-recent
1575     gnus-newsgroup-expirable
1576     gnus-newsgroup-killed
1577     gnus-newsgroup-unseen
1578     gnus-newsgroup-seen
1579     gnus-newsgroup-cached
1580     gnus-newsgroup-downloadable
1581     gnus-newsgroup-undownloaded
1582     gnus-newsgroup-unsendable
1583
1584     gnus-newsgroup-begin gnus-newsgroup-end
1585     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1586     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1587     gnus-newsgroup-last-directory
1588     gnus-newsgroup-auto-expire
1589     gnus-newsgroup-processable
1590     gnus-newsgroup-unfetched
1591     gnus-newsgroup-articles
1592     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1593     gnus-newsgroup-headers gnus-newsgroup-threads
1594     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1595     gnus-current-article gnus-current-headers gnus-have-all-headers
1596     gnus-last-article gnus-article-internal-prepare-hook
1597     (gnus-summary-article-delete-hook . global)
1598     (gnus-summary-article-move-hook . global)
1599     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1600     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1601     gnus-thread-expunge-below
1602     gnus-score-alist gnus-current-score-file
1603     (gnus-summary-expunge-below . global)
1604     (gnus-summary-mark-below . global)
1605     (gnus-orphan-score . global)
1606     gnus-newsgroup-active gnus-scores-exclude-files
1607     gnus-newsgroup-highest
1608     gnus-newsgroup-history gnus-newsgroup-ancient
1609     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1610     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1611     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1612     (gnus-newsgroup-expunged-tally . 0)
1613     gnus-cache-removable-articles
1614     gnus-newsgroup-data gnus-newsgroup-data-reverse
1615     gnus-newsgroup-limit gnus-newsgroup-limits
1616     gnus-newsgroup-charset gnus-newsgroup-display
1617     gnus-summary-use-undownloaded-faces)
1618   "Variables that are buffer-local to the summary buffers.")
1619
1620 (defvar gnus-newsgroup-variables nil
1621   "A list of variables that have separate values in different newsgroups.
1622 A list of newsgroup (summary buffer) local variables, or cons of
1623 variables and their default expressions to be evalled (when the default
1624 values are not nil), that should be made global while the summary buffer
1625 is active.
1626
1627 Note: The default expressions will be evaluated (using function `eval')
1628 before assignment to the local variable rather than just assigned to it.
1629 If the default expression is the symbol `global', that symbol will not
1630 be evaluated but the global value of the local variable will be used
1631 instead.
1632
1633 These variables can be used to set variables in the group parameters
1634 while still allowing them to affect operations done in other buffers.
1635 For example:
1636
1637 \(setq gnus-newsgroup-variables
1638      '(message-use-followup-to
1639        (gnus-visible-headers .
1640          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1641 ")
1642
1643 (eval-when-compile
1644   ;; Bind features so that require will believe that gnus-sum has
1645   ;; already been loaded (avoids infinite recursion)
1646   (let ((features (cons 'gnus-sum features)))
1647     (require 'gnus-art)))
1648
1649 ;; MIME stuff.
1650
1651 (defvar gnus-decode-encoded-word-methods
1652   '(mail-decode-encoded-word-string)
1653   "List of methods used to decode encoded words.
1654
1655 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1656 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1657 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1658 whose names match REGEXP.
1659
1660 For example:
1661 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1662  mail-decode-encoded-word-string
1663  (\"chinese\" . rfc1843-decode-string))")
1664
1665 (defvar gnus-decode-encoded-word-methods-cache nil)
1666
1667 (defun gnus-multi-decode-encoded-word-string (string)
1668   "Apply the functions from `gnus-encoded-word-methods' that match."
1669   (unless (and gnus-decode-encoded-word-methods-cache
1670                (eq gnus-newsgroup-name
1671                    (car gnus-decode-encoded-word-methods-cache)))
1672     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1673     (dolist (method gnus-decode-encoded-word-methods)
1674       (if (symbolp method)
1675           (nconc gnus-decode-encoded-word-methods-cache (list method))
1676         (if (and gnus-newsgroup-name
1677                  (string-match (car method) gnus-newsgroup-name))
1678             (nconc gnus-decode-encoded-word-methods-cache
1679                    (list (cdr method)))))))
1680   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1681     (setq string (funcall method string))))
1682
1683 ;; Subject simplification.
1684
1685 (defun gnus-simplify-whitespace (str)
1686   "Remove excessive whitespace from STR."
1687   ;; Multiple spaces.
1688   (while (string-match "[ \t][ \t]+" str)
1689     (setq str (concat (substring str 0 (match-beginning 0))
1690                         " "
1691                         (substring str (match-end 0)))))
1692   ;; Leading spaces.
1693   (when (string-match "^[ \t]+" str)
1694     (setq str (substring str (match-end 0))))
1695   ;; Trailing spaces.
1696   (when (string-match "[ \t]+$" str)
1697     (setq str (substring str 0 (match-beginning 0))))
1698   str)
1699
1700 (defun gnus-simplify-all-whitespace (str)
1701   "Remove all whitespace from STR."
1702   (while (string-match "[ \t\n]+" str)
1703     (setq str (replace-match "" nil nil str)))
1704   str)
1705
1706 (defsubst gnus-simplify-subject-re (subject)
1707   "Remove \"Re:\" from subject lines."
1708   (if (string-match message-subject-re-regexp subject)
1709       (substring subject (match-end 0))
1710     subject))
1711
1712 (defun gnus-simplify-subject (subject &optional re-only)
1713   "Remove `Re:' and words in parentheses.
1714 If RE-ONLY is non-nil, strip leading `Re:'s only."
1715   (let ((case-fold-search t))           ;Ignore case.
1716     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1717     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1718       (setq subject (substring subject (match-end 0))))
1719     ;; Remove uninteresting prefixes.
1720     (when (and (not re-only)
1721                gnus-simplify-ignored-prefixes
1722                (string-match gnus-simplify-ignored-prefixes subject))
1723       (setq subject (substring subject (match-end 0))))
1724     ;; Remove words in parentheses from end.
1725     (unless re-only
1726       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1727         (setq subject (substring subject 0 (match-beginning 0)))))
1728     ;; Return subject string.
1729     subject))
1730
1731 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1732 ;; all whitespace.
1733 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1734   (goto-char (point-min))
1735   (while (re-search-forward regexp nil t)
1736     (replace-match (or newtext ""))))
1737
1738 (defun gnus-simplify-buffer-fuzzy ()
1739   "Simplify string in the buffer fuzzily.
1740 The string in the accessible portion of the current buffer is simplified.
1741 It is assumed to be a single-line subject.
1742 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1743 matter is removed.  Additional things can be deleted by setting
1744 `gnus-simplify-subject-fuzzy-regexp'."
1745   (let ((case-fold-search t)
1746         (modified-tick))
1747     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1748
1749     (while (not (eq modified-tick (buffer-modified-tick)))
1750       (setq modified-tick (buffer-modified-tick))
1751       (cond
1752        ((listp gnus-simplify-subject-fuzzy-regexp)
1753         (mapc 'gnus-simplify-buffer-fuzzy-step
1754               gnus-simplify-subject-fuzzy-regexp))
1755        (gnus-simplify-subject-fuzzy-regexp
1756         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1757       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1758       (gnus-simplify-buffer-fuzzy-step
1759        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1760       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1761
1762     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1763     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1764     (gnus-simplify-buffer-fuzzy-step " $")
1765     (gnus-simplify-buffer-fuzzy-step "^ +")))
1766
1767 (defun gnus-simplify-subject-fuzzy (subject)
1768   "Simplify a subject string fuzzily.
1769 See `gnus-simplify-buffer-fuzzy' for details."
1770   (save-excursion
1771     (gnus-set-work-buffer)
1772     (let ((case-fold-search t))
1773       ;; Remove uninteresting prefixes.
1774       (when (and gnus-simplify-ignored-prefixes
1775                  (string-match gnus-simplify-ignored-prefixes subject))
1776         (setq subject (substring subject (match-end 0))))
1777       (insert subject)
1778       (inline (gnus-simplify-buffer-fuzzy))
1779       (buffer-string))))
1780
1781 (defsubst gnus-simplify-subject-fully (subject)
1782   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1783   (cond
1784    (gnus-simplify-subject-functions
1785     (gnus-map-function gnus-simplify-subject-functions subject))
1786    ((null gnus-summary-gather-subject-limit)
1787     (gnus-simplify-subject-re subject))
1788    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1789     (gnus-simplify-subject-fuzzy subject))
1790    ((numberp gnus-summary-gather-subject-limit)
1791     (truncate-string-to-width (gnus-simplify-subject-re subject)
1792                               gnus-summary-gather-subject-limit))
1793    (t
1794     subject)))
1795
1796 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1797   "Check whether two subjects are equal.
1798 If optional argument SIMPLE-FIRST is t, first argument is already
1799 simplified."
1800   (cond
1801    ((null simple-first)
1802     (equal (gnus-simplify-subject-fully s1)
1803            (gnus-simplify-subject-fully s2)))
1804    (t
1805     (equal s1
1806            (gnus-simplify-subject-fully s2)))))
1807
1808 (defun gnus-summary-bubble-group ()
1809   "Increase the score of the current group.
1810 This is a handy function to add to `gnus-summary-exit-hook' to
1811 increase the score of each group you read."
1812   (gnus-group-add-score gnus-newsgroup-name))
1813
1814 \f
1815 ;;;
1816 ;;; Gnus summary mode
1817 ;;;
1818
1819 (put 'gnus-summary-mode 'mode-class 'special)
1820
1821 (defvar gnus-article-commands-menu)
1822
1823 ;; Non-orthogonal keys
1824
1825 (gnus-define-keys gnus-summary-mode-map
1826   " " gnus-summary-next-page
1827   "\177" gnus-summary-prev-page
1828   [delete] gnus-summary-prev-page
1829   [backspace] gnus-summary-prev-page
1830   "\r" gnus-summary-scroll-up
1831   "\M-\r" gnus-summary-scroll-down
1832   "n" gnus-summary-next-unread-article
1833   "p" gnus-summary-prev-unread-article
1834   "N" gnus-summary-next-article
1835   "P" gnus-summary-prev-article
1836   "\M-\C-n" gnus-summary-next-same-subject
1837   "\M-\C-p" gnus-summary-prev-same-subject
1838   "\M-n" gnus-summary-next-unread-subject
1839   "\M-p" gnus-summary-prev-unread-subject
1840   "." gnus-summary-first-unread-article
1841   "," gnus-summary-best-unread-article
1842   "\M-s" gnus-summary-search-article-forward
1843   "\M-r" gnus-summary-search-article-backward
1844   "\M-S" gnus-summary-repeat-search-article-forward
1845   "\M-R" gnus-summary-repeat-search-article-backward
1846   "<" gnus-summary-beginning-of-article
1847   ">" gnus-summary-end-of-article
1848   "j" gnus-summary-goto-article
1849   "^" gnus-summary-refer-parent-article
1850   "\M-^" gnus-summary-refer-article
1851   "u" gnus-summary-tick-article-forward
1852   "!" gnus-summary-tick-article-forward
1853   "U" gnus-summary-tick-article-backward
1854   "d" gnus-summary-mark-as-read-forward
1855   "D" gnus-summary-mark-as-read-backward
1856   "E" gnus-summary-mark-as-expirable
1857   "\M-u" gnus-summary-clear-mark-forward
1858   "\M-U" gnus-summary-clear-mark-backward
1859   "k" gnus-summary-kill-same-subject-and-select
1860   "\C-k" gnus-summary-kill-same-subject
1861   "\M-\C-k" gnus-summary-kill-thread
1862   "\M-\C-l" gnus-summary-lower-thread
1863   "e" gnus-summary-edit-article
1864   "#" gnus-summary-mark-as-processable
1865   "\M-#" gnus-summary-unmark-as-processable
1866   "\M-\C-t" gnus-summary-toggle-threads
1867   "\M-\C-s" gnus-summary-show-thread
1868   "\M-\C-h" gnus-summary-hide-thread
1869   "\M-\C-f" gnus-summary-next-thread
1870   "\M-\C-b" gnus-summary-prev-thread
1871   [(meta down)] gnus-summary-next-thread
1872   [(meta up)] gnus-summary-prev-thread
1873   "\M-\C-u" gnus-summary-up-thread
1874   "\M-\C-d" gnus-summary-down-thread
1875   "&" gnus-summary-execute-command
1876   "c" gnus-summary-catchup-and-exit
1877   "\C-w" gnus-summary-mark-region-as-read
1878   "\C-t" gnus-summary-toggle-truncation
1879   "?" gnus-summary-mark-as-dormant
1880   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1881   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1882   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1883   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1884   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1885   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1886   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1887   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1888   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1889   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1890   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1891   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1892   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1893   "=" gnus-summary-expand-window
1894   "\C-x\C-s" gnus-summary-reselect-current-group
1895   "\M-g" gnus-summary-rescan-group
1896   "\C-c\C-r" gnus-summary-caesar-message
1897   "f" gnus-summary-followup
1898   "F" gnus-summary-followup-with-original
1899   "C" gnus-summary-cancel-article
1900   "r" gnus-summary-reply
1901   "R" gnus-summary-reply-with-original
1902   "\C-c\C-f" gnus-summary-mail-forward
1903   "o" gnus-summary-save-article
1904   "\C-o" gnus-summary-save-article-mail
1905   "|" gnus-summary-pipe-output
1906   "\M-k" gnus-summary-edit-local-kill
1907   "\M-K" gnus-summary-edit-global-kill
1908   ;; "V" gnus-version
1909   "\C-c\C-d" gnus-summary-describe-group
1910   "q" gnus-summary-exit
1911   "Q" gnus-summary-exit-no-update
1912   "\C-c\C-i" gnus-info-find-node
1913   gnus-mouse-2 gnus-mouse-pick-article
1914   [follow-link] mouse-face
1915   "m" gnus-summary-mail-other-window
1916   "a" gnus-summary-post-news
1917   "x" gnus-summary-limit-to-unread
1918   "s" gnus-summary-isearch-article
1919   [tab] gnus-summary-widget-forward
1920   "t" gnus-summary-toggle-header
1921   "g" gnus-summary-show-article
1922   "l" gnus-summary-goto-last-article
1923   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1924   "\C-d" gnus-summary-enter-digest-group
1925   "\M-\C-d" gnus-summary-read-document
1926   "\M-\C-e" gnus-summary-edit-parameters
1927   "\M-\C-a" gnus-summary-customize-parameters
1928   "\C-c\C-b" gnus-bug
1929   "*" gnus-cache-enter-article
1930   "\M-*" gnus-cache-remove-article
1931   "\M-&" gnus-summary-universal-argument
1932   "\C-l" gnus-recenter
1933   "I" gnus-summary-increase-score
1934   "L" gnus-summary-lower-score
1935   "\M-i" gnus-symbolic-argument
1936   "h" gnus-summary-select-article-buffer
1937
1938   "b" gnus-article-view-part
1939   "\M-t" gnus-summary-toggle-display-buttonized
1940
1941   "V" gnus-summary-score-map
1942   "X" gnus-uu-extract-map
1943   "S" gnus-summary-send-map)
1944
1945 ;; Sort of orthogonal keymap
1946 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1947   "t" gnus-summary-tick-article-forward
1948   "!" gnus-summary-tick-article-forward
1949   "d" gnus-summary-mark-as-read-forward
1950   "r" gnus-summary-mark-as-read-forward
1951   "c" gnus-summary-clear-mark-forward
1952   " " gnus-summary-clear-mark-forward
1953   "e" gnus-summary-mark-as-expirable
1954   "x" gnus-summary-mark-as-expirable
1955   "?" gnus-summary-mark-as-dormant
1956   "b" gnus-summary-set-bookmark
1957   "B" gnus-summary-remove-bookmark
1958   "#" gnus-summary-mark-as-processable
1959   "\M-#" gnus-summary-unmark-as-processable
1960   "S" gnus-summary-limit-include-expunged
1961   "C" gnus-summary-catchup
1962   "H" gnus-summary-catchup-to-here
1963   "h" gnus-summary-catchup-from-here
1964   "\C-c" gnus-summary-catchup-all
1965   "k" gnus-summary-kill-same-subject-and-select
1966   "K" gnus-summary-kill-same-subject
1967   "P" gnus-uu-mark-map)
1968
1969 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1970   "c" gnus-summary-clear-above
1971   "u" gnus-summary-tick-above
1972   "m" gnus-summary-mark-above
1973   "k" gnus-summary-kill-below)
1974
1975 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1976   "/" gnus-summary-limit-to-subject
1977   "n" gnus-summary-limit-to-articles
1978   "b" gnus-summary-limit-to-bodies
1979   "h" gnus-summary-limit-to-headers
1980   "w" gnus-summary-pop-limit
1981   "s" gnus-summary-limit-to-subject
1982   "a" gnus-summary-limit-to-author
1983   "u" gnus-summary-limit-to-unread
1984   "m" gnus-summary-limit-to-marks
1985   "M" gnus-summary-limit-exclude-marks
1986   "v" gnus-summary-limit-to-score
1987   "*" gnus-summary-limit-include-cached
1988   "D" gnus-summary-limit-include-dormant
1989   "T" gnus-summary-limit-include-thread
1990   "d" gnus-summary-limit-exclude-dormant
1991   "t" gnus-summary-limit-to-age
1992   "." gnus-summary-limit-to-unseen
1993   "x" gnus-summary-limit-to-extra
1994   "p" gnus-summary-limit-to-display-predicate
1995   "E" gnus-summary-limit-include-expunged
1996   "c" gnus-summary-limit-exclude-childless-dormant
1997   "C" gnus-summary-limit-mark-excluded-as-read
1998   "o" gnus-summary-insert-old-articles
1999   "N" gnus-summary-insert-new-articles
2000   "S" gnus-summary-limit-to-singletons
2001   "r" gnus-summary-limit-to-replied
2002   "R" gnus-summary-limit-to-recipient
2003   "A" gnus-summary-limit-to-address)
2004
2005 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2006   "n" gnus-summary-next-unread-article
2007   "p" gnus-summary-prev-unread-article
2008   "N" gnus-summary-next-article
2009   "P" gnus-summary-prev-article
2010   "\C-n" gnus-summary-next-same-subject
2011   "\C-p" gnus-summary-prev-same-subject
2012   "\M-n" gnus-summary-next-unread-subject
2013   "\M-p" gnus-summary-prev-unread-subject
2014   "f" gnus-summary-first-unread-article
2015   "b" gnus-summary-best-unread-article
2016   "j" gnus-summary-goto-article
2017   "g" gnus-summary-goto-subject
2018   "l" gnus-summary-goto-last-article
2019   "o" gnus-summary-pop-article)
2020
2021 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2022   "k" gnus-summary-kill-thread
2023   "E" gnus-summary-expire-thread
2024   "l" gnus-summary-lower-thread
2025   "i" gnus-summary-raise-thread
2026   "T" gnus-summary-toggle-threads
2027   "t" gnus-summary-rethread-current
2028   "^" gnus-summary-reparent-thread
2029   "\M-^" gnus-summary-reparent-children
2030   "s" gnus-summary-show-thread
2031   "S" gnus-summary-show-all-threads
2032   "h" gnus-summary-hide-thread
2033   "H" gnus-summary-hide-all-threads
2034   "n" gnus-summary-next-thread
2035   "p" gnus-summary-prev-thread
2036   "u" gnus-summary-up-thread
2037   "o" gnus-summary-top-thread
2038   "d" gnus-summary-down-thread
2039   "#" gnus-uu-mark-thread
2040   "\M-#" gnus-uu-unmark-thread)
2041
2042 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2043   "g" gnus-summary-prepare
2044   "c" gnus-summary-insert-cached-articles
2045   "d" gnus-summary-insert-dormant-articles
2046   "t" gnus-summary-insert-ticked-articles)
2047
2048 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2049   "c" gnus-summary-catchup-and-exit
2050   "C" gnus-summary-catchup-all-and-exit
2051   "E" gnus-summary-exit-no-update
2052   "Q" gnus-summary-exit
2053   "Z" gnus-summary-exit
2054   "n" gnus-summary-catchup-and-goto-next-group
2055   "p" gnus-summary-catchup-and-goto-prev-group
2056   "R" gnus-summary-reselect-current-group
2057   "G" gnus-summary-rescan-group
2058   "N" gnus-summary-next-group
2059   "s" gnus-summary-save-newsrc
2060   "P" gnus-summary-prev-group)
2061
2062 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2063   " " gnus-summary-next-page
2064   "n" gnus-summary-next-page
2065   "\177" gnus-summary-prev-page
2066   [delete] gnus-summary-prev-page
2067   "p" gnus-summary-prev-page
2068   "\r" gnus-summary-scroll-up
2069   "\M-\r" gnus-summary-scroll-down
2070   "<" gnus-summary-beginning-of-article
2071   ">" gnus-summary-end-of-article
2072   "b" gnus-summary-beginning-of-article
2073   "e" gnus-summary-end-of-article
2074   "^" gnus-summary-refer-parent-article
2075   "r" gnus-summary-refer-parent-article
2076   "C" gnus-summary-show-complete-article
2077   "D" gnus-summary-enter-digest-group
2078   "R" gnus-summary-refer-references
2079   "T" gnus-summary-refer-thread
2080   "W" gnus-warp-to-article
2081   "g" gnus-summary-show-article
2082   "s" gnus-summary-isearch-article
2083   [tab] gnus-summary-widget-forward
2084   "P" gnus-summary-print-article
2085   "S" gnus-sticky-article
2086   "M" gnus-mailing-list-insinuate
2087   "t" gnus-article-babel)
2088
2089 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2090   "b" gnus-article-add-buttons
2091   "B" gnus-article-add-buttons-to-head
2092   "o" gnus-article-treat-overstrike
2093   "e" gnus-article-emphasize
2094   "w" gnus-article-fill-cited-article
2095   "Q" gnus-article-fill-long-lines
2096   "L" gnus-article-toggle-truncate-lines
2097   "C" gnus-article-capitalize-sentences
2098   "c" gnus-article-remove-cr
2099   "q" gnus-article-de-quoted-unreadable
2100   "6" gnus-article-de-base64-unreadable
2101   "Z" gnus-article-decode-HZ
2102   "A" gnus-article-treat-ansi-sequences
2103   "h" gnus-article-wash-html
2104   "u" gnus-article-unsplit-urls
2105   "s" gnus-summary-force-verify-and-decrypt
2106   "f" gnus-article-display-x-face
2107   "l" gnus-summary-stop-page-breaking
2108   "r" gnus-summary-caesar-message
2109   "m" gnus-summary-morse-message
2110   "t" gnus-summary-toggle-header
2111   "g" gnus-treat-smiley
2112   "v" gnus-summary-verbose-headers
2113   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2114   "p" gnus-article-verify-x-pgp-sig
2115   "d" gnus-article-treat-dumbquotes
2116   "U" gnus-article-treat-non-ascii
2117   "i" gnus-summary-idna-message)
2118
2119 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2120   ;; mnemonic: deuglif*Y*
2121   "u" gnus-article-outlook-unwrap-lines
2122   "a" gnus-article-outlook-repair-attribution
2123   "c" gnus-article-outlook-rearrange-citation
2124   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2125
2126 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2127   "a" gnus-article-hide
2128   "h" gnus-article-hide-headers
2129   "b" gnus-article-hide-boring-headers
2130   "s" gnus-article-hide-signature
2131   "c" gnus-article-hide-citation
2132   "C" gnus-article-hide-citation-in-followups
2133   "l" gnus-article-hide-list-identifiers
2134   "B" gnus-article-strip-banner
2135   "P" gnus-article-hide-pem
2136   "\C-c" gnus-article-hide-citation-maybe)
2137
2138 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2139   "a" gnus-article-highlight
2140   "h" gnus-article-highlight-headers
2141   "c" gnus-article-highlight-citation
2142   "s" gnus-article-highlight-signature)
2143
2144 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2145   "f" gnus-article-treat-fold-headers
2146   "u" gnus-article-treat-unfold-headers
2147   "n" gnus-article-treat-fold-newsgroups)
2148
2149 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2150   "x" gnus-article-display-x-face
2151   "d" gnus-article-display-face
2152   "s" gnus-treat-smiley
2153   "D" gnus-article-remove-images
2154   "W" gnus-article-show-images
2155   "f" gnus-treat-from-picon
2156   "m" gnus-treat-mail-picon
2157   "n" gnus-treat-newsgroups-picon
2158   "g" gnus-treat-from-gravatar
2159   "h" gnus-treat-mail-gravatar)
2160
2161 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2162   "w" gnus-article-decode-mime-words
2163   "c" gnus-article-decode-charset
2164   "v" gnus-mime-view-all-parts
2165   "b" gnus-article-view-part)
2166
2167 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2168   "z" gnus-article-date-ut
2169   "u" gnus-article-date-ut
2170   "l" gnus-article-date-local
2171   "p" gnus-article-date-english
2172   "e" gnus-article-date-lapsed
2173   "o" gnus-article-date-original
2174   "i" gnus-article-date-iso8601
2175   "s" gnus-article-date-user)
2176
2177 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2178   "t" gnus-article-remove-trailing-blank-lines
2179   "l" gnus-article-strip-leading-blank-lines
2180   "m" gnus-article-strip-multiple-blank-lines
2181   "a" gnus-article-strip-blank-lines
2182   "A" gnus-article-strip-all-blank-lines
2183   "s" gnus-article-strip-leading-space
2184   "e" gnus-article-strip-trailing-space
2185   "w" gnus-article-remove-leading-whitespace)
2186
2187 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2188   "v" gnus-version
2189   "d" gnus-summary-describe-group
2190   "h" gnus-summary-describe-briefly
2191   "i" gnus-info-find-node)
2192
2193 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2194   "e" gnus-summary-expire-articles
2195   "\M-\C-e" gnus-summary-expire-articles-now
2196   "\177" gnus-summary-delete-article
2197   [delete] gnus-summary-delete-article
2198   [backspace] gnus-summary-delete-article
2199   "m" gnus-summary-move-article
2200   "r" gnus-summary-respool-article
2201   "w" gnus-summary-edit-article
2202   "c" gnus-summary-copy-article
2203   "B" gnus-summary-crosspost-article
2204   "q" gnus-summary-respool-query
2205   "t" gnus-summary-respool-trace
2206   "i" gnus-summary-import-article
2207   "I" gnus-summary-create-article
2208   "p" gnus-summary-article-posted-p)
2209
2210 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2211   "o" gnus-summary-save-article
2212   "m" gnus-summary-save-article-mail
2213   "F" gnus-summary-write-article-file
2214   "r" gnus-summary-save-article-rmail
2215   "f" gnus-summary-save-article-file
2216   "b" gnus-summary-save-article-body-file
2217   "B" gnus-summary-write-article-body-file
2218   "h" gnus-summary-save-article-folder
2219   "v" gnus-summary-save-article-vm
2220   "p" gnus-summary-pipe-output
2221   "P" gnus-summary-muttprint)
2222
2223 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2224   "b" gnus-summary-display-buttonized
2225   "m" gnus-summary-repair-multipart
2226   "v" gnus-article-view-part
2227   "o" gnus-article-save-part
2228   "O" gnus-article-save-part-and-strip
2229   "r" gnus-article-replace-part
2230   "d" gnus-article-delete-part
2231   "t" gnus-article-view-part-as-type
2232   "j" gnus-article-jump-to-part
2233   "c" gnus-article-copy-part
2234   "C" gnus-article-view-part-as-charset
2235   "e" gnus-article-view-part-externally
2236   "H" gnus-article-browse-html-article
2237   "E" gnus-article-encrypt-body
2238   "i" gnus-article-inline-part
2239   "|" gnus-article-pipe-part)
2240
2241 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2242   "p" gnus-summary-mark-as-processable
2243   "u" gnus-summary-unmark-as-processable
2244   "U" gnus-summary-unmark-all-processable
2245   "v" gnus-uu-mark-over
2246   "s" gnus-uu-mark-series
2247   "r" gnus-uu-mark-region
2248   "g" gnus-uu-unmark-region
2249   "R" gnus-uu-mark-by-regexp
2250   "G" gnus-uu-unmark-by-regexp
2251   "t" gnus-uu-mark-thread
2252   "T" gnus-uu-unmark-thread
2253   "a" gnus-uu-mark-all
2254   "b" gnus-uu-mark-buffer
2255   "S" gnus-uu-mark-sparse
2256   "k" gnus-summary-kill-process-mark
2257   "y" gnus-summary-yank-process-mark
2258   "w" gnus-summary-save-process-mark
2259   "i" gnus-uu-invert-processable)
2260
2261 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2262   ;;"x" gnus-uu-extract-any
2263   "m" gnus-summary-save-parts
2264   "u" gnus-uu-decode-uu
2265   "U" gnus-uu-decode-uu-and-save
2266   "s" gnus-uu-decode-unshar
2267   "S" gnus-uu-decode-unshar-and-save
2268   "o" gnus-uu-decode-save
2269   "O" gnus-uu-decode-save
2270   "b" gnus-uu-decode-binhex
2271   "B" gnus-uu-decode-binhex
2272   "Y" gnus-uu-decode-yenc
2273   "p" gnus-uu-decode-postscript
2274   "P" gnus-uu-decode-postscript-and-save)
2275
2276 (gnus-define-keys
2277     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2278   "u" gnus-uu-decode-uu-view
2279   "U" gnus-uu-decode-uu-and-save-view
2280   "s" gnus-uu-decode-unshar-view
2281   "S" gnus-uu-decode-unshar-and-save-view
2282   "o" gnus-uu-decode-save-view
2283   "O" gnus-uu-decode-save-view
2284   "b" gnus-uu-decode-binhex-view
2285   "B" gnus-uu-decode-binhex-view
2286   "p" gnus-uu-decode-postscript-view
2287   "P" gnus-uu-decode-postscript-and-save-view)
2288
2289 (defvar gnus-article-post-menu nil)
2290
2291 (defconst gnus-summary-menu-maxlen 20)
2292
2293 (defun gnus-summary-menu-split (menu)
2294   ;; If we have lots of elements, divide them into groups of 20
2295   ;; and make a pane (or submenu) for each one.
2296   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2297       (let ((menu menu) sublists next
2298             (i 1))
2299         (while menu
2300           ;; Pull off the next gnus-summary-menu-maxlen elements
2301           ;; and make them the next element of sublist.
2302           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2303           (if next
2304               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2305                       nil))
2306           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2307                                              (aref (car (last menu)) 0)) menu)
2308                                sublists))
2309           (setq i (1+ i))
2310           (setq menu next))
2311         (nreverse sublists))
2312     ;; Few elements--put them all in one pane.
2313     menu))
2314
2315 (defun gnus-summary-make-menu-bar ()
2316   (gnus-turn-off-edit-menu 'summary)
2317
2318   (unless (boundp 'gnus-summary-misc-menu)
2319
2320     (easy-menu-define
2321       gnus-summary-kill-menu gnus-summary-mode-map ""
2322       (cons
2323        "Score"
2324        (nconc
2325         (list
2326          ["Customize" gnus-score-customize t])
2327         (gnus-make-score-map 'increase)
2328         (gnus-make-score-map 'lower)
2329         '(("Mark"
2330            ["Kill below" gnus-summary-kill-below t]
2331            ["Mark above" gnus-summary-mark-above t]
2332            ["Tick above" gnus-summary-tick-above t]
2333            ["Clear above" gnus-summary-clear-above t])
2334           ["Current score" gnus-summary-current-score t]
2335           ["Set score" gnus-summary-set-score t]
2336           ["Switch current score file..." gnus-score-change-score-file t]
2337           ["Set mark below..." gnus-score-set-mark-below t]
2338           ["Set expunge below..." gnus-score-set-expunge-below t]
2339           ["Edit current score file" gnus-score-edit-current-scores t]
2340           ["Edit score file..." gnus-score-edit-file t]
2341           ["Trace score" gnus-score-find-trace t]
2342           ["Find words" gnus-score-find-favourite-words t]
2343           ["Rescore buffer" gnus-summary-rescore t]
2344           ["Increase score..." gnus-summary-increase-score t]
2345           ["Lower score..." gnus-summary-lower-score t]))))
2346
2347     ;; Define both the Article menu in the summary buffer and the
2348     ;; equivalent Commands menu in the article buffer here for
2349     ;; consistency.
2350     (let ((innards
2351            `(("Hide"
2352               ["All" gnus-article-hide t]
2353               ["Headers" gnus-article-hide-headers t]
2354               ["Signature" gnus-article-hide-signature t]
2355               ["Citation" gnus-article-hide-citation t]
2356               ["List identifiers" gnus-article-hide-list-identifiers t]
2357               ["Banner" gnus-article-strip-banner t]
2358               ["Boring headers" gnus-article-hide-boring-headers t])
2359              ("Highlight"
2360               ["All" gnus-article-highlight t]
2361               ["Headers" gnus-article-highlight-headers t]
2362               ["Signature" gnus-article-highlight-signature t]
2363               ["Citation" gnus-article-highlight-citation t])
2364              ("MIME"
2365               ["Words" gnus-article-decode-mime-words t]
2366               ["Charset" gnus-article-decode-charset t]
2367               ["QP" gnus-article-de-quoted-unreadable t]
2368               ["Base64" gnus-article-de-base64-unreadable t]
2369               ["View MIME buttons" gnus-summary-display-buttonized t]
2370               ["View all" gnus-mime-view-all-parts t]
2371               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2372               ["Encrypt body" gnus-article-encrypt-body
2373                :active (not (gnus-group-read-only-p))
2374                ,@(if (featurep 'xemacs) nil
2375                    '(:help "Encrypt the message body on disk"))]
2376               ["Extract all parts..." gnus-summary-save-parts t]
2377               ("Multipart"
2378                ["Repair multipart" gnus-summary-repair-multipart t]
2379                ["Pipe part..." gnus-article-pipe-part t]
2380                ["Inline part" gnus-article-inline-part t]
2381                ["View part as type..." gnus-article-view-part-as-type t]
2382                ["Encrypt body" gnus-article-encrypt-body
2383                 :active (not (gnus-group-read-only-p))
2384                ,@(if (featurep 'xemacs) nil
2385                    '(:help "Encrypt the message body on disk"))]
2386                ["View part externally" gnus-article-view-part-externally t]
2387                ["View HTML parts in browser" gnus-article-browse-html-article t]
2388                ["View part with charset..." gnus-article-view-part-as-charset t]
2389                ["Copy part" gnus-article-copy-part t]
2390                ["Save part..." gnus-article-save-part t]
2391                ["View part" gnus-article-view-part t]))
2392              ("Date"
2393               ["Local" gnus-article-date-local t]
2394               ["ISO8601" gnus-article-date-iso8601 t]
2395               ["UT" gnus-article-date-ut t]
2396               ["Original" gnus-article-date-original t]
2397               ["Lapsed" gnus-article-date-lapsed t]
2398               ["User-defined" gnus-article-date-user t])
2399              ("Display"
2400               ["Remove images" gnus-article-remove-images t]
2401               ["Toggle smiley" gnus-treat-smiley t]
2402               ["Show X-Face" gnus-article-display-x-face t]
2403               ["Show picons in From" gnus-treat-from-picon t]
2404               ["Show picons in mail headers" gnus-treat-mail-picon t]
2405               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2406               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2407               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2408               ("View as different encoding"
2409                ,@(gnus-summary-menu-split
2410                   (mapcar
2411                    (lambda (cs)
2412                      ;; Since easymenu under Emacs doesn't allow
2413                      ;; lambda forms for menu commands, we should
2414                      ;; provide intern'ed function symbols.
2415                      (let ((command (intern (format "\
2416 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2417                        (fset command
2418                              `(lambda ()
2419                                 (interactive)
2420                                 (let ((gnus-summary-show-article-charset-alist
2421                                        '((1 . ,cs))))
2422                                   (gnus-summary-show-article 1))))
2423                        `[,(symbol-name cs) ,command t]))
2424                    (sort (if (fboundp 'coding-system-list)
2425                              (coding-system-list)
2426                            (mapcar 'car mm-mime-mule-charset-alist))
2427                          'string<)))))
2428              ("Washing"
2429               ("Remove Blanks"
2430                ["Leading" gnus-article-strip-leading-blank-lines t]
2431                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2432                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2433                ["All of the above" gnus-article-strip-blank-lines t]
2434                ["All" gnus-article-strip-all-blank-lines t]
2435                ["Leading space" gnus-article-strip-leading-space t]
2436                ["Trailing space" gnus-article-strip-trailing-space t]
2437                ["Leading space in headers"
2438                 gnus-article-remove-leading-whitespace t])
2439               ["Overstrike" gnus-article-treat-overstrike t]
2440               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2441               ["Non-ASCII" gnus-article-treat-non-ascii t]
2442               ["Emphasis" gnus-article-emphasize t]
2443               ["Word wrap" gnus-article-fill-cited-article t]
2444               ["Fill long lines" gnus-article-fill-long-lines t]
2445               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2446               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2447               ["Remove CR" gnus-article-remove-cr t]
2448               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2449               ["Base64" gnus-article-de-base64-unreadable t]
2450               ["Rot 13" gnus-summary-caesar-message
2451                ,@(if (featurep 'xemacs) '(t)
2452                    '(:help "\"Caesar rotate\" article by 13"))]
2453               ["De-IDNA" gnus-summary-idna-message t]
2454               ["Morse decode" gnus-summary-morse-message t]
2455               ["Unix pipe..." gnus-summary-pipe-message t]
2456               ["Add buttons" gnus-article-add-buttons t]
2457               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2458               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2459               ["Verbose header" gnus-summary-verbose-headers t]
2460               ["Toggle header" gnus-summary-toggle-header t]
2461               ["Unfold headers" gnus-article-treat-unfold-headers t]
2462               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2463               ["Html" gnus-article-wash-html t]
2464               ["Unsplit URLs" gnus-article-unsplit-urls t]
2465               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2466               ["Decode HZ" gnus-article-decode-HZ t]
2467               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2468               ("(Outlook) Deuglify"
2469                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2470                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2471                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2472                ["Full (Outlook) deuglify"
2473                 gnus-article-outlook-deuglify-article t])
2474               )
2475              ("Output"
2476               ["Save in default format..." gnus-summary-save-article
2477                ,@(if (featurep 'xemacs) '(t)
2478                    '(:help "Save article using default method"))]
2479               ["Save in file..." gnus-summary-save-article-file
2480                ,@(if (featurep 'xemacs) '(t)
2481                    '(:help "Save article in file"))]
2482               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2483               ["Save in MH folder..." gnus-summary-save-article-folder t]
2484               ["Save in VM folder..." gnus-summary-save-article-vm t]
2485               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2486               ["Save body in file..." gnus-summary-save-article-body-file t]
2487               ["Pipe through a filter..." gnus-summary-pipe-output t]
2488               ["Print with Muttprint..." gnus-summary-muttprint t]
2489               ["Print" gnus-summary-print-article
2490                ,@(if (featurep 'xemacs) '(t)
2491                    '(:help "Generate and print a PostScript image"))])
2492              ("Copy, move,... (Backend)"
2493               ,@(if (featurep 'xemacs) nil
2494                   '(:help "Copying, moving, expiring articles..."))
2495               ["Respool article..." gnus-summary-respool-article t]
2496               ["Move article..." gnus-summary-move-article
2497                (gnus-check-backend-function
2498                 'request-move-article gnus-newsgroup-name)]
2499               ["Copy article..." gnus-summary-copy-article t]
2500               ["Crosspost article..." gnus-summary-crosspost-article
2501                (gnus-check-backend-function
2502                 'request-replace-article gnus-newsgroup-name)]
2503               ["Import file..." gnus-summary-import-article
2504                (gnus-check-backend-function
2505                 'request-accept-article gnus-newsgroup-name)]
2506               ["Create article..." gnus-summary-create-article
2507                (gnus-check-backend-function
2508                 'request-accept-article gnus-newsgroup-name)]
2509               ["Check if posted" gnus-summary-article-posted-p t]
2510               ["Edit article" gnus-summary-edit-article
2511                (not (gnus-group-read-only-p))]
2512               ["Delete article" gnus-summary-delete-article
2513                (gnus-check-backend-function
2514                 'request-expire-articles gnus-newsgroup-name)]
2515               ["Query respool" gnus-summary-respool-query t]
2516               ["Trace respool" gnus-summary-respool-trace t]
2517               ["Delete expirable articles" gnus-summary-expire-articles-now
2518                (gnus-check-backend-function
2519                 'request-expire-articles gnus-newsgroup-name)])
2520              ("Extract"
2521               ["Uudecode" gnus-uu-decode-uu
2522                ,@(if (featurep 'xemacs) '(t)
2523                    '(:help "Decode uuencoded article(s)"))]
2524               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2525               ["Unshar" gnus-uu-decode-unshar t]
2526               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2527               ["Save" gnus-uu-decode-save t]
2528               ["Binhex" gnus-uu-decode-binhex t]
2529               ["Postscript" gnus-uu-decode-postscript t]
2530               ["All MIME parts" gnus-summary-save-parts t])
2531              ("Cache"
2532               ["Enter article" gnus-cache-enter-article t]
2533               ["Remove article" gnus-cache-remove-article t])
2534              ["Translate" gnus-article-babel t]
2535              ["Select article buffer" gnus-summary-select-article-buffer t]
2536              ["Make article buffer sticky" gnus-sticky-article t]
2537              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2538              ["Isearch article..." gnus-summary-isearch-article t]
2539              ["Beginning of the article" gnus-summary-beginning-of-article t]
2540              ["End of the article" gnus-summary-end-of-article t]
2541              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2542              ["Fetch referenced articles" gnus-summary-refer-references t]
2543              ["Fetch current thread" gnus-summary-refer-thread t]
2544              ["Fetch article with id..." gnus-summary-refer-article t]
2545              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2546              ["Redisplay" gnus-summary-show-article t]
2547              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2548       (easy-menu-define
2549         gnus-summary-article-menu gnus-summary-mode-map ""
2550         (cons "Article" innards))
2551
2552       (if (not (keymapp gnus-summary-article-menu))
2553           (easy-menu-define
2554             gnus-article-commands-menu gnus-article-mode-map ""
2555             (cons "Commands" innards))
2556         ;; in Emacs, don't share menu.
2557         (setq gnus-article-commands-menu
2558               (copy-keymap gnus-summary-article-menu))
2559         (define-key gnus-article-mode-map [menu-bar commands]
2560           (cons "Commands" gnus-article-commands-menu))))
2561
2562     (easy-menu-define
2563       gnus-summary-thread-menu gnus-summary-mode-map ""
2564       '("Threads"
2565         ["Find all messages in thread" gnus-summary-refer-thread t]
2566         ["Toggle threading" gnus-summary-toggle-threads t]
2567         ["Hide threads" gnus-summary-hide-all-threads t]
2568         ["Show threads" gnus-summary-show-all-threads t]
2569         ["Hide thread" gnus-summary-hide-thread t]
2570         ["Show thread" gnus-summary-show-thread t]
2571         ["Go to next thread" gnus-summary-next-thread t]
2572         ["Go to previous thread" gnus-summary-prev-thread t]
2573         ["Go down thread" gnus-summary-down-thread t]
2574         ["Go up thread" gnus-summary-up-thread t]
2575         ["Top of thread" gnus-summary-top-thread t]
2576         ["Mark thread as read" gnus-summary-kill-thread t]
2577         ["Mark thread as expired" gnus-summary-expire-thread t]
2578         ["Lower thread score" gnus-summary-lower-thread t]
2579         ["Raise thread score" gnus-summary-raise-thread t]
2580         ["Rethread current" gnus-summary-rethread-current t]))
2581
2582     (easy-menu-define
2583       gnus-summary-post-menu gnus-summary-mode-map ""
2584       `("Post"
2585         ["Send a message (mail or news)" gnus-summary-post-news
2586          ,@(if (featurep 'xemacs) '(t)
2587              '(:help "Compose a new message (mail or news)"))]
2588         ["Followup" gnus-summary-followup
2589          ,@(if (featurep 'xemacs) '(t)
2590              '(:help "Post followup to this article"))]
2591         ["Followup and yank" gnus-summary-followup-with-original
2592          ,@(if (featurep 'xemacs) '(t)
2593              '(:help "Post followup to this article, quoting its contents"))]
2594         ["Supersede article" gnus-summary-supersede-article t]
2595         ["Cancel article" gnus-summary-cancel-article
2596          ,@(if (featurep 'xemacs) '(t)
2597              '(:help "Cancel an article you posted"))]
2598         ["Reply" gnus-summary-reply t]
2599         ["Reply and yank" gnus-summary-reply-with-original t]
2600         ["Wide reply" gnus-summary-wide-reply t]
2601         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2602          ,@(if (featurep 'xemacs) '(t)
2603              '(:help "Mail a reply, quoting this article"))]
2604         ["Very wide reply" gnus-summary-very-wide-reply t]
2605         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2606          ,@(if (featurep 'xemacs) '(t)
2607              '(:help "Mail a very wide reply, quoting this article"))]
2608         ["Mail forward" gnus-summary-mail-forward t]
2609         ["Post forward" gnus-summary-post-forward t]
2610         ["Digest and mail" gnus-uu-digest-mail-forward t]
2611         ["Digest and post" gnus-uu-digest-post-forward t]
2612         ["Resend message" gnus-summary-resend-message t]
2613         ["Resend message edit" gnus-summary-resend-message-edit t]
2614         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2615         ["Send a mail" gnus-summary-mail-other-window t]
2616         ["Create a local message" gnus-summary-news-other-window t]
2617         ["Uuencode and post" gnus-uu-post-news
2618          ,@(if (featurep 'xemacs) '(t)
2619              '(:help "Post a uuencoded article"))]
2620         ["Followup via news" gnus-summary-followup-to-mail t]
2621         ["Followup via news and yank"
2622          gnus-summary-followup-to-mail-with-original t]
2623         ["Strip signature on reply"
2624          (lambda ()
2625            (interactive)
2626            (if (not (memq message-cite-function
2627                           '(message-cite-original-without-signature
2628                             message-cite-original)))
2629                ;; Stupid workaround for XEmacs not honoring :visible.
2630                (message "Can't toggle this value of `message-cite-function'")
2631              (setq message-cite-function
2632                    (if (eq message-cite-function
2633                            'message-cite-original-without-signature)
2634                        'message-cite-original
2635                      'message-cite-original-without-signature))))
2636          ;; XEmacs barfs on :visible.
2637          ,@(if (featurep 'xemacs) nil
2638              '(:visible (memq message-cite-function
2639                               '(message-cite-original-without-signature
2640                                 message-cite-original))))
2641          :style toggle
2642          :selected (eq message-cite-function
2643                        'message-cite-original-without-signature)
2644          ,@(if (featurep 'xemacs) nil
2645              '(:help "Strip signature from cited article when replying."))]
2646         ;;("Draft"
2647         ;;["Send" gnus-summary-send-draft t]
2648         ;;["Send bounced" gnus-resend-bounced-mail t])
2649         ))
2650
2651     (cond
2652      ((not (keymapp gnus-summary-post-menu))
2653       (setq gnus-article-post-menu gnus-summary-post-menu))
2654      ((not gnus-article-post-menu)
2655       ;; Don't share post menu.
2656       (setq gnus-article-post-menu
2657             (copy-keymap gnus-summary-post-menu))))
2658     (define-key gnus-article-mode-map [menu-bar post]
2659       (cons "Post" gnus-article-post-menu))
2660
2661     (easy-menu-define
2662       gnus-summary-misc-menu gnus-summary-mode-map ""
2663       `("Gnus"
2664         ("Mark Read"
2665          ["Mark as read" gnus-summary-mark-as-read-forward t]
2666          ["Mark same subject and select"
2667           gnus-summary-kill-same-subject-and-select t]
2668          ["Mark same subject" gnus-summary-kill-same-subject t]
2669          ["Catchup" gnus-summary-catchup
2670           ,@(if (featurep 'xemacs) '(t)
2671               '(:help "Mark unread articles in this group as read"))]
2672          ["Catchup all" gnus-summary-catchup-all t]
2673          ["Catchup to here" gnus-summary-catchup-to-here t]
2674          ["Catchup from here" gnus-summary-catchup-from-here t]
2675          ["Catchup region" gnus-summary-mark-region-as-read
2676           (gnus-mark-active-p)]
2677          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2678         ("Mark Various"
2679          ["Tick" gnus-summary-tick-article-forward t]
2680          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2681          ["Remove marks" gnus-summary-clear-mark-forward t]
2682          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2683          ["Set bookmark" gnus-summary-set-bookmark t]
2684          ["Remove bookmark" gnus-summary-remove-bookmark t])
2685         ("Limit to"
2686          ["Marks..." gnus-summary-limit-to-marks t]
2687          ["Subject..." gnus-summary-limit-to-subject t]
2688          ["Author..." gnus-summary-limit-to-author t]
2689          ["Recipient..." gnus-summary-limit-to-recipient t]
2690          ["Address..." gnus-summary-limit-to-address t]
2691          ["Age..." gnus-summary-limit-to-age t]
2692          ["Extra..." gnus-summary-limit-to-extra t]
2693          ["Score..." gnus-summary-limit-to-score t]
2694          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2695          ["Unread" gnus-summary-limit-to-unread t]
2696          ["Unseen" gnus-summary-limit-to-unseen t]
2697          ["Singletons" gnus-summary-limit-to-singletons t]
2698          ["Replied" gnus-summary-limit-to-replied t]
2699          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2700          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2701          ["Pop limit" gnus-summary-pop-limit t]
2702          ["Show dormant" gnus-summary-limit-include-dormant t]
2703          ["Hide childless dormant"
2704           gnus-summary-limit-exclude-childless-dormant t]
2705          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2706          ["Hide marked" gnus-summary-limit-exclude-marks t]
2707          ["Show expunged" gnus-summary-limit-include-expunged t])
2708         ("Process Mark"
2709          ["Set mark" gnus-summary-mark-as-processable t]
2710          ["Remove mark" gnus-summary-unmark-as-processable t]
2711          ["Remove all marks" gnus-summary-unmark-all-processable t]
2712          ["Invert marks" gnus-uu-invert-processable t]
2713          ["Mark above" gnus-uu-mark-over t]
2714          ["Mark series" gnus-uu-mark-series t]
2715          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2716          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2717          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2718          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2719          ["Mark all" gnus-uu-mark-all t]
2720          ["Mark buffer" gnus-uu-mark-buffer t]
2721          ["Mark sparse" gnus-uu-mark-sparse t]
2722          ["Mark thread" gnus-uu-mark-thread t]
2723          ["Unmark thread" gnus-uu-unmark-thread t]
2724          ("Process Mark Sets"
2725           ["Kill" gnus-summary-kill-process-mark t]
2726           ["Yank" gnus-summary-yank-process-mark
2727            gnus-newsgroup-process-stack]
2728           ["Save" gnus-summary-save-process-mark t]
2729           ["Run command on marked..." gnus-summary-universal-argument t]))
2730         ("Registry Marks")
2731         ("Scroll article"
2732          ["Page forward" gnus-summary-next-page
2733           ,@(if (featurep 'xemacs) '(t)
2734               '(:help "Show next page of article"))]
2735          ["Page backward" gnus-summary-prev-page
2736           ,@(if (featurep 'xemacs) '(t)
2737               '(:help "Show previous page of article"))]
2738          ["Line forward" gnus-summary-scroll-up t])
2739         ("Move"
2740          ["Next unread article" gnus-summary-next-unread-article t]
2741          ["Previous unread article" gnus-summary-prev-unread-article t]
2742          ["Next article" gnus-summary-next-article t]
2743          ["Previous article" gnus-summary-prev-article t]
2744          ["Next unread subject" gnus-summary-next-unread-subject t]
2745          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2746          ["Next article same subject" gnus-summary-next-same-subject t]
2747          ["Previous article same subject" gnus-summary-prev-same-subject t]
2748          ["First unread article" gnus-summary-first-unread-article t]
2749          ["Best unread article" gnus-summary-best-unread-article t]
2750          ["Go to subject number..." gnus-summary-goto-subject t]
2751          ["Go to article number..." gnus-summary-goto-article t]
2752          ["Go to the last article" gnus-summary-goto-last-article t]
2753          ["Pop article off history" gnus-summary-pop-article t])
2754         ("Sort"
2755          ["Sort by number" gnus-summary-sort-by-number t]
2756          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2757          ["Sort by author" gnus-summary-sort-by-author t]
2758          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2759          ["Sort by subject" gnus-summary-sort-by-subject t]
2760          ["Sort by date" gnus-summary-sort-by-date t]
2761          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2762          ["Sort by score" gnus-summary-sort-by-score t]
2763          ["Sort by lines" gnus-summary-sort-by-lines t]
2764          ["Sort by characters" gnus-summary-sort-by-chars t]
2765          ["Randomize" gnus-summary-sort-by-random t]
2766          ["Original sort" gnus-summary-sort-by-original t])
2767         ("Help"
2768          ["Describe group" gnus-summary-describe-group t]
2769          ["Read manual" gnus-info-find-node t])
2770         ("Modes"
2771          ["Pick and read" gnus-pick-mode t]
2772          ["Binary" gnus-binary-mode t])
2773         ("Regeneration"
2774          ["Regenerate" gnus-summary-prepare t]
2775          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2776          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2777          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2778          ["Toggle threading" gnus-summary-toggle-threads t])
2779         ["See old articles" gnus-summary-insert-old-articles t]
2780         ["See new articles" gnus-summary-insert-new-articles t]
2781         ["Filter articles..." gnus-summary-execute-command t]
2782         ["Run command on articles..." gnus-summary-universal-argument t]
2783         ["Search articles forward..." gnus-summary-search-article-forward t]
2784         ["Search articles backward..." gnus-summary-search-article-backward t]
2785         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2786         ["Expand window" gnus-summary-expand-window t]
2787         ["Expire expirable articles" gnus-summary-expire-articles
2788          (gnus-check-backend-function
2789           'request-expire-articles gnus-newsgroup-name)]
2790         ["Edit local kill file" gnus-summary-edit-local-kill t]
2791         ["Edit main kill file" gnus-summary-edit-global-kill t]
2792         ["Edit group parameters" gnus-summary-edit-parameters t]
2793         ["Customize group parameters" gnus-summary-customize-parameters t]
2794         ["Send a bug report" gnus-bug t]
2795         ("Exit"
2796          ["Catchup and exit" gnus-summary-catchup-and-exit
2797           ,@(if (featurep 'xemacs) '(t)
2798               '(:help "Mark unread articles in this group as read, then exit"))]
2799          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2800          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2801          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2802          ["Exit group" gnus-summary-exit
2803           ,@(if (featurep 'xemacs) '(t)
2804               '(:help "Exit current group, return to group selection mode"))]
2805          ["Exit group without updating" gnus-summary-exit-no-update t]
2806          ["Exit and goto next group" gnus-summary-next-group t]
2807          ["Exit and goto prev group" gnus-summary-prev-group t]
2808          ["Reselect group" gnus-summary-reselect-current-group t]
2809          ["Rescan group" gnus-summary-rescan-group t]
2810          ["Update dribble" gnus-summary-save-newsrc t])))
2811
2812     (gnus-run-hooks 'gnus-summary-menu-hook)))
2813
2814 (defvar gnus-summary-tool-bar-map nil)
2815
2816 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2817 ;; affect _new_ message buffers.  We might add a function that walks thru all
2818 ;; summary-mode buffers and force the update.
2819 (defun gnus-summary-tool-bar-update (&optional symbol value)
2820   "Update summary mode toolbar.
2821 Setter function for custom variables."
2822   (setq-default gnus-summary-tool-bar-map nil)
2823   (when symbol
2824     ;; When used as ":set" function:
2825     (set-default symbol value))
2826   (when (gnus-buffer-live-p gnus-summary-buffer)
2827     (with-current-buffer gnus-summary-buffer
2828       (gnus-summary-make-tool-bar))))
2829
2830 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2831                                      'gnus-summary-tool-bar-gnome
2832                                    'gnus-summary-tool-bar-retro)
2833   "Specifies the Gnus summary tool bar.
2834
2835 It can be either a list or a symbol refering to a list.  See
2836 `gmm-tool-bar-from-list' for the format of the list.  The
2837 default key map is `gnus-summary-mode-map'.
2838
2839 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2840 `gnus-summary-tool-bar-retro'."
2841   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2842                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2843                  (repeat :tag "User defined list" gmm-tool-bar-item)
2844                  (symbol))
2845   :version "23.1" ;; No Gnus
2846   :initialize 'custom-initialize-default
2847   :set 'gnus-summary-tool-bar-update
2848   :group 'gnus-summary)
2849
2850 (defcustom gnus-summary-tool-bar-gnome
2851   '((gnus-summary-post-news "mail/compose" nil)
2852     (gnus-summary-insert-new-articles "mail/inbox" nil
2853                                       :visible (or (not gnus-agent)
2854                                                    gnus-plugged))
2855     (gnus-summary-reply-with-original "mail/reply")
2856     (gnus-summary-reply "mail/reply" nil :visible nil)
2857     (gnus-summary-followup-with-original "mail/reply-all")
2858     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2859     (gnus-summary-mail-forward "mail/forward")
2860     (gnus-summary-save-article "mail/save")
2861     (gnus-summary-search-article-forward "search" nil :visible nil)
2862     (gnus-summary-print-article "print")
2863     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2864     ;; Some new commands that may need more suitable icons:
2865     (gnus-summary-save-newsrc "save" nil :visible nil)
2866     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2867     (gnus-summary-prev-article "left-arrow")
2868     (gnus-summary-next-article "right-arrow")
2869     (gnus-summary-next-page "next-page")
2870     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2871     ;;
2872     ;; Maybe some sort-by-... could be added:
2873     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2874     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2875     (gnus-summary-mark-as-expirable
2876      "delete" nil
2877      :visible (gnus-check-backend-function 'request-expire-articles
2878                                            gnus-newsgroup-name))
2879     (gnus-summary-mark-as-spam
2880      "mail/spam" t
2881      :visible (and (fboundp 'spam-group-ham-contents-p)
2882                    (spam-group-ham-contents-p gnus-newsgroup-name))
2883      :help "Mark as spam")
2884     (gnus-summary-mark-as-read-forward
2885      "mail/not-spam" nil
2886      :visible (and (fboundp 'spam-group-spam-contents-p)
2887                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2888     ;;
2889     (gnus-summary-exit "exit")
2890     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2891     (gnus-info-find-node "help"))
2892   "List of functions for the summary tool bar (GNOME style).
2893
2894 See `gmm-tool-bar-from-list' for the format of the list."
2895   :type '(repeat gmm-tool-bar-item)
2896   :version "23.1" ;; No Gnus
2897   :initialize 'custom-initialize-default
2898   :set 'gnus-summary-tool-bar-update
2899   :group 'gnus-summary)
2900
2901 (defcustom gnus-summary-tool-bar-retro
2902   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2903     (gnus-summary-next-unread-article "gnus/next-ur")
2904     (gnus-summary-post-news "gnus/post")
2905     (gnus-summary-followup-with-original "gnus/fuwo")
2906     (gnus-summary-followup "gnus/followup")
2907     (gnus-summary-reply-with-original "gnus/reply-wo")
2908     (gnus-summary-reply "gnus/reply")
2909     (gnus-summary-caesar-message "gnus/rot13")
2910     (gnus-uu-decode-uu "gnus/uu-decode")
2911     (gnus-summary-save-article-file "gnus/save-aif")
2912     (gnus-summary-save-article "gnus/save-art")
2913     (gnus-uu-post-news "gnus/uu-post")
2914     (gnus-summary-catchup "gnus/catchup")
2915     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2916     (gnus-summary-exit "gnus/exit-summ")
2917     ;; Some new command that may need more suitable icons:
2918     (gnus-summary-print-article "gnus/print" nil :visible nil)
2919     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2920     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2921     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2922     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2923     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2924     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2925     ;;
2926     (gnus-info-find-node "gnus/help" nil :visible nil))
2927   "List of functions for the summary tool bar (retro look).
2928
2929 See `gmm-tool-bar-from-list' for the format of the list."
2930   :type '(repeat gmm-tool-bar-item)
2931   :version "23.1" ;; No Gnus
2932   :initialize 'custom-initialize-default
2933   :set 'gnus-summary-tool-bar-update
2934   :group 'gnus-summary)
2935
2936 (defcustom gnus-summary-tool-bar-zap-list t
2937   "List of icon items from the global tool bar.
2938 These items are not displayed in the Gnus summary mode tool bar.
2939
2940 See `gmm-tool-bar-from-list' for the format of the list."
2941   :type 'gmm-tool-bar-zap-list
2942   :version "23.1" ;; No Gnus
2943   :initialize 'custom-initialize-default
2944   :set 'gnus-summary-tool-bar-update
2945   :group 'gnus-summary)
2946
2947 (defvar image-load-path)
2948 (defvar tool-bar-map)
2949
2950 (defun gnus-summary-make-tool-bar (&optional force)
2951   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2952 When FORCE, rebuild the tool bar."
2953   (when (and (not (featurep 'xemacs))
2954              (boundp 'tool-bar-mode)
2955              tool-bar-mode
2956              (or (not gnus-summary-tool-bar-map) force))
2957     (let* ((load-path
2958             (gmm-image-load-path-for-library "gnus"
2959                                              "mail/save.xpm"
2960                                              nil t))
2961            (image-load-path (cons (car load-path)
2962                                   (when (boundp 'image-load-path)
2963                                     image-load-path)))
2964            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2965                                         gnus-summary-tool-bar-zap-list
2966                                         'gnus-summary-mode-map)))
2967       (when map
2968         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2969         ;; uses it's value.
2970         (setq gnus-summary-tool-bar-map map))))
2971   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2972
2973 (defun gnus-score-set-default (var value)
2974   "A version of set that updates the GNU Emacs menu-bar."
2975   (set var value)
2976   ;; It is the message that forces the active status to be updated.
2977   (message ""))
2978
2979 (defun gnus-make-score-map (type)
2980   "Make a summary score map of type TYPE."
2981   (if t
2982       nil
2983     (let ((headers '(("author" "from" string)
2984                      ("subject" "subject" string)
2985                      ("article body" "body" string)
2986                      ("article head" "head" string)
2987                      ("xref" "xref" string)
2988                      ("extra header" "extra" string)
2989                      ("lines" "lines" number)
2990                      ("followups to author" "followup" string)))
2991           (types '((number ("less than" <)
2992                            ("greater than" >)
2993                            ("equal" =))
2994                    (string ("substring" s)
2995                            ("exact string" e)
2996                            ("fuzzy string" f)
2997                            ("regexp" r))))
2998           (perms '(("temporary" (current-time-string))
2999                    ("permanent" nil)
3000                    ("immediate" now)))
3001           header)
3002       (list
3003        (apply
3004         'nconc
3005         (list
3006          (if (eq type 'lower)
3007              "Lower score"
3008            "Increase score"))
3009         (let (outh)
3010           (while headers
3011             (setq header (car headers))
3012             (setq outh
3013                   (cons
3014                    (apply
3015                     'nconc
3016                     (list (car header))
3017                     (let ((ts (cdr (assoc (nth 2 header) types)))
3018                           outt)
3019                       (while ts
3020                         (setq outt
3021                               (cons
3022                                (apply
3023                                 'nconc
3024                                 (list (caar ts))
3025                                 (let ((ps perms)
3026                                       outp)
3027                                   (while ps
3028                                     (setq outp
3029                                           (cons
3030                                            (vector
3031                                             (caar ps)
3032                                             (list
3033                                              'gnus-summary-score-entry
3034                                              (nth 1 header)
3035                                              (if (or (string= (nth 1 header)
3036                                                               "head")
3037                                                      (string= (nth 1 header)
3038                                                               "body"))
3039                                                  ""
3040                                                (list 'gnus-summary-header
3041                                                      (nth 1 header)))
3042                                              (list 'quote (nth 1 (car ts)))
3043                                              (list 'gnus-score-delta-default
3044                                                    nil)
3045                                              (nth 1 (car ps))
3046                                              t)
3047                                             t)
3048                                            outp))
3049                                     (setq ps (cdr ps)))
3050                                   (list (nreverse outp))))
3051                                outt))
3052                         (setq ts (cdr ts)))
3053                       (list (nreverse outt))))
3054                    outh))
3055             (setq headers (cdr headers)))
3056           (list (nreverse outh))))))))
3057
3058
3059 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3060 (defvar bookmark-make-record-function)
3061 \f
3062
3063 (defun gnus-summary-mode (&optional group)
3064   "Major mode for reading articles.
3065
3066 All normal editing commands are switched off.
3067 \\<gnus-summary-mode-map>
3068 Each line in this buffer represents one article.  To read an
3069 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3070 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3071 respectively.
3072
3073 You can also post articles and send mail from this buffer.  To
3074 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3075 of an article, type `\\[gnus-summary-reply]'.
3076
3077 There are approx. one gazillion commands you can execute in this
3078 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3079
3080 The following commands are available:
3081
3082 \\{gnus-summary-mode-map}"
3083   (interactive)
3084   (kill-all-local-variables)
3085   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3086     (gnus-summary-make-local-variables))
3087   (gnus-summary-make-local-variables)
3088   (setq gnus-newsgroup-name group)
3089   (when (gnus-visual-p 'summary-menu 'menu)
3090     (gnus-summary-make-menu-bar)
3091     (gnus-summary-make-tool-bar))
3092   (gnus-make-thread-indent-array)
3093   (gnus-simplify-mode-line)
3094   (setq major-mode 'gnus-summary-mode)
3095   (setq mode-name "Summary")
3096   (use-local-map gnus-summary-mode-map)
3097   (buffer-disable-undo)
3098   (setq buffer-read-only t              ;Disable modification
3099         show-trailing-whitespace nil)
3100   (setq truncate-lines t)
3101   (add-to-invisibility-spec '(gnus-sum . t))
3102   (gnus-summary-set-display-table)
3103   (gnus-set-default-directory)
3104   (make-local-variable 'gnus-summary-line-format)
3105   (make-local-variable 'gnus-summary-line-format-spec)
3106   (make-local-variable 'gnus-summary-dummy-line-format)
3107   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3108   (make-local-variable 'gnus-summary-mark-positions)
3109   (gnus-make-local-hook 'pre-command-hook)
3110   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3111   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3112   (turn-on-gnus-mailing-list-mode)
3113   (mm-enable-multibyte)
3114   (set (make-local-variable 'bookmark-make-record-function)
3115        'gnus-summary-bookmark-make-record)
3116   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3117   (gnus-update-summary-mark-positions))
3118
3119 (defun gnus-summary-make-local-variables ()
3120   "Make all the local summary buffer variables."
3121   (let (global)
3122     (dolist (local gnus-summary-local-variables)
3123       (if (consp local)
3124           (progn
3125             (if (eq (cdr local) 'global)
3126                 ;; Copy the global value of the variable.
3127                 (setq global (symbol-value (car local)))
3128               ;; Use the value from the list.
3129               (setq global (eval (cdr local))))
3130             (set (make-local-variable (car local)) global))
3131         ;; Simple nil-valued local variable.
3132         (set (make-local-variable local) nil)))))
3133
3134 ;; Summary data functions.
3135
3136 (defmacro gnus-data-number (data)
3137   `(car ,data))
3138
3139 (defmacro gnus-data-set-number (data number)
3140   `(setcar ,data ,number))
3141
3142 (defmacro gnus-data-mark (data)
3143   `(nth 1 ,data))
3144
3145 (defmacro gnus-data-set-mark (data mark)
3146   `(setcar (nthcdr 1 ,data) ,mark))
3147
3148 (defmacro gnus-data-pos (data)
3149   `(nth 2 ,data))
3150
3151 (defmacro gnus-data-set-pos (data pos)
3152   `(setcar (nthcdr 2 ,data) ,pos))
3153
3154 (defmacro gnus-data-header (data)
3155   `(nth 3 ,data))
3156
3157 (defmacro gnus-data-set-header (data header)
3158   `(setf (nth 3 ,data) ,header))
3159
3160 (defmacro gnus-data-level (data)
3161   `(nth 4 ,data))
3162
3163 (defmacro gnus-data-unread-p (data)
3164   `(= (nth 1 ,data) gnus-unread-mark))
3165
3166 (defmacro gnus-data-read-p (data)
3167   `(/= (nth 1 ,data) gnus-unread-mark))
3168
3169 (defmacro gnus-data-pseudo-p (data)
3170   `(consp (nth 3 ,data)))
3171
3172 (defmacro gnus-data-find (number)
3173   `(assq ,number gnus-newsgroup-data))
3174
3175 (defmacro gnus-data-find-list (number &optional data)
3176   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3177      (memq (assq ,number bdata)
3178            bdata)))
3179
3180 (defmacro gnus-data-make (number mark pos header level)
3181   `(list ,number ,mark ,pos ,header ,level))
3182
3183 (defun gnus-data-enter (after-article number mark pos header level offset)
3184   (let ((data (gnus-data-find-list after-article)))
3185     (unless data
3186       (error "No such article: %d" after-article))
3187     (setcdr data (cons (gnus-data-make number mark pos header level)
3188                        (cdr data)))
3189     (setq gnus-newsgroup-data-reverse nil)
3190     (gnus-data-update-list (cddr data) offset)))
3191
3192 (defun gnus-data-enter-list (after-article list &optional offset)
3193   (when list
3194     (let ((data (and after-article (gnus-data-find-list after-article)))
3195           (ilist list))
3196       (if (not (or data
3197                    after-article))
3198           (let ((odata gnus-newsgroup-data))
3199             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3200             (when offset
3201               (gnus-data-update-list odata offset)))
3202         ;; Find the last element in the list to be spliced into the main
3203         ;; list.
3204         (setq list (last list))
3205         (if (not data)
3206             (progn
3207               (setcdr list gnus-newsgroup-data)
3208               (setq gnus-newsgroup-data ilist)
3209               (when offset
3210                 (gnus-data-update-list (cdr list) offset)))
3211           (setcdr list (cdr data))
3212           (setcdr data ilist)
3213           (when offset
3214             (gnus-data-update-list (cdr list) offset))))
3215       (setq gnus-newsgroup-data-reverse nil))))
3216
3217 (defun gnus-data-remove (article &optional offset)
3218   (let ((data gnus-newsgroup-data))
3219     (if (= (gnus-data-number (car data)) article)
3220         (progn
3221           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3222                 gnus-newsgroup-data-reverse nil)
3223           (when offset
3224             (gnus-data-update-list gnus-newsgroup-data offset)))
3225       (while (cdr data)
3226         (when (= (gnus-data-number (cadr data)) article)
3227           (setcdr data (cddr data))
3228           (when offset
3229             (gnus-data-update-list (cdr data) offset))
3230           (setq data nil
3231                 gnus-newsgroup-data-reverse nil))
3232         (setq data (cdr data))))))
3233
3234 (defmacro gnus-data-list (backward)
3235   `(if ,backward
3236        (or gnus-newsgroup-data-reverse
3237            (setq gnus-newsgroup-data-reverse
3238                  (reverse gnus-newsgroup-data)))
3239      gnus-newsgroup-data))
3240
3241 (defun gnus-data-update-list (data offset)
3242   "Add OFFSET to the POS of all data entries in DATA."
3243   (setq gnus-newsgroup-data-reverse nil)
3244   (while data
3245     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3246     (setq data (cdr data))))
3247
3248 (defun gnus-summary-article-pseudo-p (article)
3249   "Say whether this article is a pseudo article or not."
3250   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3251
3252 (defmacro gnus-summary-article-sparse-p (article)
3253   "Say whether this article is a sparse article or not."
3254   `(memq ,article gnus-newsgroup-sparse))
3255
3256 (defmacro gnus-summary-article-ancient-p (article)
3257   "Say whether this article is a sparse article or not."
3258   `(memq ,article gnus-newsgroup-ancient))
3259
3260 (defun gnus-article-parent-p (number)
3261   "Say whether this article is a parent or not."
3262   (let ((data (gnus-data-find-list number)))
3263     (and (cdr data)              ; There has to be an article after...
3264          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3265             (gnus-data-level (nth 1 data))))))
3266
3267 (defun gnus-article-children (number)
3268   "Return a list of all children to NUMBER."
3269   (let* ((data (gnus-data-find-list number))
3270          (level (gnus-data-level (car data)))
3271          children)
3272     (setq data (cdr data))
3273     (while (and data
3274                 (= (gnus-data-level (car data)) (1+ level)))
3275       (push (gnus-data-number (car data)) children)
3276       (setq data (cdr data)))
3277     children))
3278
3279 (defmacro gnus-summary-skip-intangible ()
3280   "If the current article is intangible, then jump to a different article."
3281   '(let ((to (get-text-property (point) 'gnus-intangible)))
3282      (and to (gnus-summary-goto-subject to))))
3283
3284 (defmacro gnus-summary-article-intangible-p ()
3285   "Say whether this article is intangible or not."
3286   '(get-text-property (point) 'gnus-intangible))
3287
3288 (defun gnus-article-read-p (article)
3289   "Say whether ARTICLE is read or not."
3290   (not (or (memq article gnus-newsgroup-marked)
3291            (memq article gnus-newsgroup-spam-marked)
3292            (memq article gnus-newsgroup-unreads)
3293            (memq article gnus-newsgroup-unselected)
3294            (memq article gnus-newsgroup-dormant))))
3295
3296 ;; Some summary mode macros.
3297
3298 (defmacro gnus-summary-article-number ()
3299   "The article number of the article on the current line.
3300 If there isn't an article number here, then we return the current
3301 article number."
3302   '(progn
3303      (gnus-summary-skip-intangible)
3304      (or (get-text-property (point) 'gnus-number)
3305          (gnus-summary-last-subject))))
3306
3307 (defmacro gnus-summary-article-header (&optional number)
3308   "Return the header of article NUMBER."
3309   `(gnus-data-header (gnus-data-find
3310                       ,(or number '(gnus-summary-article-number)))))
3311
3312 (defmacro gnus-summary-thread-level (&optional number)
3313   "Return the level of thread that starts with article NUMBER."
3314   `(if (and (eq gnus-summary-make-false-root 'dummy)
3315             (get-text-property (point) 'gnus-intangible))
3316        0
3317      (gnus-data-level (gnus-data-find
3318                        ,(or number '(gnus-summary-article-number))))))
3319
3320 (defmacro gnus-summary-article-mark (&optional number)
3321   "Return the mark of article NUMBER."
3322   `(gnus-data-mark (gnus-data-find
3323                     ,(or number '(gnus-summary-article-number)))))
3324
3325 (defmacro gnus-summary-article-pos (&optional number)
3326   "Return the position of the line of article NUMBER."
3327   `(gnus-data-pos (gnus-data-find
3328                    ,(or number '(gnus-summary-article-number)))))
3329
3330 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3331 (defmacro gnus-summary-article-subject (&optional number)
3332   "Return current subject string or nil if nothing."
3333   `(let ((headers
3334           ,(if number
3335                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3336              '(gnus-data-header (assq (gnus-summary-article-number)
3337                                       gnus-newsgroup-data)))))
3338      (and headers
3339           (vectorp headers)
3340           (mail-header-subject headers))))
3341
3342 (defmacro gnus-summary-article-score (&optional number)
3343   "Return current article score."
3344   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3345                   gnus-newsgroup-scored))
3346        gnus-summary-default-score 0))
3347
3348 (defun gnus-summary-article-children (&optional number)
3349   "Return a list of article numbers that are children of article NUMBER."
3350   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3351          (level (gnus-data-level (car data)))
3352          l children)
3353     (while (and (setq data (cdr data))
3354                 (> (setq l (gnus-data-level (car data))) level))
3355       (and (= (1+ level) l)
3356            (push (gnus-data-number (car data))
3357                  children)))
3358     (nreverse children)))
3359
3360 (defun gnus-summary-article-parent (&optional number)
3361   "Return the article number of the parent of article NUMBER."
3362   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3363                                     (gnus-data-list t)))
3364          (level (gnus-data-level (car data))))
3365     (if (zerop level)
3366         ()                              ; This is a root.
3367       ;; We search until we find an article with a level less than
3368       ;; this one.  That function has to be the parent.
3369       (while (and (setq data (cdr data))
3370                   (not (< (gnus-data-level (car data)) level))))
3371       (and data (gnus-data-number (car data))))))
3372
3373 (defun gnus-unread-mark-p (mark)
3374   "Say whether MARK is the unread mark."
3375   (= mark gnus-unread-mark))
3376
3377 (defun gnus-read-mark-p (mark)
3378   "Say whether MARK is one of the marks that mark as read.
3379 This is all marks except unread, ticked, dormant, and expirable."
3380   (not (or (= mark gnus-unread-mark)
3381            (= mark gnus-ticked-mark)
3382            (= mark gnus-spam-mark)
3383            (= mark gnus-dormant-mark)
3384            (= mark gnus-expirable-mark))))
3385
3386 (defmacro gnus-article-mark (number)
3387   "Return the MARK of article NUMBER.
3388 This macro should only be used when computing the mark the \"first\"
3389 time; i.e., when generating the summary lines.  After that,
3390 `gnus-summary-article-mark' should be used to examine the
3391 marks of articles."
3392   `(cond
3393     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3394     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3395     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3396     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3397     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3398     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3399     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3400     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3401            gnus-ancient-mark))))
3402
3403 ;; Saving hidden threads.
3404
3405 (defmacro gnus-save-hidden-threads (&rest forms)
3406   "Save hidden threads, eval FORMS, and restore the hidden threads."
3407   (let ((config (make-symbol "config")))
3408     `(let ((,config (gnus-hidden-threads-configuration)))
3409        (unwind-protect
3410            (save-excursion
3411              ,@forms)
3412          (gnus-restore-hidden-threads-configuration ,config)))))
3413 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3414 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3415
3416 (defun gnus-data-compute-positions ()
3417   "Compute the positions of all articles."
3418   (setq gnus-newsgroup-data-reverse nil)
3419   (let ((data gnus-newsgroup-data))
3420     (save-excursion
3421       (gnus-save-hidden-threads
3422         (gnus-summary-show-all-threads)
3423         (goto-char (point-min))
3424         (while data
3425           (while (get-text-property (point) 'gnus-intangible)
3426             (forward-line 1))
3427           (gnus-data-set-pos (car data) (+ (point) 3))
3428           (setq data (cdr data))
3429           (forward-line 1))))))
3430
3431 (defun gnus-hidden-threads-configuration ()
3432   "Return the current hidden threads configuration."
3433   (save-excursion
3434     (let (config)
3435       (goto-char (point-min))
3436       (while (not (eobp))
3437         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3438           (push (save-excursion (forward-line 0) (point)) config))
3439         (forward-line 1))
3440       config)))
3441
3442 (defun gnus-restore-hidden-threads-configuration (config)
3443   "Restore hidden threads configuration from CONFIG."
3444   (save-excursion
3445     (let (point (inhibit-read-only t))
3446       (while (setq point (pop config))
3447         (goto-char point)
3448         (gnus-summary-hide-thread)))))
3449
3450 ;; Various summary mode internalish functions.
3451
3452 (defun gnus-mouse-pick-article (e)
3453   (interactive "e")
3454   (mouse-set-point e)
3455   (gnus-summary-next-page nil t))
3456
3457 (defun gnus-summary-set-display-table ()
3458   "Change the display table.
3459 Odd characters have a tendency to mess
3460 up nicely formatted displays - we make all possible glyphs
3461 display only a single character."
3462
3463   ;; We start from the standard display table, if any.
3464   (let ((table (or (copy-sequence standard-display-table)
3465                    (make-display-table)))
3466         (i 32))
3467     ;; Nix out all the control chars...
3468     (while (>= (setq i (1- i)) 0)
3469       (gnus-put-display-table i [??] table))
3470    ;; ... but not newline and cr, of course.  (cr is necessary for the
3471     ;; selective display).
3472     (gnus-put-display-table ?\n nil table)
3473     (gnus-put-display-table ?\r nil table)
3474     ;; We keep TAB as well.
3475     (gnus-put-display-table ?\t nil table)
3476     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3477     ;; Emacs 23 (unicode), that are not set already.
3478     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3479                  160
3480                256)))
3481       (while (>= (setq i (1- i)) 127)
3482         ;; Only modify if the entry is nil.
3483         (unless (gnus-get-display-table i table)
3484           (gnus-put-display-table i [??] table))))
3485     (setq buffer-display-table table)))
3486
3487 (defun gnus-summary-set-article-display-arrow (pos)
3488   "Update the overlay arrow to point to line at position POS."
3489   (when gnus-summary-display-arrow
3490     (make-local-variable 'overlay-arrow-position)
3491     (make-local-variable 'overlay-arrow-string)
3492     (save-excursion
3493       (goto-char pos)
3494       (beginning-of-line)
3495       (unless overlay-arrow-position
3496         (setq overlay-arrow-position (make-marker)))
3497       (setq overlay-arrow-string "=>"
3498             overlay-arrow-position (set-marker overlay-arrow-position
3499                                                (point)
3500                                                (current-buffer))))))
3501
3502 (defun gnus-summary-setup-buffer (group)
3503   "Initialize summary buffer."
3504   (let ((buffer (gnus-summary-buffer-name group))
3505         (dead-name (concat "*Dead Summary "
3506                            (gnus-group-decoded-name group) "*")))
3507     ;; If a dead summary buffer exists, we kill it.
3508     (when (gnus-buffer-live-p dead-name)
3509       (gnus-kill-buffer dead-name))
3510     (if (get-buffer buffer)
3511         (progn
3512           (set-buffer buffer)
3513           (setq gnus-summary-buffer (current-buffer))
3514           (not gnus-newsgroup-prepared))
3515       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3516       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3517       (gnus-summary-mode group)
3518       (when (gnus-group-quit-config group)
3519         (set (make-local-variable 'gnus-single-article-buffer) nil))
3520       (make-local-variable 'gnus-article-buffer)
3521       (make-local-variable 'gnus-article-current)
3522       (make-local-variable 'gnus-original-article-buffer)
3523       (setq gnus-newsgroup-name group)
3524       ;; Set any local variables in the group parameters.
3525       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3526       t)))
3527
3528 (defun gnus-set-global-variables ()
3529   "Set the global equivalents of the buffer-local variables.
3530 They are set to the latest values they had.  These reflect the summary
3531 buffer that was in action when the last article was fetched."
3532   (when (eq major-mode 'gnus-summary-mode)
3533     (setq gnus-summary-buffer (current-buffer))
3534     (let ((name gnus-newsgroup-name)
3535           (marked gnus-newsgroup-marked)
3536           (spam gnus-newsgroup-spam-marked)
3537           (unread gnus-newsgroup-unreads)
3538           (headers gnus-current-headers)
3539           (data gnus-newsgroup-data)
3540           (summary gnus-summary-buffer)
3541           (article-buffer gnus-article-buffer)
3542           (original gnus-original-article-buffer)
3543           (gac gnus-article-current)
3544           (reffed gnus-reffed-article-number)
3545           (score-file gnus-current-score-file)
3546           (default-charset gnus-newsgroup-charset)
3547           vlist)
3548       (let ((locals gnus-newsgroup-variables))
3549         (while locals
3550           (if (consp (car locals))
3551               (push (eval (caar locals)) vlist)
3552             (push (eval (car locals)) vlist))
3553           (setq locals (cdr locals)))
3554         (setq vlist (nreverse vlist)))
3555       (with-current-buffer gnus-group-buffer
3556         (setq gnus-newsgroup-name name
3557               gnus-newsgroup-marked marked
3558               gnus-newsgroup-spam-marked spam
3559               gnus-newsgroup-unreads unread
3560               gnus-current-headers headers
3561               gnus-newsgroup-data data
3562               gnus-article-current gac
3563               gnus-summary-buffer summary
3564               gnus-article-buffer article-buffer
3565               gnus-original-article-buffer original
3566               gnus-reffed-article-number reffed
3567               gnus-current-score-file score-file
3568               gnus-newsgroup-charset default-charset)
3569         (let ((locals gnus-newsgroup-variables))
3570           (while locals
3571             (if (consp (car locals))
3572                 (set (caar locals) (pop vlist))
3573               (set (car locals) (pop vlist)))
3574             (setq locals (cdr locals))))
3575         ;; The article buffer also has local variables.
3576         (when (gnus-buffer-live-p gnus-article-buffer)
3577           (set-buffer gnus-article-buffer)
3578           (setq gnus-summary-buffer summary))))))
3579
3580 (defun gnus-summary-article-unread-p (article)
3581   "Say whether ARTICLE is unread or not."
3582   (memq article gnus-newsgroup-unreads))
3583
3584 (defun gnus-summary-first-article-p (&optional article)
3585   "Return whether ARTICLE is the first article in the buffer."
3586   (if (not (setq article (or article (gnus-summary-article-number))))
3587       nil
3588     (eq article (caar gnus-newsgroup-data))))
3589
3590 (defun gnus-summary-last-article-p (&optional article)
3591   "Return whether ARTICLE is the last article in the buffer."
3592   (if (not (setq article (or article (gnus-summary-article-number))))
3593       ;; All non-existent numbers are the last article.  :-)
3594       t
3595     (not (cdr (gnus-data-find-list article)))))
3596
3597 (defun gnus-make-thread-indent-array (&optional n)
3598   (when (or n
3599             (progn (setq n 200) nil)
3600             (null gnus-thread-indent-array)
3601             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3602     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3603           gnus-thread-indent-array-level gnus-thread-indent-level)
3604     (while (>= n 0)
3605       (aset gnus-thread-indent-array n
3606             (make-string (* n gnus-thread-indent-level) ? ))
3607       (setq n (1- n)))))
3608
3609 (defun gnus-update-summary-mark-positions ()
3610   "Compute where the summary marks are to go."
3611   (save-excursion
3612     (when (gnus-buffer-exists-p gnus-summary-buffer)
3613       (set-buffer gnus-summary-buffer))
3614     (let ((spec gnus-summary-line-format-spec)
3615           pos)
3616       (save-excursion
3617         (gnus-set-work-buffer)
3618         (let ((gnus-tmp-unread ?Z)
3619               (gnus-replied-mark ?Z)
3620               (gnus-score-below-mark ?Z)
3621               (gnus-score-over-mark ?Z)
3622               (gnus-undownloaded-mark ?Z)
3623               (gnus-summary-line-format-spec spec)
3624               (gnus-newsgroup-downloadable '(0))
3625               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3626               case-fold-search ignores)
3627           ;; Here, all marks are bound to Z.
3628           (gnus-summary-insert-line header
3629                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3630           (goto-char (point-min))
3631           ;; Memorize the positions of the same characters as dummy marks.
3632           (while (re-search-forward "[A-D]" nil t)
3633             (push (point) ignores))
3634           (erase-buffer)
3635           ;; We use A-D as dummy marks in order to know column positions
3636           ;; where marks should be inserted.
3637           (setq gnus-tmp-unread ?A
3638                 gnus-replied-mark ?B
3639                 gnus-score-below-mark ?C
3640                 gnus-score-over-mark ?C
3641                 gnus-undownloaded-mark ?D)
3642           (gnus-summary-insert-line header
3643                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3644           ;; Ignore characters which aren't dummy marks.
3645           (dolist (p ignores)
3646             (delete-region (goto-char (1- p)) p)
3647             (insert ?Z))
3648           (goto-char (point-min))
3649           (setq pos (list (cons 'unread
3650                                 (and (search-forward "A" nil t)
3651                                      (- (point) (point-min) 1)))))
3652           (goto-char (point-min))
3653           (push (cons 'replied (and (search-forward "B" nil t)
3654                                     (- (point) (point-min) 1)))
3655                 pos)
3656           (goto-char (point-min))
3657           (push (cons 'score (and (search-forward "C" nil t)
3658                                   (- (point) (point-min) 1)))
3659                 pos)
3660           (goto-char (point-min))
3661           (push (cons 'download (and (search-forward "D" nil t)
3662                                      (- (point) (point-min) 1)))
3663                 pos)))
3664       (setq gnus-summary-mark-positions pos))))
3665
3666 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3667   "Insert a dummy root in the summary buffer."
3668   (beginning-of-line)
3669   (gnus-add-text-properties
3670    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3671    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3672
3673 (defun gnus-summary-extract-address-component (from)
3674   (or (car (funcall gnus-extract-address-components from))
3675       from))
3676
3677 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3678   (let ((mail-parse-charset gnus-newsgroup-charset)
3679         (ignored-from-addresses (gnus-ignored-from-addresses))
3680         ; Is it really necessary to do this next part for each summary line?
3681         ; Luckily, doesn't seem to slow things down much.
3682         (mail-parse-ignored-charsets
3683          (with-current-buffer gnus-summary-buffer
3684            gnus-newsgroup-ignored-charsets)))
3685     (or
3686      (and ignored-from-addresses
3687           (string-match ignored-from-addresses gnus-tmp-from)
3688           (let ((extra-headers (mail-header-extra header))
3689                 to
3690                 newsgroups)
3691             (cond
3692              ((setq to (cdr (assq 'To extra-headers)))
3693               (concat gnus-summary-to-prefix
3694                       (inline
3695                         (gnus-summary-extract-address-component
3696                          (funcall gnus-decode-encoded-address-function to)))))
3697              ((setq newsgroups
3698                     (or
3699                      (cdr (assq 'Newsgroups extra-headers))
3700                      (and
3701                       (memq 'Newsgroups gnus-extra-headers)
3702                       (eq (car (gnus-find-method-for-group
3703                                 gnus-newsgroup-name)) 'nntp)
3704                       (gnus-group-real-name gnus-newsgroup-name))))
3705               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3706      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3707
3708 (defun gnus-summary-insert-line (gnus-tmp-header
3709                                  gnus-tmp-level gnus-tmp-current
3710                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3711                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3712                                  &optional gnus-tmp-dummy gnus-tmp-score
3713                                  gnus-tmp-process)
3714   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3715       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3716                                           gnus-tmp-level)))
3717   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3718          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3719          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3720          (gnus-tmp-score-char
3721           (if (or (null gnus-summary-default-score)
3722                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3723                       gnus-summary-zcore-fuzz))
3724               ?                         ;Whitespace
3725             (if (< gnus-tmp-score gnus-summary-default-score)
3726                 gnus-score-below-mark gnus-score-over-mark)))
3727          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3728          (gnus-tmp-replied
3729           (cond (gnus-tmp-process gnus-process-mark)
3730                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3731                  gnus-cached-mark)
3732                 (gnus-tmp-replied gnus-replied-mark)
3733                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3734                  gnus-forwarded-mark)
3735                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3736                  gnus-saved-mark)
3737                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3738                  gnus-recent-mark)
3739                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3740                  gnus-unseen-mark)
3741                 (t gnus-no-mark)))
3742          (gnus-tmp-downloaded
3743           (cond (undownloaded
3744                  gnus-undownloaded-mark)
3745                 (gnus-newsgroup-agentized
3746                  gnus-downloaded-mark)
3747                 (t
3748                  gnus-no-mark)))
3749          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3750          (gnus-tmp-name
3751           (cond
3752            ((string-match "<[^>]+> *$" gnus-tmp-from)
3753             (let ((beg (match-beginning 0)))
3754               (or (and (string-match "^\".+\"" gnus-tmp-from)
3755                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3756                   (substring gnus-tmp-from 0 beg))))
3757            ((string-match "(.+)" gnus-tmp-from)
3758             (substring gnus-tmp-from
3759                        (1+ (match-beginning 0)) (1- (match-end 0))))
3760            (t gnus-tmp-from)))
3761          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3762          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3763          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3764          (inhibit-read-only t))
3765     (when (string= gnus-tmp-name "")
3766       (setq gnus-tmp-name gnus-tmp-from))
3767     (unless (numberp gnus-tmp-lines)
3768       (setq gnus-tmp-lines -1))
3769     (if (= gnus-tmp-lines -1)
3770         (setq gnus-tmp-lines "?")
3771       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3772     (condition-case ()
3773         (gnus-put-text-property
3774          (point)
3775          (progn (eval gnus-summary-line-format-spec) (point))
3776          'gnus-number gnus-tmp-number)
3777       (error (gnus-message 5 "Error updating the summary line")))
3778     (when (gnus-visual-p 'summary-highlight 'highlight)
3779       (forward-line -1)
3780       (gnus-summary-highlight-line)
3781       (gnus-run-hooks 'gnus-summary-update-hook)
3782       (forward-line 1))))
3783
3784 (defun gnus-summary-update-line (&optional dont-update)
3785   "Update summary line after change."
3786   (when (and gnus-summary-default-score
3787              (not gnus-summary-inhibit-highlight))
3788     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3789            (article (gnus-summary-article-number))
3790            (score (gnus-summary-article-score article)))
3791       (unless dont-update
3792         (if (and gnus-summary-mark-below
3793                  (< (gnus-summary-article-score)
3794                     gnus-summary-mark-below))
3795             ;; This article has a low score, so we mark it as read.
3796             (when (memq article gnus-newsgroup-unreads)
3797               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3798           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3799             ;; This article was previously marked as read on account
3800             ;; of a low score, but now it has risen, so we mark it as
3801             ;; unread.
3802             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3803         (gnus-summary-update-mark
3804          (if (or (null gnus-summary-default-score)
3805                  (<= (abs (- score gnus-summary-default-score))
3806                      gnus-summary-zcore-fuzz))
3807              ?                          ;Whitespace
3808            (if (< score gnus-summary-default-score)
3809                gnus-score-below-mark gnus-score-over-mark))
3810          'score))
3811       ;; Do visual highlighting.
3812       (when (gnus-visual-p 'summary-highlight 'highlight)
3813         (gnus-summary-highlight-line)
3814         (gnus-run-hooks 'gnus-summary-update-hook)))))
3815
3816 (defvar gnus-tmp-new-adopts nil)
3817
3818 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3819   "Return the number of articles in THREAD.
3820 This may be 0 in some cases -- if none of the articles in
3821 the thread are to be displayed."
3822   (let* ((number
3823          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3824           (cond
3825            ((not (listp thread))
3826             1)
3827            ((and (consp thread) (cdr thread))
3828             (apply
3829              '+ 1 (mapcar
3830                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3831            ((null thread)
3832             1)
3833            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3834             1)
3835            (t 0))))
3836     (when (and level (zerop level) gnus-tmp-new-adopts)
3837       (incf number
3838             (apply '+ (mapcar
3839                        'gnus-summary-number-of-articles-in-thread
3840                        gnus-tmp-new-adopts))))
3841     (if char
3842         (if (> number 1) gnus-not-empty-thread-mark
3843           gnus-empty-thread-mark)
3844       number)))
3845
3846 (defsubst gnus-summary-line-message-size (head)
3847   "Return pretty-printed version of message size.
3848 This function is intended to be used in
3849 `gnus-summary-line-format-alist'."
3850   (let ((c (or (mail-header-chars head) -1)))
3851     (cond ((< c 0) "n/a")               ; chars not available
3852           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3853           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3854           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3855           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3856
3857 (defcustom gnus-summary-user-date-format-alist
3858   '(((gnus-seconds-today) . "Today, %H:%M")
3859     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3860     (604800 . "%A %H:%M")               ; That's one week
3861     ((gnus-seconds-month) . "%A %d")
3862     ((gnus-seconds-year) . "%B %d")
3863     (t . "%b %d %Y"))                   ; This one is used when no other
3864                                         ; does match
3865   "Specifies date format depending on age of article.
3866 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3867 seconds) or a Lisp expression evaluating to a number.  When the age of
3868 the article is less than this number, then use `format-time-string'
3869 with the corresponding FORMAT for displaying the date of the article.
3870 If AGE is not a number or a Lisp expression evaluating to a
3871 non-number, then the corresponding FORMAT is used as a default value.
3872
3873 Note that the list is processed from the beginning, so it should be
3874 sorted by ascending AGE.  Also note that items following the first
3875 non-number AGE will be ignored.
3876
3877 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3878 and `gnus-seconds-year' in the AGE spec.  They return the number of
3879 seconds passed since the start of today, of this month, of this year,
3880 respectively."
3881   :version "24.1"
3882   :group 'gnus-summary-format
3883   :type '(alist :key-type sexp :value-type string))
3884 (make-obsolete-variable 'gnus-user-date-format-alist
3885                         'gnus-summary-user-date-format-alist "24.1")
3886
3887 (defun gnus-user-date (messy-date)
3888   "Format the messy-date according to `gnus-summary-user-date-format-alist'.
3889 Returns \"  ?  \" if there's bad input or if another error occurs.
3890 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3891   (condition-case ()
3892       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3893              (now (gnus-float-time))
3894              ;;If we don't find something suitable we'll use this one
3895              (my-format "%b %d '%y"))
3896         (let* ((difference (- now messy-date))
3897                (templist gnus-summary-user-date-format-alist)
3898                (top (eval (caar templist))))
3899           (while (if (numberp top) (< top difference) (not top))
3900             (progn
3901               (setq templist (cdr templist))
3902               (setq top (eval (caar templist)))))
3903           (if (stringp (cdr (car templist)))
3904               (setq my-format (cdr (car templist)))))
3905         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3906     (error "  ?   ")))
3907
3908 (defun gnus-summary-set-local-parameters (group)
3909   "Go through the local params of GROUP and set all variable specs in that list."
3910   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3911                                         ; really variables.
3912     (dolist (elem (gnus-group-find-parameter group))
3913       (and (consp elem)                 ; Has to be a cons.
3914            (consp (cdr elem))           ; The cdr has to be a list.
3915            (symbolp (car elem))         ; Has to be a symbol in there.
3916            (not (memq (car elem) vars))
3917            (ignore-errors
3918              (push (car elem) vars)
3919              ;; Variables like `gnus-show-threads' that are globally
3920              ;; bound, if used as group parameters, need to get to be
3921              ;; buffer-local, whereas just parameters like `gcc-self',
3922              ;; `timestamp', etc. should not be bound as variables.
3923              (if (boundp (car elem))
3924                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3925                (eval (nth 1 elem))))))))
3926
3927 (defun gnus-summary-read-group (group &optional show-all no-article
3928                                       kill-buffer no-display backward
3929                                       select-articles)
3930   "Start reading news in newsgroup GROUP.
3931 If SHOW-ALL is non-nil, already read articles are also listed.
3932 If NO-ARTICLE is non-nil, no article is selected initially.
3933 If NO-DISPLAY, don't generate a summary buffer."
3934   (let (result)
3935     (while (and group
3936                 (null (setq result
3937                             (let ((gnus-auto-select-next nil))
3938                               (or (gnus-summary-read-group-1
3939                                    group show-all no-article
3940                                    kill-buffer no-display
3941                                    select-articles)
3942                                   (setq show-all nil
3943                                         select-articles nil)))))
3944                 (eq gnus-auto-select-next 'quietly))
3945       (set-buffer gnus-group-buffer)
3946       ;; The entry function called above goes to the next
3947       ;; group automatically, so we go two groups back
3948       ;; if we are searching for the previous group.
3949       (when backward
3950         (gnus-group-prev-unread-group 2))
3951       (if (not (equal group (gnus-group-group-name)))
3952           (setq group (gnus-group-group-name))
3953         (setq group nil)))
3954     result))
3955
3956 (defun gnus-summary-read-group-1 (group show-all no-article
3957                                         kill-buffer no-display
3958                                         &optional select-articles)
3959   ;; Killed foreign groups can't be entered.
3960   ;;  (when (and (not (gnus-group-native-p group))
3961   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3962   ;;    (error "Dead non-native groups can't be entered"))
3963   (gnus-message 5 "Retrieving newsgroup: %s..."
3964                 (gnus-group-decoded-name group))
3965   (let* ((new-group (gnus-summary-setup-buffer group))
3966          (quit-config (gnus-group-quit-config group))
3967          (did-select (and new-group (gnus-select-newsgroup
3968                                      group show-all select-articles))))
3969     (cond
3970      ;; This summary buffer exists already, so we just select it.
3971      ((not new-group)
3972       (gnus-set-global-variables)
3973       (when kill-buffer
3974         (gnus-kill-or-deaden-summary kill-buffer))
3975       (gnus-configure-windows 'summary 'force)
3976       (gnus-set-mode-line 'summary)
3977       (gnus-summary-position-point)
3978       (message "")
3979       t)
3980      ;; We couldn't select this group.
3981      ((null did-select)
3982       (when (and (eq major-mode 'gnus-summary-mode)
3983                  (not (equal (current-buffer) kill-buffer)))
3984         (kill-buffer (current-buffer))
3985         (if (not quit-config)
3986             (progn
3987               ;; Update the info -- marks might need to be removed,
3988               ;; for instance.
3989               (gnus-summary-update-info)
3990               (set-buffer gnus-group-buffer)
3991               (gnus-group-jump-to-group group)
3992               (gnus-group-next-unread-group 1))
3993           (gnus-handle-ephemeral-exit quit-config)))
3994       (if (null (gnus-list-of-unread-articles group))
3995           (gnus-message 3 "Group %s contains no messages" group)
3996         (gnus-message 3 "Can't select group"))
3997       nil)
3998      ;; The user did a `C-g' while prompting for number of articles,
3999      ;; so we exit this group.
4000      ((eq did-select 'quit)
4001       (and (eq major-mode 'gnus-summary-mode)
4002            (not (equal (current-buffer) kill-buffer))
4003            (kill-buffer (current-buffer)))
4004       (when kill-buffer
4005         (gnus-kill-or-deaden-summary kill-buffer))
4006       (if (not quit-config)
4007           (progn
4008             (set-buffer gnus-group-buffer)
4009             (gnus-group-jump-to-group group)
4010             (gnus-configure-windows 'group 'force))
4011         (gnus-handle-ephemeral-exit quit-config))
4012       ;; Finally signal the quit.
4013       (signal 'quit nil))
4014      ;; The group was successfully selected.
4015      (t
4016       (gnus-set-global-variables)
4017       ;; Save the active value in effect when the group was entered.
4018       (setq gnus-newsgroup-active
4019             (gnus-copy-sequence
4020              (gnus-active gnus-newsgroup-name)))
4021       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4022       ;; You can change the summary buffer in some way with this hook.
4023       (gnus-run-hooks 'gnus-select-group-hook)
4024       (when (memq 'summary (gnus-update-format-specifications
4025                             nil 'summary 'summary-mode 'summary-dummy))
4026         ;; The format specification for the summary line was updated,
4027         ;; so we need to update the mark positions as well.
4028         (gnus-update-summary-mark-positions))
4029       ;; Do score processing.
4030       (when gnus-use-scoring
4031         (gnus-possibly-score-headers))
4032       ;; Check whether to fill in the gaps in the threads.
4033       (when gnus-build-sparse-threads
4034         (gnus-build-sparse-threads))
4035       ;; Find the initial limit.
4036       (if show-all
4037           (let ((gnus-newsgroup-dormant nil))
4038             (gnus-summary-initial-limit show-all))
4039         (gnus-summary-initial-limit show-all))
4040       ;; Generate the summary buffer.
4041       (unless no-display
4042         (gnus-summary-prepare))
4043       (when gnus-use-trees
4044         (gnus-tree-open group)
4045         (setq gnus-summary-highlight-line-function
4046               'gnus-tree-highlight-article))
4047       ;; If the summary buffer is empty, but there are some low-scored
4048       ;; articles or some excluded dormants, we include these in the
4049       ;; buffer.
4050       (when (and (zerop (buffer-size))
4051                  (not no-display))
4052         (cond (gnus-newsgroup-dormant
4053                (gnus-summary-limit-include-dormant))
4054               ((and gnus-newsgroup-scored show-all)
4055                (gnus-summary-limit-include-expunged t))))
4056       ;; Function `gnus-apply-kill-file' must be called in this hook.
4057       (gnus-run-hooks 'gnus-apply-kill-hook)
4058       (if (and (zerop (buffer-size))
4059                (not no-display))
4060           (progn
4061             ;; This newsgroup is empty.
4062             (gnus-summary-catchup-and-exit nil t)
4063             (gnus-message 6 "No unread news")
4064             (when kill-buffer
4065               (gnus-kill-or-deaden-summary kill-buffer))
4066             ;; Return nil from this function.
4067             nil)
4068         ;; Hide conversation thread subtrees.  We cannot do this in
4069         ;; gnus-summary-prepare-hook since kill processing may not
4070         ;; work with hidden articles.
4071         (gnus-summary-maybe-hide-threads)
4072         (gnus-configure-windows 'summary)
4073         (when kill-buffer
4074           (gnus-kill-or-deaden-summary kill-buffer))
4075         (gnus-summary-auto-select-subject)
4076         ;; Show first unread article if requested.
4077         (if (and (not no-article)
4078                  (not no-display)
4079                  gnus-newsgroup-unreads
4080                  gnus-auto-select-first)
4081             (progn
4082               (let ((art (gnus-summary-article-number)))
4083                 (unless (and (not gnus-plugged)
4084                              (or (memq art gnus-newsgroup-undownloaded)
4085                                  (memq art gnus-newsgroup-downloadable)))
4086                   (gnus-summary-goto-article art))))
4087           ;; Don't select any articles.
4088           (gnus-summary-position-point)
4089           (gnus-configure-windows 'summary 'force)
4090           (gnus-set-mode-line 'summary))
4091         (when (and gnus-auto-center-group
4092                    (get-buffer-window gnus-group-buffer t))
4093           ;; Gotta use windows, because recenter does weird stuff if
4094           ;; the current buffer ain't the displayed window.
4095           (let ((owin (selected-window)))
4096             (select-window (get-buffer-window gnus-group-buffer t))
4097             (when (gnus-group-goto-group group)
4098               (recenter))
4099             (select-window owin)))
4100         ;; Mark this buffer as "prepared".
4101         (setq gnus-newsgroup-prepared t)
4102         (gnus-run-hooks 'gnus-summary-prepared-hook)
4103         (unless (gnus-ephemeral-group-p group)
4104           (gnus-group-update-group group))
4105         t)))))
4106
4107 (defun gnus-summary-auto-select-subject ()
4108   "Select the subject line on initial group entry."
4109   (goto-char (point-min))
4110   (cond
4111    ((eq gnus-auto-select-subject 'best)
4112     (gnus-summary-best-unread-subject))
4113    ((eq gnus-auto-select-subject 'unread)
4114     (gnus-summary-first-unread-subject))
4115    ((eq gnus-auto-select-subject 'unseen)
4116     (gnus-summary-first-unseen-subject))
4117    ((eq gnus-auto-select-subject 'unseen-or-unread)
4118     (gnus-summary-first-unseen-or-unread-subject))
4119    ((eq gnus-auto-select-subject 'first)
4120     ;; Do nothing.
4121     )
4122    ((functionp gnus-auto-select-subject)
4123     (funcall gnus-auto-select-subject))))
4124
4125 (defun gnus-summary-prepare ()
4126   "Generate the summary buffer."
4127   (interactive)
4128   (let ((inhibit-read-only t))
4129     (erase-buffer)
4130     (setq gnus-newsgroup-data nil
4131           gnus-newsgroup-data-reverse nil)
4132     (gnus-run-hooks 'gnus-summary-generate-hook)
4133     ;; Generate the buffer, either with threads or without.
4134     (when gnus-newsgroup-headers
4135       (gnus-summary-prepare-threads
4136        (if gnus-show-threads
4137            (gnus-sort-gathered-threads
4138             (funcall gnus-summary-thread-gathering-function
4139                      (gnus-sort-threads
4140                       (gnus-cut-threads (gnus-make-threads)))))
4141          ;; Unthreaded display.
4142          (gnus-sort-articles gnus-newsgroup-headers))))
4143     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4144     ;; Call hooks for modifying summary buffer.
4145     (goto-char (point-min))
4146     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4147
4148 (defsubst gnus-general-simplify-subject (subject)
4149   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4150   (setq subject
4151         (cond
4152          ;; Truncate the subject.
4153          (gnus-simplify-subject-functions
4154           (gnus-map-function gnus-simplify-subject-functions subject))
4155          ((numberp gnus-summary-gather-subject-limit)
4156           (setq subject (gnus-simplify-subject-re subject))
4157           (if (> (length subject) gnus-summary-gather-subject-limit)
4158               (substring subject 0 gnus-summary-gather-subject-limit)
4159             subject))
4160          ;; Fuzzily simplify it.
4161          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4162           (gnus-simplify-subject-fuzzy subject))
4163          ;; Just remove the leading "Re:".
4164          (t
4165           (gnus-simplify-subject-re subject))))
4166
4167   (if (and gnus-summary-gather-exclude-subject
4168            (string-match gnus-summary-gather-exclude-subject subject))
4169       nil                         ; This article shouldn't be gathered
4170     subject))
4171
4172 (defun gnus-summary-simplify-subject-query ()
4173   "Query where the respool algorithm would put this article."
4174   (interactive)
4175   (gnus-summary-select-article)
4176   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4177
4178 (defun gnus-gather-threads-by-subject (threads)
4179   "Gather threads by looking at Subject headers."
4180   (if (not gnus-summary-make-false-root)
4181       threads
4182     (let ((hashtb (gnus-make-hashtable 1024))
4183           (prev threads)
4184           (result threads)
4185           subject hthread whole-subject)
4186       (while threads
4187         (setq subject (gnus-general-simplify-subject
4188                        (setq whole-subject (mail-header-subject
4189                                             (caar threads)))))
4190         (when subject
4191           (if (setq hthread (gnus-gethash subject hashtb))
4192               (progn
4193                 ;; We enter a dummy root into the thread, if we
4194                 ;; haven't done that already.
4195                 (unless (stringp (caar hthread))
4196                   (setcar hthread (list whole-subject (car hthread))))
4197                 ;; We add this new gathered thread to this gathered
4198                 ;; thread.
4199                 (setcdr (car hthread)
4200                         (nconc (cdar hthread) (list (car threads))))
4201                 ;; Remove it from the list of threads.
4202                 (setcdr prev (cdr threads))
4203                 (setq threads prev))
4204             ;; Enter this thread into the hash table.
4205             (gnus-sethash subject
4206                           (if gnus-summary-make-false-root-always
4207                               (progn
4208                                 ;; If you want a dummy root above all
4209                                 ;; threads...
4210                                 (setcar threads (list whole-subject
4211                                                       (car threads)))
4212                                 threads)
4213                             threads)
4214                           hashtb)))
4215         (setq prev threads)
4216         (setq threads (cdr threads)))
4217       result)))
4218
4219 (defun gnus-gather-threads-by-references (threads)
4220   "Gather threads by looking at References headers."
4221   (let ((idhashtb (gnus-make-hashtable 1024))
4222         (thhashtb (gnus-make-hashtable 1024))
4223         (prev threads)
4224         (result threads)
4225         ids references id gthread gid entered ref)
4226     (while threads
4227       (when (setq references (mail-header-references (caar threads)))
4228         (setq id (mail-header-id (caar threads))
4229               ids (inline (gnus-split-references references))
4230               entered nil)
4231         (while (setq ref (pop ids))
4232           (setq ids (delete ref ids))
4233           (if (not (setq gid (gnus-gethash ref idhashtb)))
4234               (progn
4235                 (gnus-sethash ref id idhashtb)
4236                 (gnus-sethash id threads thhashtb))
4237             (setq gthread (gnus-gethash gid thhashtb))
4238             (unless entered
4239               ;; We enter a dummy root into the thread, if we
4240               ;; haven't done that already.
4241               (unless (stringp (caar gthread))
4242                 (setcar gthread (list (mail-header-subject (caar gthread))
4243                                       (car gthread))))
4244               ;; We add this new gathered thread to this gathered
4245               ;; thread.
4246               (setcdr (car gthread)
4247                       (nconc (cdar gthread) (list (car threads)))))
4248             ;; Add it into the thread hash table.
4249             (gnus-sethash id gthread thhashtb)
4250             (setq entered t)
4251             ;; Remove it from the list of threads.
4252             (setcdr prev (cdr threads))
4253             (setq threads prev))))
4254       (setq prev threads)
4255       (setq threads (cdr threads)))
4256     result))
4257
4258 (defun gnus-sort-gathered-threads (threads)
4259   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4260   (let ((result threads))
4261     (while threads
4262       (when (stringp (caar threads))
4263         (setcdr (car threads)
4264                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4265       (setq threads (cdr threads)))
4266     result))
4267
4268 (defun gnus-thread-loop-p (root thread)
4269   "Say whether ROOT is in THREAD."
4270   (let ((stack (list thread))
4271         (infloop 0)
4272         th)
4273     (while (setq thread (pop stack))
4274       (setq th (cdr thread))
4275       (while (and th
4276                   (not (eq (caar th) root)))
4277         (pop th))
4278       (if th
4279           ;; We have found a loop.
4280           (let (ref-dep)
4281             (setcdr thread (delq (car th) (cdr thread)))
4282             (if (boundp (setq ref-dep (intern "none"
4283                                               gnus-newsgroup-dependencies)))
4284                 (setcdr (symbol-value ref-dep)
4285                         (nconc (cdr (symbol-value ref-dep))
4286                                (list (car th))))
4287               (set ref-dep (list nil (car th))))
4288             (setq infloop 1
4289                   stack nil))
4290         ;; Push all the subthreads onto the stack.
4291         (push (cdr thread) stack)))
4292     infloop))
4293
4294 (defun gnus-make-threads ()
4295   "Go through the dependency hashtb and find the roots.  Return all threads."
4296   (let (threads)
4297     (while (catch 'infloop
4298              (mapatoms
4299               (lambda (refs)
4300                 ;; Deal with self-referencing References loops.
4301                 (when (and (car (symbol-value refs))
4302                            (not (zerop
4303                                  (apply
4304                                   '+
4305                                   (mapcar
4306                                    (lambda (thread)
4307                                      (gnus-thread-loop-p
4308                                       (car (symbol-value refs)) thread))
4309                                    (cdr (symbol-value refs)))))))
4310                   (setq threads nil)
4311                   (throw 'infloop t))
4312                 (unless (car (symbol-value refs))
4313                   ;; These threads do not refer back to any other
4314                   ;; articles, so they're roots.
4315                   (setq threads (append (cdr (symbol-value refs)) threads))))
4316               gnus-newsgroup-dependencies)))
4317     threads))
4318
4319 ;; Build the thread tree.
4320 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4321   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4322
4323 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4324 if it was already present.
4325
4326 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4327 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4328 Message-IDs will be renamed to a unique Message-ID before being
4329 entered.
4330
4331 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4332   (let* ((id (mail-header-id header))
4333          (id-dep (and id (intern id dependencies)))
4334          parent-id ref ref-dep ref-header replaced)
4335     ;; Enter this `header' in the `dependencies' table.
4336     (cond
4337      ((not id-dep)
4338       (setq header nil))
4339      ;; The first two cases do the normal part: enter a new `header'
4340      ;; in the `dependencies' table.
4341      ((not (boundp id-dep))
4342       (set id-dep (list header)))
4343      ((null (car (symbol-value id-dep)))
4344       (setcar (symbol-value id-dep) header))
4345
4346      ;; From here the `header' was already present in the
4347      ;; `dependencies' table.
4348      (force-new
4349       ;; Overrides an existing entry;
4350       ;; just set the header part of the entry.
4351       (setcar (symbol-value id-dep) header)
4352       (setq replaced t))
4353
4354      ;; Renames the existing `header' to a unique Message-ID.
4355      ((not gnus-summary-ignore-duplicates)
4356       ;; An article with this Message-ID has already been seen.
4357       ;; We rename the Message-ID.
4358       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4359            (list header))
4360       (mail-header-set-id header id))
4361
4362      ;; The last case ignores an existing entry, except it adds any
4363      ;; additional Xrefs (in case the two articles came from different
4364      ;; servers.
4365      ;; Also sets `header' to `nil' meaning that the `dependencies'
4366      ;; table was *not* modified.
4367      (t
4368       (mail-header-set-xref
4369        (car (symbol-value id-dep))
4370        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4371                    "")
4372                (or (mail-header-xref header) "")))
4373       (setq header nil)))
4374
4375     (when (and header (not replaced))
4376       ;; First check that we are not creating a References loop.
4377       (setq parent-id (gnus-parent-id (mail-header-references header)))
4378       (setq ref parent-id)
4379       (while (and ref
4380                   (setq ref-dep (intern-soft ref dependencies))
4381                   (boundp ref-dep)
4382                   (setq ref-header (car (symbol-value ref-dep))))
4383         (if (string= id ref)
4384             ;; Yuk!  This is a reference loop.  Make the article be a
4385             ;; root article.
4386             (progn
4387               (mail-header-set-references (car (symbol-value id-dep)) "none")
4388               (setq ref nil)
4389               (setq parent-id nil))
4390           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4391       (setq ref-dep (intern (or parent-id "none") dependencies))
4392       (if (boundp ref-dep)
4393           (setcdr (symbol-value ref-dep)
4394                   (nconc (cdr (symbol-value ref-dep))
4395                          (list (symbol-value id-dep))))
4396         (set ref-dep (list nil (symbol-value id-dep)))))
4397     header))
4398
4399 (defun gnus-extract-message-id-from-in-reply-to (string)
4400   (if (string-match "<[^>]+>" string)
4401       (substring string (match-beginning 0) (match-end 0))
4402     nil))
4403
4404 (defun gnus-build-sparse-threads ()
4405   (let ((headers gnus-newsgroup-headers)
4406         (mail-parse-charset gnus-newsgroup-charset)
4407         (gnus-summary-ignore-duplicates t)
4408         header references generation relations
4409         subject child end new-child date)
4410     ;; First we create an alist of generations/relations, where
4411     ;; generations is how much we trust the relation, and the relation
4412     ;; is parent/child.
4413     (gnus-message 7 "Making sparse threads...")
4414     (save-excursion
4415       (nnheader-set-temp-buffer " *gnus sparse threads*")
4416       (while (setq header (pop headers))
4417         (when (and (setq references (mail-header-references header))
4418                    (not (string= references "")))
4419           (insert references)
4420           (setq child (mail-header-id header)
4421                 subject (mail-header-subject header)
4422                 date (mail-header-date header)
4423                 generation 0)
4424           (while (search-backward ">" nil t)
4425             (setq end (1+ (point)))
4426             (when (search-backward "<" nil t)
4427               (setq new-child (buffer-substring (point) end))
4428               (push (list (incf generation)
4429                           child (setq child new-child)
4430                           subject date)
4431                     relations)))
4432           (when child
4433             (push (list (1+ generation) child nil subject) relations))
4434           (erase-buffer)))
4435       (kill-buffer (current-buffer)))
4436     ;; Sort over trustworthiness.
4437     (dolist (relation (sort relations 'car-less-than-car))
4438       (when (gnus-dependencies-add-header
4439              (make-full-mail-header
4440               gnus-reffed-article-number
4441               (nth 3 relation) "" (or (nth 4 relation) "")
4442               (nth 1 relation)
4443               (or (nth 2 relation) "") 0 0 "")
4444              gnus-newsgroup-dependencies nil)
4445         (push gnus-reffed-article-number gnus-newsgroup-limit)
4446         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4447         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4448               gnus-newsgroup-reads)
4449         (decf gnus-reffed-article-number)))
4450     (gnus-message 7 "Making sparse threads...done")))
4451
4452 (defun gnus-build-old-threads ()
4453   ;; Look at all the articles that refer back to old articles, and
4454   ;; fetch the headers for the articles that aren't there.  This will
4455   ;; build complete threads - if the roots haven't been expired by the
4456   ;; server, that is.
4457   (let ((mail-parse-charset gnus-newsgroup-charset)
4458         id heads)
4459     (mapatoms
4460      (lambda (refs)
4461        (when (not (car (symbol-value refs)))
4462          (setq heads (cdr (symbol-value refs)))
4463          (while heads
4464            (if (memq (mail-header-number (caar heads))
4465                      gnus-newsgroup-dormant)
4466                (setq heads (cdr heads))
4467              (setq id (symbol-name refs))
4468              (while (and (setq id (gnus-build-get-header id))
4469                          (not (car (gnus-id-to-thread id)))))
4470              (setq heads nil)))))
4471      gnus-newsgroup-dependencies)))
4472
4473 (defsubst gnus-remove-odd-characters (string)
4474   "Translate STRING into something that doesn't contain weird characters."
4475   (mm-subst-char-in-string
4476    ?\r ?\-
4477    (mm-subst-char-in-string ?\n ?\- string t) t))
4478
4479 ;; This function has to be called with point after the article number
4480 ;; on the beginning of the line.
4481 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4482   (let ((eol (point-at-eol))
4483         (buffer (current-buffer))
4484         header references in-reply-to)
4485
4486     ;; overview: [num subject from date id refs chars lines misc]
4487     (unwind-protect
4488         (let (x)
4489           (narrow-to-region (point) eol)
4490           (unless (eobp)
4491             (forward-char))
4492
4493           (setq header
4494                 (make-full-mail-header
4495                  number                 ; number
4496                  (condition-case ()     ; subject
4497                      (gnus-remove-odd-characters
4498                       (funcall gnus-decode-encoded-word-function
4499                                (setq x (nnheader-nov-field))))
4500                    (error x))
4501                  (condition-case ()     ; from
4502                      (gnus-remove-odd-characters
4503                       (funcall gnus-decode-encoded-address-function
4504                                (setq x (nnheader-nov-field))))
4505                    (error x))
4506                  (nnheader-nov-field)   ; date
4507                  (nnheader-nov-read-message-id number)  ; id
4508                  (setq references (nnheader-nov-field)) ; refs
4509                  (nnheader-nov-read-integer) ; chars
4510                  (nnheader-nov-read-integer) ; lines
4511                  (unless (eobp)
4512                    (if (looking-at "Xref: ")
4513                        (goto-char (match-end 0)))
4514                    (nnheader-nov-field)) ; Xref
4515                  (nnheader-nov-parse-extra)))) ; extra
4516
4517       (widen))
4518
4519     (when (and (string= references "")
4520                (setq in-reply-to (mail-header-extra header))
4521                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4522       (mail-header-set-references
4523        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4524
4525     (when gnus-alter-header-function
4526       (funcall gnus-alter-header-function header))
4527     (gnus-dependencies-add-header header dependencies force-new)))
4528
4529 (defun gnus-build-get-header (id)
4530   "Look through the buffer of NOV lines and find the header to ID.
4531 Enter this line into the dependencies hash table, and return
4532 the id of the parent article (if any)."
4533   (let ((deps gnus-newsgroup-dependencies)
4534         found header)
4535     (prog1
4536         (with-current-buffer nntp-server-buffer
4537           (let ((case-fold-search nil))
4538             (goto-char (point-min))
4539             (while (and (not found)
4540                         (search-forward id nil t))
4541               (beginning-of-line)
4542               (setq found (looking-at
4543                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4544                                    (regexp-quote id))))
4545               (or found (beginning-of-line 2)))
4546             (when found
4547               (beginning-of-line)
4548               (and
4549                (setq header (gnus-nov-parse-line
4550                              (read (current-buffer)) deps))
4551                (gnus-parent-id (mail-header-references header))))))
4552       (when header
4553         (let ((number (mail-header-number header)))
4554           (push number gnus-newsgroup-limit)
4555           (push header gnus-newsgroup-headers)
4556           (if (memq number gnus-newsgroup-unselected)
4557               (progn
4558                 (setq gnus-newsgroup-unreads
4559                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4560                                                number))
4561                 (setq gnus-newsgroup-unselected
4562                       (delq number gnus-newsgroup-unselected)))
4563             (push number gnus-newsgroup-ancient)))))))
4564
4565 (defun gnus-build-all-threads ()
4566   "Read all the headers."
4567   (let ((gnus-summary-ignore-duplicates t)
4568         (mail-parse-charset gnus-newsgroup-charset)
4569         (dependencies gnus-newsgroup-dependencies)
4570         header article)
4571     (with-current-buffer nntp-server-buffer
4572       (let ((case-fold-search nil))
4573         (goto-char (point-min))
4574         (while (not (eobp))
4575           (ignore-errors
4576             (setq article (read (current-buffer))
4577                   header (gnus-nov-parse-line article dependencies t)))
4578           (when header
4579             (with-current-buffer gnus-summary-buffer
4580               (push header gnus-newsgroup-headers)
4581               (if (memq (setq article (mail-header-number header))
4582                         gnus-newsgroup-unselected)
4583                   (progn
4584                     (setq gnus-newsgroup-unreads
4585                           (gnus-add-to-sorted-list
4586                            gnus-newsgroup-unreads article))
4587                     (setq gnus-newsgroup-unselected
4588                           (delq article gnus-newsgroup-unselected)))
4589                 (push article gnus-newsgroup-ancient)))
4590             (forward-line 1)))))))
4591
4592 (defun gnus-summary-update-article-line (article header)
4593   "Update the line for ARTICLE using HEADER."
4594   (let* ((id (mail-header-id header))
4595          (thread (gnus-id-to-thread id)))
4596     (unless thread
4597       (error "Article in no thread"))
4598     ;; Update the thread.
4599     (setcar thread header)
4600     (gnus-summary-goto-subject article)
4601     (let* ((datal (gnus-data-find-list article))
4602            (data (car datal))
4603            (inhibit-read-only t)
4604            (level (gnus-summary-thread-level)))
4605       (gnus-delete-line)
4606       (let ((inserted (- (point)
4607                          (progn
4608                            (gnus-summary-insert-line
4609                             header level nil
4610                             (memq article gnus-newsgroup-undownloaded)
4611                             (gnus-article-mark article)
4612                             (memq article gnus-newsgroup-replied)
4613                             (memq article gnus-newsgroup-expirable)
4614                             ;; Only insert the Subject string when it's different
4615                             ;; from the previous Subject string.
4616                             (if (and
4617                                  gnus-show-threads
4618                                  (gnus-subject-equal
4619                                   (condition-case ()
4620                                       (mail-header-subject
4621                                        (gnus-data-header
4622                                         (cadr
4623                                          (gnus-data-find-list
4624                                           article
4625                                           (gnus-data-list t)))))
4626                                     ;; Error on the side of excessive subjects.
4627                                     (error ""))
4628                                   (mail-header-subject header)))
4629                                 ""
4630                               (mail-header-subject header))
4631                             nil (cdr (assq article gnus-newsgroup-scored))
4632                             (memq article gnus-newsgroup-processable))
4633                            (point)))))
4634         (when (cdr datal)
4635           (gnus-data-update-list
4636            (cdr datal)
4637            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4638
4639 (defun gnus-summary-update-article (article &optional iheader)
4640   "Update ARTICLE in the summary buffer."
4641   (set-buffer gnus-summary-buffer)
4642   (let* ((header (gnus-summary-article-header article))
4643          (id (mail-header-id header))
4644          (data (gnus-data-find article))
4645          (thread (gnus-id-to-thread id))
4646          (references (mail-header-references header))
4647          (parent
4648           (gnus-id-to-thread
4649            (or (gnus-parent-id
4650                 (when (and references
4651                            (not (equal "" references)))
4652                   references))
4653                "none")))
4654          (inhibit-read-only t)
4655          (old (car thread)))
4656     (when thread
4657       (unless iheader
4658         (setcar thread nil)
4659         (when parent
4660           (delq thread parent)))
4661       (if (gnus-summary-insert-subject id header)
4662           ;; Set the (possibly) new article number in the data structure.
4663           (gnus-data-set-number data (gnus-id-to-article id))
4664         (setcar thread old)
4665         nil))))
4666
4667 (defun gnus-rebuild-thread (id &optional line)
4668   "Rebuild the thread containing ID.
4669 If LINE, insert the rebuilt thread starting on line LINE."
4670   (let ((inhibit-read-only t)
4671         old-pos current thread data)
4672     (if (not gnus-show-threads)
4673         (setq thread (list (car (gnus-id-to-thread id))))
4674       ;; Get the thread this article is part of.
4675       (setq thread (gnus-remove-thread id)))
4676     (setq old-pos (point-at-bol))
4677     (setq current (save-excursion
4678                     (and (re-search-backward "[\r\n]" nil t)
4679                          (gnus-summary-article-number))))
4680     ;; If this is a gathered thread, we have to go some re-gathering.
4681     (when (stringp (car thread))
4682       (let ((subject (car thread))
4683             roots thr)
4684         (setq thread (cdr thread))
4685         (while thread
4686           (unless (memq (setq thr (gnus-id-to-thread
4687                                    (gnus-root-id
4688                                     (mail-header-id (caar thread)))))
4689                         roots)
4690             (push thr roots))
4691           (setq thread (cdr thread)))
4692         ;; We now have all (unique) roots.
4693         (if (= (length roots) 1)
4694             ;; All the loose roots are now one solid root.
4695             (setq thread (car roots))
4696           (setq thread (cons subject (gnus-sort-threads roots))))))
4697     (let (threads)
4698       ;; We then insert this thread into the summary buffer.
4699       (when line
4700         (goto-char (point-min))
4701         (forward-line (1- line)))
4702       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4703         (if gnus-show-threads
4704             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4705           (gnus-summary-prepare-unthreaded thread))
4706         (setq data (nreverse gnus-newsgroup-data))
4707         (setq threads gnus-newsgroup-threads))
4708       ;; We splice the new data into the data structure.
4709       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4710       ;;!!! then we want to insert at the beginning of the buffer.
4711       ;;!!! That happens to be true with Gnus now, but that may
4712       ;;!!! change in the future.  Perhaps.
4713       (gnus-data-enter-list
4714        (if line nil current) data (- (point) old-pos))
4715       (setq gnus-newsgroup-threads
4716             (nconc threads gnus-newsgroup-threads))
4717       (gnus-data-compute-positions))))
4718
4719 (defun gnus-number-to-header (number)
4720   "Return the header for article NUMBER."
4721   (let ((headers gnus-newsgroup-headers))
4722     (while (and headers
4723                 (not (= number (mail-header-number (car headers)))))
4724       (pop headers))
4725     (when headers
4726       (car headers))))
4727
4728 (defun gnus-parent-headers (in-headers &optional generation)
4729   "Return the headers of the GENERATIONeth parent of HEADERS."
4730   (unless generation
4731     (setq generation 1))
4732   (let ((parent t)
4733         (headers in-headers)
4734         references)
4735     (while (and parent
4736                 (not (zerop generation))
4737                 (setq references (mail-header-references headers)))
4738       (setq headers (if (and references
4739                              (setq parent (gnus-parent-id references)))
4740                         (car (gnus-id-to-thread parent))
4741                       nil))
4742       (decf generation))
4743     (and (not (eq headers in-headers))
4744          headers)))
4745
4746 (defun gnus-id-to-thread (id)
4747   "Return the (sub-)thread where ID appears."
4748   (gnus-gethash id gnus-newsgroup-dependencies))
4749
4750 (defun gnus-id-to-article (id)
4751   "Return the article number of ID."
4752   (let ((thread (gnus-id-to-thread id)))
4753     (when (and thread
4754                (car thread))
4755       (mail-header-number (car thread)))))
4756
4757 (defun gnus-id-to-header (id)
4758   "Return the article headers of ID."
4759   (car (gnus-id-to-thread id)))
4760
4761 (defun gnus-article-displayed-root-p (article)
4762   "Say whether ARTICLE is a root(ish) article."
4763   (let ((level (gnus-summary-thread-level article))
4764         (refs (mail-header-references  (gnus-summary-article-header article)))
4765         particle)
4766     (cond
4767      ((null level) nil)
4768      ((zerop level) t)
4769      ((null refs) t)
4770      ((null (gnus-parent-id refs)) t)
4771      ((and (= 1 level)
4772            (null (setq particle (gnus-id-to-article
4773                                  (gnus-parent-id refs))))
4774            (null (gnus-summary-thread-level particle)))))))
4775
4776 (defun gnus-root-id (id)
4777   "Return the id of the root of the thread where ID appears."
4778   (let (last-id prev)
4779     (while (and id (setq prev (car (gnus-id-to-thread id))))
4780       (setq last-id id
4781             id (gnus-parent-id (mail-header-references prev))))
4782     last-id))
4783
4784 (defun gnus-articles-in-thread (thread)
4785   "Return the list of articles in THREAD."
4786   (cons (mail-header-number (car thread))
4787         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4788
4789 (defun gnus-remove-thread (id &optional dont-remove)
4790   "Remove the thread that has ID in it."
4791   (let (headers thread last-id)
4792     ;; First go up in this thread until we find the root.
4793     (setq last-id (gnus-root-id id)
4794           headers (message-flatten-list (gnus-id-to-thread last-id)))
4795     ;; We have now found the real root of this thread.  It might have
4796     ;; been gathered into some loose thread, so we have to search
4797     ;; through the threads to find the thread we wanted.
4798     (let ((threads gnus-newsgroup-threads)
4799           sub)
4800       (while threads
4801         (setq sub (car threads))
4802         (if (stringp (car sub))
4803             ;; This is a gathered thread, so we look at the roots
4804             ;; below it to find whether this article is in this
4805             ;; gathered root.
4806             (progn
4807               (setq sub (cdr sub))
4808               (while sub
4809                 (when (member (caar sub) headers)
4810                   (setq thread (car threads)
4811                         threads nil
4812                         sub nil))
4813                 (setq sub (cdr sub))))
4814           ;; It's an ordinary thread, so we check it.
4815           (when (eq (car sub) (car headers))
4816             (setq thread sub
4817                   threads nil)))
4818         (setq threads (cdr threads)))
4819       ;; If this article is in no thread, then it's a root.
4820       (if thread
4821           (unless dont-remove
4822             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4823         (setq thread (gnus-id-to-thread last-id)))
4824       (when thread
4825         (prog1
4826             thread                      ; We return this thread.
4827           (unless dont-remove
4828             (if (stringp (car thread))
4829                 (progn
4830                   ;; If we use dummy roots, then we have to remove the
4831                   ;; dummy root as well.
4832                   (when (eq gnus-summary-make-false-root 'dummy)
4833                     ;; We go to the dummy root by going to
4834                     ;; the first sub-"thread", and then one line up.
4835                     (gnus-summary-goto-article
4836                      (mail-header-number (caadr thread)))
4837                     (forward-line -1)
4838                     (gnus-delete-line)
4839                     (gnus-data-compute-positions))
4840                   (setq thread (cdr thread))
4841                   (while thread
4842                     (gnus-remove-thread-1 (car thread))
4843                     (setq thread (cdr thread))))
4844               (gnus-remove-thread-1 thread))))))))
4845
4846 (defun gnus-remove-thread-1 (thread)
4847   "Remove the thread THREAD recursively."
4848   (let ((number (mail-header-number (pop thread)))
4849         d)
4850     (setq thread (reverse thread))
4851     (while thread
4852       (gnus-remove-thread-1 (pop thread)))
4853     (when (setq d (gnus-data-find number))
4854       (goto-char (gnus-data-pos d))
4855       (gnus-summary-show-thread)
4856       (gnus-data-remove
4857        number
4858        (- (point-at-bol)
4859           (prog1
4860               (1+ (point-at-eol))
4861             (gnus-delete-line)))))))
4862
4863 (defun gnus-sort-threads-recursive (threads func)
4864   (sort (mapcar (lambda (thread)
4865                   (cons (car thread)
4866                         (and (cdr thread)
4867                              (gnus-sort-threads-recursive (cdr thread) func))))
4868                 threads) func))
4869
4870 (defun gnus-sort-threads-loop (threads func)
4871   (let* ((superthread (cons nil threads))
4872          (stack (list (cons superthread threads)))
4873          remaining-threads thread)
4874     (while stack
4875       (setq remaining-threads (cdr (car stack)))
4876       (if remaining-threads
4877           (progn (setq thread (car remaining-threads))
4878                  (setcdr (car stack) (cdr remaining-threads))
4879                  (if (cdr thread)
4880                      (push (cons thread (cdr thread)) stack)))
4881         (setq thread (caar stack))
4882         (setcdr thread (sort (cdr thread) func))
4883         (pop stack)))
4884     (cdr superthread)))
4885
4886 (defun gnus-sort-threads (threads)
4887   "Sort THREADS."
4888   (if (not gnus-thread-sort-functions)
4889       threads
4890     (gnus-message 8 "Sorting threads...")
4891     (prog1
4892         (condition-case nil
4893             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4894               (gnus-sort-threads-recursive
4895                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4896           ;; Even after binding max-lisp-eval-depth, the recursive
4897           ;; sorter might fail for very long threads.  In that case,
4898           ;; try using a (less well-tested) non-recursive sorter.
4899           (error (gnus-message 9 "Sorting threads with loop...")
4900                  (gnus-sort-threads-loop
4901                   threads (gnus-make-sort-function
4902                            gnus-thread-sort-functions))))
4903       (gnus-message 8 "Sorting threads...done"))))
4904
4905 (defun gnus-sort-articles (articles)
4906   "Sort ARTICLES."
4907   (when gnus-article-sort-functions
4908     (gnus-message 7 "Sorting articles...")
4909     (prog1
4910         (setq gnus-newsgroup-headers
4911               (sort articles (gnus-make-sort-function
4912                               gnus-article-sort-functions)))
4913       (gnus-message 7 "Sorting articles...done"))))
4914
4915 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4916 (defmacro gnus-thread-header (thread)
4917   "Return header of first article in THREAD.
4918 Note that THREAD must never, ever be anything else than a variable -
4919 using some other form will lead to serious barfage."
4920   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4921   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4922   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4923         (vector thread) 2))
4924
4925 (defsubst gnus-article-sort-by-number (h1 h2)
4926   "Sort articles by article number."
4927   (< (mail-header-number h1)
4928      (mail-header-number h2)))
4929
4930 (defun gnus-thread-sort-by-number (h1 h2)
4931   "Sort threads by root article number."
4932   (gnus-article-sort-by-number
4933    (gnus-thread-header h1) (gnus-thread-header h2)))
4934
4935 (defsubst gnus-article-sort-by-random (h1 h2)
4936   "Sort articles randomly."
4937   (zerop (random 2)))
4938
4939 (defun gnus-thread-sort-by-random (h1 h2)
4940   "Sort threads randomly."
4941   (gnus-article-sort-by-random
4942    (gnus-thread-header h1) (gnus-thread-header h2)))
4943
4944 (defsubst gnus-article-sort-by-lines (h1 h2)
4945   "Sort articles by article Lines header."
4946   (< (mail-header-lines h1)
4947      (mail-header-lines h2)))
4948
4949 (defun gnus-thread-sort-by-lines (h1 h2)
4950   "Sort threads by root article Lines header."
4951   (gnus-article-sort-by-lines
4952    (gnus-thread-header h1) (gnus-thread-header h2)))
4953
4954 (defsubst gnus-article-sort-by-chars (h1 h2)
4955   "Sort articles by octet length."
4956   (< (mail-header-chars h1)
4957      (mail-header-chars h2)))
4958
4959 (defun gnus-thread-sort-by-chars (h1 h2)
4960   "Sort threads by root article octet length."
4961   (gnus-article-sort-by-chars
4962    (gnus-thread-header h1) (gnus-thread-header h2)))
4963
4964 (defsubst gnus-article-sort-by-author (h1 h2)
4965   "Sort articles by root author."
4966   (gnus-string<
4967    (let ((extract (funcall
4968                    gnus-extract-address-components
4969                    (mail-header-from h1))))
4970      (or (car extract) (cadr extract) ""))
4971    (let ((extract (funcall
4972                    gnus-extract-address-components
4973                    (mail-header-from h2))))
4974      (or (car extract) (cadr extract) ""))))
4975
4976 (defun gnus-thread-sort-by-author (h1 h2)
4977   "Sort threads by root author."
4978   (gnus-article-sort-by-author
4979    (gnus-thread-header h1)  (gnus-thread-header h2)))
4980
4981 (defsubst gnus-article-sort-by-recipient (h1 h2)
4982   "Sort articles by recipient."
4983   (gnus-string<
4984    (let ((extract (funcall
4985                    gnus-extract-address-components
4986                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4987      (or (car extract) (cadr extract)))
4988    (let ((extract (funcall
4989                    gnus-extract-address-components
4990                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4991      (or (car extract) (cadr extract)))))
4992
4993 (defun gnus-thread-sort-by-recipient (h1 h2)
4994   "Sort threads by root recipient."
4995   (gnus-article-sort-by-recipient
4996    (gnus-thread-header h1) (gnus-thread-header h2)))
4997
4998 (defsubst gnus-article-sort-by-subject (h1 h2)
4999   "Sort articles by root subject."
5000   (gnus-string<
5001    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5002    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5003
5004 (defun gnus-thread-sort-by-subject (h1 h2)
5005   "Sort threads by root subject."
5006   (gnus-article-sort-by-subject
5007    (gnus-thread-header h1) (gnus-thread-header h2)))
5008
5009 (defsubst gnus-article-sort-by-date (h1 h2)
5010   "Sort articles by root article date."
5011   (time-less-p
5012    (gnus-date-get-time (mail-header-date h1))
5013    (gnus-date-get-time (mail-header-date h2))))
5014
5015 (defun gnus-thread-sort-by-date (h1 h2)
5016   "Sort threads by root article date."
5017   (gnus-article-sort-by-date
5018    (gnus-thread-header h1) (gnus-thread-header h2)))
5019
5020 (defsubst gnus-article-sort-by-score (h1 h2)
5021   "Sort articles by root article score.
5022 Unscored articles will be counted as having a score of zero."
5023   (> (or (cdr (assq (mail-header-number h1)
5024                     gnus-newsgroup-scored))
5025          gnus-summary-default-score 0)
5026      (or (cdr (assq (mail-header-number h2)
5027                     gnus-newsgroup-scored))
5028          gnus-summary-default-score 0)))
5029
5030 (defun gnus-thread-sort-by-score (h1 h2)
5031   "Sort threads by root article score."
5032   (gnus-article-sort-by-score
5033    (gnus-thread-header h1) (gnus-thread-header h2)))
5034
5035 (defun gnus-thread-sort-by-total-score (h1 h2)
5036   "Sort threads by the sum of all scores in the thread.
5037 Unscored articles will be counted as having a score of zero."
5038   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5039
5040 (defun gnus-thread-total-score (thread)
5041   ;; This function find the total score of THREAD.
5042   (cond
5043    ((null thread)
5044     0)
5045    ((consp thread)
5046     (if (stringp (car thread))
5047         (apply gnus-thread-score-function 0
5048                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5049       (gnus-thread-total-score-1 thread)))
5050    (t
5051     (gnus-thread-total-score-1 (list thread)))))
5052
5053 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5054   "Sort articles by number."
5055   (gnus-article-sort-by-number h1 h2))
5056
5057 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5058   "Sort threads such that the thread with the most recently arrived article comes first."
5059   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5060
5061 (defun gnus-thread-highest-number (thread)
5062   "Return the highest article number in THREAD."
5063   (apply 'max (mapcar (lambda (header)
5064                         (mail-header-number header))
5065                       (message-flatten-list thread))))
5066
5067 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5068   "Sort articles by number."
5069   (gnus-article-sort-by-date h1 h2))
5070
5071 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5072   "Sort threads such that the thread with the most recently dated article comes first."
5073   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5074
5075 ; Since this is called not only to sort the top-level threads, but
5076 ; also in recursive sorts to order the articles within a thread, each
5077 ; article will be processed many times.  Thus it speeds things up
5078 ; quite a bit to use gnus-date-get-time, which caches the time value.
5079 (defun gnus-thread-latest-date (thread)
5080   "Return the highest article date in THREAD."
5081   (apply 'max
5082          (mapcar (lambda (header) (gnus-float-time
5083                                    (gnus-date-get-time
5084                                     (mail-header-date header))))
5085                  (message-flatten-list thread))))
5086
5087 (defun gnus-thread-total-score-1 (root)
5088   ;; This function find the total score of the thread below ROOT.
5089   (setq root (car root))
5090   (apply gnus-thread-score-function
5091          (or (append
5092               (mapcar 'gnus-thread-total-score
5093                       (cdr (gnus-id-to-thread (mail-header-id root))))
5094               (when (> (mail-header-number root) 0)
5095                 (list (or (cdr (assq (mail-header-number root)
5096                                      gnus-newsgroup-scored))
5097                           gnus-summary-default-score 0))))
5098              (list gnus-summary-default-score)
5099              '(0))))
5100
5101 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5102 (defvar gnus-tmp-prev-subject nil)
5103 (defvar gnus-tmp-false-parent nil)
5104 (defvar gnus-tmp-root-expunged nil)
5105 (defvar gnus-tmp-dummy-line nil)
5106
5107 (defun gnus-extra-header (type &optional header)
5108   "Return the extra header of TYPE."
5109   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5110       ""))
5111
5112 (defvar gnus-tmp-thread-tree-header-string "")
5113
5114 (defcustom gnus-sum-thread-tree-root "> "
5115   "With %B spec, used for the root of a thread.
5116 If nil, use subject instead."
5117   :version "22.1"
5118   :type '(radio (const :format "%v  " nil) string)
5119   :group 'gnus-thread)
5120
5121 (defcustom gnus-sum-thread-tree-false-root "> "
5122   "With %B spec, used for a false root of a thread.
5123 If nil, use subject instead."
5124   :version "22.1"
5125   :type '(radio (const :format "%v  " nil) string)
5126   :group 'gnus-thread)
5127
5128 (defcustom gnus-sum-thread-tree-single-indent ""
5129   "With %B spec, used for a thread with just one message.
5130 If nil, use subject instead."
5131   :version "22.1"
5132   :type '(radio (const :format "%v  " nil) string)
5133   :group 'gnus-thread)
5134
5135 (defcustom gnus-sum-thread-tree-vertical "| "
5136   "With %B spec, used for drawing a vertical line."
5137   :version "22.1"
5138   :type 'string
5139   :group 'gnus-thread)
5140
5141 (defcustom gnus-sum-thread-tree-indent "  "
5142   "With %B spec, used for indenting."
5143   :version "22.1"
5144   :type 'string
5145   :group 'gnus-thread)
5146
5147 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5148   "With %B spec, used for a leaf with brothers."
5149   :version "22.1"
5150   :type 'string
5151   :group 'gnus-thread)
5152
5153 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5154   "With %B spec, used for a leaf without brothers."
5155   :version "22.1"
5156   :type 'string
5157   :group 'gnus-thread)
5158
5159 (defcustom gnus-summary-display-while-building nil
5160   "If non-nil, show and update the summary buffer as it's being built.
5161 If the value is t, update the buffer after every line is inserted.  If
5162 the value is an integer (N), update the display every N lines."
5163   :version "22.1"
5164   :group 'gnus-thread
5165   :type '(choice (const :tag "off" nil)
5166                  number
5167                  (const :tag "frequently" t)))
5168
5169 (defun gnus-summary-prepare-threads (threads)
5170   "Prepare summary buffer from THREADS and indentation LEVEL.
5171 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5172 or a straight list of headers."
5173   (gnus-message 7 "Generating summary...")
5174
5175   (setq gnus-newsgroup-threads threads)
5176   (beginning-of-line)
5177
5178   (let ((gnus-tmp-level 0)
5179         (default-score (or gnus-summary-default-score 0))
5180         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5181         (building-line-count gnus-summary-display-while-building)
5182         (building-count (integerp gnus-summary-display-while-building))
5183         thread number subject stack state gnus-tmp-gathered beg-match
5184         new-roots gnus-tmp-new-adopts thread-end simp-subject
5185         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5186         gnus-tmp-replied gnus-tmp-subject-or-nil
5187         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5188         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5189         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5190         tree-stack)
5191
5192     (setq gnus-tmp-prev-subject nil
5193           gnus-tmp-thread-tree-header-string "")
5194
5195     (if (vectorp (car threads))
5196         ;; If this is a straight (sic) list of headers, then a
5197         ;; threaded summary display isn't required, so we just create
5198         ;; an unthreaded one.
5199         (gnus-summary-prepare-unthreaded threads)
5200
5201       ;; Do the threaded display.
5202
5203       (if gnus-summary-display-while-building
5204           (switch-to-buffer (buffer-name)))
5205       (while (or threads stack gnus-tmp-new-adopts new-roots)
5206
5207         (if (and (= gnus-tmp-level 0)
5208                  (or (not stack)
5209                      (= (caar stack) 0))
5210                  (not gnus-tmp-false-parent)
5211                  (or gnus-tmp-new-adopts new-roots))
5212             (if gnus-tmp-new-adopts
5213                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5214                       thread (list (car gnus-tmp-new-adopts))
5215                       gnus-tmp-header (caar thread)
5216                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5217               (when new-roots
5218                 (setq thread (list (car new-roots))
5219                       gnus-tmp-header (caar thread)
5220                       new-roots (cdr new-roots))))
5221
5222           (if threads
5223               ;; If there are some threads, we do them before the
5224               ;; threads on the stack.
5225               (setq thread threads
5226                     gnus-tmp-header (caar thread))
5227             ;; There were no current threads, so we pop something off
5228             ;; the stack.
5229             (setq state (car stack)
5230                   gnus-tmp-level (car state)
5231                   tree-stack (cadr state)
5232                   thread (caddr state)
5233                   stack (cdr stack)
5234                   gnus-tmp-header (caar thread))))
5235
5236         (setq gnus-tmp-false-parent nil)
5237         (setq gnus-tmp-root-expunged nil)
5238         (setq thread-end nil)
5239
5240         (if (stringp gnus-tmp-header)
5241             ;; The header is a dummy root.
5242             (cond
5243              ((eq gnus-summary-make-false-root 'adopt)
5244               ;; We let the first article adopt the rest.
5245               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5246                                                (cddar thread)))
5247               (setq gnus-tmp-gathered
5248                     (nconc (mapcar
5249                             (lambda (h) (mail-header-number (car h)))
5250                             (cddar thread))
5251                            gnus-tmp-gathered))
5252               (setq thread (cons (list (caar thread)
5253                                        (cadar thread))
5254                                  (cdr thread)))
5255               (setq gnus-tmp-level -1
5256                     gnus-tmp-false-parent t))
5257              ((eq gnus-summary-make-false-root 'empty)
5258               ;; We print adopted articles with empty subject fields.
5259               (setq gnus-tmp-gathered
5260                     (nconc (mapcar
5261                             (lambda (h) (mail-header-number (car h)))
5262                             (cddar thread))
5263                            gnus-tmp-gathered))
5264               (setq gnus-tmp-level -1))
5265              ((eq gnus-summary-make-false-root 'dummy)
5266               ;; We remember that we probably want to output a dummy
5267               ;; root.
5268               (setq gnus-tmp-dummy-line gnus-tmp-header)
5269               (setq gnus-tmp-prev-subject gnus-tmp-header))
5270              (t
5271               ;; We do not make a root for the gathered
5272               ;; sub-threads at all.
5273               (setq gnus-tmp-level -1)))
5274
5275           (setq number (mail-header-number gnus-tmp-header)
5276                 subject (mail-header-subject gnus-tmp-header)
5277                 simp-subject (gnus-simplify-subject-fully subject))
5278
5279           (cond
5280            ;; If the thread has changed subject, we might want to make
5281            ;; this subthread into a root.
5282            ((and (null gnus-thread-ignore-subject)
5283                  (not (zerop gnus-tmp-level))
5284                  gnus-tmp-prev-subject
5285                  (not (string= gnus-tmp-prev-subject simp-subject)))
5286             (setq new-roots (nconc new-roots (list (car thread)))
5287                   thread-end t
5288                   gnus-tmp-header nil))
5289            ;; If the article lies outside the current limit,
5290            ;; then we do not display it.
5291            ((not (memq number gnus-newsgroup-limit))
5292             (setq gnus-tmp-gathered
5293                   (nconc (mapcar
5294                           (lambda (h) (mail-header-number (car h)))
5295                           (cdar thread))
5296                          gnus-tmp-gathered))
5297             (setq gnus-tmp-new-adopts (if (cdar thread)
5298                                           (append gnus-tmp-new-adopts
5299                                                   (cdar thread))
5300                                         gnus-tmp-new-adopts)
5301                   thread-end t
5302                   gnus-tmp-header nil)
5303             (when (zerop gnus-tmp-level)
5304               (setq gnus-tmp-root-expunged t)))
5305            ;; Perhaps this article is to be marked as read?
5306            ((and gnus-summary-mark-below
5307                  (< (or (cdr (assq number gnus-newsgroup-scored))
5308                         default-score)
5309                     gnus-summary-mark-below)
5310                  ;; Don't touch sparse articles.
5311                  (not (gnus-summary-article-sparse-p number))
5312                  (not (gnus-summary-article-ancient-p number)))
5313             (setq gnus-newsgroup-unreads
5314                   (delq number gnus-newsgroup-unreads))
5315             (if gnus-newsgroup-auto-expire
5316                 (setq gnus-newsgroup-expirable
5317                       (gnus-add-to-sorted-list
5318                        gnus-newsgroup-expirable number))
5319               (push (cons number gnus-low-score-mark)
5320                     gnus-newsgroup-reads))))
5321
5322           (when gnus-tmp-header
5323             ;; We may have an old dummy line to output before this
5324             ;; article.
5325             (when (and gnus-tmp-dummy-line
5326                        (gnus-subject-equal
5327                         gnus-tmp-dummy-line
5328                         (mail-header-subject gnus-tmp-header)))
5329               (gnus-summary-insert-dummy-line
5330                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5331               (setq gnus-tmp-dummy-line nil))
5332
5333             ;; Compute the mark.
5334             (setq gnus-tmp-unread (gnus-article-mark number))
5335
5336             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5337                                   gnus-tmp-header gnus-tmp-level)
5338                   gnus-newsgroup-data)
5339
5340             ;; Actually insert the line.
5341             (setq
5342              gnus-tmp-subject-or-nil
5343              (cond
5344               ((and gnus-thread-ignore-subject
5345                     gnus-tmp-prev-subject
5346                     (not (string= gnus-tmp-prev-subject simp-subject)))
5347                subject)
5348               ((zerop gnus-tmp-level)
5349                (if (and (eq gnus-summary-make-false-root 'empty)
5350                         (memq number gnus-tmp-gathered)
5351                         gnus-tmp-prev-subject
5352                         (string= gnus-tmp-prev-subject simp-subject))
5353                    gnus-summary-same-subject
5354                  subject))
5355               (t gnus-summary-same-subject)))
5356             (if (and (eq gnus-summary-make-false-root 'adopt)
5357                      (= gnus-tmp-level 1)
5358                      (memq number gnus-tmp-gathered))
5359                 (setq gnus-tmp-opening-bracket ?\<
5360                       gnus-tmp-closing-bracket ?\>)
5361               (setq gnus-tmp-opening-bracket ?\[
5362                     gnus-tmp-closing-bracket ?\]))
5363             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5364                 (gnus-make-thread-indent-array
5365                  (max (* 2 (length gnus-thread-indent-array))
5366                       gnus-tmp-level)))
5367             (setq
5368              gnus-tmp-indentation
5369              (aref gnus-thread-indent-array gnus-tmp-level)
5370              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5371              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5372                                 gnus-summary-default-score 0)
5373              gnus-tmp-score-char
5374              (if (or (null gnus-summary-default-score)
5375                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5376                          gnus-summary-zcore-fuzz))
5377                  ?                      ;Whitespace
5378                (if (< gnus-tmp-score gnus-summary-default-score)
5379                    gnus-score-below-mark gnus-score-over-mark))
5380              gnus-tmp-replied
5381              (cond ((memq number gnus-newsgroup-processable)
5382                     gnus-process-mark)
5383                    ((memq number gnus-newsgroup-cached)
5384                     gnus-cached-mark)
5385                    ((memq number gnus-newsgroup-replied)
5386                     gnus-replied-mark)
5387                    ((memq number gnus-newsgroup-forwarded)
5388                     gnus-forwarded-mark)
5389                    ((memq number gnus-newsgroup-saved)
5390                     gnus-saved-mark)
5391                    ((memq number gnus-newsgroup-recent)
5392                     gnus-recent-mark)
5393                    ((memq number gnus-newsgroup-unseen)
5394                     gnus-unseen-mark)
5395                    (t gnus-no-mark))
5396              gnus-tmp-downloaded
5397              (cond ((memq number gnus-newsgroup-undownloaded)
5398                     gnus-undownloaded-mark)
5399                    (gnus-newsgroup-agentized
5400                     gnus-downloaded-mark)
5401                    (t
5402                     gnus-no-mark))
5403              gnus-tmp-from (mail-header-from gnus-tmp-header)
5404              gnus-tmp-name
5405              (cond
5406               ((string-match "<[^>]+> *$" gnus-tmp-from)
5407                (setq beg-match (match-beginning 0))
5408                (or (and (string-match "^\".+\"" gnus-tmp-from)
5409                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5410                    (substring gnus-tmp-from 0 beg-match)))
5411               ((string-match "(.+)" gnus-tmp-from)
5412                (substring gnus-tmp-from
5413                           (1+ (match-beginning 0)) (1- (match-end 0))))
5414               (t gnus-tmp-from))
5415
5416              ;; Do the %B string
5417              gnus-tmp-thread-tree-header-string
5418              (cond
5419               ((not gnus-show-threads) "")
5420               ((zerop gnus-tmp-level)
5421                (cond ((cdar thread)
5422                       (or gnus-sum-thread-tree-root subject))
5423                      (gnus-tmp-new-adopts
5424                       (or gnus-sum-thread-tree-false-root subject))
5425                      (t
5426                       (or gnus-sum-thread-tree-single-indent subject))))
5427               (t
5428                (concat (apply 'concat
5429                               (mapcar (lambda (item)
5430                                         (if (= item 1)
5431                                             gnus-sum-thread-tree-vertical
5432                                           gnus-sum-thread-tree-indent))
5433                                       (cdr (reverse tree-stack))))
5434                        (if (nth 1 thread)
5435                            gnus-sum-thread-tree-leaf-with-other
5436                          gnus-sum-thread-tree-single-leaf)))))
5437             (when (string= gnus-tmp-name "")
5438               (setq gnus-tmp-name gnus-tmp-from))
5439             (unless (numberp gnus-tmp-lines)
5440               (setq gnus-tmp-lines -1))
5441             (if (= gnus-tmp-lines -1)
5442                 (setq gnus-tmp-lines "?")
5443               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5444             (gnus-put-text-property
5445              (point)
5446              (progn (eval gnus-summary-line-format-spec) (point))
5447              'gnus-number number)
5448             (when gnus-visual-p
5449               (forward-line -1)
5450               (gnus-summary-highlight-line)
5451               (when gnus-summary-update-hook
5452                 (gnus-run-hooks 'gnus-summary-update-hook))
5453               (forward-line 1))
5454
5455             (setq gnus-tmp-prev-subject simp-subject)))
5456
5457         (when (nth 1 thread)
5458           (push (list (max 0 gnus-tmp-level)
5459                       (copy-sequence tree-stack)
5460                       (nthcdr 1 thread))
5461                 stack))
5462         (push (if (nth 1 thread) 1 0) tree-stack)
5463         (incf gnus-tmp-level)
5464         (setq threads (if thread-end nil (cdar thread)))
5465         (if gnus-summary-display-while-building
5466             (if building-count
5467                 (progn
5468                   ;; use a set frequency
5469                   (setq building-line-count (1- building-line-count))
5470                   (when (= building-line-count 0)
5471                     (sit-for 0)
5472                     (setq building-line-count
5473                           gnus-summary-display-while-building)))
5474               ;; always
5475               (sit-for 0)))
5476         (unless threads
5477           (setq gnus-tmp-level 0)))))
5478   (gnus-message 7 "Generating summary...done"))
5479
5480 (defun gnus-summary-prepare-unthreaded (headers)
5481   "Generate an unthreaded summary buffer based on HEADERS."
5482   (let (header number mark)
5483
5484     (beginning-of-line)
5485
5486     (while headers
5487       ;; We may have to root out some bad articles...
5488       (when (memq (setq number (mail-header-number
5489                                 (setq header (pop headers))))
5490                   gnus-newsgroup-limit)
5491         ;; Mark article as read when it has a low score.
5492         (when (and gnus-summary-mark-below
5493                    (< (or (cdr (assq number gnus-newsgroup-scored))
5494                           gnus-summary-default-score 0)
5495                       gnus-summary-mark-below)
5496                    (not (gnus-summary-article-ancient-p number)))
5497           (setq gnus-newsgroup-unreads
5498                 (delq number gnus-newsgroup-unreads))
5499           (if gnus-newsgroup-auto-expire
5500               (push number gnus-newsgroup-expirable)
5501             (push (cons number gnus-low-score-mark)
5502                   gnus-newsgroup-reads)))
5503
5504         (setq mark (gnus-article-mark number))
5505         (push (gnus-data-make number mark (1+ (point)) header 0)
5506               gnus-newsgroup-data)
5507         (gnus-summary-insert-line
5508          header 0 number
5509          (memq number gnus-newsgroup-undownloaded)
5510          mark (memq number gnus-newsgroup-replied)
5511          (memq number gnus-newsgroup-expirable)
5512          (mail-header-subject header) nil
5513          (cdr (assq number gnus-newsgroup-scored))
5514          (memq number gnus-newsgroup-processable))))))
5515
5516 (defun gnus-summary-remove-list-identifiers ()
5517   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5518   (let ((regexp (if (consp gnus-list-identifiers)
5519                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5520                   gnus-list-identifiers))
5521         changed subject)
5522     (when regexp
5523       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5524       (dolist (header gnus-newsgroup-headers)
5525         (setq subject (mail-header-subject header)
5526               changed nil)
5527         (while (string-match regexp subject)
5528           (setq subject
5529                 (concat (substring subject 0 (match-beginning 1))
5530                         (substring subject (match-end 0)))
5531                 changed t))
5532         (when changed
5533           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5534             (setq subject
5535                   (concat (substring subject 0 (match-beginning 1))
5536                           (substring subject (match-end 1)))))
5537           (mail-header-set-subject header subject))))))
5538
5539 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5540   "Fetch headers of ARTICLES."
5541   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5542     (gnus-message 5 "Fetching headers for %s..." name)
5543     (prog1
5544         (if (eq 'nov
5545                 (setq gnus-headers-retrieved-by
5546                       (gnus-retrieve-headers
5547                        articles gnus-newsgroup-name
5548                        (or limit
5549                            ;; We might want to fetch old headers, but
5550                            ;; not if there is only 1 article.
5551                            (and (or (and
5552                                      (not (eq gnus-fetch-old-headers 'some))
5553                                      (not (numberp gnus-fetch-old-headers)))
5554                                     (> (length articles) 1))
5555                                 gnus-fetch-old-headers)))))
5556             (gnus-get-newsgroup-headers-xover
5557              articles force-new dependencies gnus-newsgroup-name t)
5558           (gnus-get-newsgroup-headers dependencies force-new))
5559       (gnus-message 5 "Fetching headers for %s...done" name))))
5560
5561 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5562   "Select newsgroup GROUP.
5563 If READ-ALL is non-nil, all articles in the group are selected.
5564 If SELECT-ARTICLES, only select those articles from GROUP."
5565   (let* ((entry (gnus-group-entry group))
5566          ;;!!! Dirty hack; should be removed.
5567          (gnus-summary-ignore-duplicates
5568           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5569               t
5570             gnus-summary-ignore-duplicates))
5571          (info (nth 2 entry))
5572          charset articles fetched-articles cached)
5573
5574     (unless (gnus-check-server
5575              (set (make-local-variable 'gnus-current-select-method)
5576                   (gnus-find-method-for-group group)))
5577       (error "Couldn't open server"))
5578     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5579
5580     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5581         (gnus-activate-group group)     ; Or we can activate it...
5582         (progn                          ; Or we bug out.
5583           (when (equal major-mode 'gnus-summary-mode)
5584             (gnus-kill-buffer (current-buffer)))
5585           (error
5586            "Couldn't activate group %s: %s"
5587            (mm-decode-coding-string group charset)
5588            (mm-decode-coding-string (gnus-status-message group) charset))))
5589
5590     (unless (gnus-request-group group t)
5591       (when (equal major-mode 'gnus-summary-mode)
5592         (gnus-kill-buffer (current-buffer)))
5593       (error "Couldn't request group %s: %s"
5594              (mm-decode-coding-string group charset)
5595              (mm-decode-coding-string (gnus-status-message group) charset)))
5596
5597     (when (and gnus-agent
5598                (gnus-active group))
5599       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5600
5601       (setq gnus-summary-use-undownloaded-faces
5602             (gnus-agent-find-parameter
5603              group
5604              'agent-enable-undownloaded-faces)))
5605
5606     (setq gnus-newsgroup-name group
5607           gnus-newsgroup-unselected nil
5608           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5609
5610     (let ((display (gnus-group-find-parameter group 'display)))
5611       (setq gnus-newsgroup-display
5612             (cond
5613              ((not (zerop (or (car-safe read-all) 0)))
5614               ;; The user entered the group with C-u SPC/RET, let's show
5615               ;; all articles.
5616               'gnus-not-ignore)
5617              ((eq display 'all)
5618               'gnus-not-ignore)
5619              ((arrayp display)
5620               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5621              ((numberp display)
5622               ;; The following is probably the "correct" solution, but
5623               ;; it makes Gnus fetch all headers and then limit the
5624               ;; articles (which is slow), so instead we hack the
5625               ;; select-articles parameter instead. -- Simon Josefsson
5626               ;; <jas@kth.se>
5627               ;;
5628               ;; (gnus-byte-compile
5629               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5630               ;;                         display)))))
5631               (setq select-articles
5632                     (gnus-uncompress-range
5633                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5634                              (if (> tmp 0)
5635                                  tmp
5636                                1))
5637                            (cdr (gnus-active group)))))
5638               nil)
5639              (t
5640               nil))))
5641
5642     (gnus-summary-setup-default-charset)
5643
5644     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5645     (when (gnus-virtual-group-p group)
5646       (setq cached gnus-newsgroup-cached))
5647
5648     (setq gnus-newsgroup-unreads
5649           (gnus-sorted-ndifference
5650            (gnus-sorted-ndifference gnus-newsgroup-unreads
5651                                     gnus-newsgroup-marked)
5652            gnus-newsgroup-dormant))
5653
5654     (setq gnus-newsgroup-processable nil)
5655
5656     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5657
5658     ;; Adjust and set lists of article marks.
5659     (when info
5660       (gnus-adjust-marked-articles info))
5661     (if (setq articles select-articles)
5662         (setq gnus-newsgroup-unselected
5663               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5664       (setq articles (gnus-articles-to-read group read-all)))
5665
5666     (cond
5667      ((null articles)
5668       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5669       'quit)
5670      ((eq articles 0) nil)
5671      (t
5672       ;; Init the dependencies hash table.
5673       (setq gnus-newsgroup-dependencies
5674             (gnus-make-hashtable (length articles)))
5675       (gnus-set-global-variables)
5676       ;; Retrieve the headers and read them in.
5677
5678       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5679
5680       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5681       (when cached
5682         (setq gnus-newsgroup-cached cached))
5683
5684       ;; Suppress duplicates?
5685       (when gnus-suppress-duplicates
5686         (gnus-dup-suppress-articles))
5687
5688       ;; Set the initial limit.
5689       (setq gnus-newsgroup-limit (copy-sequence articles))
5690       ;; Remove canceled articles from the list of unread articles.
5691       (setq fetched-articles
5692             (mapcar (lambda (headers) (mail-header-number headers))
5693                     gnus-newsgroup-headers))
5694       (setq gnus-newsgroup-articles fetched-articles)
5695       (setq gnus-newsgroup-unreads
5696             (gnus-sorted-nintersection
5697              gnus-newsgroup-unreads fetched-articles))
5698       (gnus-compute-unseen-list)
5699
5700       ;; Removed marked articles that do not exist.
5701       (gnus-update-missing-marks
5702        (gnus-sorted-difference articles fetched-articles))
5703       ;; We might want to build some more threads first.
5704       (when (and gnus-fetch-old-headers
5705                  (eq gnus-headers-retrieved-by 'nov))
5706         (if (eq gnus-fetch-old-headers 'invisible)
5707             (gnus-build-all-threads)
5708           (gnus-build-old-threads)))
5709       ;; Let the Gnus agent mark articles as read.
5710       (when gnus-agent
5711         (gnus-agent-get-undownloaded-list))
5712       ;; Remove list identifiers from subject
5713       (when gnus-list-identifiers
5714         (gnus-summary-remove-list-identifiers))
5715       ;; Check whether auto-expire is to be done in this group.
5716       (setq gnus-newsgroup-auto-expire
5717             (gnus-group-auto-expirable-p group))
5718       ;; Set up the article buffer now, if necessary.
5719       (unless (and gnus-single-article-buffer
5720                    (equal gnus-article-buffer "*Article*"))
5721         (gnus-article-setup-buffer))
5722       ;; First and last article in this newsgroup.
5723       (when gnus-newsgroup-headers
5724         (setq gnus-newsgroup-begin
5725               (mail-header-number (car gnus-newsgroup-headers))
5726               gnus-newsgroup-end
5727               (mail-header-number
5728                (gnus-last-element gnus-newsgroup-headers))))
5729       ;; GROUP is successfully selected.
5730       (or gnus-newsgroup-headers t)))))
5731
5732 (defun gnus-compute-unseen-list ()
5733   ;; The `seen' marks are treated specially.
5734   (if (not gnus-newsgroup-seen)
5735       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5736     (setq gnus-newsgroup-unseen
5737           (gnus-inverse-list-range-intersection
5738            gnus-newsgroup-articles gnus-newsgroup-seen))))
5739
5740 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5741
5742 (defun gnus-summary-display-make-predicate (display)
5743   (require 'gnus-agent)
5744   (when (= (length display) 1)
5745     (setq display (car display)))
5746   (unless gnus-summary-display-cache
5747     (dolist (elem (append '((unread . unread)
5748                             (read . read)
5749                             (unseen . unseen))
5750                           gnus-article-mark-lists))
5751       (push (cons (cdr elem)
5752                   (gnus-byte-compile    ;Why bother?
5753                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5754             gnus-summary-display-cache)))
5755   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5756         (gnus-category-predicate-cache gnus-summary-display-cache))
5757     (gnus-get-predicate display)))
5758
5759 ;; Uses the dynamically bound `gnus-number' variable.
5760 (defvar gnus-number)
5761 (defun gnus-article-marked-p (type &optional article)
5762   (let ((article (or article gnus-number)))
5763     (cond
5764      ((eq type 'tick)
5765       (memq article gnus-newsgroup-marked))
5766      ((eq type 'spam)
5767       (memq article gnus-newsgroup-spam-marked))
5768      ((eq type 'unsend)
5769       (memq article gnus-newsgroup-unsendable))
5770      ((eq type 'undownload)
5771       (memq article gnus-newsgroup-undownloaded))
5772      ((eq type 'download)
5773       (memq article gnus-newsgroup-downloadable))
5774      ((eq type 'unread)
5775       (memq article gnus-newsgroup-unreads))
5776      ((eq type 'read)
5777       (memq article gnus-newsgroup-reads))
5778      ((eq type 'dormant)
5779       (memq article gnus-newsgroup-dormant) )
5780      ((eq type 'expire)
5781       (memq article gnus-newsgroup-expirable))
5782      ((eq type 'reply)
5783       (memq article gnus-newsgroup-replied))
5784      ((eq type 'killed)
5785       (memq article gnus-newsgroup-killed))
5786      ((eq type 'bookmark)
5787       (assq article gnus-newsgroup-bookmarks))
5788      ((eq type 'score)
5789       (assq article gnus-newsgroup-scored))
5790      ((eq type 'save)
5791       (memq article gnus-newsgroup-saved))
5792      ((eq type 'cache)
5793       (memq article gnus-newsgroup-cached))
5794      ((eq type 'forward)
5795       (memq article gnus-newsgroup-forwarded))
5796      ((eq type 'seen)
5797       (not (memq article gnus-newsgroup-unseen)))
5798      ((eq type 'recent)
5799       (memq article gnus-newsgroup-recent))
5800      (t t))))
5801
5802 (defun gnus-articles-to-read (group &optional read-all)
5803   "Find out what articles the user wants to read."
5804   (let* ((articles
5805           ;; Select all articles if `read-all' is non-nil, or if there
5806           ;; are no unread articles.
5807           (if (or read-all
5808                   (and (zerop (length gnus-newsgroup-marked))
5809                        (zerop (length gnus-newsgroup-unreads)))
5810                   ;; Fetch all if the predicate is non-nil.
5811                   gnus-newsgroup-display)
5812               ;; We want to select the headers for all the articles in
5813               ;; the group, so we select either all the active
5814               ;; articles in the group, or (if that's nil), the
5815               ;; articles in the cache.
5816               (or
5817                (if gnus-newsgroup-maximum-articles
5818                    (let ((active (gnus-active group)))
5819                      (gnus-uncompress-range
5820                       (cons (max (car active)
5821                                  (- (cdr active)
5822                                     gnus-newsgroup-maximum-articles
5823                                     -1))
5824                             (cdr active))))
5825                  (gnus-uncompress-range (gnus-active group)))
5826                (gnus-cache-articles-in-group group))
5827             ;; Select only the "normal" subset of articles.
5828             (gnus-sorted-nunion
5829              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5830              gnus-newsgroup-unreads)))
5831          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5832          (scored (length scored-list))
5833          (number (length articles))
5834          (marked (+ (length gnus-newsgroup-marked)
5835                     (length gnus-newsgroup-dormant)))
5836          (select
5837           (cond
5838            ((numberp read-all)
5839             read-all)
5840            ((numberp gnus-newsgroup-display)
5841             gnus-newsgroup-display)
5842            (t
5843             (condition-case ()
5844                 (cond
5845                  ((and (or (<= scored marked) (= scored number))
5846                        (numberp gnus-large-newsgroup)
5847                        (> number gnus-large-newsgroup))
5848                   (let* ((cursor-in-echo-area nil)
5849                          (initial (gnus-parameter-large-newsgroup-initial
5850                                    gnus-newsgroup-name))
5851                          (input
5852                           (read-string
5853                            (format
5854                             "How many articles from %s (%s %d): "
5855                             (gnus-group-decoded-name gnus-newsgroup-name)
5856                             (if initial "max" "default")
5857                             number)
5858                            (if initial
5859                                (cons (number-to-string initial)
5860                                      0)))))
5861                     (if (string-match "^[ \t]*$" input) number input)))
5862                  ((and (> scored marked) (< scored number)
5863                        (> (- scored number) 20))
5864                   (let ((input
5865                          (read-string
5866                           (format "%s %s (%d scored, %d total): "
5867                                   "How many articles from"
5868                                   (gnus-group-decoded-name group)
5869                                   scored number))))
5870                     (if (string-match "^[ \t]*$" input)
5871                         number input)))
5872                  (t number))
5873               (quit
5874                (message "Quit getting the articles to read")
5875                nil))))))
5876     (setq select (if (stringp select) (string-to-number select) select))
5877     (if (or (null select) (zerop select))
5878         select
5879       (if (and (not (zerop scored)) (<= (abs select) scored))
5880           (progn
5881             (setq articles (sort scored-list '<))
5882             (setq number (length articles)))
5883         (setq articles (copy-sequence articles)))
5884
5885       (when (< (abs select) number)
5886         (if (< select 0)
5887             ;; Select the N oldest articles.
5888             (setcdr (nthcdr (1- (abs select)) articles) nil)
5889           ;; Select the N most recent articles.
5890           (setq articles (nthcdr (- number select) articles))))
5891       (setq gnus-newsgroup-unselected
5892             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5893       (when gnus-alter-articles-to-read-function
5894         (setq articles
5895               (sort
5896                (funcall gnus-alter-articles-to-read-function
5897                         gnus-newsgroup-name articles)
5898                '<)))
5899       articles)))
5900
5901 (defun gnus-killed-articles (killed articles)
5902   (let (out)
5903     (while articles
5904       (when (inline (gnus-member-of-range (car articles) killed))
5905         (push (car articles) out))
5906       (setq articles (cdr articles)))
5907     out))
5908
5909 (defun gnus-uncompress-marks (marks)
5910   "Uncompress the mark ranges in MARKS."
5911   (let ((uncompressed '(score bookmark))
5912         out)
5913     (while marks
5914       (if (memq (caar marks) uncompressed)
5915           (push (car marks) out)
5916         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5917       (setq marks (cdr marks)))
5918     out))
5919
5920 (defun gnus-article-mark-to-type (mark)
5921   "Return the type of MARK."
5922   (or (cadr (assq mark gnus-article-special-mark-lists))
5923       'list))
5924
5925 (defun gnus-article-unpropagatable-p (mark)
5926   "Return whether MARK should be propagated to back end."
5927   (memq mark gnus-article-unpropagated-mark-lists))
5928
5929 (defun gnus-adjust-marked-articles (info)
5930   "Set all article lists and remove all marks that are no longer valid."
5931   (let* ((marked-lists (gnus-info-marks info))
5932          (active (gnus-active (gnus-info-group info)))
5933          (min (car active))
5934          (max (cdr active))
5935          (types gnus-article-mark-lists)
5936          marks var articles article mark mark-type
5937          bgn end)
5938     ;; Hack to avoid adjusting marks for imap.
5939     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5940               'nnimap)
5941       (setq min 1))
5942
5943     (dolist (marks marked-lists)
5944       (setq mark (car marks)
5945             mark-type (gnus-article-mark-to-type mark)
5946             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5947
5948       ;; We set the variable according to the type of the marks list,
5949       ;; and then adjust the marks to a subset of the active articles.
5950       (cond
5951        ;; Adjust "simple" lists - compressed yet unsorted
5952        ((eq mark-type 'list)
5953         ;; Simultaneously uncompress and clip to active range
5954         ;; See gnus-uncompress-range for a description of possible marks
5955         (let (l lh)
5956           (if (not (cadr marks))
5957               (set var nil)
5958             (setq articles (if (numberp (cddr marks))
5959                                (list (cdr marks))
5960                              (cdr marks))
5961                   lh (cons nil nil)
5962                   l lh)
5963
5964             (while (setq article (pop articles))
5965               (cond ((consp article)
5966                      (setq bgn (max (car article) min)
5967                            end (min (cdr article) max))
5968                      (while (<= bgn end)
5969                        (setq l (setcdr l (cons bgn nil))
5970                              bgn (1+ bgn))))
5971                     ((and (<= min article)
5972                           (>= max article))
5973                      (setq l (setcdr l (cons article nil))))))
5974             (set var (cdr lh)))))
5975        ;; Adjust assocs.
5976        ((eq mark-type 'tuple)
5977         (set var (setq articles (cdr marks)))
5978         (when (not (listp (cdr (symbol-value var))))
5979           (set var (list (symbol-value var))))
5980         (when (not (listp (cdr articles)))
5981           (setq articles (list articles)))
5982         (while articles
5983           (when (or (not (consp (setq article (pop articles))))
5984                     (< (car article) min)
5985                     (> (car article) max))
5986             (set var (delq article (symbol-value var))))))
5987        ;; Adjust ranges (sloppily).
5988        ((eq mark-type 'range)
5989         (cond
5990          ((eq mark 'seen)
5991           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5992           ;; It should be (seen (NUM1 . NUM2)).
5993           (when (numberp (cddr marks))
5994             (setcdr marks (list (cdr marks))))
5995           (setq articles (cdr marks))
5996           (while (and articles
5997                       (or (and (consp (car articles))
5998                                (> min (cdar articles)))
5999                           (and (numberp (car articles))
6000                                (> min (car articles)))))
6001             (pop articles))
6002           (set var articles))))))))
6003
6004 (defun gnus-update-missing-marks (missing)
6005   "Go through the list of MISSING articles and remove them from the mark lists."
6006   (when missing
6007     (let (var m)
6008       ;; Go through all types.
6009       (dolist (elem gnus-article-mark-lists)
6010         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6011           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6012           (when (symbol-value var)
6013             ;; This list has articles.  So we delete all missing
6014             ;; articles from it.
6015             (setq m missing)
6016             (while m
6017               (set var (delq (pop m) (symbol-value var))))))))))
6018
6019 (defun gnus-update-marks ()
6020   "Enter the various lists of marked articles into the newsgroup info list."
6021   (let ((types gnus-article-mark-lists)
6022         (info (gnus-get-info gnus-newsgroup-name))
6023         type list newmarked symbol delta-marks)
6024     (when info
6025       ;; Add all marks lists to the list of marks lists.
6026       (while (setq type (pop types))
6027         (setq list (symbol-value
6028                     (setq symbol
6029                           (intern (format "gnus-newsgroup-%s" (car type))))))
6030
6031         (when list
6032           ;; Get rid of the entries of the articles that have the
6033           ;; default score.
6034           (when (and (eq (cdr type) 'score)
6035                      gnus-save-score
6036                      list)
6037             (let* ((arts list)
6038                    (prev (cons nil list))
6039                    (all prev))
6040               (while arts
6041                 (if (or (not (consp (car arts)))
6042                         (= (cdar arts) gnus-summary-default-score))
6043                     (setcdr prev (cdr arts))
6044                   (setq prev arts))
6045                 (setq arts (cdr arts)))
6046               (setq list (cdr all)))))
6047
6048         (when (eq (cdr type) 'seen)
6049           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6050
6051         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6052           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6053
6054         (when (and (gnus-check-backend-function
6055                     'request-set-mark gnus-newsgroup-name)
6056                    (not (gnus-article-unpropagatable-p (cdr type))))
6057           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6058                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
6059                  (add (gnus-remove-from-range
6060                        (gnus-copy-sequence list) old)))
6061             (when add
6062               (push (list add 'add (list (cdr type))) delta-marks))
6063             (when del
6064               ;; Don't delete marks from outside the active range.  This
6065               ;; shouldn't happen, but is a sanity check.
6066               (setq del (gnus-sorted-range-intersection
6067                          (gnus-active gnus-newsgroup-name) del))
6068               (push (list del 'del (list (cdr type))) delta-marks))))
6069
6070         (when list
6071           (push (cons (cdr type) list) newmarked)))
6072
6073       (when delta-marks
6074         (unless (gnus-check-group gnus-newsgroup-name)
6075           (error "Can't open server for %s" gnus-newsgroup-name))
6076         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6077
6078       ;; Enter these new marks into the info of the group.
6079       (if (nthcdr 3 info)
6080           (setcar (nthcdr 3 info) newmarked)
6081         ;; Add the marks lists to the end of the info.
6082         (when newmarked
6083           (setcdr (nthcdr 2 info) (list newmarked))))
6084
6085       ;; Cut off the end of the info if there's nothing else there.
6086       (let ((i 5))
6087         (while (and (> i 2)
6088                     (not (nth i info)))
6089           (when (nthcdr (decf i) info)
6090             (setcdr (nthcdr i info) nil)))))))
6091
6092 (defun gnus-set-mode-line (where)
6093   "Set the mode line of the article or summary buffers.
6094 If WHERE is `summary', the summary mode line format will be used."
6095   ;; Is this mode line one we keep updated?
6096   (when (and (memq where gnus-updated-mode-lines)
6097              (symbol-value
6098               (intern (format "gnus-%s-mode-line-format-spec" where))))
6099     (let (mode-string)
6100       ;; We evaluate this in the summary buffer since these
6101       ;; variables are buffer-local to that buffer.
6102       (with-current-buffer gnus-summary-buffer
6103         ;; We bind all these variables that are used in the `eval' form
6104         ;; below.
6105         (let* ((mformat (symbol-value
6106                          (intern
6107                           (format "gnus-%s-mode-line-format-spec" where))))
6108                (gnus-tmp-group-name (gnus-mode-string-quote
6109                                      (gnus-group-decoded-name
6110                                       gnus-newsgroup-name)))
6111                (gnus-tmp-article-number (or gnus-current-article 0))
6112                (gnus-tmp-unread gnus-newsgroup-unreads)
6113                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6114                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6115                (gnus-tmp-unread-and-unselected
6116                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6117                             (zerop gnus-tmp-unselected))
6118                        "")
6119                       ((zerop gnus-tmp-unselected)
6120                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6121                       (t (format "{%d(+%d) more}"
6122                                  gnus-tmp-unread-and-unticked
6123                                  gnus-tmp-unselected))))
6124                (gnus-tmp-subject
6125                 (if (and gnus-current-headers
6126                          (vectorp gnus-current-headers))
6127                     (gnus-mode-string-quote
6128                      (mail-header-subject gnus-current-headers))
6129                   ""))
6130                bufname-length max-len
6131                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6132           (setq mode-string (eval mformat))
6133           (setq bufname-length (if (string-match "%b" mode-string)
6134                                    (- (length
6135                                        (buffer-name
6136                                         (if (eq where 'summary)
6137                                             nil
6138                                           (get-buffer gnus-article-buffer))))
6139                                       2)
6140                                  0))
6141           (setq max-len (max 4 (if gnus-mode-non-string-length
6142                                    (- (window-width)
6143                                       gnus-mode-non-string-length
6144                                       bufname-length)
6145                                  (length mode-string))))
6146           ;; We might have to chop a bit of the string off...
6147           (when (> (length mode-string) max-len)
6148             (setq mode-string
6149                   (concat (truncate-string-to-width mode-string (- max-len 3))
6150                           "...")))))
6151       ;; Update the mode line.
6152       (setq mode-line-buffer-identification
6153             (gnus-mode-line-buffer-identification (list mode-string)))
6154       (set-buffer-modified-p t))))
6155
6156 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6157   "Go through the HEADERS list and add all Xrefs to a hash table.
6158 The resulting hash table is returned, or nil if no Xrefs were found."
6159   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6160          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6161          (xref-hashtb (gnus-make-hashtable))
6162          start group entry number xrefs header)
6163     (while headers
6164       (setq header (pop headers))
6165       (when (and (setq xrefs (mail-header-xref header))
6166                  (not (memq (setq number (mail-header-number header))
6167                             unreads)))
6168         (setq start 0)
6169         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6170           (setq start (match-end 0))
6171           (setq group (if prefix
6172                           (concat prefix (substring xrefs (match-beginning 1)
6173                                                     (match-end 1)))
6174                         (substring xrefs (match-beginning 1) (match-end 1))))
6175           (setq number
6176                 (string-to-number (substring xrefs (match-beginning 2)
6177                                           (match-end 2))))
6178           (if (setq entry (gnus-gethash group xref-hashtb))
6179               (setcdr entry (cons number (cdr entry)))
6180             (gnus-sethash group (cons number nil) xref-hashtb)))))
6181     (and start xref-hashtb)))
6182
6183 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6184   "Look through all the headers and mark the Xrefs as read."
6185   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6186         name info xref-hashtb idlist method nth4)
6187     (with-current-buffer gnus-group-buffer
6188       (when (setq xref-hashtb
6189                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6190         (mapatoms
6191          (lambda (group)
6192            (unless (string= from-newsgroup (setq name (symbol-name group)))
6193              (setq idlist (symbol-value group))
6194              ;; Dead groups are not updated.
6195              (and (prog1
6196                       (setq info (gnus-get-info name))
6197                     (when (stringp (setq nth4 (gnus-info-method info)))
6198                       (setq nth4 (gnus-server-to-method nth4))))
6199                   ;; Only do the xrefs if the group has the same
6200                   ;; select method as the group we have just read.
6201                   (or (gnus-methods-equal-p
6202                        nth4 (gnus-find-method-for-group from-newsgroup))
6203                       virtual
6204                       (equal nth4 (setq method (gnus-find-method-for-group
6205                                                 from-newsgroup)))
6206                       (and (equal (car nth4) (car method))
6207                            (equal (nth 1 nth4) (nth 1 method))))
6208                   gnus-use-cross-reference
6209                   (or (not (eq gnus-use-cross-reference t))
6210                       virtual
6211                       ;; Only do cross-references on subscribed
6212                       ;; groups, if that is what is wanted.
6213                       (<= (gnus-info-level info) gnus-level-subscribed))
6214                   (gnus-group-make-articles-read name idlist))))
6215          xref-hashtb)))))
6216
6217 (defun gnus-compute-read-articles (group articles)
6218   (let* ((entry (gnus-group-entry group))
6219          (info (nth 2 entry))
6220          (active (gnus-active group))
6221          ninfo)
6222     (when entry
6223       ;; First peel off all invalid article numbers.
6224       (when active
6225         (let ((ids articles)
6226               id first)
6227           (while (setq id (pop ids))
6228             (when (and first (> id (cdr active)))
6229               ;; We'll end up in this situation in one particular
6230               ;; obscure situation.  If you re-scan a group and get
6231               ;; a new article that is cross-posted to a different
6232               ;; group that has not been re-scanned, you might get
6233               ;; crossposted article that has a higher number than
6234               ;; Gnus believes possible.  So we re-activate this
6235               ;; group as well.  This might mean doing the
6236               ;; crossposting thingy will *increase* the number
6237               ;; of articles in some groups.  Tsk, tsk.
6238               (setq active (or (gnus-activate-group group) active)))
6239             (when (or (> id (cdr active))
6240                       (< id (car active)))
6241               (setq articles (delq id articles))))))
6242       ;; If the read list is nil, we init it.
6243       (if (and active
6244                (null (gnus-info-read info))
6245                (> (car active) 1))
6246           (setq ninfo (cons 1 (1- (car active))))
6247         (setq ninfo (gnus-info-read info)))
6248       ;; Then we add the read articles to the range.
6249       (gnus-add-to-range
6250        ninfo (setq articles (sort articles '<))))))
6251
6252 (defun gnus-group-make-articles-read (group articles)
6253   "Update the info of GROUP to say that ARTICLES are read."
6254   (let* ((num 0)
6255          (entry (gnus-group-entry group))
6256          (info (nth 2 entry))
6257          (active (gnus-active group))
6258          range)
6259     (if (not entry)
6260         ;; Group that Gnus doesn't know exists, but still allow the
6261         ;; backend to set marks.
6262         (gnus-request-set-mark
6263          group (list (list (gnus-compress-sequence (sort articles #'<))
6264                            'add '(read))))
6265       ;; Normal, subscribed groups.
6266       (setq range (gnus-compute-read-articles group articles))
6267       (with-current-buffer gnus-group-buffer
6268         (gnus-undo-register
6269           `(progn
6270              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6271              (gnus-info-set-read ',info ',(gnus-info-read info))
6272              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6273              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6274              (gnus-group-update-group ,group t))))
6275       ;; Add the read articles to the range.
6276       (gnus-info-set-read info range)
6277       (gnus-request-set-mark group (list (list range 'add '(read))))
6278       ;; Then we have to re-compute how many unread
6279       ;; articles there are in this group.
6280       (when active
6281         (cond
6282          ((not range)
6283           (setq num (- (1+ (cdr active)) (car active))))
6284          ((not (listp (cdr range)))
6285           (setq num (- (cdr active) (- (1+ (cdr range))
6286                                        (car range)))))
6287          (t
6288           (while range
6289             (if (numberp (car range))
6290                 (setq num (1+ num))
6291               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6292             (setq range (cdr range)))
6293           (setq num (- (cdr active) num))))
6294         ;; Update the number of unread articles.
6295         (setcar entry num)
6296         ;; Update the group buffer.
6297         (unless (gnus-ephemeral-group-p group)
6298           (gnus-group-update-group group t))))))
6299
6300 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6301   (let ((cur nntp-server-buffer)
6302         (dependencies
6303          (or dependencies
6304              (with-current-buffer gnus-summary-buffer
6305                gnus-newsgroup-dependencies)))
6306         headers id end ref number
6307         (mail-parse-charset gnus-newsgroup-charset)
6308         (mail-parse-ignored-charsets
6309          (save-current-buffer (condition-case nil
6310                                   (set-buffer gnus-summary-buffer)
6311                                 (error))
6312                               gnus-newsgroup-ignored-charsets)))
6313     (with-current-buffer nntp-server-buffer
6314       ;; Translate all TAB characters into SPACE characters.
6315       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6316       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6317       (ietf-drums-unfold-fws)
6318       (gnus-run-hooks 'gnus-parse-headers-hook)
6319       (let ((case-fold-search t)
6320             in-reply-to header p lines chars)
6321         (goto-char (point-min))
6322         ;; Search to the beginning of the next header.  Error messages
6323         ;; do not begin with 2 or 3.
6324         (while (re-search-forward "^[23][0-9]+ " nil t)
6325           (setq id nil
6326                 ref nil)
6327           ;; This implementation of this function, with nine
6328           ;; search-forwards instead of the one re-search-forward and
6329           ;; a case (which basically was the old function) is actually
6330           ;; about twice as fast, even though it looks messier.  You
6331           ;; can't have everything, I guess.  Speed and elegance
6332           ;; doesn't always go hand in hand.
6333           (setq
6334            header
6335            (vector
6336             ;; Number.
6337             (prog1
6338                 (setq number (read cur))
6339               (end-of-line)
6340               (setq p (point))
6341               (narrow-to-region (point)
6342                                 (or (and (search-forward "\n.\n" nil t)
6343                                          (- (point) 2))
6344                                     (point))))
6345             ;; Subject.
6346             (progn
6347               (goto-char p)
6348               (if (search-forward "\nsubject:" nil t)
6349                   (funcall gnus-decode-encoded-word-function
6350                            (nnheader-header-value))
6351                 "(none)"))
6352             ;; From.
6353             (progn
6354               (goto-char p)
6355               (if (search-forward "\nfrom:" nil t)
6356                   (funcall gnus-decode-encoded-address-function
6357                            (nnheader-header-value))
6358                 "(nobody)"))
6359             ;; Date.
6360             (progn
6361               (goto-char p)
6362               (if (search-forward "\ndate:" nil t)
6363                   (nnheader-header-value) ""))
6364             ;; Message-ID.
6365             (progn
6366               (goto-char p)
6367               (setq id (if (re-search-forward
6368                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6369                            ;; We do it this way to make sure the Message-ID
6370                            ;; is (somewhat) syntactically valid.
6371                            (buffer-substring (match-beginning 1)
6372                                              (match-end 1))
6373                          ;; If there was no message-id, we just fake one
6374                          ;; to make subsequent routines simpler.
6375                          (nnheader-generate-fake-message-id number))))
6376             ;; References.
6377             (progn
6378               (goto-char p)
6379               (if (search-forward "\nreferences:" nil t)
6380                   (progn
6381                     (setq end (point))
6382                     (prog1
6383                         (nnheader-header-value)
6384                       (setq ref
6385                             (buffer-substring
6386                              (progn
6387                                (end-of-line)
6388                                (search-backward ">" end t)
6389                                (1+ (point)))
6390                              (progn
6391                                (search-backward "<" end t)
6392                                (point))))))
6393                 ;; Get the references from the in-reply-to header if there
6394                 ;; were no references and the in-reply-to header looks
6395                 ;; promising.
6396                 (if (and (search-forward "\nin-reply-to:" nil t)
6397                          (setq in-reply-to (nnheader-header-value))
6398                          (string-match "<[^>]+>" in-reply-to))
6399                     (let (ref2)
6400                       (setq ref (substring in-reply-to (match-beginning 0)
6401                                            (match-end 0)))
6402                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6403                         (setq ref2 (substring in-reply-to (match-beginning 0)
6404                                               (match-end 0)))
6405                         (when (> (length ref2) (length ref))
6406                           (setq ref ref2)))
6407                       ref)
6408                   (setq ref nil))))
6409             ;; Chars.
6410             (progn
6411               (goto-char p)
6412               (if (search-forward "\nchars: " nil t)
6413                   (if (numberp (setq chars (ignore-errors (read cur))))
6414                       chars -1)
6415                 -1))
6416             ;; Lines.
6417             (progn
6418               (goto-char p)
6419               (if (search-forward "\nlines: " nil t)
6420                   (if (numberp (setq lines (ignore-errors (read cur))))
6421                       lines -1)
6422                 -1))
6423             ;; Xref.
6424             (progn
6425               (goto-char p)
6426               (and (search-forward "\nxref:" nil t)
6427                    (nnheader-header-value)))
6428             ;; Extra.
6429             (when gnus-extra-headers
6430               (let ((extra gnus-extra-headers)
6431                     out)
6432                 (while extra
6433                   (goto-char p)
6434                   (when (search-forward
6435                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6436                     (push (cons (car extra) (nnheader-header-value))
6437                           out))
6438                   (pop extra))
6439                 out))))
6440           (when (equal id ref)
6441             (setq ref nil))
6442
6443           (when gnus-alter-header-function
6444             (funcall gnus-alter-header-function header)
6445             (setq id (mail-header-id header)
6446                   ref (gnus-parent-id (mail-header-references header))))
6447
6448           (when (setq header
6449                       (gnus-dependencies-add-header
6450                        header dependencies force-new))
6451             (push header headers))
6452           (goto-char (point-max))
6453           (widen))
6454         (nreverse headers)))))
6455
6456 ;; Goes through the xover lines and returns a list of vectors
6457 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6458                                                   force-new dependencies
6459                                                   group also-fetch-heads)
6460   "Parse the news overview data in the server buffer.
6461 Return a list of headers that match SEQUENCE (see
6462 `nntp-retrieve-headers')."
6463   ;; Get the Xref when the users reads the articles since most/some
6464   ;; NNTP servers do not include Xrefs when using XOVER.
6465   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6466   (let ((mail-parse-charset gnus-newsgroup-charset)
6467         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6468         (cur nntp-server-buffer)
6469         (dependencies (or dependencies gnus-newsgroup-dependencies))
6470         (allp (cond
6471                ((eq gnus-read-all-available-headers t)
6472                 t)
6473                ((and (stringp gnus-read-all-available-headers)
6474                      group)
6475                 (string-match gnus-read-all-available-headers group))
6476                (t
6477                 nil)))
6478         number headers header)
6479     (with-current-buffer nntp-server-buffer
6480       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6481       ;; Allow the user to mangle the headers before parsing them.
6482       (gnus-run-hooks 'gnus-parse-headers-hook)
6483       (goto-char (point-min))
6484       (gnus-parse-without-error
6485         (while (and (or sequence allp)
6486                     (not (eobp)))
6487           (setq number (read cur))
6488           (when (not allp)
6489             (while (and sequence
6490                         (< (car sequence) number))
6491               (setq sequence (cdr sequence))))
6492           (when (and (or allp
6493                          (and sequence
6494                               (eq number (car sequence))))
6495                      (progn
6496                        (setq sequence (cdr sequence))
6497                        (setq header (inline
6498                                       (gnus-nov-parse-line
6499                                        number dependencies force-new)))))
6500             (push header headers))
6501           (forward-line 1)))
6502       ;; A common bug in inn is that if you have posted an article and
6503       ;; then retrieves the active file, it will answer correctly --
6504       ;; the new article is included.  However, a NOV entry for the
6505       ;; article may not have been generated yet, so this may fail.
6506       ;; We work around this problem by retrieving the last few
6507       ;; headers using HEAD.
6508       (if (or (not also-fetch-heads)
6509               (not sequence))
6510           ;; We (probably) got all the headers.
6511           (nreverse headers)
6512         (let ((gnus-nov-is-evil t))
6513           (nconc
6514            (nreverse headers)
6515            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6516              (gnus-get-newsgroup-headers))))))))
6517
6518 (defun gnus-article-get-xrefs ()
6519   "Fill in the Xref value in `gnus-current-headers', if necessary.
6520 This is meant to be called in `gnus-article-internal-prepare-hook'."
6521   (let ((headers (with-current-buffer gnus-summary-buffer
6522                    gnus-current-headers)))
6523     (or (not gnus-use-cross-reference)
6524         (not headers)
6525         (and (mail-header-xref headers)
6526              (not (string= (mail-header-xref headers) "")))
6527         (let ((case-fold-search t)
6528               xref)
6529           (save-restriction
6530             (nnheader-narrow-to-headers)
6531             (goto-char (point-min))
6532             (when (or (and (not (eobp))
6533                            (eq (downcase (char-after)) ?x)
6534                            (looking-at "Xref:"))
6535                       (search-forward "\nXref:" nil t))
6536               (goto-char (1+ (match-end 0)))
6537               (setq xref (buffer-substring (point) (point-at-eol)))
6538               (mail-header-set-xref headers xref)))))))
6539
6540 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6541   "Find article ID and insert the summary line for that article.
6542 OLD-HEADER can either be a header or a line number to insert
6543 the subject line on."
6544   (let* ((line (and (numberp old-header) old-header))
6545          (old-header (and (vectorp old-header) old-header))
6546          (header (cond ((and old-header use-old-header)
6547                         old-header)
6548                        ((and (numberp id)
6549                              (gnus-number-to-header id))
6550                         (gnus-number-to-header id))
6551                        (t
6552                         (gnus-read-header id))))
6553          (number (and (numberp id) id))
6554          d)
6555     (when header
6556       ;; Rebuild the thread that this article is part of and go to the
6557       ;; article we have fetched.
6558       (when (and (not gnus-show-threads)
6559                  old-header)
6560         (when (and number
6561                    (setq d (gnus-data-find (mail-header-number old-header))))
6562           (goto-char (gnus-data-pos d))
6563           (gnus-data-remove
6564            number
6565            (- (point-at-bol)
6566               (prog1
6567                   (1+ (point-at-eol))
6568                 (gnus-delete-line))))))
6569       ;; Remove list identifiers from subject.
6570       (when gnus-list-identifiers
6571         (let ((gnus-newsgroup-headers (list header)))
6572           (gnus-summary-remove-list-identifiers)))
6573       (when old-header
6574         (mail-header-set-number header (mail-header-number old-header)))
6575       (setq gnus-newsgroup-sparse
6576             (delq (setq number (mail-header-number header))
6577                   gnus-newsgroup-sparse))
6578       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6579       (push number gnus-newsgroup-limit)
6580       (gnus-rebuild-thread (mail-header-id header) line)
6581       (gnus-summary-goto-subject number nil t))
6582     (when (and (numberp number)
6583                (> number 0))
6584       ;; We have to update the boundaries even if we can't fetch the
6585       ;; article if ID is a number -- so that the next `P' or `N'
6586       ;; command will fetch the previous (or next) article even
6587       ;; if the one we tried to fetch this time has been canceled.
6588       (when (> number gnus-newsgroup-end)
6589         (setq gnus-newsgroup-end number))
6590       (when (< number gnus-newsgroup-begin)
6591         (setq gnus-newsgroup-begin number))
6592       (setq gnus-newsgroup-unselected
6593             (delq number gnus-newsgroup-unselected)))
6594     ;; Report back a success?
6595     (and header (mail-header-number header))))
6596
6597 ;;; Process/prefix in the summary buffer
6598
6599 (defun gnus-summary-work-articles (n)
6600   "Return a list of articles to be worked upon.
6601 The prefix argument, the list of process marked articles, and the
6602 current article will be taken into consideration."
6603   (with-current-buffer gnus-summary-buffer
6604     (cond
6605      (n
6606       ;; A numerical prefix has been given.
6607       (setq n (prefix-numeric-value n))
6608       (let ((backward (< n 0))
6609             (n (abs (prefix-numeric-value n)))
6610             articles article)
6611         (save-excursion
6612           (while
6613               (and (> n 0)
6614                    (push (setq article (gnus-summary-article-number))
6615                          articles)
6616                    (if backward
6617                        (gnus-summary-find-prev nil article)
6618                      (gnus-summary-find-next nil article)))
6619             (decf n)))
6620         (nreverse articles)))
6621      ((and (gnus-region-active-p) (mark))
6622       (message "region active")
6623       ;; Work on the region between point and mark.
6624       (let ((max (max (point) (mark)))
6625             articles article)
6626         (save-excursion
6627           (goto-char (min (point) (mark)))
6628           (while
6629               (and
6630                (push (setq article (gnus-summary-article-number)) articles)
6631                (gnus-summary-find-next nil article)
6632                (< (point) max)))
6633           (nreverse articles))))
6634      (gnus-newsgroup-processable
6635       ;; There are process-marked articles present.
6636       ;; Save current state.
6637       (gnus-summary-save-process-mark)
6638       ;; Return the list.
6639       (reverse gnus-newsgroup-processable))
6640      (t
6641       ;; Just return the current article.
6642       (list (gnus-summary-article-number))))))
6643
6644 (defmacro gnus-summary-iterate (arg &rest forms)
6645   "Iterate over the process/prefixed articles and do FORMS.
6646 ARG is the interactive prefix given to the command.  FORMS will be
6647 executed with point over the summary line of the articles."
6648   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6649     `(let ((,articles (gnus-summary-work-articles ,arg)))
6650        (while ,articles
6651          (gnus-summary-goto-subject (car ,articles))
6652          ,@forms
6653          (pop ,articles)))))
6654
6655 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6656 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6657
6658 (defun gnus-summary-save-process-mark ()
6659   "Push the current set of process marked articles on the stack."
6660   (interactive)
6661   (push (copy-sequence gnus-newsgroup-processable)
6662         gnus-newsgroup-process-stack))
6663
6664 (defun gnus-summary-kill-process-mark ()
6665   "Push the current set of process marked articles on the stack and unmark."
6666   (interactive)
6667   (gnus-summary-save-process-mark)
6668   (gnus-summary-unmark-all-processable))
6669
6670 (defun gnus-summary-yank-process-mark ()
6671   "Pop the last process mark state off the stack and restore it."
6672   (interactive)
6673   (unless gnus-newsgroup-process-stack
6674     (error "Empty mark stack"))
6675   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6676
6677 (defun gnus-summary-process-mark-set (set)
6678   "Make SET into the current process marked articles."
6679   (gnus-summary-unmark-all-processable)
6680   (mapc 'gnus-summary-set-process-mark set))
6681
6682 ;;; Searching and stuff
6683
6684 (defun gnus-summary-search-group (&optional backward use-level)
6685   "Search for next unread newsgroup.
6686 If optional argument BACKWARD is non-nil, search backward instead."
6687   (with-current-buffer gnus-group-buffer
6688     (when (gnus-group-search-forward
6689            backward nil (if use-level (gnus-group-group-level) nil))
6690       (gnus-group-group-name))))
6691
6692 (defun gnus-summary-best-group (&optional exclude-group)
6693   "Find the name of the best unread group.
6694 If EXCLUDE-GROUP, do not go to this group."
6695   (with-current-buffer gnus-group-buffer
6696     (save-excursion
6697       (gnus-group-best-unread-group exclude-group))))
6698
6699 (defun gnus-summary-find-next (&optional unread article backward)
6700   (if backward
6701       (gnus-summary-find-prev unread article)
6702     (let* ((dummy (gnus-summary-article-intangible-p))
6703            (article (or article (gnus-summary-article-number)))
6704            (data (gnus-data-find-list article))
6705            result)
6706       (when (and (not dummy)
6707                  (or (not gnus-summary-check-current)
6708                      (not unread)
6709                      (not (gnus-data-unread-p (car data)))))
6710         (setq data (cdr data)))
6711       (when (setq result
6712                   (if unread
6713                       (progn
6714                         (while data
6715                           (unless (memq (gnus-data-number (car data))
6716                                         (cond
6717                                          ((eq gnus-auto-goto-ignores
6718                                               'always-undownloaded)
6719                                           gnus-newsgroup-undownloaded)
6720                                          (gnus-plugged
6721                                           nil)
6722                                          ((eq gnus-auto-goto-ignores
6723                                               'unfetched)
6724                                           gnus-newsgroup-unfetched)
6725                                          ((eq gnus-auto-goto-ignores
6726                                               'undownloaded)
6727                                           gnus-newsgroup-undownloaded)))
6728                             (when (gnus-data-unread-p (car data))
6729                               (setq result (car data)
6730                                     data nil)))
6731                           (setq data (cdr data)))
6732                         result)
6733                     (car data)))
6734         (goto-char (gnus-data-pos result))
6735         (gnus-data-number result)))))
6736
6737 (defun gnus-summary-find-prev (&optional unread article)
6738   (let* ((eobp (eobp))
6739          (article (or article (gnus-summary-article-number)))
6740          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6741          result)
6742     (when (and (not eobp)
6743                (or (not gnus-summary-check-current)
6744                    (not unread)
6745                    (not (gnus-data-unread-p (car data)))))
6746       (setq data (cdr data)))
6747     (when (setq result
6748                 (if unread
6749                     (progn
6750                       (while data
6751                         (unless (memq (gnus-data-number (car data))
6752                                       (cond
6753                                        ((eq gnus-auto-goto-ignores
6754                                             'always-undownloaded)
6755                                         gnus-newsgroup-undownloaded)
6756                                        (gnus-plugged
6757                                         nil)
6758                                        ((eq gnus-auto-goto-ignores
6759                                             'unfetched)
6760                                         gnus-newsgroup-unfetched)
6761                                        ((eq gnus-auto-goto-ignores
6762                                             'undownloaded)
6763                                         gnus-newsgroup-undownloaded)))
6764                           (when (gnus-data-unread-p (car data))
6765                             (setq result (car data)
6766                                   data nil)))
6767                         (setq data (cdr data)))
6768                       result)
6769                   (car data)))
6770       (goto-char (gnus-data-pos result))
6771       (gnus-data-number result))))
6772
6773 (defun gnus-summary-find-subject (subject &optional unread backward article)
6774   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6775          (article (or article (gnus-summary-article-number)))
6776          (articles (gnus-data-list backward))
6777          (arts (gnus-data-find-list article articles))
6778          result)
6779     (when (or (not gnus-summary-check-current)
6780               (not unread)
6781               (not (gnus-data-unread-p (car arts))))
6782       (setq arts (cdr arts)))
6783     (while arts
6784       (and (or (not unread)
6785                (gnus-data-unread-p (car arts)))
6786            (vectorp (gnus-data-header (car arts)))
6787            (gnus-subject-equal
6788             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6789            (setq result (car arts)
6790                  arts nil))
6791       (setq arts (cdr arts)))
6792     (and result
6793          (goto-char (gnus-data-pos result))
6794          (gnus-data-number result))))
6795
6796 (defun gnus-summary-search-forward (&optional unread subject backward)
6797   "Search forward for an article.
6798 If UNREAD, look for unread articles.  If SUBJECT, look for
6799 articles with that subject.  If BACKWARD, search backward instead."
6800   (cond (subject (gnus-summary-find-subject subject unread backward))
6801         (backward (gnus-summary-find-prev unread))
6802         (t (gnus-summary-find-next unread))))
6803
6804 (defun gnus-recenter (&optional n)
6805   "Center point in window and redisplay frame.
6806 Also do horizontal recentering."
6807   (interactive "P")
6808   (when (and gnus-auto-center-summary
6809              (not (eq gnus-auto-center-summary 'vertical)))
6810     (gnus-horizontal-recenter))
6811   (if (fboundp 'recenter-top-bottom)
6812       (recenter-top-bottom n)
6813     (recenter n)))
6814
6815 (put 'gnus-recenter 'isearch-scroll t)
6816
6817 (defun gnus-forward-line-ignore-invisible (n)
6818   "Move N lines forward (backward if N is negative).
6819 Like forward-line, but skip over (and don't count) invisible lines."
6820   (let (done)
6821     (while (and (> n 0) (not done))
6822       ;; If the following character is currently invisible,
6823       ;; skip all characters with that same `invisible' property value.
6824       (while (gnus-invisible-p (point))
6825         (goto-char (gnus-next-char-property-change (point))))
6826       (forward-line 1)
6827       (if (eobp)
6828           (setq done t)
6829         (setq n (1- n))))
6830     (while (and (< n 0) (not done))
6831       (forward-line -1)
6832       (if (bobp) (setq done t)
6833         (setq n (1+ n))
6834         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6835           (goto-char (gnus-previous-char-property-change (point))))))))
6836
6837 (defun gnus-summary-recenter ()
6838   "Center point in the summary window.
6839 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6840 displayed, no centering will be performed."
6841   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6842   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6843   (interactive)
6844   ;; The user has to want it.
6845   (when gnus-auto-center-summary
6846     (let* ((top (cond ((< (window-height) 4) 0)
6847                       ((< (window-height) 7) 1)
6848                       (t (if (numberp gnus-auto-center-summary)
6849                              gnus-auto-center-summary
6850                            (/ (1- (window-height)) 2)))))
6851            (height (1- (window-height)))
6852            (bottom (save-excursion
6853                      (goto-char (point-max))
6854                      (gnus-forward-line-ignore-invisible (- height))
6855                      (point)))
6856            (window (get-buffer-window (current-buffer))))
6857       (when (get-buffer-window gnus-article-buffer)
6858         ;; Only do recentering when the article buffer is displayed,
6859         ;; Set the window start to either `bottom', which is the biggest
6860         ;; possible valid number, or the second line from the top,
6861         ;; whichever is the least.
6862         (let ((top-pos (save-excursion
6863                          (gnus-forward-line-ignore-invisible (- top))
6864                          (point))))
6865           (if (> bottom top-pos)
6866               ;; Keep the second line from the top visible
6867               (set-window-start window top-pos)
6868             ;; Try to keep the bottom line visible; if it's partially
6869             ;; obscured, either scroll one more line to make it fully
6870             ;; visible, or revert to using TOP-POS.
6871             (save-excursion
6872               (goto-char (point-max))
6873               (gnus-forward-line-ignore-invisible -1)
6874               (let ((last-line-start (point)))
6875                 (goto-char bottom)
6876                 (set-window-start window (point) t)
6877                 (when (not (pos-visible-in-window-p last-line-start window))
6878                   (gnus-forward-line-ignore-invisible 1)
6879                   (set-window-start window (min (point) top-pos) t)))))))
6880       ;; Do horizontal recentering while we're at it.
6881       (when (and (get-buffer-window (current-buffer) t)
6882                  (not (eq gnus-auto-center-summary 'vertical)))
6883         (let ((selected (selected-window)))
6884           (select-window (get-buffer-window (current-buffer) t))
6885           (gnus-summary-position-point)
6886           (gnus-horizontal-recenter)
6887           (select-window selected))))))
6888
6889 (defun gnus-summary-jump-to-group (newsgroup)
6890   "Move point to NEWSGROUP in group mode buffer."
6891   ;; Keep update point of group mode buffer if visible.
6892   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6893       (save-window-excursion
6894         ;; Take care of tree window mode.
6895         (when (get-buffer-window gnus-group-buffer)
6896           (pop-to-buffer gnus-group-buffer))
6897         (gnus-group-jump-to-group newsgroup))
6898     (save-excursion
6899       ;; Take care of tree window mode.
6900       (if (get-buffer-window gnus-group-buffer 0)
6901           (pop-to-buffer gnus-group-buffer)
6902         (set-buffer gnus-group-buffer))
6903       (gnus-group-jump-to-group newsgroup))))
6904
6905 ;; This function returns a list of article numbers based on the
6906 ;; difference between the ranges of read articles in this group and
6907 ;; the range of active articles.
6908 (defun gnus-list-of-unread-articles (group)
6909   (let* ((read (gnus-info-read (gnus-get-info group)))
6910          (active (or (gnus-active group) (gnus-activate-group group)))
6911          (last (or (cdr active)
6912                    (error "Group %s couldn't be activated " group)))
6913          (bottom (if gnus-newsgroup-maximum-articles
6914                      (max (car active)
6915                           (- last gnus-newsgroup-maximum-articles -1))
6916                    (car active)))
6917          first nlast unread)
6918     ;; If none are read, then all are unread.
6919     (if (not read)
6920         (setq first bottom)
6921       ;; If the range of read articles is a single range, then the
6922       ;; first unread article is the article after the last read
6923       ;; article.  Sounds logical, doesn't it?
6924       (if (and (not (listp (cdr read)))
6925                (or (< (car read) bottom)
6926                    (progn (setq read (list read))
6927                           nil)))
6928           (setq first (max bottom (1+ (cdr read))))
6929         ;; `read' is a list of ranges.
6930         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6931                                   (caar read)))
6932                   1)
6933           (setq first bottom))
6934         (while read
6935           (when first
6936             (while (< first nlast)
6937               (setq unread (cons first unread)
6938                     first (1+ first))))
6939           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6940           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6941           (setq read (cdr read)))))
6942     ;; And add the last unread articles.
6943     (while (<= first last)
6944       (setq unread (cons first unread)
6945             first (1+ first)))
6946     ;; Return the list of unread articles.
6947     (delq 0 (nreverse unread))))
6948
6949 (defun gnus-list-of-read-articles (group)
6950   "Return a list of unread, unticked and non-dormant articles."
6951   (let* ((info (gnus-get-info group))
6952          (marked (gnus-info-marks info))
6953          (active (gnus-active group)))
6954     (and info active
6955          (gnus-list-range-difference
6956           (gnus-list-range-difference
6957            (gnus-sorted-complement
6958             (gnus-uncompress-range
6959              (if gnus-newsgroup-maximum-articles
6960                  (cons (max (car active)
6961                             (- (cdr active)
6962                                gnus-newsgroup-maximum-articles
6963                                -1))
6964                        (cdr active))
6965                active))
6966             (gnus-list-of-unread-articles group))
6967            (cdr (assq 'dormant marked)))
6968           (cdr (assq 'tick marked))))))
6969
6970 ;; This function returns a sequence of article numbers based on the
6971 ;; difference between the ranges of read articles in this group and
6972 ;; the range of active articles.
6973 (defun gnus-sequence-of-unread-articles (group)
6974   (let* ((read (gnus-info-read (gnus-get-info group)))
6975          (active (or (gnus-active group) (gnus-activate-group group)))
6976          (last (cdr active))
6977          (bottom (if gnus-newsgroup-maximum-articles
6978                      (max (car active)
6979                           (- last gnus-newsgroup-maximum-articles -1))
6980                    (car active)))
6981          first nlast unread)
6982     ;; If none are read, then all are unread.
6983     (if (not read)
6984         (setq first bottom)
6985       ;; If the range of read articles is a single range, then the
6986       ;; first unread article is the article after the last read
6987       ;; article.  Sounds logical, doesn't it?
6988       (if (and (not (listp (cdr read)))
6989                (or (< (car read) bottom)
6990                    (progn (setq read (list read))
6991                           nil)))
6992           (setq first (max bottom (1+ (cdr read))))
6993         ;; `read' is a list of ranges.
6994         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6995                                   (caar read)))
6996                   1)
6997           (setq first bottom))
6998         (while read
6999           (when first
7000             (push (cons first nlast) unread))
7001           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7002           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7003           (setq read (cdr read)))))
7004     ;; And add the last unread articles.
7005     (cond ((not (and first last))
7006            nil)
7007           ((< first last)
7008            (push (cons first last) unread))
7009           ((= first last)
7010            (push first unread)))
7011     ;; Return the sequence of unread articles.
7012     (delq 0 (nreverse unread))))
7013
7014 ;; Various summary commands
7015
7016 (defun gnus-summary-select-article-buffer ()
7017   "Reconfigure windows to show the article buffer.
7018 If `gnus-widen-article-buffer' is set, show only the article
7019 buffer."
7020   (interactive)
7021   (if (not (gnus-buffer-live-p gnus-article-buffer))
7022       (error "There is no article buffer for this summary buffer")
7023     (unless (get-buffer-window gnus-article-buffer)
7024       (gnus-summary-show-article))
7025     (gnus-configure-windows
7026      (if gnus-widen-article-window
7027          'only-article
7028        'article)
7029      t)
7030     (select-window (get-buffer-window gnus-article-buffer))))
7031
7032 (defun gnus-summary-universal-argument (arg)
7033   "Perform any operation on all articles that are process/prefixed."
7034   (interactive "P")
7035   (let ((articles (gnus-summary-work-articles arg))
7036         func article)
7037     (if (eq
7038          (setq
7039           func
7040           (key-binding
7041            (read-key-sequence
7042             (substitute-command-keys
7043              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7044          'undefined)
7045         (gnus-error 1 "Undefined key")
7046       (save-excursion
7047         (while articles
7048           (gnus-summary-goto-subject (setq article (pop articles)))
7049           (let (gnus-newsgroup-processable)
7050             (command-execute func))
7051           (gnus-summary-remove-process-mark article)))))
7052   (gnus-summary-position-point))
7053
7054 (defun gnus-summary-toggle-truncation (&optional arg)
7055   "Toggle truncation of summary lines.
7056 With ARG, turn line truncation on if ARG is positive."
7057   (interactive "P")
7058   (setq truncate-lines
7059         (if (null arg) (not truncate-lines)
7060           (> (prefix-numeric-value arg) 0)))
7061   (redraw-display))
7062
7063 (defun gnus-summary-find-for-reselect ()
7064   "Return the number of an article to stay on across a reselect.
7065 The current article is considered, then following articles, then previous
7066 articles.  An article is sought which is not cancelled and isn't a temporary
7067 insertion from another group.  If there's no such then return a dummy 0."
7068   (let (found)
7069     (dolist (rev '(nil t))
7070       (unless found      ; don't demand the reverse list if we don't need it
7071         (let ((data (gnus-data-find-list
7072                      (gnus-summary-article-number) (gnus-data-list rev))))
7073           (while (and data (not found))
7074             (if (and (< 0 (gnus-data-number (car data)))
7075                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7076                 (setq found (gnus-data-number (car data))))
7077             (setq data (cdr data))))))
7078     (or found 0)))
7079
7080 (defun gnus-summary-reselect-current-group (&optional all rescan)
7081   "Exit and then reselect the current newsgroup.
7082 The prefix argument ALL means to select all articles."
7083   (interactive "P")
7084   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7085     (error "Ephemeral groups can't be reselected"))
7086   (let ((current-subject (gnus-summary-find-for-reselect))
7087         (group gnus-newsgroup-name))
7088     (setq gnus-newsgroup-begin nil)
7089     (gnus-summary-exit nil 'leave-hidden)
7090     ;; We have to adjust the point of group mode buffer because
7091     ;; point was moved to the next unread newsgroup by exiting.
7092     (gnus-summary-jump-to-group group)
7093     (when rescan
7094       (save-excursion
7095         (gnus-group-get-new-news-this-group 1)))
7096     (gnus-group-read-group all t)
7097     (gnus-summary-goto-subject current-subject nil t)))
7098
7099 (defun gnus-summary-rescan-group (&optional all)
7100   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7101   (interactive "P")
7102   (let ((config gnus-current-window-configuration))
7103     (gnus-summary-reselect-current-group all t)
7104     (gnus-configure-windows config)
7105     (when (eq config 'article)
7106       (gnus-summary-select-article))))
7107
7108 (defun gnus-summary-update-info (&optional non-destructive)
7109   (save-excursion
7110     (let ((group gnus-newsgroup-name))
7111       (when group
7112         (when gnus-newsgroup-kill-headers
7113           (setq gnus-newsgroup-killed
7114                 (gnus-compress-sequence
7115                  (gnus-sorted-union
7116                   (gnus-list-range-intersection
7117                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7118                   gnus-newsgroup-unreads)
7119                  t)))
7120         (unless (listp (cdr gnus-newsgroup-killed))
7121           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7122         (let ((headers gnus-newsgroup-headers))
7123           ;; Set the new ranges of read articles.
7124           (with-current-buffer gnus-group-buffer
7125             (gnus-undo-force-boundary))
7126           (gnus-update-read-articles
7127            group (gnus-sorted-union
7128                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7129           ;; Set the current article marks.
7130           (let ((gnus-newsgroup-scored
7131                  (if (and (not gnus-save-score)
7132                           (not non-destructive))
7133                      nil
7134                    gnus-newsgroup-scored)))
7135             (save-excursion
7136               (gnus-update-marks)))
7137           ;; Do the cross-ref thing.
7138           (when gnus-use-cross-reference
7139             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7140           ;; Do not switch windows but change the buffer to work.
7141           (set-buffer gnus-group-buffer)
7142           (unless (gnus-ephemeral-group-p group)
7143             (gnus-group-update-group group)))))))
7144
7145 (defun gnus-summary-save-newsrc (&optional force)
7146   "Save the current number of read/marked articles in the dribble buffer.
7147 The dribble buffer will then be saved.
7148 If FORCE (the prefix), also save the .newsrc file(s)."
7149   (interactive "P")
7150   (gnus-summary-update-info t)
7151   (if force
7152       (gnus-save-newsrc-file)
7153     (gnus-dribble-save)))
7154
7155 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7156
7157 (defun gnus-summary-exit (&optional temporary leave-hidden)
7158   "Exit reading current newsgroup, and then return to group selection mode.
7159 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7160   (interactive)
7161   (gnus-set-global-variables)
7162   (when (gnus-buffer-live-p gnus-article-buffer)
7163     (with-current-buffer gnus-article-buffer
7164       (mm-destroy-parts gnus-article-mime-handles)
7165       ;; Set it to nil for safety reason.
7166       (setq gnus-article-mime-handle-alist nil)
7167       (setq gnus-article-mime-handles nil)))
7168   (gnus-kill-save-kill-buffer)
7169   (gnus-async-halt-prefetch)
7170   (let* ((group gnus-newsgroup-name)
7171          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7172          (gnus-group-is-exiting-p t)
7173          (article-buffer gnus-article-buffer)
7174          (mode major-mode)
7175          (group-point nil)
7176          (buf (current-buffer)))
7177     (unless quit-config
7178       ;; Do adaptive scoring, and possibly save score files.
7179       (when gnus-newsgroup-adaptive
7180         (gnus-score-adaptive))
7181       (when gnus-use-scoring
7182         (gnus-score-save)))
7183     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7184     (when gnus-use-cache
7185       (gnus-cache-possibly-remove-articles)
7186       (gnus-cache-save-buffers))
7187     (gnus-async-prefetch-remove-group group)
7188     (when gnus-suppress-duplicates
7189       (gnus-dup-enter-articles))
7190     (when gnus-use-trees
7191       (gnus-tree-close group))
7192     (when gnus-use-cache
7193       (gnus-cache-write-active))
7194     ;; Remove entries for this group.
7195     (nnmail-purge-split-history (gnus-group-real-name group))
7196     ;; Make all changes in this group permanent.
7197     (unless quit-config
7198       (gnus-run-hooks 'gnus-exit-group-hook)
7199       (gnus-summary-update-info))
7200     (gnus-close-group group)
7201     ;; Make sure where we were, and go to next newsgroup.
7202     (set-buffer gnus-group-buffer)
7203     (unless quit-config
7204       (gnus-group-jump-to-group group))
7205     (gnus-run-hooks 'gnus-summary-exit-hook)
7206     (unless (or quit-config
7207                 (not gnus-summary-next-group-on-exit)
7208                 ;; If this group has disappeared from the summary
7209                 ;; buffer, don't skip forwards.
7210                 (not (string= group (gnus-group-group-name))))
7211       (gnus-group-next-unread-group 1))
7212     (setq group-point (point))
7213     (if temporary
7214         nil                             ;Nothing to do.
7215       (set-buffer buf)
7216       (if (not gnus-kill-summary-on-exit)
7217           (progn
7218             (gnus-deaden-summary)
7219             (setq mode nil))
7220         (when (get-buffer gnus-article-buffer)
7221           (bury-buffer gnus-article-buffer))
7222         ;; Return to group mode buffer.
7223         (when (eq mode 'gnus-summary-mode)
7224           (gnus-kill-buffer buf)))
7225
7226       (setq gnus-current-select-method gnus-select-method)
7227       (set-buffer gnus-group-buffer)
7228       (if quit-config
7229           (gnus-handle-ephemeral-exit quit-config)
7230         (goto-char group-point)
7231         ;; If gnus-group-buffer is already displayed, make sure we also move
7232         ;; the cursor in the window that displays it.
7233         (let ((win (get-buffer-window (current-buffer) 0)))
7234           (if win (set-window-point win (point))))
7235         (unless leave-hidden
7236           (gnus-configure-windows 'group 'force)))
7237
7238       ;; If we have several article buffers, we kill them at exit.
7239       (unless gnus-single-article-buffer
7240         (when (gnus-buffer-live-p article-buffer)
7241           (with-current-buffer article-buffer
7242             ;; Don't kill sticky article buffers
7243             (unless (eq major-mode 'gnus-sticky-article-mode)
7244               (gnus-kill-buffer article-buffer)
7245               (setq gnus-article-current nil))))
7246         (gnus-kill-buffer gnus-original-article-buffer))
7247
7248       ;; Clear the current group name.
7249       (unless quit-config
7250         (setq gnus-newsgroup-name nil)))))
7251
7252 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7253 (defun gnus-summary-exit-no-update (&optional no-questions)
7254   "Quit reading current newsgroup without updating read article info."
7255   (interactive)
7256   (let* ((group gnus-newsgroup-name)
7257          (gnus-group-is-exiting-p t)
7258          (gnus-group-is-exiting-without-update-p t)
7259          (quit-config (gnus-group-quit-config group)))
7260     (when (or no-questions
7261               gnus-expert-user
7262               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7263       (gnus-async-halt-prefetch)
7264       (run-hooks 'gnus-summary-prepare-exit-hook)
7265       (when (gnus-buffer-live-p gnus-article-buffer)
7266         (with-current-buffer gnus-article-buffer
7267           (mm-destroy-parts gnus-article-mime-handles)
7268           ;; Set it to nil for safety reason.
7269           (setq gnus-article-mime-handle-alist nil)
7270           (setq gnus-article-mime-handles nil)))
7271       ;; If we have several article buffers, we kill them at exit.
7272       (unless gnus-single-article-buffer
7273         (gnus-kill-buffer gnus-article-buffer)
7274         (gnus-kill-buffer gnus-original-article-buffer)
7275         (setq gnus-article-current nil))
7276       ;; Return to the group buffer.
7277       (gnus-configure-windows 'group 'force)
7278       (if (not gnus-kill-summary-on-exit)
7279           (gnus-deaden-summary)
7280         (gnus-close-group group)
7281         (gnus-kill-buffer gnus-summary-buffer))
7282       (unless gnus-single-article-buffer
7283         (setq gnus-article-current nil))
7284       (when gnus-use-trees
7285         (gnus-tree-close group))
7286       (gnus-async-prefetch-remove-group group)
7287       (when (get-buffer gnus-article-buffer)
7288         (bury-buffer gnus-article-buffer))
7289       ;; Clear the current group name.
7290       (setq gnus-newsgroup-name nil)
7291       (unless (gnus-ephemeral-group-p group)
7292         (gnus-group-update-group group))
7293       (when (equal (gnus-group-group-name) group)
7294         (gnus-group-next-unread-group 1))
7295       (when quit-config
7296         (gnus-handle-ephemeral-exit quit-config)))))
7297
7298 (defun gnus-handle-ephemeral-exit (quit-config)
7299   "Handle movement when leaving an ephemeral group.
7300 The state which existed when entering the ephemeral is reset."
7301   (if (not (buffer-name (car quit-config)))
7302       (gnus-configure-windows 'group 'force)
7303     (set-buffer (car quit-config))
7304     (cond ((eq major-mode 'gnus-summary-mode)
7305            (gnus-set-global-variables))
7306           ((eq major-mode 'gnus-article-mode)
7307            (save-current-buffer
7308              ;; The `gnus-summary-buffer' variable may point
7309              ;; to the old summary buffer when using a single
7310              ;; article buffer.
7311              (unless (gnus-buffer-live-p gnus-summary-buffer)
7312                (set-buffer gnus-group-buffer))
7313              (set-buffer gnus-summary-buffer)
7314              (gnus-set-global-variables))))
7315     (if (or (eq (cdr quit-config) 'article)
7316             (eq (cdr quit-config) 'pick))
7317         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7318             (gnus-configure-windows 'pick 'force)
7319           (gnus-configure-windows (cdr quit-config) 'force))
7320       (gnus-configure-windows (cdr quit-config) 'force))
7321     (when (eq major-mode 'gnus-summary-mode)
7322       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7323                                                      next-unread-noselect))
7324           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7325                                   'next-noselect)
7326                               (gnus-summary-next-subject 1 nil t))
7327                              ((eq gnus-auto-select-on-ephemeral-exit
7328                                   'next-unread-noselect)
7329                               (gnus-summary-next-subject 1 t t))))
7330             ;; Hide the article buffer which displays the article different
7331             ;; from the one that the cursor points to in the summary buffer.
7332             (gnus-configure-windows 'summary 'force))
7333         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7334                (gnus-summary-next-subject 1))
7335               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7336                (gnus-summary-next-subject 1 t))))
7337       (gnus-summary-recenter)
7338       (gnus-summary-position-point))))
7339
7340 ;;; Dead summaries.
7341
7342 (defvar gnus-dead-summary-mode-map
7343   (let ((map (make-keymap)))
7344     (suppress-keymap map)
7345     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7346     (dolist (key '("\C-d" "\r" "\177" [delete]))
7347       (define-key map key 'gnus-summary-wake-up-the-dead))
7348     (dolist (key '("q" "Q"))
7349       (define-key map key 'bury-buffer))
7350     map))
7351
7352 (define-minor-mode gnus-dead-summary-mode
7353   "Minor mode for Gnus summary buffers."
7354   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7355   (unless (derived-mode-p 'gnus-summary-mode)
7356     (setq gnus-dead-summary-mode nil)))
7357
7358 (defun gnus-deaden-summary ()
7359   "Make the current summary buffer into a dead summary buffer."
7360   ;; Kill any previous dead summary buffer.
7361   (when (and gnus-dead-summary
7362              (buffer-name gnus-dead-summary))
7363     (with-current-buffer gnus-dead-summary
7364       (when gnus-dead-summary-mode
7365         (kill-buffer (current-buffer)))))
7366   ;; Make this the current dead summary.
7367   (setq gnus-dead-summary (current-buffer))
7368   (gnus-dead-summary-mode 1)
7369   (let ((name (buffer-name)))
7370     (when (string-match "Summary" name)
7371       (rename-buffer
7372        (concat (substring name 0 (match-beginning 0)) "Dead "
7373                (substring name (match-beginning 0)))
7374        t)
7375       (bury-buffer))))
7376
7377 (defun gnus-kill-or-deaden-summary (buffer)
7378   "Kill or deaden the summary BUFFER."
7379   (save-excursion
7380     (when (and (buffer-name buffer)
7381                (not gnus-single-article-buffer))
7382       (with-current-buffer buffer
7383         (gnus-kill-buffer gnus-article-buffer)
7384         (gnus-kill-buffer gnus-original-article-buffer)))
7385     (cond
7386      ;; Kill the buffer.
7387      (gnus-kill-summary-on-exit
7388       (when (and gnus-use-trees
7389                  (gnus-buffer-exists-p buffer))
7390         (with-current-buffer buffer
7391           (gnus-tree-close gnus-newsgroup-name)))
7392       (gnus-kill-buffer buffer))
7393      ;; Deaden the buffer.
7394      ((gnus-buffer-exists-p buffer)
7395       (with-current-buffer buffer
7396         (gnus-deaden-summary))))))
7397
7398 (defun gnus-summary-wake-up-the-dead (&rest args)
7399   "Wake up the dead summary buffer."
7400   (interactive)
7401   (gnus-dead-summary-mode -1)
7402   (let ((name (buffer-name)))
7403     (when (string-match "Dead " name)
7404       (rename-buffer
7405        (concat (substring name 0 (match-beginning 0))
7406                (substring name (match-end 0)))
7407        t)))
7408   (gnus-message 3 "This dead summary is now alive again"))
7409
7410 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7411 (defun gnus-summary-describe-group (&optional force)
7412   "Describe the current newsgroup."
7413   (interactive "P")
7414   (gnus-group-describe-group force gnus-newsgroup-name))
7415
7416 (defun gnus-summary-describe-briefly ()
7417   "Describe summary mode commands briefly."
7418   (interactive)
7419   (gnus-message 6 "%s" (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")))
7420
7421 ;; Walking around group mode buffer from summary mode.
7422
7423 (defun gnus-summary-next-group (&optional no-article target-group backward)
7424   "Exit current newsgroup and then select next unread newsgroup.
7425 If prefix argument NO-ARTICLE is non-nil, no article is selected
7426 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7427 previous group instead."
7428   (interactive "P")
7429   ;; Stop pre-fetching.
7430   (gnus-async-halt-prefetch)
7431   (let ((current-group gnus-newsgroup-name)
7432         (current-buffer (current-buffer))
7433         entered)
7434     ;; First we semi-exit this group to update Xrefs and all variables.
7435     ;; We can't do a real exit, because the window conf must remain
7436     ;; the same in case the user is prompted for info, and we don't
7437     ;; want the window conf to change before that...
7438     (gnus-summary-exit t)
7439     (while (not entered)
7440       ;; Then we find what group we are supposed to enter.
7441       (set-buffer gnus-group-buffer)
7442       (gnus-group-jump-to-group current-group)
7443       (setq target-group
7444             (or target-group
7445                 (if (eq gnus-keep-same-level 'best)
7446                     (gnus-summary-best-group gnus-newsgroup-name)
7447                   (gnus-summary-search-group backward gnus-keep-same-level))))
7448       (if (not target-group)
7449           ;; There are no further groups, so we return to the group
7450           ;; buffer.
7451           (progn
7452             (gnus-message 5 "Returning to the group buffer")
7453             (setq entered t)
7454             (when (gnus-buffer-live-p current-buffer)
7455               (set-buffer current-buffer)
7456               (gnus-summary-exit))
7457             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7458         ;; We try to enter the target group.
7459         (gnus-group-jump-to-group target-group)
7460         (let ((unreads (gnus-group-group-unread)))
7461           (if (and (or (eq t unreads)
7462                        (and unreads (not (zerop unreads))))
7463                    (gnus-summary-read-group
7464                     target-group nil no-article
7465                     (and (buffer-name current-buffer) current-buffer)
7466                     nil backward))
7467               (setq entered t)
7468             (setq current-group target-group
7469                   target-group nil)))))))
7470
7471 (defun gnus-summary-prev-group (&optional no-article)
7472   "Exit current newsgroup and then select previous unread newsgroup.
7473 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7474   (interactive "P")
7475   (gnus-summary-next-group no-article nil t))
7476
7477 ;; Walking around summary lines.
7478
7479 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7480   "Go to the first subject satisfying any non-nil constraint.
7481 If UNREAD is non-nil, the article should be unread.
7482 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7483 If UNSEEN is non-nil, the article should be unseen as well as unread.
7484 Returns the article selected or nil if there are no matching articles."
7485   (interactive "P")
7486   (cond
7487    ;; Empty summary.
7488    ((null gnus-newsgroup-data)
7489     (gnus-message 3 "No articles in the group")
7490     nil)
7491    ;; Pick the first article.
7492    ((not (or unread undownloaded unseen))
7493     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7494     (gnus-data-number (car gnus-newsgroup-data)))
7495    ;; Find the first unread article.
7496    (t
7497     (let ((data gnus-newsgroup-data))
7498       (while (and data
7499                   (let ((num (gnus-data-number (car data))))
7500                     (or (memq num gnus-newsgroup-unfetched)
7501                         (not (or (and unread
7502                                       (memq num gnus-newsgroup-unreads))
7503                                  (and undownloaded
7504                                       (memq num gnus-newsgroup-undownloaded))
7505                                  (and unseen
7506                                       (memq num gnus-newsgroup-unseen)
7507                                       (memq num gnus-newsgroup-unreads)))))))
7508         (setq data (cdr data)))
7509       (prog1
7510           (if data
7511               (progn
7512                 (goto-char (gnus-data-pos (car data)))
7513                 (gnus-data-number (car data)))
7514             (gnus-message 3 "No more%s articles"
7515                           (let* ((r (when unread " unread"))
7516                                  (d (when undownloaded " undownloaded"))
7517                                  (s (when unseen " unseen"))
7518                                  (l (delq nil (list r d s))))
7519                             (cond ((= 3 (length l))
7520                                    (concat r "," d ", or" s))
7521                                   ((= 2 (length l))
7522                                    (concat (car l) ", or" (cadr l)))
7523                                   ((= 1 (length l))
7524                                    (car l))
7525                                   (t
7526                                    ""))))
7527             nil
7528             )
7529         (gnus-summary-position-point))))))
7530
7531 (defun gnus-summary-next-subject (n &optional unread dont-display)
7532   "Go to next N'th summary line.
7533 If N is negative, go to the previous N'th subject line.
7534 If UNREAD is non-nil, only unread articles are selected.
7535 The difference between N and the actual number of steps taken is
7536 returned."
7537   (interactive "p")
7538   (let ((backward (< n 0))
7539         (n (abs n)))
7540     (while (and (> n 0)
7541                 (if backward
7542                     (gnus-summary-find-prev unread)
7543                   (gnus-summary-find-next unread)))
7544       (unless (zerop (setq n (1- n)))
7545         (gnus-summary-show-thread)))
7546     (when (/= 0 n)
7547       (gnus-message 7 "No more%s articles"
7548                     (if unread " unread" "")))
7549     (unless dont-display
7550       (gnus-summary-recenter)
7551       (gnus-summary-position-point))
7552     n))
7553
7554 (defun gnus-summary-next-unread-subject (n)
7555   "Go to next N'th unread summary line."
7556   (interactive "p")
7557   (gnus-summary-next-subject n t))
7558
7559 (defun gnus-summary-prev-subject (n &optional unread)
7560   "Go to previous N'th summary line.
7561 If optional argument UNREAD is non-nil, only unread article is selected."
7562   (interactive "p")
7563   (gnus-summary-next-subject (- n) unread))
7564
7565 (defun gnus-summary-prev-unread-subject (n)
7566   "Go to previous N'th unread summary line."
7567   (interactive "p")
7568   (gnus-summary-next-subject (- n) t))
7569
7570 (defun gnus-summary-goto-subjects (articles)
7571   "Insert the subject header for ARTICLES in the current buffer."
7572   (save-excursion
7573     (dolist (article articles)
7574       (gnus-summary-goto-subject article t)))
7575   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7576   (gnus-summary-position-point))
7577
7578 (defun gnus-summary-goto-subject (article &optional force silent)
7579   "Go to the subject line of ARTICLE.
7580 If FORCE, also allow jumping to articles not currently shown."
7581   (interactive "nArticle number: ")
7582   (unless (numberp article)
7583     (error "Article %s is not a number" article))
7584   (let ((b (point))
7585         (data (gnus-data-find article)))
7586     ;; We read in the article if we have to.
7587     (and (not data)
7588          force
7589          (gnus-summary-insert-subject
7590           article
7591           (if (or (numberp force) (vectorp force)) force)
7592           t)
7593          (setq data (gnus-data-find article)))
7594     (goto-char b)
7595     (if (not data)
7596         (progn
7597           (unless silent
7598             (gnus-message 3 "Can't find article %d" article))
7599           nil)
7600       (let ((pt (gnus-data-pos data)))
7601         (goto-char pt)
7602         (gnus-summary-set-article-display-arrow pt))
7603       (gnus-summary-position-point)
7604       article)))
7605
7606 ;; Walking around summary lines with displaying articles.
7607
7608 (defun gnus-summary-expand-window (&optional arg)
7609   "Make the summary buffer take up the entire Emacs frame.
7610 Given a prefix, will force an `article' buffer configuration."
7611   (interactive "P")
7612   (if arg
7613       (gnus-configure-windows 'article 'force)
7614     (gnus-configure-windows 'summary 'force)))
7615
7616 (defun gnus-summary-display-article (article &optional all-header)
7617   "Display ARTICLE in article buffer."
7618   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7619                (with-current-buffer gnus-article-buffer
7620                  (eq major-mode 'gnus-article-mode)))
7621     (gnus-article-setup-buffer))
7622   (gnus-set-global-variables)
7623   (with-current-buffer gnus-article-buffer
7624     (setq gnus-article-charset gnus-newsgroup-charset)
7625     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7626     (mm-enable-multibyte))
7627   (if (null article)
7628       nil
7629     (prog1
7630         (if gnus-summary-display-article-function
7631             (funcall gnus-summary-display-article-function article all-header)
7632           (gnus-article-prepare article all-header))
7633       (gnus-run-hooks 'gnus-select-article-hook)
7634       (when (and gnus-current-article
7635                  (not (zerop gnus-current-article)))
7636         (gnus-summary-goto-subject gnus-current-article))
7637       (gnus-summary-recenter)
7638       (when (and gnus-use-trees gnus-show-threads)
7639         (gnus-possibly-generate-tree article)
7640         (gnus-highlight-selected-tree article))
7641       ;; Successfully display article.
7642       (gnus-article-set-window-start
7643        (cdr (assq article gnus-newsgroup-bookmarks))))))
7644
7645 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7646   "Select the current article.
7647 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7648 non-nil, the article will be re-fetched even if it already present in
7649 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7650 be displayed."
7651   ;; Make sure we are in the summary buffer to work around bbdb bug.
7652   (unless (eq major-mode 'gnus-summary-mode)
7653     (set-buffer gnus-summary-buffer))
7654   (let ((article (or article (gnus-summary-article-number)))
7655         (all-headers (not (not all-headers))) ;Must be t or nil.
7656         gnus-summary-display-article-function)
7657     (and (not pseudo)
7658          (gnus-summary-article-pseudo-p article)
7659          (error "This is a pseudo-article"))
7660     (with-current-buffer gnus-summary-buffer
7661       (if (or (and gnus-single-article-buffer
7662                    (or (null gnus-current-article)
7663                        (null gnus-article-current)
7664                        (null (get-buffer gnus-article-buffer))
7665                        (not (eq article (cdr gnus-article-current)))
7666                        (not (equal (car gnus-article-current)
7667                                    gnus-newsgroup-name))
7668                        (not (get-buffer gnus-original-article-buffer))))
7669               (and (not gnus-single-article-buffer)
7670                    (or (null gnus-current-article)
7671                        (not (get-buffer gnus-original-article-buffer))
7672                        (not (eq gnus-current-article article))))
7673               force)
7674           ;; The requested article is different from the current article.
7675           (progn
7676             (gnus-summary-display-article article all-headers)
7677             (when (gnus-buffer-live-p gnus-article-buffer)
7678               (with-current-buffer gnus-article-buffer
7679                 (if (not gnus-article-decoded-p) ;; a local variable
7680                     (mm-disable-multibyte))))
7681             (gnus-article-set-window-start
7682              (cdr (assq article gnus-newsgroup-bookmarks)))
7683             article)
7684         'old))))
7685
7686 (defun gnus-summary-force-verify-and-decrypt ()
7687   "Display buttons for signed/encrypted parts and verify/decrypt them."
7688   (interactive)
7689   (let ((mm-verify-option 'known)
7690         (mm-decrypt-option 'known)
7691         (gnus-article-emulate-mime t)
7692         (gnus-buttonized-mime-types (append (list "multipart/signed"
7693                                                   "multipart/encrypted")
7694                                             gnus-buttonized-mime-types)))
7695     (gnus-summary-select-article nil 'force)))
7696
7697 (defun gnus-summary-set-current-mark (&optional current-mark)
7698   "Obsolete function."
7699   nil)
7700
7701 (defun gnus-summary-next-article (&optional unread subject backward push)
7702   "Select the next article.
7703 If UNREAD, only unread articles are selected.
7704 If SUBJECT, only articles with SUBJECT are selected.
7705 If BACKWARD, the previous article is selected instead of the next."
7706   (interactive "P")
7707   ;; Make sure we are in the summary buffer.
7708   (unless (eq major-mode 'gnus-summary-mode)
7709     (set-buffer gnus-summary-buffer))
7710   (cond
7711    ;; Is there such an article?
7712    ((and (gnus-summary-search-forward unread subject backward)
7713          (or (gnus-summary-display-article (gnus-summary-article-number))
7714              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7715     (gnus-summary-position-point))
7716    ;; If not, we try the first unread, if that is wanted.
7717    ((and subject
7718          gnus-auto-select-same
7719          (gnus-summary-first-unread-article))
7720     (gnus-summary-position-point)
7721     (gnus-message 6 "Wrapped"))
7722    ;; Try to get next/previous article not displayed in this group.
7723    ((and gnus-auto-extend-newsgroup
7724          (not unread) (not subject))
7725     (gnus-summary-goto-article
7726      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7727      nil (count-lines (point-min) (point))))
7728    ;; Go to next/previous group.
7729    (t
7730     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7731       (gnus-summary-jump-to-group gnus-newsgroup-name))
7732     (let ((cmd (if (featurep 'xemacs)
7733                    last-command-char
7734                  last-command-event))
7735           (point
7736            (with-current-buffer gnus-group-buffer
7737              (point)))
7738           (group
7739            (if (eq gnus-keep-same-level 'best)
7740                (gnus-summary-best-group gnus-newsgroup-name)
7741              (gnus-summary-search-group backward gnus-keep-same-level))))
7742       ;; Select next unread newsgroup automagically.
7743       (cond
7744        ((or (not gnus-auto-select-next)
7745             (not cmd))
7746         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7747        ((or (eq gnus-auto-select-next 'quietly)
7748             (and (eq gnus-auto-select-next 'slightly-quietly)
7749                  push)
7750             (and (eq gnus-auto-select-next 'almost-quietly)
7751                  (gnus-summary-last-article-p)))
7752         ;; Select quietly.
7753         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7754             (gnus-summary-exit)
7755           (gnus-message 7 "No more%s articles (%s)..."
7756                         (if unread " unread" "")
7757                         (if group (concat "selecting " group)
7758                           "exiting"))
7759           (gnus-summary-next-group nil group backward)))
7760        (t
7761         (when (gnus-key-press-event-p last-input-event)
7762           (gnus-summary-walk-group-buffer
7763            gnus-newsgroup-name cmd unread backward point))))))))
7764
7765 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7766   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7767                       (?\C-p (gnus-group-prev-unread-group 1))))
7768         (cursor-in-echo-area t)
7769         keve key group ended prompt)
7770     (with-current-buffer gnus-group-buffer
7771       (goto-char start)
7772       (setq group
7773             (if (eq gnus-keep-same-level 'best)
7774                 (gnus-summary-best-group gnus-newsgroup-name)
7775               (gnus-summary-search-group backward gnus-keep-same-level))))
7776     (while (not ended)
7777       (setq prompt
7778             (format
7779              "No more%s articles%s " (if unread " unread" "")
7780              (if (and group
7781                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7782                  (format " (Type %s for %s [%s])"
7783                          (single-key-description cmd)
7784                          (gnus-group-decoded-name group)
7785                          (gnus-group-unread group))
7786                (format " (Type %s to exit %s)"
7787                        (single-key-description cmd)
7788                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7789       ;; Confirm auto selection.
7790       (setq key (car (setq keve (gnus-read-event-char prompt)))
7791             ended t)
7792       (cond
7793        ((assq key keystrokes)
7794         (let ((obuf (current-buffer)))
7795           (switch-to-buffer gnus-group-buffer)
7796           (when group
7797             (gnus-group-jump-to-group group))
7798           (eval (cadr (assq key keystrokes)))
7799           (setq group (gnus-group-group-name))
7800           (switch-to-buffer obuf))
7801         (setq ended nil))
7802        ((equal key cmd)
7803         (if (or (not group)
7804                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7805             (gnus-summary-exit)
7806           (gnus-summary-next-group nil group backward)))
7807        (t
7808         (push (cdr keve) unread-command-events))))))
7809
7810 (defun gnus-summary-next-unread-article ()
7811   "Select unread article after current one."
7812   (interactive)
7813   (gnus-summary-next-article
7814    (or (not (eq gnus-summary-goto-unread 'never))
7815        (gnus-summary-last-article-p (gnus-summary-article-number)))
7816    (and gnus-auto-select-same
7817         (gnus-summary-article-subject))))
7818
7819 (defun gnus-summary-prev-article (&optional unread subject)
7820   "Select the article before the current one.
7821 If UNREAD is non-nil, only unread articles are selected."
7822   (interactive "P")
7823   (gnus-summary-next-article unread subject t))
7824
7825 (defun gnus-summary-prev-unread-article ()
7826   "Select unread article before current one."
7827   (interactive)
7828   (gnus-summary-prev-article
7829    (or (not (eq gnus-summary-goto-unread 'never))
7830        (gnus-summary-first-article-p (gnus-summary-article-number)))
7831    (and gnus-auto-select-same
7832         (gnus-summary-article-subject))))
7833
7834 (defun gnus-summary-next-page (&optional lines circular stop)
7835   "Show next page of the selected article.
7836 If at the end of the current article, select the next article.
7837 LINES says how many lines should be scrolled up.
7838
7839 If CIRCULAR is non-nil, go to the start of the article instead of
7840 selecting the next article when reaching the end of the current
7841 article.
7842
7843 If STOP is non-nil, just stop when reaching the end of the message.
7844
7845 Also see the variable `gnus-article-skip-boring'."
7846   (interactive "P")
7847   (setq gnus-summary-buffer (current-buffer))
7848   (gnus-set-global-variables)
7849   (let ((article (gnus-summary-article-number))
7850         (article-window (get-buffer-window gnus-article-buffer t))
7851         endp)
7852     ;; If the buffer is empty, we have no article.
7853     (unless article
7854       (error "No article to select"))
7855     (gnus-configure-windows 'article)
7856     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7857         (if (and (eq gnus-summary-goto-unread 'never)
7858                  (not (gnus-summary-last-article-p article)))
7859             (gnus-summary-next-article)
7860           (gnus-summary-next-unread-article))
7861       (if (or (null gnus-current-article)
7862               (null gnus-article-current)
7863               (/= article (cdr gnus-article-current))
7864               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7865           ;; Selected subject is different from current article's.
7866           (gnus-summary-display-article article)
7867         (when article-window
7868           (gnus-eval-in-buffer-window gnus-article-buffer
7869             (setq endp (or (gnus-article-next-page lines)
7870                            (gnus-article-only-boring-p))))
7871           (when endp
7872             (cond ((or stop gnus-summary-stop-at-end-of-message)
7873                    (gnus-message 3 "End of message"))
7874                   (circular
7875                    (gnus-summary-beginning-of-article))
7876                   (lines
7877                    (gnus-message 3 "End of message"))
7878                   ((null lines)
7879                    (if (and (eq gnus-summary-goto-unread 'never)
7880                             (not (gnus-summary-last-article-p article)))
7881                        (gnus-summary-next-article)
7882                      (gnus-summary-next-unread-article))))))))
7883     (gnus-summary-recenter)
7884     (gnus-summary-position-point)))
7885
7886 (defun gnus-summary-prev-page (&optional lines move)
7887   "Show previous page of selected article.
7888 Argument LINES specifies lines to be scrolled down.
7889 If MOVE, move to the previous unread article if point is at
7890 the beginning of the buffer."
7891   (interactive "P")
7892   (let ((article (gnus-summary-article-number))
7893         (article-window (get-buffer-window gnus-article-buffer t))
7894         endp)
7895     (gnus-configure-windows 'article)
7896     (if (or (null gnus-current-article)
7897             (null gnus-article-current)
7898             (/= article (cdr gnus-article-current))
7899             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7900         ;; Selected subject is different from current article's.
7901         (gnus-summary-display-article article)
7902       (gnus-summary-recenter)
7903       (when article-window
7904         (gnus-eval-in-buffer-window gnus-article-buffer
7905           (setq endp (gnus-article-prev-page lines)))
7906         (when (and move endp)
7907           (cond (lines
7908                  (gnus-message 3 "Beginning of message"))
7909                 ((null lines)
7910                  (if (and (eq gnus-summary-goto-unread 'never)
7911                           (not (gnus-summary-first-article-p article)))
7912                      (gnus-summary-prev-article)
7913                    (gnus-summary-prev-unread-article))))))))
7914   (gnus-summary-position-point))
7915
7916 (defun gnus-summary-prev-page-or-article (&optional lines)
7917   "Show previous page of selected article.
7918 Argument LINES specifies lines to be scrolled down.
7919 If at the beginning of the article, go to the next article."
7920   (interactive "P")
7921   (gnus-summary-prev-page lines t))
7922
7923 (defun gnus-summary-scroll-up (lines)
7924   "Scroll up (or down) one line current article.
7925 Argument LINES specifies lines to be scrolled up (or down if negative).
7926 If no article is selected, then the current article will be selected first."
7927   (interactive "p")
7928   (gnus-configure-windows 'article)
7929   (gnus-summary-show-thread)
7930   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7931     (gnus-eval-in-buffer-window gnus-article-buffer
7932       (cond ((> lines 0)
7933              (when (gnus-article-next-page lines)
7934                (gnus-message 3 "End of message")))
7935             ((< lines 0)
7936              (gnus-article-prev-page (- lines))))))
7937   (gnus-summary-recenter)
7938   (gnus-summary-position-point))
7939
7940 (defun gnus-summary-scroll-down (lines)
7941   "Scroll down (or up) one line current article.
7942 Argument LINES specifies lines to be scrolled down (or up if negative).
7943 If no article is selected, then the current article will be selected first."
7944   (interactive "p")
7945   (gnus-summary-scroll-up (- lines)))
7946
7947 (defun gnus-summary-next-same-subject ()
7948   "Select next article which has the same subject as current one."
7949   (interactive)
7950   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7951
7952 (defun gnus-summary-prev-same-subject ()
7953   "Select previous article which has the same subject as current one."
7954   (interactive)
7955   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7956
7957 (defun gnus-summary-next-unread-same-subject ()
7958   "Select next unread article which has the same subject as current one."
7959   (interactive)
7960   (gnus-summary-next-article t (gnus-summary-article-subject)))
7961
7962 (defun gnus-summary-prev-unread-same-subject ()
7963   "Select previous unread article which has the same subject as current one."
7964   (interactive)
7965   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7966
7967 (defun gnus-summary-first-unread-article ()
7968   "Select the first unread article.
7969 Return nil if there are no unread articles."
7970   (interactive)
7971   (prog1
7972       (when (gnus-summary-first-subject t)
7973         (gnus-summary-show-thread)
7974         (gnus-summary-first-subject t)
7975         (gnus-summary-display-article (gnus-summary-article-number)))
7976     (gnus-summary-position-point)))
7977
7978 (defun gnus-summary-first-unread-subject ()
7979   "Place the point on the subject line of the first unread article.
7980 Return nil if there are no unread articles."
7981   (interactive)
7982   (prog1
7983       (when (gnus-summary-first-subject t)
7984         (gnus-summary-show-thread)
7985         (gnus-summary-first-subject t))
7986     (gnus-summary-position-point)))
7987
7988 (defun gnus-summary-first-unseen-subject ()
7989   "Place the point on the subject line of the first unseen article.
7990 Return nil if there are no unseen articles."
7991   (interactive)
7992   (prog1
7993       (when (gnus-summary-first-subject nil nil t)
7994         (gnus-summary-show-thread)
7995         (gnus-summary-first-subject nil nil t))
7996     (gnus-summary-position-point)))
7997
7998 (defun gnus-summary-first-unseen-or-unread-subject ()
7999   "Place the point on the subject line of the first unseen and unread article.
8000 If all article have been seen, on the subject line of the first unread
8001 article."
8002   (interactive)
8003   (prog1
8004       (unless (when (gnus-summary-first-subject nil nil t)
8005                 (gnus-summary-show-thread)
8006                 (gnus-summary-first-subject nil nil t))
8007         (when (gnus-summary-first-subject t)
8008           (gnus-summary-show-thread)
8009           (gnus-summary-first-subject t)))
8010     (gnus-summary-position-point)))
8011
8012 (defun gnus-summary-first-article ()
8013   "Select the first article.
8014 Return nil if there are no articles."
8015   (interactive)
8016   (prog1
8017       (when (gnus-summary-first-subject)
8018         (gnus-summary-show-thread)
8019         (gnus-summary-first-subject)
8020         (gnus-summary-display-article (gnus-summary-article-number)))
8021     (gnus-summary-position-point)))
8022
8023 (defun gnus-summary-best-unread-article (&optional arg)
8024   "Select the unread article with the highest score.
8025 If given a prefix argument, select the next unread article that has a
8026 score higher than the default score."
8027   (interactive "P")
8028   (let ((article (if arg
8029                      (gnus-summary-better-unread-subject)
8030                    (gnus-summary-best-unread-subject))))
8031     (if article
8032         (gnus-summary-goto-article article)
8033       (error "No unread articles"))))
8034
8035 (defun gnus-summary-best-unread-subject ()
8036   "Select the unread subject with the highest score."
8037   (interactive)
8038   (let ((best -1000000)
8039         (data gnus-newsgroup-data)
8040         article score)
8041     (while data
8042       (and (gnus-data-unread-p (car data))
8043            (> (setq score
8044                     (gnus-summary-article-score (gnus-data-number (car data))))
8045               best)
8046            (setq best score
8047                  article (gnus-data-number (car data))))
8048       (setq data (cdr data)))
8049     (when article
8050       (gnus-summary-goto-subject article))
8051     (gnus-summary-position-point)
8052     article))
8053
8054 (defun gnus-summary-better-unread-subject ()
8055   "Select the first unread subject that has a score over the default score."
8056   (interactive)
8057   (let ((data gnus-newsgroup-data)
8058         article score)
8059     (while (and (setq article (gnus-data-number (car data)))
8060                 (or (gnus-data-read-p (car data))
8061                     (not (> (gnus-summary-article-score article)
8062                             gnus-summary-default-score))))
8063       (setq data (cdr data)))
8064     (when article
8065       (gnus-summary-goto-subject article))
8066     (gnus-summary-position-point)
8067     article))
8068
8069 (defun gnus-summary-last-subject ()
8070   "Go to the last displayed subject line in the group."
8071   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8072     (when article
8073       (gnus-summary-goto-subject article))))
8074
8075 (defun gnus-summary-goto-article (article &optional all-headers force)
8076   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8077 If ALL-HEADERS is non-nil, no header lines are hidden.
8078 If FORCE, go to the article even if it isn't displayed.  If FORCE
8079 is a number, it is the line the article is to be displayed on."
8080   (interactive
8081    (list
8082     (gnus-completing-read
8083      "Article number or Message-ID"
8084      (mapcar 'int-to-string gnus-newsgroup-limit))
8085     current-prefix-arg
8086     t))
8087   (prog1
8088       (if (and (stringp article)
8089                (string-match "@\\|%40" article))
8090           (gnus-summary-refer-article article)
8091         (when (stringp article)
8092           (setq article (string-to-number article)))
8093         (if (gnus-summary-goto-subject article force)
8094             (gnus-summary-display-article article all-headers)
8095           (gnus-message 4 "Couldn't go to article %s" article) nil))
8096     (gnus-summary-position-point)))
8097
8098 (defun gnus-summary-goto-last-article ()
8099   "Go to the previously read article."
8100   (interactive)
8101   (prog1
8102       (when gnus-last-article
8103         (gnus-summary-goto-article gnus-last-article nil t))
8104     (gnus-summary-position-point)))
8105
8106 (defun gnus-summary-pop-article (number)
8107   "Pop one article off the history and go to the previous.
8108 NUMBER articles will be popped off."
8109   (interactive "p")
8110   (let (to)
8111     (setq gnus-newsgroup-history
8112           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8113     (if to
8114         (gnus-summary-goto-article (car to) nil t)
8115       (error "Article history empty")))
8116   (gnus-summary-position-point))
8117
8118 ;; Summary commands and functions for limiting the summary buffer.
8119
8120 (defun gnus-summary-limit-to-articles (n)
8121   "Limit the summary buffer to the next N articles.
8122 If not given a prefix, use the process marked articles instead."
8123   (interactive "P")
8124   (prog1
8125       (let ((articles (gnus-summary-work-articles n)))
8126         (setq gnus-newsgroup-processable nil)
8127         (gnus-summary-limit articles))
8128     (gnus-summary-position-point)))
8129
8130 (defun gnus-summary-pop-limit (&optional total)
8131   "Restore the previous limit.
8132 If given a prefix, remove all limits."
8133   (interactive "P")
8134   (when total
8135     (setq gnus-newsgroup-limits
8136           (list (mapcar (lambda (h) (mail-header-number h))
8137                         gnus-newsgroup-headers))))
8138   (unless gnus-newsgroup-limits
8139     (error "No limit to pop"))
8140   (prog1
8141       (gnus-summary-limit nil 'pop)
8142     (gnus-summary-position-point)))
8143
8144 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8145   "Limit the summary buffer to articles that have subjects that match a regexp.
8146 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8147   (interactive
8148    (list (read-string (if current-prefix-arg
8149                           "Exclude subject (regexp): "
8150                         "Limit to subject (regexp): "))
8151          nil current-prefix-arg))
8152   (unless header
8153     (setq header "subject"))
8154   (when (not (equal "" subject))
8155     (prog1
8156         (let ((articles (gnus-summary-find-matching
8157                          (or header "subject") subject 'all nil nil
8158                          not-matching)))
8159           (unless articles
8160             (error "Found no matches for \"%s\"" subject))
8161           (gnus-summary-limit articles))
8162       (gnus-summary-position-point))))
8163
8164 (defun gnus-summary-limit-to-author (from &optional not-matching)
8165   "Limit the summary buffer to articles that have authors that match a regexp.
8166 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8167   (interactive
8168    (list (read-string (if current-prefix-arg
8169                           "Exclude author (regexp): "
8170                         "Limit to author (regexp): "))
8171          current-prefix-arg))
8172   (gnus-summary-limit-to-subject from "from" not-matching))
8173
8174 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8175   "Limit the summary buffer to articles with the given RECIPIENT.
8176
8177 If NOT-MATCHING, exclude RECIPIENT.
8178
8179 To and Cc headers are checked.  You need to include them in
8180 `nnmail-extra-headers'."
8181   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8182   (interactive
8183    (list (read-string (format "%s recipient (regexp): "
8184                               (if current-prefix-arg "Exclude" "Limit to")))
8185          current-prefix-arg))
8186   (when (not (equal "" recipient))
8187     (prog1 (let* ((to
8188                    (if (memq 'To nnmail-extra-headers)
8189                        (gnus-summary-find-matching
8190                         (cons 'extra 'To) recipient 'all nil nil
8191                         not-matching)
8192                      (gnus-message
8193                       1 "`To' isn't present in `nnmail-extra-headers'")
8194                      (sit-for 1)
8195                      nil))
8196                   (cc
8197                    (if (memq 'Cc nnmail-extra-headers)
8198                        (gnus-summary-find-matching
8199                         (cons 'extra 'Cc) recipient 'all nil nil
8200                         not-matching)
8201                      (gnus-message
8202                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8203                      (sit-for 1)
8204                      nil))
8205                   (articles
8206                    (if not-matching
8207                        ;; We need the numbers that are in both lists:
8208                        (mapcar (lambda (a)
8209                                  (and (memq a to) a))
8210                                cc)
8211                      (nconc to cc))))
8212              (unless articles
8213                (error "Found no matches for \"%s\"" recipient))
8214              (gnus-summary-limit articles))
8215       (gnus-summary-position-point))))
8216
8217 (defun gnus-summary-limit-to-address (address &optional not-matching)
8218   "Limit the summary buffer to articles with the given ADDRESS.
8219
8220 If NOT-MATCHING, exclude ADDRESS.
8221
8222 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8223 in `nnmail-extra-headers'."
8224   (interactive
8225    (list (read-string (format "%s address (regexp): "
8226                               (if current-prefix-arg "Exclude" "Limit to")))
8227          current-prefix-arg))
8228   (when (not (equal "" address))
8229     (prog1 (let* ((to
8230                    (if (memq 'To nnmail-extra-headers)
8231                        (gnus-summary-find-matching
8232                         (cons 'extra 'To) address 'all nil nil
8233                         not-matching)
8234                      (gnus-message
8235                       1 "`To' isn't present in `nnmail-extra-headers'")
8236                      (sit-for 1)
8237                      t))
8238                   (cc
8239                    (if (memq 'Cc nnmail-extra-headers)
8240                        (gnus-summary-find-matching
8241                         (cons 'extra 'Cc) address 'all nil nil
8242                         not-matching)
8243                      (gnus-message
8244                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8245                      (sit-for 1)
8246                      t))
8247                   (from
8248                    (gnus-summary-find-matching "from" address
8249                                                'all nil nil not-matching))
8250                   (articles
8251                    (if not-matching
8252                        ;; We need the numbers that are in all lists:
8253                        (if (eq cc t)
8254                            (if (eq to t)
8255                                from
8256                              (mapcar (lambda (a) (car (memq a from))) to))
8257                          (if (eq to t)
8258                              (mapcar (lambda (a) (car (memq a from))) cc)
8259                            (mapcar (lambda (a) (car (memq a from)))
8260                                    (mapcar (lambda (a) (car (memq a to)))
8261                                            cc))))
8262                      (nconc (if (eq to t) nil to)
8263                             (if (eq cc t) nil cc)
8264                             from))))
8265              (unless articles
8266                (error "Found no matches for \"%s\"" address))
8267              (gnus-summary-limit articles))
8268       (gnus-summary-position-point))))
8269
8270 (defun gnus-summary-limit-strange-charsets-predicate (header)
8271   (when (fboundp 'char-charset)
8272     (let ((string (concat (mail-header-subject header)
8273                           (mail-header-from header)))
8274           charset found)
8275       (dotimes (i (1- (length string)))
8276         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8277         (when (string-match "unicode\\|big\\|japanese" charset)
8278           (setq found t)))
8279       found)))
8280
8281 (defun gnus-summary-limit-to-predicate (predicate)
8282   "Limit to articles where PREDICATE returns non-nil.
8283 PREDICATE will be called with the header structures of the
8284 articles."
8285   (let ((articles nil)
8286         (case-fold-search t))
8287     (dolist (header gnus-newsgroup-headers)
8288       (when (funcall predicate header)
8289         (push (mail-header-number header) articles)))
8290     (gnus-summary-limit (nreverse articles))))
8291
8292 (defun gnus-summary-limit-to-age (age &optional younger-p)
8293   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8294 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8295 articles that are younger than AGE days."
8296   (interactive
8297    (let ((younger current-prefix-arg)
8298          (days-got nil)
8299          days)
8300      (while (not days-got)
8301        (setq days (if younger
8302                       (read-string "Limit to articles younger than (in days, older when negative): ")
8303                     (read-string
8304                      "Limit to articles older than (in days, younger when negative): ")))
8305        (when (> (length days) 0)
8306          (setq days (read days)))
8307        (if (numberp days)
8308            (progn
8309              (setq days-got t)
8310              (when (< days 0)
8311                (setq younger (not younger))
8312                (setq days (* days -1))))
8313          (message "Please enter a number.")
8314          (sleep-for 1)))
8315      (list days younger)))
8316   (prog1
8317       (let ((data gnus-newsgroup-data)
8318             (cutoff (days-to-time age))
8319             articles d date is-younger)
8320         (while (setq d (pop data))
8321           (when (and (vectorp (gnus-data-header d))
8322                      (setq date (mail-header-date (gnus-data-header d))))
8323             (setq is-younger (time-less-p
8324                               (time-since (gnus-date-get-time date))
8325                               cutoff))
8326             (when (if younger-p
8327                       is-younger
8328                     (not is-younger))
8329               (push (gnus-data-number d) articles))))
8330         (gnus-summary-limit (nreverse articles)))
8331     (gnus-summary-position-point)))
8332
8333 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8334   "Limit the summary buffer to articles that match an 'extra' header."
8335   (interactive
8336    (let ((header
8337           (intern
8338            (gnus-completing-read
8339             (if current-prefix-arg
8340                 "Exclude extra header"
8341               "Limit extra header")
8342             (mapcar 'symbol-name gnus-extra-headers)
8343             t nil nil
8344             (symbol-name (car gnus-extra-headers))))))
8345      (list header
8346            (read-string (format "%s header %s (regexp): "
8347                                 (if current-prefix-arg "Exclude" "Limit to")
8348                                 header))
8349            current-prefix-arg)))
8350   (when (not (equal "" regexp))
8351     (prog1
8352         (let ((articles (gnus-summary-find-matching
8353                          (cons 'extra header) regexp 'all nil nil
8354                          not-matching)))
8355           (unless articles
8356             (error "Found no matches for \"%s\"" regexp))
8357           (gnus-summary-limit articles))
8358       (gnus-summary-position-point))))
8359
8360 (defun gnus-summary-limit-to-display-predicate ()
8361   "Limit the summary buffer to the predicated in the `display' group parameter."
8362   (interactive)
8363   (unless gnus-newsgroup-display
8364     (error "There is no `display' group parameter"))
8365   (let (articles)
8366     (dolist (gnus-number gnus-newsgroup-articles)
8367       (when (funcall gnus-newsgroup-display)
8368         (push gnus-number articles)))
8369     (gnus-summary-limit articles))
8370   (gnus-summary-position-point))
8371
8372 (defun gnus-summary-limit-to-unread (&optional all)
8373   "Limit the summary buffer to articles that are not marked as read.
8374 If ALL is non-nil, limit strictly to unread articles."
8375   (interactive "P")
8376   (if all
8377       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8378     (gnus-summary-limit-to-marks
8379      ;; Concat all the marks that say that an article is read and have
8380      ;; those removed.
8381      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8382            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8383            gnus-low-score-mark gnus-expirable-mark
8384            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8385            gnus-duplicate-mark)
8386      'reverse)))
8387
8388 (defun gnus-summary-limit-to-headers (match &optional reverse)
8389   "Limit the summary buffer to articles that have headers that match MATCH.
8390 If REVERSE (the prefix), limit to articles that don't match."
8391   (interactive "sMatch headers (regexp): \nP")
8392   (gnus-summary-limit-to-bodies match reverse t))
8393
8394 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8395   "Limit the summary buffer to articles that have bodies that match MATCH.
8396 If REVERSE (the prefix), limit to articles that don't match."
8397   (interactive "sMatch body (regexp): \nP")
8398   (let ((articles nil)
8399         (gnus-select-article-hook nil)  ;Disable hook.
8400         (gnus-article-prepare-hook nil)
8401         (gnus-use-article-prefetch nil)
8402         (gnus-keep-backlog nil)
8403         (gnus-break-pages nil)
8404         (gnus-summary-display-arrow nil)
8405         (gnus-updated-mode-lines nil)
8406         (gnus-auto-center-summary nil)
8407         (gnus-display-mime-function nil))
8408     (dolist (data gnus-newsgroup-data)
8409       (let (gnus-mark-article-hook)
8410         (gnus-summary-select-article t t nil (gnus-data-number data)))
8411       (with-current-buffer gnus-article-buffer
8412         (article-goto-body)
8413         (let* ((case-fold-search t)
8414                (found (if headersp
8415                           (re-search-backward match nil t)
8416                         (re-search-forward match nil t))))
8417           (when (or (and found
8418                          (not reverse))
8419                     (and (not found)
8420                          reverse))
8421             (push (gnus-data-number data) articles)))))
8422     (if (not articles)
8423         (message "No messages matched")
8424       (gnus-summary-limit articles)))
8425   (gnus-summary-position-point))
8426
8427 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8428   "Limit the summary buffer to articles that aren't part on any thread.
8429 If THREADSP (the prefix), limit to articles that are in threads."
8430   (interactive "P")
8431   (let ((articles nil)
8432         thread-articles
8433         threads)
8434     (dolist (thread gnus-newsgroup-threads)
8435       (if (stringp (car thread))
8436           (dolist (thread (cdr thread))
8437             (push thread threads))
8438         (push thread threads)))
8439     (dolist (thread threads)
8440       (setq thread-articles (gnus-articles-in-thread thread))
8441       (when (or (and threadsp
8442                      (> (length thread-articles) 1))
8443                 (and (not threadsp)
8444                      (= (length thread-articles) 1)))
8445         (setq articles (nconc thread-articles articles))))
8446     (if (not articles)
8447         (message "No messages matched")
8448       (gnus-summary-limit articles))
8449     (gnus-summary-position-point)))
8450
8451 (defun gnus-summary-limit-to-replied (&optional unreplied)
8452   "Limit the summary buffer to replied articles.
8453 If UNREPLIED (the prefix), limit to unreplied articles."
8454   (interactive "P")
8455   (if unreplied
8456       (gnus-summary-limit
8457        (gnus-set-difference gnus-newsgroup-articles
8458         gnus-newsgroup-replied))
8459     (gnus-summary-limit gnus-newsgroup-replied))
8460   (gnus-summary-position-point))
8461
8462 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8463   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8464 If REVERSE, limit the summary buffer to articles that are marked
8465 with MARKS.  MARKS can either be a string of marks or a list of marks.
8466 Returns how many articles were removed."
8467   (interactive "sMarks: ")
8468   (gnus-summary-limit-to-marks marks t))
8469
8470 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8471   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8472 If REVERSE (the prefix), limit the summary buffer to articles that are
8473 not marked with MARKS.  MARKS can either be a string of marks or a
8474 list of marks.
8475 Returns how many articles were removed."
8476   (interactive "sMarks: \nP")
8477   (prog1
8478       (let ((data gnus-newsgroup-data)
8479             (marks (if (listp marks) marks
8480                      (append marks nil))) ; Transform to list.
8481             articles)
8482         (while data
8483           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8484                   (memq (gnus-data-mark (car data)) marks))
8485             (push (gnus-data-number (car data)) articles))
8486           (setq data (cdr data)))
8487         (gnus-summary-limit articles))
8488     (gnus-summary-position-point)))
8489
8490 (defun gnus-summary-limit-to-score (score)
8491   "Limit to articles with score at or above SCORE."
8492   (interactive "NLimit to articles with score of at least: ")
8493   (let ((data gnus-newsgroup-data)
8494         articles)
8495     (while data
8496       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8497                 score)
8498         (push (gnus-data-number (car data)) articles))
8499       (setq data (cdr data)))
8500     (prog1
8501         (gnus-summary-limit articles)
8502       (gnus-summary-position-point))))
8503
8504 (defun gnus-summary-limit-to-unseen ()
8505   "Limit to unseen articles."
8506   (interactive)
8507   (prog1
8508       (gnus-summary-limit gnus-newsgroup-unseen)
8509     (gnus-summary-position-point)))
8510
8511 (defun gnus-summary-limit-include-thread (id)
8512   "Display all the hidden articles that is in the thread with ID in it.
8513 When called interactively, ID is the Message-ID of the current
8514 article."
8515   (interactive (list (mail-header-id (gnus-summary-article-header))))
8516   (let ((articles (gnus-articles-in-thread
8517                    (gnus-id-to-thread (gnus-root-id id))))
8518         ;;we REALLY want the whole thread---this prevents cut-threads
8519         ;;from removing the thread we want to include.
8520         (gnus-fetch-old-headers nil)
8521         (gnus-build-sparse-threads nil))
8522     (prog1
8523         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8524       (gnus-summary-limit-include-matching-articles
8525        "subject"
8526        (regexp-quote (gnus-simplify-subject-re
8527                       (mail-header-subject (gnus-id-to-header id)))))
8528       (gnus-summary-position-point))))
8529
8530 (defun gnus-summary-limit-include-matching-articles (header regexp)
8531   "Display all the hidden articles that have HEADERs that match REGEXP."
8532   (interactive (list (read-string "Match on header: ")
8533                      (read-string "Regexp: ")))
8534   (let ((articles (gnus-find-matching-articles header regexp)))
8535     (prog1
8536         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8537       (gnus-summary-position-point))))
8538
8539 (defun gnus-summary-insert-dormant-articles ()
8540   "Insert all the dormant articles for this group into the current buffer."
8541   (interactive)
8542   (let ((gnus-verbose (max 6 gnus-verbose)))
8543     (if (not gnus-newsgroup-dormant)
8544         (gnus-message 3 "No dormant articles for this group")
8545       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8546
8547 (defun gnus-summary-insert-ticked-articles ()
8548   "Insert ticked articles for this group into the current buffer."
8549   (interactive)
8550   (let ((gnus-verbose (max 6 gnus-verbose)))
8551     (if (not gnus-newsgroup-marked)
8552         (gnus-message 3 "No ticked articles for this group")
8553       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8554
8555 (defun gnus-summary-limit-include-dormant ()
8556   "Display all the hidden articles that are marked as dormant.
8557 Note that this command only works on a subset of the articles currently
8558 fetched for this group."
8559   (interactive)
8560   (unless gnus-newsgroup-dormant
8561     (error "There are no dormant articles in this group"))
8562   (prog1
8563       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8564     (gnus-summary-position-point)))
8565
8566 (defun gnus-summary-include-articles (articles)
8567   "Fetch the headers for ARTICLES and then display the summary lines."
8568   (let ((gnus-inhibit-demon t)
8569         (gnus-agent nil)
8570         (gnus-read-all-available-headers t))
8571     (setq gnus-newsgroup-headers
8572           (gnus-merge
8573            'list gnus-newsgroup-headers
8574            (gnus-fetch-headers articles nil t)
8575            'gnus-article-sort-by-number))
8576     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8577
8578 (defun gnus-summary-limit-exclude-dormant ()
8579   "Hide all dormant articles."
8580   (interactive)
8581   (prog1
8582       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8583     (gnus-summary-position-point)))
8584
8585 (defun gnus-summary-limit-exclude-childless-dormant ()
8586   "Hide all dormant articles that have no children."
8587   (interactive)
8588   (let ((data (gnus-data-list t))
8589         articles d children)
8590     ;; Find all articles that are either not dormant or have
8591     ;; children.
8592     (while (setq d (pop data))
8593       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8594                 (and (setq children
8595                            (gnus-article-children (gnus-data-number d)))
8596                      (let (found)
8597                        (while children
8598                          (when (memq (car children) articles)
8599                            (setq children nil
8600                                  found t))
8601                          (pop children))
8602                        found)))
8603         (push (gnus-data-number d) articles)))
8604     ;; Do the limiting.
8605     (prog1
8606         (gnus-summary-limit articles)
8607       (gnus-summary-position-point))))
8608
8609 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8610   "Mark all unread excluded articles as read.
8611 If ALL, mark even excluded ticked and dormants as read."
8612   (interactive "P")
8613   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8614   (let ((articles (gnus-sorted-ndifference
8615                    (sort
8616                     (mapcar (lambda (h) (mail-header-number h))
8617                             gnus-newsgroup-headers)
8618                     '<)
8619                    gnus-newsgroup-limit))
8620         article)
8621     (setq gnus-newsgroup-unreads
8622           (gnus-sorted-intersection gnus-newsgroup-unreads
8623                                     gnus-newsgroup-limit))
8624     (if all
8625         (setq gnus-newsgroup-dormant nil
8626               gnus-newsgroup-marked nil
8627               gnus-newsgroup-reads
8628               (nconc
8629                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8630                gnus-newsgroup-reads))
8631       (while (setq article (pop articles))
8632         (unless (or (memq article gnus-newsgroup-dormant)
8633                     (memq article gnus-newsgroup-marked))
8634           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8635
8636 (defun gnus-summary-limit (articles &optional pop)
8637   (if pop
8638       ;; We pop the previous limit off the stack and use that.
8639       (setq articles (car gnus-newsgroup-limits)
8640             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8641     ;; We use the new limit, so we push the old limit on the stack.
8642     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8643   ;; Set the limit.
8644   (setq gnus-newsgroup-limit articles)
8645   (let ((total (length gnus-newsgroup-data))
8646         (data (gnus-data-find-list (gnus-summary-article-number)))
8647         (gnus-summary-mark-below nil)   ; Inhibit this.
8648         found)
8649     ;; This will do all the work of generating the new summary buffer
8650     ;; according to the new limit.
8651     (gnus-summary-prepare)
8652     ;; Hide any threads, possibly.
8653     (gnus-summary-maybe-hide-threads)
8654     ;; Try to return to the article you were at, or one in the
8655     ;; neighborhood.
8656     (when data
8657       ;; We try to find some article after the current one.
8658       (while data
8659         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8660           (setq data nil
8661                 found t))
8662         (setq data (cdr data))))
8663     (unless found
8664       ;; If there is no data, that means that we were after the last
8665       ;; article.  The same goes when we can't find any articles
8666       ;; after the current one.
8667       (goto-char (point-max))
8668       (gnus-summary-find-prev))
8669     (gnus-set-mode-line 'summary)
8670     ;; We return how many articles were removed from the summary
8671     ;; buffer as a result of the new limit.
8672     (- total (length gnus-newsgroup-data))))
8673
8674 (defsubst gnus-invisible-cut-children (threads)
8675   (let ((num 0))
8676     (while threads
8677       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8678         (incf num))
8679       (pop threads))
8680     (< num 2)))
8681
8682 (defsubst gnus-cut-thread (thread)
8683   "Go forwards in the thread until we find an article that we want to display."
8684   (when (or (eq gnus-fetch-old-headers 'some)
8685             (eq gnus-fetch-old-headers 'invisible)
8686             (numberp gnus-fetch-old-headers)
8687             (eq gnus-build-sparse-threads 'some)
8688             (eq gnus-build-sparse-threads 'more))
8689     ;; Deal with old-fetched headers and sparse threads.
8690     (while (and
8691             thread
8692             (or
8693              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8694              (gnus-summary-article-ancient-p
8695               (mail-header-number (car thread))))
8696             (if (or (<= (length (cdr thread)) 1)
8697                     (eq gnus-fetch-old-headers 'invisible))
8698                 (setq gnus-newsgroup-limit
8699                       (delq (mail-header-number (car thread))
8700                             gnus-newsgroup-limit)
8701                       thread (cadr thread))
8702               (when (gnus-invisible-cut-children (cdr thread))
8703                 (let ((th (cdr thread)))
8704                   (while th
8705                     (if (memq (mail-header-number (caar th))
8706                               gnus-newsgroup-limit)
8707                         (setq thread (car th)
8708                               th nil)
8709                       (setq th (cdr th))))))))))
8710   thread)
8711
8712 (defun gnus-cut-threads (threads)
8713   "Cut off all uninteresting articles from the beginning of THREADS."
8714   (when (or (eq gnus-fetch-old-headers 'some)
8715             (eq gnus-fetch-old-headers 'invisible)
8716             (numberp gnus-fetch-old-headers)
8717             (eq gnus-build-sparse-threads 'some)
8718             (eq gnus-build-sparse-threads 'more))
8719     (let ((th threads))
8720       (while th
8721         (setcar th (gnus-cut-thread (car th)))
8722         (setq th (cdr th)))))
8723   ;; Remove nixed out threads.
8724   (delq nil threads))
8725
8726 (defun gnus-summary-initial-limit (&optional show-if-empty)
8727   "Figure out what the initial limit is supposed to be on group entry.
8728 This entails weeding out unwanted dormants, low-scored articles,
8729 fetch-old-headers verbiage, and so on."
8730   ;; Most groups have nothing to remove.
8731   (unless (or gnus-inhibit-limiting
8732               (and (null gnus-newsgroup-dormant)
8733                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8734                    (not (eq gnus-fetch-old-headers 'some))
8735                    (not (numberp gnus-fetch-old-headers))
8736                    (not (eq gnus-fetch-old-headers 'invisible))
8737                    (null gnus-summary-expunge-below)
8738                    (not (eq gnus-build-sparse-threads 'some))
8739                    (not (eq gnus-build-sparse-threads 'more))
8740                    (null gnus-thread-expunge-below)))
8741     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8742     (setq gnus-newsgroup-limit nil)
8743     (mapatoms
8744      (lambda (node)
8745        (unless (car (symbol-value node))
8746          ;; These threads have no parents -- they are roots.
8747          (let ((nodes (cdr (symbol-value node)))
8748                thread)
8749            (while nodes
8750              (if (and gnus-thread-expunge-below
8751                       (< (gnus-thread-total-score (car nodes))
8752                          gnus-thread-expunge-below))
8753                  (gnus-expunge-thread (pop nodes))
8754                (setq thread (pop nodes))
8755                (gnus-summary-limit-children thread))))))
8756      gnus-newsgroup-dependencies)
8757     ;; If this limitation resulted in an empty group, we might
8758     ;; pop the previous limit and use it instead.
8759     (when (and (not gnus-newsgroup-limit)
8760                show-if-empty)
8761       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8762     gnus-newsgroup-limit))
8763
8764 (defun gnus-summary-limit-children (thread)
8765   "Return 1 if this subthread is visible and 0 if it is not."
8766   ;; First we get the number of visible children to this thread.  This
8767   ;; is done by recursing down the thread using this function, so this
8768   ;; will really go down to a leaf article first, before slowly
8769   ;; working its way up towards the root.
8770   (when thread
8771     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8772            (children
8773            (if (cdr thread)
8774                (apply '+ (mapcar 'gnus-summary-limit-children
8775                                  (cdr thread)))
8776              0))
8777            (number (mail-header-number (car thread)))
8778            score)
8779       (if (and
8780            (not (memq number gnus-newsgroup-marked))
8781            (or
8782             ;; If this article is dormant and has absolutely no visible
8783             ;; children, then this article isn't visible.
8784             (and (memq number gnus-newsgroup-dormant)
8785                  (zerop children))
8786             ;; If this is "fetch-old-headered" and there is no
8787             ;; visible children, then we don't want this article.
8788             (and (or (eq gnus-fetch-old-headers 'some)
8789                      (numberp gnus-fetch-old-headers))
8790                  (gnus-summary-article-ancient-p number)
8791                  (zerop children))
8792             ;; If this is "fetch-old-headered" and `invisible', then
8793             ;; we don't want this article.
8794             (and (eq gnus-fetch-old-headers 'invisible)
8795                  (gnus-summary-article-ancient-p number))
8796             ;; If this is a sparsely inserted article with no children,
8797             ;; we don't want it.
8798             (and (eq gnus-build-sparse-threads 'some)
8799                  (gnus-summary-article-sparse-p number)
8800                  (zerop children))
8801             ;; If we use expunging, and this article is really
8802             ;; low-scored, then we don't want this article.
8803             (when (and gnus-summary-expunge-below
8804                        (< (setq score
8805                                 (or (cdr (assq number gnus-newsgroup-scored))
8806                                     gnus-summary-default-score))
8807                           gnus-summary-expunge-below))
8808               ;; We increase the expunge-tally here, but that has
8809               ;; nothing to do with the limits, really.
8810               (incf gnus-newsgroup-expunged-tally)
8811               ;; We also mark as read here, if that's wanted.
8812               (when (and gnus-summary-mark-below
8813                          (< score gnus-summary-mark-below))
8814                 (setq gnus-newsgroup-unreads
8815                       (delq number gnus-newsgroup-unreads))
8816                 (if gnus-newsgroup-auto-expire
8817                     (push number gnus-newsgroup-expirable)
8818                   (push (cons number gnus-low-score-mark)
8819                         gnus-newsgroup-reads)))
8820               t)
8821             ;; Do the `display' group parameter.
8822             (and gnus-newsgroup-display
8823                  (let ((gnus-number number))
8824                    (not (funcall gnus-newsgroup-display))))))
8825           ;; Nope, invisible article.
8826           0
8827         ;; Ok, this article is to be visible, so we add it to the limit
8828         ;; and return 1.
8829         (push number gnus-newsgroup-limit)
8830         1))))
8831
8832 (defun gnus-expunge-thread (thread)
8833   "Mark all articles in THREAD as read."
8834   (let* ((number (mail-header-number (car thread))))
8835     (incf gnus-newsgroup-expunged-tally)
8836     ;; We also mark as read here, if that's wanted.
8837     (setq gnus-newsgroup-unreads
8838           (delq number gnus-newsgroup-unreads))
8839     (if gnus-newsgroup-auto-expire
8840         (push number gnus-newsgroup-expirable)
8841       (push (cons number gnus-low-score-mark)
8842             gnus-newsgroup-reads)))
8843   ;; Go recursively through all subthreads.
8844   (mapcar 'gnus-expunge-thread (cdr thread)))
8845
8846 ;; Summary article oriented commands
8847
8848 (defun gnus-summary-refer-parent-article (n)
8849   "Refer parent article N times.
8850 If N is negative, go to ancestor -N instead.
8851 The difference between N and the number of articles fetched is returned."
8852   (interactive "p")
8853   (let ((skip 1)
8854         error header ref)
8855     (when (not (natnump n))
8856       (setq skip (abs n)
8857             n 1))
8858     (while (and (> n 0)
8859                 (not error))
8860       (setq header (gnus-summary-article-header))
8861       (if (and (eq (mail-header-number header)
8862                    (cdr gnus-article-current))
8863                (equal gnus-newsgroup-name
8864                       (car gnus-article-current)))
8865           ;; If we try to find the parent of the currently
8866           ;; displayed article, then we take a look at the actual
8867           ;; References header, since this is slightly more
8868           ;; reliable than the References field we got from the
8869           ;; server.
8870           (with-current-buffer gnus-original-article-buffer
8871             (nnheader-narrow-to-headers)
8872             (unless (setq ref (message-fetch-field "references"))
8873               (when (setq ref (message-fetch-field "in-reply-to"))
8874                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8875             (widen))
8876         (setq ref
8877               ;; It's not the current article, so we take a bet on
8878               ;; the value we got from the server.
8879               (mail-header-references header)))
8880       (if (and ref
8881                (not (equal ref "")))
8882           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8883             (gnus-message 1 "Couldn't find parent"))
8884         (gnus-message 1 "No references in article %d"
8885                       (gnus-summary-article-number))
8886         (setq error t))
8887       (decf n))
8888     (gnus-summary-position-point)
8889     n))
8890
8891 (defun gnus-summary-refer-references ()
8892   "Fetch all articles mentioned in the References header.
8893 Return the number of articles fetched."
8894   (interactive)
8895   (let ((ref (mail-header-references (gnus-summary-article-header)))
8896         (current (gnus-summary-article-number))
8897         (n 0))
8898     (if (or (not ref)
8899             (equal ref ""))
8900         (error "No References in the current article")
8901       ;; For each Message-ID in the References header...
8902       (while (string-match "<[^>]*>" ref)
8903         (incf n)
8904         ;; ... fetch that article.
8905         (gnus-summary-refer-article
8906          (prog1 (match-string 0 ref)
8907            (setq ref (substring ref (match-end 0))))))
8908       (gnus-summary-goto-subject current)
8909       (gnus-summary-position-point)
8910       n)))
8911
8912 (defun gnus-summary-refer-thread (&optional limit)
8913   "Fetch all articles in the current thread.
8914 If no backend-specific 'request-thread function is available
8915 fetch LIMIT (the numerical prefix) old headers. If LIMIT is nil
8916 fetch what's specified by the `gnus-refer-thread-limit'
8917 variable."
8918   (interactive "P")
8919   (gnus-warp-to-article)
8920   (let* ((header (gnus-summary-article-header))
8921          (id (mail-header-id header))
8922          (gnus-inhibit-demon t)
8923          (gnus-summary-ignore-duplicates t)
8924          (gnus-read-all-available-headers t)
8925          (limit (if limit (prefix-numeric-value limit)
8926                   gnus-refer-thread-limit)))
8927     (setq gnus-newsgroup-headers
8928           (gnus-merge
8929            'list gnus-newsgroup-headers
8930            (if (gnus-check-backend-function
8931                 'request-thread gnus-newsgroup-name)
8932                (gnus-request-thread header)
8933              (let* ((last (if (numberp limit)
8934                               (min (+ (mail-header-number header)
8935                                       limit)
8936                                    gnus-newsgroup-highest)
8937                             gnus-newsgroup-highest))
8938                     (subject (gnus-simplify-subject
8939                               (mail-header-subject header)))
8940                     (refs (split-string (or (mail-header-references header)
8941                                             "")))
8942                     (gnus-parse-headers-hook
8943                      (lambda () (goto-char (point-min))
8944                        (keep-lines
8945                         (regexp-opt (append refs (list id subject)))))))
8946                (gnus-fetch-headers (list last) (if (numberp limit)
8947                                                    (* 2 limit) limit) t)))
8948            'gnus-article-sort-by-number))
8949     (gnus-summary-limit-include-thread id)))
8950
8951 (defun gnus-summary-refer-article (message-id)
8952   "Fetch an article specified by MESSAGE-ID."
8953   (interactive "sMessage-ID: ")
8954   (when (and (stringp message-id)
8955              (not (zerop (length message-id))))
8956     (setq message-id (gnus-replace-in-string message-id " " ""))
8957     ;; Construct the correct Message-ID if necessary.
8958     ;; Suggested by tale@pawl.rpi.edu.
8959     (unless (string-match "^<" message-id)
8960       (setq message-id (concat "<" message-id)))
8961     (unless (string-match ">$" message-id)
8962       (setq message-id (concat message-id ">")))
8963     ;; People often post MIDs from URLs, so unhex it:
8964     (unless (string-match "@" message-id)
8965       (setq message-id (gnus-url-unhex-string message-id)))
8966     (let* ((header (gnus-id-to-header message-id))
8967            (sparse (and header
8968                         (gnus-summary-article-sparse-p
8969                          (mail-header-number header))
8970                         (memq (mail-header-number header)
8971                               gnus-newsgroup-limit)))
8972            number)
8973       (cond
8974        ;; If the article is present in the buffer we just go to it.
8975        ((and header
8976              (or (not (gnus-summary-article-sparse-p
8977                        (mail-header-number header)))
8978                  sparse))
8979         (prog1
8980             (gnus-summary-goto-article
8981              (mail-header-number header) nil t)
8982           (when sparse
8983             (gnus-summary-update-article (mail-header-number header)))))
8984        (t
8985         ;; We fetch the article.
8986         (catch 'found
8987           (dolist (gnus-override-method (gnus-refer-article-methods))
8988             (when (and (gnus-check-server gnus-override-method)
8989                        ;; Fetch the header,
8990                        (setq number (gnus-summary-insert-subject message-id)))
8991               ;; and display the article.
8992               (gnus-summary-select-article nil nil nil number)
8993               (throw 'found t)))
8994           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8995
8996 (defun gnus-refer-article-methods ()
8997   "Return a list of referable methods."
8998   (cond
8999    ;; No method, so we default to current and native.
9000    ((null gnus-refer-article-method)
9001     (list gnus-current-select-method gnus-select-method))
9002    ;; Current.
9003    ((eq 'current gnus-refer-article-method)
9004     (list gnus-current-select-method))
9005    ;; List of select methods.
9006    ((not (and (symbolp (car gnus-refer-article-method))
9007               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9008     (let (out)
9009       (dolist (method gnus-refer-article-method)
9010         (push (if (eq 'current method)
9011                   gnus-current-select-method
9012                 method)
9013               out))
9014       (nreverse out)))
9015    ;; One single select method.
9016    (t
9017     (list gnus-refer-article-method))))
9018
9019 (defun gnus-summary-edit-parameters ()
9020   "Edit the group parameters of the current group."
9021   (interactive)
9022   (gnus-group-edit-group gnus-newsgroup-name 'params))
9023
9024 (defun gnus-summary-customize-parameters ()
9025   "Customize the group parameters of the current group."
9026   (interactive)
9027   (gnus-group-customize gnus-newsgroup-name))
9028
9029 (defun gnus-summary-enter-digest-group (&optional force)
9030   "Enter an nndoc group based on the current article.
9031 If FORCE, force a digest interpretation.  If not, try to guess
9032 what the document format is.
9033
9034 To control what happens when you exit the group, see the
9035 `gnus-auto-select-on-ephemeral-exit' variable."
9036   (interactive "P")
9037   (let ((conf gnus-current-window-configuration))
9038     (save-window-excursion
9039       (save-excursion
9040         (let (gnus-article-prepare-hook
9041               gnus-display-mime-function
9042               gnus-break-pages)
9043           (gnus-summary-select-article))))
9044     (setq gnus-current-window-configuration conf)
9045     (let* ((name (format "%s-%d"
9046                          (gnus-group-prefixed-name
9047                           gnus-newsgroup-name (list 'nndoc ""))
9048                          (with-current-buffer gnus-summary-buffer
9049                            gnus-current-article)))
9050            (ogroup gnus-newsgroup-name)
9051            (params (append (gnus-info-params (gnus-get-info ogroup))
9052                            (list (cons 'to-group ogroup))
9053                            (list (cons 'parent-group ogroup))
9054                            (list (cons 'save-article-group ogroup))))
9055            (case-fold-search t)
9056            (buf (current-buffer))
9057            dig to-address)
9058       (with-current-buffer gnus-original-article-buffer
9059         ;; Have the digest group inherit the main mail address of
9060         ;; the parent article.
9061         (when (setq to-address (or (gnus-fetch-field "reply-to")
9062                                    (gnus-fetch-field "from")))
9063           (setq params
9064                 (append
9065                  (list (cons 'to-address
9066                              (funcall gnus-decode-encoded-address-function
9067                                       to-address))))))
9068         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9069         (insert-buffer-substring gnus-original-article-buffer)
9070         ;; Remove lines that may lead nndoc to misinterpret the
9071         ;; document type.
9072         (narrow-to-region
9073          (goto-char (point-min))
9074          (or (search-forward "\n\n" nil t) (point)))
9075         (goto-char (point-min))
9076         (delete-matching-lines "^Path:\\|^From ")
9077         (widen))
9078       (unwind-protect
9079           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9080                     (gnus-newsgroup-ephemeral-ignored-charsets
9081                      gnus-newsgroup-ignored-charsets))
9082                 (gnus-group-read-ephemeral-group
9083                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9084                               (nndoc-article-type
9085                                ,(if force 'mbox 'guess)))
9086                  t nil nil nil
9087                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9088                                                         "ADAPT")))))
9089               ;; Make all postings to this group go to the parent group.
9090               (nconc (gnus-info-params (gnus-get-info name))
9091                      params)
9092             ;; Couldn't select this doc group.
9093             (switch-to-buffer buf)
9094             (gnus-set-global-variables)
9095             (gnus-configure-windows 'summary)
9096             (gnus-message 3 "Article couldn't be entered?"))
9097         (kill-buffer dig)))))
9098
9099 (defun gnus-summary-read-document (n)
9100   "Open a new group based on the current article(s).
9101 This will allow you to read digests and other similar
9102 documents as newsgroups.
9103 Obeys the standard process/prefix convention."
9104   (interactive "P")
9105   (let* ((ogroup gnus-newsgroup-name)
9106          (params (append (gnus-info-params (gnus-get-info ogroup))
9107                          (list (cons 'to-group ogroup))))
9108          group egroup groups vgroup)
9109     (dolist (article (gnus-summary-work-articles n))
9110       (setq group (format "%s-%d" gnus-newsgroup-name article))
9111       (gnus-summary-remove-process-mark article)
9112       (when (gnus-summary-display-article article)
9113         (save-excursion ;;What for?
9114           (with-temp-buffer
9115             (insert-buffer-substring gnus-original-article-buffer)
9116             ;; Remove some headers that may lead nndoc to make
9117             ;; the wrong guess.
9118             (message-narrow-to-head)
9119             (goto-char (point-min))
9120             (delete-matching-lines "^Path:\\|^From ")
9121             (widen)
9122             (if (setq egroup
9123                       (gnus-group-read-ephemeral-group
9124                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9125                                      (nndoc-article-type guess))
9126                        t nil t))
9127                 (progn
9128                   ;; Make all postings to this group go to the parent group.
9129                   (nconc (gnus-info-params (gnus-get-info egroup))
9130                          params)
9131                   (push egroup groups))
9132               ;; Couldn't select this doc group.
9133               (gnus-error 3 "Article couldn't be entered"))))))
9134     ;; Now we have selected all the documents.
9135     (cond
9136      ((not groups)
9137       (error "None of the articles could be interpreted as documents"))
9138      ((gnus-group-read-ephemeral-group
9139        (setq vgroup (format
9140                      "nnvirtual:%s-%s" gnus-newsgroup-name
9141                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9142        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9143        t
9144        (cons (current-buffer) 'summary)))
9145      (t
9146       (error "Couldn't select virtual nndoc group")))))
9147
9148 (defun gnus-summary-widget-forward (arg)
9149   "Move point to the next field or button in the article.
9150 With optional ARG, move across that many fields."
9151   (interactive "p")
9152   (gnus-summary-select-article)
9153   (gnus-configure-windows 'article)
9154   (select-window (gnus-get-buffer-window gnus-article-buffer))
9155   (widget-forward arg))
9156
9157 (defun gnus-summary-isearch-article (&optional regexp-p)
9158   "Do incremental search forward on the current article.
9159 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9160   (interactive "P")
9161   (gnus-summary-select-article)
9162   (gnus-configure-windows 'article)
9163   (gnus-eval-in-buffer-window gnus-article-buffer
9164     (save-restriction
9165       (widen)
9166       (isearch-forward regexp-p))))
9167
9168 (defun gnus-summary-repeat-search-article-forward ()
9169   "Repeat the previous search forwards."
9170   (interactive)
9171   (unless gnus-last-search-regexp
9172     (error "No previous search"))
9173   (gnus-summary-search-article-forward gnus-last-search-regexp))
9174
9175 (defun gnus-summary-repeat-search-article-backward ()
9176   "Repeat the previous search backwards."
9177   (interactive)
9178   (unless gnus-last-search-regexp
9179     (error "No previous search"))
9180   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9181
9182 (defun gnus-summary-search-article-forward (regexp &optional backward)
9183   "Search for an article containing REGEXP forward.
9184 If BACKWARD, search backward instead."
9185   (interactive
9186    (list (read-string
9187           (format "Search article %s (regexp%s): "
9188                   (if current-prefix-arg "backward" "forward")
9189                   (if gnus-last-search-regexp
9190                       (concat ", default " gnus-last-search-regexp)
9191                     "")))
9192          current-prefix-arg))
9193   (if (string-equal regexp "")
9194       (setq regexp (or gnus-last-search-regexp ""))
9195     (setq gnus-last-search-regexp regexp)
9196     (setq gnus-article-before-search gnus-current-article))
9197   ;; Intentionally set gnus-last-article.
9198   (setq gnus-last-article gnus-article-before-search)
9199   (let ((gnus-last-article gnus-last-article))
9200     (if (gnus-summary-search-article regexp backward)
9201         (gnus-summary-show-thread)
9202       (signal 'search-failed (list regexp)))))
9203
9204 (defun gnus-summary-search-article-backward (regexp)
9205   "Search for an article containing REGEXP backward."
9206   (interactive
9207    (list (read-string
9208           (format "Search article backward (regexp%s): "
9209                   (if gnus-last-search-regexp
9210                       (concat ", default " gnus-last-search-regexp)
9211                     "")))))
9212   (gnus-summary-search-article-forward regexp 'backward))
9213
9214 (defun gnus-summary-search-article (regexp &optional backward)
9215   "Search for an article containing REGEXP.
9216 Optional argument BACKWARD means do search for backward.
9217 `gnus-select-article-hook' is not called during the search."
9218   ;; We have to require this here to make sure that the following
9219   ;; dynamic binding isn't shadowed by autoloading.
9220   (require 'gnus-async)
9221   (require 'gnus-art)
9222   (let ((gnus-select-article-hook nil)  ;Disable hook.
9223         (gnus-article-prepare-hook nil)
9224         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9225         (gnus-use-article-prefetch nil)
9226         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9227         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9228         (gnus-visual nil)
9229         (gnus-keep-backlog nil)
9230         (gnus-break-pages nil)
9231         (gnus-summary-display-arrow nil)
9232         (gnus-updated-mode-lines nil)
9233         (gnus-auto-center-summary nil)
9234         (sum (current-buffer))
9235         (gnus-display-mime-function nil)
9236         (found nil)
9237         point)
9238     (gnus-save-hidden-threads
9239       (gnus-summary-select-article)
9240       (set-buffer gnus-article-buffer)
9241       (goto-char (window-point (get-buffer-window (current-buffer))))
9242       (when backward
9243         (forward-line -1))
9244       (while (not found)
9245         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9246         (if (if backward
9247                 (re-search-backward regexp nil t)
9248               (re-search-forward regexp nil t))
9249             ;; We found the regexp.
9250             (progn
9251               (setq found 'found)
9252               (beginning-of-line)
9253               (set-window-start
9254                (get-buffer-window (current-buffer))
9255                (point))
9256               (forward-line 1)
9257               (set-window-point
9258                (get-buffer-window (current-buffer))
9259                (point))
9260               (set-buffer sum)
9261               (setq point (point)))
9262           ;; We didn't find it, so we go to the next article.
9263           (set-buffer sum)
9264           (setq found 'not)
9265           (while (eq found 'not)
9266             (if (not (if backward (gnus-summary-find-prev)
9267                        (gnus-summary-find-next)))
9268                 ;; No more articles.
9269                 (setq found t)
9270               ;; Select the next article and adjust point.
9271               (unless (gnus-summary-article-sparse-p
9272                        (gnus-summary-article-number))
9273                 (setq found nil)
9274                 (gnus-summary-select-article)
9275                 (set-buffer gnus-article-buffer)
9276                 (widen)
9277                 (goto-char (if backward (point-max) (point-min))))))))
9278       (gnus-message 7 ""))
9279     ;; Return whether we found the regexp.
9280     (when (eq found 'found)
9281       (goto-char point)
9282       (gnus-summary-show-thread)
9283       (gnus-summary-goto-subject gnus-current-article)
9284       (gnus-summary-position-point)
9285       t)))
9286
9287 (defun gnus-find-matching-articles (header regexp)
9288   "Return a list of all articles that match REGEXP on HEADER.
9289 This search includes all articles in the current group that Gnus has
9290 fetched headers for, whether they are displayed or not."
9291   (let ((articles nil)
9292         ;; Can't eta-reduce because it's a macro.
9293         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9294         (case-fold-search t))
9295     (dolist (header gnus-newsgroup-headers)
9296       (when (string-match regexp (funcall func header))
9297         (push (mail-header-number header) articles)))
9298     (nreverse articles)))
9299
9300 (defun gnus-summary-find-matching (header regexp &optional backward unread
9301                                           not-case-fold not-matching)
9302   "Return a list of all articles that match REGEXP on HEADER.
9303 The search stars on the current article and goes forwards unless
9304 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9305 If UNREAD is non-nil, only unread articles will
9306 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9307 in the comparisons. If NOT-MATCHING, return a list of all articles that
9308 not match REGEXP on HEADER."
9309   (let ((case-fold-search (not not-case-fold))
9310         articles d func)
9311     (if (consp header)
9312         (if (eq (car header) 'extra)
9313             (setq func
9314                   `(lambda (h)
9315                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9316                          "")))
9317           (error "%s is an invalid header" header))
9318       (unless (fboundp (intern (concat "mail-header-" header)))
9319         (error "%s is not a valid header" header))
9320       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9321     (dolist (d (if (eq backward 'all)
9322                    gnus-newsgroup-data
9323                  (gnus-data-find-list
9324                   (gnus-summary-article-number)
9325                   (gnus-data-list backward))))
9326       (when (and (or (not unread)       ; We want all articles...
9327                      (gnus-data-unread-p d)) ; Or just unreads.
9328                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9329                  (if not-matching
9330                      (not (string-match
9331                            regexp
9332                            (funcall func (gnus-data-header d))))
9333                    (string-match regexp
9334                                  (funcall func (gnus-data-header d)))))
9335         (push (gnus-data-number d) articles))) ; Success!
9336     (nreverse articles)))
9337
9338 (defun gnus-summary-execute-command (header regexp command &optional backward)
9339   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9340 If HEADER is an empty string (or nil), the match is done on the entire
9341 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9342   (interactive
9343    (list (let ((completion-ignore-case t))
9344            (gnus-completing-read
9345             "Header name"
9346             (mapcar 'symbol-name
9347                     (append
9348                      '(Number Subject From Lines Date
9349                        Message-ID Xref References Body)
9350                      gnus-extra-headers))
9351             'require-match))
9352          (read-string "Regexp: ")
9353          (read-key-sequence "Command: ")
9354          current-prefix-arg))
9355   (when (equal header "Body")
9356     (setq header ""))
9357   ;; Hidden thread subtrees must be searched as well.
9358   (gnus-summary-show-all-threads)
9359   ;; We don't want to change current point nor window configuration.
9360   (save-excursion
9361     (save-window-excursion
9362       (let (gnus-visual
9363             gnus-treat-strip-trailing-blank-lines
9364             gnus-treat-strip-leading-blank-lines
9365             gnus-treat-strip-multiple-blank-lines
9366             gnus-treat-hide-boring-headers
9367             gnus-treat-fold-newsgroups
9368             gnus-article-prepare-hook)
9369         (gnus-message 6 "Executing %s..." (key-description command))
9370         ;; We'd like to execute COMMAND interactively so as to give arguments.
9371         (gnus-execute header regexp
9372                       `(call-interactively ',(key-binding command))
9373                       backward)
9374         (gnus-message 6 "Executing %s...done" (key-description command))))))
9375
9376 (defun gnus-summary-beginning-of-article ()
9377   "Scroll the article back to the beginning."
9378   (interactive)
9379   (gnus-summary-select-article)
9380   (gnus-configure-windows 'article)
9381   (gnus-eval-in-buffer-window gnus-article-buffer
9382     (widen)
9383     (goto-char (point-min))
9384     (when gnus-break-pages
9385       (gnus-narrow-to-page))))
9386
9387 (defun gnus-summary-end-of-article ()
9388   "Scroll to the end of the article."
9389   (interactive)
9390   (gnus-summary-select-article)
9391   (gnus-configure-windows 'article)
9392   (gnus-eval-in-buffer-window gnus-article-buffer
9393     (widen)
9394     (goto-char (point-max))
9395     (recenter -3)
9396     (when gnus-break-pages
9397       (gnus-narrow-to-page))))
9398
9399 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9400   "Truncate to LEN and quote all \"(\"'s in STRING."
9401   (gnus-replace-in-string (if (and len (> (length string) len))
9402                               (substring string 0 len)
9403                             string)
9404                           "[()]" "\\\\\\&"))
9405
9406 (defun gnus-summary-print-article (&optional filename n)
9407   "Generate and print a PostScript image of the process-marked (mail) articles.
9408
9409 If used interactively, print the current article if none are
9410 process-marked.  With prefix arg, prompt the user for the name of the
9411 file to save in.
9412
9413 When used from Lisp, accept two optional args FILENAME and N.  N means
9414 to print the next N articles.  If N is negative, print the N previous
9415 articles.  If N is nil and articles have been marked with the process
9416 mark, print these instead.
9417
9418 If the optional first argument FILENAME is nil, send the image to the
9419 printer.  If FILENAME is a string, save the PostScript image in a file with
9420 that name.  If FILENAME is a number, prompt the user for the name of the file
9421 to save in."
9422   (interactive (list (ps-print-preprint current-prefix-arg)))
9423   (dolist (article (gnus-summary-work-articles n))
9424     (gnus-summary-select-article nil nil 'pseudo article)
9425     (gnus-eval-in-buffer-window gnus-article-buffer
9426       (gnus-print-buffer))
9427     (gnus-summary-remove-process-mark article))
9428   (ps-despool filename))
9429
9430 (defun gnus-print-buffer ()
9431   (let ((ps-left-header
9432          (list
9433           (concat "("
9434                   (gnus-summary-print-truncate-and-quote
9435                    (mail-header-subject gnus-current-headers)
9436                    66) ")")
9437           (concat "("
9438                   (gnus-summary-print-truncate-and-quote
9439                    (mail-header-from gnus-current-headers)
9440                    45) ")")))
9441         (ps-right-header
9442          (list
9443           "/pagenumberstring load"
9444           (concat "("
9445                   (mail-header-date gnus-current-headers) ")"))))
9446     (gnus-run-hooks 'gnus-ps-print-hook)
9447     (save-excursion
9448       (if ps-print-color-p
9449           (ps-spool-buffer-with-faces)
9450         (ps-spool-buffer)))))
9451
9452 (defun gnus-summary-show-complete-article ()
9453   "Show a complete version of the current article.
9454 This is only useful if you're looking at a partial version of the
9455 article currently."
9456   (interactive)
9457   (let ((gnus-keep-backlog nil)
9458         (gnus-use-cache nil)
9459         (gnus-agent nil)
9460         (variable (intern
9461                    (format "%s-fetch-partial-articles"
9462                            (car (gnus-find-method-for-group
9463                                  gnus-newsgroup-name)))
9464                    obarray))
9465         old-val)
9466     (unwind-protect
9467         (progn
9468           (setq old-val (symbol-value variable))
9469           (set variable nil)
9470           (gnus-flush-original-article-buffer)
9471           (gnus-summary-show-article))
9472       (set variable old-val))))
9473
9474 (defun gnus-summary-show-article (&optional arg)
9475   "Force redisplaying of the current article.
9476 If ARG (the prefix) is a number, show the article with the charset
9477 defined in `gnus-summary-show-article-charset-alist', or the charset
9478 input.
9479 If ARG (the prefix) is non-nil and not a number, show the article,
9480 but without running any of the article treatment functions
9481 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9482 C-u g', show the raw article."
9483   (interactive "P")
9484   (cond
9485    ((numberp arg)
9486     (gnus-summary-show-article t)
9487     (let ((gnus-newsgroup-charset
9488            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9489                (mm-read-coding-system
9490                 "View as charset: " ;; actually it is coding system.
9491                 (with-current-buffer gnus-article-buffer
9492                   (mm-detect-coding-region (point) (point-max))))))
9493           (gnus-newsgroup-ignored-charsets 'gnus-all))
9494       (gnus-summary-select-article nil 'force)
9495       (let ((deps gnus-newsgroup-dependencies)
9496             head header lines)
9497         (with-current-buffer gnus-original-article-buffer
9498           (save-restriction
9499             (message-narrow-to-head)
9500             (setq head (buffer-string))
9501             (goto-char (point-min))
9502             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9503               (goto-char (point-max))
9504               (widen)
9505               (setq lines (1- (count-lines (point) (point-max))))))
9506           (with-temp-buffer
9507             (insert (format "211 %d Article retrieved.\n"
9508                             (cdr gnus-article-current)))
9509             (insert head)
9510             (if lines (insert (format "Lines: %d\n" lines)))
9511             (insert ".\n")
9512             (let ((nntp-server-buffer (current-buffer)))
9513               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9514         (gnus-data-set-header
9515          (gnus-data-find (cdr gnus-article-current))
9516          header)
9517         (gnus-summary-update-article-line
9518          (cdr gnus-article-current) header)
9519         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9520           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9521    ((not arg)
9522     ;; Select the article the normal way.
9523     (gnus-summary-select-article nil 'force))
9524    ((or (equal arg '(16))
9525         (eq arg t))
9526     ;; C-u C-u g
9527     (let ((gnus-inhibit-article-treatments t))
9528       (gnus-summary-select-article nil 'force)))
9529    (t
9530     ;; We have to require this here to make sure that the following
9531     ;; dynamic binding isn't shadowed by autoloading.
9532     (require 'gnus-async)
9533     (require 'gnus-art)
9534     ;; Bind the article treatment functions to nil.
9535     (let ((gnus-have-all-headers t)
9536           gnus-article-prepare-hook
9537           gnus-article-decode-hook
9538           gnus-display-mime-function
9539           gnus-break-pages)
9540       ;; Destroy any MIME parts.
9541       (when (gnus-buffer-live-p gnus-article-buffer)
9542         (with-current-buffer gnus-article-buffer
9543           (mm-destroy-parts gnus-article-mime-handles)
9544           ;; Set it to nil for safety reason.
9545           (setq gnus-article-mime-handle-alist nil)
9546           (setq gnus-article-mime-handles nil)))
9547       (gnus-summary-select-article nil 'force))))
9548   (gnus-summary-goto-subject gnus-current-article)
9549   (gnus-summary-position-point))
9550
9551 (defun gnus-summary-show-raw-article ()
9552   "Show the raw article without any article massaging functions being run."
9553   (interactive)
9554   (gnus-summary-show-article t))
9555
9556 (defun gnus-summary-verbose-headers (&optional arg)
9557   "Toggle permanent full header display.
9558 If ARG is a positive number, turn header display on.
9559 If ARG is a negative number, turn header display off."
9560   (interactive "P")
9561   (setq gnus-show-all-headers
9562         (cond ((or (not (numberp arg))
9563                    (zerop arg))
9564                (not gnus-show-all-headers))
9565               ((natnump arg)
9566                t)))
9567   (gnus-summary-show-article))
9568
9569 (defun gnus-summary-toggle-header (&optional arg)
9570   "Show the headers if they are hidden, or hide them if they are shown.
9571 If ARG is a positive number, show the entire header.
9572 If ARG is a negative number, hide the unwanted header lines."
9573   (interactive "P")
9574   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9575                      (get-buffer-window gnus-article-buffer t))))
9576     (with-current-buffer gnus-article-buffer
9577       (widen)
9578       (article-narrow-to-head)
9579       (let* ((inhibit-read-only t)
9580              (inhibit-point-motion-hooks t)
9581              (hidden (if (numberp arg)
9582                          (>= arg 0)
9583                        (or (not (looking-at "[^ \t\n]+:"))
9584                            (gnus-article-hidden-text-p 'headers))))
9585              s e)
9586         (delete-region (point-min) (point-max))
9587         (with-current-buffer gnus-original-article-buffer
9588           (goto-char (setq s (point-min)))
9589           (setq e (if (search-forward "\n\n" nil t)
9590                       (1- (point))
9591                     (point-max))))
9592         (insert-buffer-substring gnus-original-article-buffer s e)
9593         (run-hooks 'gnus-article-decode-hook)
9594         (if hidden
9595             (let ((gnus-treat-hide-headers nil)
9596                   (gnus-treat-hide-boring-headers nil))
9597               (gnus-delete-wash-type 'headers)
9598               (gnus-treat-article 'head))
9599           (gnus-treat-article 'head))
9600         (widen)
9601         (if window
9602             (set-window-start window (goto-char (point-min))))
9603         (if gnus-break-pages
9604             (gnus-narrow-to-page)
9605           (when (gnus-visual-p 'page-marker)
9606             (let ((inhibit-read-only t))
9607               (gnus-remove-text-with-property 'gnus-prev)
9608               (gnus-remove-text-with-property 'gnus-next))))
9609         (gnus-set-mode-line 'article)))))
9610
9611 (defun gnus-summary-show-all-headers ()
9612   "Make all header lines visible."
9613   (interactive)
9614   (gnus-summary-toggle-header 1))
9615
9616 (defun gnus-summary-caesar-message (&optional arg)
9617   "Caesar rotate the current article by 13.
9618 With a non-numerical prefix, also rotate headers.  A numerical
9619 prefix specifies how many places to rotate each letter forward."
9620   (interactive "P")
9621   (gnus-summary-select-article)
9622   (let ((mail-header-separator ""))
9623     (gnus-eval-in-buffer-window gnus-article-buffer
9624       (save-restriction
9625         (widen)
9626         (let ((start (window-start))
9627               (inhibit-read-only t))
9628           (if (equal arg '(4))
9629               (message-caesar-buffer-body nil t)
9630             (message-caesar-buffer-body arg))
9631           (set-window-start (get-buffer-window (current-buffer)) start)))))
9632   ;; Create buttons and stuff...
9633   (gnus-treat-article nil))
9634
9635 (declare-function idna-to-unicode "ext:idna" (str))
9636
9637 (defun gnus-summary-idna-message (&optional arg)
9638   "Decode IDNA encoded domain names in the current articles.
9639 IDNA encoded domain names looks like `xn--bar'.  If a string
9640 remain unencoded after running this function, it is likely an
9641 invalid IDNA string (`xn--bar' is invalid).
9642
9643 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9644 installed for this command to work."
9645   (interactive "P")
9646   (if (not (and (condition-case nil (require 'idna)
9647                   (file-error))
9648                 (mm-coding-system-p 'utf-8)
9649                 (executable-find (symbol-value 'idna-program))))
9650       (gnus-message
9651        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9652     (gnus-summary-select-article)
9653     (let ((mail-header-separator ""))
9654       (gnus-eval-in-buffer-window gnus-article-buffer
9655         (save-restriction
9656           (widen)
9657           (let ((start (window-start))
9658                 buffer-read-only)
9659             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9660               (replace-match (idna-to-unicode (match-string 1))))
9661             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9662
9663 (defun gnus-summary-morse-message (&optional arg)
9664   "Morse decode the current article."
9665   (interactive "P")
9666   (gnus-summary-select-article)
9667   (let ((mail-header-separator ""))
9668     (gnus-eval-in-buffer-window gnus-article-buffer
9669       (save-excursion
9670         (save-restriction
9671           (widen)
9672           (let ((pos (window-start))
9673                 (inhibit-read-only t))
9674             (goto-char (point-min))
9675             (when (message-goto-body)
9676               (gnus-narrow-to-body))
9677             (goto-char (point-min))
9678             (while (search-forward "·" (point-max) t)
9679               (replace-match "."))
9680             (unmorse-region (point-min) (point-max))
9681             (widen)
9682             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9683
9684 (defun gnus-summary-stop-page-breaking ()
9685   "Stop page breaking in the current article."
9686   (interactive)
9687   (gnus-summary-select-article)
9688   (gnus-eval-in-buffer-window gnus-article-buffer
9689     (widen)
9690     (when (gnus-visual-p 'page-marker)
9691       (let ((inhibit-read-only t))
9692         (gnus-remove-text-with-property 'gnus-prev)
9693         (gnus-remove-text-with-property 'gnus-next))
9694       (setq gnus-page-broken nil))))
9695
9696 (defun gnus-summary-move-article (&optional n to-newsgroup
9697                                             select-method action)
9698   "Move the current article to a different newsgroup.
9699 If N is a positive number, move the N next articles.
9700 If N is a negative number, move the N previous articles.
9701 If N is nil and any articles have been marked with the process mark,
9702 move those articles instead.
9703 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9704 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9705 re-spool using this method.
9706
9707 When called interactively with TO-NEWSGROUP being nil, the value of
9708 the variable `gnus-move-split-methods' is used for finding a default
9709 for the target newsgroup.
9710
9711 For this function to work, both the current newsgroup and the
9712 newsgroup that you want to move to have to support the `request-move'
9713 and `request-accept' functions.
9714
9715 ACTION can be either `move' (the default), `crosspost' or `copy'."
9716   (interactive "P")
9717   (unless action
9718     (setq action 'move))
9719   ;; Check whether the source group supports the required functions.
9720   (cond ((and (eq action 'move)
9721               (not (gnus-check-backend-function
9722                     'request-move-article gnus-newsgroup-name)))
9723          (error "The current group does not support article moving"))
9724         ((and (eq action 'crosspost)
9725               (not (gnus-check-backend-function
9726                     'request-replace-article gnus-newsgroup-name)))
9727          (error "The current group does not support article editing")))
9728   (let ((articles (gnus-summary-work-articles n))
9729         (prefix (if (gnus-check-backend-function
9730                      'request-move-article gnus-newsgroup-name)
9731                     (funcall gnus-move-group-prefix-function
9732                              gnus-newsgroup-name)
9733                   ""))
9734         (names '((move "Move" "Moving")
9735                  (copy "Copy" "Copying")
9736                  (crosspost "Crosspost" "Crossposting")))
9737         (copy-buf (save-excursion
9738                     (nnheader-set-temp-buffer " *copy article*")))
9739         art-group to-method new-xref article to-groups
9740         articles-to-update-marks encoded)
9741     (unless (assq action names)
9742       (error "Unknown action %s" action))
9743     ;; Read the newsgroup name.
9744     (when (and (not to-newsgroup)
9745                (not select-method))
9746       (if (and gnus-move-split-methods
9747                (not
9748                 (and (memq gnus-current-article articles)
9749                      (gnus-buffer-live-p gnus-original-article-buffer))))
9750           ;; When `gnus-move-split-methods' is non-nil, we have to
9751           ;; select an article to give `gnus-read-move-group-name' an
9752           ;; opportunity to suggest an appropriate default.  However,
9753           ;; we needn't render or mark the article.
9754           (let ((gnus-display-mime-function nil)
9755                 (gnus-article-prepare-hook nil)
9756                 (gnus-mark-article-hook nil))
9757             (gnus-summary-select-article nil nil nil (car articles))))
9758       (setq to-newsgroup (gnus-read-move-group-name
9759                           (cadr (assq action names))
9760                           (symbol-value
9761                            (intern (format "gnus-current-%s-group" action)))
9762                           articles prefix)
9763             encoded to-newsgroup
9764             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9765       (set (intern (format "gnus-current-%s-group" action))
9766            (mm-decode-coding-string
9767             to-newsgroup
9768             (gnus-group-name-charset to-method to-newsgroup))))
9769     (unless to-method
9770       (setq to-method (or select-method
9771                           (gnus-server-to-method
9772                            (gnus-group-method to-newsgroup)))))
9773     (setq to-newsgroup
9774           (or encoded
9775               (and to-newsgroup
9776                    (mm-encode-coding-string
9777                     to-newsgroup
9778                     (gnus-group-name-charset to-method to-newsgroup)))))
9779     ;; Check the method we are to move this article to...
9780     (unless (gnus-check-backend-function
9781              'request-accept-article (car to-method))
9782       (error "%s does not support article copying" (car to-method)))
9783     (unless (gnus-check-server to-method)
9784       (error "Can't open server %s" (car to-method)))
9785     (gnus-message 6 "%s to %s: %s..."
9786                   (caddr (assq action names))
9787                   (or (car select-method)
9788                       (gnus-group-decoded-name to-newsgroup))
9789                   articles)
9790     (while articles
9791       (setq article (pop articles))
9792       ;; Set any marks that may have changed in the summary buffer.
9793       (when gnus-preserve-marks
9794         (gnus-summary-push-marks-to-backend article))
9795       (setq
9796        art-group
9797        (cond
9798         ;; Move the article.
9799         ((eq action 'move)
9800          ;; Remove this article from future suppression.
9801          (gnus-dup-unsuppress-article article)
9802          (let* ((from-method (gnus-find-method-for-group
9803                               gnus-newsgroup-name))
9804                 (to-method (or select-method
9805                                (gnus-find-method-for-group to-newsgroup)))
9806                 (move-is-internal (gnus-server-equal from-method to-method)))
9807            (gnus-request-move-article
9808             article                     ; Article to move
9809             gnus-newsgroup-name         ; From newsgroup
9810             (nth 1 (gnus-find-method-for-group
9811                     gnus-newsgroup-name)) ; Server
9812             (list 'gnus-request-accept-article
9813                   to-newsgroup (list 'quote select-method)
9814                   (not articles) t)     ; Accept form
9815             (not articles)              ; Only save nov last time
9816             (and move-is-internal
9817                  to-newsgroup           ; Not respooling
9818                                         ; Is this move internal?
9819                  (gnus-group-real-name to-newsgroup)))))
9820         ;; Copy the article.
9821         ((eq action 'copy)
9822          (with-current-buffer copy-buf
9823            (when (gnus-request-article-this-buffer article
9824                                                    gnus-newsgroup-name)
9825              (save-restriction
9826                (nnheader-narrow-to-headers)
9827                (dolist (hdr gnus-copy-article-ignored-headers)
9828                  (message-remove-header hdr t)))
9829              (gnus-request-accept-article
9830               to-newsgroup select-method (not articles) t))))
9831         ;; Crosspost the article.
9832         ((eq action 'crosspost)
9833          (let ((xref (message-tokenize-header
9834                       (mail-header-xref (gnus-summary-article-header
9835                                          article))
9836                       " ")))
9837            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9838                                   ":" (number-to-string article)))
9839            (unless xref
9840              (setq xref (list (system-name))))
9841            (setq new-xref
9842                  (concat
9843                   (mapconcat 'identity
9844                              (delete "Xref:" (delete new-xref xref))
9845                              " ")
9846                   " " new-xref))
9847            (with-current-buffer copy-buf
9848              ;; First put the article in the destination group.
9849              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9850              (when (consp (setq art-group
9851                                 (gnus-request-accept-article
9852                                  to-newsgroup select-method (not articles)
9853                                  t)))
9854                (setq new-xref (concat new-xref " " (car art-group)
9855                                       ":"
9856                                       (number-to-string (cdr art-group))))
9857                ;; Now we have the new Xrefs header, so we insert
9858                ;; it and replace the new article.
9859                (nnheader-replace-header "Xref" new-xref)
9860                (gnus-request-replace-article
9861                 (cdr art-group) to-newsgroup (current-buffer) t)
9862                art-group))))))
9863       (cond
9864        ((not art-group)
9865         (gnus-message 1 "Couldn't %s article %s: %s"
9866                       (cadr (assq action names)) article
9867                       (nnheader-get-report (car to-method))))
9868        ((eq art-group 'junk)
9869         (when (eq action 'move)
9870           (gnus-summary-mark-article article gnus-canceled-mark)
9871           (gnus-message 4 "Deleted article %s" article)
9872           ;; run the delete hook
9873           (run-hook-with-args 'gnus-summary-article-delete-hook
9874                               action
9875                               (gnus-data-header
9876                                (assoc article (gnus-data-list nil)))
9877                               gnus-newsgroup-name nil
9878                               select-method)))
9879        (t
9880         (let* ((pto-group (gnus-group-prefixed-name
9881                            (car art-group) to-method))
9882                (info (gnus-get-info pto-group))
9883                (to-group (gnus-info-group info))
9884                to-marks)
9885           ;; Update the group that has been moved to.
9886           (when (and info
9887                      (memq action '(move copy)))
9888             (unless (member to-group to-groups)
9889               (push to-group to-groups))
9890
9891             (when (and (not (memq article gnus-newsgroup-unreads))
9892                        (cdr art-group))
9893               (push 'read to-marks)
9894               (gnus-info-set-read
9895                info (gnus-add-to-range (gnus-info-read info)
9896                                        (list (cdr art-group)))))
9897
9898             ;; See whether the article is to be put in the cache.
9899             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9900                    (marks (if expirable
9901                               gnus-article-mark-lists
9902                             (delete '(expirable . expire)
9903                                     (copy-sequence
9904                                      gnus-article-mark-lists))))
9905                    (to-article (cdr art-group)))
9906
9907               ;; Enter the article into the cache in the new group,
9908               ;; if that is required.
9909               (when (and to-article
9910                          gnus-use-cache)
9911                 (gnus-cache-possibly-enter-article
9912                  to-group to-article
9913                  (memq article gnus-newsgroup-marked)
9914                  (memq article gnus-newsgroup-dormant)
9915                  (memq article gnus-newsgroup-unreads)))
9916
9917               (when (and gnus-preserve-marks
9918                          to-article)
9919                 ;; Copy any marks over to the new group.
9920                 (when (and (equal to-group gnus-newsgroup-name)
9921                            (not (memq article gnus-newsgroup-unreads)))
9922                   ;; Mark this article as read in this group.
9923                   (push (cons to-article gnus-read-mark)
9924                         gnus-newsgroup-reads)
9925                   ;; Increase the active status of this group.
9926                   (setcdr (gnus-active to-group) to-article)
9927                   (setcdr gnus-newsgroup-active to-article))
9928
9929                 (while marks
9930                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9931                     (when (memq article (symbol-value
9932                                          (intern (format "gnus-newsgroup-%s"
9933                                                          (caar marks)))))
9934                       (push (cdar marks) to-marks)
9935                       ;; If the other group is the same as this group,
9936                       ;; then we have to add the mark to the list.
9937                       (when (equal to-group gnus-newsgroup-name)
9938                         (set (intern (format "gnus-newsgroup-%s"
9939                                              (caar marks)))
9940                              (cons to-article
9941                                    (symbol-value
9942                                     (intern (format "gnus-newsgroup-%s"
9943                                                     (caar marks)))))))
9944                       ;; Copy the marks to other group.
9945                       (gnus-add-marked-articles
9946                        to-group (cdar marks) (list to-article) info)))
9947                   (setq marks (cdr marks)))
9948
9949                 (when (and expirable
9950                            gnus-mark-copied-or-moved-articles-as-expirable
9951                            (not (memq 'expire to-marks)))
9952                   ;; Mark this article as expirable.
9953                   (push 'expire to-marks)
9954                   (when (equal to-group gnus-newsgroup-name)
9955                     (push to-article gnus-newsgroup-expirable))
9956                   ;; Copy the expirable mark to other group.
9957                   (gnus-add-marked-articles
9958                    to-group 'expire (list to-article) info))
9959
9960                 (when to-marks
9961                   (gnus-request-set-mark
9962                    to-group (list (list (list to-article) 'add to-marks)))))
9963
9964               (gnus-dribble-enter
9965                (concat "(gnus-group-set-info '"
9966                        (gnus-prin1-to-string (gnus-get-info to-group))
9967                        ")"))))
9968
9969           ;; Update the Xref header in this article to point to
9970           ;; the new crossposted article we have just created.
9971           (when (eq action 'crosspost)
9972             (with-current-buffer copy-buf
9973               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9974               (nnheader-replace-header "Xref" new-xref)
9975               (gnus-request-replace-article
9976                article gnus-newsgroup-name (current-buffer) t)))
9977
9978           ;; run the move/copy/crosspost/respool hook
9979           (run-hook-with-args 'gnus-summary-article-move-hook
9980                               action
9981                               (gnus-data-header
9982                                (assoc article (gnus-data-list nil)))
9983                               gnus-newsgroup-name
9984                               to-newsgroup
9985                               select-method))
9986
9987         ;;;!!!Why is this necessary?
9988         (set-buffer gnus-summary-buffer)
9989
9990         (when (eq action 'move)
9991           (save-excursion
9992             (gnus-summary-goto-subject article)
9993             (gnus-summary-mark-article article gnus-canceled-mark)))))
9994       (push article articles-to-update-marks))
9995
9996     (save-excursion
9997       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9998     ;; Re-activate all groups that have been moved to.
9999     (with-current-buffer gnus-group-buffer
10000       (let ((gnus-group-marked to-groups))
10001         (gnus-group-get-new-news-this-group nil t)))
10002
10003     (gnus-kill-buffer copy-buf)
10004     (gnus-summary-position-point)
10005     (gnus-set-mode-line 'summary)))
10006
10007 (defun gnus-summary-push-marks-to-backend (article)
10008   (let ((set nil)
10009         (marks gnus-article-mark-lists))
10010     (unless (memq article gnus-newsgroup-unreads)
10011       (push 'read set))
10012     (while marks
10013       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10014                  (memq article (symbol-value
10015                                 (intern (format "gnus-newsgroup-%s"
10016                                                 (caar marks))))))
10017         (push (cdar marks) set))
10018       (pop marks))
10019     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
10020
10021 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10022   "Copy the current article to some other group.
10023 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10024 When called interactively, if TO-NEWSGROUP is nil, use the value of
10025 the variable `gnus-move-split-methods' for finding a default target
10026 newsgroup.
10027 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10028 re-spool using this method."
10029   (interactive "P")
10030   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10031
10032 (defun gnus-summary-crosspost-article (&optional n)
10033   "Crosspost the current article to some other group."
10034   (interactive "P")
10035   (gnus-summary-move-article n nil nil 'crosspost))
10036
10037 (defcustom gnus-summary-respool-default-method nil
10038   "Default method type for respooling an article.
10039 If nil, use to the current newsgroup method."
10040   :type 'symbol
10041   :group 'gnus-summary-mail)
10042
10043 (defun gnus-summary-respool-article (&optional n method)
10044   "Respool the current article.
10045 The article will be squeezed through the mail spooling process again,
10046 which means that it will be put in some mail newsgroup or other
10047 depending on `nnmail-split-methods'.
10048 If N is a positive number, respool the N next articles.
10049 If N is a negative number, respool the N previous articles.
10050 If N is nil and any articles have been marked with the process mark,
10051 respool those articles instead.
10052
10053 Respooling can be done both from mail groups and \"real\" newsgroups.
10054 In the former case, the articles in question will be moved from the
10055 current group into whatever groups they are destined to.  In the
10056 latter case, they will be copied into the relevant groups."
10057   (interactive
10058    (list current-prefix-arg
10059          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10060                 (methname
10061                  (symbol-name (or gnus-summary-respool-default-method
10062                                   (car (gnus-find-method-for-group
10063                                         gnus-newsgroup-name)))))
10064                 (method
10065                  (gnus-completing-read
10066                   "Backend to use when respooling"
10067                   methods t nil 'gnus-mail-method-history methname))
10068                 ms)
10069            (cond
10070             ((zerop (length (setq ms (gnus-servers-using-backend
10071                                       (intern method)))))
10072              (list (intern method) ""))
10073             ((= 1 (length ms))
10074              (car ms))
10075             (t
10076              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10077                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10078                            ms-alist))))))))
10079   (unless method
10080     (error "No method given for respooling"))
10081   (if (assoc (symbol-name
10082               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10083              (gnus-methods-using 'respool))
10084       (gnus-summary-move-article n nil method)
10085     (gnus-summary-copy-article n nil method)))
10086
10087 (defun gnus-summary-import-article (file &optional edit)
10088   "Import an arbitrary file into a mail newsgroup."
10089   (interactive "fImport file: \nP")
10090   (let ((group gnus-newsgroup-name)
10091         (now (current-time))
10092         atts lines group-art)
10093     (unless (gnus-check-backend-function 'request-accept-article group)
10094       (error "%s does not support article importing" group))
10095     (or (file-readable-p file)
10096         (not (file-regular-p file))
10097         (error "Can't read %s" file))
10098     (with-current-buffer (gnus-get-buffer-create " *import file*")
10099       (erase-buffer)
10100       (nnheader-insert-file-contents file)
10101       (goto-char (point-min))
10102       (if (nnheader-article-p)
10103           (save-restriction
10104             (goto-char (point-min))
10105             (search-forward "\n\n" nil t)
10106             (narrow-to-region (point-min) (1- (point)))
10107             (goto-char (point-min))
10108             (unless (re-search-forward "^date:" nil t)
10109               (goto-char (point-max))
10110               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10111        ;; This doesn't look like an article, so we fudge some headers.
10112         (setq atts (file-attributes file)
10113               lines (count-lines (point-min) (point-max)))
10114         (insert "From: " (read-string "From: ") "\n"
10115                 "Subject: " (read-string "Subject: ") "\n"
10116                 "Date: " (message-make-date (nth 5 atts)) "\n"
10117                 "Message-ID: " (message-make-message-id) "\n"
10118                 "Lines: " (int-to-string lines) "\n"
10119                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10120       (setq group-art (gnus-request-accept-article group nil t))
10121       (kill-buffer (current-buffer)))
10122     (setq gnus-newsgroup-active (gnus-activate-group group))
10123     (forward-line 1)
10124     (gnus-summary-goto-article (cdr group-art) nil t)
10125     (when edit
10126       (gnus-summary-edit-article))))
10127
10128 (defun gnus-summary-create-article ()
10129   "Create an article in a mail newsgroup."
10130   (interactive)
10131   (let ((group gnus-newsgroup-name)
10132         (now (current-time))
10133         group-art)
10134     (unless (gnus-check-backend-function 'request-accept-article group)
10135       (error "%s does not support article importing" group))
10136     (with-current-buffer (gnus-get-buffer-create " *import file*")
10137       (erase-buffer)
10138       (goto-char (point-min))
10139       ;; This doesn't look like an article, so we fudge some headers.
10140       (insert "From: " (read-string "From: ") "\n"
10141               "Subject: " (read-string "Subject: ") "\n"
10142               "Date: " (message-make-date now) "\n"
10143               "Message-ID: " (message-make-message-id) "\n")
10144       (setq group-art (gnus-request-accept-article group nil t))
10145       (kill-buffer (current-buffer)))
10146     (setq gnus-newsgroup-active (gnus-activate-group group))
10147     (forward-line 1)
10148     (gnus-summary-goto-article (cdr group-art) nil t)
10149     (gnus-summary-edit-article)))
10150
10151 (defun gnus-summary-article-posted-p ()
10152   "Say whether the current (mail) article is available from news as well.
10153 This will be the case if the article has both been mailed and posted."
10154   (interactive)
10155   (let ((id (mail-header-references (gnus-summary-article-header)))
10156         (gnus-override-method (car (gnus-refer-article-methods))))
10157     (if (gnus-request-head id "")
10158         (gnus-message 2 "The current message was found on %s"
10159                       gnus-override-method)
10160       (gnus-message 2 "The current message couldn't be found on %s"
10161                     gnus-override-method)
10162       nil)))
10163
10164 (defun gnus-summary-expire-articles (&optional now)
10165   "Expire all articles that are marked as expirable in the current group."
10166   (interactive)
10167   (when (and (not gnus-group-is-exiting-without-update-p)
10168              (gnus-check-backend-function
10169               'request-expire-articles gnus-newsgroup-name))
10170     ;; This backend supports expiry.
10171     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10172            (expirable (if total
10173                           (progn
10174                             ;; We need to update the info for
10175                             ;; this group for `gnus-list-of-read-articles'
10176                             ;; to give us the right answer.
10177                             (gnus-run-hooks 'gnus-exit-group-hook)
10178                             (gnus-summary-update-info)
10179                             (gnus-list-of-read-articles gnus-newsgroup-name))
10180                         (setq gnus-newsgroup-expirable
10181                               (sort gnus-newsgroup-expirable '<))))
10182            (expiry-wait (if now 'immediate
10183                           (gnus-group-find-parameter
10184                            gnus-newsgroup-name 'expiry-wait)))
10185            (nnmail-expiry-target
10186             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10187                 nnmail-expiry-target))
10188            es)
10189       (when expirable
10190         ;; There are expirable articles in this group, so we run them
10191         ;; through the expiry process.
10192         (gnus-message 6 "Expiring articles...")
10193         (unless (gnus-check-group gnus-newsgroup-name)
10194           (error "Can't open server for %s" gnus-newsgroup-name))
10195         ;; The list of articles that weren't expired is returned.
10196         (save-excursion
10197           (if expiry-wait
10198               (let ((nnmail-expiry-wait-function nil)
10199                     (nnmail-expiry-wait expiry-wait))
10200                 (setq es (gnus-request-expire-articles
10201                           expirable gnus-newsgroup-name)))
10202             (setq es (gnus-request-expire-articles
10203                       expirable gnus-newsgroup-name)))
10204           (unless total
10205             (setq gnus-newsgroup-expirable es))
10206           ;; We go through the old list of expirable, and mark all
10207           ;; really expired articles as nonexistent.
10208           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10209             (let ((gnus-use-cache nil))
10210               (dolist (article expirable)
10211                 (when (and (not (memq article es))
10212                            (gnus-data-find article))
10213                   (gnus-summary-mark-article article gnus-canceled-mark)
10214                   (run-hook-with-args 'gnus-summary-article-expire-hook
10215                                       'delete
10216                                       (gnus-data-header
10217                                        (assoc article (gnus-data-list nil)))
10218                                       gnus-newsgroup-name
10219                                       nil
10220                                       nil))))))
10221         (gnus-message 6 "Expiring articles...done")))))
10222
10223 (defun gnus-summary-expire-articles-now ()
10224   "Expunge all expirable articles in the current group.
10225 This means that *all* articles that are marked as expirable will be
10226 deleted forever, right now."
10227   (interactive)
10228   (or gnus-expert-user
10229       (gnus-yes-or-no-p
10230        "Are you really, really sure you want to delete all expirable messages? ")
10231       (error "Phew!"))
10232   (gnus-summary-expire-articles t))
10233
10234 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10235 (defun gnus-summary-delete-article (&optional n)
10236   "Delete the N next (mail) articles.
10237 This command actually deletes articles.  This is not a marking
10238 command.  The article will disappear forever from your life, never to
10239 return.
10240
10241 If N is negative, delete backwards.
10242 If N is nil and articles have been marked with the process mark,
10243 delete these instead.
10244
10245 If `gnus-novice-user' is non-nil you will be asked for
10246 confirmation before the articles are deleted."
10247   (interactive "P")
10248   (unless (gnus-check-backend-function 'request-expire-articles
10249                                        gnus-newsgroup-name)
10250     (error "The current newsgroup does not support article deletion"))
10251   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10252     (error "Couldn't open server"))
10253   ;; Compute the list of articles to delete.
10254   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10255         (nnmail-expiry-target 'delete)
10256         not-deleted)
10257     (if (and gnus-novice-user
10258              (not (gnus-yes-or-no-p
10259                    (format "Do you really want to delete %s forever? "
10260                            (if (> (length articles) 1)
10261                                (format "these %s articles" (length articles))
10262                              "this article")))))
10263         ()
10264       ;; Delete the articles.
10265       (setq not-deleted (gnus-request-expire-articles
10266                          articles gnus-newsgroup-name 'force))
10267       (save-excursion
10268         (while articles
10269           (gnus-summary-remove-process-mark (car articles))
10270           ;; The backend might not have been able to delete the article
10271           ;; after all.
10272           (unless (memq (car articles) not-deleted)
10273             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10274             (let* ((article (car articles))
10275                    (ghead  (gnus-data-header
10276                             (assoc article (gnus-data-list nil)))))
10277               (run-hook-with-args 'gnus-summary-article-delete-hook
10278                                   'delete ghead gnus-newsgroup-name nil
10279                                   nil)))
10280           (setq articles (cdr articles))))
10281       (when not-deleted
10282         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10283     (gnus-summary-position-point)
10284     (gnus-set-mode-line 'summary)
10285     not-deleted))
10286
10287 (defun gnus-summary-edit-article (&optional arg)
10288   "Edit the current article.
10289 This will have permanent effect only in mail groups.
10290 If ARG is nil, edit the decoded articles.
10291 If ARG is 1, edit the raw articles.
10292 If ARG is 2, edit the raw articles even in read-only groups.
10293 If ARG is 3, edit the articles with the current handles.
10294 Otherwise, allow editing of articles even in read-only
10295 groups."
10296   (interactive "P")
10297   (let (force raw current-handles)
10298     (cond
10299      ((null arg))
10300      ((eq arg 1)
10301       (setq raw t))
10302      ((eq arg 2)
10303       (setq raw t
10304             force t))
10305      ((eq arg 3)
10306       (setq current-handles
10307             (and (gnus-buffer-live-p gnus-article-buffer)
10308                  (with-current-buffer gnus-article-buffer
10309                    (prog1
10310                        gnus-article-mime-handles
10311                      (setq gnus-article-mime-handles nil))))))
10312      (t
10313       (setq force t)))
10314     (when (and raw (not force)
10315                (member gnus-newsgroup-name '("nndraft:delayed"
10316                                              "nndraft:drafts"
10317                                              "nndraft:queue")))
10318       (error "Can't edit the raw article in group %s"
10319              gnus-newsgroup-name))
10320     (with-current-buffer gnus-summary-buffer
10321       (let ((mail-parse-charset gnus-newsgroup-charset)
10322             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10323         (gnus-set-global-variables)
10324         (when (and (not force)
10325                    (gnus-group-read-only-p))
10326           (error "The current newsgroup does not support article editing"))
10327         (gnus-summary-show-article t)
10328         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10329           (with-current-buffer gnus-article-buffer
10330             (mm-enable-multibyte)))
10331         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10332             (setq raw t))
10333         (gnus-article-edit-article
10334          (if raw 'ignore
10335            `(lambda ()
10336               (let ((mbl mml-buffer-list))
10337                 (setq mml-buffer-list nil)
10338                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10339                   (mime-to-mml ,'current-handles))
10340                 (let ((mbl1 mml-buffer-list))
10341                   (setq mml-buffer-list mbl)
10342                   (set (make-local-variable 'mml-buffer-list) mbl1))
10343                 (gnus-make-local-hook 'kill-buffer-hook)
10344                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10345          `(lambda (no-highlight)
10346             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10347                   (message-options message-options)
10348                   (message-options-set-recipient)
10349                   (mail-parse-ignored-charsets
10350                    ',gnus-newsgroup-ignored-charsets)
10351                   (rfc2047-header-encoding-alist
10352                    ',(let ((charset (gnus-group-name-charset
10353                                      (gnus-find-method-for-group
10354                                       gnus-newsgroup-name)
10355                                      gnus-newsgroup-name)))
10356                        (append (list (cons "Newsgroups" charset)
10357                                      (cons "Followup-To" charset)
10358                                      (cons "Xref" charset))
10359                                rfc2047-header-encoding-alist))))
10360               ,(if (not raw) '(progn
10361                                 (mml-to-mime)
10362                                 (mml-destroy-buffers)
10363                                 (remove-hook 'kill-buffer-hook
10364                                              'mml-destroy-buffers t)
10365                                 (kill-local-variable 'mml-buffer-list)))
10366               (gnus-summary-edit-article-done
10367                ,(or (mail-header-references gnus-current-headers) "")
10368                ,(gnus-group-read-only-p)
10369                ,gnus-summary-buffer no-highlight))))))))
10370
10371 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10372
10373 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10374                                                  no-highlight)
10375   "Make edits to the current article permanent."
10376   (interactive)
10377   (save-excursion
10378     ;; The buffer restriction contains the entire article if it exists.
10379     (when (article-goto-body)
10380       (let ((lines (count-lines (point) (point-max)))
10381             (length (- (point-max) (point)))
10382             (case-fold-search t)
10383             (body (copy-marker (point))))
10384         (goto-char (point-min))
10385         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10386           (delete-region (match-beginning 1) (match-end 1))
10387           (insert (number-to-string length)))
10388         (goto-char (point-min))
10389         (when (re-search-forward
10390                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10391           (delete-region (match-beginning 1) (match-end 1))
10392           (insert (number-to-string length)))
10393         (goto-char (point-min))
10394         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10395           (delete-region (match-beginning 1) (match-end 1))
10396           (insert (number-to-string lines))))))
10397   ;; Replace the article.
10398   (let ((buf (current-buffer))
10399         (article (cdr gnus-article-current))
10400         replace-result)
10401     (with-temp-buffer
10402       (insert-buffer-substring buf)
10403       (if (and (not read-only)
10404                (not (setq replace-result
10405                           (gnus-request-replace-article
10406                            article (car gnus-article-current)
10407                            (current-buffer) t))))
10408           (error "Couldn't replace article")
10409         ;; If we got a number back, then that's the new article number
10410         ;; for this article.  Otherwise, the article number didn't change.
10411         (when (numberp replace-result)
10412           (with-current-buffer gnus-summary-buffer
10413             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10414             (gnus-summary-limit gnus-newsgroup-limit)
10415             (setq article replace-result)
10416             (gnus-summary-goto-subject article t)))
10417         ;; Update the summary buffer.
10418         (if (and references
10419                  (equal (message-tokenize-header references " ")
10420                         (message-tokenize-header
10421                          (or (message-fetch-field "references") "") " ")))
10422             ;; We only have to update this line.
10423             (save-excursion
10424               (save-restriction
10425                 (message-narrow-to-head)
10426                 (let ((head (buffer-substring-no-properties
10427                              (point-min) (point-max)))
10428                       header)
10429                   (with-temp-buffer
10430                     (insert (format "211 %d Article retrieved.\n" article))
10431                     (insert head)
10432                     (insert ".\n")
10433                     (let ((nntp-server-buffer (current-buffer)))
10434                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10435                     (with-current-buffer gnus-summary-buffer
10436                       (gnus-data-set-header (gnus-data-find article) header)
10437                       (gnus-summary-update-article-line article header)
10438                       (if (gnus-summary-goto-subject article nil t)
10439                           (gnus-summary-update-secondary-mark article)))))))
10440           ;; Update threads.
10441           (set-buffer (or buffer gnus-summary-buffer))
10442           (gnus-summary-update-article article)
10443           (if (gnus-summary-goto-subject article nil t)
10444               (gnus-summary-update-secondary-mark article)))
10445         ;; Prettify the article buffer again.
10446         (unless no-highlight
10447           (with-current-buffer gnus-article-buffer
10448             ;;!!! Fix this -- article should be rehighlighted.
10449             ;;(gnus-run-hooks 'gnus-article-display-hook)
10450             (set-buffer gnus-original-article-buffer)
10451             (gnus-request-article
10452              article (car gnus-article-current) (current-buffer))))
10453         ;; Prettify the summary buffer line.
10454         (when (gnus-visual-p 'summary-highlight 'highlight)
10455           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10456
10457 (defun gnus-summary-edit-wash (key)
10458   "Perform editing command KEY in the article buffer."
10459   (interactive
10460    (list
10461     (progn
10462       (message "%s" (concat (this-command-keys) "- "))
10463       (read-char))))
10464   (message "")
10465   (gnus-summary-edit-article)
10466   (execute-kbd-macro (concat (this-command-keys) key))
10467   (gnus-article-edit-done))
10468
10469 ;;; Respooling
10470
10471 (defun gnus-summary-respool-query (&optional silent trace)
10472   "Query where the respool algorithm would put this article."
10473   (interactive)
10474   (let (gnus-mark-article-hook)
10475     (gnus-summary-select-article)
10476     (with-current-buffer gnus-original-article-buffer
10477       (let ((groups (nnmail-article-group 'identity trace)))
10478         (unless silent
10479           (if groups
10480               (message "This message would go to %s"
10481                        (mapconcat 'car groups ", "))
10482             (message "This message would go to no groups"))
10483           groups)))))
10484
10485 (defun gnus-summary-respool-trace ()
10486   "Trace where the respool algorithm would put this article.
10487 Display a buffer showing all fancy splitting patterns which matched."
10488   (interactive)
10489   (gnus-summary-respool-query nil t))
10490
10491 ;; Summary marking commands.
10492
10493 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10494   "Mark articles which has the same subject as read, and then select the next.
10495 If UNMARK is positive, remove any kind of mark.
10496 If UNMARK is negative, tick articles."
10497   (interactive "P")
10498   (when unmark
10499     (setq unmark (prefix-numeric-value unmark)))
10500   (let ((count
10501          (gnus-summary-mark-same-subject
10502           (gnus-summary-article-subject) unmark)))
10503     ;; Select next unread article.  If auto-select-same mode, should
10504     ;; select the first unread article.
10505     (gnus-summary-next-article t (and gnus-auto-select-same
10506                                       (gnus-summary-article-subject)))
10507     (gnus-message 7 "%d article%s marked as %s"
10508                   count (if (= count 1) " is" "s are")
10509                   (if unmark "unread" "read"))))
10510
10511 (defun gnus-summary-kill-same-subject (&optional unmark)
10512   "Mark articles which has the same subject as read.
10513 If UNMARK is positive, remove any kind of mark.
10514 If UNMARK is negative, tick articles."
10515   (interactive "P")
10516   (when unmark
10517     (setq unmark (prefix-numeric-value unmark)))
10518   (let ((count
10519          (gnus-summary-mark-same-subject
10520           (gnus-summary-article-subject) unmark)))
10521     ;; If marked as read, go to next unread subject.
10522     (when (null unmark)
10523       ;; Go to next unread subject.
10524       (gnus-summary-next-subject 1 t))
10525     (gnus-message 7 "%d articles are marked as %s"
10526                   count (if unmark "unread" "read"))))
10527
10528 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10529   "Mark articles with same SUBJECT as read, and return marked number.
10530 If optional argument UNMARK is positive, remove any kinds of marks.
10531 If optional argument UNMARK is negative, mark articles as unread instead."
10532   (let ((count 1))
10533     (save-excursion
10534       (cond
10535        ((null unmark)                   ; Mark as read.
10536         (while (and
10537                 (progn
10538                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10539                   (gnus-summary-show-thread) t)
10540                 (gnus-summary-find-subject subject))
10541           (setq count (1+ count))))
10542        ((> unmark 0)                    ; Tick.
10543         (while (and
10544                 (progn
10545                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10546                   (gnus-summary-show-thread) t)
10547                 (gnus-summary-find-subject subject))
10548           (setq count (1+ count))))
10549        (t                               ; Mark as unread.
10550         (while (and
10551                 (progn
10552                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10553                   (gnus-summary-show-thread) t)
10554                 (gnus-summary-find-subject subject))
10555           (setq count (1+ count)))))
10556       (gnus-set-mode-line 'summary)
10557       ;; Return the number of marked articles.
10558       count)))
10559
10560 (defun gnus-summary-mark-as-processable (n &optional unmark)
10561   "Set the process mark on the next N articles.
10562 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10563 the process mark instead.  The difference between N and the actual
10564 number of articles marked is returned."
10565   (interactive "P")
10566   (if (and (null n) (gnus-region-active-p))
10567       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10568     (setq n (prefix-numeric-value n))
10569     (let ((backward (< n 0))
10570           (n (abs n)))
10571       (while (and
10572               (> n 0)
10573               (if unmark
10574                   (gnus-summary-remove-process-mark
10575                    (gnus-summary-article-number))
10576                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10577               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10578         (setq n (1- n)))
10579       (when (/= 0 n)
10580         (gnus-message 7 "No more articles"))
10581       (gnus-summary-recenter)
10582       (gnus-summary-position-point)
10583       n)))
10584
10585 (defun gnus-summary-unmark-as-processable (n)
10586   "Remove the process mark from the next N articles.
10587 If N is negative, unmark backward instead.  The difference between N and
10588 the actual number of articles unmarked is returned."
10589   (interactive "P")
10590   (gnus-summary-mark-as-processable n t))
10591
10592 (defun gnus-summary-unmark-all-processable ()
10593   "Remove the process mark from all articles."
10594   (interactive)
10595   (save-excursion
10596     (while gnus-newsgroup-processable
10597       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10598   (gnus-summary-position-point))
10599
10600 (defun gnus-summary-add-mark (article type)
10601   "Mark ARTICLE with a mark of TYPE."
10602   (let ((vtype (car (assq type gnus-article-mark-lists)))
10603         var)
10604     (if (not vtype)
10605         (error "No such mark type: %s" type)
10606       (setq var (intern (format "gnus-newsgroup-%s" type)))
10607       (set var (cons article (symbol-value var)))
10608       (if (memq type '(processable cached replied forwarded recent saved))
10609           (gnus-summary-update-secondary-mark article)
10610         ;;; !!! This is bogus.  We should find out what primary
10611         ;;; !!! mark we want to set.
10612         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10613
10614 (defun gnus-summary-mark-as-expirable (n)
10615   "Mark N articles forward as expirable.
10616 If N is negative, mark backward instead.  The difference between N and
10617 the actual number of articles marked is returned."
10618   (interactive "p")
10619   (gnus-summary-mark-forward n gnus-expirable-mark))
10620
10621 (defun gnus-summary-mark-as-spam (n)
10622   "Mark N articles forward as spam.
10623 If N is negative, mark backward instead.  The difference between N and
10624 the actual number of articles marked is returned."
10625   (interactive "p")
10626   (gnus-summary-mark-forward n gnus-spam-mark))
10627
10628 (defun gnus-summary-mark-article-as-replied (article)
10629   "Mark ARTICLE as replied to and update the summary line.
10630 ARTICLE can also be a list of articles."
10631   (interactive (list (gnus-summary-article-number)))
10632   (let ((articles (if (listp article) article (list article))))
10633     (dolist (article articles)
10634       (unless (numberp article)
10635         (error "%s is not a number" article))
10636       (push article gnus-newsgroup-replied)
10637       (let ((inhibit-read-only t))
10638         (when (gnus-summary-goto-subject article nil t)
10639           (gnus-summary-update-secondary-mark article))))))
10640
10641 (defun gnus-summary-mark-article-as-forwarded (article)
10642   "Mark ARTICLE as forwarded and update the summary line.
10643 ARTICLE can also be a list of articles."
10644   (let ((articles (if (listp article) article (list article))))
10645     (dolist (article articles)
10646       (push article gnus-newsgroup-forwarded)
10647       (let ((inhibit-read-only t))
10648         (when (gnus-summary-goto-subject article nil t)
10649           (gnus-summary-update-secondary-mark article))))))
10650
10651 (defun gnus-summary-set-bookmark (article)
10652   "Set a bookmark in current article."
10653   (interactive (list (gnus-summary-article-number)))
10654   (when (or (not (get-buffer gnus-article-buffer))
10655             (not gnus-current-article)
10656             (not gnus-article-current)
10657             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10658     (error "No current article selected"))
10659   ;; Remove old bookmark, if one exists.
10660   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10661   ;; Set the new bookmark, which is on the form
10662   ;; (article-number . line-number-in-body).
10663   (push
10664    (cons article
10665          (with-current-buffer gnus-article-buffer
10666            (count-lines
10667             (min (point)
10668                  (save-excursion
10669                    (article-goto-body)
10670                    (point)))
10671             (point))))
10672    gnus-newsgroup-bookmarks)
10673   (gnus-message 6 "A bookmark has been added to the current article."))
10674
10675 (defun gnus-summary-remove-bookmark (article)
10676   "Remove the bookmark from the current article."
10677   (interactive (list (gnus-summary-article-number)))
10678   ;; Remove old bookmark, if one exists.
10679   (if (not (assq article gnus-newsgroup-bookmarks))
10680       (gnus-message 6 "No bookmark in current article.")
10681     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10682     (gnus-message 6 "Removed bookmark.")))
10683
10684 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10685 (defun gnus-summary-mark-as-dormant (n)
10686   "Mark N articles forward as dormant.
10687 If N is negative, mark backward instead.  The difference between N and
10688 the actual number of articles marked is returned."
10689   (interactive "p")
10690   (gnus-summary-mark-forward n gnus-dormant-mark))
10691
10692 (defun gnus-summary-set-process-mark (article)
10693   "Set the process mark on ARTICLE and update the summary line."
10694   (setq gnus-newsgroup-processable
10695         (cons article
10696               (delq article gnus-newsgroup-processable)))
10697   (when (gnus-summary-goto-subject article)
10698     (gnus-summary-show-thread)
10699     (gnus-summary-goto-subject article)
10700     (gnus-summary-update-secondary-mark article)))
10701
10702 (defun gnus-summary-remove-process-mark (&rest articles)
10703   "Remove the process mark from ARTICLES and update the summary line."
10704   (dolist (article articles)
10705     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10706     (when (gnus-summary-goto-subject article)
10707       (gnus-summary-show-thread)
10708       (gnus-summary-goto-subject article)
10709       (gnus-summary-update-secondary-mark article)))
10710   t)
10711
10712 (defun gnus-summary-set-saved-mark (article)
10713   "Set the process mark on ARTICLE and update the summary line."
10714   (push article gnus-newsgroup-saved)
10715   (when (gnus-summary-goto-subject article)
10716     (gnus-summary-update-secondary-mark article)))
10717
10718 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10719   "Mark N articles as read forwards.
10720 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10721 The difference between N and the actual number of articles marked is
10722 returned.
10723 If NO-EXPIRE, auto-expiry will be inhibited."
10724   (interactive "p")
10725   (gnus-summary-show-thread)
10726   (let ((backward (< n 0))
10727         (gnus-summary-goto-unread
10728          (and gnus-summary-goto-unread
10729               (not (eq gnus-summary-goto-unread 'never))
10730               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10731                                     gnus-ticked-mark gnus-dormant-mark)))))
10732         (n (abs n))
10733         (mark (or mark gnus-del-mark)))
10734     (while (and (> n 0)
10735                 (gnus-summary-mark-article nil mark no-expire)
10736                 (zerop (gnus-summary-next-subject
10737                         (if backward -1 1)
10738                         (and gnus-summary-goto-unread
10739                              (not (eq gnus-summary-goto-unread 'never)))
10740                         t)))
10741       (setq n (1- n)))
10742     (when (/= 0 n)
10743       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10744     (gnus-summary-recenter)
10745     (gnus-summary-position-point)
10746     (gnus-set-mode-line 'summary)
10747     n))
10748
10749 (defun gnus-summary-mark-article-as-read (mark)
10750   "Mark the current article quickly as read with MARK."
10751   (let ((article (gnus-summary-article-number)))
10752     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10753     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10754     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10755     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10756     (push (cons article mark) gnus-newsgroup-reads)
10757     ;; Possibly remove from cache, if that is used.
10758     (when gnus-use-cache
10759       (gnus-cache-enter-remove-article article))
10760     ;; Allow the backend to change the mark.
10761     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10762     ;; Check for auto-expiry.
10763     (when (and gnus-newsgroup-auto-expire
10764                (memq mark gnus-auto-expirable-marks))
10765       (setq mark gnus-expirable-mark)
10766       ;; Let the backend know about the mark change.
10767       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10768       (push article gnus-newsgroup-expirable))
10769     ;; Set the mark in the buffer.
10770     (gnus-summary-update-mark mark 'unread)
10771     t))
10772
10773 (defun gnus-summary-mark-article-as-unread (mark)
10774   "Mark the current article quickly as unread with MARK."
10775   (let* ((article (gnus-summary-article-number))
10776          (old-mark (gnus-summary-article-mark article)))
10777     ;; Allow the backend to change the mark.
10778     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10779     (if (eq mark old-mark)
10780         t
10781       (if (<= article 0)
10782           (progn
10783             (gnus-error 1 "Can't mark negative article numbers")
10784             nil)
10785         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10786         (setq gnus-newsgroup-spam-marked
10787               (delq article gnus-newsgroup-spam-marked))
10788         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10789         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10790         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10791         (cond ((= mark gnus-ticked-mark)
10792                (setq gnus-newsgroup-marked
10793                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10794                                               article)))
10795               ((= mark gnus-spam-mark)
10796                (setq gnus-newsgroup-spam-marked
10797                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10798                                               article)))
10799               ((= mark gnus-dormant-mark)
10800                (setq gnus-newsgroup-dormant
10801                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10802                                               article)))
10803               (t
10804                (setq gnus-newsgroup-unreads
10805                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10806                                               article))))
10807         (gnus-alist-pull article gnus-newsgroup-reads)
10808
10809         ;; See whether the article is to be put in the cache.
10810         (and gnus-use-cache
10811              (vectorp (gnus-summary-article-header article))
10812              (save-excursion
10813                (gnus-cache-possibly-enter-article
10814                 gnus-newsgroup-name article
10815                 (= mark gnus-ticked-mark)
10816                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10817
10818         ;; Fix the mark.
10819         (gnus-summary-update-mark mark 'unread)
10820         t))))
10821
10822 (defun gnus-summary-mark-article (&optional article mark no-expire)
10823   "Mark ARTICLE with MARK.  MARK can be any character.
10824 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10825 `??' (dormant) and `?E' (expirable).
10826 If MARK is nil, then the default character `?r' is used.
10827 If ARTICLE is nil, then the article on the current line will be
10828 marked.
10829 If NO-EXPIRE, auto-expiry will be inhibited."
10830   ;; The mark might be a string.
10831   (when (stringp mark)
10832     (setq mark (aref mark 0)))
10833   ;; If no mark is given, then we check auto-expiring.
10834   (when (null mark)
10835     (setq mark gnus-del-mark))
10836   (when (and (not no-expire)
10837              gnus-newsgroup-auto-expire
10838              (memq mark gnus-auto-expirable-marks))
10839     (setq mark gnus-expirable-mark))
10840   (let ((article (or article (gnus-summary-article-number)))
10841         (old-mark (gnus-summary-article-mark article)))
10842     ;; Allow the backend to change the mark.
10843     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10844     (if (eq mark old-mark)
10845         t
10846       (unless article
10847         (error "No article on current line"))
10848       (if (not (if (or (= mark gnus-unread-mark)
10849                        (= mark gnus-ticked-mark)
10850                        (= mark gnus-spam-mark)
10851                        (= mark gnus-dormant-mark))
10852                    (gnus-mark-article-as-unread article mark)
10853                  (gnus-mark-article-as-read article mark)))
10854           t
10855         ;; See whether the article is to be put in the cache.
10856         (and gnus-use-cache
10857              (not (= mark gnus-canceled-mark))
10858              (vectorp (gnus-summary-article-header article))
10859              (save-excursion
10860                (gnus-cache-possibly-enter-article
10861                 gnus-newsgroup-name article
10862                 (= mark gnus-ticked-mark)
10863                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10864
10865         (when (gnus-summary-goto-subject article nil t)
10866           (let ((inhibit-read-only t))
10867             (gnus-summary-show-thread)
10868             ;; Fix the mark.
10869             (gnus-summary-update-mark mark 'unread)
10870             t))))))
10871
10872 (defun gnus-summary-update-secondary-mark (article)
10873   "Update the secondary (read, process, cache) mark."
10874   (gnus-summary-update-mark
10875    (cond ((memq article gnus-newsgroup-processable)
10876           gnus-process-mark)
10877          ((memq article gnus-newsgroup-cached)
10878           gnus-cached-mark)
10879          ((memq article gnus-newsgroup-replied)
10880           gnus-replied-mark)
10881          ((memq article gnus-newsgroup-forwarded)
10882           gnus-forwarded-mark)
10883          ((memq article gnus-newsgroup-saved)
10884           gnus-saved-mark)
10885          ((memq article gnus-newsgroup-recent)
10886           gnus-recent-mark)
10887          ((memq article gnus-newsgroup-unseen)
10888           gnus-unseen-mark)
10889          (t gnus-no-mark))
10890    'replied)
10891   (when (gnus-visual-p 'summary-highlight 'highlight)
10892     (gnus-summary-highlight-line)
10893     (gnus-run-hooks 'gnus-summary-update-hook))
10894   t)
10895
10896 (defun gnus-summary-update-download-mark (article)
10897   "Update the download mark."
10898   (gnus-summary-update-mark
10899    (cond ((memq article gnus-newsgroup-undownloaded)
10900           gnus-undownloaded-mark)
10901          (gnus-newsgroup-agentized
10902           gnus-downloaded-mark)
10903          (t
10904           gnus-no-mark))
10905    'download)
10906   (gnus-summary-update-line t)
10907   t)
10908
10909 (defun gnus-summary-update-mark (mark type)
10910   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10911         (inhibit-read-only t))
10912     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10913     (when forward
10914       (when (looking-at "\r")
10915         (incf forward))
10916       (when (<= (+ forward (point)) (point-max))
10917         ;; Go to the right position on the line.
10918         (goto-char (+ forward (point)))
10919         ;; Replace the old mark with the new mark.
10920         (let ((to-insert
10921                (mm-subst-char-in-string
10922                 (char-after) mark
10923                 (buffer-substring (point) (1+ (point))))))
10924           (delete-region (point) (1+ (point)))
10925           (insert to-insert))
10926         ;; Optionally update the marks by some user rule.
10927         (when (eq type 'unread)
10928           (gnus-data-set-mark
10929            (gnus-data-find (gnus-summary-article-number)) mark)
10930           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10931
10932 (defun gnus-mark-article-as-read (article &optional mark)
10933   "Enter ARTICLE in the pertinent lists and remove it from others."
10934   ;; Make the article expirable.
10935   (let ((mark (or mark gnus-del-mark)))
10936     (setq gnus-newsgroup-expirable
10937           (if (= mark gnus-expirable-mark)
10938               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10939             (delq article gnus-newsgroup-expirable)))
10940     ;; Remove from unread and marked lists.
10941     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10942     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10943     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10944     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10945     (push (cons article mark) gnus-newsgroup-reads)
10946     ;; Possibly remove from cache, if that is used.
10947     (when gnus-use-cache
10948       (gnus-cache-enter-remove-article article))
10949     t))
10950
10951 (defun gnus-mark-article-as-unread (article &optional mark)
10952   "Enter ARTICLE in the pertinent lists and remove it from others."
10953   (let ((mark (or mark gnus-ticked-mark)))
10954     (if (<= article 0)
10955         (progn
10956           (gnus-error 1 "Can't mark negative article numbers")
10957           nil)
10958       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10959             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10960             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10961             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10962             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10963
10964       ;; Unsuppress duplicates?
10965       (when gnus-suppress-duplicates
10966         (gnus-dup-unsuppress-article article))
10967
10968       (cond ((= mark gnus-ticked-mark)
10969              (setq gnus-newsgroup-marked
10970                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10971             ((= mark gnus-spam-mark)
10972              (setq gnus-newsgroup-spam-marked
10973                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10974                                             article)))
10975             ((= mark gnus-dormant-mark)
10976              (setq gnus-newsgroup-dormant
10977                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10978             (t
10979              (setq gnus-newsgroup-unreads
10980                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10981       (gnus-alist-pull article gnus-newsgroup-reads)
10982       t)))
10983
10984 (defun gnus-summary-tick-article-forward (n)
10985   "Tick N articles forwards.
10986 If N is negative, tick backwards instead.
10987 The difference between N and the number of articles ticked is returned."
10988   (interactive "p")
10989   (gnus-summary-mark-forward n gnus-ticked-mark))
10990
10991 (defun gnus-summary-tick-article-backward (n)
10992   "Tick N articles backwards.
10993 The difference between N and the number of articles ticked is returned."
10994   (interactive "p")
10995   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10996
10997 (defun gnus-summary-tick-article (&optional article clear-mark)
10998   "Mark current article as unread.
10999 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11000 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11001   (interactive)
11002   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11003                                        gnus-ticked-mark)))
11004
11005 (defun gnus-summary-mark-as-read-forward (n)
11006   "Mark N articles as read forwards.
11007 If N is negative, mark backwards instead.
11008 The difference between N and the actual number of articles marked is
11009 returned."
11010   (interactive "p")
11011   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11012
11013 (defun gnus-summary-mark-as-read-backward (n)
11014   "Mark the N articles as read backwards.
11015 The difference between N and the actual number of articles marked is
11016 returned."
11017   (interactive "p")
11018   (gnus-summary-mark-forward
11019    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11020
11021 (defun gnus-summary-mark-as-read (&optional article mark)
11022   "Mark current article as read.
11023 ARTICLE specifies the article to be marked as read.
11024 MARK specifies a string to be inserted at the beginning of the line."
11025   (gnus-summary-mark-article article mark))
11026
11027 (defun gnus-summary-clear-mark-forward (n)
11028   "Clear marks from N articles forward.
11029 If N is negative, clear backward instead.
11030 The difference between N and the number of marks cleared is returned."
11031   (interactive "p")
11032   (gnus-summary-mark-forward n gnus-unread-mark))
11033
11034 (defun gnus-summary-clear-mark-backward (n)
11035   "Clear marks from N articles backward.
11036 The difference between N and the number of marks cleared is returned."
11037   (interactive "p")
11038   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11039
11040 (defun gnus-summary-mark-unread-as-read ()
11041   "Intended to be used by `gnus-mark-article-hook'."
11042   (when (memq gnus-current-article gnus-newsgroup-unreads)
11043     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11044
11045 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11046   "Intended to be used by `gnus-mark-article-hook'."
11047   (let ((mark (gnus-summary-article-mark)))
11048     (when (or (gnus-unread-mark-p mark)
11049               (gnus-read-mark-p mark))
11050       (gnus-summary-mark-article gnus-current-article
11051                                  (or new-mark gnus-read-mark)))))
11052
11053 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11054   "Intended to be used by `gnus-mark-article-hook'."
11055   (let ((mark (gnus-summary-article-mark)))
11056     (when (or (gnus-unread-mark-p mark)
11057               (gnus-read-mark-p mark))
11058       (gnus-summary-mark-article (gnus-summary-article-number)
11059                                  (or new-mark gnus-read-mark)))))
11060
11061 (defun gnus-summary-mark-unread-as-ticked ()
11062   "Intended to be used by `gnus-mark-article-hook'."
11063   (when (memq gnus-current-article gnus-newsgroup-unreads)
11064     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11065
11066 (defun gnus-summary-mark-region-as-read (point mark all)
11067   "Mark all unread articles between point and mark as read.
11068 If given a prefix, mark all articles between point and mark as read,
11069 even ticked and dormant ones."
11070   (interactive "r\nP")
11071   (save-excursion
11072     (let (article)
11073       (goto-char point)
11074       (beginning-of-line)
11075       (while (and
11076               (< (point) mark)
11077               (progn
11078                 (when (or all
11079                           (memq (setq article (gnus-summary-article-number))
11080                                 gnus-newsgroup-unreads))
11081                   (gnus-summary-mark-article article gnus-del-mark))
11082                 t)
11083               (gnus-summary-find-next))))))
11084
11085 (defun gnus-summary-mark-below (score mark)
11086   "Mark articles with score less than SCORE with MARK."
11087   (interactive "P\ncMark: ")
11088   (setq score (if score
11089                   (prefix-numeric-value score)
11090                 (or gnus-summary-default-score 0)))
11091   (with-current-buffer gnus-summary-buffer
11092     (goto-char (point-min))
11093     (while
11094         (progn
11095           (and (< (gnus-summary-article-score) score)
11096                (gnus-summary-mark-article nil mark))
11097           (gnus-summary-find-next)))))
11098
11099 (defun gnus-summary-kill-below (&optional score)
11100   "Mark articles with score below SCORE as read."
11101   (interactive "P")
11102   (gnus-summary-mark-below score gnus-killed-mark))
11103
11104 (defun gnus-summary-clear-above (&optional score)
11105   "Clear all marks from articles with score above SCORE."
11106   (interactive "P")
11107   (gnus-summary-mark-above score gnus-unread-mark))
11108
11109 (defun gnus-summary-tick-above (&optional score)
11110   "Tick all articles with score above SCORE."
11111   (interactive "P")
11112   (gnus-summary-mark-above score gnus-ticked-mark))
11113
11114 (defun gnus-summary-mark-above (score mark)
11115   "Mark articles with score over SCORE with MARK."
11116   (interactive "P\ncMark: ")
11117   (setq score (if score
11118                   (prefix-numeric-value score)
11119                 (or gnus-summary-default-score 0)))
11120   (with-current-buffer gnus-summary-buffer
11121     (goto-char (point-min))
11122     (while (and (progn
11123                   (when (> (gnus-summary-article-score) score)
11124                     (gnus-summary-mark-article nil mark))
11125                   t)
11126                 (gnus-summary-find-next)))))
11127
11128 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11129 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11130 (defun gnus-summary-limit-include-expunged (&optional no-error)
11131   "Display all the hidden articles that were expunged for low scores."
11132   (interactive)
11133   (let ((inhibit-read-only t))
11134     (let ((scored gnus-newsgroup-scored)
11135           headers h)
11136       (while scored
11137         (unless (gnus-summary-article-header (caar scored))
11138           (and (setq h (gnus-number-to-header (caar scored)))
11139                (< (cdar scored) gnus-summary-expunge-below)
11140                (push h headers)))
11141         (setq scored (cdr scored)))
11142       (if (not headers)
11143           (when (not no-error)
11144             (error "No expunged articles hidden"))
11145         (goto-char (point-min))
11146         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11147         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11148         (dolist (x headers)
11149           (push (mail-header-number x) gnus-newsgroup-limit))
11150         (gnus-summary-prepare-unthreaded (nreverse headers))
11151         (goto-char (point-min))
11152         (gnus-summary-position-point)
11153         t))))
11154
11155 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11156   "Mark all unread articles in this newsgroup as read.
11157 If prefix argument ALL is non-nil, ticked and dormant articles will
11158 also be marked as read.
11159 If QUIETLY is non-nil, no questions will be asked.
11160
11161 If TO-HERE is non-nil, it should be a point in the buffer.  All
11162 articles before (after, if REVERSE is set) this point will be marked
11163 as read.
11164
11165 Note that this function will only catch up the unread article
11166 in the current summary buffer limitation.
11167
11168 The number of articles marked as read is returned."
11169   (interactive "P")
11170   (prog1
11171       (save-excursion
11172         (when (or quietly
11173                   (not gnus-interactive-catchup) ;Without confirmation?
11174                   gnus-expert-user
11175                   (gnus-y-or-n-p
11176                    (if all
11177                        "Mark absolutely all articles as read? "
11178                      "Mark all unread articles as read? ")))
11179           (if (and not-mark
11180                    (not gnus-newsgroup-adaptive)
11181                    (not gnus-newsgroup-auto-expire)
11182                    (not gnus-suppress-duplicates)
11183                    (or (not gnus-use-cache)
11184                        (eq gnus-use-cache 'passive)))
11185               (progn
11186                 (when all
11187                   (setq gnus-newsgroup-marked nil
11188                         gnus-newsgroup-spam-marked nil
11189                         gnus-newsgroup-dormant nil))
11190                 (setq gnus-newsgroup-unreads
11191                       (gnus-sorted-nunion
11192                        (gnus-sorted-intersection gnus-newsgroup-unreads
11193                                                  gnus-newsgroup-downloadable)
11194                        (gnus-sorted-difference gnus-newsgroup-unfetched
11195                                                gnus-newsgroup-cached))))
11196             ;; We actually mark all articles as canceled, which we
11197             ;; have to do when using auto-expiry or adaptive scoring.
11198             (gnus-summary-show-all-threads)
11199             (if (and to-here reverse)
11200                 (progn
11201                   (goto-char to-here)
11202                   (gnus-summary-mark-current-read-and-unread-as-read
11203                    gnus-catchup-mark)
11204                   (while (gnus-summary-find-next (not all))
11205                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11206               (when (gnus-summary-first-subject (not all))
11207                 (while (and
11208                         (if to-here (< (point) to-here) t)
11209                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11210                         (gnus-summary-find-next (not all))))))
11211             (gnus-set-mode-line 'summary))
11212           t))
11213     (gnus-summary-position-point)))
11214
11215 (defun gnus-summary-catchup-to-here (&optional all)
11216   "Mark all unticked articles before the current one as read.
11217 If ALL is non-nil, also mark ticked and dormant articles as read."
11218   (interactive "P")
11219   (save-excursion
11220     (gnus-save-hidden-threads
11221       (let ((beg (point)))
11222         ;; We check that there are unread articles.
11223         (when (or all (gnus-summary-find-prev))
11224           (gnus-summary-catchup all t beg)))))
11225   (gnus-summary-position-point))
11226
11227 (defun gnus-summary-catchup-from-here (&optional all)
11228   "Mark all unticked articles after (and including) the current one as read.
11229 If ALL is non-nil, also mark ticked and dormant articles as read."
11230   (interactive "P")
11231   (save-excursion
11232     (gnus-save-hidden-threads
11233       (let ((beg (point)))
11234         ;; We check that there are unread articles.
11235         (when (or all (gnus-summary-find-next))
11236           (gnus-summary-catchup all t beg nil t)))))
11237   (gnus-summary-position-point))
11238
11239 (defun gnus-summary-catchup-all (&optional quietly)
11240   "Mark all articles in this newsgroup as read.
11241 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11242 instead, which marks only unread articles as read."
11243   (interactive "P")
11244   (gnus-summary-catchup t quietly))
11245
11246 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11247   "Mark all unread articles in this group as read, then exit.
11248 If prefix argument ALL is non-nil, all articles are marked as read.
11249 If QUIETLY is non-nil, no questions will be asked."
11250   (interactive "P")
11251   (when (gnus-summary-catchup all quietly nil 'fast)
11252     ;; Select next newsgroup or exit.
11253     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11254              (eq gnus-auto-select-next 'quietly))
11255         (gnus-summary-next-group nil)
11256       (gnus-summary-exit))))
11257
11258 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11259   "Mark all articles in this newsgroup as read, and then exit.
11260 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11261 instead, which marks only unread articles as read."
11262   (interactive "P")
11263   (gnus-summary-catchup-and-exit t quietly))
11264
11265 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11266   "Mark all articles in this group as read and select the next group.
11267 If given a prefix, mark all articles, unread as well as ticked, as
11268 read."
11269   (interactive "P")
11270   (save-excursion
11271     (gnus-summary-catchup all))
11272   (gnus-summary-next-group))
11273
11274 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11275   "Mark all articles in this group as read and select the previous group.
11276 If given a prefix, mark all articles, unread as well as ticked, as
11277 read."
11278   (interactive "P")
11279   (save-excursion
11280     (gnus-summary-catchup all))
11281   (gnus-summary-next-group nil nil t))
11282
11283 ;;;
11284 ;;; with article
11285 ;;;
11286
11287 (defmacro gnus-with-article (article &rest forms)
11288   "Select ARTICLE and perform FORMS in the original article buffer.
11289 Then replace the article with the result."
11290   `(progn
11291      ;; We don't want the article to be marked as read.
11292      (let (gnus-mark-article-hook)
11293        (gnus-summary-select-article t t nil ,article))
11294      (set-buffer gnus-original-article-buffer)
11295      ,@forms
11296      (if (not (gnus-check-backend-function
11297                'request-replace-article (car gnus-article-current)))
11298          (gnus-message 5 "Read-only group; not replacing")
11299        (unless (gnus-request-replace-article
11300                 ,article (car gnus-article-current)
11301                 (current-buffer) t)
11302          (error "Couldn't replace article")))
11303      ;; The cache and backlog have to be flushed somewhat.
11304      (when gnus-keep-backlog
11305        (gnus-backlog-remove-article
11306         (car gnus-article-current) (cdr gnus-article-current)))
11307      (when gnus-use-cache
11308        (gnus-cache-update-article
11309         (car gnus-article-current) (cdr gnus-article-current)))))
11310
11311 (put 'gnus-with-article 'lisp-indent-function 1)
11312 (put 'gnus-with-article 'edebug-form-spec '(form body))
11313
11314 ;; Thread-based commands.
11315
11316 (defun gnus-summary-articles-in-thread (&optional article)
11317   "Return a list of all articles in the current thread.
11318 If ARTICLE is non-nil, return all articles in the thread that starts
11319 with that article."
11320   (let* ((article (or article (gnus-summary-article-number)))
11321          (data (gnus-data-find-list article))
11322          (top-level (gnus-data-level (car data)))
11323          (top-subject
11324           (cond ((null gnus-thread-operation-ignore-subject)
11325                  (gnus-simplify-subject-re
11326                   (mail-header-subject (gnus-data-header (car data)))))
11327                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11328                  (gnus-simplify-subject-fuzzy
11329                   (mail-header-subject (gnus-data-header (car data)))))
11330                 (t nil)))
11331          (end-point (save-excursion
11332                       (goto-char (gnus-data-pos (car data)))
11333                       (if (gnus-summary-go-to-next-thread)
11334                           (point) (point-max))))
11335          articles)
11336     (while (and data
11337                 (< (gnus-data-pos (car data)) end-point))
11338       (when (or (not top-subject)
11339                 (string= top-subject
11340                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11341                              (gnus-simplify-subject-fuzzy
11342                               (mail-header-subject
11343                                (gnus-data-header (car data))))
11344                            (gnus-simplify-subject-re
11345                             (mail-header-subject
11346                              (gnus-data-header (car data)))))))
11347         (push (gnus-data-number (car data)) articles))
11348       (unless (and (setq data (cdr data))
11349                    (> (gnus-data-level (car data)) top-level))
11350         (setq data nil)))
11351     ;; Return the list of articles.
11352     (nreverse articles)))
11353
11354 (defun gnus-summary-rethread-current ()
11355   "Rethread the thread the current article is part of."
11356   (interactive)
11357   (let* ((gnus-show-threads t)
11358          (article (gnus-summary-article-number))
11359          (id (mail-header-id (gnus-summary-article-header)))
11360          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11361     (unless id
11362       (error "No article on the current line"))
11363     (gnus-rebuild-thread id)
11364     (gnus-summary-goto-subject article)))
11365
11366 (defun gnus-summary-reparent-thread ()
11367   "Make the current article child of the marked (or previous) article.
11368
11369 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11370 is non-nil or the Subject: of both articles are the same."
11371   (interactive)
11372   (unless (not (gnus-group-read-only-p))
11373     (error "The current newsgroup does not support article editing"))
11374   (unless (<= (length gnus-newsgroup-processable) 1)
11375     (error "No more than one article may be marked"))
11376   (let ((child (gnus-summary-article-number))
11377         ;; First grab the marked article, otherwise one line up.
11378         (parent (if (not (null gnus-newsgroup-processable))
11379                     (car gnus-newsgroup-processable)
11380                   (save-excursion
11381                     (if (eq (forward-line -1) 0)
11382                         (gnus-summary-article-number)
11383                       (error "Beginning of summary buffer"))))))
11384     (gnus-summary-reparent-children parent (list child))))
11385
11386 (defun gnus-summary-reparent-children (parent children)
11387   "Make PARENT the parent of CHILDREN.
11388 When called interactively, PARENT is the current article and CHILDREN
11389 are the process-marked articles."
11390   (interactive
11391    (list (gnus-summary-article-number)
11392          (gnus-summary-work-articles nil)))
11393   (dolist (child children)
11394     (save-window-excursion
11395       (let ((gnus-article-buffer " *reparent*"))
11396         (unless (not (eq parent child))
11397           (error "An article may not be self-referential"))
11398         (let ((message-id (mail-header-id
11399                            (gnus-summary-article-header parent))))
11400           (unless (and message-id (not (equal message-id "")))
11401             (error "No message-id in desired parent"))
11402           (gnus-with-article child
11403             (save-restriction
11404               (goto-char (point-min))
11405               (message-narrow-to-head)
11406               (if (re-search-forward "^References: " nil t)
11407                   (progn
11408                     (re-search-forward "^[^ \t]" nil t)
11409                     (forward-line -1)
11410                     (end-of-line)
11411                     (insert " " message-id))
11412                 (insert "References: " message-id "\n"))))
11413           (set-buffer gnus-summary-buffer)
11414           (gnus-summary-unmark-all-processable)
11415           (gnus-summary-update-article child)
11416           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11417             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11418           (gnus-summary-rethread-current)
11419           (gnus-message 3 "Article %d is now the child of article %d"
11420                         child parent))))))
11421
11422 (defun gnus-summary-toggle-threads (&optional arg)
11423   "Toggle showing conversation threads.
11424 If ARG is positive number, turn showing conversation threads on."
11425   (interactive "P")
11426   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11427     (setq gnus-show-threads
11428           (if (null arg) (not gnus-show-threads)
11429             (> (prefix-numeric-value arg) 0)))
11430     (gnus-summary-prepare)
11431     (gnus-summary-goto-subject current)
11432     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11433     (gnus-summary-position-point)))
11434
11435 (eval-and-compile
11436   (if (fboundp 'remove-overlays)
11437       (defalias 'gnus-remove-overlays 'remove-overlays)
11438     (defun gnus-remove-overlays (beg end name val)
11439       "Clear BEG and END of overlays whose property NAME has value VAL.
11440 For compatibility with XEmacs."
11441       (dolist (ov (gnus-overlays-in beg end))
11442         (when (eq (gnus-overlay-get ov name) val)
11443           (gnus-delete-overlay ov))))))
11444
11445 (defun gnus-summary-show-all-threads ()
11446   "Show all threads."
11447   (interactive)
11448   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11449   (gnus-summary-position-point))
11450
11451 (defsubst gnus-summary--inv (p)
11452   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11453
11454 (defun gnus-summary-show-thread ()
11455   "Show thread subtrees.
11456 Returns nil if no thread was there to be shown."
11457   (interactive)
11458   (let* ((orig (point))
11459          (end (point-at-eol))
11460          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11461          ;; Leave point at bol
11462          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11463          (eoi (when end
11464                 (if (fboundp 'next-single-char-property-change)
11465                     (or (next-single-char-property-change end 'invisible)
11466                         (point-max))
11467                   (while (progn
11468                            (end-of-line 2)
11469                            (and (not (eobp))
11470                                 (eq (get-char-property (point) 'invisible)
11471                                     'gnus-sum))))
11472                   (point)))))
11473     (when eoi
11474       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11475       (goto-char orig)
11476       (gnus-summary-position-point)
11477       eoi)))
11478
11479 (defun gnus-summary-maybe-hide-threads ()
11480   "If requested, hide the threads that should be hidden."
11481   (when (and gnus-show-threads
11482              gnus-thread-hide-subtree)
11483     (gnus-summary-hide-all-threads
11484      (if (or (consp gnus-thread-hide-subtree)
11485              (functionp gnus-thread-hide-subtree))
11486          (gnus-make-predicate gnus-thread-hide-subtree)
11487        nil))))
11488
11489 ;;; Hiding predicates.
11490
11491 (defun gnus-article-unread-p (header)
11492   (memq (mail-header-number header) gnus-newsgroup-unreads))
11493
11494 (defun gnus-article-unseen-p (header)
11495   (memq (mail-header-number header) gnus-newsgroup-unseen))
11496
11497 (defun gnus-map-articles (predicate articles)
11498   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11499   (apply 'gnus-or (mapcar predicate
11500                           (mapcar (lambda (number)
11501                                     (gnus-summary-article-header number))
11502                                   articles))))
11503
11504 (defun gnus-summary-hide-all-threads (&optional predicate)
11505   "Hide all thread subtrees.
11506 If PREDICATE is supplied, threads that satisfy this predicate
11507 will not be hidden."
11508   (interactive)
11509   (save-excursion
11510     (goto-char (point-min))
11511     (let ((end nil))
11512       (while (not end)
11513         (when (or (not predicate)
11514                   (gnus-map-articles
11515                    predicate (gnus-summary-article-children)))
11516             (gnus-summary-hide-thread))
11517         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11518   (gnus-summary-position-point))
11519
11520 (defun gnus-summary-hide-thread ()
11521   "Hide thread subtrees.
11522 If PREDICATE is supplied, threads that satisfy this predicate
11523 will not be hidden.
11524 Returns nil if no threads were there to be hidden."
11525   (interactive)
11526   (let ((start (point))
11527         (starteol (line-end-position))
11528         (article (gnus-summary-article-number)))
11529     (goto-char start)
11530     ;; Go forward until either the buffer ends or the subthread ends.
11531     (when (and (not (eobp))
11532                (or (zerop (gnus-summary-next-thread 1 t))
11533                    (goto-char (point-max))))
11534       (if (and (> (point) start)
11535                ;; FIXME: this should actually search for a non-invisible \n.
11536                (search-backward "\n" start t))
11537           (progn
11538             (when (> (point) starteol)
11539               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11540               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11541                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11542                 (gnus-overlay-put ol 'evaporate t)))
11543             (gnus-summary-goto-subject article))
11544         (goto-char start)
11545         nil))))
11546
11547 (defun gnus-summary-go-to-next-thread (&optional previous)
11548   "Go to the same level (or less) next thread.
11549 If PREVIOUS is non-nil, go to previous thread instead.
11550 Return the article number moved to, or nil if moving was impossible."
11551   (let ((level (gnus-summary-thread-level))
11552         (way (if previous -1 1))
11553         (beg (point)))
11554     (forward-line way)
11555     (while (and (not (eobp))
11556                 (< level (gnus-summary-thread-level)))
11557       (forward-line way))
11558     (if (eobp)
11559         (progn
11560           (goto-char beg)
11561           nil)
11562       (setq beg (point))
11563       (prog1
11564           (gnus-summary-article-number)
11565         (goto-char beg)))))
11566
11567 (defun gnus-summary-next-thread (n &optional silent)
11568   "Go to the same level next N'th thread.
11569 If N is negative, search backward instead.
11570 Returns the difference between N and the number of skips actually
11571 done.
11572
11573 If SILENT, don't output messages."
11574   (interactive "p")
11575   (let ((backward (< n 0))
11576         (n (abs n)))
11577     (while (and (> n 0)
11578                 (gnus-summary-go-to-next-thread backward))
11579       (decf n))
11580     (unless silent
11581       (gnus-summary-position-point))
11582     (when (and (not silent) (/= 0 n))
11583       (gnus-message 7 "No more threads"))
11584     n))
11585
11586 (defun gnus-summary-prev-thread (n)
11587   "Go to the same level previous N'th thread.
11588 Returns the difference between N and the number of skips actually
11589 done."
11590   (interactive "p")
11591   (gnus-summary-next-thread (- n)))
11592
11593 (defun gnus-summary-go-down-thread ()
11594   "Go down one level in the current thread."
11595   (let ((children (gnus-summary-article-children)))
11596     (when children
11597       (gnus-summary-goto-subject (car children)))))
11598
11599 (defun gnus-summary-go-up-thread ()
11600   "Go up one level in the current thread."
11601   (let ((parent (gnus-summary-article-parent)))
11602     (when parent
11603       (gnus-summary-goto-subject parent))))
11604
11605 (defun gnus-summary-down-thread (n)
11606   "Go down thread N steps.
11607 If N is negative, go up instead.
11608 Returns the difference between N and how many steps down that were
11609 taken."
11610   (interactive "p")
11611   (let ((up (< n 0))
11612         (n (abs n)))
11613     (while (and (> n 0)
11614                 (if up (gnus-summary-go-up-thread)
11615                   (gnus-summary-go-down-thread)))
11616       (setq n (1- n)))
11617     (gnus-summary-position-point)
11618     (when (/= 0 n)
11619       (gnus-message 7 "Can't go further"))
11620     n))
11621
11622 (defun gnus-summary-up-thread (n)
11623   "Go up thread N steps.
11624 If N is negative, go down instead.
11625 Returns the difference between N and how many steps down that were
11626 taken."
11627   (interactive "p")
11628   (gnus-summary-down-thread (- n)))
11629
11630 (defun gnus-summary-top-thread ()
11631   "Go to the top of the thread."
11632   (interactive)
11633   (while (gnus-summary-go-up-thread))
11634   (gnus-summary-article-number))
11635
11636 (defun gnus-summary-expire-thread ()
11637   "Mark articles under current thread as expired."
11638   (interactive)
11639   (gnus-summary-kill-thread 0))
11640
11641 (defun gnus-summary-kill-thread (&optional unmark)
11642   "Mark articles under current thread as read.
11643 If the prefix argument is positive, remove any kinds of marks.
11644 If the prefix argument is zero, mark thread as expired.
11645 If the prefix argument is negative, tick articles instead."
11646   (interactive "P")
11647   (when unmark
11648     (setq unmark (prefix-numeric-value unmark)))
11649   (let ((articles (gnus-summary-articles-in-thread))
11650         (hide (or (null unmark) (= unmark 0))))
11651     (save-excursion
11652       ;; Expand the thread.
11653       (gnus-summary-show-thread)
11654       ;; Mark all the articles.
11655       (while articles
11656         (gnus-summary-goto-subject (car articles))
11657         (cond ((null unmark)
11658                (gnus-summary-mark-article-as-read gnus-killed-mark))
11659               ((> unmark 0)
11660                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11661               ((= unmark 0)
11662                (gnus-summary-mark-article nil gnus-expirable-mark))
11663               (t
11664                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11665         (setq articles (cdr articles))))
11666     ;; Hide killed subtrees when hide is true.
11667     (and hide
11668          gnus-thread-hide-killed
11669          (gnus-summary-hide-thread))
11670     ;; If hide is t, go to next unread subject.
11671     (when hide
11672       ;; Go to next unread subject.
11673       (gnus-summary-next-subject 1 t)))
11674   (gnus-set-mode-line 'summary))
11675
11676 ;; Summary sorting commands
11677
11678 (defun gnus-summary-sort-by-number (&optional reverse)
11679   "Sort the summary buffer by article number.
11680 Argument REVERSE means reverse order."
11681   (interactive "P")
11682   (gnus-summary-sort 'number reverse))
11683
11684 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11685   "Sort the summary buffer by most recent article number.
11686 Argument REVERSE means reverse order."
11687   (interactive "P")
11688   (gnus-summary-sort 'most-recent-number reverse))
11689
11690 (defun gnus-summary-sort-by-random (&optional reverse)
11691   "Randomize the order in the summary buffer.
11692 Argument REVERSE means to randomize in reverse order."
11693   (interactive "P")
11694   (gnus-summary-sort 'random reverse))
11695
11696 (defun gnus-summary-sort-by-author (&optional reverse)
11697   "Sort the summary buffer by author name alphabetically.
11698 If `case-fold-search' is non-nil, case of letters is ignored.
11699 Argument REVERSE means reverse order."
11700   (interactive "P")
11701   (gnus-summary-sort 'author reverse))
11702
11703 (defun gnus-summary-sort-by-recipient (&optional reverse)
11704   "Sort the summary buffer by recipient name alphabetically.
11705 If `case-fold-search' is non-nil, case of letters is ignored.
11706 Argument REVERSE means reverse order."
11707   (interactive "P")
11708   (gnus-summary-sort 'recipient reverse))
11709
11710 (defun gnus-summary-sort-by-subject (&optional reverse)
11711   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11712 If `case-fold-search' is non-nil, case of letters is ignored.
11713 Argument REVERSE means reverse order."
11714   (interactive "P")
11715   (gnus-summary-sort 'subject reverse))
11716
11717 (defun gnus-summary-sort-by-date (&optional reverse)
11718   "Sort the summary buffer by date.
11719 Argument REVERSE means reverse order."
11720   (interactive "P")
11721   (gnus-summary-sort 'date reverse))
11722
11723 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11724   "Sort the summary buffer by most recent date.
11725 Argument REVERSE means reverse order."
11726   (interactive "P")
11727   (gnus-summary-sort 'most-recent-date reverse))
11728
11729 (defun gnus-summary-sort-by-score (&optional reverse)
11730   "Sort the summary buffer by score.
11731 Argument REVERSE means reverse order."
11732   (interactive "P")
11733   (gnus-summary-sort 'score reverse))
11734
11735 (defun gnus-summary-sort-by-lines (&optional reverse)
11736   "Sort the summary buffer by the number of lines.
11737 Argument REVERSE means reverse order."
11738   (interactive "P")
11739   (gnus-summary-sort 'lines reverse))
11740
11741 (defun gnus-summary-sort-by-chars (&optional reverse)
11742   "Sort the summary buffer by article length.
11743 Argument REVERSE means reverse order."
11744   (interactive "P")
11745   (gnus-summary-sort 'chars reverse))
11746
11747 (defun gnus-summary-sort-by-original (&optional reverse)
11748   "Sort the summary buffer using the default sorting method.
11749 Argument REVERSE means reverse order."
11750   (interactive "P")
11751   (let* ((inhibit-read-only t)
11752          (gnus-summary-prepare-hook nil))
11753     ;; We do the sorting by regenerating the threads.
11754     (gnus-summary-prepare)
11755     ;; Hide subthreads if needed.
11756     (gnus-summary-maybe-hide-threads)))
11757
11758 (defun gnus-summary-sort (predicate reverse)
11759   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11760   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11761          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11762          (gnus-thread-sort-functions
11763           (if (not reverse)
11764               thread
11765             `(lambda (t1 t2)
11766                (,thread t2 t1))))
11767          (gnus-sort-gathered-threads-function
11768           gnus-thread-sort-functions)
11769          (gnus-article-sort-functions
11770           (if (not reverse)
11771               article
11772             `(lambda (t1 t2)
11773                (,article t2 t1))))
11774          (inhibit-read-only t)
11775          (gnus-summary-prepare-hook nil))
11776     ;; We do the sorting by regenerating the threads.
11777     (gnus-summary-prepare)
11778     ;; Hide subthreads if needed.
11779     (gnus-summary-maybe-hide-threads)))
11780
11781 ;; Summary saving commands.
11782
11783 (defun gnus-summary-save-article (&optional n not-saved)
11784   "Save the current article using the default saver function.
11785 If N is a positive number, save the N next articles.
11786 If N is a negative number, save the N previous articles.
11787 If N is nil and any articles have been marked with the process mark,
11788 save those articles instead.
11789 The variable `gnus-default-article-saver' specifies the saver function.
11790
11791 If the optional second argument NOT-SAVED is non-nil, articles saved
11792 will not be marked as saved."
11793   (interactive "P")
11794   (require 'gnus-art)
11795   (let* ((articles (gnus-summary-work-articles n))
11796          (save-buffer (save-excursion
11797                         (nnheader-set-temp-buffer " *Gnus Save*")))
11798          (num (length articles))
11799          ;; Whether to save decoded articles or raw articles.
11800          (decode (when gnus-article-save-coding-system
11801                    (get gnus-default-article-saver :decode)))
11802          ;; When saving many articles in a single file, use the other
11803          ;; function to save articles other than the first one.
11804          (saver2 (get gnus-default-article-saver :function))
11805          (gnus-prompt-before-saving (if saver2
11806                                         t
11807                                       gnus-prompt-before-saving))
11808          (gnus-default-article-saver gnus-default-article-saver)
11809          header file)
11810     (dolist (article articles)
11811       (setq header (gnus-summary-article-header article))
11812       (if (not (vectorp header))
11813           ;; This is a pseudo-article.
11814           (if (assq 'name header)
11815               (gnus-copy-file (cdr (assq 'name header)))
11816             (gnus-message 1 "Article %d is unsaveable" article))
11817         ;; This is a real article.
11818         (save-window-excursion
11819           (gnus-summary-select-article decode decode nil article)
11820           (gnus-summary-goto-subject article))
11821         (with-current-buffer save-buffer
11822           (erase-buffer)
11823           (insert-buffer-substring (if decode
11824                                        gnus-article-buffer
11825                                      gnus-original-article-buffer)))
11826         (setq file (gnus-article-save save-buffer file num))
11827         (gnus-summary-remove-process-mark article)
11828         (unless not-saved
11829           (gnus-summary-set-saved-mark article)))
11830       (when saver2
11831         (setq gnus-default-article-saver saver2
11832               saver2 nil)))
11833     (gnus-kill-buffer save-buffer)
11834     (gnus-summary-position-point)
11835     (gnus-set-mode-line 'summary)
11836     n))
11837
11838 (defun gnus-summary-pipe-output (&optional n sym)
11839   "Pipe the current article to a subprocess.
11840 If N is a positive number, pipe the N next articles.
11841 If N is a negative number, pipe the N previous articles.
11842 If N is nil and any articles have been marked with the process mark,
11843 pipe those articles instead.
11844 The default command to which articles are piped is specified by the
11845 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11846 will be prompted for the command.
11847
11848 The properties `:decode' and `:headers' that are put to the function
11849 symbol `gnus-summary-save-in-pipe' control whether this function
11850 decodes articles and what headers to keep (see the doc string for the
11851 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11852 is neither omitted nor the symbol `r', force including all headers
11853 regardless of the `:headers' property.  If it is the symbol `r',
11854 articles that are not decoded and include all headers will be piped
11855 no matter what the properties `:decode' and `:headers' are."
11856   (interactive (gnus-interactive "P\ny"))
11857   (require 'gnus-art)
11858   (let* ((articles (gnus-summary-work-articles n))
11859          (result-buffer "*Shell Command Output*")
11860          (all-headers (not (memq sym '(nil r))))
11861          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11862          (raw (eq sym 'r))
11863          (headers (get 'gnus-summary-save-in-pipe :headers))
11864          command result)
11865     (unless (numberp (car articles))
11866       (error "No article to pipe"))
11867     (setq command (gnus-read-shell-command
11868                    (concat "Shell command on "
11869                            (if (cdr articles)
11870                                (format "these %d articles" (length articles))
11871                              "this article")
11872                            ": ")
11873                    gnus-summary-pipe-output-default-command))
11874     (when (string-equal command "")
11875       (error "A command is required"))
11876     (when all-headers
11877       (put 'gnus-summary-save-in-pipe :headers nil))
11878     (unwind-protect
11879         (while articles
11880           (gnus-summary-goto-subject (pop articles))
11881           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11882           (when (and (get-buffer result-buffer)
11883                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11884             (setq result (concat result (with-current-buffer result-buffer
11885                                           (buffer-string))))))
11886       (put 'gnus-summary-save-in-pipe :headers headers))
11887     (unless (zerop (length result))
11888       (if (with-current-buffer (get-buffer-create result-buffer)
11889             (erase-buffer)
11890             (insert result)
11891             (prog1
11892                 (and (= (count-lines (point-min) (point)) 1)
11893                      (progn
11894                        (end-of-line 0)
11895                        (<= (current-column)
11896                            (window-width (minibuffer-window)))))
11897               (goto-char (point-min))))
11898           (message "%s" (substring result 0 -1))
11899         (message nil)
11900         (gnus-configure-windows 'pipe)))))
11901
11902 (defun gnus-summary-save-article-mail (&optional arg)
11903   "Append the current article to a Unix mail box file.
11904 If N is a positive number, save the N next articles.
11905 If N is a negative number, save the N previous articles.
11906 If N is nil and any articles have been marked with the process mark,
11907 save those articles instead."
11908   (interactive "P")
11909   (require 'gnus-art)
11910   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11911     (gnus-summary-save-article arg)))
11912
11913 (defun gnus-summary-save-article-rmail (&optional arg)
11914   "Append the current article to an rmail file.
11915 If N is a positive number, save the N next articles.
11916 If N is a negative number, save the N previous articles.
11917 If N is nil and any articles have been marked with the process mark,
11918 save those articles instead."
11919   (interactive "P")
11920   (require 'gnus-art)
11921   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11922     (gnus-summary-save-article arg)))
11923
11924 (defun gnus-summary-save-article-file (&optional arg)
11925   "Append the current article to a file.
11926 If N is a positive number, save the N next articles.
11927 If N is a negative number, save the N previous articles.
11928 If N is nil and any articles have been marked with the process mark,
11929 save those articles instead."
11930   (interactive "P")
11931   (require 'gnus-art)
11932   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11933     (gnus-summary-save-article arg)))
11934
11935 (defun gnus-summary-write-article-file (&optional arg)
11936   "Write the current article to a file, deleting the previous file.
11937 If N is a positive number, save the N next articles.
11938 If N is a negative number, save the N previous articles.
11939 If N is nil and any articles have been marked with the process mark,
11940 save those articles instead."
11941   (interactive "P")
11942   (require 'gnus-art)
11943   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11944     (gnus-summary-save-article arg)))
11945
11946 (defun gnus-summary-save-article-body-file (&optional arg)
11947   "Append the current article body to a file.
11948 If N is a positive number, save the N next articles.
11949 If N is a negative number, save the N previous articles.
11950 If N is nil and any articles have been marked with the process mark,
11951 save those articles instead."
11952   (interactive "P")
11953   (require 'gnus-art)
11954   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11955     (gnus-summary-save-article arg)))
11956
11957 (defun gnus-summary-write-article-body-file (&optional arg)
11958   "Write the current article body to a file, deleting the previous file.
11959 If N is a positive number, save the N next articles.
11960 If N is a negative number, save the N previous articles.
11961 If N is nil and any articles have been marked with the process mark,
11962 save those articles instead."
11963   (interactive "P")
11964   (require 'gnus-art)
11965   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11966     (gnus-summary-save-article arg)))
11967
11968 (defun gnus-summary-muttprint (&optional arg)
11969   "Print the current article using Muttprint.
11970 If N is a positive number, save the N next articles.
11971 If N is a negative number, save the N previous articles.
11972 If N is nil and any articles have been marked with the process mark,
11973 save those articles instead."
11974   (interactive "P")
11975   (require 'gnus-art)
11976   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11977     (gnus-summary-save-article arg t)))
11978
11979 (defun gnus-summary-pipe-message (program)
11980   "Pipe the current article through PROGRAM."
11981   (interactive "sProgram: ")
11982   (gnus-summary-select-article)
11983   (let ((mail-header-separator ""))
11984     (gnus-eval-in-buffer-window gnus-article-buffer
11985       (save-restriction
11986         (widen)
11987         (let ((start (window-start))
11988               (inhibit-read-only t))
11989           (message-pipe-buffer-body program)
11990           (set-window-start (get-buffer-window (current-buffer)) start))))))
11991
11992 (defun gnus-get-split-value (methods)
11993   "Return a value based on the split METHODS."
11994   (let (split-name method result match)
11995     (when methods
11996       (with-current-buffer gnus-original-article-buffer
11997         (save-restriction
11998           (nnheader-narrow-to-headers)
11999           (while (and methods (not split-name))
12000             (goto-char (point-min))
12001             (setq method (pop methods))
12002             (setq match (car method))
12003             (when (cond
12004                    ((stringp match)
12005                     ;; Regular expression.
12006                     (ignore-errors
12007                       (re-search-forward match nil t)))
12008                    ((functionp match)
12009                     ;; Function.
12010                     (save-restriction
12011                       (widen)
12012                       (setq result (funcall match gnus-newsgroup-name))))
12013                    ((consp match)
12014                     ;; Form.
12015                     (save-restriction
12016                       (widen)
12017                       (setq result (eval match)))))
12018               (setq split-name (cdr method))
12019               (cond ((stringp result)
12020                      (push (expand-file-name
12021                             result gnus-article-save-directory)
12022                            split-name))
12023                     ((consp result)
12024                      (setq split-name (append result split-name)))))))))
12025     (nreverse split-name)))
12026
12027 (defun gnus-valid-move-group-p (group)
12028   (and (symbolp group)
12029        (boundp group)
12030        (symbol-name group)
12031        (symbol-value group)
12032        (gnus-get-function (gnus-find-method-for-group
12033                            (symbol-name group)) 'request-accept-article t)))
12034
12035 (defun gnus-read-move-group-name (prompt default articles prefix)
12036   "Read a group name."
12037   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12038          (minibuffer-confirm-incomplete nil) ; XEmacs
12039          (prom
12040           (format "%s %s to"
12041                   prompt
12042                   (if (> (length articles) 1)
12043                       (format "these %d articles" (length articles))
12044                     "this article")))
12045          (to-newsgroup
12046           (cond
12047            ((null split-name)
12048             (gnus-group-completing-read
12049              prom
12050              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12051              nil prefix nil default))
12052            ((= 1 (length split-name))
12053             (gnus-group-completing-read
12054              prom
12055              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12056              nil prefix 'gnus-group-history (car split-name)))
12057            (t
12058             (gnus-completing-read
12059              prom (nreverse split-name) nil nil 'gnus-group-history))))
12060          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12061          encoded)
12062     (when to-newsgroup
12063       (if (or (string= to-newsgroup "")
12064               (string= to-newsgroup prefix))
12065           (setq to-newsgroup default))
12066       (unless to-newsgroup
12067         (error "No group name entered"))
12068       (setq encoded (mm-encode-coding-string
12069                      to-newsgroup
12070                      (gnus-group-name-charset to-method to-newsgroup)))
12071       (or (gnus-active encoded)
12072           (gnus-activate-group encoded nil nil to-method)
12073           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12074                                      to-newsgroup))
12075               (or (and (gnus-request-create-group encoded to-method)
12076                        (gnus-activate-group encoded nil nil to-method)
12077                        (gnus-subscribe-group encoded))
12078                   (error "Couldn't create group %s" to-newsgroup)))
12079           (error "No such group: %s" to-newsgroup))
12080       encoded)))
12081
12082 (defvar gnus-summary-save-parts-counter)
12083 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12084
12085 (defun gnus-summary-save-parts (type dir n &optional reverse)
12086   "Save parts matching TYPE to DIR.
12087 If REVERSE, save parts that do not match TYPE."
12088   (interactive
12089    (list (read-string "Save parts of type: "
12090                       (or (car gnus-summary-save-parts-type-history)
12091                           gnus-summary-save-parts-default-mime)
12092                       'gnus-summary-save-parts-type-history)
12093          (setq gnus-summary-save-parts-last-directory
12094                (read-directory-name "Save to directory: "
12095                                     gnus-summary-save-parts-last-directory
12096                                     nil t))
12097          current-prefix-arg))
12098   (gnus-summary-iterate n
12099     (let ((gnus-display-mime-function nil)
12100           gnus-article-prepare-hook
12101           gnus-article-decode-hook
12102           gnus-display-mime-function
12103           gnus-break-pages
12104           (gnus-inhibit-treatment t))
12105       (gnus-summary-select-article))
12106     (with-current-buffer gnus-article-buffer
12107       (let ((handles (or gnus-article-mime-handles
12108                          (mm-dissect-buffer nil gnus-article-loose-mime)
12109                          (and gnus-article-emulate-mime
12110                               (mm-uu-dissect))))
12111             (gnus-summary-save-parts-counter 1))
12112         (when handles
12113           (gnus-summary-save-parts-1 type dir handles reverse)
12114           (unless gnus-article-mime-handles ;; Don't destroy this case.
12115             (mm-destroy-parts handles)))))))
12116
12117 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12118   (if (stringp (car handle))
12119       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12120               (cdr handle))
12121     (when (if reverse
12122               (not (string-match type (mm-handle-media-type handle)))
12123             (string-match type (mm-handle-media-type handle)))
12124       (let ((file (expand-file-name
12125                    (gnus-map-function
12126                     mm-file-name-rewrite-functions
12127                     (file-name-nondirectory
12128                      (or
12129                       (mail-content-type-get
12130                        (mm-handle-disposition handle) 'filename)
12131                       (mail-content-type-get
12132                        (mm-handle-type handle) 'name)
12133                       (format "%s.%d.%d" gnus-newsgroup-name
12134                               (cdr gnus-article-current)
12135                               gnus-summary-save-parts-counter))))
12136                    dir)))
12137         (incf gnus-summary-save-parts-counter)
12138         (unless (file-exists-p file)
12139           (mm-save-part-to-file handle file))))))
12140
12141 ;; Summary extract commands
12142
12143 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12144   (let ((inhibit-read-only t)
12145         (article (gnus-summary-article-number))
12146         after-article b e)
12147     (unless (gnus-summary-goto-subject article)
12148       (error "No such article: %d" article))
12149     (gnus-summary-position-point)
12150     ;; If all commands are to be bunched up on one line, we collect
12151     ;; them here.
12152     (unless gnus-view-pseudos-separately
12153       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12154             files action)
12155         (while ps
12156           (setq action (cdr (assq 'action (car ps))))
12157           (setq files (list (cdr (assq 'name (car ps)))))
12158           (while (and ps (cdr ps)
12159                       (string= (or action "1")
12160                                (or (cdr (assq 'action (cadr ps))) "2")))
12161             (push (cdr (assq 'name (cadr ps))) files)
12162             (setcdr ps (cddr ps)))
12163           (when files
12164             (when (not (string-match "%s" action))
12165               (push " " files))
12166             (push " " files)
12167             (when (assq 'execute (car ps))
12168               (setcdr (assq 'execute (car ps))
12169                       (funcall (if (string-match "%s" action)
12170                                    'format 'concat)
12171                                action
12172                                (mapconcat
12173                                 (lambda (f)
12174                                   (if (equal f " ")
12175                                       f
12176                                     (shell-quote-argument f)))
12177                                 files " ")))))
12178           (setq ps (cdr ps)))))
12179     (if (and gnus-view-pseudos (not not-view))
12180         (while pslist
12181           (when (assq 'execute (car pslist))
12182             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12183                                   (eq gnus-view-pseudos 'not-confirm)))
12184           (setq pslist (cdr pslist)))
12185       (save-excursion
12186         (while pslist
12187           (setq after-article (or (cdr (assq 'article (car pslist)))
12188                                   (gnus-summary-article-number)))
12189           (gnus-summary-goto-subject after-article)
12190           (forward-line 1)
12191           (setq b (point))
12192           (insert "    " (file-name-nondirectory
12193                           (cdr (assq 'name (car pslist))))
12194                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12195           (setq e (point))
12196           (forward-line -1)             ; back to `b'
12197           (gnus-add-text-properties
12198            b (1- e) (list 'gnus-number gnus-reffed-article-number
12199                           gnus-mouse-face-prop gnus-mouse-face))
12200           (gnus-data-enter
12201            after-article gnus-reffed-article-number
12202            gnus-unread-mark b (car pslist) 0 (- e b))
12203           (setq gnus-newsgroup-unreads
12204                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12205                                          gnus-reffed-article-number))
12206           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12207           (setq pslist (cdr pslist)))))))
12208
12209 (defun gnus-pseudos< (p1 p2)
12210   (let ((c1 (cdr (assq 'action p1)))
12211         (c2 (cdr (assq 'action p2))))
12212     (and c1 c2 (string< c1 c2))))
12213
12214 (defun gnus-request-pseudo-article (props)
12215   (cond ((assq 'execute props)
12216          (gnus-execute-command (cdr (assq 'execute props)))))
12217   (let ((gnus-current-article (gnus-summary-article-number)))
12218     (gnus-run-hooks 'gnus-mark-article-hook)))
12219
12220 (defun gnus-execute-command (command &optional automatic)
12221   (save-excursion
12222     (gnus-article-setup-buffer)
12223     (set-buffer gnus-article-buffer)
12224     (setq buffer-read-only nil)
12225     (let ((command (if automatic command
12226                      (read-string "Command: " (cons command 0)))))
12227       (erase-buffer)
12228       (insert "$ " command "\n\n")
12229       (if gnus-view-pseudo-asynchronously
12230           (start-process "gnus-execute" (current-buffer) shell-file-name
12231                          shell-command-switch command)
12232         (call-process shell-file-name nil t nil
12233                       shell-command-switch command)))))
12234
12235 ;; Summary kill commands.
12236
12237 (defun gnus-summary-edit-global-kill (article)
12238   "Edit the \"global\" kill file."
12239   (interactive (list (gnus-summary-article-number)))
12240   (gnus-group-edit-global-kill article))
12241
12242 (defun gnus-summary-edit-local-kill ()
12243   "Edit a local kill file applied to the current newsgroup."
12244   (interactive)
12245   (setq gnus-current-headers (gnus-summary-article-header))
12246   (gnus-group-edit-local-kill
12247    (gnus-summary-article-number) gnus-newsgroup-name))
12248
12249 ;;; Header reading.
12250
12251 (defun gnus-read-header (id &optional header)
12252   "Read the headers of article ID and enter them into the Gnus system."
12253   (let ((group gnus-newsgroup-name)
12254         (gnus-override-method
12255          (or
12256           gnus-override-method
12257           (and (gnus-news-group-p gnus-newsgroup-name)
12258                (car (gnus-refer-article-methods)))))
12259         where)
12260     ;; First we check to see whether the header in question is already
12261     ;; fetched.
12262     (if (stringp id)
12263         ;; This is a Message-ID.
12264         (setq header (or header (gnus-id-to-header id)))
12265       ;; This is an article number.
12266       (setq header (or header (gnus-summary-article-header id))))
12267     (if (and header
12268              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12269         ;; We have found the header.
12270         header
12271       ;; We have to really fetch the header to this article.
12272       (with-current-buffer nntp-server-buffer
12273         (when (setq where (gnus-request-head id group))
12274           (nnheader-fold-continuation-lines)
12275           (goto-char (point-max))
12276           (insert ".\n")
12277           (goto-char (point-min))
12278           (insert "211 ")
12279           (princ (cond
12280                   ((numberp id) id)
12281                   ((cdr where) (cdr where))
12282                   (header (mail-header-number header))
12283                   (t gnus-reffed-article-number))
12284                  (current-buffer))
12285           (insert " Article retrieved.\n"))
12286         (if (or (not where)
12287                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12288             ()                          ; Malformed head.
12289           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12290             (when (and (stringp id)
12291                        (or
12292                         (not (string= (gnus-group-real-name group)
12293                                       (car where)))
12294                         (not (gnus-server-equal gnus-override-method
12295                                                 (gnus-group-method group)))))
12296               ;; If we fetched by Message-ID and the article came from
12297               ;; a different group (or server), we fudge some bogus
12298               ;; article numbers for this article.
12299               (mail-header-set-number header gnus-reffed-article-number))
12300             (with-current-buffer gnus-summary-buffer
12301               (decf gnus-reffed-article-number)
12302               (gnus-remove-header (mail-header-number header))
12303               (push header gnus-newsgroup-headers)
12304               (setq gnus-current-headers header)
12305               (push (mail-header-number header) gnus-newsgroup-limit)))
12306           header)))))
12307
12308 (defun gnus-remove-header (number)
12309   "Remove header NUMBER from `gnus-newsgroup-headers'."
12310   (if (and gnus-newsgroup-headers
12311            (= number (mail-header-number (car gnus-newsgroup-headers))))
12312       (pop gnus-newsgroup-headers)
12313     (let ((headers gnus-newsgroup-headers))
12314       (while (and (cdr headers)
12315                   (not (= number (mail-header-number (cadr headers)))))
12316         (pop headers))
12317       (when (cdr headers)
12318         (setcdr headers (cddr headers))))))
12319
12320 ;;;
12321 ;;; summary highlights
12322 ;;;
12323
12324 (defun gnus-highlight-selected-summary ()
12325   "Highlight selected article in summary buffer."
12326   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12327   (when gnus-summary-selected-face
12328     (save-excursion
12329       (let* ((beg (point-at-bol))
12330              (end (point-at-eol))
12331              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12332              (from (if (get-text-property beg gnus-mouse-face-prop)
12333                        beg
12334                      (or (next-single-property-change
12335                           beg gnus-mouse-face-prop nil end)
12336                          beg)))
12337              (to
12338               (if (= from end)
12339                   (- from 2)
12340                 (or (next-single-property-change
12341                      from gnus-mouse-face-prop nil end)
12342                     end))))
12343         ;; If no mouse-face prop on line we will have to = from = end,
12344         ;; so we highlight the entire line instead.
12345         (when (= (+ to 2) from)
12346           (setq from beg)
12347           (setq to end))
12348         (if gnus-newsgroup-selected-overlay
12349             ;; Move old overlay.
12350             (gnus-move-overlay
12351              gnus-newsgroup-selected-overlay from to (current-buffer))
12352           ;; Create new overlay.
12353           (gnus-overlay-put
12354            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12355            'face gnus-summary-selected-face))))))
12356
12357 (defvar gnus-summary-highlight-line-cached nil)
12358 (defvar gnus-summary-highlight-line-trigger nil)
12359
12360 (defun gnus-summary-highlight-line-0 ()
12361   (if (and (eq gnus-summary-highlight-line-trigger
12362                gnus-summary-highlight)
12363            gnus-summary-highlight-line-cached)
12364       gnus-summary-highlight-line-cached
12365     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12366           gnus-summary-highlight-line-cached
12367           (let* ((cond (list 'cond))
12368                  (c cond)
12369                  (list gnus-summary-highlight))
12370             (while list
12371               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12372                               nil))
12373               (setq c (cdr c)
12374                     list (cdr list)))
12375             (gnus-byte-compile (list 'lambda nil cond))))))
12376
12377 (defun gnus-summary-highlight-line ()
12378   "Highlight current line according to `gnus-summary-highlight'."
12379   (let* ((beg (point-at-bol))
12380          (article (or (gnus-summary-article-number) gnus-current-article))
12381          (score (or (cdr (assq article
12382                                gnus-newsgroup-scored))
12383                     gnus-summary-default-score 0))
12384          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12385          (inhibit-read-only t)
12386          (default gnus-summary-default-score)
12387          (default-high gnus-summary-default-high-score)
12388          (default-low gnus-summary-default-low-score)
12389          (uncached (and gnus-summary-use-undownloaded-faces
12390                         (memq article gnus-newsgroup-undownloaded)
12391                         (not (memq article gnus-newsgroup-cached)))))
12392     (let ((face (funcall (gnus-summary-highlight-line-0))))
12393       (unless (eq face (get-text-property beg 'face))
12394         (gnus-put-text-property-excluding-characters-with-faces
12395          beg (point-at-eol) 'face
12396          (setq face (if (boundp face) (symbol-value face) face)))
12397         (when gnus-summary-highlight-line-function
12398           (funcall gnus-summary-highlight-line-function article face))))))
12399
12400 (defun gnus-update-read-articles (group unread &optional compute)
12401   "Update the list of read articles in GROUP.
12402 UNREAD is a sorted list."
12403   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12404         (info (gnus-get-info group))
12405         (prev 1)
12406         read)
12407     (if (or (not info) (not active))
12408         ;; There is no info on this group if it was, in fact,
12409         ;; killed.  Gnus stores no information on killed groups, so
12410         ;; there's nothing to be done.
12411         ;; One could store the information somewhere temporarily,
12412         ;; perhaps...  Hmmm...
12413         ()
12414       ;; Remove any negative articles numbers.
12415       (while (and unread (< (car unread) 0))
12416         (setq unread (cdr unread)))
12417       ;; Remove any expired article numbers
12418       (while (and unread (< (car unread) (car active)))
12419         (setq unread (cdr unread)))
12420       ;; Compute the ranges of read articles by looking at the list of
12421       ;; unread articles.
12422       (while unread
12423         (when (/= (car unread) prev)
12424           (push (if (= prev (1- (car unread))) prev
12425                   (cons prev (1- (car unread))))
12426                 read))
12427         (setq prev (1+ (car unread)))
12428         (setq unread (cdr unread)))
12429       (when (<= prev (cdr active))
12430         (push (cons prev (cdr active)) read))
12431       (setq read (if (> (length read) 1) (nreverse read) read))
12432       (if compute
12433           read
12434         (save-excursion
12435           (let (setmarkundo)
12436             ;; Propagate the read marks to the backend.
12437             (when (and gnus-propagate-marks
12438                        (gnus-check-backend-function 'request-set-mark group))
12439               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12440                     (add (gnus-remove-from-range read (gnus-info-read info))))
12441                 (when (or add del)
12442                   (unless (gnus-check-group group)
12443                     (error "Can't open server for %s" group))
12444                   (gnus-request-set-mark
12445                    group (delq nil (list (if add (list add 'add '(read)))
12446                                          (if del (list del 'del '(read))))))
12447                   (setq setmarkundo
12448                         `(gnus-request-set-mark
12449                           ,group
12450                           ',(delq nil (list
12451                                        (if del (list del 'add '(read)))
12452                                        (if add (list add 'del '(read))))))))))
12453             (set-buffer gnus-group-buffer)
12454             (gnus-undo-register
12455               `(progn
12456                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12457                  (gnus-info-set-read ',info ',(gnus-info-read info))
12458                  (gnus-get-unread-articles-in-group ',info
12459                                                     (gnus-active ,group))
12460                  (gnus-group-update-group ,group t)
12461                  ,setmarkundo))))
12462         ;; Enter this list into the group info.
12463         (gnus-info-set-read info read)
12464         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12465         (gnus-get-unread-articles-in-group info (gnus-active group))
12466         t))))
12467
12468 (defun gnus-offer-save-summaries ()
12469   "Offer to save all active summary buffers."
12470   (let (buffers)
12471     ;; Go through all buffers and find all summaries.
12472     (dolist (buffer (buffer-list))
12473       (when (and (setq buffer (buffer-name buffer))
12474                  (string-match "Summary" buffer)
12475                  (with-current-buffer buffer
12476                    ;; We check that this is, indeed, a summary buffer.
12477                    (and (eq major-mode 'gnus-summary-mode)
12478                         ;; Also make sure this isn't bogus.
12479                         gnus-newsgroup-prepared
12480                         ;; Also make sure that this isn't a
12481                         ;; dead summary buffer.
12482                         (not gnus-dead-summary-mode))))
12483         (push buffer buffers)))
12484     ;; Go through all these summary buffers and offer to save them.
12485     (when buffers
12486       (save-excursion
12487         (map-y-or-n-p
12488          "Update summary buffer %s? "
12489          (lambda (buf)
12490            (switch-to-buffer buf)
12491            (gnus-summary-exit))
12492          buffers)))))
12493
12494 (defun gnus-summary-setup-default-charset ()
12495   "Setup newsgroup default charset."
12496   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12497       (setq gnus-newsgroup-charset nil)
12498     (let* ((ignored-charsets
12499             (or gnus-newsgroup-ephemeral-ignored-charsets
12500                 (append
12501                  (and gnus-newsgroup-name
12502                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12503                  gnus-newsgroup-ignored-charsets))))
12504       (setq gnus-newsgroup-charset
12505             (or gnus-newsgroup-ephemeral-charset
12506                 (and gnus-newsgroup-name
12507                      (gnus-parameter-charset gnus-newsgroup-name))
12508                 gnus-default-charset))
12509       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12510            ignored-charsets))))
12511
12512 ;;;
12513 ;;; Mime Commands
12514 ;;;
12515
12516 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12517   "Display the current article buffer fully MIME-buttonized.
12518 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12519 treated as multipart/mixed."
12520   (interactive "P")
12521   (require 'gnus-art)
12522   (let ((gnus-unbuttonized-mime-types nil)
12523         (gnus-mime-display-multipart-as-mixed show-all-parts))
12524     (gnus-summary-show-article)))
12525
12526 (defun gnus-summary-repair-multipart (article)
12527   "Add a Content-Type header to a multipart article without one."
12528   (interactive (list (gnus-summary-article-number)))
12529   (gnus-with-article article
12530     (message-narrow-to-head)
12531     (message-remove-header "Mime-Version")
12532     (goto-char (point-max))
12533     (insert "Mime-Version: 1.0\n")
12534     (widen)
12535     (when (search-forward "\n--" nil t)
12536       (let ((separator (buffer-substring (point) (point-at-eol))))
12537         (message-narrow-to-head)
12538         (message-remove-header "Content-Type")
12539         (goto-char (point-max))
12540         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12541                         separator))
12542         (widen))))
12543   (let (gnus-mark-article-hook)
12544     (gnus-summary-select-article t t nil article)))
12545
12546 (defun gnus-summary-toggle-display-buttonized ()
12547   "Toggle the buttonizing of the article buffer."
12548   (interactive)
12549   (require 'gnus-art)
12550   (if (setq gnus-inhibit-mime-unbuttonizing
12551             (not gnus-inhibit-mime-unbuttonizing))
12552       (let ((gnus-unbuttonized-mime-types nil))
12553         (gnus-summary-show-article))
12554     (gnus-summary-show-article)))
12555
12556 ;;;
12557 ;;; Generic summary marking commands
12558 ;;;
12559
12560 (defvar gnus-summary-marking-alist
12561   '((read gnus-del-mark "d")
12562     (unread gnus-unread-mark "u")
12563     (ticked gnus-ticked-mark "!")
12564     (dormant gnus-dormant-mark "?")
12565     (expirable gnus-expirable-mark "e"))
12566   "An alist of names/marks/keystrokes.")
12567
12568 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12569 (defvar gnus-summary-mark-map)
12570
12571 (defun gnus-summary-make-all-marking-commands ()
12572   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12573   (dolist (elem gnus-summary-marking-alist)
12574     (apply 'gnus-summary-make-marking-command elem)))
12575
12576 (defun gnus-summary-make-marking-command (name mark keystroke)
12577   (let ((map (make-sparse-keymap)))
12578     (define-key gnus-summary-generic-mark-map keystroke map)
12579     (dolist (lway `((next "next" next nil "n")
12580                     (next-unread "next unread" next t "N")
12581                     (prev "previous" prev nil "p")
12582                     (prev-unread "previous unread" prev t "P")
12583                     (nomove "" nil nil ,keystroke)))
12584       (let ((func (gnus-summary-make-marking-command-1
12585                    mark (car lway) lway name)))
12586         (setq func (eval func))
12587         (define-key map (nth 4 lway) func)))))
12588
12589 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12590   `(defun ,(intern
12591             (format "gnus-summary-put-mark-as-%s%s"
12592                     name (if (eq way 'nomove)
12593                              ""
12594                            (concat "-" (symbol-name way)))))
12595      (n)
12596      ,(format
12597        "Mark the current article as %s%s.
12598 If N, the prefix, then repeat N times.
12599 If N is negative, move in reverse order.
12600 The difference between N and the actual number of articles marked is
12601 returned."
12602        name (cadr lway))
12603      (interactive "p")
12604      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12605
12606 (defun gnus-summary-generic-mark (n mark move unread)
12607   "Mark N articles with MARK."
12608   (unless (eq major-mode 'gnus-summary-mode)
12609     (error "This command can only be used in the summary buffer"))
12610   (gnus-summary-show-thread)
12611   (let ((nummove
12612          (cond
12613           ((eq move 'next) 1)
12614           ((eq move 'prev) -1)
12615           (t 0))))
12616     (if (zerop nummove)
12617         (setq n 1)
12618       (when (< n 0)
12619         (setq n (abs n)
12620               nummove (* -1 nummove))))
12621     (while (and (> n 0)
12622                 (gnus-summary-mark-article nil mark)
12623                 (zerop (gnus-summary-next-subject nummove unread t)))
12624       (setq n (1- n)))
12625     (when (/= 0 n)
12626       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12627     (gnus-summary-recenter)
12628     (gnus-summary-position-point)
12629     (gnus-set-mode-line 'summary)
12630     n))
12631
12632 (defun gnus-summary-insert-articles (articles)
12633   (when (setq articles
12634               (gnus-sorted-difference articles
12635                                       (mapcar (lambda (h)
12636                                                 (mail-header-number h))
12637                                               gnus-newsgroup-headers)))
12638     (setq gnus-newsgroup-headers
12639           (gnus-merge 'list
12640                       gnus-newsgroup-headers
12641                       (gnus-fetch-headers articles)
12642                       'gnus-article-sort-by-number))
12643     ;; Suppress duplicates?
12644     (when gnus-suppress-duplicates
12645       (gnus-dup-suppress-articles))
12646
12647     (if (and gnus-fetch-old-headers
12648              (eq gnus-headers-retrieved-by 'nov))
12649         ;; We might want to build some more threads first.
12650         (if (eq gnus-fetch-old-headers 'invisible)
12651             (gnus-build-all-threads)
12652           (gnus-build-old-threads))
12653       ;; Mark the inserted articles that are unread as unread.
12654       (setq gnus-newsgroup-unreads
12655             (gnus-sorted-nunion
12656              gnus-newsgroup-unreads
12657              (gnus-sorted-nintersection
12658               (gnus-list-of-unread-articles gnus-newsgroup-name)
12659               articles)))
12660       ;; Mark the inserted articles as selected so that the information
12661       ;; of the marks having been changed by a user may be updated when
12662       ;; exiting this group.  See `gnus-summary-update-info'.
12663       (dolist (art articles)
12664         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12665     ;; Let the Gnus agent mark articles as read.
12666     (when gnus-agent
12667       (gnus-agent-get-undownloaded-list))
12668     ;; Remove list identifiers from subject
12669     (when gnus-list-identifiers
12670       (gnus-summary-remove-list-identifiers))
12671     ;; First and last article in this newsgroup.
12672     (when gnus-newsgroup-headers
12673       (setq gnus-newsgroup-begin
12674             (mail-header-number (car gnus-newsgroup-headers))
12675             gnus-newsgroup-end
12676             (mail-header-number
12677              (gnus-last-element gnus-newsgroup-headers))))
12678     (when gnus-use-scoring
12679       (gnus-possibly-score-headers))))
12680
12681 (defun gnus-summary-insert-old-articles (&optional all)
12682   "Insert all old articles in this group.
12683 If ALL is non-nil, already read articles become readable.
12684 If ALL is a number, fetch this number of articles."
12685   (interactive "P")
12686   (prog1
12687       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12688             older len)
12689         (setq older
12690               ;; Some nntp servers lie about their active range.  When
12691               ;; this happens, the active range can be in the millions.
12692               ;; Use a compressed range to avoid creating a huge list.
12693               (gnus-range-difference (list gnus-newsgroup-active) old))
12694         (setq len (gnus-range-length older))
12695         (cond
12696          ((null older) nil)
12697          ((numberp all)
12698           (if (< all len)
12699               (let ((older-range (nreverse older)))
12700                 (setq older nil)
12701
12702                 (while (> all 0)
12703                   (let* ((r (pop older-range))
12704                          (min (if (numberp r) r (car r)))
12705                          (max (if (numberp r) r (cdr r))))
12706                     (while (and (<= min max)
12707                                 (> all 0))
12708                       (push max older)
12709                       (setq all (1- all)
12710                             max (1- max))))))
12711             (setq older (gnus-uncompress-range older))))
12712          (all
12713           (setq older (gnus-uncompress-range older)))
12714          (t
12715           (when (and (numberp gnus-large-newsgroup)
12716                    (> len gnus-large-newsgroup))
12717               (let* ((cursor-in-echo-area nil)
12718                      (initial (gnus-parameter-large-newsgroup-initial
12719                                gnus-newsgroup-name))
12720                      (input
12721                       (read-string
12722                        (format
12723                         "How many articles from %s (%s %d): "
12724                         (gnus-group-decoded-name gnus-newsgroup-name)
12725                         (if initial "max" "default")
12726                         len)
12727                        (if initial
12728                            (cons (number-to-string initial)
12729                                  0)))))
12730                 (unless (string-match "^[ \t]*$" input)
12731                   (setq all (string-to-number input))
12732                   (if (< all len)
12733                       (let ((older-range (nreverse older)))
12734                         (setq older nil)
12735
12736                         (while (> all 0)
12737                           (let* ((r (pop older-range))
12738                                  (min (if (numberp r) r (car r)))
12739                                  (max (if (numberp r) r (cdr r))))
12740                             (while (and (<= min max)
12741                                         (> all 0))
12742                               (push max older)
12743                               (setq all (1- all)
12744                                     max (1- max))))))))))
12745           (setq older (gnus-uncompress-range older))))
12746         (if (not older)
12747             (message "No old news.")
12748           (gnus-summary-insert-articles older)
12749           (gnus-summary-limit (gnus-sorted-nunion old older))))
12750     (gnus-summary-position-point)))
12751
12752 (defun gnus-summary-insert-new-articles ()
12753   "Insert all new articles in this group."
12754   (interactive)
12755   (prog1
12756       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12757             (old-high gnus-newsgroup-highest)
12758             (nnmail-fetched-sources (list t))
12759             i new)
12760         (setq gnus-newsgroup-active
12761               (gnus-copy-sequence
12762                (gnus-activate-group gnus-newsgroup-name 'scan)))
12763         (setq i (cdr gnus-newsgroup-active)
12764               gnus-newsgroup-highest i)
12765         (while (> i old-high)
12766           (push i new)
12767           (decf i))
12768         (if (not new)
12769             (message "No gnus is bad news")
12770           (gnus-summary-insert-articles new)
12771           (setq gnus-newsgroup-unreads
12772                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12773           (gnus-summary-limit (gnus-sorted-nunion old new))))
12774     (gnus-summary-position-point)))
12775
12776 ;;; Bookmark support for Gnus.
12777 (declare-function bookmark-make-record-default
12778                   "bookmark" (&optional no-file no-context posn))
12779 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12780 (declare-function bookmark-default-handler "bookmark" (bmk))
12781 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12782 (defvar bookmark-yank-point)
12783 (defvar bookmark-current-buffer)
12784
12785 (defun gnus-summary-bookmark-make-record ()
12786   "Make a bookmark entry for a Gnus summary buffer."
12787   (let (pos buf)
12788     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12789       (save-restriction              ; FIXME is it necessary to widen?
12790         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12791       (setq buf "art") ; We are recording bookmark from article buffer.
12792       (setq bookmark-yank-point (point))
12793       (setq bookmark-current-buffer (current-buffer))
12794       (gnus-article-show-summary))      ; Go back in summary buffer.
12795     ;; We are now recording bookmark from summary buffer.
12796     (unless buf (setq buf "sum"))
12797     (let* ((subject (elt (gnus-summary-article-header) 1))
12798            (grp     (car gnus-article-current))
12799            (art     (cdr gnus-article-current))
12800            (head    (gnus-summary-article-header art))
12801            (id      (mail-header-id head)))
12802       `(,subject
12803         ,@(condition-case nil
12804               (bookmark-make-record-default 'no-file 'no-context pos)
12805             (wrong-number-of-arguments
12806              (bookmark-make-record-default 'point-only)))
12807         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12808         (group . ,grp) (article . ,art)
12809         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12810
12811 ;;;###autoload
12812 (defun gnus-summary-bookmark-jump (bookmark)
12813   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12814 BOOKMARK is a bookmark name or a bookmark record."
12815   (let ((group    (bookmark-prop-get bookmark 'group))
12816         (article  (bookmark-prop-get bookmark 'article))
12817         (id       (bookmark-prop-get bookmark 'message-id))
12818         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12819     (gnus-fetch-group group (list article))
12820     (gnus-summary-insert-cached-articles)
12821     (gnus-summary-goto-article id nil 'force)
12822     ;; FIXME we have to wait article buffer is ready (only large buffer)
12823     ;; Is there a better solution to know that?
12824     ;; If we don't wait `bookmark-default-handler' will have no chance
12825     ;; to set position. However there is no error, just wrong pos.
12826     (sit-for 1)
12827     (when (string= buf "Gnus-art")
12828       (other-window 1))
12829     (bookmark-default-handler
12830      `(""
12831        (buffer . ,(current-buffer))
12832        . ,(bookmark-get-bookmark-record bookmark)))))
12833
12834 (gnus-summary-make-all-marking-commands)
12835
12836 (gnus-ems-redefine)
12837
12838 (provide 'gnus-sum)
12839
12840 (run-hooks 'gnus-sum-load-hook)
12841
12842 ;; Local Variables:
12843 ;; coding: iso-8859-1
12844 ;; End:
12845
12846 ;;; gnus-sum.el ends here