Make subthread sorting customizable and add docs for it.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2013 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-refer-thread-use-nnir nil
122   "*Use nnir to search an entire server when referring threads. A
123 nil value will only search for thread-related articles in the
124 current group."
125   :version "24.1"
126   :group 'gnus-thread
127   :type 'boolean)
128
129 (defcustom gnus-summary-make-false-root 'adopt
130   "*nil means that Gnus won't gather loose threads.
131 If the root of a thread has expired or been read in a previous
132 session, the information necessary to build a complete thread has been
133 lost.  Instead of having many small sub-threads from this original thread
134 scattered all over the summary buffer, Gnus can gather them.
135
136 If non-nil, Gnus will try to gather all loose sub-threads from an
137 original thread into one large thread.
138
139 If this variable is non-nil, it should be one of `none', `adopt',
140 `dummy' or `empty'.
141
142 If this variable is `none', Gnus will not make a false root, but just
143 present the sub-threads after another.
144 If this variable is `dummy', Gnus will create a dummy root that will
145 have all the sub-threads as children.
146 If this variable is `adopt', Gnus will make one of the \"children\"
147 the parent and mark all the step-children as such.
148 If this variable is `empty', the \"children\" are printed with empty
149 subject fields.  (Or rather, they will be printed with a string
150 given by the `gnus-summary-same-subject' variable.)"
151   :group 'gnus-thread
152   :type '(choice (const :tag "off" nil)
153                  (const none)
154                  (const dummy)
155                  (const adopt)
156                  (const empty)))
157
158 (defcustom gnus-summary-make-false-root-always nil
159   "Always make a false dummy root."
160   :version "22.1"
161   :group 'gnus-thread
162   :type 'boolean)
163
164 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
165   "*A regexp to match subjects to be excluded from loose thread gathering.
166 As loose thread gathering is done on subjects only, that means that
167 there can be many false gatherings performed.  By rooting out certain
168 common subjects, gathering might become saner."
169   :group 'gnus-thread
170   :type 'regexp)
171
172 (defcustom gnus-summary-gather-subject-limit nil
173   "*Maximum length of subject comparisons when gathering loose threads.
174 Use nil to compare full subjects.  Setting this variable to a low
175 number will help gather threads that have been corrupted by
176 newsreaders chopping off subject lines, but it might also mean that
177 unrelated articles that have subject that happen to begin with the
178 same few characters will be incorrectly gathered.
179
180 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
181 comparing subjects."
182   :group 'gnus-thread
183   :type '(choice (const :tag "off" nil)
184                  (const fuzzy)
185                  (sexp :menu-tag "on" t)))
186
187 (defcustom gnus-simplify-subject-functions nil
188   "List of functions taking a string argument that simplify subjects.
189 The functions are applied recursively.
190
191 Useful functions to put in this list include:
192 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
193 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
194   :group 'gnus-thread
195   :type '(repeat function))
196
197 (defcustom gnus-simplify-ignored-prefixes nil
198   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
199   :group 'gnus-thread
200   :type '(choice (const :tag "off" nil)
201                  regexp))
202
203 (defcustom gnus-build-sparse-threads nil
204   "*If non-nil, fill in the gaps in threads.
205 If `some', only fill in the gaps that are needed to tie loose threads
206 together.  If `more', fill in all leaf nodes that Gnus can find.  If
207 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
208   :group 'gnus-thread
209   :type '(choice (const :tag "off" nil)
210                  (const some)
211                  (const more)
212                  (sexp :menu-tag "all" t)))
213
214 (defcustom gnus-summary-thread-gathering-function
215   'gnus-gather-threads-by-subject
216   "*Function used for gathering loose threads.
217 There are two pre-defined functions: `gnus-gather-threads-by-subject',
218 which only takes Subjects into consideration; and
219 `gnus-gather-threads-by-references', which compared the References
220 headers of the articles to find matches."
221   :group 'gnus-thread
222   :type '(radio (function-item gnus-gather-threads-by-subject)
223                 (function-item gnus-gather-threads-by-references)
224                 (function :tag "other")))
225
226 (defcustom gnus-summary-same-subject ""
227   "*String indicating that the current article has the same subject as the previous.
228 This variable will only be used if the value of
229 `gnus-summary-make-false-root' is `empty'."
230   :group 'gnus-summary-format
231   :type 'string)
232
233 (defcustom gnus-summary-goto-unread nil
234   "*If t, many commands will go to the next unread article.
235 This applies to marking commands as well as other commands that
236 \"naturally\" select the next article, like, for instance, `SPC' at
237 the end of an article.
238
239 If nil, the marking commands do NOT go to the next unread article
240 \(they go to the next article instead).  If `never', commands that
241 usually go to the next unread article, will go to the next article,
242 whether it is read or not."
243   :version "24.1"
244   :group 'gnus-summary-marks
245   :link '(custom-manual "(gnus)Setting Marks")
246   :type '(choice (const :tag "off" nil)
247                  (const never)
248                  (sexp :menu-tag "on" t)))
249
250 (defcustom gnus-summary-default-score 0
251   "*Default article score level.
252 All scores generated by the score files will be added to this score.
253 If this variable is nil, scoring will be disabled."
254   :group 'gnus-score-default
255   :type '(choice (const :tag "disable")
256                  integer))
257
258 (defcustom gnus-summary-default-high-score 0
259   "*Default threshold for a high scored article.
260 An article will be highlighted as high scored if its score is greater
261 than this score."
262   :version "22.1"
263   :group 'gnus-score-default
264   :type 'integer)
265
266 (defcustom gnus-summary-default-low-score 0
267   "*Default threshold for a low scored article.
268 An article will be highlighted as low scored if its score is smaller
269 than this score."
270   :version "22.1"
271   :group 'gnus-score-default
272   :type 'integer)
273
274 (defcustom gnus-summary-zcore-fuzz 0
275   "*Fuzziness factor for the zcore in the summary buffer.
276 Articles with scores closer than this to `gnus-summary-default-score'
277 will not be marked."
278   :group 'gnus-summary-format
279   :type 'integer)
280
281 (defcustom gnus-simplify-subject-fuzzy-regexp nil
282   "*Strings to be removed when doing fuzzy matches.
283 This can either be a regular expression or list of regular expressions
284 that will be removed from subject strings if fuzzy subject
285 simplification is selected."
286   :group 'gnus-thread
287   :type '(repeat regexp))
288
289 (defcustom gnus-show-threads t
290   "*If non-nil, display threads in summary mode."
291   :group 'gnus-thread
292   :type 'boolean)
293
294 (defcustom gnus-thread-hide-subtree nil
295   "*If non-nil, hide all threads initially.
296 This can be a predicate specifier which says which threads to hide.
297 If threads are hidden, you have to run the command
298 `gnus-summary-show-thread' by hand or select an article."
299   :group 'gnus-thread
300   :type '(radio (sexp :format "Non-nil\n"
301                       :match (lambda (widget value)
302                                (not (or (consp value) (functionp value))))
303                       :value t)
304                 (const nil)
305                 (sexp :tag "Predicate specifier")))
306
307 (defcustom gnus-thread-hide-killed t
308   "*If non-nil, hide killed threads automatically."
309   :group 'gnus-thread
310   :type 'boolean)
311
312 (defcustom gnus-thread-ignore-subject t
313   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
314 If nil, articles that have different subjects from their parents will
315 start separate threads."
316   :group 'gnus-thread
317   :type 'boolean)
318
319 (defcustom gnus-thread-operation-ignore-subject t
320   "*If non-nil, subjects will be ignored when doing thread commands.
321 This affects commands like `gnus-summary-kill-thread' and
322 `gnus-summary-lower-thread'.
323
324 If this variable is nil, articles in the same thread with different
325 subjects will not be included in the operation in question.  If this
326 variable is `fuzzy', only articles that have subjects that are fuzzily
327 equal will be included."
328   :group 'gnus-thread
329   :type '(choice (const :tag "off" nil)
330                  (const fuzzy)
331                  (sexp :tag "on" t)))
332
333 (defcustom gnus-thread-indent-level 4
334   "*Number that says how much each sub-thread should be indented."
335   :group 'gnus-thread
336   :type 'integer)
337
338 (defcustom gnus-auto-extend-newsgroup t
339   "*If non-nil, extend newsgroup forward and backward when requested."
340   :group 'gnus-summary-choose
341   :type 'boolean)
342
343 (defcustom gnus-auto-select-first t
344   "If non-nil, select an article on group entry.
345 An article is selected automatically when entering a group
346 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
347 `gnus-summary-catchup-and-goto-next-group'.
348
349 Which article is selected is controlled by the variable
350 `gnus-auto-select-subject'.
351
352 If you want to prevent automatic selection of articles in some
353 newsgroups, set the variable to nil in `gnus-select-group-hook'."
354   ;; Commands include...
355   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
356   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
357   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
358   :group 'gnus-group-select
359   :type '(choice (const :tag "none" nil)
360                  (sexp :menu-tag "first" t)))
361
362 (defcustom gnus-auto-select-subject 'unseen-or-unread
363   "*Says what subject to place under point when entering a group.
364
365 This variable can either be the symbols `first' (place point on the
366 first subject), `unread' (place point on the subject line of the first
367 unread article), `best' (place point on the subject line of the
368 highest-scored article), `unseen' (place point on the subject line of
369 the first unseen article), `unseen-or-unread' (place point on the subject
370 line of the first unseen article or, if all articles have been seen, on the
371 subject line of the first unread article), or a function to be called to
372 place point on some subject line."
373   :version "24.1"
374   :group 'gnus-group-select
375   :type '(choice (const best)
376                  (const unread)
377                  (const first)
378                  (const unseen)
379                  (const unseen-or-unread)
380                  (function :tag "Function to call")))
381
382 (defcustom gnus-auto-select-next t
383   "*If non-nil, offer to go to the next group from the end of the previous.
384 If the value is t and the next newsgroup is empty, Gnus will exit
385 summary mode and go back to group mode.  If the value is neither nil
386 nor t, Gnus will select the following unread newsgroup.  In
387 particular, if the value is the symbol `quietly', the next unread
388 newsgroup will be selected without any confirmation, and if it is
389 `almost-quietly', the next group will be selected without any
390 confirmation if you are located on the last article in the group.
391 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
392 will go to the next group without confirmation."
393   :group 'gnus-summary-maneuvering
394   :type '(choice (const :tag "off" nil)
395                  (const quietly)
396                  (const almost-quietly)
397                  (const slightly-quietly)
398                  (sexp :menu-tag "on" t)))
399
400 (defcustom gnus-auto-select-same nil
401   "*If non-nil, select the next article with the same subject.
402 If there are no more articles with the same subject, go to
403 the first unread article."
404   :group 'gnus-summary-maneuvering
405   :type 'boolean)
406
407 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
408   "What article should be selected after exiting an ephemeral group.
409 Valid values include:
410
411 `next'
412   Select the next article.
413 `next-unread'
414   Select the next unread article.
415 `next-noselect'
416   Move the cursor to the next article.  This is the default.
417 `next-unread-noselect'
418   Move the cursor to the next unread article.
419
420 If it has any other value or there is no next (unread) article, the
421 article selected before entering to the ephemeral group will appear."
422   :version "23.1" ;; No Gnus
423   :group 'gnus-summary-maneuvering
424   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
425                  (const next) (const next-unread)
426                  (const next-noselect) (const next-unread-noselect)
427                  (sexp :tag "other" :value nil)))
428
429 (defcustom gnus-auto-goto-ignores 'unfetched
430   "*Says how to handle unfetched articles when maneuvering.
431
432 This variable can either be the symbols nil (maneuver to any
433 article), `undownloaded' (maneuvering while unplugged ignores articles
434 that have not been fetched), `always-undownloaded' (maneuvering always
435 ignores articles that have not been fetched), `unfetched' (maneuvering
436 ignores articles whose headers have not been fetched).
437
438 NOTE: The list of unfetched articles will always be nil when plugged
439 and, when unplugged, a subset of the undownloaded article list."
440   :version "22.1"
441   :group 'gnus-summary-maneuvering
442   :type '(choice (const :tag "None" nil)
443                  (const :tag "Undownloaded when unplugged" undownloaded)
444                  (const :tag "Undownloaded" always-undownloaded)
445                  (const :tag "Unfetched" unfetched)))
446
447 (defcustom gnus-summary-check-current nil
448   "*If non-nil, consider the current article when moving.
449 The \"unread\" movement commands will stay on the same line if the
450 current article is unread."
451   :group 'gnus-summary-maneuvering
452   :type 'boolean)
453
454 (defcustom gnus-auto-center-summary
455   (max (or (bound-and-true-p scroll-margin) 0) 2)
456   "*If non-nil, always center the current summary buffer.
457 In particular, if `vertical' do only vertical recentering.  If non-nil
458 and non-`vertical', do both horizontal and vertical recentering."
459   :group 'gnus-summary-maneuvering
460   :type '(choice (const :tag "none" nil)
461                  (const vertical)
462                  (integer :tag "height")
463                  (sexp :menu-tag "both" t)))
464
465 (defcustom gnus-auto-center-group t
466   "If non-nil, always center the group buffer."
467   :group 'gnus-summary-maneuvering
468   :type 'boolean)
469
470 (defcustom gnus-show-all-headers nil
471   "*If non-nil, don't hide any headers."
472   :group 'gnus-article-hiding
473   :group 'gnus-article-headers
474   :type 'boolean)
475
476 (defcustom gnus-summary-ignore-duplicates nil
477   "*If non-nil, ignore articles with identical Message-ID headers."
478   :group 'gnus-summary
479   :type 'boolean)
480
481 (defcustom gnus-single-article-buffer nil
482   "*If non-nil, display all articles in the same buffer.
483 If nil, each group will get its own article buffer."
484   :version "24.1"
485   :group 'gnus-article-various
486   :type 'boolean)
487
488 (defcustom gnus-widen-article-window nil
489   "If non-nil, selecting the article buffer will display only the article buffer."
490   :version "24.1"
491   :group 'gnus-article-various
492   :type 'boolean)
493
494 (defcustom gnus-break-pages t
495   "*If non-nil, do page breaking on articles.
496 The page delimiter is specified by the `gnus-page-delimiter'
497 variable."
498   :group 'gnus-article-various
499   :type 'boolean)
500
501 (defcustom gnus-move-split-methods nil
502   "*Variable used to suggest where articles are to be moved to.
503 It uses the same syntax as the `gnus-split-methods' variable.
504 However, whereas `gnus-split-methods' specifies file names as targets,
505 this variable specifies group names."
506   :group 'gnus-summary-mail
507   :type '(repeat (choice (list :value (fun) function)
508                          (cons :value ("" "") regexp (repeat string))
509                          (sexp :value nil))))
510
511 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
512   "Function used to compute default prefix for article move/copy/etc prompts.
513 The function should take one argument, a group name, and return a
514 string with the suggested prefix."
515   :group 'gnus-summary-mail
516   :type 'function)
517
518 ;; FIXME: Although the custom type is `character' for the following variables,
519 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
520
521 (defcustom gnus-unread-mark ?           ;Whitespace
522   "*Mark used for unread articles."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-ticked-mark ?!
527   "*Mark used for ticked articles."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-dormant-mark ??
532   "*Mark used for dormant articles."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-del-mark ?r
537   "*Mark used for del'd articles."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-read-mark ?R
542   "*Mark used for read articles."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-expirable-mark ?E
547   "*Mark used for expirable articles."
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-killed-mark ?K
552   "*Mark used for killed articles."
553   :group 'gnus-summary-marks
554   :type 'character)
555
556 (defcustom gnus-spam-mark ?$
557   "*Mark used for spam articles."
558   :version "22.1"
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-kill-file-mark ?X
563   "*Mark used for articles killed by kill files."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-low-score-mark ?Y
568   "*Mark used for articles with a low score."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-catchup-mark ?C
573   "*Mark used for articles that are caught up."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-replied-mark ?A
578   "*Mark used for articles that have been replied to."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-forwarded-mark ?F
583   "*Mark used for articles that have been forwarded."
584   :version "22.1"
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-recent-mark ?N
589   "*Mark used for articles that are recent."
590   :version "22.1"
591   :group 'gnus-summary-marks
592   :type 'character)
593
594 (defcustom gnus-cached-mark ?*
595   "*Mark used for articles that are in the cache."
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-saved-mark ?S
600   "*Mark used for articles that have been saved."
601   :group 'gnus-summary-marks
602   :type 'character)
603
604 (defcustom gnus-unseen-mark ?.
605   "*Mark used for articles that haven't been seen."
606   :version "22.1"
607   :group 'gnus-summary-marks
608   :type 'character)
609
610 (defcustom gnus-no-mark ?               ;Whitespace
611   "*Mark used for articles that have no other secondary mark."
612   :version "22.1"
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-ancient-mark ?O
617   "*Mark used for ancient articles."
618   :group 'gnus-summary-marks
619   :type 'character)
620
621 (defcustom gnus-sparse-mark ?Q
622   "*Mark used for sparsely reffed articles."
623   :group 'gnus-summary-marks
624   :type 'character)
625
626 (defcustom gnus-canceled-mark ?G
627   "*Mark used for canceled articles."
628   :group 'gnus-summary-marks
629   :type 'character)
630
631 (defcustom gnus-duplicate-mark ?M
632   "*Mark used for duplicate articles."
633   :group 'gnus-summary-marks
634   :type 'character)
635
636 (defcustom gnus-undownloaded-mark ?-
637   "*Mark used for articles that weren't downloaded."
638   :version "22.1"
639   :group 'gnus-summary-marks
640   :type 'character)
641
642 (defcustom gnus-downloaded-mark ?+
643   "*Mark used for articles that were downloaded."
644   :group 'gnus-summary-marks
645   :type 'character)
646
647 (defcustom gnus-downloadable-mark ?%
648   "*Mark used for articles that are to be downloaded."
649   :group 'gnus-summary-marks
650   :type 'character)
651
652 (defcustom gnus-unsendable-mark ?=
653   "*Mark used for articles that won't be sent."
654   :group 'gnus-summary-marks
655   :type 'character)
656
657 (defcustom gnus-score-over-mark ?+
658   "*Score mark used for articles with high scores."
659   :group 'gnus-summary-marks
660   :type 'character)
661
662 (defcustom gnus-score-below-mark ?-
663   "*Score mark used for articles with low scores."
664   :group 'gnus-summary-marks
665   :type 'character)
666
667 (defcustom gnus-empty-thread-mark ?     ;Whitespace
668   "*There is no thread under the article."
669   :group 'gnus-summary-marks
670   :type 'character)
671
672 (defcustom gnus-not-empty-thread-mark ?=
673   "*There is a thread under the article."
674   :group 'gnus-summary-marks
675   :type 'character)
676
677 (defcustom gnus-view-pseudo-asynchronously nil
678   "*If non-nil, Gnus will view pseudo-articles asynchronously."
679   :group 'gnus-extract-view
680   :type 'boolean)
681
682 (defcustom gnus-auto-expirable-marks
683   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
684         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
685         gnus-duplicate-mark)
686   "*The list of marks converted into expiration if a group is auto-expirable."
687   :version "24.1"
688   :group 'gnus-summary
689   :type '(repeat character))
690
691 (defcustom gnus-inhibit-user-auto-expire t
692   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
693   :version "21.1"
694   :group 'gnus-summary
695   :type 'boolean)
696
697 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
698   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
699 If nil, the expirable marks will be unchanged except that the marks
700 will be removed when copying or moving articles to a group that has
701 not turned auto-expire on.  If non-nil, articles that have been read
702 will be marked as expirable when being copied or moved to a group in
703 which auto-expire is turned on."
704   :version "23.2"
705   :type 'boolean
706   :group 'gnus-summary-marks)
707
708 (defcustom gnus-view-pseudos nil
709   "*If `automatic', pseudo-articles will be viewed automatically.
710 If `not-confirm', pseudos will be viewed automatically, and the user
711 will not be asked to confirm the command."
712   :group 'gnus-extract-view
713   :type '(choice (const :tag "off" nil)
714                  (const automatic)
715                  (const not-confirm)))
716
717 (defcustom gnus-view-pseudos-separately t
718   "*If non-nil, one pseudo-article will be created for each file to be viewed.
719 If nil, all files that use the same viewing command will be given as a
720 list of parameters to that command."
721   :group 'gnus-extract-view
722   :type 'boolean)
723
724 (defcustom gnus-insert-pseudo-articles t
725   "*If non-nil, insert pseudo-articles when decoding articles."
726   :group 'gnus-extract-view
727   :type 'boolean)
728
729 (defcustom gnus-summary-dummy-line-format
730   "   %(:                             :%) %S\n"
731   "*The format specification for the dummy roots in the summary buffer.
732 It works along the same lines as a normal formatting string,
733 with some simple extensions.
734
735 %S  The subject
736
737 General format specifiers can also be used.
738 See `(gnus)Formatting Variables'."
739   :link '(custom-manual "(gnus)Formatting Variables")
740   :group 'gnus-threading
741   :type 'string)
742
743 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
744   "*The format specification for the summary mode line.
745 It works along the same lines as a normal formatting string,
746 with some simple extensions:
747
748 %G  Group name
749 %p  Unprefixed group name
750 %A  Current article number
751 %z  Current article score
752 %V  Gnus version
753 %U  Number of unread articles in the group
754 %e  Number of unselected articles in the group
755 %Z  A string with unread/unselected article counts
756 %g  Shortish group name
757 %S  Subject of the current article
758 %u  User-defined spec
759 %s  Current score file name
760 %d  Number of dormant articles
761 %r  Number of articles that have been marked as read in this session
762 %E  Number of articles expunged by the score files"
763   :group 'gnus-summary-format
764   :type 'string)
765
766 (defcustom gnus-list-identifiers nil
767   "Regexp that matches list identifiers to be removed from subject.
768 This can also be a list of regexps."
769   :version "21.1"
770   :group 'gnus-summary-format
771   :group 'gnus-article-hiding
772   :type '(choice (const :tag "none" nil)
773                  (regexp :value ".*")
774                  (repeat :value (".*") regexp)))
775
776 (defcustom gnus-summary-mark-below 0
777   "*Mark all articles with a score below this variable as read.
778 This variable is local to each summary buffer and usually set by the
779 score file."
780   :group 'gnus-score-default
781   :type 'integer)
782
783 (defun gnus-widget-reversible-match (widget value)
784   "Ignoring WIDGET, convert VALUE to internal form.
785 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
786   ;; (debug value)
787   (or (symbolp value)
788       (and (listp value)
789            (eq (length value) 2)
790            (eq (nth 0 value) 'not)
791            (symbolp (nth 1 value)))))
792
793 (defun gnus-widget-reversible-to-internal (widget value)
794   "Ignoring WIDGET, convert VALUE to internal form.
795 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
796 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
797   ;; (debug value)
798   (if (atom value)
799       (list value nil)
800     (list (nth 1 value) t)))
801
802 (defun gnus-widget-reversible-to-external (widget value)
803   "Ignoring WIDGET, convert VALUE to external form.
804 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
805 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
806   ;; (debug value)
807   (if (nth 1 value)
808       (list 'not (nth 0 value))
809     (nth 0 value)))
810
811 (define-widget 'gnus-widget-reversible 'group
812   "A `group' that convert values."
813   :match 'gnus-widget-reversible-match
814   :value-to-internal 'gnus-widget-reversible-to-internal
815   :value-to-external 'gnus-widget-reversible-to-external)
816
817 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
818   "*List of functions used for sorting articles in the summary buffer.
819
820 Each function takes two articles and returns non-nil if the first
821 article should be sorted before the other.  If you use more than one
822 function, the primary sort function should be the last.  You should
823 probably always include `gnus-article-sort-by-number' in the list of
824 sorting functions -- preferably first.  Also note that sorting by date
825 is often much slower than sorting by number, and the sorting order is
826 very similar.  (Sorting by date means sorting by the time the message
827 was sent, sorting by number means sorting by arrival time.)
828
829 Each item can also be a list `(not F)' where F is a function;
830 this reverses the sort order.
831
832 Ready-made functions include `gnus-article-sort-by-number',
833 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
834 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
835 and `gnus-article-sort-by-score'.
836
837 When threading is turned on, the variable `gnus-thread-sort-functions'
838 controls how articles are sorted."
839   :group 'gnus-summary-sort
840   :type '(repeat (gnus-widget-reversible
841                   (choice (function-item gnus-article-sort-by-number)
842                           (function-item gnus-article-sort-by-author)
843                           (function-item gnus-article-sort-by-subject)
844                           (function-item gnus-article-sort-by-date)
845                           (function-item gnus-article-sort-by-score)
846                           (function-item gnus-article-sort-by-random)
847                           (function :tag "other"))
848                   (boolean :tag "Reverse order"))))
849
850 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
851   "*List of functions used for sorting threads in the summary buffer.
852 By default, threads are sorted by article number.
853
854 Each function takes two threads and returns non-nil if the first
855 thread should be sorted before the other.  If you use more than one
856 function, the primary sort function should be the last.  You should
857 probably always include `gnus-thread-sort-by-number' in the list of
858 sorting functions -- preferably first.  Also note that sorting by date
859 is often much slower than sorting by number, and the sorting order is
860 very similar.  (Sorting by date means sorting by the time the message
861 was sent, sorting by number means sorting by arrival time.)
862
863 Each list item can also be a list `(not F)' where F is a
864 function; this specifies reversed sort order.
865
866 Ready-made functions include `gnus-thread-sort-by-number',
867 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
868 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
869 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
870 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
871 and `gnus-thread-sort-by-total-score' (see
872 `gnus-thread-score-function').
873
874 When threading is turned off, the variable
875 `gnus-article-sort-functions' controls how articles are sorted.
876
877 By default, threads and their subthreads are sorted according to
878 the value of this variable.  To use a different sorting order for
879 subthreads, customize `gnus-subthread-sort-functions'."
880   :group 'gnus-summary-sort
881   :type '(repeat
882           (gnus-widget-reversible
883            (choice (function-item gnus-thread-sort-by-number)
884                    (function-item gnus-thread-sort-by-author)
885                    (function-item gnus-thread-sort-by-recipient)
886                    (function-item gnus-thread-sort-by-subject)
887                    (function-item gnus-thread-sort-by-date)
888                    (function-item gnus-thread-sort-by-score)
889                    (function-item gnus-thread-sort-by-most-recent-number)
890                    (function-item gnus-thread-sort-by-most-recent-date)
891                    (function-item gnus-thread-sort-by-random)
892                    (function-item gnus-thread-sort-by-total-score)
893                    (function :tag "other"))
894            (boolean :tag "Reverse order"))))
895
896 (defcustom gnus-subthread-sort-functions 'gnus-thread-sort-functions
897   "*List of functions used for sorting subthreads in the summary buffer.
898 By default, subthreads are sorted the same as threads, i.e.,
899 according to the value of `gnus-thread-sort-functions'."
900   :group 'gnus-summary-sort
901   :type '(choice
902           (const :tag "Sort subthreads like threads" gnus-thread-sort-functions)
903           (repeat
904            (gnus-widget-reversible
905             (choice (function-item gnus-thread-sort-by-number)
906                     (function-item gnus-thread-sort-by-author)
907                     (function-item gnus-thread-sort-by-recipient)
908                     (function-item gnus-thread-sort-by-subject)
909                     (function-item gnus-thread-sort-by-date)
910                     (function-item gnus-thread-sort-by-score)
911                     (function-item gnus-thread-sort-by-most-recent-number)
912                     (function-item gnus-thread-sort-by-most-recent-date)
913                     (function-item gnus-thread-sort-by-random)
914                     (function-item gnus-thread-sort-by-total-score)
915                     (function :tag "other"))
916             (boolean :tag "Reverse order")))))
917
918 (defcustom gnus-thread-score-function '+
919   "*Function used for calculating the total score of a thread.
920
921 The function is called with the scores of the article and each
922 subthread and should then return the score of the thread.
923
924 Some functions you can use are `+', `max', or `min'."
925   :group 'gnus-summary-sort
926   :type 'function)
927
928 (defcustom gnus-summary-expunge-below nil
929   "All articles that have a score less than this variable will be expunged.
930 This variable is local to the summary buffers."
931   :group 'gnus-score-default
932   :type '(choice (const :tag "off" nil)
933                  integer))
934
935 (defcustom gnus-thread-expunge-below nil
936   "All threads that have a total score less than this variable will be expunged.
937 See `gnus-thread-score-function' for en explanation of what a
938 \"thread score\" is.
939
940 This variable is local to the summary buffers."
941   :group 'gnus-threading
942   :group 'gnus-score-default
943   :type '(choice (const :tag "off" nil)
944                  integer))
945
946 (defcustom gnus-summary-mode-hook nil
947   "*A hook for Gnus summary mode.
948 This hook is run before any variables are set in the summary buffer."
949   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
950   :group 'gnus-summary-various
951   :type 'hook)
952
953 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
954 (when (featurep 'xemacs)
955   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
956   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
957   (add-hook 'gnus-summary-mode-hook
958             'gnus-xmas-switch-horizontal-scrollbar-off))
959
960 (defcustom gnus-summary-menu-hook nil
961   "*Hook run after the creation of the summary mode menu."
962   :group 'gnus-summary-visual
963   :type 'hook)
964
965 (defcustom gnus-summary-exit-hook nil
966   "*A hook called on exit from the summary buffer.
967 It will be called with point in the group buffer."
968   :group 'gnus-summary-exit
969   :type 'hook)
970
971 (defcustom gnus-summary-prepare-hook nil
972   "*A hook called after the summary buffer has been generated.
973 If you want to modify the summary buffer, you can use this hook."
974   :group 'gnus-summary-various
975   :type 'hook)
976
977 (defcustom gnus-summary-prepared-hook nil
978   "*A hook called as the last thing after the summary buffer has been generated."
979   :group 'gnus-summary-various
980   :type 'hook)
981
982 (defcustom gnus-summary-generate-hook nil
983   "*A hook run just before generating the summary buffer.
984 This hook is commonly used to customize threading variables and the
985 like."
986   :group 'gnus-summary-various
987   :type 'hook)
988
989 (defcustom gnus-select-group-hook nil
990   "*A hook called when a newsgroup is selected.
991
992 If you'd like to simplify subjects like the
993 `gnus-summary-next-same-subject' command does, you can use the
994 following hook:
995
996  (add-hook gnus-select-group-hook
997            (lambda ()
998              (mapcar (lambda (header)
999                        (mail-header-set-subject
1000                         header
1001                         (gnus-simplify-subject
1002                          (mail-header-subject header) 're-only)))
1003                      gnus-newsgroup-headers)))"
1004   :group 'gnus-group-select
1005   :type 'hook)
1006
1007 (defcustom gnus-select-article-hook nil
1008   "*A hook called when an article is selected."
1009   :group 'gnus-summary-choose
1010   :options '(gnus-agent-fetch-selected-article)
1011   :type 'hook)
1012
1013 (defcustom gnus-visual-mark-article-hook
1014   (list 'gnus-highlight-selected-summary)
1015   "*Hook run after selecting an article in the summary buffer.
1016 It is meant to be used for highlighting the article in some way.  It
1017 is not run if `gnus-visual' is nil."
1018   :group 'gnus-summary-visual
1019   :type 'hook)
1020
1021 (defcustom gnus-parse-headers-hook nil
1022   "*A hook called before parsing the headers."
1023   :group 'gnus-various
1024   :type 'hook)
1025
1026 (defcustom gnus-exit-group-hook nil
1027   "*A hook called when exiting summary mode.
1028 This hook is not called from the non-updating exit commands like `Q'."
1029   :group 'gnus-various
1030   :type 'hook)
1031
1032 (defcustom gnus-summary-update-hook nil
1033   "*A hook called when a summary line is changed.
1034 The hook will not be called if `gnus-visual' is nil.
1035
1036 The default function `gnus-summary-highlight-line' will
1037 highlight the line according to the `gnus-summary-highlight'
1038 variable."
1039   :group 'gnus-summary-visual
1040   :type 'hook)
1041
1042 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1043   "*A hook called when an article is selected for the first time.
1044 The hook is intended to mark an article as read (or unread)
1045 automatically when it is selected."
1046   :group 'gnus-summary-choose
1047   :type 'hook)
1048
1049 (defcustom gnus-group-no-more-groups-hook nil
1050   "*A hook run when returning to group mode having no more (unread) groups."
1051   :group 'gnus-group-select
1052   :type 'hook)
1053
1054 (defcustom gnus-ps-print-hook nil
1055   "*A hook run before ps-printing something from Gnus."
1056   :group 'gnus-summary
1057   :type 'hook)
1058
1059 (defcustom gnus-summary-article-move-hook nil
1060   "*A hook called after an article is moved, copied, respooled, or crossposted."
1061   :version "22.1"
1062   :group 'gnus-summary
1063   :type 'hook)
1064
1065 (defcustom gnus-summary-article-delete-hook nil
1066   "*A hook called after an article is deleted."
1067   :version "22.1"
1068   :group 'gnus-summary
1069   :type 'hook)
1070
1071 (defcustom gnus-summary-article-expire-hook nil
1072   "*A hook called after an article is expired."
1073   :version "22.1"
1074   :group 'gnus-summary
1075   :type 'hook)
1076
1077 (defcustom gnus-summary-display-arrow
1078   (and (fboundp 'display-graphic-p)
1079        (display-graphic-p))
1080   "*If non-nil, display an arrow highlighting the current article."
1081   :version "22.1"
1082   :group 'gnus-summary
1083   :type 'boolean)
1084
1085 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1086   "Face used for highlighting the current article in the summary buffer."
1087   :group 'gnus-summary-visual
1088   :type 'face)
1089
1090 (defvar gnus-tmp-downloaded nil)
1091
1092 (defcustom gnus-summary-highlight
1093   '(((eq mark gnus-canceled-mark)
1094      . gnus-summary-cancelled)
1095     ((and uncached (> score default-high))
1096      . gnus-summary-high-undownloaded)
1097     ((and uncached (< score default-low))
1098      . gnus-summary-low-undownloaded)
1099     (uncached
1100      . gnus-summary-normal-undownloaded)
1101     ((and (> score default-high)
1102           (or (eq mark gnus-dormant-mark)
1103               (eq mark gnus-ticked-mark)))
1104      . gnus-summary-high-ticked)
1105     ((and (< score default-low)
1106           (or (eq mark gnus-dormant-mark)
1107               (eq mark gnus-ticked-mark)))
1108      . gnus-summary-low-ticked)
1109     ((or (eq mark gnus-dormant-mark)
1110          (eq mark gnus-ticked-mark))
1111      . gnus-summary-normal-ticked)
1112     ((and (> score default-high) (eq mark gnus-ancient-mark))
1113      . gnus-summary-high-ancient)
1114     ((and (< score default-low) (eq mark gnus-ancient-mark))
1115      . gnus-summary-low-ancient)
1116     ((eq mark gnus-ancient-mark)
1117      . gnus-summary-normal-ancient)
1118     ((and (> score default-high) (eq mark gnus-unread-mark))
1119      . gnus-summary-high-unread)
1120     ((and (< score default-low) (eq mark gnus-unread-mark))
1121      . gnus-summary-low-unread)
1122     ((eq mark gnus-unread-mark)
1123      . gnus-summary-normal-unread)
1124     ((> score default-high)
1125      . gnus-summary-high-read)
1126     ((< score default-low)
1127      . gnus-summary-low-read)
1128     (t
1129      . gnus-summary-normal-read))
1130   "*Controls the highlighting of summary buffer lines.
1131
1132 A list of (FORM . FACE) pairs.  When deciding how a particular
1133 summary line should be displayed, each form is evaluated.  The content
1134 of the face field after the first true form is used.  You can change
1135 how those summary lines are displayed, by editing the face field.
1136
1137 You can use the following variables in the FORM field.
1138
1139 score:        The article's score.
1140 default:      The default article score.
1141 default-high: The default score for high scored articles.
1142 default-low:  The default score for low scored articles.
1143 below:        The score below which articles are automatically marked as read.
1144 mark:         The article's mark.
1145 uncached:     Non-nil if the article is uncached."
1146   :group 'gnus-summary-visual
1147   :type '(repeat (cons (sexp :tag "Form" nil)
1148                        face)))
1149 (put 'gnus-summary-highlight 'risky-local-variable t)
1150
1151 (defcustom gnus-alter-header-function nil
1152   "Function called to allow alteration of article header structures.
1153 The function is called with one parameter, the article header vector,
1154 which it may alter in any way."
1155   :type '(choice (const :tag "None" nil)
1156                  function)
1157   :group 'gnus-summary)
1158
1159 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1160   "Function used to decode a string with encoded words.")
1161
1162 (defvar gnus-decode-encoded-address-function
1163   'mail-decode-encoded-address-string
1164   "Function used to decode addresses with encoded words.")
1165
1166 (defcustom gnus-extra-headers '(To Cc Keywords Gcc Newsgroups)
1167   "*Extra headers to parse."
1168   :version "24.1"                       ; added Cc Keywords Gcc
1169   :group 'gnus-summary
1170   :type '(repeat symbol))
1171
1172 (defcustom gnus-ignored-from-addresses
1173   (and user-mail-address
1174        (not (string= user-mail-address ""))
1175        (regexp-quote user-mail-address))
1176   "*From headers that may be suppressed in favor of To headers.
1177 This can be a regexp or a list of regexps."
1178   :version "21.1"
1179   :group 'gnus-summary
1180   :type '(choice regexp
1181                  (repeat :tag "Regexp List" regexp)))
1182
1183 (defsubst gnus-ignored-from-addresses ()
1184   (gmm-regexp-concat gnus-ignored-from-addresses))
1185
1186 (defcustom gnus-summary-to-prefix "-> "
1187   "*String prefixed to the To field in the summary line when
1188 using `gnus-ignored-from-addresses'."
1189   :version "22.1"
1190   :group 'gnus-summary
1191   :type 'string)
1192
1193 (defcustom gnus-summary-newsgroup-prefix "=> "
1194   "*String prefixed to the Newsgroup field in the summary
1195 line when using the option `gnus-ignored-from-addresses'."
1196   :version "22.1"
1197   :group 'gnus-summary
1198   :type 'string)
1199
1200 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1201   "List of charsets that should be ignored.
1202 When these charsets are used in the \"charset\" parameter, the
1203 default charset will be used instead."
1204   :version "21.1"
1205   :type '(repeat symbol)
1206   :group 'gnus-charset)
1207
1208 (defcustom gnus-newsgroup-maximum-articles nil
1209   "The maximum number of articles a newsgroup.
1210 If this is a number, old articles in a newsgroup exceeding this number
1211 are silently ignored.  If it is nil, no article is ignored.  Note that
1212 setting this variable to a number might prevent you from reading very
1213 old articles."
1214   :group 'gnus-group-select
1215   :version "22.2"
1216   :type '(choice (const :tag "No limit" nil)
1217                  integer))
1218
1219 (gnus-define-group-parameter
1220  ignored-charsets
1221  :type list
1222  :function-document
1223  "Return the ignored charsets of GROUP."
1224  :variable gnus-group-ignored-charsets-alist
1225  :variable-default
1226  '(("alt\\.chinese\\.text" iso-8859-1))
1227  :variable-document
1228  "Alist of regexps (to match group names) and charsets that should be ignored.
1229 When these charsets are used in the \"charset\" parameter, the
1230 default charset will be used instead."
1231  :variable-group gnus-charset
1232  :variable-type '(repeat (cons (regexp :tag "Group")
1233                                (repeat symbol)))
1234  :parameter-type '(choice :tag "Ignored charsets"
1235                           :value nil
1236                           (repeat (symbol)))
1237  :parameter-document       "\
1238 List of charsets that should be ignored.
1239
1240 When these charsets are used in the \"charset\" parameter, the
1241 default charset will be used instead.")
1242
1243 (defcustom gnus-group-highlight-words-alist nil
1244   "Alist of group regexps and highlight regexps.
1245 This variable uses the same syntax as `gnus-emphasis-alist'."
1246   :version "21.1"
1247   :type '(repeat (cons (regexp :tag "Group")
1248                        (repeat (list (regexp :tag "Highlight regexp")
1249                                      (number :tag "Group for entire word" 0)
1250                                      (number :tag "Group for displayed part" 0)
1251                                      (symbol :tag "Face"
1252                                              gnus-emphasis-highlight-words)))))
1253   :group 'gnus-summary-visual)
1254
1255 (defcustom gnus-summary-show-article-charset-alist
1256   nil
1257   "Alist of number and charset.
1258 The article will be shown with the charset corresponding to the
1259 numbered argument.
1260 For example: ((1 . cn-gb-2312) (2 . big5))."
1261   :version "21.1"
1262   :type '(repeat (cons (number :tag "Argument" 1)
1263                        (symbol :tag "Charset")))
1264   :group 'gnus-charset)
1265
1266 (defcustom gnus-preserve-marks t
1267   "Whether marks are preserved when moving, copying and respooling messages."
1268   :version "21.1"
1269   :type 'boolean
1270   :group 'gnus-summary-marks)
1271
1272 (defcustom gnus-alter-articles-to-read-function nil
1273   "Function to be called to alter the list of articles to be selected."
1274   :type '(choice (const nil) function)
1275   :group 'gnus-summary)
1276
1277 (defcustom gnus-orphan-score nil
1278   "*All orphans get this score added.  Set in the score file."
1279   :group 'gnus-score-default
1280   :type '(choice (const nil)
1281                  integer))
1282
1283 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1284   "*A regexp to match MIME parts when saving multiple parts of a
1285 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1286 This regexp will be used by default when prompting the user for which
1287 type of files to save."
1288   :group 'gnus-summary
1289   :type 'regexp)
1290
1291 (defcustom gnus-read-all-available-headers nil
1292   "Whether Gnus should parse all headers made available to it.
1293 This is mostly relevant for slow back ends where the user may
1294 wish to widen the summary buffer to include all headers
1295 that were fetched."
1296   :version "22.1"
1297   :group 'gnus-summary
1298   :type '(choice boolean regexp))
1299
1300 (defcustom gnus-summary-pipe-output-default-command nil
1301   "Command (and optional arguments) used to pipe article to subprocess.
1302 This will be used as the default command if it is non-nil.  The value
1303 will be updated if you modify it when executing the command
1304 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1305   :version "23.1" ;; No Gnus
1306   :group 'gnus-summary
1307   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1308
1309 (defcustom gnus-summary-muttprint-program "muttprint"
1310   "Command (and optional arguments) used to run Muttprint.
1311 The value will be updated if you modify it when executing the command
1312 `gnus-summary-muttprint'."
1313   :version "22.1"
1314   :group 'gnus-summary
1315   :type 'string)
1316
1317 (defcustom gnus-article-loose-mime t
1318   "If non-nil, don't require MIME-Version header.
1319 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1320 supply the MIME-Version header or deliberately strip it from the mail.
1321 If non-nil (the default), Gnus will treat some articles as MIME
1322 even if the MIME-Version header is missing."
1323   :version "22.1"
1324   :type 'boolean
1325   :group 'gnus-article-mime)
1326
1327 (defcustom gnus-article-emulate-mime t
1328   "If non-nil, use MIME emulation for uuencode and the like.
1329 This means that Gnus will search message bodies for text that look
1330 like uuencoded bits, yEncoded bits, and so on, and present that using
1331 the normal Gnus MIME machinery."
1332   :version "22.1"
1333   :type 'boolean
1334   :group 'gnus-article-mime)
1335
1336 ;;; Internal variables
1337
1338 (defvar gnus-summary-display-cache nil)
1339 (defvar gnus-article-mime-handles nil)
1340 (defvar gnus-article-decoded-p nil)
1341 (defvar gnus-article-charset nil)
1342 (defvar gnus-article-ignored-charsets nil)
1343 (defvar gnus-scores-exclude-files nil)
1344 (defvar gnus-page-broken nil)
1345
1346 (defvar gnus-original-article nil)
1347 (defvar gnus-article-internal-prepare-hook nil)
1348 (defvar gnus-newsgroup-process-stack nil)
1349
1350 (defvar gnus-thread-indent-array nil)
1351 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1352 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1353   "Function called to sort the articles within a thread after it has been gathered together.")
1354
1355 (defvar gnus-summary-save-parts-type-history nil)
1356 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1357
1358 ;; Avoid highlighting in kill files.
1359 (defvar gnus-summary-inhibit-highlight nil)
1360 (defvar gnus-newsgroup-selected-overlay nil)
1361 (defvar gnus-inhibit-limiting nil)
1362 (defvar gnus-newsgroup-adaptive-score-file nil)
1363 (defvar gnus-current-score-file nil)
1364 (defvar gnus-current-move-group nil)
1365 (defvar gnus-current-copy-group nil)
1366 (defvar gnus-current-crosspost-group nil)
1367 (defvar gnus-newsgroup-display nil)
1368
1369 (defvar gnus-newsgroup-dependencies nil)
1370 (defvar gnus-newsgroup-adaptive nil)
1371 (defvar gnus-summary-display-article-function nil)
1372 (defvar gnus-summary-highlight-line-function nil
1373   "Function called after highlighting a summary line.")
1374
1375 (defvar gnus-summary-line-format-alist
1376   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1377     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1378     (?s gnus-tmp-subject-or-nil ?s)
1379     (?n gnus-tmp-name ?s)
1380     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1381         ?s)
1382     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1383             gnus-tmp-from) ?s)
1384     (?F gnus-tmp-from ?s)
1385     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1386     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1387     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1388     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1389     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1390     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1391     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1392     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1393     (?L gnus-tmp-lines ?s)
1394     (?Z (or (nnir-article-rsv (mail-header-number gnus-tmp-header))
1395             0) ?d)
1396     (?G (or (nnir-article-group (mail-header-number gnus-tmp-header))
1397             "") ?s)
1398     (?g (or (gnus-group-short-name
1399              (nnir-article-group (mail-header-number gnus-tmp-header)))
1400             "") ?s)
1401     (?O gnus-tmp-downloaded ?c)
1402     (?I gnus-tmp-indentation ?s)
1403     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1404     (?R gnus-tmp-replied ?c)
1405     (?\[ gnus-tmp-opening-bracket ?c)
1406     (?\] gnus-tmp-closing-bracket ?c)
1407     (?\> (make-string gnus-tmp-level ? ) ?s)
1408     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1409     (?i gnus-tmp-score ?d)
1410     (?z gnus-tmp-score-char ?c)
1411     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1412     (?U gnus-tmp-unread ?c)
1413     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1414         ?s)
1415     (?t (gnus-summary-number-of-articles-in-thread
1416          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1417         ?d)
1418     (?e (gnus-summary-number-of-articles-in-thread
1419          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1420         ?c)
1421     (?u gnus-tmp-user-defined ?s)
1422     (?P (gnus-pick-line-number) ?d)
1423     (?B gnus-tmp-thread-tree-header-string ?s)
1424     (user-date (gnus-user-date
1425                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1426   "An alist of format specifications that can appear in summary lines.
1427 These are paired with what variables they correspond with, along with
1428 the type of the variable (string, integer, character, etc).")
1429
1430 (defvar gnus-summary-dummy-line-format-alist
1431   `((?S gnus-tmp-subject ?s)
1432     (?N gnus-tmp-number ?d)
1433     (?u gnus-tmp-user-defined ?s)))
1434
1435 (defvar gnus-summary-mode-line-format-alist
1436   `((?G gnus-tmp-group-name ?s)
1437     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1438     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1439     (?A gnus-tmp-article-number ?d)
1440     (?Z gnus-tmp-unread-and-unselected ?s)
1441     (?V gnus-version ?s)
1442     (?U gnus-tmp-unread-and-unticked ?d)
1443     (?S gnus-tmp-subject ?s)
1444     (?e gnus-tmp-unselected ?d)
1445     (?u gnus-tmp-user-defined ?s)
1446     (?d (length gnus-newsgroup-dormant) ?d)
1447     (?t (length gnus-newsgroup-marked) ?d)
1448     (?h (length gnus-newsgroup-spam-marked) ?d)
1449     (?r (length gnus-newsgroup-reads) ?d)
1450     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1451     (?E gnus-newsgroup-expunged-tally ?d)
1452     (?s (gnus-current-score-file-nondirectory) ?s)))
1453
1454 ;; This is here rather than in gnus-art for compilation reasons.
1455 (defvar gnus-article-mode-line-format-alist
1456   (nconc '((?w (gnus-article-wash-status) ?s)
1457            (?m (gnus-article-mime-part-status) ?s))
1458          gnus-summary-mode-line-format-alist))
1459
1460 (defvar gnus-last-search-regexp nil
1461   "Default regexp for article search command.")
1462
1463 (defvar gnus-last-shell-command nil
1464   "Default shell command on article.")
1465
1466 (defvar gnus-newsgroup-agentized nil
1467   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1468 (defvar gnus-newsgroup-begin nil)
1469 (defvar gnus-newsgroup-end nil)
1470 (defvar gnus-newsgroup-last-rmail nil)
1471 (defvar gnus-newsgroup-last-mail nil)
1472 (defvar gnus-newsgroup-last-folder nil)
1473 (defvar gnus-newsgroup-last-file nil)
1474 (defvar gnus-newsgroup-last-directory nil)
1475 (defvar gnus-newsgroup-auto-expire nil)
1476 (defvar gnus-newsgroup-active nil)
1477 (defvar gnus-newsgroup-highest nil)
1478
1479 (defvar gnus-newsgroup-data nil)
1480 (defvar gnus-newsgroup-data-reverse nil)
1481 (defvar gnus-newsgroup-limit nil)
1482 (defvar gnus-newsgroup-limits nil)
1483 (defvar gnus-summary-use-undownloaded-faces nil)
1484
1485 (defvar gnus-newsgroup-unreads nil
1486   "Sorted list of unread articles in the current newsgroup.")
1487
1488 (defvar gnus-newsgroup-unselected nil
1489   "Sorted list of unselected unread articles in the current newsgroup.")
1490
1491 (defvar gnus-newsgroup-reads nil
1492   "Alist of read articles and article marks in the current newsgroup.")
1493
1494 (defvar gnus-newsgroup-expunged-tally nil)
1495
1496 (defvar gnus-newsgroup-marked nil
1497   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1498
1499 (defvar gnus-newsgroup-spam-marked nil
1500   "List of ranges of articles that have been marked as spam.")
1501
1502 (defvar gnus-newsgroup-killed nil
1503   "List of ranges of articles that have been through the scoring process.")
1504
1505 (defvar gnus-newsgroup-cached nil
1506   "Sorted list of articles that come from the article cache.")
1507
1508 (defvar gnus-newsgroup-saved nil
1509   "List of articles that have been saved.")
1510
1511 (defvar gnus-newsgroup-kill-headers nil)
1512
1513 (defvar gnus-newsgroup-replied nil
1514   "List of articles that have been replied to in the current newsgroup.")
1515
1516 (defvar gnus-newsgroup-forwarded nil
1517   "List of articles that have been forwarded in the current newsgroup.")
1518
1519 (defvar gnus-newsgroup-expirable nil
1520   "Sorted list of articles in the current newsgroup that can be expired.")
1521
1522 (defvar gnus-newsgroup-processable nil
1523   "List of articles in the current newsgroup that can be processed.")
1524
1525 (defvar gnus-newsgroup-downloadable nil
1526   "Sorted list of articles in the current newsgroup that can be processed.")
1527
1528 (defvar gnus-newsgroup-unfetched nil
1529   "Sorted list of articles in the current newsgroup whose headers have
1530 not been fetched into the agent.
1531
1532 This list will always be a subset of gnus-newsgroup-undownloaded.")
1533
1534 (defvar gnus-newsgroup-undownloaded nil
1535   "List of articles in the current newsgroup that haven't been downloaded.")
1536
1537 (defvar gnus-newsgroup-unsendable nil
1538   "List of articles in the current newsgroup that won't be sent.")
1539
1540 (defvar gnus-newsgroup-bookmarks nil
1541   "List of articles in the current newsgroup that have bookmarks.")
1542
1543 (defvar gnus-newsgroup-dormant nil
1544   "Sorted list of dormant articles in the current newsgroup.")
1545
1546 (defvar gnus-newsgroup-unseen nil
1547   "List of unseen articles in the current newsgroup.")
1548
1549 (defvar gnus-newsgroup-seen nil
1550   "Range of seen articles in the current newsgroup.")
1551
1552 (defvar gnus-newsgroup-unexist nil
1553   "Range of unexisting articles in the current newsgroup.")
1554
1555 (defvar gnus-newsgroup-articles nil
1556   "List of articles in the current newsgroup.")
1557
1558 (defvar gnus-newsgroup-scored nil
1559   "List of scored articles in the current newsgroup.")
1560
1561 (defvar gnus-newsgroup-headers nil
1562   "List of article headers in the current newsgroup.")
1563
1564 (defvar gnus-newsgroup-threads nil)
1565
1566 (defvar gnus-newsgroup-prepared nil
1567   "Whether the current group has been prepared properly.")
1568
1569 (defvar gnus-newsgroup-ancient nil
1570   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1571
1572 (defvar gnus-newsgroup-sparse nil)
1573
1574 (defvar gnus-current-article nil)
1575 (defvar gnus-article-current nil)
1576 (defvar gnus-current-headers nil)
1577 (defvar gnus-have-all-headers nil)
1578 (defvar gnus-last-article nil)
1579 (defvar gnus-newsgroup-history nil)
1580 (defvar gnus-newsgroup-charset nil)
1581 (defvar gnus-newsgroup-ephemeral-charset nil)
1582 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1583
1584 (defvar gnus-article-before-search nil)
1585
1586 (defvar gnus-summary-local-variables
1587   '(gnus-newsgroup-name
1588
1589     ;; Marks lists
1590     gnus-newsgroup-unreads
1591     gnus-newsgroup-unselected
1592     gnus-newsgroup-marked
1593     gnus-newsgroup-spam-marked
1594     gnus-newsgroup-reads
1595     gnus-newsgroup-saved
1596     gnus-newsgroup-replied
1597     gnus-newsgroup-forwarded
1598     gnus-newsgroup-expirable
1599     gnus-newsgroup-killed
1600     gnus-newsgroup-unseen
1601     gnus-newsgroup-seen
1602     gnus-newsgroup-unexist
1603     gnus-newsgroup-cached
1604     gnus-newsgroup-downloadable
1605     gnus-newsgroup-undownloaded
1606     gnus-newsgroup-unsendable
1607
1608     gnus-newsgroup-begin gnus-newsgroup-end
1609     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1610     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1611     gnus-newsgroup-last-directory
1612     gnus-newsgroup-auto-expire
1613     gnus-newsgroup-processable
1614     gnus-newsgroup-unfetched
1615     gnus-newsgroup-articles
1616     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1617     gnus-newsgroup-headers gnus-newsgroup-threads
1618     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1619     gnus-current-article gnus-current-headers gnus-have-all-headers
1620     gnus-last-article gnus-article-internal-prepare-hook
1621     (gnus-summary-article-delete-hook . global)
1622     (gnus-summary-article-move-hook . global)
1623     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1624     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1625     gnus-thread-expunge-below
1626     gnus-score-alist gnus-current-score-file
1627     (gnus-summary-expunge-below . global)
1628     (gnus-summary-mark-below . global)
1629     (gnus-orphan-score . global)
1630     gnus-newsgroup-active gnus-scores-exclude-files
1631     gnus-newsgroup-highest
1632     gnus-newsgroup-history gnus-newsgroup-ancient
1633     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1634     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1635     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1636     (gnus-newsgroup-expunged-tally . 0)
1637     gnus-cache-removable-articles
1638     gnus-newsgroup-data gnus-newsgroup-data-reverse
1639     gnus-newsgroup-limit gnus-newsgroup-limits
1640     gnus-newsgroup-charset gnus-newsgroup-display
1641     gnus-summary-use-undownloaded-faces)
1642   "Variables that are buffer-local to the summary buffers.")
1643
1644 (defvar gnus-newsgroup-variables nil
1645   "A list of variables that have separate values in different newsgroups.
1646 A list of newsgroup (summary buffer) local variables, or cons of
1647 variables and their default expressions to be evalled (when the default
1648 values are not nil), that should be made global while the summary buffer
1649 is active.
1650
1651 Note: The default expressions will be evaluated (using function `eval')
1652 before assignment to the local variable rather than just assigned to it.
1653 If the default expression is the symbol `global', that symbol will not
1654 be evaluated but the global value of the local variable will be used
1655 instead.
1656
1657 These variables can be used to set variables in the group parameters
1658 while still allowing them to affect operations done in other buffers.
1659 For example:
1660
1661 \(setq gnus-newsgroup-variables
1662      '(message-use-followup-to
1663        (gnus-visible-headers .
1664          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1665 ")
1666
1667 (eval-when-compile
1668   ;; Bind features so that require will believe that gnus-sum has
1669   ;; already been loaded (avoids infinite recursion)
1670   (let ((features (cons 'gnus-sum features)))
1671     (require 'gnus-art)))
1672
1673 ;; MIME stuff.
1674
1675 (defvar gnus-decode-encoded-word-methods
1676   '(mail-decode-encoded-word-string)
1677   "List of methods used to decode encoded words.
1678
1679 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1680 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1681 \(REGEXP . FUNCTION), FUNCTION will be applied only to the newsgroups
1682 whose names match REGEXP.
1683
1684 For example:
1685 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1686  mail-decode-encoded-word-string
1687  (\"chinese\" . rfc1843-decode-string))")
1688
1689 (defvar gnus-decode-encoded-word-methods-cache nil)
1690
1691 (defun gnus-multi-decode-encoded-word-string (string)
1692   "Apply the functions from `gnus-encoded-word-methods' that match."
1693   (unless (and gnus-decode-encoded-word-methods-cache
1694                (eq gnus-newsgroup-name
1695                    (car gnus-decode-encoded-word-methods-cache)))
1696     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1697     (dolist (method gnus-decode-encoded-word-methods)
1698       (if (symbolp method)
1699           (nconc gnus-decode-encoded-word-methods-cache (list method))
1700         (if (and gnus-newsgroup-name
1701                  (string-match (car method) gnus-newsgroup-name))
1702             (nconc gnus-decode-encoded-word-methods-cache
1703                    (list (cdr method)))))))
1704   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1705     (setq string (funcall method string))))
1706
1707 ;; Subject simplification.
1708
1709 (defun gnus-simplify-whitespace (str)
1710   "Remove excessive whitespace from STR."
1711   ;; Multiple spaces.
1712   (while (string-match "[ \t][ \t]+" str)
1713     (setq str (concat (substring str 0 (match-beginning 0))
1714                         " "
1715                         (substring str (match-end 0)))))
1716   ;; Leading spaces.
1717   (when (string-match "^[ \t]+" str)
1718     (setq str (substring str (match-end 0))))
1719   ;; Trailing spaces.
1720   (when (string-match "[ \t]+$" str)
1721     (setq str (substring str 0 (match-beginning 0))))
1722   str)
1723
1724 (defun gnus-simplify-all-whitespace (str)
1725   "Remove all whitespace from STR."
1726   (while (string-match "[ \t\n]+" str)
1727     (setq str (replace-match "" nil nil str)))
1728   str)
1729
1730 (defsubst gnus-simplify-subject-re (subject)
1731   "Remove \"Re:\" from subject lines."
1732   (if (string-match message-subject-re-regexp subject)
1733       (substring subject (match-end 0))
1734     subject))
1735
1736 (defun gnus-simplify-subject (subject &optional re-only)
1737   "Remove `Re:' and words in parentheses.
1738 If RE-ONLY is non-nil, strip leading `Re:'s only."
1739   (let ((case-fold-search t))           ;Ignore case.
1740     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1741     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1742       (setq subject (substring subject (match-end 0))))
1743     ;; Remove uninteresting prefixes.
1744     (when (and (not re-only)
1745                gnus-simplify-ignored-prefixes
1746                (string-match gnus-simplify-ignored-prefixes subject))
1747       (setq subject (substring subject (match-end 0))))
1748     ;; Remove words in parentheses from end.
1749     (unless re-only
1750       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1751         (setq subject (substring subject 0 (match-beginning 0)))))
1752     ;; Return subject string.
1753     subject))
1754
1755 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1756 ;; all whitespace.
1757 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1758   (goto-char (point-min))
1759   (while (re-search-forward regexp nil t)
1760     (replace-match (or newtext ""))))
1761
1762 (defun gnus-simplify-buffer-fuzzy (regexp)
1763   "Simplify string in the buffer fuzzily.
1764 The string in the accessible portion of the current buffer is simplified.
1765 It is assumed to be a single-line subject.
1766 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1767 matter is removed.  Additional things can be deleted by setting
1768 `gnus-simplify-subject-fuzzy-regexp'."
1769   (let ((case-fold-search t)
1770         (modified-tick))
1771     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1772
1773     (while (not (eq modified-tick (buffer-modified-tick)))
1774       (setq modified-tick (buffer-modified-tick))
1775       (cond
1776        ((listp regexp)
1777         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1778        (regexp
1779         (gnus-simplify-buffer-fuzzy-step regexp)))
1780       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1781       (gnus-simplify-buffer-fuzzy-step
1782        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1783       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1784
1785     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1786     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1787     (gnus-simplify-buffer-fuzzy-step " $")
1788     (gnus-simplify-buffer-fuzzy-step "^ +")))
1789
1790 (defun gnus-simplify-subject-fuzzy (subject)
1791   "Simplify a subject string fuzzily.
1792 See `gnus-simplify-buffer-fuzzy' for details."
1793   (save-excursion
1794     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1795       (gnus-set-work-buffer)
1796       (let ((case-fold-search t))
1797         ;; Remove uninteresting prefixes.
1798         (when (and gnus-simplify-ignored-prefixes
1799                    (string-match gnus-simplify-ignored-prefixes subject))
1800           (setq subject (substring subject (match-end 0))))
1801         (insert subject)
1802         (inline (gnus-simplify-buffer-fuzzy regexp))
1803         (buffer-string)))))
1804
1805 (defsubst gnus-simplify-subject-fully (subject)
1806   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1807   (cond
1808    (gnus-simplify-subject-functions
1809     (gnus-map-function gnus-simplify-subject-functions subject))
1810    ((null gnus-summary-gather-subject-limit)
1811     (gnus-simplify-subject-re subject))
1812    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1813     (gnus-simplify-subject-fuzzy subject))
1814    ((numberp gnus-summary-gather-subject-limit)
1815     (truncate-string-to-width (gnus-simplify-subject-re subject)
1816                               gnus-summary-gather-subject-limit))
1817    (t
1818     subject)))
1819
1820 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1821   "Check whether two subjects are equal.
1822 If optional argument SIMPLE-FIRST is t, first argument is already
1823 simplified."
1824   (cond
1825    ((null simple-first)
1826     (equal (gnus-simplify-subject-fully s1)
1827            (gnus-simplify-subject-fully s2)))
1828    (t
1829     (equal s1
1830            (gnus-simplify-subject-fully s2)))))
1831
1832 (defun gnus-summary-bubble-group ()
1833   "Increase the score of the current group.
1834 This is a handy function to add to `gnus-summary-exit-hook' to
1835 increase the score of each group you read."
1836   (gnus-group-add-score gnus-newsgroup-name))
1837
1838 \f
1839 ;;;
1840 ;;; Gnus summary mode
1841 ;;;
1842
1843 (put 'gnus-summary-mode 'mode-class 'special)
1844
1845 (defvar gnus-article-commands-menu)
1846
1847 ;; Non-orthogonal keys
1848
1849 (gnus-define-keys gnus-summary-mode-map
1850   " " gnus-summary-next-page
1851   [?\S-\ ] gnus-summary-prev-page
1852   "\177" gnus-summary-prev-page
1853   [delete] gnus-summary-prev-page
1854   [backspace] gnus-summary-prev-page
1855   "\r" gnus-summary-scroll-up
1856   "\M-\r" gnus-summary-scroll-down
1857   "n" gnus-summary-next-unread-article
1858   "p" gnus-summary-prev-unread-article
1859   "N" gnus-summary-next-article
1860   "P" gnus-summary-prev-article
1861   "\M-\C-n" gnus-summary-next-same-subject
1862   "\M-\C-p" gnus-summary-prev-same-subject
1863   "\M-n" gnus-summary-next-unread-subject
1864   "\M-p" gnus-summary-prev-unread-subject
1865   "." gnus-summary-first-unread-article
1866   "," gnus-summary-best-unread-article
1867   "\M-s" gnus-summary-search-article-forward
1868   "\M-r" gnus-summary-search-article-backward
1869   "\M-S" gnus-summary-repeat-search-article-forward
1870   "\M-R" gnus-summary-repeat-search-article-backward
1871   "<" gnus-summary-beginning-of-article
1872   ">" gnus-summary-end-of-article
1873   "j" gnus-summary-goto-article
1874   "^" gnus-summary-refer-parent-article
1875   "\M-^" gnus-summary-refer-article
1876   "u" gnus-summary-tick-article-forward
1877   "!" gnus-summary-tick-article-forward
1878   "U" gnus-summary-tick-article-backward
1879   "d" gnus-summary-mark-as-read-forward
1880   "D" gnus-summary-mark-as-read-backward
1881   "E" gnus-summary-mark-as-expirable
1882   "\M-u" gnus-summary-clear-mark-forward
1883   "\M-U" gnus-summary-clear-mark-backward
1884   "k" gnus-summary-kill-same-subject-and-select
1885   "\C-k" gnus-summary-kill-same-subject
1886   "\M-\C-k" gnus-summary-kill-thread
1887   "\M-\C-l" gnus-summary-lower-thread
1888   "e" gnus-summary-edit-article
1889   "#" gnus-summary-mark-as-processable
1890   "\M-#" gnus-summary-unmark-as-processable
1891   "\M-\C-t" gnus-summary-toggle-threads
1892   "\M-\C-s" gnus-summary-show-thread
1893   "\M-\C-h" gnus-summary-hide-thread
1894   "\M-\C-f" gnus-summary-next-thread
1895   "\M-\C-b" gnus-summary-prev-thread
1896   [(meta down)] gnus-summary-next-thread
1897   [(meta up)] gnus-summary-prev-thread
1898   "\M-\C-u" gnus-summary-up-thread
1899   "\M-\C-d" gnus-summary-down-thread
1900   "&" gnus-summary-execute-command
1901   "c" gnus-summary-catchup-and-exit
1902   "\C-w" gnus-summary-mark-region-as-read
1903   "\C-t" gnus-summary-toggle-truncation
1904   "?" gnus-summary-mark-as-dormant
1905   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1906   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1907   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1908   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1909   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1910   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1911   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1912   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1913   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1914   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1915   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1916   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1917   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1918   "=" gnus-summary-expand-window
1919   "\C-x\C-s" gnus-summary-reselect-current-group
1920   "\M-g" gnus-summary-rescan-group
1921   "\C-c\C-r" gnus-summary-caesar-message
1922   "f" gnus-summary-followup
1923   "F" gnus-summary-followup-with-original
1924   "C" gnus-summary-cancel-article
1925   "r" gnus-summary-reply
1926   "R" gnus-summary-reply-with-original
1927   "\C-c\C-f" gnus-summary-mail-forward
1928   "o" gnus-summary-save-article
1929   "\C-o" gnus-summary-save-article-mail
1930   "|" gnus-summary-pipe-output
1931   "\M-k" gnus-summary-edit-local-kill
1932   "\M-K" gnus-summary-edit-global-kill
1933   ;; "V" gnus-version
1934   "\C-c\C-d" gnus-summary-describe-group
1935   "q" gnus-summary-exit
1936   "Q" gnus-summary-exit-no-update
1937   "\C-c\C-i" gnus-info-find-node
1938   gnus-mouse-2 gnus-mouse-pick-article
1939   [follow-link] mouse-face
1940   "m" gnus-summary-mail-other-window
1941   "a" gnus-summary-post-news
1942   "x" gnus-summary-limit-to-unread
1943   "s" gnus-summary-isearch-article
1944   "\t" gnus-summary-widget-forward
1945   [backtab] gnus-summary-widget-backward
1946   "t" gnus-summary-toggle-header
1947   "g" gnus-summary-show-article
1948   "l" gnus-summary-goto-last-article
1949   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1950   "\C-d" gnus-summary-enter-digest-group
1951   "\M-\C-d" gnus-summary-read-document
1952   "\M-\C-e" gnus-summary-edit-parameters
1953   "\M-\C-a" gnus-summary-customize-parameters
1954   "\C-c\C-b" gnus-bug
1955   "*" gnus-cache-enter-article
1956   "\M-*" gnus-cache-remove-article
1957   "\M-&" gnus-summary-universal-argument
1958   "\C-l" gnus-recenter
1959   "I" gnus-summary-increase-score
1960   "L" gnus-summary-lower-score
1961   "\M-i" gnus-symbolic-argument
1962   "h" gnus-summary-select-article-buffer
1963
1964   "b" gnus-article-view-part
1965   "\M-t" gnus-summary-toggle-display-buttonized
1966
1967   "V" gnus-summary-score-map
1968   "X" gnus-uu-extract-map
1969   "S" gnus-summary-send-map)
1970
1971 ;; Sort of orthogonal keymap
1972 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1973   "t" gnus-summary-tick-article-forward
1974   "!" gnus-summary-tick-article-forward
1975   "d" gnus-summary-mark-as-read-forward
1976   "r" gnus-summary-mark-as-read-forward
1977   "c" gnus-summary-clear-mark-forward
1978   " " gnus-summary-clear-mark-forward
1979   "e" gnus-summary-mark-as-expirable
1980   "x" gnus-summary-mark-as-expirable
1981   "?" gnus-summary-mark-as-dormant
1982   "b" gnus-summary-set-bookmark
1983   "B" gnus-summary-remove-bookmark
1984   "#" gnus-summary-mark-as-processable
1985   "\M-#" gnus-summary-unmark-as-processable
1986   "S" gnus-summary-limit-include-expunged
1987   "C" gnus-summary-catchup
1988   "H" gnus-summary-catchup-to-here
1989   "h" gnus-summary-catchup-from-here
1990   "\C-c" gnus-summary-catchup-all
1991   "k" gnus-summary-kill-same-subject-and-select
1992   "K" gnus-summary-kill-same-subject
1993   "P" gnus-uu-mark-map)
1994
1995 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1996   "c" gnus-summary-clear-above
1997   "u" gnus-summary-tick-above
1998   "m" gnus-summary-mark-above
1999   "k" gnus-summary-kill-below)
2000
2001 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
2002   "/" gnus-summary-limit-to-subject
2003   "n" gnus-summary-limit-to-articles
2004   "b" gnus-summary-limit-to-bodies
2005   "h" gnus-summary-limit-to-headers
2006   "w" gnus-summary-pop-limit
2007   "s" gnus-summary-limit-to-subject
2008   "a" gnus-summary-limit-to-author
2009   "u" gnus-summary-limit-to-unread
2010   "m" gnus-summary-limit-to-marks
2011   "M" gnus-summary-limit-exclude-marks
2012   "v" gnus-summary-limit-to-score
2013   "*" gnus-summary-limit-include-cached
2014   "D" gnus-summary-limit-include-dormant
2015   "T" gnus-summary-limit-include-thread
2016   "d" gnus-summary-limit-exclude-dormant
2017   "t" gnus-summary-limit-to-age
2018   "." gnus-summary-limit-to-unseen
2019   "x" gnus-summary-limit-to-extra
2020   "p" gnus-summary-limit-to-display-predicate
2021   "E" gnus-summary-limit-include-expunged
2022   "c" gnus-summary-limit-exclude-childless-dormant
2023   "C" gnus-summary-limit-mark-excluded-as-read
2024   "o" gnus-summary-insert-old-articles
2025   "N" gnus-summary-insert-new-articles
2026   "S" gnus-summary-limit-to-singletons
2027   "r" gnus-summary-limit-to-replied
2028   "R" gnus-summary-limit-to-recipient
2029   "A" gnus-summary-limit-to-address)
2030
2031 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2032   "n" gnus-summary-next-unread-article
2033   "p" gnus-summary-prev-unread-article
2034   "N" gnus-summary-next-article
2035   "P" gnus-summary-prev-article
2036   "\C-n" gnus-summary-next-same-subject
2037   "\C-p" gnus-summary-prev-same-subject
2038   "\M-n" gnus-summary-next-unread-subject
2039   "\M-p" gnus-summary-prev-unread-subject
2040   "f" gnus-summary-first-unread-article
2041   "b" gnus-summary-best-unread-article
2042   "j" gnus-summary-goto-article
2043   "g" gnus-summary-goto-subject
2044   "l" gnus-summary-goto-last-article
2045   "o" gnus-summary-pop-article)
2046
2047 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2048   "k" gnus-summary-kill-thread
2049   "E" gnus-summary-expire-thread
2050   "l" gnus-summary-lower-thread
2051   "i" gnus-summary-raise-thread
2052   "T" gnus-summary-toggle-threads
2053   "t" gnus-summary-rethread-current
2054   "^" gnus-summary-reparent-thread
2055   "\M-^" gnus-summary-reparent-children
2056   "s" gnus-summary-show-thread
2057   "S" gnus-summary-show-all-threads
2058   "h" gnus-summary-hide-thread
2059   "H" gnus-summary-hide-all-threads
2060   "n" gnus-summary-next-thread
2061   "p" gnus-summary-prev-thread
2062   "u" gnus-summary-up-thread
2063   "o" gnus-summary-top-thread
2064   "d" gnus-summary-down-thread
2065   "#" gnus-uu-mark-thread
2066   "\M-#" gnus-uu-unmark-thread)
2067
2068 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2069   "g" gnus-summary-prepare
2070   "c" gnus-summary-insert-cached-articles
2071   "d" gnus-summary-insert-dormant-articles
2072   "t" gnus-summary-insert-ticked-articles)
2073
2074 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2075   "c" gnus-summary-catchup-and-exit
2076   "C" gnus-summary-catchup-all-and-exit
2077   "E" gnus-summary-exit-no-update
2078   "Q" gnus-summary-exit
2079   "Z" gnus-summary-exit
2080   "n" gnus-summary-catchup-and-goto-next-group
2081   "p" gnus-summary-catchup-and-goto-prev-group
2082   "R" gnus-summary-reselect-current-group
2083   "G" gnus-summary-rescan-group
2084   "N" gnus-summary-next-group
2085   "s" gnus-summary-save-newsrc
2086   "P" gnus-summary-prev-group)
2087
2088 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2089   " " gnus-summary-next-page
2090   "n" gnus-summary-next-page
2091   [?\S-\ ] gnus-summary-prev-page
2092   "\177" gnus-summary-prev-page
2093   [delete] gnus-summary-prev-page
2094   "p" gnus-summary-prev-page
2095   "\r" gnus-summary-scroll-up
2096   "\M-\r" gnus-summary-scroll-down
2097   "<" gnus-summary-beginning-of-article
2098   ">" gnus-summary-end-of-article
2099   "b" gnus-summary-beginning-of-article
2100   "e" gnus-summary-end-of-article
2101   "^" gnus-summary-refer-parent-article
2102   "r" gnus-summary-refer-parent-article
2103   "C" gnus-summary-show-complete-article
2104   "D" gnus-summary-enter-digest-group
2105   "R" gnus-summary-refer-references
2106   "T" gnus-summary-refer-thread
2107   "W" gnus-warp-to-article
2108   "g" gnus-summary-show-article
2109   "s" gnus-summary-isearch-article
2110   "\t" gnus-summary-widget-forward
2111   [backtab] gnus-summary-widget-backward
2112   "P" gnus-summary-print-article
2113   "S" gnus-sticky-article
2114   "M" gnus-mailing-list-insinuate
2115   "t" gnus-article-babel)
2116
2117 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2118   "b" gnus-article-add-buttons
2119   "B" gnus-article-add-buttons-to-head
2120   "o" gnus-article-treat-overstrike
2121   "e" gnus-article-emphasize
2122   "w" gnus-article-fill-cited-article
2123   "Q" gnus-article-fill-long-lines
2124   "L" gnus-article-toggle-truncate-lines
2125   "C" gnus-article-capitalize-sentences
2126   "c" gnus-article-remove-cr
2127   "q" gnus-article-de-quoted-unreadable
2128   "6" gnus-article-de-base64-unreadable
2129   "Z" gnus-article-decode-HZ
2130   "A" gnus-article-treat-ansi-sequences
2131   "h" gnus-article-wash-html
2132   "u" gnus-article-unsplit-urls
2133   "s" gnus-summary-force-verify-and-decrypt
2134   "f" gnus-article-display-x-face
2135   "l" gnus-summary-stop-page-breaking
2136   "r" gnus-summary-caesar-message
2137   "m" gnus-summary-morse-message
2138   "t" gnus-summary-toggle-header
2139   "g" gnus-treat-smiley
2140   "v" gnus-summary-verbose-headers
2141   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2142   "p" gnus-article-verify-x-pgp-sig
2143   "d" gnus-article-treat-dumbquotes
2144   "U" gnus-article-treat-non-ascii
2145   "i" gnus-summary-idna-message)
2146
2147 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2148   ;; mnemonic: deuglif*Y*
2149   "u" gnus-article-outlook-unwrap-lines
2150   "a" gnus-article-outlook-repair-attribution
2151   "c" gnus-article-outlook-rearrange-citation
2152   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2153
2154 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2155   "a" gnus-article-hide
2156   "h" gnus-article-hide-headers
2157   "b" gnus-article-hide-boring-headers
2158   "s" gnus-article-hide-signature
2159   "c" gnus-article-hide-citation
2160   "C" gnus-article-hide-citation-in-followups
2161   "l" gnus-article-hide-list-identifiers
2162   "B" gnus-article-strip-banner
2163   "P" gnus-article-hide-pem
2164   "\C-c" gnus-article-hide-citation-maybe)
2165
2166 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2167   "a" gnus-article-highlight
2168   "h" gnus-article-highlight-headers
2169   "c" gnus-article-highlight-citation
2170   "s" gnus-article-highlight-signature)
2171
2172 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2173   "f" gnus-article-treat-fold-headers
2174   "u" gnus-article-treat-unfold-headers
2175   "n" gnus-article-treat-fold-newsgroups)
2176
2177 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2178   "x" gnus-article-display-x-face
2179   "d" gnus-article-display-face
2180   "s" gnus-treat-smiley
2181   "D" gnus-article-remove-images
2182   "W" gnus-article-show-images
2183   "f" gnus-treat-from-picon
2184   "m" gnus-treat-mail-picon
2185   "n" gnus-treat-newsgroups-picon
2186   "g" gnus-treat-from-gravatar
2187   "h" gnus-treat-mail-gravatar)
2188
2189 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2190   "w" gnus-article-decode-mime-words
2191   "c" gnus-article-decode-charset
2192   "v" gnus-mime-view-all-parts
2193   "b" gnus-article-view-part)
2194
2195 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2196   "z" gnus-article-date-ut
2197   "u" gnus-article-date-ut
2198   "l" gnus-article-date-local
2199   "p" gnus-article-date-english
2200   "e" gnus-article-date-lapsed
2201   "o" gnus-article-date-original
2202   "i" gnus-article-date-iso8601
2203   "s" gnus-article-date-user)
2204
2205 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2206   "t" gnus-article-remove-trailing-blank-lines
2207   "l" gnus-article-strip-leading-blank-lines
2208   "m" gnus-article-strip-multiple-blank-lines
2209   "a" gnus-article-strip-blank-lines
2210   "A" gnus-article-strip-all-blank-lines
2211   "s" gnus-article-strip-leading-space
2212   "e" gnus-article-strip-trailing-space
2213   "w" gnus-article-remove-leading-whitespace)
2214
2215 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2216   "v" gnus-version
2217   "d" gnus-summary-describe-group
2218   "h" gnus-summary-describe-briefly
2219   "i" gnus-info-find-node)
2220
2221 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2222   "e" gnus-summary-expire-articles
2223   "\M-\C-e" gnus-summary-expire-articles-now
2224   "\177" gnus-summary-delete-article
2225   [delete] gnus-summary-delete-article
2226   [backspace] gnus-summary-delete-article
2227   "m" gnus-summary-move-article
2228   "r" gnus-summary-respool-article
2229   "w" gnus-summary-edit-article
2230   "c" gnus-summary-copy-article
2231   "B" gnus-summary-crosspost-article
2232   "q" gnus-summary-respool-query
2233   "t" gnus-summary-respool-trace
2234   "i" gnus-summary-import-article
2235   "I" gnus-summary-create-article
2236   "p" gnus-summary-article-posted-p)
2237
2238 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2239   "o" gnus-summary-save-article
2240   "m" gnus-summary-save-article-mail
2241   "F" gnus-summary-write-article-file
2242   "r" gnus-summary-save-article-rmail
2243   "f" gnus-summary-save-article-file
2244   "b" gnus-summary-save-article-body-file
2245   "B" gnus-summary-write-article-body-file
2246   "h" gnus-summary-save-article-folder
2247   "v" gnus-summary-save-article-vm
2248   "p" gnus-summary-pipe-output
2249   "P" gnus-summary-muttprint)
2250
2251 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2252   "b" gnus-summary-display-buttonized
2253   "m" gnus-summary-repair-multipart
2254   "v" gnus-article-view-part
2255   "o" gnus-article-save-part
2256   "O" gnus-article-save-part-and-strip
2257   "r" gnus-article-replace-part
2258   "d" gnus-article-delete-part
2259   "t" gnus-article-view-part-as-type
2260   "j" gnus-article-jump-to-part
2261   "c" gnus-article-copy-part
2262   "C" gnus-article-view-part-as-charset
2263   "e" gnus-article-view-part-externally
2264   "H" gnus-article-browse-html-article
2265   "E" gnus-article-encrypt-body
2266   "i" gnus-article-inline-part
2267   "|" gnus-article-pipe-part)
2268
2269 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2270   "p" gnus-summary-mark-as-processable
2271   "u" gnus-summary-unmark-as-processable
2272   "U" gnus-summary-unmark-all-processable
2273   "v" gnus-uu-mark-over
2274   "s" gnus-uu-mark-series
2275   "r" gnus-uu-mark-region
2276   "g" gnus-uu-unmark-region
2277   "R" gnus-uu-mark-by-regexp
2278   "G" gnus-uu-unmark-by-regexp
2279   "t" gnus-uu-mark-thread
2280   "T" gnus-uu-unmark-thread
2281   "a" gnus-uu-mark-all
2282   "b" gnus-uu-mark-buffer
2283   "S" gnus-uu-mark-sparse
2284   "k" gnus-summary-kill-process-mark
2285   "y" gnus-summary-yank-process-mark
2286   "w" gnus-summary-save-process-mark
2287   "i" gnus-uu-invert-processable)
2288
2289 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2290   ;;"x" gnus-uu-extract-any
2291   "m" gnus-summary-save-parts
2292   "u" gnus-uu-decode-uu
2293   "U" gnus-uu-decode-uu-and-save
2294   "s" gnus-uu-decode-unshar
2295   "S" gnus-uu-decode-unshar-and-save
2296   "o" gnus-uu-decode-save
2297   "O" gnus-uu-decode-save
2298   "b" gnus-uu-decode-binhex
2299   "B" gnus-uu-decode-binhex
2300   "Y" gnus-uu-decode-yenc
2301   "p" gnus-uu-decode-postscript
2302   "P" gnus-uu-decode-postscript-and-save)
2303
2304 (gnus-define-keys
2305     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2306   "u" gnus-uu-decode-uu-view
2307   "U" gnus-uu-decode-uu-and-save-view
2308   "s" gnus-uu-decode-unshar-view
2309   "S" gnus-uu-decode-unshar-and-save-view
2310   "o" gnus-uu-decode-save-view
2311   "O" gnus-uu-decode-save-view
2312   "b" gnus-uu-decode-binhex-view
2313   "B" gnus-uu-decode-binhex-view
2314   "p" gnus-uu-decode-postscript-view
2315   "P" gnus-uu-decode-postscript-and-save-view)
2316
2317 (defvar gnus-article-post-menu nil)
2318
2319 (defconst gnus-summary-menu-maxlen 20)
2320
2321 (defun gnus-summary-menu-split (menu)
2322   ;; If we have lots of elements, divide them into groups of 20
2323   ;; and make a pane (or submenu) for each one.
2324   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2325       (let ((menu menu) sublists next
2326             (i 1))
2327         (while menu
2328           ;; Pull off the next gnus-summary-menu-maxlen elements
2329           ;; and make them the next element of sublist.
2330           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2331           (if next
2332               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2333                       nil))
2334           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2335                                              (aref (car (last menu)) 0)) menu)
2336                                sublists))
2337           (setq i (1+ i))
2338           (setq menu next))
2339         (nreverse sublists))
2340     ;; Few elements--put them all in one pane.
2341     menu))
2342
2343 (defun gnus-summary-make-menu-bar ()
2344   (gnus-turn-off-edit-menu 'summary)
2345
2346   (unless (boundp 'gnus-summary-misc-menu)
2347
2348     (easy-menu-define
2349       gnus-summary-kill-menu gnus-summary-mode-map ""
2350       (cons
2351        "Score"
2352        (nconc
2353         (list
2354          ["Customize" gnus-score-customize t])
2355         (gnus-make-score-map 'increase)
2356         (gnus-make-score-map 'lower)
2357         '(("Mark"
2358            ["Kill below" gnus-summary-kill-below t]
2359            ["Mark above" gnus-summary-mark-above t]
2360            ["Tick above" gnus-summary-tick-above t]
2361            ["Clear above" gnus-summary-clear-above t])
2362           ["Current score" gnus-summary-current-score t]
2363           ["Set score" gnus-summary-set-score t]
2364           ["Switch current score file..." gnus-score-change-score-file t]
2365           ["Set mark below..." gnus-score-set-mark-below t]
2366           ["Set expunge below..." gnus-score-set-expunge-below t]
2367           ["Edit current score file" gnus-score-edit-current-scores t]
2368           ["Edit score file..." gnus-score-edit-file t]
2369           ["Trace score" gnus-score-find-trace t]
2370           ["Find words" gnus-score-find-favourite-words t]
2371           ["Rescore buffer" gnus-summary-rescore t]
2372           ["Increase score..." gnus-summary-increase-score t]
2373           ["Lower score..." gnus-summary-lower-score t]))))
2374
2375     ;; Define both the Article menu in the summary buffer and the
2376     ;; equivalent Commands menu in the article buffer here for
2377     ;; consistency.
2378     (let ((innards
2379            `(("Hide"
2380               ["All" gnus-article-hide t]
2381               ["Headers" gnus-article-hide-headers t]
2382               ["Signature" gnus-article-hide-signature t]
2383               ["Citation" gnus-article-hide-citation t]
2384               ["List identifiers" gnus-article-hide-list-identifiers t]
2385               ["Banner" gnus-article-strip-banner t]
2386               ["Boring headers" gnus-article-hide-boring-headers t])
2387              ("Highlight"
2388               ["All" gnus-article-highlight t]
2389               ["Headers" gnus-article-highlight-headers t]
2390               ["Signature" gnus-article-highlight-signature t]
2391               ["Citation" gnus-article-highlight-citation t])
2392              ("MIME"
2393               ["Words" gnus-article-decode-mime-words t]
2394               ["Charset" gnus-article-decode-charset t]
2395               ["QP" gnus-article-de-quoted-unreadable t]
2396               ["Base64" gnus-article-de-base64-unreadable t]
2397               ["View MIME buttons" gnus-summary-display-buttonized t]
2398               ["View all" gnus-mime-view-all-parts t]
2399               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2400               ["Encrypt body" gnus-article-encrypt-body
2401                :active (not (gnus-group-read-only-p))
2402                ,@(if (featurep 'xemacs) nil
2403                    '(:help "Encrypt the message body on disk"))]
2404               ["Extract all parts..." gnus-summary-save-parts t]
2405               ("Multipart"
2406                ["Repair multipart" gnus-summary-repair-multipart t]
2407                ["Pipe part..." gnus-article-pipe-part t]
2408                ["Inline part" gnus-article-inline-part t]
2409                ["View part as type..." gnus-article-view-part-as-type t]
2410                ["Encrypt body" gnus-article-encrypt-body
2411                 :active (not (gnus-group-read-only-p))
2412                ,@(if (featurep 'xemacs) nil
2413                    '(:help "Encrypt the message body on disk"))]
2414                ["View part externally" gnus-article-view-part-externally t]
2415                ["View HTML parts in browser" gnus-article-browse-html-article t]
2416                ["View part with charset..." gnus-article-view-part-as-charset t]
2417                ["Copy part" gnus-article-copy-part t]
2418                ["Save part..." gnus-article-save-part t]
2419                ["View part" gnus-article-view-part t]))
2420              ("Date"
2421               ["Local" gnus-article-date-local t]
2422               ["ISO8601" gnus-article-date-iso8601 t]
2423               ["UT" gnus-article-date-ut t]
2424               ["Original" gnus-article-date-original t]
2425               ["Lapsed" gnus-article-date-lapsed t]
2426               ["User-defined" gnus-article-date-user t])
2427              ("Display"
2428               ["Remove images" gnus-article-remove-images t]
2429               ["Toggle smiley" gnus-treat-smiley t]
2430               ["Show X-Face" gnus-article-display-x-face t]
2431               ["Show picons in From" gnus-treat-from-picon t]
2432               ["Show picons in mail headers" gnus-treat-mail-picon t]
2433               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2434               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2435               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2436               ("View as different encoding"
2437                ,@(gnus-summary-menu-split
2438                   (mapcar
2439                    (lambda (cs)
2440                      ;; Since easymenu under Emacs doesn't allow
2441                      ;; lambda forms for menu commands, we should
2442                      ;; provide intern'ed function symbols.
2443                      (let ((command (intern (format "\
2444 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2445                        (fset command
2446                              `(lambda ()
2447                                 (interactive)
2448                                 (let ((gnus-summary-show-article-charset-alist
2449                                        '((1 . ,cs))))
2450                                   (gnus-summary-show-article 1))))
2451                        `[,(symbol-name cs) ,command t]))
2452                    (sort (if (fboundp 'coding-system-list)
2453                              (coding-system-list)
2454                            (mapcar 'car mm-mime-mule-charset-alist))
2455                          'string<)))))
2456              ("Washing"
2457               ("Remove Blanks"
2458                ["Leading" gnus-article-strip-leading-blank-lines t]
2459                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2460                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2461                ["All of the above" gnus-article-strip-blank-lines t]
2462                ["All" gnus-article-strip-all-blank-lines t]
2463                ["Leading space" gnus-article-strip-leading-space t]
2464                ["Trailing space" gnus-article-strip-trailing-space t]
2465                ["Leading space in headers"
2466                 gnus-article-remove-leading-whitespace t])
2467               ["Overstrike" gnus-article-treat-overstrike t]
2468               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2469               ["Non-ASCII" gnus-article-treat-non-ascii t]
2470               ["Emphasis" gnus-article-emphasize t]
2471               ["Word wrap" gnus-article-fill-cited-article t]
2472               ["Fill long lines" gnus-article-fill-long-lines t]
2473               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2474               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2475               ["Remove CR" gnus-article-remove-cr t]
2476               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2477               ["Base64" gnus-article-de-base64-unreadable t]
2478               ["Rot 13" gnus-summary-caesar-message
2479                ,@(if (featurep 'xemacs) '(t)
2480                    '(:help "\"Caesar rotate\" article by 13"))]
2481               ["De-IDNA" gnus-summary-idna-message t]
2482               ["Morse decode" gnus-summary-morse-message t]
2483               ["Unix pipe..." gnus-summary-pipe-message t]
2484               ["Add buttons" gnus-article-add-buttons t]
2485               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2486               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2487               ["Verbose header" gnus-summary-verbose-headers t]
2488               ["Toggle header" gnus-summary-toggle-header t]
2489               ["Unfold headers" gnus-article-treat-unfold-headers t]
2490               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2491               ["Html" gnus-article-wash-html t]
2492               ["Unsplit URLs" gnus-article-unsplit-urls t]
2493               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2494               ["Decode HZ" gnus-article-decode-HZ t]
2495               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2496               ("(Outlook) Deuglify"
2497                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2498                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2499                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2500                ["Full (Outlook) deuglify"
2501                 gnus-article-outlook-deuglify-article t])
2502               )
2503              ("Output"
2504               ["Save in default format..." gnus-summary-save-article
2505                ,@(if (featurep 'xemacs) '(t)
2506                    '(:help "Save article using default method"))]
2507               ["Save in file..." gnus-summary-save-article-file
2508                ,@(if (featurep 'xemacs) '(t)
2509                    '(:help "Save article in file"))]
2510               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2511               ["Save in MH folder..." gnus-summary-save-article-folder t]
2512               ["Save in VM folder..." gnus-summary-save-article-vm t]
2513               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2514               ["Save body in file..." gnus-summary-save-article-body-file t]
2515               ["Pipe through a filter..." gnus-summary-pipe-output t]
2516               ["Print with Muttprint..." gnus-summary-muttprint t]
2517               ["Print" gnus-summary-print-article
2518                ,@(if (featurep 'xemacs) '(t)
2519                    '(:help "Generate and print a PostScript image"))])
2520              ("Copy, move,... (Backend)"
2521               ,@(if (featurep 'xemacs) nil
2522                   '(:help "Copying, moving, expiring articles..."))
2523               ["Respool article..." gnus-summary-respool-article t]
2524               ["Move article..." gnus-summary-move-article
2525                (gnus-check-backend-function
2526                 'request-move-article gnus-newsgroup-name)]
2527               ["Copy article..." gnus-summary-copy-article t]
2528               ["Crosspost article..." gnus-summary-crosspost-article
2529                (gnus-check-backend-function
2530                 'request-replace-article gnus-newsgroup-name)]
2531               ["Import file..." gnus-summary-import-article
2532                (gnus-check-backend-function
2533                 'request-accept-article gnus-newsgroup-name)]
2534               ["Create article..." gnus-summary-create-article
2535                (gnus-check-backend-function
2536                 'request-accept-article gnus-newsgroup-name)]
2537               ["Check if posted" gnus-summary-article-posted-p t]
2538               ["Edit article" gnus-summary-edit-article
2539                (not (gnus-group-read-only-p))]
2540               ["Delete article" gnus-summary-delete-article
2541                (gnus-check-backend-function
2542                 'request-expire-articles gnus-newsgroup-name)]
2543               ["Query respool" gnus-summary-respool-query t]
2544               ["Trace respool" gnus-summary-respool-trace t]
2545               ["Delete expirable articles" gnus-summary-expire-articles-now
2546                (gnus-check-backend-function
2547                 'request-expire-articles gnus-newsgroup-name)])
2548              ("Extract"
2549               ["Uudecode" gnus-uu-decode-uu
2550                ,@(if (featurep 'xemacs) '(t)
2551                    '(:help "Decode uuencoded article(s)"))]
2552               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2553               ["Unshar" gnus-uu-decode-unshar t]
2554               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2555               ["Save" gnus-uu-decode-save t]
2556               ["Binhex" gnus-uu-decode-binhex t]
2557               ["PostScript" gnus-uu-decode-postscript t]
2558               ["All MIME parts" gnus-summary-save-parts t])
2559              ("Cache"
2560               ["Enter article" gnus-cache-enter-article t]
2561               ["Remove article" gnus-cache-remove-article t])
2562              ["Translate" gnus-article-babel t]
2563              ["Select article buffer" gnus-summary-select-article-buffer t]
2564              ["Make article buffer sticky" gnus-sticky-article t]
2565              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2566              ["Isearch article..." gnus-summary-isearch-article t]
2567              ["Beginning of the article" gnus-summary-beginning-of-article t]
2568              ["End of the article" gnus-summary-end-of-article t]
2569              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2570              ["Fetch referenced articles" gnus-summary-refer-references t]
2571              ["Fetch current thread" gnus-summary-refer-thread t]
2572              ["Fetch article with id..." gnus-summary-refer-article t]
2573              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2574              ["Redisplay" gnus-summary-show-article t]
2575              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2576       (easy-menu-define
2577         gnus-summary-article-menu gnus-summary-mode-map ""
2578         (cons "Article" innards))
2579
2580       (if (not (keymapp gnus-summary-article-menu))
2581           (easy-menu-define
2582             gnus-article-commands-menu gnus-article-mode-map ""
2583             (cons "Commands" innards))
2584         ;; in Emacs, don't share menu.
2585         (setq gnus-article-commands-menu
2586               (copy-keymap gnus-summary-article-menu))
2587         (define-key gnus-article-mode-map [menu-bar commands]
2588           (cons "Commands" gnus-article-commands-menu))))
2589
2590     (easy-menu-define
2591       gnus-summary-thread-menu gnus-summary-mode-map ""
2592       '("Threads"
2593         ["Find all messages in thread" gnus-summary-refer-thread t]
2594         ["Toggle threading" gnus-summary-toggle-threads t]
2595         ["Hide threads" gnus-summary-hide-all-threads t]
2596         ["Show threads" gnus-summary-show-all-threads t]
2597         ["Hide thread" gnus-summary-hide-thread t]
2598         ["Show thread" gnus-summary-show-thread t]
2599         ["Go to next thread" gnus-summary-next-thread t]
2600         ["Go to previous thread" gnus-summary-prev-thread t]
2601         ["Go down thread" gnus-summary-down-thread t]
2602         ["Go up thread" gnus-summary-up-thread t]
2603         ["Top of thread" gnus-summary-top-thread t]
2604         ["Mark thread as read" gnus-summary-kill-thread t]
2605         ["Mark thread as expired" gnus-summary-expire-thread t]
2606         ["Lower thread score" gnus-summary-lower-thread t]
2607         ["Raise thread score" gnus-summary-raise-thread t]
2608         ["Rethread current" gnus-summary-rethread-current t]))
2609
2610     (easy-menu-define
2611       gnus-summary-post-menu gnus-summary-mode-map ""
2612       `("Post"
2613         ["Send a message (mail or news)" gnus-summary-post-news
2614          ,@(if (featurep 'xemacs) '(t)
2615              '(:help "Compose a new message (mail or news)"))]
2616         ["Followup" gnus-summary-followup
2617          ,@(if (featurep 'xemacs) '(t)
2618              '(:help "Post followup to this article"))]
2619         ["Followup and yank" gnus-summary-followup-with-original
2620          ,@(if (featurep 'xemacs) '(t)
2621              '(:help "Post followup to this article, quoting its contents"))]
2622         ["Supersede article" gnus-summary-supersede-article t]
2623         ["Cancel article" gnus-summary-cancel-article
2624          ,@(if (featurep 'xemacs) '(t)
2625              '(:help "Cancel an article you posted"))]
2626         ["Reply" gnus-summary-reply t]
2627         ["Reply and yank" gnus-summary-reply-with-original t]
2628         ["Wide reply" gnus-summary-wide-reply t]
2629         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2630          ,@(if (featurep 'xemacs) '(t)
2631              '(:help "Mail a reply, quoting this article"))]
2632         ["Very wide reply" gnus-summary-very-wide-reply t]
2633         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2634          ,@(if (featurep 'xemacs) '(t)
2635              '(:help "Mail a very wide reply, quoting this article"))]
2636         ["Mail forward" gnus-summary-mail-forward t]
2637         ["Post forward" gnus-summary-post-forward t]
2638         ["Digest and mail" gnus-uu-digest-mail-forward t]
2639         ["Digest and post" gnus-uu-digest-post-forward t]
2640         ["Resend message" gnus-summary-resend-message t]
2641         ["Resend message edit" gnus-summary-resend-message-edit t]
2642         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2643         ["Send a mail" gnus-summary-mail-other-window t]
2644         ["Create a local message" gnus-summary-news-other-window t]
2645         ["Uuencode and post" gnus-uu-post-news
2646          ,@(if (featurep 'xemacs) '(t)
2647              '(:help "Post a uuencoded article"))]
2648         ["Followup via news" gnus-summary-followup-to-mail t]
2649         ["Followup via news and yank"
2650          gnus-summary-followup-to-mail-with-original t]
2651         ["Strip signature on reply"
2652          (lambda ()
2653            (interactive)
2654            (if (not (memq message-cite-function
2655                           '(message-cite-original-without-signature
2656                             message-cite-original)))
2657                ;; Stupid workaround for XEmacs not honoring :visible.
2658                (message "Can't toggle this value of `message-cite-function'")
2659              (setq message-cite-function
2660                    (if (eq message-cite-function
2661                            'message-cite-original-without-signature)
2662                        'message-cite-original
2663                      'message-cite-original-without-signature))))
2664          ;; XEmacs barfs on :visible.
2665          ,@(if (featurep 'xemacs) nil
2666              '(:visible (memq message-cite-function
2667                               '(message-cite-original-without-signature
2668                                 message-cite-original))))
2669          :style toggle
2670          :selected (eq message-cite-function
2671                        'message-cite-original-without-signature)
2672          ,@(if (featurep 'xemacs) nil
2673              '(:help "Strip signature from cited article when replying."))]
2674         ;;("Draft"
2675         ;;["Send" gnus-summary-send-draft t]
2676         ;;["Send bounced" gnus-resend-bounced-mail t])
2677         ))
2678
2679     (cond
2680      ((not (keymapp gnus-summary-post-menu))
2681       (setq gnus-article-post-menu gnus-summary-post-menu))
2682      ((not gnus-article-post-menu)
2683       ;; Don't share post menu.
2684       (setq gnus-article-post-menu
2685             (copy-keymap gnus-summary-post-menu))))
2686     (define-key gnus-article-mode-map [menu-bar post]
2687       (cons "Post" gnus-article-post-menu))
2688
2689     (easy-menu-define
2690       gnus-summary-misc-menu gnus-summary-mode-map ""
2691       `("Gnus"
2692         ("Mark Read"
2693          ["Mark as read" gnus-summary-mark-as-read-forward t]
2694          ["Mark same subject and select"
2695           gnus-summary-kill-same-subject-and-select t]
2696          ["Mark same subject" gnus-summary-kill-same-subject t]
2697          ["Catchup" gnus-summary-catchup
2698           ,@(if (featurep 'xemacs) '(t)
2699               '(:help "Mark unread articles in this group as read"))]
2700          ["Catchup all" gnus-summary-catchup-all t]
2701          ["Catchup to here" gnus-summary-catchup-to-here t]
2702          ["Catchup from here" gnus-summary-catchup-from-here t]
2703          ["Catchup region" gnus-summary-mark-region-as-read
2704           (gnus-mark-active-p)]
2705          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2706         ("Mark Various"
2707          ["Tick" gnus-summary-tick-article-forward t]
2708          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2709          ["Remove marks" gnus-summary-clear-mark-forward t]
2710          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2711          ["Set bookmark" gnus-summary-set-bookmark t]
2712          ["Remove bookmark" gnus-summary-remove-bookmark t])
2713         ("Limit to"
2714          ["Marks..." gnus-summary-limit-to-marks t]
2715          ["Subject..." gnus-summary-limit-to-subject t]
2716          ["Author..." gnus-summary-limit-to-author t]
2717          ["Recipient..." gnus-summary-limit-to-recipient t]
2718          ["Address..." gnus-summary-limit-to-address t]
2719          ["Age..." gnus-summary-limit-to-age t]
2720          ["Extra..." gnus-summary-limit-to-extra t]
2721          ["Score..." gnus-summary-limit-to-score t]
2722          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2723          ["Unread" gnus-summary-limit-to-unread t]
2724          ["Unseen" gnus-summary-limit-to-unseen t]
2725          ["Singletons" gnus-summary-limit-to-singletons t]
2726          ["Replied" gnus-summary-limit-to-replied t]
2727          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2728          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2729          ["Pop limit" gnus-summary-pop-limit t]
2730          ["Show dormant" gnus-summary-limit-include-dormant t]
2731          ["Hide childless dormant"
2732           gnus-summary-limit-exclude-childless-dormant t]
2733          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2734          ["Hide marked" gnus-summary-limit-exclude-marks t]
2735          ["Show expunged" gnus-summary-limit-include-expunged t])
2736         ("Process Mark"
2737          ["Set mark" gnus-summary-mark-as-processable t]
2738          ["Remove mark" gnus-summary-unmark-as-processable t]
2739          ["Remove all marks" gnus-summary-unmark-all-processable t]
2740          ["Invert marks" gnus-uu-invert-processable t]
2741          ["Mark above" gnus-uu-mark-over t]
2742          ["Mark series" gnus-uu-mark-series t]
2743          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2744          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2745          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2746          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2747          ["Mark all" gnus-uu-mark-all t]
2748          ["Mark buffer" gnus-uu-mark-buffer t]
2749          ["Mark sparse" gnus-uu-mark-sparse t]
2750          ["Mark thread" gnus-uu-mark-thread t]
2751          ["Unmark thread" gnus-uu-unmark-thread t]
2752          ("Process Mark Sets"
2753           ["Kill" gnus-summary-kill-process-mark t]
2754           ["Yank" gnus-summary-yank-process-mark
2755            gnus-newsgroup-process-stack]
2756           ["Save" gnus-summary-save-process-mark t]
2757           ["Run command on marked..." gnus-summary-universal-argument t]))
2758         ("Registry Marks")
2759         ("Scroll article"
2760          ["Page forward" gnus-summary-next-page
2761           ,@(if (featurep 'xemacs) '(t)
2762               '(:help "Show next page of article"))]
2763          ["Page backward" gnus-summary-prev-page
2764           ,@(if (featurep 'xemacs) '(t)
2765               '(:help "Show previous page of article"))]
2766          ["Line forward" gnus-summary-scroll-up t])
2767         ("Move"
2768          ["Next unread article" gnus-summary-next-unread-article t]
2769          ["Previous unread article" gnus-summary-prev-unread-article t]
2770          ["Next article" gnus-summary-next-article t]
2771          ["Previous article" gnus-summary-prev-article t]
2772          ["Next unread subject" gnus-summary-next-unread-subject t]
2773          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2774          ["Next article same subject" gnus-summary-next-same-subject t]
2775          ["Previous article same subject" gnus-summary-prev-same-subject t]
2776          ["First unread article" gnus-summary-first-unread-article t]
2777          ["Best unread article" gnus-summary-best-unread-article t]
2778          ["Go to subject number..." gnus-summary-goto-subject t]
2779          ["Go to article number..." gnus-summary-goto-article t]
2780          ["Go to the last article" gnus-summary-goto-last-article t]
2781          ["Pop article off history" gnus-summary-pop-article t])
2782         ("Sort"
2783          ["Sort by number" gnus-summary-sort-by-number t]
2784          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2785          ["Sort by author" gnus-summary-sort-by-author t]
2786          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2787          ["Sort by subject" gnus-summary-sort-by-subject t]
2788          ["Sort by date" gnus-summary-sort-by-date t]
2789          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2790          ["Sort by score" gnus-summary-sort-by-score t]
2791          ["Sort by lines" gnus-summary-sort-by-lines t]
2792          ["Sort by characters" gnus-summary-sort-by-chars t]
2793          ["Randomize" gnus-summary-sort-by-random t]
2794          ["Original sort" gnus-summary-sort-by-original t])
2795         ("Help"
2796          ["Describe group" gnus-summary-describe-group t]
2797          ["Read manual" gnus-info-find-node t])
2798         ("Modes"
2799          ["Pick and read" gnus-pick-mode t]
2800          ["Binary" gnus-binary-mode t])
2801         ("Regeneration"
2802          ["Regenerate" gnus-summary-prepare t]
2803          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2804          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2805          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2806          ["Toggle threading" gnus-summary-toggle-threads t])
2807         ["See old articles" gnus-summary-insert-old-articles t]
2808         ["See new articles" gnus-summary-insert-new-articles t]
2809         ["Filter articles..." gnus-summary-execute-command t]
2810         ["Run command on articles..." gnus-summary-universal-argument t]
2811         ["Search articles forward..." gnus-summary-search-article-forward t]
2812         ["Search articles backward..." gnus-summary-search-article-backward t]
2813         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2814         ["Expand window" gnus-summary-expand-window t]
2815         ["Expire expirable articles" gnus-summary-expire-articles
2816          (gnus-check-backend-function
2817           'request-expire-articles gnus-newsgroup-name)]
2818         ["Edit local kill file" gnus-summary-edit-local-kill t]
2819         ["Edit main kill file" gnus-summary-edit-global-kill t]
2820         ["Edit group parameters" gnus-summary-edit-parameters t]
2821         ["Customize group parameters" gnus-summary-customize-parameters t]
2822         ["Send a bug report" gnus-bug t]
2823         ("Exit"
2824          ["Catchup and exit" gnus-summary-catchup-and-exit
2825           ,@(if (featurep 'xemacs) '(t)
2826               '(:help "Mark unread articles in this group as read, then exit"))]
2827          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2828          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2829          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2830          ["Exit group" gnus-summary-exit
2831           ,@(if (featurep 'xemacs) '(t)
2832               '(:help "Exit current group, return to group selection mode"))]
2833          ["Exit group without updating" gnus-summary-exit-no-update t]
2834          ["Exit and goto next group" gnus-summary-next-group t]
2835          ["Exit and goto prev group" gnus-summary-prev-group t]
2836          ["Reselect group" gnus-summary-reselect-current-group t]
2837          ["Rescan group" gnus-summary-rescan-group t]
2838          ["Update dribble" gnus-summary-save-newsrc t])))
2839
2840     (gnus-run-hooks 'gnus-summary-menu-hook)))
2841
2842 (defvar gnus-summary-tool-bar-map nil)
2843
2844 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2845 ;; affect _new_ message buffers.  We might add a function that walks thru all
2846 ;; summary-mode buffers and force the update.
2847 (defun gnus-summary-tool-bar-update (&optional symbol value)
2848   "Update summary mode toolbar.
2849 Setter function for custom variables."
2850   (setq-default gnus-summary-tool-bar-map nil)
2851   (when symbol
2852     ;; When used as ":set" function:
2853     (set-default symbol value))
2854   (when (gnus-buffer-live-p gnus-summary-buffer)
2855     (with-current-buffer gnus-summary-buffer
2856       (gnus-summary-make-tool-bar))))
2857
2858 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2859                                      'gnus-summary-tool-bar-gnome
2860                                    'gnus-summary-tool-bar-retro)
2861   "Specifies the Gnus summary tool bar.
2862
2863 It can be either a list or a symbol referring to a list.  See
2864 `gmm-tool-bar-from-list' for the format of the list.  The
2865 default key map is `gnus-summary-mode-map'.
2866
2867 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2868 `gnus-summary-tool-bar-retro'."
2869   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2870                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2871                  (repeat :tag "User defined list" gmm-tool-bar-item)
2872                  (symbol))
2873   :version "23.1" ;; No Gnus
2874   :initialize 'custom-initialize-default
2875   :set 'gnus-summary-tool-bar-update
2876   :group 'gnus-summary)
2877
2878 (defcustom gnus-summary-tool-bar-gnome
2879   '((gnus-summary-post-news "mail/compose" nil)
2880     (gnus-summary-insert-new-articles "mail/inbox" nil
2881                                       :visible (or (not gnus-agent)
2882                                                    gnus-plugged))
2883     (gnus-summary-reply-with-original "mail/reply")
2884     (gnus-summary-reply "mail/reply" nil :visible nil)
2885     (gnus-summary-followup-with-original "mail/reply-all")
2886     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2887     (gnus-summary-mail-forward "mail/forward")
2888     (gnus-summary-save-article "mail/save")
2889     (gnus-summary-search-article-forward "search" nil :visible nil)
2890     (gnus-summary-print-article "print")
2891     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2892     ;; Some new commands that may need more suitable icons:
2893     (gnus-summary-save-newsrc "save" nil :visible nil)
2894     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2895     (gnus-summary-prev-article "left-arrow")
2896     (gnus-summary-next-article "right-arrow")
2897     (gnus-summary-next-page "next-page")
2898     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2899     ;;
2900     ;; Maybe some sort-by-... could be added:
2901     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2902     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2903     (gnus-summary-mark-as-expirable
2904      "delete" nil
2905      :visible (gnus-check-backend-function 'request-expire-articles
2906                                            gnus-newsgroup-name))
2907     (gnus-summary-mark-as-spam
2908      "mail/spam" t
2909      :visible (and (fboundp 'spam-group-ham-contents-p)
2910                    (spam-group-ham-contents-p gnus-newsgroup-name))
2911      :help "Mark as spam")
2912     (gnus-summary-mark-as-read-forward
2913      "mail/not-spam" nil
2914      :visible (and (fboundp 'spam-group-spam-contents-p)
2915                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2916     ;;
2917     (gnus-summary-exit "exit")
2918     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2919     (gnus-info-find-node "help"))
2920   "List of functions for the summary tool bar (GNOME style).
2921
2922 See `gmm-tool-bar-from-list' for the format of the list."
2923   :type '(repeat gmm-tool-bar-item)
2924   :version "23.1" ;; No Gnus
2925   :initialize 'custom-initialize-default
2926   :set 'gnus-summary-tool-bar-update
2927   :group 'gnus-summary)
2928
2929 (defcustom gnus-summary-tool-bar-retro
2930   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2931     (gnus-summary-next-unread-article "gnus/next-ur")
2932     (gnus-summary-post-news "gnus/post")
2933     (gnus-summary-followup-with-original "gnus/fuwo")
2934     (gnus-summary-followup "gnus/followup")
2935     (gnus-summary-reply-with-original "gnus/reply-wo")
2936     (gnus-summary-reply "gnus/reply")
2937     (gnus-summary-caesar-message "gnus/rot13")
2938     (gnus-uu-decode-uu "gnus/uu-decode")
2939     (gnus-summary-save-article-file "gnus/save-aif")
2940     (gnus-summary-save-article "gnus/save-art")
2941     (gnus-uu-post-news "gnus/uu-post")
2942     (gnus-summary-catchup "gnus/catchup")
2943     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2944     (gnus-summary-exit "gnus/exit-summ")
2945     ;; Some new command that may need more suitable icons:
2946     (gnus-summary-print-article "gnus/print" nil :visible nil)
2947     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2948     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2949     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2950     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2951     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2952     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2953     ;;
2954     (gnus-info-find-node "gnus/help" nil :visible nil))
2955   "List of functions for the summary tool bar (retro look).
2956
2957 See `gmm-tool-bar-from-list' for the format of the list."
2958   :type '(repeat gmm-tool-bar-item)
2959   :version "23.1" ;; No Gnus
2960   :initialize 'custom-initialize-default
2961   :set 'gnus-summary-tool-bar-update
2962   :group 'gnus-summary)
2963
2964 (defcustom gnus-summary-tool-bar-zap-list t
2965   "List of icon items from the global tool bar.
2966 These items are not displayed in the Gnus summary mode tool bar.
2967
2968 See `gmm-tool-bar-from-list' for the format of the list."
2969   :type 'gmm-tool-bar-zap-list
2970   :version "23.1" ;; No Gnus
2971   :initialize 'custom-initialize-default
2972   :set 'gnus-summary-tool-bar-update
2973   :group 'gnus-summary)
2974
2975 (defvar image-load-path)
2976 (defvar tool-bar-map)
2977
2978 (defun gnus-summary-make-tool-bar (&optional force)
2979   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2980 When FORCE, rebuild the tool bar."
2981   (when (and (not (featurep 'xemacs))
2982              (boundp 'tool-bar-mode)
2983              tool-bar-mode
2984              (or (not gnus-summary-tool-bar-map) force))
2985     (let* ((load-path
2986             (gmm-image-load-path-for-library "gnus"
2987                                              "mail/save.xpm"
2988                                              nil t))
2989            (image-load-path (cons (car load-path)
2990                                   (when (boundp 'image-load-path)
2991                                     image-load-path)))
2992            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2993                                         gnus-summary-tool-bar-zap-list
2994                                         'gnus-summary-mode-map)))
2995       (when map
2996         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2997         ;; uses its value.
2998         (setq gnus-summary-tool-bar-map map))))
2999   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
3000
3001 (defun gnus-make-score-map (type)
3002   "Make a summary score map of type TYPE."
3003   (if t
3004       nil
3005     (let ((headers '(("author" "from" string)
3006                      ("subject" "subject" string)
3007                      ("article body" "body" string)
3008                      ("article head" "head" string)
3009                      ("xref" "xref" string)
3010                      ("extra header" "extra" string)
3011                      ("lines" "lines" number)
3012                      ("followups to author" "followup" string)))
3013           (types '((number ("less than" <)
3014                            ("greater than" >)
3015                            ("equal" =))
3016                    (string ("substring" s)
3017                            ("exact string" e)
3018                            ("fuzzy string" f)
3019                            ("regexp" r))))
3020           (perms '(("temporary" (current-time-string))
3021                    ("permanent" nil)
3022                    ("immediate" now)))
3023           header)
3024       (list
3025        (apply
3026         'nconc
3027         (list
3028          (if (eq type 'lower)
3029              "Lower score"
3030            "Increase score"))
3031         (let (outh)
3032           (while headers
3033             (setq header (car headers))
3034             (setq outh
3035                   (cons
3036                    (apply
3037                     'nconc
3038                     (list (car header))
3039                     (let ((ts (cdr (assoc (nth 2 header) types)))
3040                           outt)
3041                       (while ts
3042                         (setq outt
3043                               (cons
3044                                (apply
3045                                 'nconc
3046                                 (list (caar ts))
3047                                 (let ((ps perms)
3048                                       outp)
3049                                   (while ps
3050                                     (setq outp
3051                                           (cons
3052                                            (vector
3053                                             (caar ps)
3054                                             (list
3055                                              'gnus-summary-score-entry
3056                                              (nth 1 header)
3057                                              (if (or (string= (nth 1 header)
3058                                                               "head")
3059                                                      (string= (nth 1 header)
3060                                                               "body"))
3061                                                  ""
3062                                                (list 'gnus-summary-header
3063                                                      (nth 1 header)))
3064                                              (list 'quote (nth 1 (car ts)))
3065                                              (list 'gnus-score-delta-default
3066                                                    nil)
3067                                              (nth 1 (car ps))
3068                                              t)
3069                                             t)
3070                                            outp))
3071                                     (setq ps (cdr ps)))
3072                                   (list (nreverse outp))))
3073                                outt))
3074                         (setq ts (cdr ts)))
3075                       (list (nreverse outt))))
3076                    outh))
3077             (setq headers (cdr headers)))
3078           (list (nreverse outh))))))))
3079
3080
3081 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3082 (defvar bookmark-make-record-function)
3083 \f
3084 (defvar bidi-paragraph-direction)
3085
3086 (defun gnus-summary-mode (&optional group)
3087   "Major mode for reading articles.
3088
3089 All normal editing commands are switched off.
3090 \\<gnus-summary-mode-map>
3091 Each line in this buffer represents one article.  To read an
3092 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3093 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3094 respectively.
3095
3096 You can also post articles and send mail from this buffer.  To
3097 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3098 of an article, type `\\[gnus-summary-reply]'.
3099
3100 There are approx. one gazillion commands you can execute in this
3101 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3102
3103 The following commands are available:
3104
3105 \\{gnus-summary-mode-map}"
3106   (interactive)
3107   (kill-all-local-variables)
3108   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3109     (gnus-summary-make-local-variables))
3110   (gnus-summary-make-local-variables)
3111   (setq gnus-newsgroup-name group)
3112   (when (gnus-visual-p 'summary-menu 'menu)
3113     (gnus-summary-make-menu-bar)
3114     (gnus-summary-make-tool-bar))
3115   (gnus-make-thread-indent-array)
3116   (gnus-simplify-mode-line)
3117   (setq major-mode 'gnus-summary-mode)
3118   (setq mode-name "Summary")
3119   (use-local-map gnus-summary-mode-map)
3120   (buffer-disable-undo)
3121   (setq buffer-read-only t              ;Disable modification
3122         show-trailing-whitespace nil)
3123   (setq truncate-lines t)
3124   ;; Force paragraph direction to be left-to-right.  Don't make it
3125   ;; bound globally in old Emacsen and XEmacsen.
3126   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3127   (add-to-invisibility-spec '(gnus-sum . t))
3128   (gnus-summary-set-display-table)
3129   (gnus-set-default-directory)
3130   (make-local-variable 'gnus-summary-line-format)
3131   (make-local-variable 'gnus-summary-line-format-spec)
3132   (make-local-variable 'gnus-summary-dummy-line-format)
3133   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3134   (make-local-variable 'gnus-summary-mark-positions)
3135   (gnus-make-local-hook 'pre-command-hook)
3136   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3137   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3138   (turn-on-gnus-mailing-list-mode)
3139   (mm-enable-multibyte)
3140   (set (make-local-variable 'bookmark-make-record-function)
3141        'gnus-summary-bookmark-make-record)
3142   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3143   (gnus-update-summary-mark-positions))
3144
3145 (defun gnus-summary-make-local-variables ()
3146   "Make all the local summary buffer variables."
3147   (let (global)
3148     (dolist (local gnus-summary-local-variables)
3149       (if (consp local)
3150           (progn
3151             (if (eq (cdr local) 'global)
3152                 ;; Copy the global value of the variable.
3153                 (setq global (symbol-value (car local)))
3154               ;; Use the value from the list.
3155               (setq global (eval (cdr local))))
3156             (set (make-local-variable (car local)) global))
3157         ;; Simple nil-valued local variable.
3158         (set (make-local-variable local) nil)))))
3159
3160 ;; Summary data functions.
3161
3162 (defmacro gnus-data-number (data)
3163   `(car ,data))
3164
3165 (defmacro gnus-data-set-number (data number)
3166   `(setcar ,data ,number))
3167
3168 (defmacro gnus-data-mark (data)
3169   `(nth 1 ,data))
3170
3171 (defmacro gnus-data-set-mark (data mark)
3172   `(setcar (nthcdr 1 ,data) ,mark))
3173
3174 (defmacro gnus-data-pos (data)
3175   `(nth 2 ,data))
3176
3177 (defmacro gnus-data-set-pos (data pos)
3178   `(setcar (nthcdr 2 ,data) ,pos))
3179
3180 (defmacro gnus-data-header (data)
3181   `(nth 3 ,data))
3182
3183 (defmacro gnus-data-set-header (data header)
3184   `(setf (nth 3 ,data) ,header))
3185
3186 (defmacro gnus-data-level (data)
3187   `(nth 4 ,data))
3188
3189 (defmacro gnus-data-unread-p (data)
3190   `(= (nth 1 ,data) gnus-unread-mark))
3191
3192 (defmacro gnus-data-read-p (data)
3193   `(/= (nth 1 ,data) gnus-unread-mark))
3194
3195 (defmacro gnus-data-pseudo-p (data)
3196   `(consp (nth 3 ,data)))
3197
3198 (defmacro gnus-data-find (number)
3199   `(assq ,number gnus-newsgroup-data))
3200
3201 (defmacro gnus-data-find-list (number &optional data)
3202   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3203      (memq (assq ,number bdata)
3204            bdata)))
3205
3206 (defmacro gnus-data-make (number mark pos header level)
3207   `(list ,number ,mark ,pos ,header ,level))
3208
3209 (defun gnus-data-enter (after-article number mark pos header level offset)
3210   (let ((data (gnus-data-find-list after-article)))
3211     (unless data
3212       (error "No such article: %d" after-article))
3213     (setcdr data (cons (gnus-data-make number mark pos header level)
3214                        (cdr data)))
3215     (setq gnus-newsgroup-data-reverse nil)
3216     (gnus-data-update-list (cddr data) offset)))
3217
3218 (defun gnus-data-enter-list (after-article list &optional offset)
3219   (when list
3220     (let ((data (and after-article (gnus-data-find-list after-article)))
3221           (ilist list))
3222       (if (not (or data
3223                    after-article))
3224           (let ((odata gnus-newsgroup-data))
3225             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3226             (when offset
3227               (gnus-data-update-list odata offset)))
3228         ;; Find the last element in the list to be spliced into the main
3229         ;; list.
3230         (setq list (last list))
3231         (if (not data)
3232             (progn
3233               (setcdr list gnus-newsgroup-data)
3234               (setq gnus-newsgroup-data ilist)
3235               (when offset
3236                 (gnus-data-update-list (cdr list) offset)))
3237           (setcdr list (cdr data))
3238           (setcdr data ilist)
3239           (when offset
3240             (gnus-data-update-list (cdr list) offset))))
3241       (setq gnus-newsgroup-data-reverse nil))))
3242
3243 (defun gnus-data-remove (article &optional offset)
3244   (let ((data gnus-newsgroup-data))
3245     (if (= (gnus-data-number (car data)) article)
3246         (progn
3247           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3248                 gnus-newsgroup-data-reverse nil)
3249           (when offset
3250             (gnus-data-update-list gnus-newsgroup-data offset)))
3251       (while (cdr data)
3252         (when (= (gnus-data-number (cadr data)) article)
3253           (setcdr data (cddr data))
3254           (when offset
3255             (gnus-data-update-list (cdr data) offset))
3256           (setq data nil
3257                 gnus-newsgroup-data-reverse nil))
3258         (setq data (cdr data))))))
3259
3260 (defmacro gnus-data-list (backward)
3261   `(if ,backward
3262        (or gnus-newsgroup-data-reverse
3263            (setq gnus-newsgroup-data-reverse
3264                  (reverse gnus-newsgroup-data)))
3265      gnus-newsgroup-data))
3266
3267 (defun gnus-data-update-list (data offset)
3268   "Add OFFSET to the POS of all data entries in DATA."
3269   (setq gnus-newsgroup-data-reverse nil)
3270   (while data
3271     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3272     (setq data (cdr data))))
3273
3274 (defun gnus-summary-article-pseudo-p (article)
3275   "Say whether this article is a pseudo article or not."
3276   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3277
3278 (defmacro gnus-summary-article-sparse-p (article)
3279   "Say whether this article is a sparse article or not."
3280   `(memq ,article gnus-newsgroup-sparse))
3281
3282 (defmacro gnus-summary-article-ancient-p (article)
3283   "Say whether this article is a sparse article or not."
3284   `(memq ,article gnus-newsgroup-ancient))
3285
3286 (defun gnus-article-children (number)
3287   "Return a list of all children to NUMBER."
3288   (let* ((data (gnus-data-find-list number))
3289          (level (gnus-data-level (car data)))
3290          children)
3291     (setq data (cdr data))
3292     (while (and data
3293                 (= (gnus-data-level (car data)) (1+ level)))
3294       (push (gnus-data-number (car data)) children)
3295       (setq data (cdr data)))
3296     children))
3297
3298 (defmacro gnus-summary-skip-intangible ()
3299   "If the current article is intangible, then jump to a different article."
3300   '(let ((to (get-text-property (point) 'gnus-intangible)))
3301      (and to (gnus-summary-goto-subject to))))
3302
3303 (defmacro gnus-summary-article-intangible-p ()
3304   "Say whether this article is intangible or not."
3305   '(get-text-property (point) 'gnus-intangible))
3306
3307 ;; Some summary mode macros.
3308
3309 (defmacro gnus-summary-article-number ()
3310   "The article number of the article on the current line.
3311 If there isn't an article number here, then we return the current
3312 article number."
3313   '(progn
3314      (gnus-summary-skip-intangible)
3315      (or (get-text-property (point) 'gnus-number)
3316          (gnus-summary-last-subject))))
3317
3318 (defmacro gnus-summary-article-header (&optional number)
3319   "Return the header of article NUMBER."
3320   `(gnus-data-header (gnus-data-find
3321                       ,(or number '(gnus-summary-article-number)))))
3322
3323 (defmacro gnus-summary-thread-level (&optional number)
3324   "Return the level of thread that starts with article NUMBER."
3325   `(if (and (eq gnus-summary-make-false-root 'dummy)
3326             (get-text-property (point) 'gnus-intangible))
3327        0
3328      (gnus-data-level (gnus-data-find
3329                        ,(or number '(gnus-summary-article-number))))))
3330
3331 (defmacro gnus-summary-article-mark (&optional number)
3332   "Return the mark of article NUMBER."
3333   `(gnus-data-mark (gnus-data-find
3334                     ,(or number '(gnus-summary-article-number)))))
3335
3336 (defmacro gnus-summary-article-pos (&optional number)
3337   "Return the position of the line of article NUMBER."
3338   `(gnus-data-pos (gnus-data-find
3339                    ,(or number '(gnus-summary-article-number)))))
3340
3341 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3342 (defmacro gnus-summary-article-subject (&optional number)
3343   "Return current subject string or nil if nothing."
3344   `(let ((headers
3345           ,(if number
3346                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3347              '(gnus-data-header (assq (gnus-summary-article-number)
3348                                       gnus-newsgroup-data)))))
3349      (and headers
3350           (vectorp headers)
3351           (mail-header-subject headers))))
3352
3353 (defmacro gnus-summary-article-score (&optional number)
3354   "Return current article score."
3355   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3356                   gnus-newsgroup-scored))
3357        gnus-summary-default-score 0))
3358
3359 (defun gnus-summary-article-children (&optional number)
3360   "Return a list of article numbers that are children of article NUMBER."
3361   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3362          (level (gnus-data-level (car data)))
3363          l children)
3364     (while (and (setq data (cdr data))
3365                 (> (setq l (gnus-data-level (car data))) level))
3366       (and (= (1+ level) l)
3367            (push (gnus-data-number (car data))
3368                  children)))
3369     (nreverse children)))
3370
3371 (defun gnus-summary-article-parent (&optional number)
3372   "Return the article number of the parent of article NUMBER."
3373   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3374                                     (gnus-data-list t)))
3375          (level (gnus-data-level (car data))))
3376     (if (zerop level)
3377         ()                              ; This is a root.
3378       ;; We search until we find an article with a level less than
3379       ;; this one.  That function has to be the parent.
3380       (while (and (setq data (cdr data))
3381                   (not (< (gnus-data-level (car data)) level))))
3382       (and data (gnus-data-number (car data))))))
3383
3384 (defun gnus-unread-mark-p (mark)
3385   "Say whether MARK is the unread mark."
3386   (= mark gnus-unread-mark))
3387
3388 (defun gnus-read-mark-p (mark)
3389   "Say whether MARK is one of the marks that mark as read.
3390 This is all marks except unread, ticked, dormant, and expirable."
3391   (not (or (= mark gnus-unread-mark)
3392            (= mark gnus-ticked-mark)
3393            (= mark gnus-spam-mark)
3394            (= mark gnus-dormant-mark)
3395            (= mark gnus-expirable-mark))))
3396
3397 (defmacro gnus-article-mark (number)
3398   "Return the MARK of article NUMBER.
3399 This macro should only be used when computing the mark the \"first\"
3400 time; i.e., when generating the summary lines.  After that,
3401 `gnus-summary-article-mark' should be used to examine the
3402 marks of articles."
3403   `(cond
3404     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3405     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3406     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3407     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3408     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3409     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3410     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3411     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3412            gnus-ancient-mark))))
3413
3414 ;; Saving hidden threads.
3415
3416 (defmacro gnus-save-hidden-threads (&rest forms)
3417   "Save hidden threads, eval FORMS, and restore the hidden threads."
3418   (let ((config (make-symbol "config")))
3419     `(let ((,config (gnus-hidden-threads-configuration)))
3420        (unwind-protect
3421            (save-excursion
3422              ,@forms)
3423          (gnus-restore-hidden-threads-configuration ,config)))))
3424 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3425 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3426
3427 (defun gnus-data-compute-positions ()
3428   "Compute the positions of all articles."
3429   (setq gnus-newsgroup-data-reverse nil)
3430   (let ((data gnus-newsgroup-data))
3431     (save-excursion
3432       (gnus-save-hidden-threads
3433         (gnus-summary-show-all-threads)
3434         (goto-char (point-min))
3435         (while data
3436           (while (get-text-property (point) 'gnus-intangible)
3437             (forward-line 1))
3438           (gnus-data-set-pos (car data) (+ (point) 3))
3439           (setq data (cdr data))
3440           (forward-line 1))))))
3441
3442 (defun gnus-hidden-threads-configuration ()
3443   "Return the current hidden threads configuration."
3444   (save-excursion
3445     (let (config)
3446       (goto-char (point-min))
3447       (while (not (eobp))
3448         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3449           (push (save-excursion (forward-line 0) (point)) config))
3450         (forward-line 1))
3451       config)))
3452
3453 (defun gnus-restore-hidden-threads-configuration (config)
3454   "Restore hidden threads configuration from CONFIG."
3455   (save-excursion
3456     (let (point (inhibit-read-only t))
3457       (while (setq point (pop config))
3458         (goto-char point)
3459         (gnus-summary-hide-thread)))))
3460
3461 ;; Various summary mode internalish functions.
3462
3463 (defun gnus-mouse-pick-article (e)
3464   (interactive "e")
3465   (mouse-set-point e)
3466   (gnus-summary-next-page nil t))
3467
3468 (defun gnus-summary-set-display-table ()
3469   "Change the display table.
3470 Odd characters have a tendency to mess
3471 up nicely formatted displays - we make all possible glyphs
3472 display only a single character."
3473
3474   ;; We start from the standard display table, if any.
3475   (let ((table (or (copy-sequence standard-display-table)
3476                    (make-display-table)))
3477         (i 32))
3478     ;; Nix out all the control chars...
3479     (while (>= (setq i (1- i)) 0)
3480       (gnus-put-display-table i [??] table))
3481    ;; ... but not newline and cr, of course.  (cr is necessary for the
3482     ;; selective display).
3483     (gnus-put-display-table ?\n nil table)
3484     (gnus-put-display-table ?\r nil table)
3485     ;; We keep TAB as well.
3486     (gnus-put-display-table ?\t nil table)
3487     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3488     ;; Emacs 23 (unicode), that are not set already.
3489     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3490                  160
3491                256)))
3492       (while (>= (setq i (1- i)) 127)
3493         ;; Only modify if the entry is nil.
3494         (unless (gnus-get-display-table i table)
3495           (gnus-put-display-table i [??] table))))
3496     (setq buffer-display-table table)))
3497
3498 (defun gnus-summary-set-article-display-arrow (pos)
3499   "Update the overlay arrow to point to line at position POS."
3500   (when gnus-summary-display-arrow
3501     (make-local-variable 'overlay-arrow-position)
3502     (make-local-variable 'overlay-arrow-string)
3503     (save-excursion
3504       (goto-char pos)
3505       (beginning-of-line)
3506       (unless overlay-arrow-position
3507         (setq overlay-arrow-position (make-marker)))
3508       (setq overlay-arrow-string "=>"
3509             overlay-arrow-position (set-marker overlay-arrow-position
3510                                                (point)
3511                                                (current-buffer))))))
3512
3513 (defun gnus-summary-setup-buffer (group)
3514   "Initialize summary buffer.
3515 If the setup was successful, non-nil is returned."
3516   (let ((buffer (gnus-summary-buffer-name group))
3517         (dead-name (concat "*Dead Summary "
3518                            (gnus-group-decoded-name group) "*")))
3519     ;; If a dead summary buffer exists, we kill it.
3520     (when (gnus-buffer-live-p dead-name)
3521       (gnus-kill-buffer dead-name))
3522     (if (get-buffer buffer)
3523         (progn
3524           (set-buffer buffer)
3525           (setq gnus-summary-buffer (current-buffer))
3526           (not gnus-newsgroup-prepared))
3527       (set-buffer (gnus-get-buffer-create buffer))
3528       (setq gnus-summary-buffer (current-buffer))
3529       (gnus-summary-mode group)
3530       (when (gnus-group-quit-config group)
3531         (set (make-local-variable 'gnus-single-article-buffer) nil))
3532       (make-local-variable 'gnus-article-buffer)
3533       (make-local-variable 'gnus-article-current)
3534       (make-local-variable 'gnus-original-article-buffer)
3535       (setq gnus-newsgroup-name group)
3536       ;; Set any local variables in the group parameters.
3537       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3538       t)))
3539
3540 (defun gnus-set-global-variables ()
3541   "Set the global equivalents of the buffer-local variables.
3542 They are set to the latest values they had.  These reflect the summary
3543 buffer that was in action when the last article was fetched."
3544   (when (eq major-mode 'gnus-summary-mode)
3545     (setq gnus-summary-buffer (current-buffer))
3546     (let ((name gnus-newsgroup-name)
3547           (marked gnus-newsgroup-marked)
3548           (spam gnus-newsgroup-spam-marked)
3549           (unread gnus-newsgroup-unreads)
3550           (headers gnus-current-headers)
3551           (data gnus-newsgroup-data)
3552           (summary gnus-summary-buffer)
3553           (article-buffer gnus-article-buffer)
3554           (original gnus-original-article-buffer)
3555           (gac gnus-article-current)
3556           (reffed gnus-reffed-article-number)
3557           (score-file gnus-current-score-file)
3558           (default-charset gnus-newsgroup-charset)
3559           vlist)
3560       (let ((locals gnus-newsgroup-variables))
3561         (while locals
3562           (if (consp (car locals))
3563               (push (eval (caar locals)) vlist)
3564             (push (eval (car locals)) vlist))
3565           (setq locals (cdr locals)))
3566         (setq vlist (nreverse vlist)))
3567       (with-temp-buffer
3568         (setq gnus-newsgroup-name name
3569               gnus-newsgroup-marked marked
3570               gnus-newsgroup-spam-marked spam
3571               gnus-newsgroup-unreads unread
3572               gnus-current-headers headers
3573               gnus-newsgroup-data data
3574               gnus-article-current gac
3575               gnus-summary-buffer summary
3576               gnus-article-buffer article-buffer
3577               gnus-original-article-buffer original
3578               gnus-reffed-article-number reffed
3579               gnus-current-score-file score-file
3580               gnus-newsgroup-charset default-charset)
3581         (let ((locals gnus-newsgroup-variables))
3582           (while locals
3583             (if (consp (car locals))
3584                 (set (caar locals) (pop vlist))
3585               (set (car locals) (pop vlist)))
3586             (setq locals (cdr locals))))))))
3587
3588 (defun gnus-summary-article-unread-p (article)
3589   "Say whether ARTICLE is unread or not."
3590   (memq article gnus-newsgroup-unreads))
3591
3592 (defun gnus-summary-first-article-p (&optional article)
3593   "Return whether ARTICLE is the first article in the buffer."
3594   (if (not (setq article (or article (gnus-summary-article-number))))
3595       nil
3596     (eq article (caar gnus-newsgroup-data))))
3597
3598 (defun gnus-summary-last-article-p (&optional article)
3599   "Return whether ARTICLE is the last article in the buffer."
3600   (if (not (setq article (or article (gnus-summary-article-number))))
3601       ;; All non-existent numbers are the last article.  :-)
3602       t
3603     (not (cdr (gnus-data-find-list article)))))
3604
3605 (defun gnus-make-thread-indent-array (&optional n)
3606   (when (or n
3607             (progn (setq n 200) nil)
3608             (null gnus-thread-indent-array)
3609             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3610     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3611           gnus-thread-indent-array-level gnus-thread-indent-level)
3612     (while (>= n 0)
3613       (aset gnus-thread-indent-array n
3614             (make-string (* n gnus-thread-indent-level) ? ))
3615       (setq n (1- n)))))
3616
3617 (defun gnus-update-summary-mark-positions ()
3618   "Compute where the summary marks are to go."
3619   (save-excursion
3620     (when (gnus-buffer-exists-p gnus-summary-buffer)
3621       (set-buffer gnus-summary-buffer))
3622     (let ((spec gnus-summary-line-format-spec)
3623           pos)
3624       (save-excursion
3625         (gnus-set-work-buffer)
3626         (let ((gnus-tmp-unread ?Z)
3627               (gnus-replied-mark ?Z)
3628               (gnus-score-below-mark ?Z)
3629               (gnus-score-over-mark ?Z)
3630               (gnus-undownloaded-mark ?Z)
3631               (gnus-summary-line-format-spec spec)
3632               (gnus-newsgroup-downloadable '(0))
3633               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3634               case-fold-search ignores)
3635           ;; Here, all marks are bound to Z.
3636           (gnus-summary-insert-line header
3637                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3638           (goto-char (point-min))
3639           ;; Memorize the positions of the same characters as dummy marks.
3640           (while (re-search-forward "[A-D]" nil t)
3641             (push (point) ignores))
3642           (erase-buffer)
3643           ;; We use A-D as dummy marks in order to know column positions
3644           ;; where marks should be inserted.
3645           (setq gnus-tmp-unread ?A
3646                 gnus-replied-mark ?B
3647                 gnus-score-below-mark ?C
3648                 gnus-score-over-mark ?C
3649                 gnus-undownloaded-mark ?D)
3650           (gnus-summary-insert-line header
3651                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3652           ;; Ignore characters which aren't dummy marks.
3653           (dolist (p ignores)
3654             (delete-region (goto-char (1- p)) p)
3655             (insert ?Z))
3656           (goto-char (point-min))
3657           (setq pos (list (cons 'unread
3658                                 (and (search-forward "A" nil t)
3659                                      (- (point) (point-min) 1)))))
3660           (goto-char (point-min))
3661           (push (cons 'replied (and (search-forward "B" nil t)
3662                                     (- (point) (point-min) 1)))
3663                 pos)
3664           (goto-char (point-min))
3665           (push (cons 'score (and (search-forward "C" nil t)
3666                                   (- (point) (point-min) 1)))
3667                 pos)
3668           (goto-char (point-min))
3669           (push (cons 'download (and (search-forward "D" nil t)
3670                                      (- (point) (point-min) 1)))
3671                 pos)))
3672       (setq gnus-summary-mark-positions pos))))
3673
3674 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3675   "Insert a dummy root in the summary buffer."
3676   (beginning-of-line)
3677   (gnus-add-text-properties
3678    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3679    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3680
3681 (defun gnus-summary-extract-address-component (from)
3682   (or (car (funcall gnus-extract-address-components from))
3683       from))
3684
3685 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3686   (let ((mail-parse-charset gnus-newsgroup-charset)
3687         (ignored-from-addresses (gnus-ignored-from-addresses))
3688         ;; Is it really necessary to do this next part for each summary line?
3689         ;; Luckily, doesn't seem to slow things down much.
3690         (mail-parse-ignored-charsets
3691          (with-current-buffer gnus-summary-buffer
3692            gnus-newsgroup-ignored-charsets)))
3693     (or
3694      (and ignored-from-addresses
3695           (string-match ignored-from-addresses gnus-tmp-from)
3696           (let ((extra-headers (mail-header-extra header))
3697                 to
3698                 newsgroups)
3699             (cond
3700              ((setq to (cdr (assq 'To extra-headers)))
3701               (concat gnus-summary-to-prefix
3702                       (inline
3703                         (gnus-summary-extract-address-component
3704                          (funcall gnus-decode-encoded-address-function to)))))
3705              ((setq newsgroups
3706                     (or
3707                      (cdr (assq 'Newsgroups extra-headers))
3708                      (and
3709                       (memq 'Newsgroups gnus-extra-headers)
3710                       (eq (car (gnus-find-method-for-group
3711                                 gnus-newsgroup-name)) 'nntp)
3712                       (gnus-group-real-name gnus-newsgroup-name))))
3713               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3714      (gnus-string-mark-left-to-right
3715       (inline
3716         (gnus-summary-extract-address-component gnus-tmp-from))))))
3717
3718 (defun gnus-summary-insert-line (gnus-tmp-header
3719                                  gnus-tmp-level gnus-tmp-current
3720                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3721                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3722                                  &optional gnus-tmp-dummy gnus-tmp-score
3723                                  gnus-tmp-process)
3724   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3725       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3726                                           gnus-tmp-level)))
3727   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3728          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3729          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3730          (gnus-tmp-score-char
3731           (if (or (null gnus-summary-default-score)
3732                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3733                       gnus-summary-zcore-fuzz))
3734               ?                         ;Whitespace
3735             (if (< gnus-tmp-score gnus-summary-default-score)
3736                 gnus-score-below-mark gnus-score-over-mark)))
3737          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3738          (gnus-tmp-replied
3739           (cond (gnus-tmp-process gnus-process-mark)
3740                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3741                  gnus-cached-mark)
3742                 (gnus-tmp-replied gnus-replied-mark)
3743                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3744                  gnus-forwarded-mark)
3745                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3746                  gnus-saved-mark)
3747                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3748                  gnus-unseen-mark)
3749                 (t gnus-no-mark)))
3750          (gnus-tmp-downloaded
3751           (cond (undownloaded
3752                  gnus-undownloaded-mark)
3753                 (gnus-newsgroup-agentized
3754                  gnus-downloaded-mark)
3755                 (t
3756                  gnus-no-mark)))
3757          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3758          (gnus-tmp-name
3759           (cond
3760            ((string-match "<[^>]+> *$" gnus-tmp-from)
3761             (let ((beg (match-beginning 0)))
3762               (or (and (string-match "^\".+\"" gnus-tmp-from)
3763                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3764                   (substring gnus-tmp-from 0 beg))))
3765            ((string-match "(.+)" gnus-tmp-from)
3766             (substring gnus-tmp-from
3767                        (1+ (match-beginning 0)) (1- (match-end 0))))
3768            (t gnus-tmp-from)))
3769          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3770          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3771          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3772          (inhibit-read-only t))
3773     (when (string= gnus-tmp-name "")
3774       (setq gnus-tmp-name gnus-tmp-from))
3775     (unless (numberp gnus-tmp-lines)
3776       (setq gnus-tmp-lines -1))
3777     (if (= gnus-tmp-lines -1)
3778         (setq gnus-tmp-lines "?")
3779       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3780     (condition-case ()
3781         (gnus-put-text-property
3782          (point)
3783          (progn (eval gnus-summary-line-format-spec) (point))
3784          'gnus-number gnus-tmp-number)
3785       (error (gnus-message 5 "Error updating the summary line")))
3786     (when (gnus-visual-p 'summary-highlight 'highlight)
3787       (forward-line -1)
3788       (gnus-summary-highlight-line)
3789       (gnus-run-hooks 'gnus-summary-update-hook)
3790       (forward-line 1))))
3791
3792 (defun gnus-summary-update-line (&optional dont-update)
3793   "Update summary line after change."
3794   (when (and gnus-summary-default-score
3795              (not gnus-summary-inhibit-highlight))
3796     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3797            (article (gnus-summary-article-number))
3798            (score (gnus-summary-article-score article)))
3799       (unless dont-update
3800         (if (and gnus-summary-mark-below
3801                  (< (gnus-summary-article-score)
3802                     gnus-summary-mark-below))
3803             ;; This article has a low score, so we mark it as read.
3804             (when (memq article gnus-newsgroup-unreads)
3805               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3806           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3807             ;; This article was previously marked as read on account
3808             ;; of a low score, but now it has risen, so we mark it as
3809             ;; unread.
3810             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3811         (gnus-summary-update-mark
3812          (if (or (null gnus-summary-default-score)
3813                  (<= (abs (- score gnus-summary-default-score))
3814                      gnus-summary-zcore-fuzz))
3815              ?                          ;Whitespace
3816            (if (< score gnus-summary-default-score)
3817                gnus-score-below-mark gnus-score-over-mark))
3818          'score))
3819       ;; Do visual highlighting.
3820       (when (gnus-visual-p 'summary-highlight 'highlight)
3821         (gnus-summary-highlight-line)
3822         (gnus-run-hooks 'gnus-summary-update-hook)))))
3823
3824 (defvar gnus-tmp-new-adopts nil)
3825
3826 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3827   "Return the number of articles in THREAD.
3828 This may be 0 in some cases -- if none of the articles in
3829 the thread are to be displayed."
3830   (let* ((number
3831          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3832           (cond
3833            ((not (listp thread))
3834             1)
3835            ((and (consp thread) (cdr thread))
3836             (apply
3837              '+ 1 (mapcar
3838                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3839            ((null thread)
3840             1)
3841            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3842             1)
3843            (t 0))))
3844     (when (and level (zerop level) gnus-tmp-new-adopts)
3845       (incf number
3846             (apply '+ (mapcar
3847                        'gnus-summary-number-of-articles-in-thread
3848                        gnus-tmp-new-adopts))))
3849     (if char
3850         (if (> number 1) gnus-not-empty-thread-mark
3851           gnus-empty-thread-mark)
3852       number)))
3853
3854 (defsubst gnus-summary-line-message-size (head)
3855   "Return pretty-printed version of message size.
3856 This function is intended to be used in
3857 `gnus-summary-line-format-alist'."
3858   (let ((c (or (mail-header-chars head) -1)))
3859     (cond ((< c 0) "n/a")               ; chars not available
3860           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3861           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3862           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3863           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3864
3865 (defcustom gnus-user-date-format-alist
3866   '(((gnus-seconds-today) . "Today, %H:%M")
3867     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3868     (604800 . "%A %H:%M")               ; That's one week
3869     ((gnus-seconds-month) . "%A %d")
3870     ((gnus-seconds-year) . "%B %d")
3871     (t . "%b %d %Y"))                   ; This one is used when no other
3872                                         ; does match
3873   "Specifies date format depending on age of article.
3874 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3875 seconds) or a Lisp expression evaluating to a number.  When the age of
3876 the article is less than this number, then use `format-time-string'
3877 with the corresponding FORMAT for displaying the date of the article.
3878 If AGE is not a number or a Lisp expression evaluating to a
3879 non-number, then the corresponding FORMAT is used as a default value.
3880
3881 Note that the list is processed from the beginning, so it should be
3882 sorted by ascending AGE.  Also note that items following the first
3883 non-number AGE will be ignored.
3884
3885 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3886 and `gnus-seconds-year' in the AGE spec.  They return the number of
3887 seconds passed since the start of today, of this month, of this year,
3888 respectively."
3889   :version "24.1"
3890   :group 'gnus-summary-format
3891   :type '(alist :key-type sexp :value-type string))
3892
3893 (defun gnus-user-date (messy-date)
3894   "Format the messy-date according to `gnus-user-date-format-alist'.
3895 Returns \"  ?  \" if there's bad input or if another error occurs.
3896 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3897   (condition-case ()
3898       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3899              (now (gnus-float-time))
3900              ;;If we don't find something suitable we'll use this one
3901              (my-format "%b %d '%y"))
3902         (let* ((difference (- now messy-date))
3903                (templist gnus-user-date-format-alist)
3904                (top (eval (caar templist))))
3905           (while (if (numberp top) (< top difference) (not top))
3906             (progn
3907               (setq templist (cdr templist))
3908               (setq top (eval (caar templist)))))
3909           (if (stringp (cdr (car templist)))
3910               (setq my-format (cdr (car templist)))))
3911         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3912     (error "  ?   ")))
3913
3914 (defun gnus-summary-set-local-parameters (group)
3915   "Go through the local params of GROUP and set all variable specs in that list."
3916   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3917                                         ; really variables.
3918     (dolist (elem (gnus-group-find-parameter group))
3919       (and (consp elem)                 ; Has to be a cons.
3920            (consp (cdr elem))           ; The cdr has to be a list.
3921            (symbolp (car elem))         ; Has to be a symbol in there.
3922            (not (memq (car elem) vars))
3923            (ignore-errors
3924              (push (car elem) vars)
3925              ;; Variables like `gnus-show-threads' that are globally
3926              ;; bound, if used as group parameters, need to get to be
3927              ;; buffer-local, whereas just parameters like `gcc-self',
3928              ;; `timestamp', etc. should not be bound as variables.
3929              (if (boundp (car elem))
3930                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3931                (eval (nth 1 elem))))))))
3932
3933 (defun gnus-summary-read-group (group &optional show-all no-article
3934                                       kill-buffer no-display backward
3935                                       select-articles)
3936   "Start reading news in newsgroup GROUP.
3937 If SHOW-ALL is non-nil, already read articles are also listed.
3938 If NO-ARTICLE is non-nil, no article is selected initially.
3939 If NO-DISPLAY, don't generate the summary buffer contents.
3940 If KILL-BUFFER, it should be a buffer that's killed once the new
3941 summary buffer has been generated.
3942 If BACKWARD, move point to the previous group in the group buffer
3943 If SELECT-ARTICLES, only select those articles from GROUP."
3944   (let (result)
3945     (while (and group
3946                 (null (setq result
3947                             (let ((gnus-auto-select-next nil))
3948                               (or (gnus-summary-read-group-1
3949                                    group show-all no-article
3950                                    kill-buffer no-display
3951                                    select-articles)
3952                                   (setq show-all nil
3953                                         select-articles nil)))))
3954                 (eq gnus-auto-select-next 'quietly))
3955       (set-buffer gnus-group-buffer)
3956       ;; The entry function called above goes to the next
3957       ;; group automatically, so we go two groups back
3958       ;; if we are searching for the previous group.
3959       (when backward
3960         (gnus-group-prev-unread-group 2))
3961       (if (not (equal group (gnus-group-group-name)))
3962           (setq group (gnus-group-group-name))
3963         (setq group nil)))
3964     result))
3965
3966 (defun gnus-summary-read-group-1 (group show-all no-article
3967                                         kill-buffer no-display
3968                                         &optional select-articles)
3969   ;; Killed foreign groups can't be entered.
3970   ;;  (when (and (not (gnus-group-native-p group))
3971   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3972   ;;    (error "Dead non-native groups can't be entered"))
3973   (gnus-message 7 "Retrieving newsgroup: %s..."
3974                 (gnus-group-decoded-name group))
3975   (let* ((new-group (gnus-summary-setup-buffer group))
3976          (quit-config (gnus-group-quit-config group))
3977          (did-select (and new-group (gnus-select-newsgroup
3978                                      group show-all select-articles))))
3979     (cond
3980      ;; This summary buffer exists already, so we just select it.
3981      ((not new-group)
3982       (gnus-set-global-variables)
3983       (when kill-buffer
3984         (gnus-kill-or-deaden-summary kill-buffer))
3985       (gnus-configure-windows 'summary 'force)
3986       (gnus-set-mode-line 'summary)
3987       (gnus-summary-position-point)
3988       (message "")
3989       t)
3990      ;; We couldn't select this group.
3991      ((null did-select)
3992       (when (and (eq major-mode 'gnus-summary-mode)
3993                  (not (equal (current-buffer) kill-buffer)))
3994         (kill-buffer (current-buffer))
3995         (if (not quit-config)
3996             (progn
3997               ;; Update the info -- marks might need to be removed,
3998               ;; for instance.
3999               (gnus-summary-update-info)
4000               (set-buffer gnus-group-buffer)
4001               (gnus-group-jump-to-group group)
4002               (gnus-group-next-unread-group 1))
4003           (gnus-handle-ephemeral-exit quit-config)))
4004       (if (null (gnus-list-of-unread-articles group))
4005           (gnus-message 3 "Group %s contains no messages" group)
4006         (gnus-message 3 "Can't select group"))
4007       nil)
4008      ;; The user did a `C-g' while prompting for number of articles,
4009      ;; so we exit this group.
4010      ((eq did-select 'quit)
4011       (and (eq major-mode 'gnus-summary-mode)
4012            (not (equal (current-buffer) kill-buffer))
4013            (kill-buffer (current-buffer)))
4014       (when kill-buffer
4015         (gnus-kill-or-deaden-summary kill-buffer))
4016       (if (not quit-config)
4017           (progn
4018             (set-buffer gnus-group-buffer)
4019             (gnus-group-jump-to-group group)
4020             (gnus-configure-windows 'group 'force))
4021         (gnus-handle-ephemeral-exit quit-config))
4022       ;; Finally signal the quit.
4023       (signal 'quit nil))
4024      ;; The group was successfully selected.
4025      (t
4026       (gnus-set-global-variables)
4027       ;; Save the active value in effect when the group was entered.
4028       (setq gnus-newsgroup-active
4029             (gnus-copy-sequence
4030              (gnus-active gnus-newsgroup-name)))
4031       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4032       ;; You can change the summary buffer in some way with this hook.
4033       (gnus-run-hooks 'gnus-select-group-hook)
4034       (when (memq 'summary (gnus-update-format-specifications
4035                             nil 'summary 'summary-mode 'summary-dummy))
4036         ;; The format specification for the summary line was updated,
4037         ;; so we need to update the mark positions as well.
4038         (gnus-update-summary-mark-positions))
4039       ;; Do score processing.
4040       (when gnus-use-scoring
4041         (gnus-possibly-score-headers))
4042       ;; Check whether to fill in the gaps in the threads.
4043       (when gnus-build-sparse-threads
4044         (gnus-build-sparse-threads))
4045       ;; Find the initial limit.
4046       (if show-all
4047           (let ((gnus-newsgroup-dormant nil))
4048             (gnus-summary-initial-limit show-all))
4049         (gnus-summary-initial-limit show-all))
4050       ;; Generate the summary buffer.
4051       (unless no-display
4052         (gnus-summary-prepare))
4053       (when gnus-use-trees
4054         (gnus-tree-open group)
4055         (setq gnus-summary-highlight-line-function
4056               'gnus-tree-highlight-article))
4057       ;; If the summary buffer is empty, but there are some low-scored
4058       ;; articles or some excluded dormants, we include these in the
4059       ;; buffer.
4060       (when (and (zerop (buffer-size))
4061                  (not no-display))
4062         (cond (gnus-newsgroup-dormant
4063                (gnus-summary-limit-include-dormant))
4064               ((and gnus-newsgroup-scored show-all)
4065                (gnus-summary-limit-include-expunged t))))
4066       ;; Function `gnus-apply-kill-file' must be called in this hook.
4067       (gnus-run-hooks 'gnus-apply-kill-hook)
4068       (if (and (zerop (buffer-size))
4069                (not no-display))
4070           (progn
4071             ;; This newsgroup is empty.
4072             (gnus-summary-catchup-and-exit nil t)
4073             (gnus-message 6 "No unread news")
4074             (when kill-buffer
4075               (gnus-kill-or-deaden-summary kill-buffer))
4076             ;; Return nil from this function.
4077             nil)
4078         ;; Hide conversation thread subtrees.  We cannot do this in
4079         ;; gnus-summary-prepare-hook since kill processing may not
4080         ;; work with hidden articles.
4081         (gnus-summary-maybe-hide-threads)
4082         (gnus-configure-windows 'summary)
4083         (when kill-buffer
4084           (gnus-kill-or-deaden-summary kill-buffer))
4085         (gnus-summary-auto-select-subject)
4086         ;; Show first unread article if requested.
4087         (if (and (not no-article)
4088                  (not no-display)
4089                  gnus-newsgroup-unreads
4090                  gnus-auto-select-first)
4091             (progn
4092               (let ((art (gnus-summary-article-number)))
4093                 (when (and art
4094                            gnus-plugged
4095                            (not (memq art gnus-newsgroup-undownloaded))
4096                            (not (memq art gnus-newsgroup-downloadable)))
4097                   (gnus-summary-goto-article art))))
4098           ;; Don't select any articles.
4099           (gnus-summary-position-point)
4100           (gnus-configure-windows 'summary 'force)
4101           (gnus-set-mode-line 'summary))
4102         (when (and gnus-auto-center-group
4103                    (get-buffer-window gnus-group-buffer t))
4104           ;; Gotta use windows, because recenter does weird stuff if
4105           ;; the current buffer ain't the displayed window.
4106           (let ((owin (selected-window)))
4107             (select-window (get-buffer-window gnus-group-buffer t))
4108             (when (gnus-group-goto-group group)
4109               (recenter))
4110             (select-window owin)))
4111         ;; Mark this buffer as "prepared".
4112         (setq gnus-newsgroup-prepared t)
4113         (gnus-run-hooks 'gnus-summary-prepared-hook)
4114         (unless (gnus-ephemeral-group-p group)
4115           (gnus-group-update-group group nil t))
4116         t)))))
4117
4118 (defun gnus-summary-auto-select-subject ()
4119   "Select the subject line on initial group entry."
4120   (goto-char (point-min))
4121   (cond
4122    ((eq gnus-auto-select-subject 'best)
4123     (gnus-summary-best-unread-subject))
4124    ((eq gnus-auto-select-subject 'unread)
4125     (gnus-summary-first-unread-subject))
4126    ((eq gnus-auto-select-subject 'unseen)
4127     (gnus-summary-first-unseen-subject))
4128    ((eq gnus-auto-select-subject 'unseen-or-unread)
4129     (gnus-summary-first-unseen-or-unread-subject))
4130    ((eq gnus-auto-select-subject 'first)
4131     ;; Do nothing.
4132     )
4133    ((functionp gnus-auto-select-subject)
4134     (funcall gnus-auto-select-subject))))
4135
4136 (defun gnus-summary-prepare ()
4137   "Generate the summary buffer."
4138   (interactive)
4139   (let ((inhibit-read-only t))
4140     (erase-buffer)
4141     (setq gnus-newsgroup-data nil
4142           gnus-newsgroup-data-reverse nil)
4143     (gnus-run-hooks 'gnus-summary-generate-hook)
4144     ;; Generate the buffer, either with threads or without.
4145     (when gnus-newsgroup-headers
4146       (gnus-summary-prepare-threads
4147        (if gnus-show-threads
4148            (gnus-sort-gathered-threads
4149             (funcall gnus-summary-thread-gathering-function
4150                      (gnus-sort-threads
4151                       (gnus-cut-threads (gnus-make-threads)))))
4152          ;; Unthreaded display.
4153          (gnus-sort-articles gnus-newsgroup-headers))))
4154     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4155     ;; Call hooks for modifying summary buffer.
4156     (goto-char (point-min))
4157     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4158
4159 (defsubst gnus-general-simplify-subject (subject)
4160   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4161   (setq subject
4162         (cond
4163          ;; Truncate the subject.
4164          (gnus-simplify-subject-functions
4165           (gnus-map-function gnus-simplify-subject-functions subject))
4166          ((numberp gnus-summary-gather-subject-limit)
4167           (setq subject (gnus-simplify-subject-re subject))
4168           (if (> (length subject) gnus-summary-gather-subject-limit)
4169               (substring subject 0 gnus-summary-gather-subject-limit)
4170             subject))
4171          ;; Fuzzily simplify it.
4172          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4173           (gnus-simplify-subject-fuzzy subject))
4174          ;; Just remove the leading "Re:".
4175          (t
4176           (gnus-simplify-subject-re subject))))
4177
4178   (if (and gnus-summary-gather-exclude-subject
4179            (string-match gnus-summary-gather-exclude-subject subject))
4180       nil                         ; This article shouldn't be gathered
4181     subject))
4182
4183 (defun gnus-summary-simplify-subject-query ()
4184   "Query where the respool algorithm would put this article."
4185   (interactive)
4186   (gnus-summary-select-article)
4187   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4188
4189 (defun gnus-gather-threads-by-subject (threads)
4190   "Gather threads by looking at Subject headers."
4191   (if (not gnus-summary-make-false-root)
4192       threads
4193     (let ((hashtb (gnus-make-hashtable 1024))
4194           (prev threads)
4195           (result threads)
4196           subject hthread whole-subject)
4197       (while threads
4198         (setq subject (gnus-general-simplify-subject
4199                        (setq whole-subject (mail-header-subject
4200                                             (caar threads)))))
4201         (when subject
4202           (if (setq hthread (gnus-gethash subject hashtb))
4203               (progn
4204                 ;; We enter a dummy root into the thread, if we
4205                 ;; haven't done that already.
4206                 (unless (stringp (caar hthread))
4207                   (setcar hthread (list whole-subject (car hthread))))
4208                 ;; We add this new gathered thread to this gathered
4209                 ;; thread.
4210                 (setcdr (car hthread)
4211                         (nconc (cdar hthread) (list (car threads))))
4212                 ;; Remove it from the list of threads.
4213                 (setcdr prev (cdr threads))
4214                 (setq threads prev))
4215             ;; Enter this thread into the hash table.
4216             (gnus-sethash subject
4217                           (if gnus-summary-make-false-root-always
4218                               (progn
4219                                 ;; If you want a dummy root above all
4220                                 ;; threads...
4221                                 (setcar threads (list whole-subject
4222                                                       (car threads)))
4223                                 threads)
4224                             threads)
4225                           hashtb)))
4226         (setq prev threads)
4227         (setq threads (cdr threads)))
4228       result)))
4229
4230 (defun gnus-gather-threads-by-references (threads)
4231   "Gather threads by looking at References headers."
4232   (let ((idhashtb (gnus-make-hashtable 1024))
4233         (thhashtb (gnus-make-hashtable 1024))
4234         (prev threads)
4235         (result threads)
4236         ids references id gthread gid entered ref)
4237     (while threads
4238       (when (setq references (mail-header-references (caar threads)))
4239         (setq id (mail-header-id (caar threads))
4240               ids (inline (gnus-split-references references))
4241               entered nil)
4242         (while (setq ref (pop ids))
4243           (setq ids (delete ref ids))
4244           (if (not (setq gid (gnus-gethash ref idhashtb)))
4245               (progn
4246                 (gnus-sethash ref id idhashtb)
4247                 (gnus-sethash id threads thhashtb))
4248             (setq gthread (gnus-gethash gid thhashtb))
4249             (unless entered
4250               ;; We enter a dummy root into the thread, if we
4251               ;; haven't done that already.
4252               (unless (stringp (caar gthread))
4253                 (setcar gthread (list (mail-header-subject (caar gthread))
4254                                       (car gthread))))
4255               ;; We add this new gathered thread to this gathered
4256               ;; thread.
4257               (setcdr (car gthread)
4258                       (nconc (cdar gthread) (list (car threads)))))
4259             ;; Add it into the thread hash table.
4260             (gnus-sethash id gthread thhashtb)
4261             (setq entered t)
4262             ;; Remove it from the list of threads.
4263             (setcdr prev (cdr threads))
4264             (setq threads prev))))
4265       (setq prev threads)
4266       (setq threads (cdr threads)))
4267     result))
4268
4269 (defun gnus-sort-gathered-threads (threads)
4270   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4271   (let ((result threads))
4272     (while threads
4273       (when (stringp (caar threads))
4274         (setcdr (car threads)
4275                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4276       (setq threads (cdr threads)))
4277     result))
4278
4279 (defun gnus-thread-loop-p (root thread)
4280   "Say whether ROOT is in THREAD."
4281   (let ((stack (list thread))
4282         (infloop 0)
4283         th)
4284     (while (setq thread (pop stack))
4285       (setq th (cdr thread))
4286       (while (and th
4287                   (not (eq (caar th) root)))
4288         (pop th))
4289       (if th
4290           ;; We have found a loop.
4291           (let (ref-dep)
4292             (setcdr thread (delq (car th) (cdr thread)))
4293             (if (boundp (setq ref-dep (intern "none"
4294                                               gnus-newsgroup-dependencies)))
4295                 (setcdr (symbol-value ref-dep)
4296                         (nconc (cdr (symbol-value ref-dep))
4297                                (list (car th))))
4298               (set ref-dep (list nil (car th))))
4299             (setq infloop 1
4300                   stack nil))
4301         ;; Push all the subthreads onto the stack.
4302         (push (cdr thread) stack)))
4303     infloop))
4304
4305 (defun gnus-make-threads ()
4306   "Go through the dependency hashtb and find the roots.  Return all threads."
4307   (let (threads)
4308     (while (catch 'infloop
4309              (mapatoms
4310               (lambda (refs)
4311                 ;; Deal with self-referencing References loops.
4312                 (when (and (car (symbol-value refs))
4313                            (not (zerop
4314                                  (apply
4315                                   '+
4316                                   (mapcar
4317                                    (lambda (thread)
4318                                      (gnus-thread-loop-p
4319                                       (car (symbol-value refs)) thread))
4320                                    (cdr (symbol-value refs)))))))
4321                   (setq threads nil)
4322                   (throw 'infloop t))
4323                 (unless (car (symbol-value refs))
4324                   ;; These threads do not refer back to any other
4325                   ;; articles, so they're roots.
4326                   (setq threads (append (cdr (symbol-value refs)) threads))))
4327               gnus-newsgroup-dependencies)))
4328     threads))
4329
4330 ;; Build the thread tree.
4331 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4332   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4333
4334 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4335 if it was already present.
4336
4337 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4338 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4339 Message-IDs will be renamed to a unique Message-ID before being
4340 entered.
4341
4342 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4343   (let* ((id (mail-header-id header))
4344          (id-dep (and id (intern id dependencies)))
4345          parent-id ref ref-dep ref-header replaced)
4346     ;; Enter this `header' in the `dependencies' table.
4347     (cond
4348      ((not id-dep)
4349       (setq header nil))
4350      ;; The first two cases do the normal part: enter a new `header'
4351      ;; in the `dependencies' table.
4352      ((not (boundp id-dep))
4353       (set id-dep (list header)))
4354      ((null (car (symbol-value id-dep)))
4355       (setcar (symbol-value id-dep) header))
4356
4357      ;; From here the `header' was already present in the
4358      ;; `dependencies' table.
4359      (force-new
4360       ;; Overrides an existing entry;
4361       ;; just set the header part of the entry.
4362       (setcar (symbol-value id-dep) header)
4363       (setq replaced t))
4364
4365      ;; Renames the existing `header' to a unique Message-ID.
4366      ((not gnus-summary-ignore-duplicates)
4367       ;; An article with this Message-ID has already been seen.
4368       ;; We rename the Message-ID.
4369       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4370            (list header))
4371       (mail-header-set-id header id))
4372
4373      ;; The last case ignores an existing entry, except it adds any
4374      ;; additional Xrefs (in case the two articles came from different
4375      ;; servers.
4376      ;; Also sets `header' to `nil' meaning that the `dependencies'
4377      ;; table was *not* modified.
4378      (t
4379       (mail-header-set-xref
4380        (car (symbol-value id-dep))
4381        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4382                    "")
4383                (or (mail-header-xref header) "")))
4384       (setq header nil)))
4385
4386     (when (and header (not replaced))
4387       ;; First check that we are not creating a References loop.
4388       (setq parent-id (gnus-parent-id (mail-header-references header)))
4389       (setq ref parent-id)
4390       (while (and ref
4391                   (setq ref-dep (intern-soft ref dependencies))
4392                   (boundp ref-dep)
4393                   (setq ref-header (car (symbol-value ref-dep))))
4394         (if (string= id ref)
4395             ;; Yuk!  This is a reference loop.  Make the article be a
4396             ;; root article.
4397             (progn
4398               (mail-header-set-references (car (symbol-value id-dep)) "none")
4399               (setq ref nil)
4400               (setq parent-id nil))
4401           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4402       (setq ref-dep (intern (or parent-id "none") dependencies))
4403       (if (boundp ref-dep)
4404           (setcdr (symbol-value ref-dep)
4405                   (nconc (cdr (symbol-value ref-dep))
4406                          (list (symbol-value id-dep))))
4407         (set ref-dep (list nil (symbol-value id-dep)))))
4408     header))
4409
4410 (defun gnus-extract-message-id-from-in-reply-to (string)
4411   (if (string-match "<[^>]+>" string)
4412       (substring string (match-beginning 0) (match-end 0))
4413     nil))
4414
4415 (defun gnus-build-sparse-threads ()
4416   (let ((headers gnus-newsgroup-headers)
4417         (mail-parse-charset gnus-newsgroup-charset)
4418         (gnus-summary-ignore-duplicates t)
4419         header references generation relations
4420         subject child end new-child date)
4421     ;; First we create an alist of generations/relations, where
4422     ;; generations is how much we trust the relation, and the relation
4423     ;; is parent/child.
4424     (gnus-message 7 "Making sparse threads...")
4425     (save-excursion
4426       (nnheader-set-temp-buffer " *gnus sparse threads*")
4427       (while (setq header (pop headers))
4428         (when (and (setq references (mail-header-references header))
4429                    (not (string= references "")))
4430           (insert references)
4431           (setq child (mail-header-id header)
4432                 subject (mail-header-subject header)
4433                 date (mail-header-date header)
4434                 generation 0)
4435           (while (search-backward ">" nil t)
4436             (setq end (1+ (point)))
4437             (when (search-backward "<" nil t)
4438               (setq new-child (buffer-substring (point) end))
4439               (push (list (incf generation)
4440                           child (setq child new-child)
4441                           subject date)
4442                     relations)))
4443           (when child
4444             (push (list (1+ generation) child nil subject) relations))
4445           (erase-buffer)))
4446       (kill-buffer (current-buffer)))
4447     ;; Sort over trustworthiness.
4448     (dolist (relation (sort relations 'car-less-than-car))
4449       (when (gnus-dependencies-add-header
4450              (make-full-mail-header
4451               gnus-reffed-article-number
4452               (nth 3 relation) "" (or (nth 4 relation) "")
4453               (nth 1 relation)
4454               (or (nth 2 relation) "") 0 0 "")
4455              gnus-newsgroup-dependencies nil)
4456         (push gnus-reffed-article-number gnus-newsgroup-limit)
4457         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4458         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4459               gnus-newsgroup-reads)
4460         (decf gnus-reffed-article-number)))
4461     (gnus-message 7 "Making sparse threads...done")))
4462
4463 (defun gnus-build-old-threads ()
4464   ;; Look at all the articles that refer back to old articles, and
4465   ;; fetch the headers for the articles that aren't there.  This will
4466   ;; build complete threads - if the roots haven't been expired by the
4467   ;; server, that is.
4468   (let ((mail-parse-charset gnus-newsgroup-charset)
4469         id heads)
4470     (mapatoms
4471      (lambda (refs)
4472        (when (not (car (symbol-value refs)))
4473          (setq heads (cdr (symbol-value refs)))
4474          (while heads
4475            (if (memq (mail-header-number (caar heads))
4476                      gnus-newsgroup-dormant)
4477                (setq heads (cdr heads))
4478              (setq id (symbol-name refs))
4479              (while (and (setq id (gnus-build-get-header id))
4480                          (not (car (gnus-id-to-thread id)))))
4481              (setq heads nil)))))
4482      gnus-newsgroup-dependencies)))
4483
4484 (defsubst gnus-remove-odd-characters (string)
4485   "Translate STRING into something that doesn't contain weird characters."
4486   (mm-subst-char-in-string
4487    ?\r ?\-
4488    (mm-subst-char-in-string ?\n ?\- string t) t))
4489
4490 ;; This function has to be called with point after the article number
4491 ;; on the beginning of the line.
4492 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4493   (let ((eol (point-at-eol))
4494         (buffer (current-buffer))
4495         header references in-reply-to)
4496
4497     ;; overview: [num subject from date id refs chars lines misc]
4498     (unwind-protect
4499         (let (x)
4500           (narrow-to-region (point) eol)
4501           (unless (eobp)
4502             (forward-char))
4503
4504           (setq header
4505                 (make-full-mail-header
4506                  number                 ; number
4507                  (condition-case ()     ; subject
4508                      (gnus-remove-odd-characters
4509                       (funcall gnus-decode-encoded-word-function
4510                                (setq x (nnheader-nov-field))))
4511                    (error x))
4512                  (condition-case ()     ; from
4513                      (gnus-remove-odd-characters
4514                       (funcall gnus-decode-encoded-address-function
4515                                (setq x (nnheader-nov-field))))
4516                    (error x))
4517                  (nnheader-nov-field)   ; date
4518                  (nnheader-nov-read-message-id number)  ; id
4519                  (setq references (nnheader-nov-field)) ; refs
4520                  (nnheader-nov-read-integer) ; chars
4521                  (nnheader-nov-read-integer) ; lines
4522                  (unless (eobp)
4523                    (if (looking-at "Xref: ")
4524                        (goto-char (match-end 0)))
4525                    (nnheader-nov-field)) ; Xref
4526                  (nnheader-nov-parse-extra)))) ; extra
4527
4528       (widen))
4529
4530     (when (and (string= references "")
4531                (setq in-reply-to (mail-header-extra header))
4532                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4533       (mail-header-set-references
4534        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4535
4536     (when gnus-alter-header-function
4537       (funcall gnus-alter-header-function header))
4538     (gnus-dependencies-add-header header dependencies force-new)))
4539
4540 (defun gnus-build-get-header (id)
4541   "Look through the buffer of NOV lines and find the header to ID.
4542 Enter this line into the dependencies hash table, and return
4543 the id of the parent article (if any)."
4544   (let ((deps gnus-newsgroup-dependencies)
4545         found header)
4546     (prog1
4547         (with-current-buffer nntp-server-buffer
4548           (let ((case-fold-search nil))
4549             (goto-char (point-min))
4550             (while (and (not found)
4551                         (search-forward id nil t))
4552               (beginning-of-line)
4553               (setq found (looking-at
4554                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4555                                    (regexp-quote id))))
4556               (or found (beginning-of-line 2)))
4557             (when found
4558               (beginning-of-line)
4559               (and
4560                (setq header (gnus-nov-parse-line
4561                              (read (current-buffer)) deps))
4562                (gnus-parent-id (mail-header-references header))))))
4563       (when header
4564         (let ((number (mail-header-number header)))
4565           (push number gnus-newsgroup-limit)
4566           (push header gnus-newsgroup-headers)
4567           (if (memq number gnus-newsgroup-unselected)
4568               (progn
4569                 (setq gnus-newsgroup-unreads
4570                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4571                                                number))
4572                 (setq gnus-newsgroup-unselected
4573                       (delq number gnus-newsgroup-unselected)))
4574             (push number gnus-newsgroup-ancient)))))))
4575
4576 (defun gnus-build-all-threads ()
4577   "Read all the headers."
4578   (let ((gnus-summary-ignore-duplicates t)
4579         (mail-parse-charset gnus-newsgroup-charset)
4580         (dependencies gnus-newsgroup-dependencies)
4581         header article)
4582     (with-current-buffer nntp-server-buffer
4583       (let ((case-fold-search nil))
4584         (goto-char (point-min))
4585         (while (not (eobp))
4586           (ignore-errors
4587             (setq article (read (current-buffer))
4588                   header (gnus-nov-parse-line article dependencies t)))
4589           (when header
4590             (with-current-buffer gnus-summary-buffer
4591               (push header gnus-newsgroup-headers)
4592               (if (memq (setq article (mail-header-number header))
4593                         gnus-newsgroup-unselected)
4594                   (progn
4595                     (setq gnus-newsgroup-unreads
4596                           (gnus-add-to-sorted-list
4597                            gnus-newsgroup-unreads article))
4598                     (setq gnus-newsgroup-unselected
4599                           (delq article gnus-newsgroup-unselected)))
4600                 (push article gnus-newsgroup-ancient)))
4601             (forward-line 1)))))))
4602
4603 (defun gnus-summary-update-article-line (article header)
4604   "Update the line for ARTICLE using HEADER."
4605   (let* ((id (mail-header-id header))
4606          (thread (gnus-id-to-thread id)))
4607     (unless thread
4608       (error "Article in no thread"))
4609     ;; Update the thread.
4610     (setcar thread header)
4611     (gnus-summary-goto-subject article)
4612     (let* ((datal (gnus-data-find-list article))
4613            (data (car datal))
4614            (inhibit-read-only t)
4615            (level (gnus-summary-thread-level)))
4616       (gnus-delete-line)
4617       (let ((inserted (- (point)
4618                          (progn
4619                            (gnus-summary-insert-line
4620                             header level nil
4621                             (memq article gnus-newsgroup-undownloaded)
4622                             (gnus-article-mark article)
4623                             (memq article gnus-newsgroup-replied)
4624                             (memq article gnus-newsgroup-expirable)
4625                             ;; Only insert the Subject string when it's different
4626                             ;; from the previous Subject string.
4627                             (if (and
4628                                  gnus-show-threads
4629                                  (gnus-subject-equal
4630                                   (condition-case ()
4631                                       (mail-header-subject
4632                                        (gnus-data-header
4633                                         (cadr
4634                                          (gnus-data-find-list
4635                                           article
4636                                           (gnus-data-list t)))))
4637                                     ;; Error on the side of excessive subjects.
4638                                     (error ""))
4639                                   (mail-header-subject header)))
4640                                 ""
4641                               (mail-header-subject header))
4642                             nil (cdr (assq article gnus-newsgroup-scored))
4643                             (memq article gnus-newsgroup-processable))
4644                            (point)))))
4645         (when (cdr datal)
4646           (gnus-data-update-list
4647            (cdr datal)
4648            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4649
4650 (defun gnus-summary-update-article (article &optional iheader)
4651   "Update ARTICLE in the summary buffer."
4652   (set-buffer gnus-summary-buffer)
4653   (let* ((header (gnus-summary-article-header article))
4654          (id (mail-header-id header))
4655          (data (gnus-data-find article))
4656          (thread (gnus-id-to-thread id))
4657          (references (mail-header-references header))
4658          (parent
4659           (gnus-id-to-thread
4660            (or (gnus-parent-id
4661                 (when (and references
4662                            (not (equal "" references)))
4663                   references))
4664                "none")))
4665          (inhibit-read-only t)
4666          (old (car thread)))
4667     (when thread
4668       (unless iheader
4669         (setcar thread nil)
4670         (when parent
4671           (delq thread parent)))
4672       (if (gnus-summary-insert-subject id header)
4673           ;; Set the (possibly) new article number in the data structure.
4674           (gnus-data-set-number data (gnus-id-to-article id))
4675         (setcar thread old)
4676         nil))))
4677
4678 (defun gnus-rebuild-thread (id &optional line)
4679   "Rebuild the thread containing ID.
4680 If LINE, insert the rebuilt thread starting on line LINE."
4681   (let ((inhibit-read-only t)
4682         old-pos current thread data)
4683     (if (not gnus-show-threads)
4684         (setq thread (list (car (gnus-id-to-thread id))))
4685       ;; Get the thread this article is part of.
4686       (setq thread (gnus-remove-thread id)))
4687     (setq old-pos (point-at-bol))
4688     (setq current (save-excursion
4689                     (and (re-search-backward "[\r\n]" nil t)
4690                          (gnus-summary-article-number))))
4691     ;; If this is a gathered thread, we have to go some re-gathering.
4692     (when (stringp (car thread))
4693       (let ((subject (car thread))
4694             roots thr)
4695         (setq thread (cdr thread))
4696         (while thread
4697           (unless (memq (setq thr (gnus-id-to-thread
4698                                    (gnus-root-id
4699                                     (mail-header-id (caar thread)))))
4700                         roots)
4701             (push thr roots))
4702           (setq thread (cdr thread)))
4703         ;; We now have all (unique) roots.
4704         (if (= (length roots) 1)
4705             ;; All the loose roots are now one solid root.
4706             (setq thread (car roots))
4707           (setq thread (cons subject (gnus-sort-threads roots))))))
4708     (let (threads)
4709       ;; We then insert this thread into the summary buffer.
4710       (when line
4711         (goto-char (point-min))
4712         (forward-line (1- line)))
4713       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4714         (if gnus-show-threads
4715             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4716           (gnus-summary-prepare-unthreaded thread))
4717         (setq data (nreverse gnus-newsgroup-data))
4718         (setq threads gnus-newsgroup-threads))
4719       ;; We splice the new data into the data structure.
4720       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4721       ;;!!! then we want to insert at the beginning of the buffer.
4722       ;;!!! That happens to be true with Gnus now, but that may
4723       ;;!!! change in the future.  Perhaps.
4724       (gnus-data-enter-list
4725        (if line nil current) data (- (point) old-pos))
4726       (setq gnus-newsgroup-threads
4727             (nconc threads gnus-newsgroup-threads))
4728       (gnus-data-compute-positions))))
4729
4730 (defun gnus-number-to-header (number)
4731   "Return the header for article NUMBER."
4732   (let ((headers gnus-newsgroup-headers))
4733     (while (and headers
4734                 (not (= number (mail-header-number (car headers)))))
4735       (pop headers))
4736     (when headers
4737       (car headers))))
4738
4739 (defun gnus-parent-headers (in-headers &optional generation)
4740   "Return the headers of the GENERATIONth parent of HEADERS."
4741   (unless generation
4742     (setq generation 1))
4743   (let ((parent t)
4744         (headers in-headers)
4745         references)
4746     (while (and parent
4747                 (not (zerop generation))
4748                 (setq references (mail-header-references headers)))
4749       (setq headers (if (and references
4750                              (setq parent (gnus-parent-id references)))
4751                         (car (gnus-id-to-thread parent))
4752                       nil))
4753       (decf generation))
4754     (and (not (eq headers in-headers))
4755          headers)))
4756
4757 (defun gnus-id-to-thread (id)
4758   "Return the (sub-)thread where ID appears."
4759   (gnus-gethash id gnus-newsgroup-dependencies))
4760
4761 (defun gnus-id-to-article (id)
4762   "Return the article number of ID."
4763   (let ((thread (gnus-id-to-thread id)))
4764     (when (and thread
4765                (car thread))
4766       (mail-header-number (car thread)))))
4767
4768 (defun gnus-id-to-header (id)
4769   "Return the article headers of ID."
4770   (car (gnus-id-to-thread id)))
4771
4772 (defun gnus-article-displayed-root-p (article)
4773   "Say whether ARTICLE is a root(ish) article."
4774   (let ((level (gnus-summary-thread-level article))
4775         (refs (mail-header-references  (gnus-summary-article-header article)))
4776         particle)
4777     (cond
4778      ((null level) nil)
4779      ((zerop level) t)
4780      ((null refs) t)
4781      ((null (gnus-parent-id refs)) t)
4782      ((and (= 1 level)
4783            (null (setq particle (gnus-id-to-article
4784                                  (gnus-parent-id refs))))
4785            (null (gnus-summary-thread-level particle)))))))
4786
4787 (defun gnus-root-id (id)
4788   "Return the id of the root of the thread where ID appears."
4789   (let (last-id prev)
4790     (while (and id (setq prev (car (gnus-id-to-thread id))))
4791       (setq last-id id
4792             id (gnus-parent-id (mail-header-references prev))))
4793     last-id))
4794
4795 (defun gnus-articles-in-thread (thread)
4796   "Return the list of articles in THREAD."
4797   (cons (mail-header-number (car thread))
4798         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4799
4800 (defun gnus-remove-thread (id &optional dont-remove)
4801   "Remove the thread that has ID in it."
4802   (let (headers thread last-id)
4803     ;; First go up in this thread until we find the root.
4804     (setq last-id (gnus-root-id id)
4805           headers (message-flatten-list (gnus-id-to-thread last-id)))
4806     ;; We have now found the real root of this thread.  It might have
4807     ;; been gathered into some loose thread, so we have to search
4808     ;; through the threads to find the thread we wanted.
4809     (let ((threads gnus-newsgroup-threads)
4810           sub)
4811       (while threads
4812         (setq sub (car threads))
4813         (if (stringp (car sub))
4814             ;; This is a gathered thread, so we look at the roots
4815             ;; below it to find whether this article is in this
4816             ;; gathered root.
4817             (progn
4818               (setq sub (cdr sub))
4819               (while sub
4820                 (when (member (caar sub) headers)
4821                   (setq thread (car threads)
4822                         threads nil
4823                         sub nil))
4824                 (setq sub (cdr sub))))
4825           ;; It's an ordinary thread, so we check it.
4826           (when (eq (car sub) (car headers))
4827             (setq thread sub
4828                   threads nil)))
4829         (setq threads (cdr threads)))
4830       ;; If this article is in no thread, then it's a root.
4831       (if thread
4832           (unless dont-remove
4833             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4834         (setq thread (gnus-id-to-thread last-id)))
4835       (when thread
4836         (prog1
4837             thread                      ; We return this thread.
4838           (unless dont-remove
4839             (if (stringp (car thread))
4840                 (progn
4841                   ;; If we use dummy roots, then we have to remove the
4842                   ;; dummy root as well.
4843                   (when (eq gnus-summary-make-false-root 'dummy)
4844                     ;; We go to the dummy root by going to
4845                     ;; the first sub-"thread", and then one line up.
4846                     (gnus-summary-goto-article
4847                      (mail-header-number (caadr thread)))
4848                     (forward-line -1)
4849                     (gnus-delete-line)
4850                     (gnus-data-compute-positions))
4851                   (setq thread (cdr thread))
4852                   (while thread
4853                     (gnus-remove-thread-1 (car thread))
4854                     (setq thread (cdr thread))))
4855               (gnus-remove-thread-1 thread))))))))
4856
4857 (defun gnus-remove-thread-1 (thread)
4858   "Remove the thread THREAD recursively."
4859   (let ((number (mail-header-number (pop thread)))
4860         d)
4861     (setq thread (reverse thread))
4862     (while thread
4863       (gnus-remove-thread-1 (pop thread)))
4864     (when (setq d (gnus-data-find number))
4865       (goto-char (gnus-data-pos d))
4866       (gnus-summary-show-thread)
4867       (gnus-data-remove
4868        number
4869        (- (point-at-bol)
4870           (prog1
4871               (1+ (point-at-eol))
4872             (gnus-delete-line)))))))
4873
4874 (defun gnus-sort-threads-recursive (threads func)
4875   ;; Responsible for sorting the root articles of threads.
4876   (let ((subthread-sort-func (if (eq gnus-subthread-sort-functions
4877                                      'gnus-thread-sort-functions)
4878                                  func
4879                                (gnus-make-sort-function
4880                                 gnus-subthread-sort-functions))))
4881     (sort (mapcar (lambda (thread)
4882                     (cons (car thread)
4883                           (and (cdr thread)
4884                                (gnus-sort-subthreads-recursive
4885                                 (cdr thread) subthread-sort-func))))
4886                   threads) func)))
4887
4888 (defun gnus-sort-subthreads-recursive (threads func)
4889   ;; Responsible for sorting subthreads.
4890   (sort (mapcar (lambda (thread)
4891                   (cons (car thread)
4892                         (and (cdr thread)
4893                              (gnus-sort-subthreads-recursive (cdr thread) func))))
4894                 threads) func))
4895
4896 (defun gnus-sort-threads-loop (threads func)
4897   (let* ((superthread (cons nil threads))
4898          (stack (list (cons superthread threads)))
4899          remaining-threads thread)
4900     (while stack
4901       (setq remaining-threads (cdr (car stack)))
4902       (if remaining-threads
4903           (progn (setq thread (car remaining-threads))
4904                  (setcdr (car stack) (cdr remaining-threads))
4905                  (if (cdr thread)
4906                      (push (cons thread (cdr thread)) stack)))
4907         (setq thread (caar stack))
4908         (setcdr thread (sort (cdr thread) func))
4909         (pop stack)))
4910     (cdr superthread)))
4911
4912 (defun gnus-sort-threads (threads)
4913   "Sort THREADS."
4914   (if (not gnus-thread-sort-functions)
4915       threads
4916     (gnus-message 8 "Sorting threads...")
4917     (prog1
4918         (condition-case nil
4919             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000))
4920                   (sort-func (gnus-make-sort-function gnus-thread-sort-functions)))
4921               (gnus-sort-threads-recursive threads sort-func))
4922           ;; Even after binding max-lisp-eval-depth, the recursive
4923           ;; sorter might fail for very long threads.  In that case,
4924           ;; try using a (less well-tested) non-recursive sorter.
4925           (error (gnus-message 9 "Sorting threads with loop...")
4926                  (gnus-sort-threads-loop
4927                   threads (gnus-make-sort-function
4928                            gnus-thread-sort-functions))))
4929       (gnus-message 8 "Sorting threads...done"))))
4930
4931 (defun gnus-sort-articles (articles)
4932   "Sort ARTICLES."
4933   (when gnus-article-sort-functions
4934     (gnus-message 7 "Sorting articles...")
4935     (prog1
4936         (setq gnus-newsgroup-headers
4937               (sort articles (gnus-make-sort-function
4938                               gnus-article-sort-functions)))
4939       (gnus-message 7 "Sorting articles...done"))))
4940
4941 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4942 (defmacro gnus-thread-header (thread)
4943   "Return header of first article in THREAD.
4944 Note that THREAD must never, ever be anything else than a variable -
4945 using some other form will lead to serious barfage."
4946   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4947   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4948   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4949         (vector thread) 2))
4950
4951 (defsubst gnus-article-sort-by-number (h1 h2)
4952   "Sort articles by article number."
4953   (< (mail-header-number h1)
4954      (mail-header-number h2)))
4955
4956 (defun gnus-thread-sort-by-number (h1 h2)
4957   "Sort threads by root article number."
4958   (gnus-article-sort-by-number
4959    (gnus-thread-header h1) (gnus-thread-header h2)))
4960
4961 (defsubst gnus-article-sort-by-random (h1 h2)
4962   "Sort articles randomly."
4963   (zerop (random 2)))
4964
4965 (defun gnus-thread-sort-by-random (h1 h2)
4966   "Sort threads randomly."
4967   (gnus-article-sort-by-random
4968    (gnus-thread-header h1) (gnus-thread-header h2)))
4969
4970 (defsubst gnus-article-sort-by-lines (h1 h2)
4971   "Sort articles by article Lines header."
4972   (< (mail-header-lines h1)
4973      (mail-header-lines h2)))
4974
4975 (defun gnus-thread-sort-by-lines (h1 h2)
4976   "Sort threads by root article Lines header."
4977   (gnus-article-sort-by-lines
4978    (gnus-thread-header h1) (gnus-thread-header h2)))
4979
4980 (defsubst gnus-article-sort-by-chars (h1 h2)
4981   "Sort articles by octet length."
4982   (< (mail-header-chars h1)
4983      (mail-header-chars h2)))
4984
4985 (defun gnus-thread-sort-by-chars (h1 h2)
4986   "Sort threads by root article octet length."
4987   (gnus-article-sort-by-chars
4988    (gnus-thread-header h1) (gnus-thread-header h2)))
4989
4990 (defsubst gnus-article-sort-by-author (h1 h2)
4991   "Sort articles by root author."
4992   (gnus-string<
4993    (let ((extract (funcall
4994                    gnus-extract-address-components
4995                    (mail-header-from h1))))
4996      (or (car extract) (cadr extract) ""))
4997    (let ((extract (funcall
4998                    gnus-extract-address-components
4999                    (mail-header-from h2))))
5000      (or (car extract) (cadr extract) ""))))
5001
5002 (defun gnus-thread-sort-by-author (h1 h2)
5003   "Sort threads by root author."
5004   (gnus-article-sort-by-author
5005    (gnus-thread-header h1)  (gnus-thread-header h2)))
5006
5007 (defsubst gnus-article-sort-by-recipient (h1 h2)
5008   "Sort articles by recipient."
5009   (gnus-string<
5010    (let ((extract (funcall
5011                    gnus-extract-address-components
5012                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
5013      (or (car extract) (cadr extract)))
5014    (let ((extract (funcall
5015                    gnus-extract-address-components
5016                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
5017      (or (car extract) (cadr extract)))))
5018
5019 (defun gnus-thread-sort-by-recipient (h1 h2)
5020   "Sort threads by root recipient."
5021   (gnus-article-sort-by-recipient
5022    (gnus-thread-header h1) (gnus-thread-header h2)))
5023
5024 (defsubst gnus-article-sort-by-subject (h1 h2)
5025   "Sort articles by root subject."
5026   (gnus-string<
5027    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5028    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5029
5030 (defun gnus-thread-sort-by-subject (h1 h2)
5031   "Sort threads by root subject."
5032   (gnus-article-sort-by-subject
5033    (gnus-thread-header h1) (gnus-thread-header h2)))
5034
5035 (defsubst gnus-article-sort-by-date (h1 h2)
5036   "Sort articles by root article date."
5037   (time-less-p
5038    (gnus-date-get-time (mail-header-date h1))
5039    (gnus-date-get-time (mail-header-date h2))))
5040
5041 (defun gnus-thread-sort-by-date (h1 h2)
5042   "Sort threads by root article date."
5043   (gnus-article-sort-by-date
5044    (gnus-thread-header h1) (gnus-thread-header h2)))
5045
5046 (defsubst gnus-article-sort-by-score (h1 h2)
5047   "Sort articles by root article score.
5048 Unscored articles will be counted as having a score of zero."
5049   (> (or (cdr (assq (mail-header-number h1)
5050                     gnus-newsgroup-scored))
5051          gnus-summary-default-score 0)
5052      (or (cdr (assq (mail-header-number h2)
5053                     gnus-newsgroup-scored))
5054          gnus-summary-default-score 0)))
5055
5056 (defun gnus-thread-sort-by-score (h1 h2)
5057   "Sort threads by root article score."
5058   (gnus-article-sort-by-score
5059    (gnus-thread-header h1) (gnus-thread-header h2)))
5060
5061 (defun gnus-thread-sort-by-total-score (h1 h2)
5062   "Sort threads by the sum of all scores in the thread.
5063 Unscored articles will be counted as having a score of zero."
5064   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5065
5066 (defun gnus-thread-total-score (thread)
5067   ;; This function find the total score of THREAD.
5068   (cond
5069    ((null thread)
5070     0)
5071    ((consp thread)
5072     (if (stringp (car thread))
5073         (apply gnus-thread-score-function 0
5074                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5075       (gnus-thread-total-score-1 thread)))
5076    (t
5077     (gnus-thread-total-score-1 (list thread)))))
5078
5079 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5080   "Sort articles by number."
5081   (gnus-article-sort-by-number h1 h2))
5082
5083 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5084   "Sort threads such that the thread with the most recently arrived article comes first."
5085   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5086
5087 (defun gnus-thread-highest-number (thread)
5088   "Return the highest article number in THREAD."
5089   (apply 'max (mapcar (lambda (header)
5090                         (mail-header-number header))
5091                       (message-flatten-list thread))))
5092
5093 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5094   "Sort articles by number."
5095   (gnus-article-sort-by-date h1 h2))
5096
5097 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5098   "Sort threads such that the thread with the most recently dated article comes first."
5099   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5100
5101 ; Since this is called not only to sort the top-level threads, but
5102 ; also in recursive sorts to order the articles within a thread, each
5103 ; article will be processed many times.  Thus it speeds things up
5104 ; quite a bit to use gnus-date-get-time, which caches the time value.
5105 (defun gnus-thread-latest-date (thread)
5106   "Return the highest article date in THREAD."
5107   (apply 'max
5108          (mapcar (lambda (header) (gnus-float-time
5109                                    (gnus-date-get-time
5110                                     (mail-header-date header))))
5111                  (message-flatten-list thread))))
5112
5113 (defun gnus-thread-total-score-1 (root)
5114   ;; This function find the total score of the thread below ROOT.
5115   (setq root (car root))
5116   (apply gnus-thread-score-function
5117          (or (append
5118               (mapcar 'gnus-thread-total-score
5119                       (cdr (gnus-id-to-thread (mail-header-id root))))
5120               (when (> (mail-header-number root) 0)
5121                 (list (or (cdr (assq (mail-header-number root)
5122                                      gnus-newsgroup-scored))
5123                           gnus-summary-default-score 0))))
5124              (list gnus-summary-default-score)
5125              '(0))))
5126
5127 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5128 (defvar gnus-tmp-prev-subject nil)
5129 (defvar gnus-tmp-false-parent nil)
5130 (defvar gnus-tmp-root-expunged nil)
5131 (defvar gnus-tmp-dummy-line nil)
5132
5133 (defun gnus-extra-header (type &optional header)
5134   "Return the extra header of TYPE."
5135   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5136       ""))
5137
5138 (defvar gnus-tmp-thread-tree-header-string "")
5139
5140 (defcustom gnus-sum-thread-tree-root "> "
5141   "With %B spec, used for the root of a thread.
5142 If nil, use subject instead."
5143   :version "22.1"
5144   :type '(radio (const :format "%v  " nil) string)
5145   :group 'gnus-thread)
5146
5147 (defcustom gnus-sum-thread-tree-false-root "> "
5148   "With %B spec, used for a false root of a thread.
5149 If nil, use subject instead."
5150   :version "22.1"
5151   :type '(radio (const :format "%v  " nil) string)
5152   :group 'gnus-thread)
5153
5154 (defcustom gnus-sum-thread-tree-single-indent ""
5155   "With %B spec, used for a thread with just one message.
5156 If nil, use subject instead."
5157   :version "22.1"
5158   :type '(radio (const :format "%v  " nil) string)
5159   :group 'gnus-thread)
5160
5161 (defcustom gnus-sum-thread-tree-vertical "| "
5162   "With %B spec, used for drawing a vertical line."
5163   :version "22.1"
5164   :type 'string
5165   :group 'gnus-thread)
5166
5167 (defcustom gnus-sum-thread-tree-indent "  "
5168   "With %B spec, used for indenting."
5169   :version "22.1"
5170   :type 'string
5171   :group 'gnus-thread)
5172
5173 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5174   "With %B spec, used for a leaf with brothers."
5175   :version "22.1"
5176   :type 'string
5177   :group 'gnus-thread)
5178
5179 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5180   "With %B spec, used for a leaf without brothers."
5181   :version "22.1"
5182   :type 'string
5183   :group 'gnus-thread)
5184
5185 (defcustom gnus-summary-display-while-building nil
5186   "If non-nil, show and update the summary buffer as it's being built.
5187 If the value is t, update the buffer after every line is inserted.  If
5188 the value is an integer (N), update the display every N lines."
5189   :version "22.1"
5190   :group 'gnus-thread
5191   :type '(choice (const :tag "off" nil)
5192                  number
5193                  (const :tag "frequently" t)))
5194
5195 (defun gnus-summary-prepare-threads (threads)
5196   "Prepare summary buffer from THREADS and indentation LEVEL.
5197 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5198 or a straight list of headers."
5199   (gnus-message 7 "Generating summary...")
5200
5201   (setq gnus-newsgroup-threads threads)
5202   (beginning-of-line)
5203
5204   (let ((gnus-tmp-level 0)
5205         (default-score (or gnus-summary-default-score 0))
5206         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5207         (building-line-count gnus-summary-display-while-building)
5208         (building-count (integerp gnus-summary-display-while-building))
5209         thread number subject stack state gnus-tmp-gathered beg-match
5210         new-roots gnus-tmp-new-adopts thread-end simp-subject
5211         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5212         gnus-tmp-replied gnus-tmp-subject-or-nil
5213         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5214         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5215         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5216         tree-stack)
5217
5218     (setq gnus-tmp-prev-subject nil
5219           gnus-tmp-thread-tree-header-string "")
5220
5221     (if (vectorp (car threads))
5222         ;; If this is a straight (sic) list of headers, then a
5223         ;; threaded summary display isn't required, so we just create
5224         ;; an unthreaded one.
5225         (gnus-summary-prepare-unthreaded threads)
5226
5227       ;; Do the threaded display.
5228
5229       (if gnus-summary-display-while-building
5230           (switch-to-buffer (buffer-name)))
5231       (while (or threads stack gnus-tmp-new-adopts new-roots)
5232
5233         (if (and (= gnus-tmp-level 0)
5234                  (or (not stack)
5235                      (= (caar stack) 0))
5236                  (not gnus-tmp-false-parent)
5237                  (or gnus-tmp-new-adopts new-roots))
5238             (if gnus-tmp-new-adopts
5239                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5240                       thread (list (car gnus-tmp-new-adopts))
5241                       gnus-tmp-header (caar thread)
5242                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5243               (when new-roots
5244                 (setq thread (list (car new-roots))
5245                       gnus-tmp-header (caar thread)
5246                       new-roots (cdr new-roots))))
5247
5248           (if threads
5249               ;; If there are some threads, we do them before the
5250               ;; threads on the stack.
5251               (setq thread threads
5252                     gnus-tmp-header (caar thread))
5253             ;; There were no current threads, so we pop something off
5254             ;; the stack.
5255             (setq state (car stack)
5256                   gnus-tmp-level (car state)
5257                   tree-stack (cadr state)
5258                   thread (caddr state)
5259                   stack (cdr stack)
5260                   gnus-tmp-header (caar thread))))
5261
5262         (setq gnus-tmp-false-parent nil)
5263         (setq gnus-tmp-root-expunged nil)
5264         (setq thread-end nil)
5265
5266         (if (stringp gnus-tmp-header)
5267             ;; The header is a dummy root.
5268             (cond
5269              ((eq gnus-summary-make-false-root 'adopt)
5270               ;; We let the first article adopt the rest.
5271               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5272                                                (cddar thread)))
5273               (setq gnus-tmp-gathered
5274                     (nconc (mapcar
5275                             (lambda (h) (mail-header-number (car h)))
5276                             (cddar thread))
5277                            gnus-tmp-gathered))
5278               (setq thread (cons (list (caar thread)
5279                                        (cadar thread))
5280                                  (cdr thread)))
5281               (setq gnus-tmp-level -1
5282                     gnus-tmp-false-parent t))
5283              ((eq gnus-summary-make-false-root 'empty)
5284               ;; We print adopted articles with empty subject fields.
5285               (setq gnus-tmp-gathered
5286                     (nconc (mapcar
5287                             (lambda (h) (mail-header-number (car h)))
5288                             (cddar thread))
5289                            gnus-tmp-gathered))
5290               (setq gnus-tmp-level -1))
5291              ((eq gnus-summary-make-false-root 'dummy)
5292               ;; We remember that we probably want to output a dummy
5293               ;; root.
5294               (setq gnus-tmp-dummy-line gnus-tmp-header)
5295               (setq gnus-tmp-prev-subject gnus-tmp-header))
5296              (t
5297               ;; We do not make a root for the gathered
5298               ;; sub-threads at all.
5299               (setq gnus-tmp-level -1)))
5300
5301           (setq number (mail-header-number gnus-tmp-header)
5302                 subject (mail-header-subject gnus-tmp-header)
5303                 simp-subject (gnus-simplify-subject-fully subject))
5304
5305           (cond
5306            ;; If the thread has changed subject, we might want to make
5307            ;; this subthread into a root.
5308            ((and (null gnus-thread-ignore-subject)
5309                  (not (zerop gnus-tmp-level))
5310                  gnus-tmp-prev-subject
5311                  (not (string= gnus-tmp-prev-subject simp-subject)))
5312             (setq new-roots (nconc new-roots (list (car thread)))
5313                   thread-end t
5314                   gnus-tmp-header nil))
5315            ;; If the article lies outside the current limit,
5316            ;; then we do not display it.
5317            ((not (memq number gnus-newsgroup-limit))
5318             (setq gnus-tmp-gathered
5319                   (nconc (mapcar
5320                           (lambda (h) (mail-header-number (car h)))
5321                           (cdar thread))
5322                          gnus-tmp-gathered))
5323             (setq gnus-tmp-new-adopts (if (cdar thread)
5324                                           (append gnus-tmp-new-adopts
5325                                                   (cdar thread))
5326                                         gnus-tmp-new-adopts)
5327                   thread-end t
5328                   gnus-tmp-header nil)
5329             (when (zerop gnus-tmp-level)
5330               (setq gnus-tmp-root-expunged t)))
5331            ;; Perhaps this article is to be marked as read?
5332            ((and gnus-summary-mark-below
5333                  (< (or (cdr (assq number gnus-newsgroup-scored))
5334                         default-score)
5335                     gnus-summary-mark-below)
5336                  ;; Don't touch sparse articles.
5337                  (not (gnus-summary-article-sparse-p number))
5338                  (not (gnus-summary-article-ancient-p number)))
5339             (setq gnus-newsgroup-unreads
5340                   (delq number gnus-newsgroup-unreads))
5341             (if gnus-newsgroup-auto-expire
5342                 (setq gnus-newsgroup-expirable
5343                       (gnus-add-to-sorted-list
5344                        gnus-newsgroup-expirable number))
5345               (push (cons number gnus-low-score-mark)
5346                     gnus-newsgroup-reads))))
5347
5348           (when gnus-tmp-header
5349             ;; We may have an old dummy line to output before this
5350             ;; article.
5351             (when (and gnus-tmp-dummy-line
5352                        (gnus-subject-equal
5353                         gnus-tmp-dummy-line
5354                         (mail-header-subject gnus-tmp-header)))
5355               (gnus-summary-insert-dummy-line
5356                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5357               (setq gnus-tmp-dummy-line nil))
5358
5359             ;; Compute the mark.
5360             (setq gnus-tmp-unread (gnus-article-mark number))
5361
5362             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5363                                   gnus-tmp-header gnus-tmp-level)
5364                   gnus-newsgroup-data)
5365
5366             ;; Actually insert the line.
5367             (setq
5368              gnus-tmp-subject-or-nil
5369              (cond
5370               ((and gnus-thread-ignore-subject
5371                     gnus-tmp-prev-subject
5372                     (not (string= gnus-tmp-prev-subject simp-subject)))
5373                subject)
5374               ((zerop gnus-tmp-level)
5375                (if (and (eq gnus-summary-make-false-root 'empty)
5376                         (memq number gnus-tmp-gathered)
5377                         gnus-tmp-prev-subject
5378                         (string= gnus-tmp-prev-subject simp-subject))
5379                    gnus-summary-same-subject
5380                  subject))
5381               (t gnus-summary-same-subject)))
5382             (if (and (eq gnus-summary-make-false-root 'adopt)
5383                      (= gnus-tmp-level 1)
5384                      (memq number gnus-tmp-gathered))
5385                 (setq gnus-tmp-opening-bracket ?\<
5386                       gnus-tmp-closing-bracket ?\>)
5387               (setq gnus-tmp-opening-bracket ?\[
5388                     gnus-tmp-closing-bracket ?\]))
5389             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5390                 (gnus-make-thread-indent-array
5391                  (max (* 2 (length gnus-thread-indent-array))
5392                       gnus-tmp-level)))
5393             (setq
5394              gnus-tmp-indentation
5395              (aref gnus-thread-indent-array gnus-tmp-level)
5396              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5397              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5398                                 gnus-summary-default-score 0)
5399              gnus-tmp-score-char
5400              (if (or (null gnus-summary-default-score)
5401                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5402                          gnus-summary-zcore-fuzz))
5403                  ?                      ;Whitespace
5404                (if (< gnus-tmp-score gnus-summary-default-score)
5405                    gnus-score-below-mark gnus-score-over-mark))
5406              gnus-tmp-replied
5407              (cond ((memq number gnus-newsgroup-processable)
5408                     gnus-process-mark)
5409                    ((memq number gnus-newsgroup-cached)
5410                     gnus-cached-mark)
5411                    ((memq number gnus-newsgroup-replied)
5412                     gnus-replied-mark)
5413                    ((memq number gnus-newsgroup-forwarded)
5414                     gnus-forwarded-mark)
5415                    ((memq number gnus-newsgroup-saved)
5416                     gnus-saved-mark)
5417                    ((memq number gnus-newsgroup-unseen)
5418                     gnus-unseen-mark)
5419                    (t gnus-no-mark))
5420              gnus-tmp-downloaded
5421              (cond ((memq number gnus-newsgroup-undownloaded)
5422                     gnus-undownloaded-mark)
5423                    (gnus-newsgroup-agentized
5424                     gnus-downloaded-mark)
5425                    (t
5426                     gnus-no-mark))
5427              gnus-tmp-from (mail-header-from gnus-tmp-header)
5428              gnus-tmp-name
5429              (cond
5430               ((string-match "<[^>]+> *$" gnus-tmp-from)
5431                (setq beg-match (match-beginning 0))
5432                (or (and (string-match "^\".+\"" gnus-tmp-from)
5433                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5434                    (substring gnus-tmp-from 0 beg-match)))
5435               ((string-match "(.+)" gnus-tmp-from)
5436                (substring gnus-tmp-from
5437                           (1+ (match-beginning 0)) (1- (match-end 0))))
5438               (t gnus-tmp-from))
5439
5440              ;; Do the %B string
5441              gnus-tmp-thread-tree-header-string
5442              (cond
5443               ((not gnus-show-threads) "")
5444               ((zerop gnus-tmp-level)
5445                (cond ((cdar thread)
5446                       (or gnus-sum-thread-tree-root subject))
5447                      (gnus-tmp-new-adopts
5448                       (or gnus-sum-thread-tree-false-root subject))
5449                      (t
5450                       (or gnus-sum-thread-tree-single-indent subject))))
5451               (t
5452                (concat (apply 'concat
5453                               (mapcar (lambda (item)
5454                                         (if (= item 1)
5455                                             gnus-sum-thread-tree-vertical
5456                                           gnus-sum-thread-tree-indent))
5457                                       (cdr (reverse tree-stack))))
5458                        (if (nth 1 thread)
5459                            gnus-sum-thread-tree-leaf-with-other
5460                          gnus-sum-thread-tree-single-leaf)))))
5461             (when (string= gnus-tmp-name "")
5462               (setq gnus-tmp-name gnus-tmp-from))
5463             (unless (numberp gnus-tmp-lines)
5464               (setq gnus-tmp-lines -1))
5465             (if (= gnus-tmp-lines -1)
5466                 (setq gnus-tmp-lines "?")
5467               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5468             (gnus-put-text-property
5469              (point)
5470              (progn (eval gnus-summary-line-format-spec) (point))
5471              'gnus-number number)
5472             (when gnus-visual-p
5473               (forward-line -1)
5474               (gnus-summary-highlight-line)
5475               (when gnus-summary-update-hook
5476                 (gnus-run-hooks 'gnus-summary-update-hook))
5477               (forward-line 1))
5478
5479             (setq gnus-tmp-prev-subject simp-subject)))
5480
5481         (when (nth 1 thread)
5482           (push (list (max 0 gnus-tmp-level)
5483                       (copy-sequence tree-stack)
5484                       (nthcdr 1 thread))
5485                 stack))
5486         (push (if (nth 1 thread) 1 0) tree-stack)
5487         (incf gnus-tmp-level)
5488         (setq threads (if thread-end nil (cdar thread)))
5489         (if gnus-summary-display-while-building
5490             (if building-count
5491                 (progn
5492                   ;; use a set frequency
5493                   (setq building-line-count (1- building-line-count))
5494                   (when (= building-line-count 0)
5495                     (sit-for 0)
5496                     (setq building-line-count
5497                           gnus-summary-display-while-building)))
5498               ;; always
5499               (sit-for 0)))
5500         (unless threads
5501           (setq gnus-tmp-level 0)))))
5502   (gnus-message 7 "Generating summary...done"))
5503
5504 (defun gnus-summary-prepare-unthreaded (headers)
5505   "Generate an unthreaded summary buffer based on HEADERS."
5506   (let (header number mark)
5507
5508     (beginning-of-line)
5509
5510     (while headers
5511       ;; We may have to root out some bad articles...
5512       (when (memq (setq number (mail-header-number
5513                                 (setq header (pop headers))))
5514                   gnus-newsgroup-limit)
5515         ;; Mark article as read when it has a low score.
5516         (when (and gnus-summary-mark-below
5517                    (< (or (cdr (assq number gnus-newsgroup-scored))
5518                           gnus-summary-default-score 0)
5519                       gnus-summary-mark-below)
5520                    (not (gnus-summary-article-ancient-p number)))
5521           (setq gnus-newsgroup-unreads
5522                 (delq number gnus-newsgroup-unreads))
5523           (if gnus-newsgroup-auto-expire
5524               (push number gnus-newsgroup-expirable)
5525             (push (cons number gnus-low-score-mark)
5526                   gnus-newsgroup-reads)))
5527
5528         (setq mark (gnus-article-mark number))
5529         (push (gnus-data-make number mark (1+ (point)) header 0)
5530               gnus-newsgroup-data)
5531         (gnus-summary-insert-line
5532          header 0 number
5533          (memq number gnus-newsgroup-undownloaded)
5534          mark (memq number gnus-newsgroup-replied)
5535          (memq number gnus-newsgroup-expirable)
5536          (mail-header-subject header) nil
5537          (cdr (assq number gnus-newsgroup-scored))
5538          (memq number gnus-newsgroup-processable))))))
5539
5540 (declare-function gnus-parameter-list-identifier "gnus-art" (name) t)
5541
5542 (defun gnus-group-get-list-identifiers (group)
5543   "Get list identifier regexp for GROUP."
5544   (or (gnus-parameter-list-identifier group)
5545       (if (consp gnus-list-identifiers)
5546           (mapconcat 'identity gnus-list-identifiers " *\\|")
5547         gnus-list-identifiers)))
5548
5549 (defun gnus-summary-remove-list-identifiers ()
5550   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5551   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5552         changed subject)
5553     (when regexp
5554       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5555       (dolist (header gnus-newsgroup-headers)
5556         (setq subject (mail-header-subject header)
5557               changed nil)
5558         (while (string-match regexp subject)
5559           (setq subject
5560                 (concat (substring subject 0 (match-beginning 1))
5561                         (substring subject (match-end 0)))
5562                 changed t))
5563         (when changed
5564           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5565             (setq subject
5566                   (concat (substring subject 0 (match-beginning 1))
5567                           (substring subject (match-end 1)))))
5568           (mail-header-set-subject header subject))))))
5569
5570 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5571   "Fetch headers of ARTICLES."
5572   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5573     (gnus-message 7 "Fetching headers for %s..." name)
5574     (prog1
5575         (if (eq 'nov
5576                 (setq gnus-headers-retrieved-by
5577                       (gnus-retrieve-headers
5578                        articles gnus-newsgroup-name
5579                        (or limit
5580                            ;; We might want to fetch old headers, but
5581                            ;; not if there is only 1 article.
5582                            (and (or (and
5583                                      (not (eq gnus-fetch-old-headers 'some))
5584                                      (not (numberp gnus-fetch-old-headers)))
5585                                     (> (length articles) 1))
5586                                 gnus-fetch-old-headers)))))
5587             (gnus-get-newsgroup-headers-xover
5588              articles force-new dependencies gnus-newsgroup-name t)
5589           (gnus-get-newsgroup-headers dependencies force-new))
5590       (gnus-message 7 "Fetching headers for %s...done" name))))
5591
5592 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5593   "Select newsgroup GROUP.
5594 If READ-ALL is non-nil, all articles in the group are selected.
5595 If SELECT-ARTICLES, only select those articles from GROUP."
5596   (let* ((entry (gnus-group-entry group))
5597          ;;!!! Dirty hack; should be removed.
5598          (gnus-summary-ignore-duplicates
5599           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5600               t
5601             gnus-summary-ignore-duplicates))
5602          (info (nth 2 entry))
5603          charset articles fetched-articles cached)
5604
5605     (unless (gnus-check-server
5606              (set (make-local-variable 'gnus-current-select-method)
5607                   (gnus-find-method-for-group group)))
5608       (error "Couldn't open server"))
5609     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5610
5611     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5612         (gnus-activate-group group)     ; Or we can activate it...
5613         (progn                          ; Or we bug out.
5614           (when (equal major-mode 'gnus-summary-mode)
5615             (gnus-kill-buffer (current-buffer)))
5616           (error
5617            "Couldn't activate group %s: %s"
5618            (mm-decode-coding-string group charset)
5619            (mm-decode-coding-string (gnus-status-message group) charset))))
5620
5621     (unless (gnus-request-group group t)
5622       (when (equal major-mode 'gnus-summary-mode)
5623         (gnus-kill-buffer (current-buffer)))
5624       (error "Couldn't request group %s: %s"
5625              (mm-decode-coding-string group charset)
5626              (mm-decode-coding-string (gnus-status-message group) charset)))
5627
5628     (when (and gnus-agent
5629                (gnus-active group))
5630       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5631
5632       (setq gnus-summary-use-undownloaded-faces
5633             (gnus-agent-find-parameter
5634              group
5635              'agent-enable-undownloaded-faces)))
5636
5637     (setq gnus-newsgroup-name group
5638           gnus-newsgroup-unselected nil
5639           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5640
5641     (let ((display (gnus-group-find-parameter group 'display)))
5642       (setq gnus-newsgroup-display
5643             (cond
5644              ((not (zerop (or (car-safe read-all) 0)))
5645               ;; The user entered the group with C-u SPC/RET, let's show
5646               ;; all articles.
5647               'gnus-not-ignore)
5648              ((eq display 'all)
5649               'gnus-not-ignore)
5650              ((arrayp display)
5651               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5652              ((numberp display)
5653               ;; The following is probably the "correct" solution, but
5654               ;; it makes Gnus fetch all headers and then limit the
5655               ;; articles (which is slow), so instead we hack the
5656               ;; select-articles parameter instead. -- Simon Josefsson
5657               ;; <jas@kth.se>
5658               ;;
5659               ;; (gnus-byte-compile
5660               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5661               ;;                         display)))))
5662               (setq select-articles
5663                     (gnus-uncompress-range
5664                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5665                              (if (> tmp 0)
5666                                  tmp
5667                                1))
5668                            (cdr (gnus-active group)))))
5669               nil)
5670              (t
5671               nil))))
5672
5673     (gnus-summary-setup-default-charset)
5674
5675     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5676     (when (gnus-virtual-group-p group)
5677       (setq cached gnus-newsgroup-cached))
5678
5679     (setq gnus-newsgroup-unreads
5680           (gnus-sorted-ndifference
5681            (gnus-sorted-ndifference gnus-newsgroup-unreads
5682                                     gnus-newsgroup-marked)
5683            gnus-newsgroup-dormant))
5684
5685     (setq gnus-newsgroup-processable nil)
5686
5687     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5688
5689     ;; Adjust and set lists of article marks.
5690     (when info
5691       (gnus-adjust-marked-articles info))
5692     (if (setq articles select-articles)
5693         (setq gnus-newsgroup-unselected
5694               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5695       (setq articles (gnus-articles-to-read group read-all)))
5696
5697     (cond
5698      ((null articles)
5699       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5700       'quit)
5701      ((eq articles 0) nil)
5702      (t
5703       ;; Init the dependencies hash table.
5704       (setq gnus-newsgroup-dependencies
5705             (gnus-make-hashtable (length articles)))
5706       (if (gnus-buffer-live-p gnus-group-buffer)
5707           (gnus-set-global-variables)
5708         (set-default 'gnus-newsgroup-name gnus-newsgroup-name))
5709       ;; Retrieve the headers and read them in.
5710
5711       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5712
5713       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5714       (when cached
5715         (setq gnus-newsgroup-cached cached))
5716
5717       ;; Suppress duplicates?
5718       (when gnus-suppress-duplicates
5719         (gnus-dup-suppress-articles))
5720
5721       ;; Set the initial limit.
5722       (setq gnus-newsgroup-limit (copy-sequence articles))
5723       ;; Remove canceled articles from the list of unread articles.
5724       (setq fetched-articles
5725             (mapcar (lambda (headers) (mail-header-number headers))
5726                     gnus-newsgroup-headers))
5727       (setq gnus-newsgroup-articles fetched-articles)
5728       (setq gnus-newsgroup-unreads
5729             (gnus-sorted-nintersection
5730              gnus-newsgroup-unreads fetched-articles))
5731       (gnus-compute-unseen-list)
5732
5733       ;; Removed marked articles that do not exist.
5734       (gnus-update-missing-marks
5735        (gnus-sorted-difference articles fetched-articles))
5736       ;; We might want to build some more threads first.
5737       (when (and gnus-fetch-old-headers
5738                  (eq gnus-headers-retrieved-by 'nov))
5739         (if (eq gnus-fetch-old-headers 'invisible)
5740             (gnus-build-all-threads)
5741           (gnus-build-old-threads)))
5742       ;; Let the Gnus agent mark articles as read.
5743       (when gnus-agent
5744         (gnus-agent-get-undownloaded-list))
5745       ;; Remove list identifiers from subject
5746       (gnus-summary-remove-list-identifiers)
5747       ;; Check whether auto-expire is to be done in this group.
5748       (setq gnus-newsgroup-auto-expire
5749             (and (gnus-group-auto-expirable-p group)
5750                  (not (gnus-group-read-only-p group))))
5751       ;; Set up the article buffer now, if necessary.
5752       (unless (and gnus-single-article-buffer
5753                    (equal gnus-article-buffer "*Article*"))
5754         (gnus-article-setup-buffer))
5755       ;; First and last article in this newsgroup.
5756       (when gnus-newsgroup-headers
5757         (setq gnus-newsgroup-begin
5758               (mail-header-number (car gnus-newsgroup-headers))
5759               gnus-newsgroup-end
5760               (mail-header-number
5761                (gnus-last-element gnus-newsgroup-headers))))
5762       ;; GROUP is successfully selected.
5763       (or gnus-newsgroup-headers t)))))
5764
5765 (defun gnus-compute-unseen-list ()
5766   ;; The `seen' marks are treated specially.
5767   (if (not gnus-newsgroup-seen)
5768       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5769     (setq gnus-newsgroup-unseen
5770           (gnus-inverse-list-range-intersection
5771            gnus-newsgroup-articles gnus-newsgroup-seen))))
5772
5773 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5774
5775 (defun gnus-summary-display-make-predicate (display)
5776   (require 'gnus-agent)
5777   (when (= (length display) 1)
5778     (setq display (car display)))
5779   (unless gnus-summary-display-cache
5780     (dolist (elem (append '((unread . unread)
5781                             (read . read)
5782                             (unseen . unseen))
5783                           gnus-article-mark-lists))
5784       (push (cons (cdr elem)
5785                   (gnus-byte-compile    ;Why bother?
5786                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5787             gnus-summary-display-cache)))
5788   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5789         (gnus-category-predicate-cache gnus-summary-display-cache))
5790     (gnus-get-predicate display)))
5791
5792 ;; Uses the dynamically bound `gnus-number' variable.
5793 (defvar gnus-number)
5794 (defun gnus-article-marked-p (type &optional article)
5795   (let ((article (or article gnus-number)))
5796     (cond
5797      ((eq type 'tick)
5798       (memq article gnus-newsgroup-marked))
5799      ((eq type 'spam)
5800       (memq article gnus-newsgroup-spam-marked))
5801      ((eq type 'unsend)
5802       (memq article gnus-newsgroup-unsendable))
5803      ((eq type 'undownload)
5804       (memq article gnus-newsgroup-undownloaded))
5805      ((eq type 'download)
5806       (memq article gnus-newsgroup-downloadable))
5807      ((eq type 'unread)
5808       (memq article gnus-newsgroup-unreads))
5809      ((eq type 'read)
5810       (memq article gnus-newsgroup-reads))
5811      ((eq type 'dormant)
5812       (memq article gnus-newsgroup-dormant) )
5813      ((eq type 'expire)
5814       (memq article gnus-newsgroup-expirable))
5815      ((eq type 'reply)
5816       (memq article gnus-newsgroup-replied))
5817      ((eq type 'killed)
5818       (memq article gnus-newsgroup-killed))
5819      ((eq type 'bookmark)
5820       (assq article gnus-newsgroup-bookmarks))
5821      ((eq type 'score)
5822       (assq article gnus-newsgroup-scored))
5823      ((eq type 'save)
5824       (memq article gnus-newsgroup-saved))
5825      ((eq type 'cache)
5826       (memq article gnus-newsgroup-cached))
5827      ((eq type 'forward)
5828       (memq article gnus-newsgroup-forwarded))
5829      ((eq type 'seen)
5830       (not (memq article gnus-newsgroup-unseen)))
5831      (t t))))
5832
5833 (defun gnus-articles-to-read (group &optional read-all)
5834   "Find out what articles the user wants to read."
5835   (let* ((only-read-p t)
5836          (articles
5837           (gnus-list-range-difference
5838           ;; Select all articles if `read-all' is non-nil, or if there
5839           ;; are no unread articles.
5840           (if (or read-all
5841                   (and (zerop (length gnus-newsgroup-marked))
5842                        (zerop (length gnus-newsgroup-unreads)))
5843                   ;; Fetch all if the predicate is non-nil.
5844                   gnus-newsgroup-display)
5845               ;; We want to select the headers for all the articles in
5846               ;; the group, so we select either all the active
5847               ;; articles in the group, or (if that's nil), the
5848               ;; articles in the cache.
5849               (or
5850                (if gnus-newsgroup-maximum-articles
5851                    (let ((active (gnus-active group)))
5852                      (gnus-uncompress-range
5853                       (cons (max (car active)
5854                                  (- (cdr active)
5855                                     gnus-newsgroup-maximum-articles
5856                                     -1))
5857                             (cdr active))))
5858                  (gnus-uncompress-range (gnus-active group)))
5859                (gnus-cache-articles-in-group group))
5860             ;; Select only the "normal" subset of articles.
5861             (setq only-read-p nil)
5862             (gnus-sorted-nunion
5863              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5864              gnus-newsgroup-unreads))
5865           (cdr (assq 'unexist (gnus-info-marks (gnus-get-info group))))))
5866          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5867          (scored (length scored-list))
5868          (number (length articles))
5869          (marked (+ (length gnus-newsgroup-marked)
5870                     (length gnus-newsgroup-dormant)))
5871          (select
5872           (cond
5873            ((numberp read-all)
5874             read-all)
5875            ((numberp gnus-newsgroup-display)
5876             gnus-newsgroup-display)
5877            (t
5878             (condition-case ()
5879                 (cond
5880                  ((and (or (<= scored marked) (= scored number))
5881                        (numberp gnus-large-newsgroup)
5882                        (> number gnus-large-newsgroup))
5883                   (let* ((cursor-in-echo-area nil)
5884                          (initial (gnus-parameter-large-newsgroup-initial
5885                                    gnus-newsgroup-name))
5886                          (default (if only-read-p
5887                                       (or initial gnus-large-newsgroup)
5888                                     number))
5889                          (input
5890                           (read-string
5891                            (if only-read-p
5892                                (format
5893                                 "How many articles from %s (available %d, default %d): "
5894                                 (gnus-group-decoded-name
5895                                  (gnus-group-real-name gnus-newsgroup-name))
5896                                 number default)
5897                              (format
5898                               "How many articles from %s (%d default): "
5899                               (gnus-group-decoded-name
5900                                (gnus-group-real-name gnus-newsgroup-name))
5901                               default))
5902                            nil
5903                            nil
5904                            (number-to-string default))))
5905                     (if (string-match "^[ \t]*$" input) number input)))
5906                  ((and (> scored marked) (< scored number)
5907                        (> (- scored number) 20))
5908                   (let ((input
5909                          (read-string
5910                           (format "%s %s (%d scored, %d total): "
5911                                   "How many articles from"
5912                                   (gnus-group-decoded-name
5913                                    (gnus-group-real-name gnus-newsgroup-name))
5914                                   scored number))))
5915                     (if (string-match "^[ \t]*$" input)
5916                         number input)))
5917                  (t number))
5918               (quit
5919                (message "Quit getting the articles to read")
5920                nil))))))
5921     (setq select (if (stringp select) (string-to-number select) select))
5922     (if (or (null select) (zerop select))
5923         select
5924       (if (and (not (zerop scored)) (<= (abs select) scored))
5925           (progn
5926             (setq articles (sort scored-list '<))
5927             (setq number (length articles)))
5928         (setq articles (copy-sequence articles)))
5929
5930       (when (< (abs select) number)
5931         (if (< select 0)
5932             ;; Select the N oldest articles.
5933             (setcdr (nthcdr (1- (abs select)) articles) nil)
5934           ;; Select the N most recent articles.
5935           (setq articles (nthcdr (- number select) articles))))
5936       (setq gnus-newsgroup-unselected
5937             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5938       (when gnus-alter-articles-to-read-function
5939         (setq articles
5940               (sort
5941                (funcall gnus-alter-articles-to-read-function
5942                         gnus-newsgroup-name articles)
5943                '<)))
5944       articles)))
5945
5946 (defun gnus-killed-articles (killed articles)
5947   (let (out)
5948     (while articles
5949       (when (inline (gnus-member-of-range (car articles) killed))
5950         (push (car articles) out))
5951       (setq articles (cdr articles)))
5952     out))
5953
5954 (defun gnus-article-mark-to-type (mark)
5955   "Return the type of MARK."
5956   (or (cadr (assq mark gnus-article-special-mark-lists))
5957       'list))
5958
5959 (defun gnus-article-unpropagatable-p (mark)
5960   "Return whether MARK should be propagated to back end."
5961   (memq mark gnus-article-unpropagated-mark-lists))
5962
5963 (defun gnus-adjust-marked-articles (info)
5964   "Set all article lists and remove all marks that are no longer valid."
5965   (let* ((marked-lists (gnus-info-marks info))
5966          (active (gnus-active (gnus-info-group info)))
5967          (min (car active))
5968          (max (cdr active))
5969          (types gnus-article-mark-lists)
5970          marks var articles article mark mark-type
5971          bgn end)
5972     ;; Hack to avoid adjusting marks for imap.
5973     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5974               'nnimap)
5975       (setq min 1))
5976
5977     (dolist (marks marked-lists)
5978       (setq mark (car marks)
5979             mark-type (gnus-article-mark-to-type mark)
5980             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5981       ;; We set the variable according to the type of the marks list,
5982       ;; and then adjust the marks to a subset of the active articles.
5983       (cond
5984        ;; Adjust "simple" lists - compressed yet unsorted
5985        ((eq mark-type 'list)
5986         ;; Simultaneously uncompress and clip to active range
5987         ;; See gnus-uncompress-range for a description of possible marks
5988         (let (l lh)
5989           (if (not (cadr marks))
5990               (set var nil)
5991             (setq articles (if (numberp (cddr marks))
5992                                (list (cdr marks))
5993                              (cdr marks))
5994                   lh (cons nil nil)
5995                   l lh)
5996
5997             (while (setq article (pop articles))
5998               (cond ((consp article)
5999                      (setq bgn (max (car article) min)
6000                            end (min (cdr article) max))
6001                      (while (<= bgn end)
6002                        (setq l (setcdr l (cons bgn nil))
6003                              bgn (1+ bgn))))
6004                     ((and (<= min article)
6005                           (>= max article))
6006                      (setq l (setcdr l (cons article nil))))))
6007             (set var (cdr lh)))))
6008        ;; Adjust assocs.
6009        ((eq mark-type 'tuple)
6010         (set var (setq articles (cdr marks)))
6011         (when (not (listp (cdr (symbol-value var))))
6012           (set var (list (symbol-value var))))
6013         (when (not (listp (cdr articles)))
6014           (setq articles (list articles)))
6015         (while articles
6016           (when (or (not (consp (setq article (pop articles))))
6017                     (< (car article) min)
6018                     (> (car article) max))
6019             (set var (delq article (symbol-value var))))))
6020        ;; Adjust ranges (sloppily).
6021        ((eq mark-type 'range)
6022         (cond
6023          ((eq mark 'seen)
6024           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6025           ;; It should be (seen (NUM1 . NUM2)).
6026           (when (numberp (cddr marks))
6027             (setcdr marks (list (cdr marks))))
6028           (setq articles (cdr marks))
6029           (while (and articles
6030                       (or (and (consp (car articles))
6031                                (> min (cdar articles)))
6032                           (and (numberp (car articles))
6033                                (> min (car articles)))))
6034             (pop articles))
6035           (set var articles))
6036          ((eq mark 'unexist)
6037           (set var (cdr marks)))))))))
6038
6039 (defun gnus-update-missing-marks (missing)
6040   "Go through the list of MISSING articles and remove them from the mark lists."
6041   (when missing
6042     (let (var m)
6043       ;; Go through all types.
6044       (dolist (elem gnus-article-mark-lists)
6045         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6046           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6047           (when (symbol-value var)
6048             ;; This list has articles.  So we delete all missing
6049             ;; articles from it.
6050             (setq m missing)
6051             (while m
6052               (set var (delq (pop m) (symbol-value var))))))))))
6053
6054 (defun gnus-update-marks ()
6055   "Enter the various lists of marked articles into the newsgroup info list."
6056   (let ((types gnus-article-mark-lists)
6057         (info (gnus-get-info gnus-newsgroup-name))
6058         type list newmarked symbol delta-marks)
6059     (when info
6060       ;; Add all marks lists to the list of marks lists.
6061       (while (setq type (pop types))
6062         (setq list (symbol-value
6063                     (setq symbol
6064                           (intern (format "gnus-newsgroup-%s" (car type))))))
6065
6066         (when list
6067           ;; Get rid of the entries of the articles that have the
6068           ;; default score.
6069           (when (and (eq (cdr type) 'score)
6070                      gnus-save-score
6071                      list)
6072             (let* ((arts list)
6073                    (prev (cons nil list))
6074                    (all prev))
6075               (while arts
6076                 (if (or (not (consp (car arts)))
6077                         (= (cdar arts) gnus-summary-default-score))
6078                     (setcdr prev (cdr arts))
6079                   (setq prev arts))
6080                 (setq arts (cdr arts)))
6081               (setq list (cdr all)))))
6082
6083         (when (eq (cdr type) 'seen)
6084           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6085
6086         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6087           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6088
6089         (when (and (gnus-check-backend-function
6090                     'request-set-mark gnus-newsgroup-name)
6091                    (not (gnus-article-unpropagatable-p (cdr type))))
6092           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6093                  ;; Don't do anything about marks for articles we
6094                  ;; didn't actually get any headers for.
6095                  (del
6096                   (gnus-list-range-intersection
6097                    gnus-newsgroup-articles
6098                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6099                  (add
6100                   (gnus-list-range-intersection
6101                    gnus-newsgroup-articles
6102                    (gnus-remove-from-range
6103                     (gnus-copy-sequence list) old))))
6104             (when add
6105               (push (list add 'add (list (cdr type))) delta-marks))
6106             (when del
6107               ;; Don't delete marks from outside the active range.
6108               ;; This shouldn't happen, but is a sanity check.
6109               (setq del (gnus-sorted-range-intersection
6110                          (gnus-active gnus-newsgroup-name) del))
6111               (push (list del 'del (list (cdr type))) delta-marks))))
6112
6113         (when (or list
6114                   (eq (cdr type) 'unexist))
6115           (push (cons (cdr type) list) newmarked)))
6116
6117       (when delta-marks
6118         (unless (gnus-check-group gnus-newsgroup-name)
6119           (error "Can't open server for %s" gnus-newsgroup-name))
6120         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6121
6122       ;; Enter these new marks into the info of the group.
6123       (if (nthcdr 3 info)
6124           (setcar (nthcdr 3 info) newmarked)
6125         ;; Add the marks lists to the end of the info.
6126         (when newmarked
6127           (setcdr (nthcdr 2 info) (list newmarked))))
6128
6129       ;; Cut off the end of the info if there's nothing else there.
6130       (let ((i 5))
6131         (while (and (> i 2)
6132                     (not (nth i info)))
6133           (when (nthcdr (decf i) info)
6134             (setcdr (nthcdr i info) nil)))))))
6135
6136 (defun gnus-set-mode-line (where)
6137   "Set the mode line of the article or summary buffers.
6138 If WHERE is `summary', the summary mode line format will be used."
6139   ;; Is this mode line one we keep updated?
6140   (when (and (memq where gnus-updated-mode-lines)
6141              (symbol-value
6142               (intern (format "gnus-%s-mode-line-format-spec" where))))
6143     (let (mode-string)
6144       ;; We evaluate this in the summary buffer since these
6145       ;; variables are buffer-local to that buffer.
6146       (with-current-buffer gnus-summary-buffer
6147         ;; We bind all these variables that are used in the `eval' form
6148         ;; below.
6149         (let* ((mformat (symbol-value
6150                          (intern
6151                           (format "gnus-%s-mode-line-format-spec" where))))
6152                (gnus-tmp-group-name (gnus-mode-string-quote
6153                                      (gnus-group-decoded-name
6154                                       gnus-newsgroup-name)))
6155                (gnus-tmp-article-number (or gnus-current-article 0))
6156                (gnus-tmp-unread gnus-newsgroup-unreads)
6157                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6158                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6159                (gnus-tmp-unread-and-unselected
6160                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6161                             (zerop gnus-tmp-unselected))
6162                        "")
6163                       ((zerop gnus-tmp-unselected)
6164                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6165                       (t (format "{%d(+%d) more}"
6166                                  gnus-tmp-unread-and-unticked
6167                                  gnus-tmp-unselected))))
6168                (gnus-tmp-subject
6169                 (if (and gnus-current-headers
6170                          (vectorp gnus-current-headers))
6171                     (gnus-mode-string-quote
6172                      (mail-header-subject gnus-current-headers))
6173                   ""))
6174                bufname-length max-len
6175                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6176           (setq mode-string (eval mformat))
6177           (setq bufname-length (if (string-match "%b" mode-string)
6178                                    (- (length
6179                                        (buffer-name
6180                                         (if (eq where 'summary)
6181                                             nil
6182                                           (get-buffer gnus-article-buffer))))
6183                                       2)
6184                                  0))
6185           (setq max-len (max 4 (if gnus-mode-non-string-length
6186                                    (- (window-width)
6187                                       gnus-mode-non-string-length
6188                                       bufname-length)
6189                                  (length mode-string))))
6190           ;; We might have to chop a bit of the string off...
6191           (when (> (length mode-string) max-len)
6192             (setq mode-string
6193                   (concat (truncate-string-to-width mode-string (- max-len 3))
6194                           "...")))))
6195       ;; Update the mode line.
6196       (setq mode-line-buffer-identification
6197             (gnus-mode-line-buffer-identification (list mode-string)))
6198       (set-buffer-modified-p t))))
6199
6200 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6201   "Go through the HEADERS list and add all Xrefs to a hash table.
6202 The resulting hash table is returned, or nil if no Xrefs were found."
6203   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6204          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6205          (xref-hashtb (gnus-make-hashtable))
6206          start group entry number xrefs header)
6207     (while headers
6208       (setq header (pop headers))
6209       (when (and (setq xrefs (mail-header-xref header))
6210                  (not (memq (setq number (mail-header-number header))
6211                             unreads)))
6212         (setq start 0)
6213         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6214           (setq start (match-end 0))
6215           (setq group (if prefix
6216                           (concat prefix (substring xrefs (match-beginning 1)
6217                                                     (match-end 1)))
6218                         (substring xrefs (match-beginning 1) (match-end 1))))
6219           (setq number
6220                 (string-to-number (substring xrefs (match-beginning 2)
6221                                           (match-end 2))))
6222           (if (setq entry (gnus-gethash group xref-hashtb))
6223               (setcdr entry (cons number (cdr entry)))
6224             (gnus-sethash group (cons number nil) xref-hashtb)))))
6225     (and start xref-hashtb)))
6226
6227 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6228   "Look through all the headers and mark the Xrefs as read."
6229   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6230         name info xref-hashtb idlist method nth4)
6231     (with-current-buffer gnus-group-buffer
6232       (when (setq xref-hashtb
6233                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6234         (mapatoms
6235          (lambda (group)
6236            (unless (string= from-newsgroup (setq name (symbol-name group)))
6237              (setq idlist (symbol-value group))
6238              ;; Dead groups are not updated.
6239              (and (prog1
6240                       (setq info (gnus-get-info name))
6241                     (when (stringp (setq nth4 (gnus-info-method info)))
6242                       (setq nth4 (gnus-server-to-method nth4))))
6243                   ;; Only do the xrefs if the group has the same
6244                   ;; select method as the group we have just read.
6245                   (or (gnus-methods-equal-p
6246                        nth4 (gnus-find-method-for-group from-newsgroup))
6247                       virtual
6248                       (equal nth4 (setq method (gnus-find-method-for-group
6249                                                 from-newsgroup)))
6250                       (and (equal (car nth4) (car method))
6251                            (equal (nth 1 nth4) (nth 1 method))))
6252                   gnus-use-cross-reference
6253                   (or (not (eq gnus-use-cross-reference t))
6254                       virtual
6255                       ;; Only do cross-references on subscribed
6256                       ;; groups, if that is what is wanted.
6257                       (<= (gnus-info-level info) gnus-level-subscribed))
6258                   (gnus-group-make-articles-read name idlist))))
6259          xref-hashtb)))))
6260
6261 (defun gnus-compute-read-articles (group articles)
6262   (let* ((entry (gnus-group-entry group))
6263          (info (nth 2 entry))
6264          (active (gnus-active group))
6265          ninfo)
6266     (when entry
6267       ;; First peel off all invalid article numbers.
6268       (when active
6269         (let ((ids articles)
6270               id first)
6271           (while (setq id (pop ids))
6272             (when (and first (> id (cdr active)))
6273               ;; We'll end up in this situation in one particular
6274               ;; obscure situation.  If you re-scan a group and get
6275               ;; a new article that is cross-posted to a different
6276               ;; group that has not been re-scanned, you might get
6277               ;; crossposted article that has a higher number than
6278               ;; Gnus believes possible.  So we re-activate this
6279               ;; group as well.  This might mean doing the
6280               ;; crossposting thingy will *increase* the number
6281               ;; of articles in some groups.  Tsk, tsk.
6282               (setq active (or (gnus-activate-group group) active)))
6283             (when (or (> id (cdr active))
6284                       (< id (car active)))
6285               (setq articles (delq id articles))))))
6286       ;; If the read list is nil, we init it.
6287       (if (and active
6288                (null (gnus-info-read info))
6289                (> (car active) 1))
6290           (setq ninfo (cons 1 (1- (car active))))
6291         (setq ninfo (gnus-info-read info)))
6292       ;; Then we add the read articles to the range.
6293       (gnus-add-to-range
6294        ninfo (setq articles (sort articles '<))))))
6295
6296 (defun gnus-group-make-articles-read (group articles)
6297   "Update the info of GROUP to say that ARTICLES are read."
6298   (let* ((num 0)
6299          (entry (gnus-group-entry group))
6300          (info (nth 2 entry))
6301          (active (gnus-active group))
6302          (set-marks
6303           (gnus-method-option-p
6304            (gnus-find-method-for-group group)
6305            'server-marks))
6306          range)
6307     (if (not entry)
6308         ;; Group that Gnus doesn't know exists, but still allow the
6309         ;; backend to set marks.
6310         (when set-marks
6311           (gnus-request-set-mark
6312            group (list (list (gnus-compress-sequence (sort articles #'<))
6313                              'add '(read)))))
6314       ;; Normal, subscribed groups.
6315       (setq range (gnus-compute-read-articles group articles))
6316       (with-current-buffer gnus-group-buffer
6317         (gnus-undo-register
6318           `(progn
6319              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6320              (gnus-info-set-read ',info ',(gnus-info-read info))
6321              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6322              (when ,set-marks
6323                (gnus-request-set-mark
6324                 ,group (list (list ',range 'del '(read)))))
6325              (gnus-group-update-group ,group t))))
6326       ;; Add the read articles to the range.
6327       (gnus-info-set-read info range)
6328       (when set-marks
6329         (gnus-request-set-mark group (list (list range 'add '(read)))))
6330       ;; Then we have to re-compute how many unread
6331       ;; articles there are in this group.
6332       (when active
6333         (cond
6334          ((not range)
6335           (setq num (- (1+ (cdr active)) (car active))))
6336          ((not (listp (cdr range)))
6337           (setq num (- (cdr active) (- (1+ (cdr range))
6338                                        (car range)))))
6339          (t
6340           (while range
6341             (if (numberp (car range))
6342                 (setq num (1+ num))
6343               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6344             (setq range (cdr range)))
6345           (setq num (- (cdr active) num))))
6346         ;; Update the number of unread articles.
6347         (setcar entry num)
6348         ;; Update the group buffer.
6349         (unless (gnus-ephemeral-group-p group)
6350           (gnus-group-update-group group t))))))
6351
6352 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6353   (let ((cur nntp-server-buffer)
6354         (dependencies
6355          (or dependencies
6356              (with-current-buffer gnus-summary-buffer
6357                gnus-newsgroup-dependencies)))
6358         headers id end ref number
6359         (mail-parse-charset gnus-newsgroup-charset)
6360         (mail-parse-ignored-charsets
6361          (save-current-buffer (condition-case nil
6362                                   (set-buffer gnus-summary-buffer)
6363                                 (error))
6364                               gnus-newsgroup-ignored-charsets)))
6365     (with-current-buffer nntp-server-buffer
6366       ;; Translate all TAB characters into SPACE characters.
6367       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6368       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6369       (ietf-drums-unfold-fws)
6370       (gnus-run-hooks 'gnus-parse-headers-hook)
6371       (let ((case-fold-search t)
6372             in-reply-to header p lines chars)
6373         (goto-char (point-min))
6374         ;; Search to the beginning of the next header.  Error messages
6375         ;; do not begin with 2 or 3.
6376         (while (re-search-forward "^[23][0-9]+ " nil t)
6377           (setq id nil
6378                 ref nil)
6379           ;; This implementation of this function, with nine
6380           ;; search-forwards instead of the one re-search-forward and
6381           ;; a case (which basically was the old function) is actually
6382           ;; about twice as fast, even though it looks messier.  You
6383           ;; can't have everything, I guess.  Speed and elegance
6384           ;; doesn't always go hand in hand.
6385           (setq
6386            header
6387            (vector
6388             ;; Number.
6389             (prog1
6390                 (setq number (read cur))
6391               (end-of-line)
6392               (setq p (point))
6393               (narrow-to-region (point)
6394                                 (or (and (search-forward "\n.\n" nil t)
6395                                          (- (point) 2))
6396                                     (point))))
6397             ;; Subject.
6398             (progn
6399               (goto-char p)
6400               (if (search-forward "\nsubject:" nil t)
6401                   (funcall gnus-decode-encoded-word-function
6402                            (nnheader-header-value))
6403                 "(none)"))
6404             ;; From.
6405             (progn
6406               (goto-char p)
6407               (if (search-forward "\nfrom:" nil t)
6408                   (funcall gnus-decode-encoded-address-function
6409                            (nnheader-header-value))
6410                 "(nobody)"))
6411             ;; Date.
6412             (progn
6413               (goto-char p)
6414               (if (search-forward "\ndate:" nil t)
6415                   (nnheader-header-value) ""))
6416             ;; Message-ID.
6417             (progn
6418               (goto-char p)
6419               (setq id (if (re-search-forward
6420                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6421                            ;; We do it this way to make sure the Message-ID
6422                            ;; is (somewhat) syntactically valid.
6423                            (buffer-substring (match-beginning 1)
6424                                              (match-end 1))
6425                          ;; If there was no message-id, we just fake one
6426                          ;; to make subsequent routines simpler.
6427                          (nnheader-generate-fake-message-id number))))
6428             ;; References.
6429             (progn
6430               (goto-char p)
6431               (if (search-forward "\nreferences:" nil t)
6432                   (progn
6433                     (setq end (point))
6434                     (prog1
6435                         (nnheader-header-value)
6436                       (setq ref
6437                             (buffer-substring
6438                              (progn
6439                                (end-of-line)
6440                                (search-backward ">" end t)
6441                                (1+ (point)))
6442                              (progn
6443                                (search-backward "<" end t)
6444                                (point))))))
6445                 ;; Get the references from the in-reply-to header if there
6446                 ;; were no references and the in-reply-to header looks
6447                 ;; promising.
6448                 (if (and (search-forward "\nin-reply-to:" nil t)
6449                          (setq in-reply-to (nnheader-header-value))
6450                          (string-match "<[^>]+>" in-reply-to))
6451                     (let (ref2)
6452                       (setq ref (substring in-reply-to (match-beginning 0)
6453                                            (match-end 0)))
6454                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6455                         (setq ref2 (substring in-reply-to (match-beginning 0)
6456                                               (match-end 0)))
6457                         (when (> (length ref2) (length ref))
6458                           (setq ref ref2)))
6459                       ref)
6460                   (setq ref nil))))
6461             ;; Chars.
6462             (progn
6463               (goto-char p)
6464               (if (search-forward "\nchars: " nil t)
6465                   (if (numberp (setq chars (ignore-errors (read cur))))
6466                       chars -1)
6467                 -1))
6468             ;; Lines.
6469             (progn
6470               (goto-char p)
6471               (if (search-forward "\nlines: " nil t)
6472                   (if (numberp (setq lines (ignore-errors (read cur))))
6473                       lines -1)
6474                 -1))
6475             ;; Xref.
6476             (progn
6477               (goto-char p)
6478               (and (search-forward "\nxref:" nil t)
6479                    (nnheader-header-value)))
6480             ;; Extra.
6481             (when gnus-extra-headers
6482               (let ((extra gnus-extra-headers)
6483                     out)
6484                 (while extra
6485                   (goto-char p)
6486                   (when (search-forward
6487                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6488                     (push (cons (car extra) (nnheader-header-value))
6489                           out))
6490                   (pop extra))
6491                 out))))
6492           (when (equal id ref)
6493             (setq ref nil))
6494
6495           (when gnus-alter-header-function
6496             (funcall gnus-alter-header-function header)
6497             (setq id (mail-header-id header)
6498                   ref (gnus-parent-id (mail-header-references header))))
6499
6500           (when (setq header
6501                       (gnus-dependencies-add-header
6502                        header dependencies force-new))
6503             (push header headers))
6504           (goto-char (point-max))
6505           (widen))
6506         (nreverse headers)))))
6507
6508 ;; Goes through the xover lines and returns a list of vectors
6509 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6510                                                   force-new dependencies
6511                                                   group also-fetch-heads)
6512   "Parse the news overview data in the server buffer.
6513 Return a list of headers that match SEQUENCE (see
6514 `nntp-retrieve-headers')."
6515   ;; Get the Xref when the users reads the articles since most/some
6516   ;; NNTP servers do not include Xrefs when using XOVER.
6517   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6518   (let ((mail-parse-charset gnus-newsgroup-charset)
6519         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6520         (cur nntp-server-buffer)
6521         (dependencies (or dependencies gnus-newsgroup-dependencies))
6522         (allp (cond
6523                ((eq gnus-read-all-available-headers t)
6524                 t)
6525                ((and (stringp gnus-read-all-available-headers)
6526                      group)
6527                 (string-match gnus-read-all-available-headers group))
6528                (t
6529                 nil)))
6530         number headers header)
6531     (with-current-buffer nntp-server-buffer
6532       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6533       ;; Allow the user to mangle the headers before parsing them.
6534       (gnus-run-hooks 'gnus-parse-headers-hook)
6535       (goto-char (point-min))
6536       (gnus-parse-without-error
6537         (while (and (or sequence allp)
6538                     (not (eobp)))
6539           (setq number (read cur))
6540           (when (not allp)
6541             (while (and sequence
6542                         (< (car sequence) number))
6543               (setq sequence (cdr sequence))))
6544           (when (and (or allp
6545                          (and sequence
6546                               (eq number (car sequence))))
6547                      (progn
6548                        (setq sequence (cdr sequence))
6549                        (setq header (inline
6550                                       (gnus-nov-parse-line
6551                                        number dependencies force-new)))))
6552             (push header headers))
6553           (forward-line 1)))
6554       ;; A common bug in inn is that if you have posted an article and
6555       ;; then retrieves the active file, it will answer correctly --
6556       ;; the new article is included.  However, a NOV entry for the
6557       ;; article may not have been generated yet, so this may fail.
6558       ;; We work around this problem by retrieving the last few
6559       ;; headers using HEAD.
6560       (if (or (not also-fetch-heads)
6561               (not sequence))
6562           ;; We (probably) got all the headers.
6563           (nreverse headers)
6564         (let ((gnus-nov-is-evil t))
6565           (nconc
6566            (nreverse headers)
6567            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6568              (gnus-get-newsgroup-headers))))))))
6569
6570 (defun gnus-article-get-xrefs ()
6571   "Fill in the Xref value in `gnus-current-headers', if necessary.
6572 This is meant to be called in `gnus-article-internal-prepare-hook'."
6573   (let ((headers (with-current-buffer gnus-summary-buffer
6574                    gnus-current-headers)))
6575     (or (not gnus-use-cross-reference)
6576         (not headers)
6577         (and (mail-header-xref headers)
6578              (not (string= (mail-header-xref headers) "")))
6579         (let ((case-fold-search t)
6580               xref)
6581           (save-restriction
6582             (nnheader-narrow-to-headers)
6583             (goto-char (point-min))
6584             (when (or (and (not (eobp))
6585                            (eq (downcase (char-after)) ?x)
6586                            (looking-at "Xref:"))
6587                       (search-forward "\nXref:" nil t))
6588               (goto-char (1+ (match-end 0)))
6589               (setq xref (buffer-substring (point) (point-at-eol)))
6590               (mail-header-set-xref headers xref)))))))
6591
6592 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6593   "Find article ID and insert the summary line for that article.
6594 OLD-HEADER can either be a header or a line number to insert
6595 the subject line on.
6596 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6597 and OLD-HEADER will be used when the summary line is inserted,
6598 too, instead of trying to fetch new headers."
6599   (let* ((line (and (numberp old-header) old-header))
6600          (old-header (and (vectorp old-header) old-header))
6601          (header (cond ((and old-header use-old-header)
6602                         old-header)
6603                        ((and (numberp id)
6604                              (gnus-number-to-header id))
6605                         (gnus-number-to-header id))
6606                        (t
6607                         (gnus-read-header id))))
6608          (number (and (numberp id) id))
6609          d)
6610     (when header
6611       ;; Rebuild the thread that this article is part of and go to the
6612       ;; article we have fetched.
6613       (when (and (not gnus-show-threads)
6614                  old-header)
6615         (when (and number
6616                    (setq d (gnus-data-find (mail-header-number old-header))))
6617           (goto-char (gnus-data-pos d))
6618           (gnus-data-remove
6619            number
6620            (- (point-at-bol)
6621               (prog1
6622                   (1+ (point-at-eol))
6623                 (gnus-delete-line))))))
6624       ;; Remove list identifiers from subject.
6625       (let ((gnus-newsgroup-headers (list header)))
6626         (gnus-summary-remove-list-identifiers))
6627       (when old-header
6628         (mail-header-set-number header (mail-header-number old-header)))
6629       (setq gnus-newsgroup-sparse
6630             (delq (setq number (mail-header-number header))
6631                   gnus-newsgroup-sparse))
6632       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6633       (push number gnus-newsgroup-limit)
6634       (gnus-rebuild-thread (mail-header-id header) line)
6635       (gnus-summary-goto-subject number nil t))
6636     (when (and (numberp number)
6637                (> number 0))
6638       ;; We have to update the boundaries even if we can't fetch the
6639       ;; article if ID is a number -- so that the next `P' or `N'
6640       ;; command will fetch the previous (or next) article even
6641       ;; if the one we tried to fetch this time has been canceled.
6642       (unless (and gnus-newsgroup-end (< number gnus-newsgroup-end))
6643         (setq gnus-newsgroup-end number))
6644       (unless (and gnus-newsgroup-begin (> number gnus-newsgroup-begin))
6645         (setq gnus-newsgroup-begin number))
6646       (setq gnus-newsgroup-unselected
6647             (delq number gnus-newsgroup-unselected)))
6648     ;; Report back a success?
6649     (and header (mail-header-number header))))
6650
6651 ;;; Process/prefix in the summary buffer
6652
6653 (defun gnus-summary-work-articles (n)
6654   "Return a list of articles to be worked upon.
6655 The prefix argument, the list of process marked articles, and the
6656 current article will be taken into consideration."
6657   (with-current-buffer gnus-summary-buffer
6658     (cond
6659      (n
6660       ;; A numerical prefix has been given.
6661       (setq n (prefix-numeric-value n))
6662       (let ((backward (< n 0))
6663             (n (abs (prefix-numeric-value n)))
6664             articles article)
6665         (save-excursion
6666           (while
6667               (and (> n 0)
6668                    (push (setq article (gnus-summary-article-number))
6669                          articles)
6670                    (if backward
6671                        (gnus-summary-find-prev nil article)
6672                      (gnus-summary-find-next nil article)))
6673             (decf n)))
6674         (nreverse articles)))
6675      ((and (gnus-region-active-p) (mark))
6676       (message "region active")
6677       ;; Work on the region between point and mark.
6678       (let ((max (max (point) (mark)))
6679             articles article)
6680         (save-excursion
6681           (goto-char (min (point) (mark)))
6682           (while
6683               (and
6684                (push (setq article (gnus-summary-article-number)) articles)
6685                (gnus-summary-find-next nil article)
6686                (< (point) max)))
6687           (nreverse articles))))
6688      (gnus-newsgroup-processable
6689       ;; There are process-marked articles present.
6690       ;; Save current state.
6691       (gnus-summary-save-process-mark)
6692       ;; Return the list.
6693       (reverse gnus-newsgroup-processable))
6694      (t
6695       ;; Just return the current article.
6696       (list (gnus-summary-article-number))))))
6697
6698 (defmacro gnus-summary-iterate (arg &rest forms)
6699   "Iterate over the process/prefixed articles and do FORMS.
6700 ARG is the interactive prefix given to the command.  FORMS will be
6701 executed with point over the summary line of the articles."
6702   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6703     `(let ((,articles (gnus-summary-work-articles ,arg)))
6704        (while ,articles
6705          (gnus-summary-goto-subject (car ,articles))
6706          ,@forms
6707          (pop ,articles)))))
6708
6709 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6710 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6711
6712 (defun gnus-summary-save-process-mark ()
6713   "Push the current set of process marked articles on the stack."
6714   (interactive)
6715   (push (copy-sequence gnus-newsgroup-processable)
6716         gnus-newsgroup-process-stack))
6717
6718 (defun gnus-summary-kill-process-mark ()
6719   "Push the current set of process marked articles on the stack and unmark."
6720   (interactive)
6721   (gnus-summary-save-process-mark)
6722   (gnus-summary-unmark-all-processable))
6723
6724 (defun gnus-summary-yank-process-mark ()
6725   "Pop the last process mark state off the stack and restore it."
6726   (interactive)
6727   (unless gnus-newsgroup-process-stack
6728     (error "Empty mark stack"))
6729   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6730
6731 (defun gnus-summary-process-mark-set (set)
6732   "Make SET into the current process marked articles."
6733   (gnus-summary-unmark-all-processable)
6734   (mapc 'gnus-summary-set-process-mark set))
6735
6736 ;;; Searching and stuff
6737
6738 (defun gnus-summary-search-group (&optional backward use-level)
6739   "Search for next unread newsgroup.
6740 If optional argument BACKWARD is non-nil, search backward instead."
6741   (with-current-buffer gnus-group-buffer
6742     (when (gnus-group-search-forward
6743            backward nil (if use-level (gnus-group-group-level) nil))
6744       (gnus-group-group-name))))
6745
6746 (defun gnus-summary-best-group (&optional exclude-group)
6747   "Find the name of the best unread group.
6748 If EXCLUDE-GROUP, do not go to this group."
6749   (with-current-buffer gnus-group-buffer
6750     (save-excursion
6751       (gnus-group-best-unread-group exclude-group))))
6752
6753 (defun gnus-summary-find-next (&optional unread article backward)
6754   (if backward
6755       (gnus-summary-find-prev unread article)
6756     (let* ((dummy (gnus-summary-article-intangible-p))
6757            (article (or article (gnus-summary-article-number)))
6758            (data (gnus-data-find-list article))
6759            result)
6760       (when (and (not dummy)
6761                  (or (not gnus-summary-check-current)
6762                      (not unread)
6763                      (not (gnus-data-unread-p (car data)))))
6764         (setq data (cdr data)))
6765       (when (setq result
6766                   (if unread
6767                       (progn
6768                         (while data
6769                           (unless (memq (gnus-data-number (car data))
6770                                         (cond
6771                                          ((eq gnus-auto-goto-ignores
6772                                               'always-undownloaded)
6773                                           gnus-newsgroup-undownloaded)
6774                                          (gnus-plugged
6775                                           nil)
6776                                          ((eq gnus-auto-goto-ignores
6777                                               'unfetched)
6778                                           gnus-newsgroup-unfetched)
6779                                          ((eq gnus-auto-goto-ignores
6780                                               'undownloaded)
6781                                           gnus-newsgroup-undownloaded)))
6782                             (when (gnus-data-unread-p (car data))
6783                               (setq result (car data)
6784                                     data nil)))
6785                           (setq data (cdr data)))
6786                         result)
6787                     (car data)))
6788         (goto-char (gnus-data-pos result))
6789         (gnus-data-number result)))))
6790
6791 (defun gnus-summary-find-prev (&optional unread article)
6792   (let* ((eobp (eobp))
6793          (article (or article (gnus-summary-article-number)))
6794          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6795          result)
6796     (when (and (not eobp)
6797                (or (not gnus-summary-check-current)
6798                    (not unread)
6799                    (not (gnus-data-unread-p (car data)))))
6800       (setq data (cdr data)))
6801     (when (setq result
6802                 (if unread
6803                     (progn
6804                       (while data
6805                         (unless (memq (gnus-data-number (car data))
6806                                       (cond
6807                                        ((eq gnus-auto-goto-ignores
6808                                             'always-undownloaded)
6809                                         gnus-newsgroup-undownloaded)
6810                                        (gnus-plugged
6811                                         nil)
6812                                        ((eq gnus-auto-goto-ignores
6813                                             'unfetched)
6814                                         gnus-newsgroup-unfetched)
6815                                        ((eq gnus-auto-goto-ignores
6816                                             'undownloaded)
6817                                         gnus-newsgroup-undownloaded)))
6818                           (when (gnus-data-unread-p (car data))
6819                             (setq result (car data)
6820                                   data nil)))
6821                         (setq data (cdr data)))
6822                       result)
6823                   (car data)))
6824       (goto-char (gnus-data-pos result))
6825       (gnus-data-number result))))
6826
6827 (defun gnus-summary-find-subject (subject &optional unread backward article)
6828   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6829          (article (or article (gnus-summary-article-number)))
6830          (articles (gnus-data-list backward))
6831          (arts (gnus-data-find-list article articles))
6832          result)
6833     (when (or (not gnus-summary-check-current)
6834               (not unread)
6835               (not (gnus-data-unread-p (car arts))))
6836       (setq arts (cdr arts)))
6837     (while arts
6838       (and (or (not unread)
6839                (gnus-data-unread-p (car arts)))
6840            (vectorp (gnus-data-header (car arts)))
6841            (gnus-subject-equal
6842             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6843            (setq result (car arts)
6844                  arts nil))
6845       (setq arts (cdr arts)))
6846     (and result
6847          (goto-char (gnus-data-pos result))
6848          (gnus-data-number result))))
6849
6850 (defun gnus-summary-search-forward (&optional unread subject backward)
6851   "Search forward for an article.
6852 If UNREAD, look for unread articles.  If SUBJECT, look for
6853 articles with that subject.  If BACKWARD, search backward instead."
6854   (cond (subject (gnus-summary-find-subject subject unread backward))
6855         (backward (gnus-summary-find-prev unread))
6856         (t (gnus-summary-find-next unread))))
6857
6858 (defun gnus-recenter (&optional n)
6859   "Center point in window and redisplay frame.
6860 Also do horizontal recentering."
6861   (interactive "P")
6862   (when (and gnus-auto-center-summary
6863              (not (eq gnus-auto-center-summary 'vertical)))
6864     (gnus-horizontal-recenter))
6865   (if (fboundp 'recenter-top-bottom)
6866       (recenter-top-bottom n)
6867     (recenter n)))
6868
6869 (put 'gnus-recenter 'isearch-scroll t)
6870
6871 (defun gnus-forward-line-ignore-invisible (n)
6872   "Move N lines forward (backward if N is negative).
6873 Like forward-line, but skip over (and don't count) invisible lines."
6874   (let (done)
6875     (while (and (> n 0) (not done))
6876       ;; If the following character is currently invisible,
6877       ;; skip all characters with that same `invisible' property value.
6878       (while (gnus-invisible-p (point))
6879         (goto-char (gnus-next-char-property-change (point))))
6880       (forward-line 1)
6881       (if (eobp)
6882           (setq done t)
6883         (setq n (1- n))))
6884     (while (and (< n 0) (not done))
6885       (forward-line -1)
6886       (if (bobp) (setq done t)
6887         (setq n (1+ n))
6888         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6889           (goto-char (gnus-previous-char-property-change (point))))))))
6890
6891 (defun gnus-summary-recenter ()
6892   "Center point in the summary window.
6893 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6894 displayed, no centering will be performed."
6895   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6896   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6897   (interactive)
6898   ;; The user has to want it.
6899   (when gnus-auto-center-summary
6900     (let* ((top (cond ((< (window-height) 4) 0)
6901                       ((< (window-height) 7) 1)
6902                       (t (if (numberp gnus-auto-center-summary)
6903                              gnus-auto-center-summary
6904                            (/ (1- (window-height)) 2)))))
6905            (height (1- (window-height)))
6906            (bottom (save-excursion
6907                      (goto-char (point-max))
6908                      (gnus-forward-line-ignore-invisible (- height))
6909                      (point)))
6910            (window (get-buffer-window (current-buffer))))
6911       (when (get-buffer-window gnus-article-buffer)
6912         ;; Only do recentering when the article buffer is displayed,
6913         ;; Set the window start to either `bottom', which is the biggest
6914         ;; possible valid number, or the second line from the top,
6915         ;; whichever is the least.
6916         (let ((top-pos (save-excursion
6917                          (gnus-forward-line-ignore-invisible (- top))
6918                          (point))))
6919           (if (> bottom top-pos)
6920               ;; Keep the second line from the top visible
6921               (set-window-start window top-pos)
6922             ;; Try to keep the bottom line visible; if it's partially
6923             ;; obscured, either scroll one more line to make it fully
6924             ;; visible, or revert to using TOP-POS.
6925             (save-excursion
6926               (goto-char (point-max))
6927               (gnus-forward-line-ignore-invisible -1)
6928               (let ((last-line-start (point)))
6929                 (goto-char bottom)
6930                 (set-window-start window (point) t)
6931                 (when (not (pos-visible-in-window-p last-line-start window))
6932                   (gnus-forward-line-ignore-invisible 1)
6933                   (set-window-start window (min (point) top-pos) t)))))))
6934       ;; Do horizontal recentering while we're at it.
6935       (when (and (get-buffer-window (current-buffer) t)
6936                  (not (eq gnus-auto-center-summary 'vertical)))
6937         (let ((selected (selected-window)))
6938           (select-window (get-buffer-window (current-buffer) t))
6939           (gnus-summary-position-point)
6940           (gnus-horizontal-recenter)
6941           (select-window selected))))))
6942
6943 (defun gnus-summary-jump-to-group (newsgroup)
6944   "Move point to NEWSGROUP in group mode buffer."
6945   ;; Keep update point of group mode buffer if visible.
6946   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6947       (save-window-excursion
6948         ;; Take care of tree window mode.
6949         (when (get-buffer-window gnus-group-buffer)
6950           (pop-to-buffer gnus-group-buffer))
6951         (gnus-group-jump-to-group newsgroup))
6952     (save-excursion
6953       ;; Take care of tree window mode.
6954       (if (get-buffer-window gnus-group-buffer 0)
6955           (pop-to-buffer gnus-group-buffer)
6956         (set-buffer gnus-group-buffer))
6957       (gnus-group-jump-to-group newsgroup))))
6958
6959 ;; This function returns a list of article numbers based on the
6960 ;; difference between the ranges of read articles in this group and
6961 ;; the range of active articles.
6962 (defun gnus-list-of-unread-articles (group)
6963   (let* ((read (gnus-info-read (gnus-get-info group)))
6964          (active (or (gnus-active group) (gnus-activate-group group)))
6965          (last (or (cdr active)
6966                    (error "Group %s couldn't be activated " group)))
6967          (bottom (if gnus-newsgroup-maximum-articles
6968                      (max (car active)
6969                           (- last gnus-newsgroup-maximum-articles -1))
6970                    (car active)))
6971          first nlast unread)
6972     ;; If none are read, then all are unread.
6973     (if (not read)
6974         (setq first bottom)
6975       ;; If the range of read articles is a single range, then the
6976       ;; first unread article is the article after the last read
6977       ;; article.  Sounds logical, doesn't it?
6978       (if (and (not (listp (cdr read)))
6979                (or (< (car read) bottom)
6980                    (progn (setq read (list read))
6981                           nil)))
6982           (setq first (max bottom (1+ (cdr read))))
6983         ;; `read' is a list of ranges.
6984         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6985                                   (caar read)))
6986                   1)
6987           (setq first bottom))
6988         (while read
6989           (when first
6990             (while (< first nlast)
6991               (setq unread (cons first unread)
6992                     first (1+ first))))
6993           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6994           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6995           (setq read (cdr read)))))
6996     ;; And add the last unread articles.
6997     (while (<= first last)
6998       (setq unread (cons first unread)
6999             first (1+ first)))
7000     ;; Return the list of unread articles.
7001     (delq 0 (nreverse unread))))
7002
7003 (defun gnus-list-of-read-articles (group)
7004   "Return a list of unread, unticked and non-dormant articles."
7005   (let* ((info (gnus-get-info group))
7006          (marked (gnus-info-marks info))
7007          (active (gnus-active group)))
7008     (and info active
7009          (gnus-list-range-difference
7010           (gnus-list-range-difference
7011            (gnus-sorted-complement
7012             (gnus-uncompress-range
7013              (if gnus-newsgroup-maximum-articles
7014                  (cons (max (car active)
7015                             (- (cdr active)
7016                                gnus-newsgroup-maximum-articles
7017                                -1))
7018                        (cdr active))
7019                active))
7020             (gnus-list-of-unread-articles group))
7021            (cdr (assq 'dormant marked)))
7022           (cdr (assq 'tick marked))))))
7023
7024 ;; This function returns a sequence of article numbers based on the
7025 ;; difference between the ranges of read articles in this group and
7026 ;; the range of active articles.
7027 (defun gnus-sequence-of-unread-articles (group)
7028   (let* ((read (gnus-info-read (gnus-get-info group)))
7029          (active (or (gnus-active group) (gnus-activate-group group)))
7030          (last (cdr active))
7031          (bottom (if gnus-newsgroup-maximum-articles
7032                      (max (car active)
7033                           (- last gnus-newsgroup-maximum-articles -1))
7034                    (car active)))
7035          first nlast unread)
7036     ;; If none are read, then all are unread.
7037     (if (not read)
7038         (setq first bottom)
7039       ;; If the range of read articles is a single range, then the
7040       ;; first unread article is the article after the last read
7041       ;; article.  Sounds logical, doesn't it?
7042       (if (and (not (listp (cdr read)))
7043                (or (< (car read) bottom)
7044                    (progn (setq read (list read))
7045                           nil)))
7046           (setq first (max bottom (1+ (cdr read))))
7047         ;; `read' is a list of ranges.
7048         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7049                                   (caar read)))
7050                   1)
7051           (setq first bottom))
7052         (while read
7053           (when first
7054             (push (cons first nlast) unread))
7055           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7056           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7057           (setq read (cdr read)))))
7058     ;; And add the last unread articles.
7059     (cond ((not (and first last))
7060            nil)
7061           ((< first last)
7062            (push (cons first last) unread))
7063           ((= first last)
7064            (push first unread)))
7065     ;; Return the sequence of unread articles.
7066     (delq 0 (nreverse unread))))
7067
7068 ;; Various summary commands
7069
7070 (defun gnus-summary-select-article-buffer ()
7071   "Reconfigure windows to show the article buffer.
7072 If `gnus-widen-article-window' is set, show only the article
7073 buffer."
7074   (interactive)
7075   (if (not (gnus-buffer-live-p gnus-article-buffer))
7076       (error "There is no article buffer for this summary buffer")
7077     (unless (get-buffer-window gnus-article-buffer)
7078       (gnus-summary-show-article))
7079     (gnus-configure-windows
7080      (if gnus-widen-article-window
7081          'only-article
7082        'article)
7083      t)
7084     (select-window (get-buffer-window gnus-article-buffer))))
7085
7086 (defun gnus-summary-universal-argument (arg)
7087   "Perform any operation on all articles that are process/prefixed."
7088   (interactive "P")
7089   (let ((articles (gnus-summary-work-articles arg))
7090         func article)
7091     (if (eq
7092          (setq
7093           func
7094           (key-binding
7095            (read-key-sequence
7096             (substitute-command-keys
7097              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7098          'undefined)
7099         (gnus-error 1 "Undefined key")
7100       (save-excursion
7101         (while articles
7102           (gnus-summary-goto-subject (setq article (pop articles)))
7103           (let (gnus-newsgroup-processable)
7104             (command-execute func))
7105           (gnus-summary-remove-process-mark article)))))
7106   (gnus-summary-position-point))
7107
7108 (defun gnus-summary-toggle-truncation (&optional arg)
7109   "Toggle truncation of summary lines.
7110 With ARG, turn line truncation on if ARG is positive."
7111   (interactive "P")
7112   (setq truncate-lines
7113         (if (null arg) (not truncate-lines)
7114           (> (prefix-numeric-value arg) 0)))
7115   (redraw-display))
7116
7117 (defun gnus-summary-find-for-reselect ()
7118   "Return the number of an article to stay on across a reselect.
7119 The current article is considered, then following articles, then previous
7120 articles.  An article is sought which is not canceled and isn't a temporary
7121 insertion from another group.  If there's no such then return a dummy 0."
7122   (let (found)
7123     (dolist (rev '(nil t))
7124       (unless found      ; don't demand the reverse list if we don't need it
7125         (let ((data (gnus-data-find-list
7126                      (gnus-summary-article-number) (gnus-data-list rev))))
7127           (while (and data (not found))
7128             (if (and (< 0 (gnus-data-number (car data)))
7129                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7130                 (setq found (gnus-data-number (car data))))
7131             (setq data (cdr data))))))
7132     (or found 0)))
7133
7134 (defun gnus-summary-reselect-current-group (&optional all rescan)
7135   "Exit and then reselect the current newsgroup.
7136 The prefix argument ALL means to select all articles."
7137   (interactive "P")
7138   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7139     (error "Ephemeral groups can't be reselected"))
7140   (let ((current-subject (gnus-summary-find-for-reselect))
7141         (group gnus-newsgroup-name))
7142     (setq gnus-newsgroup-begin nil)
7143     (gnus-summary-exit nil 'leave-hidden)
7144     ;; We have to adjust the point of group mode buffer because
7145     ;; point was moved to the next unread newsgroup by exiting.
7146     (gnus-summary-jump-to-group group)
7147     (when rescan
7148       (save-excursion
7149         (gnus-group-get-new-news-this-group 1)))
7150     (gnus-group-read-group all t)
7151     (gnus-summary-goto-subject current-subject nil t)))
7152
7153 (defun gnus-summary-rescan-group (&optional all)
7154   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7155   (interactive "P")
7156   (let ((config gnus-current-window-configuration))
7157     (gnus-summary-reselect-current-group all t)
7158     (gnus-configure-windows config)
7159     (when (eq config 'article)
7160       (gnus-summary-select-article))))
7161
7162 (defun gnus-summary-update-info (&optional non-destructive)
7163   (save-excursion
7164     (let ((group gnus-newsgroup-name))
7165       (when group
7166         (when gnus-newsgroup-kill-headers
7167           (setq gnus-newsgroup-killed
7168                 (gnus-compress-sequence
7169                  (gnus-sorted-union
7170                   (gnus-list-range-intersection
7171                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7172                   gnus-newsgroup-unreads)
7173                  t)))
7174         (unless (listp (cdr gnus-newsgroup-killed))
7175           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7176         (let ((headers gnus-newsgroup-headers)
7177               (ephemeral-p (gnus-ephemeral-group-p group))
7178               info)
7179           (unless ephemeral-p
7180             (setq info (copy-sequence (gnus-get-info group))
7181                   info (delq (gnus-info-params info) info)))
7182           ;; Set the new ranges of read articles.
7183           (with-current-buffer gnus-group-buffer
7184             (gnus-undo-force-boundary))
7185           (gnus-update-read-articles
7186            group (gnus-sorted-union
7187                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7188           ;; Set the current article marks.
7189           (let ((gnus-newsgroup-scored
7190                  (if (and (not gnus-save-score)
7191                           (not non-destructive))
7192                      nil
7193                    gnus-newsgroup-scored)))
7194             (save-excursion
7195               (gnus-update-marks)))
7196           ;; Do the cross-ref thing.
7197           (when gnus-use-cross-reference
7198             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7199           ;; Do not switch windows but change the buffer to work.
7200           (set-buffer gnus-group-buffer)
7201           (unless ephemeral-p
7202             (gnus-group-update-group
7203              group nil
7204              (equal info
7205                     (setq info (copy-sequence (gnus-get-info group))
7206                           info (delq (gnus-info-params info) info))))))))))
7207
7208 (defun gnus-summary-save-newsrc (&optional force)
7209   "Save the current number of read/marked articles in the dribble buffer.
7210 The dribble buffer will then be saved.
7211 If FORCE (the prefix), also save the .newsrc file(s)."
7212   (interactive "P")
7213   (gnus-summary-update-info t)
7214   (if force
7215       (gnus-save-newsrc-file)
7216     (gnus-dribble-save)))
7217
7218 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7219
7220 (defun gnus-summary-exit (&optional temporary leave-hidden)
7221   "Exit reading current newsgroup, and then return to group selection mode.
7222 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7223   (interactive)
7224   (gnus-set-global-variables)
7225   (when (gnus-buffer-live-p gnus-article-buffer)
7226     (with-current-buffer gnus-article-buffer
7227       (mm-destroy-parts gnus-article-mime-handles)
7228       ;; Set it to nil for safety reason.
7229       (setq gnus-article-mime-handle-alist nil)
7230       (setq gnus-article-mime-handles nil)))
7231   (gnus-kill-save-kill-buffer)
7232   (gnus-async-halt-prefetch)
7233   (let* ((group gnus-newsgroup-name)
7234          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7235          (gnus-group-is-exiting-p t)
7236          (article-buffer gnus-article-buffer)
7237          (original-article-buffer gnus-original-article-buffer)
7238          (mode major-mode)
7239          (group-point nil)
7240          (buf (current-buffer))
7241          ;; `gnus-single-article-buffer' is nil buffer-locally in
7242          ;; ephemeral group of which summary buffer will be killed,
7243          ;; but the global value may be non-nil.
7244          (single-article-buffer gnus-single-article-buffer))
7245     (unless quit-config
7246       ;; Do adaptive scoring, and possibly save score files.
7247       (when gnus-newsgroup-adaptive
7248         (gnus-score-adaptive))
7249       (when gnus-use-scoring
7250         (gnus-score-save)))
7251     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7252     (when gnus-use-cache
7253       (gnus-cache-possibly-remove-articles)
7254       (gnus-cache-save-buffers))
7255     (gnus-async-prefetch-remove-group group)
7256     (when gnus-suppress-duplicates
7257       (gnus-dup-enter-articles))
7258     (when gnus-use-trees
7259       (gnus-tree-close group))
7260     (when gnus-use-cache
7261       (gnus-cache-write-active))
7262     ;; Remove entries for this group.
7263     (nnmail-purge-split-history (gnus-group-real-name group))
7264     ;; Make all changes in this group permanent.
7265     (unless quit-config
7266       (gnus-run-hooks 'gnus-exit-group-hook)
7267       (gnus-summary-update-info))
7268     (gnus-close-group group)
7269     ;; Make sure where we were, and go to next newsgroup.
7270     (when (buffer-live-p (get-buffer gnus-group-buffer))
7271       (set-buffer gnus-group-buffer))
7272     (unless quit-config
7273       (gnus-group-jump-to-group group))
7274     (gnus-run-hooks 'gnus-summary-exit-hook)
7275     (unless (or quit-config
7276                 (not gnus-summary-next-group-on-exit)
7277                 ;; If this group has disappeared from the summary
7278                 ;; buffer, don't skip forwards.
7279                 (not (string= group (gnus-group-group-name))))
7280       (gnus-group-next-unread-group 1))
7281     (setq group-point (point))
7282     (if temporary
7283         nil                             ;Nothing to do.
7284       (set-buffer buf)
7285       (if (not gnus-kill-summary-on-exit)
7286           (progn
7287             (gnus-deaden-summary)
7288             (setq mode nil))
7289         (when (get-buffer gnus-article-buffer)
7290           (bury-buffer gnus-article-buffer))
7291         ;; Return to group mode buffer.
7292         (when (eq mode 'gnus-summary-mode)
7293           (gnus-kill-buffer buf)))
7294
7295       (setq gnus-current-select-method gnus-select-method)
7296       (when (gnus-buffer-live-p gnus-group-buffer)
7297         (set-buffer gnus-group-buffer))
7298       (if quit-config
7299           (gnus-handle-ephemeral-exit quit-config)
7300         (goto-char group-point)
7301         ;; If gnus-group-buffer is already displayed, make sure we also move
7302         ;; the cursor in the window that displays it.
7303         (let ((win (get-buffer-window (current-buffer) 0)))
7304           (if win (set-window-point win (point))))
7305         (unless leave-hidden
7306           (gnus-configure-windows 'group 'force)))
7307
7308       ;; If we have several article buffers, we kill them at exit.
7309       (unless single-article-buffer
7310         (when (gnus-buffer-live-p article-buffer)
7311           (with-current-buffer article-buffer
7312             ;; Don't kill sticky article buffers
7313             (unless (eq major-mode 'gnus-sticky-article-mode)
7314               (gnus-kill-buffer article-buffer)
7315               (setq gnus-article-current nil))))
7316         (gnus-kill-buffer original-article-buffer))
7317
7318       ;; Clear the current group name.
7319       (unless quit-config
7320         (setq gnus-newsgroup-name nil)))))
7321
7322 (declare-function gnus-article-stop-animations "gnus-art" ())
7323 (declare-function gnus-stop-downloads "gnus-art" ())
7324
7325 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7326 (defun gnus-summary-exit-no-update (&optional no-questions)
7327   "Quit reading current newsgroup without updating read article info."
7328   (interactive)
7329   (let* ((group gnus-newsgroup-name)
7330          (gnus-group-is-exiting-p t)
7331          (gnus-group-is-exiting-without-update-p t)
7332          (quit-config (gnus-group-quit-config group)))
7333     (when (or no-questions
7334               gnus-expert-user
7335               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7336       (gnus-async-halt-prefetch)
7337       (run-hooks 'gnus-summary-prepare-exit-hook)
7338       (when (gnus-buffer-live-p gnus-article-buffer)
7339         (with-current-buffer gnus-article-buffer
7340           (gnus-article-stop-animations)
7341           (gnus-stop-downloads)
7342           (mm-destroy-parts gnus-article-mime-handles)
7343           ;; Set it to nil for safety reason.
7344           (setq gnus-article-mime-handle-alist nil)
7345           (setq gnus-article-mime-handles nil)))
7346       ;; If we have several article buffers, we kill them at exit.
7347       (unless gnus-single-article-buffer
7348         (gnus-kill-buffer gnus-article-buffer)
7349         (gnus-kill-buffer gnus-original-article-buffer)
7350         (setq gnus-article-current nil))
7351       ;; Return to the group buffer.
7352       (if (not gnus-kill-summary-on-exit)
7353           (progn
7354             (gnus-deaden-summary)
7355             (gnus-configure-windows 'group 'force))
7356         (gnus-configure-windows 'group 'force)
7357         (gnus-close-group group)
7358         (gnus-kill-buffer gnus-summary-buffer))
7359       (unless gnus-single-article-buffer
7360         (setq gnus-article-current nil))
7361       (when gnus-use-trees
7362         (gnus-tree-close group))
7363       (gnus-async-prefetch-remove-group group)
7364       (when (get-buffer gnus-article-buffer)
7365         (bury-buffer gnus-article-buffer))
7366       ;; Clear the current group name.
7367       (setq gnus-newsgroup-name nil)
7368       (unless (gnus-ephemeral-group-p group)
7369         (gnus-group-update-group group nil t))
7370       (when (equal (gnus-group-group-name) group)
7371         (gnus-group-next-unread-group 1))
7372       (when quit-config
7373         (gnus-handle-ephemeral-exit quit-config)))))
7374
7375 (defun gnus-handle-ephemeral-exit (quit-config)
7376   "Handle movement when leaving an ephemeral group.
7377 The state which existed when entering the ephemeral is reset."
7378   (if (not (buffer-live-p (car quit-config)))
7379       (when (gnus-buffer-live-p gnus-group-buffer)
7380         (gnus-configure-windows 'group 'force))
7381     (set-buffer (car quit-config))
7382     (unless (eq (cdr quit-config) 'group)
7383       (setq gnus-current-select-method
7384             (gnus-find-method-for-group gnus-newsgroup-name)))
7385     (cond ((eq major-mode 'gnus-summary-mode)
7386            (gnus-set-global-variables))
7387           ((eq major-mode 'gnus-article-mode)
7388            (save-current-buffer
7389              ;; The `gnus-summary-buffer' variable may point
7390              ;; to the old summary buffer when using a single
7391              ;; article buffer.
7392              (unless (gnus-buffer-live-p gnus-summary-buffer)
7393                (set-buffer gnus-group-buffer))
7394              (set-buffer gnus-summary-buffer)
7395              (gnus-set-global-variables))))
7396     (if (or (eq (cdr quit-config) 'article)
7397             (eq (cdr quit-config) 'pick))
7398         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7399             (gnus-configure-windows 'pick 'force)
7400           (gnus-configure-windows (cdr quit-config) 'force))
7401       (gnus-configure-windows (cdr quit-config) 'force))
7402     (when (eq major-mode 'gnus-summary-mode)
7403       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7404                                                      next-unread-noselect))
7405           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7406                                   'next-noselect)
7407                               (gnus-summary-next-subject 1 nil t))
7408                              ((eq gnus-auto-select-on-ephemeral-exit
7409                                   'next-unread-noselect)
7410                               (gnus-summary-next-subject 1 t t))))
7411             ;; Hide the article buffer which displays the article different
7412             ;; from the one that the cursor points to in the summary buffer.
7413             (gnus-configure-windows 'summary 'force))
7414         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7415                (gnus-summary-next-subject 1))
7416               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7417                (gnus-summary-next-subject 1 t))))
7418       (gnus-summary-recenter)
7419       (gnus-summary-position-point))))
7420
7421 ;;; Dead summaries.
7422
7423 (defvar gnus-dead-summary-mode-map
7424   (let ((map (make-keymap)))
7425     (suppress-keymap map)
7426     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7427     (dolist (key '("\C-d" "\r" "\177" [delete]))
7428       (define-key map key 'gnus-summary-wake-up-the-dead))
7429     (dolist (key '("q" "Q"))
7430       (define-key map key 'bury-buffer))
7431     map))
7432
7433 (define-minor-mode gnus-dead-summary-mode
7434   "Minor mode for Gnus summary buffers."
7435   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7436   (unless (derived-mode-p 'gnus-summary-mode)
7437     (setq gnus-dead-summary-mode nil)))
7438
7439 (defun gnus-deaden-summary ()
7440   "Make the current summary buffer into a dead summary buffer."
7441   ;; Kill any previous dead summary buffer.
7442   (when (and gnus-dead-summary
7443              (buffer-name gnus-dead-summary))
7444     (with-current-buffer gnus-dead-summary
7445       (when gnus-dead-summary-mode
7446         (kill-buffer (current-buffer)))))
7447   ;; Make this the current dead summary.
7448   (setq gnus-dead-summary (current-buffer))
7449   (gnus-dead-summary-mode 1)
7450   (let ((name (buffer-name)))
7451     (when (string-match "Summary" name)
7452       (rename-buffer
7453        (concat (substring name 0 (match-beginning 0)) "Dead "
7454                (substring name (match-beginning 0)))
7455        t)
7456       (bury-buffer))))
7457
7458 (defun gnus-kill-or-deaden-summary (buffer)
7459   "Kill or deaden the summary BUFFER."
7460   (save-excursion
7461     (when (and (buffer-name buffer)
7462                (not gnus-single-article-buffer))
7463       (with-current-buffer buffer
7464         (gnus-kill-buffer gnus-article-buffer)
7465         (gnus-kill-buffer gnus-original-article-buffer)))
7466     (cond
7467      ;; Kill the buffer.
7468      (gnus-kill-summary-on-exit
7469       (when (and gnus-use-trees
7470                  (gnus-buffer-exists-p buffer))
7471         (with-current-buffer buffer
7472           (gnus-tree-close gnus-newsgroup-name)))
7473       (gnus-kill-buffer buffer))
7474      ;; Deaden the buffer.
7475      ((gnus-buffer-exists-p buffer)
7476       (with-current-buffer buffer
7477         (gnus-deaden-summary))))))
7478
7479 (defun gnus-summary-wake-up-the-dead (&rest args)
7480   "Wake up the dead summary buffer."
7481   (interactive)
7482   (gnus-dead-summary-mode -1)
7483   (let ((name (buffer-name)))
7484     (when (string-match "Dead " name)
7485       (rename-buffer
7486        (concat (substring name 0 (match-beginning 0))
7487                (substring name (match-end 0)))
7488        t)))
7489   (gnus-message 3 "This dead summary is now alive again"))
7490
7491 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7492 (defun gnus-summary-describe-group (&optional force)
7493   "Describe the current newsgroup."
7494   (interactive "P")
7495   (gnus-group-describe-group force gnus-newsgroup-name))
7496
7497 (defun gnus-summary-describe-briefly ()
7498   "Describe summary mode commands briefly."
7499   (interactive)
7500   (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")))
7501
7502 ;; Walking around group mode buffer from summary mode.
7503
7504 (defun gnus-summary-next-group (&optional no-article target-group backward)
7505   "Exit current newsgroup and then select next unread newsgroup.
7506 If prefix argument NO-ARTICLE is non-nil, no article is selected
7507 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7508 previous group instead."
7509   (interactive "P")
7510   ;; Stop pre-fetching.
7511   (gnus-async-halt-prefetch)
7512   (let ((current-group gnus-newsgroup-name)
7513         (current-buffer (current-buffer))
7514         entered)
7515     ;; First we semi-exit this group to update Xrefs and all variables.
7516     ;; We can't do a real exit, because the window conf must remain
7517     ;; the same in case the user is prompted for info, and we don't
7518     ;; want the window conf to change before that...
7519     (gnus-summary-exit t)
7520     (while (not entered)
7521       ;; Then we find what group we are supposed to enter.
7522       (set-buffer gnus-group-buffer)
7523       (gnus-group-jump-to-group current-group)
7524       (setq target-group
7525             (or target-group
7526                 (if (eq gnus-keep-same-level 'best)
7527                     (gnus-summary-best-group gnus-newsgroup-name)
7528                   (gnus-summary-search-group backward gnus-keep-same-level))))
7529       (if (not target-group)
7530           ;; There are no further groups, so we return to the group
7531           ;; buffer.
7532           (progn
7533             (gnus-message 5 "Returning to the group buffer")
7534             (setq entered t)
7535             (when (gnus-buffer-live-p current-buffer)
7536               (set-buffer current-buffer)
7537               (gnus-summary-exit))
7538             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7539         ;; We try to enter the target group.
7540         (gnus-group-jump-to-group target-group)
7541         (let ((unreads (gnus-group-group-unread)))
7542           (if (and (or (eq t unreads)
7543                        (and unreads (not (zerop unreads))))
7544                    (gnus-summary-read-group
7545                     target-group nil no-article
7546                     (and (buffer-name current-buffer) current-buffer)
7547                     nil backward))
7548               (setq entered t)
7549             (setq current-group target-group
7550                   target-group nil)))))))
7551
7552 (defun gnus-summary-prev-group (&optional no-article)
7553   "Exit current newsgroup and then select previous unread newsgroup.
7554 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7555   (interactive "P")
7556   (gnus-summary-next-group no-article nil t))
7557
7558 ;; Walking around summary lines.
7559
7560 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7561   "Go to the first subject satisfying any non-nil constraint.
7562 If UNREAD is non-nil, the article should be unread.
7563 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7564 If UNSEEN is non-nil, the article should be unseen as well as unread.
7565 Returns the article selected or nil if there are no matching articles."
7566   (interactive "P")
7567   (cond
7568    ;; Empty summary.
7569    ((null gnus-newsgroup-data)
7570     (gnus-message 3 "No articles in the group")
7571     nil)
7572    ;; Pick the first article.
7573    ((not (or unread undownloaded unseen))
7574     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7575     (gnus-data-number (car gnus-newsgroup-data)))
7576    ;; Find the first unread article.
7577    (t
7578     (let ((data gnus-newsgroup-data))
7579       (while (and data
7580                   (let ((num (gnus-data-number (car data))))
7581                     (or (memq num gnus-newsgroup-unfetched)
7582                         (not (or (and unread
7583                                       (memq num gnus-newsgroup-unreads))
7584                                  (and undownloaded
7585                                       (memq num gnus-newsgroup-undownloaded))
7586                                  (and unseen
7587                                       (memq num gnus-newsgroup-unseen)
7588                                       (memq num gnus-newsgroup-unreads)))))))
7589         (setq data (cdr data)))
7590       (prog1
7591           (if data
7592               (progn
7593                 (goto-char (gnus-data-pos (car data)))
7594                 (gnus-data-number (car data)))
7595             (gnus-message 3 "No more%s articles"
7596                           (let* ((r (when unread " unread"))
7597                                  (d (when undownloaded " undownloaded"))
7598                                  (s (when unseen " unseen"))
7599                                  (l (delq nil (list r d s))))
7600                             (cond ((= 3 (length l))
7601                                    (concat r "," d ", or" s))
7602                                   ((= 2 (length l))
7603                                    (concat (car l) ", or" (cadr l)))
7604                                   ((= 1 (length l))
7605                                    (car l))
7606                                   (t
7607                                    ""))))
7608             nil
7609             )
7610         (gnus-summary-position-point))))))
7611
7612 (defun gnus-summary-next-subject (n &optional unread dont-display)
7613   "Go to next N'th summary line.
7614 If N is negative, go to the previous N'th subject line.
7615 If UNREAD is non-nil, only unread articles are selected.
7616 The difference between N and the actual number of steps taken is
7617 returned."
7618   (interactive "p")
7619   (let ((backward (< n 0))
7620         (n (abs n)))
7621     (while (and (> n 0)
7622                 (if backward
7623                     (gnus-summary-find-prev unread)
7624                   (gnus-summary-find-next unread)))
7625       (unless (zerop (setq n (1- n)))
7626         (gnus-summary-show-thread)))
7627     (when (/= 0 n)
7628       (gnus-message 7 "No more%s articles"
7629                     (if unread " unread" "")))
7630     (unless dont-display
7631       (gnus-summary-recenter)
7632       (gnus-summary-position-point))
7633     n))
7634
7635 (defun gnus-summary-next-unread-subject (n)
7636   "Go to next N'th unread summary line."
7637   (interactive "p")
7638   (gnus-summary-next-subject n t))
7639
7640 (defun gnus-summary-prev-subject (n &optional unread)
7641   "Go to previous N'th summary line.
7642 If optional argument UNREAD is non-nil, only unread article is selected."
7643   (interactive "p")
7644   (gnus-summary-next-subject (- n) unread))
7645
7646 (defun gnus-summary-prev-unread-subject (n)
7647   "Go to previous N'th unread summary line."
7648   (interactive "p")
7649   (gnus-summary-next-subject (- n) t))
7650
7651 (defun gnus-summary-goto-subjects (articles)
7652   "Insert the subject header for ARTICLES in the current buffer."
7653   (save-excursion
7654     (dolist (article articles)
7655       (gnus-summary-goto-subject article t)))
7656   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7657   (gnus-summary-position-point))
7658
7659 (defun gnus-summary-goto-subject (article &optional force silent)
7660   "Go to the subject line of ARTICLE.
7661 If FORCE, also allow jumping to articles not currently shown."
7662   (interactive "nArticle number: ")
7663   (unless (numberp article)
7664     (error "Article %s is not a number" article))
7665   (let ((b (point))
7666         (data (gnus-data-find article)))
7667     ;; We read in the article if we have to.
7668     (and (not data)
7669          force
7670          (gnus-summary-insert-subject
7671           article
7672           (if (or (numberp force) (vectorp force)) force)
7673           t)
7674          (setq data (gnus-data-find article)))
7675     (goto-char b)
7676     (if (not data)
7677         (progn
7678           (unless silent
7679             (gnus-message 3 "Can't find article %d" article))
7680           nil)
7681       (let ((pt (gnus-data-pos data)))
7682         (goto-char pt)
7683         (gnus-summary-set-article-display-arrow pt))
7684       (gnus-summary-position-point)
7685       article)))
7686
7687 ;; Walking around summary lines with displaying articles.
7688
7689 (defun gnus-summary-expand-window (&optional arg)
7690   "Make the summary buffer take up the entire Emacs frame.
7691 Given a prefix, will force an `article' buffer configuration."
7692   (interactive "P")
7693   (if arg
7694       (gnus-configure-windows 'article 'force)
7695     (gnus-configure-windows 'summary 'force)))
7696
7697 (defun gnus-summary-display-article (article &optional all-header)
7698   "Display ARTICLE in article buffer."
7699   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7700                (with-current-buffer gnus-article-buffer
7701                  (eq major-mode 'gnus-article-mode)))
7702     (gnus-article-setup-buffer))
7703   (gnus-set-global-variables)
7704   (with-current-buffer gnus-article-buffer
7705     (setq gnus-article-charset gnus-newsgroup-charset)
7706     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7707     (mm-enable-multibyte))
7708   (if (null article)
7709       nil
7710     (prog1
7711         (if gnus-summary-display-article-function
7712             (funcall gnus-summary-display-article-function article all-header)
7713           (gnus-article-prepare article all-header))
7714       (gnus-run-hooks 'gnus-select-article-hook)
7715       (when (and gnus-current-article
7716                  (not (zerop gnus-current-article)))
7717         (gnus-summary-goto-subject gnus-current-article))
7718       (gnus-summary-recenter)
7719       (when (and gnus-use-trees gnus-show-threads)
7720         (gnus-possibly-generate-tree article)
7721         (gnus-highlight-selected-tree article))
7722       ;; Successfully display article.
7723       (gnus-article-set-window-start
7724        (cdr (assq article gnus-newsgroup-bookmarks))))))
7725
7726 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7727   "Select the current article.
7728 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7729 non-nil, the article will be re-fetched even if it already present in
7730 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7731 be displayed."
7732   ;; Make sure we are in the summary buffer to work around bbdb bug.
7733   (unless (eq major-mode 'gnus-summary-mode)
7734     (set-buffer gnus-summary-buffer))
7735   (let ((article (or article (gnus-summary-article-number)))
7736         (all-headers (not (not all-headers))) ;Must be t or nil.
7737         gnus-summary-display-article-function)
7738     (and (not pseudo)
7739          (gnus-summary-article-pseudo-p article)
7740          (error "This is a pseudo-article"))
7741     (with-current-buffer gnus-summary-buffer
7742       (if (or (and gnus-single-article-buffer
7743                    (or (null gnus-current-article)
7744                        (null gnus-article-current)
7745                        (null (get-buffer gnus-article-buffer))
7746                        (not (eq article (cdr gnus-article-current)))
7747                        (not (equal (car gnus-article-current)
7748                                    gnus-newsgroup-name))
7749                        (not (get-buffer gnus-original-article-buffer))))
7750               (and (not gnus-single-article-buffer)
7751                    (or (null gnus-current-article)
7752                        (not (get-buffer gnus-original-article-buffer))
7753                        (not (eq gnus-current-article article))))
7754               force)
7755           ;; The requested article is different from the current article.
7756           (progn
7757             (gnus-summary-display-article article all-headers)
7758             (when (gnus-buffer-live-p gnus-article-buffer)
7759               (with-current-buffer gnus-article-buffer
7760                 (if (not gnus-article-decoded-p) ;; a local variable
7761                     (mm-disable-multibyte))))
7762             (gnus-article-set-window-start
7763              (cdr (assq article gnus-newsgroup-bookmarks)))
7764             article)
7765         'old))))
7766
7767 (defun gnus-summary-force-verify-and-decrypt ()
7768   "Display buttons for signed/encrypted parts and verify/decrypt them."
7769   (interactive)
7770   (let ((mm-verify-option 'known)
7771         (mm-decrypt-option 'known)
7772         (gnus-article-emulate-mime t)
7773         (gnus-buttonized-mime-types (append (list "multipart/signed"
7774                                                   "multipart/encrypted")
7775                                             gnus-buttonized-mime-types)))
7776     (gnus-summary-select-article nil 'force)))
7777
7778 (defun gnus-summary-next-article (&optional unread subject backward push)
7779   "Select the next article.
7780 If UNREAD, only unread articles are selected.
7781 If SUBJECT, only articles with SUBJECT are selected.
7782 If BACKWARD, the previous article is selected instead of the next."
7783   (interactive "P")
7784   ;; Make sure we are in the summary buffer.
7785   (unless (eq major-mode 'gnus-summary-mode)
7786     (set-buffer gnus-summary-buffer))
7787   (cond
7788    ;; Is there such an article?
7789    ((and (gnus-summary-search-forward unread subject backward)
7790          (or (gnus-summary-display-article (gnus-summary-article-number))
7791              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7792     (gnus-summary-position-point))
7793    ;; If not, we try the first unread, if that is wanted.
7794    ((and subject
7795          gnus-auto-select-same
7796          (gnus-summary-first-unread-article))
7797     (gnus-summary-position-point)
7798     (gnus-message 6 "Wrapped"))
7799    ;; Try to get next/previous article not displayed in this group.
7800    ((and gnus-auto-extend-newsgroup
7801          (not unread) (not subject))
7802     (gnus-summary-goto-article
7803      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7804      nil (count-lines (point-min) (point))))
7805    ;; Go to next/previous group.
7806    (t
7807     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7808       (gnus-summary-jump-to-group gnus-newsgroup-name))
7809     (let ((cmd (if (featurep 'xemacs)
7810                    last-command-char
7811                  last-command-event))
7812           (point
7813            (with-current-buffer gnus-group-buffer
7814              (point)))
7815           (current-summary (current-buffer))
7816           (group
7817            (if (eq gnus-keep-same-level 'best)
7818                (gnus-summary-best-group gnus-newsgroup-name)
7819              (gnus-summary-search-group backward gnus-keep-same-level))))
7820       ;; Select next unread newsgroup automagically.
7821       (cond
7822        ((or (not gnus-auto-select-next)
7823             (not cmd))
7824         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7825        ((or (eq gnus-auto-select-next 'quietly)
7826             (and (eq gnus-auto-select-next 'slightly-quietly)
7827                  push)
7828             (and (eq gnus-auto-select-next 'almost-quietly)
7829                  (gnus-summary-last-article-p)))
7830         ;; Select quietly.
7831         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7832             (gnus-summary-exit)
7833           (gnus-message 7 "No more%s articles (%s)..."
7834                         (if unread " unread" "")
7835                         (if group (concat "selecting " group)
7836                           "exiting"))
7837           (gnus-summary-next-group nil group backward)))
7838        (t
7839         (when (gnus-key-press-event-p last-input-event)
7840           ;; Somehow or other, we may now have selected a different
7841           ;; window.  Make point go back to the summary buffer.
7842           (when (eq current-summary (current-buffer))
7843             ;; FIXME: This burps when get-buffer-window returns nil.
7844             (select-window (get-buffer-window current-summary 0)))
7845           (gnus-summary-walk-group-buffer
7846            gnus-newsgroup-name cmd unread backward point))))))))
7847
7848 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7849   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7850                       (?\C-p (gnus-group-prev-unread-group 1))))
7851         (cursor-in-echo-area t)
7852         keve key group ended prompt)
7853     (with-current-buffer gnus-group-buffer
7854       (goto-char start)
7855       (setq group
7856             (if (eq gnus-keep-same-level 'best)
7857                 (gnus-summary-best-group gnus-newsgroup-name)
7858               (gnus-summary-search-group backward gnus-keep-same-level))))
7859     (while (not ended)
7860       (setq prompt
7861             (format
7862              "No more%s articles%s " (if unread " unread" "")
7863              (if (and group
7864                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7865                  (format " (Type %s for %s [%s])"
7866                          (single-key-description cmd)
7867                          (gnus-group-decoded-name group)
7868                          (gnus-group-unread group))
7869                (format " (Type %s to exit %s)"
7870                        (single-key-description cmd)
7871                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7872       ;; Confirm auto selection.
7873       (setq key (car (setq keve (gnus-read-event-char prompt)))
7874             ended t)
7875       (cond
7876        ((assq key keystrokes)
7877         (let ((obuf (current-buffer)))
7878           (switch-to-buffer gnus-group-buffer)
7879           (when group
7880             (gnus-group-jump-to-group group))
7881           (eval (cadr (assq key keystrokes)))
7882           (setq group (gnus-group-group-name))
7883           (switch-to-buffer obuf))
7884         (setq ended nil))
7885        ((equal key cmd)
7886         (if (or (not group)
7887                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7888             (gnus-summary-exit)
7889           (gnus-summary-next-group nil group backward)))
7890        (t
7891         (push (cdr keve) unread-command-events))))))
7892
7893 (defun gnus-summary-next-unread-article ()
7894   "Select unread article after current one."
7895   (interactive)
7896   (gnus-summary-next-article
7897    (or (not (eq gnus-summary-goto-unread 'never))
7898        (gnus-summary-last-article-p (gnus-summary-article-number)))
7899    (and gnus-auto-select-same
7900         (gnus-summary-article-subject))))
7901
7902 (defun gnus-summary-prev-article (&optional unread subject)
7903   "Select the article before the current one.
7904 If UNREAD is non-nil, only unread articles are selected."
7905   (interactive "P")
7906   (gnus-summary-next-article unread subject t))
7907
7908 (defun gnus-summary-prev-unread-article ()
7909   "Select unread article before current one."
7910   (interactive)
7911   (gnus-summary-prev-article
7912    (or (not (eq gnus-summary-goto-unread 'never))
7913        (gnus-summary-first-article-p (gnus-summary-article-number)))
7914    (and gnus-auto-select-same
7915         (gnus-summary-article-subject))))
7916
7917 (declare-function gnus-article-only-boring-p "gnus-art" ())
7918
7919 (defun gnus-summary-next-page (&optional lines circular stop)
7920   "Show next page of the selected article.
7921 If at the end of the current article, select the next article.
7922 LINES says how many lines should be scrolled up.
7923
7924 If CIRCULAR is non-nil, go to the start of the article instead of
7925 selecting the next article when reaching the end of the current
7926 article.
7927
7928 If STOP is non-nil, just stop when reaching the end of the message.
7929
7930 Also see the variable `gnus-article-skip-boring'."
7931   (interactive "P")
7932   (gnus-set-global-variables)
7933   (let ((article (gnus-summary-article-number))
7934         (article-window (get-buffer-window gnus-article-buffer t))
7935         endp)
7936     ;; If the buffer is empty, we have no article.
7937     (unless article
7938       (error "No article to select"))
7939     (gnus-configure-windows 'article)
7940     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7941         (if (and (eq gnus-summary-goto-unread 'never)
7942                  (not (gnus-summary-last-article-p article)))
7943             (gnus-summary-next-article)
7944           (gnus-summary-next-unread-article))
7945       (if (or (null gnus-current-article)
7946               (null gnus-article-current)
7947               (/= article (cdr gnus-article-current))
7948               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7949           ;; Selected subject is different from current article's.
7950           (gnus-summary-display-article article)
7951         (when article-window
7952           (gnus-eval-in-buffer-window gnus-article-buffer
7953             (setq endp (or (gnus-article-next-page lines)
7954                            (gnus-article-only-boring-p))))
7955           (when endp
7956             (cond ((or stop gnus-summary-stop-at-end-of-message)
7957                    (gnus-message 3 "End of message"))
7958                   (circular
7959                    (gnus-summary-beginning-of-article))
7960                   (lines
7961                    (gnus-message 3 "End of message"))
7962                   ((null lines)
7963                    (if (and (eq gnus-summary-goto-unread 'never)
7964                             (not (gnus-summary-last-article-p article)))
7965                        (gnus-summary-next-article)
7966                      (gnus-summary-next-unread-article))))))))
7967     (gnus-summary-recenter)
7968     (gnus-summary-position-point)))
7969
7970 (defun gnus-summary-prev-page (&optional lines move)
7971   "Show previous page of selected article.
7972 Argument LINES specifies lines to be scrolled down.
7973 If MOVE, move to the previous unread article if point is at
7974 the beginning of the buffer."
7975   (interactive "P")
7976   (let ((article (gnus-summary-article-number))
7977         (article-window (get-buffer-window gnus-article-buffer t))
7978         endp)
7979     (gnus-configure-windows 'article)
7980     (if (or (null gnus-current-article)
7981             (null gnus-article-current)
7982             (/= article (cdr gnus-article-current))
7983             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7984         ;; Selected subject is different from current article's.
7985         (gnus-summary-display-article article)
7986       (gnus-summary-recenter)
7987       (when article-window
7988         (gnus-eval-in-buffer-window gnus-article-buffer
7989           (setq endp (gnus-article-prev-page lines)))
7990         (when (and move endp)
7991           (cond (lines
7992                  (gnus-message 3 "Beginning of message"))
7993                 ((null lines)
7994                  (if (and (eq gnus-summary-goto-unread 'never)
7995                           (not (gnus-summary-first-article-p article)))
7996                      (gnus-summary-prev-article)
7997                    (gnus-summary-prev-unread-article))))))))
7998   (gnus-summary-position-point))
7999
8000 (defun gnus-summary-prev-page-or-article (&optional lines)
8001   "Show previous page of selected article.
8002 Argument LINES specifies lines to be scrolled down.
8003 If at the beginning of the article, go to the next article."
8004   (interactive "P")
8005   (gnus-summary-prev-page lines t))
8006
8007 (defun gnus-summary-scroll-up (lines)
8008   "Scroll up (or down) one line current article.
8009 Argument LINES specifies lines to be scrolled up (or down if negative).
8010 If no article is selected, then the current article will be selected first."
8011   (interactive "p")
8012   (gnus-configure-windows 'article)
8013   (gnus-summary-show-thread)
8014   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
8015     (gnus-eval-in-buffer-window gnus-article-buffer
8016       (cond ((> lines 0)
8017              (when (gnus-article-next-page lines)
8018                (gnus-message 3 "End of message")))
8019             ((< lines 0)
8020              (gnus-article-prev-page (- lines))))))
8021   (gnus-summary-recenter)
8022   (gnus-summary-position-point))
8023
8024 (defun gnus-summary-scroll-down (lines)
8025   "Scroll down (or up) one line current article.
8026 Argument LINES specifies lines to be scrolled down (or up if negative).
8027 If no article is selected, then the current article will be selected first."
8028   (interactive "p")
8029   (gnus-summary-scroll-up (- lines)))
8030
8031 (defun gnus-summary-next-same-subject ()
8032   "Select next article which has the same subject as current one."
8033   (interactive)
8034   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8035
8036 (defun gnus-summary-prev-same-subject ()
8037   "Select previous article which has the same subject as current one."
8038   (interactive)
8039   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8040
8041 (defun gnus-summary-next-unread-same-subject ()
8042   "Select next unread article which has the same subject as current one."
8043   (interactive)
8044   (gnus-summary-next-article t (gnus-summary-article-subject)))
8045
8046 (defun gnus-summary-prev-unread-same-subject ()
8047   "Select previous unread article which has the same subject as current one."
8048   (interactive)
8049   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8050
8051 (defun gnus-summary-first-unread-article ()
8052   "Select the first unread article.
8053 Return nil if there are no unread articles."
8054   (interactive)
8055   (prog1
8056       (when (gnus-summary-first-subject t)
8057         (gnus-summary-show-thread)
8058         (gnus-summary-first-subject t)
8059         (gnus-summary-display-article (gnus-summary-article-number)))
8060     (gnus-summary-position-point)))
8061
8062 (defun gnus-summary-first-unread-subject ()
8063   "Place the point on the subject line of the first unread article.
8064 Return nil if there are no unread articles."
8065   (interactive)
8066   (prog1
8067       (when (gnus-summary-first-subject t)
8068         (gnus-summary-show-thread)
8069         (gnus-summary-first-subject t))
8070     (gnus-summary-position-point)))
8071
8072 (defun gnus-summary-first-unseen-subject ()
8073   "Place the point on the subject line of the first unseen article.
8074 Return nil if there are no unseen articles."
8075   (interactive)
8076   (prog1
8077       (when (gnus-summary-first-subject nil nil t)
8078         (gnus-summary-show-thread)
8079         (gnus-summary-first-subject nil nil t))
8080     (gnus-summary-position-point)))
8081
8082 (defun gnus-summary-first-unseen-or-unread-subject ()
8083   "Place the point on the subject line of the first unseen and unread article.
8084 If all article have been seen, on the subject line of the first unread
8085 article."
8086   (interactive)
8087   (prog1
8088       (unless (when (gnus-summary-first-subject nil nil t)
8089                 (gnus-summary-show-thread)
8090                 (gnus-summary-first-subject nil nil t))
8091         (when (gnus-summary-first-subject t)
8092           (gnus-summary-show-thread)
8093           (gnus-summary-first-subject t)))
8094     (gnus-summary-position-point)))
8095
8096 (defun gnus-summary-first-article ()
8097   "Select the first article.
8098 Return nil if there are no articles."
8099   (interactive)
8100   (prog1
8101       (when (gnus-summary-first-subject)
8102         (gnus-summary-show-thread)
8103         (gnus-summary-first-subject)
8104         (gnus-summary-display-article (gnus-summary-article-number)))
8105     (gnus-summary-position-point)))
8106
8107 (defun gnus-summary-best-unread-article (&optional arg)
8108   "Select the unread article with the highest score.
8109 If given a prefix argument, select the next unread article that has a
8110 score higher than the default score."
8111   (interactive "P")
8112   (let ((article (if arg
8113                      (gnus-summary-better-unread-subject)
8114                    (gnus-summary-best-unread-subject))))
8115     (if article
8116         (gnus-summary-goto-article article)
8117       (error "No unread articles"))))
8118
8119 (defun gnus-summary-best-unread-subject ()
8120   "Select the unread subject with the highest score."
8121   (interactive)
8122   (let ((best -1000000)
8123         (data gnus-newsgroup-data)
8124         article score)
8125     (while data
8126       (and (gnus-data-unread-p (car data))
8127            (> (setq score
8128                     (gnus-summary-article-score (gnus-data-number (car data))))
8129               best)
8130            (setq best score
8131                  article (gnus-data-number (car data))))
8132       (setq data (cdr data)))
8133     (when article
8134       (gnus-summary-goto-subject article))
8135     (gnus-summary-position-point)
8136     article))
8137
8138 (defun gnus-summary-better-unread-subject ()
8139   "Select the first unread subject that has a score over the default score."
8140   (interactive)
8141   (let ((data gnus-newsgroup-data)
8142         article score)
8143     (while (and (setq article (gnus-data-number (car data)))
8144                 (or (gnus-data-read-p (car data))
8145                     (not (> (gnus-summary-article-score article)
8146                             gnus-summary-default-score))))
8147       (setq data (cdr data)))
8148     (when article
8149       (gnus-summary-goto-subject article))
8150     (gnus-summary-position-point)
8151     article))
8152
8153 (defun gnus-summary-last-subject ()
8154   "Go to the last displayed subject line in the group."
8155   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8156     (when article
8157       (gnus-summary-goto-subject article))))
8158
8159 (defun gnus-summary-goto-article (article &optional all-headers force)
8160   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8161 If ALL-HEADERS is non-nil, no header lines are hidden.
8162 If FORCE, go to the article even if it isn't displayed.  If FORCE
8163 is a number, it is the line the article is to be displayed on."
8164   (interactive
8165    (list
8166     (gnus-completing-read
8167      "Article number or Message-ID"
8168      (mapcar 'int-to-string gnus-newsgroup-limit))
8169     current-prefix-arg
8170     t))
8171   (prog1
8172       (if (and (stringp article)
8173                (string-match "@\\|%40" article))
8174           (gnus-summary-refer-article article)
8175         (when (stringp article)
8176           (setq article (string-to-number article)))
8177         (if (gnus-summary-goto-subject article force)
8178             (gnus-summary-display-article article all-headers)
8179           (gnus-message 4 "Couldn't go to article %s" article) nil))
8180     (gnus-summary-position-point)))
8181
8182 (defun gnus-summary-goto-last-article ()
8183   "Go to the previously read article."
8184   (interactive)
8185   (prog1
8186       (when gnus-last-article
8187         (gnus-summary-goto-article gnus-last-article nil t))
8188     (gnus-summary-position-point)))
8189
8190 (defun gnus-summary-pop-article (number)
8191   "Pop one article off the history and go to the previous.
8192 NUMBER articles will be popped off."
8193   (interactive "p")
8194   (let (to)
8195     (setq gnus-newsgroup-history
8196           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8197     (if to
8198         (gnus-summary-goto-article (car to) nil t)
8199       (error "Article history empty")))
8200   (gnus-summary-position-point))
8201
8202 ;; Summary commands and functions for limiting the summary buffer.
8203
8204 (defun gnus-summary-limit-to-articles (n)
8205   "Limit the summary buffer to the next N articles.
8206 If not given a prefix, use the process marked articles instead."
8207   (interactive "P")
8208   (prog1
8209       (let ((articles (gnus-summary-work-articles n)))
8210         (setq gnus-newsgroup-processable nil)
8211         (gnus-summary-limit articles))
8212     (gnus-summary-position-point)))
8213
8214 (defun gnus-summary-pop-limit (&optional total)
8215   "Restore the previous limit.
8216 If given a prefix, remove all limits."
8217   (interactive "P")
8218   (when total
8219     (setq gnus-newsgroup-limits
8220           (list (mapcar (lambda (h) (mail-header-number h))
8221                         gnus-newsgroup-headers))))
8222   (unless gnus-newsgroup-limits
8223     (error "No limit to pop"))
8224   (prog1
8225       (gnus-summary-limit nil 'pop)
8226     (gnus-summary-position-point)))
8227
8228 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8229   "Limit the summary buffer to articles that have subjects that match a regexp.
8230 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8231   (interactive
8232    (list (read-string (if current-prefix-arg
8233                           "Exclude subject (regexp): "
8234                         "Limit to subject (regexp): "))
8235          nil current-prefix-arg))
8236   (unless header
8237     (setq header "subject"))
8238   (when (not (equal "" subject))
8239     (prog1
8240         (let ((articles (gnus-summary-find-matching
8241                          (or header "subject") subject 'all nil nil
8242                          not-matching)))
8243           (unless articles
8244             (error "Found no matches for \"%s\"" subject))
8245           (gnus-summary-limit articles))
8246       (gnus-summary-position-point))))
8247
8248 (defun gnus-summary-limit-to-author (from &optional not-matching)
8249   "Limit the summary buffer to articles that have authors that match a regexp.
8250 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8251   (interactive
8252    (list (let* ((header (gnus-summary-article-header))
8253                 (default (and header (car (mail-header-parse-address
8254                                            (mail-header-from header))))))
8255            (read-string (concat (if current-prefix-arg
8256                                     "Exclude author (regexp"
8257                                   "Limit to author (regexp")
8258                                 (if default
8259                                     (concat ", default \"" default "\"): ")
8260                                   "): "))
8261                         nil nil
8262                         default))
8263          current-prefix-arg))
8264   (gnus-summary-limit-to-subject from "from" not-matching))
8265
8266 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8267   "Limit the summary buffer to articles with the given RECIPIENT.
8268
8269 If NOT-MATCHING, exclude RECIPIENT.
8270
8271 To and Cc headers are checked.  You need to include them in
8272 `nnmail-extra-headers'."
8273   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8274   (interactive
8275    (list (read-string (format "%s recipient (regexp): "
8276                               (if current-prefix-arg "Exclude" "Limit to")))
8277          current-prefix-arg))
8278   (when (not (equal "" recipient))
8279     (prog1 (let* ((to
8280                    (if (memq 'To nnmail-extra-headers)
8281                        (gnus-summary-find-matching
8282                         (cons 'extra 'To) recipient 'all nil nil
8283                         not-matching)
8284                      (gnus-message
8285                       1 "`To' isn't present in `nnmail-extra-headers'")
8286                      (sit-for 1)
8287                      nil))
8288                   (cc
8289                    (if (memq 'Cc nnmail-extra-headers)
8290                        (gnus-summary-find-matching
8291                         (cons 'extra 'Cc) recipient 'all nil nil
8292                         not-matching)
8293                      (gnus-message
8294                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8295                      (sit-for 1)
8296                      nil))
8297                   (articles
8298                    (if not-matching
8299                        ;; We need the numbers that are in both lists:
8300                        (mapcar (lambda (a)
8301                                  (and (memq a to) a))
8302                                cc)
8303                      (nconc to cc))))
8304              (unless articles
8305                (error "Found no matches for \"%s\"" recipient))
8306              (gnus-summary-limit articles))
8307       (gnus-summary-position-point))))
8308
8309 (defun gnus-summary-limit-to-address (address &optional not-matching)
8310   "Limit the summary buffer to articles with the given ADDRESS.
8311
8312 If NOT-MATCHING, exclude ADDRESS.
8313
8314 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8315 in `nnmail-extra-headers'."
8316   (interactive
8317    (list (read-string (format "%s address (regexp): "
8318                               (if current-prefix-arg "Exclude" "Limit to")))
8319          current-prefix-arg))
8320   (when (not (equal "" address))
8321     (prog1 (let* ((to
8322                    (if (memq 'To nnmail-extra-headers)
8323                        (gnus-summary-find-matching
8324                         (cons 'extra 'To) address 'all nil nil
8325                         not-matching)
8326                      (gnus-message
8327                       1 "`To' isn't present in `nnmail-extra-headers'")
8328                      (sit-for 1)
8329                      t))
8330                   (cc
8331                    (if (memq 'Cc nnmail-extra-headers)
8332                        (gnus-summary-find-matching
8333                         (cons 'extra 'Cc) address 'all nil nil
8334                         not-matching)
8335                      (gnus-message
8336                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8337                      (sit-for 1)
8338                      t))
8339                   (from
8340                    (gnus-summary-find-matching "from" address
8341                                                'all nil nil not-matching))
8342                   (articles
8343                    (if not-matching
8344                        ;; We need the numbers that are in all lists:
8345                        (if (eq cc t)
8346                            (if (eq to t)
8347                                from
8348                              (mapcar (lambda (a) (car (memq a from))) to))
8349                          (if (eq to t)
8350                              (mapcar (lambda (a) (car (memq a from))) cc)
8351                            (mapcar (lambda (a) (car (memq a from)))
8352                                    (mapcar (lambda (a) (car (memq a to)))
8353                                            cc))))
8354                      (nconc (if (eq to t) nil to)
8355                             (if (eq cc t) nil cc)
8356                             from))))
8357              (unless articles
8358                (error "Found no matches for \"%s\"" address))
8359              (gnus-summary-limit articles))
8360       (gnus-summary-position-point))))
8361
8362 (defun gnus-summary-limit-strange-charsets-predicate (header)
8363   (when (fboundp 'char-charset)
8364     (let ((string (concat (mail-header-subject header)
8365                           (mail-header-from header)))
8366           charset found)
8367       (dotimes (i (1- (length string)))
8368         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8369         (when (string-match "unicode\\|big\\|japanese" charset)
8370           (setq found t)))
8371       found)))
8372
8373 (defun gnus-summary-limit-to-predicate (predicate)
8374   "Limit to articles where PREDICATE returns non-nil.
8375 PREDICATE will be called with the header structures of the
8376 articles."
8377   (let ((articles nil)
8378         (case-fold-search t))
8379     (dolist (header gnus-newsgroup-headers)
8380       (when (funcall predicate header)
8381         (push (mail-header-number header) articles)))
8382     (gnus-summary-limit (nreverse articles))))
8383
8384 (defun gnus-summary-limit-to-age (age &optional younger-p)
8385   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8386 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8387 articles that are younger than AGE days."
8388   (interactive
8389    (let ((younger current-prefix-arg)
8390          (days-got nil)
8391          days)
8392      (while (not days-got)
8393        (setq days (if younger
8394                       (read-string "Limit to articles younger than (in days, older when negative): ")
8395                     (read-string
8396                      "Limit to articles older than (in days, younger when negative): ")))
8397        (when (> (length days) 0)
8398          (setq days (read days)))
8399        (if (numberp days)
8400            (progn
8401              (setq days-got t)
8402              (when (< days 0)
8403                (setq younger (not younger))
8404                (setq days (* days -1))))
8405          (message "Please enter a number.")
8406          (sleep-for 1)))
8407      (list days younger)))
8408   (prog1
8409       (let ((data gnus-newsgroup-data)
8410             (cutoff (days-to-time age))
8411             articles d date is-younger)
8412         (while (setq d (pop data))
8413           (when (and (vectorp (gnus-data-header d))
8414                      (setq date (mail-header-date (gnus-data-header d))))
8415             (setq is-younger (time-less-p
8416                               (time-since (gnus-date-get-time date))
8417                               cutoff))
8418             (when (if younger-p
8419                       is-younger
8420                     (not is-younger))
8421               (push (gnus-data-number d) articles))))
8422         (gnus-summary-limit (nreverse articles)))
8423     (gnus-summary-position-point)))
8424
8425 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8426   "Limit the summary buffer to articles that match an 'extra' header."
8427   (interactive
8428    (let ((header
8429           (intern
8430            (gnus-completing-read
8431             (if current-prefix-arg
8432                 "Exclude extra header"
8433               "Limit extra header")
8434             (mapcar 'symbol-name gnus-extra-headers)
8435             t nil nil
8436             (symbol-name (car gnus-extra-headers))))))
8437      (list header
8438            (read-string (format "%s header %s (regexp): "
8439                                 (if current-prefix-arg "Exclude" "Limit to")
8440                                 header))
8441            current-prefix-arg)))
8442   (when (not (equal "" regexp))
8443     (prog1
8444         (let ((articles (gnus-summary-find-matching
8445                          (cons 'extra header) regexp 'all nil nil
8446                          not-matching)))
8447           (unless articles
8448             (error "Found no matches for \"%s\"" regexp))
8449           (gnus-summary-limit articles))
8450       (gnus-summary-position-point))))
8451
8452 (defun gnus-summary-limit-to-display-predicate ()
8453   "Limit the summary buffer to the predicated in the `display' group parameter."
8454   (interactive)
8455   (unless gnus-newsgroup-display
8456     (error "There is no `display' group parameter"))
8457   (let (articles)
8458     (dolist (gnus-number gnus-newsgroup-articles)
8459       (when (funcall gnus-newsgroup-display)
8460         (push gnus-number articles)))
8461     (gnus-summary-limit articles))
8462   (gnus-summary-position-point))
8463
8464 (defun gnus-summary-limit-to-unread (&optional all)
8465   "Limit the summary buffer to articles that are not marked as read.
8466 If ALL is non-nil, limit strictly to unread articles."
8467   (interactive "P")
8468   (if all
8469       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8470     (gnus-summary-limit-to-marks
8471      ;; Concat all the marks that say that an article is read and have
8472      ;; those removed.
8473      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8474            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8475            gnus-low-score-mark gnus-expirable-mark
8476            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8477            gnus-duplicate-mark)
8478      'reverse)))
8479
8480 (defun gnus-summary-limit-to-headers (match &optional reverse)
8481   "Limit the summary buffer to articles that have headers that match MATCH.
8482 If REVERSE (the prefix), limit to articles that don't match."
8483   (interactive "sMatch headers (regexp): \nP")
8484   (gnus-summary-limit-to-bodies match reverse t))
8485
8486 (declare-function article-goto-body "gnus-art" ())
8487
8488 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8489   "Limit the summary buffer to articles that have bodies that match MATCH.
8490 If REVERSE (the prefix), limit to articles that don't match."
8491   (interactive "sMatch body (regexp): \nP")
8492   (let ((articles nil)
8493         (gnus-select-article-hook nil)  ;Disable hook.
8494         (gnus-article-prepare-hook nil)
8495         (gnus-use-article-prefetch nil)
8496         (gnus-keep-backlog nil)
8497         (gnus-break-pages nil)
8498         (gnus-summary-display-arrow nil)
8499         (gnus-updated-mode-lines nil)
8500         (gnus-auto-center-summary nil)
8501         (gnus-display-mime-function nil))
8502     (dolist (data gnus-newsgroup-data)
8503       (let (gnus-mark-article-hook)
8504         (gnus-summary-select-article t t nil (gnus-data-number data)))
8505       (with-current-buffer gnus-article-buffer
8506         (article-goto-body)
8507         (let* ((case-fold-search t)
8508                (found (if headersp
8509                           (re-search-backward match nil t)
8510                         (re-search-forward match nil t))))
8511           (when (or (and found
8512                          (not reverse))
8513                     (and (not found)
8514                          reverse))
8515             (push (gnus-data-number data) articles)))))
8516     (if (not articles)
8517         (message "No messages matched")
8518       (gnus-summary-limit articles)))
8519   (gnus-summary-position-point))
8520
8521 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8522   "Limit the summary buffer to articles that aren't part on any thread.
8523 If THREADSP (the prefix), limit to articles that are in threads."
8524   (interactive "P")
8525   (let ((articles nil)
8526         thread-articles
8527         threads)
8528     (dolist (thread gnus-newsgroup-threads)
8529       (if (stringp (car thread))
8530           (dolist (thread (cdr thread))
8531             (push thread threads))
8532         (push thread threads)))
8533     (dolist (thread threads)
8534       (setq thread-articles (gnus-articles-in-thread thread))
8535       (when (or (and threadsp
8536                      (> (length thread-articles) 1))
8537                 (and (not threadsp)
8538                      (= (length thread-articles) 1)))
8539         (setq articles (nconc thread-articles articles))))
8540     (if (not articles)
8541         (message "No messages matched")
8542       (gnus-summary-limit articles))
8543     (gnus-summary-position-point)))
8544
8545 (defun gnus-summary-limit-to-replied (&optional unreplied)
8546   "Limit the summary buffer to replied articles.
8547 If UNREPLIED (the prefix), limit to unreplied articles."
8548   (interactive "P")
8549   (if unreplied
8550       (gnus-summary-limit
8551        (gnus-set-difference gnus-newsgroup-articles
8552         gnus-newsgroup-replied))
8553     (gnus-summary-limit gnus-newsgroup-replied))
8554   (gnus-summary-position-point))
8555
8556 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8557   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8558 If REVERSE, limit the summary buffer to articles that are marked
8559 with MARKS.  MARKS can either be a string of marks or a list of marks.
8560 Returns how many articles were removed."
8561   (interactive "sMarks: ")
8562   (gnus-summary-limit-to-marks marks t))
8563
8564 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8565   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8566 If REVERSE (the prefix), limit the summary buffer to articles that are
8567 not marked with MARKS.  MARKS can either be a string of marks or a
8568 list of marks.
8569 Returns how many articles were removed."
8570   (interactive "sMarks: \nP")
8571   (prog1
8572       (let ((data gnus-newsgroup-data)
8573             (marks (if (listp marks) marks
8574                      (append marks nil))) ; Transform to list.
8575             articles)
8576         (while data
8577           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8578                   (memq (gnus-data-mark (car data)) marks))
8579             (push (gnus-data-number (car data)) articles))
8580           (setq data (cdr data)))
8581         (gnus-summary-limit articles))
8582     (gnus-summary-position-point)))
8583
8584 (defun gnus-summary-limit-to-score (score)
8585   "Limit to articles with score at or above SCORE."
8586   (interactive "NLimit to articles with score of at least: ")
8587   (let ((data gnus-newsgroup-data)
8588         articles)
8589     (while data
8590       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8591                 score)
8592         (push (gnus-data-number (car data)) articles))
8593       (setq data (cdr data)))
8594     (prog1
8595         (gnus-summary-limit articles)
8596       (gnus-summary-position-point))))
8597
8598 (defun gnus-summary-limit-to-unseen ()
8599   "Limit to unseen articles."
8600   (interactive)
8601   (prog1
8602       (gnus-summary-limit gnus-newsgroup-unseen)
8603     (gnus-summary-position-point)))
8604
8605 (defun gnus-summary-limit-include-thread (id)
8606   "Display all the hidden articles that is in the thread with ID in it.
8607 When called interactively, ID is the Message-ID of the current
8608 article."
8609   (interactive (list (mail-header-id (gnus-summary-article-header))))
8610   (let ((articles (gnus-articles-in-thread
8611                    (gnus-id-to-thread (gnus-root-id id))))
8612         ;;we REALLY want the whole thread---this prevents cut-threads
8613         ;;from removing the thread we want to include.
8614         (gnus-fetch-old-headers nil)
8615         (gnus-build-sparse-threads nil))
8616     (prog1
8617         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8618       (gnus-summary-limit-include-matching-articles
8619        "subject"
8620        (regexp-quote (gnus-simplify-subject-re
8621                       (mail-header-subject (gnus-id-to-header id)))))
8622       (gnus-summary-position-point))))
8623
8624 (defun gnus-summary-limit-include-matching-articles (header regexp)
8625   "Display all the hidden articles that have HEADERs that match REGEXP."
8626   (interactive (list (read-string "Match on header: ")
8627                      (read-string "Regexp: ")))
8628   (let ((articles (gnus-find-matching-articles header regexp)))
8629     (prog1
8630         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8631       (gnus-summary-position-point))))
8632
8633 (defun gnus-summary-insert-dormant-articles ()
8634   "Insert all the dormant articles for this group into the current buffer."
8635   (interactive)
8636   (let ((gnus-verbose (max 6 gnus-verbose)))
8637     (if (not gnus-newsgroup-dormant)
8638         (gnus-message 3 "No dormant articles for this group")
8639       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8640
8641 (defun gnus-summary-insert-ticked-articles ()
8642   "Insert ticked articles for this group into the current buffer."
8643   (interactive)
8644   (let ((gnus-verbose (max 6 gnus-verbose)))
8645     (if (not gnus-newsgroup-marked)
8646         (gnus-message 3 "No ticked articles for this group")
8647       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8648
8649 (defun gnus-summary-limit-include-dormant ()
8650   "Display all the hidden articles that are marked as dormant.
8651 Note that this command only works on a subset of the articles currently
8652 fetched for this group."
8653   (interactive)
8654   (unless gnus-newsgroup-dormant
8655     (error "There are no dormant articles in this group"))
8656   (prog1
8657       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8658     (gnus-summary-position-point)))
8659
8660 (defun gnus-summary-include-articles (articles)
8661   "Fetch the headers for ARTICLES and then display the summary lines."
8662   (let ((gnus-inhibit-demon t)
8663         (gnus-agent nil)
8664         (gnus-read-all-available-headers t))
8665     (setq gnus-newsgroup-headers
8666           (gnus-merge
8667            'list gnus-newsgroup-headers
8668            (gnus-fetch-headers articles nil t)
8669            'gnus-article-sort-by-number))
8670     (setq gnus-newsgroup-articles
8671           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8672     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8673
8674 (defun gnus-summary-limit-exclude-dormant ()
8675   "Hide all dormant articles."
8676   (interactive)
8677   (prog1
8678       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8679     (gnus-summary-position-point)))
8680
8681 (defun gnus-summary-limit-exclude-childless-dormant ()
8682   "Hide all dormant articles that have no children."
8683   (interactive)
8684   (let ((data (gnus-data-list t))
8685         articles d children)
8686     ;; Find all articles that are either not dormant or have
8687     ;; children.
8688     (while (setq d (pop data))
8689       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8690                 (and (setq children
8691                            (gnus-article-children (gnus-data-number d)))
8692                      (let (found)
8693                        (while children
8694                          (when (memq (car children) articles)
8695                            (setq children nil
8696                                  found t))
8697                          (pop children))
8698                        found)))
8699         (push (gnus-data-number d) articles)))
8700     ;; Do the limiting.
8701     (prog1
8702         (gnus-summary-limit articles)
8703       (gnus-summary-position-point))))
8704
8705 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8706   "Mark all unread excluded articles as read.
8707 If ALL, mark even excluded ticked and dormants as read."
8708   (interactive "P")
8709   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8710   (let ((articles (gnus-sorted-ndifference
8711                    (sort
8712                     (mapcar (lambda (h) (mail-header-number h))
8713                             gnus-newsgroup-headers)
8714                     '<)
8715                    gnus-newsgroup-limit))
8716         article)
8717     (setq gnus-newsgroup-unreads
8718           (gnus-sorted-intersection gnus-newsgroup-unreads
8719                                     gnus-newsgroup-limit))
8720     (if all
8721         (setq gnus-newsgroup-dormant nil
8722               gnus-newsgroup-marked nil
8723               gnus-newsgroup-reads
8724               (nconc
8725                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8726                gnus-newsgroup-reads))
8727       (while (setq article (pop articles))
8728         (unless (or (memq article gnus-newsgroup-dormant)
8729                     (memq article gnus-newsgroup-marked))
8730           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8731
8732 (defun gnus-summary-limit (articles &optional pop)
8733   (if pop
8734       ;; We pop the previous limit off the stack and use that.
8735       (setq articles (car gnus-newsgroup-limits)
8736             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8737     ;; We use the new limit, so we push the old limit on the stack.
8738     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8739   ;; Set the limit.
8740   (setq gnus-newsgroup-limit articles)
8741   (let ((total (length gnus-newsgroup-data))
8742         (data (gnus-data-find-list (gnus-summary-article-number)))
8743         (gnus-summary-mark-below nil)   ; Inhibit this.
8744         found)
8745     ;; This will do all the work of generating the new summary buffer
8746     ;; according to the new limit.
8747     (gnus-summary-prepare)
8748     ;; Hide any threads, possibly.
8749     (gnus-summary-maybe-hide-threads)
8750     ;; Try to return to the article you were at, or one in the
8751     ;; neighborhood.
8752     (when data
8753       ;; We try to find some article after the current one.
8754       (while data
8755         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8756           (setq data nil
8757                 found t))
8758         (setq data (cdr data))))
8759     (unless found
8760       ;; If there is no data, that means that we were after the last
8761       ;; article.  The same goes when we can't find any articles
8762       ;; after the current one.
8763       (goto-char (point-max))
8764       (gnus-summary-find-prev))
8765     (gnus-set-mode-line 'summary)
8766     ;; We return how many articles were removed from the summary
8767     ;; buffer as a result of the new limit.
8768     (- total (length gnus-newsgroup-data))))
8769
8770 (defsubst gnus-invisible-cut-children (threads)
8771   (let ((num 0))
8772     (while threads
8773       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8774         (incf num))
8775       (pop threads))
8776     (< num 2)))
8777
8778 (defsubst gnus-cut-thread (thread)
8779   "Go forwards in the thread until we find an article that we want to display."
8780   (when (or (eq gnus-fetch-old-headers 'some)
8781             (eq gnus-fetch-old-headers 'invisible)
8782             (numberp gnus-fetch-old-headers)
8783             (eq gnus-build-sparse-threads 'some)
8784             (eq gnus-build-sparse-threads 'more))
8785     ;; Deal with old-fetched headers and sparse threads.
8786     (while (and
8787             thread
8788             (or
8789              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8790              (gnus-summary-article-ancient-p
8791               (mail-header-number (car thread))))
8792             (if (or (<= (length (cdr thread)) 1)
8793                     (eq gnus-fetch-old-headers 'invisible))
8794                 (setq gnus-newsgroup-limit
8795                       (delq (mail-header-number (car thread))
8796                             gnus-newsgroup-limit)
8797                       thread (cadr thread))
8798               (when (gnus-invisible-cut-children (cdr thread))
8799                 (let ((th (cdr thread)))
8800                   (while th
8801                     (if (memq (mail-header-number (caar th))
8802                               gnus-newsgroup-limit)
8803                         (setq thread (car th)
8804                               th nil)
8805                       (setq th (cdr th))))))))))
8806   thread)
8807
8808 (defun gnus-cut-threads (threads)
8809   "Cut off all uninteresting articles from the beginning of THREADS."
8810   (when (or (eq gnus-fetch-old-headers 'some)
8811             (eq gnus-fetch-old-headers 'invisible)
8812             (numberp gnus-fetch-old-headers)
8813             (eq gnus-build-sparse-threads 'some)
8814             (eq gnus-build-sparse-threads 'more))
8815     (let ((th threads))
8816       (while th
8817         (setcar th (gnus-cut-thread (car th)))
8818         (setq th (cdr th)))))
8819   ;; Remove nixed out threads.
8820   (delq nil threads))
8821
8822 (defun gnus-summary-initial-limit (&optional show-if-empty)
8823   "Figure out what the initial limit is supposed to be on group entry.
8824 This entails weeding out unwanted dormants, low-scored articles,
8825 fetch-old-headers verbiage, and so on."
8826   ;; Most groups have nothing to remove.
8827   (unless (or gnus-inhibit-limiting
8828               (and (null gnus-newsgroup-dormant)
8829                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8830                    (not (eq gnus-fetch-old-headers 'some))
8831                    (not (numberp gnus-fetch-old-headers))
8832                    (not (eq gnus-fetch-old-headers 'invisible))
8833                    (null gnus-summary-expunge-below)
8834                    (not (eq gnus-build-sparse-threads 'some))
8835                    (not (eq gnus-build-sparse-threads 'more))
8836                    (null gnus-thread-expunge-below)))
8837     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8838     (setq gnus-newsgroup-limit nil)
8839     (mapatoms
8840      (lambda (node)
8841        (unless (car (symbol-value node))
8842          ;; These threads have no parents -- they are roots.
8843          (let ((nodes (cdr (symbol-value node)))
8844                thread)
8845            (while nodes
8846              (if (and gnus-thread-expunge-below
8847                       (< (gnus-thread-total-score (car nodes))
8848                          gnus-thread-expunge-below))
8849                  (gnus-expunge-thread (pop nodes))
8850                (setq thread (pop nodes))
8851                (gnus-summary-limit-children thread))))))
8852      gnus-newsgroup-dependencies)
8853     ;; If this limitation resulted in an empty group, we might
8854     ;; pop the previous limit and use it instead.
8855     (when (and (not gnus-newsgroup-limit)
8856                show-if-empty)
8857       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8858     gnus-newsgroup-limit))
8859
8860 (defun gnus-summary-limit-children (thread)
8861   "Return 1 if this subthread is visible and 0 if it is not."
8862   ;; First we get the number of visible children to this thread.  This
8863   ;; is done by recursing down the thread using this function, so this
8864   ;; will really go down to a leaf article first, before slowly
8865   ;; working its way up towards the root.
8866   (when thread
8867     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8868            (children
8869            (if (cdr thread)
8870                (apply '+ (mapcar 'gnus-summary-limit-children
8871                                  (cdr thread)))
8872              0))
8873            (number (mail-header-number (car thread)))
8874            score)
8875       (if (and
8876            (not (memq number gnus-newsgroup-marked))
8877            (or
8878             ;; If this article is dormant and has absolutely no visible
8879             ;; children, then this article isn't visible.
8880             (and (memq number gnus-newsgroup-dormant)
8881                  (zerop children))
8882             ;; If this is "fetch-old-headered" and there is no
8883             ;; visible children, then we don't want this article.
8884             (and (or (eq gnus-fetch-old-headers 'some)
8885                      (numberp gnus-fetch-old-headers))
8886                  (gnus-summary-article-ancient-p number)
8887                  (zerop children))
8888             ;; If this is "fetch-old-headered" and `invisible', then
8889             ;; we don't want this article.
8890             (and (eq gnus-fetch-old-headers 'invisible)
8891                  (gnus-summary-article-ancient-p number))
8892             ;; If this is a sparsely inserted article with no children,
8893             ;; we don't want it.
8894             (and (eq gnus-build-sparse-threads 'some)
8895                  (gnus-summary-article-sparse-p number)
8896                  (zerop children))
8897             ;; If we use expunging, and this article is really
8898             ;; low-scored, then we don't want this article.
8899             (when (and gnus-summary-expunge-below
8900                        (< (setq score
8901                                 (or (cdr (assq number gnus-newsgroup-scored))
8902                                     gnus-summary-default-score))
8903                           gnus-summary-expunge-below))
8904               ;; We increase the expunge-tally here, but that has
8905               ;; nothing to do with the limits, really.
8906               (incf gnus-newsgroup-expunged-tally)
8907               ;; We also mark as read here, if that's wanted.
8908               (when (and gnus-summary-mark-below
8909                          (< score gnus-summary-mark-below))
8910                 (setq gnus-newsgroup-unreads
8911                       (delq number gnus-newsgroup-unreads))
8912                 (if gnus-newsgroup-auto-expire
8913                     (push number gnus-newsgroup-expirable)
8914                   (push (cons number gnus-low-score-mark)
8915                         gnus-newsgroup-reads)))
8916               t)
8917             ;; Do the `display' group parameter.
8918             (and gnus-newsgroup-display
8919                  (let ((gnus-number number))
8920                    (not (funcall gnus-newsgroup-display))))))
8921           ;; Nope, invisible article.
8922           0
8923         ;; Ok, this article is to be visible, so we add it to the limit
8924         ;; and return 1.
8925         (push number gnus-newsgroup-limit)
8926         1))))
8927
8928 (defun gnus-expunge-thread (thread)
8929   "Mark all articles in THREAD as read."
8930   (let* ((number (mail-header-number (car thread))))
8931     (incf gnus-newsgroup-expunged-tally)
8932     ;; We also mark as read here, if that's wanted.
8933     (setq gnus-newsgroup-unreads
8934           (delq number gnus-newsgroup-unreads))
8935     (if gnus-newsgroup-auto-expire
8936         (push number gnus-newsgroup-expirable)
8937       (push (cons number gnus-low-score-mark)
8938             gnus-newsgroup-reads)))
8939   ;; Go recursively through all subthreads.
8940   (mapcar 'gnus-expunge-thread (cdr thread)))
8941
8942 ;; Summary article oriented commands
8943
8944 (defun gnus-summary-refer-parent-article (n)
8945   "Refer parent article N times.
8946 If N is negative, go to ancestor -N instead.
8947 The difference between N and the number of articles fetched is returned."
8948   (interactive "p")
8949   (let ((skip 1)
8950         error header ref)
8951     (when (not (natnump n))
8952       (setq skip (abs n)
8953             n 1))
8954     (while (and (> n 0)
8955                 (not error))
8956       (setq header (gnus-summary-article-header))
8957       (if (and (eq (mail-header-number header)
8958                    (cdr gnus-article-current))
8959                (equal gnus-newsgroup-name
8960                       (car gnus-article-current)))
8961           ;; If we try to find the parent of the currently
8962           ;; displayed article, then we take a look at the actual
8963           ;; References header, since this is slightly more
8964           ;; reliable than the References field we got from the
8965           ;; server.
8966           (with-current-buffer gnus-original-article-buffer
8967             (nnheader-narrow-to-headers)
8968             (unless (setq ref (message-fetch-field "references"))
8969               (when (setq ref (message-fetch-field "in-reply-to"))
8970                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8971             (widen))
8972         (setq ref
8973               ;; It's not the current article, so we take a bet on
8974               ;; the value we got from the server.
8975               (mail-header-references header)))
8976       (if (and ref
8977                (not (equal ref "")))
8978           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8979             (gnus-message 1 "Couldn't find parent"))
8980         (gnus-message 1 "No references in article %d"
8981                       (gnus-summary-article-number))
8982         (setq error t))
8983       (decf n))
8984     (gnus-summary-position-point)
8985     n))
8986
8987 (defun gnus-summary-refer-references ()
8988   "Fetch all articles mentioned in the References header.
8989 Return the number of articles fetched."
8990   (interactive)
8991   (let ((ref (mail-header-references (gnus-summary-article-header)))
8992         (current (gnus-summary-article-number))
8993         (n 0))
8994     (if (or (not ref)
8995             (equal ref ""))
8996         (error "No References in the current article")
8997       ;; For each Message-ID in the References header...
8998       (while (string-match "<[^>]*>" ref)
8999         (incf n)
9000         ;; ... fetch that article.
9001         (gnus-summary-refer-article
9002          (prog1 (match-string 0 ref)
9003            (setq ref (substring ref (match-end 0))))))
9004       (gnus-summary-goto-subject current)
9005       (gnus-summary-position-point)
9006       n)))
9007
9008 (defun gnus-delete-duplicate-headers (headers)
9009   ;; First remove leading duplicates.
9010   (while (and (> (length headers) 1)
9011               (= (mail-header-number (car headers))
9012                  (mail-header-number (cadr headers))))
9013     (pop headers))
9014   ;; Then the rest.
9015   (let ((result headers))
9016     (while (> (length headers) 1)
9017       (if (= (mail-header-number (car headers))
9018              (mail-header-number (cadr headers)))
9019           (setcdr headers (cddr headers))
9020         (pop headers)))
9021     result))
9022
9023 (defun gnus-summary-refer-thread (&optional limit)
9024   "Fetch all articles in the current thread. For backends that
9025 know how to search for threads (currently only 'nnimap) a
9026 non-numeric prefix arg will use nnir to search the entire
9027 server; without a prefix arg only the current group is
9028 searched. If the variable `gnus-refer-thread-use-nnir' is
9029 non-nil the prefix arg has the reverse meaning. If no
9030 backend-specific 'request-thread function is available fetch
9031 LIMIT (the numerical prefix) old headers. If LIMIT is
9032 non-numeric or nil fetch the number specified by the
9033 `gnus-refer-thread-limit' variable."
9034   (interactive "P")
9035   (gnus-warp-to-article)
9036   (let* ((header (gnus-summary-article-header))
9037          (id (mail-header-id header))
9038          (gnus-inhibit-demon t)
9039          (gnus-summary-ignore-duplicates t)
9040          (gnus-read-all-available-headers t)
9041          (gnus-refer-thread-use-nnir
9042           (if (and (not (null limit)) (listp limit))
9043               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9044          (new-headers
9045           (if (gnus-check-backend-function
9046                'request-thread gnus-newsgroup-name)
9047               (gnus-request-thread header gnus-newsgroup-name)
9048             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9049                             gnus-refer-thread-limit))
9050                    (last (if (numberp limit)
9051                              (min (+ (mail-header-number header)
9052                                      limit)
9053                                   gnus-newsgroup-highest)
9054                            gnus-newsgroup-highest))
9055                    (subject (gnus-simplify-subject
9056                              (mail-header-subject header)))
9057                    (refs (split-string (or (mail-header-references header)
9058                                            "")))
9059                    (gnus-parse-headers-hook
9060                     `(lambda () (goto-char (point-min))
9061                       (keep-lines
9062                        (regexp-opt ',(append refs (list id subject)))))))
9063               (gnus-fetch-headers (list last) (if (numberp limit)
9064                                                   (* 2 limit) limit) t))))
9065          article-ids)
9066     (when (listp new-headers)
9067       (dolist (header new-headers)
9068         (push (mail-header-number header) article-ids)
9069         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9070           (push (mail-header-number header) gnus-newsgroup-unreads)
9071           (setq gnus-newsgroup-unselected
9072                 (delete (mail-header-number header)
9073                         gnus-newsgroup-unselected))))
9074       (setq gnus-newsgroup-headers
9075             (gnus-delete-duplicate-headers
9076              (gnus-merge
9077               'list gnus-newsgroup-headers new-headers
9078               'gnus-article-sort-by-number)))
9079       (setq gnus-newsgroup-articles
9080             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9081       (gnus-summary-limit-include-thread id)))
9082   (gnus-summary-show-thread))
9083
9084 (defun gnus-summary-refer-article (message-id)
9085   "Fetch an article specified by MESSAGE-ID."
9086   (interactive "sMessage-ID: ")
9087   (gnus-warp-to-article)
9088   (when (and (stringp message-id)
9089              (not (zerop (length message-id))))
9090     (setq message-id (gnus-replace-in-string message-id " " ""))
9091     ;; Construct the correct Message-ID if necessary.
9092     ;; Suggested by tale@pawl.rpi.edu.
9093     (unless (string-match "^<" message-id)
9094       (setq message-id (concat "<" message-id)))
9095     (unless (string-match ">$" message-id)
9096       (setq message-id (concat message-id ">")))
9097     ;; People often post MIDs from URLs, so unhex it:
9098     (unless (string-match "@" message-id)
9099       (setq message-id (gnus-url-unhex-string message-id)))
9100     (let* ((header (gnus-id-to-header message-id))
9101            (sparse (and header
9102                         (gnus-summary-article-sparse-p
9103                          (mail-header-number header))
9104                         (memq (mail-header-number header)
9105                               gnus-newsgroup-limit)))
9106            number)
9107       (cond
9108        ;; If the article is present in the buffer we just go to it.
9109        ((and header
9110              (or (not (gnus-summary-article-sparse-p
9111                        (mail-header-number header)))
9112                  sparse))
9113         (prog1
9114             (gnus-summary-goto-article
9115              (mail-header-number header) nil t)
9116           (when sparse
9117             (gnus-summary-update-article (mail-header-number header)))))
9118        (t
9119         ;; We fetch the article.
9120         (catch 'found
9121           (dolist (gnus-override-method (gnus-refer-article-methods))
9122             (when (and (gnus-check-server gnus-override-method)
9123                        ;; Fetch the header,
9124                        (setq number (gnus-summary-insert-subject message-id)))
9125               ;; and display the article.
9126               (gnus-summary-select-article nil nil nil number)
9127               (throw 'found t)))
9128           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9129
9130 (defun gnus-refer-article-methods ()
9131   "Return a list of referable methods."
9132   (cond
9133    ;; No method, so we default to current and native.
9134    ((null gnus-refer-article-method)
9135     (list gnus-current-select-method gnus-select-method))
9136    ;; Current.
9137    ((eq 'current gnus-refer-article-method)
9138     (list gnus-current-select-method))
9139    ;; List of select methods.
9140    ((not (and (symbolp (car gnus-refer-article-method))
9141               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9142     (let (out)
9143       (dolist (method gnus-refer-article-method)
9144         (push (if (eq 'current method)
9145                   gnus-current-select-method
9146                 (if (eq 'nnir (car method))
9147                     (list
9148                      'nnir
9149                      (or (cadr method)
9150                          (gnus-method-to-server gnus-current-select-method)))
9151                   method))
9152               out))
9153       (nreverse out)))
9154    ;; One single select method.
9155    (t
9156     (list gnus-refer-article-method))))
9157
9158 (defun gnus-summary-edit-parameters ()
9159   "Edit the group parameters of the current group."
9160   (interactive)
9161   (gnus-group-edit-group gnus-newsgroup-name 'params))
9162
9163 (defun gnus-summary-customize-parameters ()
9164   "Customize the group parameters of the current group."
9165   (interactive)
9166   (gnus-group-customize gnus-newsgroup-name))
9167
9168 (defun gnus-summary-enter-digest-group (&optional force)
9169   "Enter an nndoc group based on the current article.
9170 If FORCE, force a digest interpretation.  If not, try to guess
9171 what the document format is.
9172
9173 To control what happens when you exit the group, see the
9174 `gnus-auto-select-on-ephemeral-exit' variable."
9175   (interactive "P")
9176   (let ((conf gnus-current-window-configuration))
9177     (save-window-excursion
9178       (save-excursion
9179         (let (gnus-article-prepare-hook
9180               gnus-display-mime-function
9181               gnus-break-pages)
9182           (gnus-summary-select-article))))
9183     (setq gnus-current-window-configuration conf)
9184     (let* ((name (format "%s-%d"
9185                          (gnus-group-prefixed-name
9186                           gnus-newsgroup-name (list 'nndoc ""))
9187                          (with-current-buffer gnus-summary-buffer
9188                            gnus-current-article)))
9189            (ogroup gnus-newsgroup-name)
9190            (params (append (gnus-info-params (gnus-get-info ogroup))
9191                            (list (cons 'to-group ogroup))
9192                            (list (cons 'parent-group ogroup))
9193                            (list (cons 'save-article-group ogroup))))
9194            (case-fold-search t)
9195            (buf (current-buffer))
9196            dig to-address charset)
9197       (with-current-buffer gnus-original-article-buffer
9198         ;; Have the digest group inherit the main mail address of
9199         ;; the parent article.
9200         (when (setq to-address (or (gnus-fetch-field "reply-to")
9201                                    (gnus-fetch-field "from")))
9202           (setq params
9203                 (append
9204                  (list (cons 'to-address
9205                              (funcall gnus-decode-encoded-address-function
9206                                       to-address))))))
9207         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9208         (insert-buffer-substring gnus-original-article-buffer)
9209         (narrow-to-region
9210          (goto-char (point-min))
9211          (or (search-forward "\n\n" nil t) (point)))
9212         ;; Remove lines that may lead nndoc to misinterpret the
9213         ;; document type.
9214         (goto-char (point-min))
9215         (delete-matching-lines "^Path:\\|^From ")
9216         ;; Parse charset, and decode content transfer encoding.
9217         (setq charset (mail-content-type-get
9218                        (mail-header-parse-content-type
9219                         (or (gnus-fetch-field "content-type") ""))
9220                        'charset))
9221         (let ((encoding (gnus-fetch-field "content-transfer-encoding")))
9222           (when encoding
9223             (message-remove-header "content-transfer-encoding")
9224             (goto-char (point-max))
9225             (widen)
9226             (narrow-to-region (point) (point-max))
9227             (mm-decode-content-transfer-encoding
9228              (intern (downcase (mail-header-strip encoding))))))
9229         (widen))
9230       (unwind-protect
9231           (if (let ((gnus-newsgroup-ephemeral-charset
9232                      (if charset
9233                          (intern (downcase (gnus-strip-whitespace charset)))
9234                        gnus-newsgroup-charset))
9235                     (gnus-newsgroup-ephemeral-ignored-charsets
9236                      gnus-newsgroup-ignored-charsets))
9237                 (gnus-group-read-ephemeral-group
9238                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9239                               (nndoc-article-type
9240                                ,(if force 'mbox 'guess)))
9241                  t nil nil nil
9242                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9243                                                         "ADAPT")))))
9244               ;; Make all postings to this group go to the parent group.
9245               (nconc (gnus-info-params (gnus-get-info name))
9246                      params)
9247             ;; Couldn't select this doc group.
9248             (switch-to-buffer buf)
9249             (gnus-set-global-variables)
9250             (gnus-configure-windows 'summary)
9251             (gnus-message 3 "Article couldn't be entered?"))
9252         (kill-buffer dig)))))
9253
9254 (defun gnus-summary-read-document (n)
9255   "Open a new group based on the current article(s).
9256 This will allow you to read digests and other similar
9257 documents as newsgroups.
9258 Obeys the standard process/prefix convention."
9259   (interactive "P")
9260   (let* ((ogroup gnus-newsgroup-name)
9261          (params (append (gnus-info-params (gnus-get-info ogroup))
9262                          (list (cons 'to-group ogroup))))
9263          group egroup groups vgroup)
9264     (dolist (article (gnus-summary-work-articles n))
9265       (setq group (format "%s-%d" gnus-newsgroup-name article))
9266       (gnus-summary-remove-process-mark article)
9267       (when (gnus-summary-display-article article)
9268         (save-excursion ;;What for?
9269           (with-temp-buffer
9270             (insert-buffer-substring gnus-original-article-buffer)
9271             ;; Remove some headers that may lead nndoc to make
9272             ;; the wrong guess.
9273             (message-narrow-to-head)
9274             (goto-char (point-min))
9275             (delete-matching-lines "^Path:\\|^From ")
9276             (widen)
9277             (if (setq egroup
9278                       (gnus-group-read-ephemeral-group
9279                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9280                                      (nndoc-article-type guess))
9281                        t nil t))
9282                 (progn
9283                   ;; Make all postings to this group go to the parent group.
9284                   (nconc (gnus-info-params (gnus-get-info egroup))
9285                          params)
9286                   (push egroup groups))
9287               ;; Couldn't select this doc group.
9288               (gnus-error 3 "Article couldn't be entered"))))))
9289     ;; Now we have selected all the documents.
9290     (cond
9291      ((not groups)
9292       (error "None of the articles could be interpreted as documents"))
9293      ((gnus-group-read-ephemeral-group
9294        (setq vgroup (format
9295                      "nnvirtual:%s-%s" gnus-newsgroup-name
9296                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9297        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9298        t
9299        (cons (current-buffer) 'summary)))
9300      (t
9301       (error "Couldn't select virtual nndoc group")))))
9302
9303 (defun gnus-summary-widget-forward (arg)
9304   "Move point to the next field or button in the article.
9305 With optional ARG, move across that many fields."
9306   (interactive "p")
9307   (gnus-summary-select-article)
9308   (gnus-configure-windows 'article)
9309   (select-window (gnus-get-buffer-window gnus-article-buffer))
9310   (widget-forward arg))
9311
9312 (defun gnus-summary-widget-backward (arg)
9313   "Move point to the previous field or button in the article.
9314 With optional ARG, move across that many fields."
9315   (interactive "p")
9316   (gnus-summary-select-article)
9317   (gnus-configure-windows 'article)
9318   (select-window (gnus-get-buffer-window gnus-article-buffer))
9319   (unless (widget-at (point))
9320     (goto-char (point-max)))
9321   (widget-backward arg))
9322
9323 (defun gnus-summary-isearch-article (&optional regexp-p)
9324   "Do incremental search forward on the current article.
9325 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9326   (interactive "P")
9327   (gnus-summary-select-article)
9328   (gnus-configure-windows 'article)
9329   (gnus-eval-in-buffer-window gnus-article-buffer
9330     (save-restriction
9331       (widen)
9332       (isearch-forward regexp-p))))
9333
9334 (defun gnus-summary-repeat-search-article-forward ()
9335   "Repeat the previous search forwards."
9336   (interactive)
9337   (unless gnus-last-search-regexp
9338     (error "No previous search"))
9339   (gnus-summary-search-article-forward gnus-last-search-regexp))
9340
9341 (defun gnus-summary-repeat-search-article-backward ()
9342   "Repeat the previous search backwards."
9343   (interactive)
9344   (unless gnus-last-search-regexp
9345     (error "No previous search"))
9346   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9347
9348 (defun gnus-summary-search-article-forward (regexp &optional backward)
9349   "Search for an article containing REGEXP forward.
9350 If BACKWARD, search backward instead."
9351   (interactive
9352    (list (read-string
9353           (format "Search article %s (regexp%s): "
9354                   (if current-prefix-arg "backward" "forward")
9355                   (if gnus-last-search-regexp
9356                       (concat ", default " gnus-last-search-regexp)
9357                     "")))
9358          current-prefix-arg))
9359   (if (string-equal regexp "")
9360       (setq regexp (or gnus-last-search-regexp ""))
9361     (setq gnus-last-search-regexp regexp)
9362     (setq gnus-article-before-search gnus-current-article))
9363   ;; Intentionally set gnus-last-article.
9364   (setq gnus-last-article gnus-article-before-search)
9365   (let ((gnus-last-article gnus-last-article))
9366     (if (gnus-summary-search-article regexp backward)
9367         (gnus-summary-show-thread)
9368       (signal 'search-failed (list regexp)))))
9369
9370 (defun gnus-summary-search-article-backward (regexp)
9371   "Search for an article containing REGEXP backward."
9372   (interactive
9373    (list (read-string
9374           (format "Search article backward (regexp%s): "
9375                   (if gnus-last-search-regexp
9376                       (concat ", default " gnus-last-search-regexp)
9377                     "")))))
9378   (gnus-summary-search-article-forward regexp 'backward))
9379
9380 (defun gnus-summary-search-article (regexp &optional backward)
9381   "Search for an article containing REGEXP.
9382 Optional argument BACKWARD means do search for backward.
9383 `gnus-select-article-hook' is not called during the search."
9384   ;; We have to require this here to make sure that the following
9385   ;; dynamic binding isn't shadowed by autoloading.
9386   (require 'gnus-async)
9387   (require 'gnus-art)
9388   (let ((gnus-select-article-hook nil)  ;Disable hook.
9389         (gnus-article-prepare-hook nil)
9390         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9391         (gnus-use-article-prefetch nil)
9392         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9393         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9394         (gnus-visual nil)
9395         (gnus-keep-backlog nil)
9396         (gnus-break-pages nil)
9397         (gnus-summary-display-arrow nil)
9398         (gnus-updated-mode-lines nil)
9399         (gnus-auto-center-summary nil)
9400         (sum (current-buffer))
9401         (gnus-display-mime-function nil)
9402         (found nil)
9403         point)
9404     (gnus-save-hidden-threads
9405       (gnus-summary-select-article)
9406       (set-buffer gnus-article-buffer)
9407       (goto-char (window-point (get-buffer-window (current-buffer))))
9408       (when backward
9409         (forward-line -1))
9410       (while (not found)
9411         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9412         (if (if backward
9413                 (re-search-backward regexp nil t)
9414               (re-search-forward regexp nil t))
9415             ;; We found the regexp.
9416             (progn
9417               (setq found 'found)
9418               (beginning-of-line)
9419               (set-window-start
9420                (get-buffer-window (current-buffer))
9421                (point))
9422               (forward-line 1)
9423               (set-window-point
9424                (get-buffer-window (current-buffer))
9425                (point))
9426               (set-buffer sum)
9427               (setq point (point)))
9428           ;; We didn't find it, so we go to the next article.
9429           (set-buffer sum)
9430           (setq found 'not)
9431           (while (eq found 'not)
9432             (if (not (if backward (gnus-summary-find-prev)
9433                        (gnus-summary-find-next)))
9434                 ;; No more articles.
9435                 (setq found t)
9436               ;; Select the next article and adjust point.
9437               (unless (gnus-summary-article-sparse-p
9438                        (gnus-summary-article-number))
9439                 (setq found nil)
9440                 (gnus-summary-select-article)
9441                 (set-buffer gnus-article-buffer)
9442                 (widen)
9443                 (goto-char (if backward (point-max) (point-min))))))))
9444       (gnus-message 7 ""))
9445     ;; Return whether we found the regexp.
9446     (when (eq found 'found)
9447       (goto-char point)
9448       (gnus-summary-show-thread)
9449       (gnus-summary-goto-subject gnus-current-article)
9450       (gnus-summary-position-point)
9451       t)))
9452
9453 (defun gnus-find-matching-articles (header regexp)
9454   "Return a list of all articles that match REGEXP on HEADER.
9455 This search includes all articles in the current group that Gnus has
9456 fetched headers for, whether they are displayed or not."
9457   (let ((articles nil)
9458         ;; Can't eta-reduce because it's a macro.
9459         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9460         (case-fold-search t))
9461     (dolist (header gnus-newsgroup-headers)
9462       (when (string-match regexp (funcall func header))
9463         (push (mail-header-number header) articles)))
9464     (nreverse articles)))
9465
9466 (defun gnus-summary-find-matching (header regexp &optional backward unread
9467                                           not-case-fold not-matching)
9468   "Return a list of all articles that match REGEXP on HEADER.
9469 The search stars on the current article and goes forwards unless
9470 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9471 If UNREAD is non-nil, only unread articles will
9472 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9473 in the comparisons. If NOT-MATCHING, return a list of all articles that
9474 not match REGEXP on HEADER."
9475   (let ((case-fold-search (not not-case-fold))
9476         articles d func)
9477     (if (consp header)
9478         (if (eq (car header) 'extra)
9479             (setq func
9480                   `(lambda (h)
9481                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9482                          "")))
9483           (error "%s is an invalid header" header))
9484       (unless (fboundp (intern (concat "mail-header-" header)))
9485         (error "%s is not a valid header" header))
9486       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9487     (dolist (d (if (eq backward 'all)
9488                    gnus-newsgroup-data
9489                  (gnus-data-find-list
9490                   (gnus-summary-article-number)
9491                   (gnus-data-list backward))))
9492       (when (and (or (not unread)       ; We want all articles...
9493                      (gnus-data-unread-p d)) ; Or just unreads.
9494                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9495                  (if not-matching
9496                      (not (string-match
9497                            regexp
9498                            (funcall func (gnus-data-header d))))
9499                    (string-match regexp
9500                                  (funcall func (gnus-data-header d)))))
9501         (push (gnus-data-number d) articles))) ; Success!
9502     (nreverse articles)))
9503
9504 (defun gnus-summary-execute-command (header regexp command &optional backward)
9505   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9506 If HEADER is an empty string (or nil), the match is done on the entire
9507 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9508   (interactive
9509    (list (let ((completion-ignore-case t))
9510            (gnus-completing-read
9511             "Header name"
9512             (mapcar 'symbol-name
9513                     (append
9514                      '(Number Subject From Lines Date
9515                        Message-ID Xref References Body)
9516                      gnus-extra-headers))
9517             'require-match))
9518          (read-string "Regexp: ")
9519          (read-key-sequence "Command: ")
9520          current-prefix-arg))
9521   (when (equal header "Body")
9522     (setq header ""))
9523   ;; Hidden thread subtrees must be searched as well.
9524   (gnus-summary-show-all-threads)
9525   ;; We don't want to change current point nor window configuration.
9526   (save-excursion
9527     (save-window-excursion
9528       (let (gnus-visual
9529             gnus-treat-strip-trailing-blank-lines
9530             gnus-treat-strip-leading-blank-lines
9531             gnus-treat-strip-multiple-blank-lines
9532             gnus-treat-hide-boring-headers
9533             gnus-treat-fold-newsgroups
9534             gnus-article-prepare-hook)
9535         (gnus-message 6 "Executing %s..." (key-description command))
9536         ;; We'd like to execute COMMAND interactively so as to give arguments.
9537         (gnus-execute header regexp
9538                       `(call-interactively ',(key-binding command))
9539                       backward)
9540         (gnus-message 6 "Executing %s...done" (key-description command))))))
9541
9542 (defun gnus-summary-beginning-of-article ()
9543   "Scroll the article back to the beginning."
9544   (interactive)
9545   (gnus-summary-select-article)
9546   (gnus-configure-windows 'article)
9547   (gnus-eval-in-buffer-window gnus-article-buffer
9548     (widen)
9549     (goto-char (point-min))
9550     (when gnus-break-pages
9551       (gnus-narrow-to-page))))
9552
9553 (defun gnus-summary-end-of-article ()
9554   "Scroll to the end of the article."
9555   (interactive)
9556   (gnus-summary-select-article)
9557   (gnus-configure-windows 'article)
9558   (gnus-eval-in-buffer-window gnus-article-buffer
9559     (widen)
9560     (goto-char (point-max))
9561     (recenter -3)
9562     (when gnus-break-pages
9563       (gnus-narrow-to-page))))
9564
9565 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9566   "Truncate to LEN and quote all \"(\"'s in STRING."
9567   (gnus-replace-in-string (if (and len (> (length string) len))
9568                               (substring string 0 len)
9569                             string)
9570                           "[()]" "\\\\\\&"))
9571
9572 (defun gnus-summary-print-article (&optional filename n)
9573   "Generate and print a PostScript image of the process-marked (mail) articles.
9574
9575 If used interactively, print the current article if none are
9576 process-marked.  With prefix arg, prompt the user for the name of the
9577 file to save in.
9578
9579 When used from Lisp, accept two optional args FILENAME and N.  N means
9580 to print the next N articles.  If N is negative, print the N previous
9581 articles.  If N is nil and articles have been marked with the process
9582 mark, print these instead.
9583
9584 If the optional first argument FILENAME is nil, send the image to the
9585 printer.  If FILENAME is a string, save the PostScript image in a file with
9586 that name.  If FILENAME is a number, prompt the user for the name of the file
9587 to save in."
9588   (interactive (list (ps-print-preprint current-prefix-arg)))
9589   (dolist (article (gnus-summary-work-articles n))
9590     (gnus-summary-select-article nil nil 'pseudo article)
9591     (gnus-eval-in-buffer-window gnus-article-buffer
9592       (gnus-print-buffer))
9593     (gnus-summary-remove-process-mark article))
9594   (ps-despool filename))
9595
9596 (defun gnus-print-buffer ()
9597   (let ((ps-left-header
9598          (list
9599           (concat "("
9600                   (gnus-summary-print-truncate-and-quote
9601                    (mail-header-subject gnus-current-headers)
9602                    66) ")")
9603           (concat "("
9604                   (gnus-summary-print-truncate-and-quote
9605                    (mail-header-from gnus-current-headers)
9606                    45) ")")))
9607         (ps-right-header
9608          (list
9609           "/pagenumberstring load"
9610           (concat "("
9611                   (mail-header-date gnus-current-headers) ")"))))
9612     (gnus-run-hooks 'gnus-ps-print-hook)
9613     (save-excursion
9614       (if ps-print-color-p
9615           (ps-spool-buffer-with-faces)
9616         (ps-spool-buffer)))))
9617
9618 (declare-function gnus-flush-original-article-buffer "gnus-art" ())
9619
9620 (defun gnus-summary-show-complete-article ()
9621   "Show a complete version of the current article.
9622 This is only useful if you're looking at a partial version of the
9623 article currently."
9624   (interactive)
9625   (let ((gnus-keep-backlog nil)
9626         (gnus-use-cache nil)
9627         (gnus-agent nil)
9628         (variable (intern
9629                    (format "%s-fetch-partial-articles"
9630                            (car (gnus-find-method-for-group
9631                                  gnus-newsgroup-name)))
9632                    obarray))
9633         old-val)
9634     (unwind-protect
9635         (progn
9636           (setq old-val (symbol-value variable))
9637           (set variable nil)
9638           (gnus-flush-original-article-buffer)
9639           (gnus-summary-show-article))
9640       (set variable old-val))))
9641
9642 (defun gnus-summary-show-article (&optional arg)
9643   "Force redisplaying of the current article.
9644 If ARG (the prefix) is a number, show the article with the charset
9645 defined in `gnus-summary-show-article-charset-alist', or the charset
9646 input.
9647 If ARG (the prefix) is non-nil and not a number, show the article,
9648 but without running any of the article treatment functions
9649 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9650 C-u g', show the raw article."
9651   (interactive "P")
9652   (cond
9653    ((numberp arg)
9654     (gnus-summary-show-article t)
9655     (let ((gnus-newsgroup-charset
9656            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9657                (mm-read-coding-system
9658                 "View as charset: " ;; actually it is coding system.
9659                 (with-current-buffer gnus-article-buffer
9660                   (mm-detect-coding-region (point) (point-max))))))
9661           (gnus-newsgroup-ignored-charsets 'gnus-all))
9662       (gnus-summary-select-article nil 'force)
9663       (let ((deps gnus-newsgroup-dependencies)
9664             head header lines)
9665         (with-current-buffer gnus-original-article-buffer
9666           (save-restriction
9667             (message-narrow-to-head)
9668             (setq head (buffer-string))
9669             (goto-char (point-min))
9670             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9671               (goto-char (point-max))
9672               (widen)
9673               (setq lines (1- (count-lines (point) (point-max))))))
9674           (with-temp-buffer
9675             (insert (format "211 %d Article retrieved.\n"
9676                             (cdr gnus-article-current)))
9677             (insert head)
9678             (if lines (insert (format "Lines: %d\n" lines)))
9679             (insert ".\n")
9680             (let ((nntp-server-buffer (current-buffer)))
9681               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9682         (gnus-data-set-header
9683          (gnus-data-find (cdr gnus-article-current))
9684          header)
9685         (gnus-summary-update-article-line
9686          (cdr gnus-article-current) header)
9687         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9688           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9689    ((not arg)
9690     ;; Select the article the normal way.
9691     (if (eq mm-text-html-renderer 'shr)
9692         (progn
9693           (require 'shr)
9694           (let ((shr-ignore-cache t))
9695             (gnus-summary-select-article nil 'force)))
9696       (gnus-summary-select-article nil 'force)))
9697    ((equal arg '(16))
9698     ;; C-u C-u g
9699     (let ((gnus-inhibit-article-treatments t))
9700       (gnus-summary-select-article nil 'force)))
9701    (t
9702     ;; We have to require this here to make sure that the following
9703     ;; dynamic binding isn't shadowed by autoloading.
9704     (require 'gnus-async)
9705     (require 'gnus-art)
9706     ;; Bind the article treatment functions to nil.
9707     (let ((gnus-have-all-headers t)
9708           gnus-article-prepare-hook
9709           gnus-article-decode-hook
9710           gnus-display-mime-function
9711           gnus-break-pages)
9712       ;; Destroy any MIME parts.
9713       (when (gnus-buffer-live-p gnus-article-buffer)
9714         (with-current-buffer gnus-article-buffer
9715           (gnus-article-stop-animations)
9716           (gnus-stop-downloads)
9717           (mm-destroy-parts gnus-article-mime-handles)
9718           ;; Set it to nil for safety reason.
9719           (setq gnus-article-mime-handle-alist nil)
9720           (setq gnus-article-mime-handles nil)))
9721       (gnus-summary-select-article nil 'force))))
9722   (gnus-summary-goto-subject gnus-current-article)
9723   (gnus-summary-position-point))
9724
9725 (defun gnus-summary-show-raw-article ()
9726   "Show the raw article without any article massaging functions being run."
9727   (interactive)
9728   (gnus-summary-show-article t))
9729
9730 (defun gnus-summary-verbose-headers (&optional arg)
9731   "Toggle permanent full header display.
9732 If ARG is a positive number, turn header display on.
9733 If ARG is a negative number, turn header display off."
9734   (interactive "P")
9735   (setq gnus-show-all-headers
9736         (cond ((or (not (numberp arg))
9737                    (zerop arg))
9738                (not gnus-show-all-headers))
9739               ((natnump arg)
9740                t)))
9741   (gnus-summary-show-article))
9742
9743 (declare-function article-narrow-to-head "gnus-art" ())
9744 (declare-function gnus-article-hidden-text-p "gnus-art" (type))
9745 (declare-function gnus-delete-wash-type "gnus-art" (type))
9746
9747 (defun gnus-summary-toggle-header (&optional arg)
9748   "Show the headers if they are hidden, or hide them if they are shown.
9749 If ARG is a positive number, show the entire header.
9750 If ARG is a negative number, hide the unwanted header lines."
9751   (interactive "P")
9752   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9753                      (get-buffer-window gnus-article-buffer t))))
9754     (with-current-buffer gnus-article-buffer
9755       (widen)
9756       (article-narrow-to-head)
9757       (let* ((inhibit-read-only t)
9758              (inhibit-point-motion-hooks t)
9759              (hidden (if (numberp arg)
9760                          (>= arg 0)
9761                        (or (not (looking-at "[^ \t\n]+:"))
9762                            (gnus-article-hidden-text-p 'headers))))
9763              s e)
9764         (delete-region (point-min) (point-max))
9765         (with-current-buffer gnus-original-article-buffer
9766           (goto-char (setq s (point-min)))
9767           (setq e (if (search-forward "\n\n" nil t)
9768                       (1- (point))
9769                     (point-max))))
9770         (insert-buffer-substring gnus-original-article-buffer s e)
9771         (run-hooks 'gnus-article-decode-hook)
9772         (if hidden
9773             (let ((gnus-treat-hide-headers nil)
9774                   (gnus-treat-hide-boring-headers nil))
9775               (gnus-delete-wash-type 'headers)
9776               (gnus-treat-article 'head))
9777           (gnus-treat-article 'head))
9778         (widen)
9779         (if window
9780             (set-window-start window (goto-char (point-min))))
9781         (if gnus-break-pages
9782             (gnus-narrow-to-page)
9783           (when (gnus-visual-p 'page-marker)
9784             (let ((inhibit-read-only t))
9785               (gnus-remove-text-with-property 'gnus-prev)
9786               (gnus-remove-text-with-property 'gnus-next))))
9787         (gnus-set-mode-line 'article)))))
9788
9789 (defun gnus-summary-show-all-headers ()
9790   "Make all header lines visible."
9791   (interactive)
9792   (gnus-summary-toggle-header 1))
9793
9794 (defun gnus-summary-caesar-message (&optional arg)
9795   "Caesar rotate the current article by 13.
9796 With a non-numerical prefix, also rotate headers.  A numerical
9797 prefix specifies how many places to rotate each letter forward."
9798   (interactive "P")
9799   (gnus-summary-select-article)
9800   (let ((mail-header-separator ""))
9801     (gnus-eval-in-buffer-window gnus-article-buffer
9802       (save-restriction
9803         (widen)
9804         (let ((start (window-start))
9805               (inhibit-read-only t))
9806           (if (equal arg '(4))
9807               (message-caesar-buffer-body nil t)
9808             (message-caesar-buffer-body arg))
9809           (set-window-start (get-buffer-window (current-buffer)) start)))))
9810   ;; Create buttons and stuff...
9811   (gnus-treat-article nil))
9812
9813 (declare-function idna-to-unicode "ext:idna" (str))
9814
9815 (defun gnus-summary-idna-message (&optional arg)
9816   "Decode IDNA encoded domain names in the current articles.
9817 IDNA encoded domain names looks like `xn--bar'.  If a string
9818 remain unencoded after running this function, it is likely an
9819 invalid IDNA string (`xn--bar' is invalid).
9820
9821 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9822 installed for this command to work."
9823   (interactive "P")
9824   (if (not (and (condition-case nil (require 'idna)
9825                   (file-error))
9826                 (mm-coding-system-p 'utf-8)
9827                 (executable-find (symbol-value 'idna-program))))
9828       (gnus-message
9829        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9830     (gnus-summary-select-article)
9831     (let ((mail-header-separator ""))
9832       (gnus-eval-in-buffer-window gnus-article-buffer
9833         (save-restriction
9834           (widen)
9835           (let ((start (window-start))
9836                 buffer-read-only)
9837             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9838               (replace-match (idna-to-unicode (match-string 1))))
9839             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9840
9841 (defun gnus-summary-morse-message (&optional arg)
9842   "Morse decode the current article."
9843   (interactive "P")
9844   (gnus-summary-select-article)
9845   (let ((mail-header-separator ""))
9846     (gnus-eval-in-buffer-window gnus-article-buffer
9847       (save-excursion
9848         (save-restriction
9849           (widen)
9850           (let ((pos (window-start))
9851                 (inhibit-read-only t))
9852             (goto-char (point-min))
9853             (when (message-goto-body)
9854               (gnus-narrow-to-body))
9855             (goto-char (point-min))
9856             (while (search-forward "·" (point-max) t)
9857               (replace-match "."))
9858             (unmorse-region (point-min) (point-max))
9859             (widen)
9860             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9861
9862 (defun gnus-summary-stop-page-breaking ()
9863   "Stop page breaking in the current article."
9864   (interactive)
9865   (gnus-summary-select-article)
9866   (gnus-eval-in-buffer-window gnus-article-buffer
9867     (widen)
9868     (when (gnus-visual-p 'page-marker)
9869       (let ((inhibit-read-only t))
9870         (gnus-remove-text-with-property 'gnus-prev)
9871         (gnus-remove-text-with-property 'gnus-next))
9872       (setq gnus-page-broken nil))))
9873
9874 (defun gnus-summary-move-article (&optional n to-newsgroup
9875                                             select-method action)
9876   "Move the current article to a different newsgroup.
9877 If N is a positive number, move the N next articles.
9878 If N is a negative number, move the N previous articles.
9879 If N is nil and any articles have been marked with the process mark,
9880 move those articles instead.
9881 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9882 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9883 re-spool using this method.
9884
9885 When called interactively with TO-NEWSGROUP being nil, the value of
9886 the variable `gnus-move-split-methods' is used for finding a default
9887 for the target newsgroup.
9888
9889 For this function to work, both the current newsgroup and the
9890 newsgroup that you want to move to have to support the `request-move'
9891 and `request-accept' functions.
9892
9893 ACTION can be either `move' (the default), `crosspost' or `copy'."
9894   (interactive "P")
9895   (unless action
9896     (setq action 'move))
9897   ;; Check whether the source group supports the required functions.
9898   (cond ((and (eq action 'move)
9899               (not (gnus-check-backend-function
9900                     'request-move-article gnus-newsgroup-name)))
9901          (error "The current group does not support article moving"))
9902         ((and (eq action 'crosspost)
9903               (not (gnus-check-backend-function
9904                     'request-replace-article gnus-newsgroup-name)))
9905          (error "The current group does not support article editing")))
9906   (let ((articles (gnus-summary-work-articles n))
9907         (prefix (if (gnus-check-backend-function
9908                      'request-move-article gnus-newsgroup-name)
9909                     (funcall gnus-move-group-prefix-function
9910                              gnus-newsgroup-name)
9911                   ""))
9912         (names '((move "Move" "Moving")
9913                  (copy "Copy" "Copying")
9914                  (crosspost "Crosspost" "Crossposting")))
9915         (copy-buf (save-excursion
9916                     (nnheader-set-temp-buffer " *copy article*")))
9917         art-group to-method new-xref article to-groups
9918         articles-to-update-marks encoded)
9919     (unless (assq action names)
9920       (error "Unknown action %s" action))
9921     ;; Read the newsgroup name.
9922     (when (and (not to-newsgroup)
9923                (not select-method))
9924       (if (and gnus-move-split-methods
9925                (not
9926                 (and (memq gnus-current-article articles)
9927                      (gnus-buffer-live-p gnus-original-article-buffer))))
9928           ;; When `gnus-move-split-methods' is non-nil, we have to
9929           ;; select an article to give `gnus-read-move-group-name' an
9930           ;; opportunity to suggest an appropriate default.  However,
9931           ;; we needn't render or mark the article.
9932           (let ((gnus-display-mime-function nil)
9933                 (gnus-article-prepare-hook nil)
9934                 (gnus-mark-article-hook nil))
9935             (gnus-summary-select-article nil nil nil (car articles))))
9936       (setq to-newsgroup (gnus-read-move-group-name
9937                           (cadr (assq action names))
9938                           (symbol-value
9939                            (intern (format "gnus-current-%s-group" action)))
9940                           articles prefix)
9941             encoded to-newsgroup
9942             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9943       (set (intern (format "gnus-current-%s-group" action))
9944            (mm-decode-coding-string
9945             to-newsgroup
9946             (gnus-group-name-charset to-method to-newsgroup))))
9947     (unless to-method
9948       (setq to-method (or select-method
9949                           (gnus-server-to-method
9950                            (gnus-group-method to-newsgroup)))))
9951     (setq to-newsgroup
9952           (or encoded
9953               (and to-newsgroup
9954                    (mm-encode-coding-string
9955                     to-newsgroup
9956                     (gnus-group-name-charset to-method to-newsgroup)))))
9957     ;; Check the method we are to move this article to...
9958     (unless (gnus-check-backend-function
9959              'request-accept-article (car to-method))
9960       (error "%s does not support article copying" (car to-method)))
9961     (unless (gnus-check-server to-method)
9962       (error "Can't open server %s" (car to-method)))
9963     (gnus-message 6 "%s to %s: %s..."
9964                   (caddr (assq action names))
9965                   (or (car select-method)
9966                       (gnus-group-decoded-name to-newsgroup))
9967                   articles)
9968     (while articles
9969       (setq article (pop articles))
9970       ;; Set any marks that may have changed in the summary buffer.
9971       (when gnus-preserve-marks
9972         (gnus-summary-push-marks-to-backend article))
9973       (setq
9974        art-group
9975        (cond
9976         ;; Move the article.
9977         ((eq action 'move)
9978          ;; Remove this article from future suppression.
9979          (gnus-dup-unsuppress-article article)
9980          (let* ((from-method (gnus-find-method-for-group
9981                               gnus-newsgroup-name))
9982                 (to-method (or select-method
9983                                (gnus-find-method-for-group to-newsgroup)))
9984                 (move-is-internal (gnus-server-equal from-method to-method)))
9985            (gnus-request-move-article
9986             article                     ; Article to move
9987             gnus-newsgroup-name         ; From newsgroup
9988             (nth 1 (gnus-find-method-for-group
9989                     gnus-newsgroup-name)) ; Server
9990             (list 'gnus-request-accept-article
9991                   to-newsgroup (list 'quote select-method)
9992                   (not articles) t)     ; Accept form
9993             (not articles)              ; Only save nov last time
9994             (and move-is-internal
9995                  to-newsgroup           ; Not respooling
9996                                         ; Is this move internal?
9997                  (gnus-group-real-name to-newsgroup)))))
9998         ;; Copy the article.
9999         ((eq action 'copy)
10000          (with-current-buffer copy-buf
10001            (when (gnus-request-article-this-buffer article
10002                                                    gnus-newsgroup-name)
10003              (save-restriction
10004                (nnheader-narrow-to-headers)
10005                (dolist (hdr gnus-copy-article-ignored-headers)
10006                  (message-remove-header hdr t)))
10007              (gnus-request-accept-article
10008               to-newsgroup select-method (not articles) t))))
10009         ;; Crosspost the article.
10010         ((eq action 'crosspost)
10011          (let ((xref (message-tokenize-header
10012                       (mail-header-xref (gnus-summary-article-header
10013                                          article))
10014                       " ")))
10015            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
10016                                   ":" (number-to-string article)))
10017            (unless xref
10018              (setq xref (list (system-name))))
10019            (setq new-xref
10020                  (concat
10021                   (mapconcat 'identity
10022                              (delete "Xref:" (delete new-xref xref))
10023                              " ")
10024                   " " new-xref))
10025            (with-current-buffer copy-buf
10026              ;; First put the article in the destination group.
10027              (gnus-request-article-this-buffer article gnus-newsgroup-name)
10028              (when (consp (setq art-group
10029                                 (gnus-request-accept-article
10030                                  to-newsgroup select-method (not articles)
10031                                  t)))
10032                (setq new-xref (concat new-xref " " (car art-group)
10033                                       ":"
10034                                       (number-to-string (cdr art-group))))
10035                ;; Now we have the new Xrefs header, so we insert
10036                ;; it and replace the new article.
10037                (nnheader-replace-header "Xref" new-xref)
10038                (gnus-request-replace-article
10039                 (cdr art-group) to-newsgroup (current-buffer) t)
10040                art-group))))))
10041       (cond
10042        ((not art-group)
10043         (gnus-message 1 "Couldn't %s article %s: %s"
10044                       (cadr (assq action names)) article
10045                       (nnheader-get-report (car to-method))))
10046        ((eq art-group 'junk)
10047         (when (eq action 'move)
10048           (gnus-summary-mark-article article gnus-canceled-mark)
10049           (gnus-message 4 "Deleted article %s" article)
10050           ;; run the delete hook
10051           (run-hook-with-args 'gnus-summary-article-delete-hook
10052                               action
10053                               (gnus-data-header
10054                                (assoc article (gnus-data-list nil)))
10055                               gnus-newsgroup-name nil
10056                               select-method)))
10057        (t
10058         (let* ((pto-group (gnus-group-prefixed-name
10059                            (car art-group) to-method))
10060                (info (gnus-get-info pto-group))
10061                (to-group (gnus-info-group info))
10062                to-marks)
10063           ;; Update the group that has been moved to.
10064           (when (and info
10065                      (memq action '(move copy)))
10066             (unless (member to-group to-groups)
10067               (push to-group to-groups))
10068
10069             (when (and (not (memq article gnus-newsgroup-unreads))
10070                        (cdr art-group))
10071               (push 'read to-marks)
10072               (gnus-info-set-read
10073                info (gnus-add-to-range (gnus-info-read info)
10074                                        (list (cdr art-group)))))
10075
10076             ;; See whether the article is to be put in the cache.
10077             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10078                    (marks (if expirable
10079                               gnus-article-mark-lists
10080                             (delete '(expirable . expire)
10081                                     (copy-sequence
10082                                      gnus-article-mark-lists))))
10083                    (to-article (cdr art-group)))
10084
10085               ;; Enter the article into the cache in the new group,
10086               ;; if that is required.
10087               (when (and to-article
10088                          gnus-use-cache)
10089                 (gnus-cache-possibly-enter-article
10090                  to-group to-article
10091                  (memq article gnus-newsgroup-marked)
10092                  (memq article gnus-newsgroup-dormant)
10093                  (memq article gnus-newsgroup-unreads)))
10094
10095               (when (and gnus-preserve-marks
10096                          to-article)
10097                 ;; Copy any marks over to the new group.
10098                 (when (and (equal to-group gnus-newsgroup-name)
10099                            (not (memq article gnus-newsgroup-unreads)))
10100                   ;; Mark this article as read in this group.
10101                   (push (cons to-article gnus-read-mark)
10102                         gnus-newsgroup-reads)
10103                   ;; Increase the active status of this group.
10104                   (setcdr (gnus-active to-group) to-article)
10105                   (setcdr gnus-newsgroup-active to-article))
10106
10107                 (while marks
10108                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10109                     (when (memq article (symbol-value
10110                                          (intern (format "gnus-newsgroup-%s"
10111                                                          (caar marks)))))
10112                       (push (cdar marks) to-marks)
10113                       ;; If the other group is the same as this group,
10114                       ;; then we have to add the mark to the list.
10115                       (when (equal to-group gnus-newsgroup-name)
10116                         (set (intern (format "gnus-newsgroup-%s"
10117                                              (caar marks)))
10118                              (cons to-article
10119                                    (symbol-value
10120                                     (intern (format "gnus-newsgroup-%s"
10121                                                     (caar marks)))))))
10122                       ;; Copy the marks to other group.
10123                       (gnus-add-marked-articles
10124                        to-group (cdar marks) (list to-article) info)))
10125                   (setq marks (cdr marks)))
10126
10127                 (when (and expirable
10128                            gnus-mark-copied-or-moved-articles-as-expirable
10129                            (not (memq 'expire to-marks)))
10130                   ;; Mark this article as expirable.
10131                   (push 'expire to-marks)
10132                   (when (equal to-group gnus-newsgroup-name)
10133                     (push to-article gnus-newsgroup-expirable))
10134                   ;; Copy the expirable mark to other group.
10135                   (gnus-add-marked-articles
10136                    to-group 'expire (list to-article) info))
10137
10138                 (when (and to-marks
10139                            (gnus-method-option-p
10140                             (gnus-find-method-for-group to-group)
10141                             'server-marks))
10142                   (gnus-request-set-mark
10143                    to-group (list (list (list to-article) 'add to-marks)))))
10144
10145               (gnus-dribble-enter
10146                (concat "(gnus-group-set-info '"
10147                        (gnus-prin1-to-string (gnus-get-info to-group))
10148                        ")")
10149                (concat "^(gnus-group-set-info '(\""
10150                        (regexp-quote to-group) "\""))))
10151
10152           ;; Update the Xref header in this article to point to
10153           ;; the new crossposted article we have just created.
10154           (when (eq action 'crosspost)
10155             (with-current-buffer copy-buf
10156               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10157               (nnheader-replace-header "Xref" new-xref)
10158               (gnus-request-replace-article
10159                article gnus-newsgroup-name (current-buffer) t)))
10160
10161           ;; run the move/copy/crosspost/respool hook
10162           (run-hook-with-args 'gnus-summary-article-move-hook
10163                               action
10164                               (gnus-data-header
10165                                (assoc article (gnus-data-list nil)))
10166                               gnus-newsgroup-name
10167                               to-newsgroup
10168                               select-method))
10169
10170         ;;;!!!Why is this necessary?
10171         (set-buffer gnus-summary-buffer)
10172
10173         (when (eq action 'move)
10174           (save-excursion
10175             (gnus-summary-goto-subject article)
10176             (gnus-summary-mark-article article gnus-canceled-mark)))))
10177       (push article articles-to-update-marks))
10178
10179     (save-excursion
10180       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10181     ;; Re-activate all groups that have been moved to.
10182     (with-current-buffer gnus-group-buffer
10183       (let ((gnus-group-marked to-groups))
10184         (gnus-group-get-new-news-this-group nil t)))
10185
10186     (gnus-kill-buffer copy-buf)
10187     (gnus-summary-position-point)
10188     (gnus-set-mode-line 'summary)))
10189
10190 (defun gnus-summary-push-marks-to-backend (article)
10191   (let ((set nil)
10192         (del nil)
10193         (marks gnus-article-mark-lists))
10194     (unless (memq article gnus-newsgroup-unreads)
10195       (push 'read set))
10196     (while marks
10197       (if (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10198                (memq article (symbol-value
10199                               (intern (format "gnus-newsgroup-%s"
10200                                               (caar marks))))))
10201           (push (cdar marks) set)
10202         (push (cdar marks) del))
10203       (pop marks))
10204     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)
10205                                                  ((,article) del ,del)))))
10206
10207 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10208   "Copy the current article to some other group.
10209 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10210 When called interactively, if TO-NEWSGROUP is nil, use the value of
10211 the variable `gnus-move-split-methods' for finding a default target
10212 newsgroup.
10213 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10214 re-spool using this method."
10215   (interactive "P")
10216   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10217
10218 (defun gnus-summary-crosspost-article (&optional n)
10219   "Crosspost the current article to some other group."
10220   (interactive "P")
10221   (gnus-summary-move-article n nil nil 'crosspost))
10222
10223 (defcustom gnus-summary-respool-default-method nil
10224   "Default method type for respooling an article.
10225 If nil, use to the current newsgroup method."
10226   :type 'symbol
10227   :group 'gnus-summary-mail)
10228
10229 (defun gnus-summary-respool-article (&optional n method)
10230   "Respool the current article.
10231 The article will be squeezed through the mail spooling process again,
10232 which means that it will be put in some mail newsgroup or other
10233 depending on `nnmail-split-methods'.
10234 If N is a positive number, respool the N next articles.
10235 If N is a negative number, respool the N previous articles.
10236 If N is nil and any articles have been marked with the process mark,
10237 respool those articles instead.
10238
10239 Respooling can be done both from mail groups and \"real\" newsgroups.
10240 In the former case, the articles in question will be moved from the
10241 current group into whatever groups they are destined to.  In the
10242 latter case, they will be copied into the relevant groups."
10243   (interactive
10244    (list current-prefix-arg
10245          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10246                 (methname
10247                  (symbol-name (or gnus-summary-respool-default-method
10248                                   (car (gnus-find-method-for-group
10249                                         gnus-newsgroup-name)))))
10250                 (method
10251                  (gnus-completing-read
10252                   "Backend to use when respooling"
10253                   methods t nil 'gnus-mail-method-history methname))
10254                 ms)
10255            (cond
10256             ((zerop (length (setq ms (gnus-servers-using-backend
10257                                       (intern method)))))
10258              (list (intern method) ""))
10259             ((= 1 (length ms))
10260              (car ms))
10261             (t
10262              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10263                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10264                            ms-alist))))))))
10265   (unless method
10266     (error "No method given for respooling"))
10267   (if (assoc (symbol-name
10268               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10269              (gnus-methods-using 'respool))
10270       (gnus-summary-move-article n nil method)
10271     (gnus-summary-copy-article n nil method)))
10272
10273 (defun gnus-summary-import-article (file &optional edit)
10274   "Import an arbitrary file into a mail newsgroup."
10275   (interactive "fImport file: \nP")
10276   (let ((group gnus-newsgroup-name)
10277         (now (current-time))
10278         atts lines group-art)
10279     (unless (gnus-check-backend-function 'request-accept-article group)
10280       (error "%s does not support article importing" group))
10281     (or (file-readable-p file)
10282         (not (file-regular-p file))
10283         (error "Can't read %s" file))
10284     (with-current-buffer (gnus-get-buffer-create " *import file*")
10285       (erase-buffer)
10286       (nnheader-insert-file-contents file)
10287       (goto-char (point-min))
10288       (if (nnheader-article-p)
10289           (save-restriction
10290             (goto-char (point-min))
10291             (search-forward "\n\n" nil t)
10292             (narrow-to-region (point-min) (1- (point)))
10293             (goto-char (point-min))
10294             (unless (re-search-forward "^date:" nil t)
10295               (goto-char (point-max))
10296               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10297        ;; This doesn't look like an article, so we fudge some headers.
10298         (setq atts (file-attributes file)
10299               lines (count-lines (point-min) (point-max)))
10300         (insert "From: " (read-string "From: ") "\n"
10301                 "Subject: " (read-string "Subject: ") "\n"
10302                 "Date: " (message-make-date (nth 5 atts)) "\n"
10303                 "Message-ID: " (message-make-message-id) "\n"
10304                 "Lines: " (int-to-string lines) "\n"
10305                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10306       (setq group-art (gnus-request-accept-article group nil t))
10307       (kill-buffer (current-buffer)))
10308     (setq gnus-newsgroup-active (gnus-activate-group group))
10309     (forward-line 1)
10310     (gnus-summary-goto-article (cdr group-art) nil t)
10311     (when edit
10312       (gnus-summary-edit-article))))
10313
10314 (defun gnus-summary-create-article ()
10315   "Create an article in a mail newsgroup."
10316   (interactive)
10317   (let ((group gnus-newsgroup-name)
10318         (now (current-time))
10319         group-art)
10320     (unless (gnus-check-backend-function 'request-accept-article group)
10321       (error "%s does not support article importing" group))
10322     (with-current-buffer (gnus-get-buffer-create " *import file*")
10323       (erase-buffer)
10324       (goto-char (point-min))
10325       ;; This doesn't look like an article, so we fudge some headers.
10326       (insert "From: " (read-string "From: ") "\n"
10327               "Subject: " (read-string "Subject: ") "\n"
10328               "Date: " (message-make-date now) "\n"
10329               "Message-ID: " (message-make-message-id) "\n")
10330       (setq group-art (gnus-request-accept-article group nil t))
10331       (kill-buffer (current-buffer)))
10332     (setq gnus-newsgroup-active (gnus-activate-group group))
10333     (forward-line 1)
10334     (gnus-summary-goto-article (cdr group-art) nil t)
10335     (gnus-summary-edit-article)))
10336
10337 (defun gnus-summary-article-posted-p ()
10338   "Say whether the current (mail) article is available from news as well.
10339 This will be the case if the article has both been mailed and posted."
10340   (interactive)
10341   (let ((id (mail-header-references (gnus-summary-article-header)))
10342         (gnus-override-method (car (gnus-refer-article-methods))))
10343     (if (gnus-request-head id "")
10344         (gnus-message 2 "The current message was found on %s"
10345                       gnus-override-method)
10346       (gnus-message 2 "The current message couldn't be found on %s"
10347                     gnus-override-method)
10348       nil)))
10349
10350 (defun gnus-summary-expire-articles (&optional now)
10351   "Expire all articles that are marked as expirable in the current group."
10352   (interactive)
10353   (when (and (not gnus-group-is-exiting-without-update-p)
10354              (gnus-check-backend-function
10355               'request-expire-articles gnus-newsgroup-name))
10356     ;; This backend supports expiry.
10357     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10358            (expirable
10359             (gnus-list-range-difference
10360              (if total
10361                  (progn
10362                    ;; We need to update the info for
10363                    ;; this group for `gnus-list-of-read-articles'
10364                    ;; to give us the right answer.
10365                    (gnus-run-hooks 'gnus-exit-group-hook)
10366                    (gnus-summary-update-info)
10367                    (gnus-list-of-read-articles gnus-newsgroup-name))
10368                (setq gnus-newsgroup-expirable
10369                      (sort gnus-newsgroup-expirable '<)))
10370              gnus-newsgroup-unexist))
10371            (expiry-wait (if now 'immediate
10372                           (gnus-group-find-parameter
10373                            gnus-newsgroup-name 'expiry-wait)))
10374            (nnmail-expiry-target
10375             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10376                 nnmail-expiry-target))
10377            es)
10378       (when expirable
10379         ;; There are expirable articles in this group, so we run them
10380         ;; through the expiry process.
10381         (gnus-message 6 "Expiring articles...")
10382         (when (gnus-check-group gnus-newsgroup-name)
10383           ;; The list of articles that weren't expired is returned.
10384           (save-excursion
10385             (if expiry-wait
10386                 (let ((nnmail-expiry-wait-function nil)
10387                       (nnmail-expiry-wait expiry-wait))
10388                   (setq es (gnus-request-expire-articles
10389                             expirable gnus-newsgroup-name)))
10390               (setq es (gnus-request-expire-articles
10391                         expirable gnus-newsgroup-name)))
10392             (unless total
10393               (setq gnus-newsgroup-expirable es))
10394             ;; We go through the old list of expirable, and mark all
10395             ;; really expired articles as nonexistent.
10396             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10397               (let ((gnus-use-cache nil))
10398                 (dolist (article expirable)
10399                   (when (and (not (memq article es))
10400                              (gnus-data-find article))
10401                     (gnus-summary-mark-article article gnus-canceled-mark)
10402                     (run-hook-with-args 'gnus-summary-article-expire-hook
10403                                         'delete
10404                                         (gnus-data-header
10405                                          (assoc article (gnus-data-list nil)))
10406                                         gnus-newsgroup-name
10407                                         nil
10408                                         nil)))))))
10409         (gnus-message 6 "Expiring articles...done")))))
10410
10411 (defun gnus-summary-expire-articles-now ()
10412   "Expunge all expirable articles in the current group.
10413 This means that *all* articles that are marked as expirable will be
10414 deleted forever, right now."
10415   (interactive)
10416   (or gnus-expert-user
10417       (gnus-yes-or-no-p
10418        "Are you really, really sure you want to delete all expirable messages? ")
10419       (error "Phew!"))
10420   (gnus-summary-expire-articles t))
10421
10422 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10423 (defun gnus-summary-delete-article (&optional n)
10424   "Delete the N next (mail) articles.
10425 This command actually deletes articles.  This is not a marking
10426 command.  The article will disappear forever from your life, never to
10427 return.
10428
10429 If N is negative, delete backwards.
10430 If N is nil and articles have been marked with the process mark,
10431 delete these instead.
10432
10433 If `gnus-novice-user' is non-nil you will be asked for
10434 confirmation before the articles are deleted."
10435   (interactive "P")
10436   (unless (gnus-check-backend-function 'request-expire-articles
10437                                        gnus-newsgroup-name)
10438     (error "The current newsgroup does not support article deletion"))
10439   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10440     (error "Couldn't open server"))
10441   ;; Compute the list of articles to delete.
10442   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10443         (nnmail-expiry-target 'delete)
10444         not-deleted)
10445     (if (and gnus-novice-user
10446              (not (gnus-yes-or-no-p
10447                    (format "Do you really want to delete %s forever? "
10448                            (if (> (length articles) 1)
10449                                (format "these %s articles" (length articles))
10450                              "this article")))))
10451         ()
10452       ;; Delete the articles.
10453       (setq not-deleted (gnus-request-expire-articles
10454                          articles gnus-newsgroup-name 'force))
10455       (save-excursion
10456         (while articles
10457           (gnus-summary-remove-process-mark (car articles))
10458           ;; The backend might not have been able to delete the article
10459           ;; after all.
10460           (unless (memq (car articles) not-deleted)
10461             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10462             (let* ((article (car articles))
10463                    (ghead  (gnus-data-header
10464                             (assoc article (gnus-data-list nil)))))
10465               (run-hook-with-args 'gnus-summary-article-delete-hook
10466                                   'delete ghead gnus-newsgroup-name nil
10467                                   nil)))
10468           (setq articles (cdr articles))))
10469       (when not-deleted
10470         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10471     (gnus-summary-position-point)
10472     (gnus-set-mode-line 'summary)
10473     not-deleted))
10474
10475 (defun gnus-summary-edit-article (&optional arg)
10476   "Edit the current article.
10477 This will have permanent effect only in mail groups.
10478 If ARG is nil, edit the decoded articles.
10479 If ARG is 1, edit the raw articles.
10480 If ARG is 2, edit the raw articles even in read-only groups.
10481 If ARG is 3, edit the articles with the current handles.
10482 Otherwise, allow editing of articles even in read-only
10483 groups."
10484   (interactive "P")
10485   (let (force raw current-handles)
10486     (cond
10487      ((null arg))
10488      ((eq arg 1)
10489       (setq raw t))
10490      ((eq arg 2)
10491       (setq raw t
10492             force t))
10493      ((eq arg 3)
10494       (setq current-handles
10495             (and (gnus-buffer-live-p gnus-article-buffer)
10496                  (with-current-buffer gnus-article-buffer
10497                    (prog1
10498                        gnus-article-mime-handles
10499                      (setq gnus-article-mime-handles nil))))))
10500      (t
10501       (setq force t)))
10502     (when (and raw (not force)
10503                (member gnus-newsgroup-name '("nndraft:delayed"
10504                                              "nndraft:drafts"
10505                                              "nndraft:queue")))
10506       (error "Can't edit the raw article in group %s"
10507              gnus-newsgroup-name))
10508     (with-current-buffer gnus-summary-buffer
10509       (let ((mail-parse-charset gnus-newsgroup-charset)
10510             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10511         (gnus-set-global-variables)
10512         (when (and (not force)
10513                    (gnus-group-read-only-p))
10514           (error "The current newsgroup does not support article editing"))
10515         (gnus-summary-show-article t)
10516         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10517           (with-current-buffer gnus-article-buffer
10518             (mm-enable-multibyte)))
10519         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10520             (setq raw t))
10521         (gnus-article-edit-article
10522          (if raw 'ignore
10523            `(lambda ()
10524               (let ((mbl mml-buffer-list))
10525                 (setq mml-buffer-list nil)
10526                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10527                   (mime-to-mml ,'current-handles))
10528                 (let ((mbl1 mml-buffer-list))
10529                   (setq mml-buffer-list mbl)
10530                   (set (make-local-variable 'mml-buffer-list) mbl1))
10531                 (gnus-make-local-hook 'kill-buffer-hook)
10532                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10533          `(lambda (no-highlight)
10534             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10535                   (message-options message-options)
10536                   (message-options-set-recipient)
10537                   (mail-parse-ignored-charsets
10538                    ',gnus-newsgroup-ignored-charsets)
10539                   (rfc2047-header-encoding-alist
10540                    ',(let ((charset (gnus-group-name-charset
10541                                      (gnus-find-method-for-group
10542                                       gnus-newsgroup-name)
10543                                      gnus-newsgroup-name)))
10544                        (append (list (cons "Newsgroups" charset)
10545                                      (cons "Followup-To" charset)
10546                                      (cons "Xref" charset))
10547                                rfc2047-header-encoding-alist))))
10548               ,(if (not raw) '(progn
10549                                 (mml-to-mime)
10550                                 (mml-destroy-buffers)
10551                                 (remove-hook 'kill-buffer-hook
10552                                              'mml-destroy-buffers t)
10553                                 (kill-local-variable 'mml-buffer-list)))
10554               (gnus-summary-edit-article-done
10555                ,(or (mail-header-references gnus-current-headers) "")
10556                ,(gnus-group-read-only-p)
10557                ,gnus-summary-buffer no-highlight))))))))
10558
10559 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10560
10561 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10562                                                  no-highlight)
10563   "Make edits to the current article permanent."
10564   (interactive)
10565   (save-excursion
10566     ;; The buffer restriction contains the entire article if it exists.
10567     (when (article-goto-body)
10568       (let ((lines (count-lines (point) (point-max)))
10569             (length (- (point-max) (point)))
10570             (case-fold-search t)
10571             (body (copy-marker (point))))
10572         (goto-char (point-min))
10573         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10574           (delete-region (match-beginning 1) (match-end 1))
10575           (insert (number-to-string length)))
10576         (goto-char (point-min))
10577         (when (re-search-forward
10578                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10579           (delete-region (match-beginning 1) (match-end 1))
10580           (insert (number-to-string length)))
10581         (goto-char (point-min))
10582         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10583           (delete-region (match-beginning 1) (match-end 1))
10584           (insert (number-to-string lines))))))
10585   ;; Replace the article.
10586   (let ((buf (current-buffer))
10587         (article (cdr gnus-article-current))
10588         replace-result)
10589     (with-temp-buffer
10590       (insert-buffer-substring buf)
10591       (if (and (not read-only)
10592                (not (setq replace-result
10593                           (gnus-request-replace-article
10594                            article (car gnus-article-current)
10595                            (current-buffer) t))))
10596           (error "Couldn't replace article")
10597         ;; If we got a number back, then that's the new article number
10598         ;; for this article.  Otherwise, the article number didn't change.
10599         (when (numberp replace-result)
10600           (with-current-buffer gnus-summary-buffer
10601             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10602             (gnus-summary-limit gnus-newsgroup-limit)
10603             (setq article replace-result)
10604             (gnus-summary-goto-subject article t)))
10605         ;; Update the summary buffer.
10606         (if (and references
10607                  (equal (message-tokenize-header references " ")
10608                         (message-tokenize-header
10609                          (or (message-fetch-field "references") "") " ")))
10610             ;; We only have to update this line.
10611             (save-excursion
10612               (save-restriction
10613                 (message-narrow-to-head)
10614                 (let ((head (buffer-substring-no-properties
10615                              (point-min) (point-max)))
10616                       header)
10617                   (with-temp-buffer
10618                     (insert (format "211 %d Article retrieved.\n" article))
10619                     (insert head)
10620                     (insert ".\n")
10621                     (let ((nntp-server-buffer (current-buffer)))
10622                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10623                     (with-current-buffer gnus-summary-buffer
10624                       (gnus-data-set-header (gnus-data-find article) header)
10625                       (gnus-summary-update-article-line article header)
10626                       (if (gnus-summary-goto-subject article nil t)
10627                           (gnus-summary-update-secondary-mark article)))))))
10628           ;; Update threads.
10629           (set-buffer (or buffer gnus-summary-buffer))
10630           (gnus-summary-update-article article)
10631           (if (gnus-summary-goto-subject article nil t)
10632               (gnus-summary-update-secondary-mark article)))
10633         ;; Prettify the article buffer again.
10634         (unless no-highlight
10635           (with-current-buffer gnus-article-buffer
10636             ;;!!! Fix this -- article should be rehighlighted.
10637             ;;(gnus-run-hooks 'gnus-article-display-hook)
10638             (set-buffer gnus-original-article-buffer)
10639             (gnus-request-article
10640              article (car gnus-article-current) (current-buffer))))
10641         ;; Prettify the summary buffer line.
10642         (when (gnus-visual-p 'summary-highlight 'highlight)
10643           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10644
10645 (defun gnus-summary-edit-wash (key)
10646   "Perform editing command KEY in the article buffer."
10647   (interactive
10648    (list
10649     (progn
10650       (message "%s" (concat (this-command-keys) "- "))
10651       (read-char))))
10652   (message "")
10653   (gnus-summary-edit-article)
10654   (execute-kbd-macro (concat (this-command-keys) key))
10655   (gnus-article-edit-done))
10656
10657 ;;; Respooling
10658
10659 (defun gnus-summary-respool-query (&optional silent trace)
10660   "Query where the respool algorithm would put this article."
10661   (interactive)
10662   (let (gnus-mark-article-hook)
10663     (gnus-summary-select-article)
10664     (with-current-buffer gnus-original-article-buffer
10665       (let ((groups (nnmail-article-group 'identity trace)))
10666         (unless silent
10667           (if groups
10668               (message "This message would go to %s"
10669                        (mapconcat 'car groups ", "))
10670             (message "This message would go to no groups"))
10671           groups)))))
10672
10673 (defun gnus-summary-respool-trace ()
10674   "Trace where the respool algorithm would put this article.
10675 Display a buffer showing all fancy splitting patterns which matched."
10676   (interactive)
10677   (gnus-summary-respool-query nil t))
10678
10679 ;; Summary marking commands.
10680
10681 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10682   "Mark articles which has the same subject as read, and then select the next.
10683 If UNMARK is positive, remove any kind of mark.
10684 If UNMARK is negative, tick articles."
10685   (interactive "P")
10686   (when unmark
10687     (setq unmark (prefix-numeric-value unmark)))
10688   (let ((count
10689          (gnus-summary-mark-same-subject
10690           (gnus-summary-article-subject) unmark)))
10691     ;; Select next unread article.  If auto-select-same mode, should
10692     ;; select the first unread article.
10693     (gnus-summary-next-article t (and gnus-auto-select-same
10694                                       (gnus-summary-article-subject)))
10695     (gnus-message 7 "%d article%s marked as %s"
10696                   count (if (= count 1) " is" "s are")
10697                   (if unmark "unread" "read"))))
10698
10699 (defun gnus-summary-kill-same-subject (&optional unmark)
10700   "Mark articles which has the same subject as read.
10701 If UNMARK is positive, remove any kind of mark.
10702 If UNMARK is negative, tick articles."
10703   (interactive "P")
10704   (when unmark
10705     (setq unmark (prefix-numeric-value unmark)))
10706   (let ((count
10707          (gnus-summary-mark-same-subject
10708           (gnus-summary-article-subject) unmark)))
10709     ;; If marked as read, go to next unread subject.
10710     (when (null unmark)
10711       ;; Go to next unread subject.
10712       (gnus-summary-next-subject 1 t))
10713     (gnus-message 7 "%d articles are marked as %s"
10714                   count (if unmark "unread" "read"))))
10715
10716 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10717   "Mark articles with same SUBJECT as read, and return marked number.
10718 If optional argument UNMARK is positive, remove any kinds of marks.
10719 If optional argument UNMARK is negative, mark articles as unread instead."
10720   (let ((count 1))
10721     (save-excursion
10722       (cond
10723        ((null unmark)                   ; Mark as read.
10724         (while (and
10725                 (progn
10726                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10727                   (gnus-summary-show-thread) t)
10728                 (gnus-summary-find-subject subject))
10729           (setq count (1+ count))))
10730        ((> unmark 0)                    ; Tick.
10731         (while (and
10732                 (progn
10733                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10734                   (gnus-summary-show-thread) t)
10735                 (gnus-summary-find-subject subject))
10736           (setq count (1+ count))))
10737        (t                               ; Mark as unread.
10738         (while (and
10739                 (progn
10740                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10741                   (gnus-summary-show-thread) t)
10742                 (gnus-summary-find-subject subject))
10743           (setq count (1+ count)))))
10744       (gnus-set-mode-line 'summary)
10745       ;; Return the number of marked articles.
10746       count)))
10747
10748 (defun gnus-summary-mark-as-processable (n &optional unmark)
10749   "Set the process mark on the next N articles.
10750 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10751 the process mark instead.  The difference between N and the actual
10752 number of articles marked is returned."
10753   (interactive "P")
10754   (if (and (null n) (gnus-region-active-p))
10755       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10756     (setq n (prefix-numeric-value n))
10757     (let ((backward (< n 0))
10758           (n (abs n)))
10759       (while (and
10760               (> n 0)
10761               (if unmark
10762                   (gnus-summary-remove-process-mark
10763                    (gnus-summary-article-number))
10764                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10765               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10766         (setq n (1- n)))
10767       (when (/= 0 n)
10768         (gnus-message 7 "No more articles"))
10769       (gnus-summary-recenter)
10770       (gnus-summary-position-point)
10771       n)))
10772
10773 (defun gnus-summary-unmark-as-processable (n)
10774   "Remove the process mark from the next N articles.
10775 If N is negative, unmark backward instead.  The difference between N and
10776 the actual number of articles unmarked is returned."
10777   (interactive "P")
10778   (gnus-summary-mark-as-processable n t))
10779
10780 (defun gnus-summary-unmark-all-processable ()
10781   "Remove the process mark from all articles."
10782   (interactive)
10783   (save-excursion
10784     (while gnus-newsgroup-processable
10785       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10786   (gnus-summary-position-point))
10787
10788 (defun gnus-summary-add-mark (article type)
10789   "Mark ARTICLE with a mark of TYPE."
10790   (let ((vtype (car (assq type gnus-article-mark-lists)))
10791         var)
10792     (if (not vtype)
10793         (error "No such mark type: %s" type)
10794       (setq var (intern (format "gnus-newsgroup-%s" type)))
10795       (set var (cons article (symbol-value var)))
10796       (if (memq type '(processable cached replied forwarded recent saved))
10797           (gnus-summary-update-secondary-mark article)
10798         ;;; !!! This is bogus.  We should find out what primary
10799         ;;; !!! mark we want to set.
10800         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10801
10802 (defun gnus-summary-mark-as-expirable (n)
10803   "Mark N articles forward as expirable.
10804 If N is negative, mark backward instead.  The difference between N and
10805 the actual number of articles marked is returned."
10806   (interactive "p")
10807   (gnus-summary-mark-forward n gnus-expirable-mark))
10808
10809 (defun gnus-summary-mark-as-spam (n)
10810   "Mark N articles forward as spam.
10811 If N is negative, mark backward instead.  The difference between N and
10812 the actual number of articles marked is returned."
10813   (interactive "p")
10814   (gnus-summary-mark-forward n gnus-spam-mark))
10815
10816 (defun gnus-summary-mark-article-as-replied (article)
10817   "Mark ARTICLE as replied to and update the summary line.
10818 ARTICLE can also be a list of articles."
10819   (interactive (list (gnus-summary-article-number)))
10820   (let ((articles (if (listp article) article (list article))))
10821     (dolist (article articles)
10822       (unless (numberp article)
10823         (error "%s is not a number" article))
10824       (push article gnus-newsgroup-replied)
10825       (let ((inhibit-read-only t))
10826         (when (gnus-summary-goto-subject article nil t)
10827           (gnus-summary-update-secondary-mark article))))))
10828
10829 (defun gnus-summary-mark-article-as-forwarded (article)
10830   "Mark ARTICLE as forwarded and update the summary line.
10831 ARTICLE can also be a list of articles."
10832   (let ((articles (if (listp article) article (list article))))
10833     (dolist (article articles)
10834       (push article gnus-newsgroup-forwarded)
10835       (let ((inhibit-read-only t))
10836         (when (gnus-summary-goto-subject article nil t)
10837           (gnus-summary-update-secondary-mark article))))))
10838
10839 (defun gnus-summary-set-bookmark (article)
10840   "Set a bookmark in current article."
10841   (interactive (list (gnus-summary-article-number)))
10842   (when (or (not (get-buffer gnus-article-buffer))
10843             (not gnus-current-article)
10844             (not gnus-article-current)
10845             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10846     (error "No current article selected"))
10847   ;; Remove old bookmark, if one exists.
10848   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10849   ;; Set the new bookmark, which is on the form
10850   ;; (article-number . line-number-in-body).
10851   (push
10852    (cons article
10853          (with-current-buffer gnus-article-buffer
10854            (count-lines
10855             (min (point)
10856                  (save-excursion
10857                    (article-goto-body)
10858                    (point)))
10859             (point))))
10860    gnus-newsgroup-bookmarks)
10861   (gnus-message 6 "A bookmark has been added to the current article."))
10862
10863 (defun gnus-summary-remove-bookmark (article)
10864   "Remove the bookmark from the current article."
10865   (interactive (list (gnus-summary-article-number)))
10866   ;; Remove old bookmark, if one exists.
10867   (if (not (assq article gnus-newsgroup-bookmarks))
10868       (gnus-message 6 "No bookmark in current article.")
10869     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10870     (gnus-message 6 "Removed bookmark.")))
10871
10872 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10873 (defun gnus-summary-mark-as-dormant (n)
10874   "Mark N articles forward as dormant.
10875 If N is negative, mark backward instead.  The difference between N and
10876 the actual number of articles marked is returned."
10877   (interactive "p")
10878   (gnus-summary-mark-forward n gnus-dormant-mark))
10879
10880 (defun gnus-summary-set-process-mark (article)
10881   "Set the process mark on ARTICLE and update the summary line."
10882   (setq gnus-newsgroup-processable
10883         (cons article
10884               (delq article gnus-newsgroup-processable)))
10885   (when (gnus-summary-goto-subject article)
10886     (gnus-summary-show-thread)
10887     (gnus-summary-goto-subject article)
10888     (gnus-summary-update-secondary-mark article)))
10889
10890 (defun gnus-summary-remove-process-mark (&rest articles)
10891   "Remove the process mark from ARTICLES and update the summary line."
10892   (dolist (article articles)
10893     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10894     (when (gnus-summary-goto-subject article)
10895       (gnus-summary-show-thread)
10896       (gnus-summary-goto-subject article)
10897       (gnus-summary-update-secondary-mark article)))
10898   t)
10899
10900 (defun gnus-summary-set-saved-mark (article)
10901   "Set the process mark on ARTICLE and update the summary line."
10902   (push article gnus-newsgroup-saved)
10903   (when (gnus-summary-goto-subject article)
10904     (gnus-summary-update-secondary-mark article)))
10905
10906 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10907   "Mark N articles as read forwards.
10908 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10909 The difference between N and the actual number of articles marked is
10910 returned.
10911 If NO-EXPIRE, auto-expiry will be inhibited."
10912   (interactive "p")
10913   (gnus-summary-show-thread)
10914   (let ((backward (< n 0))
10915         (gnus-summary-goto-unread
10916          (and gnus-summary-goto-unread
10917               (not (eq gnus-summary-goto-unread 'never))
10918               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10919                                     gnus-ticked-mark gnus-dormant-mark)))))
10920         (n (abs n))
10921         (mark (or mark gnus-del-mark)))
10922     (while (and (> n 0)
10923                 (gnus-summary-mark-article nil mark no-expire)
10924                 (zerop (gnus-summary-next-subject
10925                         (if backward -1 1)
10926                         (and gnus-summary-goto-unread
10927                              (not (eq gnus-summary-goto-unread 'never)))
10928                         t)))
10929       (setq n (1- n)))
10930     (when (/= 0 n)
10931       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10932     (gnus-summary-recenter)
10933     (gnus-summary-position-point)
10934     (gnus-set-mode-line 'summary)
10935     n))
10936
10937 (defun gnus-summary-mark-article-as-read (mark)
10938   "Mark the current article quickly as read with MARK."
10939   (let ((article (gnus-summary-article-number)))
10940     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10941     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10942     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10943     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10944     (push (cons article mark) gnus-newsgroup-reads)
10945     ;; Possibly remove from cache, if that is used.
10946     (when gnus-use-cache
10947       (gnus-cache-enter-remove-article article))
10948     ;; Allow the backend to change the mark.
10949     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10950     ;; Check for auto-expiry.
10951     (when (and gnus-newsgroup-auto-expire
10952                (memq mark gnus-auto-expirable-marks))
10953       (setq mark gnus-expirable-mark)
10954       ;; Let the backend know about the mark change.
10955       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10956       (push article gnus-newsgroup-expirable))
10957     ;; Set the mark in the buffer.
10958     (gnus-summary-update-mark mark 'unread)
10959     t))
10960
10961 (defun gnus-summary-mark-article-as-unread (mark)
10962   "Mark the current article quickly as unread with MARK."
10963   (let* ((article (gnus-summary-article-number))
10964          (old-mark (gnus-summary-article-mark article)))
10965     ;; Allow the backend to change the mark.
10966     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10967     (if (eq mark old-mark)
10968         t
10969       (if (<= article 0)
10970           (progn
10971             (gnus-error 1 "Can't mark negative article numbers")
10972             nil)
10973         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10974         (setq gnus-newsgroup-spam-marked
10975               (delq article gnus-newsgroup-spam-marked))
10976         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10977         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10978         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10979         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10980         (cond ((= mark gnus-ticked-mark)
10981                (setq gnus-newsgroup-marked
10982                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10983                                               article)))
10984               ((= mark gnus-spam-mark)
10985                (setq gnus-newsgroup-spam-marked
10986                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10987                                               article)))
10988               ((= mark gnus-dormant-mark)
10989                (setq gnus-newsgroup-dormant
10990                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10991                                               article)))
10992               (t
10993                (setq gnus-newsgroup-unreads
10994                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10995                                               article))))
10996         (gnus-alist-pull article gnus-newsgroup-reads)
10997
10998         ;; See whether the article is to be put in the cache.
10999         (and gnus-use-cache
11000              (vectorp (gnus-summary-article-header article))
11001              (save-excursion
11002                (gnus-cache-possibly-enter-article
11003                 gnus-newsgroup-name article
11004                 (= mark gnus-ticked-mark)
11005                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11006
11007         ;; Fix the mark.
11008         (gnus-summary-update-mark mark 'unread)
11009         t))))
11010
11011 (defun gnus-summary-mark-article (&optional article mark no-expire)
11012   "Mark ARTICLE with MARK.  MARK can be any character.
11013 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11014 `??' (dormant) and `?E' (expirable).
11015 If MARK is nil, then the default character `?r' is used.
11016 If ARTICLE is nil, then the article on the current line will be
11017 marked.
11018 If NO-EXPIRE, auto-expiry will be inhibited."
11019   ;; The mark might be a string.
11020   (when (stringp mark)
11021     (setq mark (aref mark 0)))
11022   ;; If no mark is given, then we check auto-expiring.
11023   (when (null mark)
11024     (setq mark gnus-del-mark))
11025   (when (and (not no-expire)
11026              gnus-newsgroup-auto-expire
11027              (memq mark gnus-auto-expirable-marks))
11028     (setq mark gnus-expirable-mark))
11029   (let ((article (or article (gnus-summary-article-number)))
11030         (old-mark (gnus-summary-article-mark article)))
11031     ;; Allow the backend to change the mark.
11032     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11033     (if (eq mark old-mark)
11034         t
11035       (unless article
11036         (error "No article on current line"))
11037       (if (not (if (or (= mark gnus-unread-mark)
11038                        (= mark gnus-ticked-mark)
11039                        (= mark gnus-spam-mark)
11040                        (= mark gnus-dormant-mark))
11041                    (gnus-mark-article-as-unread article mark)
11042                  (gnus-mark-article-as-read article mark)))
11043           t
11044         ;; See whether the article is to be put in the cache.
11045         (and gnus-use-cache
11046              (not (= mark gnus-canceled-mark))
11047              (vectorp (gnus-summary-article-header article))
11048              (save-excursion
11049                (gnus-cache-possibly-enter-article
11050                 gnus-newsgroup-name article
11051                 (= mark gnus-ticked-mark)
11052                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11053
11054         (when (gnus-summary-goto-subject article nil t)
11055           (let ((inhibit-read-only t))
11056             (gnus-summary-show-thread)
11057             ;; Fix the mark.
11058             (gnus-summary-update-mark mark 'unread)
11059             t))))))
11060
11061 (defun gnus-summary-update-secondary-mark (article)
11062   "Update the secondary (read, process, cache) mark."
11063   (gnus-summary-update-mark
11064    (cond ((memq article gnus-newsgroup-processable)
11065           gnus-process-mark)
11066          ((memq article gnus-newsgroup-cached)
11067           gnus-cached-mark)
11068          ((memq article gnus-newsgroup-replied)
11069           gnus-replied-mark)
11070          ((memq article gnus-newsgroup-forwarded)
11071           gnus-forwarded-mark)
11072          ((memq article gnus-newsgroup-saved)
11073           gnus-saved-mark)
11074          ((memq article gnus-newsgroup-unseen)
11075           gnus-unseen-mark)
11076          (t gnus-no-mark))
11077    'replied)
11078   (when (gnus-visual-p 'summary-highlight 'highlight)
11079     (gnus-summary-highlight-line)
11080     (gnus-run-hooks 'gnus-summary-update-hook))
11081   t)
11082
11083 (defun gnus-summary-update-download-mark (article)
11084   "Update the download mark."
11085   (gnus-summary-update-mark
11086    (cond ((memq article gnus-newsgroup-undownloaded)
11087           gnus-undownloaded-mark)
11088          (gnus-newsgroup-agentized
11089           gnus-downloaded-mark)
11090          (t
11091           gnus-no-mark))
11092    'download)
11093   (gnus-summary-update-line t)
11094   t)
11095
11096 (defun gnus-summary-update-mark (mark type)
11097   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11098         (inhibit-read-only t))
11099     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11100     (when forward
11101       (when (looking-at "\r")
11102         (incf forward))
11103       (when (<= (+ forward (point)) (point-max))
11104         ;; Go to the right position on the line.
11105         (goto-char (+ forward (point)))
11106         ;; Replace the old mark with the new mark.
11107         (let ((to-insert
11108                (mm-subst-char-in-string
11109                 (char-after) mark
11110                 (buffer-substring (point) (1+ (point))))))
11111           (delete-region (point) (1+ (point)))
11112           (insert to-insert))
11113         ;; Optionally update the marks by some user rule.
11114         (when (eq type 'unread)
11115           (gnus-data-set-mark
11116            (gnus-data-find (gnus-summary-article-number)) mark)
11117           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11118
11119 (defun gnus-mark-article-as-read (article &optional mark)
11120   "Enter ARTICLE in the pertinent lists and remove it from others."
11121   ;; Make the article expirable.
11122   (let ((mark (or mark gnus-del-mark)))
11123     (setq gnus-newsgroup-expirable
11124           (if (= mark gnus-expirable-mark)
11125               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11126             (delq article gnus-newsgroup-expirable)))
11127     ;; Remove from unread and marked lists.
11128     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11129     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11130     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11131     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11132     (push (cons article mark) gnus-newsgroup-reads)
11133     ;; Possibly remove from cache, if that is used.
11134     (when gnus-use-cache
11135       (gnus-cache-enter-remove-article article))
11136     t))
11137
11138 (defun gnus-mark-article-as-unread (article &optional mark)
11139   "Enter ARTICLE in the pertinent lists and remove it from others."
11140   (let ((mark (or mark gnus-ticked-mark)))
11141     (if (<= article 0)
11142         (progn
11143           (gnus-error 1 "Can't mark negative article numbers")
11144           nil)
11145       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11146             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11147             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11148             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11149             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11150
11151       ;; Unsuppress duplicates?
11152       (when gnus-suppress-duplicates
11153         (gnus-dup-unsuppress-article article))
11154
11155       (cond ((= mark gnus-ticked-mark)
11156              (setq gnus-newsgroup-marked
11157                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11158             ((= mark gnus-spam-mark)
11159              (setq gnus-newsgroup-spam-marked
11160                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11161                                             article)))
11162             ((= mark gnus-dormant-mark)
11163              (setq gnus-newsgroup-dormant
11164                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11165             (t
11166              (setq gnus-newsgroup-unreads
11167                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11168       (gnus-alist-pull article gnus-newsgroup-reads)
11169       t)))
11170
11171 (defun gnus-summary-tick-article-forward (n)
11172   "Tick N articles forwards.
11173 If N is negative, tick backwards instead.
11174 The difference between N and the number of articles ticked is returned."
11175   (interactive "p")
11176   (gnus-summary-mark-forward n gnus-ticked-mark))
11177
11178 (defun gnus-summary-tick-article-backward (n)
11179   "Tick N articles backwards.
11180 The difference between N and the number of articles ticked is returned."
11181   (interactive "p")
11182   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11183
11184 (defun gnus-summary-tick-article (&optional article clear-mark)
11185   "Mark current article as unread.
11186 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11187 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11188   (interactive)
11189   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11190                                        gnus-ticked-mark)))
11191
11192 (defun gnus-summary-mark-as-read-forward (n)
11193   "Mark N articles as read forwards.
11194 If N is negative, mark backwards instead.
11195 The difference between N and the actual number of articles marked is
11196 returned."
11197   (interactive "p")
11198   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11199
11200 (defun gnus-summary-mark-as-read-backward (n)
11201   "Mark the N articles as read backwards.
11202 The difference between N and the actual number of articles marked is
11203 returned."
11204   (interactive "p")
11205   (gnus-summary-mark-forward
11206    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11207
11208 (defun gnus-summary-mark-as-read (&optional article mark)
11209   "Mark current article as read.
11210 ARTICLE specifies the article to be marked as read.
11211 MARK specifies a string to be inserted at the beginning of the line."
11212   (gnus-summary-mark-article article mark))
11213
11214 (defun gnus-summary-clear-mark-forward (n)
11215   "Clear marks from N articles forward.
11216 If N is negative, clear backward instead.
11217 The difference between N and the number of marks cleared is returned."
11218   (interactive "p")
11219   (gnus-summary-mark-forward n gnus-unread-mark))
11220
11221 (defun gnus-summary-clear-mark-backward (n)
11222   "Clear marks from N articles backward.
11223 The difference between N and the number of marks cleared is returned."
11224   (interactive "p")
11225   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11226
11227 (defun gnus-summary-mark-unread-as-read ()
11228   "Intended to be used by `gnus-mark-article-hook'."
11229   (when (memq gnus-current-article gnus-newsgroup-unreads)
11230     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11231
11232 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11233   "Intended to be used by `gnus-mark-article-hook'."
11234   (let ((mark (gnus-summary-article-mark)))
11235     (when (or (gnus-unread-mark-p mark)
11236               (gnus-read-mark-p mark))
11237       (gnus-summary-mark-article gnus-current-article
11238                                  (or new-mark gnus-read-mark)))))
11239
11240 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11241   "Intended to be used by `gnus-mark-article-hook'."
11242   (let ((mark (gnus-summary-article-mark)))
11243     (when (or (gnus-unread-mark-p mark)
11244               (gnus-read-mark-p mark))
11245       (gnus-summary-mark-article (gnus-summary-article-number)
11246                                  (or new-mark gnus-read-mark)))))
11247
11248 (defun gnus-summary-mark-unread-as-ticked ()
11249   "Intended to be used by `gnus-mark-article-hook'."
11250   (when (memq gnus-current-article gnus-newsgroup-unreads)
11251     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11252
11253 (defun gnus-summary-mark-region-as-read (point mark all)
11254   "Mark all unread articles between point and mark as read.
11255 If given a prefix, mark all articles between point and mark as read,
11256 even ticked and dormant ones."
11257   (interactive "r\nP")
11258   (save-excursion
11259     (let (article)
11260       (goto-char point)
11261       (beginning-of-line)
11262       (while (and
11263               (< (point) mark)
11264               (progn
11265                 (when (or all
11266                           (memq (setq article (gnus-summary-article-number))
11267                                 gnus-newsgroup-unreads))
11268                   (gnus-summary-mark-article article gnus-del-mark))
11269                 t)
11270               (gnus-summary-find-next))))))
11271
11272 (defun gnus-summary-mark-below (score mark)
11273   "Mark articles with score less than SCORE with MARK."
11274   (interactive "P\ncMark: ")
11275   (setq score (if score
11276                   (prefix-numeric-value score)
11277                 (or gnus-summary-default-score 0)))
11278   (with-current-buffer gnus-summary-buffer
11279     (goto-char (point-min))
11280     (while
11281         (progn
11282           (and (< (gnus-summary-article-score) score)
11283                (gnus-summary-mark-article nil mark))
11284           (gnus-summary-find-next)))))
11285
11286 (defun gnus-summary-kill-below (&optional score)
11287   "Mark articles with score below SCORE as read."
11288   (interactive "P")
11289   (gnus-summary-mark-below score gnus-killed-mark))
11290
11291 (defun gnus-summary-clear-above (&optional score)
11292   "Clear all marks from articles with score above SCORE."
11293   (interactive "P")
11294   (gnus-summary-mark-above score gnus-unread-mark))
11295
11296 (defun gnus-summary-tick-above (&optional score)
11297   "Tick all articles with score above SCORE."
11298   (interactive "P")
11299   (gnus-summary-mark-above score gnus-ticked-mark))
11300
11301 (defun gnus-summary-mark-above (score mark)
11302   "Mark articles with score over SCORE with MARK."
11303   (interactive "P\ncMark: ")
11304   (setq score (if score
11305                   (prefix-numeric-value score)
11306                 (or gnus-summary-default-score 0)))
11307   (with-current-buffer gnus-summary-buffer
11308     (goto-char (point-min))
11309     (while (and (progn
11310                   (when (> (gnus-summary-article-score) score)
11311                     (gnus-summary-mark-article nil mark))
11312                   t)
11313                 (gnus-summary-find-next)))))
11314
11315 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11316 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11317 (defun gnus-summary-limit-include-expunged (&optional no-error)
11318   "Display all the hidden articles that were expunged for low scores."
11319   (interactive)
11320   (let ((inhibit-read-only t))
11321     (let ((scored gnus-newsgroup-scored)
11322           headers h)
11323       (while scored
11324         (unless (gnus-summary-article-header (caar scored))
11325           (and (setq h (gnus-number-to-header (caar scored)))
11326                (< (cdar scored) gnus-summary-expunge-below)
11327                (push h headers)))
11328         (setq scored (cdr scored)))
11329       (if (not headers)
11330           (when (not no-error)
11331             (error "No expunged articles hidden"))
11332         (goto-char (point-min))
11333         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11334         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11335         (dolist (x headers)
11336           (push (mail-header-number x) gnus-newsgroup-limit))
11337         (gnus-summary-prepare-unthreaded (nreverse headers))
11338         (goto-char (point-min))
11339         (gnus-summary-position-point)
11340         t))))
11341
11342 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11343   "Mark all unread articles in this newsgroup as read.
11344 If prefix argument ALL is non-nil, ticked and dormant articles will
11345 also be marked as read.
11346 If QUIETLY is non-nil, no questions will be asked.
11347
11348 If TO-HERE is non-nil, it should be a point in the buffer.  All
11349 articles before (after, if REVERSE is set) this point will be marked
11350 as read.
11351
11352 Note that this function will only catch up the unread article
11353 in the current summary buffer limitation.
11354
11355 The number of articles marked as read is returned."
11356   (interactive "P")
11357   (prog1
11358       (save-excursion
11359         (when (or quietly
11360                   (not gnus-interactive-catchup) ;Without confirmation?
11361                   gnus-expert-user
11362                   (gnus-y-or-n-p
11363                    (if all
11364                        "Mark absolutely all articles as read? "
11365                      "Mark all unread articles as read? ")))
11366           (if (and not-mark
11367                    (not gnus-newsgroup-adaptive)
11368                    (not gnus-newsgroup-auto-expire)
11369                    (not gnus-suppress-duplicates)
11370                    (or (not gnus-use-cache)
11371                        (eq gnus-use-cache 'passive)))
11372               (progn
11373                 (when all
11374                   (setq gnus-newsgroup-marked nil
11375                         gnus-newsgroup-spam-marked nil
11376                         gnus-newsgroup-dormant nil))
11377                 (setq gnus-newsgroup-unreads
11378                       (gnus-sorted-nunion
11379                        (gnus-sorted-intersection gnus-newsgroup-unreads
11380                                                  gnus-newsgroup-downloadable)
11381                        (gnus-sorted-difference gnus-newsgroup-unfetched
11382                                                gnus-newsgroup-cached))))
11383             ;; We actually mark all articles as canceled, which we
11384             ;; have to do when using auto-expiry or adaptive scoring.
11385             (gnus-summary-show-all-threads)
11386             (if (and to-here reverse)
11387                 (progn
11388                   (goto-char to-here)
11389                   (gnus-summary-mark-current-read-and-unread-as-read
11390                    gnus-catchup-mark)
11391                   (while (gnus-summary-find-next (not all))
11392                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11393               (when (gnus-summary-first-subject (not all))
11394                 (while (and
11395                         (if to-here (< (point) to-here) t)
11396                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11397                         (gnus-summary-find-next (not all))))))
11398             (gnus-set-mode-line 'summary))
11399           t))
11400     (gnus-summary-position-point)))
11401
11402 (defun gnus-summary-catchup-to-here (&optional all)
11403   "Mark all unticked articles before the current one as read.
11404 If ALL is non-nil, also mark ticked and dormant articles as read."
11405   (interactive "P")
11406   (save-excursion
11407     (gnus-save-hidden-threads
11408       (let ((beg (point)))
11409         ;; We check that there are unread articles.
11410         (when (or all (gnus-summary-find-prev))
11411           (gnus-summary-catchup all t beg)))))
11412   (gnus-summary-position-point))
11413
11414 (defun gnus-summary-catchup-from-here (&optional all)
11415   "Mark all unticked articles after (and including) the current one as read.
11416 If ALL is non-nil, also mark ticked and dormant articles as read."
11417   (interactive "P")
11418   (save-excursion
11419     (gnus-save-hidden-threads
11420       (let ((beg (point)))
11421         ;; We check that there are unread articles.
11422         (when (or all (gnus-summary-find-next))
11423           (gnus-summary-catchup all t beg nil t)))))
11424   (gnus-summary-position-point))
11425
11426 (defun gnus-summary-catchup-all (&optional quietly)
11427   "Mark all articles in this newsgroup as read.
11428 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11429 instead, which marks only unread articles as read."
11430   (interactive "P")
11431   (gnus-summary-catchup t quietly))
11432
11433 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11434   "Mark all unread articles in this group as read, then exit.
11435 If prefix argument ALL is non-nil, all articles are marked as read.
11436 If QUIETLY is non-nil, no questions will be asked."
11437   (interactive "P")
11438   (when (gnus-summary-catchup all quietly nil 'fast)
11439     ;; Select next newsgroup or exit.
11440     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11441              (eq gnus-auto-select-next 'quietly))
11442         (gnus-summary-next-group nil)
11443       (gnus-summary-exit))))
11444
11445 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11446   "Mark all articles in this newsgroup as read, and then exit.
11447 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11448 instead, which marks only unread articles as read."
11449   (interactive "P")
11450   (gnus-summary-catchup-and-exit t quietly))
11451
11452 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11453   "Mark all articles in this group as read and select the next group.
11454 If given a prefix, mark all articles, unread as well as ticked, as
11455 read."
11456   (interactive "P")
11457   (save-excursion
11458     (gnus-summary-catchup all))
11459   (gnus-summary-next-group))
11460
11461 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11462   "Mark all articles in this group as read and select the previous group.
11463 If given a prefix, mark all articles, unread as well as ticked, as
11464 read."
11465   (interactive "P")
11466   (save-excursion
11467     (gnus-summary-catchup all))
11468   (gnus-summary-next-group nil nil t))
11469
11470 ;;;
11471 ;;; with article
11472 ;;;
11473
11474 (defmacro gnus-with-article (article &rest forms)
11475   "Select ARTICLE and perform FORMS in the original article buffer.
11476 Then replace the article with the result."
11477   `(progn
11478      ;; We don't want the article to be marked as read.
11479      (let (gnus-mark-article-hook)
11480        (gnus-summary-select-article t t nil ,article))
11481      (set-buffer gnus-original-article-buffer)
11482      ,@forms
11483      (if (not (gnus-check-backend-function
11484                'request-replace-article (car gnus-article-current)))
11485          (gnus-message 5 "Read-only group; not replacing")
11486        (unless (gnus-request-replace-article
11487                 ,article (car gnus-article-current)
11488                 (current-buffer) t)
11489          (error "Couldn't replace article")))
11490      ;; The cache and backlog have to be flushed somewhat.
11491      (when gnus-keep-backlog
11492        (gnus-backlog-remove-article
11493         (car gnus-article-current) (cdr gnus-article-current)))
11494      (when gnus-use-cache
11495        (gnus-cache-update-article
11496         (car gnus-article-current) (cdr gnus-article-current)))))
11497
11498 (put 'gnus-with-article 'lisp-indent-function 1)
11499 (put 'gnus-with-article 'edebug-form-spec '(form body))
11500
11501 ;; Thread-based commands.
11502
11503 (defun gnus-summary-articles-in-thread (&optional article)
11504   "Return a list of all articles in the current thread.
11505 If ARTICLE is non-nil, return all articles in the thread that starts
11506 with that article."
11507   (let* ((article (or article (gnus-summary-article-number)))
11508          (data (gnus-data-find-list article))
11509          (top-level (gnus-data-level (car data)))
11510          (top-subject
11511           (cond ((null gnus-thread-operation-ignore-subject)
11512                  (gnus-simplify-subject-re
11513                   (mail-header-subject (gnus-data-header (car data)))))
11514                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11515                  (gnus-simplify-subject-fuzzy
11516                   (mail-header-subject (gnus-data-header (car data)))))
11517                 (t nil)))
11518          (end-point (save-excursion
11519                       (goto-char (gnus-data-pos (car data)))
11520                       (if (gnus-summary-go-to-next-thread)
11521                           (point) (point-max))))
11522          articles)
11523     (while (and data
11524                 (< (gnus-data-pos (car data)) end-point))
11525       (when (or (not top-subject)
11526                 (string= top-subject
11527                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11528                              (gnus-simplify-subject-fuzzy
11529                               (mail-header-subject
11530                                (gnus-data-header (car data))))
11531                            (gnus-simplify-subject-re
11532                             (mail-header-subject
11533                              (gnus-data-header (car data)))))))
11534         (push (gnus-data-number (car data)) articles))
11535       (unless (and (setq data (cdr data))
11536                    (> (gnus-data-level (car data)) top-level))
11537         (setq data nil)))
11538     ;; Return the list of articles.
11539     (nreverse articles)))
11540
11541 (defun gnus-summary-rethread-current ()
11542   "Rethread the thread the current article is part of."
11543   (interactive)
11544   (let* ((gnus-show-threads t)
11545          (article (gnus-summary-article-number))
11546          (id (mail-header-id (gnus-summary-article-header)))
11547          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11548     (unless id
11549       (error "No article on the current line"))
11550     (gnus-rebuild-thread id)
11551     (gnus-summary-goto-subject article)))
11552
11553 (defun gnus-summary-reparent-thread ()
11554   "Make the current article child of the marked (or previous) article.
11555
11556 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11557 is non-nil or the Subject: of both articles are the same."
11558   (interactive)
11559   (unless (not (gnus-group-read-only-p))
11560     (error "The current newsgroup does not support article editing"))
11561   (unless (<= (length gnus-newsgroup-processable) 1)
11562     (error "No more than one article may be marked"))
11563   (let ((child (gnus-summary-article-number))
11564         ;; First grab the marked article, otherwise one line up.
11565         (parent (if (not (null gnus-newsgroup-processable))
11566                     (car gnus-newsgroup-processable)
11567                   (save-excursion
11568                     (if (eq (forward-line -1) 0)
11569                         (gnus-summary-article-number)
11570                       (error "Beginning of summary buffer"))))))
11571     (gnus-summary-reparent-children parent (list child))))
11572
11573 (defun gnus-summary-reparent-children (parent children)
11574   "Make PARENT the parent of CHILDREN.
11575 When called interactively, PARENT is the current article and CHILDREN
11576 are the process-marked articles."
11577   (interactive
11578    (list (gnus-summary-article-number)
11579          (gnus-summary-work-articles nil)))
11580   (dolist (child children)
11581     (save-window-excursion
11582       (let ((gnus-article-buffer " *reparent*"))
11583         (unless (not (eq parent child))
11584           (error "An article may not be self-referential"))
11585         (let ((message-id (mail-header-id
11586                            (gnus-summary-article-header parent))))
11587           (unless (and message-id (not (equal message-id "")))
11588             (error "No message-id in desired parent"))
11589           (gnus-with-article child
11590             (save-restriction
11591               (goto-char (point-min))
11592               (message-narrow-to-head)
11593               (if (re-search-forward "^References: " nil t)
11594                   (progn
11595                     (re-search-forward "^[^ \t]" nil t)
11596                     (forward-line -1)
11597                     (end-of-line)
11598                     (insert " " message-id))
11599                 (insert "References: " message-id "\n"))))
11600           (set-buffer gnus-summary-buffer)
11601           (gnus-summary-unmark-all-processable)
11602           (gnus-summary-update-article child)
11603           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11604             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11605           (gnus-summary-rethread-current)
11606           (gnus-message 3 "Article %d is now the child of article %d"
11607                         child parent))))))
11608
11609 (defun gnus-summary-toggle-threads (&optional arg)
11610   "Toggle showing conversation threads.
11611 If ARG is positive number, turn showing conversation threads on."
11612   (interactive "P")
11613   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11614     (setq gnus-show-threads
11615           (if (null arg) (not gnus-show-threads)
11616             (> (prefix-numeric-value arg) 0)))
11617     (gnus-summary-prepare)
11618     (gnus-summary-goto-subject current)
11619     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11620     (gnus-summary-position-point)))
11621
11622 (eval-and-compile
11623   (if (fboundp 'remove-overlays)
11624       (defalias 'gnus-remove-overlays 'remove-overlays)
11625     (defun gnus-remove-overlays (beg end name val)
11626       "Clear BEG and END of overlays whose property NAME has value VAL.
11627 For compatibility with XEmacs."
11628       (dolist (ov (gnus-overlays-in beg end))
11629         (when (eq (gnus-overlay-get ov name) val)
11630           (gnus-delete-overlay ov))))))
11631
11632 (defun gnus-summary-show-all-threads ()
11633   "Show all threads."
11634   (interactive)
11635   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11636   (gnus-summary-position-point))
11637
11638 (defsubst gnus-summary--inv (p)
11639   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11640
11641 (defun gnus-summary-show-thread ()
11642   "Show thread subtrees.
11643 Returns nil if no thread was there to be shown."
11644   (interactive)
11645   (let* ((orig (point))
11646          (end (point-at-eol))
11647          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11648          ;; Leave point at bol
11649          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11650          (eoi (when end
11651                 (if (fboundp 'next-single-char-property-change)
11652                     ;; Note: XEmacs version of n-s-c-p-c may return nil
11653                     (or (next-single-char-property-change end 'invisible)
11654                         (point-max))
11655                   (while (progn
11656                            (end-of-line 2)
11657                            (and (not (eobp))
11658                                 (eq (get-char-property (point) 'invisible)
11659                                     'gnus-sum))))
11660                   (point)))))
11661     (when eoi
11662       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11663       (goto-char orig)
11664       (gnus-summary-position-point)
11665       eoi)))
11666
11667 (defun gnus-summary-maybe-hide-threads ()
11668   "If requested, hide the threads that should be hidden."
11669   (when (and gnus-show-threads
11670              gnus-thread-hide-subtree)
11671     (gnus-summary-hide-all-threads
11672      (if (or (consp gnus-thread-hide-subtree)
11673              (functionp gnus-thread-hide-subtree))
11674          (gnus-make-predicate gnus-thread-hide-subtree)
11675        nil))))
11676
11677 ;;; Hiding predicates.
11678
11679 (defun gnus-article-unread-p (header)
11680   (memq (mail-header-number header) gnus-newsgroup-unreads))
11681
11682 (defun gnus-article-unseen-p (header)
11683   (memq (mail-header-number header) gnus-newsgroup-unseen))
11684
11685 (defun gnus-map-articles (predicate articles)
11686   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11687   (apply 'gnus-or (mapcar predicate
11688                           (mapcar (lambda (number)
11689                                     (gnus-summary-article-header number))
11690                                   articles))))
11691
11692 (defun gnus-summary-hide-all-threads (&optional predicate)
11693   "Hide all thread subtrees.
11694 If PREDICATE is supplied, threads that satisfy this predicate
11695 will not be hidden."
11696   (interactive)
11697   (save-excursion
11698     (goto-char (point-min))
11699     (let ((end nil)
11700           (count 0))
11701       (while (not end)
11702         (incf count)
11703         (when (zerop (mod count 1000))
11704           (message "Hiding all threads... %d" count))
11705         (when (or (not predicate)
11706                   (gnus-map-articles
11707                    predicate (gnus-summary-article-children)))
11708             (gnus-summary-hide-thread))
11709         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11710   (gnus-summary-position-point))
11711
11712 (defun gnus-summary-hide-thread ()
11713   "Hide thread subtrees.
11714 If PREDICATE is supplied, threads that satisfy this predicate
11715 will not be hidden.
11716 Returns nil if no threads were there to be hidden."
11717   (interactive)
11718   (beginning-of-line)
11719   (let ((start (point))
11720         (starteol (line-end-position))
11721         (article (gnus-summary-article-number)))
11722     ;; Go forward until either the buffer ends or the subthread ends.
11723     (when (and (not (eobp))
11724                (or (zerop (gnus-summary-next-thread 1 t))
11725                    (goto-char (point-max))))
11726       (if (and (> (point) start)
11727                ;; FIXME: this should actually search for a non-invisible \n.
11728                (search-backward "\n" start t))
11729           (progn
11730             (when (> (point) starteol)
11731               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11732               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11733                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11734                 (gnus-overlay-put ol 'evaporate t)))
11735             (gnus-summary-goto-subject article)
11736             (when (> start (point))
11737               (message "Hiding the thread moved us backwards, aborting!")
11738               (goto-char (point-max))))
11739         (goto-char start)
11740         nil))))
11741
11742 (defun gnus-summary-go-to-next-thread (&optional previous)
11743   "Go to the same level (or less) next thread.
11744 If PREVIOUS is non-nil, go to previous thread instead.
11745 Return the article number moved to, or nil if moving was impossible."
11746   (let ((level (gnus-summary-thread-level))
11747         (way (if previous -1 1))
11748         (beg (point)))
11749     (forward-line way)
11750     (while (and (not (eobp))
11751                 (< level (gnus-summary-thread-level)))
11752       (forward-line way))
11753     (if (eobp)
11754         (progn
11755           (goto-char beg)
11756           nil)
11757       (setq beg (point))
11758       (prog1
11759           (gnus-summary-article-number)
11760         (goto-char beg)))))
11761
11762 (defun gnus-summary-next-thread (n &optional silent)
11763   "Go to the same level next N'th thread.
11764 If N is negative, search backward instead.
11765 Returns the difference between N and the number of skips actually
11766 done.
11767
11768 If SILENT, don't output messages."
11769   (interactive "p")
11770   (let ((backward (< n 0))
11771         (n (abs n)))
11772     (while (and (> n 0)
11773                 (gnus-summary-go-to-next-thread backward))
11774       (decf n))
11775     (unless silent
11776       (gnus-summary-position-point))
11777     (when (and (not silent) (/= 0 n))
11778       (gnus-message 7 "No more threads"))
11779     n))
11780
11781 (defun gnus-summary-prev-thread (n)
11782   "Go to the same level previous N'th thread.
11783 Returns the difference between N and the number of skips actually
11784 done."
11785   (interactive "p")
11786   (gnus-summary-next-thread (- n)))
11787
11788 (defun gnus-summary-go-down-thread ()
11789   "Go down one level in the current thread."
11790   (let ((children (gnus-summary-article-children)))
11791     (when children
11792       (gnus-summary-goto-subject (car children)))))
11793
11794 (defun gnus-summary-go-up-thread ()
11795   "Go up one level in the current thread."
11796   (let ((parent (gnus-summary-article-parent)))
11797     (when parent
11798       (gnus-summary-goto-subject parent))))
11799
11800 (defun gnus-summary-down-thread (n)
11801   "Go down thread N steps.
11802 If N is negative, go up instead.
11803 Returns the difference between N and how many steps down that were
11804 taken."
11805   (interactive "p")
11806   (let ((up (< n 0))
11807         (n (abs n)))
11808     (while (and (> n 0)
11809                 (if up (gnus-summary-go-up-thread)
11810                   (gnus-summary-go-down-thread)))
11811       (setq n (1- n)))
11812     (gnus-summary-position-point)
11813     (when (/= 0 n)
11814       (gnus-message 7 "Can't go further"))
11815     n))
11816
11817 (defun gnus-summary-up-thread (n)
11818   "Go up thread N steps.
11819 If N is negative, go down instead.
11820 Returns the difference between N and how many steps down that were
11821 taken."
11822   (interactive "p")
11823   (gnus-summary-down-thread (- n)))
11824
11825 (defun gnus-summary-top-thread ()
11826   "Go to the top of the thread."
11827   (interactive)
11828   (while (gnus-summary-go-up-thread))
11829   (gnus-summary-article-number))
11830
11831 (defun gnus-summary-expire-thread ()
11832   "Mark articles under current thread as expired."
11833   (interactive)
11834   (gnus-summary-kill-thread 0))
11835
11836 (defun gnus-summary-kill-thread (&optional unmark)
11837   "Mark articles under current thread as read.
11838 If the prefix argument is positive, remove any kinds of marks.
11839 If the prefix argument is zero, mark thread as expired.
11840 If the prefix argument is negative, tick articles instead."
11841   (interactive "P")
11842   (when unmark
11843     (setq unmark (prefix-numeric-value unmark)))
11844   (let ((articles (gnus-summary-articles-in-thread))
11845         (hide (or (null unmark) (= unmark 0))))
11846     (save-excursion
11847       ;; Expand the thread.
11848       (gnus-summary-show-thread)
11849       ;; Mark all the articles.
11850       (while articles
11851         (gnus-summary-goto-subject (car articles))
11852         (cond ((null unmark)
11853                (gnus-summary-mark-article-as-read gnus-killed-mark))
11854               ((> unmark 0)
11855                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11856               ((= unmark 0)
11857                (gnus-summary-mark-article nil gnus-expirable-mark))
11858               (t
11859                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11860         (setq articles (cdr articles))))
11861     ;; Hide killed subtrees when hide is true.
11862     (and hide
11863          gnus-thread-hide-killed
11864          (gnus-summary-hide-thread))
11865     ;; If hide is t, go to next unread subject.
11866     (when hide
11867       ;; Go to next unread subject.
11868       (gnus-summary-next-subject 1 t)))
11869   (gnus-set-mode-line 'summary))
11870
11871 ;; Summary sorting commands
11872
11873 (defun gnus-summary-sort-by-number (&optional reverse)
11874   "Sort the summary buffer by article number.
11875 Argument REVERSE means reverse order."
11876   (interactive "P")
11877   (gnus-summary-sort 'number reverse))
11878
11879 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11880   "Sort the summary buffer by most recent article number.
11881 Argument REVERSE means reverse order."
11882   (interactive "P")
11883   (gnus-summary-sort 'most-recent-number reverse))
11884
11885 (defun gnus-summary-sort-by-random (&optional reverse)
11886   "Randomize the order in the summary buffer.
11887 Argument REVERSE means to randomize in reverse order."
11888   (interactive "P")
11889   (gnus-summary-sort 'random reverse))
11890
11891 (defun gnus-summary-sort-by-author (&optional reverse)
11892   "Sort the summary buffer by author name alphabetically.
11893 If `case-fold-search' is non-nil, case of letters is ignored.
11894 Argument REVERSE means reverse order."
11895   (interactive "P")
11896   (gnus-summary-sort 'author reverse))
11897
11898 (defun gnus-summary-sort-by-recipient (&optional reverse)
11899   "Sort the summary buffer by recipient name alphabetically.
11900 If `case-fold-search' is non-nil, case of letters is ignored.
11901 Argument REVERSE means reverse order."
11902   (interactive "P")
11903   (gnus-summary-sort 'recipient reverse))
11904
11905 (defun gnus-summary-sort-by-subject (&optional reverse)
11906   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11907 If `case-fold-search' is non-nil, case of letters is ignored.
11908 Argument REVERSE means reverse order."
11909   (interactive "P")
11910   (gnus-summary-sort 'subject reverse))
11911
11912 (defun gnus-summary-sort-by-date (&optional reverse)
11913   "Sort the summary buffer by date.
11914 Argument REVERSE means reverse order."
11915   (interactive "P")
11916   (gnus-summary-sort 'date reverse))
11917
11918 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11919   "Sort the summary buffer by most recent date.
11920 Argument REVERSE means reverse order."
11921   (interactive "P")
11922   (gnus-summary-sort 'most-recent-date reverse))
11923
11924 (defun gnus-summary-sort-by-score (&optional reverse)
11925   "Sort the summary buffer by score.
11926 Argument REVERSE means reverse order."
11927   (interactive "P")
11928   (gnus-summary-sort 'score reverse))
11929
11930 (defun gnus-summary-sort-by-lines (&optional reverse)
11931   "Sort the summary buffer by the number of lines.
11932 Argument REVERSE means reverse order."
11933   (interactive "P")
11934   (gnus-summary-sort 'lines reverse))
11935
11936 (defun gnus-summary-sort-by-chars (&optional reverse)
11937   "Sort the summary buffer by article length.
11938 Argument REVERSE means reverse order."
11939   (interactive "P")
11940   (gnus-summary-sort 'chars reverse))
11941
11942 (defun gnus-summary-sort-by-original (&optional reverse)
11943   "Sort the summary buffer using the default sorting method.
11944 Argument REVERSE means reverse order."
11945   (interactive "P")
11946   (let* ((inhibit-read-only t)
11947          (gnus-summary-prepare-hook nil))
11948     ;; We do the sorting by regenerating the threads.
11949     (gnus-summary-prepare)
11950     ;; Hide subthreads if needed.
11951     (gnus-summary-maybe-hide-threads)))
11952
11953 (defun gnus-summary-sort (predicate reverse)
11954   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11955   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11956          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11957          (gnus-thread-sort-functions
11958           (if (not reverse)
11959               thread
11960             `(lambda (t1 t2)
11961                (,thread t2 t1))))
11962          (gnus-sort-gathered-threads-function
11963           gnus-thread-sort-functions)
11964          (gnus-article-sort-functions
11965           (if (not reverse)
11966               article
11967             `(lambda (t1 t2)
11968                (,article t2 t1))))
11969          (inhibit-read-only t)
11970          (gnus-summary-prepare-hook nil))
11971     ;; We do the sorting by regenerating the threads.
11972     (gnus-summary-prepare)
11973     ;; Hide subthreads if needed.
11974     (gnus-summary-maybe-hide-threads)))
11975
11976 ;; Summary saving commands.
11977
11978 (defun gnus-summary-save-article (&optional n not-saved)
11979   "Save the current article using the default saver function.
11980 If N is a positive number, save the N next articles.
11981 If N is a negative number, save the N previous articles.
11982 If N is nil and any articles have been marked with the process mark,
11983 save those articles instead.
11984 The variable `gnus-default-article-saver' specifies the saver function.
11985
11986 If the optional second argument NOT-SAVED is non-nil, articles saved
11987 will not be marked as saved."
11988   (interactive "P")
11989   (require 'gnus-art)
11990   (let* ((articles (gnus-summary-work-articles n))
11991          (save-buffer (save-excursion
11992                         (nnheader-set-temp-buffer " *Gnus Save*")))
11993          (num (length articles))
11994          ;; Whether to save decoded articles or raw articles.
11995          (decode (when gnus-article-save-coding-system
11996                    (get gnus-default-article-saver :decode)))
11997          ;; When saving many articles in a single file, use the other
11998          ;; function to save articles other than the first one.
11999          (saver2 (get gnus-default-article-saver :function))
12000          (gnus-prompt-before-saving (if saver2
12001                                         t
12002                                       gnus-prompt-before-saving))
12003          (gnus-default-article-saver gnus-default-article-saver)
12004          header file)
12005     (dolist (article articles)
12006       (setq header (gnus-summary-article-header article))
12007       (if (not (vectorp header))
12008           ;; This is a pseudo-article.
12009           (if (assq 'name header)
12010               (gnus-copy-file (cdr (assq 'name header)))
12011             (gnus-message 1 "Article %d is unsavable" article))
12012         ;; This is a real article.
12013         (save-window-excursion
12014           (gnus-summary-select-article decode decode nil article)
12015           (gnus-summary-goto-subject article))
12016         (with-current-buffer save-buffer
12017           (erase-buffer)
12018           (insert-buffer-substring (if decode
12019                                        gnus-article-buffer
12020                                      gnus-original-article-buffer)))
12021         (setq file (gnus-article-save save-buffer file num))
12022         (gnus-summary-remove-process-mark article)
12023         (unless not-saved
12024           (gnus-summary-set-saved-mark article)))
12025       (when saver2
12026         (setq gnus-default-article-saver saver2
12027               saver2 nil)))
12028     (gnus-kill-buffer save-buffer)
12029     (gnus-summary-position-point)
12030     (gnus-set-mode-line 'summary)
12031     n))
12032
12033 (declare-function gnus-summary-save-in-pipe "gnus-art" (&optional command raw))
12034
12035 (defun gnus-summary-pipe-output (&optional n sym)
12036   "Pipe the current article to a subprocess.
12037 If N is a positive number, pipe the N next articles.
12038 If N is a negative number, pipe the N previous articles.
12039 If N is nil and any articles have been marked with the process mark,
12040 pipe those articles instead.
12041 The default command to which articles are piped is specified by the
12042 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
12043 will be prompted for the command.
12044
12045 The properties `:decode' and `:headers' that are put to the function
12046 symbol `gnus-summary-save-in-pipe' control whether this function
12047 decodes articles and what headers to keep (see the doc string for the
12048 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
12049 is neither omitted nor the symbol `r', force including all headers
12050 regardless of the `:headers' property.  If it is the symbol `r',
12051 articles that are not decoded and include all headers will be piped
12052 no matter what the properties `:decode' and `:headers' are."
12053   (interactive (gnus-interactive "P\ny"))
12054   (require 'gnus-art)
12055   (let* ((articles (gnus-summary-work-articles n))
12056          (result-buffer "*Shell Command Output*")
12057          (all-headers (not (memq sym '(nil r))))
12058          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
12059          (raw (eq sym 'r))
12060          (headers (get 'gnus-summary-save-in-pipe :headers))
12061          command result)
12062     (unless (numberp (car articles))
12063       (error "No article to pipe"))
12064     (setq command (gnus-read-shell-command
12065                    (concat "Shell command on "
12066                            (if (cdr articles)
12067                                (format "these %d articles" (length articles))
12068                              "this article")
12069                            ": ")
12070                    gnus-summary-pipe-output-default-command))
12071     (when (string-equal command "")
12072       (error "A command is required"))
12073     (when all-headers
12074       (put 'gnus-summary-save-in-pipe :headers nil))
12075     (unwind-protect
12076         (while articles
12077           (gnus-summary-goto-subject (pop articles))
12078           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12079           (when (and (get-buffer result-buffer)
12080                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12081             (setq result (concat result (with-current-buffer result-buffer
12082                                           (buffer-string))))))
12083       (put 'gnus-summary-save-in-pipe :headers headers))
12084     (unless (zerop (length result))
12085       (if (with-current-buffer (get-buffer-create result-buffer)
12086             (erase-buffer)
12087             (insert result)
12088             (prog1
12089                 (and (= (count-lines (point-min) (point)) 1)
12090                      (progn
12091                        (end-of-line 0)
12092                        (<= (current-column)
12093                            (window-width (minibuffer-window)))))
12094               (goto-char (point-min))))
12095           (message "%s" (substring result 0 -1))
12096         (message nil)
12097         (gnus-configure-windows 'pipe)))))
12098
12099 (defun gnus-summary-save-article-mail (&optional arg)
12100   "Append the current article to a Unix mail box file.
12101 If N is a positive number, save the N next articles.
12102 If N is a negative number, save the N previous articles.
12103 If N is nil and any articles have been marked with the process mark,
12104 save those articles instead."
12105   (interactive "P")
12106   (require 'gnus-art)
12107   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12108     (gnus-summary-save-article arg)))
12109
12110 (defun gnus-summary-save-article-rmail (&optional arg)
12111   "Append the current article to an rmail file.
12112 If N is a positive number, save the N next articles.
12113 If N is a negative number, save the N previous articles.
12114 If N is nil and any articles have been marked with the process mark,
12115 save those articles instead."
12116   (interactive "P")
12117   (require 'gnus-art)
12118   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12119     (gnus-summary-save-article arg)))
12120
12121 (defun gnus-summary-save-article-file (&optional arg)
12122   "Append the current article to a file.
12123 If N is a positive number, save the N next articles.
12124 If N is a negative number, save the N previous articles.
12125 If N is nil and any articles have been marked with the process mark,
12126 save those articles instead."
12127   (interactive "P")
12128   (require 'gnus-art)
12129   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12130     (gnus-summary-save-article arg)))
12131
12132 (defun gnus-summary-write-article-file (&optional arg)
12133   "Write the current article to a file, deleting the previous file.
12134 If N is a positive number, save the N next articles.
12135 If N is a negative number, save the N previous articles.
12136 If N is nil and any articles have been marked with the process mark,
12137 save those articles instead."
12138   (interactive "P")
12139   (require 'gnus-art)
12140   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12141     (gnus-summary-save-article arg)))
12142
12143 (defun gnus-summary-save-article-body-file (&optional arg)
12144   "Append the current article body to a file.
12145 If N is a positive number, save the N next articles.
12146 If N is a negative number, save the N previous articles.
12147 If N is nil and any articles have been marked with the process mark,
12148 save those articles instead."
12149   (interactive "P")
12150   (require 'gnus-art)
12151   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12152     (gnus-summary-save-article arg)))
12153
12154 (defun gnus-summary-write-article-body-file (&optional arg)
12155   "Write the current article body to a file, deleting the previous file.
12156 If N is a positive number, save the N next articles.
12157 If N is a negative number, save the N previous articles.
12158 If N is nil and any articles have been marked with the process mark,
12159 save those articles instead."
12160   (interactive "P")
12161   (require 'gnus-art)
12162   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12163     (gnus-summary-save-article arg)))
12164
12165 (defun gnus-summary-muttprint (&optional arg)
12166   "Print the current article using Muttprint.
12167 If N is a positive number, save the N next articles.
12168 If N is a negative number, save the N previous articles.
12169 If N is nil and any articles have been marked with the process mark,
12170 save those articles instead."
12171   (interactive "P")
12172   (require 'gnus-art)
12173   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12174     (gnus-summary-save-article arg t)))
12175
12176 (defun gnus-summary-pipe-message (program)
12177   "Pipe the current article through PROGRAM."
12178   (interactive "sProgram: ")
12179   (gnus-summary-select-article)
12180   (let ((mail-header-separator ""))
12181     (gnus-eval-in-buffer-window gnus-article-buffer
12182       (save-restriction
12183         (widen)
12184         (let ((start (window-start))
12185               (inhibit-read-only t))
12186           (message-pipe-buffer-body program)
12187           (set-window-start (get-buffer-window (current-buffer)) start))))))
12188
12189 (defun gnus-get-split-value (methods)
12190   "Return a value based on the split METHODS."
12191   (let (split-name method result match)
12192     (when methods
12193       (with-current-buffer gnus-original-article-buffer
12194         (save-restriction
12195           (nnheader-narrow-to-headers)
12196           (while (and methods (not split-name))
12197             (goto-char (point-min))
12198             (setq method (pop methods))
12199             (setq match (car method))
12200             (when (cond
12201                    ((stringp match)
12202                     ;; Regular expression.
12203                     (ignore-errors
12204                       (re-search-forward match nil t)))
12205                    ((functionp match)
12206                     ;; Function.
12207                     (save-restriction
12208                       (widen)
12209                       (setq result (funcall match gnus-newsgroup-name))))
12210                    ((consp match)
12211                     ;; Form.
12212                     (save-restriction
12213                       (widen)
12214                       (setq result (eval match)))))
12215               (setq split-name (cdr method))
12216               (cond ((stringp result)
12217                      (push (expand-file-name
12218                             result gnus-article-save-directory)
12219                            split-name))
12220                     ((consp result)
12221                      (setq split-name (append result split-name)))))))))
12222     (nreverse split-name)))
12223
12224 (defun gnus-valid-move-group-p (group)
12225   (and (symbolp group)
12226        (boundp group)
12227        (symbol-name group)
12228        (symbol-value group)
12229        (gnus-get-function (gnus-find-method-for-group
12230                            (symbol-name group)) 'request-accept-article t)))
12231
12232 (defun gnus-read-move-group-name (prompt default articles prefix)
12233   "Read a group name."
12234   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12235          (minibuffer-confirm-incomplete nil) ; XEmacs
12236          (prom
12237           (format "%s %s to"
12238                   prompt
12239                   (if (> (length articles) 1)
12240                       (format "these %d articles" (length articles))
12241                     "this article")))
12242          (to-newsgroup
12243           (cond
12244            ((null split-name)
12245             (gnus-group-completing-read
12246              prom
12247              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12248              nil prefix nil default))
12249            ((= 1 (length split-name))
12250             (gnus-group-completing-read
12251              prom
12252              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12253              nil prefix 'gnus-group-history (car split-name)))
12254            (t
12255             (gnus-completing-read
12256              prom (nreverse split-name) nil nil 'gnus-group-history))))
12257          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12258          encoded)
12259     (when to-newsgroup
12260       (if (or (string= to-newsgroup "")
12261               (string= to-newsgroup prefix))
12262           (setq to-newsgroup default))
12263       (unless to-newsgroup
12264         (error "No group name entered"))
12265       (setq encoded (mm-encode-coding-string
12266                      to-newsgroup
12267                      (gnus-group-name-charset to-method to-newsgroup)))
12268       (or (gnus-active encoded)
12269           (gnus-activate-group encoded nil nil to-method)
12270           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12271                                      to-newsgroup))
12272               (or (and (gnus-request-create-group encoded to-method)
12273                        (gnus-activate-group encoded nil nil to-method)
12274                        (gnus-subscribe-group encoded))
12275                   (error "Couldn't create group %s" to-newsgroup)))
12276           (error "No such group: %s" to-newsgroup))
12277       encoded)))
12278
12279 (defvar gnus-summary-save-parts-counter)
12280 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12281
12282 (defun gnus-summary-save-parts (type dir n &optional reverse)
12283   "Save parts matching TYPE to DIR.
12284 If REVERSE, save parts that do not match TYPE."
12285   (interactive
12286    (list (read-string "Save parts of type: "
12287                       (or (car gnus-summary-save-parts-type-history)
12288                           gnus-summary-save-parts-default-mime)
12289                       'gnus-summary-save-parts-type-history)
12290          (setq gnus-summary-save-parts-last-directory
12291                (read-directory-name "Save to directory: "
12292                                     gnus-summary-save-parts-last-directory
12293                                     nil t))
12294          current-prefix-arg))
12295   (gnus-summary-iterate n
12296     (let ((gnus-display-mime-function nil)
12297           gnus-article-prepare-hook
12298           gnus-article-decode-hook
12299           gnus-display-mime-function
12300           gnus-break-pages
12301           (gnus-inhibit-treatment t))
12302       (gnus-summary-select-article))
12303     (with-current-buffer gnus-article-buffer
12304       (let ((handles (or gnus-article-mime-handles
12305                          (mm-dissect-buffer nil gnus-article-loose-mime)
12306                          (and gnus-article-emulate-mime
12307                               (mm-uu-dissect))))
12308             (gnus-summary-save-parts-counter 1))
12309         (when handles
12310           (gnus-summary-save-parts-1 type dir handles reverse)
12311           (unless gnus-article-mime-handles ;; Don't destroy this case.
12312             (mm-destroy-parts handles)))))))
12313
12314 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12315   (if (stringp (car handle))
12316       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12317               (cdr handle))
12318     (when (if reverse
12319               (not (string-match type (mm-handle-media-type handle)))
12320             (string-match type (mm-handle-media-type handle)))
12321       (let ((file (expand-file-name
12322                    (gnus-map-function
12323                     mm-file-name-rewrite-functions
12324                     (file-name-nondirectory
12325                      (or
12326                       (mm-handle-filename handle)
12327                       (format "%s.%d.%d" gnus-newsgroup-name
12328                               (cdr gnus-article-current)
12329                               gnus-summary-save-parts-counter))))
12330                    dir)))
12331         (incf gnus-summary-save-parts-counter)
12332         (unless (file-exists-p file)
12333           (mm-save-part-to-file handle file))))))
12334
12335 ;; Summary extract commands
12336
12337 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12338   (let ((inhibit-read-only t)
12339         (article (gnus-summary-article-number))
12340         after-article b e)
12341     (unless (gnus-summary-goto-subject article)
12342       (error "No such article: %d" article))
12343     (gnus-summary-position-point)
12344     ;; If all commands are to be bunched up on one line, we collect
12345     ;; them here.
12346     (unless gnus-view-pseudos-separately
12347       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12348             files action)
12349         (while ps
12350           (setq action (cdr (assq 'action (car ps))))
12351           (setq files (list (cdr (assq 'name (car ps)))))
12352           (while (and ps (cdr ps)
12353                       (string= (or action "1")
12354                                (or (cdr (assq 'action (cadr ps))) "2")))
12355             (push (cdr (assq 'name (cadr ps))) files)
12356             (setcdr ps (cddr ps)))
12357           (when files
12358             (when (not (string-match "%s" action))
12359               (push " " files))
12360             (push " " files)
12361             (when (assq 'execute (car ps))
12362               (setcdr (assq 'execute (car ps))
12363                       (funcall (if (string-match "%s" action)
12364                                    'format 'concat)
12365                                action
12366                                (mapconcat
12367                                 (lambda (f)
12368                                   (if (equal f " ")
12369                                       f
12370                                     (shell-quote-argument f)))
12371                                 files " ")))))
12372           (setq ps (cdr ps)))))
12373     (if (and gnus-view-pseudos (not not-view))
12374         (while pslist
12375           (when (assq 'execute (car pslist))
12376             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12377                                   (eq gnus-view-pseudos 'not-confirm)))
12378           (setq pslist (cdr pslist)))
12379       (save-excursion
12380         (while pslist
12381           (setq after-article (or (cdr (assq 'article (car pslist)))
12382                                   (gnus-summary-article-number)))
12383           (gnus-summary-goto-subject after-article)
12384           (forward-line 1)
12385           (setq b (point))
12386           (insert "    " (file-name-nondirectory
12387                           (cdr (assq 'name (car pslist))))
12388                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12389           (setq e (point))
12390           (forward-line -1)             ; back to `b'
12391           (gnus-add-text-properties
12392            b (1- e) (list 'gnus-number gnus-reffed-article-number
12393                           gnus-mouse-face-prop gnus-mouse-face))
12394           (gnus-data-enter
12395            after-article gnus-reffed-article-number
12396            gnus-unread-mark b (car pslist) 0 (- e b))
12397           (setq gnus-newsgroup-unreads
12398                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12399                                          gnus-reffed-article-number))
12400           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12401           (setq pslist (cdr pslist)))))))
12402
12403 (defun gnus-pseudos< (p1 p2)
12404   (let ((c1 (cdr (assq 'action p1)))
12405         (c2 (cdr (assq 'action p2))))
12406     (and c1 c2 (string< c1 c2))))
12407
12408 (defun gnus-request-pseudo-article (props)
12409   (cond ((assq 'execute props)
12410          (gnus-execute-command (cdr (assq 'execute props)))))
12411   (let ((gnus-current-article (gnus-summary-article-number)))
12412     (gnus-run-hooks 'gnus-mark-article-hook)))
12413
12414 (defun gnus-execute-command (command &optional automatic)
12415   (save-excursion
12416     (gnus-article-setup-buffer)
12417     (set-buffer gnus-article-buffer)
12418     (setq buffer-read-only nil)
12419     (let ((command (if automatic command
12420                      (read-string "Command: " (cons command 0)))))
12421       (erase-buffer)
12422       (insert "$ " command "\n\n")
12423       (if gnus-view-pseudo-asynchronously
12424           (start-process "gnus-execute" (current-buffer) shell-file-name
12425                          shell-command-switch command)
12426         (call-process shell-file-name nil t nil
12427                       shell-command-switch command)))))
12428
12429 ;; Summary kill commands.
12430
12431 (defun gnus-summary-edit-global-kill (article)
12432   "Edit the \"global\" kill file."
12433   (interactive (list (gnus-summary-article-number)))
12434   (gnus-group-edit-global-kill article))
12435
12436 (defun gnus-summary-edit-local-kill ()
12437   "Edit a local kill file applied to the current newsgroup."
12438   (interactive)
12439   (setq gnus-current-headers (gnus-summary-article-header))
12440   (gnus-group-edit-local-kill
12441    (gnus-summary-article-number) gnus-newsgroup-name))
12442
12443 ;;; Header reading.
12444
12445 (defun gnus-read-header (id &optional header)
12446   "Read the headers of article ID and enter them into the Gnus system."
12447   (let ((group gnus-newsgroup-name)
12448         (gnus-override-method
12449          (or
12450           gnus-override-method
12451           (and (gnus-news-group-p gnus-newsgroup-name)
12452                (car (gnus-refer-article-methods)))))
12453         where)
12454     ;; First we check to see whether the header in question is already
12455     ;; fetched.
12456     (if (stringp id)
12457         ;; This is a Message-ID.
12458         (setq header (or header (gnus-id-to-header id)))
12459       ;; This is an article number.
12460       (setq header (or header (gnus-summary-article-header id))))
12461     (if (and header
12462              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12463         ;; We have found the header.
12464         header
12465       ;; We have to really fetch the header to this article.
12466       (with-current-buffer nntp-server-buffer
12467         (when (setq where (gnus-request-head id group))
12468           (nnheader-fold-continuation-lines)
12469           (goto-char (point-max))
12470           (insert ".\n")
12471           (goto-char (point-min))
12472           (insert "211 ")
12473           (princ (cond
12474                   ((numberp id) id)
12475                   ((cdr where) (cdr where))
12476                   (header (mail-header-number header))
12477                   (t gnus-reffed-article-number))
12478                  (current-buffer))
12479           (insert " Article retrieved.\n"))
12480         (if (or (not where)
12481                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12482             ()                          ; Malformed head.
12483           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12484             (when (and (bound-and-true-p gnus-registry-enabled)
12485                        (not (gnus-ephemeral-group-p (car where))))
12486               (gnus-registry-handle-action
12487                (mail-header-id header) nil
12488                (gnus-group-prefixed-name
12489                 (car where)
12490                 (or gnus-override-method (gnus-find-method-for-group group)))
12491                (mail-header-subject header)
12492                (mail-header-from header)))
12493             (when (and (stringp id)
12494                        (or
12495                         (not (string= (gnus-group-real-name group)
12496                                       (car where)))
12497                         (not (gnus-server-equal gnus-override-method
12498                                                 (gnus-group-method group)))))
12499               ;; If we fetched by Message-ID and the article came from
12500               ;; a different group (or server), we fudge some bogus
12501               ;; article numbers for this article.
12502               (mail-header-set-number header gnus-reffed-article-number))
12503             (with-current-buffer gnus-summary-buffer
12504               (decf gnus-reffed-article-number)
12505               (gnus-remove-header (mail-header-number header))
12506               (push header gnus-newsgroup-headers)
12507               (setq gnus-current-headers header)
12508               (push (mail-header-number header) gnus-newsgroup-limit)))
12509           header)))))
12510
12511 (defun gnus-remove-header (number)
12512   "Remove header NUMBER from `gnus-newsgroup-headers'."
12513   (if (and gnus-newsgroup-headers
12514            (= number (mail-header-number (car gnus-newsgroup-headers))))
12515       (pop gnus-newsgroup-headers)
12516     (let ((headers gnus-newsgroup-headers))
12517       (while (and (cdr headers)
12518                   (not (= number (mail-header-number (cadr headers)))))
12519         (pop headers))
12520       (when (cdr headers)
12521         (setcdr headers (cddr headers))))))
12522
12523 ;;;
12524 ;;; summary highlights
12525 ;;;
12526
12527 (defun gnus-highlight-selected-summary ()
12528   "Highlight selected article in summary buffer."
12529   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12530   (when gnus-summary-selected-face
12531     (save-excursion
12532       (let* ((beg (point-at-bol))
12533              (end (point-at-eol))
12534              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12535              (from (if (get-text-property beg gnus-mouse-face-prop)
12536                        beg
12537                      (or (next-single-property-change
12538                           beg gnus-mouse-face-prop nil end)
12539                          beg)))
12540              (to
12541               (if (= from end)
12542                   (- from 2)
12543                 (or (next-single-property-change
12544                      from gnus-mouse-face-prop nil end)
12545                     end))))
12546         ;; If no mouse-face prop on line we will have to = from = end,
12547         ;; so we highlight the entire line instead.
12548         (when (= (+ to 2) from)
12549           (setq from beg)
12550           (setq to end))
12551         (if gnus-newsgroup-selected-overlay
12552             ;; Move old overlay.
12553             (gnus-move-overlay
12554              gnus-newsgroup-selected-overlay from to (current-buffer))
12555           ;; Create new overlay.
12556           (gnus-overlay-put
12557            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12558            'face gnus-summary-selected-face))))))
12559
12560 (defvar gnus-summary-highlight-line-cached nil)
12561 (defvar gnus-summary-highlight-line-trigger nil)
12562
12563 (defun gnus-summary-highlight-line-0 ()
12564   (if (and (eq gnus-summary-highlight-line-trigger
12565                gnus-summary-highlight)
12566            gnus-summary-highlight-line-cached)
12567       gnus-summary-highlight-line-cached
12568     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12569           gnus-summary-highlight-line-cached
12570           (let* ((cond (list 'cond))
12571                  (c cond)
12572                  (list gnus-summary-highlight))
12573             (while list
12574               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12575                               nil))
12576               (setq c (cdr c)
12577                     list (cdr list)))
12578             (gnus-byte-compile (list 'lambda nil cond))))))
12579
12580 (defun gnus-summary-highlight-line ()
12581   "Highlight current line according to `gnus-summary-highlight'."
12582   (let* ((beg (point-at-bol))
12583          (article (or (gnus-summary-article-number) gnus-current-article))
12584          (score (or (cdr (assq article
12585                                gnus-newsgroup-scored))
12586                     gnus-summary-default-score 0))
12587          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12588          (inhibit-read-only t)
12589          (default gnus-summary-default-score)
12590          (default-high gnus-summary-default-high-score)
12591          (default-low gnus-summary-default-low-score)
12592          (uncached (and gnus-summary-use-undownloaded-faces
12593                         (memq article gnus-newsgroup-undownloaded)
12594                         (not (memq article gnus-newsgroup-cached)))))
12595     (let ((face (funcall (gnus-summary-highlight-line-0))))
12596       (unless (eq face (gnus-get-text-property-excluding-characters-with-faces beg 'face))
12597         (gnus-put-text-property-excluding-characters-with-faces
12598          beg (point-at-eol) 'face
12599          (setq face (if (boundp face) (symbol-value face) face)))
12600         (when gnus-summary-highlight-line-function
12601           (funcall gnus-summary-highlight-line-function article face))))))
12602
12603 (defun gnus-update-read-articles (group unread &optional compute)
12604   "Update the list of read articles in GROUP.
12605 UNREAD is a sorted list."
12606   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12607         (info (gnus-get-info group))
12608         (prev 1)
12609         read)
12610     (if (or (not info) (not active))
12611         ;; There is no info on this group if it was, in fact,
12612         ;; killed.  Gnus stores no information on killed groups, so
12613         ;; there's nothing to be done.
12614         ;; One could store the information somewhere temporarily,
12615         ;; perhaps...  Hmmm...
12616         ()
12617       ;; Remove any negative articles numbers.
12618       (while (and unread (< (car unread) 0))
12619         (setq unread (cdr unread)))
12620       ;; Remove any expired article numbers
12621       (while (and unread (< (car unread) (car active)))
12622         (setq unread (cdr unread)))
12623       ;; Compute the ranges of read articles by looking at the list of
12624       ;; unread articles.
12625       (while unread
12626         (when (/= (car unread) prev)
12627           (push (if (= prev (1- (car unread))) prev
12628                   (cons prev (1- (car unread))))
12629                 read))
12630         (setq prev (1+ (car unread)))
12631         (setq unread (cdr unread)))
12632       (when (<= prev (cdr active))
12633         (push (cons prev (cdr active)) read))
12634       (setq read (if (> (length read) 1) (nreverse read) read))
12635       (if compute
12636           read
12637         (save-excursion
12638           (let (setmarkundo)
12639             ;; Propagate the read marks to the backend.
12640             (when (and (gnus-method-option-p
12641                         (gnus-find-method-for-group group)
12642                         'server-marks)
12643                        (gnus-check-backend-function 'request-set-mark group))
12644               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12645                     (add (gnus-remove-from-range read (gnus-info-read info))))
12646                 (when (or add del)
12647                   (unless (gnus-check-group group)
12648                     (error "Can't open server for %s" group))
12649                   (gnus-request-set-mark
12650                    group (delq nil (list (if add (list add 'add '(read)))
12651                                          (if del (list del 'del '(read))))))
12652                   (setq setmarkundo
12653                         `(gnus-request-set-mark
12654                           ,group
12655                           ',(delq nil (list
12656                                        (if del (list del 'add '(read)))
12657                                        (if add (list add 'del '(read))))))))))
12658             (set-buffer gnus-group-buffer)
12659             (gnus-undo-register
12660               `(progn
12661                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12662                  (gnus-info-set-read ',info ',(gnus-info-read info))
12663                  (gnus-get-unread-articles-in-group ',info
12664                                                     (gnus-active ,group))
12665                  (gnus-group-update-group ,group t)
12666                  ,setmarkundo))))
12667         ;; Enter this list into the group info.
12668         (gnus-info-set-read info read)
12669         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12670         (gnus-get-unread-articles-in-group info (gnus-active group))
12671         t))))
12672
12673 (defun gnus-offer-save-summaries ()
12674   "Offer to save all active summary buffers."
12675   (let (buffers)
12676     ;; Go through all buffers and find all summaries.
12677     (dolist (buffer (buffer-list))
12678       (when (and (setq buffer (buffer-name buffer))
12679                  (string-match "Summary" buffer)
12680                  (with-current-buffer buffer
12681                    ;; We check that this is, indeed, a summary buffer.
12682                    (and (eq major-mode 'gnus-summary-mode)
12683                         ;; Also make sure this isn't bogus.
12684                         gnus-newsgroup-prepared
12685                         ;; Also make sure that this isn't a
12686                         ;; dead summary buffer.
12687                         (not gnus-dead-summary-mode))))
12688         (push buffer buffers)))
12689     ;; Go through all these summary buffers and offer to save them.
12690     (when buffers
12691       (save-excursion
12692         (if (eq gnus-interactive-exit 'quiet)
12693             (dolist (buffer buffers)
12694               (switch-to-buffer buffer)
12695               (gnus-summary-exit))
12696           (map-y-or-n-p
12697            "Update summary buffer %s? "
12698            (lambda (buf)
12699              (switch-to-buffer buf)
12700              (gnus-summary-exit))
12701            buffers))))))
12702
12703 (defun gnus-summary-setup-default-charset ()
12704   "Setup newsgroup default charset."
12705   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12706       (setq gnus-newsgroup-charset nil)
12707     (let* ((ignored-charsets
12708             (or gnus-newsgroup-ephemeral-ignored-charsets
12709                 (append
12710                  (and gnus-newsgroup-name
12711                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12712                  gnus-newsgroup-ignored-charsets))))
12713       (setq gnus-newsgroup-charset
12714             (or gnus-newsgroup-ephemeral-charset
12715                 (and gnus-newsgroup-name
12716                      (gnus-parameter-charset gnus-newsgroup-name))
12717                 gnus-default-charset))
12718       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12719            ignored-charsets))))
12720
12721 ;;;
12722 ;;; Mime Commands
12723 ;;;
12724
12725 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12726   "Display the current article buffer fully MIME-buttonized.
12727 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12728 treated as multipart/mixed."
12729   (interactive "P")
12730   (require 'gnus-art)
12731   (let ((gnus-unbuttonized-mime-types nil)
12732         (gnus-mime-display-multipart-as-mixed show-all-parts))
12733     (gnus-summary-show-article)))
12734
12735 (defun gnus-summary-repair-multipart (article)
12736   "Add a Content-Type header to a multipart article without one."
12737   (interactive (list (gnus-summary-article-number)))
12738   (gnus-with-article article
12739     (message-narrow-to-head)
12740     (message-remove-header "Mime-Version")
12741     (goto-char (point-max))
12742     (insert "Mime-Version: 1.0\n")
12743     (widen)
12744     (when (search-forward "\n--" nil t)
12745       (let ((separator (buffer-substring (point) (point-at-eol))))
12746         (message-narrow-to-head)
12747         (message-remove-header "Content-Type")
12748         (goto-char (point-max))
12749         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12750                         separator))
12751         (widen))))
12752   (let (gnus-mark-article-hook)
12753     (gnus-summary-select-article t t nil article)))
12754
12755 (defun gnus-summary-toggle-display-buttonized ()
12756   "Toggle the buttonizing of the article buffer."
12757   (interactive)
12758   (require 'gnus-art)
12759   (if (setq gnus-inhibit-mime-unbuttonizing
12760             (not gnus-inhibit-mime-unbuttonizing))
12761       (let ((gnus-unbuttonized-mime-types nil))
12762         (gnus-summary-show-article))
12763     (gnus-summary-show-article)))
12764
12765 ;;;
12766 ;;; Generic summary marking commands
12767 ;;;
12768
12769 (defvar gnus-summary-marking-alist
12770   '((read gnus-del-mark "d")
12771     (unread gnus-unread-mark "u")
12772     (ticked gnus-ticked-mark "!")
12773     (dormant gnus-dormant-mark "?")
12774     (expirable gnus-expirable-mark "e"))
12775   "An alist of names/marks/keystrokes.")
12776
12777 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12778 (defvar gnus-summary-mark-map)
12779
12780 (defun gnus-summary-make-all-marking-commands ()
12781   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12782   (dolist (elem gnus-summary-marking-alist)
12783     (apply 'gnus-summary-make-marking-command elem)))
12784
12785 (defun gnus-summary-make-marking-command (name mark keystroke)
12786   (let ((map (make-sparse-keymap)))
12787     (define-key gnus-summary-generic-mark-map keystroke map)
12788     (dolist (lway `((next "next" next nil "n")
12789                     (next-unread "next unread" next t "N")
12790                     (prev "previous" prev nil "p")
12791                     (prev-unread "previous unread" prev t "P")
12792                     (nomove "" nil nil ,keystroke)))
12793       (let ((func (gnus-summary-make-marking-command-1
12794                    mark (car lway) lway name)))
12795         (setq func (eval func))
12796         (define-key map (nth 4 lway) func)))))
12797
12798 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12799   `(defun ,(intern
12800             (format "gnus-summary-put-mark-as-%s%s"
12801                     name (if (eq way 'nomove)
12802                              ""
12803                            (concat "-" (symbol-name way)))))
12804      (n)
12805      ,(format
12806        "Mark the current article as %s%s.
12807 If N, the prefix, then repeat N times.
12808 If N is negative, move in reverse order.
12809 The difference between N and the actual number of articles marked is
12810 returned."
12811        name (cadr lway))
12812      (interactive "p")
12813      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12814
12815 (defun gnus-summary-generic-mark (n mark move unread)
12816   "Mark N articles with MARK."
12817   (unless (eq major-mode 'gnus-summary-mode)
12818     (error "This command can only be used in the summary buffer"))
12819   (gnus-summary-show-thread)
12820   (let ((nummove
12821          (cond
12822           ((eq move 'next) 1)
12823           ((eq move 'prev) -1)
12824           (t 0))))
12825     (if (zerop nummove)
12826         (setq n 1)
12827       (when (< n 0)
12828         (setq n (abs n)
12829               nummove (* -1 nummove))))
12830     (while (and (> n 0)
12831                 (gnus-summary-mark-article nil mark)
12832                 (zerop (gnus-summary-next-subject nummove unread t)))
12833       (setq n (1- n)))
12834     (when (/= 0 n)
12835       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12836     (gnus-summary-recenter)
12837     (gnus-summary-position-point)
12838     (gnus-set-mode-line 'summary)
12839     n))
12840
12841 (defun gnus-summary-insert-articles (articles)
12842   (when (setq articles
12843               (gnus-sorted-difference articles
12844                                       (mapcar (lambda (h)
12845                                                 (mail-header-number h))
12846                                               gnus-newsgroup-headers)))
12847     (setq gnus-newsgroup-headers
12848           (gnus-merge 'list
12849                       gnus-newsgroup-headers
12850                       (gnus-fetch-headers articles nil t)
12851                       'gnus-article-sort-by-number))
12852     (setq gnus-newsgroup-articles
12853           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12854     ;; Suppress duplicates?
12855     (when gnus-suppress-duplicates
12856       (gnus-dup-suppress-articles))
12857
12858     (if (and gnus-fetch-old-headers
12859              (eq gnus-headers-retrieved-by 'nov))
12860         ;; We might want to build some more threads first.
12861         (if (eq gnus-fetch-old-headers 'invisible)
12862             (gnus-build-all-threads)
12863           (gnus-build-old-threads))
12864       ;; Mark the inserted articles that are unread as unread.
12865       (setq gnus-newsgroup-unreads
12866             (gnus-sorted-nunion
12867              gnus-newsgroup-unreads
12868              (gnus-sorted-nintersection
12869               (gnus-list-of-unread-articles gnus-newsgroup-name)
12870               articles)))
12871       ;; Mark the inserted articles as selected so that the information
12872       ;; of the marks having been changed by a user may be updated when
12873       ;; exiting this group.  See `gnus-summary-update-info'.
12874       (dolist (art articles)
12875         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12876     ;; Let the Gnus agent mark articles as read.
12877     (when gnus-agent
12878       (gnus-agent-get-undownloaded-list))
12879     ;; Remove list identifiers from subject
12880     (gnus-summary-remove-list-identifiers)
12881     ;; First and last article in this newsgroup.
12882     (when gnus-newsgroup-headers
12883       (setq gnus-newsgroup-begin
12884             (mail-header-number (car gnus-newsgroup-headers))
12885             gnus-newsgroup-end
12886             (mail-header-number
12887              (gnus-last-element gnus-newsgroup-headers))))
12888     (when gnus-use-scoring
12889       (gnus-possibly-score-headers))))
12890
12891 (defun gnus-summary-insert-old-articles (&optional all)
12892   "Insert all old articles in this group.
12893 If ALL is non-nil, already read articles become readable.
12894 If ALL is a number, fetch this number of articles."
12895   (interactive "P")
12896   (prog1
12897       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12898             older len)
12899         (setq older
12900               ;; Some nntp servers lie about their active range.  When
12901               ;; this happens, the active range can be in the millions.
12902               ;; Use a compressed range to avoid creating a huge list.
12903               (gnus-range-difference
12904                (gnus-range-difference (list gnus-newsgroup-active) old)
12905                gnus-newsgroup-unexist))
12906         (setq len (gnus-range-length older))
12907         (cond
12908          ((null older) nil)
12909          ((numberp all)
12910           (if (< all len)
12911               (let ((older-range (nreverse older)))
12912                 (setq older nil)
12913
12914                 (while (> all 0)
12915                   (let* ((r (pop older-range))
12916                          (min (if (numberp r) r (car r)))
12917                          (max (if (numberp r) r (cdr r))))
12918                     (while (and (<= min max)
12919                                 (> all 0))
12920                       (push max older)
12921                       (setq all (1- all)
12922                             max (1- max))))))
12923             (setq older (gnus-uncompress-range older))))
12924          (all
12925           (setq older (gnus-uncompress-range older)))
12926          (t
12927           (when (and (numberp gnus-large-newsgroup)
12928                    (> len gnus-large-newsgroup))
12929               (let* ((cursor-in-echo-area nil)
12930                      (initial (gnus-parameter-large-newsgroup-initial
12931                                gnus-newsgroup-name))
12932                      (input
12933                       (read-string
12934                        (format
12935                         "How many articles from %s (%s %d): "
12936                         (gnus-group-decoded-name gnus-newsgroup-name)
12937                         (if initial "max" "default")
12938                         len)
12939                        nil nil
12940                        (and initial
12941                             (number-to-string initial)))))
12942                 (unless (string-match "^[ \t]*$" input)
12943                   (setq all (string-to-number input))
12944                   (if (< all len)
12945                       (let ((older-range (nreverse older)))
12946                         (setq older nil)
12947
12948                         (while (> all 0)
12949                           (let* ((r (pop older-range))
12950                                  (min (if (numberp r) r (car r)))
12951                                  (max (if (numberp r) r (cdr r))))
12952                             (while (and (<= min max)
12953                                         (> all 0))
12954                               (push max older)
12955                               (setq all (1- all)
12956                                     max (1- max))))))))))
12957           (setq older (gnus-uncompress-range older))))
12958         (if (not older)
12959             (message "No old news.")
12960           (gnus-summary-insert-articles older)
12961           (gnus-summary-limit (gnus-sorted-nunion old older))))
12962     (gnus-summary-position-point)))
12963
12964 (defun gnus-summary-insert-new-articles ()
12965   "Insert all new articles in this group."
12966   (interactive)
12967   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12968         (old-high gnus-newsgroup-highest)
12969         (nnmail-fetched-sources (list t))
12970         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12971         i new)
12972     (unless new-active
12973       (error "Couldn't fetch new data"))
12974     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12975     (setq i (cdr gnus-newsgroup-active)
12976           gnus-newsgroup-highest i)
12977     (while (> i old-high)
12978       (push i new)
12979       (decf i))
12980     (if (not new)
12981         (message "No gnus is bad news")
12982       (gnus-summary-insert-articles new)
12983       (setq gnus-newsgroup-unreads
12984             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12985       (gnus-summary-limit (gnus-sorted-nunion old new))))
12986   (gnus-summary-position-point))
12987
12988 ;;; Bookmark support for Gnus.
12989 (declare-function gnus-article-show-summary "gnus-art" ())
12990 (declare-function bookmark-make-record-default
12991                   "bookmark" (&optional no-file no-context posn))
12992 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12993 (declare-function bookmark-default-handler "bookmark" (bmk))
12994 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12995 (defvar bookmark-yank-point)
12996 (defvar bookmark-current-buffer)
12997
12998 (defun gnus-summary-bookmark-make-record ()
12999   "Make a bookmark entry for a Gnus summary buffer."
13000   (let (pos buf)
13001     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
13002       (save-restriction              ; FIXME is it necessary to widen?
13003         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
13004       (setq buf "art") ; We are recording bookmark from article buffer.
13005       (setq bookmark-yank-point (point))
13006       (setq bookmark-current-buffer (current-buffer))
13007       (gnus-article-show-summary))      ; Go back in summary buffer.
13008     ;; We are now recording bookmark from summary buffer.
13009     (unless buf (setq buf "sum"))
13010     (let* ((subject (elt (gnus-summary-article-header) 1))
13011            (grp     (car gnus-article-current))
13012            (art     (cdr gnus-article-current))
13013            (head    (gnus-summary-article-header art))
13014            (id      (mail-header-id head)))
13015       `(,subject
13016         ,@(condition-case nil
13017               (bookmark-make-record-default 'no-file 'no-context pos)
13018             (wrong-number-of-arguments
13019              (bookmark-make-record-default 'point-only)))
13020         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
13021         (group . ,grp) (article . ,art)
13022         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
13023
13024 ;;;###autoload
13025 (defun gnus-summary-bookmark-jump (bookmark)
13026   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
13027 BOOKMARK is a bookmark name or a bookmark record."
13028   (let ((group    (bookmark-prop-get bookmark 'group))
13029         (article  (bookmark-prop-get bookmark 'article))
13030         (id       (bookmark-prop-get bookmark 'message-id))
13031         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
13032     (gnus-fetch-group group (list article))
13033     (gnus-summary-insert-cached-articles)
13034     (gnus-summary-goto-article id nil 'force)
13035     ;; FIXME we have to wait article buffer is ready (only large buffer)
13036     ;; Is there a better solution to know that?
13037     ;; If we don't wait `bookmark-default-handler' will have no chance
13038     ;; to set position. However there is no error, just wrong pos.
13039     (sit-for 1)
13040     (when (string= buf "Gnus-art")
13041       (other-window 1))
13042     (bookmark-default-handler
13043      `(""
13044        (buffer . ,(current-buffer))
13045        . ,(bookmark-get-bookmark-record bookmark)))))
13046
13047 (gnus-summary-make-all-marking-commands)
13048
13049 (gnus-ems-redefine)
13050
13051 (provide 'gnus-sum)
13052
13053 (run-hooks 'gnus-sum-load-hook)
13054
13055 ;; Local Variables:
13056 ;; coding: utf-8
13057 ;; End:
13058
13059 ;;; gnus-sum.el ends here